Show
Ignore:
Timestamp:
04/10/04 20:01:30 (9 years ago)
Author:
alexcv
Message:

formatting

Files:
1 modified

Legend:

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

    r18 r39  
    1717 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   * 
    1818 *                                                                  * 
    19 \********************************************************************/ 
     19 \********************************************************************/ 
    2020 
    2121/* $Header$ */ 
    2222/** @internal 
    23     @file conf.c 
    24     @brief Config file parsing 
    25     @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> 
    26 */ 
     23  @file conf.c 
     24  @brief Config file parsing 
     25  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> 
     26 */ 
    2727#include "common.h" 
    2828 
     
    4545        oBadOption, 
    4646        oDaemon, 
    47     oDebugLevel, 
    48     oGatewayID, 
    49     oGatewayInterface, 
    50     oGatewayAddress, 
    51     oGatewayPort, 
    52     oAuthservHostname, 
    53     oAuthservPort, 
    54     oAuthservPath, 
    55     oAuthservLoginUrl, 
    56     oHTTPDMaxConn, 
    57     oHTTPDName, 
    58     oClientTimeout, 
    59     oCheckInterval, 
    60     oFWScriptsPath, 
    61     oFWType, 
     47        oDebugLevel, 
     48        oGatewayID, 
     49        oGatewayInterface, 
     50        oGatewayAddress, 
     51        oGatewayPort, 
     52        oAuthservHostname, 
     53        oAuthservPort, 
     54        oAuthservPath, 
     55        oAuthservLoginUrl, 
     56        oHTTPDMaxConn, 
     57        oHTTPDName, 
     58        oClientTimeout, 
     59        oCheckInterval, 
     60        oFWScriptsPath, 
     61        oFWType, 
    6262} OpCodes; 
    6363 
     
    6565        const char *name; 
    6666        OpCodes opcode; 
    67     int required; 
     67        int required; 
    6868} keywords[] = { 
    6969        { "daemon",             oDaemon }, 
     
    8080        { "httpdname",          oHTTPDName }, 
    8181        { "clienttimeout",      oClientTimeout }, 
    82     { "checkinterval",      oCheckInterval }, 
    83     { "fwscriptspath",      oFWScriptsPath }, 
    84     { "fwtype",             oFWType }, 
    85     { NULL,                 oBadOption }, 
     82        { "checkinterval",      oCheckInterval }, 
     83        { "fwscriptspath",      oFWScriptsPath }, 
     84        { "fwtype",             oFWType }, 
     85        { NULL,                 oBadOption }, 
    8686}; 
    8787 
     
    8989config_init(void) 
    9090{ 
    91     debug(D_LOG_DEBUG, "Setting default config parameters"); 
    92     config.configfile = (char *)malloc(255); 
    93     strcpy(config.configfile, DEFAULT_CONFIGFILE); 
    94     config.daemon = 1; 
    95     config.debuglevel = DEFAULT_DEBUGLEVEL; 
    96     config.httpdmaxconn = DEFAULT_HTTPDMAXCONN; 
    97     config.gw_id = DEFAULT_GATEWAYID; 
    98     config.gw_interface = NULL; 
    99     config.gw_address = NULL; 
    100     config.gw_port = DEFAULT_GATEWAYPORT; 
    101     config.authserv_hostname = NULL; 
    102     config.authserv_port = DEFAULT_AUTHSERVPORT; 
    103     config.authserv_path = NULL; 
    104     config.authserv_loginurl = NULL; 
    105     config.httpdname = NULL; 
    106     config.clienttimeout = DEFAULT_CLIENTTIMEOUT; 
    107     config.checkinterval = DEFAULT_CHECKINTERVAL; 
    108     config.fwscripts_path = DEFAULT_FWSCRIPTS_PATH; 
    109     config.fwtype = DEFAULT_FWTYPE; 
     91        debug(D_LOG_DEBUG, "Setting default config parameters"); 
     92        config.configfile = (char *)malloc(255); 
     93        strcpy(config.configfile, DEFAULT_CONFIGFILE); 
     94        config.daemon = 1; 
     95        config.debuglevel = DEFAULT_DEBUGLEVEL; 
     96        config.httpdmaxconn = DEFAULT_HTTPDMAXCONN; 
     97        config.gw_id = DEFAULT_GATEWAYID; 
     98        config.gw_interface = NULL; 
     99        config.gw_address = NULL; 
     100        config.gw_port = DEFAULT_GATEWAYPORT; 
     101        config.authserv_hostname = NULL; 
     102        config.authserv_port = DEFAULT_AUTHSERVPORT; 
     103        config.authserv_path = NULL; 
     104        config.authserv_loginurl = NULL; 
     105        config.httpdname = NULL; 
     106        config.clienttimeout = DEFAULT_CLIENTTIMEOUT; 
     107        config.checkinterval = DEFAULT_CHECKINTERVAL; 
     108        config.fwscripts_path = DEFAULT_FWSCRIPTS_PATH; 
     109        config.fwtype = DEFAULT_FWTYPE; 
    110110} 
    111111 
     
    117117        for (i = 0; keywords[i].name; i++) 
    118118                if (strcasecmp(cp, keywords[i].name) == 0) 
    119             return keywords[i].opcode; 
    120  
    121         debug(D_LOG_ERR, "%s: line %d: Bad configuration option: %s", filename, linenum, cp); 
     119                        return keywords[i].opcode; 
     120 
     121        debug(D_LOG_ERR, "%s: line %d: Bad configuration option: %s",  
     122                        filename, linenum, cp); 
    122123        return oBadOption; 
    123124} 
     
    126127config_read(char *filename) 
    127128{ 
    128     FILE *fd; 
    129     char line[MAX_BUF], *s, *p1, *p2; 
    130     int linenum = 0, opcode, value; 
    131  
    132     debug(D_LOG_INFO, "Reading configuration file '%s'", filename); 
    133  
    134     if (!(fd = fopen(filename, "r"))) { 
    135         debug(D_LOG_ERR, "Could not open configuration file '%s', exiting...", filename); 
    136         exit(1); 
    137     } 
    138  
    139     while (!feof(fd) && fgets(line, MAX_BUF, fd)) { 
    140             s = line; 
    141  
    142         if (s[strlen(s) - 1] == '\n') 
    143             s[strlen(s) - 1] = '\0'; 
    144  
    145         if ((p1 = strchr(s, ' '))) { 
    146             p1[0] = '\0'; 
    147         } else if ((p1 = strchr(s, '\t'))) { 
    148             p1[0] = '\0'; 
    149         } 
    150  
    151         if (p1) { 
    152             p1++; 
    153  
    154             if ((p2 = strchr(p1, ' '))) { 
    155                 p2[0] = '\0'; 
    156             } else if ((p2 = strstr(p1, "\r\n"))) { 
    157                 p2[0] = '\0'; 
    158             } else if ((p2 = strchr(p1, '\n'))) { 
    159                 p2[0] = '\0'; 
    160             } 
    161         } 
    162  
    163         if (p1 && p1[0] != '\0') { 
    164             /* Strip trailing spaces */ 
    165             /* Strip tailing spaces */ 
    166  
    167                 if ((strncmp(s, "#", 1)) != 0) { 
    168                 debug(D_LOG_DEBUG, "Parsing token: %s, value: %s", s, p1); 
    169                 opcode = parse_token(s, filename, linenum); 
    170  
    171                 switch(opcode) { 
    172                     case oDaemon: 
    173                         if (((value = parse_value(p1)) != -1)) { 
    174                             config.daemon = value; 
    175                         } 
    176                         break; 
    177                     case oGatewayID: 
    178                         config.gw_id = get_string(p1); 
    179                         break; 
    180                     case oGatewayInterface: 
    181                         config.gw_interface = get_string(p1); 
    182                         break; 
    183                     case oGatewayAddress: 
    184                         config.gw_address = get_string(p1); 
    185                         break; 
    186                     case oGatewayPort: 
    187                         sscanf(p1, "%d", &config.gw_port); 
    188                         break; 
    189                     case oAuthservHostname: 
    190                         config.authserv_hostname = get_string(p1); 
    191                         break; 
    192                     case oHTTPDName: 
    193                         config.httpdname = get_string(p1); 
    194                         break; 
    195                     case oHTTPDMaxConn: 
    196                         sscanf(p1, "%d", &config.httpdmaxconn); 
    197                         break; 
    198                     case oAuthservPath: 
    199                         config.authserv_path = get_string(p1); 
    200                         break; 
    201                     case oAuthservLoginUrl: 
    202                         config.authserv_loginurl = get_string(p1); 
    203                         break; 
    204                     case oBadOption: 
    205                         exit(-1); 
    206                         break; 
    207                     case oCheckInterval: 
    208                         sscanf(p1, "%d", &config.checkinterval); 
    209                         break; 
    210                     case oClientTimeout: 
    211                         sscanf(p1, "%d", &config.clienttimeout); 
    212                         break; 
    213                 } 
    214             } 
    215         } 
    216     } 
    217  
    218     fclose(fd); 
     129        FILE *fd; 
     130        char line[MAX_BUF], *s, *p1, *p2; 
     131        int linenum = 0, opcode, value; 
     132 
     133        debug(D_LOG_INFO, "Reading configuration file '%s'", filename); 
     134 
     135        if (!(fd = fopen(filename, "r"))) { 
     136                debug(D_LOG_ERR, "Could not open configuration file '%s', " 
     137                                "exiting...", filename); 
     138                exit(1); 
     139        } 
     140 
     141        while (!feof(fd) && fgets(line, MAX_BUF, fd)) { 
     142                s = line; 
     143 
     144                if (s[strlen(s) - 1] == '\n') 
     145                        s[strlen(s) - 1] = '\0'; 
     146 
     147                if ((p1 = strchr(s, ' '))) { 
     148                        p1[0] = '\0'; 
     149                } else if ((p1 = strchr(s, '\t'))) { 
     150                        p1[0] = '\0'; 
     151                } 
     152 
     153                if (p1) { 
     154                        p1++; 
     155 
     156                        if ((p2 = strchr(p1, ' '))) { 
     157                                p2[0] = '\0'; 
     158                        } else if ((p2 = strstr(p1, "\r\n"))) { 
     159                                p2[0] = '\0'; 
     160                        } else if ((p2 = strchr(p1, '\n'))) { 
     161                                p2[0] = '\0'; 
     162                        } 
     163                } 
     164 
     165                if (p1 && p1[0] != '\0') { 
     166                        /* Strip trailing spaces */ 
     167                        /* Strip tailing spaces */ 
     168 
     169                        if ((strncmp(s, "#", 1)) != 0) { 
     170                                debug(D_LOG_DEBUG, "Parsing token: %s, " 
     171                                                "value: %s", s, p1); 
     172                                opcode = parse_token(s, filename, linenum); 
     173 
     174                                switch(opcode) { 
     175                                case oDaemon: 
     176                                        if (((value = parse_value(p1)) != -1)) { 
     177                                                config.daemon = value; 
     178                                        } 
     179                                        break; 
     180                                case oGatewayID: 
     181                                        config.gw_id = get_string(p1); 
     182                                        break; 
     183                                case oGatewayInterface: 
     184                                        config.gw_interface = get_string(p1); 
     185                                        break; 
     186                                case oGatewayAddress: 
     187                                        config.gw_address = get_string(p1); 
     188                                        break; 
     189                                case oGatewayPort: 
     190                                        sscanf(p1, "%d", &config.gw_port); 
     191                                        break; 
     192                                case oAuthservHostname: 
     193                                        config.authserv_hostname =  
     194                                                get_string(p1); 
     195                                        break; 
     196                                case oHTTPDName: 
     197                                        config.httpdname = get_string(p1); 
     198                                        break; 
     199                                case oHTTPDMaxConn: 
     200                                        sscanf(p1, "%d", &config.httpdmaxconn); 
     201                                        break; 
     202                                case oAuthservPath: 
     203                                        config.authserv_path = get_string(p1); 
     204                                        break; 
     205                                case oAuthservLoginUrl: 
     206                                        config.authserv_loginurl =  
     207                                                get_string(p1); 
     208                                        break; 
     209                                case oBadOption: 
     210                                        exit(-1); 
     211                                        break; 
     212                                case oCheckInterval: 
     213                                        sscanf(p1, "%d", &config.checkinterval); 
     214                                        break; 
     215                                case oClientTimeout: 
     216                                        sscanf(p1, "%d", &config.clienttimeout); 
     217                                        break; 
     218                                } 
     219                        } 
     220                } 
     221        } 
     222 
     223        fclose(fd); 
    219224} 
    220225 
     
    222227parse_value(char *line) 
    223228{ 
    224     if (strcasecmp(line, "yes") == 0) { 
    225         return 1; 
    226     } 
    227     if (strcasecmp(line, "no") == 0) { 
    228         return 0; 
    229     } 
    230     if (strcmp(line, "1") == 0) { 
    231         return 1; 
    232     } 
    233     if (strcmp(line, "0") == 0) { 
    234         return 0; 
    235     } 
    236  
    237     return -1; 
    238 } 
    239  
    240 char * 
     229        if (strcasecmp(line, "yes") == 0) { 
     230                return 1; 
     231        } 
     232        if (strcasecmp(line, "no") == 0) { 
     233                return 0; 
     234        } 
     235        if (strcmp(line, "1") == 0) { 
     236                return 1; 
     237        } 
     238        if (strcmp(line, "0") == 0) { 
     239                return 0; 
     240        } 
     241 
     242        return -1; 
     243} 
     244 
     245        char * 
    241246get_string(char *ptr) 
    242247{ 
    243     char *buf; 
    244  
    245     buf = (char *)malloc(MAX_BUF); 
    246  
    247     strncpy(buf, ptr, MAX_BUF); 
    248     return buf; 
     248        char *buf; 
     249 
     250        buf = (char *)malloc(MAX_BUF); 
     251 
     252        strncpy(buf, ptr, MAX_BUF); 
     253        return buf; 
    249254} 
    250255 
     
    252257config_validate(void) 
    253258{ 
    254     config_notnull(config.gw_id, "GatewayID"); 
    255     config_notnull(config.gw_interface, "GatewayInterface"); 
    256     config_notnull(config.gw_address, "GatewayAddress"); 
    257     config_notnull(config.authserv_hostname, "AuthservHostname"); 
    258     config_notnull(config.authserv_path, "AuthservPath"); 
    259     config_notnull(config.authserv_loginurl, "AuthservLoginUrl"); 
    260  
    261     if (missing_parms) { 
    262         debug(D_LOG_ERR, "Configuration is not complete, exiting..."); 
    263         exit(-1); 
    264     } 
     259        config_notnull(config.gw_id, "GatewayID"); 
     260        config_notnull(config.gw_interface, "GatewayInterface"); 
     261        config_notnull(config.gw_address, "GatewayAddress"); 
     262        config_notnull(config.authserv_hostname, "AuthservHostname"); 
     263        config_notnull(config.authserv_path, "AuthservPath"); 
     264        config_notnull(config.authserv_loginurl, "AuthservLoginUrl"); 
     265 
     266        if (missing_parms) { 
     267                debug(D_LOG_ERR, "Configuration is not complete, exiting..."); 
     268                exit(-1); 
     269        } 
    265270} 
    266271 
     
    268273config_notnull(void *parm, char *parmname) 
    269274{ 
    270     if (parm == NULL) { 
    271         debug(D_LOG_ERR, "%s is not set", parmname); 
    272         missing_parms = 1; 
    273     } 
    274 } 
    275  
     275        if (parm == NULL) { 
     276                debug(D_LOG_ERR, "%s is not set", parmname); 
     277                missing_parms = 1; 
     278        } 
     279} 
     280