Changeset 18

Show
Ignore:
Timestamp:
03/13/04 00:18:22 (9 years ago)
Author:
aprilp
Message:

* Modified the way firewall scripts are called so we can configure
them in the config file (a bit more modular than it was)
* Added simple linked list to keep track of clients and to
keep a counter of the utilization and send it to the auth
server
* Fixed CRLF/formatting in phpauth/auth/index.php
* Hacked phpauth/auth/index.php to handle very basic utilization tracking

Location:
trunk/wifidog
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog/ChangeLog

    r17 r18  
    11# $Header$ 
     2 
     32004-03-13  Philippe April <papril777@yahoo.com> 
     4    * Modified the way firewall scripts are called so we can configure 
     5    them in the config file (a bit more modular than it was) 
     6    * Added simple linked list to keep track of clients and to 
     7    keep a counter of the utilization and send it to the auth 
     8    server 
     9    * Fixed CRLF/formatting in phpauth/auth/index.php 
     10    * Hacked phpauth/auth/index.php to handle very basic utilization tracking 
    211 
    3122004-03-12  Philippe April <papril777@yahoo.com> 
  • trunk/wifidog/src/common.h

    r17 r18  
    3232#include <sys/types.h> 
    3333#include <sys/socket.h> 
     34#include <sys/stat.h> 
    3435#include <netinet/in.h> 
    3536#include <arpa/inet.h> 
     
    5354#define MAX_BUF 4096 
    5455 
     56#define SCRIPT_FWINIT       "fw.init" 
     57#define SCRIPT_FWACCESS     "fw.access" 
     58#define SCRIPT_FWDESTROY    "fw.destroy" 
     59#define SCRIPT_FWCOUNTERS   "fw.counters" 
     60 
    5561#endif /* _COMMON_H_ */ 
  • trunk/wifidog/src/conf.c

    r9 r18  
    3636#define DEFAULT_CLIENTTIMEOUT 5 
    3737#define DEFAULT_CHECKINTERVAL 5 
     38#define DEFAULT_FWSCRIPTS_PATH "." 
     39#define DEFAULT_FWTYPE "." 
    3840 
    3941s_config config; 
     
    5658    oClientTimeout, 
    5759    oCheckInterval, 
     60    oFWScriptsPath, 
     61    oFWType, 
    5862} OpCodes; 
    5963 
     
    7781        { "clienttimeout",      oClientTimeout }, 
    7882    { "checkinterval",      oCheckInterval }, 
     83    { "fwscriptspath",      oFWScriptsPath }, 
     84    { "fwtype",             oFWType }, 
    7985    { NULL,                 oBadOption }, 
    8086}; 
     
    100106    config.clienttimeout = DEFAULT_CLIENTTIMEOUT; 
    101107    config.checkinterval = DEFAULT_CHECKINTERVAL; 
     108    config.fwscripts_path = DEFAULT_FWSCRIPTS_PATH; 
     109    config.fwtype = DEFAULT_FWTYPE; 
    102110} 
    103111 
  • trunk/wifidog/src/conf.h

    r9 r18  
    5151    int clienttimeout; 
    5252    int checkinterval; 
     53    char *fwscripts_path; 
     54    char *fwtype; 
    5355} s_config; 
    5456 
  • trunk/wifidog/src/firewall.c

    r17 r18  
    3030extern s_config config; 
    3131 
     32t_node list; 
     33t_node *firstnode; 
     34t_node *curnode; 
     35 
    3236int 
    3337fw_allow(char *ip, char *mac, int profile) 
    3438{ 
    35     char buf[MAX_BUF]; 
    36     char *command[] = {"./fw.access", "allow", ip, mac, buf, NULL}; 
    37  
    38     sprintf(buf, "%d", profile); 
     39    char s_profile[16]; 
     40    char script[MAX_BUF]; 
     41    struct stat st; 
     42    char *command[] = {script, "allow", ip, mac, s_profile, NULL}; 
     43 
     44    sprintf(s_profile, "%-10d", profile); 
     45    sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWACCESS); 
     46 
     47    if (-1 == (stat(script, &st))) { 
     48        debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
     49        return(1); 
     50    } 
    3951 
    4052    return(execute(command)); 
     
    4456fw_deny(char *ip, char *mac, int profile) 
    4557{ 
    46     char buf[MAX_BUF]; 
    47     char *command[] = {"./fw.access", "deny", ip, mac, buf, NULL}; 
    48  
    49     sprintf(buf, "%d", profile); 
     58    char s_profile[16]; 
     59    char script[MAX_BUF]; 
     60    struct stat st; 
     61    char *command[] = {script, "deny", ip, mac, s_profile, NULL}; 
     62 
     63    sprintf(s_profile, "%-10d", profile); 
     64    sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWACCESS); 
     65 
     66    if (-1 == (stat(script, &st))) { 
     67        debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 
     68        return(1); 
     69    } 
    5070 
    5171    return(execute(command)); 
     
    104124fw_init(void) 
    105125{ 
    106     char port[255]; 
    107     char *command[] = {"./fw.init", config.gw_interface, config.gw_address, port, config.authserv_hostname, NULL}; 
    108  
    109     sprintf(port, "%d", config.gw_port); 
    110  
     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    } 
    111140 
    112141    debug(D_LOG_INFO, "Setting firewall rules"); 
    113142 
    114     if (execute(command) != 0) { 
     143    if ((rc = execute(command)) != 0) { 
    115144        debug(D_LOG_ERR, "Could not setup firewall, exiting..."); 
    116145        exit(1); 
    117146    } 
    118147 
    119     return(0); 
     148    return(rc); 
    120149} 
    121150 
     
    123152fw_destroy(void) 
    124153{ 
    125     char *command[] = {"./fw.destroy", NULL}; 
     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    } 
    126164 
    127165    debug(D_LOG_INFO, "Flushing firewall rules"); 
    128     execute(command); 
    129  
    130     return(0); 
     166 
     167    return(execute(command)); 
    131168} 
    132169 
     
    138175    int profile, rc; 
    139176    char ip[255], mac[255]; 
    140  
    141     if (!(output = popen("./fw.counters", "r"))) { 
     177    char script[MAX_BUF]; 
     178    t_node *p1; 
     179 
     180    sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWCOUNTERS); 
     181 
     182    if (!(output = popen(script, "r"))) { 
    142183        debug(D_LOG_ERR, "popen(): %s", strerror(errno)); 
    143     } 
    144     while (!(feof(output)) && output) { 
    145         rc = fscanf(output, "%ld %s %s %d", &counter, ip, mac, &profile); 
    146         if (rc == 4 && rc != EOF) { 
    147  
    148             /* TODO Update the counter onthe auth server */ 
    149             /* but to do that we will need to keep track of the */ 
    150             /* token to associate it with the session */ 
    151              
    152             /* TODO If the client is not active for x seconds */ 
    153             /* timeout the client and destroy token */ 
    154             debug(D_LOG_DEBUG, "Counter for %s: %ld bytes", ip, counter); 
     184    } else { 
     185        while (!(feof(output)) && output) { 
     186            rc = fscanf(output, "%ld %s %s %d", &counter, ip, mac, &profile); 
     187            if (rc == 4 && rc != EOF) { 
     188 
     189                /* TODO If the client is not active for x seconds 
     190                 * timeout the client and destroy token. 
     191                 * Maybe this should be done on the auth server*/ 
     192 
     193                p1 = node_find_by_ip(ip); 
     194                if (!(p1)) { 
     195                    debug(D_LOG_DEBUG, "Client %s not found in linked list", ip); 
     196                } else { 
     197                    p1->counter = counter; 
     198                    if ((profile = auth(p1->ip, p1->mac, p1->token, p1->counter)) == -1) { 
     199                        /* User has to be kicked out */ 
     200                    } 
     201                    debug(D_LOG_DEBUG, "Updated client %s counter to %ld bytes", ip, counter); 
     202                } 
     203            } 
    155204        } 
    156     } 
    157     pclose(output); 
    158 } 
    159  
     205        pclose(output); 
     206    } 
     207} 
     208 
     209void 
     210node_init(void) 
     211{ 
     212    firstnode = curnode = &list; 
     213    firstnode->next = NULL; 
     214} 
     215 
     216t_node * 
     217node_add(char *ip, char *mac, char *token, long int counter) 
     218{ 
     219    void *ptr; 
     220 
     221    ptr = curnode; 
     222 
     223    strcpy(curnode->ip, ip); 
     224    strcpy(curnode->mac, mac); 
     225    strcpy(curnode->token, token); 
     226    curnode->counter = 0; 
     227 
     228    curnode->next = (t_node *)malloc(sizeof(t_node)); 
     229    curnode = curnode->next; 
     230 
     231    debug(D_LOG_DEBUG, "Added a new node to linked list: IP: %s Token: %s", ip, token); 
     232 
     233    return ptr; 
     234} 
     235 
     236t_node * 
     237node_find_by_ip(char *ip) 
     238{ 
     239    t_node *ptr; 
     240 
     241    ptr = firstnode; 
     242    while (NULL != ptr->next) { 
     243        if (0 == strcmp(ptr->ip, ip)) 
     244            return ptr; 
     245        ptr = ptr->next; 
     246    }  
     247 
     248    return NULL; 
     249} 
     250 
     251t_node * 
     252node_find_by_token(char *token) 
     253{ 
     254    t_node *ptr; 
     255 
     256    ptr = firstnode; 
     257    while (NULL != ptr->next) { 
     258        if (0 == strcmp(ptr->token, token)) 
     259            return ptr; 
     260        ptr = ptr->next; 
     261    }  
     262 
     263    return NULL; 
     264} 
     265 
  • trunk/wifidog/src/firewall.h

    r9 r18  
    2828#define _FIREWALL_H_ 
    2929 
     30typedef struct { 
     31    void *next; 
     32    char ip[16]; 
     33    char mac[32]; 
     34    char token[33]; 
     35    long int counter; 
     36} t_node; 
     37 
    3038int fw_init(void); 
    3139int fw_destroy(void); 
     
    3644char *arp_get(char *req_ip); 
    3745 
     46void node_init(void); 
     47t_node *node_add(char *ip, char *mac, char *token, long int counter); 
     48t_node *node_find_by_ip(char *ip); 
     49t_node *node_find_by_token(char *token); 
     50 
    3851#endif /* _FIREWALL_H_ */ 
  • trunk/wifidog/src/gateway.c

    r17 r18  
    4242    int fdmax, i, cnt_last_check; 
    4343 
     44    /* Initialize the linked list */ 
     45    node_init(); 
     46 
    4447    FD_ZERO(&master); 
    4548    FD_ZERO(&read_fds); 
     
    110113 
    111114    fw_init(); 
    112     tv.tv_sec = config.checkinterval; 
    113115    last_checked = time(NULL); 
    114116 
    115117    while(1) { 
     118        tv.tv_sec = config.checkinterval; 
     119        tv.tv_usec = 0; 
    116120        read_fds = master; 
    117121        if (select(fdmax + 1, &read_fds, NULL, NULL, &tv) == -1) { 
  • trunk/wifidog/src/http.c

    r17 r18  
    9595                    if ((rc = fw_allow(ip, mac, profile)) == 0) { 
    9696                        http_body(body, "You %s at %s, have been granted profile %d!", ip, mac, profile); 
     97 
     98                        /* Add client's IP and token into a linked list so we can keep 
     99                         * track of it on the auth server, only if he's not there already */ 
     100                        if (!node_find_by_ip(ip)) { 
     101                            node_add(ip, mac, token, 0); 
     102                        } 
    97103                    } else { 
    98104                        http_body(body, "Authentication was succesful, but the firewall could not be modified, I got return code %d, please contact the systems administrators"); 
     
    213219        memset(&(their_addr.sin_zero), '\0', 8); 
    214220 
     221        debug(D_LOG_DEBUG, "Connecting to auth server %s on port %d", config.authserv_hostname, config.authserv_port); 
     222 
    215223        if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { 
    216224            debug(D_LOG_ERR, "connect(): %s", strerror(errno)); 
     
    218226        } 
    219227 
    220         sprintf(buf, "GET %s?ip=%s&mac=%s&token=%s&stats=%ld\n\n", config.authserv_path, ip, mac, token, stats); 
     228        sprintf(buf, "GET %s?ip=%s&mac=%s&token=%s&stats=%ld HTTP/1.1\nHost: %s\n\n", config.authserv_path, ip, mac, token, stats, config.authserv_hostname); 
    221229        sock_send(sockfd, buf); 
     230 
     231        debug(D_LOG_DEBUG, "Sending HTTP request:\n#####\n%s\n#####", buf); 
    222232         
    223233        if ((numbytes = recv(sockfd, buf, MAX_BUF - 1, 0)) == -1) { 
     
    232242        if ((p1 = strstr(buf, "Profile: "))) { 
    233243            if (sscanf(p1, "Profile: %d", &profile) == 1) { 
     244                debug(D_LOG_DEBUG, "Auth server returned profile %d", profile); 
    234245                return(profile); 
    235246            } else { 
     247                debug(D_LOG_DEBUG, "Auth server did not return expected information"); 
    236248                return(-1); 
    237249            }