]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant / Configuration backend: text file | |
3 | * Copyright (c) 2003-2007, 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_peer/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 | WPA_KEY_MGMT_FT_PSK)) && | |
108 | !ssid->psk_set) { | |
109 | wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key " | |
110 | "management, but no PSK configured.", line); | |
111 | errors++; | |
112 | } | |
113 | ||
114 | if ((ssid->group_cipher & WPA_CIPHER_CCMP) && | |
115 | !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) && | |
116 | !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) { | |
117 | /* Group cipher cannot be stronger than the pairwise cipher. */ | |
118 | wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher" | |
119 | " list since it was not allowed for pairwise " | |
120 | "cipher", line); | |
121 | ssid->group_cipher &= ~WPA_CIPHER_CCMP; | |
122 | } | |
123 | ||
124 | return errors; | |
125 | } | |
126 | ||
127 | ||
128 | static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id) | |
129 | { | |
130 | struct wpa_ssid *ssid; | |
131 | int errors = 0, end = 0; | |
132 | char buf[256], *pos, *pos2; | |
133 | ||
134 | wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block", | |
135 | *line); | |
136 | ssid = os_zalloc(sizeof(*ssid)); | |
137 | if (ssid == NULL) | |
138 | return NULL; | |
139 | ssid->id = id; | |
140 | ||
141 | wpa_config_set_network_defaults(ssid); | |
142 | ||
143 | while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { | |
144 | if (os_strcmp(pos, "}") == 0) { | |
145 | end = 1; | |
146 | break; | |
147 | } | |
148 | ||
149 | pos2 = os_strchr(pos, '='); | |
150 | if (pos2 == NULL) { | |
151 | wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line " | |
152 | "'%s'.", *line, pos); | |
153 | errors++; | |
154 | continue; | |
155 | } | |
156 | ||
157 | *pos2++ = '\0'; | |
158 | if (*pos2 == '"') { | |
159 | if (os_strchr(pos2 + 1, '"') == NULL) { | |
160 | wpa_printf(MSG_ERROR, "Line %d: invalid " | |
161 | "quotation '%s'.", *line, pos2); | |
162 | errors++; | |
163 | continue; | |
164 | } | |
165 | } | |
166 | ||
167 | if (wpa_config_set(ssid, pos, pos2, *line) < 0) | |
168 | errors++; | |
169 | } | |
170 | ||
171 | if (!end) { | |
172 | wpa_printf(MSG_ERROR, "Line %d: network block was not " | |
173 | "terminated properly.", *line); | |
174 | errors++; | |
175 | } | |
176 | ||
177 | errors += wpa_config_validate_network(ssid, *line); | |
178 | ||
179 | if (errors) { | |
180 | wpa_config_free_ssid(ssid); | |
181 | ssid = NULL; | |
182 | } | |
183 | ||
184 | return ssid; | |
185 | } | |
186 | ||
187 | ||
188 | #ifndef CONFIG_NO_CONFIG_BLOBS | |
189 | static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line, | |
190 | const char *name) | |
191 | { | |
192 | struct wpa_config_blob *blob; | |
193 | char buf[256], *pos; | |
194 | unsigned char *encoded = NULL, *nencoded; | |
195 | int end = 0; | |
196 | size_t encoded_len = 0, len; | |
197 | ||
198 | wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'", | |
199 | *line, name); | |
200 | ||
201 | while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) { | |
202 | if (os_strcmp(pos, "}") == 0) { | |
203 | end = 1; | |
204 | break; | |
205 | } | |
206 | ||
207 | len = os_strlen(pos); | |
208 | nencoded = os_realloc(encoded, encoded_len + len); | |
209 | if (nencoded == NULL) { | |
210 | wpa_printf(MSG_ERROR, "Line %d: not enough memory for " | |
211 | "blob", *line); | |
212 | os_free(encoded); | |
213 | return NULL; | |
214 | } | |
215 | encoded = nencoded; | |
216 | os_memcpy(encoded + encoded_len, pos, len); | |
217 | encoded_len += len; | |
218 | } | |
219 | ||
220 | if (!end) { | |
221 | wpa_printf(MSG_ERROR, "Line %d: blob was not terminated " | |
222 | "properly", *line); | |
223 | os_free(encoded); | |
224 | return NULL; | |
225 | } | |
226 | ||
227 | blob = os_zalloc(sizeof(*blob)); | |
228 | if (blob == NULL) { | |
229 | os_free(encoded); | |
230 | return NULL; | |
231 | } | |
232 | blob->name = os_strdup(name); | |
233 | blob->data = base64_decode(encoded, encoded_len, &blob->len); | |
234 | os_free(encoded); | |
235 | ||
236 | if (blob->name == NULL || blob->data == NULL) { | |
237 | wpa_config_free_blob(blob); | |
238 | return NULL; | |
239 | } | |
240 | ||
241 | return blob; | |
242 | } | |
243 | ||
244 | ||
245 | static int wpa_config_process_blob(struct wpa_config *config, FILE *f, | |
246 | int *line, char *bname) | |
247 | { | |
248 | char *name_end; | |
249 | struct wpa_config_blob *blob; | |
250 | ||
251 | name_end = os_strchr(bname, '='); | |
252 | if (name_end == NULL) { | |
253 | wpa_printf(MSG_ERROR, "Line %d: no blob name terminator", | |
254 | *line); | |
255 | return -1; | |
256 | } | |
257 | *name_end = '\0'; | |
258 | ||
259 | blob = wpa_config_read_blob(f, line, bname); | |
260 | if (blob == NULL) { | |
261 | wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s", | |
262 | *line, bname); | |
263 | return -1; | |
264 | } | |
265 | wpa_config_set_blob(config, blob); | |
266 | return 0; | |
267 | } | |
268 | #endif /* CONFIG_NO_CONFIG_BLOBS */ | |
269 | ||
270 | ||
271 | #ifdef CONFIG_CTRL_IFACE | |
272 | static int wpa_config_process_ctrl_interface(struct wpa_config *config, | |
273 | char *pos) | |
274 | { | |
275 | os_free(config->ctrl_interface); | |
276 | config->ctrl_interface = os_strdup(pos); | |
277 | wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface); | |
278 | return 0; | |
279 | } | |
280 | ||
281 | ||
282 | static int wpa_config_process_ctrl_interface_group(struct wpa_config *config, | |
283 | char *pos) | |
284 | { | |
285 | os_free(config->ctrl_interface_group); | |
286 | config->ctrl_interface_group = os_strdup(pos); | |
287 | wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)", | |
288 | config->ctrl_interface_group); | |
289 | return 0; | |
290 | } | |
291 | #endif /* CONFIG_CTRL_IFACE */ | |
292 | ||
293 | ||
294 | static int wpa_config_process_eapol_version(struct wpa_config *config, | |
295 | int line, char *pos) | |
296 | { | |
297 | config->eapol_version = atoi(pos); | |
298 | if (config->eapol_version < 1 || config->eapol_version > 2) { | |
299 | wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): " | |
300 | "'%s'.", line, config->eapol_version, pos); | |
301 | return -1; | |
302 | } | |
303 | wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version); | |
304 | return 0; | |
305 | } | |
306 | ||
307 | ||
308 | static int wpa_config_process_ap_scan(struct wpa_config *config, char *pos) | |
309 | { | |
310 | config->ap_scan = atoi(pos); | |
311 | wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan); | |
312 | return 0; | |
313 | } | |
314 | ||
315 | ||
316 | static int wpa_config_process_fast_reauth(struct wpa_config *config, char *pos) | |
317 | { | |
318 | config->fast_reauth = atoi(pos); | |
319 | wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth); | |
320 | return 0; | |
321 | } | |
322 | ||
323 | ||
324 | #ifdef EAP_TLS_OPENSSL | |
325 | ||
326 | static int wpa_config_process_opensc_engine_path(struct wpa_config *config, | |
327 | char *pos) | |
328 | { | |
329 | os_free(config->opensc_engine_path); | |
330 | config->opensc_engine_path = os_strdup(pos); | |
331 | wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'", | |
332 | config->opensc_engine_path); | |
333 | return 0; | |
334 | } | |
335 | ||
336 | ||
337 | static int wpa_config_process_pkcs11_engine_path(struct wpa_config *config, | |
338 | char *pos) | |
339 | { | |
340 | os_free(config->pkcs11_engine_path); | |
341 | config->pkcs11_engine_path = os_strdup(pos); | |
342 | wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'", | |
343 | config->pkcs11_engine_path); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | ||
348 | static int wpa_config_process_pkcs11_module_path(struct wpa_config *config, | |
349 | char *pos) | |
350 | { | |
351 | os_free(config->pkcs11_module_path); | |
352 | config->pkcs11_module_path = os_strdup(pos); | |
353 | wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'", | |
354 | config->pkcs11_module_path); | |
355 | return 0; | |
356 | } | |
357 | ||
358 | #endif /* EAP_TLS_OPENSSL */ | |
359 | ||
360 | ||
361 | static int wpa_config_process_driver_param(struct wpa_config *config, | |
362 | char *pos) | |
363 | { | |
364 | os_free(config->driver_param); | |
365 | config->driver_param = os_strdup(pos); | |
366 | wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param); | |
367 | return 0; | |
368 | } | |
369 | ||
370 | ||
371 | static int wpa_config_process_pmk_lifetime(struct wpa_config *config, | |
372 | char *pos) | |
373 | { | |
374 | config->dot11RSNAConfigPMKLifetime = atoi(pos); | |
375 | wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d", | |
376 | config->dot11RSNAConfigPMKLifetime); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | ||
381 | static int wpa_config_process_pmk_reauth_threshold(struct wpa_config *config, | |
382 | char *pos) | |
383 | { | |
384 | config->dot11RSNAConfigPMKReauthThreshold = atoi(pos); | |
385 | wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d", | |
386 | config->dot11RSNAConfigPMKReauthThreshold); | |
387 | return 0; | |
388 | } | |
389 | ||
390 | ||
391 | static int wpa_config_process_sa_timeout(struct wpa_config *config, char *pos) | |
392 | { | |
393 | config->dot11RSNAConfigSATimeout = atoi(pos); | |
394 | wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d", | |
395 | config->dot11RSNAConfigSATimeout); | |
396 | return 0; | |
397 | } | |
398 | ||
399 | ||
400 | #ifndef CONFIG_NO_CONFIG_WRITE | |
401 | static int wpa_config_process_update_config(struct wpa_config *config, | |
402 | char *pos) | |
403 | { | |
404 | config->update_config = atoi(pos); | |
405 | wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config); | |
406 | return 0; | |
407 | } | |
408 | #endif /* CONFIG_NO_CONFIG_WRITE */ | |
409 | ||
410 | ||
411 | static int wpa_config_process_load_dynamic_eap(int line, char *so) | |
412 | { | |
413 | int ret; | |
414 | wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so); | |
415 | ret = eap_peer_method_load(so); | |
416 | if (ret == -2) { | |
417 | wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not " | |
418 | "reloading."); | |
419 | } else if (ret) { | |
420 | wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP " | |
421 | "method '%s'.", line, so); | |
422 | return -1; | |
423 | } | |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
428 | ||
429 | static int wpa_config_process_global(struct wpa_config *config, char *pos, | |
430 | int line) | |
431 | { | |
432 | #ifdef CONFIG_CTRL_IFACE | |
433 | if (os_strncmp(pos, "ctrl_interface=", 15) == 0) | |
434 | return wpa_config_process_ctrl_interface(config, pos + 15); | |
435 | ||
436 | if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0) | |
437 | return wpa_config_process_ctrl_interface_group(config, | |
438 | pos + 21); | |
439 | #endif /* CONFIG_CTRL_IFACE */ | |
440 | ||
441 | if (os_strncmp(pos, "eapol_version=", 14) == 0) | |
442 | return wpa_config_process_eapol_version(config, line, | |
443 | pos + 14); | |
444 | ||
445 | if (os_strncmp(pos, "ap_scan=", 8) == 0) | |
446 | return wpa_config_process_ap_scan(config, pos + 8); | |
447 | ||
448 | if (os_strncmp(pos, "fast_reauth=", 12) == 0) | |
449 | return wpa_config_process_fast_reauth(config, pos + 12); | |
450 | ||
451 | #ifdef EAP_TLS_OPENSSL | |
452 | if (os_strncmp(pos, "opensc_engine_path=", 19) == 0) | |
453 | return wpa_config_process_opensc_engine_path(config, pos + 19); | |
454 | ||
455 | if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0) | |
456 | return wpa_config_process_pkcs11_engine_path(config, pos + 19); | |
457 | ||
458 | if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0) | |
459 | return wpa_config_process_pkcs11_module_path(config, pos + 19); | |
460 | #endif /* EAP_TLS_OPENSSL */ | |
461 | ||
462 | if (os_strncmp(pos, "driver_param=", 13) == 0) | |
463 | return wpa_config_process_driver_param(config, pos + 13); | |
464 | ||
465 | if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) == 0) | |
466 | return wpa_config_process_pmk_lifetime(config, pos + 27); | |
467 | ||
468 | if (os_strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=", 34) == 0) | |
469 | return wpa_config_process_pmk_reauth_threshold(config, | |
470 | pos + 34); | |
471 | ||
472 | if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) == 0) | |
473 | return wpa_config_process_sa_timeout(config, pos + 25); | |
474 | ||
475 | #ifndef CONFIG_NO_CONFIG_WRITE | |
476 | if (os_strncmp(pos, "update_config=", 14) == 0) | |
477 | return wpa_config_process_update_config(config, pos + 14); | |
478 | #endif /* CONFIG_NO_CONFIG_WRITE */ | |
479 | ||
480 | if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0) | |
481 | return wpa_config_process_load_dynamic_eap(line, pos + 17); | |
482 | ||
483 | return -1; | |
484 | } | |
485 | ||
486 | ||
487 | struct wpa_config * wpa_config_read(const char *name) | |
488 | { | |
489 | FILE *f; | |
490 | char buf[256], *pos; | |
491 | int errors = 0, line = 0; | |
492 | struct wpa_ssid *ssid, *tail = NULL, *head = NULL; | |
493 | struct wpa_config *config; | |
494 | int id = 0; | |
495 | ||
496 | config = wpa_config_alloc_empty(NULL, NULL); | |
497 | if (config == NULL) | |
498 | return NULL; | |
499 | wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name); | |
500 | f = fopen(name, "r"); | |
501 | if (f == NULL) { | |
502 | os_free(config); | |
503 | return NULL; | |
504 | } | |
505 | ||
506 | while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) { | |
507 | if (os_strcmp(pos, "network={") == 0) { | |
508 | ssid = wpa_config_read_network(f, &line, id++); | |
509 | if (ssid == NULL) { | |
510 | wpa_printf(MSG_ERROR, "Line %d: failed to " | |
511 | "parse network block.", line); | |
512 | errors++; | |
513 | continue; | |
514 | } | |
515 | if (head == NULL) { | |
516 | head = tail = ssid; | |
517 | } else { | |
518 | tail->next = ssid; | |
519 | tail = ssid; | |
520 | } | |
521 | if (wpa_config_add_prio_network(config, ssid)) { | |
522 | wpa_printf(MSG_ERROR, "Line %d: failed to add " | |
523 | "network block to priority list.", | |
524 | line); | |
525 | errors++; | |
526 | continue; | |
527 | } | |
528 | #ifndef CONFIG_NO_CONFIG_BLOBS | |
529 | } else if (os_strncmp(pos, "blob-base64-", 12) == 0) { | |
530 | if (wpa_config_process_blob(config, f, &line, pos + 12) | |
531 | < 0) { | |
532 | errors++; | |
533 | continue; | |
534 | } | |
535 | #endif /* CONFIG_NO_CONFIG_BLOBS */ | |
536 | } else if (wpa_config_process_global(config, pos, line) < 0) { | |
537 | wpa_printf(MSG_ERROR, "Line %d: Invalid configuration " | |
538 | "line '%s'.", line, pos); | |
539 | errors++; | |
540 | continue; | |
541 | } | |
542 | } | |
543 | ||
544 | fclose(f); | |
545 | ||
546 | config->ssid = head; | |
547 | wpa_config_debug_dump_networks(config); | |
548 | ||
549 | if (errors) { | |
550 | wpa_config_free(config); | |
551 | config = NULL; | |
552 | head = NULL; | |
553 | } | |
554 | ||
555 | return config; | |
556 | } | |
557 | ||
558 | ||
559 | #ifndef CONFIG_NO_CONFIG_WRITE | |
560 | ||
561 | static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid) | |
562 | { | |
563 | char *value = wpa_config_get(ssid, field); | |
564 | if (value == NULL) | |
565 | return; | |
566 | fprintf(f, "\t%s=%s\n", field, value); | |
567 | os_free(value); | |
568 | } | |
569 | ||
570 | ||
571 | static void write_int(FILE *f, const char *field, int value, int def) | |
572 | { | |
573 | if (value == def) | |
574 | return; | |
575 | fprintf(f, "\t%s=%d\n", field, value); | |
576 | } | |
577 | ||
578 | ||
579 | static void write_bssid(FILE *f, struct wpa_ssid *ssid) | |
580 | { | |
581 | char *value = wpa_config_get(ssid, "bssid"); | |
582 | if (value == NULL) | |
583 | return; | |
584 | fprintf(f, "\tbssid=%s\n", value); | |
585 | os_free(value); | |
586 | } | |
587 | ||
588 | ||
589 | static void write_psk(FILE *f, struct wpa_ssid *ssid) | |
590 | { | |
591 | char *value = wpa_config_get(ssid, "psk"); | |
592 | if (value == NULL) | |
593 | return; | |
594 | fprintf(f, "\tpsk=%s\n", value); | |
595 | os_free(value); | |
596 | } | |
597 | ||
598 | ||
599 | static void write_proto(FILE *f, struct wpa_ssid *ssid) | |
600 | { | |
601 | char *value; | |
602 | ||
603 | if (ssid->proto == DEFAULT_PROTO) | |
604 | return; | |
605 | ||
606 | value = wpa_config_get(ssid, "proto"); | |
607 | if (value == NULL) | |
608 | return; | |
609 | if (value[0]) | |
610 | fprintf(f, "\tproto=%s\n", value); | |
611 | os_free(value); | |
612 | } | |
613 | ||
614 | ||
615 | static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid) | |
616 | { | |
617 | char *value; | |
618 | ||
619 | if (ssid->key_mgmt == DEFAULT_KEY_MGMT) | |
620 | return; | |
621 | ||
622 | value = wpa_config_get(ssid, "key_mgmt"); | |
623 | if (value == NULL) | |
624 | return; | |
625 | if (value[0]) | |
626 | fprintf(f, "\tkey_mgmt=%s\n", value); | |
627 | os_free(value); | |
628 | } | |
629 | ||
630 | ||
631 | static void write_pairwise(FILE *f, struct wpa_ssid *ssid) | |
632 | { | |
633 | char *value; | |
634 | ||
635 | if (ssid->pairwise_cipher == DEFAULT_PAIRWISE) | |
636 | return; | |
637 | ||
638 | value = wpa_config_get(ssid, "pairwise"); | |
639 | if (value == NULL) | |
640 | return; | |
641 | if (value[0]) | |
642 | fprintf(f, "\tpairwise=%s\n", value); | |
643 | os_free(value); | |
644 | } | |
645 | ||
646 | ||
647 | static void write_group(FILE *f, struct wpa_ssid *ssid) | |
648 | { | |
649 | char *value; | |
650 | ||
651 | if (ssid->group_cipher == DEFAULT_GROUP) | |
652 | return; | |
653 | ||
654 | value = wpa_config_get(ssid, "group"); | |
655 | if (value == NULL) | |
656 | return; | |
657 | if (value[0]) | |
658 | fprintf(f, "\tgroup=%s\n", value); | |
659 | os_free(value); | |
660 | } | |
661 | ||
662 | ||
663 | static void write_auth_alg(FILE *f, struct wpa_ssid *ssid) | |
664 | { | |
665 | char *value; | |
666 | ||
667 | if (ssid->auth_alg == 0) | |
668 | return; | |
669 | ||
670 | value = wpa_config_get(ssid, "auth_alg"); | |
671 | if (value == NULL) | |
672 | return; | |
673 | if (value[0]) | |
674 | fprintf(f, "\tauth_alg=%s\n", value); | |
675 | os_free(value); | |
676 | } | |
677 | ||
678 | ||
679 | #ifdef IEEE8021X_EAPOL | |
680 | static void write_eap(FILE *f, struct wpa_ssid *ssid) | |
681 | { | |
682 | char *value; | |
683 | ||
684 | value = wpa_config_get(ssid, "eap"); | |
685 | if (value == NULL) | |
686 | return; | |
687 | ||
688 | if (value[0]) | |
689 | fprintf(f, "\teap=%s\n", value); | |
690 | os_free(value); | |
691 | } | |
692 | #endif /* IEEE8021X_EAPOL */ | |
693 | ||
694 | ||
695 | static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid) | |
696 | { | |
697 | char field[20], *value; | |
698 | int res; | |
699 | ||
700 | res = os_snprintf(field, sizeof(field), "wep_key%d", idx); | |
701 | if (res < 0 || (size_t) res >= sizeof(field)) | |
702 | return; | |
703 | value = wpa_config_get(ssid, field); | |
704 | if (value) { | |
705 | fprintf(f, "\t%s=%s\n", field, value); | |
706 | os_free(value); | |
707 | } | |
708 | } | |
709 | ||
710 | ||
711 | static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid) | |
712 | { | |
713 | int i; | |
714 | ||
715 | #define STR(t) write_str(f, #t, ssid) | |
716 | #define INT(t) write_int(f, #t, ssid->t, 0) | |
717 | #define INTe(t) write_int(f, #t, ssid->eap.t, 0) | |
718 | #define INT_DEF(t, def) write_int(f, #t, ssid->t, def) | |
719 | #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def) | |
720 | ||
721 | STR(ssid); | |
722 | INT(scan_ssid); | |
723 | write_bssid(f, ssid); | |
724 | write_psk(f, ssid); | |
725 | write_proto(f, ssid); | |
726 | write_key_mgmt(f, ssid); | |
727 | write_pairwise(f, ssid); | |
728 | write_group(f, ssid); | |
729 | write_auth_alg(f, ssid); | |
730 | #ifdef IEEE8021X_EAPOL | |
731 | write_eap(f, ssid); | |
732 | STR(identity); | |
733 | STR(anonymous_identity); | |
734 | STR(password); | |
735 | STR(ca_cert); | |
736 | STR(ca_path); | |
737 | STR(client_cert); | |
738 | STR(private_key); | |
739 | STR(private_key_passwd); | |
740 | STR(dh_file); | |
741 | STR(subject_match); | |
742 | STR(altsubject_match); | |
743 | STR(ca_cert2); | |
744 | STR(ca_path2); | |
745 | STR(client_cert2); | |
746 | STR(private_key2); | |
747 | STR(private_key2_passwd); | |
748 | STR(dh_file2); | |
749 | STR(subject_match2); | |
750 | STR(altsubject_match2); | |
751 | STR(phase1); | |
752 | STR(phase2); | |
753 | STR(pcsc); | |
754 | STR(pin); | |
755 | STR(engine_id); | |
756 | STR(key_id); | |
757 | INTe(engine); | |
758 | INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS); | |
759 | #endif /* IEEE8021X_EAPOL */ | |
760 | for (i = 0; i < 4; i++) | |
761 | write_wep_key(f, i, ssid); | |
762 | INT(wep_tx_keyidx); | |
763 | INT(priority); | |
764 | #ifdef IEEE8021X_EAPOL | |
765 | INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND); | |
766 | STR(pac_file); | |
767 | INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE); | |
768 | #endif /* IEEE8021X_EAPOL */ | |
769 | INT(mode); | |
770 | INT(proactive_key_caching); | |
771 | INT(disabled); | |
772 | INT(peerkey); | |
773 | #ifdef CONFIG_IEEE80211W | |
774 | INT(ieee80211w); | |
775 | #endif /* CONFIG_IEEE80211W */ | |
776 | STR(id_str); | |
777 | ||
778 | #undef STR | |
779 | #undef INT | |
780 | #undef INT_DEF | |
781 | } | |
782 | ||
783 | ||
784 | #ifndef CONFIG_NO_CONFIG_BLOBS | |
785 | static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob) | |
786 | { | |
787 | unsigned char *encoded; | |
788 | ||
789 | encoded = base64_encode(blob->data, blob->len, NULL); | |
790 | if (encoded == NULL) | |
791 | return -1; | |
792 | ||
793 | fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded); | |
794 | os_free(encoded); | |
795 | return 0; | |
796 | } | |
797 | #endif /* CONFIG_NO_CONFIG_BLOBS */ | |
798 | ||
799 | ||
800 | static void wpa_config_write_global(FILE *f, struct wpa_config *config) | |
801 | { | |
802 | #ifdef CONFIG_CTRL_IFACE | |
803 | if (config->ctrl_interface) | |
804 | fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface); | |
805 | if (config->ctrl_interface_group) | |
806 | fprintf(f, "ctrl_interface_group=%s\n", | |
807 | config->ctrl_interface_group); | |
808 | #endif /* CONFIG_CTRL_IFACE */ | |
809 | if (config->eapol_version != DEFAULT_EAPOL_VERSION) | |
810 | fprintf(f, "eapol_version=%d\n", config->eapol_version); | |
811 | if (config->ap_scan != DEFAULT_AP_SCAN) | |
812 | fprintf(f, "ap_scan=%d\n", config->ap_scan); | |
813 | if (config->fast_reauth != DEFAULT_FAST_REAUTH) | |
814 | fprintf(f, "fast_reauth=%d\n", config->fast_reauth); | |
815 | #ifdef EAP_TLS_OPENSSL | |
816 | if (config->opensc_engine_path) | |
817 | fprintf(f, "opensc_engine_path=%s\n", | |
818 | config->opensc_engine_path); | |
819 | if (config->pkcs11_engine_path) | |
820 | fprintf(f, "pkcs11_engine_path=%s\n", | |
821 | config->pkcs11_engine_path); | |
822 | if (config->pkcs11_module_path) | |
823 | fprintf(f, "pkcs11_module_path=%s\n", | |
824 | config->pkcs11_module_path); | |
825 | #endif /* EAP_TLS_OPENSSL */ | |
826 | if (config->driver_param) | |
827 | fprintf(f, "driver_param=%s\n", config->driver_param); | |
828 | if (config->dot11RSNAConfigPMKLifetime) | |
829 | fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n", | |
830 | config->dot11RSNAConfigPMKLifetime); | |
831 | if (config->dot11RSNAConfigPMKReauthThreshold) | |
832 | fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n", | |
833 | config->dot11RSNAConfigPMKReauthThreshold); | |
834 | if (config->dot11RSNAConfigSATimeout) | |
835 | fprintf(f, "dot11RSNAConfigSATimeout=%d\n", | |
836 | config->dot11RSNAConfigSATimeout); | |
837 | if (config->update_config) | |
838 | fprintf(f, "update_config=%d\n", config->update_config); | |
839 | } | |
840 | ||
841 | #endif /* CONFIG_NO_CONFIG_WRITE */ | |
842 | ||
843 | ||
844 | int wpa_config_write(const char *name, struct wpa_config *config) | |
845 | { | |
846 | #ifndef CONFIG_NO_CONFIG_WRITE | |
847 | FILE *f; | |
848 | struct wpa_ssid *ssid; | |
849 | #ifndef CONFIG_NO_CONFIG_BLOBS | |
850 | struct wpa_config_blob *blob; | |
851 | #endif /* CONFIG_NO_CONFIG_BLOBS */ | |
852 | int ret = 0; | |
853 | ||
854 | wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name); | |
855 | ||
856 | f = fopen(name, "w"); | |
857 | if (f == NULL) { | |
858 | wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name); | |
859 | return -1; | |
860 | } | |
861 | ||
862 | wpa_config_write_global(f, config); | |
863 | ||
864 | for (ssid = config->ssid; ssid; ssid = ssid->next) { | |
865 | fprintf(f, "\nnetwork={\n"); | |
866 | wpa_config_write_network(f, ssid); | |
867 | fprintf(f, "}\n"); | |
868 | } | |
869 | ||
870 | #ifndef CONFIG_NO_CONFIG_BLOBS | |
871 | for (blob = config->blobs; blob; blob = blob->next) { | |
872 | ret = wpa_config_write_blob(f, blob); | |
873 | if (ret) | |
874 | break; | |
875 | } | |
876 | #endif /* CONFIG_NO_CONFIG_BLOBS */ | |
877 | ||
878 | fclose(f); | |
879 | ||
880 | wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully", | |
881 | name, ret ? "un" : ""); | |
882 | return ret; | |
883 | #else /* CONFIG_NO_CONFIG_WRITE */ | |
884 | return -1; | |
885 | #endif /* CONFIG_NO_CONFIG_WRITE */ | |
886 | } |