📄 config.c
字号:
} user->password = malloc(16); if (user->password == NULL) { printf("Failed to allocate memory for EAP " "password hash\n"); goto failed; } if (hexstr2bin(pos, user->password, 16) < 0) { printf("Invalid hash password on line %d in " "'%s'\n", line, fname); goto failed; } user->password_len = 16; user->password_hash = 1; pos = pos2; } else { pos2 = pos; while (*pos2 != '\0' && *pos2 != ' ' && *pos2 != '\t' && *pos2 != '#') pos2++; if ((pos2 - pos) & 1) { printf("Invalid hex password on line %d in " "'%s'\n", line, fname); goto failed; } user->password = malloc((pos2 - pos) / 2); if (user->password == NULL) { printf("Failed to allocate memory for EAP " "password\n"); goto failed; } if (hexstr2bin(pos, user->password, (pos2 - pos) / 2) < 0) { printf("Invalid hex password on line %d in " "'%s'\n", line, fname); goto failed; } user->password_len = (pos2 - pos) / 2; pos = pos2; } while (*pos == ' ' || *pos == '\t') pos++; if (strncmp(pos, "[2]", 3) == 0) { user->phase2 = 1; } done: if (tail == NULL) { tail = conf->eap_user = user; } else { tail->next = user; tail = user; } continue; failed: if (user) { free(user->password); free(user->identity); free(user); } ret = -1; break; } fclose(f); return ret;}#endif /* EAP_SERVER */static inthostapd_config_read_radius_addr(struct hostapd_radius_server **server, int *num_server, const char *val, int def_port, struct hostapd_radius_server **curr_serv){ struct hostapd_radius_server *nserv; int ret; static int server_index = 1; nserv = realloc(*server, (*num_server + 1) * sizeof(*nserv)); if (nserv == NULL) return -1; *server = nserv; nserv = &nserv[*num_server]; (*num_server)++; (*curr_serv) = nserv; memset(nserv, 0, sizeof(*nserv)); nserv->port = def_port; ret = hostapd_parse_ip_addr(val, &nserv->addr); nserv->index = server_index++; return ret;}static int hostapd_config_parse_key_mgmt(int line, const char *value){ int val = 0, last; char *start, *end, *buf; buf = strdup(value); if (buf == NULL) return -1; start = buf; while (start != '\0') { while (*start == ' ' || *start == '\t') start++; if (*start == '\0') break; end = start; while (*end != ' ' && *end != '\t' && *end != '\0') end++; last = *end == '\0'; *end = '\0'; if (strcmp(start, "WPA-PSK") == 0) val |= WPA_KEY_MGMT_PSK; else if (strcmp(start, "WPA-EAP") == 0) val |= WPA_KEY_MGMT_IEEE8021X; else { printf("Line %d: invalid key_mgmt '%s'", line, start); free(buf); return -1; } if (last) break; start = end + 1; } free(buf); if (val == 0) { printf("Line %d: no key_mgmt values configured.", line); return -1; } return val;}static int hostapd_config_parse_cipher(int line, const char *value){ int val = 0, last; char *start, *end, *buf; buf = strdup(value); if (buf == NULL) return -1; start = buf; while (start != '\0') { while (*start == ' ' || *start == '\t') start++; if (*start == '\0') break; end = start; while (*end != ' ' && *end != '\t' && *end != '\0') end++; last = *end == '\0'; *end = '\0'; if (strcmp(start, "CCMP") == 0) val |= WPA_CIPHER_CCMP; else if (strcmp(start, "TKIP") == 0) val |= WPA_CIPHER_TKIP; else if (strcmp(start, "WEP104") == 0) val |= WPA_CIPHER_WEP104; else if (strcmp(start, "WEP40") == 0) val |= WPA_CIPHER_WEP40; else if (strcmp(start, "NONE") == 0) val |= WPA_CIPHER_NONE; else { printf("Line %d: invalid cipher '%s'.", line, start); free(buf); return -1; } if (last) break; start = end + 1; } free(buf); if (val == 0) { printf("Line %d: no cipher values configured.", line); return -1; } return val;}static int hostapd_config_check(struct hostapd_bss_config *conf){ if (conf->ieee802_1x && !conf->eap_server && !conf->radius->auth_servers) { printf("Invalid IEEE 802.1X configuration (no EAP " "authenticator configured).\n"); return -1; } if (conf->wpa && (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) && conf->ssid.wpa_psk == NULL && conf->ssid.wpa_passphrase == NULL && conf->ssid.wpa_psk_file == NULL) { printf("WPA-PSK enabled, but PSK or passphrase is not " "configured.\n"); return -1; } return 0;}static int hostapd_config_read_wep(struct hostapd_wep_keys *wep, int keyidx, char *val){ size_t len = strlen(val); if (keyidx < 0 || keyidx > 3 || wep->key[keyidx] != NULL) return -1; if (val[0] == '"') { if (len < 2 || val[len - 1] != '"') return -1; len -= 2; wep->key[keyidx] = malloc(len); if (wep->key[keyidx] == NULL) return -1; memcpy(wep->key[keyidx], val + 1, len); wep->len[keyidx] = len; } else { if (len & 1) return -1; len /= 2; wep->key[keyidx] = malloc(len); if (wep->key[keyidx] == NULL) return -1; wep->len[keyidx] = len; if (hexstr2bin(val, wep->key[keyidx], len) < 0) return -1; } wep->keys_set++; return 0;}static int hostapd_parse_rates(int **rate_list, char *val){ int *list; int count; char *pos, *end; free(*rate_list); *rate_list = NULL; pos = val; count = 0; while (*pos != '\0') { if (*pos == ' ') count++; pos++; } list = malloc(sizeof(int) * (count + 2)); if (list == NULL) return -1; pos = val; count = 0; while (*pos != '\0') { end = strchr(pos, ' '); if (end) *end = '\0'; list[count++] = atoi(pos); if (!end) break; pos = end + 1; } list[count] = -1; *rate_list = list; return 0;}struct hostapd_config * hostapd_config_read(const char *fname){ struct hostapd_config *conf; struct hostapd_bss_config *bss; FILE *f; char buf[256], *pos; int line = 0, i; int errors = 0; f = fopen(fname, "r"); if (f == NULL) { printf("Could not open configuration file '%s' for reading.\n", fname); return NULL; } conf = hostapd_config_defaults(); if (conf == NULL) { fclose(f); return NULL; } bss = conf->last_bss = conf->bss; while (fgets(buf, sizeof(buf), f)) { bss = conf->last_bss; line++; if (buf[0] == '#') continue; pos = buf; while (*pos != '\0') { if (*pos == '\n') { *pos = '\0'; break; } pos++; } if (buf[0] == '\0') continue; pos = strchr(buf, '='); if (pos == NULL) { printf("Line %d: invalid line '%s'\n", line, buf); errors++; continue; } *pos = '\0'; pos++; if (strcmp(buf, "interface") == 0) { snprintf(conf->bss[0].iface, sizeof(conf->bss[0].iface), "%s", pos); } else if (strcmp(buf, "bridge") == 0) { snprintf(bss->bridge, sizeof(bss->bridge), "%s", pos); } else if (strcmp(buf, "driver") == 0) { conf->driver = driver_lookup(pos); if (conf->driver == NULL) { printf("Line %d: invalid/unknown driver " "'%s'\n", line, pos); errors++; } } else if (strcmp(buf, "debug") == 0) { bss->debug = atoi(pos); } else if (strcmp(buf, "logger_syslog_level") == 0) { bss->logger_syslog_level = atoi(pos); } else if (strcmp(buf, "logger_stdout_level") == 0) { bss->logger_stdout_level = atoi(pos); } else if (strcmp(buf, "logger_syslog") == 0) { bss->logger_syslog = atoi(pos); } else if (strcmp(buf, "logger_stdout") == 0) { bss->logger_stdout = atoi(pos); } else if (strcmp(buf, "dump_file") == 0) { bss->dump_log_name = strdup(pos); } else if (strcmp(buf, "ssid") == 0) { bss->ssid.ssid_len = strlen(pos); if (bss->ssid.ssid_len >= HOSTAPD_MAX_SSID_LEN || bss->ssid.ssid_len < 1) { printf("Line %d: invalid SSID '%s'\n", line, pos); errors++; } memcpy(bss->ssid.ssid, pos, bss->ssid.ssid_len); bss->ssid.ssid[bss->ssid.ssid_len] = '\0'; bss->ssid.ssid_set = 1; } else if (strcmp(buf, "macaddr_acl") == 0) { bss->macaddr_acl = atoi(pos); if (bss->macaddr_acl != ACCEPT_UNLESS_DENIED && bss->macaddr_acl != DENY_UNLESS_ACCEPTED && bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) { printf("Line %d: unknown macaddr_acl %d\n", line, bss->macaddr_acl); } } else if (strcmp(buf, "accept_mac_file") == 0) { if (hostapd_config_read_maclist(pos, &bss->accept_mac, &bss->num_accept_mac)) { printf("Line %d: Failed to read " "accept_mac_file '%s'\n", line, pos); errors++; } } else if (strcmp(buf, "deny_mac_file") == 0) { if (hostapd_config_read_maclist(pos, &bss->deny_mac, &bss->num_deny_mac)) { printf("Line %d: Failed to read " "deny_mac_file '%s'\n", line, pos); errors++; } } else if (strcmp(buf, "ap_max_inactivity") == 0) { bss->ap_max_inactivity = atoi(pos); } else if (strcmp(buf, "assoc_ap_addr") == 0) { if (hwaddr_aton(pos, bss->assoc_ap_addr)) { printf("Line %d: invalid MAC address '%s'\n", line, pos); errors++; } bss->assoc_ap = 1; } else if (strcmp(buf, "ieee8021x") == 0) { bss->ieee802_1x = atoi(pos); } else if (strcmp(buf, "eapol_version") == 0) { bss->eapol_version = atoi(pos); if (bss->eapol_version < 1 || bss->eapol_version > 2) { printf("Line %d: invalid EAPOL " "version (%d): '%s'.\n", line, bss->eapol_version, pos); errors++; } else wpa_printf(MSG_DEBUG, "eapol_version=%d", bss->eapol_version);#ifdef EAP_SERVER } else if (strcmp(buf, "eap_authenticator") == 0) { bss->eap_server = atoi(pos); printf("Line %d: obsolete eap_authenticator used; " "this has been renamed to eap_server\n", line); } else if (strcmp(buf, "eap_server") == 0) { bss->eap_server = atoi(pos); } else if (strcmp(buf, "eap_user_file") == 0) { if (hostapd_config_read_eap_user(pos, bss)) errors++; } else if (strcmp(buf, "ca_cert") == 0) { free(bss->ca_cert); bss->ca_cert = strdup(pos); } else if (strcmp(buf, "server_cert") == 0) { free(bss->server_cert); bss->server_cert = strdup(pos); } else if (strcmp(buf, "private_key") == 0) { free(bss->private_key); bss->private_key = strdup(pos); } else if (strcmp(buf, "private_key_passwd") == 0) { free(bss->private_key_passwd); bss->private_key_passwd = strdup(pos); } else if (strcmp(buf, "check_crl") == 0) { bss->check_crl = atoi(pos);#ifdef EAP_SIM } else if (strcmp(buf, "eap_sim_db") == 0) { free(bss->eap_sim_db); bss->eap_sim_db = strdup(pos);#endif /* EAP_SIM */#endif /* EAP_SERVER */ } else if (strcmp(buf, "eap_message") == 0) { char *term; bss->eap_req_id_text = strdup(pos); if (bss->eap_req_id_text == NULL) { printf("Line %d: Failed to allocate memory " "for eap_req_id_text\n", line); errors++; continue; } bss->eap_req_id_text_len = strlen(bss->eap_req_id_text); term = strstr(bss->eap_req_id_text, "\\0"); if (term) { *term++ = '\0'; memmove(term, term + 1, bss->eap_req_id_text_len - (term - bss->eap_req_id_text) - 1); bss->eap_req_id_text_len--; } } else if (strcmp(buf, "wep_key_len_broadcast") == 0) { bss->default_wep_key_len = atoi(pos); if (bss->default_wep_key_len > 13) { printf("Line %d: invalid WEP key len %lu " "(= %lu bits)\n", line, (unsigned long) bss->default_wep_key_len, (unsigned long) bss->default_wep_key_len * 8); errors++; } } else if (strcmp(buf, "wep_key_len_unicast") == 0) { bss->individual_wep_key_len = atoi(pos); if (bss->individual_wep_key_len < 0 || bss->individual_wep_key_len > 13) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -