config_file.c revision ea69e84a6f4455c59348485895d3d5e3af77a65b
1/* 2 * WPA Supplicant / Configuration backend: text file 3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 * 8 * This file implements a configuration backend for text files. All the 9 * configuration information is stored in a text file that uses a format 10 * described in the sample configuration file, wpa_supplicant.conf. 11 */ 12 13#include "includes.h" 14 15#include "common.h" 16#include "config.h" 17#include "base64.h" 18#include "uuid.h" 19#include "p2p/p2p.h" 20#include "eap_peer/eap_methods.h" 21#include "eap_peer/eap.h" 22 23 24static int newline_terminated(const char *buf, size_t buflen) 25{ 26 size_t len = os_strlen(buf); 27 if (len == 0) 28 return 0; 29 if (len == buflen - 1 && buf[buflen - 1] != '\r' && 30 buf[len - 1] != '\n') 31 return 0; 32 return 1; 33} 34 35 36static void skip_line_end(FILE *stream) 37{ 38 char buf[100]; 39 while (fgets(buf, sizeof(buf), stream)) { 40 buf[sizeof(buf) - 1] = '\0'; 41 if (newline_terminated(buf, sizeof(buf))) 42 return; 43 } 44} 45 46 47/** 48 * wpa_config_get_line - Read the next configuration file line 49 * @s: Buffer for the line 50 * @size: The buffer length 51 * @stream: File stream to read from 52 * @line: Pointer to a variable storing the file line number 53 * @_pos: Buffer for the pointer to the beginning of data on the text line or 54 * %NULL if not needed (returned value used instead) 55 * Returns: Pointer to the beginning of data on the text line or %NULL if no 56 * more text lines are available. 57 * 58 * This function reads the next non-empty line from the configuration file and 59 * removes comments. The returned string is guaranteed to be null-terminated. 60 */ 61static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line, 62 char **_pos) 63{ 64 char *pos, *end, *sstart; 65 66 while (fgets(s, size, stream)) { 67 (*line)++; 68 s[size - 1] = '\0'; 69 if (!newline_terminated(s, size)) { 70 /* 71 * The line was truncated - skip rest of it to avoid 72 * confusing error messages. 73 */ 74 wpa_printf(MSG_INFO, "Long line in configuration file " 75 "truncated"); 76 skip_line_end(stream); 77 } 78 pos = s; 79 80 /* Skip white space from the beginning of line. */ 81 while (*pos == ' ' || *pos == '\t' || *pos == '\r') 82 pos++; 83 84 /* Skip comment lines and empty lines */ 85 if (*pos == '#' || *pos == '\n' || *pos == '\0') 86 continue; 87 88 /* 89 * Remove # comments unless they are within a double quoted 90 * string. 91 */ 92 sstart = os_strchr(pos, '"'); 93 if (sstart) 94 sstart = os_strrchr(sstart + 1, '"'); 95 if (!sstart) 96 sstart = pos; 97 end = os_strchr(sstart, '#'); 98 if (end) 99 *end-- = '\0'; 100 else 101 end = pos + os_strlen(pos) - 1; 102 103 /* Remove trailing white space. */ 104 while (end > pos && 105 (*end == '\n' || *end == ' ' || *end == '\t' || 106 *end == '\r')) 107 *end-- = '\0'; 108 109 if (*pos == '\0') 110 continue; 111 112 if (_pos) 113 *_pos = pos; 114 return pos; 115 } 116 117 if (_pos) 118 *_pos = NULL; 119 return NULL; 120} 121 122 123static int wpa_config_validate_network(struct wpa_ssid *ssid, int line) 124{ 125 int errors = 0; 126 127 if (ssid->passphrase) { 128 if (ssid->psk_set) { 129 wpa_printf(MSG_ERROR, "Line %d: both PSK and " 130 "passphrase configured.", line); 131 errors++; 132 } 133 wpa_config_update_psk(ssid); 134 } 135 136 if ((ssid->group_cipher & WPA_CIPHER_CCMP) && 137 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) && 138 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) { 139 /* Group cipher cannot be stronger than the pairwise cipher. */ 140 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher" 141 " list since it was not allowed for pairwise " 142 "cipher", line); 143 ssid->group_cipher &= ~WPA_CIPHER_CCMP; 144 } 145 146 return errors; 147} 148 149 150static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id) 151{ 152 struct wpa_ssid *ssid; 153 int errors = 0, end = 0; 154 char buf[2000], *pos, *pos2; 155 156 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block", 157 *line); 158 ssid = os_zalloc(sizeof(*ssid)); 159 if (ssid == NULL) 160 return NULL; 161 ssid->id = id; 162 163 wpa_config_set_network_defaults(ssid); 164 165 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { 166 if (os_strcmp(pos, "}") == 0) { 167 end = 1; 168 break; 169 } 170 171 pos2 = os_strchr(pos, '='); 172 if (pos2 == NULL) { 173 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line " 174 "'%s'.", *line, pos); 175 errors++; 176 continue; 177 } 178 179 *pos2++ = '\0'; 180 if (*pos2 == '"') { 181 if (os_strchr(pos2 + 1, '"') == NULL) { 182 wpa_printf(MSG_ERROR, "Line %d: invalid " 183 "quotation '%s'.", *line, pos2); 184 errors++; 185 continue; 186 } 187 } 188 189 if (wpa_config_set(ssid, pos, pos2, *line) < 0) 190 errors++; 191 } 192 193 if (!end) { 194 wpa_printf(MSG_ERROR, "Line %d: network block was not " 195 "terminated properly.", *line); 196 errors++; 197 } 198 199 errors += wpa_config_validate_network(ssid, *line); 200 201 if (errors) { 202 wpa_config_free_ssid(ssid); 203 ssid = NULL; 204 } 205 206 return ssid; 207} 208 209 210static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id) 211{ 212 struct wpa_cred *cred; 213 int errors = 0, end = 0; 214 char buf[256], *pos, *pos2; 215 216 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line); 217 cred = os_zalloc(sizeof(*cred)); 218 if (cred == NULL) 219 return NULL; 220 cred->id = id; 221 222 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { 223 if (os_strcmp(pos, "}") == 0) { 224 end = 1; 225 break; 226 } 227 228 pos2 = os_strchr(pos, '='); 229 if (pos2 == NULL) { 230 wpa_printf(MSG_ERROR, "Line %d: Invalid cred line " 231 "'%s'.", *line, pos); 232 errors++; 233 continue; 234 } 235 236 *pos2++ = '\0'; 237 if (*pos2 == '"') { 238 if (os_strchr(pos2 + 1, '"') == NULL) { 239 wpa_printf(MSG_ERROR, "Line %d: invalid " 240 "quotation '%s'.", *line, pos2); 241 errors++; 242 continue; 243 } 244 } 245 246 if (wpa_config_set_cred(cred, pos, pos2, *line) < 0) 247 errors++; 248 } 249 250 if (!end) { 251 wpa_printf(MSG_ERROR, "Line %d: cred block was not " 252 "terminated properly.", *line); 253 errors++; 254 } 255 256 if (errors) { 257 wpa_config_free_cred(cred); 258 cred = NULL; 259 } 260 261 return cred; 262} 263 264 265#ifndef CONFIG_NO_CONFIG_BLOBS 266static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line, 267 const char *name) 268{ 269 struct wpa_config_blob *blob; 270 char buf[256], *pos; 271 unsigned char *encoded = NULL, *nencoded; 272 int end = 0; 273 size_t encoded_len = 0, len; 274 275 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'", 276 *line, name); 277 278 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { 279 if (os_strcmp(pos, "}") == 0) { 280 end = 1; 281 break; 282 } 283 284 len = os_strlen(pos); 285 nencoded = os_realloc(encoded, encoded_len + len); 286 if (nencoded == NULL) { 287 wpa_printf(MSG_ERROR, "Line %d: not enough memory for " 288 "blob", *line); 289 os_free(encoded); 290 return NULL; 291 } 292 encoded = nencoded; 293 os_memcpy(encoded + encoded_len, pos, len); 294 encoded_len += len; 295 } 296 297 if (!end) { 298 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated " 299 "properly", *line); 300 os_free(encoded); 301 return NULL; 302 } 303 304 blob = os_zalloc(sizeof(*blob)); 305 if (blob == NULL) { 306 os_free(encoded); 307 return NULL; 308 } 309 blob->name = os_strdup(name); 310 blob->data = base64_decode(encoded, encoded_len, &blob->len); 311 os_free(encoded); 312 313 if (blob->name == NULL || blob->data == NULL) { 314 wpa_config_free_blob(blob); 315 return NULL; 316 } 317 318 return blob; 319} 320 321 322static int wpa_config_process_blob(struct wpa_config *config, FILE *f, 323 int *line, char *bname) 324{ 325 char *name_end; 326 struct wpa_config_blob *blob; 327 328 name_end = os_strchr(bname, '='); 329 if (name_end == NULL) { 330 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator", 331 *line); 332 return -1; 333 } 334 *name_end = '\0'; 335 336 blob = wpa_config_read_blob(f, line, bname); 337 if (blob == NULL) { 338 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s", 339 *line, bname); 340 return -1; 341 } 342 wpa_config_set_blob(config, blob); 343 return 0; 344} 345#endif /* CONFIG_NO_CONFIG_BLOBS */ 346 347 348struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp) 349{ 350 FILE *f; 351 char buf[512], *pos; 352 int errors = 0, line = 0; 353 struct wpa_ssid *ssid, *tail = NULL, *head = NULL; 354 struct wpa_cred *cred, *cred_tail = NULL, *cred_head = NULL; 355 struct wpa_config *config; 356 int id = 0; 357 int cred_id = 0; 358 359 if (name == NULL) 360 return NULL; 361 if (cfgp) 362 config = cfgp; 363 else 364 config = wpa_config_alloc_empty(NULL, NULL); 365 if (config == NULL) { 366 wpa_printf(MSG_ERROR, "Failed to allocate config file " 367 "structure"); 368 return NULL; 369 } 370 head = config->ssid; 371 cred_head = config->cred; 372 373 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name); 374 f = fopen(name, "r"); 375 if (f == NULL) { 376 wpa_printf(MSG_ERROR, "Failed to open config file '%s', " 377 "error: %s", name, strerror(errno)); 378 os_free(config); 379 return NULL; 380 } 381 382 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) { 383 if (os_strcmp(pos, "network={") == 0) { 384 ssid = wpa_config_read_network(f, &line, id++); 385 if (ssid == NULL) { 386 wpa_printf(MSG_ERROR, "Line %d: failed to " 387 "parse network block.", line); 388 errors++; 389 continue; 390 } 391 if (head == NULL) { 392 head = tail = ssid; 393 } else { 394 tail->next = ssid; 395 tail = ssid; 396 } 397 if (wpa_config_add_prio_network(config, ssid)) { 398 wpa_printf(MSG_ERROR, "Line %d: failed to add " 399 "network block to priority list.", 400 line); 401 errors++; 402 continue; 403 } 404 } else if (os_strcmp(pos, "cred={") == 0) { 405 cred = wpa_config_read_cred(f, &line, cred_id++); 406 if (cred == NULL) { 407 wpa_printf(MSG_ERROR, "Line %d: failed to " 408 "parse cred block.", line); 409 errors++; 410 continue; 411 } 412 if (cred_head == NULL) { 413 cred_head = cred_tail = cred; 414 } else { 415 cred_tail->next = cred; 416 cred_tail = cred; 417 } 418#ifndef CONFIG_NO_CONFIG_BLOBS 419 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) { 420 if (wpa_config_process_blob(config, f, &line, pos + 12) 421 < 0) { 422 wpa_printf(MSG_ERROR, "Line %d: failed to " 423 "process blob.", line); 424 errors++; 425 continue; 426 } 427#endif /* CONFIG_NO_CONFIG_BLOBS */ 428 } else if (wpa_config_process_global(config, pos, line) < 0) { 429 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration " 430 "line '%s'.", line, pos); 431 errors++; 432 continue; 433 } 434 } 435 436 fclose(f); 437 438 config->ssid = head; 439 wpa_config_debug_dump_networks(config); 440 config->cred = cred_head; 441 442#ifndef WPA_IGNORE_CONFIG_ERRORS 443 if (errors) { 444 wpa_config_free(config); 445 config = NULL; 446 head = NULL; 447 } 448#endif /* WPA_IGNORE_CONFIG_ERRORS */ 449 450 return config; 451} 452 453 454#ifndef CONFIG_NO_CONFIG_WRITE 455 456static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid) 457{ 458 char *value = wpa_config_get(ssid, field); 459 if (value == NULL) 460 return; 461 fprintf(f, "\t%s=%s\n", field, value); 462 os_free(value); 463} 464 465 466static void write_int(FILE *f, const char *field, int value, int def) 467{ 468 if (value == def) 469 return; 470 fprintf(f, "\t%s=%d\n", field, value); 471} 472 473 474static void write_bssid(FILE *f, struct wpa_ssid *ssid) 475{ 476 char *value = wpa_config_get(ssid, "bssid"); 477 if (value == NULL) 478 return; 479 fprintf(f, "\tbssid=%s\n", value); 480 os_free(value); 481} 482 483 484static void write_psk(FILE *f, struct wpa_ssid *ssid) 485{ 486 char *value = wpa_config_get(ssid, "psk"); 487 if (value == NULL) 488 return; 489 fprintf(f, "\tpsk=%s\n", value); 490 os_free(value); 491} 492 493 494static void write_proto(FILE *f, struct wpa_ssid *ssid) 495{ 496 char *value; 497 498 if (ssid->proto == DEFAULT_PROTO) 499 return; 500 501 value = wpa_config_get(ssid, "proto"); 502 if (value == NULL) 503 return; 504 if (value[0]) 505 fprintf(f, "\tproto=%s\n", value); 506 os_free(value); 507} 508 509 510static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid) 511{ 512 char *value; 513 514 if (ssid->key_mgmt == DEFAULT_KEY_MGMT) 515 return; 516 517 value = wpa_config_get(ssid, "key_mgmt"); 518 if (value == NULL) 519 return; 520 if (value[0]) 521 fprintf(f, "\tkey_mgmt=%s\n", value); 522 os_free(value); 523} 524 525 526static void write_pairwise(FILE *f, struct wpa_ssid *ssid) 527{ 528 char *value; 529 530 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE) 531 return; 532 533 value = wpa_config_get(ssid, "pairwise"); 534 if (value == NULL) 535 return; 536 if (value[0]) 537 fprintf(f, "\tpairwise=%s\n", value); 538 os_free(value); 539} 540 541 542static void write_group(FILE *f, struct wpa_ssid *ssid) 543{ 544 char *value; 545 546 if (ssid->group_cipher == DEFAULT_GROUP) 547 return; 548 549 value = wpa_config_get(ssid, "group"); 550 if (value == NULL) 551 return; 552 if (value[0]) 553 fprintf(f, "\tgroup=%s\n", value); 554 os_free(value); 555} 556 557 558static void write_auth_alg(FILE *f, struct wpa_ssid *ssid) 559{ 560 char *value; 561 562 if (ssid->auth_alg == 0) 563 return; 564 565 value = wpa_config_get(ssid, "auth_alg"); 566 if (value == NULL) 567 return; 568 if (value[0]) 569 fprintf(f, "\tauth_alg=%s\n", value); 570 os_free(value); 571} 572 573 574#ifdef IEEE8021X_EAPOL 575static void write_eap(FILE *f, struct wpa_ssid *ssid) 576{ 577 char *value; 578 579 value = wpa_config_get(ssid, "eap"); 580 if (value == NULL) 581 return; 582 583 if (value[0]) 584 fprintf(f, "\teap=%s\n", value); 585 os_free(value); 586} 587#endif /* IEEE8021X_EAPOL */ 588 589 590static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid) 591{ 592 char field[20], *value; 593 int res; 594 595 res = os_snprintf(field, sizeof(field), "wep_key%d", idx); 596 if (res < 0 || (size_t) res >= sizeof(field)) 597 return; 598 value = wpa_config_get(ssid, field); 599 if (value) { 600 fprintf(f, "\t%s=%s\n", field, value); 601 os_free(value); 602 } 603} 604 605 606#ifdef CONFIG_P2P 607static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid) 608{ 609 char *value = wpa_config_get(ssid, "p2p_client_list"); 610 if (value == NULL) 611 return; 612 fprintf(f, "\tp2p_client_list=%s\n", value); 613 os_free(value); 614} 615#endif /* CONFIG_P2P */ 616 617 618static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid) 619{ 620 int i; 621 622#define STR(t) write_str(f, #t, ssid) 623#define INT(t) write_int(f, #t, ssid->t, 0) 624#define INTe(t) write_int(f, #t, ssid->eap.t, 0) 625#define INT_DEF(t, def) write_int(f, #t, ssid->t, def) 626#define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def) 627 628 STR(ssid); 629 INT(scan_ssid); 630 write_bssid(f, ssid); 631 write_psk(f, ssid); 632 write_proto(f, ssid); 633 write_key_mgmt(f, ssid); 634 INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD); 635 write_pairwise(f, ssid); 636 write_group(f, ssid); 637 write_auth_alg(f, ssid); 638 STR(bgscan); 639 STR(autoscan); 640#ifdef IEEE8021X_EAPOL 641 write_eap(f, ssid); 642 STR(identity); 643 STR(anonymous_identity); 644 STR(password); 645 STR(ca_cert); 646 STR(ca_path); 647 STR(client_cert); 648 STR(private_key); 649 STR(private_key_passwd); 650 STR(dh_file); 651 STR(subject_match); 652 STR(altsubject_match); 653 STR(ca_cert2); 654 STR(ca_path2); 655 STR(client_cert2); 656 STR(private_key2); 657 STR(private_key2_passwd); 658 STR(dh_file2); 659 STR(subject_match2); 660 STR(altsubject_match2); 661 STR(phase1); 662 STR(phase2); 663 STR(pcsc); 664 STR(pin); 665 STR(engine_id); 666 STR(key_id); 667 STR(cert_id); 668 STR(ca_cert_id); 669 STR(key2_id); 670 STR(pin2); 671 STR(engine2_id); 672 STR(cert2_id); 673 STR(ca_cert2_id); 674 INTe(engine); 675 INTe(engine2); 676 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS); 677#endif /* IEEE8021X_EAPOL */ 678 for (i = 0; i < 4; i++) 679 write_wep_key(f, i, ssid); 680 INT(wep_tx_keyidx); 681 INT(priority); 682#ifdef IEEE8021X_EAPOL 683 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND); 684 STR(pac_file); 685 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE); 686#endif /* IEEE8021X_EAPOL */ 687 INT(mode); 688 INT(frequency); 689 write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1); 690 INT(disabled); 691 INT(peerkey); 692#ifdef CONFIG_IEEE80211W 693 write_int(f, "ieee80211w", ssid->ieee80211w, 694 MGMT_FRAME_PROTECTION_DEFAULT); 695#endif /* CONFIG_IEEE80211W */ 696 STR(id_str); 697#ifdef CONFIG_P2P 698 write_p2p_client_list(f, ssid); 699#endif /* CONFIG_P2P */ 700 INT(dtim_period); 701 INT(beacon_int); 702 703#undef STR 704#undef INT 705#undef INT_DEF 706} 707 708 709static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred) 710{ 711 if (cred->priority) 712 fprintf(f, "\tpriority=%d\n", cred->priority); 713 if (cred->pcsc) 714 fprintf(f, "\tpcsc=%d\n", cred->pcsc); 715 if (cred->realm) 716 fprintf(f, "\trealm=\"%s\"\n", cred->realm); 717 if (cred->username) 718 fprintf(f, "\tusername=\"%s\"\n", cred->username); 719 if (cred->password && cred->ext_password) 720 fprintf(f, "\tpassword=ext:%s\n", cred->password); 721 else if (cred->password) 722 fprintf(f, "\tpassword=\"%s\"\n", cred->password); 723 if (cred->ca_cert) 724 fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert); 725 if (cred->client_cert) 726 fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert); 727 if (cred->private_key) 728 fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key); 729 if (cred->private_key_passwd) 730 fprintf(f, "\tprivate_key_passwd=\"%s\"\n", 731 cred->private_key_passwd); 732 if (cred->imsi) 733 fprintf(f, "\timsi=\"%s\"\n", cred->imsi); 734 if (cred->milenage) 735 fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage); 736 if (cred->domain) 737 fprintf(f, "\tdomain=\"%s\"\n", cred->domain); 738 if (cred->roaming_consortium_len) { 739 size_t i; 740 fprintf(f, "\troaming_consortium="); 741 for (i = 0; i < cred->roaming_consortium_len; i++) 742 fprintf(f, "%02x", cred->roaming_consortium[i]); 743 fprintf(f, "\n"); 744 } 745 if (cred->eap_method) { 746 const char *name; 747 name = eap_get_name(cred->eap_method[0].vendor, 748 cred->eap_method[0].method); 749 fprintf(f, "\teap=%s\n", name); 750 } 751 if (cred->phase1) 752 fprintf(f, "\tphase1=\"%s\"\n", cred->phase1); 753 if (cred->phase2) 754 fprintf(f, "\tphase2=\"%s\"\n", cred->phase2); 755 if (cred->excluded_ssid) { 756 size_t i, j; 757 for (i = 0; i < cred->num_excluded_ssid; i++) { 758 struct excluded_ssid *e = &cred->excluded_ssid[i]; 759 fprintf(f, "\texcluded_ssid="); 760 for (j = 0; j < e->ssid_len; j++) 761 fprintf(f, "%02x", e->ssid[j]); 762 fprintf(f, "\n"); 763 } 764 } 765} 766 767 768#ifndef CONFIG_NO_CONFIG_BLOBS 769static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob) 770{ 771 unsigned char *encoded; 772 773 encoded = base64_encode(blob->data, blob->len, NULL); 774 if (encoded == NULL) 775 return -1; 776 777 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded); 778 os_free(encoded); 779 return 0; 780} 781#endif /* CONFIG_NO_CONFIG_BLOBS */ 782 783 784static void write_global_bin(FILE *f, const char *field, 785 const struct wpabuf *val) 786{ 787 size_t i; 788 const u8 *pos; 789 790 if (val == NULL) 791 return; 792 793 fprintf(f, "%s=", field); 794 pos = wpabuf_head(val); 795 for (i = 0; i < wpabuf_len(val); i++) 796 fprintf(f, "%02X", *pos++); 797 fprintf(f, "\n"); 798} 799 800 801static void wpa_config_write_global(FILE *f, struct wpa_config *config) 802{ 803#ifdef CONFIG_CTRL_IFACE 804 if (config->ctrl_interface) 805 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface); 806 if (config->ctrl_interface_group) 807 fprintf(f, "ctrl_interface_group=%s\n", 808 config->ctrl_interface_group); 809#endif /* CONFIG_CTRL_IFACE */ 810 if (config->eapol_version != DEFAULT_EAPOL_VERSION) 811 fprintf(f, "eapol_version=%d\n", config->eapol_version); 812 if (config->ap_scan != DEFAULT_AP_SCAN) 813 fprintf(f, "ap_scan=%d\n", config->ap_scan); 814 if (config->disable_scan_offload) 815 fprintf(f, "disable_scan_offload=%d\n", 816 config->disable_scan_offload); 817 if (config->fast_reauth != DEFAULT_FAST_REAUTH) 818 fprintf(f, "fast_reauth=%d\n", config->fast_reauth); 819 if (config->opensc_engine_path) 820 fprintf(f, "opensc_engine_path=%s\n", 821 config->opensc_engine_path); 822 if (config->pkcs11_engine_path) 823 fprintf(f, "pkcs11_engine_path=%s\n", 824 config->pkcs11_engine_path); 825 if (config->pkcs11_module_path) 826 fprintf(f, "pkcs11_module_path=%s\n", 827 config->pkcs11_module_path); 828 if (config->pcsc_reader) 829 fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader); 830 if (config->pcsc_pin) 831 fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin); 832 if (config->driver_param) 833 fprintf(f, "driver_param=%s\n", config->driver_param); 834 if (config->dot11RSNAConfigPMKLifetime) 835 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n", 836 config->dot11RSNAConfigPMKLifetime); 837 if (config->dot11RSNAConfigPMKReauthThreshold) 838 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n", 839 config->dot11RSNAConfigPMKReauthThreshold); 840 if (config->dot11RSNAConfigSATimeout) 841 fprintf(f, "dot11RSNAConfigSATimeout=%d\n", 842 config->dot11RSNAConfigSATimeout); 843 if (config->update_config) 844 fprintf(f, "update_config=%d\n", config->update_config); 845#ifdef CONFIG_WPS 846 if (!is_nil_uuid(config->uuid)) { 847 char buf[40]; 848 uuid_bin2str(config->uuid, buf, sizeof(buf)); 849 fprintf(f, "uuid=%s\n", buf); 850 } 851 if (config->device_name) 852 fprintf(f, "device_name=%s\n", config->device_name); 853 if (config->manufacturer) 854 fprintf(f, "manufacturer=%s\n", config->manufacturer); 855 if (config->model_name) 856 fprintf(f, "model_name=%s\n", config->model_name); 857 if (config->model_number) 858 fprintf(f, "model_number=%s\n", config->model_number); 859 if (config->serial_number) 860 fprintf(f, "serial_number=%s\n", config->serial_number); 861 { 862 char _buf[WPS_DEV_TYPE_BUFSIZE], *buf; 863 buf = wps_dev_type_bin2str(config->device_type, 864 _buf, sizeof(_buf)); 865 if (os_strcmp(buf, "0-00000000-0") != 0) 866 fprintf(f, "device_type=%s\n", buf); 867 } 868 if (WPA_GET_BE32(config->os_version)) 869 fprintf(f, "os_version=%08x\n", 870 WPA_GET_BE32(config->os_version)); 871 if (config->config_methods) 872 fprintf(f, "config_methods=%s\n", config->config_methods); 873 if (config->wps_cred_processing) 874 fprintf(f, "wps_cred_processing=%d\n", 875 config->wps_cred_processing); 876 if (config->wps_vendor_ext_m1) { 877 int i, len = wpabuf_len(config->wps_vendor_ext_m1); 878 const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1); 879 if (len > 0) { 880 fprintf(f, "wps_vendor_ext_m1="); 881 for (i = 0; i < len; i++) 882 fprintf(f, "%02x", *p++); 883 fprintf(f, "\n"); 884 } 885 } 886#endif /* CONFIG_WPS */ 887#ifdef CONFIG_P2P 888 if (config->p2p_listen_reg_class) 889 fprintf(f, "p2p_listen_reg_class=%u\n", 890 config->p2p_listen_reg_class); 891 if (config->p2p_listen_channel) 892 fprintf(f, "p2p_listen_channel=%u\n", 893 config->p2p_listen_channel); 894 if (config->p2p_oper_reg_class) 895 fprintf(f, "p2p_oper_reg_class=%u\n", 896 config->p2p_oper_reg_class); 897 if (config->p2p_oper_channel) 898 fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel); 899 if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT) 900 fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent); 901 if (config->p2p_ssid_postfix) 902 fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix); 903 if (config->persistent_reconnect) 904 fprintf(f, "persistent_reconnect=%u\n", 905 config->persistent_reconnect); 906 if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS) 907 fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss); 908 if (config->p2p_group_idle) 909 fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle); 910 if (config->p2p_pref_chan) { 911 unsigned int i; 912 fprintf(f, "p2p_pref_chan="); 913 for (i = 0; i < config->num_p2p_pref_chan; i++) { 914 fprintf(f, "%s%u:%u", i > 0 ? "," : "", 915 config->p2p_pref_chan[i].op_class, 916 config->p2p_pref_chan[i].chan); 917 } 918 fprintf(f, "\n"); 919 } 920 if (config->p2p_go_ht40) 921 fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40); 922 if (config->p2p_disabled) 923 fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled); 924 if (config->p2p_no_group_iface) 925 fprintf(f, "p2p_no_group_iface=%u\n", 926 config->p2p_no_group_iface); 927 if (config->p2p_ignore_shared_freq) 928 fprintf(f, "p2p_ignore_shared_freq=%u\n", 929 config->p2p_ignore_shared_freq); 930#endif /* CONFIG_P2P */ 931 if (config->country[0] && config->country[1]) { 932 fprintf(f, "country=%c%c\n", 933 config->country[0], config->country[1]); 934 } 935 if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT) 936 fprintf(f, "bss_max_count=%u\n", config->bss_max_count); 937 if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE) 938 fprintf(f, "bss_expiration_age=%u\n", 939 config->bss_expiration_age); 940 if (config->bss_expiration_scan_count != 941 DEFAULT_BSS_EXPIRATION_SCAN_COUNT) 942 fprintf(f, "bss_expiration_scan_count=%u\n", 943 config->bss_expiration_scan_count); 944 if (config->filter_ssids) 945 fprintf(f, "filter_ssids=%d\n", config->filter_ssids); 946 if (config->max_num_sta != DEFAULT_MAX_NUM_STA) 947 fprintf(f, "max_num_sta=%u\n", config->max_num_sta); 948 if (config->disassoc_low_ack) 949 fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack); 950#ifdef CONFIG_HS20 951 if (config->hs20) 952 fprintf(f, "hs20=1\n"); 953#endif /* CONFIG_HS20 */ 954#ifdef CONFIG_INTERWORKING 955 if (config->interworking) 956 fprintf(f, "interworking=%u\n", config->interworking); 957 if (!is_zero_ether_addr(config->hessid)) 958 fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid)); 959 if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE) 960 fprintf(f, "access_network_type=%d\n", 961 config->access_network_type); 962#endif /* CONFIG_INTERWORKING */ 963 if (config->pbc_in_m1) 964 fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1); 965 if (config->wps_nfc_pw_from_config) { 966 if (config->wps_nfc_dev_pw_id) 967 fprintf(f, "wps_nfc_dev_pw_id=%d\n", 968 config->wps_nfc_dev_pw_id); 969 write_global_bin(f, "wps_nfc_dh_pubkey", 970 config->wps_nfc_dh_pubkey); 971 write_global_bin(f, "wps_nfc_dh_privkey", 972 config->wps_nfc_dh_privkey); 973 write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw); 974 } 975 976 if (config->ext_password_backend) 977 fprintf(f, "ext_password_backend=%s\n", 978 config->ext_password_backend); 979 if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY) 980 fprintf(f, "p2p_go_max_inactivity=%d\n", 981 config->p2p_go_max_inactivity); 982 if (config->auto_interworking) 983 fprintf(f, "auto_interworking=%d\n", 984 config->auto_interworking); 985 if (config->okc) 986 fprintf(f, "okc=%d\n", config->okc); 987 if (config->pmf) 988 fprintf(f, "pmf=%d\n", config->pmf); 989 if (config->dtim_period) 990 fprintf(f, "dtim_period=%d\n", config->dtim_period); 991 if (config->beacon_int) 992 fprintf(f, "beacon_int=%d\n", config->beacon_int); 993 994 if (config->sae_groups) { 995 int i; 996 fprintf(f, "sae_groups="); 997 for (i = 0; config->sae_groups[i] >= 0; i++) { 998 fprintf(f, "%s%d", i > 0 ? " " : "", 999 config->sae_groups[i]); 1000 } 1001 fprintf(f, "\n"); 1002 } 1003 1004 if (config->ap_vendor_elements) { 1005 int i, len = wpabuf_len(config->ap_vendor_elements); 1006 const u8 *p = wpabuf_head_u8(config->ap_vendor_elements); 1007 if (len > 0) { 1008 fprintf(f, "ap_vendor_elements="); 1009 for (i = 0; i < len; i++) 1010 fprintf(f, "%02x", *p++); 1011 fprintf(f, "\n"); 1012 } 1013 } 1014 1015 if (config->ignore_old_scan_res) 1016 fprintf(f, "ignore_old_scan_res=%d\n", 1017 config->ignore_old_scan_res); 1018 1019 if (config->freq_list && config->freq_list[0]) { 1020 int i; 1021 fprintf(f, "freq_list="); 1022 for (i = 0; config->freq_list[i]; i++) { 1023 fprintf(f, "%s%u", i > 0 ? " " : "", 1024 config->freq_list[i]); 1025 } 1026 fprintf(f, "\n"); 1027 } 1028 if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ) 1029 fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq); 1030 1031 if (config->sched_scan_interval) 1032 fprintf(f, "sched_scan_interval=%u\n", 1033 config->sched_scan_interval); 1034} 1035 1036#endif /* CONFIG_NO_CONFIG_WRITE */ 1037 1038 1039int wpa_config_write(const char *name, struct wpa_config *config) 1040{ 1041#ifndef CONFIG_NO_CONFIG_WRITE 1042 FILE *f; 1043 struct wpa_ssid *ssid; 1044 struct wpa_cred *cred; 1045#ifndef CONFIG_NO_CONFIG_BLOBS 1046 struct wpa_config_blob *blob; 1047#endif /* CONFIG_NO_CONFIG_BLOBS */ 1048 int ret = 0; 1049 1050 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name); 1051 1052 f = fopen(name, "w"); 1053 if (f == NULL) { 1054 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name); 1055 return -1; 1056 } 1057 1058 wpa_config_write_global(f, config); 1059 1060 for (cred = config->cred; cred; cred = cred->next) { 1061 fprintf(f, "\ncred={\n"); 1062 wpa_config_write_cred(f, cred); 1063 fprintf(f, "}\n"); 1064 } 1065 1066 for (ssid = config->ssid; ssid; ssid = ssid->next) { 1067 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary) 1068 continue; /* do not save temporary networks */ 1069 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set && 1070 !ssid->passphrase) 1071 continue; /* do not save invalid network */ 1072 fprintf(f, "\nnetwork={\n"); 1073 wpa_config_write_network(f, ssid); 1074 fprintf(f, "}\n"); 1075 } 1076 1077#ifndef CONFIG_NO_CONFIG_BLOBS 1078 for (blob = config->blobs; blob; blob = blob->next) { 1079 ret = wpa_config_write_blob(f, blob); 1080 if (ret) 1081 break; 1082 } 1083#endif /* CONFIG_NO_CONFIG_BLOBS */ 1084 1085 fclose(f); 1086 1087 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully", 1088 name, ret ? "un" : ""); 1089 return ret; 1090#else /* CONFIG_NO_CONFIG_WRITE */ 1091 return -1; 1092#endif /* CONFIG_NO_CONFIG_WRITE */ 1093} 1094