Exemplo n.º 1
0
struct hostapd_config * hostapd_config_read(const char *fname)
{
	struct hostapd_config *conf;
	FILE *f;
	char buf[256], *pos, *pos1;
	int idx, rate;
	enum { WEP_KEY_ASCII, WEP_KEY_HEX, WEP_KEY_UNKNOWN_TYPE };
	int line = 0, errors = 0, wep_key_type = 0;
	char *accept_mac_file = NULL, *deny_mac_file = NULL;

	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;
	}

	while (fgets(buf, sizeof(buf), f)) {
		line++;

		if (buf[0] == '#')
			continue;
		pos = buf;
		while (*pos != '\0') {
			if ((*pos == '\n') || (*pos == '\r')){
				*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->iface, sizeof(conf->iface), "%s", pos);
		} else if (strcmp(buf, "debug") == 0) {
			conf->debug = atoi(pos);
		} else if (strcmp(buf, "logger_syslog_level") == 0) {
			conf->logger_syslog_level = atoi(pos);
		} else if (strcmp(buf, "logger_stdout_level") == 0) {
			conf->logger_stdout_level = atoi(pos);
		} else if (strcmp(buf, "logger_syslog") == 0) {
			conf->logger_syslog = atoi(pos);
		} else if (strcmp(buf, "logger_stdout") == 0) {
			conf->logger_stdout = atoi(pos);
		} else if (strcmp(buf, "manuf_file") == 0) {
			conf->manuf_file = strdup(pos);
		} else if (strcmp(buf, "dump_file") == 0) {
			conf->dump_log_name = strdup(pos);
		} else if (strcmp(buf, "daemonize") == 0) {
			conf->daemonize = atoi(pos);
		} else if (strcmp(buf, "wireless_enable") == 0) {
			conf->wireless_enable = atoi(pos);
		} else if (strcmp(buf, "auto_link") == 0) {
			conf->auto_link = atoi(pos);
		} else if (strcmp(buf, "ssid_patch") == 0) {
			conf->ssid_patch = atoi(pos);
		} else if (strcmp(buf, "speed_booster") == 0) {
			conf->speed_booster = atoi(pos);
		} else if (strcmp(buf, "ap_mode") == 0) {
			conf->ap_mode = atoi(pos);
		} else if (strcmp(buf, "g_protect") == 0) {
			conf->g_protect = atoi(pos);
		} else if (strcmp(buf, "iw_mode") == 0) {
			conf->iw_mode = atoi(pos);
		} else if (strcmp(buf, "oper_rate") == 0) {
			idx = 0;
			pos1 = pos;
			while (*pos != '\0') {
				if (idx >= HOSTAPD_RATE_LEN)
					break;
				pos = strchr(pos1, ',');
				if (pos == NULL) {
					conf->oper_rate |=
						rate2index(atoi(pos1));
					break;
				}
				*pos++ = '\0';
				conf->oper_rate |= rate2index(atoi(pos1));
				pos1 = pos;
			}
		} else if (strcmp(buf, "basic_rate") == 0) {
			idx = 0;
			pos1 = pos;
			while (*pos != '\0') {
				if (idx >= HOSTAPD_RATE_LEN)
					break;
				pos = strchr(pos1, ',');
				if (pos == NULL) {
					conf->basic_rate |=
						rate2index(atoi(pos1));
					break;
				}
				*pos++ = '\0';
				conf->basic_rate |= rate2index(atoi(pos1));
				pos1 = pos;
			}
		} else if (strcmp(buf, "fixed_tx_data_rate") == 0) {
			conf->fixed_tx_data_rate = atoi(pos);
		} else if (strcmp(buf, "fixed_tx_b_rate") == 0) {
			conf->fixed_tx_b_rate = rate2idx(atoi(pos));
			if (conf->fixed_tx_b_rate < 0) {
				printf("Line %d: invalid rate '%d'\n", line,
					conf->fixed_tx_b_rate);
				errors++;
			}
		} else if (strcmp(buf, "fixed_tx_g_rate") == 0) {
			conf->fixed_tx_g_rate = rate2idx(atoi(pos));
			if (conf->fixed_tx_g_rate < 0) {
				printf("Line %d: invalid rate '%d'\n", line,
					conf->fixed_tx_g_rate);
				errors++;
			}
		} else if (strcmp(buf, "beacon_interval") == 0) {
			conf->beacon_interval = atoi(pos);
		} else if (strcmp(buf, "dtim_period") == 0) {
			conf->dtim_period = atoi(pos);
		} else if (strcmp(buf, "rts_threshold") == 0) {
			conf->rts_threshold = atoi(pos);
		} else if (strcmp(buf, "fragment_threshold") == 0) {
			conf->fragment_threshold = atoi(pos);
		} else if (strcmp(buf, "short_retry_limit") == 0) {
			conf->short_retry_limit = atoi(pos);
		} else if (strcmp(buf, "long_retry_limit") == 0) {
			conf->long_retry_limit = atoi(pos);
		} else if (strcmp(buf, "channel") == 0) {
			conf->channel = atoi(pos);
		} else if (strcmp(buf, "current_tx_power_level") == 0) {
			conf->current_tx_power_level = atoi(pos);
		} else if (strcmp(buf, "short_preamble") == 0) {
			conf->short_preamble = atoi(pos);
		} else if (strcmp(buf, "hide_ssid") == 0) {
			conf->hide_ssid = atoi(pos);
		} else if (strcmp(buf, "ssid") == 0) {
			conf->ssid_len = strlen(pos);
			if (conf->ssid_len > HOSTAPD_SSID_LEN ||
			    conf->ssid_len < 1) {
				printf("Line %d: invalid SSID '%s'\n", line,
				       pos);
				errors++;
			}
			memcpy(conf->ssid, pos, conf->ssid_len);
			conf->ssid[conf->ssid_len] = '\0';
		} else if (strcmp(buf, "macaddr_acl") == 0) {
			conf->macaddr_acl = atoi(pos);
			if (conf->macaddr_acl != ACCEPT_UNLESS_DENIED &&
			    conf->macaddr_acl != DENY_UNLESS_ACCEPTED &&
			    conf->macaddr_acl != ACL_NOT_APPLY &&
			    conf->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
				printf("Line %d: unknown macaddr_acl %d\n",
				       line, conf->macaddr_acl);
			}
		} else if (strcmp(buf, "accept_mac_file") == 0) {
			accept_mac_file = strdup(pos);
			if (!accept_mac_file) {
				printf("Line %d: allocation failed\n", line);
				errors++;
			}
		} else if (strcmp(buf, "deny_mac_file") == 0) {
			deny_mac_file = strdup(pos);
			if (!deny_mac_file) {
				printf("Line %d: allocation failed\n", line);
				errors++;
			}
		} else if (strcmp(buf, "assoc_ap_addr") == 0) {
			if (hwaddr_aton(pos, conf->assoc_ap_addr)) {
				printf("Line %d: invalid MAC address '%s'\n",
				       line, pos);
				errors++;
			}
			conf->assoc_ap = 1;
		} else if (strcmp(buf, "wpa_mode") == 0) {
			conf->wpa_mode = atoi(pos);
		} else if (strcmp(buf, "wpa_encry") == 0) {
			conf->wpa_encry = atoi(pos);
			if ((conf->wpa_encry != HOSTAPD_WPA_ENCRY_TKIP) &&
			    (conf->wpa_encry != HOSTAPD_WPA_ENCRY_CCMP)) {
			    	printf("Line %d: invalid WPA encryption method %d\n",
				       line, conf->wpa_encry);
			}
		} else if (strcmp(buf, "wpa_passphrase") == 0) {
			int len = strlen(pos);
			if (len < 8 || len > 63) {
				printf("Line %d: invalid WPA passphrase length"
				       "%d (expected 8..63)\n", line, len);
				errors++;
			} else
				conf->wpa_passphrase = strdup(pos);
#ifndef AP_WPA2
		} else if (strcmp(buf, "wpa_group_rekey_time") == 0) {
			conf->wpa_group_rekey_time = atoi(pos);
#else
		} else if (strcmp(buf, "group_rekey_time") == 0) {
			conf->group_rekey_time = atoi(pos);
		} else if (strcmp(buf, "wpa2_mode") == 0) {
			conf->wpa2_mode = atoi(pos);
		} else if (strcmp(buf, "wpa2_encry") == 0) {
			conf->wpa2_encry = atoi(pos);
#if 0
			if ((conf->wpa2_encry != HOSTAPD_WPA2_ENCRY_TKIP_OR_CCMP) &&
			    (conf->wpa2_encry != HOSTAPD_WPA2_ENCRY_CCMP_ONLY)) {
#else
			if (conf->wpa2_encry != HOSTAPD_WPA2_ENCRY_CCMP_ONLY) {
#endif
			    	printf("Line %d: invalid WPA2 encryption method %d\n",
				       line, conf->wpa2_encry);
			}
		} else if (strcmp(buf, "wpa2_passphrase") == 0) {
			int len = strlen(pos);
			if (len < 8 || len > 63) {
				printf("Line %d: invalid WPA2 passphrase length"
				       "%d (expected 8..63)\n", line, len);
				errors++;
			} else
				conf->wpa2_passphrase = strdup(pos);
#endif
		} else if (strcmp(buf, "wlan_tx_gpio") == 0) {
			conf->wlan_tx_gpio = atoi(pos);
		} else if (strcmp(buf, "wlan_rx_gpio") == 0) {
			conf->wlan_rx_gpio = atoi(pos);
		} else if (strcmp(buf, "antenna") == 0) {
			conf->antenna = atoi(pos);
		} else if (strcmp(buf, "watchdog_timer") == 0) {
			conf->watchdog_timer = atoi(pos);
		} else if (strcmp(buf, "ieee8021x") == 0) {
			conf->ieee802_1x = atoi(pos);
		} else if (strcmp(buf, "minimal_eap") == 0) {
			conf->minimal_eap = atoi(pos);
		} else if (strcmp(buf, "eap_message") == 0) {
			conf->eap_req_id_text = strdup(pos);
		} else if (strcmp(buf, "wep_key_len") == 0) {
			conf->wep_key_len = atoi(pos);
			if (conf->wep_key_len != 5 &&
				conf->wep_key_len != 13) {
				printf("Line %d: invalid WEP key len %d "
				       "(= %d bits)\n", line,
				       conf->wep_key_len,
				       conf->wep_key_len * 8);
				errors++;
			}
		} else if (strcmp(buf, "wep_default_key") == 0) {
			conf->default_wep_key = atoi(pos);
			if (conf->default_wep_key < 1 ||
			    conf->default_wep_key > 4) {
				printf("Line %d: invalid default WEP key %d\n",
				       line, conf->default_wep_key);
				errors++;
			}
		} else if (strcmp(buf, "wep_key_type") == 0) {
			if (strcmp(pos, "ascii") == 0) {
				wep_key_type = WEP_KEY_ASCII;
			} else if (strcmp(pos, "hex") == 0) {
				wep_key_type = WEP_KEY_HEX;
			} else {
				wep_key_type = WEP_KEY_UNKNOWN_TYPE;
				printf("Line %d: invalid WEP key type %s\n",
				       line, pos);
				errors++;
			}
		} else if (strcmp(buf, "wep_default_key1") == 0) {
			conf->default_wep_key1 = strdup(pos);
		} else if (strcmp(buf, "wep_default_key2") == 0) {
			conf->default_wep_key2 = strdup(pos);
		} else if (strcmp(buf, "wep_default_key3") == 0) {
			conf->default_wep_key3 = strdup(pos);
		} else if (strcmp(buf, "wep_default_key4") == 0) {
			conf->default_wep_key4 = strdup(pos);
		} else if (strcmp(buf, "wep_rekey_period") == 0) {
			conf->wep_rekeying_period = atoi(pos);
			if (conf->wep_rekeying_period < 0) {
				printf("Line %d: invalid period %d\n",
				       line, conf->wep_rekeying_period);
				errors++;
			}
		} else if (strcmp(buf, "privacy_invoked") == 0) {
			conf->privacy_invoked = atoi(pos);
		} else if (strcmp(buf, "exclude_unencrypted") == 0) {
			conf->exclude_unencrypted = atoi(pos);
		} else if (strcmp(buf, "eapol_key_index_workaround") == 0) {
			conf->eapol_key_index_workaround = atoi(pos);
		} else if (strcmp(buf, "iapp_interface") == 0) {
			conf->ieee802_11f = 1;
			snprintf(conf->iapp_iface, sizeof(conf->iapp_iface),
				 "%s", pos);
		} else if (strcmp(buf, "own_ip_addr") == 0) {
			if (!inet_aton(pos, &conf->own_ip_addr)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (strcmp(buf, "auth_server_addr") == 0) {
			if (hostapd_config_read_radius_addr(
				    &conf->auth_servers,
				    &conf->num_auth_servers, pos, 1812,
				    &conf->auth_server)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (conf->auth_server &&
			   strcmp(buf, "auth_server_port") == 0) {
			conf->auth_server->port = atoi(pos);
		} else if (conf->auth_server &&
			   strcmp(buf, "auth_server_shared_secret") == 0) {
			int len = strlen(pos);
			if (len == 0) {
				/* RFC 2865, Ch. 3 */
				printf("Line %d: empty shared secret is not "
				       "allowed.\n", line);
				errors++;
			}
			conf->auth_server->shared_secret = strdup(pos);
			conf->auth_server->shared_secret_len = len;
		} else if (strcmp(buf, "acct_server_addr") == 0) {
			if (hostapd_config_read_radius_addr(
				    &conf->acct_servers,
				    &conf->num_acct_servers, pos, 1813,
				    &conf->acct_server)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (conf->acct_server &&
			   strcmp(buf, "acct_server_port") == 0) {
			conf->acct_server->port = atoi(pos);
		} else if (conf->acct_server &&
			   strcmp(buf, "acct_server_shared_secret") == 0) {
			int len = strlen(pos);
			if (len == 0) {
				/* RFC 2865, Ch. 3 */
				printf("Line %d: empty shared secret is not "
				       "allowed.\n", line);
				errors++;
			}
			conf->acct_server->shared_secret = strdup(pos);
			conf->acct_server->shared_secret_len = len;
		} else if (strcmp(buf, "radius_retry_primary_interval") == 0) {
			conf->radius_retry_primary_interval = atoi(pos);
		} else if (strcmp(buf, "radius_acct_interim_interval") == 0) {
			conf->radius_acct_interim_interval = atoi(pos);
		} else if (strcmp(buf, "auth_algs") == 0) {
			conf->auth_algs = atoi(pos);
        } else if (strcmp(buf, "op_mode") == 0) {
			conf->op_mode = atoi(pos);
         } else if (strcmp(buf, "sta_bridge") == 0) {
			conf->sta_bridge = atoi(pos);            
		} else {
			printf("Line %d: unknown configuration item '%s'\n",
			       line, buf);
			errors++;
		}
	}

	if ((conf->basic_rate & conf->oper_rate) != conf->basic_rate) {
			printf("Configured basic rate set mismatches configured"
			       " operational rate set\n");
			errors++;
	}

	if (((conf->fixed_tx_b_rate & conf->oper_rate) != conf->fixed_tx_b_rate) ||
		((conf->fixed_tx_g_rate & conf->oper_rate) != conf->fixed_tx_g_rate)) {
			printf("Configured fixed transmit rate mismatches any "
			       "configured operational rate set\n");
			errors++;
	}

	/* Hexdecimal WEP key conversion */
	if (wep_key_type == WEP_KEY_HEX) {
		for (idx = 0; idx < 4; idx++) {
			char *wep_key;

			switch (idx) {
			case 0:
				wep_key = conf->default_wep_key1;
				break;
			case 1:
				wep_key = conf->default_wep_key2;
				break;
			case 2:
				wep_key = conf->default_wep_key3;
				break;
			case 3:
				wep_key = conf->default_wep_key4;
				break;
			}
			if (wep_key && wep_key[0]) {
				if ((conf->wep_key_len == 5) ||
				    (conf->wep_key_len == 13)) {
					int hex_len = (2 * conf->wep_key_len);
					int i1 = 0, i2 = 0, num1, num2;

					if (strlen(wep_key) != hex_len) {
						printf("WEP key %d length "
						       "mismatches\n", idx + 1);
						errors++;
						break;
					}

					while (hex_len) {
						num1 = hex2num(wep_key[i1++]);
						num2 = hex2num(wep_key[i1++]);
						if (num1 < 0 || num2 < 0) {
							printf("WEP key %d "
							       "has invalid "
							       "characters\n",
							       idx + 1);
							errors++;
							break;
						}
						num1 <<= 4;
						buf[i2++] = num1 | num2;
						hex_len -= 2;
					}

					if (! hex_len) {
						memcpy(wep_key, buf,
						       conf->wep_key_len);
						wep_key[conf->wep_key_len] = 0;
					}
				}
			}
		}
	}

	fclose(f);

	if (hostapd_config_read_maclist(accept_mac_file, &conf->accept_mac,
					&conf->num_accept_mac))
		errors++;
	free(accept_mac_file);
	if (hostapd_config_read_maclist(deny_mac_file, &conf->deny_mac,
					&conf->num_deny_mac))
		errors++;
	free(deny_mac_file);

	conf->auth_server = conf->auth_servers;
	conf->acct_server = conf->acct_servers;

	if (hostapd_config_check(conf))
		errors++;

	if (errors) {
		printf("%d errors found in configuration file '%s'\n",
		       errors, fname);
#if 0
		hostapd_config_free(conf);
		conf = NULL;
#endif
	}

	return conf;
}


static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
				       int num_servers)
{
	int i;

	for (i = 0; i < num_servers; i++) {
		free(servers[i].shared_secret);
	}
	free(servers);
}
Exemplo n.º 2
0
struct hostapd_config * hostapd_config_read(const char *fname)
{
	struct hostapd_config *conf;
	FILE *f;
	char buf[256], *pos;
	int line = 0;
	int errors = 0;
	char *accept_mac_file = NULL, *deny_mac_file = NULL;

	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;
	}

	while (fgets(buf, sizeof(buf), f)) {
		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->iface, sizeof(conf->iface), "%s", pos);
		} else if (strcmp(buf, "debug") == 0) {
			conf->debug = atoi(pos);
		} else if (strcmp(buf, "logger_syslog_level") == 0) {
			conf->logger_syslog_level = atoi(pos);
		} else if (strcmp(buf, "logger_stdout_level") == 0) {
			conf->logger_stdout_level = atoi(pos);
		} else if (strcmp(buf, "logger_syslog") == 0) {
			conf->logger_syslog = atoi(pos);
		} else if (strcmp(buf, "logger_stdout") == 0) {
			conf->logger_stdout = atoi(pos);
		} else if (strcmp(buf, "dump_file") == 0) {
			conf->dump_log_name = strdup(pos);
		} else if (strcmp(buf, "daemonize") == 0) {
			conf->daemonize = atoi(pos);
		} else if (strcmp(buf, "ssid") == 0) {
			conf->ssid_len = strlen(pos);
			if (conf->ssid_len >= HOSTAPD_SSID_LEN ||
			    conf->ssid_len < 1) {
				printf("Line %d: invalid SSID '%s'\n", line,
				       pos);
				errors++;
			}
			memcpy(conf->ssid, pos, conf->ssid_len);
			conf->ssid[conf->ssid_len] = '\0';
		} else if (strcmp(buf, "macaddr_acl") == 0) {
			conf->macaddr_acl = atoi(pos);
			if (conf->macaddr_acl != ACCEPT_UNLESS_DENIED &&
			    conf->macaddr_acl != DENY_UNLESS_ACCEPTED &&
			    conf->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
				printf("Line %d: unknown macaddr_acl %d\n",
				       line, conf->macaddr_acl);
			}
		} else if (strcmp(buf, "accept_mac_file") == 0) {
			accept_mac_file = strdup(pos);
			if (!accept_mac_file) {
				printf("Line %d: allocation failed\n", line);
				errors++;
			}
		} else if (strcmp(buf, "deny_mac_file") == 0) {
			deny_mac_file = strdup(pos);
			if (!deny_mac_file) {
				printf("Line %d: allocation failed\n", line);
				errors++;
			}
		} else if (strcmp(buf, "assoc_ap_addr") == 0) {
			if (hwaddr_aton(pos, conf->assoc_ap_addr)) {
				printf("Line %d: invalid MAC address '%s'\n",
				       line, pos);
				errors++;
			}
			conf->assoc_ap = 1;
		} else if (strcmp(buf, "ieee8021x") == 0) {
			conf->ieee802_1x = atoi(pos);
		} else if (strcmp(buf, "minimal_eap") == 0) {
			conf->minimal_eap = atoi(pos);
		} else if (strcmp(buf, "eap_message") == 0) {
			conf->eap_req_id_text = strdup(pos);
		} else if (strcmp(buf, "wep_key_len_broadcast") == 0) {
			conf->default_wep_key_len = atoi(pos);
			if (conf->default_wep_key_len > 13) {
				printf("Line %d: invalid WEP key len %d "
				       "(= %d bits)\n", line,
				       conf->default_wep_key_len,
				       conf->default_wep_key_len * 8);
				errors++;
			}
		} else if (strcmp(buf, "wep_key_len_unicast") == 0) {
			conf->individual_wep_key_len = atoi(pos);
			if (conf->individual_wep_key_len < 0 ||
			    conf->individual_wep_key_len > 13) {
				printf("Line %d: invalid WEP key len %d "
				       "(= %d bits)\n", line,
				       conf->individual_wep_key_len,
				       conf->individual_wep_key_len * 8);
				errors++;
			}
		} else if (strcmp(buf, "wep_rekey_period") == 0) {
			conf->wep_rekeying_period = atoi(pos);
			if (conf->wep_rekeying_period < 0) {
				printf("Line %d: invalid period %d\n",
				       line, conf->wep_rekeying_period);
				errors++;
			}
		} else if (strcmp(buf, "eapol_key_index_workaround") == 0) {
			conf->eapol_key_index_workaround = atoi(pos);
		} else if (strcmp(buf, "iapp_interface") == 0) {
			conf->ieee802_11f = 1;
			snprintf(conf->iapp_iface, sizeof(conf->iapp_iface),
				 "%s", pos);
		} else if (strcmp(buf, "own_ip_addr") == 0) {
			if (!inet_aton(pos, &conf->own_ip_addr)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (strcmp(buf, "auth_server_addr") == 0) {
			if (hostapd_config_read_radius_addr(
				    &conf->auth_servers,
				    &conf->num_auth_servers, pos, 1812,
				    &conf->auth_server)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (conf->auth_server &&
			   strcmp(buf, "auth_server_port") == 0) {
			conf->auth_server->port = atoi(pos);
		} else if (conf->auth_server &&
			   strcmp(buf, "auth_server_shared_secret") == 0) {
			int len = strlen(pos);
			if (len == 0) {
				/* RFC 2865, Ch. 3 */
				printf("Line %d: empty shared secret is not "
				       "allowed.\n", line);
				errors++;
			}
			conf->auth_server->shared_secret = strdup(pos);
			conf->auth_server->shared_secret_len = len;
		} else if (strcmp(buf, "acct_server_addr") == 0) {
			if (hostapd_config_read_radius_addr(
				    &conf->acct_servers,
				    &conf->num_acct_servers, pos, 1813,
				    &conf->acct_server)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (conf->acct_server &&
			   strcmp(buf, "acct_server_port") == 0) {
			conf->acct_server->port = atoi(pos);
		} else if (conf->acct_server &&
			   strcmp(buf, "acct_server_shared_secret") == 0) {
			int len = strlen(pos);
			if (len == 0) {
				/* RFC 2865, Ch. 3 */
				printf("Line %d: empty shared secret is not "
				       "allowed.\n", line);
				errors++;
			}
			conf->acct_server->shared_secret = strdup(pos);
			conf->acct_server->shared_secret_len = len;
		} else if (strcmp(buf, "radius_retry_primary_interval") == 0) {
			conf->radius_retry_primary_interval = atoi(pos);
		} else if (strcmp(buf, "radius_acct_interim_interval") == 0) {
			conf->radius_acct_interim_interval = atoi(pos);
		} else if (strcmp(buf, "auth_algs") == 0) {
			conf->auth_algs = atoi(pos);
		} else {
			printf("Line %d: unknown configuration item '%s'\n",
			       line, buf);
			errors++;
		}
	}

	fclose(f);

	if (hostapd_config_read_maclist(accept_mac_file, &conf->accept_mac,
					&conf->num_accept_mac))
		errors++;
	free(accept_mac_file);
	if (hostapd_config_read_maclist(deny_mac_file, &conf->deny_mac,
					&conf->num_deny_mac))
		errors++;
	free(deny_mac_file);

	conf->auth_server = conf->auth_servers;
	conf->acct_server = conf->acct_servers;

	if (hostapd_config_check(conf))
		errors++;

	if (errors) {
		printf("%d errors found in configuration file '%s'\n",
		       errors, fname);
		hostapd_config_free(conf);
		conf = NULL;
	}

	return conf;
}
Exemplo n.º 3
0
struct hostapd_config * hostapd_config_read(const char *fname)
{
	struct hostapd_config *conf;
	FILE *f;
	char buf[256], *pos;
	int line = 0;
	int errors = 0;
	char *accept_mac_file = NULL, *deny_mac_file = NULL;
#ifdef EAP_SERVER
	char *eap_user_file = NULL;
#endif /* EAP_SERVER */

	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;
	}

	while (fgets(buf, sizeof(buf), f)) {
		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->iface, sizeof(conf->iface), "%s", pos);
		} else if (strcmp(buf, "bridge") == 0) {
			snprintf(conf->bridge, sizeof(conf->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) {
			conf->debug = atoi(pos);
		} else if (strcmp(buf, "logger_syslog_level") == 0) {
			conf->logger_syslog_level = atoi(pos);
		} else if (strcmp(buf, "logger_stdout_level") == 0) {
			conf->logger_stdout_level = atoi(pos);
		} else if (strcmp(buf, "logger_syslog") == 0) {
			conf->logger_syslog = atoi(pos);
		} else if (strcmp(buf, "logger_stdout") == 0) {
			conf->logger_stdout = atoi(pos);
		} else if (strcmp(buf, "dump_file") == 0) {
			conf->dump_log_name = strdup(pos);
		} else if (strcmp(buf, "ssid") == 0) {
			conf->ssid_len = strlen(pos);
			if (conf->ssid_len > HOSTAPD_SSID_LEN ||
			    conf->ssid_len < 1) {
				printf("Line %d: invalid SSID '%s'\n", line,
				       pos);
				errors++;
			}
			memcpy(conf->ssid, pos, conf->ssid_len);
			conf->ssid[conf->ssid_len] = '\0';
			conf->ssid_set = 1;
		} else if (strcmp(buf, "macaddr_acl") == 0) {
			conf->macaddr_acl = atoi(pos);
			if (conf->macaddr_acl != ACCEPT_UNLESS_DENIED &&
			    conf->macaddr_acl != DENY_UNLESS_ACCEPTED &&
			    conf->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
				printf("Line %d: unknown macaddr_acl %d\n",
				       line, conf->macaddr_acl);
			}
		} else if (strcmp(buf, "accept_mac_file") == 0) {
			accept_mac_file = strdup(pos);
			if (!accept_mac_file) {
				printf("Line %d: allocation failed\n", line);
				errors++;
			}
		} else if (strcmp(buf, "deny_mac_file") == 0) {
			deny_mac_file = strdup(pos);
			if (!deny_mac_file) {
				printf("Line %d: allocation failed\n", line);
				errors++;
			}
		} else if (strcmp(buf, "assoc_ap_addr") == 0) {
			if (hwaddr_aton(pos, conf->assoc_ap_addr)) {
				printf("Line %d: invalid MAC address '%s'\n",
				       line, pos);
				errors++;
			}
			conf->assoc_ap = 1;
		} else if (strcmp(buf, "ieee8021x") == 0) {
			conf->ieee802_1x = atoi(pos);
#ifdef EAP_SERVER
		} else if (strcmp(buf, "eap_authenticator") == 0) {
			conf->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) {
			conf->eap_server = atoi(pos);
		} else if (strcmp(buf, "eap_user_file") == 0) {
			free(eap_user_file);
			eap_user_file = strdup(pos);
			if (!eap_user_file) {
				printf("Line %d: allocation failed\n", line);
				errors++;
			}
		} else if (strcmp(buf, "ca_cert") == 0) {
			free(conf->ca_cert);
			conf->ca_cert = strdup(pos);
		} else if (strcmp(buf, "server_cert") == 0) {
			free(conf->server_cert);
			conf->server_cert = strdup(pos);
		} else if (strcmp(buf, "private_key") == 0) {
			free(conf->private_key);
			conf->private_key = strdup(pos);
		} else if (strcmp(buf, "private_key_passwd") == 0) {
			free(conf->private_key_passwd);
			conf->private_key_passwd = strdup(pos);
		} else if (strcmp(buf, "check_crl") == 0) {
			conf->check_crl = atoi(pos);
#ifdef EAP_SIM
		} else if (strcmp(buf, "eap_sim_db") == 0) {
			free(conf->eap_sim_db);
			conf->eap_sim_db = strdup(pos);
#endif /* EAP_SIM */
#endif /* EAP_SERVER */
		} else if (strcmp(buf, "eap_message") == 0) {
			char *term;
			conf->eap_req_id_text = strdup(pos);
			if (conf->eap_req_id_text == NULL) {
				printf("Line %d: Failed to allocate memory "
				       "for eap_req_id_text\n", line);
				errors++;
				continue;
			}
			conf->eap_req_id_text_len =
				strlen(conf->eap_req_id_text);
			term = strstr(conf->eap_req_id_text, "\\0");
			if (term) {
				*term++ = '\0';
				memmove(term, term + 1,
					conf->eap_req_id_text_len -
					(term - conf->eap_req_id_text) - 1);
				conf->eap_req_id_text_len--;
			}
		} else if (strcmp(buf, "wep_key_len_broadcast") == 0) {
			conf->default_wep_key_len = atoi(pos);
			if (conf->default_wep_key_len > 13) {
				printf("Line %d: invalid WEP key len %lu "
				       "(= %lu bits)\n", line,
				       (unsigned long)
				       conf->default_wep_key_len,
				       (unsigned long)
				       conf->default_wep_key_len * 8);
				errors++;
			}
		} else if (strcmp(buf, "wep_key_len_unicast") == 0) {
			conf->individual_wep_key_len = atoi(pos);
			if (conf->individual_wep_key_len < 0 ||
			    conf->individual_wep_key_len > 13) {
				printf("Line %d: invalid WEP key len %d "
				       "(= %d bits)\n", line,
				       conf->individual_wep_key_len,
				       conf->individual_wep_key_len * 8);
				errors++;
			}
		} else if (strcmp(buf, "wep_rekey_period") == 0) {
			conf->wep_rekeying_period = atoi(pos);
			if (conf->wep_rekeying_period < 0) {
				printf("Line %d: invalid period %d\n",
				       line, conf->wep_rekeying_period);
				errors++;
			}
		} else if (strcmp(buf, "eap_reauth_period") == 0) {
			conf->eap_reauth_period = atoi(pos);
			if (conf->eap_reauth_period < 0) {
				printf("Line %d: invalid period %d\n",
				       line, conf->eap_reauth_period);
				errors++;
			}
		} else if (strcmp(buf, "eapol_key_index_workaround") == 0) {
			conf->eapol_key_index_workaround = atoi(pos);
#ifdef CONFIG_IAPP
		} else if (strcmp(buf, "iapp_interface") == 0) {
			conf->ieee802_11f = 1;
			snprintf(conf->iapp_iface, sizeof(conf->iapp_iface),
				 "%s", pos);
#endif /* CONFIG_IAPP */
		} else if (strcmp(buf, "own_ip_addr") == 0) {
			if (hostapd_parse_ip_addr(pos, &conf->own_ip_addr)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (strcmp(buf, "nas_identifier") == 0) {
			conf->nas_identifier = strdup(pos);
		} else if (strcmp(buf, "auth_server_addr") == 0) {
			if (hostapd_config_read_radius_addr(
				    &conf->radius->auth_servers,
				    &conf->radius->num_auth_servers, pos, 1812,
				    &conf->radius->auth_server)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (conf->radius->auth_server &&
			   strcmp(buf, "auth_server_port") == 0) {
			conf->radius->auth_server->port = atoi(pos);
		} else if (conf->radius->auth_server &&
			   strcmp(buf, "auth_server_shared_secret") == 0) {
			int len = strlen(pos);
			if (len == 0) {
				/* RFC 2865, Ch. 3 */
				printf("Line %d: empty shared secret is not "
				       "allowed.\n", line);
				errors++;
			}
			conf->radius->auth_server->shared_secret =
				(u8 *) strdup(pos);
			conf->radius->auth_server->shared_secret_len = len;
		} else if (strcmp(buf, "acct_server_addr") == 0) {
			if (hostapd_config_read_radius_addr(
				    &conf->radius->acct_servers,
				    &conf->radius->num_acct_servers, pos, 1813,
				    &conf->radius->acct_server)) {
				printf("Line %d: invalid IP address '%s'\n",
				       line, pos);
				errors++;
			}
		} else if (conf->radius->acct_server &&
			   strcmp(buf, "acct_server_port") == 0) {
			conf->radius->acct_server->port = atoi(pos);
		} else if (conf->radius->acct_server &&
			   strcmp(buf, "acct_server_shared_secret") == 0) {
			int len = strlen(pos);
			if (len == 0) {
				/* RFC 2865, Ch. 3 */
				printf("Line %d: empty shared secret is not "
				       "allowed.\n", line);
				errors++;
			}
			conf->radius->acct_server->shared_secret =
				(u8 *) strdup(pos);
			conf->radius->acct_server->shared_secret_len = len;
		} else if (strcmp(buf, "radius_so_mark") == 0) {
		    conf->radius->so_mark = atoi(pos);
		} else if (strcmp(buf, "radius_retry_primary_interval") == 0) {
			conf->radius->retry_primary_interval = atoi(pos);
		} else if (strcmp(buf, "radius_acct_interim_interval") == 0) {
			conf->radius->acct_interim_interval = atoi(pos);
		} else if (strcmp(buf, "auth_algs") == 0) {
			conf->auth_algs = atoi(pos);
			if (conf->auth_algs == 0) {
				printf("Line %d: no authentication algorithms "
				       "allowed\n",
				       line);
				errors++;
			}
		} else if (strcmp(buf, "wpa") == 0) {
			conf->wpa = atoi(pos);
		} else if (strcmp(buf, "wpa_group_rekey") == 0) {
			conf->wpa_group_rekey = atoi(pos);
		} else if (strcmp(buf, "wpa_strict_rekey") == 0) {
			conf->wpa_strict_rekey = atoi(pos);
		} else if (strcmp(buf, "wpa_gmk_rekey") == 0) {
			conf->wpa_gmk_rekey = atoi(pos);
		} else if (strcmp(buf, "wpa_passphrase") == 0) {
			int len = strlen(pos);
			if (len < 8 || len > 63) {
				printf("Line %d: invalid WPA passphrase length"
				       " %d (expected 8..63)\n", line, len);
				errors++;
			} else {
				free(conf->wpa_passphrase);
				conf->wpa_passphrase = strdup(pos);
			}
		} else if (strcmp(buf, "wpa_psk") == 0) {
			free(conf->wpa_psk);
			conf->wpa_psk = malloc(sizeof(struct hostapd_wpa_psk));
			if (conf->wpa_psk) {
				memset(conf->wpa_psk, 0,
				       sizeof(struct hostapd_wpa_psk));
			}
			if (conf->wpa_psk == NULL)
				errors++;
			else if (hexstr2bin(pos, conf->wpa_psk->psk, PMK_LEN)
				 || pos[PMK_LEN * 2] != '\0') {
				printf("Line %d: Invalid PSK '%s'.\n", line,
				       pos);
				errors++;
			} else {
				conf->wpa_psk->group = 1;
			}
		} else if (strcmp(buf, "wpa_psk_file") == 0) {
			free(conf->wpa_psk_file);
			conf->wpa_psk_file = strdup(pos);
			if (!conf->wpa_psk_file) {
				printf("Line %d: allocation failed\n", line);
				errors++;
			}
		} else if (strcmp(buf, "wpa_key_mgmt") == 0) {
			conf->wpa_key_mgmt =
				hostapd_config_parse_key_mgmt(line, pos);
			if (conf->wpa_key_mgmt == -1)
				errors++;
		} else if (strcmp(buf, "wpa_pairwise") == 0) {
			conf->wpa_pairwise =
				hostapd_config_parse_cipher(line, pos);
			if (conf->wpa_pairwise == -1 ||
			    conf->wpa_pairwise == 0)
				errors++;
			else if (conf->wpa_pairwise &
				 (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 |
				  WPA_CIPHER_WEP104)) {
				printf("Line %d: unsupported pairwise "
				       "cipher suite '%s'\n",
				       conf->wpa_pairwise, pos);
				errors++;
			} else {
				if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
					conf->wpa_group = WPA_CIPHER_TKIP;
				else
					conf->wpa_group = WPA_CIPHER_CCMP;
			}
#ifdef CONFIG_RSN_PREAUTH
		} else if (strcmp(buf, "rsn_preauth") == 0) {
			conf->rsn_preauth = atoi(pos);
		} else if (strcmp(buf, "rsn_preauth_interfaces") == 0) {
			conf->rsn_preauth_interfaces = strdup(pos);
#endif /* CONFIG_RSN_PREAUTH */
		} else if (strcmp(buf, "ctrl_interface") == 0) {
			free(conf->ctrl_interface);
			conf->ctrl_interface = strdup(pos);
		} else if (strcmp(buf, "ctrl_interface_group") == 0) {
			struct group *grp;
			char *endp;
			const char *group = pos;

			grp = getgrnam(group);
			if (grp) {
				conf->ctrl_interface_gid = grp->gr_gid;
				conf->ctrl_interface_gid_set = 1;
				wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
					   " (from group name '%s')",
					   conf->ctrl_interface_gid, group);
				continue;
			}

			/* Group name not found - try to parse this as gid */
			conf->ctrl_interface_gid = strtol(group, &endp, 10);
			if (*group == '\0' || *endp != '\0') {
				wpa_printf(MSG_DEBUG, "Line %d: Invalid group "
					   "'%s'", line, group);
				errors++;
				continue;
			}
			conf->ctrl_interface_gid_set = 1;
			wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
				   conf->ctrl_interface_gid);
#ifdef RADIUS_SERVER
		} else if (strcmp(buf, "radius_server_clients") == 0) {
			free(conf->radius_server_clients);
			conf->radius_server_clients = strdup(pos);
		} else if (strcmp(buf, "radius_server_auth_port") == 0) {
			conf->radius_server_auth_port = atoi(pos);
		} else if (strcmp(buf, "radius_server_ipv6") == 0) {
			conf->radius_server_ipv6 = atoi(pos);
#endif /* RADIUS_SERVER */

#ifdef SIMPLE_CONFIG
		} else if (strcmp(buf, "wsc_auth_mode") == 0) {
			conf->wsc_auth_mode = atoi(pos);
                } else if (strcmp(buf, "wsc_wep_key") == 0) {
                        snprintf(conf->wsc_wep_key, sizeof(conf->wsc_wep_key), "%s", pos);
#endif
		} else if (strcmp(buf, "test_socket") == 0) {
			free(conf->test_socket);
			conf->test_socket = strdup(pos);
		} else if (strcmp(buf, "use_pae_group_addr") == 0) {
			conf->use_pae_group_addr = atoi(pos);
		} else {
			printf("Line %d: unknown configuration item '%s'\n",
			       line, buf);
			errors++;
		}
	}

	fclose(f);

	if (hostapd_config_read_maclist(accept_mac_file, &conf->accept_mac,
					&conf->num_accept_mac))
		errors++;
	free(accept_mac_file);
	if (hostapd_config_read_maclist(deny_mac_file, &conf->deny_mac,
					&conf->num_deny_mac))
		errors++;
	free(deny_mac_file);

#ifdef EAP_SERVER
	if (hostapd_config_read_eap_user(eap_user_file, conf))
		errors++;
	free(eap_user_file);
#endif /* EAP_SERVER */

	conf->radius->auth_server = conf->radius->auth_servers;
	conf->radius->acct_server = conf->radius->acct_servers;

	if (hostapd_config_check(conf))
		errors++;

	if (errors) {
		printf("%d errors found in configuration file '%s'\n",
		       errors, fname);
		hostapd_config_free(conf);
		conf = NULL;
	}

	return conf;
}