diff options
Diffstat (limited to 'src')
49 files changed, 6230 insertions, 1806 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index f512ce8..6f8b46b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,13 +6,15 @@ afserver_SOURCES = afserver.c network.c file.c stats.c buf_list_node_struct.c bu thread_management.c server_signals.c usage.c logging.c daemon.c ssl_fd_struct.c \ user_stats_struct.c usr_cli_struct.c connect_user_struct.c string_functions.c \ audit_list_node_struct.c audit_list_struct.c connect_client_struct.c timeval_functions.c \ - header_buffer_struct.c + header_buffer_struct.c server_realm_struct.c server_configuration_struct.c file_server.c afclient_LDFLAGS = ${USE_RDYNAMIC} ${LINKED_LDLIB} -afclient_SOURCES = afclient.c network.c stats.c buf_list_node_struct.c buf_list_struct.c modules.c \ - client_remoteadmin.c make_ssl_handshake.c first_run.c inet_ntop.c realmnames.c \ - usernames.c http_proxy_functions.c http_proxy_client.c \ +afclient_SOURCES = afclient.c network.c file.c stats.c buf_list_node_struct.c buf_list_struct.c \ + module_struct.c client_remoteadmin.c make_ssl_handshake.c first_run.c inet_ntop.c \ + realmnames.c usernames.c http_proxy_functions.c http_proxy_client.c \ http_proxy_options_struct.c thread_management.c client_reverse_udp.c server_check.c \ client_initialization.c client_shutdown.c client_signals.c usage.c logging.c \ daemon.c base64.c ssl_routines.c connect_user_struct.c user_stats_struct.c \ string_functions.c ar_options_struct.c audit_list_node_struct.c audit_list_struct.c \ - ssl_fd_struct.c timeval_functions.c + ssl_fd_struct.c timeval_functions.c server_realm_struct.c server_configuration_struct.c \ + usr_cli_struct.c connect_client_struct.c header_buffer_struct.c \ + client_configuration_struct.c file_client.c client_realm_struct.c diff --git a/src/activefor.h b/src/activefor.h index ec2fcc5..4fcdbe0 100644 --- a/src/activefor.h +++ b/src/activefor.h @@ -49,8 +49,9 @@ #define S_STATE_OPENING 6 #define S_STATE_OPEN 7 #define S_STATE_STOPPED 11 +#define S_STATE_OPENING_CLOSED 17 -#define AF_VER(info) info" v0.7.2" +#define AF_VER(info) info" v0.7.4" #define TYPE_TCP 1 #define TYPE_UDP 3 @@ -85,49 +86,5 @@ #define TYPE_SET_COMP(type) (type|=TYPE_COMP) #define TYPE_IS_COMP(type) (type&TYPE_COMP) -typedef struct { - char* hostname; - char* users; - char* clients; - char* raclients; - char* usrpcli; - char* clim; - char* timeout; - char* realmname; - unsigned char pass[4]; - int usercon; - int usernum; - int clicon; - int clinum; - int raclicon; - int raclinum; - int upcnum; - int tmout; - int climode; - int usrclinum; - int clientcounter; - int usercounter; - char type; - char tunneltype; - char dnslookups; - char baseport; - char audit; - socklen_t addrlen; - struct sockaddr* cliaddr; - ConnectUser** contable; - ConnectClient** clitable; - ConnectClient** raclitable; - UsrCli** usrclitable; -} RealmT; - -typedef struct { - char* certif; - char* keys; - char* dateformat; - int size; - time_t starttime; - RealmT* realmtable; -} ConfigurationT; - #endif 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... */ diff --git a/src/afclient.h b/src/afclient.h index 57a9f7c..200af70 100644 --- a/src/afclient.h +++ b/src/afclient.h @@ -20,8 +20,9 @@ #include "activefor.h" #include "network.h" +#include "file.h" #include "stats.h" -#include "modules.h" +#include "module_struct.h" #include "client_remoteadmin.h" #include "make_ssl_handshake.h" #include "first_run.h" diff --git a/src/afserver.c b/src/afserver.c index 1066176..2d6e1eb 100644 --- a/src/afserver.c +++ b/src/afserver.c @@ -61,7 +61,7 @@ static struct option long_options[] = { {0, 0, 0, 0} }; -ConfigurationT config; +ServerConfiguration* config; int main(int argc, char **argv) @@ -96,13 +96,19 @@ main(int argc, char **argv) char baseport = 0; char audit = 0; char dnslookups = 0; - RealmT* pointer = NULL; + ServerRealm* pointer = NULL; struct sigaction act; time_t now; + ServerRealm** scRealmsTable; + UsrCli** srUsersClientsTable; + ConnectUser** srUsersTable; + ConnectClient** srClientsTable; + ConnectClient** srRaClientsTable; char* certif = NULL; char* keys = NULL; char* dateformat = NULL; + char* stemp = NULL; SSL_METHOD* method; SSL_CTX* ctx; @@ -115,17 +121,10 @@ main(int argc, char **argv) sigaction(SIGPIPE, &act, NULL); act.sa_handler = server_sig_int; sigaction(SIGINT, &act, NULL); + sigaction(SIGTERM, &act, NULL); TYPE_SET_SSL(mode); TYPE_SET_ZLIB(mode); - - memset(&config, 0, sizeof(config)); - - config.certif = NULL; - config.keys = NULL; - config.size = 0; - config.realmtable = NULL; - config.dateformat = NULL; #ifdef HAVE_LIBPTHREAD remember_mainthread(); @@ -322,25 +321,29 @@ main(int argc, char **argv) } else { if (certif == NULL) { - config.certif = "cacert.pem"; + if (ServerConfiguration_get_certificateFile(config) == NULL) { + ServerConfiguration_set_certificateFile(config, "cacert.pem"); + } } else { - config.certif = certif; + ServerConfiguration_set_certificateFile(config, certif); } if (keys == NULL) { - config.keys = "server.rsa"; + if (ServerConfiguration_get_keysFile(config) == NULL) { + ServerConfiguration_set_keysFile(config, "server.rsa"); + } } else { - config.keys = keys; + ServerConfiguration_set_keysFile(config, keys); } if (dateformat != NULL) { - config.dateformat = dateformat; + ServerConfiguration_set_dateFormat(config, dateformat); } - initializelogging(verbose, config.dateformat); + initializelogging(verbose, ServerConfiguration_get_dateFormat(config)); aflog(LOG_T_INIT, LOG_I_INFO, - "cfg file OK! (readed realms: %d)", config.size); + "cfg file OK! (readed realms: %d)", ServerConfiguration_get_realmsNumber(config)); if (name != NULL) aflog(LOG_T_INIT, LOG_I_WARNING, "Warning: hostname=%s will be ignored", name); @@ -358,12 +361,17 @@ main(int argc, char **argv) "Warning: password from command line will be ignored"); } } - else { - config.certif = certif; - config.keys = keys; - config.dateformat = dateformat; - - initializelogging(verbose, config.dateformat); + else { + config = ServerConfiguration_new(); + if (config == NULL) { + printf("Can't allocate memory for server configuration... exiting\n"); + exit(1); + } + ServerConfiguration_set_certificateFile(config, certif); + ServerConfiguration_set_keysFile(config, keys); + ServerConfiguration_set_dateFormat(config, dateformat); + + initializelogging(verbose, ServerConfiguration_get_dateFormat(config)); if (listen == NULL) { listencount = 1; @@ -380,52 +388,77 @@ main(int argc, char **argv) "Number of listen and manage options are not the same... exiting"); exit(1); } - if (config.certif == NULL) { - config.certif = "cacert.pem"; - } - if (config.keys == NULL) { - config.keys = "server.rsa"; - } + if (ServerConfiguration_get_certificateFile(config) == NULL) { + ServerConfiguration_set_certificateFile(config, "cacert.pem"); + } + if (ServerConfiguration_get_keysFile(config) == NULL) { + ServerConfiguration_set_keysFile(config, "server.rsa"); + } if (type == NULL) { type = "tcp"; } - config.size = 1; - config.realmtable = calloc(config.size, sizeof(RealmT)); - config.realmtable[0].hostname = name; - config.realmtable[0].usrclinum = managecount; - config.realmtable[0].usrclitable = calloc(managecount, sizeof(UsrCli*)); - for (i = 0; i < config.realmtable[0].usrclinum; ++i) { - config.realmtable[0].usrclitable[i] = UsrCli_new(); - if (config.realmtable[0].usrclitable[i] == NULL) { + ServerConfiguration_set_realmsNumber(config, 1); + scRealmsTable = calloc(1, sizeof(ServerRealm*)); + if (scRealmsTable == NULL) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Problem with allocating memory for ServerRealm* table... exiting"); + exit(1); + } + ServerConfiguration_set_realmsTable(config, scRealmsTable); + pointer = ServerRealm_new(); + if (pointer == NULL) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Problem with allocating memory for ServerRealm structure... exiting"); + exit(1); + } + scRealmsTable[0] = pointer; + ServerRealm_set_hostName(pointer, name); + ServerRealm_set_userClientPairs(pointer, managecount); + srUsersClientsTable = calloc(managecount, sizeof(UsrCli*)); + if (srUsersClientsTable == NULL) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Problem with allocating memory for UsrCli* table... exiting"); + exit(1); + } + ServerRealm_set_usersClientsTable(pointer, srUsersClientsTable); + for (i = 0; i < managecount; ++i) { + srUsersClientsTable[i] = UsrCli_new(); + if (srUsersClientsTable[i] == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Problem with allocating memory for UsrCli structure... exiting"); exit(1); } - UsrCli_set_listenPortName(config.realmtable[0].usrclitable[i], listen[i]); - UsrCli_set_managePortName(config.realmtable[0].usrclitable[i], manage[i]); + UsrCli_set_listenPortName(srUsersClientsTable[i], listen[i]); + UsrCli_set_managePortName(srUsersClientsTable[i], manage[i]); } - config.realmtable[0].users = amount; - config.realmtable[0].clients = clients; - config.realmtable[0].raclients = raclients; - config.realmtable[0].timeout = timeout; - config.realmtable[0].usrpcli = usrpcli; - config.realmtable[0].clim = clim; - config.realmtable[0].baseport = baseport; - config.realmtable[0].audit = audit; + ServerRealm_set_sUsersLimit(pointer, amount); + ServerRealm_set_sClientsLimit(pointer, clients); + ServerRealm_set_sRaClientsLimit(pointer, raclients); + ServerRealm_set_sTimeout(pointer, timeout); + ServerRealm_set_sUsersPerClient(pointer, usrpcli); + ServerRealm_set_sClientMode(pointer, clim); + ServerRealm_set_basePortOn(pointer, baseport); + ServerRealm_set_auditOn(pointer, audit); #ifdef HAVE_LIBPTHREAD - config.realmtable[0].tunneltype = tunneltype; + ServerRealm_set_tunnelType(pointer, tunneltype); #endif - config.realmtable[0].dnslookups = dnslookups; - config.realmtable[0].realmname = realmname; - memcpy(config.realmtable[0].pass, pass, 4); + ServerRealm_set_dnsLookupsOn(pointer, dnslookups); + ServerRealm_set_realmName(pointer, realmname); + ServerRealm_set_password(pointer, pass); if (strcmp(type, "tcp") == 0) { - TYPE_SET_TCP(config.realmtable[0].type); + temp = ServerRealm_get_realmType(pointer); + TYPE_SET_TCP(temp); + ServerRealm_set_realmType(pointer, temp); } else if (strcmp(type, "udp") == 0) { - TYPE_SET_UDP(config.realmtable[0].type); + temp = ServerRealm_get_realmType(pointer); + TYPE_SET_UDP(temp); + ServerRealm_set_realmType(pointer, temp); } else { - TYPE_SET_ZERO(config.realmtable[0].type); + temp = ServerRealm_get_realmType(pointer); + TYPE_SET_ZERO(temp); + ServerRealm_set_realmType(pointer, temp); } #ifdef AF_INET6 if (ipfam == -1) { @@ -434,13 +467,19 @@ main(int argc, char **argv) exit(1); } else if (ipfam == 4) { - TYPE_SET_IPV4(config.realmtable[0].type); + temp = ServerRealm_get_realmType(pointer); + TYPE_SET_IPV4(temp); + ServerRealm_set_realmType(pointer, temp); } else if (ipfam == 6) { - TYPE_SET_IPV6(config.realmtable[0].type); + temp = ServerRealm_get_realmType(pointer); + TYPE_SET_IPV6(temp); + ServerRealm_set_realmType(pointer, temp); } #endif - config.realmtable[0].type |= mode; + temp = ServerRealm_get_realmType(pointer); + temp |= mode; + ServerRealm_set_realmType(pointer, temp); } maxfdp1 = manconnecting = 0; @@ -461,25 +500,30 @@ main(int argc, char **argv) "Warning: Creating ./apf directory failed (%d)", flags); } } - if ((flags = generate_rsa_key(&config.keys))) { + keys = ServerConfiguration_get_keysFile(config); + if ((flags = generate_rsa_key(&keys))) { aflog(LOG_T_INIT, LOG_I_WARNING, "Warning: Something bad happened when generating rsa keys... (%d)", flags); } - if (SSL_CTX_use_RSAPrivateKey_file(ctx, config.keys, SSL_FILETYPE_PEM) != 1) { + ServerConfiguration_set_keysFile(config, keys); + if (SSL_CTX_use_RSAPrivateKey_file(ctx, ServerConfiguration_get_keysFile(config), SSL_FILETYPE_PEM) != 1) { aflog(LOG_T_INIT, LOG_I_CRIT, - "Setting rsa key failed (%s)... exiting", config.keys); + "Setting rsa key failed (%s)... exiting", ServerConfiguration_get_keysFile(config)); exit(1); } - if ((flags = generate_certificate(&config.certif, config.keys))) { + certif = ServerConfiguration_get_certificateFile(config); + if ((flags = generate_certificate(&certif, ServerConfiguration_get_keysFile(config)))) { aflog(LOG_T_INIT, LOG_I_WARNING, "Warning: Something bad happened when generating certificate... (%d)", flags); } - if (SSL_CTX_use_certificate_file(ctx, config.certif, SSL_FILETYPE_PEM) != 1) { + ServerConfiguration_set_certificateFile(config, certif); + if (SSL_CTX_use_certificate_file(ctx, + ServerConfiguration_get_certificateFile(config), SSL_FILETYPE_PEM) != 1) { aflog(LOG_T_INIT, LOG_I_CRIT, - "Setting certificate failed (%s)... exiting", config.certif); + "Setting certificate failed (%s)... exiting", ServerConfiguration_get_certificateFile(config)); exit(1); } - if (config.size == 0) { + if (ServerConfiguration_get_realmsNumber(config) == 0) { aflog(LOG_T_INIT, LOG_I_CRIT, "Working without sense is really without sense..."); exit(1); @@ -491,141 +535,180 @@ main(int argc, char **argv) if (!verbose) daemon(0, 0); - for (i = 0; i < config.size; ++i) { - if (config.realmtable[i].usrclinum == 0) { + scRealmsTable = ServerConfiguration_get_realmsTable(config); + for (i = 0; i < ServerConfiguration_get_realmsNumber(config); ++i) { + if (ServerRealm_get_userClientPairs(scRealmsTable[i]) == 0) { aflog(LOG_T_INIT, LOG_I_CRIT, "You have to specify at least one listen port and one manage port in each realm"); exit(1); } - for (j = 0; j < config.realmtable[i].usrclinum; ++j) { - if ((UsrCli_get_listenPortName(config.realmtable[i].usrclitable[j]) == NULL) || - (UsrCli_get_managePortName(config.realmtable[i].usrclitable[j]) == NULL)) { + srUsersClientsTable = ServerRealm_get_usersClientsTable(scRealmsTable[i]); + for (j = 0; j < ServerRealm_get_userClientPairs(scRealmsTable[i]); ++j) { + if ((UsrCli_get_listenPortName(srUsersClientsTable[j]) == NULL) || + (UsrCli_get_managePortName(srUsersClientsTable[j]) == NULL)) { aflog(LOG_T_INIT, LOG_I_CRIT, "Missing some of the variables...\nRealm: %d\nlistenport[%d]: %s\nmanageport[%d]: %s", - i, j, UsrCli_get_listenPortName(config.realmtable[i].usrclitable[j]), - j, UsrCli_get_managePortName(config.realmtable[i].usrclitable[j])); + i, j, UsrCli_get_listenPortName(srUsersClientsTable[j]), + j, UsrCli_get_managePortName(srUsersClientsTable[j])); exit(1); } } /* checking type of the realm */ - if (!TYPE_IS_SET(config.realmtable[i].type)) { + if (!TYPE_IS_SET(ServerRealm_get_realmType(scRealmsTable[i]))) { if (type != NULL) { if (strcmp(type, "tcp") == 0) { - TYPE_SET_TCP(config.realmtable[i].type); + temp = ServerRealm_get_realmType(scRealmsTable[i]); + TYPE_SET_TCP(temp); + ServerRealm_set_realmType(scRealmsTable[i], temp); } else if (strcmp(type, "udp") == 0) { - TYPE_SET_UDP(config.realmtable[i].type); + temp = ServerRealm_get_realmType(scRealmsTable[i]); + TYPE_SET_UDP(temp); + ServerRealm_set_realmType(scRealmsTable[i], temp); } else { - TYPE_SET_TCP(config.realmtable[i].type); + temp = ServerRealm_get_realmType(scRealmsTable[i]); + TYPE_SET_TCP(temp); + ServerRealm_set_realmType(scRealmsTable[i], temp); } } else { - TYPE_SET_TCP(config.realmtable[i].type); + temp = ServerRealm_get_realmType(scRealmsTable[i]); + TYPE_SET_TCP(temp); + ServerRealm_set_realmType(scRealmsTable[i], temp); } } #ifdef AF_INET6 /* using user's value for ipfam*/ - if (TYPE_IS_UNSPEC(config.realmtable[i].type)) { + if (TYPE_IS_UNSPEC(ServerRealm_get_realmType(scRealmsTable[i]))) { if (ipfam == -1) { aflog(LOG_T_INIT, LOG_I_CRIT, "Conflicting types of ip protocol family... exiting"); exit(1); } else if (ipfam == 4) { - TYPE_SET_IPV4(config.realmtable[i].type); + temp = ServerRealm_get_realmType(scRealmsTable[i]); + TYPE_SET_IPV4(temp); + ServerRealm_set_realmType(scRealmsTable[i], temp); } else if (ipfam == 6) { - TYPE_SET_IPV6(config.realmtable[i].type); + temp = ServerRealm_get_realmType(scRealmsTable[i]); + TYPE_SET_IPV6(temp); + ServerRealm_set_realmType(scRealmsTable[i], temp); } } #endif /* using user's values for zlib and ssl mode*/ if (!TYPE_IS_SSL(mode)) { - TYPE_UNSET_SSL(config.realmtable[i].type); + temp = ServerRealm_get_realmType(scRealmsTable[i]); + TYPE_UNSET_SSL(temp); + ServerRealm_set_realmType(scRealmsTable[i], temp); } if (!TYPE_IS_ZLIB(mode)) { - TYPE_UNSET_ZLIB(config.realmtable[i].type); + temp = ServerRealm_get_realmType(scRealmsTable[i]); + TYPE_UNSET_ZLIB(temp); + ServerRealm_set_realmType(scRealmsTable[i], temp); } /* using user's baseport value*/ - if (config.realmtable[i].baseport == 0) { - config.realmtable[i].baseport = baseport; + if (ServerRealm_get_basePortOn(scRealmsTable[i]) == 0) { + ServerRealm_set_basePortOn(scRealmsTable[i], baseport); } /* using user's audit value*/ - if (config.realmtable[i].audit == 0) { - config.realmtable[i].audit = audit; + if (ServerRealm_get_auditOn(scRealmsTable[i]) == 0) { + ServerRealm_set_auditOn(scRealmsTable[i], audit); } #ifdef HAVE_LIBPTHREAD /* using user's tunneltype value*/ - if (config.realmtable[i].tunneltype == 0) { + if (ServerRealm_get_tunnelType(scRealmsTable[i]) == 0) { if (tunneltype == -1) { aflog(LOG_T_INIT, LOG_I_CRIT, "Conflicting types of tunnel type... exiting"); exit(1); } - config.realmtable[i].tunneltype = tunneltype; + ServerRealm_set_tunnelType(scRealmsTable[i], tunneltype); } #endif /* using user's dnslookups value*/ - if (config.realmtable[i].dnslookups == 0) { - config.realmtable[i].dnslookups = dnslookups; + if (ServerRealm_get_dnsLookupsOn(scRealmsTable[i]) == 0) { + ServerRealm_set_dnsLookupsOn(scRealmsTable[i], dnslookups); } /* checking users amount */ - set_value(&(config.realmtable[i].users), amount, "5"); - check_value(&(config.realmtable[i].usernum), config.realmtable[i].users, "Invalid users amount"); + stemp = ServerRealm_get_sUsersLimit(scRealmsTable[i]); + set_value(&stemp, amount, "5"); + ServerRealm_set_sUsersLimit(scRealmsTable[i], stemp); + ServerRealm_set_usersLimit(scRealmsTable[i], + check_value(ServerRealm_get_sUsersLimit(scRealmsTable[i]), "Invalid users amount")); /* checking clients amount */ - set_value(&(config.realmtable[i].clients), clients, "1"); - check_value(&(config.realmtable[i].clinum), config.realmtable[i].clients, "Invalid clients amount"); + stemp = ServerRealm_get_sClientsLimit(scRealmsTable[i]); + set_value(&stemp, clients, "1"); + ServerRealm_set_sClientsLimit(scRealmsTable[i], stemp); + ServerRealm_set_clientsLimit(scRealmsTable[i], + check_value(ServerRealm_get_sClientsLimit(scRealmsTable[i]), "Invalid clients amount")); /* checking raclients amount */ - set_value(&(config.realmtable[i].raclients), raclients, "1"); - check_value(&(config.realmtable[i].raclinum), config.realmtable[i].raclients, "Invalid raclients amount"); + stemp = ServerRealm_get_sRaClientsLimit(scRealmsTable[i]); + set_value(&stemp, raclients, "1"); + ServerRealm_set_sRaClientsLimit(scRealmsTable[i], stemp); + ServerRealm_set_raClientsLimit(scRealmsTable[i], + check_value(ServerRealm_get_sRaClientsLimit(scRealmsTable[i]), "Invalid raclients amount")); /* checking usrpcli value */ - set_value(&(config.realmtable[i].usrpcli), usrpcli, config.realmtable[i].users); - check_value(&(config.realmtable[i].upcnum), config.realmtable[i].usrpcli, "Invalid usrpcli value"); + stemp = ServerRealm_get_sUsersPerClient(scRealmsTable[i]); + set_value(&stemp, usrpcli, ServerRealm_get_sUsersLimit(scRealmsTable[i])); + ServerRealm_set_sUsersPerClient(scRealmsTable[i], stemp); + ServerRealm_set_usersPerClient(scRealmsTable[i], + check_value(ServerRealm_get_sUsersPerClient(scRealmsTable[i]), "Invalid usrpcli value")); /* checking timeout value */ - set_value(&(config.realmtable[i].timeout), timeout, "5"); - check_value(&(config.realmtable[i].tmout), config.realmtable[i].timeout, "Invalid timeout value"); + stemp = ServerRealm_get_sTimeout(scRealmsTable[i]); + set_value(&stemp, timeout, "5"); + ServerRealm_set_sTimeout(scRealmsTable[i], stemp); + ServerRealm_set_timeout(scRealmsTable[i], + check_value(ServerRealm_get_sTimeout(scRealmsTable[i]), "Invalid timeout value")); /* checking climode value */ - set_value(&(config.realmtable[i].clim), clim, "1"); - check_value(&(config.realmtable[i].climode), config.realmtable[i].clim, "Invalid climode value"); + stemp = ServerRealm_get_sClientMode(scRealmsTable[i]); + set_value(&stemp, clim, "1"); + ServerRealm_set_sClientMode(scRealmsTable[i], stemp); + ServerRealm_set_clientMode(scRealmsTable[i], + check_value(ServerRealm_get_sClientMode(scRealmsTable[i]), "Invalid climode value")); /* allocating memory*/ - config.realmtable[i].contable = calloc(config.realmtable[i].usernum, sizeof(ConnectUser)); - if (config.realmtable[i].contable == NULL) { + srUsersTable = calloc(ServerRealm_get_usersLimit(scRealmsTable[i]), sizeof(ConnectUser*)); + if (srUsersTable == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - try define smaller amount of users"); exit(1); } - for (j = 0; j < config.realmtable[i].usernum; ++j) { - config.realmtable[i].contable[j] = ConnectUser_new(); - if (config.realmtable[i].contable[j] == NULL) { + ServerRealm_set_usersTable(scRealmsTable[i], srUsersTable); + for (j = 0; j < ServerRealm_get_usersLimit(scRealmsTable[i]); ++j) { + srUsersTable[j] = ConnectUser_new(); + if (srUsersTable[j] == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - try define smaller amount of users"); exit(1); } } - config.realmtable[i].clitable = calloc( config.realmtable[i].clinum, sizeof(ConnectClient)); - if (config.realmtable[i].clitable == NULL) { + srClientsTable = calloc(ServerRealm_get_clientsLimit(scRealmsTable[i]), sizeof(ConnectClient*)); + if (srClientsTable == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - try define smaller amount of clients"); exit(1); } - for (j = 0; j < config.realmtable[i].clinum; ++j) { - config.realmtable[i].clitable[j] = ConnectClient_new(); - if (config.realmtable[i].clitable[j] == NULL) { + ServerRealm_set_clientsTable(scRealmsTable[i], srClientsTable); + for (j = 0; j < ServerRealm_get_clientsLimit(scRealmsTable[i]); ++j) { + srClientsTable[j] = ConnectClient_new(); + if (srClientsTable[j] == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - try define smaller amount of clients"); exit(1); } } - config.realmtable[i].raclitable = calloc( config.realmtable[i].raclinum, sizeof(ConnectClient)); - if (config.realmtable[i].raclitable == NULL) { + srRaClientsTable = calloc(ServerRealm_get_raClientsLimit(scRealmsTable[i]), sizeof(ConnectClient*)); + if (srRaClientsTable == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - try define smaller amount of raclients"); exit(1); } - for (j = 0; j < config.realmtable[i].raclinum; ++j) { - config.realmtable[i].raclitable[j] = ConnectClient_new(); - if (config.realmtable[i].raclitable[j] == NULL) { + ServerRealm_set_raClientsTable(scRealmsTable[i], srRaClientsTable); + for (j = 0; j < ServerRealm_get_raClientsLimit(scRealmsTable[i]); ++j) { + srRaClientsTable[j] = ConnectClient_new(); + if (srRaClientsTable[j] == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - try define smaller amount of raclients"); exit(1); @@ -633,20 +716,20 @@ main(int argc, char **argv) } ipfam = 0x01; #ifdef AF_INET6 - if (TYPE_IS_IPV4(config.realmtable[i].type)) { + if (TYPE_IS_IPV4(ServerRealm_get_realmType(scRealmsTable[i]))) { ipfam |= 0x02; } - else if (TYPE_IS_IPV6(config.realmtable[i].type)) { + else if (TYPE_IS_IPV6(ServerRealm_get_realmType(scRealmsTable[i]))) { ipfam |= 0x04; } #endif - if (config.realmtable[i].baseport == 0) { - for (j = 0; j < config.realmtable[i].usrclinum; ++j) { - if (ip_listen(&temp, UsrCli_get_listenHostName(config.realmtable[i].usrclitable[j]) ? - UsrCli_get_listenHostName(config.realmtable[i].usrclitable[j]) : - config.realmtable[i].hostname, - UsrCli_get_listenPortName(config.realmtable[i].usrclitable[j]), - (&(config.realmtable[i].addrlen)), ipfam)) { + if (ServerRealm_get_basePortOn(scRealmsTable[i]) == 0) { + for (j = 0; j < ServerRealm_get_userClientPairs(scRealmsTable[i]); ++j) { + if (ip_listen(&temp, UsrCli_get_listenHostName(srUsersClientsTable[j]) ? + UsrCli_get_listenHostName(srUsersClientsTable[j]) : + ServerRealm_get_hostName(scRealmsTable[i]), + UsrCli_get_listenPortName(srUsersClientsTable[j]), + (&len), ipfam)) { aflog(LOG_T_INIT, LOG_I_CRIT, #ifdef AF_INET6 "tcp_listen_%s error for %s, %s", @@ -654,25 +737,27 @@ main(int argc, char **argv) #else "tcp_listen error for %s, %s", #endif - UsrCli_get_listenHostName(config.realmtable[i].usrclitable[j]) ? - UsrCli_get_listenHostName(config.realmtable[i].usrclitable[j]) : - config.realmtable[i].hostname, - UsrCli_get_listenPortName(config.realmtable[i].usrclitable[j])); + UsrCli_get_listenHostName(srUsersClientsTable[j]) ? + UsrCli_get_listenHostName(srUsersClientsTable[j]) : + ServerRealm_get_hostName(scRealmsTable[i]), + UsrCli_get_listenPortName(srUsersClientsTable[j])); exit(1); } - UsrCli_set_listenFd(config.realmtable[i].usrclitable[j], temp); - flags = fcntl(UsrCli_get_listenFd(config.realmtable[i].usrclitable[j]), F_GETFL, 0); - fcntl(UsrCli_get_listenFd(config.realmtable[i].usrclitable[j]), F_SETFL, flags | O_NONBLOCK); + ServerRealm_set_addressLength(scRealmsTable[i], len); + UsrCli_set_listenFd(srUsersClientsTable[j], temp); + flags = fcntl(UsrCli_get_listenFd(srUsersClientsTable[j]), F_GETFL, 0); + fcntl(UsrCli_get_listenFd(srUsersClientsTable[j]), F_SETFL, flags | O_NONBLOCK); } } - for (j = 0; j < config.realmtable[i].usrclinum; ++j) { - switch (config.realmtable[i].tunneltype) { + + for (j = 0; j < ServerRealm_get_userClientPairs(scRealmsTable[i]); ++j) { + switch (ServerRealm_get_tunnelType(scRealmsTable[i])) { case 0: { - if (ip_listen(&temp, UsrCli_get_manageHostName(config.realmtable[i].usrclitable[j]) ? - UsrCli_get_manageHostName(config.realmtable[i].usrclitable[j]) : - config.realmtable[i].hostname, - UsrCli_get_managePortName(config.realmtable[i].usrclitable[j]), - (&(config.realmtable[i].addrlen)), ipfam)) { + if (ip_listen(&temp, UsrCli_get_manageHostName(srUsersClientsTable[j]) ? + UsrCli_get_manageHostName(srUsersClientsTable[j]) : + ServerRealm_get_hostName(scRealmsTable[i]), + UsrCli_get_managePortName(srUsersClientsTable[j]), + (&len), ipfam)) { aflog(LOG_T_INIT, LOG_I_CRIT, #ifdef AF_INET6 "tcp_listen_%s error for %s, %s", @@ -680,46 +765,49 @@ main(int argc, char **argv) #else "tcp_listen error for %s, %s", #endif - UsrCli_get_manageHostName(config.realmtable[i].usrclitable[j]) ? - UsrCli_get_manageHostName(config.realmtable[i].usrclitable[j]) : - config.realmtable[i].hostname, - UsrCli_get_managePortName(config.realmtable[i].usrclitable[j])); + UsrCli_get_manageHostName(srUsersClientsTable[j]) ? + UsrCli_get_manageHostName(srUsersClientsTable[j]) : + ServerRealm_get_hostName(scRealmsTable[i]), + UsrCli_get_managePortName(srUsersClientsTable[j])); exit(1); } - UsrCli_set_manageFd(config.realmtable[i].usrclitable[j], temp); - flags = fcntl(UsrCli_get_manageFd(config.realmtable[i].usrclitable[j]), F_GETFL, 0); - fcntl(UsrCli_get_manageFd(config.realmtable[i].usrclitable[j]), F_SETFL, flags | O_NONBLOCK); + ServerRealm_set_addressLength(scRealmsTable[i], len); + UsrCli_set_manageFd(srUsersClientsTable[j], temp); + flags = fcntl(UsrCli_get_manageFd(srUsersClientsTable[j]), F_GETFL, 0); + fcntl(UsrCli_get_manageFd(srUsersClientsTable[j]), F_SETFL, flags | O_NONBLOCK); break; } #ifdef HAVE_LIBPTHREAD case 1: case 2: { if (initialize_http_proxy_server(&temp, - UsrCli_get_manageHostName(config.realmtable[i].usrclitable[j]) ? - UsrCli_get_manageHostName(config.realmtable[i].usrclitable[j]) : - config.realmtable[i].hostname, - UsrCli_get_managePortName(config.realmtable[i].usrclitable[j]), - (&(config.realmtable[i].addrlen)), ipfam, - config.realmtable[i].clinum + config.realmtable[i].raclinum, - (config.realmtable[i].tunneltype - 1), + UsrCli_get_manageHostName(srUsersClientsTable[j]) ? + UsrCli_get_manageHostName(srUsersClientsTable[j]) : + ServerRealm_get_hostName(scRealmsTable[i]), + UsrCli_get_managePortName(srUsersClientsTable[j]), + (&len), ipfam, + ServerRealm_get_clientsLimit(scRealmsTable[i]) + + ServerRealm_get_raClientsLimit(scRealmsTable[i]), + (ServerRealm_get_tunnelType(scRealmsTable[i]) - 1), ctx)) { aflog(LOG_T_INIT, LOG_I_CRIT, #ifdef AF_INET6 "http%s_proxy_listen_%s error for %s, %s", - (config.realmtable[i].tunneltype == 2) ? "s" : "", + (ServerRealm_get_tunnelType(scRealmsTable[i]) == 2) ? "s" : "", (ipfam & 0x02)?"ipv4":(ipfam & 0x04)?"ipv6":"unspec", #else "http%s_proxy_listen error for %s, %s", - (config.realmtable[i].tunneltype == 2) ? "s" : "", + (ServerRealm_get_tunnelType(scRealmsTable[i]) == 2) ? "s" : "", #endif - UsrCli_get_manageHostName(config.realmtable[i].usrclitable[j]) ? - UsrCli_get_manageHostName(config.realmtable[i].usrclitable[j]) : - config.realmtable[i].hostname, - UsrCli_get_managePortName(config.realmtable[i].usrclitable[j])); + UsrCli_get_manageHostName(srUsersClientsTable[j]) ? + UsrCli_get_manageHostName(srUsersClientsTable[j]) : + ServerRealm_get_hostName(scRealmsTable[i]), + UsrCli_get_managePortName(srUsersClientsTable[j])); exit(1); } - UsrCli_set_manageFd(config.realmtable[i].usrclitable[j], temp); - flags = fcntl(UsrCli_get_manageFd(config.realmtable[i].usrclitable[j]), F_GETFL, 0); - fcntl(UsrCli_get_manageFd(config.realmtable[i].usrclitable[j]), F_SETFL, flags | O_NONBLOCK); + ServerRealm_set_addressLength(scRealmsTable[i], len); + UsrCli_set_manageFd(srUsersClientsTable[j], temp); + flags = fcntl(UsrCli_get_manageFd(srUsersClientsTable[j]), F_GETFL, 0); + fcntl(UsrCli_get_manageFd(srUsersClientsTable[j]), F_SETFL, flags | O_NONBLOCK); break; } #endif @@ -731,58 +819,66 @@ main(int argc, char **argv) } } } - config.realmtable[i].cliaddr = malloc(config.realmtable[i].addrlen); + + ServerRealm_set_clientAddress(scRealmsTable[i], malloc(ServerRealm_get_addressLength(scRealmsTable[i]))); + if (ServerRealm_get_clientAddress(scRealmsTable[i]) == NULL) { + aflog(LOG_T_INIT, LOG_I_CRIT, + "Allocating memory for client addresses failed... exiting"); + exit(1); + } - for (j=0; j<config.realmtable[i].clinum; ++j) { - SslFd_set_ssl(ConnectClient_get_sslFd(config.realmtable[i].clitable[j]), SSL_new(ctx)); - if (SslFd_get_ssl(ConnectClient_get_sslFd(config.realmtable[i].clitable[j])) == NULL) { + for (j = 0; j < ServerRealm_get_clientsLimit(scRealmsTable[i]); ++j) { + SslFd_set_ssl(ConnectClient_get_sslFd(srClientsTable[j]), SSL_new(ctx)); + if (SslFd_get_ssl(ConnectClient_get_sslFd(srClientsTable[j])) == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, - "Creating of ssl object failed... exiting"); + "Creation of ssl object failed... exiting"); exit(1); } } - for (j=0; j<config.realmtable[i].raclinum; ++j) { - SslFd_set_ssl(ConnectClient_get_sslFd(config.realmtable[i].raclitable[j]), SSL_new(ctx)); - if (SslFd_get_ssl(ConnectClient_get_sslFd(config.realmtable[i].raclitable[j])) == NULL) { + for (j = 0; j < ServerRealm_get_raClientsLimit(scRealmsTable[i]); ++j) { + SslFd_set_ssl(ConnectClient_get_sslFd(srRaClientsTable[j]), SSL_new(ctx)); + if (SslFd_get_ssl(ConnectClient_get_sslFd(srRaClientsTable[j])) == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, - "Creating of ssl object failed... exiting"); + "Creation of ssl object failed... exiting"); exit(1); } } - for (j = 0; j < config.realmtable[i].usrclinum; ++j) { - FD_SET(UsrCli_get_manageFd(config.realmtable[i].usrclitable[j]), &allset); - maxfdp1 = (maxfdp1 > (UsrCli_get_manageFd(config.realmtable[i].usrclitable[j]) + 1)) ? - maxfdp1 : (UsrCli_get_manageFd(config.realmtable[i].usrclitable[j]) + 1); + for (j = 0; j < ServerRealm_get_userClientPairs(scRealmsTable[i]); ++j) { + FD_SET(UsrCli_get_manageFd(srUsersClientsTable[j]), &allset); + maxfdp1 = (maxfdp1 > (UsrCli_get_manageFd(srUsersClientsTable[j]) + 1)) ? + maxfdp1 : (UsrCli_get_manageFd(srUsersClientsTable[j]) + 1); } - if (config.realmtable[i].baseport == 0) { - for (j = 0; j < config.realmtable[i].usrclinum; ++j) { - FD_SET(UsrCli_get_listenFd(config.realmtable[i].usrclitable[j]), &allset); - maxfdp1 = (maxfdp1 > (UsrCli_get_listenFd(config.realmtable[i].usrclitable[j]) + 1)) ? - maxfdp1 : (UsrCli_get_listenFd(config.realmtable[i].usrclitable[j]) + 1); + if (ServerRealm_get_basePortOn(scRealmsTable[i]) == 0) { + for (j = 0; j < ServerRealm_get_userClientPairs(scRealmsTable[i]); ++j) { + FD_SET(UsrCli_get_listenFd(srUsersClientsTable[j]), &allset); + maxfdp1 = (maxfdp1 > (UsrCli_get_listenFd(srUsersClientsTable[j]) + 1)) ? + maxfdp1 : (UsrCli_get_listenFd(srUsersClientsTable[j]) + 1); } } - config.realmtable[i].usercon = 0; - config.realmtable[i].clicon = 0; - config.realmtable[i].raclicon = 0; - for (j=0; j<config.realmtable[i].clinum; ++j) { - ConnectClient_set_timer(config.realmtable[i].clitable[j], timeval_create(config.realmtable[i].tmout, 0)); - ConnectClient_set_limit(config.realmtable[i].clitable[j], config.realmtable[i].upcnum); - if (ConnectClient_create_users(config.realmtable[i].clitable[j])) { + ServerRealm_set_connectedUsers(scRealmsTable[i], 0); + ServerRealm_set_connectedClients(scRealmsTable[i], 0); + ServerRealm_set_connectedRaClients(scRealmsTable[i], 0); + for (j = 0; j < ServerRealm_get_clientsLimit(scRealmsTable[i]); ++j) { + ConnectClient_set_timer(srClientsTable[j], timeval_create(ServerRealm_get_timeout(scRealmsTable[i]), 0)); + ConnectClient_set_limit(srClientsTable[j], ServerRealm_get_usersPerClient(scRealmsTable[i])); + if (ConnectClient_create_users(srClientsTable[j])) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - try define smaller amount of usrpcli (or users)"); exit(1); } } - for (j=0; j<config.realmtable[i].raclinum; ++j) { - ConnectClient_set_timer(config.realmtable[i].raclitable[j], timeval_create(config.realmtable[i].tmout,0)); + for (j = 0; j < ServerRealm_get_raClientsLimit(scRealmsTable[i]); ++j) { + ConnectClient_set_timer(srRaClientsTable[j], + timeval_create(ServerRealm_get_timeout(scRealmsTable[i]), 0)); } } aflog(LOG_T_MAIN, LOG_I_INFO, - "SERVER STARTED realms: %d", config.size); - time(&config.starttime); + "SERVER STARTED realms: %d", ServerConfiguration_get_realmsNumber(config)); + time(&now); + ServerConfiguration_set_startTime(config, now); for ( ; ; ) { rset = allset; @@ -792,22 +888,24 @@ main(int argc, char **argv) if (manconnecting) { /* find out, in what realm client is trying to connect */ l = -1; - for (k = 0; k < config.size; ++k) { - for (j=0; j < config.realmtable[k].clinum; ++j) { - if ((ConnectClient_get_state(config.realmtable[k].clitable[j]) == CONNECTCLIENT_STATE_CONNECTING) || - (ConnectClient_get_state(config.realmtable[k].clitable[j]) == CONNECTCLIENT_STATE_AUTHORIZING)) { + for (k = 0; k < ServerConfiguration_get_realmsNumber(config); ++k) { + srClientsTable = ServerRealm_get_clientsTable(scRealmsTable[k]); + for (j=0; j < ServerRealm_get_clientsLimit(scRealmsTable[k]); ++j) { + if ((ConnectClient_get_state(srClientsTable[j]) == CONNECTCLIENT_STATE_CONNECTING) || + (ConnectClient_get_state(srClientsTable[j]) == CONNECTCLIENT_STATE_AUTHORIZING)) { i = k; - k = config.size; + k = ServerConfiguration_get_realmsNumber(config); l = 0; break; /* so i points to first good realm and j to good client */ } } if (l == -1) { - for (j=0; j < config.realmtable[k].raclinum; ++j) { - if ((ConnectClient_get_state(config.realmtable[k].raclitable[j])==CONNECTCLIENT_STATE_CONNECTING) || - (ConnectClient_get_state(config.realmtable[k].raclitable[j])==CONNECTCLIENT_STATE_AUTHORIZING)) { + srRaClientsTable = ServerRealm_get_raClientsTable(scRealmsTable[k]); + for (j=0; j < ServerRealm_get_raClientsLimit(scRealmsTable[k]); ++j) { + if ((ConnectClient_get_state(srRaClientsTable[j])==CONNECTCLIENT_STATE_CONNECTING) || + (ConnectClient_get_state(srRaClientsTable[j])==CONNECTCLIENT_STATE_AUTHORIZING)) { i = k; - k = config.size; + k = ServerConfiguration_get_realmsNumber(config); l = 1; break; /* so i points to first good realm and j to good client */ } @@ -815,29 +913,31 @@ main(int argc, char **argv) } } if (!l) { - if (select(maxfdp1,&rset,&tmpset,NULL,ConnectClient_get_timerp(config.realmtable[i].clitable[j])) == 0) { - close(SslFd_get_fd(ConnectClient_get_sslFd(config.realmtable[i].clitable[j]))); - FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(config.realmtable[i].clitable[j])), &allset); - SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(config.realmtable[i].clitable[j]))); - ConnectClient_set_state(config.realmtable[i].clitable[j], CONNECTCLIENT_STATE_FREE); + srClientsTable = ServerRealm_get_clientsTable(scRealmsTable[i]); + if (select(maxfdp1,&rset,&tmpset,NULL,ConnectClient_get_timerp(srClientsTable[j])) == 0) { + close(SslFd_get_fd(ConnectClient_get_sslFd(srClientsTable[j]))); + FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(srClientsTable[j])), &allset); + SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(srClientsTable[j]))); + ConnectClient_set_state(srClientsTable[j], CONNECTCLIENT_STATE_FREE); manconnecting--; - config.realmtable[i].clicon--; + ServerRealm_decrease_connectedClients(scRealmsTable[i]); aflog(LOG_T_CLIENT, LOG_I_WARNING, "realm[%s]: Client[%s]: SSL_accept failed (timeout)", - get_realmname(&config, i), get_clientname(pointer, j)); + get_realmname(config, i), get_clientname(scRealmsTable[i], j)); } } else { - if (select(maxfdp1,&rset,&tmpset,NULL,ConnectClient_get_timerp(config.realmtable[i].raclitable[j]))==0) { - close(SslFd_get_fd(ConnectClient_get_sslFd(config.realmtable[i].raclitable[j]))); - FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(config.realmtable[i].raclitable[j])), &allset); - SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(config.realmtable[i].raclitable[j]))); - ConnectClient_set_state(config.realmtable[i].raclitable[j], CONNECTCLIENT_STATE_FREE); + srRaClientsTable = ServerRealm_get_raClientsTable(scRealmsTable[i]); + if (select(maxfdp1,&rset,&tmpset,NULL,ConnectClient_get_timerp(srRaClientsTable[j]))==0) { + close(SslFd_get_fd(ConnectClient_get_sslFd(srRaClientsTable[j]))); + FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(srRaClientsTable[j])), &allset); + SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(srRaClientsTable[j]))); + ConnectClient_set_state(srRaClientsTable[j], CONNECTCLIENT_STATE_FREE); manconnecting--; - config.realmtable[i].clicon--; + ServerRealm_decrease_connectedClients(scRealmsTable[i]); aflog(LOG_T_CLIENT, LOG_I_WARNING, "realm[%s]: Client[%s] (ra): SSL_accept failed (timeout)", - get_realmname(&config, i), get_raclientname(pointer, j)); + get_realmname(config, i), get_raclientname(scRealmsTable[i], j)); } } } @@ -847,35 +947,39 @@ main(int argc, char **argv) aflog(LOG_T_MAIN, LOG_I_DDEBUG, "after select..."); - for (j = 0; j < config.size; ++j) { - pointer = (&(config.realmtable[j])); - for (i = 0; i <pointer->usernum; ++i) { - if ((ConnectUser_get_state(pointer->contable[i]) == S_STATE_OPEN) || - (ConnectUser_get_state(pointer->contable[i]) == S_STATE_STOPPED)) - if (FD_ISSET(ConnectUser_get_connFd(pointer->contable[i]), &rset)) { - k = eval_usernum(pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])], i); + for (j = 0; j < ServerConfiguration_get_realmsNumber(config); ++j) { + pointer = scRealmsTable[j]; + srUsersTable = ServerRealm_get_usersTable(pointer); + srClientsTable = ServerRealm_get_clientsTable(pointer); + srRaClientsTable = ServerRealm_get_raClientsTable(pointer); + srUsersClientsTable = ServerRealm_get_usersClientsTable(pointer); + for (i = 0; i < ServerRealm_get_usersLimit(pointer); ++i) { + if ((ConnectUser_get_state(srUsersTable[i]) == S_STATE_OPEN) || + (ConnectUser_get_state(srUsersTable[i]) == S_STATE_STOPPED)) + if (FD_ISSET(ConnectUser_get_connFd(srUsersTable[i]), &rset)) { + k = eval_usernum(srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])], i); aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: Client[%s]: user[%d]: FD_ISSET", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: user[%d]: FD_ISSET", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer,i)); - if (TYPE_IS_TCP(pointer->type)) { /* forwarding tcp packets */ - n = read(ConnectUser_get_connFd(pointer->contable[i]), &buff[5], 8091); + if (TYPE_IS_TCP(ServerRealm_get_realmType(pointer))) { /* forwarding tcp packets */ + n = read(ConnectUser_get_connFd(srUsersTable[i]), &buff[5], 8091); if (n == -1) { if (errno == EAGAIN) { continue; } aflog(LOG_T_USER, LOG_I_ERR, - "realm[%s]: Client[%s]: user[%d]: READ ERROR (%d)", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: user[%d]: READ ERROR (%d)", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i), errno); n = 0; } if (n) { aflog(LOG_T_USER, LOG_I_DEBUG, - "realm[%s]: Client[%s]: FROM user[%d]: MESSAGE length=%d", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: FROM user[%d]: MESSAGE length=%d", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i), n); - UserStats_add_upload(ConnectUser_get_stats(pointer->contable[i]), n); + UserStats_add_upload(ConnectUser_get_stats(srUsersTable[i]), n); if ((buff[5] == AF_S_MESSAGE) && (buff[6] == AF_S_LOGIN) && (buff[7] == AF_S_MESSAGE)) { aflog(LOG_T_USER, LOG_I_WARNING, "WARNING: got packet similiar to udp"); @@ -885,53 +989,53 @@ main(int argc, char **argv) buff[2] = k; /* low bits of user number */ buff[3] = n >> 8; /* high bits of message length */ buff[4] = n; /* low bits of message length */ - SslFd_send_message(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])]), + srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])]), buff, n+5); } else { aflog(LOG_T_USER, LOG_I_INFO, - "realm[%s]: Client[%s]: user[%d]: CLOSED", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: user[%d]: CLOSED", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i)); time(&now); aflog(LOG_T_USER, LOG_I_NOTICE, "REALM: %s CLIENT: %s USER: %d IP: %s PORT: %s DURATION: %s", - get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i), - ConnectUser_get_nameBuf(pointer->contable[i]), - ConnectUser_get_portBuf(pointer->contable[i]), - timeperiod(now - ConnectUser_get_connectTime(pointer->contable[i]))); - if (pointer->audit) { + ConnectUser_get_nameBuf(srUsersTable[i]), + ConnectUser_get_portBuf(srUsersTable[i]), + timeperiod(now - ConnectUser_get_connectTime(srUsersTable[i]))); + if (ServerRealm_get_auditOn(pointer)) { AuditList_insert_back( ConnectClient_get_auditList( - pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])]), + srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])]), AuditListNode_new_entry( get_username(pointer, i), - ConnectUser_get_nameBuf(pointer->contable[i]), - ConnectUser_get_portBuf(pointer->contable[i]), - ConnectUser_get_connectTime(pointer->contable[i]), - now - ConnectUser_get_connectTime(pointer->contable[i])) + ConnectUser_get_nameBuf(srUsersTable[i]), + ConnectUser_get_portBuf(srUsersTable[i]), + ConnectUser_get_connectTime(srUsersTable[i]), + now - ConnectUser_get_connectTime(srUsersTable[i])) ); } - close(ConnectUser_get_connFd(pointer->contable[i])); - FD_CLR(ConnectUser_get_connFd(pointer->contable[i]), &allset); - FD_CLR(ConnectUser_get_connFd(pointer->contable[i]), &wset); - ConnectUser_set_state(pointer->contable[i], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(pointer->contable[i])); + close(ConnectUser_get_connFd(srUsersTable[i])); + FD_CLR(ConnectUser_get_connFd(srUsersTable[i]), &allset); + FD_CLR(ConnectUser_get_connFd(srUsersTable[i]), &wset); + ConnectUser_set_state(srUsersTable[i], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(srUsersTable[i])); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = k >> 8; /* high bits of user number */ buff[2] = k; /* low bits of user number */ - SslFd_send_message(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])]), + srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])]), buff, 5); } } else { /* when forwarding udp packets */ - n = readn(ConnectUser_get_connFd(pointer->contable[i]), buff, 5 ); + n = readn(ConnectUser_get_connFd(srUsersTable[i]), buff, 5 ); if (n != 5) { n = 0; } @@ -940,17 +1044,17 @@ main(int argc, char **argv) length = buff[3]; length = length << 8; length += buff[4]; /* this is length of message */ - if ((n = readn(ConnectUser_get_connFd(pointer->contable[i]), &buff[5], length)) != 0) { + if ((n = readn(ConnectUser_get_connFd(srUsersTable[i]), &buff[5], length)) != 0) { aflog(LOG_T_USER, LOG_I_DEBUG, "realm[%s]: Client[%s]: FROM user[%d]: MESSAGE length=%d", - get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i), n); buff[1] = k >> 8; /* high bits of user number */ buff[2] = k; /* low bits of user number */ - SslFd_send_message(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])]), + srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])]), buff, n+5); } } @@ -961,29 +1065,29 @@ main(int argc, char **argv) if (n == 0) { aflog(LOG_T_USER, LOG_I_INFO, - "realm[%s]: Client[%s]: user[%d]: CLOSED (udp mode)", get_realmname(&config, j), + "realm[%s]: Client[%s]: user[%d]: CLOSED (udp mode)", get_realmname(config, j), get_clientname(pointer, - ConnectUser_get_whatClient(pointer->contable[i])), get_username(pointer, i)); + ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i)); time(&now); aflog(LOG_T_USER, LOG_I_NOTICE, "REALM: %s CLIENT: %s USER: %d IP: %s PORT: %s DURATION: %s", - get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i), - ConnectUser_get_nameBuf(pointer->contable[i]), - ConnectUser_get_portBuf(pointer->contable[i]), - timeperiod(now - ConnectUser_get_connectTime(pointer->contable[i]))); - close(ConnectUser_get_connFd(pointer->contable[i])); - FD_CLR(ConnectUser_get_connFd(pointer->contable[i]), &allset); - FD_CLR(ConnectUser_get_connFd(pointer->contable[i]), &wset); - ConnectUser_set_state(pointer->contable[i], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(pointer->contable[i])); + ConnectUser_get_nameBuf(srUsersTable[i]), + ConnectUser_get_portBuf(srUsersTable[i]), + timeperiod(now - ConnectUser_get_connectTime(srUsersTable[i]))); + close(ConnectUser_get_connFd(srUsersTable[i])); + FD_CLR(ConnectUser_get_connFd(srUsersTable[i]), &allset); + FD_CLR(ConnectUser_get_connFd(srUsersTable[i]), &wset); + ConnectUser_set_state(srUsersTable[i], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(srUsersTable[i])); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = k >> 8; /* high bits of user number */ buff[2] = k; /* low bits of user number */ - SslFd_send_message(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])]), + srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])]), buff, 5); } @@ -991,149 +1095,149 @@ main(int argc, char **argv) } } /* ------------------------------------ */ - for (i = 0; i <pointer->usernum; ++i) { - if (ConnectUser_get_state(pointer->contable[i]) == S_STATE_STOPPED) - if (FD_ISSET(ConnectUser_get_connFd(pointer->contable[i]), &tmpset)) { - k = eval_usernum(pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])], i); + for (i = 0; i < ServerRealm_get_usersLimit(pointer); ++i) { + if (ConnectUser_get_state(srUsersTable[i]) == S_STATE_STOPPED) + if (FD_ISSET(ConnectUser_get_connFd(srUsersTable[i]), &tmpset)) { + k = eval_usernum(srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])], i); aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: Client[%s]: user[%d]: FD_ISSET - WRITE", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: user[%d]: FD_ISSET - WRITE", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i)); - n = BufListNode_readMessageLength(BufList_get_first(ConnectUser_get_bufList(pointer->contable[i]))); - sent = write(ConnectUser_get_connFd(pointer->contable[i]), - BufListNode_readMessage(BufList_get_first(ConnectUser_get_bufList(pointer->contable[i]))), n); + n = BufListNode_readMessageLength(BufList_get_first(ConnectUser_get_bufList(srUsersTable[i]))); + sent = write(ConnectUser_get_connFd(srUsersTable[i]), + BufListNode_readMessage(BufList_get_first(ConnectUser_get_bufList(srUsersTable[i]))), n); if ((sent > 0) && (sent != n)) { - BufListNode_set_actPtr(BufList_get_first(ConnectUser_get_bufList(pointer->contable[i])), - BufListNode_get_actPtr(BufList_get_first(ConnectUser_get_bufList(pointer->contable[i]))) + sent); + BufListNode_set_actPtr(BufList_get_first(ConnectUser_get_bufList(srUsersTable[i])), + BufListNode_get_actPtr(BufList_get_first(ConnectUser_get_bufList(srUsersTable[i]))) + sent); aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: Client[%s]: user[%d]: (%d/%d)", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: user[%d]: (%d/%d)", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i), sent, n); } else if ((sent == -1) && (errno == EAGAIN)) { aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: Client[%s]: user[%d]: EAGAIN", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: user[%d]: EAGAIN", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i)); } else if (sent == -1) { aflog(LOG_T_USER, LOG_I_INFO, - "realm[%s]: Client[%s]: user[%d]: CLOSED", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: user[%d]: CLOSED", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i)); time(&now); aflog(LOG_T_USER, LOG_I_NOTICE, "REALM: %s CLIENT: %s USER: %d IP: %s PORT: %s DURATION: %s", - get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i), - ConnectUser_get_nameBuf(pointer->contable[i]), - ConnectUser_get_portBuf(pointer->contable[i]), - timeperiod(now - ConnectUser_get_connectTime(pointer->contable[i]))); - close(ConnectUser_get_connFd(pointer->contable[i])); - FD_CLR(ConnectUser_get_connFd(pointer->contable[i]), &allset); - FD_CLR(ConnectUser_get_connFd(pointer->contable[i]), &wset); - ConnectUser_set_state(pointer->contable[i], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(pointer->contable[i])); + ConnectUser_get_nameBuf(srUsersTable[i]), + ConnectUser_get_portBuf(srUsersTable[i]), + timeperiod(now - ConnectUser_get_connectTime(srUsersTable[i]))); + close(ConnectUser_get_connFd(srUsersTable[i])); + FD_CLR(ConnectUser_get_connFd(srUsersTable[i]), &allset); + FD_CLR(ConnectUser_get_connFd(srUsersTable[i]), &wset); + ConnectUser_set_state(srUsersTable[i], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(srUsersTable[i])); buff[0] = AF_S_CONCLOSED; /* closing connection */ buff[1] = k >> 8; /* high bits of user number */ buff[2] = k; /* low bits of user number */ - SslFd_send_message(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])]), + srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])]), buff, 5); } else { aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: Client[%s]: user[%d]: (%d/%d)", get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + "realm[%s]: Client[%s]: user[%d]: (%d/%d)", get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i), sent, n); - BufList_delete_first(ConnectUser_get_bufList(pointer->contable[i])); - if (BufList_get_first(ConnectUser_get_bufList(pointer->contable[i])) == NULL) { - ConnectUser_set_state(pointer->contable[i], S_STATE_OPEN); - FD_CLR(ConnectUser_get_connFd(pointer->contable[i]), &wset); + BufList_delete_first(ConnectUser_get_bufList(srUsersTable[i])); + if (BufList_get_first(ConnectUser_get_bufList(srUsersTable[i])) == NULL) { + ConnectUser_set_state(srUsersTable[i], S_STATE_OPEN); + FD_CLR(ConnectUser_get_connFd(srUsersTable[i]), &wset); buff[0] = AF_S_CAN_SEND; /* stopping transfer */ buff[1] = k >> 8; /* high bits of user number */ buff[2] = k; /* low bits of user number */ aflog(LOG_T_USER, LOG_I_DDEBUG, "realm[%s]: Client[%s]: TO user[%d]: BUFFERING MESSAGE ENDED", - get_realmname(&config, j), - get_clientname(pointer, ConnectUser_get_whatClient(pointer->contable[i])), + get_realmname(config, j), + get_clientname(pointer, ConnectUser_get_whatClient(srUsersTable[i])), get_username(pointer, i)); - SslFd_send_message(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[ConnectUser_get_whatClient(pointer->contable[i])]), + srClientsTable[ConnectUser_get_whatClient(srUsersTable[i])]), buff, 5); } } } } /* ------------------------------------ */ - if (pointer->baseport == 0) { - for (l = 0; l < pointer->usrclinum; ++l) { - if (FD_ISSET(UsrCli_get_listenFd(pointer->usrclitable[l]), &rset)) { - len = pointer->addrlen; - sent = accept(UsrCli_get_listenFd(pointer->usrclitable[l]), pointer->cliaddr, &len); + if (ServerRealm_get_basePortOn(pointer) == 0) { + for (l = 0; l < ServerRealm_get_userClientPairs(pointer); ++l) { + if (FD_ISSET(UsrCli_get_listenFd(srUsersClientsTable[l]), &rset)) { + len = ServerRealm_get_addressLength(pointer); + sent = accept(UsrCli_get_listenFd(srUsersClientsTable[l]), ServerRealm_get_clientAddress(pointer), &len); if (sent == -1) { if (errno == EAGAIN) { aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: listenfd: FD_ISSET --> EAGAIN", get_realmname(&config, j)); + "realm[%s]: listenfd: FD_ISSET --> EAGAIN", get_realmname(config, j)); } else { aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: listenfd: FD_ISSET --> errno=%d", get_realmname(&config, j), errno); + "realm[%s]: listenfd: FD_ISSET --> errno=%d", get_realmname(config, j), errno); } continue; } flags = fcntl(sent, F_GETFL, 0); fcntl(sent, F_SETFL, flags | O_NONBLOCK); aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: listenfd: FD_ISSET", get_realmname(&config, j)); - k = find_client(pointer, pointer->climode, l); - if (ConnectClient_get_state(pointer->clitable[k]) == CONNECTCLIENT_STATE_ACCEPTED) { - if (pointer->usercon == pointer->usernum) { + "realm[%s]: listenfd: FD_ISSET", get_realmname(config, j)); + k = find_client(pointer, ServerRealm_get_clientMode(pointer), l); + if (ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_ACCEPTED) { + if (ServerRealm_get_connectedUsers(pointer) == ServerRealm_get_usersLimit(pointer)) { close(sent); aflog(LOG_T_USER, LOG_I_WARNING, - "realm[%s]: user limit EXCEEDED", get_realmname(&config, j)); + "realm[%s]: user limit EXCEEDED", get_realmname(config, j)); } - else if (ConnectClient_get_connected(pointer->clitable[k]) == - ConnectClient_get_limit(pointer->clitable[k])) { + else if (ConnectClient_get_connected(srClientsTable[k]) == + ConnectClient_get_limit(srClientsTable[k])) { close(sent); aflog(LOG_T_USER, LOG_I_WARNING, "realm[%s]: Client[%s]: usrpcli limit EXCEEDED", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); } else { - for (i = 0; i < pointer->usernum; ++i) { - if (ConnectUser_get_state(pointer->contable[i]) == S_STATE_CLEAR) { - ConnectUser_set_userId(pointer->contable[i], pointer->usercounter); - ++(pointer->usercounter); + for (i = 0; i < ServerRealm_get_usersLimit(pointer); ++i) { + if (ConnectUser_get_state(srUsersTable[i]) == S_STATE_CLEAR) { + ConnectUser_set_userId(srUsersTable[i], ServerRealm_get_usersCounter(pointer)); + ServerRealm_increase_usersCounter(pointer); aflog(LOG_T_USER, LOG_I_INFO, "realm[%s]: Client[%s]: new user: CONNECTING from IP: %s", - get_realmname(&config, j), get_clientname(pointer, k), - sock_ntop(pointer->cliaddr, len, ConnectUser_get_nameBuf(pointer->contable[i]), - ConnectUser_get_portBuf(pointer->contable[i]), pointer->dnslookups)); - ConnectUser_set_connFd(pointer->contable[i], sent); - ConnectUser_set_state(pointer->contable[i], S_STATE_OPENING); - ConnectUser_set_whatClient(pointer->contable[i], k); + get_realmname(config, j), get_clientname(pointer, k), + sock_ntop(ServerRealm_get_clientAddress(pointer), len, ConnectUser_get_nameBuf(srUsersTable[i]), + ConnectUser_get_portBuf(srUsersTable[i]), ServerRealm_get_dnsLookupsOn(pointer))); + ConnectUser_set_connFd(srUsersTable[i], sent); + ConnectUser_set_state(srUsersTable[i], S_STATE_OPENING); + ConnectUser_set_whatClient(srUsersTable[i], k); time(&now); - ConnectUser_set_connectTime(pointer->contable[i], now); - UserStats_clear(ConnectUser_get_stats(pointer->contable[i])); - UserStats_set_lastActivity(ConnectUser_get_stats(pointer->contable[i]), now); - pointer->usercon++; - ConnectClient_increase_connected(pointer->clitable[k]); - memcpy(&buff[5], ConnectUser_get_nameBuf(pointer->contable[i]), 128); - memcpy(&buff[133], ConnectUser_get_portBuf(pointer->contable[i]), 7); + ConnectUser_set_connectTime(srUsersTable[i], now); + UserStats_clear(ConnectUser_get_stats(srUsersTable[i])); + UserStats_set_lastActivity(ConnectUser_get_stats(srUsersTable[i]), now); + ServerRealm_increase_connectedUsers(pointer); + ConnectClient_increase_connected(srClientsTable[k]); + memcpy(&buff[5], ConnectUser_get_nameBuf(srUsersTable[i]), 128); + memcpy(&buff[133], ConnectUser_get_portBuf(srUsersTable[i]), 7); n = 135; - i = find_usernum(pointer->clitable[k], i); + i = find_usernum(srClientsTable[k], i); buff[0] = AF_S_CONOPEN; /* opening connection */ buff[1] = i >> 8; /* high bits of user number */ buff[2] = i; /* low bits of user number */ buff[3] = n >> 8; /* high bits of message length */ buff[4] = n; /* low bits of message length */ - SslFd_send_message(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, n+5); break; } @@ -1144,26 +1248,26 @@ main(int argc, char **argv) close(sent); aflog(LOG_T_USER, LOG_I_ERR, "realm[%s]: Client(%d) is NOT CONNECTED", - get_realmname(&config, j), k); + get_realmname(config, j), k); } } } } /* ------------------------------------ */ - if (pointer->baseport == 1) { - for (k = 0; k < pointer->clinum; ++k) { - if (ConnectClient_get_state(pointer->clitable[k]) == CONNECTCLIENT_STATE_ACCEPTED) { - if (FD_ISSET(ConnectClient_get_listenFd(pointer->clitable[k]), &rset)) { - len = pointer->addrlen; - sent = accept(ConnectClient_get_listenFd(pointer->clitable[k]), pointer->cliaddr, &len); + if (ServerRealm_get_basePortOn(pointer) == 1) { + for (k = 0; k < ServerRealm_get_clientsLimit(pointer); ++k) { + if (ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_ACCEPTED) { + if (FD_ISSET(ConnectClient_get_listenFd(srClientsTable[k]), &rset)) { + len = ServerRealm_get_addressLength(pointer); + sent = accept(ConnectClient_get_listenFd(srClientsTable[k]), ServerRealm_get_clientAddress(pointer), &len); if (sent == -1) { if (errno == EAGAIN) { aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: listenfd: FD_ISSET --> EAGAIN", get_realmname(&config, j)); + "realm[%s]: listenfd: FD_ISSET --> EAGAIN", get_realmname(config, j)); } else { aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: listenfd: FD_ISSET --> errno=%d", get_realmname(&config, j), errno); + "realm[%s]: listenfd: FD_ISSET --> errno=%d", get_realmname(config, j), errno); } continue; } @@ -1171,51 +1275,51 @@ main(int argc, char **argv) fcntl(sent, F_SETFL, flags | O_NONBLOCK); aflog(LOG_T_USER, LOG_I_DDEBUG, "realm[%s]: Client[%s]: listenfd: FD_ISSET", - get_realmname(&config, j), get_clientname(pointer, k)); - if (pointer->usercon == pointer->usernum) { + get_realmname(config, j), get_clientname(pointer, k)); + if (ServerRealm_get_connectedUsers(pointer) == ServerRealm_get_usersLimit(pointer)) { close(sent); aflog(LOG_T_USER, LOG_I_WARNING, - "realm[%s]: user limit EXCEEDED", get_realmname(&config, j)); + "realm[%s]: user limit EXCEEDED", get_realmname(config, j)); } - else if(ConnectClient_get_connected(pointer->clitable[k]) == - ConnectClient_get_limit(pointer->clitable[k])) { + else if(ConnectClient_get_connected(srClientsTable[k]) == + ConnectClient_get_limit(srClientsTable[k])) { close(sent); aflog(LOG_T_USER, LOG_I_WARNING, "realm[%s]: Client[%s]: usrpcli limit EXCEEDED", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); } else { - for (i = 0; i < pointer->usernum; ++i) { - if (ConnectUser_get_state(pointer->contable[i]) == S_STATE_CLEAR) { - ConnectUser_set_userId(pointer->contable[i], pointer->usercounter); - ++(pointer->usercounter); + for (i = 0; i < ServerRealm_get_usersLimit(pointer); ++i) { + if (ConnectUser_get_state(srUsersTable[i]) == S_STATE_CLEAR) { + ConnectUser_set_userId(srUsersTable[i], ServerRealm_get_usersCounter(pointer)); + ServerRealm_increase_usersCounter(pointer); aflog(LOG_T_USER, LOG_I_INFO, "realm[%s]: Client[%s]: new user: CONNECTING from IP: %s", - get_realmname(&config, j), get_clientname(pointer, k), - sock_ntop(pointer->cliaddr, len, - ConnectUser_get_nameBuf(pointer->contable[i]), - ConnectUser_get_portBuf(pointer->contable[i]), pointer->dnslookups)); - ConnectUser_set_connFd(pointer->contable[i], sent); - ConnectUser_set_state(pointer->contable[i], S_STATE_OPENING); - ConnectUser_set_whatClient(pointer->contable[i], k); + get_realmname(config, j), get_clientname(pointer, k), + sock_ntop(ServerRealm_get_clientAddress(pointer), len, + ConnectUser_get_nameBuf(srUsersTable[i]), + ConnectUser_get_portBuf(srUsersTable[i]), ServerRealm_get_dnsLookupsOn(pointer))); + ConnectUser_set_connFd(srUsersTable[i], sent); + ConnectUser_set_state(srUsersTable[i], S_STATE_OPENING); + ConnectUser_set_whatClient(srUsersTable[i], k); time(&now); - ConnectUser_set_connectTime(pointer->contable[i], now); - UserStats_clear(ConnectUser_get_stats(pointer->contable[i])); - UserStats_set_lastActivity(ConnectUser_get_stats(pointer->contable[i]), now); - pointer->usercon++; - ConnectClient_increase_connected(pointer->clitable[k]); - memcpy(&buff[5], ConnectUser_get_nameBuf(pointer->contable[i]), 128); - memcpy(&buff[133], ConnectUser_get_portBuf(pointer->contable[i]), 7); + ConnectUser_set_connectTime(srUsersTable[i], now); + UserStats_clear(ConnectUser_get_stats(srUsersTable[i])); + UserStats_set_lastActivity(ConnectUser_get_stats(srUsersTable[i]), now); + ServerRealm_increase_connectedUsers(pointer); + ConnectClient_increase_connected(srClientsTable[k]); + memcpy(&buff[5], ConnectUser_get_nameBuf(srUsersTable[i]), 128); + memcpy(&buff[133], ConnectUser_get_portBuf(srUsersTable[i]), 7); n = 135; - i = find_usernum(pointer->clitable[k], i); + i = find_usernum(srClientsTable[k], i); buff[0] = AF_S_CONOPEN; /* opening connection */ buff[1] = i >> 8; /* high bits of user number */ buff[2] = i; /* low bits of user number */ buff[3] = n >> 8; /* high bits of message length */ buff[4] = n; /* low bits of message length */ - SslFd_send_message(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, n+5); break; } @@ -1226,25 +1330,25 @@ main(int argc, char **argv) } } /* ------------------------------------ */ - for (k = 0; k < pointer->clinum; ++k) - if ((ConnectClient_get_state(pointer->clitable[k]) > CONNECTCLIENT_STATE_FREE) && - (FD_ISSET(SslFd_get_fd(ConnectClient_get_sslFd(pointer->clitable[k])), &rset))) { - if (ConnectClient_get_state(pointer->clitable[k]) == CONNECTCLIENT_STATE_CONNECTING) { - make_ssl_initialize(ConnectClient_get_sslFd(pointer->clitable[k])); + for (k = 0; k < ServerRealm_get_clientsLimit(pointer); ++k) + if ((ConnectClient_get_state(srClientsTable[k]) > CONNECTCLIENT_STATE_FREE) && + (FD_ISSET(SslFd_get_fd(ConnectClient_get_sslFd(srClientsTable[k])), &rset))) { + if (ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_CONNECTING) { + make_ssl_initialize(ConnectClient_get_sslFd(srClientsTable[k])); aflog(LOG_T_CLIENT, LOG_I_DDEBUG, "realm[%s]: new Client[%s]: SSL_accept", - get_realmname(&config, j), get_clientname(pointer, k)); - switch (make_ssl_accept(ConnectClient_get_sslFd(pointer->clitable[k]))) { + get_realmname(config, j), get_clientname(pointer, k)); + switch (make_ssl_accept(ConnectClient_get_sslFd(srClientsTable[k]))) { case 2: { - close(SslFd_get_fd(ConnectClient_get_sslFd(pointer->clitable[k]))); - FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(pointer->clitable[k])), &allset); - SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(pointer->clitable[k]))); - ConnectClient_set_state(pointer->clitable[k], CONNECTCLIENT_STATE_FREE); + close(SslFd_get_fd(ConnectClient_get_sslFd(srClientsTable[k]))); + FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(srClientsTable[k])), &allset); + SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(srClientsTable[k]))); + ConnectClient_set_state(srClientsTable[k], CONNECTCLIENT_STATE_FREE); manconnecting--; - pointer->clicon--; + ServerRealm_decrease_connectedClients(pointer); aflog(LOG_T_CLIENT, LOG_I_ERR, "realm[%s]: new Client[%s]: DENIED by SSL_accept", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); } case 1: { continue; @@ -1252,38 +1356,38 @@ main(int argc, char **argv) default: { aflog(LOG_T_CLIENT, LOG_I_DEBUG, "realm[%s]: new Client[%s]: ACCEPTED by SSL_accept", - get_realmname(&config, j), get_clientname(pointer, k)); - ConnectClient_set_state(pointer->clitable[k], CONNECTCLIENT_STATE_AUTHORIZING); + get_realmname(config, j), get_clientname(pointer, k)); + ConnectClient_set_state(srClientsTable[k], CONNECTCLIENT_STATE_AUTHORIZING); continue; } } } aflog(LOG_T_CLIENT, LOG_I_DDEBUG, "realm[%s]: Client[%s]: commfd: FD_ISSET", - get_realmname(&config, j), get_clientname(pointer, k)); - if (ConnectClient_get_state(pointer->clitable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) { - n = SslFd_get_message(pointer->type | TYPE_SSL | TYPE_ZLIB, + get_realmname(config, j), get_clientname(pointer, k)); + if (ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) { + n = SslFd_get_message(ServerRealm_get_realmType(pointer) | TYPE_SSL | TYPE_ZLIB, ConnectClient_get_sslFd( - pointer->clitable[k]), - buff, (-1) * HeaderBuffer_to_read(ConnectClient_get_header(pointer->clitable[k]))); + srClientsTable[k]), + buff, (-1) * HeaderBuffer_to_read(ConnectClient_get_header(srClientsTable[k]))); } else { - n = SslFd_get_message(pointer->type, + n = SslFd_get_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), - buff, (-1) * HeaderBuffer_to_read(ConnectClient_get_header(pointer->clitable[k]))); + srClientsTable[k]), + buff, (-1) * HeaderBuffer_to_read(ConnectClient_get_header(srClientsTable[k]))); } if (n == -1) { if (errno == EAGAIN) { aflog(LOG_T_CLIENT, LOG_I_DDEBUG, "realm[%s]: Client[%s]: commfd: EAGAIN", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); continue; } else { aflog(LOG_T_CLIENT, LOG_I_ERR, "realm[%s]: Client[%s]: commfd: ERROR: %d", - get_realmname(&config, j), get_clientname(pointer, k), errno); + get_realmname(config, j), get_clientname(pointer, k), errno); n = 0; } } @@ -1291,10 +1395,10 @@ main(int argc, char **argv) if (n != 0) { aflog(LOG_T_CLIENT, LOG_I_DEBUG, "realm[%s]: Client[%s]: header length = %d --> buffering", - get_realmname(&config, j), get_clientname(pointer, k), n); - HeaderBuffer_store(ConnectClient_get_header(pointer->clitable[k]), buff, n); - if (HeaderBuffer_to_read(ConnectClient_get_header(pointer->clitable[k])) == 0) { - HeaderBuffer_restore(ConnectClient_get_header(pointer->clitable[k]), buff); + get_realmname(config, j), get_clientname(pointer, k), n); + HeaderBuffer_store(ConnectClient_get_header(srClientsTable[k]), buff, n); + if (HeaderBuffer_to_read(ConnectClient_get_header(srClientsTable[k])) == 0) { + HeaderBuffer_restore(ConnectClient_get_header(srClientsTable[k]), buff); n = 5; } else { @@ -1305,35 +1409,35 @@ main(int argc, char **argv) if (n==0) { aflog(LOG_T_CLIENT, LOG_I_INFO, "realm[%s]: Client[%s]: commfd: CLOSED", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); time(&now); aflog(LOG_T_CLIENT, LOG_I_NOTICE, "REALM: %s CLIENT: %s IP: %s PORT: %s DURATION: %s", - get_realmname(&config, j), + get_realmname(config, j), get_clientname(pointer, k), - ConnectClient_get_nameBuf(pointer->clitable[k]), - ConnectClient_get_portBuf(pointer->clitable[k]), - timeperiod(now - ConnectClient_get_connectTime(pointer->clitable[k]))); - if (pointer->audit) { - while (AuditList_get_first(ConnectClient_get_auditList(pointer->clitable[k]))) { + ConnectClient_get_nameBuf(srClientsTable[k]), + ConnectClient_get_portBuf(srClientsTable[k]), + timeperiod(now - ConnectClient_get_connectTime(srClientsTable[k]))); + if (ServerRealm_get_auditOn(pointer)) { + while (AuditList_get_first(ConnectClient_get_auditList(srClientsTable[k]))) { aflog(LOG_T_CLIENT, LOG_I_NOTICE, "USERID: %d IP: %s PORT: %s CONNECTED: %s DURATION: %s", AuditListNode_get_userId( AuditList_get_first( - ConnectClient_get_auditList(pointer->clitable[k]))), + ConnectClient_get_auditList(srClientsTable[k]))), AuditListNode_get_nameBuf( AuditList_get_first( - ConnectClient_get_auditList(pointer->clitable[k]))), + ConnectClient_get_auditList(srClientsTable[k]))), AuditListNode_get_portBuf( AuditList_get_first( - ConnectClient_get_auditList(pointer->clitable[k]))), + ConnectClient_get_auditList(srClientsTable[k]))), localdate(AuditListNode_get_connectTimep( AuditList_get_first( - ConnectClient_get_auditList(pointer->clitable[k])))), + ConnectClient_get_auditList(srClientsTable[k])))), timeperiod(AuditListNode_get_duration( AuditList_get_first( - ConnectClient_get_auditList(pointer->clitable[k]))))); - AuditList_delete_first(ConnectClient_get_auditList(pointer->clitable[k])); + ConnectClient_get_auditList(srClientsTable[k]))))); + AuditList_delete_first(ConnectClient_get_auditList(srClientsTable[k])); } } remove_client(pointer, k, &allset, &wset, &manconnecting); @@ -1347,17 +1451,17 @@ main(int argc, char **argv) length = length << 8; length += buff[4]; /* this is length of message */ - if ((k == pointer->clinum) && (buff[0] != AF_S_LOGIN) && + if ((k == ServerRealm_get_clientsLimit(pointer)) && (buff[0] != AF_S_LOGIN) && (buff[0] != AF_S_ADMIN_LOGIN) && (buff[0] != AF_S_ADMIN_CMD)) { buff[0] = AF_S_WRONG; } - if (ConnectClient_get_state(pointer->clitable[k]) < CONNECTCLIENT_STATE_AUTHORIZING) { + if (ConnectClient_get_state(srClientsTable[k]) < CONNECTCLIENT_STATE_AUTHORIZING) { aflog(LOG_T_CLIENT, LOG_I_WARNING, "realm[%s]: Client[%s]: Impossible behaviour --> ignoring", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); continue; } - if ((ConnectClient_get_state(pointer->clitable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) && + if ((ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) && (buff[0] != AF_S_LOGIN) && (buff[0] != AF_S_ADMIN_LOGIN)) { buff[0] = AF_S_WRONG; } @@ -1366,42 +1470,42 @@ main(int argc, char **argv) case AF_S_CONCLOSED : { n = numofcon; numofcon = eval_numofcon(pointer, k, numofcon); - if ((numofcon>=0) && (numofcon<(pointer->usernum)) && - (ConnectClient_get_state(pointer->clitable[k]) == + if ((numofcon>=0) && (numofcon<(ServerRealm_get_usersLimit(pointer))) && + (ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_ACCEPTED)) { - pointer->usercon--; - ConnectClient_decrease_connected(pointer->clitable[k]); - ConnectClient_get_users(pointer->clitable[k])[n] = -1; - if (ConnectUser_get_state(pointer->contable[numofcon]) == S_STATE_CLOSING) { - ConnectUser_set_state(pointer->contable[numofcon], S_STATE_CLEAR); + ServerRealm_decrease_connectedUsers(pointer); + ConnectClient_decrease_connected(srClientsTable[k]); + ConnectClient_get_users(srClientsTable[k])[n] = -1; + if (ConnectUser_get_state(srUsersTable[numofcon]) == S_STATE_CLOSING) { + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_CLEAR); aflog(LOG_T_USER, LOG_I_DEBUG, "realm[%s]: user[%d]: CLOSE CONFIRMED", - get_realmname(&config, j), get_username(pointer, numofcon)); + get_realmname(config, j), get_username(pointer, numofcon)); } - else if ((ConnectUser_get_state(pointer->contable[numofcon]) == S_STATE_OPEN) || - (ConnectUser_get_state(pointer->contable[numofcon]) == S_STATE_STOPPED)) { + else if ((ConnectUser_get_state(srUsersTable[numofcon]) == S_STATE_OPEN) || + (ConnectUser_get_state(srUsersTable[numofcon]) == S_STATE_STOPPED)) { aflog(LOG_T_USER, LOG_I_INFO, "realm[%s]: user[%d]: KICKED", - get_realmname(&config, j), get_username(pointer, numofcon)); + get_realmname(config, j), get_username(pointer, numofcon)); time(&now); aflog(LOG_T_USER, LOG_I_NOTICE, "REALM: %s USER: %d IP: %s PORT: %s DURATION: %s", - get_realmname(&config, j), + get_realmname(config, j), get_username(pointer, numofcon), - ConnectUser_get_nameBuf(pointer->contable[numofcon]), - ConnectUser_get_portBuf(pointer->contable[numofcon]), - timeperiod(now - ConnectUser_get_connectTime(pointer->contable[numofcon]))); - close(ConnectUser_get_connFd(pointer->contable[numofcon])); - FD_CLR(ConnectUser_get_connFd(pointer->contable[numofcon]), &allset); - FD_CLR(ConnectUser_get_connFd(pointer->contable[numofcon]), &wset); - ConnectUser_set_state(pointer->contable[numofcon], S_STATE_CLEAR); - BufList_clear(ConnectUser_get_bufList(pointer->contable[numofcon])); + ConnectUser_get_nameBuf(srUsersTable[numofcon]), + ConnectUser_get_portBuf(srUsersTable[numofcon]), + timeperiod(now - ConnectUser_get_connectTime(srUsersTable[numofcon]))); + close(ConnectUser_get_connFd(srUsersTable[numofcon])); + FD_CLR(ConnectUser_get_connFd(srUsersTable[numofcon]), &allset); + FD_CLR(ConnectUser_get_connFd(srUsersTable[numofcon]), &wset); + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_CLEAR); + BufList_clear(ConnectUser_get_bufList(srUsersTable[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(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); } } @@ -1412,17 +1516,32 @@ main(int argc, char **argv) } case AF_S_CONOPEN : { numofcon = eval_numofcon(pointer, k, numofcon); - if ((numofcon>=0) && (numofcon<(pointer->usernum)) && - (ConnectClient_get_state(pointer->clitable[k]) == + if ((numofcon>=0) && (numofcon<(ServerRealm_get_usersLimit(pointer))) && + (ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_ACCEPTED)) { - if (ConnectUser_get_state(pointer->contable[numofcon]) == S_STATE_OPENING) { + if (ConnectUser_get_state(srUsersTable[numofcon]) == + S_STATE_OPENING) { aflog(LOG_T_USER, LOG_I_INFO, "realm[%s]: user[%d]: NEW", - get_realmname(&config, j), get_username(pointer, numofcon)); - FD_SET(ConnectUser_get_connFd(pointer->contable[numofcon]), &allset); - maxfdp1 = (maxfdp1 > (ConnectUser_get_connFd(pointer->contable[numofcon]) + 1)) ? - maxfdp1 : (ConnectUser_get_connFd(pointer->contable[numofcon]) + 1); - ConnectUser_set_state(pointer->contable[numofcon], S_STATE_OPEN); + get_realmname(config, j), get_username(pointer, numofcon)); + FD_SET(ConnectUser_get_connFd(srUsersTable[numofcon]), &allset); + maxfdp1 = (maxfdp1 > (ConnectUser_get_connFd(srUsersTable[numofcon]) + 1)) ? + maxfdp1 : (ConnectUser_get_connFd(srUsersTable[numofcon]) + 1); + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_OPEN); + } + if (ConnectUser_get_state(srUsersTable[numofcon]) == + S_STATE_OPENING_CLOSED) { + aflog(LOG_T_USER, LOG_I_INFO, + "realm[%s]: user[%d]: delayed CLOSING", + get_realmname(config, j), get_username(pointer, numofcon)); + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_CLOSING); + 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(ServerRealm_get_realmType(pointer), + ConnectClient_get_sslFd( + srClientsTable[k]), + buff, 5); } } else { @@ -1433,18 +1552,24 @@ main(int argc, char **argv) case AF_S_CANT_OPEN : { n = numofcon; numofcon = eval_numofcon(pointer, k, numofcon); - if ((numofcon>=0) && (numofcon<(pointer->usernum)) && - (ConnectClient_get_state(pointer->clitable[k]) == + if ((numofcon>=0) && (numofcon<(ServerRealm_get_usersLimit(pointer))) && + (ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_ACCEPTED)) { - if (ConnectUser_get_state(pointer->contable[numofcon]) == S_STATE_OPENING) { + if ((ConnectUser_get_state(srUsersTable[numofcon]) == + S_STATE_OPENING) || + (ConnectUser_get_state(srUsersTable[numofcon]) == + S_STATE_OPENING_CLOSED)) { aflog(LOG_T_USER, LOG_I_INFO, "realm[%s]: user[%d]: DROPPED", - get_realmname(&config, j), get_username(pointer, numofcon)); - pointer->usercon--; - ConnectClient_decrease_connected(pointer->clitable[k]); - ConnectClient_get_users(pointer->clitable[k])[n] = -1; - close(ConnectUser_get_connFd(pointer->contable[numofcon])); - ConnectUser_set_state(pointer->contable[numofcon], S_STATE_CLEAR); + get_realmname(config, j), get_username(pointer, numofcon)); + ServerRealm_decrease_connectedUsers(pointer); + ConnectClient_decrease_connected(srClientsTable[k]); + ConnectClient_get_users(srClientsTable[k])[n] = -1; + if (ConnectUser_get_state(srUsersTable[numofcon]) == + S_STATE_OPENING) { + close(ConnectUser_get_connFd(srUsersTable[numofcon])); + } + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_CLEAR); } } else { @@ -1453,190 +1578,190 @@ main(int argc, char **argv) break; } case AF_S_MESSAGE : { - if (ConnectClient_get_state(pointer->clitable[k]) != + if (ConnectClient_get_state(srClientsTable[k]) != CONNECTCLIENT_STATE_ACCEPTED) { remove_client(pointer, k, &allset, &wset, &manconnecting); break; } - if (TYPE_IS_UDP(pointer->type)) { /* udp */ - n = SslFd_get_message(pointer->type, + if (TYPE_IS_UDP(ServerRealm_get_realmType(pointer))) { /* udp */ + n = SslFd_get_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), &buff[5], length); } else { - n = SslFd_get_message(pointer->type, + n = SslFd_get_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, length); } numofcon = eval_numofcon(pointer, k, numofcon); - if ((numofcon>=0) && (numofcon<(pointer->usernum))) { - if (ConnectUser_get_state(pointer->contable[numofcon]) == S_STATE_OPEN) { + if ((numofcon>=0) && (numofcon<(ServerRealm_get_usersLimit(pointer)))) { + if (ConnectUser_get_state(srUsersTable[numofcon]) == S_STATE_OPEN) { aflog(LOG_T_USER, LOG_I_DEBUG, "realm[%s]: TO user[%d]: MESSAGE length=%d", - get_realmname(&config, j), get_username(pointer, numofcon), n); - UserStats_add_download(ConnectUser_get_stats(pointer->contable[numofcon]), n); - if (TYPE_IS_UDP(pointer->type)) { /* udp */ + get_realmname(config, j), get_username(pointer, numofcon), n); + UserStats_add_download(ConnectUser_get_stats(srUsersTable[numofcon]), n); + if (TYPE_IS_UDP(ServerRealm_get_realmType(pointer))) { /* udp */ buff[1] = AF_S_LOGIN; buff[2] = AF_S_MESSAGE; buff[3] = n >> 8; /* high bits of message length */ buff[4] = n; /* low bits of message length */ - sent = write(ConnectUser_get_connFd(pointer->contable[numofcon]), buff, n+5); + sent = write(ConnectUser_get_connFd(srUsersTable[numofcon]), buff, n+5); if (sent == -1) { aflog(LOG_T_USER, LOG_I_INFO, "realm[%s]: user[%d]: CLOSED (write-udp)", - get_realmname(&config, j), get_username(pointer, numofcon)); + get_realmname(config, j), get_username(pointer, numofcon)); time(&now); aflog(LOG_T_USER, LOG_I_NOTICE, "REALM: %s USER: %d IP: %s PORT: %s DURATION: %s", - get_realmname(&config, j), + get_realmname(config, j), get_username(pointer, numofcon), - ConnectUser_get_nameBuf(pointer->contable[numofcon]), - ConnectUser_get_portBuf(pointer->contable[numofcon]), - timeperiod(now - ConnectUser_get_connectTime(pointer->contable[numofcon]))); - close(ConnectUser_get_connFd(pointer->contable[numofcon])); - FD_CLR(ConnectUser_get_connFd(pointer->contable[numofcon]), &allset); - FD_CLR(ConnectUser_get_connFd(pointer->contable[numofcon]), &wset); - ConnectUser_set_state(pointer->contable[numofcon], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(pointer->contable[numofcon])); + ConnectUser_get_nameBuf(srUsersTable[numofcon]), + ConnectUser_get_portBuf(srUsersTable[numofcon]), + timeperiod(now - ConnectUser_get_connectTime(srUsersTable[numofcon]))); + close(ConnectUser_get_connFd(srUsersTable[numofcon])); + FD_CLR(ConnectUser_get_connFd(srUsersTable[numofcon]), &allset); + FD_CLR(ConnectUser_get_connFd(srUsersTable[numofcon]), &wset); + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(srUsersTable[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(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); } } else { /* tcp */ - sent = write(ConnectUser_get_connFd(pointer->contable[numofcon]), buff, n); + sent = write(ConnectUser_get_connFd(srUsersTable[numofcon]), buff, n); if ((sent > 0) && (sent != n)) { - BufList_insert_back(ConnectUser_get_bufList(pointer->contable[numofcon]), + BufList_insert_back(ConnectUser_get_bufList(srUsersTable[numofcon]), BufListNode_new_message(sent, n, buff)); - ConnectUser_set_state(pointer->contable[numofcon], S_STATE_STOPPED); - FD_SET(ConnectUser_get_connFd(pointer->contable[numofcon]), &wset); + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_STOPPED); + FD_SET(ConnectUser_get_connFd(srUsersTable[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, "realm[%s]: TO user[%d]: BUFFERING MESSAGE STARTED (%d/%d)", - get_realmname(&config, j), get_username(pointer, numofcon), sent, n); - SslFd_send_message(pointer->type, + get_realmname(config, j), get_username(pointer, numofcon), sent, n); + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); } else if ((sent == -1) && (errno == EAGAIN)) { - BufList_insert_back(ConnectUser_get_bufList(pointer->contable[numofcon]), + BufList_insert_back(ConnectUser_get_bufList(srUsersTable[numofcon]), BufListNode_new_message(0, n, buff)); - ConnectUser_set_state(pointer->contable[numofcon], S_STATE_STOPPED); - FD_SET(ConnectUser_get_connFd(pointer->contable[numofcon]), &wset); + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_STOPPED); + FD_SET(ConnectUser_get_connFd(srUsersTable[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, "realm[%s]: TO user[%d]: BUFFERING MESSAGE STARTED (%d/%d)", - get_realmname(&config, j), get_username(pointer, numofcon), sent, n); - SslFd_send_message(pointer->type, + get_realmname(config, j), get_username(pointer, numofcon), sent, n); + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); } else if (sent == -1) { aflog(LOG_T_USER, LOG_I_INFO, "realm[%s]: user[%d]: CLOSED (write-tcp)", - get_realmname(&config, j), get_username(pointer, numofcon)); + get_realmname(config, j), get_username(pointer, numofcon)); time(&now); aflog(LOG_T_USER, LOG_I_NOTICE, "REALM: %s USER: %d IP: %s PORT: %s DURATION: %s", - get_realmname(&config, j), + get_realmname(config, j), get_username(pointer, numofcon), - ConnectUser_get_nameBuf(pointer->contable[numofcon]), - ConnectUser_get_portBuf(pointer->contable[numofcon]), - timeperiod(now - ConnectUser_get_connectTime(pointer->contable[numofcon]))); - close(ConnectUser_get_connFd(pointer->contable[numofcon])); - FD_CLR(ConnectUser_get_connFd(pointer->contable[numofcon]), &allset); - FD_CLR(ConnectUser_get_connFd(pointer->contable[numofcon]), &wset); - ConnectUser_set_state(pointer->contable[numofcon], S_STATE_CLOSING); - BufList_clear(ConnectUser_get_bufList(pointer->contable[numofcon])); + ConnectUser_get_nameBuf(srUsersTable[numofcon]), + ConnectUser_get_portBuf(srUsersTable[numofcon]), + timeperiod(now - ConnectUser_get_connectTime(srUsersTable[numofcon]))); + close(ConnectUser_get_connFd(srUsersTable[numofcon])); + FD_CLR(ConnectUser_get_connFd(srUsersTable[numofcon]), &allset); + FD_CLR(ConnectUser_get_connFd(srUsersTable[numofcon]), &wset); + ConnectUser_set_state(srUsersTable[numofcon], S_STATE_CLOSING); + BufList_clear(ConnectUser_get_bufList(srUsersTable[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(pointer->type, + SslFd_send_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); } } } - else if (ConnectUser_get_state(pointer->contable[numofcon]) == S_STATE_STOPPED) { + else if (ConnectUser_get_state(srUsersTable[numofcon]) == S_STATE_STOPPED) { aflog(LOG_T_USER, LOG_I_DDEBUG, "realm[%s]: TO user[%d]: BUFFERING MESSAGE (%d)", - get_realmname(&config, j), get_username(pointer, numofcon), n); - if (TYPE_IS_UDP(pointer->type)) { /* udp */ + get_realmname(config, j), get_username(pointer, numofcon), n); + if (TYPE_IS_UDP(ServerRealm_get_realmType(pointer))) { /* udp */ buff[1] = AF_S_LOGIN; buff[2] = AF_S_MESSAGE; buff[3] = n >> 8; /* high bits of message length */ buff[4] = n; /* low bits of message length */ - BufList_insert_back(ConnectUser_get_bufList(pointer->contable[numofcon]), + BufList_insert_back(ConnectUser_get_bufList(srUsersTable[numofcon]), BufListNode_new_message(0, n+5, buff)); } else { - BufList_insert_back(ConnectUser_get_bufList(pointer->contable[numofcon]), + BufList_insert_back(ConnectUser_get_bufList(srUsersTable[numofcon]), BufListNode_new_message(0, n, buff)); } } - else if (ConnectUser_get_state(pointer->contable[numofcon]) == S_STATE_CLOSING) { + else if (ConnectUser_get_state(srUsersTable[numofcon]) == S_STATE_CLOSING) { aflog(LOG_T_USER, LOG_I_WARNING, "realm[%s]: TO user[%d]: IGNORED message length=%d", - get_realmname(&config, j), get_username(pointer, numofcon), n); + get_realmname(config, j), get_username(pointer, numofcon), n); } else { aflog(LOG_T_USER, LOG_I_WARNING, "realm[%s]: TO user[%d]: user in wrong state - IGNORED", - get_realmname(&config, j), get_username(pointer, numofcon)); + get_realmname(config, j), get_username(pointer, numofcon)); } } else { aflog(LOG_T_USER, LOG_I_WARNING, "realm[%s]: message to non-existing user - IGNORED", - get_realmname(&config, j)); + get_realmname(config, j)); } break; } case AF_S_LOGIN : { - if ((ConnectClient_get_state(pointer->clitable[k]) == + if ((ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) && - (numofcon==(pointer->pass[0]*256+pointer->pass[1])) && - (length==(pointer->pass[2]*256+pointer->pass[3]))) { - if (k != pointer->clinum) { - ConnectClient_set_state(pointer->clitable[k], CONNECTCLIENT_STATE_ACCEPTED); + (numofcon==(ServerRealm_get_password(pointer)[0]*256+ServerRealm_get_password(pointer)[1])) && + (length==(ServerRealm_get_password(pointer)[2]*256+ServerRealm_get_password(pointer)[3]))) { + if (k != ServerRealm_get_clientsLimit(pointer)) { + ConnectClient_set_state(srClientsTable[k], CONNECTCLIENT_STATE_ACCEPTED); aflog(LOG_T_CLIENT, LOG_I_INFO, "realm[%s]: Client[%s]: pass ok - ACCESS GRANTED", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); buff[0] = AF_S_LOGIN; /* sending message */ buff[1] = ConnectClient_get_limit( - pointer->clitable[k]) >> 8;/* high bits of user number */ + srClientsTable[k]) >> 8;/* high bits of user number */ buff[2] = ConnectClient_get_limit( - pointer->clitable[k]); /* low bits of user number */ - buff[3] = pointer->type; /* type of connection */ - SslFd_send_message(pointer->type | TYPE_SSL | TYPE_ZLIB, + srClientsTable[k]); /* low bits of user number */ + buff[3] = ServerRealm_get_realmType(pointer); /* type of connection */ + SslFd_send_message(ServerRealm_get_realmType(pointer) | TYPE_SSL | TYPE_ZLIB, ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); manconnecting--; - if (pointer->baseport == 1) { + if (ServerRealm_get_basePortOn(pointer) == 1) { long tmp_val; char tmp_tab[6]; if (check_long( UsrCli_get_listenPortName( - pointer->usrclitable[ - ConnectClient_get_usrCliPair(pointer->clitable[k])]), + srUsersClientsTable[ + ConnectClient_get_usrCliPair(srClientsTable[k])]), &tmp_val)) { aflog(LOG_T_CLIENT, LOG_I_ERR, "realm[%s]: INVALID listenport - removing Client[%s]", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); remove_client(pointer, k, &allset, &wset, &manconnecting); break; } @@ -1645,63 +1770,64 @@ main(int argc, char **argv) sprintf(tmp_tab, "%d", (int)tmp_val); ipfam = 0x01; #ifdef AF_INET6 - if (TYPE_IS_IPV4(pointer->type)) { + if (TYPE_IS_IPV4(ServerRealm_get_realmType(pointer))) { ipfam |= 0x02; } - else if (TYPE_IS_IPV6(pointer->type)) { + else if (TYPE_IS_IPV6(ServerRealm_get_realmType(pointer))) { ipfam |= 0x04; } #endif - while (ip_listen(ConnectClient_get_listenFdp(pointer->clitable[k]), - UsrCli_get_listenHostName(pointer->usrclitable[ - ConnectClient_get_usrCliPair(pointer->clitable[k])]) ? - UsrCli_get_listenHostName(pointer->usrclitable[ - ConnectClient_get_usrCliPair(pointer->clitable[k])]) : - pointer->hostname, - tmp_tab, (&(pointer->addrlen)), ipfam)) { + while (ip_listen(ConnectClient_get_listenFdp(srClientsTable[k]), + UsrCli_get_listenHostName(srUsersClientsTable[ + ConnectClient_get_usrCliPair(srClientsTable[k])]) ? + UsrCli_get_listenHostName(srUsersClientsTable[ + ConnectClient_get_usrCliPair(srClientsTable[k])]) : + ServerRealm_get_hostName(pointer), + tmp_tab, (&len), ipfam)) { tmp_val = (tmp_val+1)%65536; memset(tmp_tab, 0, 6); sprintf(tmp_tab, "%d", (int)tmp_val); } - FD_SET(ConnectClient_get_listenFd(pointer->clitable[k]), &allset); - maxfdp1 = (maxfdp1>(ConnectClient_get_listenFd(pointer->clitable[k])+1)) ? - maxfdp1 : (ConnectClient_get_listenFd(pointer->clitable[k]) + 1); + ServerRealm_set_addressLength(pointer, len); + FD_SET(ConnectClient_get_listenFd(srClientsTable[k]), &allset); + maxfdp1 = (maxfdp1>(ConnectClient_get_listenFd(srClientsTable[k])+1)) ? + maxfdp1 : (ConnectClient_get_listenFd(srClientsTable[k]) + 1); aflog(LOG_T_CLIENT, LOG_I_INFO, "realm[%s]: Client[%s]: listenport=%s", - get_realmname(&config, j), get_clientname(pointer, k), tmp_tab); + get_realmname(config, j), get_clientname(pointer, k), tmp_tab); } } else { aflog(LOG_T_CLIENT, LOG_I_WARNING, - "realm[%s]: client limit EXCEEDED", get_realmname(&config, j)); + "realm[%s]: client limit EXCEEDED", get_realmname(config, j)); buff[0] = AF_S_CANT_OPEN; /* sending message */ - SslFd_send_message(pointer->type | TYPE_SSL, + SslFd_send_message(ServerRealm_get_realmType(pointer) | TYPE_SSL, ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); remove_client(pointer, k, &allset, &wset, &manconnecting); } } - else if ((ConnectClient_get_state(pointer->clitable[k]) == + else if ((ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_ACCEPTED) && (numofcon == 0)) { - n = SslFd_get_message(pointer->type, + n = SslFd_get_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, length); buff[n] = 0; aflog(LOG_T_CLIENT, LOG_I_INFO, "realm[%s]: Client[%s]: ID received: %s", - get_realmname(&config, j), get_clientname(pointer, k), buff); - ConnectClient_set_sClientId(pointer->clitable[k], (char*) buff); + get_realmname(config, j), get_clientname(pointer, k), buff); + ConnectClient_set_sClientId(srClientsTable[k], (char*) buff); } else { aflog(LOG_T_CLIENT, LOG_I_ERR, "realm[%s]: Client[%s]: Wrong password - CLOSING", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); buff[0] = AF_S_WRONG; /* sending message */ - SslFd_send_message(pointer->type | TYPE_SSL, + SslFd_send_message(ServerRealm_get_realmType(pointer) | TYPE_SSL, ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); remove_client(pointer, k, &allset, &wset, &manconnecting); } @@ -1710,97 +1836,97 @@ main(int argc, char **argv) case AF_S_DONT_SEND: { aflog(LOG_T_CLIENT, LOG_I_DEBUG, "realm[%s]: user[%d]: STOP READING", - get_realmname(&config, j), get_username(pointer, numofcon)); - FD_CLR(ConnectUser_get_connFd(pointer->contable[numofcon]), &allset); + get_realmname(config, j), get_username(pointer, numofcon)); + FD_CLR(ConnectUser_get_connFd(srUsersTable[numofcon]), &allset); break; } case AF_S_CAN_SEND: { aflog(LOG_T_CLIENT, LOG_I_DEBUG, "realm[%s]: user[%d]: START READING", - get_realmname(&config, j), get_username(pointer, numofcon)); - FD_SET(ConnectUser_get_connFd(pointer->contable[numofcon]), &allset); + get_realmname(config, j), get_username(pointer, numofcon)); + FD_SET(ConnectUser_get_connFd(srUsersTable[numofcon]), &allset); break; } case AF_S_WRONG: { aflog(LOG_T_CLIENT, LOG_I_ERR, "realm[%s]: Client[%s]: Wrong message - CLOSING", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); remove_client(pointer, k, &allset, &wset, &manconnecting); break; } case AF_S_ADMIN_LOGIN: { - if ((ConnectClient_get_state(pointer->clitable[k]) == + if ((ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) && - (numofcon == (pointer->pass[0]*256 + pointer->pass[1])) && - (length == (pointer->pass[2]*256 + pointer->pass[3]))) { + (numofcon == (ServerRealm_get_password(pointer)[0]*256 + ServerRealm_get_password(pointer)[1])) && + (length == (ServerRealm_get_password(pointer)[2]*256 + ServerRealm_get_password(pointer)[3]))) { aflog(LOG_T_MANAGE, LOG_I_INFO, "realm[%s]: Client[%s]: NEW remote admin -- pass OK", - get_realmname(&config, j), get_clientname(pointer, k)); - for (l = 0; l < pointer->raclinum; ++l) { - if (ConnectClient_get_state(pointer->raclitable[l]) == + get_realmname(config, j), get_clientname(pointer, k)); + for (l = 0; l < ServerRealm_get_raClientsLimit(pointer); ++l) { + if (ConnectClient_get_state(srRaClientsTable[l]) == CONNECTCLIENT_STATE_FREE) { SslFd_set_fd( - ConnectClient_get_sslFd(pointer->raclitable[l]), + ConnectClient_get_sslFd(srRaClientsTable[l]), SslFd_get_fd( - ConnectClient_get_sslFd(pointer->clitable[k]))); + ConnectClient_get_sslFd(srClientsTable[k]))); ConnectClient_set_connectTime( - pointer->raclitable[l], - ConnectClient_get_connectTime(pointer->clitable[k])); + srRaClientsTable[l], + ConnectClient_get_connectTime(srClientsTable[k])); #ifdef HAVE_LIBPTHREAD ConnectClient_set_tunnelType( - pointer->raclitable[l], - ConnectClient_get_tunnelType(pointer->clitable[k])); + srRaClientsTable[l], + ConnectClient_get_tunnelType(srClientsTable[k])); #endif ConnectClient_set_clientId( - pointer->raclitable[l], - ConnectClient_get_clientId(pointer->clitable[k])); + srRaClientsTable[l], + ConnectClient_get_clientId(srClientsTable[k])); ConnectClient_set_nameBuf( - pointer->raclitable[l], - ConnectClient_get_nameBuf(pointer->clitable[k])); + srRaClientsTable[l], + ConnectClient_get_nameBuf(srClientsTable[k])); ConnectClient_set_portBuf( - pointer->raclitable[l], - ConnectClient_get_portBuf(pointer->clitable[k])); + srRaClientsTable[l], + ConnectClient_get_portBuf(srClientsTable[k])); tmp_ssl = SslFd_get_ssl( - ConnectClient_get_sslFd(pointer->raclitable[l])); + ConnectClient_get_sslFd(srRaClientsTable[l])); SslFd_set_ssl_nf( - ConnectClient_get_sslFd(pointer->raclitable[l]), + ConnectClient_get_sslFd(srRaClientsTable[l]), SslFd_get_ssl( - ConnectClient_get_sslFd(pointer->clitable[k]))); + ConnectClient_get_sslFd(srClientsTable[k]))); SslFd_set_ssl_nf( - ConnectClient_get_sslFd(pointer->clitable[k]), + ConnectClient_get_sslFd(srClientsTable[k]), tmp_ssl); ConnectClient_set_state( - pointer->clitable[k], + srClientsTable[k], CONNECTCLIENT_STATE_FREE); break; } } - if (l != pointer->raclinum) { + if (l != ServerRealm_get_raClientsLimit(pointer)) { ConnectClient_set_state( - pointer->raclitable[l], + srRaClientsTable[l], CONNECTCLIENT_STATE_ACCEPTED); - pointer->raclicon++; + ServerRealm_increase_connectedRaClients(pointer); manconnecting--; sprintf((char*) &buff[5], AF_VER("AFSERVER")); n = strlen((char*) &buff[5]); buff[0] = AF_S_ADMIN_LOGIN; /* sending message */ - buff[1] = pointer->type; /* type of connection */ + buff[1] = ServerRealm_get_realmType(pointer); /* type of connection */ buff[2] = AF_RA_UNDEFINED; buff[3] = n >> 8; /* high bits of message length */ buff[4] = n; /* low bits of message length */ - SslFd_send_message(pointer->type | TYPE_SSL, + SslFd_send_message(ServerRealm_get_realmType(pointer) | TYPE_SSL, ConnectClient_get_sslFd( - pointer->raclitable[l]), + srRaClientsTable[l]), buff, n+5); } else { aflog(LOG_T_MANAGE, LOG_I_WARNING, "realm[%s]: Client[%s]: remote admin -- limit EXCEEDED", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); buff[0] = AF_S_CANT_OPEN; /* sending message */ - SslFd_send_message(pointer->type | TYPE_SSL | TYPE_ZLIB, + SslFd_send_message(ServerRealm_get_realmType(pointer) | TYPE_SSL | TYPE_ZLIB, ConnectClient_get_sslFd( - pointer->clitable[k]), + srClientsTable[k]), buff, 5); remove_client(pointer, k, &allset, &wset, &manconnecting); } @@ -1810,37 +1936,37 @@ main(int argc, char **argv) case AF_S_KEEP_ALIVE: { aflog(LOG_T_CLIENT, LOG_I_DEBUG, "realm[%s]: Client[%s]: Keep alive packet", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); break; } default : { aflog(LOG_T_CLIENT, LOG_I_ERR, "realm[%s]: Client[%s]: Unrecognized message - CLOSING", - get_realmname(&config, j), get_clientname(pointer, k)); + get_realmname(config, j), get_clientname(pointer, k)); remove_client(pointer, k, &allset, &wset, &manconnecting); } } } /* ------------------------------------ */ - for (k = 0; k < pointer->raclinum; ++k) - if ((ConnectClient_get_state(pointer->raclitable[k]) > CONNECTCLIENT_STATE_FREE) && - (FD_ISSET(SslFd_get_fd(ConnectClient_get_sslFd(pointer->raclitable[k])), &rset))) { - if (ConnectClient_get_state(pointer->raclitable[k]) == CONNECTCLIENT_STATE_CONNECTING) { - make_ssl_initialize(ConnectClient_get_sslFd(pointer->raclitable[k])); + for (k = 0; k < ServerRealm_get_raClientsLimit(pointer); ++k) + if ((ConnectClient_get_state(srRaClientsTable[k]) > CONNECTCLIENT_STATE_FREE) && + (FD_ISSET(SslFd_get_fd(ConnectClient_get_sslFd(srRaClientsTable[k])), &rset))) { + if (ConnectClient_get_state(srRaClientsTable[k]) == CONNECTCLIENT_STATE_CONNECTING) { + make_ssl_initialize(ConnectClient_get_sslFd(srRaClientsTable[k])); aflog(LOG_T_MANAGE, LOG_I_DDEBUG, "realm[%s]: new Client[%s] (ra): SSL_accept", - get_realmname(&config, j), get_raclientname(pointer, k)); - switch (make_ssl_accept(ConnectClient_get_sslFd(pointer->raclitable[k]))) { + get_realmname(config, j), get_raclientname(pointer, k)); + switch (make_ssl_accept(ConnectClient_get_sslFd(srRaClientsTable[k]))) { case 2: { - close (SslFd_get_fd(ConnectClient_get_sslFd(pointer->raclitable[k]))); - FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(pointer->raclitable[k])), &allset); - SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(pointer->raclitable[k]))); - ConnectClient_set_state(pointer->raclitable[k], CONNECTCLIENT_STATE_FREE); + close (SslFd_get_fd(ConnectClient_get_sslFd(srRaClientsTable[k]))); + FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(srRaClientsTable[k])), &allset); + SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(srRaClientsTable[k]))); + ConnectClient_set_state(srRaClientsTable[k], CONNECTCLIENT_STATE_FREE); manconnecting--; - pointer->clicon--; + ServerRealm_decrease_connectedClients(pointer); aflog(LOG_T_MANAGE, LOG_I_ERR, "realm[%s]: new Client[%s] (ra): DENIED by SSL_accept", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); } case 1: { continue; @@ -1848,30 +1974,30 @@ main(int argc, char **argv) default: { aflog(LOG_T_MANAGE, LOG_I_DEBUG, "realm[%s]: new Client[%s] (ra): ACCEPTED by SSL_accept", - get_realmname(&config, j), get_raclientname(pointer, k)); - ConnectClient_set_state(pointer->raclitable[k], CONNECTCLIENT_STATE_AUTHORIZING); + get_realmname(config, j), get_raclientname(pointer, k)); + ConnectClient_set_state(srRaClientsTable[k], CONNECTCLIENT_STATE_AUTHORIZING); continue; } } } aflog(LOG_T_MANAGE, LOG_I_DDEBUG, "realm[%s]: Client[%s] (ra): commfd: FD_ISSET", - get_realmname(&config, j), get_raclientname(pointer, k)); - n = SslFd_get_message(pointer->type | TYPE_SSL | TYPE_ZLIB, + get_realmname(config, j), get_raclientname(pointer, k)); + n = SslFd_get_message(ServerRealm_get_realmType(pointer) | TYPE_SSL | TYPE_ZLIB, ConnectClient_get_sslFd( - pointer->raclitable[k]), - buff, (-1) * HeaderBuffer_to_read(ConnectClient_get_header(pointer->raclitable[k]))); + srRaClientsTable[k]), + buff, (-1) * HeaderBuffer_to_read(ConnectClient_get_header(srRaClientsTable[k]))); if (n == -1) { if (errno == EAGAIN) { aflog(LOG_T_MANAGE, LOG_I_DDEBUG, "realm[%s]: Client[%s] (ra): commfd: EAGAIN", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); continue; } else { aflog(LOG_T_MANAGE, LOG_I_ERR, "realm[%s]: Client[%s] (ra): commfd: ERROR: %d", - get_realmname(&config, j), get_raclientname(pointer, k), errno); + get_realmname(config, j), get_raclientname(pointer, k), errno); n = 0; } } @@ -1879,10 +2005,10 @@ main(int argc, char **argv) if (n != 0) { aflog(LOG_T_MANAGE, LOG_I_WARNING, "realm[%s]: Client[%s] (ra): header length = %d --> buffering", - get_realmname(&config, j), get_raclientname(pointer, k), n); - HeaderBuffer_store(ConnectClient_get_header(pointer->raclitable[k]), buff, n); - if (HeaderBuffer_to_read(ConnectClient_get_header(pointer->raclitable[k])) == 0) { - HeaderBuffer_restore(ConnectClient_get_header(pointer->raclitable[k]), buff); + get_realmname(config, j), get_raclientname(pointer, k), n); + HeaderBuffer_store(ConnectClient_get_header(srRaClientsTable[k]), buff, n); + if (HeaderBuffer_to_read(ConnectClient_get_header(srRaClientsTable[k])) == 0) { + HeaderBuffer_restore(ConnectClient_get_header(srRaClientsTable[k]), buff); n = 5; } else { @@ -1894,7 +2020,7 @@ main(int argc, char **argv) remove_raclient(pointer, k, &allset, &wset, &manconnecting); aflog(LOG_T_MANAGE, LOG_I_INFO, "realm[%s]: Client[%s] (ra): commfd: CLOSED", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); continue; } @@ -1905,94 +2031,94 @@ main(int argc, char **argv) length = length << 8; length += buff[4]; /* this is length of message */ - if (ConnectClient_get_state(pointer->raclitable[k]) < CONNECTCLIENT_STATE_AUTHORIZING) { + if (ConnectClient_get_state(srRaClientsTable[k]) < CONNECTCLIENT_STATE_AUTHORIZING) { aflog(LOG_T_MANAGE, LOG_I_WARNING, "realm[%s]: Client[%s] (ra): Impossible behaviour --> ignoring", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); continue; } - if ((ConnectClient_get_state(pointer->raclitable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) && + if ((ConnectClient_get_state(srRaClientsTable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) && (buff[0] != AF_S_LOGIN) && (buff[0] != AF_S_ADMIN_LOGIN)) { buff[0] = AF_S_WRONG; } switch (buff[0]) { case AF_S_LOGIN : { - if ((ConnectClient_get_state(pointer->raclitable[k]) == + if ((ConnectClient_get_state(srRaClientsTable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) && - (numofcon==(pointer->pass[0]*256+pointer->pass[1])) && - (length==(pointer->pass[2]*256+pointer->pass[3]))) { - for (l = 0; l < pointer->clinum; ++l) { - if (ConnectClient_get_state(pointer->clitable[l]) == + (numofcon==(ServerRealm_get_password(pointer)[0]*256+ServerRealm_get_password(pointer)[1])) && + (length==(ServerRealm_get_password(pointer)[2]*256+ServerRealm_get_password(pointer)[3]))) { + for (l = 0; l < ServerRealm_get_clientsLimit(pointer); ++l) { + if (ConnectClient_get_state(srClientsTable[l]) == CONNECTCLIENT_STATE_FREE) { aflog(LOG_T_MANAGE | LOG_T_CLIENT, LOG_I_INFO, "realm[%s]: Client[%s] (ra) --> Client[%s]", - get_realmname(&config, j), + get_realmname(config, j), get_raclientname(pointer, k), get_clientname(pointer, l)); SslFd_set_fd( - ConnectClient_get_sslFd(pointer->clitable[l]), + ConnectClient_get_sslFd(srClientsTable[l]), SslFd_get_fd( - ConnectClient_get_sslFd(pointer->raclitable[k]))); + ConnectClient_get_sslFd(srRaClientsTable[k]))); ConnectClient_set_connectTime( - pointer->clitable[l], - ConnectClient_get_connectTime(pointer->raclitable[k])); + srClientsTable[l], + ConnectClient_get_connectTime(srRaClientsTable[k])); #ifdef HAVE_LIBPTHREAD ConnectClient_set_tunnelType( - pointer->clitable[l], - ConnectClient_get_tunnelType(pointer->raclitable[k])); + srClientsTable[l], + ConnectClient_get_tunnelType(srRaClientsTable[k])); #endif ConnectClient_set_clientId( - pointer->clitable[l], - ConnectClient_get_clientId(pointer->raclitable[k])); + srClientsTable[l], + ConnectClient_get_clientId(srRaClientsTable[k])); ConnectClient_set_nameBuf( - pointer->clitable[l], - ConnectClient_get_nameBuf(pointer->raclitable[k])); + srClientsTable[l], + ConnectClient_get_nameBuf(srRaClientsTable[k])); ConnectClient_set_portBuf( - pointer->clitable[l], - ConnectClient_get_portBuf(pointer->raclitable[k])); + srClientsTable[l], + ConnectClient_get_portBuf(srRaClientsTable[k])); tmp_ssl = SslFd_get_ssl( - ConnectClient_get_sslFd(pointer->clitable[l])); + ConnectClient_get_sslFd(srClientsTable[l])); SslFd_set_ssl_nf( - ConnectClient_get_sslFd(pointer->clitable[l]), + ConnectClient_get_sslFd(srClientsTable[l]), SslFd_get_ssl( - ConnectClient_get_sslFd(pointer->raclitable[k]))); + ConnectClient_get_sslFd(srRaClientsTable[k]))); SslFd_set_ssl_nf( - ConnectClient_get_sslFd(pointer->raclitable[k]), + ConnectClient_get_sslFd(srRaClientsTable[k]), tmp_ssl); ConnectClient_set_usrCliPair( - pointer->clitable[l], - ConnectClient_get_usrCliPair(pointer->raclitable[k])); - ConnectClient_set_state(pointer->raclitable[k], CONNECTCLIENT_STATE_FREE); + srClientsTable[l], + ConnectClient_get_usrCliPair(srRaClientsTable[k])); + ConnectClient_set_state(srRaClientsTable[k], CONNECTCLIENT_STATE_FREE); break; } } - if (l != pointer->clinum) { - ConnectClient_set_state(pointer->clitable[l], CONNECTCLIENT_STATE_ACCEPTED); + if (l != ServerRealm_get_clientsLimit(pointer)) { + ConnectClient_set_state(srClientsTable[l], CONNECTCLIENT_STATE_ACCEPTED); aflog(LOG_T_CLIENT, LOG_I_INFO, "realm[%s]: Client[%s]: pass ok - ACCESS GRANTED", - get_realmname(&config, j), get_clientname(pointer, l)); + get_realmname(config, j), get_clientname(pointer, l)); buff[0] = AF_S_LOGIN; /* sending message */ buff[1] = ConnectClient_get_limit( - pointer->clitable[l]) >> 8;/* high bits of user number */ + srClientsTable[l]) >> 8;/* high bits of user number */ buff[2] = ConnectClient_get_limit( - pointer->clitable[l]); /* low bits of user number */ - buff[3] = pointer->type; /* type of connection */ - SslFd_send_message(pointer->type | TYPE_SSL | TYPE_ZLIB, + srClientsTable[l]); /* low bits of user number */ + buff[3] = ServerRealm_get_realmType(pointer); /* type of connection */ + SslFd_send_message(ServerRealm_get_realmType(pointer) | TYPE_SSL | TYPE_ZLIB, ConnectClient_get_sslFd( - pointer->clitable[l]), + srClientsTable[l]), buff, 5); manconnecting--; - if (pointer->baseport == 1) { + if (ServerRealm_get_basePortOn(pointer) == 1) { long tmp_val; char tmp_tab[6]; if (check_long( UsrCli_get_listenPortName( - pointer->usrclitable[ - ConnectClient_get_usrCliPair(pointer->clitable[l])]), + srUsersClientsTable[ + ConnectClient_get_usrCliPair(srClientsTable[l])]), &tmp_val)) { aflog(LOG_T_CLIENT, LOG_I_ERR, "realm[%s]: INVALID listenport - removing Client[%s]", - get_realmname(&config, j), get_clientname(pointer, l)); + get_realmname(config, j), get_clientname(pointer, l)); remove_client(pointer, l, &allset, &wset, &manconnecting); break; } @@ -2001,59 +2127,60 @@ main(int argc, char **argv) sprintf(tmp_tab, "%d", (int)tmp_val); ipfam = 0x01; #ifdef AF_INET6 - if (TYPE_IS_IPV4(pointer->type)) { + if (TYPE_IS_IPV4(ServerRealm_get_realmType(pointer))) { ipfam |= 0x02; } - else if (TYPE_IS_IPV6(pointer->type)) { + else if (TYPE_IS_IPV6(ServerRealm_get_realmType(pointer))) { ipfam |= 0x04; } #endif - while (ip_listen(ConnectClient_get_listenFdp(pointer->clitable[l]), - UsrCli_get_listenHostName(pointer->usrclitable[ - ConnectClient_get_usrCliPair(pointer->clitable[l])]) ? - UsrCli_get_listenHostName(pointer->usrclitable[ - ConnectClient_get_usrCliPair(pointer->clitable[l])]) : - pointer->hostname, - tmp_tab, (&(pointer->addrlen)), ipfam)) { + while (ip_listen(ConnectClient_get_listenFdp(srClientsTable[l]), + UsrCli_get_listenHostName(srUsersClientsTable[ + ConnectClient_get_usrCliPair(srClientsTable[l])]) ? + UsrCli_get_listenHostName(srUsersClientsTable[ + ConnectClient_get_usrCliPair(srClientsTable[l])]) : + ServerRealm_get_hostName(pointer), + tmp_tab, (&len), ipfam)) { tmp_val = (tmp_val+1)%65536; memset(tmp_tab, 0, 6); sprintf(tmp_tab, "%d", (int)tmp_val); } - FD_SET(ConnectClient_get_listenFd(pointer->clitable[l]), &allset); - maxfdp1 = (maxfdp1>(ConnectClient_get_listenFd(pointer->clitable[l])+1)) ? - maxfdp1 : (ConnectClient_get_listenFd(pointer->clitable[l])+1); + ServerRealm_set_addressLength(pointer, len); + FD_SET(ConnectClient_get_listenFd(srClientsTable[l]), &allset); + maxfdp1 = (maxfdp1>(ConnectClient_get_listenFd(srClientsTable[l])+1)) ? + maxfdp1 : (ConnectClient_get_listenFd(srClientsTable[l])+1); aflog(LOG_T_CLIENT, LOG_I_INFO, "realm[%s]: Client[%s]: listenport=%s", - get_realmname(&config, j), get_clientname(pointer, l), tmp_tab); + get_realmname(config, j), get_clientname(pointer, l), tmp_tab); } } else { aflog(LOG_T_CLIENT, LOG_I_WARNING, - "realm[%s]: client limit EXCEEDED", get_realmname(&config, j)); + "realm[%s]: client limit EXCEEDED", get_realmname(config, j)); buff[0] = AF_S_CANT_OPEN; /* sending message */ - SslFd_send_message(pointer->type | TYPE_SSL | TYPE_ZLIB, + SslFd_send_message(ServerRealm_get_realmType(pointer) | TYPE_SSL | TYPE_ZLIB, ConnectClient_get_sslFd( - pointer->raclitable[k]), + srRaClientsTable[k]), buff, 5); remove_raclient(pointer, k, &allset, &wset, &manconnecting); } } - else if ((ConnectClient_get_state(pointer->raclitable[k]) == + else if ((ConnectClient_get_state(srRaClientsTable[k]) == CONNECTCLIENT_STATE_ACCEPTED) && (numofcon == 0)) { - n = SslFd_get_message(pointer->type, + n = SslFd_get_message(ServerRealm_get_realmType(pointer), ConnectClient_get_sslFd( - pointer->raclitable[k]), + srRaClientsTable[k]), buff, length); buff[n] = 0; aflog(LOG_T_MANAGE, LOG_I_INFO, "realm[%s]: Client[%s] (ra): ID received: %s", - get_realmname(&config, j), get_raclientname(pointer, k), buff); - ConnectClient_set_sClientId(pointer->raclitable[k], (char*) buff); + get_realmname(config, j), get_raclientname(pointer, k), buff); + ConnectClient_set_sClientId(srRaClientsTable[k], (char*) buff); } else { aflog(LOG_T_MANAGE, LOG_I_ERR, "realm[%s]: Client[%s] (ra): Wrong password - CLOSING", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); remove_raclient(pointer, k, &allset, &wset, &manconnecting); } break; @@ -2061,104 +2188,105 @@ main(int argc, char **argv) case AF_S_WRONG: { aflog(LOG_T_MANAGE, LOG_I_ERR, "realm[%s]: Client[%s] (ra): Wrong message - CLOSING", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); remove_raclient(pointer, k, &allset, &wset, &manconnecting); break; } case AF_S_ADMIN_LOGIN: { - if ((ConnectClient_get_state(pointer->raclitable[k]) == + if ((ConnectClient_get_state(srRaClientsTable[k]) == CONNECTCLIENT_STATE_AUTHORIZING) && - (numofcon==(pointer->pass[0]*256+pointer->pass[1])) && - (length==(pointer->pass[2]*256+pointer->pass[3]))) { + (numofcon==(ServerRealm_get_password(pointer)[0]*256+ServerRealm_get_password(pointer)[1])) && + (length==(ServerRealm_get_password(pointer)[2]*256+ServerRealm_get_password(pointer)[3]))) { aflog(LOG_T_MANAGE, LOG_I_INFO, "realm[%s]: Client[%s] (ra): NEW remote admin -- pass OK", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); ConnectClient_set_state( - pointer->raclitable[k], + srRaClientsTable[k], CONNECTCLIENT_STATE_ACCEPTED); - pointer->raclicon++; + ServerRealm_increase_connectedRaClients(pointer); manconnecting--; sprintf((char*) &buff[5], AF_VER("AFSERVER")); n = strlen((char*) &buff[5]); buff[0] = AF_S_ADMIN_LOGIN; /* sending message */ - buff[1] = pointer->type; /* type of connection */ + buff[1] = ServerRealm_get_realmType(pointer); /* type of connection */ buff[2] = AF_RA_UNDEFINED; buff[3] = n >> 8; /* high bits of message length */ buff[4] = n; /* low bits of message length */ - SslFd_send_message(pointer->type | TYPE_SSL | TYPE_ZLIB, + SslFd_send_message(ServerRealm_get_realmType(pointer) | TYPE_SSL | TYPE_ZLIB, ConnectClient_get_sslFd( - pointer->raclitable[k]), + srRaClientsTable[k]), buff, n+5); } break; } case AF_S_ADMIN_CMD: { - if (ConnectClient_get_state(pointer->raclitable[k]) == + if (ConnectClient_get_state(srRaClientsTable[k]) == CONNECTCLIENT_STATE_ACCEPTED) { - if ((n = serve_admin(&config, j, k, buff))) { + if ((n = serve_admin(config, j, k, buff))) { if (n == 1) { aflog(LOG_T_MANAGE, LOG_I_NOTICE, "realm[%s]: Client[%s] (ra): remote admin -- closing", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); remove_raclient(pointer, k, &allset, &wset, &manconnecting); } else { - for (i = 0; i < config.size; ++i) { - l = get_clientnumber(&(config.realmtable[i]), n-2); + for (i = 0; i < ServerConfiguration_get_realmsNumber(config); ++i) { + srClientsTable = ServerRealm_get_clientsTable(scRealmsTable[i]); + l = get_clientnumber(scRealmsTable[i], n-2); if (l != -1) { aflog(LOG_T_MANAGE, LOG_I_NOTICE, "realm[%s]: Client[%s] (ra): remote admin: KICKING realm[%s]: Client[%s]", - get_realmname(&config, j), get_raclientname(pointer, k), - get_realmname(&config, i), - get_clientname(&(config.realmtable[i]), l)); + get_realmname(config, j), get_raclientname(pointer, k), + get_realmname(config, i), + get_clientname(scRealmsTable[i], l)); buff[0] = AF_S_CLOSING; /* closing */ - SslFd_send_message(config.realmtable[i].type, + SslFd_send_message(ServerRealm_get_realmType(scRealmsTable[i]), ConnectClient_get_sslFd( - config.realmtable[i].clitable[l]), + srClientsTable[l]), buff, 5); time(&now); aflog(LOG_T_CLIENT, LOG_I_NOTICE, "REALM: %s CLIENT: %s IP: %s PORT: %s DURATION: %s", - get_realmname(&config, j), - get_clientname(&(config.realmtable[i]), l), - ConnectClient_get_nameBuf(config.realmtable[i].clitable[l]), - ConnectClient_get_portBuf(config.realmtable[i].clitable[l]), + get_realmname(config, j), + get_clientname(scRealmsTable[i], l), + ConnectClient_get_nameBuf(srClientsTable[l]), + ConnectClient_get_portBuf(srClientsTable[l]), timeperiod(now - ConnectClient_get_connectTime( - config.realmtable[i].clitable[l]))); - if (config.realmtable[i].audit) { + srClientsTable[l]))); + if (ServerRealm_get_auditOn(scRealmsTable[i])) { while (AuditList_get_first( ConnectClient_get_auditList( - config.realmtable[i].clitable[l]))) { + srClientsTable[l]))) { aflog(LOG_T_CLIENT, LOG_I_NOTICE, "USERID: %d IP: %s PORT: %s CONNECTED: %s DURATION: %s", AuditListNode_get_userId( AuditList_get_first( ConnectClient_get_auditList( - config.realmtable[i].clitable[l]))), + srClientsTable[l]))), AuditListNode_get_nameBuf( AuditList_get_first( ConnectClient_get_auditList( - config.realmtable[i].clitable[l]))), + srClientsTable[l]))), AuditListNode_get_portBuf( AuditList_get_first( ConnectClient_get_auditList( - config.realmtable[i].clitable[l]))), + srClientsTable[l]))), localdate( AuditListNode_get_connectTimep( AuditList_get_first( ConnectClient_get_auditList( - config.realmtable[i].clitable[l])))), + srClientsTable[l])))), timeperiod( AuditListNode_get_duration( AuditList_get_first( ConnectClient_get_auditList( - config.realmtable[i].clitable[l]))))); + srClientsTable[l]))))); AuditList_delete_first( ConnectClient_get_auditList( - config.realmtable[i].clitable[l])); + srClientsTable[l])); } } - remove_client(&(config.realmtable[i]), l, + remove_client(scRealmsTable[i], l, &allset, &wset, &manconnecting); break; } @@ -2169,7 +2297,7 @@ main(int argc, char **argv) else { aflog(LOG_T_MANAGE, LOG_I_ERR, "realm[%s]: Client[%s] (ra): remote admin -- security VIOLATION", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); remove_raclient(pointer, k, &allset, &wset, &manconnecting); } break; @@ -2177,104 +2305,105 @@ main(int argc, char **argv) case AF_S_KEEP_ALIVE: { aflog(LOG_T_MANAGE, LOG_I_DEBUG, "realm[%s]: Client[%s] (ra): Keep alive packet", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); break; } default : { aflog(LOG_T_MANAGE, LOG_I_ERR, "realm[%s]: Client[%s] (ra): Unrecognized message - CLOSING", - get_realmname(&config, j), get_raclientname(pointer, k)); + get_realmname(config, j), get_raclientname(pointer, k)); remove_raclient(pointer, k, &allset, &wset, &manconnecting); } } } /* ------------------------------------ */ - for (l = 0; l < pointer->usrclinum; ++l) { - if (FD_ISSET(UsrCli_get_manageFd(pointer->usrclitable[l]), &rset)) { + for (l = 0; l < ServerRealm_get_userClientPairs(pointer); ++l) { + if (FD_ISSET(UsrCli_get_manageFd(srUsersClientsTable[l]), &rset)) { aflog(LOG_T_CLIENT, LOG_I_DDEBUG, - "realm[%s]: managefd: FD_ISSET", get_realmname(&config, j)); - len = pointer->addrlen; + "realm[%s]: managefd: FD_ISSET", get_realmname(config, j)); + len = ServerRealm_get_addressLength(pointer); #ifdef HAVE_LIBPTHREAD - sent = get_new_socket(UsrCli_get_manageFd(pointer->usrclitable[l]), - pointer->tunneltype,pointer->cliaddr, &len, &tunneltype); + sent = get_new_socket(UsrCli_get_manageFd(srUsersClientsTable[l]), + ServerRealm_get_tunnelType(pointer),ServerRealm_get_clientAddress(pointer), &len, &tunneltype); #else - sent = accept(UsrCli_get_manageFd(pointer->usrclitable[l]), pointer->cliaddr, &len); + sent = accept(UsrCli_get_manageFd(srUsersClientsTable[l]), ServerRealm_get_clientAddress(pointer), &len); #endif if (sent == -1) { if (errno == EAGAIN) { aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: managefd: FD_ISSET --> EAGAIN", get_realmname(&config, j)); + "realm[%s]: managefd: FD_ISSET --> EAGAIN", get_realmname(config, j)); } else { aflog(LOG_T_USER, LOG_I_DDEBUG, - "realm[%s]: managefd: FD_ISSET --> errno=%d", get_realmname(&config, j), errno); + "realm[%s]: managefd: FD_ISSET --> errno=%d", get_realmname(config, j), errno); } continue; } flags = fcntl(sent, F_GETFL, 0); fcntl(sent, F_SETFL, flags | O_NONBLOCK); - for (k = 0; k < pointer->clinum; ++k) { - if (ConnectClient_get_state(pointer->clitable[k]) == CONNECTCLIENT_STATE_FREE) { - ConnectClient_set_clientId(pointer->clitable[k], pointer->clientcounter); - ++(pointer->clientcounter); + for (k = 0; k < ServerRealm_get_clientsLimit(pointer); ++k) { + if (ConnectClient_get_state(srClientsTable[k]) == CONNECTCLIENT_STATE_FREE) { + ConnectClient_set_clientId(srClientsTable[k], ServerRealm_get_clientsCounter(pointer)); + ServerRealm_increase_clientsCounter(pointer); aflog(LOG_T_CLIENT, LOG_I_INFO, "realm[%s]: new Client[%s]: CONNECTING", - get_realmname(&config, j), get_clientname(pointer, k)); - SslFd_set_fd(ConnectClient_get_sslFd(pointer->clitable[k]), sent); - ConnectClient_set_usrCliPair(pointer->clitable[k], l); + get_realmname(config, j), get_clientname(pointer, k)); + SslFd_set_fd(ConnectClient_get_sslFd(srClientsTable[k]), sent); + ConnectClient_set_usrCliPair(srClientsTable[k], l); time(&now); - ConnectClient_set_connectTime(pointer->clitable[k], now); + ConnectClient_set_connectTime(srClientsTable[k], now); #ifdef HAVE_LIBPTHREAD - ConnectClient_set_tunnelType(pointer->clitable[k], tunneltype); + ConnectClient_set_tunnelType(srClientsTable[k], tunneltype); #endif aflog(LOG_T_CLIENT, LOG_I_INFO, - "realm[%s]: new Client[%s] IP:%s", get_realmname(&config, j), get_clientname(pointer, k), - sock_ntop(pointer->cliaddr, len, ConnectClient_get_nameBuf(pointer->clitable[k]), - ConnectClient_get_portBuf(pointer->clitable[k]), pointer->dnslookups)); - FD_SET(SslFd_get_fd(ConnectClient_get_sslFd(pointer->clitable[k])), &allset); - maxfdp1 = (maxfdp1 > (SslFd_get_fd(ConnectClient_get_sslFd(pointer->clitable[k])) + 1)) ? - maxfdp1 : (SslFd_get_fd(ConnectClient_get_sslFd(pointer->clitable[k])) + 1); - pointer->clicon++; - ConnectClient_set_timer(pointer->clitable[k], timeval_create(pointer->tmout, 0)); + "realm[%s]: new Client[%s] IP:%s", get_realmname(config, j), get_clientname(pointer, k), + sock_ntop(ServerRealm_get_clientAddress(pointer), len, ConnectClient_get_nameBuf(srClientsTable[k]), + ConnectClient_get_portBuf(srClientsTable[k]), ServerRealm_get_dnsLookupsOn(pointer))); + FD_SET(SslFd_get_fd(ConnectClient_get_sslFd(srClientsTable[k])), &allset); + maxfdp1 = (maxfdp1 > (SslFd_get_fd(ConnectClient_get_sslFd(srClientsTable[k])) + 1)) ? + maxfdp1 : (SslFd_get_fd(ConnectClient_get_sslFd(srClientsTable[k])) + 1); + ServerRealm_increase_connectedClients(pointer); + ConnectClient_set_timer(srClientsTable[k], timeval_create(ServerRealm_get_timeout(pointer), 0)); manconnecting++; - ConnectClient_set_state(pointer->clitable[k], CONNECTCLIENT_STATE_CONNECTING); + ConnectClient_set_state(srClientsTable[k], CONNECTCLIENT_STATE_CONNECTING); break; } } - if (k == pointer->clinum) { - for (k = 0; k < pointer->raclinum; ++k) { - if (ConnectClient_get_state(pointer->raclitable[k]) == + if (k == ServerRealm_get_clientsLimit(pointer)) { + for (k = 0; k < ServerRealm_get_raClientsLimit(pointer); ++k) { + if (ConnectClient_get_state(srRaClientsTable[k]) == CONNECTCLIENT_STATE_FREE) { - ConnectClient_set_clientId(pointer->raclitable[k], pointer->clientcounter); - ++(pointer->clientcounter); + ConnectClient_set_clientId(srRaClientsTable[k], ServerRealm_get_clientsCounter(pointer)); + ServerRealm_increase_clientsCounter(pointer); aflog(LOG_T_MANAGE, LOG_I_INFO, "realm[%s]: new Client[%s] (ra): CONNECTING", - get_realmname(&config, j), get_raclientname(pointer, k)); - SslFd_set_fd(ConnectClient_get_sslFd(pointer->raclitable[k]), sent); - ConnectClient_set_usrCliPair(pointer->raclitable[k], l); + get_realmname(config, j), get_raclientname(pointer, k)); + SslFd_set_fd(ConnectClient_get_sslFd(srRaClientsTable[k]), sent); + ConnectClient_set_usrCliPair(srRaClientsTable[k], l); time(&now); - ConnectClient_set_connectTime(pointer->raclitable[k], now); + ConnectClient_set_connectTime(srRaClientsTable[k], now); #ifdef HAVE_LIBPTHREAD - ConnectClient_set_tunnelType(pointer->raclitable[k], tunneltype); + ConnectClient_set_tunnelType(srRaClientsTable[k], tunneltype); #endif aflog(LOG_T_MANAGE, LOG_I_INFO, "realm[%s]: new Client[%s] (ra) IP:%s", - get_realmname(&config, j), get_raclientname(pointer, k), - sock_ntop(pointer->cliaddr, len, ConnectClient_get_nameBuf(pointer->raclitable[k]), - ConnectClient_get_portBuf(pointer->raclitable[k]), pointer->dnslookups)); - FD_SET(SslFd_get_fd(ConnectClient_get_sslFd(pointer->raclitable[k])), &allset); - maxfdp1 = (maxfdp1 > (SslFd_get_fd(ConnectClient_get_sslFd(pointer->raclitable[k])) + 1)) ? - maxfdp1 : (SslFd_get_fd(ConnectClient_get_sslFd(pointer->raclitable[k])) + 1); - pointer->clicon++; - ConnectClient_set_timer(pointer->raclitable[k], timeval_create(pointer->tmout, 0)); + get_realmname(config, j), get_raclientname(pointer, k), + sock_ntop(ServerRealm_get_clientAddress(pointer), len, ConnectClient_get_nameBuf(srRaClientsTable[k]), + ConnectClient_get_portBuf(srRaClientsTable[k]), ServerRealm_get_dnsLookupsOn(pointer))); + FD_SET(SslFd_get_fd(ConnectClient_get_sslFd(srRaClientsTable[k])), &allset); + maxfdp1 = (maxfdp1 > (SslFd_get_fd(ConnectClient_get_sslFd(srRaClientsTable[k])) + 1)) ? + maxfdp1 : (SslFd_get_fd(ConnectClient_get_sslFd(srRaClientsTable[k])) + 1); + ServerRealm_increase_connectedClients(pointer); + ConnectClient_set_timer(srRaClientsTable[k], + timeval_create(ServerRealm_get_timeout(pointer), 0)); manconnecting++; - ConnectClient_set_state(pointer->raclitable[k], CONNECTCLIENT_STATE_CONNECTING); + ConnectClient_set_state(srRaClientsTable[k], CONNECTCLIENT_STATE_CONNECTING); break; } } - if (k == pointer->raclinum) { + if (k == ServerRealm_get_raClientsLimit(pointer)) { aflog(LOG_T_CLIENT | LOG_T_MANAGE, LOG_I_WARNING, - "realm[%s]: client limit EXCEEDED", get_realmname(&config, j)); + "realm[%s]: client limit EXCEEDED", get_realmname(config, j)); close(sent); } } diff --git a/src/ar_options_struct.c b/src/ar_options_struct.c index 0d95dfc..acbfb74 100644 --- a/src/ar_options_struct.c +++ b/src/ar_options_struct.c @@ -291,6 +291,6 @@ ArOptions_evaluate_values(ArOptions* ao) ao->tries = check_value_liberal(ao->artries, "Invalid ar-tries value"); } if (ao->ardelay) { - check_value(&(ao->delay), ao->ardelay, "Invalid ar-delay value"); + ao->delay = check_value(ao->ardelay, "Invalid ar-delay value"); } } diff --git a/src/client_configuration_struct.c b/src/client_configuration_struct.c new file mode 100644 index 0000000..6de1b5b --- /dev/null +++ b/src/client_configuration_struct.c @@ -0,0 +1,282 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include <config.h> + +#include <stdlib.h> +#include <string.h> + +#include "string_functions.h" +#include "client_configuration_struct.h" + +/* + * Function name: ClientConfiguration_new + * Description: Create and initialize new ClientConfiguration structure. + * Returns: Pointer to newly created ClientConfiguration structure. + */ + +ClientConfiguration* +ClientConfiguration_new() +{ + ClientConfiguration* tmp = calloc(1, sizeof(ClientConfiguration)); + if (tmp == NULL) { + return NULL; + } + return tmp; +} + +/* + * Function name: ClientConfiguration_free + * Description: Free the memory allocated for ClientConfiguration structure. + * Arguments: cc - pointer to pointer to ClientConfiguration structure + */ + +void +ClientConfiguration_free(ClientConfiguration** cc) +{ + int i; + if (cc == NULL) { + return; + } + if ((*cc) == NULL) { + return; + } + if ((*cc)->keysFile) { + free((*cc)->keysFile); + (*cc)->keysFile = NULL; + } + if ((*cc)->storeFile) { + free((*cc)->storeFile); + (*cc)->storeFile = NULL; + } + if ((*cc)->realmsTable) { + for (i = 0; i < (*cc)->realmsNumber; ++i) { + if ((*cc)->realmsTable[i]) { + ClientRealm_free(&((*cc)->realmsTable[i])); + } + } + free((*cc)->realmsTable); + (*cc)->realmsTable = NULL; + } + free((*cc)); + (*cc) = NULL; +} + +/* + * Function name: ClientConfiguration_set_keysFile + * Description: Set keys filename. + * Arguments: cc - pointer to ClientConfiguration structure + * keysFile - keys filename + */ + +void +ClientConfiguration_set_keysFile(ClientConfiguration* cc, char* keysFile) +{ + if (cc == NULL) { + return; + } + string_cp(&(cc->keysFile), keysFile); +} + +/* + * Function name: ClientConfiguration_set_storeFile + * Description: Set store filename. + * Arguments: cc - pointer to ClientConfiguration structure + * storeFile - store filename + */ + +void +ClientConfiguration_set_storeFile(ClientConfiguration* cc, char* storeFile) +{ + if (cc == NULL) { + return; + } + string_cp(&(cc->storeFile), storeFile); +} + +/* + * Function name: ClientConfiguration_set_dateFormat + * Description: Set format of the date string. + * Arguments: cc - pointer to ClientConfiguration structure + * dateFormat - format of the date string + */ + +void +ClientConfiguration_set_dateFormat(ClientConfiguration* cc, char* dateFormat) +{ + if (cc == NULL) { + return; + } + string_cp(&(cc->dateFormat), dateFormat); +} + +/* + * Function name: ClientConfiguration_set_realmsNumber + * Description: Set number of realms. + * Arguments: cc - pointer to ClientConfiguration structure + * realmsNumber - number of realms + */ + +void +ClientConfiguration_set_realmsNumber(ClientConfiguration* cc, int realmsNumber) +{ + if (cc == NULL) { + return; + } + cc->realmsNumber = realmsNumber; +} + +/* + * Function name: ClientConfiguration_set_realmsTable + * Description: Set table of realms. + * Arguments: cc - pointer to ClientConfiguration structure + * realmsTable - table of realms + */ + +void +ClientConfiguration_set_realmsTable(ClientConfiguration* cc, ClientRealm** realmsTable) +{ + int i; + if (cc == NULL) { + return; + } + if (cc->realmsTable) { + for (i = 0; i < cc->realmsNumber; ++i) { + if (cc->realmsTable[i]) { + ClientRealm_free(&(cc->realmsTable[i])); + } + } + free(cc->realmsTable); + cc->realmsTable = NULL; + } + cc->realmsTable = realmsTable; +} + +/* + * Function name: ClientConfiguration_set_ignorePublicKeys + * Description: Enable/disable the public keys checking. + * Arguments: cc - pointer to ClientConfiguration structure + * ignorePublicKeys - if the public keys checking is enabled/disabled + */ + +void +ClientConfiguration_set_ignorePublicKeys(ClientConfiguration* cc, char ignorePublicKeys) +{ + if (cc == NULL) { + return; + } + cc->ignorePublicKeys = ignorePublicKeys; +} + +/* + * Function name: ClientConfiguration_get_keysFile + * Description: Get keys filename. + * Arguments: cc - pointer to ClientConfiguration structure + * Returns: Keys filename. + */ + +char* +ClientConfiguration_get_keysFile(ClientConfiguration* cc) +{ + if (cc == NULL) { + return NULL; + } + return cc->keysFile; +} + +/* + * Function name: ClientConfiguration_get_storeFile + * Description: Get store filename. + * Arguments: cc - pointer to ClientConfiguration structure + * Returns: Store filename. + */ + +char* +ClientConfiguration_get_storeFile(ClientConfiguration* cc) +{ + if (cc == NULL) { + return NULL; + } + return cc->storeFile; +} + +/* + * Function name: ClientConfiguration_get_dateFormat + * Description: Get format of the date string. + * Arguments: cc - pointer to ClientConfiguration structure + * Returns: Format of the date string. + */ + +char* +ClientConfiguration_get_dateFormat(ClientConfiguration* cc) +{ + if (cc == NULL) { + return NULL; + } + return cc->dateFormat; +} + +/* + * Function name: ClientConfiguration_get_realmsNumber + * Description: Get number of realms. + * Arguments: cc - pointer to ClientConfiguration structure + * Returns: Number of realms. + */ + +int +ClientConfiguration_get_realmsNumber(ClientConfiguration* cc) +{ + if (cc == NULL) { + return -1; + } + return cc->realmsNumber; +} + +/* + * Function name: ClientConfiguration_get_realmsTable + * Description: Get table of realms. + * Arguments: cc - pointer to ClientConfiguration structure + * Returns: Table of realms. + */ + +ClientRealm** +ClientConfiguration_get_realmsTable(ClientConfiguration* cc) +{ + if (cc == NULL) { + return NULL; + } + return cc->realmsTable; +} + +/* + * Function name: ClientConfiguration_get_ignorePublicKeys + * Description: Check if the public keys checking is enabled/disabled + * Arguments: cc - pointer to ClientConfiguration structure + * Returns: If the public keys checking is enabled/disabled. + */ + +char +ClientConfiguration_get_ignorePublicKeys(ClientConfiguration* cc) +{ + if (cc == NULL) { + return 0; + } + return cc->ignorePublicKeys; +} diff --git a/src/client_configuration_struct.h b/src/client_configuration_struct.h new file mode 100644 index 0000000..a1292e4 --- /dev/null +++ b/src/client_configuration_struct.h @@ -0,0 +1,55 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + + +#ifndef _JS_CLIENT_CONFIGURATION_STRUCT_H +#define _JS_CLIENT_CONFIGURATION_STRUCT_H + +#include "client_realm_struct.h" + +typedef struct { + char* keysFile; + char* storeFile; + char* dateFormat; + int realmsNumber; + char ignorePublicKeys; + ClientRealm** realmsTable; +} ClientConfiguration; + +/* 'constructor' */ +ClientConfiguration* ClientConfiguration_new(); +/* 'destructor' */ +void ClientConfiguration_free(ClientConfiguration** cc); +/* setters */ +void ClientConfiguration_set_keysFile(ClientConfiguration* cc, char* keysFile); +void ClientConfiguration_set_storeFile(ClientConfiguration* cc, char* storeFile); +void ClientConfiguration_set_dateFormat(ClientConfiguration* cc, char* dateFormat); +void ClientConfiguration_set_realmsNumber(ClientConfiguration* cc, int realmsNumber); +void ClientConfiguration_set_realmsTable(ClientConfiguration* cc, ClientRealm** realmsTable); +void ClientConfiguration_set_ignorePublicKeys(ClientConfiguration* cc, char ignorePublicKeys); +/* getters */ +char* ClientConfiguration_get_keysFile(ClientConfiguration* cc); +char* ClientConfiguration_get_storeFile(ClientConfiguration* cc); +char* ClientConfiguration_get_dateFormat(ClientConfiguration* cc); +int ClientConfiguration_get_realmsNumber(ClientConfiguration* cc); +ClientRealm** ClientConfiguration_get_realmsTable(ClientConfiguration* cc); +char ClientConfiguration_get_ignorePublicKeys(ClientConfiguration* cc); + +#endif diff --git a/src/client_initialization.c b/src/client_initialization.c index 82ac632..0b5bc14 100644 --- a/src/client_initialization.c +++ b/src/client_initialization.c @@ -25,11 +25,11 @@ #include "network.h" #include "base64.h" #include "ssl_routines.h" +#include "client_configuration_struct.h" int -initialize_client_stage1(char tunneltype, SslFd* master, char* name, char* manage, - HttpProxyOptions* hpo, char ipfam, SSL_CTX* ctx, unsigned char* buff, unsigned char* pass, - char wanttoexit, char ignorepkeys) +initialize_client_stage1(ClientRealm* cr, SSL_CTX* ctx, unsigned char* buff, char wanttoexit, + char ignorePublicKeys) { int n, nlen, elen, len, tmp; unsigned int olen; @@ -40,16 +40,22 @@ initialize_client_stage1(char tunneltype, SslFd* master, char* name, char* manag unsigned char *encoded = NULL; char b64_encoded[100]; unsigned char *key_buf = NULL; - switch (tunneltype) { + switch (ClientRealm_get_tunnelType(cr)) { case 0: { - if (ip_connect(&tmp, name, manage, ipfam)) { + if (ip_connect(&tmp, ClientRealm_get_serverName(cr), + ClientRealm_get_managePort(cr), + ClientRealm_get_ipFamily(cr), + ClientRealm_get_localName(cr), + ClientRealm_get_localPort(cr))) { #ifdef AF_INET6 aflog(LOG_T_INIT, LOG_I_CRIT, "tcp_connect_%s error for %s, %s", - (ipfam & 0x02)?"ipv4":(ipfam & 0x04)?"ipv6":"unspec", name, manage); + (ClientRealm_get_ipFamily(cr) & 0x02) ? + "ipv4":(ClientRealm_get_ipFamily(cr) & 0x04) ? + "ipv6":"unspec", ClientRealm_get_serverName(cr), ClientRealm_get_managePort(cr)); #else aflog(LOG_T_INIT, LOG_I_CRIT, - "tcp_connect error for %s, %s", name, manage); + "tcp_connect error for %s, %s", ClientRealm_get_serverName(cr), ClientRealm_get_managePort(cr)); #endif if (wanttoexit) { exit(1); @@ -58,21 +64,27 @@ initialize_client_stage1(char tunneltype, SslFd* master, char* name, char* manag return 1; } } - SslFd_set_fd(master, tmp); + SslFd_set_fd(ClientRealm_get_masterSslFd(cr), tmp); break; } #ifdef HAVE_LIBPTHREAD case 1: { - if (initialize_http_proxy_client(&tmp, name, manage, hpo, ipfam, ctx)) { + if (initialize_http_proxy_client(&tmp, cr, ctx)) { #ifdef AF_INET6 aflog(LOG_T_INIT, LOG_I_CRIT, "http_proxy_connect_%s error for %s, %s (proxy: %s, %s)", - (ipfam & 0x02)?"ipv4":(ipfam & 0x04)?"ipv6":"unspec", name, manage, - HttpProxyOptions_get_proxyname(hpo), HttpProxyOptions_get_proxyport(hpo)); + (ClientRealm_get_ipFamily(cr) & 0x02) ? + "ipv4":(ClientRealm_get_ipFamily(cr) & 0x04) ? + "ipv6":"unspec", ClientRealm_get_serverName(cr), + ClientRealm_get_managePort(cr), + HttpProxyOptions_get_proxyname(ClientRealm_get_httpProxyOptions(cr)), + HttpProxyOptions_get_proxyport(ClientRealm_get_httpProxyOptions(cr))); #else aflog(LOG_T_INIT, LOG_I_CRIT, - "http_proxy_connect error for %s, %s (proxy: %s, %s)", name, manage, - HttpProxyOptions_get_proxyname(hpo), HttpProxyOptions_get_proxyport(hpo)); + "http_proxy_connect error for %s, %s (proxy: %s, %s)", ClientRealm_get_serverName(cr), + ClientRealm_get_managePort(cr), + HttpProxyOptions_get_proxyname(ClientRealm_get_httpProxyOptions(cr)), + HttpProxyOptions_get_proxyport(ClientRealm_get_httpProxyOptions(cr))); #endif if (wanttoexit) { exit(1); @@ -81,7 +93,7 @@ initialize_client_stage1(char tunneltype, SslFd* master, char* name, char* manag return 1; } } - SslFd_set_fd(master, tmp); + SslFd_set_fd(ClientRealm_get_masterSslFd(cr), tmp); break; } #endif @@ -98,22 +110,24 @@ initialize_client_stage1(char tunneltype, SslFd* master, char* name, char* manag } } - master->ssl = SSL_new(ctx); - if (SSL_set_fd(SslFd_get_ssl(master), SslFd_get_fd(master)) != 1) { + SslFd_set_ssl(ClientRealm_get_masterSslFd(cr), SSL_new(ctx)); + if (SSL_set_fd(SslFd_get_ssl(ClientRealm_get_masterSslFd(cr)), + SslFd_get_fd(ClientRealm_get_masterSslFd(cr))) != 1) { aflog(LOG_T_INIT, LOG_I_CRIT, "Problem with initializing ssl... exiting"); if (wanttoexit) { exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 2; } } aflog(LOG_T_INIT, LOG_I_INFO, "Trying SSL_connect"); - if ((n = SSL_connect(master->ssl)) == 1) { - if ((server_cert = SSL_get_peer_certificate(master->ssl)) == NULL) { + if ((n = SSL_connect(SslFd_get_ssl(ClientRealm_get_masterSslFd(cr)))) == 1) { + if ((server_cert = SSL_get_peer_certificate(SslFd_get_ssl(ClientRealm_get_masterSslFd(cr)))) == NULL) { aflog(LOG_T_MAIN, LOG_I_CRIT, "Server did not present a certificate... exiting"); exit(1); @@ -153,17 +167,17 @@ initialize_client_stage1(char tunneltype, SslFd* master, char* name, char* manag exit(1); } - switch (check_public_key(get_store_filename(), name, b64_encoded)) { + switch (check_public_key(get_store_filename(), ClientRealm_get_serverName(cr), b64_encoded)) { case SSL_PUBLIC_KEY_VALID: /* public key is ok - do nothing */ break; case SSL_PUBLIC_KEY_NOT_KNOWN: aflog(LOG_T_MAIN, LOG_I_WARNING, "WARNING: implicitly added new server's public key to the list of known hosts"); - add_public_key(get_store_filename(), name, b64_encoded); + add_public_key(get_store_filename(), ClientRealm_get_serverName(cr), b64_encoded); break; default: - if (ignorepkeys) { + if (ignorePublicKeys) { aflog(LOG_T_MAIN, LOG_I_WARNING, "WARNING: Invalid server's public key... ignoring"); } @@ -186,30 +200,34 @@ initialize_client_stage1(char tunneltype, SslFd* master, char* name, char* manag } else { aflog(LOG_T_INIT, LOG_I_CRIT, - "SSL_connect has failed (%d | %d)... exiting", n, SSL_get_error(master->ssl, n)); + "SSL_connect has failed (%d | %d)... exiting", n, + SSL_get_error(SslFd_get_ssl(ClientRealm_get_masterSslFd(cr)), n)); if (wanttoexit) { exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 3; } } buff[0] = AF_S_LOGIN; - buff[1] = pass[0]; - buff[2] = pass[1]; - buff[3] = pass[2]; - buff[4] = pass[3]; + buff[1] = ClientRealm_get_password(cr)[0]; + buff[2] = ClientRealm_get_password(cr)[1]; + buff[3] = ClientRealm_get_password(cr)[2]; + buff[4] = ClientRealm_get_password(cr)[3]; return 0; } int -initialize_client_stage2(char *type, SslFd* master, int* usernum, unsigned char* buff, char wanttoexit) +initialize_client_stage2(ClientRealm* cr, unsigned char* buff, char wanttoexit) { - SslFd_send_message(*type | TYPE_SSL | TYPE_ZLIB, master, buff, 5); + SslFd_send_message(ClientRealm_get_realmType(cr) | TYPE_SSL | TYPE_ZLIB, + ClientRealm_get_masterSslFd(cr), buff, 5); buff[0] = 0; - SslFd_get_message(*type | TYPE_SSL | TYPE_ZLIB, master, buff, -5); + SslFd_get_message(ClientRealm_get_realmType(cr) | TYPE_SSL | TYPE_ZLIB, + ClientRealm_get_masterSslFd(cr), buff, -5); if ( buff[0] == 0 ) { aflog(LOG_T_INIT, LOG_I_CRIT, @@ -218,6 +236,7 @@ initialize_client_stage2(char *type, SslFd* master, int* usernum, unsigned char* exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 1; } } @@ -228,6 +247,7 @@ initialize_client_stage2(char *type, SslFd* master, int* usernum, unsigned char* exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 1; } } @@ -238,6 +258,7 @@ initialize_client_stage2(char *type, SslFd* master, int* usernum, unsigned char* exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 1; } } @@ -248,55 +269,60 @@ initialize_client_stage2(char *type, SslFd* master, int* usernum, unsigned char* exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 1; } } - *type = buff[3]; - (*usernum) = buff[1]; - (*usernum) = (*usernum) << 8; - (*usernum) += buff[2]; + ClientRealm_set_realmType(cr, buff[3]); + ClientRealm_set_usersLimit(cr, buff[1] * 256 + buff[2]); return 0; } int -initialize_client_stage3(ConnectUser*** contable, SslFd* master, int usernum, int* buflength, socklen_t* len, - fd_set* allset, fd_set* wset, int* maxfdp1, char wanttoexit) +initialize_client_stage3(ClientRealm* cr, int* buflength, fd_set* allset, fd_set* wset, int* maxfdp1, + char wanttoexit) { int i; - (*contable) = calloc(usernum, sizeof(ConnectUser*)); - if ((*contable) == NULL) { + socklen_t len; + ConnectUser** usersTable; + usersTable = calloc(ClientRealm_get_usersLimit(cr), sizeof(ConnectUser*)); + if (usersTable == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - unable to successfully communicate with server"); if (wanttoexit) { exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 1; } } - for (i = 0; i < usernum; ++i) { - (*contable)[i] = ConnectUser_new(); - if ((*contable)[i] == NULL) { + ClientRealm_set_usersTable(cr, usersTable); + for (i = 0; i < ClientRealm_get_usersLimit(cr); ++i) { + usersTable[i] = ConnectUser_new(); + if (usersTable[i] == NULL) { aflog(LOG_T_INIT, LOG_I_CRIT, "Calloc error - unable to successfully communicate with server"); if (wanttoexit) { exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 1; } } } - (*len) = 4; - if (getsockopt(SslFd_get_fd(master), SOL_SOCKET, SO_SNDBUF, buflength, len) == -1) { + len = 4; + if (getsockopt(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), SOL_SOCKET, SO_SNDBUF, buflength, &len) == -1) { aflog(LOG_T_INIT, LOG_I_CRIT, "Can't get socket send buffer size - exiting..."); if (wanttoexit) { exit(1); } else { + close(SslFd_get_fd(ClientRealm_get_masterSslFd(cr))); return 2; } } @@ -304,7 +330,7 @@ initialize_client_stage3(ConnectUser*** contable, SslFd* master, int usernum, in FD_ZERO(allset); FD_ZERO(wset); - FD_SET(SslFd_get_fd(master), allset); - (*maxfdp1) = SslFd_get_fd(master) + 1; + FD_SET(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), allset); + (*maxfdp1) = SslFd_get_fd(ClientRealm_get_masterSslFd(cr)) + 1; return 0; } diff --git a/src/client_initialization.h b/src/client_initialization.h index 5c8c719..ec2360a 100644 --- a/src/client_initialization.h +++ b/src/client_initialization.h @@ -26,17 +26,17 @@ #include "http_proxy_client.h" #include "http_proxy_options_struct.h" #include "ssl_fd_struct.h" +#include "client_realm_struct.h" #include <openssl/ssl.h> #ifndef _JS_CLIENT_INITIALIZATION_H #define _JS_CLIENT_INITIALIZATION_H -int initialize_client_stage1(char tunneltype, SslFd* master, char* name, char* manage, - HttpProxyOptions* hpo, char ipfam, SSL_CTX* ctx, unsigned char* buff, unsigned char* pass, - char wanttoexit, char ignorepkeys); -int initialize_client_stage2(char *type, SslFd* master, int* usernum, unsigned char* buff, char wanttoexit); -int initialize_client_stage3(ConnectUser*** contable, SslFd* master, int usernum, int* buflength, - socklen_t* len, fd_set* allset, fd_set* wset, int* maxfdp1, char wanttoexit); +int initialize_client_stage1(ClientRealm* cr, SSL_CTX* ctx, unsigned char* buff, char wanttoexit, + char ignorePublicKeys); +int initialize_client_stage2(ClientRealm* cr, unsigned char* buff, char wanttoexit); +int initialize_client_stage3(ClientRealm* cr, int* buflength, fd_set* allset, fd_set* wset, int* maxfdp1, + char wanttoexit); #endif diff --git a/src/client_realm_struct.c b/src/client_realm_struct.c new file mode 100644 index 0000000..d02b508 --- /dev/null +++ b/src/client_realm_struct.c @@ -0,0 +1,1094 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include <config.h> + +#include <stdlib.h> +#include <string.h> + +#include "string_functions.h" +#include "client_realm_struct.h" +#include "client_shutdown.h" + +/* + * Function name: ClientRealm_new + * Description: Create and initialize new ClientRealm structure. + * Returns: Pointer to newly created ClientRealm structure. + */ + +ClientRealm* +ClientRealm_new() +{ + ClientRealm* tmp = calloc(1, sizeof(ClientRealm)); + if (tmp == NULL) { + return NULL; + } + tmp->password[0] = 1; + tmp->password[1] = 2; + tmp->password[2] = 3; + tmp->password[3] = 4; + tmp->masterSslFd = SslFd_new(); + if (tmp->masterSslFd == NULL) { + ClientRealm_free(&tmp); + return NULL; + } + tmp->arOptions = ArOptions_new(); + if (tmp->arOptions == NULL) { + ClientRealm_free(&tmp); + return NULL; + } + tmp->httpProxyOptions = HttpProxyOptions_new(); + if (tmp->httpProxyOptions == NULL) { + ClientRealm_free(&tmp); + return NULL; + } +#ifdef HAVE_LIBDL + tmp->userModule = Module_new(); + if (tmp->userModule == NULL) { + ClientRealm_free(&tmp); + return NULL; + } + tmp->serviceModule = Module_new(); + if (tmp->serviceModule == NULL) { + ClientRealm_free(&tmp); + return NULL; + } +#endif + return tmp; +} + +/* + * Function name: ClientRealm_free + * Description: Free the memory allocated for ClientRealm structure. + * Arguments: cr - pointer to pointer to ClientRealm structure + */ + +void +ClientRealm_free(ClientRealm** cr) +{ + int i; + if (cr == NULL) { + return; + } + if ((*cr) == NULL) { + return; + } + if ((*cr)->serverName) { + free((*cr)->serverName); + (*cr)->serverName = NULL; + } + if ((*cr)->managePort) { + free((*cr)->managePort); + (*cr)->managePort = NULL; + } + if ((*cr)->hostName) { + free((*cr)->hostName); + (*cr)->hostName = NULL; + } + if ((*cr)->destinationPort) { + free((*cr)->destinationPort); + (*cr)->destinationPort = NULL; + } + if ((*cr)->sKeepAliveTimeout) { + free((*cr)->sKeepAliveTimeout); + (*cr)->sKeepAliveTimeout = NULL; + } + if ((*cr)->realmName) { + free((*cr)->realmName); + (*cr)->realmName = NULL; + } + if ((*cr)->clientAddress) { + free((*cr)->clientAddress); + (*cr)->clientAddress = NULL; + } + if ((*cr)->usersTable) { + for (i = 0; i < (*cr)->usersLimit; ++i) { + if ((*cr)->usersTable[i]) { + ConnectUser_free(&((*cr)->usersTable[i])); + } + } + free((*cr)->usersTable); + (*cr)->usersTable = NULL; + } + free((*cr)); + (*cr) = NULL; +} + +/* + * Function name: ClientRealm_set_serverName + * Description: Set realm's server name. + * Arguments: cr - pointer to ClientRealm structure + * serverName - realm's host name + */ + +void +ClientRealm_set_serverName(ClientRealm* cr, char* serverName) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->serverName), serverName); +} + +/* + * Function name: ClientRealm_set_managePort + * Description: Set realm's manage port description. + * Arguments: cr - pointer to ClientRealm structure + * managePort - realm's manage port description + */ + +void +ClientRealm_set_managePort(ClientRealm* cr, char* managePort) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->managePort), managePort); +} + +/* + * Function name: ClientRealm_set_hostName + * Description: Set realm's host name. + * Arguments: cr - pointer to ClientRealm structure + * hostName - realm's host name + */ + +void +ClientRealm_set_hostName(ClientRealm* cr, char* hostName) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->hostName), hostName); +} + +/* + * Function name: ClientRealm_set_destinationPort + * Description: Set realm's destination port description. + * Arguments: cr - pointer to ClientRealm structure + * destinationPort - realm's destination port description + */ + +void +ClientRealm_set_destinationPort(ClientRealm* cr, char* destinationPort) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->destinationPort), destinationPort); +} + +/* + * Function name: ClientRealm_set_sKeepAliveTimeout + * Description: Set keep-alive timeout value description. + * Arguments: cr - pointer to ClientRealm structure + * sKeepAliveTimeout - keep-alive timeout value description + */ + +void +ClientRealm_set_sKeepAliveTimeout(ClientRealm* cr, char* sKeepAliveTimeout) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->sKeepAliveTimeout), sKeepAliveTimeout); +} + +/* + * Function name: ClientRealm_set_realmName + * Description: Set realm's name. + * Arguments: cr - pointer to ClientRealm structure + * realmName - realm's name + */ + +void +ClientRealm_set_realmName(ClientRealm* cr, char* realmName) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->realmName), realmName); +} + +/* + * Function name: ClientRealm_set_realmId + * Description: Set realm's id. + * Arguments: cr - pointer to ClientRealm structure + * realmId - realm's id + */ + +void +ClientRealm_set_realmId(ClientRealm* cr, char* realmId) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->realmId), realmId); +} + +/* + * Function name: ClientRealm_set_localName + * Description: Set realm's local name. + * Arguments: cr - pointer to ClientRealm structure + * localName - realm's local name + */ + +void +ClientRealm_set_localName(ClientRealm* cr, char* localName) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->localName), localName); +} + +/* + * Function name: ClientRealm_set_localPort + * Description: Set realm's local port description. + * Arguments: cr - pointer to ClientRealm structure + * localPort - realm's local port description + */ + +void +ClientRealm_set_localPort(ClientRealm* cr, char* localPort) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->localPort), localPort); +} + +/* + * Function name: ClientRealm_set_localDestinationName + * Description: Set realm's local destination name. + * Arguments: cr - pointer to ClientRealm structure + * localDestinationName - realm's local destination name + */ + +void +ClientRealm_set_localDestinationName(ClientRealm* cr, char* localDestinationName) +{ + if (cr == NULL) { + return; + } + string_cp(&(cr->localDestinationName), localDestinationName); +} + +/* + * Function name: ClientRealm_set_password + * Description: Set realm's password. + * Arguments: cr - pointer to ClientRealm structure + * password - realm's password + */ + +void +ClientRealm_set_password(ClientRealm* cr, unsigned char* password) +{ + if (cr == NULL) { + return; + } + memcpy(cr->password, password, 4); +} + +/* + * Function name: ClientRealm_set_connectedUsers + * Description: Set number of connected users. + * Arguments: cr - pointer to ClientRealm structure + * connectedUsers - number of connected users + */ + +void +ClientRealm_set_connectedUsers(ClientRealm* cr, int connectedUsers) +{ + if (cr == NULL) { + return; + } + cr->connectedUsers = connectedUsers; +} + +/* + * Function name: ClientRealm_set_usersLimit + * Description: Set limit of connected users. + * Arguments: cr - pointer to ClientRealm structure + * usersLimit - limit of connected users + */ + +void +ClientRealm_set_usersLimit(ClientRealm* cr, int usersLimit) +{ + if (cr == NULL) { + return; + } + cr->usersLimit = usersLimit; +} + +/* + * Function name: ClientRealm_set_keepAliveTimeout + * Description: Set keep-alive timeout value. + * Arguments: cr - pointer to ClientRealm structure + * keepAliveTimeout - keep-alive timeout value + */ + +void +ClientRealm_set_keepAliveTimeout(ClientRealm* cr, int keepAliveTimeout) +{ + if (cr == NULL) { + return; + } + cr->keepAliveTimeout = keepAliveTimeout; +} + +/* + * Function name: ClientRealm_set_clientMode + * Description: Set client mode. + * Arguments: cr - pointer to ClientRealm structure + * clientMode - client mode + */ + +void +ClientRealm_set_clientMode(ClientRealm* cr, int clientMode) +{ + if (cr == NULL) { + return; + } + cr->clientMode = clientMode; +} + +/* + * Function name: ClientRealm_set_ipFamily + * Description: Set IP family. + * Arguments: cr - pointer to ClientRealm structure + * ipFamily - IP family + */ + +void +ClientRealm_set_ipFamily(ClientRealm* cr, char ipFamily) +{ + if (cr == NULL) { + return; + } + cr->ipFamily = ipFamily; +} + +/* + * Function name: ClientRealm_set_realmType + * Description: Set type of the realm. + * Arguments: cr - pointer to ClientRealm structure + * realmType - type of the realm + */ + +void +ClientRealm_set_realmType(ClientRealm* cr, char realmType) +{ + if (cr == NULL) { + return; + } + cr->realmType = realmType; +} + +/* + * Function name: ClientRealm_set_tunnelType + * Description: Set type of the tunnel. + * Arguments: cr - pointer to ClientRealm structure + * tunnelType - type of the tunnel + */ + +void +ClientRealm_set_tunnelType(ClientRealm* cr, char tunnelType) +{ + if (cr == NULL) { + return; + } + cr->tunnelType = tunnelType; +} + +/* + * Function name: ClientRealm_set_keepAlive + * Description: Set keep-alive timeval struct. + * Arguments: cr - pointer to ClientRealm structure + * keepAlive - keep-alive timeval struct + */ + +void +ClientRealm_set_keepAlive(ClientRealm* cr, struct timeval keepAlive) +{ + if (cr == NULL) { + return; + } + cr->keepAlive = keepAlive; +} + +/* + * Function name: ClientRealm_set_addressLength + * Description: Set client's address length. + * Arguments: cr - pointer to ClientRealm structure + * addressLength - client's address length + */ + +void +ClientRealm_set_addressLength(ClientRealm* cr, socklen_t addressLength) +{ + if (cr == NULL) { + return; + } + cr->addressLength = addressLength; +} + +/* + * Function name: ClientRealm_set_clientAddress + * Description: Set client's network address. + * Arguments: cr - pointer to ClientRealm structure + * clientAddress - client's network address + */ + +void +ClientRealm_set_clientAddress(ClientRealm* cr, struct sockaddr* clientAddress) +{ + if (cr == NULL) { + return; + } + if (cr->clientAddress) { + free(cr->clientAddress); + cr->clientAddress = NULL; + } + cr->clientAddress = clientAddress; +} + +/* + * Function name: ClientRealm_set_masterSslFd + * Description: Set client realm's master sslfd. + * Arguments: cr - pointer to ClientRealm structure + * masterSslFd - client realm's master sslfd + */ + +void +ClientRealm_set_masterSslFd(ClientRealm* cr, SslFd* masterSslFd) +{ + if (cr == NULL) { + return; + } + if (cr->masterSslFd) { + SslFd_free(&(cr->masterSslFd)); + } + cr->masterSslFd = masterSslFd; +} + +/* + * Function name: ClientRealm_set_httpProxyOptions + * Description: Set client realm's http proxy options. + * Arguments: cr - pointer to ClientRealm structure + * httpProxyOptions - client realm's http proxy options + */ + +void +ClientRealm_set_httpProxyOptions(ClientRealm* cr, HttpProxyOptions* httpProxyOptions) +{ + if (cr == NULL) { + return; + } + if (cr->httpProxyOptions) { + HttpProxyOptions_free(&(cr->httpProxyOptions)); + } + cr->httpProxyOptions = httpProxyOptions; +} + +/* + * Function name: ClientRealm_set_arOptions + * Description: Set client realm's auto-reconnect options. + * Arguments: cr - pointer to ClientRealm structure + * arOptions - client realm's auto-reconnect options + */ + +void +ClientRealm_set_arOptions(ClientRealm* cr, ArOptions* arOptions) +{ + if (cr == NULL) { + return; + } + if (cr->arOptions) { + ArOptions_free(&(cr->arOptions)); + } + cr->arOptions = arOptions; +} + +/* + * Function name: ClientRealm_set_usersTable + * Description: Set table of users. + * Arguments: cr - pointer to ClientRealm structure + * usersTable - table of users + */ + +void +ClientRealm_set_usersTable(ClientRealm* cr, ConnectUser** usersTable) +{ + int i; + if (cr == NULL) { + return; + } + if (cr->usersTable) { + for (i = 0; i < cr->usersLimit; ++i) { + if (cr->usersTable[i]) { + ConnectUser_free(&(cr->usersTable[i])); + } + } + free(cr->usersTable); + cr->usersTable = NULL; + } + cr->usersTable = usersTable; +} + +#ifdef HAVE_LIBDL +/* + * Function name: ClientRealm_set_userModule + * Description: Set a module for user's packets filtering. + * Arguments: cr - pointer to ClientRealm structure + * userModule - module for user's packets filtering + */ + +void +ClientRealm_set_userModule(ClientRealm* cr, Module* userModule) +{ + if (cr == NULL) { + return; + } + if (cr->userModule) { + Module_free(&(cr->userModule)); + } + cr->userModule = userModule; +} + +/* + * Function name: ClientRealm_set_serviceModule + * Description: Set a module for service's packets filtering. + * Arguments: cr - pointer to ClientRealm structure + * serviceModule - module for service's packets filtering + */ + +void +ClientRealm_set_serviceModule(ClientRealm* cr, Module* serviceModule) +{ + if (cr == NULL) { + return; + } + if (cr->serviceModule) { + Module_free(&(cr->serviceModule)); + } + cr->serviceModule = serviceModule; +} +#endif + +/* + * Function name: ClientRealm_get_serverName + * Description: Get realm's server name. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's server name. + */ + +char* +ClientRealm_get_serverName(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->serverName; +} + +/* + * Function name: ClientRealm_get_managePort + * Description: Get realm's manage port description. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's manage port description. + */ + +char* +ClientRealm_get_managePort(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->managePort; +} + +/* + * Function name: ClientRealm_get_hostName + * Description: Get realm's host name. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's host name. + */ + +char* +ClientRealm_get_hostName(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->hostName; +} + +/* + * Function name: ClientRealm_get_destinationPort + * Description: Get realm's destination port description. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's destination port description. + */ + +char* +ClientRealm_get_destinationPort(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->destinationPort; +} + +/* + * Function name: ClientRealm_get_sKeepAliveTimeout + * Description: Get keep-alive timeout value description. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Keep-alive timeout value description. + */ + +char* +ClientRealm_get_sKeepAliveTimeout(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->sKeepAliveTimeout; +} + +/* + * Function name: ClientRealm_get_realmName + * Description: Get realm's name. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's name. + */ + +char* +ClientRealm_get_realmName(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->realmName; +} + +/* + * Function name: ClientRealm_get_realmId + * Description: Get realm's id. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's id. + */ + +char* +ClientRealm_get_realmId(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->realmId; +} + + +/* + * Function name: ClientRealm_get_localName + * Description: Get realm's local name. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's local name. + */ + +char* +ClientRealm_get_localName(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->localName; +} + + +/* + * Function name: ClientRealm_get_localPort + * Description: Get realm's local port description. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's local port description. + */ + +char* +ClientRealm_get_localPort(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->localPort; +} + + +/* + * Function name: ClientRealm_get_localDestinationName + * Description: Get realm's local destination name. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Realm's local destination name. + */ + +char* +ClientRealm_get_localDestinationName(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->localDestinationName; +} + +/* + * Function name: ClientRealm_get_password + * Description: Get realm's password. + * Arguments: sr - pointer to ClientRealm structure + * Returns: Realm's password. + */ + +unsigned char* +ClientRealm_get_password(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->password; +} + +/* + * Function name: ClientRealm_get_connectedUsers + * Description: Get number of connected users. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Number of connected users. + */ + +int +ClientRealm_get_connectedUsers(ClientRealm* cr) +{ + if (cr == NULL) { + return -1; + } + return cr->connectedUsers; +} + +/* + * Function name: ClientRealm_get_usersLimit + * Description: Get limit of connected users. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Limit of connected users. + */ + +int +ClientRealm_get_usersLimit(ClientRealm* cr) +{ + if (cr == NULL) { + return -1; + } + return cr->usersLimit; +} + +/* + * Function name: ClientRealm_get_keepAliveTimeout + * Description: Get keep-alive timeout value. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Keep-alive timeout value. + */ + +int +ClientRealm_get_keepAliveTimeout(ClientRealm* cr) +{ + if (cr == NULL) { + return 0; + } + return cr->keepAliveTimeout; +} + +/* + * Function name: ClientRealm_get_clientMode + * Description: Get client mode. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Client mode. + */ + +int +ClientRealm_get_clientMode(ClientRealm* cr) +{ + if (cr == NULL) { + return 0; + } + return cr->clientMode; +} + +/* + * Function name: ClientRealm_get_ipFamily + * Description: Get IP family. + * Arguments: cr - pointer to ClientRealm structure + * Returns: IP family. + */ + +char +ClientRealm_get_ipFamily(ClientRealm* cr) +{ + if (cr == NULL) { + return 0; + } + return cr->ipFamily; +} + +/* + * Function name: ClientRealm_get_realmType + * Description: Get type of the realm. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Type of the realm. + */ + +char +ClientRealm_get_realmType(ClientRealm* cr) +{ + if (cr == NULL) { + return 0; + } + return cr->realmType; +} + +/* + * Function name: ClientRealm_get_tunnelType + * Description: Get type of the tunnel. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Type of the tunnel. + */ + +char +ClientRealm_get_tunnelType(ClientRealm* cr) +{ + if (cr == NULL) { + return 0; + } + return cr->tunnelType; +} + +/* + * Function name: ClientRealm_get_keepAlive + * Description: Get keep-alive timeval struct. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Keep-alive timeval struct. + */ + +struct timeval +ClientRealm_get_keepAlive(ClientRealm* cr) +{ + struct timeval tmp = {0, 0}; + if (cr == NULL) { + return tmp; + } + return cr->keepAlive; +} + +/* + * Function name: ClientRealm_get_addressLength + * Description: Get client's address length. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Client's address length. + */ + +socklen_t +ClientRealm_get_addressLength(ClientRealm* cr) +{ + if (cr == NULL) { + return 0; + } + return cr->addressLength; +} + +/* + * Function name: ClientRealm_get_clientAddress + * Description: Get client's network address. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Client's network address. + */ + +struct sockaddr* +ClientRealm_get_clientAddress(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->clientAddress; +} + +/* + * Function name: ClientRealm_get_masterSslFd + * Description: Get client realm's master sslfd. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Client realm's master sslfd. + */ + +SslFd* +ClientRealm_get_masterSslFd(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->masterSslFd; +} + +/* + * Function name: ClientRealm_get_httpProxyOptions + * Description: Get client realm's http proxy options. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Client realm's http proxy options. + */ + +HttpProxyOptions* +ClientRealm_get_httpProxyOptions(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->httpProxyOptions; +} + +/* + * Function name: ClientRealm_get_arOptions + * Description: Get client realm's auto-reconnect options. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Client realm's auto-reconnect options. + */ + +ArOptions* +ClientRealm_get_arOptions(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->arOptions; +} + +/* + * Function name: ClientRealm_get_usersTable + * Description: Get table of users. + * Arguments: cr - pointer to ClientRealm structure + * Returns: Table of users. + */ + +ConnectUser** +ClientRealm_get_usersTable(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->usersTable; +} + +#ifdef HAVE_LIBDL +/* + * Function name: ClientRealm_get_userModule + * Description: Get a module for user's packets filtering. + * Arguments: cr - pointer to ClientRealm structure + * Returns: A module for user's packets filtering. + */ + +Module* +ClientRealm_get_userModule(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->userModule; +} + +/* + * Function name: ClientRealm_get_serviceModule + * Description: Get a module for service's packets filtering. + * Arguments: cr - pointer to ClientRealm structure + * Returns: A module for service's packets filtering. + */ + +Module* +ClientRealm_get_serviceModule(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return cr->serviceModule; +} +#endif + +/* + * Function name: ClientRealm_increase_connectedUsers + * Description: Increase number of connected users. + * Arguments: cr - pointer to ClientRealm structure + */ + +void +ClientRealm_increase_connectedUsers(ClientRealm* cr) +{ + if (cr == NULL) { + return; + } + ClientRealm_set_connectedUsers(cr, ClientRealm_get_connectedUsers(cr) + 1); +} + +/* + * Function name: ClientRealm_decrease_connectedUsers + * Description: Decrease number of connected users. + * Arguments: cr - pointer to ClientRealm structure + */ + +void +ClientRealm_decrease_connectedUsers(ClientRealm* cr) +{ + if (cr == NULL) { + return; + } + ClientRealm_set_connectedUsers(cr, ClientRealm_get_connectedUsers(cr) - 1); +} + +/* + * Function name: ClientRealm_closeUsersConnections + * Description: Close all users' connections and free usersTable + * Arguments: cr - pointer to ClientRealm structure + */ + +void +ClientRealm_closeUsersConnections(ClientRealm* cr) +{ + if (cr == NULL) { + return; + } + close_connections(ClientRealm_get_usersLimit(cr), &(cr->usersTable)); +} + +/* + * Function name: ClientRealm_get_keepAlivePointer + * Description: Get pointer to keep-alive structure + * Arguments: cr - pointer to ClientRealm structure + * Returns: Pointer to keep-alive structure + */ + +struct timeval* +ClientRealm_get_keepAlivePointer(ClientRealm* cr) +{ + if (cr == NULL) { + return NULL; + } + return (&(cr->keepAlive)); +} diff --git a/src/client_realm_struct.h b/src/client_realm_struct.h new file mode 100644 index 0000000..01459d9 --- /dev/null +++ b/src/client_realm_struct.h @@ -0,0 +1,143 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include "activefor.h" +#include "ssl_fd_struct.h" +#include "http_proxy_options_struct.h" +#include "ar_options_struct.h" +#include "module_struct.h" + +#ifndef _JS_CLIENT_REALM_STRUCT_H +#define _JS_CLIENT_REALM_STRUCT_H + +#define CLIENTREALM_MODE_UNKNOWN -1 +#define CLIENTREALM_MODE_TCP 0 +#define CLIENTREALM_MODE_UDP 1 +#define CLIENTREALM_MODE_REMOTE 2 +#define CLIENTREALM_MODE_REVERSE 3 + +#define CLIENTREALM_TUNNELTYPE_UNKNOWN -1 +#define CLIENTREALM_TUNNELTYPE_DIRECT 0 +#define CLIENTREALM_TUNNELTYPE_HTTPPROXY 1 +#define CLIENTREALM_TUNNELTYPE_HTTPSPROXY 2 + +typedef struct { + char* serverName; + char* managePort; + char* hostName; + char* destinationPort; + char* realmName; + char* sKeepAliveTimeout; + char* realmId; + char* localName; + char* localPort; + char* localDestinationName; + unsigned char password[4]; + int connectedUsers; + int usersLimit; + int clientMode; + int keepAliveTimeout; + char ipFamily; + char realmType; + char tunnelType; + struct timeval keepAlive; + socklen_t addressLength; + struct sockaddr* clientAddress; + SslFd* masterSslFd; + HttpProxyOptions* httpProxyOptions; + ArOptions* arOptions; + ConnectUser** usersTable; +#ifdef HAVE_LIBDL + Module* userModule; + Module* serviceModule; +#endif +} ClientRealm; + +/* 'constructor' */ +ClientRealm* ClientRealm_new(); +/* 'destructor' */ +void ClientRealm_free(ClientRealm** cr); +/* setters */ +void ClientRealm_set_serverName(ClientRealm* cr, char* serverName); +void ClientRealm_set_managePort(ClientRealm* cr, char* managePort); +void ClientRealm_set_hostName(ClientRealm* cr, char* hostName); +void ClientRealm_set_destinationPort(ClientRealm* cr, char* destinationPort); +void ClientRealm_set_realmName(ClientRealm* cr, char* realmName); +void ClientRealm_set_sKeepAliveTimeout(ClientRealm* cr, char* sKeepAliveTimeout); +void ClientRealm_set_realmId(ClientRealm* cr, char* realmId); +void ClientRealm_set_localName(ClientRealm* cr, char* localName); +void ClientRealm_set_localPort(ClientRealm* cr, char* localPort); +void ClientRealm_set_localDestinationName(ClientRealm* cr, char* localDestinationName); +void ClientRealm_set_password(ClientRealm* cr, unsigned char* password); +void ClientRealm_set_connectedUsers(ClientRealm* cr, int connectedUsers); +void ClientRealm_set_usersLimit(ClientRealm* cr, int usersLimit); +void ClientRealm_set_keepAliveTimeout(ClientRealm* cr, int keepAliveTimeout); +void ClientRealm_set_clientMode(ClientRealm* cr, int clientMode); +void ClientRealm_set_ipFamily(ClientRealm* cr, char ipFamily); +void ClientRealm_set_realmType(ClientRealm* cr, char realmType); +void ClientRealm_set_tunnelType(ClientRealm* cr, char tunnelType); +void ClientRealm_set_keepAlive(ClientRealm* cr, struct timeval keepAlive); +void ClientRealm_set_addressLength(ClientRealm* cr, socklen_t addressLength); +void ClientRealm_set_clientAddress(ClientRealm* cr, struct sockaddr* clientAddress); +void ClientRealm_set_masterSslFd(ClientRealm* cr, SslFd* masterSslFd); +void ClientRealm_set_httpProxyOptions(ClientRealm* cr, HttpProxyOptions* httpProxyOptions); +void ClientRealm_set_arOptions(ClientRealm* cr, ArOptions* arOptions); +void ClientRealm_set_usersTable(ClientRealm* cr, ConnectUser** usersTable); +#ifdef HAVE_LIBDL +void ClientRealm_set_userModule(ClientRealm* cr, Module* userModule); +void ClientRealm_set_serviceModule(ClientRealm* cr, Module* serviceModule); +#endif +/* getters */ +char* ClientRealm_get_serverName(ClientRealm* cr); +char* ClientRealm_get_managePort(ClientRealm* cr); +char* ClientRealm_get_hostName(ClientRealm* cr); +char* ClientRealm_get_destinationPort(ClientRealm* cr); +char* ClientRealm_get_realmName(ClientRealm* cr); +char* ClientRealm_get_sKeepAliveTimeout(ClientRealm* cr); +char* ClientRealm_get_realmId(ClientRealm* cr); +char* ClientRealm_get_localName(ClientRealm* cr); +char* ClientRealm_get_localPort(ClientRealm* cr); +char* ClientRealm_get_localDestinationName(ClientRealm* cr); +unsigned char* ClientRealm_get_password(ClientRealm* cr); +int ClientRealm_get_connectedUsers(ClientRealm* cr); +int ClientRealm_get_usersLimit(ClientRealm* cr); +int ClientRealm_get_keepAliveTimeout(ClientRealm* cr); +int ClientRealm_get_clientMode(ClientRealm* cr); +char ClientRealm_get_ipFamily(ClientRealm* cr); +char ClientRealm_get_realmType(ClientRealm* cr); +char ClientRealm_get_tunnelType(ClientRealm* cr); +struct timeval ClientRealm_get_keepAlive(ClientRealm* cr); +socklen_t ClientRealm_get_addressLength(ClientRealm* cr); +struct sockaddr* ClientRealm_get_clientAddress(ClientRealm* cr); +SslFd* ClientRealm_get_masterSslFd(ClientRealm* cr); +HttpProxyOptions* ClientRealm_get_httpProxyOptions(ClientRealm* cr); +ArOptions* ClientRealm_get_arOptions(ClientRealm* cr); +ConnectUser** ClientRealm_get_usersTable(ClientRealm* cr); +#ifdef HAVE_LIBDL +Module* ClientRealm_get_userModule(ClientRealm* cr); +Module* ClientRealm_get_serviceModule(ClientRealm* cr); +#endif +/* other */ +void ClientRealm_increase_connectedUsers(ClientRealm* cr); +void ClientRealm_decrease_connectedUsers(ClientRealm* cr); +void ClientRealm_closeUsersConnections(ClientRealm* cr); +struct timeval* ClientRealm_get_keepAlivePointer(ClientRealm* cr); + +#endif diff --git a/src/client_reverse_udp.c b/src/client_reverse_udp.c index c0a1ef4..d8cf971 100644 --- a/src/client_reverse_udp.c +++ b/src/client_reverse_udp.c @@ -25,19 +25,23 @@ #include <stdlib.h> int -initialize_client_reverse_udp(int* usernum, SslFd* master, char* name, char* manage, char ipfam, - char wanttoexit) +initialize_client_reverse_udp(ClientRealm* cr) { int tmp; - (*usernum) = 1; - if (ip_connect(&tmp, name, manage, ipfam)) { + int wanttoexit = (ArOptions_get_arStart(ClientRealm_get_arOptions(cr)) == AR_OPTION_ENABLED) ? 0 : 1; + ClientRealm_set_usersLimit(cr, 1); + if (ip_connect(&tmp, ClientRealm_get_serverName(cr), + ClientRealm_get_managePort(cr), + ClientRealm_get_ipFamily(cr), NULL, NULL)) { #ifdef AF_INET6 aflog(LOG_T_INIT, LOG_I_CRIT, "tcp_connect_%s error for %s, %s", - (ipfam & 0x02)?"ipv4":(ipfam & 0x04)?"ipv6":"unspec", name, manage); + (ClientRealm_get_ipFamily(cr) & 0x02) ? + "ipv4" : (ClientRealm_get_ipFamily(cr) & 0x04) ? + "ipv6":"unspec", ClientRealm_get_serverName(cr), ClientRealm_get_managePort(cr)); #else aflog(LOG_T_INIT, LOG_I_CRIT, - "tcp_connect error for %s, %s", name, manage); + "tcp_connect error for %s, %s", ClientRealm_get_serverName(cr), ClientRealm_get_managePort(cr)); #endif if (wanttoexit) { exit(1); @@ -47,55 +51,48 @@ initialize_client_reverse_udp(int* usernum, SslFd* master, char* name, char* man } } - SslFd_set_fd(master, tmp); - SslFd_set_ssl(master, NULL); + SslFd_set_fd(ClientRealm_get_masterSslFd(cr), tmp); + SslFd_set_ssl(ClientRealm_get_masterSslFd(cr), NULL); return 0; } void -client_reverse_udp(ConnectUser** contable, SslFd* master, char* desnam, char* despor, char type, - unsigned char* buff, int buflength) +client_reverse_udp(ClientRealm* cr, unsigned char* buff, int buflength) { - char ipfam; socklen_t len, addrlen; int maxfdp1, temp, notsent, n, length; struct sockaddr* cliaddr; fd_set rset, allset; - - ipfam = 0; -#ifdef AF_INET6 - if (TYPE_IS_IPV4(type)) { - ipfam |= 0x02; - } - else if (TYPE_IS_IPV6(type)) { - ipfam |= 0x04; - } -#endif - if (ip_listen(&temp, desnam, despor, &addrlen, ipfam)) { + + if (ip_listen(&temp, ClientRealm_get_hostName(cr), + ClientRealm_get_destinationPort(cr), &addrlen, + ClientRealm_get_ipFamily(cr) & 0xfe)) { #ifdef AF_INET6 aflog(LOG_T_INIT, LOG_I_DEBUG, "udp_listen_%s error for %s, %s", - (ipfam & 0x02)?"ipv4":(ipfam & 0x04)?"ipv6":"unspec", desnam, despor); + (ClientRealm_get_ipFamily(cr) & 0x02) ? + "ipv4":(ClientRealm_get_ipFamily(cr) & 0x04) ? + "ipv6":"unspec", ClientRealm_get_hostName(cr), ClientRealm_get_destinationPort(cr)); #else aflog(LOG_T_INIT, LOG_I_DEBUG, - "udp_listen error for %s, %s", desnam, despor); + "udp_listen error for %s, %s", ClientRealm_get_hostName(cr), ClientRealm_get_destinationPort(cr)); #endif exit(1); } - ConnectUser_set_connFd(contable[0], temp); + ConnectUser_set_connFd(ClientRealm_get_usersTable(cr)[0], temp); cliaddr = malloc(addrlen); FD_ZERO(&allset); - FD_SET(SslFd_get_fd(master), &allset); - maxfdp1 = SslFd_get_fd(master) + 1; - maxfdp1 = (maxfdp1 > (ConnectUser_get_connFd(contable[0]) + 1)) ? - maxfdp1 : (ConnectUser_get_connFd(contable[0]) + 1); - FD_SET(ConnectUser_get_connFd(contable[0]), &allset); + FD_SET(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), &allset); + maxfdp1 = SslFd_get_fd(ClientRealm_get_masterSslFd(cr)) + 1; + maxfdp1 = (maxfdp1 > (ConnectUser_get_connFd(ClientRealm_get_usersTable(cr)[0]) + 1)) ? + maxfdp1 : (ConnectUser_get_connFd(ClientRealm_get_usersTable(cr)[0]) + 1); + FD_SET(ConnectUser_get_connFd(ClientRealm_get_usersTable(cr)[0]), &allset); aflog(LOG_T_CLIENT, LOG_I_INFO, "CLIENT STARTED mode: udp reverse"); for ( ; ; ) { len = 4; - if (getsockopt(SslFd_get_fd(master), SOL_SOCKET, SO_SNDBUF, &temp, &len) != -1) { + if (getsockopt(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), SOL_SOCKET, SO_SNDBUF, &temp, &len) != -1) { if (temp != buflength) { buflength = temp; aflog(LOG_T_CLIENT, LOG_I_WARNING, @@ -110,13 +107,13 @@ client_reverse_udp(ConnectUser** contable, SslFd* master, char* desnam, char* de aflog(LOG_T_MAIN, LOG_I_DEBUG, "after select..."); - if (FD_ISSET(ConnectUser_get_connFd(contable[0]), &rset)) { /* FD_ISSET CONTABLE[0].CONNFD RSET*/ - n = recvfrom(ConnectUser_get_connFd(contable[0]), &buff[5], 8091, 0, cliaddr, &len); + if (FD_ISSET(ConnectUser_get_connFd(ClientRealm_get_usersTable(cr)[0]), &rset)) { /* FD_ISSET CONTABLE[0].CONNFD RSET*/ + n = recvfrom(ConnectUser_get_connFd(ClientRealm_get_usersTable(cr)[0]), &buff[5], 8091, 0, cliaddr, &len); #ifdef HAVE_LINUX_SOCKIOS_H # ifdef SIOCOUTQ aflog(LOG_T_MAIN, LOG_I_DDEBUG, "SIOCOUTQ is defined"); - if (ioctl(SslFd_get_fd(master), SIOCOUTQ, ¬sent)) { + if (ioctl(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), SIOCOUTQ, ¬sent)) { aflog(LOG_T_CLIENT, LOG_I_CRIT, "ioctl error -> exiting..."); exit(1); @@ -127,7 +124,7 @@ client_reverse_udp(ConnectUser** contable, SslFd* master, char* desnam, char* de # else aflog(LOG_T_MAIN< LOG_I_DDEBUG, "TIOCOUTQ is defined"); - if (ioctl(SslFd_get_fd(master), TIOCOUTQ, ¬sent)) { + if (ioctl(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), TIOCOUTQ, ¬sent)) { aflog(LOG_T_CLIENT, LOG_I_CRIT, "ioctl error -> exiting..."); exit(1); @@ -158,15 +155,15 @@ client_reverse_udp(ConnectUser** contable, SslFd* master, char* desnam, char* de buff[2] = AF_S_MESSAGE; buff[3] = n >> 8; buff[4] = n; - writen(SslFd_get_fd(master), buff, n + 5); + writen(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), buff, n + 5); } #ifdef HAVE_LINUX_SOCKIOS_H } #endif } /* - FD_ISSET CONTABLE[0].CONNFD RSET */ - if (FD_ISSET(SslFd_get_fd(master), &rset)) { /* FD_ISSET MASTER.COMMFD RSET */ - n = readn(SslFd_get_fd(master), buff, 5); + if (FD_ISSET(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), &rset)) { /* FD_ISSET MASTER.COMMFD RSET */ + n = readn(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), buff, 5); if (n == 5) { if ((buff[0] != AF_S_MESSAGE) || (buff[1] != AF_S_LOGIN) || (buff[2] != AF_S_MESSAGE)) { aflog(LOG_T_CLIENT, LOG_I_CRIT, @@ -176,7 +173,7 @@ client_reverse_udp(ConnectUser** contable, SslFd* master, char* desnam, char* de length = buff[3]; length = length << 8; length += buff[4]; /* this is length of message */ - n = readn(SslFd_get_fd(master), buff, length); + n = readn(SslFd_get_fd(ClientRealm_get_masterSslFd(cr)), buff, length); } else { n = 0; @@ -188,7 +185,7 @@ client_reverse_udp(ConnectUser** contable, SslFd* master, char* desnam, char* de } aflog(LOG_T_CLIENT, LOG_I_INFO, "Sending %d bytes to user (TO:%s)", n, sock_ntop(cliaddr, addrlen, NULL, NULL, 0)); - sendto(ConnectUser_get_connFd(contable[0]), buff, n, 0, cliaddr, addrlen); + sendto(ConnectUser_get_connFd(ClientRealm_get_usersTable(cr)[0]), buff, n, 0, cliaddr, addrlen); } /* - FD_ISSET MASTER.COMMFD RSET */ } exit(0); /* we shouldn't get here */ diff --git a/src/client_reverse_udp.h b/src/client_reverse_udp.h index 031dd8f..ecf48ba 100644 --- a/src/client_reverse_udp.h +++ b/src/client_reverse_udp.h @@ -33,10 +33,9 @@ #include "stats.h" #include "logging.h" #include "ssl_fd_struct.h" +#include "client_realm_struct.h" -int initialize_client_reverse_udp(int* usernum, SslFd* master, char* name, char* manage, char ipfam, - char wanttoexit); -void client_reverse_udp(ConnectUser** contable, SslFd* master, char* desnam, char* despor, char type, - unsigned char* buff, int buflength); +int initialize_client_reverse_udp(ClientRealm* cr); +void client_reverse_udp(ClientRealm* cr, unsigned char* buff, int buflength); #endif diff --git a/src/clientnames.c b/src/clientnames.c index 4fee9a8..3640ae9 100644 --- a/src/clientnames.c +++ b/src/clientnames.c @@ -25,37 +25,37 @@ #include "clientnames.h" char* -get_clientname(RealmT* pointer, int client) +get_clientname(ServerRealm* pointer, int client) { static char clientname[10]; - if (ConnectClient_get_sClientId(pointer->clitable[client]) == NULL) { + if (ConnectClient_get_sClientId(ServerRealm_get_clientsTable(pointer)[client]) == NULL) { memset(clientname, 0, 10); - sprintf(clientname, "%d", ConnectClient_get_clientId(pointer->clitable[client])); + sprintf(clientname, "%d", ConnectClient_get_clientId(ServerRealm_get_clientsTable(pointer)[client])); return clientname; } - return ConnectClient_get_sClientId(pointer->clitable[client]); + return ConnectClient_get_sClientId(ServerRealm_get_clientsTable(pointer)[client]); } int -get_clientid(RealmT* pointer, char* clientname) +get_clientid(ServerRealm* pointer, char* clientname) { int i, n; char guard; - for (i = 0; i < pointer->clinum; ++i) { - if (ConnectClient_get_sClientId(pointer->clitable[i]) != NULL) { - if (strcmp(clientname, ConnectClient_get_sClientId(pointer->clitable[i])) == 0) { - return ConnectClient_get_clientId(pointer->clitable[i]); + for (i = 0; i < ServerRealm_get_clientsLimit(pointer); ++i) { + if (ConnectClient_get_sClientId(ServerRealm_get_clientsTable(pointer)[i]) != NULL) { + if (strcmp(clientname, ConnectClient_get_sClientId(ServerRealm_get_clientsTable(pointer)[i])) == 0) { + return ConnectClient_get_clientId(ServerRealm_get_clientsTable(pointer)[i]); } } } if (sscanf(clientname, "%d%c", &i, &guard) == 1) { n = get_clientnumber(pointer, i); - if ((n >= 0) && (n < pointer->clinum)) { - if (ConnectClient_get_sClientId(pointer->clitable[n]) == NULL) { + if ((n >= 0) && (n < ServerRealm_get_clientsLimit(pointer))) { + if (ConnectClient_get_sClientId(ServerRealm_get_clientsTable(pointer)[n]) == NULL) { return i; } } @@ -64,11 +64,11 @@ get_clientid(RealmT* pointer, char* clientname) } int -get_clientnumber(RealmT* pointer, int clientid) +get_clientnumber(ServerRealm* pointer, int clientid) { int i; - for (i = 0; i < pointer->clinum; ++i) { - if (ConnectClient_get_clientId(pointer->clitable[i]) == clientid) { + for (i = 0; i < ServerRealm_get_clientsLimit(pointer); ++i) { + if (ConnectClient_get_clientId(ServerRealm_get_clientsTable(pointer)[i]) == clientid) { return i; } } @@ -77,37 +77,37 @@ get_clientnumber(RealmT* pointer, int clientid) } char* -get_raclientname(RealmT* pointer, int client) +get_raclientname(ServerRealm* pointer, int client) { static char clientname[10]; - if (ConnectClient_get_sClientId(pointer->raclitable[client]) == NULL) { + if (ConnectClient_get_sClientId(ServerRealm_get_raClientsTable(pointer)[client]) == NULL) { memset(clientname, 0, 10); - sprintf(clientname, "%d", ConnectClient_get_clientId(pointer->raclitable[client])); + sprintf(clientname, "%d", ConnectClient_get_clientId(ServerRealm_get_raClientsTable(pointer)[client])); return clientname; } - return ConnectClient_get_sClientId(pointer->raclitable[client]); + return ConnectClient_get_sClientId(ServerRealm_get_raClientsTable(pointer)[client]); } int -get_raclientid(RealmT* pointer, char* clientname) +get_raclientid(ServerRealm* pointer, char* clientname) { int i, n; char guard; - for (i = 0; i < pointer->raclinum; ++i) { - if (ConnectClient_get_sClientId(pointer->raclitable[i]) != NULL) { - if (strcmp(clientname, ConnectClient_get_sClientId(pointer->raclitable[i])) == 0) { - return ConnectClient_get_clientId(pointer->raclitable[i]); + for (i = 0; i < ServerRealm_get_raClientsLimit(pointer); ++i) { + if (ConnectClient_get_sClientId(ServerRealm_get_raClientsTable(pointer)[i]) != NULL) { + if (strcmp(clientname, ConnectClient_get_sClientId(ServerRealm_get_raClientsTable(pointer)[i])) == 0) { + return ConnectClient_get_clientId(ServerRealm_get_raClientsTable(pointer)[i]); } } } if (sscanf(clientname, "%d%c", &i, &guard) == 1) { n = get_raclientnumber(pointer, i); - if ((n >= 0) && (n < pointer->raclinum)) { - if (ConnectClient_get_sClientId(pointer->raclitable[n]) == NULL) { + if ((n >= 0) && (n < ServerRealm_get_raClientsLimit(pointer))) { + if (ConnectClient_get_sClientId(ServerRealm_get_raClientsTable(pointer)[n]) == NULL) { return i; } } @@ -117,11 +117,11 @@ get_raclientid(RealmT* pointer, char* clientname) } int -get_raclientnumber(RealmT* pointer, int clientid) +get_raclientnumber(ServerRealm* pointer, int clientid) { int i; - for (i = 0; i < pointer->raclinum; ++i) { - if (ConnectClient_get_clientId(pointer->raclitable[i]) == clientid) { + for (i = 0; i < ServerRealm_get_raClientsLimit(pointer); ++i) { + if (ConnectClient_get_clientId(ServerRealm_get_raClientsTable(pointer)[i]) == clientid) { return i; } } diff --git a/src/clientnames.h b/src/clientnames.h index c93a2dc..9925c25 100644 --- a/src/clientnames.h +++ b/src/clientnames.h @@ -19,17 +19,18 @@ */ #include "activefor.h" +#include "server_realm_struct.h" #ifndef _JS_CLIENTNAMES_H #define _JS_CLIENTNAMES_H -char* get_clientname(RealmT*, int); -int get_clientid(RealmT*, char*); -int get_clientnumber(RealmT*, int); +char* get_clientname(ServerRealm*, int); +int get_clientid(ServerRealm*, char*); +int get_clientnumber(ServerRealm*, int); -char* get_raclientname(RealmT*, int); -int get_raclientid(RealmT*, char*); -int get_raclientnumber(RealmT*, int); +char* get_raclientname(ServerRealm*, int); +int get_raclientid(ServerRealm*, char*); +int get_raclientnumber(ServerRealm*, int); #endif @@ -104,265 +104,3 @@ parse_line(char* buff, char* tab1, char* tab2) } return 1; } - -ConfigurationT -parsefile(char* name, int* status) -{ - static ConfigurationT cfg; - FILE* file = NULL; - int state, i, j, n, listencount, managecount; - char buff[256]; - char helpbuf1[256]; - char helpbuf2[256]; - char* tmpbuf; - - *status = 1; - - memset(buff, 0, 256); - - cfg.certif = NULL; - cfg.keys = NULL; - cfg.size = 0; - cfg.realmtable = NULL; - cfg.dateformat = NULL; - - state = F_UNKNOWN; - - file = fopen(name, "r"); - if (file == NULL) { - return cfg; - } - - while (fgets(buff, 256, file) != NULL) { /* first loop - counting realm */ - helpbuf1[0] = 0; - parse_line(buff, helpbuf1, helpbuf2); - if (strcmp(helpbuf1, "realm")==0) { - ++cfg.size; - } - } - rewind(file); - - cfg.realmtable = calloc(cfg.size, sizeof(RealmT)); - for (i=0; i<cfg.size; ++i) { - cfg.realmtable[i].pass[0] = 1; - cfg.realmtable[i].pass[1] = 2; - cfg.realmtable[i].pass[2] = 3; - cfg.realmtable[i].pass[3] = 4; - } - cfg.size = 0; - *status = 0; - - listencount = managecount = 0; - - - while (fgets(buff, 256, file) != NULL) { /* second loop - counting listen */ - (*status)++; - state = parse_line(buff, helpbuf1, helpbuf2); - if (state) { - if (strcmp(helpbuf1, "realm")==0) { - ++cfg.size; - if (listencount != managecount) { - return cfg; - } - listencount = managecount = 0; - } - else if (strcmp(helpbuf1, "listen")==0) { - if (cfg.size == 0) { - return cfg; - } - ++cfg.realmtable[cfg.size-1].usrclinum; - ++listencount; - } - else if (strcmp(helpbuf1, "manage")==0) { - if (cfg.size == 0) { - return cfg; - } - ++managecount; - } - } - } - rewind(file); - - if (listencount != managecount) { - return cfg; - } - - for (i = 0; i < cfg.size; ++i) { - cfg.realmtable[i].usrclitable = calloc(cfg.realmtable[i].usrclinum, sizeof(UsrCli*)); - for (j = 0; j < cfg.realmtable[i].usrclinum; ++j) { - cfg.realmtable[i].usrclitable[j] = UsrCli_new(); - if (cfg.realmtable[i].usrclitable[j] == NULL) { - aflog(LOG_T_INIT, LOG_I_CRIT, - "Problem with allocating memory for UsrCli structure... exiting"); - return cfg; - } - } - } - - cfg.size = 0; - *status = 0; - - - while (fgets(buff, 256, file) != NULL) { /* third loop - parsing file */ - (*status)++; - state = parse_line(buff, helpbuf1, helpbuf2); - if (helpbuf1[0] == '#') { - memset(buff, 0, 256); - continue; - } - if (state == 1) { - if (strcmp(helpbuf1, "realm")==0) { - ++cfg.size; - TYPE_SET_SSL(cfg.realmtable[cfg.size-1].type); - TYPE_SET_ZLIB(cfg.realmtable[cfg.size-1].type); - listencount = managecount = 0; - } - else if (cfg.size == 0) { - return cfg; - } - else if (strcmp(helpbuf1, "nossl")==0) { - TYPE_UNSET_SSL(cfg.realmtable[cfg.size-1].type); - } - else if (strcmp(helpbuf1, "nozlib")==0) { - TYPE_UNSET_ZLIB(cfg.realmtable[cfg.size-1].type); - } - else if (strcmp(helpbuf1, "baseport")==0) { - cfg.realmtable[cfg.size-1].baseport = 1; - } - else if (strcmp(helpbuf1, "audit")==0) { - cfg.realmtable[cfg.size-1].audit = 1; - } - else if (strcmp(helpbuf1, "dnslookups")==0) { - cfg.realmtable[cfg.size-1].dnslookups = 1; - } -#ifdef HAVE_LIBPTHREAD - else if (strcmp(helpbuf1, "enableproxy")==0) { - if (cfg.realmtable[cfg.size-1].tunneltype == 0) { - cfg.realmtable[cfg.size-1].tunneltype = 1; - } - else { - return cfg; - } - } -#endif -#ifdef AF_INET6 - else if (strcmp(helpbuf1, "ipv4")==0) { - if (TYPE_IS_UNSPEC(cfg.realmtable[cfg.size-1].type)) { - TYPE_SET_IPV4(cfg.realmtable[cfg.size-1].type); - } - else { - return cfg; - } - } - else if (strcmp(helpbuf1, "ipv6")==0) { - if (TYPE_IS_UNSPEC(cfg.realmtable[cfg.size-1].type)) { - TYPE_SET_IPV6(cfg.realmtable[cfg.size-1].type); - } - else { - return cfg; - } - } -#endif - else { - return cfg; - } - } - else if (state == 2) { - if (strcmp(helpbuf1, "realm")==0) { - ++cfg.size; - TYPE_SET_SSL(cfg.realmtable[cfg.size-1].type); - TYPE_SET_ZLIB(cfg.realmtable[cfg.size-1].type); - listencount = managecount = 0; - cfg.realmtable[cfg.size-1].realmname = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.realmtable[cfg.size-1].realmname, helpbuf2); - } - else if (strcmp(helpbuf1, "certificate")==0) { - cfg.certif = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.certif, helpbuf2); - } - else if (strcmp(helpbuf1, "key")==0) { - cfg.keys = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.keys, helpbuf2); - } - else if (strcmp(helpbuf1, "log")==0) { - tmpbuf = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(tmpbuf, helpbuf2); - addlogtarget(tmpbuf); - } - else if (strcmp(helpbuf1, "dateformat")==0) { - cfg.dateformat = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.dateformat, helpbuf2); - } - else if (cfg.size == 0) { - return cfg; - } - else if (strcmp(helpbuf1, "hostname")==0) { - cfg.realmtable[cfg.size-1].hostname = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.realmtable[cfg.size-1].hostname, helpbuf2); - } - else if (strcmp(helpbuf1, "listen")==0) { - UsrCli_set_listenPortName(cfg.realmtable[cfg.size-1].usrclitable[listencount], helpbuf2); - ++listencount; - } - else if (strcmp(helpbuf1, "pass")==0) { - n = strlen(helpbuf2); - memset(cfg.realmtable[cfg.size-1].pass, 0, 4); - for (i = 0; i < n; ++i) { - cfg.realmtable[cfg.size-1].pass[i%4] += helpbuf2[i]; - } - } - else if (strcmp(helpbuf1, "manage")==0) { - UsrCli_set_managePortName(cfg.realmtable[cfg.size-1].usrclitable[managecount], helpbuf2); - ++managecount; - } - else if (strcmp(helpbuf1, "users")==0) { - cfg.realmtable[cfg.size-1].users = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.realmtable[cfg.size-1].users, helpbuf2); - } - else if (strcmp(helpbuf1, "timeout")==0) { - cfg.realmtable[cfg.size-1].timeout = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.realmtable[cfg.size-1].timeout, helpbuf2); - } - else if (strcmp(helpbuf1, "clients")==0) { - cfg.realmtable[cfg.size-1].clients = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.realmtable[cfg.size-1].clients, helpbuf2); - } - else if (strcmp(helpbuf1, "raclients")==0) { - cfg.realmtable[cfg.size-1].raclients = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.realmtable[cfg.size-1].raclients, helpbuf2); - } - else if (strcmp(helpbuf1, "usrpcli")==0) { - cfg.realmtable[cfg.size-1].usrpcli = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.realmtable[cfg.size-1].usrpcli, helpbuf2); - } - else if (strcmp(helpbuf1, "climode")==0) { - cfg.realmtable[cfg.size-1].clim = calloc(strlen(helpbuf2)+1, sizeof(char)); - strcpy(cfg.realmtable[cfg.size-1].clim, helpbuf2); - } - else if (strcmp(helpbuf1, "proto")==0) { - if (TYPE_IS_SET(cfg.realmtable[cfg.size-1].type)) { - return cfg; - } - if (strcmp(helpbuf2, "tcp")==0) { - TYPE_SET_TCP(cfg.realmtable[cfg.size-1].type); - } - else if (strcmp(helpbuf2, "udp")==0) { - TYPE_SET_UDP(cfg.realmtable[cfg.size-1].type); - } - else { - return cfg; - } - } - else { - return cfg; - } - } - memset(buff, 0, 256); - } - - fclose(file); - - *status = 0; - return cfg; -} - @@ -20,6 +20,8 @@ #include "activefor.h" #include "network.h" +#include "server_configuration_struct.h" +#include "client_configuration_struct.h" #include <openssl/ssl.h> #ifndef _JS_FILE_H @@ -31,7 +33,9 @@ #define F_RVALUE 4 #define F_MIDDLE 5 -ConfigurationT parsefile(char*, int*); /* parse the cfg file */ +int parse_line(char* buff, char* tab1, char* tab2); /* parse one line of the file */ +ServerConfiguration* parsefile(char*, int*); /* parse the cfg file */ +ClientConfiguration* cparsefile(char*, int*); /* parse the client's cfg file */ #endif diff --git a/src/file_client.c b/src/file_client.c new file mode 100644 index 0000000..68ece7a --- /dev/null +++ b/src/file_client.c @@ -0,0 +1,261 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include <config.h> + +#include "file.h" +#include "activefor.h" +#include "logging.h" +#include "network.h" +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <limits.h> +#include <ctype.h> + +ClientConfiguration* +cparsefile(char* name, int* status) +{ + ClientConfiguration* cfg; + FILE* file = NULL; + int state, i, n; +#ifdef AF_INET6 + int temp; +#endif + char buff[256]; + char helpbuf1[256]; + char helpbuf2[256]; + char* tmpbuf; + unsigned char pass[4] = {1, 2, 3, 4}; + + *status = 1; + + memset(buff, 0, 256); + + cfg = ClientConfiguration_new(); + + if (cfg == NULL) { + printf("Can't allocate memory for client configuration... exiting\n"); + exit(1); + } + + state = F_UNKNOWN; + + file = fopen(name, "r"); + if (file == NULL) { + return cfg; + } + + ClientConfiguration_set_realmsNumber(cfg, 1); + + ClientConfiguration_set_realmsTable(cfg, + calloc(ClientConfiguration_get_realmsNumber(cfg), sizeof(ClientRealm*))); + if (ClientConfiguration_get_realmsTable(cfg) == NULL) { + printf("Can't allocate memory for ClientRealm* table... exiting\n"); + exit(1); + } + for (i = 0; i < ClientConfiguration_get_realmsNumber(cfg); ++i) { + ClientConfiguration_get_realmsTable(cfg)[i] = ClientRealm_new(); + if (ClientConfiguration_get_realmsTable(cfg)[i] == NULL) { + printf("Problem with allocating memory for ClientRealm structure... exiting"); + exit(1); + } + ClientRealm_set_password(ClientConfiguration_get_realmsTable(cfg)[i], pass); + } + *status = 0; + + while (fgets(buff, 256, file) != NULL) { /* second loop - parsing file */ + (*status)++; + state = parse_line(buff, helpbuf1, helpbuf2); + if (helpbuf1[0] == '#') { + memset(buff, 0, 256); + continue; + } + if (state == 1) { + if (strcmp(helpbuf1, "ignorepkeys") == 0) { + ClientConfiguration_set_ignorePublicKeys(cfg, 1); + } + else if (strcmp(helpbuf1, "ar-start") == 0) { + ArOptions_set_arStart(ClientRealm_get_arOptions(ClientConfiguration_get_realmsTable(cfg)[0]), + AR_OPTION_ENABLED); + } + else if (strcmp(helpbuf1, "ar-quit") == 0) { + ArOptions_set_arQuit(ClientRealm_get_arOptions(ClientConfiguration_get_realmsTable(cfg)[0]), + AR_OPTION_ENABLED); + } + else if (strcmp(helpbuf1, "noar") == 0) { + ArOptions_set_arPremature(ClientRealm_get_arOptions(ClientConfiguration_get_realmsTable(cfg)[0]), + AR_OPTION_DISABLED); + } + else if ((strcmp(helpbuf1, "u") == 0) || (strcmp(helpbuf1, "udpmode") == 0)) { + if (ClientRealm_get_clientMode(ClientConfiguration_get_realmsTable(cfg)[0]) == CLIENTREALM_MODE_TCP) { + ClientRealm_set_clientMode(ClientConfiguration_get_realmsTable(cfg)[0], CLIENTREALM_MODE_UDP); + } + else { + ClientRealm_set_clientMode(ClientConfiguration_get_realmsTable(cfg)[0], CLIENTREALM_MODE_UNKNOWN); + } + } + else if ((strcmp(helpbuf1, "U") == 0) || (strcmp(helpbuf1, "reverseudp") == 0)) { + if (ClientRealm_get_clientMode(ClientConfiguration_get_realmsTable(cfg)[0]) == CLIENTREALM_MODE_TCP) { + ClientRealm_set_clientMode(ClientConfiguration_get_realmsTable(cfg)[0], CLIENTREALM_MODE_REVERSE); + } + else { + ClientRealm_set_clientMode(ClientConfiguration_get_realmsTable(cfg)[0], CLIENTREALM_MODE_UNKNOWN); + } + } + else if ((strcmp(helpbuf1, "r") == 0) || (strcmp(helpbuf1, "remoteadmin") == 0)) { + if (ClientRealm_get_clientMode(ClientConfiguration_get_realmsTable(cfg)[0]) == CLIENTREALM_MODE_TCP) { + ClientRealm_set_clientMode(ClientConfiguration_get_realmsTable(cfg)[0], CLIENTREALM_MODE_REMOTE); + } + else { + ClientRealm_set_clientMode(ClientConfiguration_get_realmsTable(cfg)[0], CLIENTREALM_MODE_UNKNOWN); + } + } + else if ((strcmp(helpbuf1, "S") == 0) || (strcmp(helpbuf1, "use-https") == 0)) { + HttpProxyOptions_use_https(ClientRealm_get_httpProxyOptions( + ClientConfiguration_get_realmsTable(cfg)[0])); + } + else if ((strcmp(helpbuf1, "B") == 0) || (strcmp(helpbuf1, "pa-t-basic") == 0)) { + HttpProxyOptions_set_proxyauth_type(ClientRealm_get_httpProxyOptions( + ClientConfiguration_get_realmsTable(cfg)[0]), PROXYAUTH_TYPE_BASIC); + } +#ifdef HAVE_LIBPTHREAD + else if (strcmp(helpbuf1, "enableproxy")==0) { + if (ClientRealm_get_tunnelType( + ClientConfiguration_get_realmsTable(cfg)[0]) == 0) { + ClientRealm_set_tunnelType( + ClientConfiguration_get_realmsTable(cfg)[0], + 1); + } + else { + return cfg; + } + } +#endif +#ifdef AF_INET6 + else if (strcmp(helpbuf1, "ipv4")==0) { + if (TYPE_IS_UNSPEC(ClientRealm_get_realmType( + ClientConfiguration_get_realmsTable(cfg)[0]))) { + temp = ClientRealm_get_realmType(ClientConfiguration_get_realmsTable(cfg)[0]); + TYPE_SET_IPV4(temp); + ClientRealm_set_realmType(ClientConfiguration_get_realmsTable(cfg)[0], temp); + } + else { + return cfg; + } + } + else if (strcmp(helpbuf1, "ipv6")==0) { + if (TYPE_IS_UNSPEC(ClientRealm_get_realmType( + ClientConfiguration_get_realmsTable(cfg)[0]))) { + temp = ClientRealm_get_realmType(ClientConfiguration_get_realmsTable(cfg)[0]); + TYPE_SET_IPV6(temp); + ClientRealm_set_realmType(ClientConfiguration_get_realmsTable(cfg)[0], temp); + } + else { + return cfg; + } + } +#endif + else { + return cfg; + } + } + else if (state == 2) { + if ((strcmp(helpbuf1, "k") == 0) || (strcmp(helpbuf1, "keyfile") == 0)) { + ClientConfiguration_set_keysFile(cfg, helpbuf2); + } + else if ((strcmp(helpbuf1, "s") == 0) || (strcmp(helpbuf1, "storefile") == 0)) { + ClientConfiguration_set_storeFile(cfg, helpbuf2); + } + else if ((strcmp(helpbuf1, "o") == 0) || (strcmp(helpbuf1, "log") == 0)) { + tmpbuf = calloc(strlen(helpbuf2)+1, sizeof(char)); + strcpy(tmpbuf, helpbuf2); + addlogtarget(tmpbuf); + } + else if ((strcmp(helpbuf1, "D") == 0) || (strcmp(helpbuf1, "dateformat") == 0)) { + ClientConfiguration_set_dateFormat(cfg, helpbuf2); + } + else if ((strcmp(helpbuf1, "n") == 0) || (strcmp(helpbuf1, "servername") == 0)) { + ClientRealm_set_serverName(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if ((strcmp(helpbuf1, "m") == 0) || (strcmp(helpbuf1, "manageport") == 0)) { + ClientRealm_set_hostName(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if ((strcmp(helpbuf1, "d") == 0) || (strcmp(helpbuf1, "hostname") == 0)) { + ClientRealm_set_hostName(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if ((strcmp(helpbuf1, "p") == 0) || (strcmp(helpbuf1, "portnum") == 0)) { + ClientRealm_set_destinationPort(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if (strcmp(helpbuf1, "localname") == 0) { + ClientRealm_set_localName(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if (strcmp(helpbuf1, "localport") == 0) { + ClientRealm_set_localPort(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if (strcmp(helpbuf1, "localdesname") == 0) { + ClientRealm_set_localDestinationName(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if ((strcmp(helpbuf1, "i") == 0) || (strcmp(helpbuf1, "id") == 0)) { + ClientRealm_set_realmId(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if (strcmp(helpbuf1, "pass") == 0) { + n = strlen(helpbuf2); + memset(pass, 0, 4); + for (i = 0; i < n; ++i) { + pass[i%4] += helpbuf2[i]; + } + ClientRealm_set_password(ClientConfiguration_get_realmsTable(cfg)[0], pass); + } + else if ((strcmp(helpbuf1, "K") == 0) || (strcmp(helpbuf1, "keep-alive") == 0)) { + ClientRealm_set_sKeepAliveTimeout(ClientConfiguration_get_realmsTable(cfg)[0], helpbuf2); + } + else if ((strcmp(helpbuf1, "A") == 0) || (strcmp(helpbuf1, "ar-tries") == 0)) { + ArOptions_set_s_arTries(ClientRealm_get_arOptions(ClientConfiguration_get_realmsTable(cfg)[0]), + helpbuf2); + } + else if ((strcmp(helpbuf1, "T") == 0) || (strcmp(helpbuf1, "ar-delay") == 0)) { + ArOptions_set_s_arDelay(ClientRealm_get_arOptions(ClientConfiguration_get_realmsTable(cfg)[0]), + helpbuf2); + } + else if ((strcmp(helpbuf1, "P") == 0) || (strcmp(helpbuf1, "proxyname") == 0)) { + HttpProxyOptions_set_proxyname(ClientRealm_get_httpProxyOptions( + ClientConfiguration_get_realmsTable(cfg)[0]), helpbuf2); + } + else if ((strcmp(helpbuf1, "X") == 0) || (strcmp(helpbuf1, "proxyport") == 0)) { + HttpProxyOptions_set_proxyport(ClientRealm_get_httpProxyOptions( + ClientConfiguration_get_realmsTable(cfg)[0]), helpbuf2); + } + else if ((strcmp(helpbuf1, "C") == 0) || (strcmp(helpbuf1, "pa-cred") == 0)) { + HttpProxyOptions_set_proxyauth_cred(ClientRealm_get_httpProxyOptions( + ClientConfiguration_get_realmsTable(cfg)[0]), helpbuf2); + } + else { + return cfg; + } + } + memset(buff, 0, 256); + } + + fclose(file); + + *status = 0; + return cfg; +} diff --git a/src/file_server.c b/src/file_server.c new file mode 100644 index 0000000..3db91a6 --- /dev/null +++ b/src/file_server.c @@ -0,0 +1,367 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include <config.h> + +#include "file.h" +#include "activefor.h" +#include "logging.h" +#include "network.h" +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <limits.h> +#include <ctype.h> + +ServerConfiguration* +parsefile(char* name, int* status) +{ + ServerConfiguration* cfg; + FILE* file = NULL; + int state, i, j, n, listencount, managecount, temp; + char buff[256]; + char helpbuf1[256]; + char helpbuf2[256]; + char* tmpbuf; + unsigned char pass[4] = {1, 2, 3, 4}; + + *status = 1; + + memset(buff, 0, 256); + + cfg = ServerConfiguration_new(); + + if (cfg == NULL) { + printf("Can't allocate memory for server configuration... exiting\n"); + exit(1); + } + + state = F_UNKNOWN; + + file = fopen(name, "r"); + if (file == NULL) { + return cfg; + } + + while (fgets(buff, 256, file) != NULL) { /* first loop - counting realm */ + helpbuf1[0] = 0; + parse_line(buff, helpbuf1, helpbuf2); + if (strcmp(helpbuf1, "realm")==0) { + ServerConfiguration_set_realmsNumber(cfg, ServerConfiguration_get_realmsNumber(cfg) + 1); + } + } + rewind(file); + + ServerConfiguration_set_realmsTable(cfg, + calloc(ServerConfiguration_get_realmsNumber(cfg), sizeof(ServerRealm*))); + if (ServerConfiguration_get_realmsTable(cfg) == NULL) { + printf("Can't allocate memory for ServerRealm* table... exiting\n"); + exit(1); + } + for (i = 0; i < ServerConfiguration_get_realmsNumber(cfg); ++i) { + ServerConfiguration_get_realmsTable(cfg)[i] = ServerRealm_new(); + if (ServerConfiguration_get_realmsTable(cfg)[i] == NULL) { + printf("Problem with allocating memory for ServerRealm structure... exiting"); + exit(1); + } + ServerRealm_set_password(ServerConfiguration_get_realmsTable(cfg)[i], pass); + } + ServerConfiguration_set_realmsNumber(cfg, 0); + *status = 0; + + listencount = managecount = 0; + + + while (fgets(buff, 256, file) != NULL) { /* second loop - counting listen */ + (*status)++; + state = parse_line(buff, helpbuf1, helpbuf2); + if (state) { + if (strcmp(helpbuf1, "realm") == 0) { + ServerConfiguration_set_realmsNumber(cfg, ServerConfiguration_get_realmsNumber(cfg) + 1); + if (listencount != managecount) { + return cfg; + } + listencount = managecount = 0; + } + else if ((strcmp(helpbuf1, "listen") == 0) || (strcmp(helpbuf1, "listenport") == 0)) { + if (ServerConfiguration_get_realmsNumber(cfg) == 0) { + return cfg; + } + ServerRealm_set_userClientPairs( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + ServerRealm_get_userClientPairs(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]) + + 1); + ++listencount; + } + else if ((strcmp(helpbuf1, "manage") == 0) || (strcmp(helpbuf1, "manageport") == 0)) { + if (ServerConfiguration_get_realmsNumber(cfg) == 0) { + return cfg; + } + ++managecount; + } + } + } + rewind(file); + + if (listencount != managecount) { + return cfg; + } + + for (i = 0; i < ServerConfiguration_get_realmsNumber(cfg); ++i) { + ServerRealm_set_usersClientsTable(ServerConfiguration_get_realmsTable(cfg)[i], + calloc(ServerRealm_get_userClientPairs(ServerConfiguration_get_realmsTable(cfg)[i]), sizeof(UsrCli*))); + if (ServerRealm_get_usersClientsTable(ServerConfiguration_get_realmsTable(cfg)[i]) == NULL) { + printf("Problem with allocating memory for UsrCli* table... exiting"); + return cfg; + } + for (j = 0; j < ServerRealm_get_userClientPairs(ServerConfiguration_get_realmsTable(cfg)[i]); ++j) { + ServerRealm_get_usersClientsTable(ServerConfiguration_get_realmsTable(cfg)[i])[j] = UsrCli_new(); + if (ServerRealm_get_usersClientsTable(ServerConfiguration_get_realmsTable(cfg)[i])[j] == NULL) { + printf("Problem with allocating memory for UsrCli structure... exiting"); + return cfg; + } + } + } + + ServerConfiguration_set_realmsNumber(cfg, 0); + *status = 0; + + + while (fgets(buff, 256, file) != NULL) { /* third loop - parsing file */ + (*status)++; + state = parse_line(buff, helpbuf1, helpbuf2); + if (helpbuf1[0] == '#') { + memset(buff, 0, 256); + continue; + } + if (state == 1) { + if (strcmp(helpbuf1, "realm")==0) { + ServerConfiguration_set_realmsNumber(cfg, ServerConfiguration_get_realmsNumber(cfg) + 1); + temp = ServerRealm_get_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]); + TYPE_SET_SSL(temp); + TYPE_SET_ZLIB(temp); + ServerRealm_set_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1], temp); + listencount = managecount = 0; + } + else if (ServerConfiguration_get_realmsNumber(cfg) == 0) { + return cfg; + } + else if (strcmp(helpbuf1, "nossl")==0) { + temp = ServerRealm_get_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]); + TYPE_UNSET_SSL(temp); + ServerRealm_set_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1], temp); + } + else if (strcmp(helpbuf1, "nozlib")==0) { + temp = ServerRealm_get_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]); + TYPE_UNSET_ZLIB(temp); + ServerRealm_set_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1], temp); + } + else if (strcmp(helpbuf1, "baseport")==0) { + ServerRealm_set_basePortOn( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + 1); + } + else if (strcmp(helpbuf1, "audit")==0) { + ServerRealm_set_auditOn( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + 1); + } + else if (strcmp(helpbuf1, "dnslookups")==0) { + ServerRealm_set_dnsLookupsOn( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + 1); + } +#ifdef HAVE_LIBPTHREAD + else if (strcmp(helpbuf1, "enableproxy")==0) { + if (ServerRealm_get_tunnelType( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1]) == 0) { + ServerRealm_set_tunnelType( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + 1); + } + else { + return cfg; + } + } +#endif +#ifdef AF_INET6 + else if (strcmp(helpbuf1, "ipv4")==0) { + if (TYPE_IS_UNSPEC(ServerRealm_get_realmType( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1]))) { + temp = ServerRealm_get_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]); + TYPE_SET_IPV4(temp); + ServerRealm_set_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1], temp); + } + else { + return cfg; + } + } + else if (strcmp(helpbuf1, "ipv6")==0) { + if (TYPE_IS_UNSPEC(ServerRealm_get_realmType( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1]))) { + temp = ServerRealm_get_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]); + TYPE_SET_IPV6(temp); + ServerRealm_set_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1], temp); + } + else { + return cfg; + } + } +#endif + else { + return cfg; + } + } + else if (state == 2) { + if (strcmp(helpbuf1, "realm")==0) { + ServerConfiguration_set_realmsNumber(cfg, ServerConfiguration_get_realmsNumber(cfg) + 1); + temp = ServerRealm_get_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]); + TYPE_SET_SSL(temp); + TYPE_SET_ZLIB(temp); + ServerRealm_set_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1], temp); + listencount = managecount = 0; + ServerRealm_set_realmName( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + helpbuf2); + } + else if ((strcmp(helpbuf1, "certificate") == 0) || (strcmp(helpbuf1, "cerfile") == 0)) { + ServerConfiguration_set_certificateFile(cfg, helpbuf2); + } + else if ((strcmp(helpbuf1, "key") == 0) || (strcmp(helpbuf1, "keyfile") == 0)) { + ServerConfiguration_set_keysFile(cfg, helpbuf2); + } + else if (strcmp(helpbuf1, "log")==0) { + tmpbuf = calloc(strlen(helpbuf2)+1, sizeof(char)); + strcpy(tmpbuf, helpbuf2); + addlogtarget(tmpbuf); + } + else if (strcmp(helpbuf1, "dateformat")==0) { + ServerConfiguration_set_dateFormat(cfg, helpbuf2); + } + else if (ServerConfiguration_get_realmsNumber(cfg) == 0) { + return cfg; + } + else if (strcmp(helpbuf1, "hostname")==0) { + ServerRealm_set_hostName( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + helpbuf2); + } + else if ((strcmp(helpbuf1, "listen") == 0) || (strcmp(helpbuf1, "listenport") == 0)) { + UsrCli_set_listenPortName( + ServerRealm_get_usersClientsTable( + ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1])[listencount], helpbuf2); + ++listencount; + } + else if (strcmp(helpbuf1, "pass")==0) { + n = strlen(helpbuf2); + memset(pass, 0, 4); + for (i = 0; i < n; ++i) { + pass[i%4] += helpbuf2[i]; + } + ServerRealm_set_password( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + pass); + } + else if ((strcmp(helpbuf1, "manage") == 0) || (strcmp(helpbuf1, "manageport") == 0)) { + UsrCli_set_managePortName( + ServerRealm_get_usersClientsTable( + ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1])[managecount], helpbuf2); + ++managecount; + } + else if (strcmp(helpbuf1, "users")==0) { + ServerRealm_set_sUsersLimit( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + helpbuf2); + } + else if (strcmp(helpbuf1, "timeout")==0) { + ServerRealm_set_sTimeout( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + helpbuf2); + } + else if (strcmp(helpbuf1, "clients")==0) { + ServerRealm_set_sClientsLimit( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + helpbuf2); + } + else if (strcmp(helpbuf1, "raclients")==0) { + ServerRealm_set_sRaClientsLimit( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + helpbuf2); + } + else if (strcmp(helpbuf1, "usrpcli")==0) { + ServerRealm_set_sUsersPerClient( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + helpbuf2); + } + else if (strcmp(helpbuf1, "climode")==0) { + ServerRealm_set_sClientMode( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1], + helpbuf2); + } + else if (strcmp(helpbuf1, "proto")==0) { + if (TYPE_IS_SET(ServerRealm_get_realmType( + ServerConfiguration_get_realmsTable(cfg)[ServerConfiguration_get_realmsNumber(cfg) - 1]))) { + return cfg; + } + if (strcmp(helpbuf2, "tcp")==0) { + temp = ServerRealm_get_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]); + TYPE_SET_TCP(temp); + ServerRealm_set_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1], temp); + } + else if (strcmp(helpbuf2, "udp")==0) { + temp = ServerRealm_get_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1]); + TYPE_SET_UDP(temp); + ServerRealm_set_realmType(ServerConfiguration_get_realmsTable(cfg)[ + ServerConfiguration_get_realmsNumber(cfg) - 1], temp); + } + else { + return cfg; + } + } + else { + return cfg; + } + } + memset(buff, 0, 256); + } + + fclose(file); + + *status = 0; + return cfg; +} diff --git a/src/http_proxy_client.c b/src/http_proxy_client.c index 9f0cc7c..6880d73 100644 --- a/src/http_proxy_client.c +++ b/src/http_proxy_client.c @@ -136,7 +136,7 @@ http_proxy_client(void *vptr) /* postfd */ aflog(LOG_T_MAIN, LOG_I_DEBUG, "http%s proxy: connecting (postfd)...", name); - if (ip_connect(&tmp, proxyname, proxyport, type)) { + if (ip_connect(&tmp, proxyname, proxyport, type, NULL, NULL)) { clean_return(conn.sockfd); } SslFd_set_fd(conn.postFd, tmp); @@ -180,7 +180,7 @@ http_proxy_client(void *vptr) /* getfd */ aflog(LOG_T_MAIN, LOG_I_DEBUG, "http%s proxy: connecting (getfd)...", name); - if (ip_connect(&tmp, proxyname, proxyport, type)) { + if (ip_connect(&tmp, proxyname, proxyport, type, NULL, NULL)) { clean_return(conn.sockfd); } SslFd_set_fd(conn.getFd, tmp); @@ -264,7 +264,7 @@ http_proxy_client(void *vptr) /* postfd */ aflog(LOG_T_MAIN, LOG_I_DEBUG, "http%s proxy: connecting (postfd)...", name); - if (ip_connect(&tmp, proxyname, proxyport, type)) { + if (ip_connect(&tmp, proxyname, proxyport, type, NULL, NULL)) { clean_return(conn.sockfd); } SslFd_set_fd(conn.postFd, tmp); @@ -327,7 +327,7 @@ http_proxy_client(void *vptr) /* postfd */ aflog(LOG_T_MAIN, LOG_I_DEBUG, "http%s proxy: connecting (postfd)...", name); - if (ip_connect(&tmp, proxyname, proxyport, type)) { + if (ip_connect(&tmp, proxyname, proxyport, type, NULL, NULL)) { clean_return(conn.sockfd); } SslFd_set_fd(conn.postFd, tmp); @@ -367,7 +367,7 @@ http_proxy_client(void *vptr) /* postfd */ aflog(LOG_T_MAIN, LOG_I_DEBUG, "http%s proxy: connecting (postfd)...", name); - if (ip_connect(&tmp, proxyname, proxyport, type)) { + if (ip_connect(&tmp, proxyname, proxyport, type, NULL, NULL)) { clean_return(conn.sockfd); } SslFd_set_fd(conn.postFd, tmp); @@ -441,7 +441,7 @@ http_proxy_client(void *vptr) /* getfd */ aflog(LOG_T_MAIN, LOG_I_DEBUG, "http%s proxy: connecting (getfd)...", name); - if (ip_connect(&tmp, proxyname, proxyport, type)) { + if (ip_connect(&tmp, proxyname, proxyport, type, NULL, NULL)) { clean_return(conn.sockfd); } SslFd_set_fd(conn.getFd, tmp); @@ -520,8 +520,7 @@ http_proxy_client(void *vptr) } int -initialize_http_proxy_client(int* sockfd, const char *host, const char *serv, - HttpProxyOptions* hpo, const char type, SSL_CTX* ctx) +initialize_http_proxy_client(int* sockfd, ClientRealm* cr, SSL_CTX* ctx) { int retval; int sockets[2]; @@ -533,16 +532,16 @@ initialize_http_proxy_client(int* sockfd, const char *host, const char *serv, } (*sockfd) = sockets[0]; - if (HttpProxyOptions_get_proxyname(hpo) == NULL) { + if (HttpProxyOptions_get_proxyname(ClientRealm_get_httpProxyOptions(cr)) == NULL) { return 1; } start_critical_section(); - arg.host = (char*) host; - arg.serv = (char*) serv; - arg.hpo = hpo; - arg.type = (char) type; + arg.host = ClientRealm_get_serverName(cr); + arg.serv = ClientRealm_get_managePort(cr); + arg.hpo = ClientRealm_get_httpProxyOptions(cr); + arg.type = ClientRealm_get_ipFamily(cr); arg.sockfd = sockets[1]; arg.ctx = ctx; diff --git a/src/http_proxy_client.h b/src/http_proxy_client.h index 31044f3..3ca3459 100644 --- a/src/http_proxy_client.h +++ b/src/http_proxy_client.h @@ -19,11 +19,11 @@ */ #include "http_proxy_options_struct.h" +#include "client_realm_struct.h" #ifndef _JS_HTTP_PROXY_CLIENT_H #define _JS_HTTP_PROXY_CLIENT_H -int initialize_http_proxy_client(int* sockfd, const char *host, const char *serv, - HttpProxyOptions* hpo, const char type, SSL_CTX* ctx); +int initialize_http_proxy_client(int* sockfd, ClientRealm* cr, SSL_CTX* ctx); #endif diff --git a/src/logging.c b/src/logging.c index 650ce9e..4683fd7 100644 --- a/src/logging.c +++ b/src/logging.c @@ -204,7 +204,7 @@ checklogtarget(llnodeT* target) case 2: { if ((*ptr) == ',') { tab[i] = 0; - if (ip_connect(&tmpfd, desc, tab, 1)) { + if (ip_connect(&tmpfd, desc, tab, 1, NULL, NULL)) { return 5; /* can't connect to host:port */ } target->logfd = fdopen(tmpfd, "a"); diff --git a/src/module_struct.c b/src/module_struct.c new file mode 100644 index 0000000..66d760f --- /dev/null +++ b/src/module_struct.c @@ -0,0 +1,217 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include <config.h> + +#ifdef HAVE_LIBDL + +#include "string_functions.h" +#include "module_struct.h" + +#include <stdlib.h> +#include <dlfcn.h> + +/* + * Function name: Module_new + * Description: Create and initialize new Module structure. + * Returns: Pointer to newly created Module structure. + */ + +Module* +Module_new() +{ + Module* tmp = calloc(1, sizeof(Module)); + if (tmp == NULL) { + return NULL; + } + return tmp; +} + +/* + * Function name: Module_free + * Description: Free the memory allocated for Module structure. + * Arguments: m - pointer to pointer to Module structure + */ + +void +Module_free(Module** m) +{ + if (m == NULL) { + return; + } + if ((*m) == NULL) { + return; + } + Module_releaseModule(*m); + if ((*m)->fileName) { + free((*m)->fileName); + (*m)->fileName = NULL; + } + free((*m)); + (*m) = NULL; +} + +/* + * Function name: Module_set_fileName + * Description: Set the filename of the module. + * Arguments: m - pointer to Module structure + * fileName - the filename of the module + */ + +void +Module_set_fileName(Module* m, char* fileName) +{ + if (m == NULL) { + return; + } + string_cp(&(m->fileName), fileName); +} + +/* + * Function name: Module_get_fileName + * Description: Get the filename of the module. + * Arguments: m - pointer to Module structure + * Returns: The filename of the module. + */ + +char* +Module_get_fileName(Module* m) +{ + if (m == NULL) { + return NULL; + } + return m->fileName; +} + +/* + * Function name: Module_loadModule + * Description: Load the module into the memory. + * Arguments: m - pointer to Module structure + * Returns: 0 - successful/filename not set + * 1/2 - some errors occured. + */ + +int +Module_loadModule(Module* m) +{ + if (Module_get_fileName(m)) { + m->handle = dlopen(Module_get_fileName(m), RTLD_NOW); + if (!m->handle) { + return 1; + } + dlerror(); + *(void**) (&m->info) = dlsym(m->handle, "info"); + *(void**) (&m->allow) = dlsym(m->handle, "allow"); + *(void**) (&m->filter) = dlsym(m->handle, "filter"); + if (dlerror() != NULL) { + return 2; + } + m->loaded = 1; + } + return 0; +} + +/* + * Function name: Module_releaseModule + * Description: Unload the module from the memory. + * Arguments: m - pointer to Module structure + * Returns: 0 - successful/module was not loaded + * !0 - some errors occured. + */ + +int +Module_releaseModule(Module* m) +{ + if (Module_isModuleLoaded(m)) { + m->loaded = 0; + m->info = NULL; + m->allow = NULL; + m->filter = NULL; + return dlclose(m->handle); + } + return 0; +} + +/* + * Function name: Module_isModuleLoaded + * Description: Check if the module is loaded into the memory. + * Arguments: m - pointer to Module structure + * Returns: 0 - module is not loaded + * 1 - module is loaded. + */ + +int +Module_isModuleLoaded(Module* m) +{ + if (m == NULL) { + return 0; + } + return m->loaded; +} + +/* + * Function name: Module_function_info + * Description: Exec the info function of the module. + * Arguments: m - pointer to Module structure + * Returns: Result of the info function of the module. + */ + +char* +Module_function_info(Module* m) +{ + if (!Module_isModuleLoaded(m)) { + return NULL; + } + return m->info(); +} + +/* + * Function name: Module_function_allow + * Description: Exec the allow function of the module. + * Arguments: m - pointer to Module structure + * Returns: Result of the allow function of the module. + */ + +int +Module_function_allow(Module* m, char* host, char* port) +{ + if (!Module_isModuleLoaded(m)) { + return 0; + } + return m->allow(host, port); +} + +/* + * Function name: Module_function_filter + * Description: Exec the filter function of the module. + * Arguments: m - pointer to Module structure + * Returns: Result of the filter function of the module. + */ + +int +Module_function_filter(Module* m, char* host, unsigned char* message, int* messageLength) +{ + if (!Module_isModuleLoaded(m)) { + return 0; + } + return m->filter(host, message, messageLength); +} + +#endif diff --git a/src/modules.h b/src/module_struct.h index f6f8ea9..cd2c6e8 100644 --- a/src/modules.h +++ b/src/module_struct.h @@ -22,21 +22,33 @@ #ifdef HAVE_LIBDL -# ifndef _JS_MODULES_H -# define _JS_MODULES_H +# ifndef _JS_MODULE_STRUCT_H +# define _JS_MODULE_STRUCT_H typedef struct { char loaded; - char* name; + char* fileName; void* handle; char* (*info)(void); int (*allow)(char*, char*); int (*filter)(char*, unsigned char*, int*); -} moduleT; +} Module; -int loadmodule(moduleT* module); -int releasemodule(moduleT* module); -int ismloaded(moduleT* module); +/* 'constructor' */ +Module* Module_new(); +/* 'destructor' */ +void Module_free(Module** m); +/* setters */ +void Module_set_fileName(Module* m, char* fileName); +/* getters */ +char* Module_get_fileName(Module* m); +/* other */ +int Module_loadModule(Module* m); +int Module_releaseModule(Module* m); +int Module_isModuleLoaded(Module* m); +char* Module_function_info(Module* m); +int Module_function_allow(Module* m, char* host, char* port); +int Module_function_filter(Module* m, char* host, unsigned char* message, int* messageLength); # endif diff --git a/src/modules.c b/src/modules.c deleted file mode 100644 index 0d153be..0000000 --- a/src/modules.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * active port forwarder - software for secure forwarding - * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#include <config.h> - -#ifdef HAVE_LIBDL - -#include "modules.h" - -#include <stdlib.h> -#include <dlfcn.h> - -int -loadmodule(moduleT* module) -{ - if (module->name) { - module->handle = dlopen(module->name, RTLD_NOW); - if (!module->handle) { - return 1; - } - dlerror(); - *(void**) (&module->info) = dlsym(module->handle, "info"); - *(void**) (&module->allow) = dlsym(module->handle, "allow"); - *(void**) (&module->filter) = dlsym(module->handle, "filter"); - if (dlerror() != NULL) { - return 2; - } - module->loaded = 1; - } - return 0; -} - -int -releasemodule(moduleT* module) -{ - if (ismloaded(module)) { - module->loaded = 0; - module->info = NULL; - module->allow = NULL; - module->filter = NULL; - return dlclose(module->handle); - } - return 0; -} - -int -ismloaded(moduleT* module) -{ - return module->loaded; -} - -#endif diff --git a/src/network.c b/src/network.c index 38569cc..60c17e9 100644 --- a/src/network.c +++ b/src/network.c @@ -125,6 +125,7 @@ ip_listen(int* sockfd, const char *host, const char *serv, socklen_t *addrlenp, servaddr.sin_port = htons(port); if (bind((*sockfd), (struct sockaddr*) &servaddr, sizeof(servaddr))){ + printf("bind failed\n"); return 4; } @@ -137,11 +138,14 @@ ip_listen(int* sockfd, const char *host, const char *serv, socklen_t *addrlenp, } int -ip_connect(int* sockfd, const char *host, const char *serv, const char type) +ip_connect(int* sockfd, const char *host, const char *serv, const char type, + const char *lhost, const char *lserv) { #if defined(HAVE_GETADDRINFO) && defined(AF_INET6) int n; + int bindFailed; struct addrinfo hints, *res, *ressave; + struct addrinfo lhints, *lres, *lressave = NULL; bzero(&hints, sizeof(struct addrinfo)); if (type & 0x02) { @@ -160,6 +164,15 @@ ip_connect(int* sockfd, const char *host, const char *serv, const char type) hints.ai_socktype = SOCK_DGRAM; } + lhints = hints; + + if (lhost || lserv) { + if ( (n = getaddrinfo(lhost, lserv, &lhints, &lres)) != 0) { + return n; + } + lressave = lres; + } + if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) { return n; } @@ -171,7 +184,24 @@ ip_connect(int* sockfd, const char *host, const char *serv, const char type) continue; /* ignore this one */ } - if (connect((*sockfd), res->ai_addr, res->ai_addrlen) == 0) { + bindFailed = 0; + if (lhost || lserv) { + bindFailed = 1; + lres = lressave; + do { + if (bind((*sockfd), lres->ai_addr, lres->ai_addrlen) == 0) { + bindFailed = 0; + break; /* success */ + } + } while ( (lres = lres->ai_next) != NULL); + } + + if (bindFailed == 1) { + close((*sockfd)); /* ignore this one */ + continue; + } + + if (connect((*sockfd), res->ai_addr, res->ai_addrlen) == 0) { break; /* success */ } @@ -182,11 +212,15 @@ ip_connect(int* sockfd, const char *host, const char *serv, const char type) return 1; } + if (lhost || lserv) { + freeaddrinfo(lressave); + } freeaddrinfo(ressave); #else - struct sockaddr_in servaddr; + struct sockaddr_in servaddr, lservaddr; struct hostent* hostaddr; - int port; + struct hostent* lhostaddr; + int port, lport; if (type & 0x01) { (*sockfd) = socket(AF_INET, SOCK_STREAM, 0); @@ -210,8 +244,30 @@ ip_connect(int* sockfd, const char *host, const char *serv, const char type) servaddr.sin_port = htons(port); memcpy(&servaddr.sin_addr.s_addr, hostaddr->h_addr_list[0], hostaddr->h_length); + if (lhost || lserv) { + memset(&lservaddr, 0, sizeof(lservaddr)); + lservaddr.sin_family = AF_INET; + if (lserv) { + lport = atoi(lserv); + lservaddr.sin_port = htons(lport); + } + if (lhost) { + lhostaddr = gethostbyname(lhost); + if (lhostaddr == NULL) { + return 3; + } + memcpy(&lservaddr.sin_addr.s_addr, lhostaddr->h_addr_list[0], lhostaddr->h_length); + } + else { + lservaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + } + if (bind((*sockfd), (struct sockaddr*) &lservaddr, sizeof(lservaddr))){ + return 4; + } + } + if (connect((*sockfd), (struct sockaddr*) &servaddr, sizeof(servaddr))){ - return 3; + return 5; } #endif diff --git a/src/network.h b/src/network.h index 6c7e640..4702d1f 100644 --- a/src/network.h +++ b/src/network.h @@ -37,7 +37,8 @@ #include <openssl/ssl.h> int ip_listen(int* sockfd, const char *host, const char *serv, socklen_t *addrlenp, const char type); /* socket, bind, listen... */ -int ip_connect(int* sockfd, const char *host, const char *serv, const char type); /* socket, connect... */ +int ip_connect(int* sockfd, const char *host, const char *serv, const char type, + const char *lhost, const char *lserv); /* socket[, bind], connect... */ char* sock_ntop(const struct sockaddr* sa, socklen_t salen, char* namebuf, char* portbuf, char type); /* return the IP of connected user */ int SSL_writen(SSL* fd, unsigned char* buf, int amount); diff --git a/src/realmnames.c b/src/realmnames.c index 77065bf..be57f8e 100644 --- a/src/realmnames.c +++ b/src/realmnames.c @@ -25,36 +25,36 @@ #include "realmnames.h" char* -get_realmname(ConfigurationT* config, int realm) +get_realmname(ServerConfiguration* config, int realm) { static char realmname[10]; - if (config->realmtable[realm].realmname == NULL) { + if (ServerRealm_get_realmName(ServerConfiguration_get_realmsTable(config)[realm]) == NULL) { memset(realmname, 0, 10); sprintf(realmname, "%d", realm); return realmname; } - return config->realmtable[realm].realmname; + return ServerRealm_get_realmName(ServerConfiguration_get_realmsTable(config)[realm]); } int -get_realmnumber(ConfigurationT* config, char* realmname) +get_realmnumber(ServerConfiguration* config, char* realmname) { int i; char guard; - for (i = 0; i < config->size; ++i) { - if (config->realmtable[i].realmname != NULL) { - if (strcmp(realmname, config->realmtable[i].realmname) == 0) { + for (i = 0; i < ServerConfiguration_get_realmsNumber(config); ++i) { + if (ServerRealm_get_realmName(ServerConfiguration_get_realmsTable(config)[i]) != NULL) { + if (strcmp(realmname, ServerRealm_get_realmName(ServerConfiguration_get_realmsTable(config)[i])) == 0) { return i; } } } if (sscanf(realmname, "%d%c", &i, &guard) == 1) { - if ((i >= 0) && (i < config->size)) { - if (config->realmtable[i].realmname == NULL) { + if ((i >= 0) && (i < ServerConfiguration_get_realmsNumber(config))) { + if (ServerRealm_get_realmName(ServerConfiguration_get_realmsTable(config)[i]) == NULL) { return i; } } diff --git a/src/realmnames.h b/src/realmnames.h index 70ad46f..6b153d6 100644 --- a/src/realmnames.h +++ b/src/realmnames.h @@ -19,12 +19,13 @@ */ #include "activefor.h" +#include "server_configuration_struct.h" #ifndef _JS_REALMNAMES_H #define _JS_REALMNAMES_H -char* get_realmname(ConfigurationT*, int); -int get_realmnumber(ConfigurationT*, char*); +char* get_realmname(ServerConfiguration*, int); +int get_realmnumber(ServerConfiguration*, char*); #endif diff --git a/src/server_check.c b/src/server_check.c index 3fd1091..eb65d97 100644 --- a/src/server_check.c +++ b/src/server_check.c @@ -28,8 +28,8 @@ #include "stats.h" #include "logging.h" -void -check_value(int* where, char* what, char* info) +int +check_value(char* what, char* info) { long tmp = check_value_liberal(what, info); @@ -38,7 +38,7 @@ check_value(int* where, char* what, char* info) "%s: %d\n", info, tmp); exit(1); } - (*where) = tmp; + return tmp; } int diff --git a/src/server_check.h b/src/server_check.h index 0b4ddbf..5428a4a 100644 --- a/src/server_check.h +++ b/src/server_check.h @@ -21,7 +21,7 @@ #ifndef _JS_SERVER_CHECK_H #define _JS_SERVER_CHECK_H -void check_value(int* where, char* what, char* info); +int check_value(char* what, char* info); int check_value_liberal(char* what, char* info); int check_long(char* text, long* number); diff --git a/src/server_configuration_struct.c b/src/server_configuration_struct.c new file mode 100644 index 0000000..58cac07 --- /dev/null +++ b/src/server_configuration_struct.c @@ -0,0 +1,286 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include <config.h> + +#include <stdlib.h> +#include <string.h> + +#include "string_functions.h" +#include "server_configuration_struct.h" + +/* + * Function name: ServerConfiguration_new + * Description: Create and initialize new ServerConfiguration structure. + * Returns: Pointer to newly created ServerConfiguration structure. + */ + +ServerConfiguration* +ServerConfiguration_new() +{ + ServerConfiguration* tmp = calloc(1, sizeof(ServerConfiguration)); + if (tmp == NULL) { + return NULL; + } + return tmp; +} + +/* + * Function name: ServerConfiguration_free + * Description: Free the memory allocated for ServerConfiguration structure. + * Arguments: sc - pointer to pointer to ServerConfiguration structure + */ + +void +ServerConfiguration_free(ServerConfiguration** sc) +{ + int i; + if (sc == NULL) { + return; + } + if ((*sc) == NULL) { + return; + } + if ((*sc)->certificateFile) { + free((*sc)->certificateFile); + (*sc)->certificateFile = NULL; + } + if ((*sc)->keysFile) { + free((*sc)->keysFile); + (*sc)->keysFile = NULL; + } + if ((*sc)->dateFormat) { + free((*sc)->dateFormat); + (*sc)->dateFormat = NULL; + } + if ((*sc)->realmsTable) { + for (i = 0; i < (*sc)->realmsNumber; ++i) { + if ((*sc)->realmsTable[i]) { + ServerRealm_free(&((*sc)->realmsTable[i])); + } + } + free((*sc)->realmsTable); + (*sc)->realmsTable = NULL; + } + free((*sc)); + (*sc) = NULL; +} + +/* + * Function name: ServerConfiguration_set_certificateFile + * Description: Set certificate filename. + * Arguments: sc - pointer to ServerConfiguration structure + * certificateFile - certificate filename + */ + +void +ServerConfiguration_set_certificateFile(ServerConfiguration* sc, char* certificateFile) +{ + if (sc == NULL) { + return; + } + string_cp(&(sc->certificateFile), certificateFile); +} + +/* + * Function name: ServerConfiguration_set_keysFile + * Description: Set keys filename. + * Arguments: sc - pointer to ServerConfiguration structure + * keysFile - keys filename + */ + +void +ServerConfiguration_set_keysFile(ServerConfiguration* sc, char* keysFile) +{ + if (sc == NULL) { + return; + } + string_cp(&(sc->keysFile), keysFile); +} + +/* + * Function name: ServerConfiguration_set_dateFormat + * Description: Set format of the date string. + * Arguments: sc - pointer to ServerConfiguration structure + * dateFormat - format of the date string + */ + +void +ServerConfiguration_set_dateFormat(ServerConfiguration* sc, char* dateFormat) +{ + if (sc == NULL) { + return; + } + string_cp(&(sc->dateFormat), dateFormat); +} + +/* + * Function name: ServerConfiguration_set_realmsNumber + * Description: Set number of realms. + * Arguments: sc - pointer to ServerConfiguration structure + * realmsNumber - number of realms + */ + +void +ServerConfiguration_set_realmsNumber(ServerConfiguration* sc, int realmsNumber) +{ + if (sc == NULL) { + return; + } + sc->realmsNumber = realmsNumber; +} + +/* + * Function name: ServerConfiguration_set_startTime + * Description: Set start time of the server. + * Arguments: sc - pointer to ServerConfiguration structure + * startTime - start time of the server + */ + +void +ServerConfiguration_set_startTime(ServerConfiguration* sc, time_t startTime) +{ + if (sc == NULL) { + return; + } + sc->startTime = startTime; +} + +/* + * Function name: ServerConfiguration_set_realmsTable + * Description: Set table of realms. + * Arguments: sc - pointer to ServerConfiguration structure + * realmsTable - table of realms + */ + +void +ServerConfiguration_set_realmsTable(ServerConfiguration* sc, ServerRealm** realmsTable) +{ + int i; + if (sc == NULL) { + return; + } + if (sc->realmsTable) { + for (i = 0; i < sc->realmsNumber; ++i) { + if (sc->realmsTable[i]) { + ServerRealm_free(&(sc->realmsTable[i])); + } + } + free(sc->realmsTable); + sc->realmsTable = NULL; + } + sc->realmsTable = realmsTable; +} + +/* + * Function name: ServerConfiguration_get_certificateFile + * Description: Get certificate filename. + * Arguments: sc - pointer to ServerConfiguration structure + * Returns: Certificate filename. + */ + +char* +ServerConfiguration_get_certificateFile(ServerConfiguration* sc) +{ + if (sc == NULL) { + return NULL; + } + return sc->certificateFile; +} + +/* + * Function name: ServerConfiguration_get_keysFile + * Description: Get keys filename. + * Arguments: sc - pointer to ServerConfiguration structure + * Returns: Keys filename. + */ + +char* +ServerConfiguration_get_keysFile(ServerConfiguration* sc) +{ + if (sc == NULL) { + return NULL; + } + return sc->keysFile; +} + +/* + * Function name: ServerConfiguration_get_dateFormat + * Description: Get format of the date string. + * Arguments: sc - pointer to ServerConfiguration structure + * Returns: Format of the date string. + */ + +char* +ServerConfiguration_get_dateFormat(ServerConfiguration* sc) +{ + if (sc == NULL) { + return NULL; + } + return sc->dateFormat; +} + +/* + * Function name: ServerConfiguration_get_realmsNumber + * Description: Get number of realms. + * Arguments: sc - pointer to ServerConfiguration structure + * Returns: Number of realms. + */ + +int +ServerConfiguration_get_realmsNumber(ServerConfiguration* sc) +{ + if (sc == NULL) { + return -1; + } + return sc->realmsNumber; +} + +/* + * Function name: ServerConfiguration_get_startTime + * Description: Get start time of the server. + * Arguments: sc - pointer to ServerConfiguration structure + * Returns: Start time of the server. + */ + +time_t +ServerConfiguration_get_startTime(ServerConfiguration* sc) +{ + if (sc == NULL) { + return 0; + } + return sc->startTime; +} + +/* + * Function name: ServerConfiguration_get_realmsTable + * Description: Get table of realms. + * Arguments: sc - pointer to ServerConfiguration structure + * Returns: Table of realms. + */ + +ServerRealm** +ServerConfiguration_get_realmsTable(ServerConfiguration* sc) +{ + if (sc == NULL) { + return NULL; + } + return sc->realmsTable; +} diff --git a/src/server_configuration_struct.h b/src/server_configuration_struct.h new file mode 100644 index 0000000..16ebead --- /dev/null +++ b/src/server_configuration_struct.h @@ -0,0 +1,55 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + + +#ifndef _JS_SERVER_CONFIGURATION_STRUCT_H +#define _JS_SERVER_CONFIGURATION_STRUCT_H + +#include "server_realm_struct.h" + +typedef struct { + char* certificateFile; + char* keysFile; + char* dateFormat; + int realmsNumber; + time_t startTime; + ServerRealm** realmsTable; +} ServerConfiguration; + +/* 'constructor' */ +ServerConfiguration* ServerConfiguration_new(); +/* 'destructor' */ +void ServerConfiguration_free(ServerConfiguration** sc); +/* setters */ +void ServerConfiguration_set_certificateFile(ServerConfiguration* sc, char* certificateFile); +void ServerConfiguration_set_keysFile(ServerConfiguration* sc, char* keysFile); +void ServerConfiguration_set_dateFormat(ServerConfiguration* sc, char* dateFormat); +void ServerConfiguration_set_realmsNumber(ServerConfiguration* sc, int realmsNumber); +void ServerConfiguration_set_startTime(ServerConfiguration* sc, time_t startTime); +void ServerConfiguration_set_realmsTable(ServerConfiguration* sc, ServerRealm** realmsTable); +/* getters */ +char* ServerConfiguration_get_certificateFile(ServerConfiguration* sc); +char* ServerConfiguration_get_keysFile(ServerConfiguration* sc); +char* ServerConfiguration_get_dateFormat(ServerConfiguration* sc); +int ServerConfiguration_get_realmsNumber(ServerConfiguration* sc); +time_t ServerConfiguration_get_startTime(ServerConfiguration* sc); +ServerRealm** ServerConfiguration_get_realmsTable(ServerConfiguration* sc); + +#endif diff --git a/src/server_eval.c b/src/server_eval.c index 4996621..99bd962 100644 --- a/src/server_eval.c +++ b/src/server_eval.c @@ -23,10 +23,10 @@ #include "server_eval.h" int -eval_numofcon(RealmT* ptr, int client, int numofcon) +eval_numofcon(ServerRealm* ptr, int client, int numofcon) { - if ((numofcon >= 0) && (numofcon < ConnectClient_get_limit(ptr->clitable[client]))) { - numofcon = ConnectClient_get_users(ptr->clitable[client])[numofcon]; + if ((numofcon >= 0) && (numofcon < ConnectClient_get_limit(ServerRealm_get_clientsTable(ptr)[client]))) { + numofcon = ConnectClient_get_users(ServerRealm_get_clientsTable(ptr)[client])[numofcon]; } else { numofcon = -1; diff --git a/src/server_eval.h b/src/server_eval.h index 297e6ec..2de5636 100644 --- a/src/server_eval.h +++ b/src/server_eval.h @@ -24,7 +24,7 @@ #ifndef _JS_SERVER_EVAL_H #define _JS_SERVER_EVAL_H -int eval_numofcon(RealmT*, int, int); +int eval_numofcon(ServerRealm*, int, int); int eval_usernum(ConnectClient*, int); #endif diff --git a/src/server_find.c b/src/server_find.c index 73cb9ba..ca4e143 100644 --- a/src/server_find.c +++ b/src/server_find.c @@ -23,17 +23,17 @@ #include "server_find.h" int -find_client(RealmT* ptr, char mode, int usrclipair) +find_client(ServerRealm* ptr, char mode, int usrclipair) { int i; switch(mode) { case 1: { /* fill first client before go to next */ - for (i = 0; i < ptr->clinum; ++i) { - if ((ConnectClient_get_state(ptr->clitable[i]) == + for (i = 0; i < ServerRealm_get_clientsLimit(ptr); ++i) { + if ((ConnectClient_get_state(ServerRealm_get_clientsTable(ptr)[i]) == CONNECTCLIENT_STATE_ACCEPTED) && - (ConnectClient_get_usrCliPair(ptr->clitable[i]) == usrclipair)) { - if (ConnectClient_get_connected(ptr->clitable[i]) < - ConnectClient_get_limit(ptr->clitable[i])) { + (ConnectClient_get_usrCliPair(ServerRealm_get_clientsTable(ptr)[i]) == usrclipair)) { + if (ConnectClient_get_connected(ServerRealm_get_clientsTable(ptr)[i]) < + ConnectClient_get_limit(ServerRealm_get_clientsTable(ptr)[i])) { return i; } } diff --git a/src/server_find.h b/src/server_find.h index dcfe5b5..1460b5d 100644 --- a/src/server_find.h +++ b/src/server_find.h @@ -24,7 +24,7 @@ #ifndef _JS_SERVER_FIND_H #define _JS_SERVER_FIND_H -int find_client(RealmT*, char, int); +int find_client(ServerRealm*, char, int); int find_usernum(ConnectClient*, int); #endif diff --git a/src/server_realm_struct.c b/src/server_realm_struct.c new file mode 100644 index 0000000..a6cfbfd --- /dev/null +++ b/src/server_realm_struct.c @@ -0,0 +1,1327 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include <config.h> + +#include <stdlib.h> +#include <string.h> + +#include "string_functions.h" +#include "server_realm_struct.h" + +/* + * Function name: ServerRealm_new + * Description: Create and initialize new ServerRealm structure. + * Returns: Pointer to newly created ServerRealm structure. + */ + +ServerRealm* +ServerRealm_new() +{ + ServerRealm* tmp = calloc(1, sizeof(ServerRealm)); + if (tmp == NULL) { + return NULL; + } + tmp->password[0] = 1; + tmp->password[1] = 2; + tmp->password[2] = 3; + tmp->password[3] = 4; + return tmp; +} + +/* + * Function name: ServerRealm_free + * Description: Free the memory allocated for ServerRealm structure. + * Arguments: sr - pointer to pointer to ServerRealm structure + */ + +void +ServerRealm_free(ServerRealm** sr) +{ + int i; + if (sr == NULL) { + return; + } + if ((*sr) == NULL) { + return; + } + if ((*sr)->hostName) { + free((*sr)->hostName); + (*sr)->hostName = NULL; + } + if ((*sr)->sUsersLimit) { + free((*sr)->sUsersLimit); + (*sr)->sUsersLimit = NULL; + } + if ((*sr)->sClientsLimit) { + free((*sr)->sClientsLimit); + (*sr)->sClientsLimit = NULL; + } + if ((*sr)->sRaClientsLimit) { + free((*sr)->sRaClientsLimit); + (*sr)->sRaClientsLimit = NULL; + } + if ((*sr)->sUsersPerClient) { + free((*sr)->sUsersPerClient); + (*sr)->sUsersPerClient = NULL; + } + if ((*sr)->sClientMode) { + free((*sr)->sClientMode); + (*sr)->sClientMode = NULL; + } + if ((*sr)->sTimeout) { + free((*sr)->sTimeout); + (*sr)->sTimeout = NULL; + } + if ((*sr)->realmName) { + free((*sr)->realmName); + (*sr)->realmName = NULL; + } + if ((*sr)->clientAddress) { + free((*sr)->clientAddress); + (*sr)->clientAddress = NULL; + } + if ((*sr)->usersTable) { + for (i = 0; i < (*sr)->usersLimit; ++i) { + if ((*sr)->usersTable[i]) { + ConnectUser_free(&((*sr)->usersTable[i])); + } + } + free((*sr)->usersTable); + (*sr)->usersTable = NULL; + } + if ((*sr)->clientsTable) { + for (i = 0; i < (*sr)->clientsLimit; ++i) { + if ((*sr)->clientsTable[i]) { + ConnectClient_free(&((*sr)->clientsTable[i])); + } + } + free((*sr)->clientsTable); + (*sr)->clientsTable = NULL; + } + if ((*sr)->raClientsTable) { + for (i = 0; i < (*sr)->raClientsLimit; ++i) { + if ((*sr)->raClientsTable[i]) { + ConnectClient_free(&((*sr)->raClientsTable[i])); + } + } + free((*sr)->raClientsTable); + (*sr)->raClientsTable = NULL; + } + if ((*sr)->usersClientsTable) { + for (i = 0; i < (*sr)->userClientPairs; ++i) { + if ((*sr)->usersClientsTable[i]) { + UsrCli_free(&((*sr)->usersClientsTable[i])); + } + } + free((*sr)->usersClientsTable); + (*sr)->usersClientsTable = NULL; + } + free((*sr)); + (*sr) = NULL; +} + +/* + * Function name: ServerRealm_set_hostName + * Description: Set realm's host name. + * Arguments: sr - pointer to ServerRealm structure + * hostName - realm's host name + */ + +void +ServerRealm_set_hostName(ServerRealm* sr, char* hostName) +{ + if (sr == NULL) { + return; + } + string_cp(&(sr->hostName), hostName); +} + +/* + * Function name: ServerRealm_set_sUsersLimit + * Description: Set users limit description. + * Arguments: sr - pointer to ServerRealm structure + * sUsersLimit - users limit description + */ + +void +ServerRealm_set_sUsersLimit(ServerRealm* sr, char* sUsersLimit) +{ + if (sr == NULL) { + return; + } + string_cp(&(sr->sUsersLimit), sUsersLimit); +} + +/* + * Function name: ServerRealm_set_sClientsLimit + * Description: Set clients limit description. + * Arguments: sr - pointer to ServerRealm structure + * sClientsLimit - clients limit description + */ + +void +ServerRealm_set_sClientsLimit(ServerRealm* sr, char* sClientsLimit) +{ + if (sr == NULL) { + return; + } + string_cp(&(sr->sClientsLimit), sClientsLimit); +} + +/* + * Function name: ServerRealm_set_sRaClientsLimit + * Description: Set raClients limit description. + * Arguments: sr - pointer to ServerRealm structure + * sRaClientsLimit - raClients limit description + */ + +void +ServerRealm_set_sRaClientsLimit(ServerRealm* sr, char* sRaClientsLimit) +{ + if (sr == NULL) { + return; + } + string_cp(&(sr->sRaClientsLimit), sRaClientsLimit); +} + +/* + * Function name: ServerRealm_set_sUsersPerClient + * Description: Set users per client description. + * Arguments: sr - pointer to ServerRealm structure + * sUsersPerClient - users per client description + */ + +void +ServerRealm_set_sUsersPerClient(ServerRealm* sr, char* sUsersPerClient) +{ + if (sr == NULL) { + return; + } + string_cp(&(sr->sUsersPerClient), sUsersPerClient); +} + +/* + * Function name: ServerRealm_set_sClientMode + * Description: Set client mode description. + * Arguments: sr - pointer to ServerRealm structure + * sClientMode - client mode description + */ + +void +ServerRealm_set_sClientMode(ServerRealm* sr, char* sClientMode) +{ + if (sr == NULL) { + return; + } + string_cp(&(sr->sClientMode), sClientMode); +} + +/* + * Function name: ServerRealm_set_sTimeout + * Description: Set timeout value description. + * Arguments: sr - pointer to ServerRealm structure + * sTimeout - timeout value description + */ + +void +ServerRealm_set_sTimeout(ServerRealm* sr, char* sTimeout) +{ + if (sr == NULL) { + return; + } + string_cp(&(sr->sTimeout), sTimeout); +} + +/* + * Function name: ServerRealm_set_realmName + * Description: Set realm's name. + * Arguments: sr - pointer to ServerRealm structure + * realmName - realm's name + */ + +void +ServerRealm_set_realmName(ServerRealm* sr, char* realmName) +{ + if (sr == NULL) { + return; + } + string_cp(&(sr->realmName), realmName); +} + +/* + * Function name: ServerRealm_set_password + * Description: Set realm's password. + * Arguments: sr - pointer to ServerRealm structure + * password - realm's password + */ + +void +ServerRealm_set_password(ServerRealm* sr, unsigned char* password) +{ + if (sr == NULL) { + return; + } + memcpy(sr->password, password, 4); +} + +/* + * Function name: ServerRealm_set_connectedUsers + * Description: Set number of connected users. + * Arguments: sr - pointer to ServerRealm structure + * connectedUsers - number of connected users + */ + +void +ServerRealm_set_connectedUsers(ServerRealm* sr, int connectedUsers) +{ + if (sr == NULL) { + return; + } + sr->connectedUsers = connectedUsers; +} + +/* + * Function name: ServerRealm_set_usersLimit + * Description: Set limit of connected users. + * Arguments: sr - pointer to ServerRealm structure + * usersLimit - limit of connected users + */ + +void +ServerRealm_set_usersLimit(ServerRealm* sr, int usersLimit) +{ + if (sr == NULL) { + return; + } + sr->usersLimit = usersLimit; +} + +/* + * Function name: ServerRealm_set_connectedClients + * Description: Set number of connected clients. + * Arguments: sr - pointer to ServerRealm structure + * connectedClients - number of connected clients + */ + +void +ServerRealm_set_connectedClients(ServerRealm* sr, int connectedClients) +{ + if (sr == NULL) { + return; + } + sr->connectedClients = connectedClients; +} + +/* + * Function name: ServerRealm_set_clientsLimit + * Description: Set limit of connected clients. + * Arguments: sr - pointer to ServerRealm structure + * clientsLimit - limit of connected clients + */ + +void +ServerRealm_set_clientsLimit(ServerRealm* sr, int clientsLimit) +{ + if (sr == NULL) { + return; + } + sr->clientsLimit = clientsLimit; +} + +/* + * Function name: ServerRealm_set_connectedRaClients + * Description: Set number of connected raClients. + * Arguments: sr - pointer to ServerRealm structure + * connectedRaClients - number of connected raClients + */ + +void +ServerRealm_set_connectedRaClients(ServerRealm* sr, int connectedRaClients) +{ + if (sr == NULL) { + return; + } + sr->connectedRaClients = connectedRaClients; +} + +/* + * Function name: ServerRealm_set_raClientsLimit + * Description: Set limit of connected raClients. + * Arguments: sr - pointer to ServerRealm structure + * raClientsLimit - limit of connected raClients + */ + +void +ServerRealm_set_raClientsLimit(ServerRealm* sr, int raClientsLimit) +{ + if (sr == NULL) { + return; + } + sr->raClientsLimit = raClientsLimit; +} + +/* + * Function name: ServerRealm_set_usersPerClient + * Description: Set users per client limit. + * Arguments: sr - pointer to ServerRealm structure + * usersPerClient - users per client limit + */ + +void +ServerRealm_set_usersPerClient(ServerRealm* sr, int usersPerClient) +{ + if (sr == NULL) { + return; + } + sr->usersPerClient = usersPerClient; +} + +/* + * Function name: ServerRealm_set_timeout + * Description: Set timeout value. + * Arguments: sr - pointer to ServerRealm structure + * timeout - timeout value + */ + +void +ServerRealm_set_timeout(ServerRealm* sr, int timeout) +{ + if (sr == NULL) { + return; + } + sr->timeout = timeout; +} + +/* + * Function name: ServerRealm_set_clientMode + * Description: Set client mode. + * Arguments: sr - pointer to ServerRealm structure + * clientMode - client mode + */ + +void +ServerRealm_set_clientMode(ServerRealm* sr, int clientMode) +{ + if (sr == NULL) { + return; + } + sr->clientMode = clientMode; +} + +/* + * Function name: ServerRealm_set_userClientPairs + * Description: Set user-client pairs amount. + * Arguments: sr - pointer to ServerRealm structure + * userClientPairs - user-client pairs amount + */ + +void +ServerRealm_set_userClientPairs(ServerRealm* sr, int userClientPairs) +{ + if (sr == NULL) { + return; + } + sr->userClientPairs = userClientPairs; +} + +/* + * Function name: ServerRealm_set_clientsCounter + * Description: Set clients counter state. + * Arguments: sr - pointer to ServerRealm structure + * clientsCounter - clients counter state + */ + +void +ServerRealm_set_clientsCounter(ServerRealm* sr, int clientsCounter) +{ + if (sr == NULL) { + return; + } + sr->clientsCounter = clientsCounter; +} + +/* + * Function name: ServerRealm_set_usersCounter + * Description: Set users counter state. + * Arguments: sr - pointer to ServerRealm structure + * usersCounter - users counter state + */ + +void +ServerRealm_set_usersCounter(ServerRealm* sr, int usersCounter) +{ + if (sr == NULL) { + return; + } + sr->usersCounter = usersCounter; +} + +/* + * Function name: ServerRealm_set_realmType + * Description: Set type of the realm. + * Arguments: sr - pointer to ServerRealm structure + * realmType - type of the realm + */ + +void +ServerRealm_set_realmType(ServerRealm* sr, char realmType) +{ + if (sr == NULL) { + return; + } + sr->realmType = realmType; +} + +/* + * Function name: ServerRealm_set_tunnelType + * Description: Set type of the tunnel. + * Arguments: sr - pointer to ServerRealm structure + * tunnelType - type of the tunnel + */ + +void +ServerRealm_set_tunnelType(ServerRealm* sr, char tunnelType) +{ + if (sr == NULL) { + return; + } + sr->tunnelType = tunnelType; +} + +/* + * Function name: ServerRealm_set_dnsLookupsOn + * Description: Set dns lookups functionality on/off. + * Arguments: sr - pointer to ServerRealm structure + * dnsLookupsOn - dns lookups functionality on/off + */ + +void +ServerRealm_set_dnsLookupsOn(ServerRealm* sr, char dnsLookupsOn) +{ + if (sr == NULL) { + return; + } + sr->dnsLookupsOn = dnsLookupsOn; +} + +/* + * Function name: ServerRealm_set_basePortOn + * Description: Set base port functionality on/off. + * Arguments: sr - pointer to ServerRealm structure + * basePortOn - base port functionality on/off + */ + +void +ServerRealm_set_basePortOn(ServerRealm* sr, char basePortOn) +{ + if (sr == NULL) { + return; + } + sr->basePortOn = basePortOn; +} + +/* + * Function name: ServerRealm_set_auditOn + * Description: Set audit functionality on/off. + * Arguments: sr - pointer to ServerRealm structure + * auditOn - audit functionality on/off + */ + +void +ServerRealm_set_auditOn(ServerRealm* sr, char auditOn) +{ + if (sr == NULL) { + return; + } + sr->auditOn = auditOn; +} + +/* + * Function name: ServerRealm_set_addressLength + * Description: Set client's address length. + * Arguments: sr - pointer to ServerRealm structure + * addressLength - client's address length + */ + +void +ServerRealm_set_addressLength(ServerRealm* sr, socklen_t addressLength) +{ + if (sr == NULL) { + return; + } + sr->addressLength = addressLength; +} + +/* + * Function name: ServerRealm_set_clientAddress + * Description: Set client's network address. + * Arguments: sr - pointer to ServerRealm structure + * clientAddress - client's network address + */ + +void +ServerRealm_set_clientAddress(ServerRealm* sr, struct sockaddr* clientAddress) +{ + if (sr == NULL) { + return; + } + if (sr->clientAddress) { + free(sr->clientAddress); + sr->clientAddress = NULL; + } + sr->clientAddress = clientAddress; +} + +/* + * Function name: ServerRealm_set_usersTable + * Description: Set table of users. + * Arguments: sr - pointer to ServerRealm structure + * usersTable - table of users + */ + +void +ServerRealm_set_usersTable(ServerRealm* sr, ConnectUser** usersTable) +{ + int i; + if (sr == NULL) { + return; + } + if (sr->usersTable) { + for (i = 0; i < sr->usersLimit; ++i) { + if (sr->usersTable[i]) { + ConnectUser_free(&(sr->usersTable[i])); + } + } + free(sr->usersTable); + sr->usersTable = NULL; + } + sr->usersTable = usersTable; +} + +/* + * Function name: ServerRealm_set_clientsTable + * Description: Set table of clients. + * Arguments: sr - pointer to ServerRealm structure + * clientsTable - table of clients + */ + +void +ServerRealm_set_clientsTable(ServerRealm* sr, ConnectClient** clientsTable) +{ + int i; + if (sr == NULL) { + return; + } + if (sr->clientsTable) { + for (i = 0; i < sr->clientsLimit; ++i) { + if (sr->clientsTable[i]) { + ConnectClient_free(&(sr->clientsTable[i])); + } + } + free(sr->clientsTable); + sr->clientsTable = NULL; + } + sr->clientsTable = clientsTable; +} + +/* + * Function name: ServerRealm_set_raClientsTable + * Description: Set table of raClients. + * Arguments: sr - pointer to ServerRealm structure + * raClientsTable - table of raClients + */ + +void +ServerRealm_set_raClientsTable(ServerRealm* sr, ConnectClient** raClientsTable) +{ + int i; + if (sr == NULL) { + return; + } + if (sr->raClientsTable) { + for (i = 0; i < sr->raClientsLimit; ++i) { + if (sr->raClientsTable[i]) { + ConnectClient_free(&(sr->raClientsTable[i])); + } + } + free(sr->raClientsTable); + sr->raClientsTable = NULL; + } + sr->raClientsTable = raClientsTable; +} + +/* + * Function name: ServerRealm_set_usersClientsTable + * Description: Set table of user-client pairs. + * Arguments: sr - pointer to ServerRealm structure + * usersClientsTable - table of user-client pairs + */ + +void +ServerRealm_set_usersClientsTable(ServerRealm* sr, UsrCli** usersClientsTable) +{ + int i; + if (sr == NULL) { + return; + } + if (sr->usersClientsTable) { + for (i = 0; i < sr->userClientPairs; ++i) { + if (sr->usersClientsTable[i]) { + UsrCli_free(&(sr->usersClientsTable[i])); + } + } + free(sr->usersClientsTable); + sr->usersClientsTable = NULL; + } + sr->usersClientsTable = usersClientsTable; +} + +/* + * Function name: ServerRealm_get_hostName + * Description: Get realm's host name. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Realm's host name. + */ + +char* +ServerRealm_get_hostName(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->hostName; +} + +/* + * Function name: ServerRealm_get_sUsersLimit + * Description: Get users limit description. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Users limit description. + */ + +char* +ServerRealm_get_sUsersLimit(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->sUsersLimit; +} + +/* + * Function name: ServerRealm_get_sClientsLimit + * Description: Get clients limit description. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Clients limit description. + */ + +char* +ServerRealm_get_sClientsLimit(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->sClientsLimit; +} + +/* + * Function name: ServerRealm_get_sRaClientsLimit + * Description: Get raClients limit description. + * Arguments: sr - pointer to ServerRealm structure + * Returns: RaClients limit description. + */ + +char* +ServerRealm_get_sRaClientsLimit(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->sRaClientsLimit; +} + +/* + * Function name: ServerRealm_get_sUsersPerClient + * Description: Get users per client description. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Users per client description. + */ + +char* +ServerRealm_get_sUsersPerClient(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->sUsersPerClient; +} + +/* + * Function name: ServerRealm_get_sClientMode + * Description: Get client mode description. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Client mode description. + */ + +char* +ServerRealm_get_sClientMode(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->sClientMode; +} + +/* + * Function name: ServerRealm_get_sTimeout + * Description: Get timeout value description. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Timeout value description. + */ + +char* +ServerRealm_get_sTimeout(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->sTimeout; +} + +/* + * Function name: ServerRealm_get_realmName + * Description: Get realm's name. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Realm's name. + */ + +char* +ServerRealm_get_realmName(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->realmName; +} + +/* + * Function name: ServerRealm_get_password + * Description: Get realm's password. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Realm's password. + */ + +unsigned char* +ServerRealm_get_password(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->password; +} + +/* + * Function name: ServerRealm_get_connectedUsers + * Description: Get number of connected users. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Number of connected users. + */ + +int +ServerRealm_get_connectedUsers(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->connectedUsers; +} + +/* + * Function name: ServerRealm_get_usersLimit + * Description: Get limit of connected users. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Limit of connected users. + */ + +int +ServerRealm_get_usersLimit(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->usersLimit; +} + +/* + * Function name: ServerRealm_get_connectedClients + * Description: Get number of connected clients. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Number of connected clients. + */ + +int +ServerRealm_get_connectedClients(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->connectedClients; +} + +/* + * Function name: ServerRealm_get_clientsLimit + * Description: Get limit of connected clients. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Limit of connected clients. + */ + +int +ServerRealm_get_clientsLimit(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->clientsLimit; +} + +/* + * Function name: ServerRealm_get_connectedRaClients + * Description: Get number of connected raClients. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Number of connected raClients. + */ + +int +ServerRealm_get_connectedRaClients(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->connectedRaClients; +} + +/* + * Function name: ServerRealm_get_raClientsLimit + * Description: Get limit of connected raClients. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Limit of connected raClients. + */ + +int +ServerRealm_get_raClientsLimit(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->raClientsLimit; +} + +/* + * Function name: ServerRealm_get_usersPerClient + * Description: Get users per client limit. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Users per client limit. + */ + +int +ServerRealm_get_usersPerClient(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->usersPerClient; +} + +/* + * Function name: ServerRealm_get_timeout + * Description: Get timeout value. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Timeout value. + */ + +int +ServerRealm_get_timeout(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->timeout; +} + +/* + * Function name: ServerRealm_get_clientMode + * Description: Get client mode. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Client mode. + */ + +int +ServerRealm_get_clientMode(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->clientMode; +} + +/* + * Function name: ServerRealm_get_userClientPairs + * Description: Get user-client pairs amount. + * Arguments: sr - pointer to ServerRealm structure + * Returns: User-client pairs amount. + */ + +int +ServerRealm_get_userClientPairs(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->userClientPairs; +} + +/* + * Function name: ServerRealm_get_clientsCounter + * Description: Get clients counter state. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Clients counter state. + */ + +int +ServerRealm_get_clientsCounter(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->clientsCounter; +} + +/* + * Function name: ServerRealm_get_usersCounter + * Description: Get users counter state. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Users counter state. + */ + +int +ServerRealm_get_usersCounter(ServerRealm* sr) +{ + if (sr == NULL) { + return -1; + } + return sr->usersCounter; +} + +/* + * Function name: ServerRealm_get_realmType + * Description: Get type of the realm. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Type of the realm. + */ + +char +ServerRealm_get_realmType(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->realmType; +} + +/* + * Function name: ServerRealm_get_tunnelType + * Description: Get type of the tunnel. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Type of the tunnel. + */ + +char +ServerRealm_get_tunnelType(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->tunnelType; +} + +/* + * Function name: ServerRealm_get_dnsLookupsOn + * Description: Get dns lookups functionality on/off. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Dns lookups functionality on/off. + */ + +char +ServerRealm_get_dnsLookupsOn(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->dnsLookupsOn; +} + +/* + * Function name: ServerRealm_get_basePortOn + * Description: Get base port functionality on/off. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Base port functionality on/off. + */ + +char +ServerRealm_get_basePortOn(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->basePortOn; +} + +/* + * Function name: ServerRealm_get_auditOn + * Description: Get audit functionality on/off. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Audit functionality on/off. + */ + +char +ServerRealm_get_auditOn(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->auditOn; +} + +/* + * Function name: ServerRealm_get_addressLength + * Description: Get client's address length. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Client's address length. + */ + +socklen_t +ServerRealm_get_addressLength(ServerRealm* sr) +{ + if (sr == NULL) { + return 0; + } + return sr->addressLength; +} + +/* + * Function name: ServerRealm_get_clientAddress + * Description: Get client's network address. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Client's network address. + */ + +struct sockaddr* +ServerRealm_get_clientAddress(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->clientAddress; +} + +/* + * Function name: ServerRealm_get_usersTable + * Description: Get table of users. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Table of users. + */ + +ConnectUser** +ServerRealm_get_usersTable(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->usersTable; +} + +/* + * Function name: ServerRealm_get_clientsTable + * Description: Get table of clients. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Table of clients. + */ + +ConnectClient** +ServerRealm_get_clientsTable(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->clientsTable; +} + +/* + * Function name: ServerRealm_get_raClientsTable + * Description: Get table of raClients. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Table of raClients. + */ + +ConnectClient** +ServerRealm_get_raClientsTable(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->raClientsTable; +} + +/* + * Function name: ServerRealm_get_usersClientsTable + * Description: Get table of user-client pairs. + * Arguments: sr - pointer to ServerRealm structure + * Returns: Table of user-client pairs. + */ + +UsrCli** +ServerRealm_get_usersClientsTable(ServerRealm* sr) +{ + if (sr == NULL) { + return NULL; + } + return sr->usersClientsTable; +} + +/* + * Function name: ServerRealm_increase_connectedUsers + * Description: Increase number of connected users. + * Arguments: sr - pointer to ServerRealm structure + */ + +void +ServerRealm_increase_connectedUsers(ServerRealm* sr) +{ + if (sr == NULL) { + return; + } + ServerRealm_set_connectedUsers(sr, ServerRealm_get_connectedUsers(sr) + 1); +} + +/* + * Function name: ServerRealm_decrease_connectedUsers + * Description: Decrease number of connected users. + * Arguments: sr - pointer to ServerRealm structure + */ + +void +ServerRealm_decrease_connectedUsers(ServerRealm* sr) +{ + if (sr == NULL) { + return; + } + ServerRealm_set_connectedUsers(sr, ServerRealm_get_connectedUsers(sr) - 1); +} + +/* + * Function name: ServerRealm_increase_connectedClients + * Description: Increase number of connected clients. + * Arguments: sr - pointer to ServerRealm structure + */ + +void +ServerRealm_increase_connectedClients(ServerRealm* sr) +{ + if (sr == NULL) { + return; + } + ServerRealm_set_connectedClients(sr, ServerRealm_get_connectedClients(sr) + 1); +} + +/* + * Function name: ServerRealm_decrease_connectedClients + * Description: Decrease number of connected clients. + * Arguments: sr - pointer to ServerRealm structure + */ + +void +ServerRealm_decrease_connectedClients(ServerRealm* sr) +{ + if (sr == NULL) { + return; + } + ServerRealm_set_connectedClients(sr, ServerRealm_get_connectedClients(sr) - 1); +} + +/* + * Function name: ServerRealm_increase_connectedRaClients + * Description: Increase number of connected raClients. + * Arguments: sr - pointer to ServerRealm structure + */ + +void +ServerRealm_increase_connectedRaClients(ServerRealm* sr) +{ + if (sr == NULL) { + return; + } + ServerRealm_set_connectedRaClients(sr, ServerRealm_get_connectedRaClients(sr) + 1); +} + +/* + * Function name: ServerRealm_decrease_connectedRaClients + * Description: Decrease number of connected raClients. + * Arguments: sr - pointer to ServerRealm structure + */ + +void +ServerRealm_decrease_connectedRaClients(ServerRealm* sr) +{ + if (sr == NULL) { + return; + } + ServerRealm_set_connectedRaClients(sr, ServerRealm_get_connectedRaClients(sr) - 1); +} + +/* + * Function name: ServerRealm_increase_usersCounter + * Description: Increase users counter state. + * Arguments: sr - pointer to ServerRealm structure + */ + +void +ServerRealm_increase_usersCounter(ServerRealm* sr) +{ + if (sr == NULL) { + return; + } + ServerRealm_set_usersCounter(sr, ServerRealm_get_usersCounter(sr) + 1); +} + +/* + * Function name: ServerRealm_increase_clientsCounter + * Description: Increase clients counter state. + * Arguments: sr - pointer to ServerRealm structure + */ + +void +ServerRealm_increase_clientsCounter(ServerRealm* sr) +{ + if (sr == NULL) { + return; + } + ServerRealm_set_clientsCounter(sr, ServerRealm_get_clientsCounter(sr) + 1); +} diff --git a/src/server_realm_struct.h b/src/server_realm_struct.h new file mode 100644 index 0000000..67458a7 --- /dev/null +++ b/src/server_realm_struct.h @@ -0,0 +1,141 @@ +/* + * active port forwarder - software for secure forwarding + * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include "activefor.h" + +#ifndef _JS_SERVER_REALM_STRUCT_H +#define _JS_SERVER_REALM_STRUCT_H + +typedef struct { + char* hostName; + char* sUsersLimit; + char* sClientsLimit; + char* sRaClientsLimit; + char* sUsersPerClient; + char* sClientMode; + char* sTimeout; + char* realmName; + unsigned char password[4]; + int connectedUsers; + int usersLimit; + int connectedClients; + int clientsLimit; + int connectedRaClients; + int raClientsLimit; + int usersPerClient; + int timeout; + int clientMode; + int userClientPairs; + int clientsCounter; + int usersCounter; + char realmType; + char tunnelType; + char dnsLookupsOn; + char basePortOn; + char auditOn; + socklen_t addressLength; + struct sockaddr* clientAddress; + ConnectUser** usersTable; + ConnectClient** clientsTable; + ConnectClient** raClientsTable; + UsrCli** usersClientsTable; +} ServerRealm; + +/* 'constructor' */ +ServerRealm* ServerRealm_new(); +/* 'destructor' */ +void ServerRealm_free(ServerRealm** sr); +/* setters */ +void ServerRealm_set_hostName(ServerRealm* sr, char* hostName); +void ServerRealm_set_sUsersLimit(ServerRealm* sr, char* sUsersLimit); +void ServerRealm_set_sClientsLimit(ServerRealm* sr, char* sClientsLimit); +void ServerRealm_set_sRaClientsLimit(ServerRealm* sr, char* sRaClientsLimit); +void ServerRealm_set_sUsersPerClient(ServerRealm* sr, char* sUsersPerClient); +void ServerRealm_set_sClientMode(ServerRealm* sr, char* sClientMode); +void ServerRealm_set_sTimeout(ServerRealm* sr, char* sTimeout); +void ServerRealm_set_realmName(ServerRealm* sr, char* realmName); +void ServerRealm_set_password(ServerRealm* sr, unsigned char* password); +void ServerRealm_set_connectedUsers(ServerRealm* sr, int connectedUsers); +void ServerRealm_set_usersLimit(ServerRealm* sr, int usersLimit); +void ServerRealm_set_connectedClients(ServerRealm* sr, int connectedClients); +void ServerRealm_set_clientsLimit(ServerRealm* sr, int clientsLimit); +void ServerRealm_set_connectedRaClients(ServerRealm* sr, int connectedRaClients); +void ServerRealm_set_raClientsLimit(ServerRealm* sr, int raClientsLimit); +void ServerRealm_set_usersPerClient(ServerRealm* sr, int usersPerClient); +void ServerRealm_set_timeout(ServerRealm* sr, int timeout); +void ServerRealm_set_clientMode(ServerRealm* sr, int clientMode); +void ServerRealm_set_userClientPairs(ServerRealm* sr, int userClientPairs); +void ServerRealm_set_clientsCounter(ServerRealm* sr, int clientsCounter); +void ServerRealm_set_usersCounter(ServerRealm* sr, int usersCounter); +void ServerRealm_set_realmType(ServerRealm* sr, char realmType); +void ServerRealm_set_tunnelType(ServerRealm* sr, char tunnelType); +void ServerRealm_set_dnsLookupsOn(ServerRealm* sr, char dnsLookupsOn); +void ServerRealm_set_basePortOn(ServerRealm* sr, char basePortOn); +void ServerRealm_set_auditOn(ServerRealm* sr, char auditOn); +void ServerRealm_set_addressLength(ServerRealm* sr, socklen_t addressLength); +void ServerRealm_set_clientAddress(ServerRealm* sr, struct sockaddr* clientAddress); +void ServerRealm_set_usersTable(ServerRealm* sr, ConnectUser** usersTable); +void ServerRealm_set_clientsTable(ServerRealm* sr, ConnectClient** clientsTable); +void ServerRealm_set_raClientsTable(ServerRealm* sr, ConnectClient** raClientsTable); +void ServerRealm_set_usersClientsTable(ServerRealm* sr, UsrCli** usersClientsTable); +/* getters */ +char* ServerRealm_get_hostName(ServerRealm* sr); +char* ServerRealm_get_sUsersLimit(ServerRealm* sr); +char* ServerRealm_get_sClientsLimit(ServerRealm* sr); +char* ServerRealm_get_sRaClientsLimit(ServerRealm* sr); +char* ServerRealm_get_sUsersPerClient(ServerRealm* sr); +char* ServerRealm_get_sClientMode(ServerRealm* sr); +char* ServerRealm_get_sTimeout(ServerRealm* sr); +char* ServerRealm_get_realmName(ServerRealm* sr); +unsigned char* ServerRealm_get_password(ServerRealm* sr); +int ServerRealm_get_connectedUsers(ServerRealm* sr); +int ServerRealm_get_usersLimit(ServerRealm* sr); +int ServerRealm_get_connectedClients(ServerRealm* sr); +int ServerRealm_get_clientsLimit(ServerRealm* sr); +int ServerRealm_get_connectedRaClients(ServerRealm* sr); +int ServerRealm_get_raClientsLimit(ServerRealm* sr); +int ServerRealm_get_usersPerClient(ServerRealm* sr); +int ServerRealm_get_timeout(ServerRealm* sr); +int ServerRealm_get_clientMode(ServerRealm* sr); +int ServerRealm_get_userClientPairs(ServerRealm* sr); +int ServerRealm_get_clientsCounter(ServerRealm* sr); +int ServerRealm_get_usersCounter(ServerRealm* sr); +char ServerRealm_get_realmType(ServerRealm* sr); +char ServerRealm_get_tunnelType(ServerRealm* sr); +char ServerRealm_get_dnsLookupsOn(ServerRealm* sr); +char ServerRealm_get_basePortOn(ServerRealm* sr); +char ServerRealm_get_auditOn(ServerRealm* sr); +socklen_t ServerRealm_get_addressLength(ServerRealm* sr); +struct sockaddr* ServerRealm_get_clientAddress(ServerRealm* sr); +ConnectUser** ServerRealm_get_usersTable(ServerRealm* sr); +ConnectClient** ServerRealm_get_clientsTable(ServerRealm* sr); +ConnectClient** ServerRealm_get_raClientsTable(ServerRealm* sr); +UsrCli** ServerRealm_get_usersClientsTable(ServerRealm* sr); +/* other */ +void ServerRealm_increase_connectedUsers(ServerRealm* sr); +void ServerRealm_decrease_connectedUsers(ServerRealm* sr); +void ServerRealm_increase_connectedClients(ServerRealm* sr); +void ServerRealm_decrease_connectedClients(ServerRealm* sr); +void ServerRealm_increase_connectedRaClients(ServerRealm* sr); +void ServerRealm_decrease_connectedRaClients(ServerRealm* sr); +void ServerRealm_increase_usersCounter(ServerRealm* sr); +void ServerRealm_increase_clientsCounter(ServerRealm* sr); + +#endif diff --git a/src/server_remoteadmin.c b/src/server_remoteadmin.c index 79d174b..adc8587 100644 --- a/src/server_remoteadmin.c +++ b/src/server_remoteadmin.c @@ -185,15 +185,18 @@ add_uptime_to_message(unsigned char* buff, char* info, time_t period) } int -serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) +serve_admin(ServerConfiguration* config, int realm, int client, unsigned char* buff) { int length, n, i, j, ret; time_t now, tmp; llnodeT* llptr; AuditListNode* alptr; char olddf[51], newdf[51]; - char type = config->realmtable[realm].type | TYPE_SSL | TYPE_ZLIB; - SslFd* master = ConnectClient_get_sslFd(config->realmtable[realm].raclitable[client]); + ConnectClient* cpointer; + ConnectUser* upointer; + ServerRealm* pointer = ServerConfiguration_get_realmsTable(config)[realm]; + char type = ServerRealm_get_realmType(pointer) | TYPE_SSL | TYPE_ZLIB; + SslFd* master = ConnectClient_get_sslFd(ServerRealm_get_raClientsTable(pointer)[client]); olddf[50] = newdf[50] = 0; length = buff[3]; @@ -248,9 +251,12 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) } case 3: { /* info */ add_to_message(buff, AF_VER("Version:")); - add_to_message(buff, "Realms: %d", config->size); - add_to_message(buff, "Certificate: %s", config->certif); - add_to_message(buff, "Key: %s", config->keys); + add_to_message(buff, "Realms: %d", + ServerConfiguration_get_realmsNumber(config)); + add_to_message(buff, "Certificate: %s", + ServerConfiguration_get_certificateFile(config)); + add_to_message(buff, "Key: %s", + ServerConfiguration_get_keysFile(config)); llptr = getloglisthead(); i = 0; while (llptr) { @@ -258,7 +264,7 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) llptr = llptr->next; ++i; } - tmp = now - config->starttime; + tmp = now - ServerConfiguration_get_startTime(config); add_uptime_to_message(buff, "Uptime", tmp); add_to_message(buff, "Cg: %ld B", getcg()); add_to_message(buff, "Dateformat: %s", getdateformat()); @@ -266,37 +272,44 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) break; } case 4: { /* rshow */ - for (i = 0; i < config->size; ++i) { + for (i = 0; i < ServerConfiguration_get_realmsNumber(config); ++i) { + pointer = ServerConfiguration_get_realmsTable(config)[i]; add_to_message(buff, "\nRealm[%s]:", get_realmname(config, i)); - add_to_message(buff, "hostname: %s", config->realmtable[i].hostname); + add_to_message(buff, "hostname: %s", ServerRealm_get_hostName(pointer)); add_to_message(buff, "users: %d (max: %d)", - config->realmtable[i].usercon, config->realmtable[i].usernum); + ServerRealm_get_connectedUsers(pointer), + ServerRealm_get_usersLimit(pointer)); add_to_message(buff, "clients: %d (max: %d)", - config->realmtable[i].clicon-config->realmtable[i].raclicon, - config->realmtable[i].clinum); + ServerRealm_get_connectedClients(pointer) - + ServerRealm_get_connectedRaClients(pointer), + ServerRealm_get_clientsLimit(pointer)); add_to_message(buff, "raclients: %d (max: %d)", - config->realmtable[i].raclicon, config->realmtable[i].raclinum); - add_to_message(buff, "users per client: %s", config->realmtable[i].usrpcli); + ServerRealm_get_connectedRaClients(pointer), + ServerRealm_get_raClientsLimit(pointer)); + add_to_message(buff, "users per client: %s", + ServerRealm_get_sUsersPerClient(pointer)); add_to_message(buff, "user-client pairs: %d", - config->realmtable[i].usrclinum); - for (j = 0; j < config->realmtable[i].usrclinum; ++j) { + ServerRealm_get_userClientPairs(pointer)); + for (j = 0; j < ServerRealm_get_userClientPairs(pointer); ++j) { add_to_message(buff, " pair[%d]: listenport: %s, manageport: %s", j, - UsrCli_get_listenPortName(config->realmtable[i].usrclitable[j]), - UsrCli_get_managePortName(config->realmtable[i].usrclitable[j])); + UsrCli_get_listenPortName( + ServerRealm_get_usersClientsTable(pointer)[j]), + UsrCli_get_managePortName( + ServerRealm_get_usersClientsTable(pointer)[j])); } - add_to_message(buff, "climode: %s", config->realmtable[i].clim); - add_to_message(buff, "timeout: %d", config->realmtable[i].tmout); - add_to_message(buff, "baseport: %s", config->realmtable[i].baseport ? + add_to_message(buff, "climode: %s", ServerRealm_get_sClientMode(pointer)); + add_to_message(buff, "timeout: %d", ServerRealm_get_timeout(pointer)); + add_to_message(buff, "baseport: %s", ServerRealm_get_basePortOn(pointer) ? "yes" : "no"); - add_to_message(buff, "audit: %s", config->realmtable[i].audit ? - "yes" : "no"); - add_to_message(buff, "dnslookups: %s", config->realmtable[i].dnslookups ? + add_to_message(buff, "audit: %s", ServerRealm_get_auditOn(pointer) ? "yes" : "no"); + add_to_message(buff, "dnslookups: %s", + ServerRealm_get_dnsLookupsOn(pointer) ? "yes" : "no"); add_to_message(buff, "ssl: %s, zlib: %s, mode: %s", - (TYPE_IS_SSL(config->realmtable[i].type))?"yes":"no", - (TYPE_IS_ZLIB(config->realmtable[i].type))?"yes":"no", - (TYPE_IS_TCP(config->realmtable[i].type))?"tcp":"udp"); - switch (config->realmtable[i].tunneltype) { + (TYPE_IS_SSL(ServerRealm_get_realmType(pointer))) ? "yes" : "no", + (TYPE_IS_ZLIB(ServerRealm_get_realmType(pointer))) ? "yes" : "no", + (TYPE_IS_TCP(ServerRealm_get_realmType(pointer))) ? "tcp" : "udp"); + switch (ServerRealm_get_tunnelType(pointer)) { case CONNECTCLIENT_TUNNELTYPE_DIRECT: { add_to_message(buff, "tunneltype: direct"); break; @@ -319,13 +332,14 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) } case 5: { /* cshow*/ n = get_realmnumber(config, (char*) &buff[ret]); - if ((n >= 0) && (n < config->size)) { - for (i = 0; i < config->realmtable[n].clinum; ++i) { - if (ConnectClient_get_state(config->realmtable[n].clitable[i]) != - CONNECTCLIENT_STATE_FREE) { + if ((n >= 0) && (n < ServerConfiguration_get_realmsNumber(config))) { + pointer = ServerConfiguration_get_realmsTable(config)[n]; + for (i = 0; i < ServerRealm_get_clientsLimit(pointer); ++i) { + cpointer = ServerRealm_get_clientsTable(pointer)[i]; + if (ConnectClient_get_state(cpointer) != CONNECTCLIENT_STATE_FREE) { add_to_message(buff, "\nClient[%s]:", - get_clientname(&(config->realmtable[n]), i)); - switch (ConnectClient_get_state(config->realmtable[n].clitable[i])) { + get_clientname(pointer, i)); + switch (ConnectClient_get_state(cpointer)) { case CONNECTCLIENT_STATE_CONNECTING: { add_to_message(buff, "state: ssl handshake"); break; @@ -343,24 +357,21 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) } } add_to_message(buff, "users: %d (max: %d)", - ConnectClient_get_connected(config->realmtable[n].clitable[i]), - ConnectClient_get_limit(config->realmtable[n].clitable[i])); + ConnectClient_get_connected(cpointer), + ConnectClient_get_limit(cpointer)); add_to_message(buff, "user-client pair: %d", - ConnectClient_get_usrCliPair(config->realmtable[n].clitable[i])); - tmp = now - ConnectClient_get_connectTime( - config->realmtable[n].clitable[i]); + ConnectClient_get_usrCliPair(cpointer)); + tmp = now - ConnectClient_get_connectTime(cpointer); add_uptime_to_message(buff, "Connection time", tmp); add_to_message(buff, "Id: %s", - (ConnectClient_get_sClientId( - config->realmtable[n].clitable[i]) == NULL)?"": - ConnectClient_get_sClientId(config->realmtable[n].clitable[i])); + (ConnectClient_get_sClientId(cpointer) == NULL) ? "" : + ConnectClient_get_sClientId(cpointer)); add_to_message(buff, "Number: %d", - ConnectClient_get_clientId(config->realmtable[n].clitable[i])); + ConnectClient_get_clientId(cpointer)); add_to_message(buff, "IP: %s, port: %s", - ConnectClient_get_nameBuf(config->realmtable[n].clitable[i]), - ConnectClient_get_portBuf(config->realmtable[n].clitable[i])); - switch (ConnectClient_get_tunnelType( - config->realmtable[n].clitable[i])) { + ConnectClient_get_nameBuf(cpointer), + ConnectClient_get_portBuf(cpointer)); + switch (ConnectClient_get_tunnelType(cpointer)) { case CONNECTCLIENT_TUNNELTYPE_DIRECT: { add_to_message(buff, "tunneltype: direct"); break; @@ -377,10 +388,10 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) add_to_message(buff, "tunneltype: UNKNOWN"); } } - if (config->realmtable[n].audit) { + if (ServerRealm_get_auditOn(pointer)) { add_to_message(buff, "auditlog:"); alptr = AuditList_get_first( - ConnectClient_get_auditList(config->realmtable[n].clitable[i])); + ConnectClient_get_auditList(cpointer)); while (alptr) { add_to_message(buff, "userid: %d ip: %s port: %s connected: %s duration: %s", @@ -404,13 +415,14 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) } case 6: { /* ushow */ n = get_realmnumber(config, (char*) &buff[ret]); - if ((n >= 0) && (n < config->size)) { - for (i = 0; i < config->realmtable[n].usernum; ++i) { - if (ConnectUser_get_state(config->realmtable[n].contable[i]) != - S_STATE_CLEAR) { + if ((n >= 0) && (n < ServerConfiguration_get_realmsNumber(config))) { + pointer = ServerConfiguration_get_realmsTable(config)[n]; + for (i = 0; i < ServerRealm_get_usersLimit(pointer); ++i) { + upointer = ServerRealm_get_usersTable(pointer)[i]; + if (ConnectUser_get_state(upointer) != S_STATE_CLEAR) { add_to_message(buff, "\nUser[%d]:", - get_username(&(config->realmtable[n]), i)); - switch (ConnectUser_get_state(config->realmtable[n].contable[i])) { + get_username(pointer, i)); + switch (ConnectUser_get_state(upointer)) { case S_STATE_CLOSING: { add_to_message(buff, "state: closing"); break; @@ -419,6 +431,10 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) add_to_message(buff, "state: opening"); break; } + case S_STATE_OPENING_CLOSED: { + add_to_message(buff, "state: opening (closed)"); + break; + } case S_STATE_OPEN: { add_to_message(buff, "state: running"); break; @@ -432,28 +448,27 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) } } add_to_message(buff, "connected to: Client[%s]", - get_clientname(&(config->realmtable[n]), - ConnectUser_get_whatClient(config->realmtable[n].contable[i]))); - tmp = now-ConnectUser_get_connectTime(config->realmtable[n].contable[i]); + get_clientname(pointer, ConnectUser_get_whatClient(upointer))); + tmp = now-ConnectUser_get_connectTime(upointer); add_uptime_to_message(buff, "Connection time", tmp); tmp = now - UserStats_get_lastActivity( - ConnectUser_get_stats(config->realmtable[n].contable[i])); + ConnectUser_get_stats(upointer)); add_uptime_to_message(buff, "Idle time", tmp); add_to_message(buff, "IP: %s, port: %s", - ConnectUser_get_nameBuf(config->realmtable[n].contable[i]), - ConnectUser_get_portBuf(config->realmtable[n].contable[i])); + ConnectUser_get_nameBuf(upointer), + ConnectUser_get_portBuf(upointer)); add_to_message(buff, "Downloaded: %d bytes", UserStats_get_totalDownloadedBytes( - ConnectUser_get_stats(config->realmtable[n].contable[i]))); + ConnectUser_get_stats(upointer))); add_to_message(buff, "download speed: %.2f B/s", UserStats_get_downloadSpeed( - ConnectUser_get_stats(config->realmtable[n].contable[i]))); + ConnectUser_get_stats(upointer))); add_to_message(buff, "Uploaded: %d bytes", UserStats_get_totalUploadedBytes( - ConnectUser_get_stats(config->realmtable[n].contable[i]))); + ConnectUser_get_stats(upointer))); add_to_message(buff, "upload speed: %.2f B/s", UserStats_get_uploadSpeed( - ConnectUser_get_stats(config->realmtable[n].contable[i]))); + ConnectUser_get_stats(upointer))); } } send_adm_message(type, master, buff, AF_RA_STATUS_OK); @@ -468,7 +483,7 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) aflog(LOG_T_MANAGE, LOG_I_INFO, "realm[%s]: Client[%s] (ra): commfd: CLOSED", get_realmname(config, realm), - get_raclientname(&(config->realmtable[realm]), client)); + get_raclientname(pointer, client)); send_adm_message(type, master, buff, AF_RA_KICKED); return 1; } @@ -482,10 +497,11 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) break; } n = get_realmnumber(config, (char*) &buff[ret]); - if ((n >= 0) && (n < config->size)) { + if ((n >= 0) && (n < ServerConfiguration_get_realmsNumber(config))) { add_to_message(buff, "changed timeout: %d --> %d", - config->realmtable[n].tmout, i); - config->realmtable[n].tmout = i; + ServerRealm_get_timeout( + ServerConfiguration_get_realmsTable(config)[n]), i); + ServerRealm_set_timeout(ServerConfiguration_get_realmsTable(config)[n], i); send_adm_message(type, master, buff, AF_RA_STATUS_OK); break; } @@ -505,15 +521,19 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) break; } n = get_realmnumber(config, (char*) &buff[ret]); - if ((n >= 0) && (n < config->size)) { + if ((n >= 0) && (n < ServerConfiguration_get_realmsNumber(config))) { add_to_message(buff, "changed audit: %s --> %s", - config->realmtable[n].audit ? "yes" : "no", i ? "yes" : "no"); - config->realmtable[n].audit = i; + ServerRealm_get_auditOn( + ServerConfiguration_get_realmsTable(config)[n]) ? "yes" : "no", + i ? "yes" : "no"); + ServerRealm_set_auditOn(ServerConfiguration_get_realmsTable(config)[n], i); if (i == 0) { - for (i = 0; i < config->realmtable[n].clinum; ++i) { + for (i = 0; i < ServerRealm_get_clientsLimit( + ServerConfiguration_get_realmsTable(config)[n]); ++i) { AuditList_clear( ConnectClient_get_auditList( - config->realmtable[n].clitable[i])); + ServerRealm_get_clientsTable( + ServerConfiguration_get_realmsTable(config)[n])[i])); } } send_adm_message(type, master, buff, AF_RA_STATUS_OK); @@ -535,10 +555,13 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) break; } n = get_realmnumber(config, (char*) &buff[ret]); - if ((n >= 0) && (n < config->size)) { + if ((n >= 0) && (n < ServerConfiguration_get_realmsNumber(config))) { add_to_message(buff, "changed dnslookups: %s --> %s", - config->realmtable[n].dnslookups ? "yes" : "no", i ? "yes" : "no"); - config->realmtable[n].dnslookups = i; + ServerRealm_get_dnsLookupsOn( + ServerConfiguration_get_realmsTable(config)[n]) ? "yes" : "no", + i ? "yes" : "no"); + ServerRealm_set_dnsLookupsOn( + ServerConfiguration_get_realmsTable(config)[n], i); send_adm_message(type, master, buff, AF_RA_STATUS_OK); break; } @@ -567,16 +590,22 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) break; } j = -1; - for (n = 0; n < config->size; ++n) { - j = get_usernumber(&(config->realmtable[n]), i); + for (n = 0; n < ServerConfiguration_get_realmsNumber(config); ++n) { + pointer = ServerConfiguration_get_realmsTable(config)[n]; + j = get_usernumber(pointer, i); if (j != (-1)) { - if ((ConnectUser_get_state(config->realmtable[n].contable[j]) == - S_STATE_OPEN) || - (ConnectUser_get_state(config->realmtable[n].contable[j]) == - S_STATE_STOPPED)) { + upointer = ServerRealm_get_usersTable(pointer)[j]; + if ((ConnectUser_get_state(upointer) == S_STATE_OPEN) || + (ConnectUser_get_state(upointer) == S_STATE_OPENING) || + (ConnectUser_get_state(upointer) == S_STATE_STOPPED)) { add_to_message(buff, "kicked: realm[%s] user[%d]", - get_realmname(config, n), get_username(&(config->realmtable[n]), i)); - close(ConnectUser_get_connFd(config->realmtable[n].contable[j])); + get_realmname(config, n), get_username(pointer, j)); + if (ConnectUser_get_state(upointer) == S_STATE_OPENING) { + ConnectUser_set_state(upointer, S_STATE_OPENING_CLOSED); + } + else { + close(ConnectUser_get_connFd(upointer)); + } send_adm_message(type, master, buff, AF_RA_STATUS_OK); } else { @@ -606,14 +635,15 @@ serve_admin(ConfigurationT* config, int realm, int client, unsigned char* buff) break; } j = -1; - for (n = 0; n < config->size; ++n) { - j = get_clientnumber(&(config->realmtable[n]), i); + for (n = 0; n < ServerConfiguration_get_realmsNumber(config); ++n) { + pointer = ServerConfiguration_get_realmsTable(config)[n]; + j = get_clientnumber(pointer, i); if (j != (-1)) { - if (ConnectClient_get_state(config->realmtable[n].clitable[j]) > + if (ConnectClient_get_state(ServerRealm_get_clientsTable(pointer)[j]) > CONNECTCLIENT_STATE_FREE) { add_to_message(buff, "kicked: realm[%s] client[%s]", get_realmname(config, n), - get_clientname(&(config->realmtable[n]), j)); + get_clientname(pointer, j)); send_adm_message(type, master, buff, AF_RA_STATUS_OK); return (i+2); } diff --git a/src/server_remoteadmin.h b/src/server_remoteadmin.h index a85070e..f50d20c 100644 --- a/src/server_remoteadmin.h +++ b/src/server_remoteadmin.h @@ -32,6 +32,6 @@ #include "realmnames.h" #include "usernames.h" -int serve_admin(ConfigurationT*, int, int, unsigned char*); +int serve_admin(ServerConfiguration*, int, int, unsigned char*); #endif diff --git a/src/server_remove.c b/src/server_remove.c index 003c484..21941cd 100644 --- a/src/server_remove.c +++ b/src/server_remove.c @@ -21,59 +21,59 @@ #include "server_remove.h" void -remove_client(RealmT* ptr, int client, fd_set* set, fd_set* wset, int* con) +remove_client(ServerRealm* ptr, int client, fd_set* set, fd_set* wset, int* con) { int i; - if (ConnectClient_get_state(ptr->clitable[client]) == CONNECTCLIENT_STATE_ACCEPTED) { - for (i = 0; i < ptr->usernum; ++i) { - if (ConnectUser_get_whatClient(ptr->contable[i]) == client) { - if (ConnectUser_get_state(ptr->contable[i]) != S_STATE_CLEAR) { - ConnectUser_set_state(ptr->contable[i], S_STATE_CLEAR); - FD_CLR(ConnectUser_get_connFd(ptr->contable[i]), set); - FD_CLR(ConnectUser_get_connFd(ptr->contable[i]), wset); - close(ConnectUser_get_connFd(ptr->contable[i])); - ptr->usercon--; + if (ConnectClient_get_state(ServerRealm_get_clientsTable(ptr)[client]) == CONNECTCLIENT_STATE_ACCEPTED) { + for (i = 0; i < ServerRealm_get_usersLimit(ptr); ++i) { + if (ConnectUser_get_whatClient(ServerRealm_get_usersTable(ptr)[i]) == client) { + if (ConnectUser_get_state(ServerRealm_get_usersTable(ptr)[i]) != S_STATE_CLEAR) { + ConnectUser_set_state(ServerRealm_get_usersTable(ptr)[i], S_STATE_CLEAR); + FD_CLR(ConnectUser_get_connFd(ServerRealm_get_usersTable(ptr)[i]), set); + FD_CLR(ConnectUser_get_connFd(ServerRealm_get_usersTable(ptr)[i]), wset); + close(ConnectUser_get_connFd(ServerRealm_get_usersTable(ptr)[i])); + ServerRealm_decrease_connectedUsers(ptr); } } } } - for (i = 0; i < ConnectClient_get_limit(ptr->clitable[client]); ++i) { - ConnectClient_get_users(ptr->clitable[client])[i] = -1; + for (i = 0; i < ConnectClient_get_limit(ServerRealm_get_clientsTable(ptr)[client]); ++i) { + ConnectClient_get_users(ServerRealm_get_clientsTable(ptr)[client])[i] = -1; } - if ((ptr->clinum != client) && (ptr->baseport == 1)) { - close(ConnectClient_get_listenFd(ptr->clitable[client])); - FD_CLR(ConnectClient_get_listenFd(ptr->clitable[client]), set); + if ((ServerRealm_get_clientsLimit(ptr) != client) && (ServerRealm_get_basePortOn(ptr) == 1)) { + close(ConnectClient_get_listenFd(ServerRealm_get_clientsTable(ptr)[client])); + FD_CLR(ConnectClient_get_listenFd(ServerRealm_get_clientsTable(ptr)[client]), set); } - ConnectClient_set_sClientId(ptr->clitable[client], NULL); - ConnectClient_set_connected(ptr->clitable[client], 0); - close(SslFd_get_fd(ConnectClient_get_sslFd(ptr->clitable[client]))); - FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(ptr->clitable[client])), set); - if (ConnectClient_get_state(ptr->clitable[client]) == CONNECTCLIENT_STATE_AUTHORIZING) { + ConnectClient_set_sClientId(ServerRealm_get_clientsTable(ptr)[client], NULL); + ConnectClient_set_connected(ServerRealm_get_clientsTable(ptr)[client], 0); + close(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_clientsTable(ptr)[client]))); + FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_clientsTable(ptr)[client])), set); + if (ConnectClient_get_state(ServerRealm_get_clientsTable(ptr)[client]) == CONNECTCLIENT_STATE_AUTHORIZING) { (*con)--; } - SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(ptr->clitable[client]))); - ConnectClient_set_state(ptr->clitable[client], CONNECTCLIENT_STATE_FREE); - ptr->clicon--; + SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(ServerRealm_get_clientsTable(ptr)[client]))); + ConnectClient_set_state(ServerRealm_get_clientsTable(ptr)[client], CONNECTCLIENT_STATE_FREE); + ServerRealm_decrease_connectedClients(ptr); } void -remove_raclient(RealmT* ptr, int client, fd_set* set, fd_set* wset, int* con) +remove_raclient(ServerRealm* ptr, int client, fd_set* set, fd_set* wset, int* con) { int i; - for (i = 0; i < ConnectClient_get_limit(ptr->raclitable[client]); ++i) { - ConnectClient_get_users(ptr->raclitable[client])[i] = -1; + for (i = 0; i < ConnectClient_get_limit(ServerRealm_get_raClientsTable(ptr)[client]); ++i) { + ConnectClient_get_users(ServerRealm_get_raClientsTable(ptr)[client])[i] = -1; } - ConnectClient_set_sClientId(ptr->raclitable[client], NULL); - ConnectClient_set_connected(ptr->raclitable[client], 0); - close(SslFd_get_fd(ConnectClient_get_sslFd(ptr->raclitable[client]))); - FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(ptr->raclitable[client])), set); - if (ConnectClient_get_state(ptr->raclitable[client]) == CONNECTCLIENT_STATE_AUTHORIZING) { + ConnectClient_set_sClientId(ServerRealm_get_raClientsTable(ptr)[client], NULL); + ConnectClient_set_connected(ServerRealm_get_raClientsTable(ptr)[client], 0); + close(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_raClientsTable(ptr)[client]))); + FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_raClientsTable(ptr)[client])), set); + if (ConnectClient_get_state(ServerRealm_get_raClientsTable(ptr)[client]) == CONNECTCLIENT_STATE_AUTHORIZING) { (*con)--; } - SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(ptr->raclitable[client]))); - ptr->clicon--; - if (ConnectClient_get_state(ptr->raclitable[client]) == CONNECTCLIENT_STATE_ACCEPTED) { - ptr->raclicon--; + SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(ServerRealm_get_raClientsTable(ptr)[client]))); + ServerRealm_decrease_connectedClients(ptr); + if (ConnectClient_get_state(ServerRealm_get_raClientsTable(ptr)[client]) == CONNECTCLIENT_STATE_ACCEPTED) { + ServerRealm_decrease_connectedRaClients(ptr); } - ConnectClient_set_state(ptr->raclitable[client], CONNECTCLIENT_STATE_FREE); + ConnectClient_set_state(ServerRealm_get_raClientsTable(ptr)[client], CONNECTCLIENT_STATE_FREE); } diff --git a/src/server_remove.h b/src/server_remove.h index 363b1cc..3a2f804 100644 --- a/src/server_remove.h +++ b/src/server_remove.h @@ -23,8 +23,8 @@ #ifndef _JS_SERVER_REMOVE_H #define _JS_SERVER_REMOVE_H -void remove_client(RealmT*, int, fd_set*, fd_set*, int*); -void remove_raclient(RealmT*, int, fd_set*, fd_set*, int*); +void remove_client(ServerRealm*, int, fd_set*, fd_set*, int*); +void remove_raclient(ServerRealm*, int, fd_set*, fd_set*, int*); #endif diff --git a/src/server_signals.c b/src/server_signals.c index ae5388d..55ceb4a 100644 --- a/src/server_signals.c +++ b/src/server_signals.c @@ -26,14 +26,16 @@ #include "http_proxy_functions.h" #include "stats.h" #include "logging.h" +#include "server_configuration_struct.h" -extern ConfigurationT config; +extern ServerConfiguration* config; void server_sig_int(int signo) { int i, j; unsigned char buff[5]; + ServerRealm** scRealmsTable; #ifdef HAVE_LIBPTHREAD if (!is_this_a_mainthread()) { @@ -41,24 +43,28 @@ server_sig_int(int signo) } #endif - for (j = 0; j < config.size; ++j) { + for (j = 0; j < ServerConfiguration_get_realmsNumber(config); ++j) { + scRealmsTable = ServerConfiguration_get_realmsTable(config); buff[0] = AF_S_CLOSING; /* closing */ - for (i = 0; i < config.realmtable[j].clinum; ++i) { - if (ConnectClient_get_state(config.realmtable[j].clitable[i]) == + for (i = 0; i < ServerRealm_get_clientsLimit(scRealmsTable[j]); ++i) { + if (ConnectClient_get_state(ServerRealm_get_clientsTable(scRealmsTable[j])[i]) == CONNECTCLIENT_STATE_ACCEPTED) { - SslFd_send_message(config.realmtable[j].type, + SslFd_send_message(ServerRealm_get_realmType(scRealmsTable[j]), ConnectClient_get_sslFd( - config.realmtable[j].clitable[i]), + ServerRealm_get_clientsTable(scRealmsTable[j])[i]), buff, 5); + close(SslFd_get_fd(ConnectClient_get_sslFd( + ServerRealm_get_clientsTable(scRealmsTable[j])[i]))); } } - for (i = 0; i < config.realmtable[j].raclinum; ++i) { - if (ConnectClient_get_state(config.realmtable[j].raclitable[i]) == + for (i = 0; i < ServerRealm_get_raClientsLimit(scRealmsTable[j]); ++i) { + if (ConnectClient_get_state(ServerRealm_get_raClientsTable(scRealmsTable[j])[i]) == CONNECTCLIENT_STATE_ACCEPTED) { - SslFd_send_message(config.realmtable[j].type | TYPE_SSL, + SslFd_send_message(ServerRealm_get_realmType(scRealmsTable[j]) | TYPE_SSL, ConnectClient_get_sslFd( - config.realmtable[j].raclitable[i]), + ServerRealm_get_raClientsTable(scRealmsTable[j])[i]), buff, 5); + close(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_raClientsTable(scRealmsTable[j])[i]))); } } diff --git a/src/ssl_fd_struct.c b/src/ssl_fd_struct.c index 61bf98c..c8038a4 100644 --- a/src/ssl_fd_struct.c +++ b/src/ssl_fd_struct.c @@ -20,14 +20,11 @@ #include <config.h> -#include <stdlib.h> -#include <string.h> -#include <zlib.h> - #include "activefor.h" #include "stats.h" #include "logging.h" #include "ssl_fd_struct.h" +#include <zlib.h> /* * Function name: SslFd_new diff --git a/src/usage.c b/src/usage.c index 67e7046..a54dccd 100644 --- a/src/usage.c +++ b/src/usage.c @@ -58,7 +58,7 @@ server_long_usage(char* info) printf(" -f, --cfgfile - the name of the file with the configuration for the\n"); printf(" active forwarder (server)\n"); printf(" -D, --dateformat - format of the date printed in logs (see 'man strftime'\n"); - printf(" for details) (default: %%d.%%m.%%Y %%H:%%M:%%S)\n\n"); + printf(" for details) (default: %%Y-%%m-%%d %%H:%%M:%%S)\n\n"); printf(" -t, --timeout - the timeout value for the client's connection\n"); printf(" (default: 5)\n"); printf(" -u, --users - the amount of users allowed to use this server\n"); @@ -125,6 +125,12 @@ client_long_usage(char* info) printf(" destination of the packets (default: the name\n"); printf(" returned by hostname function)\n"); printf(" -p, --portnum - the port we are forwarding connection to (required)\n"); + printf(" --localname - local machine name for connection with afserver\n"); + printf(" (used to bind socket to different interfaces)\n"); + printf(" --localport - local port name for connection with afserver\n"); + printf(" (used to bind socket to different addressees)\n"); + printf(" --localdesname - local machine name for connections with destination\n"); + printf(" application (used to bind socket to different interfaces)\n"); printf(" -V, --version - display version number\n"); printf(" -h, --help - prints this help\n\n"); printf(" Authorization:\n\n"); @@ -134,10 +140,12 @@ client_long_usage(char* info) printf(" --ignorepkeys - ignore invalid server's public keys\n\n"); printf(" Configuration:\n\n"); printf(" -k, --keyfile - the name of the file with RSA key (default: client.rsa)\n"); + printf(" -f, --cfgfile - the name of the file with the configuration for the\n"); + printf(" active forwarder (client)\n"); printf(" -s, --storefile - the name of the file with stored public keys\n"); printf(" (default: known_hosts)\n"); printf(" -D, --dateformat - format of the date printed in logs (see 'man strftime'\n"); - printf(" for details) (default: %%d.%%m.%%Y %%H:%%M:%%S)\n"); + printf(" for details) (default: %%Y-%%m-%%d %%H:%%M:%%S)\n"); printf(" -K, --keep-alive N - send keepalive packets every N seconds\n"); printf(" (default: not send keepalive packets)\n\n"); printf(" Auto-reconnection:\n\n"); @@ -151,10 +159,9 @@ client_long_usage(char* info) printf(" -T, --ar-delay N - wait N seconds between reconnect tries (default: 5)\n\n"); printf(" Modes:\n\n"); printf(" -u, --udpmode - udp mode - client will use udp protocol to\n"); - printf(" communicate with the hostname:portnum (-p)\n"); + printf(" communicate with the hostname:portnum\n"); printf(" -U, --reverseudp - reverse udp forwarding. Udp packets will be forwarded\n"); - printf(" from hostname:portnum (-p) to the server name:portnum\n"); - printf(" (-m)\n"); + printf(" from hostname:portnum to the server name:manageport\n"); printf(" -r, --remoteadmin - remote administration mode. (using '-p #port' will\n"); printf(" force afclient to use port rather than stdin-stdout)\n\n"); printf(" Logging:\n\n"); diff --git a/src/usernames.c b/src/usernames.c index 175cced..a1d2c2d 100644 --- a/src/usernames.c +++ b/src/usernames.c @@ -23,18 +23,18 @@ #include "usernames.h" int -get_username(RealmT* pointer, int user) +get_username(ServerRealm* pointer, int user) { - return ConnectUser_get_userId(pointer->contable[user]); + return ConnectUser_get_userId(ServerRealm_get_usersTable(pointer)[user]); } int -get_usernumber(RealmT* pointer, int userid) +get_usernumber(ServerRealm* pointer, int userid) { int i; - - for (i = 0; i < pointer->usernum; ++i) { - if (userid == ConnectUser_get_userId(pointer->contable[i])) { + + for (i = 0; i < ServerRealm_get_usersLimit(pointer); ++i) { + if (userid == ConnectUser_get_userId(ServerRealm_get_usersTable(pointer)[i])) { return i; } } diff --git a/src/usernames.h b/src/usernames.h index 0d0cb21..eb3023d 100644 --- a/src/usernames.h +++ b/src/usernames.h @@ -19,12 +19,13 @@ */ #include "activefor.h" +#include "server_realm_struct.h" #ifndef _JS_USERNAMES_H #define _JS_USERNAMES_H -int get_username(RealmT*, int); -int get_usernumber(RealmT*, int); +int get_username(ServerRealm*, int); +int get_usernumber(ServerRealm*, int); #endif |