Changeset 34

Show
Ignore:
Timestamp:
04/07/04 19:49:26 (9 years ago)
Author:
alexcv
Message:

re-indented

Location:
trunk/wifidog/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog/src/firewall.c

    r33 r34  
    1717 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   * 
    1818 *                                                                  * 
    19 \********************************************************************/ 
     19 \********************************************************************/ 
    2020 
    2121/* $Header$ */ 
    2222/** @internal 
    23     @file firewall.c 
    24     @brief Firewall update functions 
    25     @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> 
    26 */ 
     23  @file firewall.c 
     24  @brief Firewall update functions 
     25  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> 
     26 */ 
    2727 
    2828#include "common.h" 
     
    3232t_node *firstnode = NULL; 
    3333 
    34 int 
     34        int 
    3535fw_allow(char *ip, char *mac, int profile) 
    3636{ 
    37     char s_profile[16]; 
    38     char script[MAX_BUF]; 
    39     struct stat st; 
    40     char *command[] = {script, "allow", ip, mac, s_profile, NULL}; 
    41  
    42     sprintf(s_profile, "%-10d", profile); 
    43     sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWACCESS); 
    44  
    45     if (-1 == (stat(script, &st))) { 
    46         debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
    47         return(1); 
    48     } 
    49  
    50     return(execute(command)); 
    51 } 
    52  
    53 int 
     37        char s_profile[16]; 
     38        char script[MAX_BUF]; 
     39        struct stat st; 
     40        char *command[] = {script, "allow", ip, mac, s_profile, NULL}; 
     41 
     42        sprintf(s_profile, "%-10d", profile); 
     43        sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWACCESS); 
     44 
     45        if (-1 == (stat(script, &st))) { 
     46                debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
     47                return(1); 
     48        } 
     49 
     50        return(execute(command)); 
     51} 
     52 
     53        int 
    5454fw_deny(char *ip, char *mac, int profile) 
    5555{ 
    56     char s_profile[16]; 
    57     char script[MAX_BUF]; 
    58     struct stat st; 
    59     char *command[] = {script, "deny", ip, mac, s_profile, NULL}; 
    60  
    61     sprintf(s_profile, "%-10d", profile); 
    62     sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWACCESS); 
    63  
    64     if (-1 == (stat(script, &st))) { 
    65         debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
    66         return(1); 
    67     } 
    68  
    69     return(execute(command)); 
    70 } 
    71  
    72 int 
     56        char s_profile[16]; 
     57        char script[MAX_BUF]; 
     58        struct stat st; 
     59        char *command[] = {script, "deny", ip, mac, s_profile, NULL}; 
     60 
     61        sprintf(s_profile, "%-10d", profile); 
     62        sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWACCESS); 
     63 
     64        if (-1 == (stat(script, &st))) { 
     65                debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
     66                return(1); 
     67        } 
     68 
     69        return(execute(command)); 
     70} 
     71 
     72        int 
    7373execute(char **argv) 
    7474{ 
    75     int pid, status, rc; 
    76  
    77     debug(D_LOG_DEBUG, "Executing '%s'", argv[0]); 
    78  
    79     if ((pid = fork()) < 0) {     /* fork a child process           */ 
    80         debug(D_LOG_ERR, "fork(): %s", strerror(errno)); 
    81         exit(1); 
    82     } else if (pid == 0) {          /* for the child process:         */ 
    83         if (execvp(*argv, argv) < 0) {     /* execute the command  */ 
    84             debug(D_LOG_ERR, "fork(): %s", strerror(errno)); 
    85             exit(1); 
    86         } 
    87     } else {                                  /* for the parent:      */ 
    88         do { 
    89             rc = wait(&status); 
    90         } while (rc != pid && rc != -1);        /* wait for completion  */ 
    91     } 
    92  
    93     return(status); 
    94 } 
    95  
    96 char * 
     75        int pid, status, rc; 
     76 
     77        debug(D_LOG_DEBUG, "Executing '%s'", argv[0]); 
     78 
     79        if ((pid = fork()) < 0) {     /* fork a child process           */ 
     80                debug(D_LOG_ERR, "fork(): %s", strerror(errno)); 
     81                exit(1); 
     82        } else if (pid == 0) {          /* for the child process:         */ 
     83                if (execvp(*argv, argv) < 0) {     /* execute the command  */ 
     84                        debug(D_LOG_ERR, "fork(): %s", strerror(errno)); 
     85                        exit(1); 
     86                } 
     87        } else {                                  /* for the parent:      */ 
     88                do { 
     89                        rc = wait(&status); 
     90                } while (rc != pid && rc != -1);        /* wait for completion  */ 
     91        } 
     92 
     93        return(status); 
     94} 
     95 
     96        char * 
    9797arp_get(char *req_ip) 
    9898{ 
    99     FILE *proc; 
    100     char ip[16], *mac; 
    101  
    102  
    103     if (!(proc = fopen("/proc/net/arp", "r"))) { 
    104         return NULL; 
    105     } 
    106  
    107     /* Skip first line */ 
    108     fscanf(proc, "%*s %*s %*s %*s %*s %*s %*s %*s %*s"); 
    109     mac = (char *)malloc(18); 
    110     while(!feof(proc)) { 
    111         fscanf(proc, "%15s %*s %*s %17s %*s %*s", ip, mac); 
    112         if (strcmp(ip, req_ip) == 0) { 
    113             return mac; 
    114         } 
    115     } 
    116     fclose(proc); 
    117  
    118          free(mac); 
    119  
    120     return NULL; 
    121 } 
    122  
    123 int 
     99        FILE *proc; 
     100        char ip[16], *mac; 
     101 
     102 
     103        if (!(proc = fopen("/proc/net/arp", "r"))) { 
     104                return NULL; 
     105        } 
     106 
     107        /* Skip first line */ 
     108        fscanf(proc, "%*s %*s %*s %*s %*s %*s %*s %*s %*s"); 
     109        mac = (char *)malloc(18); 
     110        while(!feof(proc)) { 
     111                fscanf(proc, "%15s %*s %*s %17s %*s %*s", ip, mac); 
     112                if (strcmp(ip, req_ip) == 0) { 
     113                        return mac; 
     114                } 
     115        } 
     116        fclose(proc); 
     117 
     118        free(mac); 
     119 
     120        return NULL; 
     121} 
     122 
     123        int 
    124124fw_init(void) 
    125125{ 
    126     char port[16]; 
    127     char script[MAX_BUF]; 
    128     int rc; 
    129     struct stat st; 
    130     char *command[] = {script, config.gw_interface, config.gw_address, port, config.authserv_hostname, NULL}; 
    131  
    132     sprintf(port, "%-5d", config.gw_port); 
    133     sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWINIT); 
    134  
    135     if (-1 == (stat(script, &st))) { 
    136         debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
    137         debug(D_LOG_ERR, "Exiting..."); 
    138         exit(1); 
    139     } 
    140  
    141     debug(D_LOG_INFO, "Setting firewall rules"); 
    142  
    143     if ((rc = execute(command)) != 0) { 
    144         debug(D_LOG_ERR, "Could not setup firewall, exiting..."); 
    145         exit(1); 
    146     } 
    147  
    148     return(rc); 
    149 } 
    150  
    151 int 
     126        char port[16]; 
     127        char script[MAX_BUF]; 
     128        int rc; 
     129        struct stat st; 
     130        char *command[] = {script, config.gw_interface, config.gw_address, port, config.authserv_hostname, NULL}; 
     131 
     132        sprintf(port, "%-5d", config.gw_port); 
     133        sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWINIT); 
     134 
     135        if (-1 == (stat(script, &st))) { 
     136                debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
     137                debug(D_LOG_ERR, "Exiting..."); 
     138                exit(1); 
     139        } 
     140 
     141        debug(D_LOG_INFO, "Setting firewall rules"); 
     142 
     143        if ((rc = execute(command)) != 0) { 
     144                debug(D_LOG_ERR, "Could not setup firewall, exiting..."); 
     145                exit(1); 
     146        } 
     147 
     148        return(rc); 
     149} 
     150 
     151        int 
    152152fw_destroy(void) 
    153153{ 
    154     char script[MAX_BUF]; 
    155     struct stat st; 
    156     char *command[] = {script, NULL}; 
    157  
    158     sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWDESTROY); 
    159  
    160     if (-1 == (stat(script, &st))) { 
    161         debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
    162         return(1); 
    163     } 
    164  
    165     debug(D_LOG_INFO, "Flushing firewall rules"); 
    166  
    167     return(execute(command)); 
    168 } 
    169  
    170 void 
     154        char script[MAX_BUF]; 
     155        struct stat st; 
     156        char *command[] = {script, NULL}; 
     157 
     158        sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWDESTROY); 
     159 
     160        if (-1 == (stat(script, &st))) { 
     161                debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
     162                return(1); 
     163        } 
     164 
     165        debug(D_LOG_INFO, "Flushing firewall rules"); 
     166 
     167        return(execute(command)); 
     168} 
     169 
     170        void 
    171171fw_counter(void) 
    172172{ 
    173     FILE *output; 
    174     long int counter; 
    175     int profile, rc; 
    176     char ip[255], mac[255]; 
    177     char script[MAX_BUF]; 
    178     t_node *p1; 
    179     ChildInfo   *ci; 
    180  
    181     sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWCOUNTERS); 
    182  
    183     if (!(output = popen(script, "r"))) { 
    184         debug(D_LOG_ERR, "popen(): %s", strerror(errno)); 
    185     } else { 
    186         while (!(feof(output)) && output) { 
    187             rc = fscanf(output, "%ld %s %s %d", &counter, ip, mac, &profile); 
    188             if (rc == 4 && rc != EOF) { 
    189  
    190                 /* TODO If the client is not active for x seconds 
    191                 * timeout the client and destroy token. 
    192                 * Maybe this should be done on the auth server*/ 
    193  
    194                 p1 = node_find_by_ip(ip); 
    195                 if (!(p1) || !(p1->active)) { 
    196                     debug(D_LOG_DEBUG, "Client %s not active", ip); 
    197                 } else { 
    198                     p1->counter = counter; 
    199  
    200                     ci = new_childinfo(); 
    201                     ci->ip = strdup(p1->ip); 
    202                     ci->mac = strdup(p1->mac); 
    203                     register_child(ci); 
    204                     
    205                     if (fork() == 0) { 
    206                         authenticate(p1->ip, p1->mac, p1->token, p1->counter); 
    207                         /* exit() is in authenticate(); */ 
    208                     } 
    209                     debug(D_LOG_DEBUG, "Updated client %s counter to %ld bytes", ip, counter); 
    210                 } 
    211             } 
    212         } 
    213         pclose(output); 
    214     } 
    215 } 
    216  
    217 void 
     173        FILE *output; 
     174        long int counter; 
     175        int profile, rc; 
     176        char ip[255], mac[255]; 
     177        char script[MAX_BUF]; 
     178        t_node *p1; 
     179        ChildInfo       *ci; 
     180 
     181        sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWCOUNTERS); 
     182 
     183        if (!(output = popen(script, "r"))) { 
     184                debug(D_LOG_ERR, "popen(): %s", strerror(errno)); 
     185        } else { 
     186                while (!(feof(output)) && output) { 
     187                        rc = fscanf(output, "%ld %s %s %d", &counter, ip, mac, &profile); 
     188                        if (rc == 4 && rc != EOF) { 
     189 
     190                                /* TODO If the client is not active for x seconds 
     191                                * timeout the client and destroy token. 
     192                                * Maybe this should be done on the auth server*/ 
     193 
     194                                p1 = node_find_by_ip(ip); 
     195                                if (!(p1) || !(p1->active)) { 
     196                                        debug(D_LOG_DEBUG, "Client %s not active", ip); 
     197                                } else { 
     198                                        p1->counter = counter; 
     199 
     200                                        ci = new_childinfo(); 
     201                                        ci->ip = strdup(p1->ip); 
     202                                        ci->mac = strdup(p1->mac); 
     203                                        register_child(ci); 
     204 
     205                                        if (fork() == 0) { 
     206                                                authenticate(p1->ip, p1->mac, p1->token, p1->counter); 
     207                                                /* exit() is in authenticate(); */ 
     208                                        } 
     209                                        debug(D_LOG_DEBUG, "Updated client %s counter to %ld bytes", ip, counter); 
     210                                } 
     211                        } 
     212                } 
     213                pclose(output); 
     214        } 
     215} 
     216 
     217        void 
    218218node_init(void) 
    219219{ 
    220     firstnode = NULL; 
    221 } 
    222  
    223 t_node * 
     220        firstnode = NULL; 
     221} 
     222 
     223        t_node * 
    224224node_add(char *ip, char *mac, char *token, long int counter, int active) 
    225225{ 
    226     t_node *curnode, 
    227            *prevnode; 
    228  
    229     prevnode = NULL; 
    230     curnode = firstnode; 
    231  
    232     while (curnode != NULL) { 
    233             prevnode = curnode; 
    234             curnode = curnode->next; 
    235     } 
    236  
    237     curnode = (t_node *)malloc(sizeof(t_node)); 
    238      
    239     if (curnode == NULL) { 
    240             debug(D_LOG_DEBUG, "Out of memory"); 
    241             exit(-1); 
    242     } 
    243  
    244     memset(curnode, 0, sizeof(t_node)); 
    245  
    246     curnode->ip = strdup(ip); 
    247     curnode->mac = strdup(mac); 
    248     curnode->token = strdup(token); 
    249     curnode->counter = counter; 
    250     curnode->active = active; 
    251  
    252     if (prevnode == NULL) { 
    253             firstnode = curnode; 
    254     } else { 
    255             prevnode->next = curnode; 
    256     } 
    257      
    258     debug(D_LOG_DEBUG, "Added a new node to linked list: IP: %s Token: %s", ip, token); 
    259  
    260     return curnode; 
    261 } 
    262  
    263 t_node * 
     226        t_node *curnode, 
     227        *prevnode; 
     228 
     229        prevnode = NULL; 
     230        curnode = firstnode; 
     231 
     232        while (curnode != NULL) { 
     233                prevnode = curnode; 
     234                curnode = curnode->next; 
     235        } 
     236 
     237        curnode = (t_node *)malloc(sizeof(t_node)); 
     238 
     239        if (curnode == NULL) { 
     240                debug(D_LOG_DEBUG, "Out of memory"); 
     241                exit(-1); 
     242        } 
     243 
     244        memset(curnode, 0, sizeof(t_node)); 
     245 
     246        curnode->ip = strdup(ip); 
     247        curnode->mac = strdup(mac); 
     248        curnode->token = strdup(token); 
     249        curnode->counter = counter; 
     250        curnode->active = active; 
     251 
     252        if (prevnode == NULL) { 
     253                firstnode = curnode; 
     254        } else { 
     255                prevnode->next = curnode; 
     256        } 
     257 
     258        debug(D_LOG_DEBUG, "Added a new node to linked list: IP: %s Token: %s", ip, token); 
     259 
     260        return curnode; 
     261} 
     262 
     263        t_node * 
    264264node_find_by_ip(char *ip) 
    265265{ 
    266     t_node *ptr; 
    267  
    268     ptr = firstnode; 
    269     while (NULL != ptr->next) { 
    270         if (0 == strcmp(ptr->ip, ip)) 
    271             return ptr; 
    272         ptr = ptr->next; 
    273     }  
    274  
    275     return NULL; 
    276 } 
    277  
    278 t_node * 
     266        t_node *ptr; 
     267 
     268        ptr = firstnode; 
     269        while (NULL != ptr->next) { 
     270                if (0 == strcmp(ptr->ip, ip)) 
     271                        return ptr; 
     272                ptr = ptr->next; 
     273        }  
     274 
     275        return NULL; 
     276} 
     277 
     278        t_node * 
    279279node_find_by_token(char *token) 
    280280{ 
    281     t_node *ptr; 
    282  
    283     ptr = firstnode; 
    284     while (NULL != ptr->next) { 
    285         if (0 == strcmp(ptr->token, token)) 
    286             return ptr; 
    287         ptr = ptr->next; 
    288     }  
    289  
    290     return NULL; 
    291 } 
    292  
    293 void 
     281        t_node *ptr; 
     282 
     283        ptr = firstnode; 
     284        while (NULL != ptr->next) { 
     285                if (0 == strcmp(ptr->token, token)) 
     286                        return ptr; 
     287                ptr = ptr->next; 
     288        }  
     289 
     290        return NULL; 
     291} 
     292 
     293        void 
    294294free_node(t_node *node) 
    295295{ 
     
    303303        if (node->token != NULL) 
    304304                free(node->token); 
    305          
     305 
    306306        free(node); 
    307307} 
    308308 
    309 void 
     309        void 
    310310node_delete(t_node *node) 
    311311{ 
    312312        t_node  *ptr; 
    313                  
     313 
    314314        ptr = firstnode; 
    315315 
  • trunk/wifidog/src/gateway.c

    r31 r34  
    22 * This program is free software; you can redistribute it and/or    * 
    33 * modify it under the terms of the GNU General Public License as   * 
    4  * published by the Free Software Foundation; either version 2 of   * 
     4 * published by the Free:Software Foundation; either version 2 of   * 
    55 * the License, or (at your option) any later version.              * 
    66 *                                                                  * 
     
    1717 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   * 
    1818 *                                                                  * 
    19 \********************************************************************/ 
     19 \********************************************************************/ 
    2020 
    2121/* $Header$ */ 
    2222/** @internal 
    23     @file gateway.c 
    24     @brief Main loop 
    25     @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> 
    26 */ 
     23  @file gateway.c 
     24  @brief Main loop 
     25  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> 
     26 */ 
    2727 
    2828#include "common.h" 
     
    3232void main_loop(void) 
    3333{ 
    34     struct timeval tv; 
    35     time_t last_checked; 
    36          httpd * webserver; 
    37          int result; 
     34        struct timeval tv; 
     35        time_t last_checked; 
     36        httpd * webserver; 
     37        int result; 
    3838 
    39     /* Initialize the linked list */ 
    40     node_init(); 
     39        /* Initialize the linked list */ 
     40        node_init(); 
    4141 
    42          // Initialize the web server 
    43     debug(D_LOG_DEBUG, "Creating web server on %s:%d", config.gw_address, config.gw_port); 
    44          webserver = httpdCreate(config.gw_address, config.gw_port); 
    45          if (webserver == NULL) { 
     42        // Initialize the web server 
     43        debug(D_LOG_DEBUG, "Creating web server on %s:%d",  
     44                        config.gw_address, config.gw_port); 
     45        webserver = httpdCreate(config.gw_address, config.gw_port); 
     46        if (webserver == NULL) { 
    4647                debug(D_LOG_ERR, "Could not create web server"); 
    4748                exit(1); 
    48          } 
    49     debug(D_LOG_DEBUG, "Assigning callbacks to web server"); 
    50          httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about); 
    51          httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth); 
    52          httpdAddC404Content(webserver, http_callback_404); 
     49        } 
     50        debug(D_LOG_DEBUG, "Assigning callbacks to web server"); 
     51        httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, 
     52                        http_callback_about); 
     53        httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, 
     54                        http_callback_auth); 
     55        httpdAddC404Content(webserver, http_callback_404); 
    5356 
    54          // Init the signals to catch chld/quit/etc 
    55          init_signals(); 
     57        // Init the signals to catch chld/quit/etc 
     58        init_signals(); 
    5659 
    57          // Reset the firewall 
    58     fw_init(); 
     60        // Reset the firewall 
     61        fw_init(); 
    5962 
    60     last_checked = time(NULL); 
     63        last_checked = time(NULL); 
    6164 
    62     debug(D_LOG_DEBUG, "Waiting for connections"); 
    63     while(1) { 
    64                  tv.tv_sec = config.checkinterval; 
    65                  tv.tv_usec = 0; 
    66                   result = httpdGetConnection(webserver, &tv); 
    67                   if (result < 0) { 
    68                           /* 
    69                                 * fixme 
    70                                 * An error occurred - should we abort? reboot the device ? 
    71                                 */ 
    72                                 debug(D_LOG_ERR, "httpdGetConnection returned %d", result); 
    73                                 exit(1); 
    74                   } 
    75                   else if (result > 0) { 
    76                           /* 
    77                                 * We got a connection 
    78                                 */ 
    79                                  debug(D_LOG_DEBUG, "Received connection from %s", webserver->clientAddr); 
    80                           if (httpdReadRequest(webserver) >=0) { 
    81                                   /* 
    82                                         * We read the request fine 
    83                                         */ 
    84                                  debug(D_LOG_DEBUG, "Processing request from %s", webserver->clientAddr); 
    85                                   httpdProcessRequest(webserver); 
    86                           } 
    87                           else { 
    88                                 debug(D_LOG_ERR, "No valid request received from %s", webserver->clientAddr); 
    89                   } 
    90                          debug(D_LOG_DEBUG, "Closing connection with %s", webserver->clientAddr); 
    91                           httpdEndRequest(webserver); 
    92                   } 
     65        debug(D_LOG_DEBUG, "Waiting for connections"); 
     66        while(1) { 
     67                tv.tv_sec = config.checkinterval; 
     68                tv.tv_usec = 0; 
     69                result = httpdGetConnection(webserver, &tv); 
     70                if (result < 0) { 
     71                        /* 
     72                         * fixme 
     73                         * An error occurred - should we abort? reboot the device ? 
     74                         */ 
     75                        debug(D_LOG_ERR, "httpdGetConnection returned %d", 
     76                                result); 
     77                        exit(1); 
     78                } else if (result > 0) { 
     79                        /* 
     80                         * We got a connection 
     81                         */ 
     82                        debug(D_LOG_DEBUG, "Received connection from %s", 
     83                                webserver->clientAddr); 
     84                        if (httpdReadRequest(webserver) >=0) { 
     85                                /* 
     86                                 * We read the request fine 
     87                                 */ 
     88                                debug(D_LOG_DEBUG, "Processing request from " 
     89                                        "%s", webserver->clientAddr); 
     90                                httpdProcessRequest(webserver); 
     91                        } 
     92                        else { 
     93                                debug(D_LOG_ERR, "No valid request received " 
     94                                        "from %s", webserver->clientAddr); 
     95                        } 
     96                        debug(D_LOG_DEBUG, "Closing connection with %s", 
     97                                webserver->clientAddr); 
     98                        httpdEndRequest(webserver); 
     99                } 
    93100 
    94         if (time(NULL) - last_checked > config.checkinterval) { 
    95             fw_counter(); 
    96             last_checked = time(NULL); 
    97         } 
    98     } 
     101                if (time(NULL) - last_checked > config.checkinterval) { 
     102                        fw_counter(); 
     103                        last_checked = time(NULL); 
     104                } 
     105        } 
    99106 
    100     fw_destroy(); 
     107        fw_destroy(); 
    101108} 
    102109 
    103 int 
     110        int 
    104111main(int argc, char **argv) 
    105112{ 
    106     config_init(); 
     113        config_init(); 
    107114 
    108     parse_commandline(argc, argv); 
     115        parse_commandline(argc, argv); 
    109116 
    110     config_read(config.configfile); 
    111     config_validate(); 
     117        config_read(config.configfile); 
     118        config_validate(); 
    112119 
    113     if (config.daemon) { 
    114         struct sigaction sa; 
    115         int childPid; 
     120        if (config.daemon) { 
     121                struct sigaction sa; 
     122                int childPid; 
    116123 
    117         debug(D_LOG_INFO, "Forking into background"); 
     124                debug(D_LOG_INFO, "Forking into background"); 
    118125 
    119         sa.sa_handler = sigchld_handler; 
    120         sigemptyset(&sa.sa_mask); 
    121         sa.sa_flags = SA_RESTART; 
    122         if (sigaction(SIGCHLD, &sa, NULL) == -1) { 
    123             debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    124             exit(1); 
    125         } 
     126                sa.sa_handler = sigchld_handler; 
     127                sigemptyset(&sa.sa_mask); 
     128                sa.sa_flags = SA_RESTART; 
     129                if (sigaction(SIGCHLD, &sa, NULL) == -1) { 
     130                        debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     131                        exit(1); 
     132                } 
    126133 
    127         switch((childPid = fork())) { 
    128             case -1: /* error */ 
    129                 debug(D_LOG_ERR, "fork(): %s", strerror(errno)); 
    130                 exit(1); 
    131                 break; 
     134                switch((childPid = fork())) { 
     135                case -1: /* error */ 
     136                        debug(D_LOG_ERR, "fork(): %s", strerror(errno)); 
     137                        exit(1); 
     138                        break; 
    132139 
    133             case 0: /* parent */ 
    134                 main_loop(); 
    135                 break; 
     140                case 0: /* parent */ 
     141                        main_loop(); 
     142                        break; 
    136143 
    137             default: /* child */ 
    138                 exit(0); 
    139                 break; 
    140         } 
    141     } else { 
    142         main_loop(); 
    143     } 
     144                default: /* child */ 
     145                        exit(0); 
     146                        break; 
     147                } 
     148        } else { 
     149                main_loop(); 
     150        } 
    144151 
    145     return(0); 
     152        return(0); 
    146153} 
    147154 
    148155void termination_handler(int s) 
    149156{ 
    150     fw_destroy(); 
     157        fw_destroy(); 
    151158 
    152     debug(D_LOG_INFO, "Exiting..."); 
    153     exit(0); 
     159        debug(D_LOG_INFO, "Exiting..."); 
     160        exit(0); 
    154161} 
    155162 
    156163void init_signals() 
    157164{ 
    158     struct sigaction sa; 
     165        struct sigaction sa; 
    159166 
    160     sa.sa_handler = sigchld_handler; 
    161     sigemptyset(&sa.sa_mask); 
    162     sa.sa_flags = SA_RESTART; 
    163     if (sigaction(SIGCHLD, &sa, NULL) == -1) { 
    164         debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    165         exit(1); 
    166     } 
     167        sa.sa_handler = sigchld_handler; 
     168        sigemptyset(&sa.sa_mask); 
     169        sa.sa_flags = SA_RESTART; 
     170        if (sigaction(SIGCHLD, &sa, NULL) == -1) { 
     171                debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     172                exit(1); 
     173        } 
    167174 
    168     sa.sa_handler = termination_handler; 
    169     sigemptyset(&sa.sa_mask); 
    170     sa.sa_flags = SA_RESTART; 
     175        sa.sa_handler = termination_handler; 
     176        sigemptyset(&sa.sa_mask); 
     177        sa.sa_flags = SA_RESTART; 
    171178 
    172     /* Trap SIGTERM */ 
    173     if (sigaction(SIGTERM, &sa, NULL) == -1) { 
    174         debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    175         exit(1); 
    176     } 
     179        /* Trap SIGTERM */ 
     180        if (sigaction(SIGTERM, &sa, NULL) == -1) { 
     181                debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     182                exit(1); 
     183        } 
    177184 
    178     /* Trap SIGQUIT */ 
    179     if (sigaction(SIGQUIT, &sa, NULL) == -1) { 
    180         debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    181         exit(1); 
    182     } 
     185        /* Trap SIGQUIT */ 
     186        if (sigaction(SIGQUIT, &sa, NULL) == -1) { 
     187                debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     188                exit(1); 
     189        } 
    183190 
    184     /* Trap SIGINT */ 
    185     if (sigaction(SIGINT, &sa, NULL) == -1) { 
    186         debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
    187         exit(1); 
    188     } 
     191        /* Trap SIGINT */ 
     192        if (sigaction(SIGINT, &sa, NULL) == -1) { 
     193                debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); 
     194                exit(1); 
     195        } 
    189196} 
    190197