76a1935b84a3806a612d749e886ef787a5b4b970
[libeap.git] / 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 struct global_parse_data {
274         char *name;
275         int (*parser)(const struct global_parse_data *data,
276                       struct wpa_config *config, int line, const char *value);
277         void *param1;
278 };
279
280
281 static int wpa_config_parse_int(const struct global_parse_data *data,
282                                 struct wpa_config *config, int line,
283                                 const char *pos)
284 {
285         int *dst;
286         dst = (int *) (((u8 *) config) + (long) data->param1);
287         *dst = atoi(pos);
288         wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
289         return 0;
290 }
291
292
293 static int wpa_config_process_country(const struct global_parse_data *data,
294                                       struct wpa_config *config, int line,
295                                       const char *pos)
296 {
297         if (!pos[0] || !pos[1]) {
298                 wpa_printf(MSG_DEBUG, "Invalid country set");
299                 return -1;
300         }
301         config->country[0] = pos[0];
302         config->country[1] = pos[1];
303         wpa_printf(MSG_DEBUG, "country='%c%c'",
304                    config->country[0], config->country[1]);
305         return 0;
306 }
307
308
309 #ifdef CONFIG_CTRL_IFACE
310 static int wpa_config_process_ctrl_interface(
311         const struct global_parse_data *data, struct wpa_config *config,
312         int line, const char *pos)
313 {
314         os_free(config->ctrl_interface);
315         config->ctrl_interface = os_strdup(pos);
316         wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
317         return 0;
318 }
319
320
321 static int wpa_config_process_ctrl_interface_group(
322         const struct global_parse_data *data, struct wpa_config *config,
323         int line, const char *pos)
324 {
325         os_free(config->ctrl_interface_group);
326         config->ctrl_interface_group = os_strdup(pos);
327         wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
328                    config->ctrl_interface_group);
329         return 0;
330 }
331 #endif /* CONFIG_CTRL_IFACE */
332
333
334 static int wpa_config_process_eapol_version(
335         const struct global_parse_data *data, struct wpa_config *config,
336         int line, const char *pos)
337 {
338         config->eapol_version = atoi(pos);
339         if (config->eapol_version < 1 || config->eapol_version > 2) {
340                 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
341                            "'%s'.", line, config->eapol_version, pos);
342                 return -1;
343         }
344         wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
345         return 0;
346 }
347
348
349 #ifdef EAP_TLS_OPENSSL
350
351 static int wpa_config_process_opensc_engine_path(
352         const struct global_parse_data *data, struct wpa_config *config,
353         int line, const char *pos)
354 {
355         os_free(config->opensc_engine_path);
356         config->opensc_engine_path = os_strdup(pos);
357         wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
358                    config->opensc_engine_path);
359         return 0;
360 }
361
362
363 static int wpa_config_process_pkcs11_engine_path(
364         const struct global_parse_data *data, struct wpa_config *config,
365         int line, const char *pos)
366 {
367         os_free(config->pkcs11_engine_path);
368         config->pkcs11_engine_path = os_strdup(pos);
369         wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
370                    config->pkcs11_engine_path);
371         return 0;
372 }
373
374
375 static int wpa_config_process_pkcs11_module_path(
376         const struct global_parse_data *data, struct wpa_config *config,
377         int line, const char *pos)
378 {
379         os_free(config->pkcs11_module_path);
380         config->pkcs11_module_path = os_strdup(pos);
381         wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
382                    config->pkcs11_module_path);
383         return 0;
384 }
385
386 #endif /* EAP_TLS_OPENSSL */
387
388
389 static int wpa_config_process_driver_param(
390         const struct global_parse_data *data, struct wpa_config *config,
391         int line, const char *pos)
392 {
393         os_free(config->driver_param);
394         config->driver_param = os_strdup(pos);
395         wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
396         return 0;
397 }
398
399
400 static int wpa_config_process_load_dynamic_eap(
401         const struct global_parse_data *data, struct wpa_config *config,
402         int line, const char *so)
403 {
404         int ret;
405         wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
406         ret = eap_peer_method_load(so);
407         if (ret == -2) {
408                 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
409                            "reloading.");
410         } else if (ret) {
411                 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
412                            "method '%s'.", line, so);
413                 return -1;
414         }
415
416         return 0;
417 }
418
419
420 #ifdef CONFIG_WPS
421
422 static int wpa_config_process_uuid(const struct global_parse_data *data,
423                                    struct wpa_config *config, int line,
424                                    const char *pos)
425 {
426         char buf[40];
427         if (uuid_str2bin(pos, config->uuid)) {
428                 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
429                 return -1;
430         }
431         uuid_bin2str(config->uuid, buf, sizeof(buf));
432         wpa_printf(MSG_DEBUG, "uuid=%s", buf);
433         return 0;
434 }
435
436
437 static int wpa_config_process_device_name(const struct global_parse_data *data,
438                                           struct wpa_config *config, int line,
439                                           const char *pos)
440 {
441         if (os_strlen(pos) > 32)
442                 return -1;
443         os_free(config->device_name);
444         config->device_name = os_strdup(pos);
445         wpa_printf(MSG_DEBUG, "device_name='%s'", config->device_name);
446         return 0;
447 }
448
449
450 static int wpa_config_process_manufacturer(
451         const struct global_parse_data *data, struct wpa_config *config,
452         int line, const char *pos)
453 {
454         if (os_strlen(pos) > 64)
455                 return -1;
456         os_free(config->manufacturer);
457         config->manufacturer = os_strdup(pos);
458         wpa_printf(MSG_DEBUG, "manufacturer='%s'", config->manufacturer);
459         return 0;
460 }
461
462
463 static int wpa_config_process_model_name(const struct global_parse_data *data,
464                                          struct wpa_config *config, int line,
465                                          const char *pos)
466 {
467         if (os_strlen(pos) > 32)
468                 return -1;
469         os_free(config->model_name);
470         config->model_name = os_strdup(pos);
471         wpa_printf(MSG_DEBUG, "model_name='%s'", config->model_name);
472         return 0;
473 }
474
475
476 static int wpa_config_process_model_number(
477         const struct global_parse_data *data, struct wpa_config *config,
478         int line, const char *pos)
479 {
480         if (os_strlen(pos) > 32)
481                 return -1;
482         os_free(config->model_number);
483         config->model_number = os_strdup(pos);
484         wpa_printf(MSG_DEBUG, "model_number='%s'", config->model_number);
485         return 0;
486 }
487
488
489 static int wpa_config_process_serial_number(
490         const struct global_parse_data *data, struct wpa_config *config,
491         int line, const char *pos)
492 {
493         if (os_strlen(pos) > 32)
494                 return -1;
495         os_free(config->serial_number);
496         config->serial_number = os_strdup(pos);
497         wpa_printf(MSG_DEBUG, "serial_number='%s'", config->serial_number);
498         return 0;
499 }
500
501
502 static int wpa_config_process_device_type(const struct global_parse_data *data,
503                                           struct wpa_config *config, int line,
504                                           const char *pos)
505 {
506         os_free(config->device_type);
507         config->device_type = os_strdup(pos);
508         wpa_printf(MSG_DEBUG, "device_type='%s'", config->device_type);
509         return 0;
510 }
511
512
513 static int wpa_config_process_os_version(const struct global_parse_data *data,
514                                          struct wpa_config *config, int line,
515                                          const char *pos)
516 {
517         if (hexstr2bin(pos, config->os_version, 4)) {
518                 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
519                 return -1;
520         }
521         wpa_printf(MSG_DEBUG, "os_version=%08x",
522                    WPA_GET_BE32(config->os_version));
523         return 0;
524 }
525
526 #endif /* CONFIG_WPS */
527
528
529 #ifdef OFFSET
530 #undef OFFSET
531 #endif /* OFFSET */
532 /* OFFSET: Get offset of a variable within the wpa_config structure */
533 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
534
535 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f)
536 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, 0
537 #define INT(f) #f, wpa_config_parse_int, OFFSET(f)
538
539 static const struct global_parse_data global_fields[] = {
540 #ifdef CONFIG_CTRL_IFACE
541         { FUNC(ctrl_interface) },
542         { FUNC(ctrl_interface_group) },
543 #endif /* CONFIG_CTRL_IFACE */
544         { FUNC(eapol_version) },
545         { INT(ap_scan) },
546         { INT(fast_reauth) },
547 #ifdef EAP_TLS_OPENSSL
548         { FUNC(opensc_engine_path) },
549         { FUNC(pkcs11_engine_path) },
550         { FUNC(pkcs11_module_path) },
551 #endif /* EAP_TLS_OPENSSL */
552         { FUNC(driver_param) },
553         { INT(dot11RSNAConfigPMKLifetime) },
554         { INT(dot11RSNAConfigPMKReauthThreshold) },
555         { INT(dot11RSNAConfigSATimeout) },
556 #ifndef CONFIG_NO_CONFIG_WRITE
557         { INT(update_config) },
558 #endif /* CONFIG_NO_CONFIG_WRITE */
559         { FUNC_NO_VAR(load_dynamic_eap) },
560 #ifdef CONFIG_WPS
561         { FUNC(uuid) },
562         { FUNC(device_name) },
563         { FUNC(manufacturer) },
564         { FUNC(model_name) },
565         { FUNC(model_number) },
566         { FUNC(serial_number) },
567         { FUNC(device_type) },
568         { FUNC(os_version) },
569 #endif /* CONFIG_WPS */
570         { FUNC(country) }
571 };
572
573 #undef FUNC
574 #undef INT
575 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
576
577
578 static int wpa_config_process_global(struct wpa_config *config, char *pos,
579                                      int line)
580 {
581         size_t i;
582         int ret = 0;
583
584         for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
585                 const struct global_parse_data *field = &global_fields[i];
586                 size_t flen = os_strlen(field->name);
587                 if (os_strncmp(pos, field->name, flen) != 0 ||
588                     pos[flen] != '=')
589                         continue;
590
591                 if (field->parser(field, config, line, pos + flen + 1)) {
592                         wpa_printf(MSG_ERROR, "Line %d: failed to "
593                                    "parse '%s'.", line, pos);
594                         ret = -1;
595                 }
596                 break;
597         }
598         if (i == NUM_GLOBAL_FIELDS) {
599                 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
600                            line, pos);
601                 ret = -1;
602         }
603
604         return ret;
605 }
606
607
608 struct wpa_config * wpa_config_read(const char *name)
609 {
610         FILE *f;
611         char buf[256], *pos;
612         int errors = 0, line = 0;
613         struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
614         struct wpa_config *config;
615         int id = 0;
616
617         config = wpa_config_alloc_empty(NULL, NULL);
618         if (config == NULL)
619                 return NULL;
620         wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
621         f = fopen(name, "r");
622         if (f == NULL) {
623                 os_free(config);
624                 return NULL;
625         }
626
627         while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
628                 if (os_strcmp(pos, "network={") == 0) {
629                         ssid = wpa_config_read_network(f, &line, id++);
630                         if (ssid == NULL) {
631                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
632                                            "parse network block.", line);
633                                 errors++;
634                                 continue;
635                         }
636                         if (head == NULL) {
637                                 head = tail = ssid;
638                         } else {
639                                 tail->next = ssid;
640                                 tail = ssid;
641                         }
642                         if (wpa_config_add_prio_network(config, ssid)) {
643                                 wpa_printf(MSG_ERROR, "Line %d: failed to add "
644                                            "network block to priority list.",
645                                            line);
646                                 errors++;
647                                 continue;
648                         }
649 #ifndef CONFIG_NO_CONFIG_BLOBS
650                 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
651                         if (wpa_config_process_blob(config, f, &line, pos + 12)
652                             < 0) {
653                                 errors++;
654                                 continue;
655                         }
656 #endif /* CONFIG_NO_CONFIG_BLOBS */
657                 } else if (wpa_config_process_global(config, pos, line) < 0) {
658                         wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
659                                    "line '%s'.", line, pos);
660                         errors++;
661                         continue;
662                 }
663         }
664
665         fclose(f);
666
667         config->ssid = head;
668         wpa_config_debug_dump_networks(config);
669
670         if (errors) {
671                 wpa_config_free(config);
672                 config = NULL;
673                 head = NULL;
674         }
675
676         return config;
677 }
678
679
680 #ifndef CONFIG_NO_CONFIG_WRITE
681
682 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
683 {
684         char *value = wpa_config_get(ssid, field);
685         if (value == NULL)
686                 return;
687         fprintf(f, "\t%s=%s\n", field, value);
688         os_free(value);
689 }
690
691
692 static void write_int(FILE *f, const char *field, int value, int def)
693 {
694         if (value == def)
695                 return;
696         fprintf(f, "\t%s=%d\n", field, value);
697 }
698
699
700 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
701 {
702         char *value = wpa_config_get(ssid, "bssid");
703         if (value == NULL)
704                 return;
705         fprintf(f, "\tbssid=%s\n", value);
706         os_free(value);
707 }
708
709
710 static void write_psk(FILE *f, struct wpa_ssid *ssid)
711 {
712         char *value = wpa_config_get(ssid, "psk");
713         if (value == NULL)
714                 return;
715         fprintf(f, "\tpsk=%s\n", value);
716         os_free(value);
717 }
718
719
720 static void write_proto(FILE *f, struct wpa_ssid *ssid)
721 {
722         char *value;
723
724         if (ssid->proto == DEFAULT_PROTO)
725                 return;
726
727         value = wpa_config_get(ssid, "proto");
728         if (value == NULL)
729                 return;
730         if (value[0])
731                 fprintf(f, "\tproto=%s\n", value);
732         os_free(value);
733 }
734
735
736 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
737 {
738         char *value;
739
740         if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
741                 return;
742
743         value = wpa_config_get(ssid, "key_mgmt");
744         if (value == NULL)
745                 return;
746         if (value[0])
747                 fprintf(f, "\tkey_mgmt=%s\n", value);
748         os_free(value);
749 }
750
751
752 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
753 {
754         char *value;
755
756         if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
757                 return;
758
759         value = wpa_config_get(ssid, "pairwise");
760         if (value == NULL)
761                 return;
762         if (value[0])
763                 fprintf(f, "\tpairwise=%s\n", value);
764         os_free(value);
765 }
766
767
768 static void write_group(FILE *f, struct wpa_ssid *ssid)
769 {
770         char *value;
771
772         if (ssid->group_cipher == DEFAULT_GROUP)
773                 return;
774
775         value = wpa_config_get(ssid, "group");
776         if (value == NULL)
777                 return;
778         if (value[0])
779                 fprintf(f, "\tgroup=%s\n", value);
780         os_free(value);
781 }
782
783
784 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
785 {
786         char *value;
787
788         if (ssid->auth_alg == 0)
789                 return;
790
791         value = wpa_config_get(ssid, "auth_alg");
792         if (value == NULL)
793                 return;
794         if (value[0])
795                 fprintf(f, "\tauth_alg=%s\n", value);
796         os_free(value);
797 }
798
799
800 #ifdef IEEE8021X_EAPOL
801 static void write_eap(FILE *f, struct wpa_ssid *ssid)
802 {
803         char *value;
804
805         value = wpa_config_get(ssid, "eap");
806         if (value == NULL)
807                 return;
808
809         if (value[0])
810                 fprintf(f, "\teap=%s\n", value);
811         os_free(value);
812 }
813 #endif /* IEEE8021X_EAPOL */
814
815
816 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
817 {
818         char field[20], *value;
819         int res;
820
821         res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
822         if (res < 0 || (size_t) res >= sizeof(field))
823                 return;
824         value = wpa_config_get(ssid, field);
825         if (value) {
826                 fprintf(f, "\t%s=%s\n", field, value);
827                 os_free(value);
828         }
829 }
830
831
832 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
833 {
834         int i;
835
836 #define STR(t) write_str(f, #t, ssid)
837 #define INT(t) write_int(f, #t, ssid->t, 0)
838 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
839 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
840 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
841
842         STR(ssid);
843         INT(scan_ssid);
844         write_bssid(f, ssid);
845         write_psk(f, ssid);
846         write_proto(f, ssid);
847         write_key_mgmt(f, ssid);
848         write_pairwise(f, ssid);
849         write_group(f, ssid);
850         write_auth_alg(f, ssid);
851 #ifdef IEEE8021X_EAPOL
852         write_eap(f, ssid);
853         STR(identity);
854         STR(anonymous_identity);
855         STR(password);
856         STR(ca_cert);
857         STR(ca_path);
858         STR(client_cert);
859         STR(private_key);
860         STR(private_key_passwd);
861         STR(dh_file);
862         STR(subject_match);
863         STR(altsubject_match);
864         STR(ca_cert2);
865         STR(ca_path2);
866         STR(client_cert2);
867         STR(private_key2);
868         STR(private_key2_passwd);
869         STR(dh_file2);
870         STR(subject_match2);
871         STR(altsubject_match2);
872         STR(phase1);
873         STR(phase2);
874         STR(pcsc);
875         STR(pin);
876         STR(engine_id);
877         STR(key_id);
878         STR(cert_id);
879         STR(ca_cert_id);
880         STR(key2_id);
881         STR(pin2);
882         STR(engine2_id);
883         STR(cert2_id);
884         STR(ca_cert2_id);
885         INTe(engine);
886         INTe(engine2);
887         INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
888 #endif /* IEEE8021X_EAPOL */
889         for (i = 0; i < 4; i++)
890                 write_wep_key(f, i, ssid);
891         INT(wep_tx_keyidx);
892         INT(priority);
893 #ifdef IEEE8021X_EAPOL
894         INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
895         STR(pac_file);
896         INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
897 #endif /* IEEE8021X_EAPOL */
898         INT(mode);
899         INT(proactive_key_caching);
900         INT(disabled);
901         INT(peerkey);
902 #ifdef CONFIG_IEEE80211W
903         INT(ieee80211w);
904 #endif /* CONFIG_IEEE80211W */
905         STR(id_str);
906
907 #undef STR
908 #undef INT
909 #undef INT_DEF
910 }
911
912
913 #ifndef CONFIG_NO_CONFIG_BLOBS
914 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
915 {
916         unsigned char *encoded;
917
918         encoded = base64_encode(blob->data, blob->len, NULL);
919         if (encoded == NULL)
920                 return -1;
921
922         fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
923         os_free(encoded);
924         return 0;
925 }
926 #endif /* CONFIG_NO_CONFIG_BLOBS */
927
928
929 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
930 {
931 #ifdef CONFIG_CTRL_IFACE
932         if (config->ctrl_interface)
933                 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
934         if (config->ctrl_interface_group)
935                 fprintf(f, "ctrl_interface_group=%s\n",
936                         config->ctrl_interface_group);
937 #endif /* CONFIG_CTRL_IFACE */
938         if (config->eapol_version != DEFAULT_EAPOL_VERSION)
939                 fprintf(f, "eapol_version=%d\n", config->eapol_version);
940         if (config->ap_scan != DEFAULT_AP_SCAN)
941                 fprintf(f, "ap_scan=%d\n", config->ap_scan);
942         if (config->fast_reauth != DEFAULT_FAST_REAUTH)
943                 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
944 #ifdef EAP_TLS_OPENSSL
945         if (config->opensc_engine_path)
946                 fprintf(f, "opensc_engine_path=%s\n",
947                         config->opensc_engine_path);
948         if (config->pkcs11_engine_path)
949                 fprintf(f, "pkcs11_engine_path=%s\n",
950                         config->pkcs11_engine_path);
951         if (config->pkcs11_module_path)
952                 fprintf(f, "pkcs11_module_path=%s\n",
953                         config->pkcs11_module_path);
954 #endif /* EAP_TLS_OPENSSL */
955         if (config->driver_param)
956                 fprintf(f, "driver_param=%s\n", config->driver_param);
957         if (config->dot11RSNAConfigPMKLifetime)
958                 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
959                         config->dot11RSNAConfigPMKLifetime);
960         if (config->dot11RSNAConfigPMKReauthThreshold)
961                 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
962                         config->dot11RSNAConfigPMKReauthThreshold);
963         if (config->dot11RSNAConfigSATimeout)
964                 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
965                         config->dot11RSNAConfigSATimeout);
966         if (config->update_config)
967                 fprintf(f, "update_config=%d\n", config->update_config);
968 #ifdef CONFIG_WPS
969         if (is_nil_uuid(config->uuid)) {
970                 char buf[40];
971                 uuid_bin2str(config->uuid, buf, sizeof(buf));
972                 fprintf(f, "uuid=%s\n", buf);
973         }
974         if (config->device_name)
975                 fprintf(f, "device_name=%s\n", config->device_name);
976         if (config->manufacturer)
977                 fprintf(f, "manufacturer=%s\n", config->manufacturer);
978         if (config->model_name)
979                 fprintf(f, "model_name=%s\n", config->model_name);
980         if (config->model_number)
981                 fprintf(f, "model_number=%s\n", config->model_number);
982         if (config->serial_number)
983                 fprintf(f, "serial_number=%s\n", config->serial_number);
984         if (config->device_type)
985                 fprintf(f, "device_type=%s\n", config->device_type);
986         if (config->os_version)
987                 fprintf(f, "os_version=%08x\n",
988                         WPA_GET_BE32(config->os_version));
989 #endif /* CONFIG_WPS */
990         if (config->country[0] && config->country[1]) {
991                 fprintf(f, "country=%c%c\n",
992                         config->country[0], config->country[1]);
993         }
994 }
995
996 #endif /* CONFIG_NO_CONFIG_WRITE */
997
998
999 int wpa_config_write(const char *name, struct wpa_config *config)
1000 {
1001 #ifndef CONFIG_NO_CONFIG_WRITE
1002         FILE *f;
1003         struct wpa_ssid *ssid;
1004 #ifndef CONFIG_NO_CONFIG_BLOBS
1005         struct wpa_config_blob *blob;
1006 #endif /* CONFIG_NO_CONFIG_BLOBS */
1007         int ret = 0;
1008
1009         wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
1010
1011         f = fopen(name, "w");
1012         if (f == NULL) {
1013                 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
1014                 return -1;
1015         }
1016
1017         wpa_config_write_global(f, config);
1018
1019         for (ssid = config->ssid; ssid; ssid = ssid->next) {
1020                 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1021                         continue; /* do not save temporary WPS networks */
1022                 fprintf(f, "\nnetwork={\n");
1023                 wpa_config_write_network(f, ssid);
1024                 fprintf(f, "}\n");
1025         }
1026
1027 #ifndef CONFIG_NO_CONFIG_BLOBS
1028         for (blob = config->blobs; blob; blob = blob->next) {
1029                 ret = wpa_config_write_blob(f, blob);
1030                 if (ret)
1031                         break;
1032         }
1033 #endif /* CONFIG_NO_CONFIG_BLOBS */
1034
1035         fclose(f);
1036
1037         wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
1038                    name, ret ? "un" : "");
1039         return ret;
1040 #else /* CONFIG_NO_CONFIG_WRITE */
1041         return -1;
1042 #endif /* CONFIG_NO_CONFIG_WRITE */
1043 }