📄 conf.c.svn-base
字号:
while (isblank(*p2)) p2++; /* Get opcode */ opcode = config_parse_token(p1, filename, *linenum); debug(LOG_DEBUG, "p1 = [%s]; p2 = [%s]", p1, p2); switch (opcode) { case oFirewallRule: _parse_firewall_rule(ruleset, p2); break; case oBadOption: default: debug(LOG_ERR, "Bad option on line %d " "in %s.", *linenum, filename); debug(LOG_ERR, "Exiting..."); exit(-1); break; } } /* Read next line */ memset(line, 0, MAX_BUF); fgets(line, MAX_BUF - 1, file); (*linenum)++; /* increment line counter. */ } debug(LOG_DEBUG, "Firewall Rule Set %s added.", ruleset);}/** @internalHelper for parse_firewall_ruleset. Parses a single rule in a ruleset*/static int_parse_firewall_rule(char *ruleset, char *leftover){ int i; int block_allow = 0; /**< 0 == block, 1 == allow */ int all_nums = 1; /**< If 0, port contained non-numerics */ int finished = 0; /**< reached end of line */ char *token = NULL; /**< First word */ char *port = NULL; /**< port to open/block */ char *protocol = NULL; /**< protocol to block, tcp/udp/icmp */ char *mask = NULL; /**< Netmask */ char *other_kw = NULL; /**< other key word */ t_firewall_ruleset *tmpr; t_firewall_ruleset *tmpr2; t_firewall_rule *tmp; t_firewall_rule *tmp2; debug(LOG_DEBUG, "leftover: %s", leftover); /* lower case */ for (i = 0; *(leftover + i) != '\0' && (*(leftover + i) = tolower(*(leftover + i))); i++); token = leftover; TO_NEXT_WORD(leftover, finished); /* Parse token */ if (!strcasecmp(token, "block") || finished) { block_allow = 0; } else if (!strcasecmp(token, "allow")) { block_allow = 1; } else { debug(LOG_ERR, "Invalid rule type %s, expecting " "\"block\" or \"allow\"", token); return -1; } /* Parse the remainder */ /* Get the protocol */ if (strncmp(leftover, "tcp", 3) == 0 || strncmp(leftover, "udp", 3) == 0 || strncmp(leftover, "icmp", 4) == 0) { protocol = leftover; TO_NEXT_WORD(leftover, finished); } /* should be exactly "port" */ if (strncmp(leftover, "port", 4) == 0) { TO_NEXT_WORD(leftover, finished); /* Get port now */ port = leftover; TO_NEXT_WORD(leftover, finished); for (i = 0; *(port + i) != '\0'; i++) if (!isdigit(*(port + i))) all_nums = 0; /*< No longer only digits */ if (!all_nums) { debug(LOG_ERR, "Invalid port %s", port); return -3; /*< Fail */ } } /* Now, further stuff is optional */ if (!finished) { /* should be exactly "to" */ other_kw = leftover; TO_NEXT_WORD(leftover, finished); if (strcmp(other_kw, "to") || finished) { debug(LOG_ERR, "Invalid or unexpected keyword %s, " "expecting \"to\"", other_kw); return -4; /*< Fail */ } /* Get port now */ mask = leftover; TO_NEXT_WORD(leftover, finished); all_nums = 1; for (i = 0; *(mask + i) != '\0'; i++) if (!isdigit(*(mask + i)) && (*(mask + i) != '.') && (*(mask + i) != '/')) all_nums = 0; /*< No longer only digits */ if (!all_nums) { debug(LOG_ERR, "Invalid mask %s", mask); return -3; /*< Fail */ } } /* Generate rule record */ tmp = safe_malloc(sizeof(t_firewall_rule)); memset((void *)tmp, 0, sizeof(t_firewall_rule)); tmp->block_allow = block_allow; if (protocol != NULL) tmp->protocol = safe_strdup(protocol); if (port != NULL) tmp->port = safe_strdup(port); if (mask == NULL) tmp->mask = safe_strdup("0.0.0.0/0"); else tmp->mask = safe_strdup(mask); debug(LOG_DEBUG, "Adding Firewall Rule %s %s port %s to %s", token, tmp->protocol, tmp->port, tmp->mask); /* Append the rule record */ if (config.rulesets == NULL) { config.rulesets = safe_malloc(sizeof(t_firewall_ruleset)); memset(config.rulesets, 0, sizeof(t_firewall_ruleset)); config.rulesets->name = safe_strdup(ruleset); tmpr = config.rulesets; } else { tmpr2 = tmpr = config.rulesets; while (tmpr != NULL && (strcmp(tmpr->name, ruleset) != 0)) { tmpr2 = tmpr; tmpr = tmpr->next; } if (tmpr == NULL) { /* Rule did not exist */ tmpr = safe_malloc(sizeof(t_firewall_ruleset)); memset(tmpr, 0, sizeof(t_firewall_ruleset)); tmpr->name = safe_strdup(ruleset); tmpr2->next = tmpr; } } /* At this point, tmpr == current ruleset */ if (tmpr->rules == NULL) { /* No rules... */ tmpr->rules = tmp; } else { tmp2 = tmpr->rules; while (tmp2->next != NULL) tmp2 = tmp2->next; tmp2->next = tmp; } return 1;}t_firewall_rule *get_ruleset(char *ruleset){ t_firewall_ruleset *tmp; for (tmp = config.rulesets; tmp != NULL && strcmp(tmp->name, ruleset) != 0; tmp = tmp->next); if (tmp == NULL) return NULL; return(tmp->rules);}/**@param filename Full path of the configuration file to be read */voidconfig_read(char *filename){ FILE *fd; char line[MAX_BUF], *s, *p1, *p2; int linenum = 0, opcode, value; debug(LOG_INFO, "Reading configuration file '%s'", filename); if (!(fd = fopen(filename, "r"))) { debug(LOG_ERR, "Could not open configuration file '%s', " "exiting...", filename); exit(1); } while (!feof(fd) && fgets(line, MAX_BUF, fd)) { linenum++; s = line; if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = '\0'; if ((p1 = strchr(s, ' '))) { p1[0] = '\0'; } else if ((p1 = strchr(s, '\t'))) { p1[0] = '\0'; } if (p1) { p1++; if ((p2 = strchr(p1, ' '))) { p2[0] = '\0'; } else if ((p2 = strstr(p1, "\r\n"))) { p2[0] = '\0'; } else if ((p2 = strchr(p1, '\n'))) { p2[0] = '\0'; } } if (p1 && p1[0] != '\0') { /* Strip trailing spaces */ if ((strncmp(s, "#", 1)) != 0) { debug(LOG_DEBUG, "Parsing token: %s, " "value: %s", s, p1); opcode = config_parse_token(s, filename, linenum); switch(opcode) { case oDaemon: if (config.daemon == -1 && ((value = parse_boolean_value(p1)) != -1)) { config.daemon = value; } break; case oExternalInterface: config.external_interface = safe_strdup(p1); break; case oGatewayID: config.gw_id = safe_strdup(p1); break; case oGatewayInterface: config.gw_interface = safe_strdup(p1); break; case oGatewayAddress: config.gw_address = safe_strdup(p1); break; case oGatewayPort: sscanf(p1, "%d", &config.gw_port); break; case oAuthServer: parse_auth_server(fd, filename, &linenum); break; case oFirewallRuleSet: parse_firewall_ruleset(p1, fd, filename, &linenum); break; case oTrustedMACList: parse_trusted_mac_list(p1); break; case oHTTPDName: config.httpdname = safe_strdup(p1); break; case oHTTPDMaxConn: sscanf(p1, "%d", &config.httpdmaxconn); break; case oBadOption: debug(LOG_ERR, "Bad option on line %d " "in %s.", linenum, filename); debug(LOG_ERR, "Exiting..."); exit(-1); break; case oCheckInterval: sscanf(p1, "%d", &config.checkinterval); break; case oWdctlSocket: free(config.wdctl_sock); config.wdctl_sock = safe_strdup(p1); break; case oClientTimeout: sscanf(p1, "%d", &config.clienttimeout); break; case oSyslogFacility: sscanf(p1, "%d", &config.syslog_facility); break; } } } } fclose(fd);}/** @internalParses a boolean value from the config file*/static intparse_boolean_value(char *line){ if (strcasecmp(line, "yes") == 0) { return 1; } if (strcasecmp(line, "no") == 0) { return 0; } if (strcmp(line, "1") == 0) { return 1; } if (strcmp(line, "0") == 0) { return 0; } return -1;}void parse_trusted_mac_list(char *ptr) { char *ptrcopy = NULL; char *possiblemac = NULL; char *mac = NULL; t_trusted_mac *p = NULL; debug(LOG_DEBUG, "Parsing string [%s] for trusted MAC addresses", ptr); mac = safe_malloc(18); /* strsep modifies original, so let's make a copy */ ptrcopy = safe_strdup(ptr); while ((possiblemac = strsep(&ptrcopy, ", "))) { if (sscanf(possiblemac, " %17[A-Fa-f0-9:]", mac) == 1) { /* Copy mac to the list */ debug(LOG_DEBUG, "Adding MAC address [%s] to trusted list", mac); if (config.trustedmaclist == NULL) { config.trustedmaclist = safe_malloc(sizeof(t_trusted_mac)); config.trustedmaclist->mac = safe_strdup(mac); config.trustedmaclist->next = NULL; } else { /* Advance to the last entry */ for (p = config.trustedmaclist; p->next != NULL; p = p->next); p->next = safe_malloc(sizeof(t_trusted_mac)); p = p->next; p->mac = safe_strdup(mac); p->next = NULL; } } } free(ptrcopy); free(mac);}/** Verifies if the configuration is complete and valid. Terminates the program if it isn't */voidconfig_validate(void){ config_notnull(config.gw_interface, "GatewayInterface"); config_notnull(config.auth_servers, "AuthServer"); if (missing_parms) { debug(LOG_ERR, "Configuration is not complete, exiting..."); exit(-1); }}/** @internal Verifies that a required parameter is not a null pointer*/static voidconfig_notnull(void *parm, char *parmname){ if (parm == NULL) { debug(LOG_ERR, "%s is not set", parmname); missing_parms = 1; }}/** * This function returns the current (first auth_server) */t_auth_serv *get_auth_server(void){ /* This is as good as atomic */ return config.auth_servers;}/** * This function marks the current auth_server, if it matches the argument, * as bad. Basically, the "bad" server becomes the last one on the list. */voidmark_auth_server_bad(t_auth_serv *bad_server){ t_auth_serv *tmp; if (config.auth_servers == bad_server && bad_server->next != NULL) { /* Go to the last */ for (tmp = config.auth_servers; tmp->next != NULL; tmp = tmp->next); /* Set bad server as last */ tmp->next = bad_server; /* Remove bad server from start of list */ config.auth_servers = bad_server->next; /* Set the next pointe to NULL in the last element */ bad_server->next = NULL; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -