Annotation of embedaddon/freevrrpd/vrrp_conf.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (c) 2001,2002 Sebastien Petit <spe@bsdfr.org>
                      3:  *
                      4:  * Redistribution and use in source forms, with and without modification,
                      5:  * are permitted provided that the following conditions are met:
                      6:  * 1. Redistributions of source code must retain the above copyright notice,
                      7:  *    this list of conditions and the following disclaimer.
                      8:  * 2. Redistributions in binary form must reproduce the above copyright notice,
                      9:  *    this list of conditions and the following disclaimer in the documentation
                     10:  *    and/or other materials provided with the distribution. Obviously, it
                     11:  *    would be nice if you gave credit where credit is due but requiring it
                     12:  *    would be too onerous.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *      This product includes software developed by Sebastien Petit.
                     16:  * 4. Neither the name of its contributors may be used to endorse or promote
                     17:  *    products derived from this software without specific prior written
                     18:  *    permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  *
                     32:  * $Id: vrrp_conf.c,v 1.10 2004/04/02 11:16:46 rival Exp $
                     33:  */
                     34: 
                     35: #include <errno.h>
                     36: #include "vrrp_conf.h"
                     37: 
                     38: int 
                     39: vrrp_conf_ident_option_arg(char *chaine, char *option, char *arg)
                     40: {
                     41:        int             i = 0;
                     42:        char           *ptr;
                     43: 
                     44:        while (isalpha(chaine[i]) && chaine[i] != 0) {
                     45:                i++;
                     46:                if (i > 1022) {
                     47:                        syslog(LOG_ERR, "a bad line was found in your configuration file (line > 1024 char), exiting...");
                     48:                        exit(-1);
                     49:                }
                     50:        }
                     51:        if (!i) {
                     52:                syslog(LOG_ERR, "a bad line was found in your configuration file: %s\n", chaine);
                     53:                exit(-1);
                     54:        }
                     55:        strncpy(option, chaine, i);
                     56:        option[i] = '\0';
                     57:        while (chaine[i] != '=' && chaine[i] != 0) {
                     58:                i++;
                     59:                if (i > 1021) {
                     60:                        syslog(LOG_ERR, "a bad line was found in your configuration file (line > 1024 char), exiting...");
                     61:                        exit(-1);
                     62:                }
                     63:        }
                     64:        i++;
                     65:        while (chaine[i] == ' ' && chaine[i] != 0) {
                     66:                i++;
                     67:                if (i > 1021) {
                     68:                        syslog(LOG_ERR, "a bad line was found in your configuration file (line > 1024 char), exiting...");
                     69:                        exit(-1);
                     70:                }
                     71:        }
                     72:        ptr = &chaine[i];
                     73:        strncpy(arg, ptr, strlen(chaine) - i);
                     74:        if (arg[strlen(chaine) - i - 1] == '\n')
                     75:                i++;
                     76:        arg[strlen(chaine) - i] = '\0';
                     77: 
                     78:        return 0;
                     79: }
                     80: 
                     81: char          **
                     82: vrrp_conf_split_args(char *args, char delimiter)
                     83: {
                     84:        char          **tabargs;
                     85:        int             i, j, nbargs = 0;
                     86:        char           *ptr;
                     87: 
                     88:        tabargs = (char **)malloc(sizeof(char *) * VRRP_CONF_MAX_ARGS);
                     89:        bzero(tabargs, sizeof(char **) * VRRP_CONF_MAX_ARGS);
                     90:        if (!tabargs) {
                     91:                syslog(LOG_ERR, "cannot malloc memory : %s", strerror(errno));
                     92:                exit(EXIT_FAILURE);
                     93:        }
                     94:        i = 0;
                     95:        while (i < strlen(args)) {
                     96:                ptr = &args[i];
                     97:                j = 0;
                     98:                while (ptr[j] != delimiter && ptr[j] != 0) {
                     99:                        i++;
                    100:                        j++;
                    101:                }
                    102:                tabargs[nbargs] = (char *)calloc(j + 1, 1);
                    103:                strncpy(tabargs[nbargs], ptr, j);
                    104:                i++;
                    105:                while (!isalnum(args[i]) && args[i] != '.' && args[i] != '/' && args[i])
                    106:                        i++;
                    107:                nbargs++;
                    108:                if (nbargs >= VRRP_CONF_MAX_ARGS) {
                    109:                        syslog(LOG_ERR, "too many arguments in the configuration file");
                    110:                        exit(EXIT_FAILURE);
                    111:                }
                    112:        }
                    113: 
                    114:        return tabargs;
                    115: }
                    116: 
                    117: void 
                    118: vrrp_conf_freeargs(char **temp)
                    119: {
                    120:        int             i = 0;
                    121: 
                    122:        while (temp[i]) {
                    123:                free(temp[i]);
                    124:                temp[i] = NULL;
                    125:                i++;
                    126:        }
                    127:        free(temp);
                    128: 
                    129:        return;
                    130: }
                    131: 
                    132: FILE           *
                    133: vrrp_conf_open_file(char *name)
                    134: {
                    135:        FILE           *stream;
                    136:        struct stat     st;
                    137: 
                    138:        stream = fopen(name, "r");
                    139:        if (! stream) {
                    140:                syslog(LOG_ERR, "cannot open configuration file %s: %s", name, strerror(errno));
                    141:                return NULL;
                    142:        }
                    143:        if (lstat(name, &st) == -1) {
                    144:                syslog(LOG_ERR, "cannot call lstat(): %s", strerror(errno));
                    145:                fclose(stream);
                    146:                return NULL;
                    147:        }
                    148:        if ((st.st_mode & S_IFMT) != S_IFREG) {
                    149:                syslog(LOG_ERR, "%s is not a regular file", name);
                    150:                fclose(stream);
                    151:                return NULL;
                    152:        }
                    153: 
                    154:        return stream;
                    155: }
                    156: 
                    157: char 
                    158: vrrp_conf_close_file(FILE * stream)
                    159: {
                    160:        if (fclose(stream) == EOF) {
                    161:                syslog(LOG_ERR, "can't close the file stream FILE *stream: %s\n", strerror(errno));
                    162:                return -1;
                    163:        }
                    164:        return 0;
                    165: }
                    166: 
                    167: char 
                    168: vrrp_conf_lecture_fichier(struct vrrp_vr * vr, FILE * stream)
                    169: {
                    170:        char            ligne[1024] = "#";
                    171:        char          **temp;
                    172:        char          **temp2;
                    173:        char          **temp3;
                    174:        char            option[1024], arg[1024];
                    175:        int             i, j;
                    176:        fpos_t          pos;
                    177:        int             optok;
                    178: 
                    179:        fgetpos(stream, &pos);
                    180:        if (!pos) {
                    181:                while (ligne[0] == '#' || ligne[0] == 0 || ligne[0] == '\n')
                    182:                        fgets(ligne, 1024, stream);
                    183:                if (strncmp(ligne, "[VRID]", 6)) {
                    184:                        syslog(LOG_ERR, "configuration file error ! cannot see [VRID] section");
                    185:                        return -1;
                    186:                }
                    187:                fgets(ligne, 1024, stream);
                    188:        }
                    189:        while (!feof(stream) && strncmp(ligne, "[VRID]", 6)) {
                    190:                if (ligne[0] != 0 && ligne[0] != '\n' && ligne[0] != '#') {
                    191:                        if (feof(stream))
                    192:                                break;
                    193:                        vrrp_conf_ident_option_arg(ligne, option, arg);
                    194:                        optok = 0;
                    195:                        if (!strcmp(option, "addr")) {
                    196:                                temp = vrrp_conf_split_args(arg, ',');
                    197:                                i = 0;
                    198:                                while (temp[i])
                    199:                                        i++;
                    200:                                vr->vr_ip = (struct vrrp_vip *) calloc(i, sizeof(struct vrrp_vip) + 1);
                    201:                                vr->vr_netmask = (u_int *) calloc(i, sizeof(u_int) + 1);
                    202:                                i = 0;
                    203:                                while (temp[i] && (i < VRRP_CONF_MAX_ARGS)) {
                    204:                                        temp2 = vrrp_conf_split_args(temp[i], ':');
                    205:                                        if (temp2[1]) {
                    206:                                                vr->vr_ip[i].if_name = (char *)calloc(IFNAMSIZ + 1, 1);
                    207:                                                strncpy(vr->vr_ip[i].if_name, temp2[0], IFNAMSIZ);
                    208:                                                temp3 = vrrp_conf_split_args(temp2[1], '/');
                    209:                                        }
                    210:                                        else
                    211:                                                temp3 = vrrp_conf_split_args(temp2[0], '/');
                    212:                                        j = 0;
                    213:                                        while (temp3[j])
                    214:                                                j++;
                    215:                                        if (j != 2) {
                    216:                                                syslog(LOG_ERR, "bad value in the configuration file for addr option: %s", arg);
                    217:                                                exit(-1);
                    218:                                        }
                    219:                                        vr->vr_ip[i].addr.s_addr = inet_addr(temp3[0]);
                    220:                                        vr->vr_netmask[i] = atoi(temp3[1]);
                    221:                                        i++;
                    222:                                        vrrp_conf_freeargs(temp3);
                    223:                                        vrrp_conf_freeargs(temp2);
                    224:                                }
                    225:                                vr->cnt_ip = i;
                    226:                                vrrp_conf_freeargs(temp);
                    227:                                optok = 1;
                    228:                        }
                    229:                        if (!strcmp(option, "interface")) {
                    230:                                temp = vrrp_conf_split_args(arg, ',');
                    231:                                if (!(vr->vr_if = vrrp_misc_search_if_entry(temp[0]))) {
                    232:                                        vr->vr_if = (struct vrrp_if *)calloc(1, sizeof(struct vrrp_if));
                    233:                                        strncpy(vr->vr_if->if_name, temp[0], sizeof(vr->vr_if->if_name));
                    234:                                }
                    235:                                vrrp_conf_freeargs(temp);
                    236:                                optok = 1;
                    237:                        }
                    238:                        if (!strcmp(option, "monitoredcircuits")) {
                    239:                                if (! strcmp(arg, "yes"))
                    240:                                        vr->useMonitoredCircuits = 1;
                    241:                                else {
                    242:                                        if (! strcmp(arg, "no"))
                    243:                                                vr->useMonitoredCircuits = 0;
                    244:                                        else {
                    245:                                                syslog(LOG_ERR, "bad value for useMonitoredCircuits: %s", arg);
                    246:                                                exit(-1);
                    247:                                        }
                    248:                                }
                    249:                                optok = 1;
                    250:                        }
                    251:                        if (!strcmp(option, "serverid")) {
                    252:                                vr->vr_id = atoi(arg);
                    253:                                optok = 1;
                    254:                        }
                    255:                        if (!strcmp(option, "carriertimeout")) {
                    256:                                if (vr->vr_if)
                    257:                                        vr->vr_if->carrier_timeout = atoi(arg);
                    258:                                else
                    259:                                        syslog(LOG_ERR, "'carriertimeout' must be specified after 'interface'");
                    260:                                optok = 1;
                    261:                        }
                    262:                        if (!strcmp(option, "spanningtreelatency")) {
                    263:                                vr->spanningTreeLatency = atoi(arg);
                    264:                                optok = 1;
                    265:                        }
                    266:                        if (!strcmp(option, "priority")) {
                    267:                                temp = vrrp_conf_split_args(arg, ',');
                    268:                                vr->priority = atoi(temp[0]);
                    269:                                vrrp_conf_freeargs(temp);
                    270:                                optok = 1;
                    271:                        }
                    272:                        if (!strcmp(option, "password")) {
                    273:                                temp = vrrp_conf_split_args(arg, ',');
                    274:                                vr->password = (char *)calloc(8, 1);
                    275:                                strncpy(vr->password, temp[0], 8);
                    276:                                vrrp_conf_freeargs(temp);
                    277:                                vr->auth_type = 1;
                    278:                                optok = 1;
                    279:                        }
                    280:                        if (!strcmp(option, "masterscript")) {
                    281:                                vr->master_script = (char *)calloc(strlen(arg)+1, 1);
                    282:                                strncpy(vr->master_script, arg, strlen(arg));
                    283:                                optok = 1;
                    284:                        }
                    285:                        if (!strcmp(option, "backupscript")) {
                    286:                                vr->backup_script = (char *)calloc(strlen(arg)+1, 1);
                    287:                                strncpy(vr->backup_script, arg, strlen(arg));
                    288:                                optok = 1;
                    289:                        }
                    290:                        if (! strcmp(option, "vridsdep")) {
                    291:                                temp = vrrp_conf_split_args(arg, ',');
                    292:                                i = 0;
                    293:                                while (temp[i]) i++;
                    294:                                vr->vridsdeps = (int *)calloc(i+1, sizeof(int));
                    295:                                i = 0;
                    296:                                while (temp[i]) {
                    297:                                        vr->vridsdeps[i] = atoi(temp[i]);
                    298:                                        i++;
                    299:                                }
                    300:                                vr->vridsdeps[i] = -1;
                    301:                                vrrp_conf_freeargs(temp);
                    302:                                optok = 1;
                    303:                        }
                    304:                        if (!strcmp(option, "useIKE")) {
                    305:                                if (! strcmp(arg, "yes"))
                    306:                                        vr->useIKE = 1;
                    307:                                else {
                    308:                                        if (! strcmp(arg, "no"))
                    309:                                                vr->useIKE = 0;
                    310:                                        else {
                    311:                                                syslog(LOG_ERR, "bad value for useIKE: %s", arg);
                    312:                                                exit(-1);
                    313:                                        }
                    314:                                }
                    315:                                optok = 1;
                    316:                        }
                    317:                        if (!strcmp(option, "AHencryption")) {
                    318:                                if (! strcmp(arg, "yes")) {
                    319:                                        vr->AHencryption = 1;
                    320:                                        vr->auth_type = 2;
                    321:                                } else {
                    322:                                        if (! strcmp(arg, "no"))
                    323:                                                vr->AHencryption = 0;
                    324:                                        else {
                    325:                                                syslog(LOG_ERR, "bad value for AHencryption: %s", arg);
                    326:                                                exit(-1);
                    327:                                        }
                    328:                                }
                    329:                                optok = 1;
                    330:                        }
                    331:                        if (!strcmp(option, "presharedkey")) {
                    332:                                if (vr->password) {
                    333:                                        syslog(LOG_ERR, "you cannot use presharedkey option with password option");
                    334:                                        exit(-1);
                    335:                                }
                    336:                                vr->password = (char *)calloc(strlen(arg)+1, 1);
                    337:                                strncpy(vr->password, arg, strlen(arg));
                    338:                                optok = 1;
                    339:                        }
                    340:                        if (!strcmp(option, "MCClearErrorsCount")) {
                    341:                                vr->monitoredCircuitsClearErrorsCount = atoi(arg);
                    342:                                optok = 1;
                    343:                        }
                    344:                        if (! optok)
                    345:                                syslog(LOG_ERR, "option '%s' unknown on configuration file, ignoring it", option);
                    346:                }
                    347:                fgets(ligne, 1024, stream);
                    348:        }
                    349:        if (feof(stream))
                    350:                return 1;
                    351: 
                    352:        return 0;
                    353: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>