void load_local_addresses(void) { FILE *fp; char buff[MAX_BUFF]; struct IP src_address; fp = fopen(LOCAL_ADDRESSES, "rt"); if(fp == NULL) { log(LOG_ERROR, "unable to open local addresses file [%s]\n", LOCAL_ADDRESSES); dodo_mode = 1; return; } log(LOG_INFO, "Loading IP List\n"); while(!dodo_mode && (fgets(buff, MAX_BUFF, fp) != NULL)) { /* skip blank lines and comments */ if((strncmp(buff, "#", 1) == 0) || (strncmp(buff, "\n", 1) == 0)) continue; if(strlen(buff) != 1) { /* chop newline */ buff[strlen(buff) - 1] = '\0'; if(get_ip(buff, &src_address)) { log(LOG_ERROR, "Invalid IP network [%s] in config file\n", buff); /* there's no need to set 'dodo_mode' here because we can continue quite happily. */ } else { add_to_ip_list(src_address); } } } fclose(fp); }
/* parse the squirm.conf file returns 1 on success, 0 on failure */ int parse_squirm_conf(char *filename) { FILE *fp; char buff[MAX_BUFF]; struct cidr src_cidr; char *tmp1, *tmp2, *pattern_filename, *fq_pattern_filename; struct subnet_block *curr_block; int methods; pattern_filename = NULL; curr_block = NULL; fp = fopen(filename, "rt"); init_lists(); if(fp == NULL) { logprint(LOG_ERROR, "unable to open configuration file [%s]\n", filename); dodo_mode = 1; return 0; } logprint(LOG_INFO, "processing configuration file [%s]\n", filename); while(!dodo_mode && (fgets(buff, MAX_BUFF, fp) != NULL)) { /* skip blank lines and comments */ if((strncmp(buff, "#", 1) == 0) || (strncmp(buff, "\n", 1) == 0)) continue; if(strlen(buff) != 1) { /* chop newline */ buff[strlen(buff) - 1] = '\0'; /* do our stuff with the line */ /* we can have the following appear begin network log abort-log pattern end */ /* begin */ if(starts_with(buff, "begin") == 1) { if (curr_block != NULL) { logprint(LOG_ERROR, "begin keyword found within block in %s\n", filename); dodo_mode = 1; return 0; } else { curr_block = add_subnet_block(); } continue; } /* network */ if (starts_with(buff, "network") == 1) { if (curr_block == NULL) { logprint(LOG_ERROR, "network keyword found outside of block in %s\n", filename); dodo_mode = 1; return 0; } else { /* we need to get the network cidr from after the keyword network */ if(load_cidr(strpbrk(buff, "0123456789"), &src_cidr)) { logprint(LOG_ERROR, "Invalid IP network [%s] in config file\n", buff); dodo_mode = 1; return 0; } else { add_to_ip_list(src_cidr, curr_block); } } continue; } /* check for an optional abort-log file name */ if (starts_with(buff, "abort-log") == 1) { if (curr_block == NULL) { logprint(LOG_ERROR, "abort-log keyword found outside of block in %s\n", filename); dodo_mode = 1; return 0; } else { curr_block->abort_log_name = gen_fq_name(get_stuff_after_keyword(buff), LOGDIR); if (curr_block->abort_log_name == NULL) { logprint(LOG_ERROR, "couldn't allocate memory in parse_squirm_conf()\n"); dodo_mode = 1; return 0; } } continue; } /* check for an optional log file name (logs matches) */ if (starts_with(buff, "log")) { if (curr_block == NULL) { logprint(LOG_ERROR, "log keyword found outside of block in %s\n", filename); dodo_mode = 1; return 0; } else { curr_block->log_name = gen_fq_name(get_stuff_after_keyword(buff), LOGDIR); if (curr_block->log_name == NULL) { logprint(LOG_ERROR, "couldn't allocate memory in parse_squirm_conf()\n"); dodo_mode = 1; return 0; } } continue; } /* checking for a pattern filename */ if (starts_with(buff, "pattern") == 1) { if (curr_block == NULL) { logprint(LOG_ERROR, "pattern keyword found outside of block in %s\n", filename); dodo_mode = 1; return -1; } else { /* format is pattern filename method[, method...] */ tmp1 = get_stuff_after_keyword (buff); /* find where white space begins again */ tmp2 = index(tmp1, ' '); if (tmp2 == NULL) { /* no white space at end, but there must be white space between the filename and obligatory method */ logprint(LOG_ERROR, "error parsing pattern file name in %s\n", filename); dodo_mode = 1; return -1; } else { pattern_filename = (char *)malloc((tmp2 - tmp1) * sizeof(char)+1); strncpy(pattern_filename, tmp1, tmp2-tmp1); pattern_filename[tmp2-tmp1] = '\0'; } if (pattern_filename == NULL) { logprint(LOG_ERROR, "couldn't allocate memory in parse_squirm_conf()\n"); dodo_mode = 1; return 0; } fq_pattern_filename = gen_fq_name(pattern_filename, ETCDIR); if (fq_pattern_filename == NULL) { logprint(LOG_ERROR, "couldn't allocate memory in parse_squirm_conf()\n"); dodo_mode = 1; return 0; } free(pattern_filename); /* now we start looking for methods */ tmp1 = tmp2; methods = 0; while (tmp1 != NULL) { tmp1 = trim_leading_white_space(tmp1); if (tmp1 == NULL) { break; } if (strncasecmp(tmp1, "get", min(strlen("get"), strlen(tmp1))) == 0) { methods |= GET; tmp1 += strlen("get"); } else if (strncasecmp(tmp1,"put", min(strlen("put"), strlen(tmp1))) == 0) { methods |= PUT; tmp1 += strlen("put"); } else if (strncasecmp(tmp1,"post", min(strlen("post"), strlen(tmp1))) == 0) { methods |= POST; tmp1 += strlen("post"); } else if (strncasecmp(tmp1, "head", min(strlen("head"), strlen(tmp1))) == 0) { methods |= HEAD; tmp1 += strlen("head"); } else if (strncasecmp(tmp1, "all", min(strlen("all"), strlen(tmp1))) == 0) { methods |= ALL; tmp1 += strlen("all"); } tmp1 = index(tmp1, ','); if (tmp1 != NULL) { tmp1++; } } /* insert all the information */ if (add_pattern_file(fq_pattern_filename, curr_block, methods) == -1) { logprint(LOG_ERROR, "couldn't parse pattern file %s\n", fq_pattern_filename); dodo_mode = 1; return 0; } free(fq_pattern_filename); } continue; } /* check for the presence of an end */ if (strstr(buff, "end") != NULL) { if (curr_block == NULL) { logprint(LOG_ERROR, "found keyword end outside of block in file %s\n", filename); dodo_mode = 1; return -1; } else { curr_block = NULL; } continue; } /* oops... not a valid option */ logprint(LOG_ERROR, "found garbage [%s] in configuration file [%s]\n", buff, filename); } } fclose(fp); return 1; }