Changeset 467
- Timestamp:
- 02/20/05 16:29:15 (4 years ago)
- Files:
-
- trunk/wifidog/ChangeLog (modified) (1 diff)
- trunk/wifidog/src/Makefile.am (modified) (2 diffs)
- trunk/wifidog/src/auth.c (modified) (3 diffs)
- trunk/wifidog/src/client_list.c (modified) (2 diffs)
- trunk/wifidog/src/commandline.c (modified) (2 diffs)
- trunk/wifidog/src/conf.c (modified) (11 diffs)
- trunk/wifidog/src/firewall.c (modified) (3 diffs)
- trunk/wifidog/src/fw_iptables.c (modified) (9 diffs)
- trunk/wifidog/src/gateway.c (modified) (3 diffs)
- trunk/wifidog/src/http.c (modified) (2 diffs)
- trunk/wifidog/src/ping_thread.c (modified) (2 diffs)
- trunk/wifidog/src/safe.c (added)
- trunk/wifidog/src/safe.h (added)
- trunk/wifidog/src/util.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/wifidog/ChangeLog
r464 r467 1 1 # $Header$ 2 3 2005-02-20 Mina Naguib <mina@ilesansfil.org> 4 * New safe.c with safe_malloc, safe_strdup, safe_asprintf and 5 safe_vasprintf with propper logging and exit when error. Replaced all 6 instances of original with safe versions in all files 7 * Fix memory leak in iptables_fw_counters_update 8 2 9 2005-02-16 Philippe April <philippe@ilesansfil.org> 3 10 * ipkg/rules - When we clean, forgot to delete ipkg-build-stamp trunk/wifidog/src/Makefile.am
r274 r467 23 23 wdctl_thread.c \ 24 24 ping_thread.c \ 25 safe.c \ 25 26 httpd_thread.c 26 27 … … 40 41 wdctl.h \ 41 42 ping_thread.h \ 43 safe.h \ 42 44 httpd_thread.h 43 45 trunk/wifidog/src/auth.c
r424 r467 39 39 #include "httpd.h" 40 40 41 #include "safe.h" 41 42 #include "conf.h" 42 43 #include "debug.h" … … 86 87 87 88 va_start(vlist, format); 88 89 vasprintf(&url, format, vlist); 90 91 asprintf(&response, "HTTP/1.1 307 Please authenticate yourself here\r\nLocation: %s\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<html><head><title>Redirection</title></head><body>Please <a href='%s'>Click here</a> if you're not redirected.", url, url); 89 safe_vasprintf(&url, format, vlist); 90 va_end(vlist); 91 92 safe_asprintf(&response, "HTTP/1.1 307 Please authenticate yourself here\r\nLocation: %s\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<html><head><title>Redirection</title></head><body>Please <a href='%s'>Click here</a> if you're not redirected.", url, url); 93 94 free(url); 92 95 93 96 debug(LOG_DEBUG, "HTTP Redirect: [%s]", response); 94 97 95 98 send(fd, response, strlen(response), 0); 99 100 free(response); 101 96 102 shutdown(fd, 2); 97 103 close(fd); 98 99 free(response);100 free(url);101 104 } 102 105 … … 154 157 } 155 158 156 mac = s trdup(client->mac);157 token = s trdup(client->token);159 mac = safe_strdup(client->mac); 160 token = safe_strdup(client->token); 158 161 159 162 pthread_mutex_unlock(&client_list_mutex); trunk/wifidog/src/client_list.c
r459 r467 40 40 #include <string.h> 41 41 42 #include "safe.h" 42 43 #include "debug.h" 43 44 #include "conf.h" … … 89 90 } 90 91 91 curclient = (t_client *) malloc(sizeof(t_client)); 92 93 if (curclient == NULL) { 94 debug(LOG_ERR, "Out of memory"); 95 exit(-1); 96 } 92 curclient = safe_malloc(sizeof(t_client)); 97 93 memset(curclient, 0, sizeof(t_client)); 98 94 99 curclient->ip = s trdup(ip);100 curclient->mac = s trdup(mac);101 curclient->token = s trdup(token);95 curclient->ip = safe_strdup(ip); 96 curclient->mac = safe_strdup(mac); 97 curclient->token = safe_strdup(token); 102 98 curclient->counters.incoming = curclient->counters.outgoing = 0; 103 99 curclient->counters.last_updated = time(NULL); trunk/wifidog/src/commandline.c
r448 r467 30 30 #include <string.h> 31 31 32 #include "safe.h" 32 33 #include "conf.h" 33 34 … … 80 81 if (optarg) { 81 82 free(config->wdctl_sock); 82 config->wdctl_sock = s trdup(optarg);83 config->wdctl_sock = safe_strdup(optarg); 83 84 } 84 85 break; trunk/wifidog/src/conf.c
r424 r467 37 37 38 38 #include "common.h" 39 39 #include "safe.h" 40 40 #include "debug.h" 41 41 #include "conf.h" … … 157 157 config.daemon = -1; 158 158 config.log_syslog = DEFAULT_LOG_SYSLOG; 159 config.wdctl_sock = s trdup(DEFAULT_WDCTL_SOCK);159 config.wdctl_sock = safe_strdup(DEFAULT_WDCTL_SOCK); 160 160 config.rulesets = NULL; 161 161 } … … 206 206 207 207 /* Defaults */ 208 path = s trdup(DEFAULT_AUTHSERVPATH);208 path = safe_strdup(DEFAULT_AUTHSERVPATH); 209 209 http_port = DEFAULT_AUTHSERVPORT; 210 210 ssl_port = DEFAULT_AUTHSERVSSLPORT; … … 250 250 switch (opcode) { 251 251 case oAuthServHostname: 252 host = s trdup(p2);252 host = safe_strdup(p2); 253 253 break; 254 254 case oAuthServPath: 255 255 free(path); 256 path = s trdup(p2);256 path = safe_strdup(p2); 257 257 break; 258 258 case oAuthServSSLPort: … … 292 292 293 293 /* Allocate memory */ 294 new = (t_auth_serv *)malloc(sizeof(t_auth_serv)); 295 if (new == NULL) { 296 debug(LOG_ERR, "Could not allocate memory for auth server " 297 "configuration"); 298 exit(1); 299 } 294 new = safe_malloc(sizeof(t_auth_serv)); 300 295 301 296 /* Fill in struct */ … … 501 496 502 497 /* Generate rule record */ 503 tmp = (t_firewall_rule *)malloc(sizeof(t_firewall_rule));498 tmp = safe_malloc(sizeof(t_firewall_rule)); 504 499 memset((void *)tmp, 0, sizeof(t_firewall_rule)); 505 500 tmp->block_allow = block_allow; 506 501 if (protocol != NULL) 507 tmp->protocol = s trdup(protocol);502 tmp->protocol = safe_strdup(protocol); 508 503 if (port != NULL) 509 tmp->port = s trdup(port);504 tmp->port = safe_strdup(port); 510 505 if (mask == NULL) 511 tmp->mask = s trdup("0.0.0.0/0");506 tmp->mask = safe_strdup("0.0.0.0/0"); 512 507 else 513 tmp->mask = s trdup(mask);508 tmp->mask = safe_strdup(mask); 514 509 515 510 debug(LOG_DEBUG, "Adding Firewall Rule %s %s port %s to %s", … … 518 513 /* Append the rule record */ 519 514 if (config.rulesets == NULL) { 520 config.rulesets = (t_firewall_ruleset *)malloc( 521 sizeof(t_firewall_ruleset)); 515 config.rulesets = safe_malloc(sizeof(t_firewall_ruleset)); 522 516 memset(config.rulesets, 0, sizeof(t_firewall_ruleset)); 523 config.rulesets->name = s trdup(ruleset);517 config.rulesets->name = safe_strdup(ruleset); 524 518 tmpr = config.rulesets; 525 519 } else { … … 531 525 if (tmpr == NULL) { 532 526 /* Rule did not exist */ 533 tmpr = (t_firewall_ruleset *)malloc( 534 sizeof(t_firewall_ruleset)); 527 tmpr = safe_malloc(sizeof(t_firewall_ruleset)); 535 528 memset(tmpr, 0, sizeof(t_firewall_ruleset)); 536 tmpr->name = s trdup(ruleset);529 tmpr->name = safe_strdup(ruleset); 537 530 tmpr2->next = tmpr; 538 531 } … … 626 619 break; 627 620 case oExternalInterface: 628 config.external_interface = s trdup(p1);621 config.external_interface = safe_strdup(p1); 629 622 break; 630 623 case oGatewayID: 631 config.gw_id = s trdup(p1);624 config.gw_id = safe_strdup(p1); 632 625 break; 633 626 case oGatewayInterface: 634 config.gw_interface = s trdup(p1);627 config.gw_interface = safe_strdup(p1); 635 628 break; 636 629 case oGatewayAddress: 637 config.gw_address = s trdup(p1);630 config.gw_address = safe_strdup(p1); 638 631 break; 639 632 case oGatewayPort: … … 649 642 break; 650 643 case oHTTPDName: 651 config.httpdname = s trdup(p1);644 config.httpdname = safe_strdup(p1); 652 645 break; 653 646 case oHTTPDMaxConn: … … 669 662 case oWdctlSocket: 670 663 free(config.wdctl_sock); 671 config.wdctl_sock = s trdup(p1);664 config.wdctl_sock = safe_strdup(p1); 672 665 break; 673 666 case oClientTimeout: trunk/wifidog/src/firewall.c
r459 r467 56 56 57 57 #include "httpd.h" 58 58 #include "safe.h" 59 59 #include "debug.h" 60 60 #include "conf.h" … … 125 125 while (!feof(proc) && (fscanf(proc, " %15[0-9.] %*s %*s %17[A-F0-9:] %*s %*s", ip, mac) == 2)) { 126 126 if (strcmp(ip, req_ip) == 0) { 127 reply = s trdup(mac);127 reply = safe_strdup(mac); 128 128 break; 129 129 } … … 212 212 p2 = p1->next; 213 213 214 ip = s trdup(p1->ip);215 token = s trdup(p1->token);216 mac = s trdup(p1->mac);214 ip = safe_strdup(p1->ip); 215 token = safe_strdup(p1->token); 216 mac = safe_strdup(p1->mac); 217 217 outgoing = p1->counters.outgoing; 218 218 incoming = p1->counters.incoming; trunk/wifidog/src/fw_iptables.c
r458 r467 41 41 #include "common.h" 42 42 43 #include "safe.h" 43 44 #include "conf.h" 44 45 #include "fw_iptables.h" … … 69 70 70 71 va_start(vlist, format); 71 vasprintf(&fmt_cmd, format, vlist); 72 asprintf(&cmd, "iptables %s", fmt_cmd); 72 safe_vasprintf(&fmt_cmd, format, vlist); 73 va_end(vlist); 74 75 safe_asprintf(&cmd, "iptables %s", fmt_cmd); 76 77 free(fmt_cmd); 78 73 79 debug(LOG_DEBUG, "Executing command: %s", cmd); 74 80 75 81 rc = execute(cmd, fw_quiet); 76 82 77 free(fmt_cmd);78 83 free(cmd); 79 84 … … 97 102 98 103 if (rule->block_allow == 1) { 99 mode = s trdup("ACCEPT");104 mode = safe_strdup("ACCEPT"); 100 105 } else { 101 mode = s trdup("REJECT");106 mode = safe_strdup("REJECT"); 102 107 } 103 108 … … 122 127 /* XXX The buffer command, an automatic variable, will get cleaned 123 128 * off of the stack when we return, so we strdup() it. */ 124 return(s trdup(command));129 return(safe_strdup(command)); 125 130 } 126 131 … … 189 194 config = config_get_config(); 190 195 LOCK_CONFIG(); 191 gw_interface = s trdup(config->gw_interface);196 gw_interface = safe_strdup(config->gw_interface); 192 197 gw_port = config->gw_port; 193 198 UNLOCK_CONFIG(); … … 343 348 int deleted = 0; 344 349 345 asprintf(&command, "iptables -t %s -L %s -n --line-numbers -v", table, chain);350 safe_asprintf(&command, "iptables -t %s -L %s -n --line-numbers -v", table, chain); 346 351 347 352 if ((p = popen(command, "r"))) { … … 356 361 if (sscanf(line, "%9[0-9]", rulenum) == 1) { 357 362 /* Delete the rule: */ 358 asprintf(&command2, "-t %s -D %s %s", table, chain, rulenum);363 safe_asprintf(&command2, "-t %s -D %s %s", table, chain, rulenum); 359 364 iptables_do_command(command2); 360 365 free(command2); … … 416 421 417 422 /* Look for outgoing traffic */ 418 asprintf(&script, "%s %s", "iptables", "-v -n -x -t mangle -L " TABLE_WIFIDOG_OUTGOING); 419 if (!(output = popen(script, "r"))) { 423 safe_asprintf(&script, "%s %s", "iptables", "-v -n -x -t mangle -L " TABLE_WIFIDOG_OUTGOING); 424 output = popen(script, "r"); 425 free(script); 426 if (!output) { 420 427 debug(LOG_ERR, "popen(): %s", strerror(errno)); 421 428 return -1; 422 429 } 423 free(script);424 430 425 431 /* skip the first two lines */ … … 453 459 454 460 /* Look for incoming traffic */ 455 asprintf(&script, "%s %s", "iptables", "-v -n -x -t mangle -L " TABLE_WIFIDOG_INCOMING); 456 if (!(output = popen(script, "r"))) { 461 safe_asprintf(&script, "%s %s", "iptables", "-v -n -x -t mangle -L " TABLE_WIFIDOG_INCOMING); 462 output = popen(script, "r"); 463 free(script); 464 if (!output) { 457 465 debug(LOG_ERR, "popen(): %s", strerror(errno)); 458 466 return -1; 459 467 } 460 free(script);461 468 462 469 /* skip the first two lines */ trunk/wifidog/src/gateway.c
r422 r467 46 46 47 47 #include "httpd.h" 48 48 #include "safe.h" 49 49 #include "debug.h" 50 50 #include "conf.h" … … 208 208 209 209 /* start control thread */ 210 pthread_create(&tid, NULL, (void *)thread_wdctl, 211 (void *)strdup(config->wdctl_sock)); 210 pthread_create(&tid, NULL, (void *)thread_wdctl, (void *)safe_strdup(config->wdctl_sock)); 212 211 pthread_detach(tid); 213 212 … … 247 246 /* The void**'s are a simulation of the normal C 248 247 * function calling sequence. */ 249 params = (void **)malloc(2 * sizeof(void *));248 params = safe_malloc(2 * sizeof(void *)); 250 249 *params = webserver; 251 250 *(params + 1) = r; trunk/wifidog/src/http.c
r428 r467 36 36 #include "httpd.h" 37 37 38 #include "safe.h" 38 39 #include "debug.h" 39 40 #include "conf.h" … … 88 89 debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr); 89 90 } 90 else if ((asprintf(&newlocation, "Location: %s://%s:%d%slogin?" 91 "gw_address=%s&gw_port=%d&gw_id=%s&url=%s", 91 else { 92 /* Re-direct them to auth server */ 93 safe_asprintf(&newlocation, "Location: %s://%s:%d%slogin?gw_address=%s&gw_port=%d&gw_id=%s&url=%s", 92 94 protocol, 93 95 auth_server->authserv_hostname, 94 96 port, 95 97 auth_server->authserv_path, 96 config->gw_address, config->gw_port, 98 config->gw_address, 99 config->gw_port, 97 100 config->gw_id, 98 url)) == -1) { 99 debug(LOG_ERR, "Failed to asprintf newlocation"); 100 httpdOutput(r, "Internal error occurred"); 101 } else { 102 /* Re-direct them to auth server */ 101 url); 103 102 httpdSetResponse(r, "307 Please authenticate yourself here\n"); 104 103 httpdAddHeader(r, newlocation); trunk/wifidog/src/ping_thread.c
r463 r467 43 43 44 44 #include "../config.h" 45 #include "safe.h" 45 46 #include "common.h" 46 47 #include "conf.h" … … 138 139 139 140 if (auth_server->last_ip == NULL) { 140 auth_server->last_ip = strdup(inet_ntoa(*h_addr)); 141 if (auth_server->last_ip == NULL) { 142 debug(LOG_CRIT, "Could not allocate memory, Banzai!"); 143 exit(-1); 144 } 141 auth_server->last_ip = safe_strdup(inet_ntoa(*h_addr)); 145 142 } else { 146 tmp_addr = s trdup(inet_ntoa(*h_addr));143 tmp_addr = safe_strdup(inet_ntoa(*h_addr)); 147 144 if (strcmp(auth_server->last_ip, tmp_addr) != 0) { 148 145 free(auth_server->last_ip); trunk/wifidog/src/util.c
r451 r467 46 46 #include <netdb.h> 47 47 48 #include "safe.h" 48 49 #include "util.h" 49 50 #include "conf.h" … … 101 102 /* XXX Calling function is reponsible for free() */ 102 103 103 h_addr = (struct in_addr *)malloc(sizeof(struct in_addr)); 104 105 if (h_addr == NULL) { 106 debug(LOG_CRIT, "Failed to allocate memory for in_addr"); 107 exit(1); 108 } 104 h_addr = safe_malloc(sizeof(struct in_addr)); 109 105 110 106 LOCK_GHBN(); … … 154 150 155 151 ip_str = (char *)inet_ntoa(in); 156 return s trdup(ip_str);152 return safe_strdup(ip_str); 157 153 } 158 154
