00001
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023
00024 #include "common.h"
00025 #include "wpa.h"
00026 #include "wpa_supplicant.h"
00027 #include "config.h"
00028 #include "base64.h"
00029
00030
00031 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line)
00032 {
00033 char *pos, *end, *sstart;
00034
00035 while (fgets(s, size, stream)) {
00036 (*line)++;
00037 s[size - 1] = '\0';
00038 pos = s;
00039
00040 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
00041 pos++;
00042 if (*pos == '#' || *pos == '\n' || *pos == '\0' ||
00043 *pos == '\r')
00044 continue;
00045
00046
00047
00048 sstart = strchr(pos, '"');
00049 if (sstart)
00050 sstart = strrchr(sstart + 1, '"');
00051 if (!sstart)
00052 sstart = pos;
00053 end = strchr(sstart, '#');
00054 if (end)
00055 *end-- = '\0';
00056 else
00057 end = pos + strlen(pos) - 1;
00058 while (end > pos &&
00059 (*end == '\n' || *end == ' ' || *end == '\t' ||
00060 *end == '\r')) {
00061 *end-- = '\0';
00062 }
00063 if (*pos == '\0')
00064 continue;
00065
00066 return pos;
00067 }
00068
00069 return NULL;
00070 }
00071
00072
00073 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
00074 {
00075 struct wpa_ssid *ssid;
00076 int errors = 0, end = 0;
00077 char buf[256], *pos, *pos2;
00078
00079 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
00080 *line);
00081 ssid = (struct wpa_ssid *) malloc(sizeof(*ssid));
00082 if (ssid == NULL)
00083 return NULL;
00084 memset(ssid, 0, sizeof(*ssid));
00085 ssid->id = id;
00086
00087 wpa_config_set_network_defaults(ssid);
00088
00089 while ((pos = wpa_config_get_line(buf, sizeof(buf), f, line))) {
00090 if (strcmp(pos, "}") == 0) {
00091 end = 1;
00092 break;
00093 }
00094
00095 pos2 = strchr(pos, '=');
00096 if (pos2 == NULL) {
00097 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
00098 "'%s'.", *line, pos);
00099 errors++;
00100 continue;
00101 }
00102
00103 *pos2++ = '\0';
00104 if (*pos2 == '"') {
00105 if (strchr(pos2 + 1, '"') == NULL) {
00106 wpa_printf(MSG_ERROR, "Line %d: invalid "
00107 "quotation '%s'.", *line, pos2);
00108 errors++;
00109 continue;
00110 }
00111 }
00112
00113 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
00114 errors++;
00115 }
00116
00117 if (!end) {
00118 wpa_printf(MSG_ERROR, "Line %d: network block was not "
00119 "terminated properly.", *line);
00120 errors++;
00121 }
00122
00123 if (ssid->passphrase) {
00124 if (ssid->psk_set) {
00125 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
00126 "passphrase configured.", *line);
00127 errors++;
00128 }
00129 wpa_config_update_psk(ssid);
00130 }
00131
00132 if ((ssid->key_mgmt & WPA_KEY_MGMT_PSK) && !ssid->psk_set) {
00133 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
00134 "management, but no PSK configured.", *line);
00135 errors++;
00136 }
00137
00138 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
00139 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP)) {
00140
00141 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
00142 " list since it was not allowed for pairwise "
00143 "cipher", *line);
00144 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
00145 }
00146
00147 if (errors) {
00148 wpa_config_free_ssid(ssid);
00149 ssid = NULL;
00150 }
00151
00152 return ssid;
00153 }
00154
00155
00156 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
00157 const char *name)
00158 {
00159 struct wpa_config_blob *blob;
00160 char buf[256], *pos;
00161 unsigned char *encoded = NULL, *nencoded;
00162 int end = 0;
00163 size_t encoded_len = 0, len;
00164
00165 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
00166 *line, name);
00167
00168 while ((pos = wpa_config_get_line(buf, sizeof(buf), f, line))) {
00169 if (strcmp(pos, "}") == 0) {
00170 end = 1;
00171 break;
00172 }
00173
00174 len = strlen(pos);
00175 nencoded = realloc(encoded, encoded_len + len);
00176 if (nencoded == NULL) {
00177 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
00178 "blob", *line);
00179 free(encoded);
00180 return NULL;
00181 }
00182 encoded = nencoded;
00183 memcpy(encoded + encoded_len, pos, len);
00184 encoded_len += len;
00185 }
00186
00187 if (!end) {
00188 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
00189 "properly", *line);
00190 free(encoded);
00191 return NULL;
00192 }
00193
00194 blob = malloc(sizeof(*blob));
00195 if (blob == NULL) {
00196 free(encoded);
00197 return NULL;
00198 }
00199 memset(blob, 0, sizeof(*blob));
00200 blob->name = strdup(name);
00201 blob->data = base64_decode(encoded, encoded_len, &blob->len);
00202 free(encoded);
00203
00204 if (blob->name == NULL || blob->data == NULL) {
00205 wpa_config_free_blob(blob);
00206 return NULL;
00207 }
00208
00209 return blob;
00210 }
00211
00212
00213 struct wpa_config * wpa_config_read(const char *name)
00214 {
00215 FILE *f;
00216 char buf[256], *pos;
00217 int errors = 0, line = 0;
00218 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
00219 struct wpa_config *config;
00220 int id = 0, prio;
00221
00222 config = wpa_config_alloc_empty(NULL, NULL);
00223 if (config == NULL)
00224 return NULL;
00225 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
00226 f = fopen(name, "r");
00227 if (f == NULL) {
00228 free(config);
00229 return NULL;
00230 }
00231
00232 while ((pos = wpa_config_get_line(buf, sizeof(buf), f, &line))) {
00233 if (strcmp(pos, "network={") == 0) {
00234 ssid = wpa_config_read_network(f, &line, id++);
00235 if (ssid == NULL) {
00236 wpa_printf(MSG_ERROR, "Line %d: failed to "
00237 "parse network block.", line);
00238 errors++;
00239 continue;
00240 }
00241 if (head == NULL) {
00242 head = tail = ssid;
00243 } else {
00244 tail->next = ssid;
00245 tail = ssid;
00246 }
00247 if (wpa_config_add_prio_network(config, ssid)) {
00248 wpa_printf(MSG_ERROR, "Line %d: failed to add "
00249 "network block to priority list.",
00250 line);
00251 errors++;
00252 continue;
00253 }
00254 } else if (strncmp(pos, "blob-base64-", 12) == 0) {
00255 char *name = pos + 12, *name_end;
00256 struct wpa_config_blob *blob;
00257
00258 name_end = strchr(name, '=');
00259 if (name_end == NULL) {
00260 wpa_printf(MSG_ERROR, "Line %d: no blob name "
00261 "terminator", line);
00262 errors++;
00263 continue;
00264 }
00265 *name_end = '\0';
00266
00267 blob = wpa_config_read_blob(f, &line, name);
00268 if (blob == NULL) {
00269 wpa_printf(MSG_ERROR, "Line %d: failed to read"
00270 " blob %s", line, name);
00271 errors++;
00272 continue;
00273 }
00274 wpa_config_set_blob(config, blob);
00275 #ifdef CONFIG_CTRL_IFACE
00276 } else if (strncmp(pos, "ctrl_interface=", 15) == 0) {
00277 free(config->ctrl_interface);
00278 config->ctrl_interface = strdup(pos + 15);
00279 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'",
00280 config->ctrl_interface);
00281 #ifndef CONFIG_CTRL_IFACE_UDP
00282 } else if (strncmp(pos, "ctrl_interface_group=", 21) == 0) {
00283 struct group *grp;
00284 char *endp;
00285 const char *group = pos + 21;
00286
00287 grp = getgrnam(group);
00288 if (grp) {
00289 config->ctrl_interface_gid = grp->gr_gid;
00290 config->ctrl_interface_gid_set = 1;
00291 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
00292 " (from group name '%s')",
00293 (int) config->ctrl_interface_gid,
00294 group);
00295 continue;
00296 }
00297
00298
00299 config->ctrl_interface_gid = strtol(group, &endp, 10);
00300 if (*group == '\0' || *endp != '\0') {
00301 wpa_printf(MSG_DEBUG, "Line %d: Invalid group "
00302 "'%s'", line, group);
00303 errors++;
00304 continue;
00305 }
00306 config->ctrl_interface_gid_set = 1;
00307 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
00308 (int) config->ctrl_interface_gid);
00309 #endif
00310 #endif
00311 } else if (strncmp(pos, "eapol_version=", 14) == 0) {
00312 config->eapol_version = atoi(pos + 14);
00313 if (config->eapol_version < 1 ||
00314 config->eapol_version > 2) {
00315 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL "
00316 "version (%d): '%s'.",
00317 line, config->eapol_version, pos);
00318 errors++;
00319 continue;
00320 }
00321 wpa_printf(MSG_DEBUG, "eapol_version=%d",
00322 config->eapol_version);
00323 } else if (strncmp(pos, "ap_scan=", 8) == 0) {
00324 config->ap_scan = atoi(pos + 8);
00325 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
00326 } else if (strncmp(pos, "fast_reauth=", 12) == 0) {
00327 config->fast_reauth = atoi(pos + 12);
00328 wpa_printf(MSG_DEBUG, "fast_reauth=%d",
00329 config->fast_reauth);
00330 } else if (strncmp(pos, "opensc_engine_path=", 19) == 0) {
00331 free(config->opensc_engine_path);
00332 config->opensc_engine_path = strdup(pos + 19);
00333 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
00334 config->opensc_engine_path);
00335 } else if (strncmp(pos, "pkcs11_engine_path=", 19) == 0) {
00336 free(config->pkcs11_engine_path);
00337 config->pkcs11_engine_path = strdup(pos + 19);
00338 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
00339 config->pkcs11_engine_path);
00340 } else if (strncmp(pos, "pkcs11_module_path=", 19) == 0) {
00341 free(config->pkcs11_module_path);
00342 config->pkcs11_module_path = strdup(pos + 19);
00343 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
00344 config->pkcs11_module_path);
00345 } else if (strncmp(pos, "driver_param=", 13) == 0) {
00346 free(config->driver_param);
00347 config->driver_param = strdup(pos + 13);
00348 wpa_printf(MSG_DEBUG, "driver_param='%s'",
00349 config->driver_param);
00350 } else if (strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) ==
00351 0) {
00352 config->dot11RSNAConfigPMKLifetime = atoi(pos + 27);
00353 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
00354 config->dot11RSNAConfigPMKLifetime);
00355 } else if (strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=",
00356 34) ==
00357 0) {
00358 config->dot11RSNAConfigPMKReauthThreshold =
00359 atoi(pos + 34);
00360 wpa_printf(MSG_DEBUG,
00361 "dot11RSNAConfigPMKReauthThreshold=%d",
00362 config->dot11RSNAConfigPMKReauthThreshold);
00363 } else if (strncmp(pos, "dot11RSNAConfigSATimeout=", 25) ==
00364 0) {
00365 config->dot11RSNAConfigSATimeout = atoi(pos + 25);
00366 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
00367 config->dot11RSNAConfigSATimeout);
00368 } else if (strncmp(pos, "update_config=", 14) == 0) {
00369 config->update_config = atoi(pos + 14);
00370 wpa_printf(MSG_DEBUG, "update_config=%d",
00371 config->update_config);
00372 } else {
00373 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
00374 "line '%s'.", line, pos);
00375 errors++;
00376 continue;
00377 }
00378 }
00379
00380 fclose(f);
00381
00382 config->ssid = head;
00383 for (prio = 0; prio < config->num_prio; prio++) {
00384 ssid = config->pssid[prio];
00385 wpa_printf(MSG_DEBUG, "Priority group %d",
00386 ssid->priority);
00387 while (ssid) {
00388 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
00389 ssid->id,
00390 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
00391 ssid = ssid->pnext;
00392 }
00393 }
00394 if (errors) {
00395 wpa_config_free(config);
00396 config = NULL;
00397 head = NULL;
00398 }
00399
00400 return config;
00401 }
00402
00403
00404 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
00405 {
00406 char *value = wpa_config_get(ssid, field);
00407 if (value == NULL)
00408 return;
00409 fprintf(f, "\t%s=%s\n", field, value);
00410 free(value);
00411 }
00412
00413
00414 static void write_int(FILE *f, const char *field, int value, int def)
00415 {
00416 if (value == def)
00417 return;
00418 fprintf(f, "\t%s=%d\n", field, value);
00419 }
00420
00421
00422 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
00423 {
00424 char *value = wpa_config_get(ssid, "bssid");
00425 if (value == NULL)
00426 return;
00427 fprintf(f, "\tbssid=%s\n", value);
00428 free(value);
00429 }
00430
00431
00432 static void write_psk(FILE *f, struct wpa_ssid *ssid)
00433 {
00434 char *value = wpa_config_get(ssid, "psk");
00435 if (value == NULL)
00436 return;
00437 fprintf(f, "\tpsk=%s\n", value);
00438 free(value);
00439 }
00440
00441
00442 static void write_proto(FILE *f, struct wpa_ssid *ssid)
00443 {
00444 char *value;
00445
00446 if (ssid->proto == DEFAULT_PROTO)
00447 return;
00448
00449 value = wpa_config_get(ssid, "proto");
00450 if (value == NULL)
00451 return;
00452 if (value[0])
00453 fprintf(f, "\tproto=%s\n", value);
00454 free(value);
00455 }
00456
00457
00458 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
00459 {
00460 char *value;
00461
00462 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
00463 return;
00464
00465 value = wpa_config_get(ssid, "key_mgmt");
00466 if (value == NULL)
00467 return;
00468 if (value[0])
00469 fprintf(f, "\tkey_mgmt=%s\n", value);
00470 free(value);
00471 }
00472
00473
00474 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
00475 {
00476 char *value;
00477
00478 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
00479 return;
00480
00481 value = wpa_config_get(ssid, "pairwise");
00482 if (value == NULL)
00483 return;
00484 if (value[0])
00485 fprintf(f, "\tpairwise=%s\n", value);
00486 free(value);
00487 }
00488
00489
00490 static void write_group(FILE *f, struct wpa_ssid *ssid)
00491 {
00492 char *value;
00493
00494 if (ssid->group_cipher == DEFAULT_GROUP)
00495 return;
00496
00497 value = wpa_config_get(ssid, "group");
00498 if (value == NULL)
00499 return;
00500 if (value[0])
00501 fprintf(f, "\tgroup=%s\n", value);
00502 free(value);
00503 }
00504
00505
00506 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
00507 {
00508 char *value;
00509
00510 if (ssid->auth_alg == 0)
00511 return;
00512
00513 value = wpa_config_get(ssid, "auth_alg");
00514 if (value == NULL)
00515 return;
00516 if (value[0])
00517 fprintf(f, "\tauth_alg=%s\n", value);
00518 free(value);
00519 }
00520
00521
00522 static void write_eap(FILE *f, struct wpa_ssid *ssid)
00523 {
00524 char *value;
00525
00526 value = wpa_config_get(ssid, "eap");
00527 if (value == NULL)
00528 return;
00529
00530 if (value[0])
00531 fprintf(f, "\teap=%s\n", value);
00532 free(value);
00533 }
00534
00535
00536 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
00537 {
00538 char field[20], *value;
00539
00540 snprintf(field, sizeof(field), "wep_key%d", idx);
00541 value = wpa_config_get(ssid, field);
00542 if (value) {
00543 fprintf(f, "\t%s=%s\n", field, value);
00544 free(value);
00545 }
00546 }
00547
00548
00549 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
00550 {
00551 int i;
00552
00553 #define STR(t) write_str(f, #t, ssid)
00554 #define INT(t) write_int(f, #t, ssid->t, 0)
00555 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
00556
00557 STR(ssid);
00558 INT(scan_ssid);
00559 write_bssid(f, ssid);
00560 write_psk(f, ssid);
00561 write_proto(f, ssid);
00562 write_key_mgmt(f, ssid);
00563 write_pairwise(f, ssid);
00564 write_group(f, ssid);
00565 write_auth_alg(f, ssid);
00566 write_eap(f, ssid);
00567 STR(identity);
00568 STR(anonymous_identity);
00569 STR(eappsk);
00570 STR(nai);
00571 STR(password);
00572 STR(ca_cert);
00573 STR(client_cert);
00574 STR(private_key);
00575 STR(private_key_passwd);
00576 STR(dh_file);
00577 STR(subject_match);
00578 STR(altsubject_match);
00579 STR(ca_cert2);
00580 STR(client_cert2);
00581 STR(private_key2);
00582 STR(private_key2_passwd);
00583 STR(dh_file2);
00584 STR(subject_match2);
00585 STR(altsubject_match2);
00586 STR(phase1);
00587 STR(phase2);
00588 STR(pcsc);
00589 STR(pin);
00590 STR(engine_id);
00591 STR(key_id);
00592 INT(engine);
00593 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
00594 for (i = 0; i < 4; i++)
00595 write_wep_key(f, i, ssid);
00596 INT(wep_tx_keyidx);
00597 INT(priority);
00598 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
00599 STR(pac_file);
00600 INT(mode);
00601 INT(proactive_key_caching);
00602 INT(disabled);
00603
00604 #undef STR
00605 #undef INT
00606 #undef INT_DEF
00607 }
00608
00609
00610 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
00611 {
00612 unsigned char *encoded;
00613
00614 encoded = base64_encode(blob->data, blob->len, NULL);
00615 if (encoded == NULL)
00616 return -1;
00617
00618 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
00619 free(encoded);
00620 return 0;
00621 }
00622
00623
00624 int wpa_config_write(const char *name, struct wpa_config *config)
00625 {
00626 FILE *f;
00627 struct wpa_ssid *ssid;
00628 struct wpa_config_blob *blob;
00629 int ret = 0;
00630
00631 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
00632
00633
00634 f = fopen(name, "w");
00635 if (f == NULL) {
00636 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
00637 return -1;
00638 }
00639
00640 #ifdef CONFIG_CTRL_IFACE
00641 if (config->ctrl_interface)
00642 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
00643 #ifndef CONFIG_CTRL_IFACE_UDP
00644 if (config->ctrl_interface_gid_set) {
00645 fprintf(f, "ctrl_interface_group=%d\n",
00646 (int) config->ctrl_interface_gid);
00647 }
00648 #endif
00649 #endif
00650 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
00651 fprintf(f, "eapol_version=%d\n", config->eapol_version);
00652 if (config->ap_scan != DEFAULT_AP_SCAN)
00653 fprintf(f, "ap_scan=%d\n", config->ap_scan);
00654 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
00655 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
00656 if (config->opensc_engine_path)
00657 fprintf(f, "opensc_engine_path=%s\n",
00658 config->opensc_engine_path);
00659 if (config->pkcs11_engine_path)
00660 fprintf(f, "pkcs11_engine_path=%s\n",
00661 config->pkcs11_engine_path);
00662 if (config->pkcs11_module_path)
00663 fprintf(f, "pkcs11_module_path=%s\n",
00664 config->pkcs11_module_path);
00665 if (config->driver_param)
00666 fprintf(f, "driver_param=%s\n", config->driver_param);
00667 if (config->dot11RSNAConfigPMKLifetime)
00668 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
00669 config->dot11RSNAConfigPMKLifetime);
00670 if (config->dot11RSNAConfigPMKReauthThreshold)
00671 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
00672 config->dot11RSNAConfigPMKReauthThreshold);
00673 if (config->dot11RSNAConfigSATimeout)
00674 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
00675 config->dot11RSNAConfigSATimeout);
00676 if (config->update_config)
00677 fprintf(f, "update_config=%d\n", config->update_config);
00678
00679 for (ssid = config->ssid; ssid; ssid = ssid->next) {
00680 fprintf(f, "\nnetwork={\n");
00681 wpa_config_write_network(f, ssid);
00682 fprintf(f, "}\n");
00683 }
00684
00685 for (blob = config->blobs; blob; blob = blob->next) {
00686 ret = wpa_config_write_blob(f, blob);
00687 if (ret)
00688 break;
00689 }
00690
00691 fclose(f);
00692
00693 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
00694 name, ret ? "un" : "");
00695 return ret;
00696 }
00697