Merge from vendor branch LESS:
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / config_file.c
1 /*
2  * WPA Supplicant / Configuration backend: text file
3  * Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.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 <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "common.h"
24 #include "wpa.h"
25 #include "wpa_supplicant.h"
26 #include "config.h"
27 #include "base64.h"
28
29
30 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line)
31 {
32         char *pos, *end, *sstart;
33
34         while (fgets(s, size, stream)) {
35                 (*line)++;
36                 s[size - 1] = '\0';
37                 pos = s;
38
39                 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
40                         pos++;
41                 if (*pos == '#' || *pos == '\n' || *pos == '\0' ||
42                     *pos == '\r')
43                         continue;
44
45                 /* Remove # comments unless they are within a double quoted
46                  * string. Remove trailing white space. */
47                 sstart = strchr(pos, '"');
48                 if (sstart)
49                         sstart = strrchr(sstart + 1, '"');
50                 if (!sstart)
51                         sstart = pos;
52                 end = strchr(sstart, '#');
53                 if (end)
54                         *end-- = '\0';
55                 else
56                         end = pos + strlen(pos) - 1;
57                 while (end > pos &&
58                        (*end == '\n' || *end == ' ' || *end == '\t' ||
59                         *end == '\r')) {
60                         *end-- = '\0';
61                 }
62                 if (*pos == '\0')
63                         continue;
64
65                 return pos;
66         }
67
68         return NULL;
69 }
70
71
72 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
73 {
74         struct wpa_ssid *ssid;
75         int errors = 0, end = 0;
76         char buf[256], *pos, *pos2;
77
78         wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
79                    *line);
80         ssid = (struct wpa_ssid *) malloc(sizeof(*ssid));
81         if (ssid == NULL)
82                 return NULL;
83         memset(ssid, 0, sizeof(*ssid));
84         ssid->id = id;
85
86         wpa_config_set_network_defaults(ssid);
87
88         while ((pos = wpa_config_get_line(buf, sizeof(buf), f, line))) {
89                 if (strcmp(pos, "}") == 0) {
90                         end = 1;
91                         break;
92                 }
93
94                 pos2 = strchr(pos, '=');
95                 if (pos2 == NULL) {
96                         wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
97                                    "'%s'.", *line, pos);
98                         errors++;
99                         continue;
100                 }
101
102                 *pos2++ = '\0';
103                 if (*pos2 == '"') {
104                         if (strchr(pos2 + 1, '"') == NULL) {
105                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
106                                            "quotation '%s'.", *line, pos2);
107                                 errors++;
108                                 continue;
109                         }
110                 }
111
112                 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
113                         errors++;
114         }
115
116         if (!end) {
117                 wpa_printf(MSG_ERROR, "Line %d: network block was not "
118                            "terminated properly.", *line);
119                 errors++;
120         }
121
122         if (ssid->passphrase) {
123                 if (ssid->psk_set) {
124                         wpa_printf(MSG_ERROR, "Line %d: both PSK and "
125                                    "passphrase configured.", *line);
126                         errors++;
127                 }
128                 wpa_config_update_psk(ssid);
129         }
130
131         if ((ssid->key_mgmt & WPA_KEY_MGMT_PSK) && !ssid->psk_set) {
132                 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
133                            "management, but no PSK configured.", *line);
134                 errors++;
135         }
136
137         if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
138             !(ssid->pairwise_cipher & WPA_CIPHER_CCMP)) {
139                 /* Group cipher cannot be stronger than the pairwise cipher. */
140                 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
141                            " list since it was not allowed for pairwise "
142                            "cipher", *line);
143                 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
144         }
145
146         if (errors) {
147                 wpa_config_free_ssid(ssid);
148                 ssid = NULL;
149         }
150
151         return ssid;
152 }
153
154
155 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
156                                                      const char *name)
157 {
158         struct wpa_config_blob *blob;
159         char buf[256], *pos;
160         unsigned char *encoded = NULL, *nencoded;
161         int end = 0;
162         size_t encoded_len = 0, len;
163
164         wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
165                    *line, name);
166
167         while ((pos = wpa_config_get_line(buf, sizeof(buf), f, line))) {
168                 if (strcmp(pos, "}") == 0) {
169                         end = 1;
170                         break;
171                 }
172
173                 len = strlen(pos);
174                 nencoded = realloc(encoded, encoded_len + len);
175                 if (nencoded == NULL) {
176                         wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
177                                    "blob", *line);
178                         free(encoded);
179                         return NULL;
180                 }
181                 encoded = nencoded;
182                 memcpy(encoded + encoded_len, pos, len);
183                 encoded_len += len;
184         }
185
186         if (!end) {
187                 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
188                            "properly", *line);
189                 free(encoded);
190                 return NULL;
191         }
192
193         blob = malloc(sizeof(*blob));
194         if (blob == NULL) {
195                 free(encoded);
196                 return NULL;
197         }
198         memset(blob, 0, sizeof(*blob));
199         blob->name = strdup(name);
200         blob->data = base64_decode(encoded, encoded_len, &blob->len);
201         free(encoded);
202
203         if (blob->name == NULL || blob->data == NULL) {
204                 wpa_config_free_blob(blob);
205                 return NULL;
206         }
207
208         return blob;
209 }
210
211
212 struct wpa_config * wpa_config_read(const char *name)
213 {
214         FILE *f;
215         char buf[256], *pos;
216         int errors = 0, line = 0;
217         struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
218         struct wpa_config *config;
219         int id = 0, prio;
220
221         config = wpa_config_alloc_empty(NULL, NULL);
222         if (config == NULL)
223                 return NULL;
224         wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
225         f = fopen(name, "r");
226         if (f == NULL) {
227                 free(config);
228                 return NULL;
229         }
230
231         while ((pos = wpa_config_get_line(buf, sizeof(buf), f, &line))) {
232                 if (strcmp(pos, "network={") == 0) {
233                         ssid = wpa_config_read_network(f, &line, id++);
234                         if (ssid == NULL) {
235                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
236                                            "parse network block.", line);
237                                 errors++;
238                                 continue;
239                         }
240                         if (head == NULL) {
241                                 head = tail = ssid;
242                         } else {
243                                 tail->next = ssid;
244                                 tail = ssid;
245                         }
246                         if (wpa_config_add_prio_network(config, ssid)) {
247                                 wpa_printf(MSG_ERROR, "Line %d: failed to add "
248                                            "network block to priority list.",
249                                            line);
250                                 errors++;
251                                 continue;
252                         }
253                 } else if (strncmp(pos, "blob-base64-", 12) == 0) {
254                         char *name = pos + 12, *name_end;
255                         struct wpa_config_blob *blob;
256
257                         name_end = strchr(name, '=');
258                         if (name_end == NULL) {
259                                 wpa_printf(MSG_ERROR, "Line %d: no blob name "
260                                            "terminator", line);
261                                 errors++;
262                                 continue;
263                         }
264                         *name_end = '\0';
265
266                         blob = wpa_config_read_blob(f, &line, name);
267                         if (blob == NULL) {
268                                 wpa_printf(MSG_ERROR, "Line %d: failed to read"
269                                            " blob %s", line, name);
270                                 errors++;
271                                 continue;
272                         }
273                         wpa_config_set_blob(config, blob);
274 #ifdef CONFIG_CTRL_IFACE
275                 } else if (strncmp(pos, "ctrl_interface=", 15) == 0) {
276                         free(config->ctrl_interface);
277                         config->ctrl_interface = strdup(pos + 15);
278                         wpa_printf(MSG_DEBUG, "ctrl_interface='%s'",
279                                    config->ctrl_interface);
280 #ifndef CONFIG_CTRL_IFACE_UDP
281                 } else if (strncmp(pos, "ctrl_interface_group=", 21) == 0) {
282                         struct group *grp;
283                         char *endp;
284                         const char *group = pos + 21;
285
286                         grp = getgrnam(group);
287                         if (grp) {
288                                 config->ctrl_interface_gid = grp->gr_gid;
289                                 config->ctrl_interface_gid_set = 1;
290                                 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
291                                            " (from group name '%s')",
292                                            (int) config->ctrl_interface_gid,
293                                            group);
294                                 continue;
295                         }
296
297                         /* Group name not found - try to parse this as gid */
298                         config->ctrl_interface_gid = strtol(group, &endp, 10);
299                         if (*group == '\0' || *endp != '\0') {
300                                 wpa_printf(MSG_DEBUG, "Line %d: Invalid group "
301                                            "'%s'", line, group);
302                                 errors++;
303                                 continue;
304                         }
305                         config->ctrl_interface_gid_set = 1;
306                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
307                                    (int) config->ctrl_interface_gid);
308 #endif /* CONFIG_CTRL_IFACE_UDP */
309 #endif /* CONFIG_CTRL_IFACE */
310                 } else if (strncmp(pos, "eapol_version=", 14) == 0) {
311                         config->eapol_version = atoi(pos + 14);
312                         if (config->eapol_version < 1 ||
313                             config->eapol_version > 2) {
314                                 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL "
315                                            "version (%d): '%s'.",
316                                            line, config->eapol_version, pos);
317                                 errors++;
318                                 continue;
319                         }
320                         wpa_printf(MSG_DEBUG, "eapol_version=%d",
321                                    config->eapol_version);
322                 } else if (strncmp(pos, "ap_scan=", 8) == 0) {
323                         config->ap_scan = atoi(pos + 8);
324                         wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
325                 } else if (strncmp(pos, "fast_reauth=", 12) == 0) {
326                         config->fast_reauth = atoi(pos + 12);
327                         wpa_printf(MSG_DEBUG, "fast_reauth=%d",
328                                    config->fast_reauth);
329                 } else if (strncmp(pos, "opensc_engine_path=", 19) == 0) {
330                         free(config->opensc_engine_path);
331                         config->opensc_engine_path = strdup(pos + 19);
332                         wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
333                                    config->opensc_engine_path);
334                 } else if (strncmp(pos, "pkcs11_engine_path=", 19) == 0) {
335                         free(config->pkcs11_engine_path);
336                         config->pkcs11_engine_path = strdup(pos + 19);
337                         wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
338                                    config->pkcs11_engine_path);
339                 } else if (strncmp(pos, "pkcs11_module_path=", 19) == 0) {
340                         free(config->pkcs11_module_path);
341                         config->pkcs11_module_path = strdup(pos + 19);
342                         wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
343                                    config->pkcs11_module_path);
344                 } else if (strncmp(pos, "driver_param=", 13) == 0) {
345                         free(config->driver_param);
346                         config->driver_param = strdup(pos + 13);
347                         wpa_printf(MSG_DEBUG, "driver_param='%s'",
348                                    config->driver_param);
349                 } else if (strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) ==
350                            0) {
351                         config->dot11RSNAConfigPMKLifetime = atoi(pos + 27);
352                         wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
353                                    config->dot11RSNAConfigPMKLifetime);
354                 } else if (strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=",
355                                    34) ==
356                            0) {
357                         config->dot11RSNAConfigPMKReauthThreshold =
358                                 atoi(pos + 34);
359                         wpa_printf(MSG_DEBUG,
360                                    "dot11RSNAConfigPMKReauthThreshold=%d",
361                                    config->dot11RSNAConfigPMKReauthThreshold);
362                 } else if (strncmp(pos, "dot11RSNAConfigSATimeout=", 25) ==
363                            0) {
364                         config->dot11RSNAConfigSATimeout = atoi(pos + 25);
365                         wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
366                                    config->dot11RSNAConfigSATimeout);
367                 } else if (strncmp(pos, "update_config=", 14) == 0) {
368                         config->update_config = atoi(pos + 14);
369                         wpa_printf(MSG_DEBUG, "update_config=%d",
370                                    config->update_config);
371                 } else {
372                         wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
373                                    "line '%s'.", line, pos);
374                         errors++;
375                         continue;
376                 }
377         }
378
379         fclose(f);
380
381         config->ssid = head;
382         for (prio = 0; prio < config->num_prio; prio++) {
383                 ssid = config->pssid[prio];
384                 wpa_printf(MSG_DEBUG, "Priority group %d",
385                            ssid->priority);
386                 while (ssid) {
387                         wpa_printf(MSG_DEBUG, "   id=%d ssid='%s'",
388                                    ssid->id,
389                                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
390                         ssid = ssid->pnext;
391                 }
392         }
393         if (errors) {
394                 wpa_config_free(config);
395                 config = NULL;
396                 head = NULL;
397         }
398
399         return config;
400 }
401
402
403 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
404 {
405         char *value = wpa_config_get(ssid, field);
406         if (value == NULL)
407                 return;
408         fprintf(f, "\t%s=%s\n", field, value);
409         free(value);
410 }
411
412
413 static void write_int(FILE *f, const char *field, int value, int def)
414 {
415         if (value == def)
416                 return;
417         fprintf(f, "\t%s=%d\n", field, value);
418 }
419
420
421 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
422 {
423         char *value = wpa_config_get(ssid, "bssid");
424         if (value == NULL)
425                 return;
426         fprintf(f, "\tbssid=%s\n", value);
427         free(value);
428 }
429
430
431 static void write_psk(FILE *f, struct wpa_ssid *ssid)
432 {
433         char *value = wpa_config_get(ssid, "psk");
434         if (value == NULL)
435                 return;
436         fprintf(f, "\tpsk=%s\n", value);
437         free(value);
438 }
439
440
441 static void write_proto(FILE *f, struct wpa_ssid *ssid)
442 {
443         char *value;
444
445         if (ssid->proto == DEFAULT_PROTO)
446                 return;
447
448         value = wpa_config_get(ssid, "proto");
449         if (value == NULL)
450                 return;
451         if (value[0])
452                 fprintf(f, "\tproto=%s\n", value);
453         free(value);
454 }
455
456
457 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
458 {
459         char *value;
460
461         if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
462                 return;
463
464         value = wpa_config_get(ssid, "key_mgmt");
465         if (value == NULL)
466                 return;
467         if (value[0])
468                 fprintf(f, "\tkey_mgmt=%s\n", value);
469         free(value);
470 }
471
472
473 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
474 {
475         char *value;
476
477         if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
478                 return;
479
480         value = wpa_config_get(ssid, "pairwise");
481         if (value == NULL)
482                 return;
483         if (value[0])
484                 fprintf(f, "\tpairwise=%s\n", value);
485         free(value);
486 }
487
488
489 static void write_group(FILE *f, struct wpa_ssid *ssid)
490 {
491         char *value;
492
493         if (ssid->group_cipher == DEFAULT_GROUP)
494                 return;
495
496         value = wpa_config_get(ssid, "group");
497         if (value == NULL)
498                 return;
499         if (value[0])
500                 fprintf(f, "\tgroup=%s\n", value);
501         free(value);
502 }
503
504
505 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
506 {
507         char *value;
508
509         if (ssid->auth_alg == 0)
510                 return;
511
512         value = wpa_config_get(ssid, "auth_alg");
513         if (value == NULL)
514                 return;
515         if (value[0])
516                 fprintf(f, "\tauth_alg=%s\n", value);
517         free(value);
518 }
519
520
521 static void write_eap(FILE *f, struct wpa_ssid *ssid)
522 {
523         char *value;
524
525         value = wpa_config_get(ssid, "eap");
526         if (value == NULL)
527                 return;
528
529         if (value[0])
530                 fprintf(f, "\teap=%s\n", value);
531         free(value);
532 }
533
534
535 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
536 {
537         char field[20], *value;
538
539         snprintf(field, sizeof(field), "wep_key%d", idx);
540         value = wpa_config_get(ssid, field);
541         if (value) {
542                 fprintf(f, "\t%s=%s\n", field, value);
543                 free(value);
544         }
545 }
546
547
548 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
549 {
550         int i;
551
552 #define STR(t) write_str(f, #t, ssid)
553 #define INT(t) write_int(f, #t, ssid->t, 0)
554 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
555
556         STR(ssid);
557         INT(scan_ssid);
558         write_bssid(f, ssid);
559         write_psk(f, ssid);
560         write_proto(f, ssid);
561         write_key_mgmt(f, ssid);
562         write_pairwise(f, ssid);
563         write_group(f, ssid);
564         write_auth_alg(f, ssid);
565         write_eap(f, ssid);
566         STR(identity);
567         STR(anonymous_identity);
568         STR(eappsk);
569         STR(nai);
570         STR(password);
571         STR(ca_cert);
572         STR(client_cert);
573         STR(private_key);
574         STR(private_key_passwd);
575         STR(dh_file);
576         STR(subject_match);
577         STR(altsubject_match);
578         STR(ca_cert2);
579         STR(client_cert2);
580         STR(private_key2);
581         STR(private_key2_passwd);
582         STR(dh_file2);
583         STR(subject_match2);
584         STR(altsubject_match2);
585         STR(phase1);
586         STR(phase2);
587         STR(pcsc);
588         STR(pin);
589         STR(engine_id);
590         STR(key_id);
591         INT(engine);
592         INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
593         for (i = 0; i < 4; i++)
594                 write_wep_key(f, i, ssid);
595         INT(wep_tx_keyidx);
596         INT(priority);
597         INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
598         STR(pac_file);
599         INT(mode);
600         INT(proactive_key_caching);
601         INT(disabled);
602
603 #undef STR
604 #undef INT
605 #undef INT_DEF
606 }
607
608
609 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
610 {
611         unsigned char *encoded;
612
613         encoded = base64_encode(blob->data, blob->len, NULL);
614         if (encoded == NULL)
615                 return -1;
616
617         fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
618         free(encoded);
619         return 0;
620 }
621
622
623 int wpa_config_write(const char *name, struct wpa_config *config)
624 {
625         FILE *f;
626         struct wpa_ssid *ssid;
627         struct wpa_config_blob *blob;
628         int ret = 0;
629
630         wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
631
632
633         f = fopen(name, "w");
634         if (f == NULL) {
635                 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
636                 return -1;
637         }
638
639 #ifdef CONFIG_CTRL_IFACE
640         if (config->ctrl_interface)
641                 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
642 #ifndef CONFIG_CTRL_IFACE_UDP
643         if (config->ctrl_interface_gid_set) {
644                 fprintf(f, "ctrl_interface_group=%d\n",
645                         (int) config->ctrl_interface_gid);
646         }
647 #endif /* CONFIG_CTRL_IFACE_UDP */
648 #endif /* CONFIG_CTRL_IFACE */
649         if (config->eapol_version != DEFAULT_EAPOL_VERSION)
650                 fprintf(f, "eapol_version=%d\n", config->eapol_version);
651         if (config->ap_scan != DEFAULT_AP_SCAN)
652                 fprintf(f, "ap_scan=%d\n", config->ap_scan);
653         if (config->fast_reauth != DEFAULT_FAST_REAUTH)
654                 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
655         if (config->opensc_engine_path)
656                 fprintf(f, "opensc_engine_path=%s\n",
657                         config->opensc_engine_path);
658         if (config->pkcs11_engine_path)
659                 fprintf(f, "pkcs11_engine_path=%s\n",
660                         config->pkcs11_engine_path);
661         if (config->pkcs11_module_path)
662                 fprintf(f, "pkcs11_module_path=%s\n",
663                         config->pkcs11_module_path);
664         if (config->driver_param)
665                 fprintf(f, "driver_param=%s\n", config->driver_param);
666         if (config->dot11RSNAConfigPMKLifetime)
667                 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
668                         config->dot11RSNAConfigPMKLifetime);
669         if (config->dot11RSNAConfigPMKReauthThreshold)
670                 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
671                         config->dot11RSNAConfigPMKReauthThreshold);
672         if (config->dot11RSNAConfigSATimeout)
673                 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
674                         config->dot11RSNAConfigSATimeout);
675         if (config->update_config)
676                 fprintf(f, "update_config=%d\n", config->update_config);
677
678         for (ssid = config->ssid; ssid; ssid = ssid->next) {
679                 fprintf(f, "\nnetwork={\n");
680                 wpa_config_write_network(f, ssid);
681                 fprintf(f, "}\n");
682         }
683
684         for (blob = config->blobs; blob; blob = blob->next) {
685                 ret = wpa_config_write_blob(f, blob);
686                 if (ret)
687                         break;
688         }
689
690         fclose(f);
691
692         wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
693                    name, ret ? "un" : "");
694         return ret;
695 }