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