Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * WPA Supplicant / Configuration backend: text file |
| 3 | * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Alternatively, this software may be distributed under the terms of BSD |
| 10 | * license. |
| 11 | * |
| 12 | * See README and COPYING for more details. |
| 13 | * |
| 14 | * This file implements a configuration backend for text files. All the |
| 15 | * configuration information is stored in a text file that uses a format |
| 16 | * described in the sample configuration file, wpa_supplicant.conf. |
| 17 | */ |
| 18 | |
| 19 | #include "includes.h" |
| 20 | |
| 21 | #include "common.h" |
| 22 | #include "config.h" |
| 23 | #include "base64.h" |
| 24 | #include "uuid.h" |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * wpa_config_get_line - Read the next configuration file line |
| 29 | * @s: Buffer for the line |
| 30 | * @size: The buffer length |
| 31 | * @stream: File stream to read from |
| 32 | * @line: Pointer to a variable storing the file line number |
| 33 | * @_pos: Buffer for the pointer to the beginning of data on the text line or |
| 34 | * %NULL if not needed (returned value used instead) |
| 35 | * Returns: Pointer to the beginning of data on the text line or %NULL if no |
| 36 | * more text lines are available. |
| 37 | * |
| 38 | * This function reads the next non-empty line from the configuration file and |
| 39 | * removes comments. The returned string is guaranteed to be null-terminated. |
| 40 | */ |
| 41 | static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line, |
| 42 | char **_pos) |
| 43 | { |
| 44 | char *pos, *end, *sstart; |
| 45 | |
| 46 | while (fgets(s, size, stream)) { |
| 47 | (*line)++; |
| 48 | s[size - 1] = '\0'; |
| 49 | pos = s; |
| 50 | |
| 51 | /* Skip white space from the beginning of line. */ |
| 52 | while (*pos == ' ' || *pos == '\t' || *pos == '\r') |
| 53 | pos++; |
| 54 | |
| 55 | /* Skip comment lines and empty lines */ |
| 56 | if (*pos == '#' || *pos == '\n' || *pos == '\0') |
| 57 | continue; |
| 58 | |
| 59 | /* |
| 60 | * Remove # comments unless they are within a double quoted |
| 61 | * string. |
| 62 | */ |
| 63 | sstart = os_strchr(pos, '"'); |
| 64 | if (sstart) |
| 65 | sstart = os_strrchr(sstart + 1, '"'); |
| 66 | if (!sstart) |
| 67 | sstart = pos; |
| 68 | end = os_strchr(sstart, '#'); |
| 69 | if (end) |
| 70 | *end-- = '\0'; |
| 71 | else |
| 72 | end = pos + os_strlen(pos) - 1; |
| 73 | |
| 74 | /* Remove trailing white space. */ |
| 75 | while (end > pos && |
| 76 | (*end == '\n' || *end == ' ' || *end == '\t' || |
| 77 | *end == '\r')) |
| 78 | *end-- = '\0'; |
| 79 | |
| 80 | if (*pos == '\0') |
| 81 | continue; |
| 82 | |
| 83 | if (_pos) |
| 84 | *_pos = pos; |
| 85 | return pos; |
| 86 | } |
| 87 | |
| 88 | if (_pos) |
| 89 | *_pos = NULL; |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | static int wpa_config_validate_network(struct wpa_ssid *ssid, int line) |
| 95 | { |
| 96 | int errors = 0; |
| 97 | |
| 98 | if (ssid->passphrase) { |
| 99 | if (ssid->psk_set) { |
| 100 | wpa_printf(MSG_ERROR, "Line %d: both PSK and " |
| 101 | "passphrase configured.", line); |
| 102 | errors++; |
| 103 | } |
| 104 | wpa_config_update_psk(ssid); |
| 105 | } |
| 106 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 107 | if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 108 | wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key " |
| 109 | "management, but no PSK configured.", line); |
| 110 | errors++; |
| 111 | } |
| 112 | |
| 113 | if ((ssid->group_cipher & WPA_CIPHER_CCMP) && |
| 114 | !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) && |
| 115 | !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) { |
| 116 | /* Group cipher cannot be stronger than the pairwise cipher. */ |
| 117 | wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher" |
| 118 | " list since it was not allowed for pairwise " |
| 119 | "cipher", line); |
| 120 | ssid->group_cipher &= ~WPA_CIPHER_CCMP; |
| 121 | } |
| 122 | |
| 123 | return errors; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id) |
| 128 | { |
| 129 | struct wpa_ssid *ssid; |
| 130 | int errors = 0, end = 0; |
| 131 | char buf[256], *pos, *pos2; |
| 132 | |
| 133 | wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block", |
| 134 | *line); |
| 135 | ssid = os_zalloc(sizeof(*ssid)); |
| 136 | if (ssid == NULL) |
| 137 | return NULL; |
| 138 | ssid->id = id; |
| 139 | |
| 140 | wpa_config_set_network_defaults(ssid); |
| 141 | |
| 142 | while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { |
| 143 | if (os_strcmp(pos, "}") == 0) { |
| 144 | end = 1; |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | pos2 = os_strchr(pos, '='); |
| 149 | if (pos2 == NULL) { |
| 150 | wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line " |
| 151 | "'%s'.", *line, pos); |
| 152 | errors++; |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | *pos2++ = '\0'; |
| 157 | if (*pos2 == '"') { |
| 158 | if (os_strchr(pos2 + 1, '"') == NULL) { |
| 159 | wpa_printf(MSG_ERROR, "Line %d: invalid " |
| 160 | "quotation '%s'.", *line, pos2); |
| 161 | errors++; |
| 162 | continue; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (wpa_config_set(ssid, pos, pos2, *line) < 0) |
| 167 | errors++; |
| 168 | } |
| 169 | |
| 170 | if (!end) { |
| 171 | wpa_printf(MSG_ERROR, "Line %d: network block was not " |
| 172 | "terminated properly.", *line); |
| 173 | errors++; |
| 174 | } |
| 175 | |
| 176 | errors += wpa_config_validate_network(ssid, *line); |
| 177 | |
| 178 | if (errors) { |
| 179 | wpa_config_free_ssid(ssid); |
| 180 | ssid = NULL; |
| 181 | } |
| 182 | |
| 183 | return ssid; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | #ifndef CONFIG_NO_CONFIG_BLOBS |
| 188 | static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line, |
| 189 | const char *name) |
| 190 | { |
| 191 | struct wpa_config_blob *blob; |
| 192 | char buf[256], *pos; |
| 193 | unsigned char *encoded = NULL, *nencoded; |
| 194 | int end = 0; |
| 195 | size_t encoded_len = 0, len; |
| 196 | |
| 197 | wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'", |
| 198 | *line, name); |
| 199 | |
| 200 | while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { |
| 201 | if (os_strcmp(pos, "}") == 0) { |
| 202 | end = 1; |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | len = os_strlen(pos); |
| 207 | nencoded = os_realloc(encoded, encoded_len + len); |
| 208 | if (nencoded == NULL) { |
| 209 | wpa_printf(MSG_ERROR, "Line %d: not enough memory for " |
| 210 | "blob", *line); |
| 211 | os_free(encoded); |
| 212 | return NULL; |
| 213 | } |
| 214 | encoded = nencoded; |
| 215 | os_memcpy(encoded + encoded_len, pos, len); |
| 216 | encoded_len += len; |
| 217 | } |
| 218 | |
| 219 | if (!end) { |
| 220 | wpa_printf(MSG_ERROR, "Line %d: blob was not terminated " |
| 221 | "properly", *line); |
| 222 | os_free(encoded); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | blob = os_zalloc(sizeof(*blob)); |
| 227 | if (blob == NULL) { |
| 228 | os_free(encoded); |
| 229 | return NULL; |
| 230 | } |
| 231 | blob->name = os_strdup(name); |
| 232 | blob->data = base64_decode(encoded, encoded_len, &blob->len); |
| 233 | os_free(encoded); |
| 234 | |
| 235 | if (blob->name == NULL || blob->data == NULL) { |
| 236 | wpa_config_free_blob(blob); |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | return blob; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | static int wpa_config_process_blob(struct wpa_config *config, FILE *f, |
| 245 | int *line, char *bname) |
| 246 | { |
| 247 | char *name_end; |
| 248 | struct wpa_config_blob *blob; |
| 249 | |
| 250 | name_end = os_strchr(bname, '='); |
| 251 | if (name_end == NULL) { |
| 252 | wpa_printf(MSG_ERROR, "Line %d: no blob name terminator", |
| 253 | *line); |
| 254 | return -1; |
| 255 | } |
| 256 | *name_end = '\0'; |
| 257 | |
| 258 | blob = wpa_config_read_blob(f, line, bname); |
| 259 | if (blob == NULL) { |
| 260 | wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s", |
| 261 | *line, bname); |
| 262 | return -1; |
| 263 | } |
| 264 | wpa_config_set_blob(config, blob); |
| 265 | return 0; |
| 266 | } |
| 267 | #endif /* CONFIG_NO_CONFIG_BLOBS */ |
| 268 | |
| 269 | |
| 270 | struct wpa_config * wpa_config_read(const char *name) |
| 271 | { |
| 272 | FILE *f; |
| 273 | char buf[256], *pos; |
| 274 | int errors = 0, line = 0; |
| 275 | struct wpa_ssid *ssid, *tail = NULL, *head = NULL; |
| 276 | struct wpa_config *config; |
| 277 | int id = 0; |
| 278 | |
| 279 | config = wpa_config_alloc_empty(NULL, NULL); |
| 280 | if (config == NULL) |
| 281 | return NULL; |
| 282 | wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name); |
| 283 | f = fopen(name, "r"); |
| 284 | if (f == NULL) { |
| 285 | os_free(config); |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) { |
| 290 | if (os_strcmp(pos, "network={") == 0) { |
| 291 | ssid = wpa_config_read_network(f, &line, id++); |
| 292 | if (ssid == NULL) { |
| 293 | wpa_printf(MSG_ERROR, "Line %d: failed to " |
| 294 | "parse network block.", line); |
| 295 | errors++; |
| 296 | continue; |
| 297 | } |
| 298 | if (head == NULL) { |
| 299 | head = tail = ssid; |
| 300 | } else { |
| 301 | tail->next = ssid; |
| 302 | tail = ssid; |
| 303 | } |
| 304 | if (wpa_config_add_prio_network(config, ssid)) { |
| 305 | wpa_printf(MSG_ERROR, "Line %d: failed to add " |
| 306 | "network block to priority list.", |
| 307 | line); |
| 308 | errors++; |
| 309 | continue; |
| 310 | } |
| 311 | #ifndef CONFIG_NO_CONFIG_BLOBS |
| 312 | } else if (os_strncmp(pos, "blob-base64-", 12) == 0) { |
| 313 | if (wpa_config_process_blob(config, f, &line, pos + 12) |
| 314 | < 0) { |
| 315 | errors++; |
| 316 | continue; |
| 317 | } |
| 318 | #endif /* CONFIG_NO_CONFIG_BLOBS */ |
| 319 | } else if (wpa_config_process_global(config, pos, line) < 0) { |
| 320 | wpa_printf(MSG_ERROR, "Line %d: Invalid configuration " |
| 321 | "line '%s'.", line, pos); |
| 322 | errors++; |
| 323 | continue; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | fclose(f); |
| 328 | |
| 329 | config->ssid = head; |
| 330 | wpa_config_debug_dump_networks(config); |
| 331 | |
| 332 | #ifndef WPA_IGNORE_CONFIG_ERRORS |
| 333 | if (errors) { |
| 334 | wpa_config_free(config); |
| 335 | config = NULL; |
| 336 | head = NULL; |
| 337 | } |
| 338 | #endif /* WPA_IGNORE_CONFIG_ERRORS */ |
| 339 | |
| 340 | return config; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 345 | |
| 346 | static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid) |
| 347 | { |
| 348 | char *value = wpa_config_get(ssid, field); |
| 349 | if (value == NULL) |
| 350 | return; |
| 351 | fprintf(f, "\t%s=%s\n", field, value); |
| 352 | os_free(value); |
| 353 | } |
| 354 | |
| 355 | |
| 356 | static void write_int(FILE *f, const char *field, int value, int def) |
| 357 | { |
| 358 | if (value == def) |
| 359 | return; |
| 360 | fprintf(f, "\t%s=%d\n", field, value); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | static void write_bssid(FILE *f, struct wpa_ssid *ssid) |
| 365 | { |
| 366 | char *value = wpa_config_get(ssid, "bssid"); |
| 367 | if (value == NULL) |
| 368 | return; |
| 369 | fprintf(f, "\tbssid=%s\n", value); |
| 370 | os_free(value); |
| 371 | } |
| 372 | |
| 373 | |
| 374 | static void write_psk(FILE *f, struct wpa_ssid *ssid) |
| 375 | { |
| 376 | char *value = wpa_config_get(ssid, "psk"); |
| 377 | if (value == NULL) |
| 378 | return; |
| 379 | fprintf(f, "\tpsk=%s\n", value); |
| 380 | os_free(value); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | static void write_proto(FILE *f, struct wpa_ssid *ssid) |
| 385 | { |
| 386 | char *value; |
| 387 | |
| 388 | if (ssid->proto == DEFAULT_PROTO) |
| 389 | return; |
| 390 | |
| 391 | value = wpa_config_get(ssid, "proto"); |
| 392 | if (value == NULL) |
| 393 | return; |
| 394 | if (value[0]) |
| 395 | fprintf(f, "\tproto=%s\n", value); |
| 396 | os_free(value); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid) |
| 401 | { |
| 402 | char *value; |
| 403 | |
| 404 | if (ssid->key_mgmt == DEFAULT_KEY_MGMT) |
| 405 | return; |
| 406 | |
| 407 | value = wpa_config_get(ssid, "key_mgmt"); |
| 408 | if (value == NULL) |
| 409 | return; |
| 410 | if (value[0]) |
| 411 | fprintf(f, "\tkey_mgmt=%s\n", value); |
| 412 | os_free(value); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | static void write_pairwise(FILE *f, struct wpa_ssid *ssid) |
| 417 | { |
| 418 | char *value; |
| 419 | |
| 420 | if (ssid->pairwise_cipher == DEFAULT_PAIRWISE) |
| 421 | return; |
| 422 | |
| 423 | value = wpa_config_get(ssid, "pairwise"); |
| 424 | if (value == NULL) |
| 425 | return; |
| 426 | if (value[0]) |
| 427 | fprintf(f, "\tpairwise=%s\n", value); |
| 428 | os_free(value); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | static void write_group(FILE *f, struct wpa_ssid *ssid) |
| 433 | { |
| 434 | char *value; |
| 435 | |
| 436 | if (ssid->group_cipher == DEFAULT_GROUP) |
| 437 | return; |
| 438 | |
| 439 | value = wpa_config_get(ssid, "group"); |
| 440 | if (value == NULL) |
| 441 | return; |
| 442 | if (value[0]) |
| 443 | fprintf(f, "\tgroup=%s\n", value); |
| 444 | os_free(value); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | static void write_auth_alg(FILE *f, struct wpa_ssid *ssid) |
| 449 | { |
| 450 | char *value; |
| 451 | |
| 452 | if (ssid->auth_alg == 0) |
| 453 | return; |
| 454 | |
| 455 | value = wpa_config_get(ssid, "auth_alg"); |
| 456 | if (value == NULL) |
| 457 | return; |
| 458 | if (value[0]) |
| 459 | fprintf(f, "\tauth_alg=%s\n", value); |
| 460 | os_free(value); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | #ifdef IEEE8021X_EAPOL |
| 465 | static void write_eap(FILE *f, struct wpa_ssid *ssid) |
| 466 | { |
| 467 | char *value; |
| 468 | |
| 469 | value = wpa_config_get(ssid, "eap"); |
| 470 | if (value == NULL) |
| 471 | return; |
| 472 | |
| 473 | if (value[0]) |
| 474 | fprintf(f, "\teap=%s\n", value); |
| 475 | os_free(value); |
| 476 | } |
| 477 | #endif /* IEEE8021X_EAPOL */ |
| 478 | |
| 479 | |
| 480 | static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid) |
| 481 | { |
| 482 | char field[20], *value; |
| 483 | int res; |
| 484 | |
| 485 | res = os_snprintf(field, sizeof(field), "wep_key%d", idx); |
| 486 | if (res < 0 || (size_t) res >= sizeof(field)) |
| 487 | return; |
| 488 | value = wpa_config_get(ssid, field); |
| 489 | if (value) { |
| 490 | fprintf(f, "\t%s=%s\n", field, value); |
| 491 | os_free(value); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 496 | #ifdef CONFIG_P2P |
| 497 | static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid) |
| 498 | { |
| 499 | char *value = wpa_config_get(ssid, "p2p_client_list"); |
| 500 | if (value == NULL) |
| 501 | return; |
| 502 | fprintf(f, "\tp2p_client_list=%s\n", value); |
| 503 | os_free(value); |
| 504 | } |
| 505 | #endif /* CONFIG_P2P */ |
| 506 | |
| 507 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 508 | static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid) |
| 509 | { |
| 510 | int i; |
| 511 | |
| 512 | #define STR(t) write_str(f, #t, ssid) |
| 513 | #define INT(t) write_int(f, #t, ssid->t, 0) |
| 514 | #define INTe(t) write_int(f, #t, ssid->eap.t, 0) |
| 515 | #define INT_DEF(t, def) write_int(f, #t, ssid->t, def) |
| 516 | #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def) |
| 517 | |
| 518 | STR(ssid); |
| 519 | INT(scan_ssid); |
| 520 | write_bssid(f, ssid); |
| 521 | write_psk(f, ssid); |
| 522 | write_proto(f, ssid); |
| 523 | write_key_mgmt(f, ssid); |
| 524 | write_pairwise(f, ssid); |
| 525 | write_group(f, ssid); |
| 526 | write_auth_alg(f, ssid); |
| 527 | #ifdef IEEE8021X_EAPOL |
| 528 | write_eap(f, ssid); |
| 529 | STR(identity); |
| 530 | STR(anonymous_identity); |
| 531 | STR(password); |
| 532 | STR(ca_cert); |
| 533 | STR(ca_path); |
| 534 | STR(client_cert); |
| 535 | STR(private_key); |
| 536 | STR(private_key_passwd); |
| 537 | STR(dh_file); |
| 538 | STR(subject_match); |
| 539 | STR(altsubject_match); |
| 540 | STR(ca_cert2); |
| 541 | STR(ca_path2); |
| 542 | STR(client_cert2); |
| 543 | STR(private_key2); |
| 544 | STR(private_key2_passwd); |
| 545 | STR(dh_file2); |
| 546 | STR(subject_match2); |
| 547 | STR(altsubject_match2); |
| 548 | STR(phase1); |
| 549 | STR(phase2); |
| 550 | STR(pcsc); |
| 551 | STR(pin); |
| 552 | STR(engine_id); |
| 553 | STR(key_id); |
| 554 | STR(cert_id); |
| 555 | STR(ca_cert_id); |
| 556 | STR(key2_id); |
| 557 | STR(pin2); |
| 558 | STR(engine2_id); |
| 559 | STR(cert2_id); |
| 560 | STR(ca_cert2_id); |
| 561 | INTe(engine); |
| 562 | INTe(engine2); |
| 563 | INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS); |
| 564 | #endif /* IEEE8021X_EAPOL */ |
| 565 | for (i = 0; i < 4; i++) |
| 566 | write_wep_key(f, i, ssid); |
| 567 | INT(wep_tx_keyidx); |
| 568 | INT(priority); |
| 569 | #ifdef IEEE8021X_EAPOL |
| 570 | INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND); |
| 571 | STR(pac_file); |
| 572 | INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE); |
| 573 | #endif /* IEEE8021X_EAPOL */ |
| 574 | INT(mode); |
| 575 | INT(proactive_key_caching); |
| 576 | INT(disabled); |
| 577 | INT(peerkey); |
| 578 | #ifdef CONFIG_IEEE80211W |
| 579 | INT(ieee80211w); |
| 580 | #endif /* CONFIG_IEEE80211W */ |
| 581 | STR(id_str); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 582 | #ifdef CONFIG_P2P |
| 583 | write_p2p_client_list(f, ssid); |
| 584 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 585 | |
| 586 | #undef STR |
| 587 | #undef INT |
| 588 | #undef INT_DEF |
| 589 | } |
| 590 | |
| 591 | |
| 592 | #ifndef CONFIG_NO_CONFIG_BLOBS |
| 593 | static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob) |
| 594 | { |
| 595 | unsigned char *encoded; |
| 596 | |
| 597 | encoded = base64_encode(blob->data, blob->len, NULL); |
| 598 | if (encoded == NULL) |
| 599 | return -1; |
| 600 | |
| 601 | fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded); |
| 602 | os_free(encoded); |
| 603 | return 0; |
| 604 | } |
| 605 | #endif /* CONFIG_NO_CONFIG_BLOBS */ |
| 606 | |
| 607 | |
| 608 | static void wpa_config_write_global(FILE *f, struct wpa_config *config) |
| 609 | { |
| 610 | #ifdef CONFIG_CTRL_IFACE |
| 611 | if (config->ctrl_interface) |
| 612 | fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface); |
| 613 | if (config->ctrl_interface_group) |
| 614 | fprintf(f, "ctrl_interface_group=%s\n", |
| 615 | config->ctrl_interface_group); |
| 616 | #endif /* CONFIG_CTRL_IFACE */ |
| 617 | if (config->eapol_version != DEFAULT_EAPOL_VERSION) |
| 618 | fprintf(f, "eapol_version=%d\n", config->eapol_version); |
| 619 | if (config->ap_scan != DEFAULT_AP_SCAN) |
| 620 | fprintf(f, "ap_scan=%d\n", config->ap_scan); |
| 621 | if (config->fast_reauth != DEFAULT_FAST_REAUTH) |
| 622 | fprintf(f, "fast_reauth=%d\n", config->fast_reauth); |
| 623 | if (config->opensc_engine_path) |
| 624 | fprintf(f, "opensc_engine_path=%s\n", |
| 625 | config->opensc_engine_path); |
| 626 | if (config->pkcs11_engine_path) |
| 627 | fprintf(f, "pkcs11_engine_path=%s\n", |
| 628 | config->pkcs11_engine_path); |
| 629 | if (config->pkcs11_module_path) |
| 630 | fprintf(f, "pkcs11_module_path=%s\n", |
| 631 | config->pkcs11_module_path); |
| 632 | if (config->driver_param) |
| 633 | fprintf(f, "driver_param=%s\n", config->driver_param); |
| 634 | if (config->dot11RSNAConfigPMKLifetime) |
| 635 | fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n", |
| 636 | config->dot11RSNAConfigPMKLifetime); |
| 637 | if (config->dot11RSNAConfigPMKReauthThreshold) |
| 638 | fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n", |
| 639 | config->dot11RSNAConfigPMKReauthThreshold); |
| 640 | if (config->dot11RSNAConfigSATimeout) |
| 641 | fprintf(f, "dot11RSNAConfigSATimeout=%d\n", |
| 642 | config->dot11RSNAConfigSATimeout); |
| 643 | if (config->update_config) |
| 644 | fprintf(f, "update_config=%d\n", config->update_config); |
| 645 | #ifdef CONFIG_WPS |
| 646 | if (!is_nil_uuid(config->uuid)) { |
| 647 | char buf[40]; |
| 648 | uuid_bin2str(config->uuid, buf, sizeof(buf)); |
| 649 | fprintf(f, "uuid=%s\n", buf); |
| 650 | } |
| 651 | if (config->device_name) |
| 652 | fprintf(f, "device_name=%s\n", config->device_name); |
| 653 | if (config->manufacturer) |
| 654 | fprintf(f, "manufacturer=%s\n", config->manufacturer); |
| 655 | if (config->model_name) |
| 656 | fprintf(f, "model_name=%s\n", config->model_name); |
| 657 | if (config->model_number) |
| 658 | fprintf(f, "model_number=%s\n", config->model_number); |
| 659 | if (config->serial_number) |
| 660 | fprintf(f, "serial_number=%s\n", config->serial_number); |
| 661 | { |
| 662 | char _buf[WPS_DEV_TYPE_BUFSIZE], *buf; |
| 663 | buf = wps_dev_type_bin2str(config->device_type, |
| 664 | _buf, sizeof(_buf)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 665 | if (os_strcmp(buf, "0-00000000-0") != 0) |
| 666 | fprintf(f, "device_type=%s\n", buf); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 667 | } |
| 668 | if (WPA_GET_BE32(config->os_version)) |
| 669 | fprintf(f, "os_version=%08x\n", |
| 670 | WPA_GET_BE32(config->os_version)); |
| 671 | if (config->config_methods) |
| 672 | fprintf(f, "config_methods=%s\n", config->config_methods); |
| 673 | if (config->wps_cred_processing) |
| 674 | fprintf(f, "wps_cred_processing=%d\n", |
| 675 | config->wps_cred_processing); |
| 676 | #endif /* CONFIG_WPS */ |
| 677 | #ifdef CONFIG_P2P |
| 678 | if (config->p2p_listen_reg_class) |
| 679 | fprintf(f, "p2p_listen_reg_class=%u\n", |
| 680 | config->p2p_listen_reg_class); |
| 681 | if (config->p2p_listen_channel) |
| 682 | fprintf(f, "p2p_listen_channel=%u\n", |
| 683 | config->p2p_listen_channel); |
| 684 | if (config->p2p_oper_reg_class) |
| 685 | fprintf(f, "p2p_oper_reg_class=%u\n", |
| 686 | config->p2p_oper_reg_class); |
| 687 | if (config->p2p_oper_channel) |
| 688 | fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel); |
| 689 | if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT) |
| 690 | fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent); |
| 691 | if (config->p2p_ssid_postfix) |
| 692 | fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix); |
| 693 | if (config->persistent_reconnect) |
| 694 | fprintf(f, "persistent_reconnect=%u\n", |
| 695 | config->persistent_reconnect); |
| 696 | if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS) |
| 697 | fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss); |
| 698 | if (config->p2p_group_idle) |
| 699 | fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle); |
| 700 | #endif /* CONFIG_P2P */ |
| 701 | if (config->country[0] && config->country[1]) { |
| 702 | fprintf(f, "country=%c%c\n", |
| 703 | config->country[0], config->country[1]); |
| 704 | } |
| 705 | if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT) |
| 706 | fprintf(f, "bss_max_count=%u\n", config->bss_max_count); |
| 707 | if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE) |
| 708 | fprintf(f, "bss_expiration_age=%u\n", |
| 709 | config->bss_expiration_age); |
| 710 | if (config->bss_expiration_scan_count != |
| 711 | DEFAULT_BSS_EXPIRATION_SCAN_COUNT) |
| 712 | fprintf(f, "bss_expiration_scan_count=%u\n", |
| 713 | config->bss_expiration_scan_count); |
| 714 | if (config->filter_ssids) |
| 715 | fprintf(f, "filter_ssids=%d\n", config->filter_ssids); |
| 716 | if (config->max_num_sta != DEFAULT_MAX_NUM_STA) |
| 717 | fprintf(f, "max_num_sta=%u\n", config->max_num_sta); |
| 718 | if (config->disassoc_low_ack) |
| 719 | fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 720 | #ifdef CONFIG_INTERWORKING |
| 721 | if (config->home_realm) |
| 722 | fprintf(f, "home_realm=%s\n", config->home_realm); |
| 723 | if (config->home_username) |
| 724 | fprintf(f, "home_username=%s\n", config->home_username); |
| 725 | if (config->home_password) |
| 726 | fprintf(f, "home_password=%s\n", config->home_password); |
| 727 | if (config->home_ca_cert) |
| 728 | fprintf(f, "home_ca_cert=%s\n", config->home_ca_cert); |
| 729 | if (config->home_imsi) |
| 730 | fprintf(f, "home_imsi=%s\n", config->home_imsi); |
| 731 | if (config->home_milenage) |
| 732 | fprintf(f, "home_milenage=%s\n", config->home_milenage); |
| 733 | if (config->interworking) |
| 734 | fprintf(f, "interworking=%u\n", config->interworking); |
| 735 | if (!is_zero_ether_addr(config->hessid)) |
| 736 | fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid)); |
| 737 | if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE) |
| 738 | fprintf(f, "access_network_type=%d\n", |
| 739 | config->access_network_type); |
| 740 | #endif /* CONFIG_INTERWORKING */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 744 | |
| 745 | |
| 746 | int wpa_config_write(const char *name, struct wpa_config *config) |
| 747 | { |
| 748 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 749 | FILE *f; |
| 750 | struct wpa_ssid *ssid; |
| 751 | #ifndef CONFIG_NO_CONFIG_BLOBS |
| 752 | struct wpa_config_blob *blob; |
| 753 | #endif /* CONFIG_NO_CONFIG_BLOBS */ |
| 754 | int ret = 0; |
| 755 | |
| 756 | wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name); |
| 757 | |
| 758 | f = fopen(name, "w"); |
| 759 | if (f == NULL) { |
| 760 | wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name); |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | wpa_config_write_global(f, config); |
| 765 | |
| 766 | for (ssid = config->ssid; ssid; ssid = ssid->next) { |
| 767 | if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary) |
| 768 | continue; /* do not save temporary networks */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 769 | if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set && |
| 770 | !ssid->passphrase) |
| 771 | continue; /* do not save invalid network */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 772 | fprintf(f, "\nnetwork={\n"); |
| 773 | wpa_config_write_network(f, ssid); |
| 774 | fprintf(f, "}\n"); |
| 775 | } |
| 776 | |
| 777 | #ifndef CONFIG_NO_CONFIG_BLOBS |
| 778 | for (blob = config->blobs; blob; blob = blob->next) { |
| 779 | ret = wpa_config_write_blob(f, blob); |
| 780 | if (ret) |
| 781 | break; |
| 782 | } |
| 783 | #endif /* CONFIG_NO_CONFIG_BLOBS */ |
| 784 | |
| 785 | fclose(f); |
| 786 | |
| 787 | wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully", |
| 788 | name, ret ? "un" : ""); |
| 789 | return ret; |
| 790 | #else /* CONFIG_NO_CONFIG_WRITE */ |
| 791 | return -1; |
| 792 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 793 | } |