From 63bbc710b23893742e5ccbd430f95bf2d29c2da6 Mon Sep 17 00:00:00 2001 From: Jakub Sławiński Date: Thu, 3 Nov 2005 20:37:56 +0100 Subject: v0.7.4 - Fixed: sockets in CLOSE_WAIT state left by afclient - Added: --localname and --localport options - Added: --localdesname option - Added: kicking user in 'opening' state - Fixed: info about kicked user - Fixed: TERM signal handling - Fixed: id lost after reconnection - Fixed: printing wrong client name in 'SSL_accept failed (timeout)' message - Fixed: ignored 'certificate' and 'key' options from config file - Added: config files for afclient - Modified: some options in afserver config file --- src/afclient.c | 917 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 609 insertions(+), 308 deletions(-) (limited to 'src/afclient.c') diff --git a/src/afclient.c b/src/afclient.c index c41ee68..b450543 100644 --- a/src/afclient.c +++ b/src/afclient.c @@ -33,6 +33,7 @@ static struct option long_options[] = { {"verbose", 0, 0, 'v'}, {"keyfile", 1, 0, 'k'}, {"storefile", 1, 0, 's'}, + {"cfgfile", 1, 0, 'f'}, {"log", 1, 0, 'o'}, {"pass", 1, 0, 301}, {"ignorepkeys", 0, 0, 302}, @@ -61,53 +62,62 @@ static struct option long_options[] = { {"ar-start", 0, 0, 305}, {"ar-quit", 0, 0, 306}, {"noar", 0, 0, 307}, + {"localname", 1, 0, 311}, + {"localport", 1, 0, 312}, + {"localdesname", 1, 0, 313}, {0, 0, 0, 0} }; +ClientConfiguration* cconfig; + int main(int argc, char **argv) { + /* * variables */ int i, n, numofcon, length, buflength, notsent, temp, temp2; - ConnectUser** contable = NULL; - SslFd* master = SslFd_new(); + ConnectUser** usersTable = NULL; unsigned char buff[9000]; char hostname[100]; - int maxfdp1, usernum, usercon; - socklen_t len, addrlen; - struct sockaddr* cliaddr; + int maxfdp1, usercon; + socklen_t aLength, addressLength; + struct sockaddr* clientAddress; fd_set rset, allset, wset, tmpset; - struct timeval keepalive; + struct timeval keepAlive; int timeout = 0; char verbose = 0; - char remote = 0; - char sendkapackets = 0; - char* name = NULL; -#ifdef HAVE_LIBPTHREAD HttpProxyOptions* hpo = HttpProxyOptions_new(); -#endif + char hpoUsed = 0; ArOptions* ao = ArOptions_new(); - char* id = NULL; - char* manage = NULL; - char* desnam = NULL; - char* despor = NULL; + ClientRealm* pointer; + char aoUsed = 0; + char passwordWasSet = 0; + char* realmId = NULL; + char* serverName = NULL; + char* managePort = NULL; + char* hostName = NULL; + char* destinationPort = NULL; + char* localName = NULL; + char* localPort = NULL; + char* localDestinationName = NULL; char* keys = NULL; char* store = NULL; char* dateformat = NULL; - char* katimeout = NULL; - char ipfam = 0; - unsigned char pass[4] = {1, 2, 3, 4}; - char udp = 0; - char reverse = 0; - char tunneltype = 0; - char type = 0; - char ignorepkeys = 0; + char* kaTimeout = NULL; + char* filenam = NULL; + char ipFamily = 0; + unsigned char password[4] = {1, 2, 3, 4}; + char udpMode = 0; + char reverseMode = 0; + char remoteMode = 0; + char realmType = 0; + char ignorePublicKeys = 0; struct sigaction act; #ifdef HAVE_LIBDL - moduleT module = {0, NULL, NULL, NULL, NULL}, secmodule = {0, NULL, NULL, NULL, NULL}; + Module *moduleA = Module_new(), *moduleB = Module_new(); #endif SSL_METHOD* method; SSL_CTX* ctx = NULL; @@ -128,11 +138,12 @@ main(int argc, char **argv) exit(1); } - if (master == NULL) { +#ifdef HAVE_LIBDL + if ((moduleA == NULL) || (moduleB == NULL)) { printf("Problems with memory allocation... exiting\n"); exit(1); } - +#endif sigfillset(&(act.sa_mask)); act.sa_flags = 0; @@ -140,6 +151,7 @@ main(int argc, char **argv) sigaction(SIGPIPE, &act, NULL); act.sa_handler = client_sig_int; sigaction(SIGINT, &act, NULL); + sigaction(SIGTERM, &act, NULL); #ifdef HAVE_LIBPTHREAD remember_mainthread(); @@ -163,7 +175,7 @@ main(int argc, char **argv) while ((n = getopt_long(argc, argv, GETOPT_LONG_LIBDL(GETOPT_LONG_LIBPTHREAD( - GETOPT_LONG_AF_INET6("huUn:m:d:p:vk:s:o:i:D:rP:X:VK:A:T:"))) + GETOPT_LONG_AF_INET6("huUn:m:d:p:vk:s:o:i:D:rP:X:VK:A:T:f:"))) , long_options, 0)) != -1) { switch (n) { case 'h': { @@ -171,45 +183,50 @@ main(int argc, char **argv) break; } case 'n': { - name = optarg; + serverName = optarg; break; } #ifdef HAVE_LIBPTHREAD case 'S': { HttpProxyOptions_use_https(hpo); + hpoUsed = 1; break; } case 'P': { HttpProxyOptions_set_proxyname(hpo, optarg); + hpoUsed = 1; break; } case 'X': { HttpProxyOptions_set_proxyport(hpo, optarg); + hpoUsed = 1; break; } case 'B': { HttpProxyOptions_set_proxyauth_type(hpo, PROXYAUTH_TYPE_BASIC); + hpoUsed = 1; break; } case 'C': { HttpProxyOptions_set_proxyauth_cred(hpo, optarg); + hpoUsed = 1; break; } #endif case 'i': { - id = optarg; + realmId = optarg; break; } case 'm': { - manage = optarg; + managePort = optarg; break; } case 'd': { - desnam = optarg; + hostName = optarg; break; } case 'p': { - despor = optarg; + destinationPort = optarg; break; } case 'v': { @@ -217,11 +234,11 @@ main(int argc, char **argv) break; } case 'u': { - udp = 1; + udpMode = 1; break; } case 'U': { - reverse = 1; + reverseMode = 1; break; } case 'k': { @@ -232,61 +249,81 @@ main(int argc, char **argv) store = optarg; break; } + case 'f': { + filenam = optarg; + break; + } case 'o': { addlogtarget(optarg); break; } case 301: { n = strlen(optarg); - memset(pass, 0, 4); + memset(password, 0, 4); for (i = 0; i < n; ++i) { - pass[i%4] += optarg[i]; + password[i%4] += optarg[i]; } + passwordWasSet = 1; break; } case 302: { - ignorepkeys = 1; + ignorePublicKeys = 1; break; } case 305: { ArOptions_set_arStart(ao, AR_OPTION_ENABLED); + aoUsed = 1; break; } case 306: { ArOptions_set_arQuit(ao, AR_OPTION_ENABLED); + aoUsed = 1; break; } case 307: { ArOptions_set_arPremature(ao, AR_OPTION_DISABLED); + aoUsed = 1; + break; + } + case 311: { + localName = optarg; + break; + } + case 312: { + localPort = optarg; + break; + } + case 313: { + localDestinationName = optarg; break; } #ifdef AF_INET6 case '4': { - if (ipfam != 0) { - ipfam = -1; + if (ipFamily != 0) { + ipFamily = -1; } else { - ipfam = 4; + ipFamily = 4; } break; } case '6': { - if (ipfam != 0) { - ipfam = -1; + if (ipFamily != 0) { + ipFamily = -1; } else { - ipfam = 6; + ipFamily = 6; } break; } #endif #ifdef HAVE_LIBDL case 'l': { - module.name = optarg; + Module_set_fileName(moduleA, optarg); break; } case 'L': { - secmodule.name = optarg; + Module_set_fileName(moduleB, optarg); break; } #endif @@ -295,7 +332,7 @@ main(int argc, char **argv) break; } case 'r': { - remote = 1; + remoteMode = 1; break; } case 'V': { @@ -304,16 +341,17 @@ main(int argc, char **argv) break; } case 'K': { - katimeout = optarg; - sendkapackets = 1; + kaTimeout = optarg; break; } case 'A': { ArOptions_set_s_arTries(ao, optarg); + aoUsed = 1; break; } case 'T': { ArOptions_set_s_arDelay(ao, optarg); + aoUsed = 1; break; } case '?': { @@ -327,92 +365,297 @@ main(int argc, char **argv) client_short_usage("Unrecognized non-option elements"); } - if (name == NULL) { + if (filenam != NULL) { + cconfig = cparsefile(filenam, &n); + if (n) { + printf("parsing failed! line:%d\n", n); + exit(1); + } + else { + if (keys == NULL) { + if (ClientConfiguration_get_keysFile(cconfig) == NULL) { + ClientConfiguration_set_keysFile(cconfig, "client.rsa"); + } + } + else { + ClientConfiguration_set_keysFile(cconfig, keys); + } + if (store == NULL) { + if (ClientConfiguration_get_storeFile(cconfig) == NULL) { + ClientConfiguration_set_storeFile(cconfig, "known_hosts"); + } + } + else { + ClientConfiguration_set_storeFile(cconfig, store); + } + if (dateformat != NULL) { + ClientConfiguration_set_dateFormat(cconfig, dateformat); + } + if (ignorePublicKeys) { + ClientConfiguration_set_ignorePublicKeys(cconfig, ignorePublicKeys); + } + + initializelogging(verbose, ClientConfiguration_get_dateFormat(cconfig)); + + aflog(LOG_T_INIT, LOG_I_INFO, + "client's cfg file OK! (readed realms: %d)", ClientConfiguration_get_realmsNumber(cconfig)); + if ((ClientConfiguration_get_realmsNumber(cconfig) == 0) || + (ClientConfiguration_get_realmsTable(cconfig) == NULL) || + ((pointer = ClientConfiguration_get_realmsTable(cconfig)[0]) == NULL)) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Working without sense is really without sense..."); + exit(1); + } + if (hpoUsed) { + ClientRealm_set_httpProxyOptions(pointer, hpo); + } + else { + HttpProxyOptions_free(&hpo); + } + if (aoUsed) { + ClientRealm_set_arOptions(pointer, ao); + } + else { + ArOptions_free(&ao); + } + if ((serverName != NULL) && (ClientRealm_get_serverName(pointer) == NULL)) { + ClientRealm_set_serverName(pointer, serverName); + } + if ((managePort != NULL) && (ClientRealm_get_managePort(pointer) == NULL)) { + ClientRealm_set_managePort(pointer, managePort); + } + if ((hostName != NULL) && (ClientRealm_get_hostName(pointer) == NULL)) { + ClientRealm_set_hostName(pointer, hostName); + } + if ((destinationPort != NULL) && (ClientRealm_get_destinationPort(pointer) == NULL)) { + ClientRealm_set_destinationPort(pointer, destinationPort); + } + if ((realmId != NULL) && (ClientRealm_get_realmId(pointer) == NULL)) { + ClientRealm_set_realmId(pointer, realmId); + } + if ((localName != NULL) && (ClientRealm_get_localName(pointer) == NULL)) { + ClientRealm_set_localName(pointer, localName); + } + if ((localPort != NULL) && (ClientRealm_get_localPort(pointer) == NULL)) { + ClientRealm_set_localPort(pointer, localPort); + } + if ((localDestinationName != NULL) & (ClientRealm_get_localDestinationName(pointer) == NULL)) { + ClientRealm_set_localDestinationName(pointer, localDestinationName); + } + if ((kaTimeout != NULL) && (ClientRealm_get_sKeepAliveTimeout(pointer) == NULL)) { + ClientRealm_set_sKeepAliveTimeout(pointer, kaTimeout); + } + if (reverseMode) { + aflog(LOG_T_INIT, LOG_I_WARNING, + "Warning: reverseudp will be ignored"); + } + if (udpMode) { + aflog(LOG_T_INIT, LOG_I_WARNING, + "Warning: udpmode will be ignored"); + } + if (remoteMode) { + aflog(LOG_T_INIT, LOG_I_WARNING, + "Warning: remoteadmin will be ignored"); + } + if (passwordWasSet) { + aflog(LOG_T_INIT, LOG_I_WARNING, + "Warning: pass will be ignored"); + } +#ifdef HAVE_LIBDL + if (Module_get_fileName(moduleA)) { + aflog(LOG_T_INIT, LOG_I_WARNING, + "Warning: load will be ignored"); + } + if (Module_get_fileName(moduleB)) { + aflog(LOG_T_INIT, LOG_I_WARNING, + "Warning: Load will be ignored"); + } +#endif + } + } + else { + cconfig = ClientConfiguration_new(); + if (cconfig == NULL) { + printf("Can't allocate memory for client configuration... exiting\n"); + exit(1); + } + ClientConfiguration_set_keysFile(cconfig, keys); + ClientConfiguration_set_storeFile(cconfig, store); + ClientConfiguration_set_dateFormat(cconfig, dateformat); + ClientConfiguration_set_realmsNumber(cconfig, 1); + ClientConfiguration_set_ignorePublicKeys(cconfig, ignorePublicKeys); + + initializelogging(verbose, ClientConfiguration_get_dateFormat(cconfig)); + + if (ClientConfiguration_get_keysFile(cconfig) == NULL) { + ClientConfiguration_set_keysFile(cconfig, "client.rsa"); + } + if (ClientConfiguration_get_storeFile(cconfig) == NULL) { + ClientConfiguration_set_storeFile(cconfig, "known_hosts"); + } + ClientConfiguration_set_realmsTable(cconfig, + calloc(ClientConfiguration_get_realmsNumber(cconfig), sizeof(ClientRealm*))); + if (ClientConfiguration_get_realmsTable(cconfig) == NULL) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Can't allocate memory for ClientRealm* table... exiting"); + exit(1); + } + pointer = ClientRealm_new(); + if (pointer == NULL) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Can't allocate memory for ClientRealm structure... exiting"); + exit(1); + } + + ClientConfiguration_get_realmsTable(cconfig)[0] = pointer; + ClientRealm_set_serverName(pointer, serverName); + ClientRealm_set_managePort(pointer, managePort); + ClientRealm_set_hostName(pointer, hostName); + ClientRealm_set_destinationPort(pointer, destinationPort); + ClientRealm_set_realmId(pointer, realmId); + ClientRealm_set_httpProxyOptions(pointer, hpo); + ClientRealm_set_arOptions(pointer, ao); + ClientRealm_set_password(pointer, password); + ClientRealm_set_localName(pointer, localName); + ClientRealm_set_localPort(pointer, localPort); + ClientRealm_set_localDestinationName(pointer, localDestinationName); + ClientRealm_set_realmId(pointer, realmId); + ClientRealm_set_sKeepAliveTimeout(pointer, kaTimeout); +#ifdef HAVE_LIBDL + ClientRealm_set_userModule(pointer, moduleA); + ClientRealm_set_serviceModule(pointer, moduleB); +#endif + + if (reverseMode) { + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_TCP) { + ClientRealm_set_clientMode(pointer, CLIENTREALM_MODE_REVERSE); + } + else { + ClientRealm_set_clientMode(pointer, CLIENTREALM_MODE_UNKNOWN); + } + } + if (udpMode) { + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_TCP) { + ClientRealm_set_clientMode(pointer, CLIENTREALM_MODE_UDP); + } + else { + ClientRealm_set_clientMode(pointer, CLIENTREALM_MODE_UNKNOWN); + } + } + if (remoteMode) { + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_TCP) { + ClientRealm_set_clientMode(pointer, CLIENTREALM_MODE_REMOTE); + } + else { + ClientRealm_set_clientMode(pointer, CLIENTREALM_MODE_UNKNOWN); + } + } + } + + /* + * WARNING: we have only one ClientRealm at the moment + */ + + if (ClientRealm_get_serverName(pointer) == NULL) { client_short_usage("Name of the server is required"); } - if (manage == NULL) { - manage = "50126"; - if (reverse) + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_UNKNOWN) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Conflicting/unknown client modes... exiting"); + exit(1); + } + if (ClientRealm_get_managePort(pointer) == NULL) { + ClientRealm_set_managePort(pointer, "50126"); + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_REVERSE) client_short_usage("Port on the server is required in reverse mode"); } #ifdef HAVE_LIBPTHREAD - if ((HttpProxyOptions_get_proxyname(hpo)) || (HttpProxyOptions_get_proxyport(hpo))) { - if (tunneltype == 0) { - tunneltype = 1; + if ((HttpProxyOptions_get_proxyname(ClientRealm_get_httpProxyOptions(pointer))) || + (HttpProxyOptions_get_proxyport(ClientRealm_get_httpProxyOptions(pointer)))) { + if (ClientRealm_get_tunnelType(pointer) == CLIENTREALM_TUNNELTYPE_DIRECT) { + ClientRealm_set_tunnelType(pointer, CLIENTREALM_TUNNELTYPE_HTTPPROXY); } else { - tunneltype = -1; + ClientRealm_set_tunnelType(pointer, CLIENTREALM_TUNNELTYPE_UNKNOWN); } } - if (tunneltype == 1) { - if (HttpProxyOptions_get_proxyport(hpo) == NULL) { - HttpProxyOptions_set_proxyport(hpo, "8080"); + if (ClientRealm_get_tunnelType(pointer) == CLIENTREALM_TUNNELTYPE_HTTPPROXY) { + if (HttpProxyOptions_get_proxyport(ClientRealm_get_httpProxyOptions(pointer)) == NULL) { + HttpProxyOptions_set_proxyport(ClientRealm_get_httpProxyOptions(pointer), "8080"); } } #endif - if (keys == NULL) { - keys = "client.rsa"; - } - if (store == NULL) { - store = "known_hosts"; - } - if ((reverse == 0) && (remote == 0) && (desnam == NULL)) { + if ((ClientRealm_get_clientMode(pointer) != CLIENTREALM_MODE_REVERSE) && + (ClientRealm_get_clientMode(pointer) != CLIENTREALM_MODE_REMOTE) && + (ClientRealm_get_hostName(pointer) == NULL)) { gethostname(hostname, 100); - desnam = hostname; + ClientRealm_set_hostName(pointer, hostname); } - if ((!remote) && (despor == NULL)) { + if ((ClientRealm_get_clientMode(pointer) != CLIENTREALM_MODE_REMOTE) && + (ClientRealm_get_destinationPort(pointer) == NULL)) { client_short_usage("Destination port number is required"); } - - initializelogging(verbose, dateformat); - if (sendkapackets) { - check_value(&timeout, katimeout, "Invalid timeout value"); - keepalive.tv_sec = timeout; - keepalive.tv_usec = 0; + if (ClientRealm_get_sKeepAliveTimeout(pointer)) { + ClientRealm_set_keepAliveTimeout(pointer, + check_value(ClientRealm_get_sKeepAliveTimeout(pointer), "Invalid timeout value")); + keepAlive.tv_sec = ClientRealm_get_keepAliveTimeout(pointer); + keepAlive.tv_usec = 0; + ClientRealm_set_keepAlive(pointer, keepAlive); + } + ArOptions_evaluate_values(ClientRealm_get_arOptions(pointer)); + + if (ignorePublicKeys) { + ClientConfiguration_set_ignorePublicKeys(cconfig, ignorePublicKeys); } - ArOptions_evaluate_values(ao); #ifdef HAVE_LIBDL - if (loadmodule(&module)) { + if (Module_loadModule(ClientRealm_get_userModule(pointer))) { aflog(LOG_T_INIT, LOG_I_CRIT, - "Loading a module %s failed!", module.name); + "Loading a module %s failed!", Module_get_fileName(ClientRealm_get_userModule(pointer))); exit(1); } - if (loadmodule(&secmodule)) { + if (Module_loadModule(ClientRealm_get_serviceModule(pointer))) { aflog(LOG_T_INIT, LOG_I_CRIT, - "Loading a module %s failed!", secmodule.name); + "Loading a module %s failed!", Module_get_fileName(ClientRealm_get_serviceModule(pointer))); exit(1); } #endif - - TYPE_SET_SSL(type); - TYPE_SET_ZLIB(type); + + TYPE_SET_ZERO(realmType); + TYPE_SET_SSL(realmType); + TYPE_SET_ZLIB(realmType); #ifdef AF_INET6 - if (ipfam == -1) { + if ((ipFamily != 0) && (ClientRealm_get_ipFamily(pointer) <= 0)) { + ClientRealm_set_ipFamily(pointer, ipFamily); + } + if (ClientRealm_get_ipFamily(pointer) == -1) { aflog(LOG_T_INIT, LOG_I_CRIT, "Conflicting types of ip protocol family... exiting"); exit(1); } - else if (ipfam == 4) { - TYPE_SET_IPV4(type); + else if (ClientRealm_get_ipFamily(pointer) == 4) { + TYPE_SET_IPV4(realmType); } - else if (ipfam == 6) { - TYPE_SET_IPV6(type); + else if (ClientRealm_get_ipFamily(pointer) == 6) { + TYPE_SET_IPV6(realmType); } #endif - ipfam = 0x01; + ipFamily = 0x01; #ifdef AF_INET6 - if (TYPE_IS_IPV4(type)) { - ipfam |= 0x02; + if (TYPE_IS_IPV4(realmType)) { + ipFamily |= 0x02; } - else if (TYPE_IS_IPV6(type)) { - ipfam |= 0x04; + else if (TYPE_IS_IPV6(realmType)) { + ipFamily |= 0x04; } #endif - if (!reverse) { + ClientRealm_set_ipFamily(pointer, ipFamily); + ClientRealm_set_realmType(pointer, realmType); + + if (ClientRealm_get_clientMode(pointer) != CLIENTREALM_MODE_REVERSE) { SSL_library_init(); method = SSLv3_client_method(); ctx = SSL_CTX_new(method); @@ -429,102 +672,117 @@ main(int argc, char **argv) "Warning: Creating ./apf directory failed (%d)", temp2); } } + store = ClientConfiguration_get_storeFile(cconfig); if ((temp2 = create_publickey_store(&store))) { aflog(LOG_T_INIT, LOG_I_WARNING, "Warning: Something bad happened when creating public key store... (%d)", temp2); } + ClientConfiguration_set_storeFile(cconfig, store); + keys = ClientConfiguration_get_keysFile(cconfig); if ((temp2 = generate_rsa_key(&keys))) { aflog(LOG_T_INIT, LOG_I_WARNING, "Warning: Something bad happened when generating rsa keys... (%d)", temp2); } + ClientConfiguration_set_keysFile(cconfig, keys); if (SSL_CTX_use_RSAPrivateKey_file(ctx, keys, SSL_FILETYPE_PEM) != 1) { aflog(LOG_T_INIT, LOG_I_CRIT, "Setting rsa key failed (%s)... exiting", keys); exit(1); } - if ((!remote) && (!verbose)) + if ((ClientRealm_get_clientMode(pointer) != CLIENTREALM_MODE_REMOTE) && + (!verbose)) daemon(0, 0); - if (remote) { + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_REMOTE) { temp2 = -1; - if (despor) { - if (ip_listen(&n, desnam, despor, &addrlen, ipfam)) { + if (ClientRealm_get_destinationPort(pointer)) { + if (ip_listen(&n, ClientRealm_get_serverName(pointer), + ClientRealm_get_destinationPort(pointer), + &addressLength, + ClientRealm_get_ipFamily(pointer))) { #ifdef AF_INET6 aflog(LOG_T_INIT, LOG_I_CRIT, "tcp_listen_%s error for %s, %s", - (ipfam & 0x02)?"ipv4":(ipfam & 0x04)?"ipv6":"unspec", desnam, despor); + (ClientRealm_get_ipFamily(pointer) & 0x02) ? + "ipv4" : + (ClientRealm_get_ipFamily(pointer) & 0x04) ? + "ipv6" : + "unspec", + ClientRealm_get_serverName(pointer), + ClientRealm_get_destinationPort(pointer)); #else aflog(LOG_T_INIT, LOG_I_CRIT, - "tcp_listen error for %s, %s", desnam, despor); + "tcp_listen error for %s, %s", ClientRealm_get_serverName(pointer), + ClientRealm_get_destinationPort(pointer)); #endif exit(1); } - cliaddr = malloc(addrlen); - temp2 = accept(n, cliaddr, &addrlen); + clientAddress = malloc(addressLength); + if (clientAddress == NULL) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Can't allocate memory for sockaddr structure... exiting"); + exit(1); + } + ClientRealm_set_addressLength(pointer, addressLength); + ClientRealm_set_clientAddress(pointer, clientAddress); + temp2 = accept(n, ClientRealm_get_clientAddress(pointer), &addressLength); } } } - i = ArOptions_get_arTries(ao); - usernum = 0; - SslFd_set_fd(master, -1); + i = ArOptions_get_arTries(ClientRealm_get_arOptions(pointer)); + SslFd_set_fd(ClientRealm_get_masterSslFd(pointer), -1); do { temp = 0; - if (SslFd_get_fd(master) != -1) { - close(SslFd_get_fd(master)); + if (SslFd_get_fd(ClientRealm_get_masterSslFd(pointer)) != -1) { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(pointer))); } - close_connections(usernum, &contable); - SslFd_set_ssl(master, NULL); + ClientRealm_closeUsersConnections(pointer); + SslFd_set_ssl(ClientRealm_get_masterSslFd(pointer), NULL); - if (!reverse) { + if (ClientRealm_get_clientMode(pointer) != CLIENTREALM_MODE_REVERSE) { if (temp == 0) { -#ifdef HAVE_LIBPTHREAD - if (initialize_client_stage1(tunneltype, master, name, manage, hpo, - ipfam, ctx, buff, pass, - (ArOptions_get_arStart(ao) == AR_OPTION_ENABLED) ? 0 : 1, - ignorepkeys)) { -#else - if (initialize_client_stage1(tunneltype, master, name, manage, NULL, - ipfam, ctx, buff, pass, - (ArOptions_get_arStart(ao) == AR_OPTION_ENABLED) ? 0 : 1, - ignorepkeys)) { -#endif + if (initialize_client_stage1(pointer, ctx, buff, + (ArOptions_get_arStart(ClientRealm_get_arOptions(pointer)) == AR_OPTION_ENABLED) ? 0 : 1, + ClientConfiguration_get_ignorePublicKeys(cconfig))) { temp = 1; } } - if ((temp == 0) && remote) { - return client_admin(type, master, buff, temp2, id); + if ((temp == 0) && (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_REMOTE)) { + return client_admin(ClientRealm_get_realmType(pointer), + ClientRealm_get_masterSslFd(pointer), buff, temp2, + ClientRealm_get_realmId(pointer)); } if (temp == 0) { - if (initialize_client_stage2(&type, master, &usernum, buff, - (ArOptions_get_arStart(ao) == AR_OPTION_ENABLED) ? 0 : 1)) { + realmType = ClientRealm_get_realmType(pointer); + if (initialize_client_stage2(pointer, buff, + (ArOptions_get_arStart(ClientRealm_get_arOptions(pointer)) == AR_OPTION_ENABLED) ? 0 : 1)) { temp = 1; } } } /* !reverse */ else { - if (initialize_client_reverse_udp(&usernum, master, name, manage, ipfam, - (ArOptions_get_arStart(ao) == AR_OPTION_ENABLED) ? 0 : 1)) { + if (initialize_client_reverse_udp(pointer)) { temp = 1; } } if (temp == 0) { - if (initialize_client_stage3(&contable, master, usernum, &buflength, &len, &allset, &wset, &maxfdp1, - (ArOptions_get_arStart(ao) == AR_OPTION_ENABLED) ? 0 : 1)) { + if (initialize_client_stage3(pointer, &buflength, &allset, &wset, &maxfdp1, + (ArOptions_get_arStart(ClientRealm_get_arOptions(pointer)) == AR_OPTION_ENABLED) ? 0 : 1)) { temp = 1; } } /* UDP REVERSE MODE */ - if ((temp == 0) && reverse) { - client_reverse_udp(contable, master, desnam, despor, type, buff, buflength); + if ((temp == 0) && (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_REVERSE)) { + client_reverse_udp(pointer, buff, buflength); } if (i > 0) { @@ -533,9 +791,10 @@ main(int argc, char **argv) if ((i != 0) && (temp == 1)) { aflog(LOG_T_INIT, LOG_I_INFO, "Trying to reconnect..."); - mysleep(ArOptions_get_arDelay(ao)); + mysleep(ArOptions_get_arDelay(ClientRealm_get_arOptions(pointer))); } if (temp == 0) { + ClientRealm_set_realmType(pointer, realmType); break; } } while (i); @@ -543,33 +802,37 @@ main(int argc, char **argv) /* NORMAL MODE */ aflog(LOG_T_CLIENT, LOG_I_INFO, - "CLIENT STARTED mode: %s", (udp)?"udp":"tcp"); + "CLIENT STARTED mode: %s", (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_UDP) ? "udp" : "tcp"); aflog(LOG_T_CLIENT, LOG_I_INFO, - "SERVER SSL: %s, ZLIB: %s, MODE: %s", (TYPE_IS_SSL(type))?"yes":"no", - (TYPE_IS_ZLIB(type))?"yes":"no", (TYPE_IS_TCP(type))?"tcp":"udp"); + "SERVER SSL: %s, ZLIB: %s, MODE: %s", (TYPE_IS_SSL(ClientRealm_get_realmType(pointer))) ? "yes" : "no", + (TYPE_IS_ZLIB(ClientRealm_get_realmType(pointer))) ? "yes" : "no", + (TYPE_IS_TCP(ClientRealm_get_realmType(pointer))) ? "tcp" : "udp"); aflog(LOG_T_CLIENT, LOG_I_NOTICE, - "CIPHER: %s VER: %s", SSL_get_cipher_name(SslFd_get_ssl(master)), - SSL_get_cipher_version(SslFd_get_ssl(master))); + "CIPHER: %s VER: %s", SSL_get_cipher_name(SslFd_get_ssl(ClientRealm_get_masterSslFd(pointer))), + SSL_get_cipher_version(SslFd_get_ssl(ClientRealm_get_masterSslFd(pointer)))); #ifdef HAVE_LIBDL - if (ismloaded(&module)) { + if (Module_isModuleLoaded(ClientRealm_get_userModule(pointer))) { aflog(LOG_T_CLIENT, LOG_I_INFO, - "LOADED MODULE: %s INFO: %s", module.name, module.info()); + "LOADED MODULE: %s INFO: %s", Module_get_fileName(ClientRealm_get_userModule(pointer)), + Module_function_info(ClientRealm_get_userModule(pointer))); } - if (ismloaded(&secmodule)) { + if (Module_isModuleLoaded(ClientRealm_get_serviceModule(pointer))) { aflog(LOG_T_CLIENT, LOG_I_INFO, - "LOADED MODULE (ser): %s INFO: %s", secmodule.name, secmodule.info()); + "LOADED MODULE (ser): %s INFO: %s", Module_get_fileName(ClientRealm_get_serviceModule(pointer)), + Module_function_info(ClientRealm_get_serviceModule(pointer))); } #endif - if (id != NULL) { + if (ClientRealm_get_realmId(pointer) != NULL) { buff[0] = AF_S_LOGIN; buff[1] = buff[2] = 0; - n = strlen(id); - memcpy(&buff[5], id, n); + n = strlen(ClientRealm_get_realmId(pointer)); + memcpy(&buff[5], ClientRealm_get_realmId(pointer), n); buff[3] = n >> 8; /* high bits of message length */ buff[4] = n; /* low bits of message length */ - SslFd_send_message(type, master, buff, n+5); + SslFd_send_message(ClientRealm_get_realmType(pointer), + ClientRealm_get_masterSslFd(pointer), buff, n+5); aflog(LOG_T_CLIENT, LOG_I_INFO, - "ID SENT: %s", id); + "ID SENT: %s", ClientRealm_get_realmId(pointer)); } for ( ; ; ) { @@ -577,14 +840,16 @@ main(int argc, char **argv) tmpset = wset; aflog(LOG_T_MAIN, LOG_I_DDEBUG, "select"); - if (sendkapackets) { - if (select(maxfdp1, &rset, &tmpset, NULL, &keepalive) == 0) { + if (ClientRealm_get_sKeepAliveTimeout(pointer)) { + if (select(maxfdp1, &rset, &tmpset, NULL, ClientRealm_get_keepAlivePointer(pointer)) == 0) { aflog(LOG_T_CLIENT, LOG_I_DEBUG, "timeout: sending keep-alive packet"); buff[0] = AF_S_KEEP_ALIVE; - SslFd_send_message(type, master, buff, 5); - keepalive.tv_sec = timeout; - keepalive.tv_usec = 0; + SslFd_send_message(ClientRealm_get_realmType(pointer), + ClientRealm_get_masterSslFd(pointer), buff, 5); + keepAlive.tv_sec = timeout; + keepAlive.tv_usec = 0; + ClientRealm_set_keepAlive(pointer, keepAlive); } } else { @@ -593,13 +858,14 @@ main(int argc, char **argv) aflog(LOG_T_MAIN, LOG_I_DDEBUG, "after select..."); - for (i = 0; i < usernum; ++i) { - if ((ConnectUser_get_state(contable[i]) == S_STATE_OPEN) || - (ConnectUser_get_state(contable[i]) == S_STATE_STOPPED)) { - if (FD_ISSET(ConnectUser_get_connFd(contable[i]), &rset)) { /* FD_ISSET CONTABLE[i].CONNFD RSET */ + usersTable = ClientRealm_get_usersTable(pointer); + for (i = 0; i < ClientRealm_get_usersLimit(pointer); ++i) { + if ((ConnectUser_get_state(usersTable[i]) == S_STATE_OPEN) || + (ConnectUser_get_state(usersTable[i]) == S_STATE_STOPPED)) { + if (FD_ISSET(ConnectUser_get_connFd(usersTable[i]), &rset)) { /* FD_ISSET CONTABLE[i].CONNFD RSET */ aflog(LOG_T_USER, LOG_I_DDEBUG, "user[%d]: FD_ISSET", i); - n = read(ConnectUser_get_connFd(contable[i]), &buff[5], 8091); + n = read(ConnectUser_get_connFd(usersTable[i]), &buff[5], 8091); if (n == -1) { aflog(LOG_T_USER, LOG_I_ERR, "error (%d): while reading from service", n); @@ -607,14 +873,15 @@ main(int argc, char **argv) } #ifdef HAVE_LINUX_SOCKIOS_H # ifdef SIOCOUTQ - if (ioctl(SslFd_get_fd(master), SIOCOUTQ, ¬sent)) { + if (ioctl(SslFd_get_fd(ClientRealm_get_masterSslFd(pointer)), SIOCOUTQ, ¬sent)) { aflog(LOG_T_USER, LOG_I_CRIT, "ioctl error -> exiting..."); exit(1); } - if (udp) { - len = 4; - if (getsockopt(SslFd_get_fd(master), SOL_SOCKET, SO_SNDBUF, &temp2, &len) != -1) { + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_UDP) { + aLength = 4; + if (getsockopt(SslFd_get_fd(ClientRealm_get_masterSslFd(pointer)), + SOL_SOCKET, SO_SNDBUF, &temp2, &aLength) != -1) { if (temp2 != buflength) { buflength = temp2; aflog(LOG_T_USER, LOG_I_WARNING, @@ -630,7 +897,7 @@ main(int argc, char **argv) "ioctl error -> exiting..."); exit(1); } - if (udp) { + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_UDP) { len = 4; if (getsockopt(master.commfd, SOL_SOCKET, SO_SNDBUF, &temp2, &len) != -1) { if (temp2 != buflength) { @@ -650,15 +917,18 @@ main(int argc, char **argv) #endif if (n) { #ifdef HAVE_LIBDL - if (ismloaded(&secmodule)) { - switch ((temp2 = secmodule.filter(ConnectUser_get_nameBuf(contable[i]), &buff[5], &n))) { + if (Module_isModuleLoaded(ClientRealm_get_serviceModule(pointer))) { + switch ((temp2 = Module_function_filter(ClientRealm_get_serviceModule(pointer), + ConnectUser_get_nameBuf(usersTable[i]), &buff[5], &n))) { case 1: case 4: { aflog(LOG_T_USER, LOG_I_WARNING, "user[%d] (by ser): PACKET IGNORED BY MODULE", i); if (temp2 == 4) { aflog(LOG_T_MAIN, LOG_I_INFO, - "RELEASED MODULE (ser): %s INFO: %s", secmodule.name, secmodule.info()); - releasemodule(&secmodule); + "RELEASED MODULE (ser): %s INFO: %s", + Module_get_fileName(ClientRealm_get_serviceModule(pointer)), + Module_function_info(ClientRealm_get_serviceModule(pointer))); + Module_releaseModule(ClientRealm_get_serviceModule(pointer)); } continue; break; @@ -666,27 +936,31 @@ main(int argc, char **argv) case 2: case 5: { aflog(LOG_T_USER, LOG_I_NOTICE, "user[%d] (by ser): DROPPED BY MODULE", i); - close(ConnectUser_get_connFd(contable[i])); - FD_CLR(ConnectUser_get_connFd(contable[i]), &allset); - FD_CLR(ConnectUser_get_connFd(contable[i]), &wset); - ConnectUser_set_state(contable[i], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(contable[i])); + close(ConnectUser_get_connFd(usersTable[i])); + FD_CLR(ConnectUser_get_connFd(usersTable[i]), &allset); + FD_CLR(ConnectUser_get_connFd(usersTable[i]), &wset); + ConnectUser_set_state(usersTable[i], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(usersTable[i])); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = i >> 8; /* high bits of user number */ buff[2] = i; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); if (temp2 == 5) { aflog(LOG_T_MAIN, LOG_I_INFO, - "RELEASED MODULE (ser): %s INFO: %s", secmodule.name, secmodule.info()); - releasemodule(&secmodule); + "RELEASED MODULE (ser): %s INFO: %s", + Module_get_fileName(ClientRealm_get_serviceModule(pointer)), + Module_function_info(ClientRealm_get_serviceModule(pointer))); + Module_releaseModule(ClientRealm_get_serviceModule(pointer)); } continue; break; } case 3: { aflog(LOG_T_MAIN, LOG_I_INFO, - "RELEASED MODULE (ser): %s INFO: %s", secmodule.name, secmodule.info()); - releasemodule(&secmodule); + "RELEASED MODULE (ser): %s INFO: %s", + Module_get_fileName(ClientRealm_get_serviceModule(pointer)), + Module_function_info(ClientRealm_get_serviceModule(pointer))); + Module_releaseModule(ClientRealm_get_serviceModule(pointer)); break; } } @@ -710,76 +984,76 @@ main(int argc, char **argv) aflog(LOG_T_USER, LOG_I_DEBUG, "user[%d]: TO msglen: %d", i, n); #endif - SslFd_send_message(type, master, buff, n+5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, n+5); } - else if (!udp) { + else if (ClientRealm_get_clientMode(pointer) != CLIENTREALM_MODE_UDP) { aflog(LOG_T_USER, LOG_I_INFO, "user[%d]: CLOSING", i); - close(ConnectUser_get_connFd(contable[i])); - FD_CLR(ConnectUser_get_connFd(contable[i]), &allset); - FD_CLR(ConnectUser_get_connFd(contable[i]), &wset); - ConnectUser_set_state(contable[i], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(contable[i])); + close(ConnectUser_get_connFd(usersTable[i])); + FD_CLR(ConnectUser_get_connFd(usersTable[i]), &allset); + FD_CLR(ConnectUser_get_connFd(usersTable[i]), &wset); + ConnectUser_set_state(usersTable[i], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(usersTable[i])); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = i >> 8; /* high bits of user number */ buff[2] = i; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); } } /* - FD_ISSET CONTABLE[i].CONNFD RSET */ } } - for (i = 0; i < usernum; ++i) { - if (ConnectUser_get_state(contable[i]) == S_STATE_STOPPED) { - if (FD_ISSET(ConnectUser_get_connFd(contable[i]), &tmpset)) { /* FD_ISSET CONTABLE[i].CONNFD TMPSET */ + for (i = 0; i < ClientRealm_get_usersLimit(pointer); ++i) { + if (ConnectUser_get_state(usersTable[i]) == S_STATE_STOPPED) { + if (FD_ISSET(ConnectUser_get_connFd(usersTable[i]), &tmpset)) { /* FD_ISSET CONTABLE[i].CONNFD TMPSET */ aflog(LOG_T_USER, LOG_I_DDEBUG, "user[%d]: FD_ISSET - WRITE", i); - n = BufListNode_readMessageLength(BufList_get_first(ConnectUser_get_bufList(contable[i]))); - temp2 = write(ConnectUser_get_connFd(contable[i]), - BufListNode_readMessage(BufList_get_first(ConnectUser_get_bufList(contable[i]))), n); + n = BufListNode_readMessageLength(BufList_get_first(ConnectUser_get_bufList(usersTable[i]))); + temp2 = write(ConnectUser_get_connFd(usersTable[i]), + BufListNode_readMessage(BufList_get_first(ConnectUser_get_bufList(usersTable[i]))), n); if ((temp2 > 0) && (temp2 != n)) { - BufListNode_set_actPtr(BufList_get_first(ConnectUser_get_bufList(contable[i])), - BufListNode_get_actPtr(BufList_get_first(ConnectUser_get_bufList(contable[i]))) + temp2); + BufListNode_set_actPtr(BufList_get_first(ConnectUser_get_bufList(usersTable[i])), + BufListNode_get_actPtr(BufList_get_first(ConnectUser_get_bufList(usersTable[i]))) + temp2); } else if ((temp2 == -1) && (errno == EAGAIN)) { aflog(LOG_T_USER, LOG_I_DEBUG, "user[%d]: Couldn't write?", i); } else if (temp2 == -1) { - close(ConnectUser_get_connFd(contable[i])); - FD_CLR(ConnectUser_get_connFd(contable[i]), &allset); - FD_CLR(ConnectUser_get_connFd(contable[i]), &wset); - ConnectUser_set_state(contable[i], S_STATE_CLOSING); + close(ConnectUser_get_connFd(usersTable[i])); + FD_CLR(ConnectUser_get_connFd(usersTable[i]), &allset); + FD_CLR(ConnectUser_get_connFd(usersTable[i]), &wset); + ConnectUser_set_state(usersTable[i], S_STATE_CLOSING); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = i >> 8; /* high bits of user number */ buff[2] = i; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); } else { - BufList_delete_first(ConnectUser_get_bufList(contable[i])); - if (BufList_get_first(ConnectUser_get_bufList(contable[i])) == NULL) { - ConnectUser_set_state(contable[i], S_STATE_OPEN); - FD_CLR(ConnectUser_get_state(contable[i]), &wset); + BufList_delete_first(ConnectUser_get_bufList(usersTable[i])); + if (BufList_get_first(ConnectUser_get_bufList(usersTable[i])) == NULL) { + ConnectUser_set_state(usersTable[i], S_STATE_OPEN); + FD_CLR(ConnectUser_get_state(usersTable[i]), &wset); buff[0] = AF_S_CAN_SEND; /* stopping transfer */ buff[1] = i >> 8; /* high bits of user number */ buff[2] = i; /* low bits of user number */ aflog(LOG_T_USER, LOG_I_DDEBUG, "FROM user[%d]: BUFFERING MESSAGE ENDED", i); - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); } } } /* - FD_ISSET CONTABLE[i].CONNFD TMPSET */ } } - if (FD_ISSET(SslFd_get_fd(master), &rset)) { /* FD_ISSET MASTER.COMMFD RSET */ + if (FD_ISSET(SslFd_get_fd(ClientRealm_get_masterSslFd(pointer)), &rset)) { /* FD_ISSET MASTER.COMMFD RSET */ aflog(LOG_T_CLIENT, LOG_I_DDEBUG, "masterfd: FD_ISSET"); - n = SslFd_get_message(type, master, buff, 5); + n = SslFd_get_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); if (n != 5) { aflog(LOG_T_CLIENT, LOG_I_ERR, "FATAL ERROR! (%d)", n); if (n == -1) { - if (TYPE_IS_SSL(type)) { - get_ssl_error(master, "FE", n); + if (TYPE_IS_SSL(ClientRealm_get_realmType(pointer))) { + get_ssl_error(ClientRealm_get_masterSslFd(pointer), "FE", n); continue; /* what happened? */ } } @@ -787,8 +1061,8 @@ main(int argc, char **argv) exit(1); } if (n == 0) { /* server quits -> we do the same... */ - i = ArOptions_get_arTries(ao); - if (ArOptions_get_arPremature(ao) == AR_OPTION_DISABLED) { + i = ArOptions_get_arTries(ClientRealm_get_arOptions(pointer)); + if (ArOptions_get_arPremature(ClientRealm_get_arOptions(pointer)) == AR_OPTION_DISABLED) { i = 0; } if (i) { @@ -796,32 +1070,27 @@ main(int argc, char **argv) "SERVER: premature quit -> auto-reconnect enabled"); } while (i) { - close_connections(usernum, &contable); - SslFd_set_ssl(master, NULL); - mysleep(ArOptions_get_arDelay(ao)); + ClientRealm_closeUsersConnections(pointer); + close(SslFd_get_fd(ClientRealm_get_masterSslFd(pointer))); + SslFd_set_ssl(ClientRealm_get_masterSslFd(pointer), NULL); + mysleep(ArOptions_get_arDelay(ClientRealm_get_arOptions(pointer))); aflog(LOG_T_CLIENT, LOG_I_INFO, "Trying to reconnect..."); temp2 = 0; if (temp2 == 0) { -#ifdef HAVE_LIBPTHREAD - if (initialize_client_stage1(tunneltype, master, name, manage, hpo, - ipfam, ctx, buff, pass, 0, ignorepkeys)) { -#else - if (initialize_client_stage1(tunneltype, master, name, manage, NULL, - ipfam, ctx, buff, pass, 0, ignorepkeys)) { -#endif + if (initialize_client_stage1(pointer, ctx, buff, 0, + ClientConfiguration_get_ignorePublicKeys(cconfig))) { temp2 = 1; } } if (temp2 == 0) { - if (initialize_client_stage2(&type, master, &usernum, buff, 0)) { + if (initialize_client_stage2(pointer, buff, 0)) { temp2 = 1; } } if (temp2 == 0) { - if (initialize_client_stage3(&contable, master, usernum, &buflength, &len, &allset, - &wset, &maxfdp1, 0)) { + if (initialize_client_stage3(pointer, &buflength, &allset, &wset, &maxfdp1, 0)) { temp2 = 1; } } @@ -830,6 +1099,18 @@ main(int argc, char **argv) n = 1; aflog(LOG_T_CLIENT, LOG_I_INFO, "Reconnected successfully..."); + usersTable = ClientRealm_get_usersTable(pointer); + if (ClientRealm_get_realmId(pointer) != NULL) { + buff[0] = AF_S_LOGIN; + buff[1] = buff[2] = 0; + n = strlen(ClientRealm_get_realmId(pointer)); + memcpy(&buff[5], ClientRealm_get_realmId(pointer), n); + buff[3] = n >> 8; /* high bits of message length */ + buff[4] = n; /* low bits of message length */ + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, n+5); + aflog(LOG_T_CLIENT, LOG_I_INFO, + "ID SENT: %s", ClientRealm_get_realmId(pointer)); + } break; } @@ -854,26 +1135,26 @@ main(int argc, char **argv) case AF_S_CONCLOSED : { aflog(LOG_T_USER, LOG_I_DDEBUG, "user[%d]: AF_S_CONCLOSED", numofcon); - if ((numofcon>=0) && (numofcon<=usernum)) { + if ((numofcon>=0) && (numofcon<=ClientRealm_get_usersLimit(pointer))) { usercon--; - if (ConnectUser_get_state(contable[numofcon]) == S_STATE_CLOSING) { - ConnectUser_set_state(contable[numofcon], S_STATE_CLEAR); + if (ConnectUser_get_state(usersTable[numofcon]) == S_STATE_CLOSING) { + ConnectUser_set_state(usersTable[numofcon], S_STATE_CLEAR); aflog(LOG_T_USER, LOG_I_INFO, "user[%d]: CLOSED", numofcon); } - else if ((ConnectUser_get_state(contable[numofcon]) == S_STATE_OPEN) || - (ConnectUser_get_state(contable[numofcon]) == S_STATE_STOPPED)) { + else if ((ConnectUser_get_state(usersTable[numofcon]) == S_STATE_OPEN) || + (ConnectUser_get_state(usersTable[numofcon]) == S_STATE_STOPPED)) { aflog(LOG_T_USER, LOG_I_INFO, "user[%d]: CLOSED", numofcon); - close(ConnectUser_get_connFd(contable[numofcon])); - FD_CLR(ConnectUser_get_connFd(contable[numofcon]), &allset); - FD_CLR(ConnectUser_get_connFd(contable[numofcon]), &wset); - ConnectUser_set_state(contable[numofcon], S_STATE_CLEAR); - BufList_clear(ConnectUser_get_bufList(contable[numofcon])); + close(ConnectUser_get_connFd(usersTable[numofcon])); + FD_CLR(ConnectUser_get_connFd(usersTable[numofcon]), &allset); + FD_CLR(ConnectUser_get_connFd(usersTable[numofcon]), &wset); + ConnectUser_set_state(usersTable[numofcon], S_STATE_CLEAR); + BufList_clear(ConnectUser_get_bufList(usersTable[numofcon])); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = numofcon >> 8; /* high bits of user number */ buff[2] = numofcon; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); } } break; @@ -881,65 +1162,71 @@ main(int argc, char **argv) case AF_S_CONOPEN : { aflog(LOG_T_USER, LOG_I_DDEBUG, "user[%d]: AF_S_CONOPEN", numofcon); - if ((numofcon>=0) && (numofcon<=usernum)) { + if ((numofcon>=0) && (numofcon<=ClientRealm_get_usersLimit(pointer))) { usercon++; - if (ConnectUser_get_state(contable[numofcon]) == S_STATE_CLEAR) { - n = SslFd_get_message(type, master, buff, length); - ConnectUser_set_nameBuf(contable[numofcon], (char*) buff); - ConnectUser_set_portBuf(contable[numofcon], (char*) &buff[128]); + if (ConnectUser_get_state(usersTable[numofcon]) == S_STATE_CLEAR) { + n = SslFd_get_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, length); + ConnectUser_set_nameBuf(usersTable[numofcon], (char*) buff); + ConnectUser_set_portBuf(usersTable[numofcon], (char*) &buff[128]); aflog(LOG_T_USER, LOG_I_INFO, "user[%d]: OPENING", numofcon); aflog(LOG_T_USER, LOG_I_INFO, "user[%d]: IP:%s PORT:%s", numofcon, - ConnectUser_get_nameBuf(contable[numofcon]), ConnectUser_get_portBuf(contable[numofcon])); + ConnectUser_get_nameBuf(usersTable[numofcon]), ConnectUser_get_portBuf(usersTable[numofcon])); #ifdef HAVE_LIBDL - if (ismloaded(&module) && module.allow(ConnectUser_get_nameBuf(contable[numofcon]), - ConnectUser_get_portBuf(contable[numofcon]))) { + if (Module_isModuleLoaded(ClientRealm_get_userModule(pointer)) && + Module_function_allow(ClientRealm_get_userModule(pointer), + ConnectUser_get_nameBuf(usersTable[numofcon]), + ConnectUser_get_portBuf(usersTable[numofcon]))) { aflog(LOG_T_USER, LOG_I_WARNING, "user[%d]: IT'S NOT ALLOWED - DROPPING", numofcon); buff[0] = AF_S_CANT_OPEN; /* not opening connection */ buff[1] = numofcon >> 8; /* high bits of user number */ buff[2] = numofcon; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); usercon--; continue; } #endif - if (udp) { - ipfam = 0; + if (ClientRealm_get_clientMode(pointer) == CLIENTREALM_MODE_UDP) { + ipFamily = 0; } else { - ipfam = 0x01; + ipFamily = 0x01; } #ifdef AF_INET6 - if (TYPE_IS_IPV4(type)) { - ipfam |= 0x02; + if (TYPE_IS_IPV4(ClientRealm_get_realmType(pointer))) { + ipFamily |= 0x02; } - else if (TYPE_IS_IPV6(type)) { - ipfam |= 0x04; + else if (TYPE_IS_IPV6(ClientRealm_get_realmType(pointer))) { + ipFamily |= 0x04; } #endif - if (ip_connect(&temp, desnam, despor, ipfam)) { + if (ip_connect(&temp, ClientRealm_get_hostName(pointer), + ClientRealm_get_destinationPort(pointer), ipFamily, + ClientRealm_get_localDestinationName(pointer), NULL)) { aflog(LOG_T_USER, LOG_I_WARNING, - "user[%d]: CAN'T CONNECT to %s:%s - DROPPING", numofcon, desnam, despor); + "user[%d]: CAN'T CONNECT to %s:%s - DROPPING", numofcon, + ClientRealm_get_hostName(pointer), + ClientRealm_get_destinationPort(pointer)); buff[0] = AF_S_CANT_OPEN; /* not opening connection */ buff[1] = numofcon >> 8; /* high bits of user number */ buff[2] = numofcon; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); usercon--; continue; } - ConnectUser_set_connFd(contable[numofcon], temp); - temp2 = fcntl(ConnectUser_get_connFd(contable[numofcon]), F_GETFL, 0); - fcntl(ConnectUser_get_connFd(contable[numofcon]), F_SETFL, temp2 | O_NONBLOCK); - FD_SET(ConnectUser_get_connFd(contable[numofcon]), &allset); - maxfdp1 = (maxfdp1 > (ConnectUser_get_connFd(contable[numofcon]) + 1)) ? - maxfdp1 : (ConnectUser_get_connFd(contable[numofcon]) + 1); + ConnectUser_set_connFd(usersTable[numofcon], temp); + temp2 = fcntl(ConnectUser_get_connFd(usersTable[numofcon]), F_GETFL, 0); + fcntl(ConnectUser_get_connFd(usersTable[numofcon]), F_SETFL, temp2 | O_NONBLOCK); + FD_SET(ConnectUser_get_connFd(usersTable[numofcon]), &allset); + maxfdp1 = (maxfdp1 > (ConnectUser_get_connFd(usersTable[numofcon]) + 1)) ? + maxfdp1 : (ConnectUser_get_connFd(usersTable[numofcon]) + 1); buff[0] = AF_S_CONOPEN; /* opening connection */ buff[1] = numofcon >> 8; /* high bits of user number */ buff[2] = numofcon; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); - ConnectUser_set_state(contable[numofcon], S_STATE_OPEN); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); + ConnectUser_set_state(usersTable[numofcon], S_STATE_OPEN); } } break; @@ -949,19 +1236,22 @@ main(int argc, char **argv) "user[%d]: AF_S_MESSAGE", numofcon); aflog(LOG_T_USER, LOG_I_DEBUG, "user[%d]: FROM msglen: %d", numofcon, length); - n = SslFd_get_message(type, master, buff, length); - if ((numofcon>=0) && (numofcon<=usernum)) { - if (ConnectUser_get_state(contable[numofcon]) == S_STATE_OPEN) { + n = SslFd_get_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, length); + if ((numofcon>=0) && (numofcon<=ClientRealm_get_usersLimit(pointer))) { + if (ConnectUser_get_state(usersTable[numofcon]) == S_STATE_OPEN) { #ifdef HAVE_LIBDL - if (ismloaded(&module)) { - switch ((temp2 = module.filter(ConnectUser_get_nameBuf(contable[numofcon]), buff, &n))) { + if (Module_isModuleLoaded(ClientRealm_get_userModule(pointer))) { + switch ((temp2 = Module_function_filter(ClientRealm_get_userModule(pointer), + ConnectUser_get_nameBuf(usersTable[numofcon]), buff, &n))) { case 1: case 4:{ aflog(LOG_T_USER, LOG_I_WARNING, "user[%d]: PACKET IGNORED BY MODULE", numofcon); if (temp2 == 4) { aflog(LOG_T_MAIN, LOG_I_INFO, - "RELEASED MODULE: %s INFO: %s", module.name, module.info()); - releasemodule(&module); + "RELEASED MODULE: %s INFO: %s", + Module_get_fileName(ClientRealm_get_userModule(pointer)), + Module_function_info(ClientRealm_get_userModule(pointer))); + Module_releaseModule(ClientRealm_get_userModule(pointer)); } continue; break; @@ -969,27 +1259,31 @@ main(int argc, char **argv) case 2: case 5:{ aflog(LOG_T_USER, LOG_I_NOTICE, "user[%d]: DROPPED BY MODULE", numofcon); - close(ConnectUser_get_connFd(contable[numofcon])); - FD_CLR(ConnectUser_get_connFd(contable[numofcon]), &allset); - FD_CLR(ConnectUser_get_connFd(contable[numofcon]), &wset); - ConnectUser_set_state(contable[numofcon], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(contable[numofcon])); + close(ConnectUser_get_connFd(usersTable[numofcon])); + FD_CLR(ConnectUser_get_connFd(usersTable[numofcon]), &allset); + FD_CLR(ConnectUser_get_connFd(usersTable[numofcon]), &wset); + ConnectUser_set_state(usersTable[numofcon], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(usersTable[numofcon])); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = numofcon >> 8; /* high bits of user number */ buff[2] = numofcon; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); if (temp2 == 5) { aflog(LOG_T_MAIN, LOG_I_INFO, - "RELEASED MODULE: %s INFO: %s", module.name, module.info()); - releasemodule(&module); + "RELEASED MODULE: %s INFO: %s", + Module_get_fileName(ClientRealm_get_userModule(pointer)), + Module_function_info(ClientRealm_get_userModule(pointer))); + Module_releaseModule(ClientRealm_get_userModule(pointer)); } continue; break; } case 3: { aflog(LOG_T_MAIN, LOG_I_INFO, - "RELEASED MODULE: %s INFO: %s", module.name, module.info()); - releasemodule(&module); + "RELEASED MODULE: %s INFO: %s", + Module_get_fileName(ClientRealm_get_userModule(pointer)), + Module_function_info(ClientRealm_get_userModule(pointer))); + Module_releaseModule(ClientRealm_get_userModule(pointer)); break; } } @@ -997,47 +1291,47 @@ main(int argc, char **argv) #endif aflog(LOG_T_USER, LOG_I_DEBUG, "user[%d]: FROM msglen: %d SENT", numofcon, n); - temp2 = write(ConnectUser_get_connFd(contable[numofcon]), buff, n); + temp2 = write(ConnectUser_get_connFd(usersTable[numofcon]), buff, n); if ((temp2 > 0) && (temp2 != n)) { - BufList_insert_back(ConnectUser_get_bufList(contable[numofcon]), + BufList_insert_back(ConnectUser_get_bufList(usersTable[numofcon]), BufListNode_new_message(temp2, n, buff)); - ConnectUser_set_state(contable[numofcon], S_STATE_STOPPED); - FD_SET(ConnectUser_get_connFd(contable[numofcon]), &wset); + ConnectUser_set_state(usersTable[numofcon], S_STATE_STOPPED); + FD_SET(ConnectUser_get_connFd(usersTable[numofcon]), &wset); buff[0] = AF_S_DONT_SEND; /* stopping transfer */ buff[1] = numofcon >> 8; /* high bits of user number */ buff[2] = numofcon; /* low bits of user number */ aflog(LOG_T_USER, LOG_I_DDEBUG, "FROM user[%d]: BUFFERING MESSAGE STARTED", numofcon); - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); } else if ((temp2 == -1) && (errno == EAGAIN)) { - BufList_insert_back(ConnectUser_get_bufList(contable[numofcon]), + BufList_insert_back(ConnectUser_get_bufList(usersTable[numofcon]), BufListNode_new_message(0, n, buff)); - ConnectUser_set_state(contable[numofcon], S_STATE_STOPPED); - FD_SET(ConnectUser_get_connFd(contable[numofcon]), &wset); + ConnectUser_set_state(usersTable[numofcon], S_STATE_STOPPED); + FD_SET(ConnectUser_get_connFd(usersTable[numofcon]), &wset); buff[0] = AF_S_DONT_SEND; /* stopping transfer */ buff[1] = numofcon >> 8; /* high bits of user number */ buff[2] = numofcon; /* low bits of user number */ aflog(LOG_T_USER, LOG_I_DDEBUG, "FROM user[%d]: BUFFERING MESSAGE STARTED", numofcon); - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); } else if (temp2 == -1) { - close(ConnectUser_get_connFd(contable[numofcon])); - FD_CLR(ConnectUser_get_connFd(contable[numofcon]), &allset); - FD_CLR(ConnectUser_get_connFd(contable[numofcon]), &wset); - ConnectUser_set_state(contable[numofcon], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(contable[numofcon])); + close(ConnectUser_get_connFd(usersTable[numofcon])); + FD_CLR(ConnectUser_get_connFd(usersTable[numofcon]), &allset); + FD_CLR(ConnectUser_get_connFd(usersTable[numofcon]), &wset); + ConnectUser_set_state(usersTable[numofcon], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(usersTable[numofcon])); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = numofcon >> 8; /* high bits of user number */ buff[2] = numofcon; /* low bits of user number */ - SslFd_send_message(type, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, 5); } } - else if (ConnectUser_get_state(contable[numofcon]) == S_STATE_STOPPED) { + else if (ConnectUser_get_state(usersTable[numofcon]) == S_STATE_STOPPED) { aflog(LOG_T_USER, LOG_I_DDEBUG, "FROM user[%d]: BUFFERING MESSAGE", numofcon); - BufList_insert_back(ConnectUser_get_bufList(contable[numofcon]), + BufList_insert_back(ConnectUser_get_bufList(usersTable[numofcon]), BufListNode_new_message(0, n, buff)); } } @@ -1045,8 +1339,8 @@ main(int argc, char **argv) } case AF_S_CLOSING : { /* server shut down */ n = 0; - i = ArOptions_get_arTries(ao); - if (ArOptions_get_arQuit(ao) == AR_OPTION_DISABLED) { + i = ArOptions_get_arTries(ClientRealm_get_arOptions(pointer)); + if (ArOptions_get_arQuit(ClientRealm_get_arOptions(pointer)) == AR_OPTION_DISABLED) { i = 0; } if (i) { @@ -1054,32 +1348,27 @@ main(int argc, char **argv) "SERVER: CLOSED -> auto-reconnect enabled"); } while (i) { - close_connections(usernum, &contable); - SslFd_set_ssl(master, NULL); - mysleep(ArOptions_get_arDelay(ao)); + ClientRealm_closeUsersConnections(pointer); + close(SslFd_get_fd(ClientRealm_get_masterSslFd(pointer))); + SslFd_set_ssl(ClientRealm_get_masterSslFd(pointer), NULL); + mysleep(ArOptions_get_arDelay(ClientRealm_get_arOptions(pointer))); aflog(LOG_T_CLIENT, LOG_I_INFO, "Trying to reconnect..."); temp2 = 0; if (temp2 == 0) { -#ifdef HAVE_LIBPTHREAD - if (initialize_client_stage1(tunneltype, master, name, manage, hpo, - ipfam, ctx, buff, pass, 0, ignorepkeys)) { -#else - if (initialize_client_stage1(tunneltype, master, name, manage, NULL, - ipfam, ctx, buff, pass, 0, ignorepkeys)) { -#endif + if (initialize_client_stage1(pointer, ctx, buff, 0, + ClientConfiguration_get_ignorePublicKeys(cconfig))) { temp2 = 1; } } if (temp2 == 0) { - if (initialize_client_stage2(&type, master, &usernum, buff, 0)) { + if (initialize_client_stage2(pointer, buff, 0)) { temp2 = 1; } } if (temp2 == 0) { - if (initialize_client_stage3(&contable, master, usernum, &buflength, &len, &allset, - &wset, &maxfdp1, 0)) { + if (initialize_client_stage3(pointer, &buflength, &allset, &wset, &maxfdp1, 0)) { temp2 = 1; } } @@ -1088,6 +1377,18 @@ main(int argc, char **argv) n = 1; aflog(LOG_T_CLIENT, LOG_I_INFO, "Reconnected successfully..."); + usersTable = ClientRealm_get_usersTable(pointer); + if (ClientRealm_get_realmId(pointer) != NULL) { + buff[0] = AF_S_LOGIN; + buff[1] = buff[2] = 0; + n = strlen(ClientRealm_get_realmId(pointer)); + memcpy(&buff[5], ClientRealm_get_realmId(pointer), n); + buff[3] = n >> 8; /* high bits of message length */ + buff[4] = n; /* low bits of message length */ + SslFd_send_message(ClientRealm_get_realmType(pointer), ClientRealm_get_masterSslFd(pointer), buff, n+5); + aflog(LOG_T_CLIENT, LOG_I_INFO, + "ID SENT: %s", ClientRealm_get_realmId(pointer)); + } break; } @@ -1105,13 +1406,13 @@ main(int argc, char **argv) case AF_S_DONT_SEND: { aflog(LOG_T_USER, LOG_I_DEBUG, "user[%d]: AF_S_DONT_SEND", numofcon); - FD_CLR(ConnectUser_get_connFd(contable[numofcon]), &allset); + FD_CLR(ConnectUser_get_connFd(usersTable[numofcon]), &allset); break; } case AF_S_CAN_SEND: { aflog(LOG_T_USER, LOG_I_DEBUG, "user[%d]: AF_S_CAN_SEND", numofcon); - FD_SET(ConnectUser_get_connFd(contable[numofcon]), &allset); + FD_SET(ConnectUser_get_connFd(usersTable[numofcon]), &allset); break; } default : { /* unrecognized type of message -> exiting... */ -- cgit v1.1