📄 config.c
字号:
#undef STR_RANGE_KEY#undef _INT#undef INT#undef INT_RANGE#undef _FUNC#undef FUNC#undef FUNC_KEY#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 = os_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; } os_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; os_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){ os_free(ssid->ssid); os_free(ssid->passphrase);#ifdef IEEE8021X_EAPOL os_free(ssid->eap_methods); os_free(ssid->identity); os_free(ssid->anonymous_identity); os_free(ssid->eappsk); os_free(ssid->nai); os_free(ssid->password); os_free(ssid->ca_cert); os_free(ssid->ca_path); os_free(ssid->client_cert); os_free(ssid->private_key); os_free(ssid->private_key_passwd); os_free(ssid->dh_file); os_free(ssid->subject_match); os_free(ssid->altsubject_match); os_free(ssid->ca_cert2); os_free(ssid->ca_path2); os_free(ssid->client_cert2); os_free(ssid->private_key2); os_free(ssid->private_key2_passwd); os_free(ssid->dh_file2); os_free(ssid->subject_match2); os_free(ssid->altsubject_match2); os_free(ssid->phase1); os_free(ssid->phase2); os_free(ssid->pcsc); os_free(ssid->pin); os_free(ssid->engine_id); os_free(ssid->key_id); os_free(ssid->otp); os_free(ssid->pending_req_otp); os_free(ssid->pac_file); os_free(ssid->new_password);#endif /* IEEE8021X_EAPOL */ os_free(ssid->id_str); os_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); } os_free(config->ctrl_interface); os_free(config->ctrl_interface_group); os_free(config->opensc_engine_path); os_free(config->pkcs11_engine_path); os_free(config->pkcs11_module_path); os_free(config->driver_param); os_free(config->pssid); os_free(config);}#ifdef IEEE8021X_EAPOL/** * wpa_config_allowed_eap_method - Check whether EAP method is allowed * @ssid: Pointer to configuration data * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types * @method: EAP type * Returns: 1 = allowed EAP method, 0 = not allowed */int wpa_config_allowed_eap_method(struct wpa_ssid *ssid, int vendor, u32 method){ int i; struct eap_method_type *m; if (ssid == NULL || ssid->eap_methods == NULL) return 1; m = ssid->eap_methods; for (i = 0; m[i].vendor != EAP_VENDOR_IETF || m[i].method != EAP_TYPE_NONE; i++) { if (m[i].vendor == vendor && m[i].method == method) return 1; } return 0;}#endif /* IEEE8021X_EAPOL *//** * 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 = os_zalloc(sizeof(*ssid)); if (ssid == NULL) return NULL; 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;#ifdef IEEE8021X_EAPOL ssid->eapol_flags = DEFAULT_EAPOL_FLAGS; ssid->eap_workaround = DEFAULT_EAP_WORKAROUND; ssid->fragment_size = DEFAULT_FRAGMENT_SIZE;#endif /* IEEE8021X_EAPOL */}/** * 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){ size_t i; int 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 (os_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){ size_t 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 (os_strcmp(var, field->name) == 0) return field->writer(field, ssid); } return NULL;}/** * wpa_config_get_no_key - Get a variable in network configuration (no keys) * @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 variable like * wpa_config_get(). The only difference is that this functions does not expose * key/password material from the configuration. In case a key/password field * is requested, the returned value is an empty string or %NULL if the variable * is not set or "*" if the variable is set (regardless of its value). 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_no_key(struct wpa_ssid *ssid, const char *var){ size_t 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 (os_strcmp(var, field->name) == 0) { char *res = field->writer(field, ssid); if (field->key_data) { if (res && res[0]) { wpa_printf(MSG_DEBUG, "Do not allow " "key_data field to be " "exposed"); os_free(res); return os_strdup("*"); } os_free(res); return NULL; } return res; } } 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 (os_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) { os_free(blob->name); os_free(blob->data); os_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 (os_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 = os_zalloc(sizeof(*config)); if (config == NULL) return NULL; config->eapol_version = DEFAULT_EAPOL_VERSION; config->ap_scan = DEFAULT_AP_SCAN; config->fast_reauth = DEFAULT_FAST_REAUTH; if (ctrl_interface) config->ctrl_interface = os_strdup(ctrl_interface); if (driver_param) config->driver_param = os_strdup(driver_param); return config;}#ifndef CONFIG_NO_STDOUT_DEBUG/** * wpa_config_debug_dump_networks - Debug dump of configured networks * @config: Configuration data from wpa_config_read() */void wpa_config_debug_dump_networks(struct wpa_config *config){ int prio; struct wpa_ssid *ssid; for (prio = 0; prio < config->num_prio; prio++) { ssid = config->pssid[prio]; wpa_printf(MSG_DEBUG, "Priority group %d", ssid->priority); while (ssid) { wpa_printf(MSG_DEBUG, " id=%d ssid='%s'", ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); ssid = ssid->pnext; } }}#endif /* CONFIG_NO_STDOUT_DEBUG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -