📄 config.c
字号:
{ STR(pin), .key_data = 1 }, { STR(engine_id) }, { STR(key_id) }, { INT(engine) }, { INT(eapol_flags) }, { FUNC(wep_key0), .key_data = 1 }, { FUNC(wep_key1), .key_data = 1 }, { FUNC(wep_key2), .key_data = 1 }, { FUNC(wep_key3), .key_data = 1 }, { INT(wep_tx_keyidx) }, { INT(priority) }, { INT(eap_workaround) }, { STR(pac_file) }, { INT_RANGE(mode, 0, 1) }, { INT_RANGE(proactive_key_caching, 0, 1) }, { INT_RANGE(disabled, 0, 1) },};#undef OFFSET#undef STR#undef STR_LEN#undef STR_RANGE#undef INT#undef INT_RANGE#undef FUNC#define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))/** * wpa_config_add_prio_network - Add a network to priority lists * @config: Configuration data from wpa_config_read() * @ssid: Pointer to the network configuration to be added to the list * Returns: 0 on success, -1 on failure * * This function is used to add a network block to the priority list of * networks. This must be called for each network when reading in the full * configuration. In addition, this can be used indirectly when updating * priorities by calling wpa_config_update_prio_list(). */int wpa_config_add_prio_network(struct wpa_config *config, struct wpa_ssid *ssid){ int prio; struct wpa_ssid *prev, **nlist; /* * Add to an existing priority list if one is available for the * configured priority level for this network. */ for (prio = 0; prio < config->num_prio; prio++) { prev = config->pssid[prio]; if (prev->priority == ssid->priority) { while (prev->pnext) prev = prev->pnext; prev->pnext = ssid; return 0; } } /* First network for this priority - add a new priority list */ nlist = realloc(config->pssid, (config->num_prio + 1) * sizeof(struct wpa_ssid *)); if (nlist == NULL) return -1; for (prio = 0; prio < config->num_prio; prio++) { if (nlist[prio]->priority < ssid->priority) break; } memmove(&nlist[prio + 1], &nlist[prio], (config->num_prio - prio) * sizeof(struct wpa_ssid *)); nlist[prio] = ssid; config->num_prio++; config->pssid = nlist; return 0;}/** * wpa_config_update_prio_list - Update network priority list * @config: Configuration data from wpa_config_read() * Returns: 0 on success, -1 on failure * * This function is called to update the priority list of networks in the * configuration when a network is being added or removed. This is also called * if a priority for a network is changed. */static int wpa_config_update_prio_list(struct wpa_config *config){ struct wpa_ssid *ssid; int ret = 0; free(config->pssid); config->pssid = NULL; config->num_prio = 0; ssid = config->ssid; while (ssid) { ssid->pnext = NULL; if (wpa_config_add_prio_network(config, ssid) < 0) ret = -1; ssid = ssid->next; } return ret;}/** * wpa_config_free_ssid - Free network/ssid configuration data * @ssid: Configuration data for the network * * This function frees all resources allocated for the network configuration * data. */void wpa_config_free_ssid(struct wpa_ssid *ssid){ free(ssid->ssid); free(ssid->passphrase); free(ssid->eap_methods); free(ssid->identity); free(ssid->anonymous_identity); free(ssid->eappsk); free(ssid->nai); free(ssid->password); free(ssid->ca_cert); free(ssid->ca_path); free(ssid->client_cert); free(ssid->private_key); free(ssid->private_key_passwd); free(ssid->dh_file); free(ssid->subject_match); free(ssid->altsubject_match); free(ssid->ca_cert2); free(ssid->ca_path2); free(ssid->client_cert2); free(ssid->private_key2); free(ssid->private_key2_passwd); free(ssid->dh_file2); free(ssid->subject_match2); free(ssid->altsubject_match2); free(ssid->phase1); free(ssid->phase2); free(ssid->pcsc); free(ssid->pin); free(ssid->engine_id); free(ssid->key_id); free(ssid->otp); free(ssid->pending_req_otp); free(ssid->pac_file); free(ssid->new_password); free(ssid);}/** * wpa_config_free - Free configuration data * @config: Configuration data from wpa_config_read() * * This function frees all resources allocated for the configuration data by * wpa_config_read(). */void wpa_config_free(struct wpa_config *config){ struct wpa_config_blob *blob, *prevblob; struct wpa_ssid *ssid, *prev = NULL; ssid = config->ssid; while (ssid) { prev = ssid; ssid = ssid->next; wpa_config_free_ssid(prev); } blob = config->blobs; prevblob = NULL; while (blob) { prevblob = blob; blob = blob->next; wpa_config_free_blob(prevblob); } free(config->ctrl_interface); free(config->opensc_engine_path); free(config->pkcs11_engine_path); free(config->pkcs11_module_path); free(config->driver_param); free(config->pssid); free(config);}/** * wpa_config_allowed_eap_method - Check whether EAP method is allowed * @ssid: Pointer to configuration data * @method: EAP type * Returns: 1 = allowed EAP method, 0 = not allowed */int wpa_config_allowed_eap_method(struct wpa_ssid *ssid, int method){ u8 *pos; if (ssid == NULL || ssid->eap_methods == NULL) return 1; pos = ssid->eap_methods; while (*pos != EAP_TYPE_NONE) { if (*pos == method) return 1; pos++; } return 0;}/** * wpa_config_get_network - Get configured network based on id * @config: Configuration data from wpa_config_read() * @id: Unique network id to search for * Returns: Network configuration or %NULL if not found */struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id){ struct wpa_ssid *ssid; ssid = config->ssid; while (ssid) { if (id == ssid->id) break; ssid = ssid->next; } return ssid;}/** * wpa_config_add_network - Add a new network with empty configuration * @config: Configuration data from wpa_config_read() * Returns: The new network configuration or %NULL if operation failed */struct wpa_ssid * wpa_config_add_network(struct wpa_config *config){ int id; struct wpa_ssid *ssid, *last = NULL; id = -1; ssid = config->ssid; while (ssid) { if (ssid->id > id) id = ssid->id; last = ssid; ssid = ssid->next; } id++; ssid = malloc(sizeof(*ssid)); if (ssid == NULL) return NULL; memset(ssid, 0, sizeof(*ssid)); ssid->id = id; if (last) last->next = ssid; else config->ssid = ssid; wpa_config_update_prio_list(config); return ssid;}/** * wpa_config_remove_network - Remove a configured network based on id * @config: Configuration data from wpa_config_read() * @id: Unique network id to search for * Returns: 0 on success, or -1 if the network was not found */int wpa_config_remove_network(struct wpa_config *config, int id){ struct wpa_ssid *ssid, *prev = NULL; ssid = config->ssid; while (ssid) { if (id == ssid->id) break; prev = ssid; ssid = ssid->next; } if (ssid == NULL) return -1; if (prev) prev->next = ssid->next; else config->ssid = ssid->next; wpa_config_update_prio_list(config); wpa_config_free_ssid(ssid); return 0;}/** * wpa_config_set_network_defaults - Set network default values * @ssid: Pointer to network configuration data */void wpa_config_set_network_defaults(struct wpa_ssid *ssid){ ssid->proto = DEFAULT_PROTO; ssid->pairwise_cipher = DEFAULT_PAIRWISE; ssid->group_cipher = DEFAULT_GROUP; ssid->key_mgmt = DEFAULT_KEY_MGMT; ssid->eapol_flags = DEFAULT_EAPOL_FLAGS; ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;}/** * wpa_config_set - Set a variable in network configuration * @ssid: Pointer to network configuration data * @var: Variable name, e.g., "ssid" * @value: Variable value * @line: Line number in configuration file or 0 if not used * Returns: 0 on success, -1 on failure * * This function can be used to set network configuration variables based on * both the configuration file and management interface input. The value * parameter must be in the same format as the text-based configuration file is * using. For example, strings are using double quotation marks. */int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value, int line){ int i, ret = 0; if (ssid == NULL || var == NULL || value == NULL) return -1; for (i = 0; i < NUM_SSID_FIELDS; i++) { const struct parse_data *field = &ssid_fields[i]; if (strcmp(var, field->name) != 0) continue; if (field->parser(field, ssid, line, value)) { if (line) { wpa_printf(MSG_ERROR, "Line %d: failed to " "parse %s '%s'.", line, var, value); } ret = -1; } break; } if (i == NUM_SSID_FIELDS) { if (line) { wpa_printf(MSG_ERROR, "Line %d: unknown network field " "'%s'.", line, var); } ret = -1; } return ret;}/** * wpa_config_get - Get a variable in network configuration * @ssid: Pointer to network configuration data * @var: Variable name, e.g., "ssid" * Returns: Value of the variable or %NULL on failure * * This function can be used to get network configuration variables. The * returned value is a copy of the configuration variable in text format, i.e,. * the same format that the text-based configuration file and wpa_config_set() * are using for the value. The caller is responsible for freeing the returned * value. */char * wpa_config_get(struct wpa_ssid *ssid, const char *var){ int i; if (ssid == NULL || var == NULL) return NULL; for (i = 0; i < NUM_SSID_FIELDS; i++) { const struct parse_data *field = &ssid_fields[i]; if (strcmp(var, field->name) == 0) return field->writer(field, ssid); } return NULL;}/** * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID * @ssid: Pointer to network configuration data * * This function must be called to update WPA PSK when either SSID or the * passphrase has changed for the network configuration. */void wpa_config_update_psk(struct wpa_ssid *ssid){ pbkdf2_sha1(ssid->passphrase, (char *) ssid->ssid, ssid->ssid_len, 4096, ssid->psk, PMK_LEN); wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)", ssid->psk, PMK_LEN); ssid->psk_set = 1;}/** * wpa_config_get_blob - Get a named configuration blob * @config: Configuration data from wpa_config_read() * @name: Name of the blob * Returns: Pointer to blob data or %NULL if not found */const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config, const char *name){ struct wpa_config_blob *blob = config->blobs; while (blob) { if (strcmp(blob->name, name) == 0) return blob; blob = blob->next; } return NULL;}/** * wpa_config_set_blob - Set or add a named configuration blob * @config: Configuration data from wpa_config_read() * @blob: New value for the blob * * Adds a new configuration blob or replaces the current value of an existing * blob. */void wpa_config_set_blob(struct wpa_config *config, struct wpa_config_blob *blob){ wpa_config_remove_blob(config, blob->name); blob->next = config->blobs; config->blobs = blob;}/** * wpa_config_free_blob - Free blob data * @blob: Pointer to blob to be freed */void wpa_config_free_blob(struct wpa_config_blob *blob){ if (blob) { free(blob->name); free(blob->data); free(blob); }}/** * wpa_config_remove_blob - Remove a named configuration blob * @config: Configuration data from wpa_config_read() * @name: Name of the blob to remove * Returns: 0 if blob was removed or -1 if blob was not found */int wpa_config_remove_blob(struct wpa_config *config, const char *name){ struct wpa_config_blob *pos = config->blobs, *prev = NULL; while (pos) { if (strcmp(pos->name, name) == 0) { if (prev) prev->next = pos->next; else config->blobs = pos->next; wpa_config_free_blob(pos); return 0; } prev = pos; pos = pos->next; } return -1;}/** * wpa_config_alloc_empty - Allocate an empty configuration * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain * socket * @driver_param: Driver parameters * Returns: Pointer to allocated configuration data or %NULL on failure */struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface, const char *driver_param){ struct wpa_config *config; config = malloc(sizeof(*config)); if (config == NULL) return NULL; memset(config, 0, sizeof(*config)); config->eapol_version = DEFAULT_EAPOL_VERSION; config->ap_scan = DEFAULT_AP_SCAN; config->fast_reauth = DEFAULT_FAST_REAUTH; if (ctrl_interface) config->ctrl_interface = strdup(ctrl_interface); if (driver_param) config->driver_param = strdup(driver_param); return config;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -