⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.c

📁 hostapd源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * hostapd / Configuration file * Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Alternatively, this software may be distributed under the terms of BSD * license. * * See README and COPYING for more details. */#include "includes.h"#ifndef CONFIG_NATIVE_WINDOWS#include <grp.h>#endif /* CONFIG_NATIVE_WINDOWS */#include "hostapd.h"#include "driver.h"#include "sha1.h"#include "eap.h"#include "radius_client.h"#include "wpa_common.h"static void hostapd_config_defaults_bss(struct hostapd_bss_config *bss){	bss->radius = (struct hostapd_radius_servers *) (bss + 1);	bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;	bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;	bss->logger_syslog = (unsigned int) -1;	bss->logger_stdout = (unsigned int) -1;	bss->auth_algs = HOSTAPD_AUTH_OPEN | HOSTAPD_AUTH_SHARED_KEY;	bss->wep_rekeying_period = 300;	bss->eap_reauth_period = 3600;	bss->wpa_group_rekey = 600;	bss->wpa_gmk_rekey = 86400;	bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;	bss->wpa_pairwise = WPA_CIPHER_TKIP;	bss->wpa_group = WPA_CIPHER_TKIP;	bss->radius_server_auth_port = 1812;	bss->ap_max_inactivity = AP_MAX_INACTIVITY;	bss->eapol_version = EAPOL_VERSION;}static struct hostapd_config * hostapd_config_defaults(void){	struct hostapd_config *conf;	struct hostapd_bss_config *bss;	conf = wpa_zalloc(sizeof(*conf));	bss = wpa_zalloc(sizeof(*bss) + sizeof(struct hostapd_radius_servers));	if (conf == NULL || bss == NULL) {		printf("Failed to allocate memory for configuration data.\n");		free(conf);		free(bss);		return NULL;	}	/* set default driver based on configuration */	conf->driver = driver_lookup("default");	if (conf->driver == NULL) {		printf("No default driver registered!\n");		free(conf);		free(bss);		return NULL;	}	hostapd_config_defaults_bss(bss);	conf->num_bss = 1;	conf->bss = bss;	conf->beacon_int = 100;	return conf;}static int hostapd_parse_ip_addr(const char *txt, struct hostapd_ip_addr *addr){	if (inet_aton(txt, &addr->u.v4)) {		addr->af = AF_INET;		return 0;	}#ifdef CONFIG_IPV6	if (inet_pton(AF_INET6, txt, &addr->u.v6) > 0) {		addr->af = AF_INET6;		return 0;	}#endif /* CONFIG_IPV6 */	return -1;}static int mac_comp(const void *a, const void *b){	return memcmp(a, b, sizeof(macaddr));}static int hostapd_config_read_maclist(const char *fname, macaddr **acl,				       int *num){	FILE *f;	char buf[128], *pos;	int line = 0;	u8 addr[ETH_ALEN];	macaddr *newacl;	if (!fname)		return 0;	f = fopen(fname, "r");	if (!f) {		printf("MAC list file '%s' not found.\n", fname);		return -1;	}	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;		if (hwaddr_aton(buf, addr)) {			printf("Invalid MAC address '%s' at line %d in '%s'\n",			       buf, line, fname);			fclose(f);			return -1;		}		newacl = (macaddr *) realloc(*acl, (*num + 1) * ETH_ALEN);		if (newacl == NULL) {			printf("MAC list reallocation failed\n");			fclose(f);			return -1;		}		*acl = newacl;		memcpy((*acl)[*num], addr, ETH_ALEN);		(*num)++;	}	fclose(f);	qsort(*acl, *num, sizeof(macaddr), mac_comp);	return 0;}static int hostapd_config_read_wpa_psk(const char *fname,				       struct hostapd_ssid *ssid){	FILE *f;	char buf[128], *pos;	int line = 0, ret = 0, len, ok;	u8 addr[ETH_ALEN];	struct hostapd_wpa_psk *psk;	if (!fname)		return 0;	f = fopen(fname, "r");	if (!f) {		printf("WPA PSK file '%s' not found.\n", fname);		return -1;	}	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;		if (hwaddr_aton(buf, addr)) {			printf("Invalid MAC address '%s' on line %d in '%s'\n",			       buf, line, fname);			ret = -1;			break;		}		psk = wpa_zalloc(sizeof(*psk));		if (psk == NULL) {			printf("WPA PSK allocation failed\n");			ret = -1;			break;		}		if (memcmp(addr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0)			psk->group = 1;		else			memcpy(psk->addr, addr, ETH_ALEN);		pos = buf + 17;		if (pos == '\0') {			printf("No PSK on line %d in '%s'\n", line, fname);			free(psk);			ret = -1;			break;		}		pos++;		ok = 0;		len = strlen(pos);		if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)			ok = 1;		else if (len >= 8 && len < 64) {			pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,				    4096, psk->psk, PMK_LEN);			ok = 1;		}		if (!ok) {			printf("Invalid PSK '%s' on line %d in '%s'\n",			       pos, line, fname);			free(psk);			ret = -1;			break;		}		psk->next = ssid->wpa_psk;		ssid->wpa_psk = psk;	}	fclose(f);	return ret;}int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf){	struct hostapd_ssid *ssid = &conf->ssid;	if (ssid->wpa_passphrase != NULL) {		if (ssid->wpa_psk != NULL) {			printf("Warning: both WPA PSK and passphrase set. "			       "Using passphrase.\n");			free(ssid->wpa_psk);		}		ssid->wpa_psk = wpa_zalloc(sizeof(struct hostapd_wpa_psk));		if (ssid->wpa_psk == NULL) {			printf("Unable to alloc space for PSK\n");			return -1;		}		wpa_hexdump_ascii(MSG_DEBUG, "SSID",				  (u8 *) ssid->ssid, ssid->ssid_len);		wpa_hexdump_ascii(MSG_DEBUG, "PSK (ASCII passphrase)",				  (u8 *) ssid->wpa_passphrase,				  strlen(ssid->wpa_passphrase));		pbkdf2_sha1(ssid->wpa_passphrase,			    ssid->ssid, ssid->ssid_len,			    4096, ssid->wpa_psk->psk, PMK_LEN);		wpa_hexdump(MSG_DEBUG, "PSK (from passphrase)",			    ssid->wpa_psk->psk, PMK_LEN);		ssid->wpa_psk->group = 1;		memset(ssid->wpa_passphrase, 0,		       strlen(ssid->wpa_passphrase));		free(ssid->wpa_passphrase);		ssid->wpa_passphrase = 0;	}	if (ssid->wpa_psk_file) {		if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,						&conf->ssid))			return -1;		free(ssid->wpa_psk_file);		ssid->wpa_psk_file = NULL;	}	return 0;}#ifdef EAP_SERVERstatic int hostapd_config_read_eap_user(const char *fname,					struct hostapd_bss_config *conf){	FILE *f;	char buf[512], *pos, *start, *pos2;	int line = 0, ret = 0, num_methods;	struct hostapd_eap_user *user, *tail = NULL;	if (!fname)		return 0;	f = fopen(fname, "r");	if (!f) {		printf("EAP user file '%s' not found.\n", fname);		return -1;	}	/* Lines: "user" METHOD,METHOD2 "password" (password optional) */	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;		user = NULL;		if (buf[0] != '"' && buf[0] != '*') {			printf("Invalid EAP identity (no \" in start) on "			       "line %d in '%s'\n", line, fname);			goto failed;		}		user = wpa_zalloc(sizeof(*user));		if (user == NULL) {			printf("EAP user allocation failed\n");			goto failed;		}		user->force_version = -1;		if (buf[0] == '*') {			pos = buf;		} else {			pos = buf + 1;			start = pos;			while (*pos != '"' && *pos != '\0')				pos++;			if (*pos == '\0') {				printf("Invalid EAP identity (no \" in end) on"				       " line %d in '%s'\n", line, fname);				goto failed;			}			user->identity = malloc(pos - start);			if (user->identity == NULL) {				printf("Failed to allocate memory for EAP "				       "identity\n");				goto failed;			}			memcpy(user->identity, start, pos - start);			user->identity_len = pos - start;			if (pos[0] == '"' && pos[1] == '*') {				user->wildcard_prefix = 1;				pos++;			}		}		pos++;		while (*pos == ' ' || *pos == '\t')			pos++;		if (*pos == '\0') {			printf("No EAP method on line %d in '%s'\n",			       line, fname);			goto failed;		}		start = pos;		while (*pos != ' ' && *pos != '\t' && *pos != '\0')			pos++;		if (*pos == '\0') {			pos = NULL;		} else {			*pos = '\0';			pos++;		}		num_methods = 0;		while (*start) {			char *pos2 = strchr(start, ',');			if (pos2) {				*pos2++ = '\0';			}			user->methods[num_methods].method =				eap_get_type(start, &user->methods[num_methods]					     .vendor);			if (user->methods[num_methods].vendor ==			    EAP_VENDOR_IETF &&			    user->methods[num_methods].method == EAP_TYPE_NONE)			{				printf("Unsupported EAP type '%s' on line %d "				       "in '%s'\n", start, line, fname);				goto failed;			}			num_methods++;			if (num_methods >= EAP_USER_MAX_METHODS)				break;			if (pos2 == NULL)				break;			start = pos2;		}		if (num_methods == 0) {			printf("No EAP types configured on line %d in '%s'\n",			       line, fname);			goto failed;		}		if (pos == NULL)			goto done;		while (*pos == ' ' || *pos == '\t')			pos++;		if (*pos == '\0')			goto done;		if (strncmp(pos, "[ver=0]", 7) == 0) {			user->force_version = 0;			goto done;		}		if (strncmp(pos, "[ver=1]", 7) == 0) {			user->force_version = 1;			goto done;		}		if (strncmp(pos, "[2]", 3) == 0) {			user->phase2 = 1;			goto done;		}		if (*pos == '"') {			pos++;			start = pos;			while (*pos != '"' && *pos != '\0')				pos++;			if (*pos == '\0') {				printf("Invalid EAP password (no \" in end) "				       "on line %d in '%s'\n", line, fname);				goto failed;			}			user->password = malloc(pos - start);			if (user->password == NULL) {				printf("Failed to allocate memory for EAP "				       "password\n");				goto failed;			}			memcpy(user->password, start, pos - start);			user->password_len = pos - start;			pos++;		} else if (strncmp(pos, "hash:", 5) == 0) {			pos += 5;			pos2 = pos;			while (*pos2 != '\0' && *pos2 != ' ' &&			       *pos2 != '\t' && *pos2 != '#')				pos2++;			if (pos2 - pos != 32) {				printf("Invalid password hash on line %d in "				       "'%s'\n", line, fname);				goto failed;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -