config_file.c revision 252726
1189251Ssam/* 2189251Ssam * WPA Supplicant / Configuration backend: text file 3252726Srpaulo * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> 4189251Ssam * 5252726Srpaulo * This software may be distributed under the terms of the BSD license. 6252726Srpaulo * See README for more details. 7189251Ssam * 8189251Ssam * This file implements a configuration backend for text files. All the 9189251Ssam * configuration information is stored in a text file that uses a format 10189251Ssam * described in the sample configuration file, wpa_supplicant.conf. 11189251Ssam */ 12189251Ssam 13189251Ssam#include "includes.h" 14189251Ssam 15189251Ssam#include "common.h" 16189251Ssam#include "config.h" 17189251Ssam#include "base64.h" 18189251Ssam#include "uuid.h" 19252726Srpaulo#include "p2p/p2p.h" 20189251Ssam#include "eap_peer/eap_methods.h" 21252726Srpaulo#include "eap_peer/eap.h" 22189251Ssam 23189251Ssam 24252726Srpaulostatic int newline_terminated(const char *buf, size_t buflen) 25252726Srpaulo{ 26252726Srpaulo size_t len = os_strlen(buf); 27252726Srpaulo if (len == 0) 28252726Srpaulo return 0; 29252726Srpaulo if (len == buflen - 1 && buf[buflen - 1] != '\r' && 30252726Srpaulo buf[len - 1] != '\n') 31252726Srpaulo return 0; 32252726Srpaulo return 1; 33252726Srpaulo} 34252726Srpaulo 35252726Srpaulo 36252726Srpaulostatic void skip_line_end(FILE *stream) 37252726Srpaulo{ 38252726Srpaulo char buf[100]; 39252726Srpaulo while (fgets(buf, sizeof(buf), stream)) { 40252726Srpaulo buf[sizeof(buf) - 1] = '\0'; 41252726Srpaulo if (newline_terminated(buf, sizeof(buf))) 42252726Srpaulo return; 43252726Srpaulo } 44252726Srpaulo} 45252726Srpaulo 46252726Srpaulo 47189251Ssam/** 48189251Ssam * wpa_config_get_line - Read the next configuration file line 49189251Ssam * @s: Buffer for the line 50189251Ssam * @size: The buffer length 51189251Ssam * @stream: File stream to read from 52189251Ssam * @line: Pointer to a variable storing the file line number 53189251Ssam * @_pos: Buffer for the pointer to the beginning of data on the text line or 54189251Ssam * %NULL if not needed (returned value used instead) 55189251Ssam * Returns: Pointer to the beginning of data on the text line or %NULL if no 56189251Ssam * more text lines are available. 57189251Ssam * 58189251Ssam * This function reads the next non-empty line from the configuration file and 59189251Ssam * removes comments. The returned string is guaranteed to be null-terminated. 60189251Ssam */ 61189251Ssamstatic char * wpa_config_get_line(char *s, int size, FILE *stream, int *line, 62189251Ssam char **_pos) 63189251Ssam{ 64189251Ssam char *pos, *end, *sstart; 65189251Ssam 66189251Ssam while (fgets(s, size, stream)) { 67189251Ssam (*line)++; 68189251Ssam s[size - 1] = '\0'; 69252726Srpaulo if (!newline_terminated(s, size)) { 70252726Srpaulo /* 71252726Srpaulo * The line was truncated - skip rest of it to avoid 72252726Srpaulo * confusing error messages. 73252726Srpaulo */ 74252726Srpaulo wpa_printf(MSG_INFO, "Long line in configuration file " 75252726Srpaulo "truncated"); 76252726Srpaulo skip_line_end(stream); 77252726Srpaulo } 78189251Ssam pos = s; 79189251Ssam 80189251Ssam /* Skip white space from the beginning of line. */ 81189251Ssam while (*pos == ' ' || *pos == '\t' || *pos == '\r') 82189251Ssam pos++; 83189251Ssam 84189251Ssam /* Skip comment lines and empty lines */ 85189251Ssam if (*pos == '#' || *pos == '\n' || *pos == '\0') 86189251Ssam continue; 87189251Ssam 88189251Ssam /* 89189251Ssam * Remove # comments unless they are within a double quoted 90189251Ssam * string. 91189251Ssam */ 92189251Ssam sstart = os_strchr(pos, '"'); 93189251Ssam if (sstart) 94189251Ssam sstart = os_strrchr(sstart + 1, '"'); 95189251Ssam if (!sstart) 96189251Ssam sstart = pos; 97189251Ssam end = os_strchr(sstart, '#'); 98189251Ssam if (end) 99189251Ssam *end-- = '\0'; 100189251Ssam else 101189251Ssam end = pos + os_strlen(pos) - 1; 102189251Ssam 103189251Ssam /* Remove trailing white space. */ 104189251Ssam while (end > pos && 105189251Ssam (*end == '\n' || *end == ' ' || *end == '\t' || 106189251Ssam *end == '\r')) 107189251Ssam *end-- = '\0'; 108189251Ssam 109189251Ssam if (*pos == '\0') 110189251Ssam continue; 111189251Ssam 112189251Ssam if (_pos) 113189251Ssam *_pos = pos; 114189251Ssam return pos; 115189251Ssam } 116189251Ssam 117189251Ssam if (_pos) 118189251Ssam *_pos = NULL; 119189251Ssam return NULL; 120189251Ssam} 121189251Ssam 122189251Ssam 123189251Ssamstatic int wpa_config_validate_network(struct wpa_ssid *ssid, int line) 124189251Ssam{ 125189251Ssam int errors = 0; 126189251Ssam 127189251Ssam if (ssid->passphrase) { 128189251Ssam if (ssid->psk_set) { 129189251Ssam wpa_printf(MSG_ERROR, "Line %d: both PSK and " 130189251Ssam "passphrase configured.", line); 131189251Ssam errors++; 132189251Ssam } 133189251Ssam wpa_config_update_psk(ssid); 134189251Ssam } 135189251Ssam 136189251Ssam if ((ssid->group_cipher & WPA_CIPHER_CCMP) && 137189251Ssam !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) && 138189251Ssam !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) { 139189251Ssam /* Group cipher cannot be stronger than the pairwise cipher. */ 140189251Ssam wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher" 141189251Ssam " list since it was not allowed for pairwise " 142189251Ssam "cipher", line); 143189251Ssam ssid->group_cipher &= ~WPA_CIPHER_CCMP; 144189251Ssam } 145189251Ssam 146189251Ssam return errors; 147189251Ssam} 148189251Ssam 149189251Ssam 150189251Ssamstatic struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id) 151189251Ssam{ 152189251Ssam struct wpa_ssid *ssid; 153189251Ssam int errors = 0, end = 0; 154252726Srpaulo char buf[2000], *pos, *pos2; 155189251Ssam 156189251Ssam wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block", 157189251Ssam *line); 158189251Ssam ssid = os_zalloc(sizeof(*ssid)); 159189251Ssam if (ssid == NULL) 160189251Ssam return NULL; 161189251Ssam ssid->id = id; 162189251Ssam 163189251Ssam wpa_config_set_network_defaults(ssid); 164189251Ssam 165189251Ssam while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { 166189251Ssam if (os_strcmp(pos, "}") == 0) { 167189251Ssam end = 1; 168189251Ssam break; 169189251Ssam } 170189251Ssam 171189251Ssam pos2 = os_strchr(pos, '='); 172189251Ssam if (pos2 == NULL) { 173189251Ssam wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line " 174189251Ssam "'%s'.", *line, pos); 175189251Ssam errors++; 176189251Ssam continue; 177189251Ssam } 178189251Ssam 179189251Ssam *pos2++ = '\0'; 180189251Ssam if (*pos2 == '"') { 181189251Ssam if (os_strchr(pos2 + 1, '"') == NULL) { 182189251Ssam wpa_printf(MSG_ERROR, "Line %d: invalid " 183189251Ssam "quotation '%s'.", *line, pos2); 184189251Ssam errors++; 185189251Ssam continue; 186189251Ssam } 187189251Ssam } 188189251Ssam 189189251Ssam if (wpa_config_set(ssid, pos, pos2, *line) < 0) 190189251Ssam errors++; 191189251Ssam } 192189251Ssam 193189251Ssam if (!end) { 194189251Ssam wpa_printf(MSG_ERROR, "Line %d: network block was not " 195189251Ssam "terminated properly.", *line); 196189251Ssam errors++; 197189251Ssam } 198189251Ssam 199189251Ssam errors += wpa_config_validate_network(ssid, *line); 200189251Ssam 201189251Ssam if (errors) { 202189251Ssam wpa_config_free_ssid(ssid); 203189251Ssam ssid = NULL; 204189251Ssam } 205189251Ssam 206189251Ssam return ssid; 207189251Ssam} 208189251Ssam 209189251Ssam 210252726Srpaulostatic struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id) 211252726Srpaulo{ 212252726Srpaulo struct wpa_cred *cred; 213252726Srpaulo int errors = 0, end = 0; 214252726Srpaulo char buf[256], *pos, *pos2; 215252726Srpaulo 216252726Srpaulo wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line); 217252726Srpaulo cred = os_zalloc(sizeof(*cred)); 218252726Srpaulo if (cred == NULL) 219252726Srpaulo return NULL; 220252726Srpaulo cred->id = id; 221252726Srpaulo 222252726Srpaulo while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { 223252726Srpaulo if (os_strcmp(pos, "}") == 0) { 224252726Srpaulo end = 1; 225252726Srpaulo break; 226252726Srpaulo } 227252726Srpaulo 228252726Srpaulo pos2 = os_strchr(pos, '='); 229252726Srpaulo if (pos2 == NULL) { 230252726Srpaulo wpa_printf(MSG_ERROR, "Line %d: Invalid cred line " 231252726Srpaulo "'%s'.", *line, pos); 232252726Srpaulo errors++; 233252726Srpaulo continue; 234252726Srpaulo } 235252726Srpaulo 236252726Srpaulo *pos2++ = '\0'; 237252726Srpaulo if (*pos2 == '"') { 238252726Srpaulo if (os_strchr(pos2 + 1, '"') == NULL) { 239252726Srpaulo wpa_printf(MSG_ERROR, "Line %d: invalid " 240252726Srpaulo "quotation '%s'.", *line, pos2); 241252726Srpaulo errors++; 242252726Srpaulo continue; 243252726Srpaulo } 244252726Srpaulo } 245252726Srpaulo 246252726Srpaulo if (wpa_config_set_cred(cred, pos, pos2, *line) < 0) 247252726Srpaulo errors++; 248252726Srpaulo } 249252726Srpaulo 250252726Srpaulo if (!end) { 251252726Srpaulo wpa_printf(MSG_ERROR, "Line %d: cred block was not " 252252726Srpaulo "terminated properly.", *line); 253252726Srpaulo errors++; 254252726Srpaulo } 255252726Srpaulo 256252726Srpaulo if (errors) { 257252726Srpaulo wpa_config_free_cred(cred); 258252726Srpaulo cred = NULL; 259252726Srpaulo } 260252726Srpaulo 261252726Srpaulo return cred; 262252726Srpaulo} 263252726Srpaulo 264252726Srpaulo 265189251Ssam#ifndef CONFIG_NO_CONFIG_BLOBS 266189251Ssamstatic struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line, 267189251Ssam const char *name) 268189251Ssam{ 269189251Ssam struct wpa_config_blob *blob; 270189251Ssam char buf[256], *pos; 271189251Ssam unsigned char *encoded = NULL, *nencoded; 272189251Ssam int end = 0; 273189251Ssam size_t encoded_len = 0, len; 274189251Ssam 275189251Ssam wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'", 276189251Ssam *line, name); 277189251Ssam 278189251Ssam while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { 279189251Ssam if (os_strcmp(pos, "}") == 0) { 280189251Ssam end = 1; 281189251Ssam break; 282189251Ssam } 283189251Ssam 284189251Ssam len = os_strlen(pos); 285189251Ssam nencoded = os_realloc(encoded, encoded_len + len); 286189251Ssam if (nencoded == NULL) { 287189251Ssam wpa_printf(MSG_ERROR, "Line %d: not enough memory for " 288189251Ssam "blob", *line); 289189251Ssam os_free(encoded); 290189251Ssam return NULL; 291189251Ssam } 292189251Ssam encoded = nencoded; 293189251Ssam os_memcpy(encoded + encoded_len, pos, len); 294189251Ssam encoded_len += len; 295189251Ssam } 296189251Ssam 297189251Ssam if (!end) { 298189251Ssam wpa_printf(MSG_ERROR, "Line %d: blob was not terminated " 299189251Ssam "properly", *line); 300189251Ssam os_free(encoded); 301189251Ssam return NULL; 302189251Ssam } 303189251Ssam 304189251Ssam blob = os_zalloc(sizeof(*blob)); 305189251Ssam if (blob == NULL) { 306189251Ssam os_free(encoded); 307189251Ssam return NULL; 308189251Ssam } 309189251Ssam blob->name = os_strdup(name); 310189251Ssam blob->data = base64_decode(encoded, encoded_len, &blob->len); 311189251Ssam os_free(encoded); 312189251Ssam 313189251Ssam if (blob->name == NULL || blob->data == NULL) { 314189251Ssam wpa_config_free_blob(blob); 315189251Ssam return NULL; 316189251Ssam } 317189251Ssam 318189251Ssam return blob; 319189251Ssam} 320189251Ssam 321189251Ssam 322189251Ssamstatic int wpa_config_process_blob(struct wpa_config *config, FILE *f, 323189251Ssam int *line, char *bname) 324189251Ssam{ 325189251Ssam char *name_end; 326189251Ssam struct wpa_config_blob *blob; 327189251Ssam 328189251Ssam name_end = os_strchr(bname, '='); 329189251Ssam if (name_end == NULL) { 330189251Ssam wpa_printf(MSG_ERROR, "Line %d: no blob name terminator", 331189251Ssam *line); 332189251Ssam return -1; 333189251Ssam } 334189251Ssam *name_end = '\0'; 335189251Ssam 336189251Ssam blob = wpa_config_read_blob(f, line, bname); 337189251Ssam if (blob == NULL) { 338189251Ssam wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s", 339189251Ssam *line, bname); 340189251Ssam return -1; 341189251Ssam } 342189251Ssam wpa_config_set_blob(config, blob); 343189251Ssam return 0; 344189251Ssam} 345189251Ssam#endif /* CONFIG_NO_CONFIG_BLOBS */ 346189251Ssam 347189251Ssam 348189251Ssamstruct wpa_config * wpa_config_read(const char *name) 349189251Ssam{ 350189251Ssam FILE *f; 351252726Srpaulo char buf[512], *pos; 352189251Ssam int errors = 0, line = 0; 353189251Ssam struct wpa_ssid *ssid, *tail = NULL, *head = NULL; 354252726Srpaulo struct wpa_cred *cred, *cred_tail = NULL, *cred_head = NULL; 355189251Ssam struct wpa_config *config; 356189251Ssam int id = 0; 357252726Srpaulo int cred_id = 0; 358189251Ssam 359189251Ssam config = wpa_config_alloc_empty(NULL, NULL); 360252726Srpaulo if (config == NULL) { 361252726Srpaulo wpa_printf(MSG_ERROR, "Failed to allocate config file " 362252726Srpaulo "structure"); 363189251Ssam return NULL; 364252726Srpaulo } 365252726Srpaulo 366189251Ssam wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name); 367189251Ssam f = fopen(name, "r"); 368189251Ssam if (f == NULL) { 369252726Srpaulo wpa_printf(MSG_ERROR, "Failed to open config file '%s', " 370252726Srpaulo "error: %s", name, strerror(errno)); 371189251Ssam os_free(config); 372189251Ssam return NULL; 373189251Ssam } 374189251Ssam 375189251Ssam while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) { 376189251Ssam if (os_strcmp(pos, "network={") == 0) { 377189251Ssam ssid = wpa_config_read_network(f, &line, id++); 378189251Ssam if (ssid == NULL) { 379189251Ssam wpa_printf(MSG_ERROR, "Line %d: failed to " 380189251Ssam "parse network block.", line); 381189251Ssam errors++; 382189251Ssam continue; 383189251Ssam } 384189251Ssam if (head == NULL) { 385189251Ssam head = tail = ssid; 386189251Ssam } else { 387189251Ssam tail->next = ssid; 388189251Ssam tail = ssid; 389189251Ssam } 390189251Ssam if (wpa_config_add_prio_network(config, ssid)) { 391189251Ssam wpa_printf(MSG_ERROR, "Line %d: failed to add " 392189251Ssam "network block to priority list.", 393189251Ssam line); 394189251Ssam errors++; 395189251Ssam continue; 396189251Ssam } 397252726Srpaulo } else if (os_strcmp(pos, "cred={") == 0) { 398252726Srpaulo cred = wpa_config_read_cred(f, &line, cred_id++); 399252726Srpaulo if (cred == NULL) { 400252726Srpaulo wpa_printf(MSG_ERROR, "Line %d: failed to " 401252726Srpaulo "parse cred block.", line); 402252726Srpaulo errors++; 403252726Srpaulo continue; 404252726Srpaulo } 405252726Srpaulo if (cred_head == NULL) { 406252726Srpaulo cred_head = cred_tail = cred; 407252726Srpaulo } else { 408252726Srpaulo cred_tail->next = cred; 409252726Srpaulo cred_tail = cred; 410252726Srpaulo } 411189251Ssam#ifndef CONFIG_NO_CONFIG_BLOBS 412189251Ssam } else if (os_strncmp(pos, "blob-base64-", 12) == 0) { 413189251Ssam if (wpa_config_process_blob(config, f, &line, pos + 12) 414189251Ssam < 0) { 415252726Srpaulo wpa_printf(MSG_ERROR, "Line %d: failed to " 416252726Srpaulo "process blob.", line); 417189251Ssam errors++; 418189251Ssam continue; 419189251Ssam } 420189251Ssam#endif /* CONFIG_NO_CONFIG_BLOBS */ 421189251Ssam } else if (wpa_config_process_global(config, pos, line) < 0) { 422189251Ssam wpa_printf(MSG_ERROR, "Line %d: Invalid configuration " 423189251Ssam "line '%s'.", line, pos); 424189251Ssam errors++; 425189251Ssam continue; 426189251Ssam } 427189251Ssam } 428189251Ssam 429189251Ssam fclose(f); 430189251Ssam 431189251Ssam config->ssid = head; 432189251Ssam wpa_config_debug_dump_networks(config); 433252726Srpaulo config->cred = cred_head; 434189251Ssam 435252726Srpaulo#ifndef WPA_IGNORE_CONFIG_ERRORS 436189251Ssam if (errors) { 437189251Ssam wpa_config_free(config); 438189251Ssam config = NULL; 439189251Ssam head = NULL; 440189251Ssam } 441252726Srpaulo#endif /* WPA_IGNORE_CONFIG_ERRORS */ 442189251Ssam 443189251Ssam return config; 444189251Ssam} 445189251Ssam 446189251Ssam 447189251Ssam#ifndef CONFIG_NO_CONFIG_WRITE 448189251Ssam 449189251Ssamstatic void write_str(FILE *f, const char *field, struct wpa_ssid *ssid) 450189251Ssam{ 451189251Ssam char *value = wpa_config_get(ssid, field); 452189251Ssam if (value == NULL) 453189251Ssam return; 454189251Ssam fprintf(f, "\t%s=%s\n", field, value); 455189251Ssam os_free(value); 456189251Ssam} 457189251Ssam 458189251Ssam 459189251Ssamstatic void write_int(FILE *f, const char *field, int value, int def) 460189251Ssam{ 461189251Ssam if (value == def) 462189251Ssam return; 463189251Ssam fprintf(f, "\t%s=%d\n", field, value); 464189251Ssam} 465189251Ssam 466189251Ssam 467189251Ssamstatic void write_bssid(FILE *f, struct wpa_ssid *ssid) 468189251Ssam{ 469189251Ssam char *value = wpa_config_get(ssid, "bssid"); 470189251Ssam if (value == NULL) 471189251Ssam return; 472189251Ssam fprintf(f, "\tbssid=%s\n", value); 473189251Ssam os_free(value); 474189251Ssam} 475189251Ssam 476189251Ssam 477189251Ssamstatic void write_psk(FILE *f, struct wpa_ssid *ssid) 478189251Ssam{ 479189251Ssam char *value = wpa_config_get(ssid, "psk"); 480189251Ssam if (value == NULL) 481189251Ssam return; 482189251Ssam fprintf(f, "\tpsk=%s\n", value); 483189251Ssam os_free(value); 484189251Ssam} 485189251Ssam 486189251Ssam 487189251Ssamstatic void write_proto(FILE *f, struct wpa_ssid *ssid) 488189251Ssam{ 489189251Ssam char *value; 490189251Ssam 491189251Ssam if (ssid->proto == DEFAULT_PROTO) 492189251Ssam return; 493189251Ssam 494189251Ssam value = wpa_config_get(ssid, "proto"); 495189251Ssam if (value == NULL) 496189251Ssam return; 497189251Ssam if (value[0]) 498189251Ssam fprintf(f, "\tproto=%s\n", value); 499189251Ssam os_free(value); 500189251Ssam} 501189251Ssam 502189251Ssam 503189251Ssamstatic void write_key_mgmt(FILE *f, struct wpa_ssid *ssid) 504189251Ssam{ 505189251Ssam char *value; 506189251Ssam 507189251Ssam if (ssid->key_mgmt == DEFAULT_KEY_MGMT) 508189251Ssam return; 509189251Ssam 510189251Ssam value = wpa_config_get(ssid, "key_mgmt"); 511189251Ssam if (value == NULL) 512189251Ssam return; 513189251Ssam if (value[0]) 514189251Ssam fprintf(f, "\tkey_mgmt=%s\n", value); 515189251Ssam os_free(value); 516189251Ssam} 517189251Ssam 518189251Ssam 519189251Ssamstatic void write_pairwise(FILE *f, struct wpa_ssid *ssid) 520189251Ssam{ 521189251Ssam char *value; 522189251Ssam 523189251Ssam if (ssid->pairwise_cipher == DEFAULT_PAIRWISE) 524189251Ssam return; 525189251Ssam 526189251Ssam value = wpa_config_get(ssid, "pairwise"); 527189251Ssam if (value == NULL) 528189251Ssam return; 529189251Ssam if (value[0]) 530189251Ssam fprintf(f, "\tpairwise=%s\n", value); 531189251Ssam os_free(value); 532189251Ssam} 533189251Ssam 534189251Ssam 535189251Ssamstatic void write_group(FILE *f, struct wpa_ssid *ssid) 536189251Ssam{ 537189251Ssam char *value; 538189251Ssam 539189251Ssam if (ssid->group_cipher == DEFAULT_GROUP) 540189251Ssam return; 541189251Ssam 542189251Ssam value = wpa_config_get(ssid, "group"); 543189251Ssam if (value == NULL) 544189251Ssam return; 545189251Ssam if (value[0]) 546189251Ssam fprintf(f, "\tgroup=%s\n", value); 547189251Ssam os_free(value); 548189251Ssam} 549189251Ssam 550189251Ssam 551189251Ssamstatic void write_auth_alg(FILE *f, struct wpa_ssid *ssid) 552189251Ssam{ 553189251Ssam char *value; 554189251Ssam 555189251Ssam if (ssid->auth_alg == 0) 556189251Ssam return; 557189251Ssam 558189251Ssam value = wpa_config_get(ssid, "auth_alg"); 559189251Ssam if (value == NULL) 560189251Ssam return; 561189251Ssam if (value[0]) 562189251Ssam fprintf(f, "\tauth_alg=%s\n", value); 563189251Ssam os_free(value); 564189251Ssam} 565189251Ssam 566189251Ssam 567189251Ssam#ifdef IEEE8021X_EAPOL 568189251Ssamstatic void write_eap(FILE *f, struct wpa_ssid *ssid) 569189251Ssam{ 570189251Ssam char *value; 571189251Ssam 572189251Ssam value = wpa_config_get(ssid, "eap"); 573189251Ssam if (value == NULL) 574189251Ssam return; 575189251Ssam 576189251Ssam if (value[0]) 577189251Ssam fprintf(f, "\teap=%s\n", value); 578189251Ssam os_free(value); 579189251Ssam} 580189251Ssam#endif /* IEEE8021X_EAPOL */ 581189251Ssam 582189251Ssam 583189251Ssamstatic void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid) 584189251Ssam{ 585189251Ssam char field[20], *value; 586189251Ssam int res; 587189251Ssam 588189251Ssam res = os_snprintf(field, sizeof(field), "wep_key%d", idx); 589189251Ssam if (res < 0 || (size_t) res >= sizeof(field)) 590189251Ssam return; 591189251Ssam value = wpa_config_get(ssid, field); 592189251Ssam if (value) { 593189251Ssam fprintf(f, "\t%s=%s\n", field, value); 594189251Ssam os_free(value); 595189251Ssam } 596189251Ssam} 597189251Ssam 598189251Ssam 599252726Srpaulo#ifdef CONFIG_P2P 600252726Srpaulostatic void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid) 601252726Srpaulo{ 602252726Srpaulo char *value = wpa_config_get(ssid, "p2p_client_list"); 603252726Srpaulo if (value == NULL) 604252726Srpaulo return; 605252726Srpaulo fprintf(f, "\tp2p_client_list=%s\n", value); 606252726Srpaulo os_free(value); 607252726Srpaulo} 608252726Srpaulo#endif /* CONFIG_P2P */ 609252726Srpaulo 610252726Srpaulo 611189251Ssamstatic void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid) 612189251Ssam{ 613189251Ssam int i; 614189251Ssam 615189251Ssam#define STR(t) write_str(f, #t, ssid) 616189251Ssam#define INT(t) write_int(f, #t, ssid->t, 0) 617189251Ssam#define INTe(t) write_int(f, #t, ssid->eap.t, 0) 618189251Ssam#define INT_DEF(t, def) write_int(f, #t, ssid->t, def) 619189251Ssam#define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def) 620189251Ssam 621189251Ssam STR(ssid); 622189251Ssam INT(scan_ssid); 623189251Ssam write_bssid(f, ssid); 624189251Ssam write_psk(f, ssid); 625189251Ssam write_proto(f, ssid); 626189251Ssam write_key_mgmt(f, ssid); 627252726Srpaulo INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD); 628189251Ssam write_pairwise(f, ssid); 629189251Ssam write_group(f, ssid); 630189251Ssam write_auth_alg(f, ssid); 631252726Srpaulo STR(bgscan); 632252726Srpaulo STR(autoscan); 633189251Ssam#ifdef IEEE8021X_EAPOL 634189251Ssam write_eap(f, ssid); 635189251Ssam STR(identity); 636189251Ssam STR(anonymous_identity); 637189251Ssam STR(password); 638189251Ssam STR(ca_cert); 639189251Ssam STR(ca_path); 640189251Ssam STR(client_cert); 641189251Ssam STR(private_key); 642189251Ssam STR(private_key_passwd); 643189251Ssam STR(dh_file); 644189251Ssam STR(subject_match); 645189251Ssam STR(altsubject_match); 646189251Ssam STR(ca_cert2); 647189251Ssam STR(ca_path2); 648189251Ssam STR(client_cert2); 649189251Ssam STR(private_key2); 650189251Ssam STR(private_key2_passwd); 651189251Ssam STR(dh_file2); 652189251Ssam STR(subject_match2); 653189251Ssam STR(altsubject_match2); 654189251Ssam STR(phase1); 655189251Ssam STR(phase2); 656189251Ssam STR(pcsc); 657189251Ssam STR(pin); 658189251Ssam STR(engine_id); 659189251Ssam STR(key_id); 660189251Ssam STR(cert_id); 661189251Ssam STR(ca_cert_id); 662189251Ssam STR(key2_id); 663189251Ssam STR(pin2); 664189251Ssam STR(engine2_id); 665189251Ssam STR(cert2_id); 666189251Ssam STR(ca_cert2_id); 667189251Ssam INTe(engine); 668189251Ssam INTe(engine2); 669189251Ssam INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS); 670189251Ssam#endif /* IEEE8021X_EAPOL */ 671189251Ssam for (i = 0; i < 4; i++) 672189251Ssam write_wep_key(f, i, ssid); 673189251Ssam INT(wep_tx_keyidx); 674189251Ssam INT(priority); 675189251Ssam#ifdef IEEE8021X_EAPOL 676189251Ssam INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND); 677189251Ssam STR(pac_file); 678189251Ssam INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE); 679189251Ssam#endif /* IEEE8021X_EAPOL */ 680189251Ssam INT(mode); 681252726Srpaulo INT(frequency); 682252726Srpaulo write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1); 683189251Ssam INT(disabled); 684189251Ssam INT(peerkey); 685189251Ssam#ifdef CONFIG_IEEE80211W 686252726Srpaulo write_int(f, "ieee80211w", ssid->ieee80211w, 687252726Srpaulo MGMT_FRAME_PROTECTION_DEFAULT); 688189251Ssam#endif /* CONFIG_IEEE80211W */ 689189251Ssam STR(id_str); 690252726Srpaulo#ifdef CONFIG_P2P 691252726Srpaulo write_p2p_client_list(f, ssid); 692252726Srpaulo#endif /* CONFIG_P2P */ 693189251Ssam 694189251Ssam#undef STR 695189251Ssam#undef INT 696189251Ssam#undef INT_DEF 697189251Ssam} 698189251Ssam 699189251Ssam 700252726Srpaulostatic void wpa_config_write_cred(FILE *f, struct wpa_cred *cred) 701252726Srpaulo{ 702252726Srpaulo if (cred->priority) 703252726Srpaulo fprintf(f, "\tpriority=%d\n", cred->priority); 704252726Srpaulo if (cred->pcsc) 705252726Srpaulo fprintf(f, "\tpcsc=%d\n", cred->pcsc); 706252726Srpaulo if (cred->realm) 707252726Srpaulo fprintf(f, "\trealm=\"%s\"\n", cred->realm); 708252726Srpaulo if (cred->username) 709252726Srpaulo fprintf(f, "\tusername=\"%s\"\n", cred->username); 710252726Srpaulo if (cred->password && cred->ext_password) 711252726Srpaulo fprintf(f, "\tpassword=ext:%s\n", cred->password); 712252726Srpaulo else if (cred->password) 713252726Srpaulo fprintf(f, "\tpassword=\"%s\"\n", cred->password); 714252726Srpaulo if (cred->ca_cert) 715252726Srpaulo fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert); 716252726Srpaulo if (cred->client_cert) 717252726Srpaulo fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert); 718252726Srpaulo if (cred->private_key) 719252726Srpaulo fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key); 720252726Srpaulo if (cred->private_key_passwd) 721252726Srpaulo fprintf(f, "\tprivate_key_passwd=\"%s\"\n", 722252726Srpaulo cred->private_key_passwd); 723252726Srpaulo if (cred->imsi) 724252726Srpaulo fprintf(f, "\timsi=\"%s\"\n", cred->imsi); 725252726Srpaulo if (cred->milenage) 726252726Srpaulo fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage); 727252726Srpaulo if (cred->domain) 728252726Srpaulo fprintf(f, "\tdomain=\"%s\"\n", cred->domain); 729252726Srpaulo if (cred->roaming_consortium_len) { 730252726Srpaulo size_t i; 731252726Srpaulo fprintf(f, "\troaming_consortium="); 732252726Srpaulo for (i = 0; i < cred->roaming_consortium_len; i++) 733252726Srpaulo fprintf(f, "%02x", cred->roaming_consortium[i]); 734252726Srpaulo fprintf(f, "\n"); 735252726Srpaulo } 736252726Srpaulo if (cred->eap_method) { 737252726Srpaulo const char *name; 738252726Srpaulo name = eap_get_name(cred->eap_method[0].vendor, 739252726Srpaulo cred->eap_method[0].method); 740252726Srpaulo fprintf(f, "\teap=%s\n", name); 741252726Srpaulo } 742252726Srpaulo if (cred->phase1) 743252726Srpaulo fprintf(f, "\tphase1=\"%s\"\n", cred->phase1); 744252726Srpaulo if (cred->phase2) 745252726Srpaulo fprintf(f, "\tphase2=\"%s\"\n", cred->phase2); 746252726Srpaulo if (cred->excluded_ssid) { 747252726Srpaulo size_t i, j; 748252726Srpaulo for (i = 0; i < cred->num_excluded_ssid; i++) { 749252726Srpaulo struct excluded_ssid *e = &cred->excluded_ssid[i]; 750252726Srpaulo fprintf(f, "\texcluded_ssid="); 751252726Srpaulo for (j = 0; j < e->ssid_len; j++) 752252726Srpaulo fprintf(f, "%02x", e->ssid[j]); 753252726Srpaulo fprintf(f, "\n"); 754252726Srpaulo } 755252726Srpaulo } 756252726Srpaulo} 757252726Srpaulo 758252726Srpaulo 759189251Ssam#ifndef CONFIG_NO_CONFIG_BLOBS 760189251Ssamstatic int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob) 761189251Ssam{ 762189251Ssam unsigned char *encoded; 763189251Ssam 764189251Ssam encoded = base64_encode(blob->data, blob->len, NULL); 765189251Ssam if (encoded == NULL) 766189251Ssam return -1; 767189251Ssam 768189251Ssam fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded); 769189251Ssam os_free(encoded); 770189251Ssam return 0; 771189251Ssam} 772189251Ssam#endif /* CONFIG_NO_CONFIG_BLOBS */ 773189251Ssam 774189251Ssam 775252726Srpaulostatic void write_global_bin(FILE *f, const char *field, 776252726Srpaulo const struct wpabuf *val) 777252726Srpaulo{ 778252726Srpaulo size_t i; 779252726Srpaulo const u8 *pos; 780252726Srpaulo 781252726Srpaulo if (val == NULL) 782252726Srpaulo return; 783252726Srpaulo 784252726Srpaulo fprintf(f, "%s=", field); 785252726Srpaulo pos = wpabuf_head(val); 786252726Srpaulo for (i = 0; i < wpabuf_len(val); i++) 787252726Srpaulo fprintf(f, "%02X", *pos++); 788252726Srpaulo fprintf(f, "\n"); 789252726Srpaulo} 790252726Srpaulo 791252726Srpaulo 792189251Ssamstatic void wpa_config_write_global(FILE *f, struct wpa_config *config) 793189251Ssam{ 794189251Ssam#ifdef CONFIG_CTRL_IFACE 795189251Ssam if (config->ctrl_interface) 796189251Ssam fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface); 797189251Ssam if (config->ctrl_interface_group) 798189251Ssam fprintf(f, "ctrl_interface_group=%s\n", 799189251Ssam config->ctrl_interface_group); 800189251Ssam#endif /* CONFIG_CTRL_IFACE */ 801189251Ssam if (config->eapol_version != DEFAULT_EAPOL_VERSION) 802189251Ssam fprintf(f, "eapol_version=%d\n", config->eapol_version); 803189251Ssam if (config->ap_scan != DEFAULT_AP_SCAN) 804189251Ssam fprintf(f, "ap_scan=%d\n", config->ap_scan); 805252726Srpaulo if (config->disable_scan_offload) 806252726Srpaulo fprintf(f, "disable_scan_offload=%d\n", 807252726Srpaulo config->disable_scan_offload); 808189251Ssam if (config->fast_reauth != DEFAULT_FAST_REAUTH) 809189251Ssam fprintf(f, "fast_reauth=%d\n", config->fast_reauth); 810189251Ssam if (config->opensc_engine_path) 811189251Ssam fprintf(f, "opensc_engine_path=%s\n", 812189251Ssam config->opensc_engine_path); 813189251Ssam if (config->pkcs11_engine_path) 814189251Ssam fprintf(f, "pkcs11_engine_path=%s\n", 815189251Ssam config->pkcs11_engine_path); 816189251Ssam if (config->pkcs11_module_path) 817189251Ssam fprintf(f, "pkcs11_module_path=%s\n", 818189251Ssam config->pkcs11_module_path); 819252726Srpaulo if (config->pcsc_reader) 820252726Srpaulo fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader); 821252726Srpaulo if (config->pcsc_pin) 822252726Srpaulo fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin); 823189251Ssam if (config->driver_param) 824189251Ssam fprintf(f, "driver_param=%s\n", config->driver_param); 825189251Ssam if (config->dot11RSNAConfigPMKLifetime) 826189251Ssam fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n", 827189251Ssam config->dot11RSNAConfigPMKLifetime); 828189251Ssam if (config->dot11RSNAConfigPMKReauthThreshold) 829189251Ssam fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n", 830189251Ssam config->dot11RSNAConfigPMKReauthThreshold); 831189251Ssam if (config->dot11RSNAConfigSATimeout) 832189251Ssam fprintf(f, "dot11RSNAConfigSATimeout=%d\n", 833189251Ssam config->dot11RSNAConfigSATimeout); 834189251Ssam if (config->update_config) 835189251Ssam fprintf(f, "update_config=%d\n", config->update_config); 836189251Ssam#ifdef CONFIG_WPS 837189251Ssam if (!is_nil_uuid(config->uuid)) { 838189251Ssam char buf[40]; 839189251Ssam uuid_bin2str(config->uuid, buf, sizeof(buf)); 840189251Ssam fprintf(f, "uuid=%s\n", buf); 841189251Ssam } 842189251Ssam if (config->device_name) 843189251Ssam fprintf(f, "device_name=%s\n", config->device_name); 844189251Ssam if (config->manufacturer) 845189251Ssam fprintf(f, "manufacturer=%s\n", config->manufacturer); 846189251Ssam if (config->model_name) 847189251Ssam fprintf(f, "model_name=%s\n", config->model_name); 848189251Ssam if (config->model_number) 849189251Ssam fprintf(f, "model_number=%s\n", config->model_number); 850189251Ssam if (config->serial_number) 851189251Ssam fprintf(f, "serial_number=%s\n", config->serial_number); 852252726Srpaulo { 853252726Srpaulo char _buf[WPS_DEV_TYPE_BUFSIZE], *buf; 854252726Srpaulo buf = wps_dev_type_bin2str(config->device_type, 855252726Srpaulo _buf, sizeof(_buf)); 856252726Srpaulo if (os_strcmp(buf, "0-00000000-0") != 0) 857252726Srpaulo fprintf(f, "device_type=%s\n", buf); 858252726Srpaulo } 859189251Ssam if (WPA_GET_BE32(config->os_version)) 860189251Ssam fprintf(f, "os_version=%08x\n", 861189251Ssam WPA_GET_BE32(config->os_version)); 862214734Srpaulo if (config->config_methods) 863214734Srpaulo fprintf(f, "config_methods=%s\n", config->config_methods); 864189251Ssam if (config->wps_cred_processing) 865189251Ssam fprintf(f, "wps_cred_processing=%d\n", 866189251Ssam config->wps_cred_processing); 867252726Srpaulo if (config->wps_vendor_ext_m1) { 868252726Srpaulo int i, len = wpabuf_len(config->wps_vendor_ext_m1); 869252726Srpaulo const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1); 870252726Srpaulo if (len > 0) { 871252726Srpaulo fprintf(f, "wps_vendor_ext_m1="); 872252726Srpaulo for (i = 0; i < len; i++) 873252726Srpaulo fprintf(f, "%02x", *p++); 874252726Srpaulo fprintf(f, "\n"); 875252726Srpaulo } 876252726Srpaulo } 877189251Ssam#endif /* CONFIG_WPS */ 878252726Srpaulo#ifdef CONFIG_P2P 879252726Srpaulo if (config->p2p_listen_reg_class) 880252726Srpaulo fprintf(f, "p2p_listen_reg_class=%u\n", 881252726Srpaulo config->p2p_listen_reg_class); 882252726Srpaulo if (config->p2p_listen_channel) 883252726Srpaulo fprintf(f, "p2p_listen_channel=%u\n", 884252726Srpaulo config->p2p_listen_channel); 885252726Srpaulo if (config->p2p_oper_reg_class) 886252726Srpaulo fprintf(f, "p2p_oper_reg_class=%u\n", 887252726Srpaulo config->p2p_oper_reg_class); 888252726Srpaulo if (config->p2p_oper_channel) 889252726Srpaulo fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel); 890252726Srpaulo if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT) 891252726Srpaulo fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent); 892252726Srpaulo if (config->p2p_ssid_postfix) 893252726Srpaulo fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix); 894252726Srpaulo if (config->persistent_reconnect) 895252726Srpaulo fprintf(f, "persistent_reconnect=%u\n", 896252726Srpaulo config->persistent_reconnect); 897252726Srpaulo if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS) 898252726Srpaulo fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss); 899252726Srpaulo if (config->p2p_group_idle) 900252726Srpaulo fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle); 901252726Srpaulo if (config->p2p_pref_chan) { 902252726Srpaulo unsigned int i; 903252726Srpaulo fprintf(f, "p2p_pref_chan="); 904252726Srpaulo for (i = 0; i < config->num_p2p_pref_chan; i++) { 905252726Srpaulo fprintf(f, "%s%u:%u", i > 0 ? "," : "", 906252726Srpaulo config->p2p_pref_chan[i].op_class, 907252726Srpaulo config->p2p_pref_chan[i].chan); 908252726Srpaulo } 909252726Srpaulo fprintf(f, "\n"); 910252726Srpaulo } 911252726Srpaulo if (config->p2p_go_ht40) 912252726Srpaulo fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40); 913252726Srpaulo if (config->p2p_disabled) 914252726Srpaulo fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled); 915252726Srpaulo if (config->p2p_no_group_iface) 916252726Srpaulo fprintf(f, "p2p_no_group_iface=%u\n", 917252726Srpaulo config->p2p_no_group_iface); 918252726Srpaulo#endif /* CONFIG_P2P */ 919189251Ssam if (config->country[0] && config->country[1]) { 920189251Ssam fprintf(f, "country=%c%c\n", 921189251Ssam config->country[0], config->country[1]); 922189251Ssam } 923214734Srpaulo if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT) 924214734Srpaulo fprintf(f, "bss_max_count=%u\n", config->bss_max_count); 925252726Srpaulo if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE) 926252726Srpaulo fprintf(f, "bss_expiration_age=%u\n", 927252726Srpaulo config->bss_expiration_age); 928252726Srpaulo if (config->bss_expiration_scan_count != 929252726Srpaulo DEFAULT_BSS_EXPIRATION_SCAN_COUNT) 930252726Srpaulo fprintf(f, "bss_expiration_scan_count=%u\n", 931252726Srpaulo config->bss_expiration_scan_count); 932214734Srpaulo if (config->filter_ssids) 933214734Srpaulo fprintf(f, "filter_ssids=%d\n", config->filter_ssids); 934252726Srpaulo if (config->max_num_sta != DEFAULT_MAX_NUM_STA) 935252726Srpaulo fprintf(f, "max_num_sta=%u\n", config->max_num_sta); 936252726Srpaulo if (config->disassoc_low_ack) 937252726Srpaulo fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack); 938252726Srpaulo#ifdef CONFIG_HS20 939252726Srpaulo if (config->hs20) 940252726Srpaulo fprintf(f, "hs20=1\n"); 941252726Srpaulo#endif /* CONFIG_HS20 */ 942252726Srpaulo#ifdef CONFIG_INTERWORKING 943252726Srpaulo if (config->interworking) 944252726Srpaulo fprintf(f, "interworking=%u\n", config->interworking); 945252726Srpaulo if (!is_zero_ether_addr(config->hessid)) 946252726Srpaulo fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid)); 947252726Srpaulo if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE) 948252726Srpaulo fprintf(f, "access_network_type=%d\n", 949252726Srpaulo config->access_network_type); 950252726Srpaulo#endif /* CONFIG_INTERWORKING */ 951252726Srpaulo if (config->pbc_in_m1) 952252726Srpaulo fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1); 953252726Srpaulo if (config->wps_nfc_dev_pw_id) 954252726Srpaulo fprintf(f, "wps_nfc_dev_pw_id=%d\n", 955252726Srpaulo config->wps_nfc_dev_pw_id); 956252726Srpaulo write_global_bin(f, "wps_nfc_dh_pubkey", config->wps_nfc_dh_pubkey); 957252726Srpaulo write_global_bin(f, "wps_nfc_dh_privkey", config->wps_nfc_dh_privkey); 958252726Srpaulo write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw); 959252726Srpaulo 960252726Srpaulo if (config->ext_password_backend) 961252726Srpaulo fprintf(f, "ext_password_backend=%s\n", 962252726Srpaulo config->ext_password_backend); 963252726Srpaulo if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY) 964252726Srpaulo fprintf(f, "p2p_go_max_inactivity=%d\n", 965252726Srpaulo config->p2p_go_max_inactivity); 966252726Srpaulo if (config->auto_interworking) 967252726Srpaulo fprintf(f, "auto_interworking=%d\n", 968252726Srpaulo config->auto_interworking); 969252726Srpaulo if (config->okc) 970252726Srpaulo fprintf(f, "okc=%d\n", config->okc); 971252726Srpaulo if (config->pmf) 972252726Srpaulo fprintf(f, "pmf=%d\n", config->pmf); 973189251Ssam} 974189251Ssam 975189251Ssam#endif /* CONFIG_NO_CONFIG_WRITE */ 976189251Ssam 977189251Ssam 978189251Ssamint wpa_config_write(const char *name, struct wpa_config *config) 979189251Ssam{ 980189251Ssam#ifndef CONFIG_NO_CONFIG_WRITE 981189251Ssam FILE *f; 982189251Ssam struct wpa_ssid *ssid; 983252726Srpaulo struct wpa_cred *cred; 984189251Ssam#ifndef CONFIG_NO_CONFIG_BLOBS 985189251Ssam struct wpa_config_blob *blob; 986189251Ssam#endif /* CONFIG_NO_CONFIG_BLOBS */ 987189251Ssam int ret = 0; 988189251Ssam 989189251Ssam wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name); 990189251Ssam 991189251Ssam f = fopen(name, "w"); 992189251Ssam if (f == NULL) { 993189251Ssam wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name); 994189251Ssam return -1; 995189251Ssam } 996189251Ssam 997189251Ssam wpa_config_write_global(f, config); 998189251Ssam 999252726Srpaulo for (cred = config->cred; cred; cred = cred->next) { 1000252726Srpaulo fprintf(f, "\ncred={\n"); 1001252726Srpaulo wpa_config_write_cred(f, cred); 1002252726Srpaulo fprintf(f, "}\n"); 1003252726Srpaulo } 1004252726Srpaulo 1005189251Ssam for (ssid = config->ssid; ssid; ssid = ssid->next) { 1006252726Srpaulo if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary) 1007252726Srpaulo continue; /* do not save temporary networks */ 1008252726Srpaulo if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set && 1009252726Srpaulo !ssid->passphrase) 1010252726Srpaulo continue; /* do not save invalid network */ 1011189251Ssam fprintf(f, "\nnetwork={\n"); 1012189251Ssam wpa_config_write_network(f, ssid); 1013189251Ssam fprintf(f, "}\n"); 1014189251Ssam } 1015189251Ssam 1016189251Ssam#ifndef CONFIG_NO_CONFIG_BLOBS 1017189251Ssam for (blob = config->blobs; blob; blob = blob->next) { 1018189251Ssam ret = wpa_config_write_blob(f, blob); 1019189251Ssam if (ret) 1020189251Ssam break; 1021189251Ssam } 1022189251Ssam#endif /* CONFIG_NO_CONFIG_BLOBS */ 1023189251Ssam 1024189251Ssam fclose(f); 1025189251Ssam 1026189251Ssam wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully", 1027189251Ssam name, ret ? "un" : ""); 1028189251Ssam return ret; 1029189251Ssam#else /* CONFIG_NO_CONFIG_WRITE */ 1030189251Ssam return -1; 1031189251Ssam#endif /* CONFIG_NO_CONFIG_WRITE */ 1032189251Ssam} 1033