OSDN Git Service

auto import from //depot/cupcake/@135843
[android-x86/external-wpa_supplicant.git] / config_file.c
1 /*
2  * WPA Supplicant / Configuration backend: text file
3  * Copyright (c) 2003-2006, 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 "eap_methods.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
107         if ((ssid->key_mgmt & WPA_KEY_MGMT_PSK) && !ssid->psk_set) {
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 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
188                                                      const char *name)
189 {
190         struct wpa_config_blob *blob;
191         char buf[256], *pos;
192         unsigned char *encoded = NULL, *nencoded;
193         int end = 0;
194         size_t encoded_len = 0, len;
195
196         wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
197                    *line, name);
198
199         while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
200                 if (os_strcmp(pos, "}") == 0) {
201                         end = 1;
202                         break;
203                 }
204
205                 len = os_strlen(pos);
206                 nencoded = os_realloc(encoded, encoded_len + len);
207                 if (nencoded == NULL) {
208                         wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
209                                    "blob", *line);
210                         os_free(encoded);
211                         return NULL;
212                 }
213                 encoded = nencoded;
214                 os_memcpy(encoded + encoded_len, pos, len);
215                 encoded_len += len;
216         }
217
218         if (!end) {
219                 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
220                            "properly", *line);
221                 os_free(encoded);
222                 return NULL;
223         }
224
225         blob = os_zalloc(sizeof(*blob));
226         if (blob == NULL) {
227                 os_free(encoded);
228                 return NULL;
229         }
230         blob->name = os_strdup(name);
231         blob->data = base64_decode(encoded, encoded_len, &blob->len);
232         os_free(encoded);
233
234         if (blob->name == NULL || blob->data == NULL) {
235                 wpa_config_free_blob(blob);
236                 return NULL;
237         }
238
239         return blob;
240 }
241
242
243 struct wpa_config * wpa_config_read(const char *name)
244 {
245         FILE *f;
246         char buf[256], *pos;
247         int errors = 0, line = 0;
248         struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
249         struct wpa_config *config;
250         int id = 0;
251
252         config = wpa_config_alloc_empty(NULL, NULL);
253         if (config == NULL)
254                 return NULL;
255         wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
256         f = fopen(name, "r");
257         if (f == NULL) {
258                 os_free(config);
259                 return NULL;
260         }
261
262         while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
263                 if (os_strcmp(pos, "network={") == 0) {
264                         ssid = wpa_config_read_network(f, &line, id++);
265                         if (ssid == NULL) {
266                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
267                                            "parse network block.", line);
268 #ifndef WPA_IGNORE_CONFIG_ERRORS
269                                 errors++;
270 #endif
271                                 continue;
272                         }
273                         if (head == NULL) {
274                                 head = tail = ssid;
275                         } else {
276                                 tail->next = ssid;
277                                 tail = ssid;
278                         }
279                         if (wpa_config_add_prio_network(config, ssid)) {
280                                 wpa_printf(MSG_ERROR, "Line %d: failed to add "
281                                            "network block to priority list.",
282                                            line);
283                                 errors++;
284                                 continue;
285                         }
286                 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
287                         char *bname = pos + 12, *name_end;
288                         struct wpa_config_blob *blob;
289
290                         name_end = os_strchr(bname, '=');
291                         if (name_end == NULL) {
292                                 wpa_printf(MSG_ERROR, "Line %d: no blob name "
293                                            "terminator", line);
294                                 errors++;
295                                 continue;
296                         }
297                         *name_end = '\0';
298
299                         blob = wpa_config_read_blob(f, &line, bname);
300                         if (blob == NULL) {
301                                 wpa_printf(MSG_ERROR, "Line %d: failed to read"
302                                            " blob %s", line, bname);
303                                 errors++;
304                                 continue;
305                         }
306                         wpa_config_set_blob(config, blob);
307 #ifdef CONFIG_CTRL_IFACE
308                 } else if (os_strncmp(pos, "ctrl_interface=", 15) == 0) {
309                         os_free(config->ctrl_interface);
310                         config->ctrl_interface = os_strdup(pos + 15);
311                         wpa_printf(MSG_DEBUG, "ctrl_interface='%s'",
312                                    config->ctrl_interface);
313                 } else if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0) {
314                         os_free(config->ctrl_interface_group);
315                         config->ctrl_interface_group = os_strdup(pos + 21);
316                         wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' "
317                                    "(DEPRECATED)",
318                                    config->ctrl_interface_group);
319 #endif /* CONFIG_CTRL_IFACE */
320                 } else if (os_strncmp(pos, "eapol_version=", 14) == 0) {
321                         config->eapol_version = atoi(pos + 14);
322                         if (config->eapol_version < 1 ||
323                             config->eapol_version > 2) {
324                                 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL "
325                                            "version (%d): '%s'.",
326                                            line, config->eapol_version, pos);
327                                 errors++;
328                                 continue;
329                         }
330                         wpa_printf(MSG_DEBUG, "eapol_version=%d",
331                                    config->eapol_version);
332                 } else if (os_strncmp(pos, "ap_scan=", 8) == 0) {
333                         config->ap_scan = atoi(pos + 8);
334                         wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
335                 } else if (os_strncmp(pos, "fast_reauth=", 12) == 0) {
336                         config->fast_reauth = atoi(pos + 12);
337                         wpa_printf(MSG_DEBUG, "fast_reauth=%d",
338                                    config->fast_reauth);
339                 } else if (os_strncmp(pos, "opensc_engine_path=", 19) == 0) {
340                         os_free(config->opensc_engine_path);
341                         config->opensc_engine_path = os_strdup(pos + 19);
342                         wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
343                                    config->opensc_engine_path);
344                 } else if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0) {
345                         os_free(config->pkcs11_engine_path);
346                         config->pkcs11_engine_path = os_strdup(pos + 19);
347                         wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
348                                    config->pkcs11_engine_path);
349                 } else if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0) {
350                         os_free(config->pkcs11_module_path);
351                         config->pkcs11_module_path = os_strdup(pos + 19);
352                         wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
353                                    config->pkcs11_module_path);
354                 } else if (os_strncmp(pos, "driver_param=", 13) == 0) {
355                         os_free(config->driver_param);
356                         config->driver_param = os_strdup(pos + 13);
357                         wpa_printf(MSG_DEBUG, "driver_param='%s'",
358                                    config->driver_param);
359                 } else if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27)
360                            == 0) {
361                         config->dot11RSNAConfigPMKLifetime = atoi(pos + 27);
362                         wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
363                                    config->dot11RSNAConfigPMKLifetime);
364                 } else if (os_strncmp(pos,
365                                       "dot11RSNAConfigPMKReauthThreshold=", 34)
366                            == 0) {
367                         config->dot11RSNAConfigPMKReauthThreshold =
368                                 atoi(pos + 34);
369                         wpa_printf(MSG_DEBUG,
370                                    "dot11RSNAConfigPMKReauthThreshold=%d",
371                                    config->dot11RSNAConfigPMKReauthThreshold);
372                 } else if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) ==
373                            0) {
374                         config->dot11RSNAConfigSATimeout = atoi(pos + 25);
375                         wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
376                                    config->dot11RSNAConfigSATimeout);
377                 } else if (os_strncmp(pos, "update_config=", 14) == 0) {
378                         config->update_config = atoi(pos + 14);
379                         wpa_printf(MSG_DEBUG, "update_config=%d",
380                                    config->update_config);
381                 } else if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0) {
382                         char *so = pos + 17;
383                         int ret;
384                         wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
385                         ret = eap_peer_method_load(so);
386                         if (ret == -2) {
387                                 wpa_printf(MSG_DEBUG, "This EAP type was "
388                                            "already loaded - not reloading.");
389                         } else if (ret) {
390                                 wpa_printf(MSG_ERROR, "Line %d: Failed to "
391                                            "load dynamic EAP method '%s'.",
392                                            line, so);
393                                 errors++;
394                         }
395                 } else {
396                         wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
397                                    "line '%s'.", line, pos);
398                         errors++;
399                         continue;
400                 }
401         }
402
403         fclose(f);
404
405         config->ssid = head;
406         wpa_config_debug_dump_networks(config);
407
408 #ifndef WPA_IGNORE_CONFIG_ERRORS
409         if (errors) {
410                 wpa_config_free(config);
411                 config = NULL;
412                 head = NULL;
413         }
414 #endif
415         return config;
416 }
417
418
419 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
420 {
421         char *value = wpa_config_get(ssid, field);
422         if (value == NULL)
423                 return;
424         fprintf(f, "\t%s=%s\n", field, value);
425         os_free(value);
426 }
427
428
429 static void write_int(FILE *f, const char *field, int value, int def)
430 {
431         if (value == def)
432                 return;
433         fprintf(f, "\t%s=%d\n", field, value);
434 }
435
436
437 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
438 {
439         char *value = wpa_config_get(ssid, "bssid");
440         if (value == NULL)
441                 return;
442         fprintf(f, "\tbssid=%s\n", value);
443         os_free(value);
444 }
445
446
447 static void write_psk(FILE *f, struct wpa_ssid *ssid)
448 {
449         char *value = wpa_config_get(ssid, "psk");
450         if (value == NULL)
451                 return;
452         fprintf(f, "\tpsk=%s\n", value);
453         os_free(value);
454 }
455
456
457 static void write_proto(FILE *f, struct wpa_ssid *ssid)
458 {
459         char *value;
460
461         if (ssid->proto == DEFAULT_PROTO)
462                 return;
463
464         value = wpa_config_get(ssid, "proto");
465         if (value == NULL)
466                 return;
467         if (value[0])
468                 fprintf(f, "\tproto=%s\n", value);
469         os_free(value);
470 }
471
472
473 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
474 {
475         char *value;
476
477         if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
478                 return;
479
480         value = wpa_config_get(ssid, "key_mgmt");
481         if (value == NULL)
482                 return;
483         if (value[0])
484                 fprintf(f, "\tkey_mgmt=%s\n", value);
485         os_free(value);
486 }
487
488
489 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
490 {
491         char *value;
492
493         if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
494                 return;
495
496         value = wpa_config_get(ssid, "pairwise");
497         if (value == NULL)
498                 return;
499         if (value[0])
500                 fprintf(f, "\tpairwise=%s\n", value);
501         os_free(value);
502 }
503
504
505 static void write_group(FILE *f, struct wpa_ssid *ssid)
506 {
507         char *value;
508
509         if (ssid->group_cipher == DEFAULT_GROUP)
510                 return;
511
512         value = wpa_config_get(ssid, "group");
513         if (value == NULL)
514                 return;
515         if (value[0])
516                 fprintf(f, "\tgroup=%s\n", value);
517         os_free(value);
518 }
519
520
521 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
522 {
523         char *value;
524
525         if (ssid->auth_alg == 0)
526                 return;
527
528         value = wpa_config_get(ssid, "auth_alg");
529         if (value == NULL)
530                 return;
531         if (value[0])
532                 fprintf(f, "\tauth_alg=%s\n", value);
533         os_free(value);
534 }
535
536
537 #ifdef IEEE8021X_EAPOL
538 static void write_eap(FILE *f, struct wpa_ssid *ssid)
539 {
540         char *value;
541
542         value = wpa_config_get(ssid, "eap");
543         if (value == NULL)
544                 return;
545
546         if (value[0])
547                 fprintf(f, "\teap=%s\n", value);
548         os_free(value);
549 }
550 #endif /* IEEE8021X_EAPOL */
551
552
553 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
554 {
555         char field[20], *value;
556
557         os_snprintf(field, sizeof(field), "wep_key%d", idx);
558         value = wpa_config_get(ssid, field);
559         if (value) {
560                 fprintf(f, "\t%s=%s\n", field, value);
561                 os_free(value);
562         }
563 }
564
565
566 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
567 {
568         int i;
569
570 #define STR(t) write_str(f, #t, ssid)
571 #define INT(t) write_int(f, #t, ssid->t, 0)
572 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
573
574         STR(ssid);
575         INT(scan_ssid);
576         write_bssid(f, ssid);
577         write_psk(f, ssid);
578         write_proto(f, ssid);
579         write_key_mgmt(f, ssid);
580         write_pairwise(f, ssid);
581         write_group(f, ssid);
582         write_auth_alg(f, ssid);
583 #ifdef IEEE8021X_EAPOL
584         write_eap(f, ssid);
585         STR(identity);
586         STR(anonymous_identity);
587         STR(eappsk);
588         STR(nai);
589         STR(password);
590         STR(ca_cert);
591         STR(ca_path);
592         STR(client_cert);
593         STR(private_key);
594         STR(private_key_passwd);
595         STR(dh_file);
596         STR(subject_match);
597         STR(altsubject_match);
598         STR(ca_cert2);
599         STR(ca_path2);
600         STR(client_cert2);
601         STR(private_key2);
602         STR(private_key2_passwd);
603         STR(dh_file2);
604         STR(subject_match2);
605         STR(altsubject_match2);
606         STR(phase1);
607         STR(phase2);
608         STR(pcsc);
609         STR(pin);
610         STR(engine_id);
611         STR(key_id);
612         INT(engine);
613         INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
614 #endif /* IEEE8021X_EAPOL */
615         for (i = 0; i < 4; i++)
616                 write_wep_key(f, i, ssid);
617         INT(wep_tx_keyidx);
618         INT(priority);
619 #ifdef IEEE8021X_EAPOL
620         INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
621         STR(pac_file);
622         INT_DEF(fragment_size, DEFAULT_FRAGMENT_SIZE);
623 #endif /* IEEE8021X_EAPOL */
624         INT(mode);
625         INT(proactive_key_caching);
626         INT(disabled);
627         INT(peerkey);
628 #ifdef CONFIG_IEEE80211W
629         INT(ieee80211w);
630 #endif /* CONFIG_IEEE80211W */
631         STR(id_str);
632
633 #undef STR
634 #undef INT
635 #undef INT_DEF
636 }
637
638
639 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
640 {
641         unsigned char *encoded;
642
643         encoded = base64_encode(blob->data, blob->len, NULL);
644         if (encoded == NULL)
645                 return -1;
646
647         fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
648         os_free(encoded);
649         return 0;
650 }
651
652
653 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
654 {
655 #ifdef CONFIG_CTRL_IFACE
656         if (config->ctrl_interface)
657                 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
658         if (config->ctrl_interface_group)
659                 fprintf(f, "ctrl_interface_group=%s\n",
660                         config->ctrl_interface_group);
661 #endif /* CONFIG_CTRL_IFACE */
662         if (config->eapol_version != DEFAULT_EAPOL_VERSION)
663                 fprintf(f, "eapol_version=%d\n", config->eapol_version);
664         if (config->ap_scan != DEFAULT_AP_SCAN)
665                 fprintf(f, "ap_scan=%d\n", config->ap_scan);
666         if (config->fast_reauth != DEFAULT_FAST_REAUTH)
667                 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
668         if (config->opensc_engine_path)
669                 fprintf(f, "opensc_engine_path=%s\n",
670                         config->opensc_engine_path);
671         if (config->pkcs11_engine_path)
672                 fprintf(f, "pkcs11_engine_path=%s\n",
673                         config->pkcs11_engine_path);
674         if (config->pkcs11_module_path)
675                 fprintf(f, "pkcs11_module_path=%s\n",
676                         config->pkcs11_module_path);
677         if (config->driver_param)
678                 fprintf(f, "driver_param=%s\n", config->driver_param);
679         if (config->dot11RSNAConfigPMKLifetime)
680                 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
681                         config->dot11RSNAConfigPMKLifetime);
682         if (config->dot11RSNAConfigPMKReauthThreshold)
683                 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
684                         config->dot11RSNAConfigPMKReauthThreshold);
685         if (config->dot11RSNAConfigSATimeout)
686                 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
687                         config->dot11RSNAConfigSATimeout);
688         if (config->update_config)
689                 fprintf(f, "update_config=%d\n", config->update_config);
690 }
691
692
693 int wpa_config_write(const char *name, struct wpa_config *config)
694 {
695         FILE *f;
696         struct wpa_ssid *ssid;
697         struct wpa_config_blob *blob;
698         int ret = 0;
699
700         wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
701
702         f = fopen(name, "w");
703         if (f == NULL) {
704                 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
705                 return -1;
706         }
707
708         wpa_config_write_global(f, config);
709
710         for (ssid = config->ssid; ssid; ssid = ssid->next) {
711                 fprintf(f, "\nnetwork={\n");
712                 wpa_config_write_network(f, ssid);
713                 fprintf(f, "}\n");
714         }
715
716         for (blob = config->blobs; blob; blob = blob->next) {
717                 ret = wpa_config_write_blob(f, blob);
718                 if (ret)
719                         break;
720         }
721
722         fclose(f);
723
724         wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
725                    name, ret ? "un" : "");
726         return ret;
727 }