Changeset 1305

Show
Ignore:
Timestamp:
11/01/07 16:04:20 (6 years ago)
Author:
benoitg
Message:
  • Apply portability patches by David Young <dyoung@…>. These have been reviewed, but not tested.
Location:
trunk/wifidog
Files:
17 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog/ChangeLog

    r1303 r1305  
    11# $Id$ 
     22007-11-01 Benoit Gr�goire  <bock@step.polymtl.ca> 
     3        * Apply portability patches by David Young <dyoung@pobox.com>.  These have been reviewed, but not tested. 
     4 
    252007-10-18 Benoit Gr�goire  <bock@step.polymtl.ca> 
    36        * fw_iptables.c: From Philippe April:  reverted change made in 1241 so we properly remove the entry from mangle.WiFiDog_Incoming when kicking out users, it was affecting statistics  
  • trunk/wifidog/libhttpd/api.c

    r1239 r1305  
    413413                        */ 
    414414                        cp = cp2 = buf; 
    415                         while(isalpha(*cp2)) 
     415                        while(isalpha((unsigned char)*cp2)) 
    416416                                cp2++; 
    417417                        *cp2 = 0; 
     
    871871                        cp = varName; 
    872872                        count2 = 0; 
    873                         while(*tmp&&(isalnum(*tmp)||*tmp == '_')&&count2 < 80) 
     873                        while (*tmp && (isalnum((unsigned char)*tmp) || *tmp == '_') && 
     874                               count2 < 80) 
    874875                        { 
    875876                                *cp++ = *tmp++; 
  • trunk/wifidog/libhttpd/httpd.h

    r1239 r1305  
    3232 
    3333#define LIB_HTTPD_H 1 
     34 
     35#include <sys/time.h> 
    3436 
    3537#if !defined(__ANSI_PROTO) 
  • trunk/wifidog/libhttpd/protocol.c

    r935 r1305  
    408408{ 
    409409        struct  tm *timePtr; 
    410  
    411         if (clock == 0) 
    412                 clock = time(NULL); 
    413         timePtr = gmtime((time_t*)&clock); 
     410        time_t t; 
     411 
     412        t = (clock == 0) ? time(NULL) : clock; 
     413        timePtr = gmtime(&t); 
    414414        strftime(ptr, HTTP_TIME_STRING_LEN,"%a, %d %b %Y %T GMT",timePtr); 
    415415} 
  • trunk/wifidog/src/auth.c

    r1243 r1305  
    9696        t_client        *client; 
    9797        t_authresponse  auth_response; 
    98         char    *ip, 
    99                 *mac, 
     98        char    *mac, 
    10099                *token; 
    101100        char *urlFragment = NULL; 
     
    108107 
    109108        if (client == NULL) { 
    110                 debug(LOG_ERR, "Could not find client for %s", ip); 
     109                debug(LOG_ERR, "Could not find client for %s", r->clientAddr); 
    111110                UNLOCK_CLIENT_LIST(); 
    112111                return; 
  • trunk/wifidog/src/auth.h

    r1104 r1305  
    2727#ifndef _AUTH_H_ 
    2828#define _AUTH_H_ 
     29 
     30#include "httpd.h" 
    2931 
    3032/** 
  • trunk/wifidog/src/centralserver.c

    r1243 r1305  
    2525 */ 
    2626 
     27#include <pthread.h> 
    2728#include <stdio.h> 
    2829#include <stdlib.h> 
     
    4647#include "debug.h" 
    4748#include "centralserver.h" 
     49#include "firewall.h" 
    4850#include "../config.h" 
    4951 
  • trunk/wifidog/src/centralserver.h

    r1243 r1305  
    2828#define _CENTRALSERVER_H_ 
    2929 
     30#include "auth.h" 
     31 
    3032/** @brief Ask the central server to login a client */ 
    3133#define REQUEST_TYPE_LOGIN     "login" 
  • trunk/wifidog/src/conf.c

    r1243 r1305  
    131131}; 
    132132 
     133static void config_notnull(void *parm, char *parmname); 
     134static int parse_boolean_value(char *); 
     135static void parse_auth_server(FILE *, char *, int *); 
     136static int _parse_firewall_rule(char *ruleset, char *leftover); 
     137static void parse_firewall_ruleset(char *, FILE *, char *, int *); 
     138 
    133139static OpCodes config_parse_token(const char *cp, const char *filename, int linenum); 
    134140 
     
    480486        /* lower case */ 
    481487        for (i = 0; *(leftover + i) != '\0' 
    482                         && (*(leftover + i) = tolower(*(leftover + i))); i++); 
     488                        && (*(leftover + i) = tolower((unsigned char)*(leftover + i))); i++); 
    483489         
    484490        token = leftover; 
     
    512518                TO_NEXT_WORD(leftover, finished); 
    513519                for (i = 0; *(port + i) != '\0'; i++) 
    514                         if (!isdigit(*(port + i))) 
     520                        if (!isdigit((unsigned char)*(port + i))) 
    515521                                all_nums = 0; /*< No longer only digits */ 
    516522                if (!all_nums) { 
     
    536542                all_nums = 1; 
    537543                for (i = 0; *(mask + i) != '\0'; i++) 
    538                         if (!isdigit(*(mask + i)) && (*(mask + i) != '.') 
     544                        if (!isdigit((unsigned char)*(mask + i)) && (*(mask + i) != '.') 
    539545                                        && (*(mask + i) != '/')) 
    540546                                all_nums = 0; /*< No longer only digits */ 
  • trunk/wifidog/src/conf.h

    r1243 r1305  
    174174t_firewall_rule *get_ruleset(char *); 
    175175 
    176 static void config_notnull(void *parm, char *parmname); 
    177 static int parse_boolean_value(char *); 
    178 static void parse_auth_server(FILE *, char *, int *); 
    179 static int _parse_firewall_rule(char *ruleset, char *leftover); 
    180 static void parse_firewall_ruleset(char *, FILE *, char *, int *); 
    181176void parse_trusted_mac_list(char *); 
    182177 
  • trunk/wifidog/src/debug.c

    r901 r1305  
    3030#include <stdarg.h> 
    3131#include <time.h> 
     32#include <unistd.h> 
    3233 
    3334#include "conf.h" 
  • trunk/wifidog/src/firewall.c

    r1243 r1305  
    5858#endif 
    5959 
     60#if defined(__NetBSD__) 
     61#include <netinet/in_systm.h> 
     62#include <netinet/ip.h> 
     63#include <netinet/ip_icmp.h> 
     64#endif 
     65 
    6066#include "httpd.h" 
    6167#include "safe.h" 
     
    106112} 
    107113 
     114/* XXX DCY */ 
    108115/** 
    109116 * Get an IP's MAC address from the ARP cache. 
     
    157164             setsockopt(icmp_fd, SOL_SOCKET, SO_DONTROUTE, &zeroopt, sizeof(zeroopt)) == -1) { 
    158165        debug(LOG_ERR, "Cannot create ICMP raw socket."); 
    159         return; 
     166        return 0; 
    160167    } 
    161168 
     
    342349} 
    343350 
    344 void icmp_ping(char *host) { 
    345   struct sockaddr_in saddr; 
    346 #ifdef __linux__ 
    347   struct {  
    348     struct ip ip; 
    349     struct icmp icmp; 
    350   } packet; 
     351void 
     352icmp_ping(char *host) 
     353{ 
     354        struct sockaddr_in saddr; 
     355#if defined(__linux__) || defined(__NetBSD__) 
     356        struct {  
     357                struct ip ip; 
     358                struct icmp icmp; 
     359        } packet; 
    351360#endif 
    352   unsigned int i, j; 
    353   int opt = 2000; 
    354   unsigned short id = rand16(); 
    355  
    356   saddr.sin_family = AF_INET; 
    357   saddr.sin_port = 0; 
    358   inet_aton(host, &saddr.sin_addr); 
    359 #ifdef HAVE_SOCKADDR_SA_LEN 
    360   saddr.sin_len = sizeof(struct sockaddr_in); 
     361        unsigned int i, j; 
     362        int opt = 2000; 
     363        unsigned short id = rand16(); 
     364 
     365        memset(&saddr, 0, sizeof(saddr)); 
     366        saddr.sin_family = AF_INET; 
     367        inet_aton(host, &saddr.sin_addr); 
     368#if defined(HAVE_SOCKADDR_SA_LEN) || defined(__NetBSD__) 
     369        saddr.sin_len = sizeof(struct sockaddr_in); 
    361370#endif 
    362371 
    363   memset(&(saddr.sin_zero), '\0', sizeof(saddr.sin_zero)); 
    364  
    365 #ifdef __linux__ 
    366   memset(&packet.icmp, 0, sizeof(packet.icmp)); 
    367   packet.icmp.icmp_type = ICMP_ECHO; 
    368   packet.icmp.icmp_id = id; 
    369   for (j = 0, i = 0; i < sizeof(struct icmp) / 2; i++) 
    370     j += ((unsigned short *)&packet.icmp)[i]; 
    371   while (j>>16) 
    372     j = (j & 0xffff) + (j >> 16);   
    373   packet.icmp.icmp_cksum = (j == 0xffff) ? j : ~j; 
    374  
    375   if (setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == -1) { 
    376       debug(LOG_ERR, "setsockopt(): %s", strerror(errno)); 
    377   } 
    378   if (sendto(icmp_fd, (char *)&packet.icmp, sizeof(struct icmp), 0, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) { 
    379       debug(LOG_ERR, "sendto(): %s", strerror(errno)); 
    380   } 
    381   opt = 1; 
    382   if (setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == -1) { 
    383       debug(LOG_ERR, "setsockopt(): %s", strerror(errno)); 
    384   } 
     372#if defined(__linux__) || defined(__NetBSD__) 
     373        memset(&packet.icmp, 0, sizeof(packet.icmp)); 
     374        packet.icmp.icmp_type = ICMP_ECHO; 
     375        packet.icmp.icmp_id = id; 
     376 
     377        for (j = 0, i = 0; i < sizeof(struct icmp) / 2; i++) 
     378                j += ((unsigned short *)&packet.icmp)[i]; 
     379 
     380        while (j >> 16) 
     381                j = (j & 0xffff) + (j >> 16);   
     382 
     383        packet.icmp.icmp_cksum = (j == 0xffff) ? j : ~j; 
     384 
     385        if (setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == -1) 
     386                debug(LOG_ERR, "setsockopt(): %s", strerror(errno)); 
     387 
     388        if (sendto(icmp_fd, (char *)&packet.icmp, sizeof(struct icmp), 0, 
     389                   (const struct sockaddr *)&saddr, sizeof(saddr)) == -1) 
     390                debug(LOG_ERR, "sendto(): %s", strerror(errno)); 
     391 
     392        opt = 1; 
     393        if (setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == -1) 
     394                debug(LOG_ERR, "setsockopt(): %s", strerror(errno)); 
    385395#endif 
    386396 
    387   return; 
     397        return; 
    388398} 
    389399 
     
    392402 
    393403  if (!been_seeded) { 
    394     int fd, n = 0; 
    395     unsigned int c = 0, seed = 0; 
    396     char sbuf[sizeof(seed)]; 
    397     char *s; 
     404    unsigned int seed = 0; 
    398405    struct timeval now; 
    399406 
  • trunk/wifidog/src/fw_iptables.c

    r1302 r1305  
    207207         UNLOCK_CONFIG(); 
    208208     
     209        if (ext_interface == NULL) { 
     210                debug(LOG_ERR, "FATAL: no external interface"); 
     211                /* XXX leaks safe_strdup()'d strings */ 
     212                return 0; 
     213        } 
    209214         /* 
    210215          * 
  • trunk/wifidog/src/gateway.c

    r1104 r1305  
    270270{ 
    271271        static  pthread_mutex_t sigterm_mutex = PTHREAD_MUTEX_INITIALIZER; 
    272         s_config *config = config_get_config(); 
    273272 
    274273        debug(LOG_INFO, "Handler for termination caught signal %d", s); 
     
    368367        request *r; 
    369368        void **params; 
    370     FILE *fh; 
    371369 
    372370    /* Set the time when wifidog started */ 
     
    420418        fw_destroy(); 
    421419        /* Then initialize it */ 
    422         fw_init(); 
     420        if (!fw_init()) { 
     421                debug(LOG_ERR, "FATAL: Failed to initialize firewall"); 
     422                exit(1); 
     423        } 
    423424 
    424425        /* Start clean up thread */ 
  • trunk/wifidog/src/ping_thread.c

    r1243 r1305  
    5151#include "ping_thread.h" 
    5252#include "util.h" 
     53#include "centralserver.h" 
    5354 
    5455static void ping(void); 
  • trunk/wifidog/src/util.c

    r1243 r1305  
    4242#include <sys/ioctl.h> 
    4343 
     44#if defined(__NetBSD__) 
     45#include <arpa/inet.h> 
     46#include <sys/socket.h> 
     47#include <ifaddrs.h> 
     48#include <net/if.h> 
     49#include <net/if_dl.h> 
     50#include <util.h> 
     51#endif 
     52 
    4453#ifdef __linux__ 
    4554#include <net/if.h> 
     
    145154} 
    146155 
    147 char *get_iface_ip(char *ifname) { 
    148 #ifdef __linux__ 
    149     struct ifreq if_data; 
     156char * 
     157get_iface_ip(char *ifname) 
     158{ 
     159#if defined(__linux__) 
     160        struct ifreq if_data; 
     161        struct in_addr in; 
     162        char *ip_str; 
     163        int sockd; 
     164        u_int32_t ip; 
     165 
     166        /* Create a socket */ 
     167        if ((sockd = socket (AF_INET, SOCK_PACKET, htons(0x8086))) < 0) { 
     168                debug(LOG_ERR, "socket(): %s", strerror(errno)); 
     169                return NULL; 
     170        } 
     171 
     172        /* Get IP of internal interface */ 
     173        strcpy (if_data.ifr_name, ifname); 
     174 
     175        /* Get the IP address */ 
     176        if (ioctl (sockd, SIOCGIFADDR, &if_data) < 0) { 
     177                debug(LOG_ERR, "ioctl(): SIOCGIFADDR %s", strerror(errno)); 
     178                return NULL; 
     179        } 
     180        memcpy ((void *) &ip, (void *) &if_data.ifr_addr.sa_data + 2, 4); 
     181        in.s_addr = ip; 
     182 
     183        ip_str = (char *)inet_ntoa(in); 
     184        close(sockd); 
     185        return safe_strdup(ip_str); 
     186#elif defined(__NetBSD__) 
     187        struct ifaddrs *ifa, *ifap; 
     188        char *str = NULL; 
     189 
     190        if (getifaddrs(&ifap) == -1) { 
     191                debug(LOG_ERR, "getifaddrs(): %s", strerror(errno)); 
     192                return NULL; 
     193        } 
     194        /* XXX arbitrarily pick the first IPv4 address */ 
     195        for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { 
     196                if (strcmp(ifa->ifa_name, ifname) == 0 && 
     197                    ifa->ifa_addr->sa_family == AF_INET) 
     198                        break; 
     199        } 
     200        if (ifa == NULL) { 
     201                debug(LOG_ERR, "%s: no IPv4 address assigned"); 
     202                goto out; 
     203        } 
     204        str = safe_strdup(inet_ntoa( 
     205            ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr)); 
     206out: 
     207        freeifaddrs(ifap); 
     208        return str; 
     209#else 
     210        return safe_strdup("0.0.0.0"); 
    150211#endif 
    151     struct in_addr in; 
    152     char *ip_str; 
    153     int sockd; 
    154     u_int32_t ip; 
    155  
    156 #ifdef __linux__ 
    157      
    158     /* Create a socket */ 
    159     if ((sockd = socket (AF_INET, SOCK_PACKET, htons(0x8086))) < 0) { 
    160         debug(LOG_ERR, "socket(): %s", strerror(errno)); 
    161         return NULL; 
    162     } 
    163  
    164     /* Get IP of internal interface */ 
    165     strcpy (if_data.ifr_name, ifname); 
    166  
    167     /* Get the IP address */ 
    168     if (ioctl (sockd, SIOCGIFADDR, &if_data) < 0) { 
    169         debug(LOG_ERR, "ioctl(): SIOCGIFADDR %s", strerror(errno)); 
    170         return NULL; 
    171     } 
    172     memcpy ((void *) &ip, (void *) &if_data.ifr_addr.sa_data + 2, 4); 
    173     in.s_addr = ip; 
    174  
    175     ip_str = (char *)inet_ntoa(in); 
    176     close(sockd); 
    177     return safe_strdup(ip_str); 
    178 #else 
    179     return safe_strdup("0.0.0.0"); 
    180 #endif 
    181 } 
    182  
    183 char *get_iface_mac (char *ifname) { 
    184 #ifdef __linux__ 
     212} 
     213 
     214char * 
     215get_iface_mac(char *ifname) 
     216{ 
     217#if defined(__linux__) 
    185218    int r, s; 
    186219    struct ifreq ifr; 
     
    203236 
    204237    hwaddr = ifr.ifr_hwaddr.sa_data; 
    205     snprintf(mac, 13, "%02X%02X%02X%02X%02X%02X",  
     238    close(s); 
     239    snprintf(mac, sizeof(mac), "%02X%02X%02X%02X%02X%02X",  
    206240       hwaddr[0] & 0xFF, 
    207241       hwaddr[1] & 0xFF, 
     
    212246       ); 
    213247        
    214     close(s); 
    215248    return safe_strdup(mac); 
     249#elif defined(__NetBSD__) 
     250        struct ifaddrs *ifa, *ifap; 
     251        const char *hwaddr; 
     252        char mac[13], *str = NULL; 
     253        struct sockaddr_dl *sdl; 
     254 
     255        if (getifaddrs(&ifap) == -1) { 
     256                debug(LOG_ERR, "getifaddrs(): %s", strerror(errno)); 
     257                return NULL; 
     258        } 
     259        for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { 
     260                if (strcmp(ifa->ifa_name, ifname) == 0 && 
     261                    ifa->ifa_addr->sa_family == AF_LINK) 
     262                        break; 
     263        } 
     264        if (ifa == NULL) { 
     265                debug(LOG_ERR, "%s: no link-layer address assigned"); 
     266                goto out; 
     267        } 
     268        sdl = (struct sockaddr_dl *)ifa->ifa_addr; 
     269        hwaddr = LLADDR(sdl); 
     270        snprintf(mac, sizeof(mac), "%02X%02X%02X%02X%02X%02X", 
     271            hwaddr[0] & 0xFF, hwaddr[1] & 0xFF, 
     272            hwaddr[2] & 0xFF, hwaddr[3] & 0xFF, 
     273            hwaddr[4] & 0xFF, hwaddr[5] & 0xFF); 
     274 
     275        str = safe_strdup(mac); 
     276out: 
     277        freeifaddrs(ifap); 
     278        return str; 
    216279#else 
    217280    return NULL; 
     
    219282} 
    220283 
    221 char *get_ext_iface (void) { 
     284char * 
     285get_ext_iface(void) 
     286{ 
    222287#ifdef __linux__ 
    223288    FILE *input; 
     
    234299        input = fopen("/proc/net/route", "r"); 
    235300        while (!feof(input)) { 
     301            /* XXX scanf(3) is unsafe, risks overrun */  
    236302            fscanf(input, "%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n", device, gw); 
    237303            if (strcmp(gw, "00000000") == 0) { 
  • trunk/wifidog/src/wdctl_thread.c

    r1241 r1305  
    5151#include "client_list.h" 
    5252#include "wdctl_thread.h" 
     53#include "gateway.h" 
     54#include "safe.h" 
    5355 
    5456/* Defined in clientlist.c */ 
     
    235237        char    *sock_name; 
    236238        struct  sockaddr_un     sa_un; 
    237         int result; 
    238239        s_config * conf = NULL; 
    239240        t_client * client = NULL;