Changeset 467

Show
Ignore:
Timestamp:
02/20/05 16:29:15 (4 years ago)
Author:
minaguib
Message:

* New safe.c with safe_malloc, safe_strdup, safe_asprintf and safe_vasprintf with propper logging and exit when error. Replaced all instances of original with safe versions in all files
* Fix memory leak in iptables_fw_counters_update

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wifidog/ChangeLog

    r464 r467  
    11# $Header$ 
     2 
     32005-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 
    292005-02-16 Philippe April <philippe@ilesansfil.org> 
    310        * ipkg/rules - When we clean, forgot to delete ipkg-build-stamp 
  • trunk/wifidog/src/Makefile.am

    r274 r467  
    2323        wdctl_thread.c \ 
    2424        ping_thread.c \ 
     25        safe.c \ 
    2526        httpd_thread.c 
    2627 
     
    4041        wdctl.h \ 
    4142        ping_thread.h \ 
     43        safe.h \ 
    4244        httpd_thread.h 
    4345 
  • trunk/wifidog/src/auth.c

    r424 r467  
    3939#include "httpd.h" 
    4040 
     41#include "safe.h" 
    4142#include "conf.h" 
    4243#include "debug.h" 
     
    8687 
    8788        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); 
    9295 
    9396        debug(LOG_DEBUG, "HTTP Redirect: [%s]", response); 
    9497         
    9598        send(fd, response, strlen(response), 0); 
     99 
     100        free(response); 
     101 
    96102        shutdown(fd, 2); 
    97103        close(fd); 
    98  
    99         free(response); 
    100         free(url); 
    101104} 
    102105 
     
    154157        } 
    155158         
    156         mac = strdup(client->mac); 
    157         token = strdup(client->token); 
     159        mac = safe_strdup(client->mac); 
     160        token = safe_strdup(client->token); 
    158161         
    159162        pthread_mutex_unlock(&client_list_mutex); 
  • trunk/wifidog/src/client_list.c

    r459 r467  
    4040#include <string.h> 
    4141 
     42#include "safe.h" 
    4243#include "debug.h" 
    4344#include "conf.h" 
     
    8990    } 
    9091 
    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)); 
    9793    memset(curclient, 0, sizeof(t_client)); 
    9894 
    99     curclient->ip = strdup(ip); 
    100     curclient->mac = strdup(mac); 
    101     curclient->token = strdup(token); 
     95    curclient->ip = safe_strdup(ip); 
     96    curclient->mac = safe_strdup(mac); 
     97    curclient->token = safe_strdup(token); 
    10298    curclient->counters.incoming = curclient->counters.outgoing = 0; 
    10399    curclient->counters.last_updated = time(NULL); 
  • trunk/wifidog/src/commandline.c

    r448 r467  
    3030#include <string.h> 
    3131 
     32#include "safe.h" 
    3233#include "conf.h" 
    3334 
     
    8081                if (optarg) { 
    8182                    free(config->wdctl_sock); 
    82                     config->wdctl_sock = strdup(optarg); 
     83                    config->wdctl_sock = safe_strdup(optarg); 
    8384                } 
    8485                break; 
  • trunk/wifidog/src/conf.c

    r424 r467  
    3737 
    3838#include "common.h" 
    39  
     39#include "safe.h" 
    4040#include "debug.h" 
    4141#include "conf.h" 
     
    157157        config.daemon = -1; 
    158158        config.log_syslog = DEFAULT_LOG_SYSLOG; 
    159         config.wdctl_sock = strdup(DEFAULT_WDCTL_SOCK); 
     159        config.wdctl_sock = safe_strdup(DEFAULT_WDCTL_SOCK); 
    160160        config.rulesets = NULL; 
    161161} 
     
    206206 
    207207        /* Defaults */ 
    208         path = strdup(DEFAULT_AUTHSERVPATH); 
     208        path = safe_strdup(DEFAULT_AUTHSERVPATH); 
    209209        http_port = DEFAULT_AUTHSERVPORT; 
    210210        ssl_port = DEFAULT_AUTHSERVSSLPORT; 
     
    250250                        switch (opcode) { 
    251251                                case oAuthServHostname: 
    252                                         host = strdup(p2); 
     252                                        host = safe_strdup(p2); 
    253253                                        break; 
    254254                                case oAuthServPath: 
    255255                                        free(path); 
    256                                         path = strdup(p2); 
     256                                        path = safe_strdup(p2); 
    257257                                        break; 
    258258                                case oAuthServSSLPort: 
     
    292292 
    293293        /* 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)); 
    300295         
    301296        /* Fill in struct */ 
     
    501496 
    502497        /* Generate rule record */ 
    503         tmp = (t_firewall_rule *)malloc(sizeof(t_firewall_rule)); 
     498        tmp = safe_malloc(sizeof(t_firewall_rule)); 
    504499        memset((void *)tmp, 0, sizeof(t_firewall_rule)); 
    505500        tmp->block_allow = block_allow; 
    506501        if (protocol != NULL) 
    507                 tmp->protocol = strdup(protocol); 
     502                tmp->protocol = safe_strdup(protocol); 
    508503        if (port != NULL) 
    509                 tmp->port = strdup(port); 
     504                tmp->port = safe_strdup(port); 
    510505        if (mask == NULL) 
    511                 tmp->mask = strdup("0.0.0.0/0"); 
     506                tmp->mask = safe_strdup("0.0.0.0/0"); 
    512507        else 
    513                 tmp->mask = strdup(mask); 
     508                tmp->mask = safe_strdup(mask); 
    514509 
    515510        debug(LOG_DEBUG, "Adding Firewall Rule %s %s port %s to %s", 
     
    518513        /* Append the rule record */ 
    519514        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)); 
    522516                memset(config.rulesets, 0, sizeof(t_firewall_ruleset)); 
    523                 config.rulesets->name = strdup(ruleset); 
     517                config.rulesets->name = safe_strdup(ruleset); 
    524518                tmpr = config.rulesets; 
    525519        } else { 
     
    531525                if (tmpr == NULL) { 
    532526                        /* Rule did not exist */ 
    533                         tmpr = (t_firewall_ruleset *)malloc( 
    534                                                 sizeof(t_firewall_ruleset)); 
     527                        tmpr = safe_malloc(sizeof(t_firewall_ruleset)); 
    535528                        memset(tmpr, 0, sizeof(t_firewall_ruleset)); 
    536                         tmpr->name = strdup(ruleset); 
     529                        tmpr->name = safe_strdup(ruleset); 
    537530                        tmpr2->next = tmpr; 
    538531                } 
     
    626619                                        break; 
    627620                                case oExternalInterface: 
    628                                         config.external_interface = strdup(p1); 
     621                                        config.external_interface = safe_strdup(p1); 
    629622                                        break; 
    630623                                case oGatewayID: 
    631                                         config.gw_id = strdup(p1); 
     624                                        config.gw_id = safe_strdup(p1); 
    632625                                        break; 
    633626                                case oGatewayInterface: 
    634                                         config.gw_interface = strdup(p1); 
     627                                        config.gw_interface = safe_strdup(p1); 
    635628                                        break; 
    636629                                case oGatewayAddress: 
    637                                         config.gw_address = strdup(p1); 
     630                                        config.gw_address = safe_strdup(p1); 
    638631                                        break; 
    639632                                case oGatewayPort: 
     
    649642                                        break; 
    650643                                case oHTTPDName: 
    651                                         config.httpdname = strdup(p1); 
     644                                        config.httpdname = safe_strdup(p1); 
    652645                                        break; 
    653646                                case oHTTPDMaxConn: 
     
    669662                                case oWdctlSocket: 
    670663                                        free(config.wdctl_sock); 
    671                                         config.wdctl_sock = strdup(p1); 
     664                                        config.wdctl_sock = safe_strdup(p1); 
    672665                                        break; 
    673666                                case oClientTimeout: 
  • trunk/wifidog/src/firewall.c

    r459 r467  
    5656 
    5757#include "httpd.h" 
    58  
     58#include "safe.h" 
    5959#include "debug.h" 
    6060#include "conf.h" 
     
    125125    while (!feof(proc) && (fscanf(proc, " %15[0-9.] %*s %*s %17[A-F0-9:] %*s %*s", ip, mac) == 2)) { 
    126126                  if (strcmp(ip, req_ip) == 0) { 
    127                                 reply = strdup(mac); 
     127                                reply = safe_strdup(mac); 
    128128                                break; 
    129129                  } 
     
    212212        p2 = p1->next; 
    213213 
    214         ip = strdup(p1->ip); 
    215         token = strdup(p1->token); 
    216         mac = strdup(p1->mac); 
     214        ip = safe_strdup(p1->ip); 
     215        token = safe_strdup(p1->token); 
     216        mac = safe_strdup(p1->mac); 
    217217            outgoing = p1->counters.outgoing; 
    218218            incoming = p1->counters.incoming; 
  • trunk/wifidog/src/fw_iptables.c

    r458 r467  
    4141#include "common.h" 
    4242 
     43#include "safe.h" 
    4344#include "conf.h" 
    4445#include "fw_iptables.h" 
     
    6970 
    7071    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 
    7379    debug(LOG_DEBUG, "Executing command: %s", cmd); 
    7480         
    7581    rc = execute(cmd, fw_quiet); 
    7682 
    77     free(fmt_cmd); 
    7883    free(cmd); 
    7984 
     
    97102     
    98103    if (rule->block_allow == 1) { 
    99         mode = strdup("ACCEPT"); 
     104        mode = safe_strdup("ACCEPT"); 
    100105    } else { 
    101         mode = strdup("REJECT"); 
     106        mode = safe_strdup("REJECT"); 
    102107    } 
    103108     
     
    122127    /* XXX The buffer command, an automatic variable, will get cleaned 
    123128     * off of the stack when we return, so we strdup() it. */ 
    124     return(strdup(command)); 
     129    return(safe_strdup(command)); 
    125130} 
    126131 
     
    189194    config = config_get_config(); 
    190195         LOCK_CONFIG(); 
    191          gw_interface = strdup(config->gw_interface); 
     196         gw_interface = safe_strdup(config->gw_interface); 
    192197         gw_port = config->gw_port; 
    193198         UNLOCK_CONFIG(); 
     
    343348        int deleted = 0; 
    344349 
    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); 
    346351 
    347352        if ((p = popen(command, "r"))) { 
     
    356361                                if (sscanf(line, "%9[0-9]", rulenum) == 1) { 
    357362                                        /* 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); 
    359364                                        iptables_do_command(command2); 
    360365                                        free(command2); 
     
    416421 
    417422    /* 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) { 
    420427        debug(LOG_ERR, "popen(): %s", strerror(errno)); 
    421428        return -1; 
    422429    } 
    423     free(script); 
    424430 
    425431    /* skip the first two lines */ 
     
    453459 
    454460    /* 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) { 
    457465        debug(LOG_ERR, "popen(): %s", strerror(errno)); 
    458466        return -1; 
    459467    } 
    460     free(script); 
    461468 
    462469    /* skip the first two lines */ 
  • trunk/wifidog/src/gateway.c

    r422 r467  
    4646 
    4747#include "httpd.h" 
    48  
     48#include "safe.h" 
    4949#include "debug.h" 
    5050#include "conf.h" 
     
    208208 
    209209        /* 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)); 
    212211        pthread_detach(tid); 
    213212         
     
    247246                        /* The void**'s are a simulation of the normal C 
    248247                         * function calling sequence. */ 
    249                         params = (void **)malloc(2 * sizeof(void *)); 
     248                        params = safe_malloc(2 * sizeof(void *)); 
    250249                        *params = webserver; 
    251250                        *(params + 1) = r; 
  • trunk/wifidog/src/http.c

    r428 r467  
    3636#include "httpd.h" 
    3737 
     38#include "safe.h" 
    3839#include "debug.h" 
    3940#include "conf.h" 
     
    8889                debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr); 
    8990        } 
    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", 
    9294                        protocol, 
    9395                        auth_server->authserv_hostname, 
    9496                        port, 
    9597                        auth_server->authserv_path, 
    96                         config->gw_address, config->gw_port,  
     98                        config->gw_address, 
     99                        config->gw_port,  
    97100                        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); 
    103102                httpdSetResponse(r, "307 Please authenticate yourself here\n"); 
    104103                httpdAddHeader(r, newlocation); 
  • trunk/wifidog/src/ping_thread.c

    r463 r467  
    4343 
    4444#include "../config.h" 
     45#include "safe.h" 
    4546#include "common.h" 
    4647#include "conf.h" 
     
    138139 
    139140        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)); 
    145142        } else { 
    146                 tmp_addr = strdup(inet_ntoa(*h_addr)); 
     143                tmp_addr = safe_strdup(inet_ntoa(*h_addr)); 
    147144                if (strcmp(auth_server->last_ip, tmp_addr) != 0) { 
    148145                        free(auth_server->last_ip); 
  • trunk/wifidog/src/util.c

    r451 r467  
    4646#include <netdb.h> 
    4747 
     48#include "safe.h" 
    4849#include "util.h" 
    4950#include "conf.h" 
     
    101102        /* XXX Calling function is reponsible for free() */ 
    102103 
    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)); 
    109105         
    110106        LOCK_GHBN(); 
     
    154150 
    155151    ip_str = (char *)inet_ntoa(in); 
    156     return strdup(ip_str); 
     152    return safe_strdup(ip_str); 
    157153} 
    158154