config_file.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * WPA Supplicant / Configuration backend: text file
  3. * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  8. * This file implements a configuration backend for text files. All the
  9. * configuration information is stored in a text file that uses a format
  10. * described in the sample configuration file, wpa_supplicant.conf.
  11. */
  12. #include "includes.h"
  13. #include "common.h"
  14. #include "config.h"
  15. #include "base64.h"
  16. #include "uuid.h"
  17. #include "p2p/p2p.h"
  18. #include "eap_peer/eap_methods.h"
  19. #include "eap_peer/eap.h"
  20. static int newline_terminated(const char *buf, size_t buflen)
  21. {
  22. size_t len = os_strlen(buf);
  23. if (len == 0)
  24. return 0;
  25. if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
  26. buf[len - 1] != '\n')
  27. return 0;
  28. return 1;
  29. }
  30. static void skip_line_end(FILE *stream)
  31. {
  32. char buf[100];
  33. while (fgets(buf, sizeof(buf), stream)) {
  34. buf[sizeof(buf) - 1] = '\0';
  35. if (newline_terminated(buf, sizeof(buf)))
  36. return;
  37. }
  38. }
  39. /**
  40. * wpa_config_get_line - Read the next configuration file line
  41. * @s: Buffer for the line
  42. * @size: The buffer length
  43. * @stream: File stream to read from
  44. * @line: Pointer to a variable storing the file line number
  45. * @_pos: Buffer for the pointer to the beginning of data on the text line or
  46. * %NULL if not needed (returned value used instead)
  47. * Returns: Pointer to the beginning of data on the text line or %NULL if no
  48. * more text lines are available.
  49. *
  50. * This function reads the next non-empty line from the configuration file and
  51. * removes comments. The returned string is guaranteed to be null-terminated.
  52. */
  53. static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
  54. char **_pos)
  55. {
  56. char *pos, *end, *sstart;
  57. while (fgets(s, size, stream)) {
  58. (*line)++;
  59. s[size - 1] = '\0';
  60. if (!newline_terminated(s, size)) {
  61. /*
  62. * The line was truncated - skip rest of it to avoid
  63. * confusing error messages.
  64. */
  65. wpa_printf(MSG_INFO, "Long line in configuration file "
  66. "truncated");
  67. skip_line_end(stream);
  68. }
  69. pos = s;
  70. /* Skip white space from the beginning of line. */
  71. while (*pos == ' ' || *pos == '\t' || *pos == '\r')
  72. pos++;
  73. /* Skip comment lines and empty lines */
  74. if (*pos == '#' || *pos == '\n' || *pos == '\0')
  75. continue;
  76. /*
  77. * Remove # comments unless they are within a double quoted
  78. * string.
  79. */
  80. sstart = os_strchr(pos, '"');
  81. if (sstart)
  82. sstart = os_strrchr(sstart + 1, '"');
  83. if (!sstart)
  84. sstart = pos;
  85. end = os_strchr(sstart, '#');
  86. if (end)
  87. *end-- = '\0';
  88. else
  89. end = pos + os_strlen(pos) - 1;
  90. /* Remove trailing white space. */
  91. while (end > pos &&
  92. (*end == '\n' || *end == ' ' || *end == '\t' ||
  93. *end == '\r'))
  94. *end-- = '\0';
  95. if (*pos == '\0')
  96. continue;
  97. if (_pos)
  98. *_pos = pos;
  99. return pos;
  100. }
  101. if (_pos)
  102. *_pos = NULL;
  103. return NULL;
  104. }
  105. static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
  106. {
  107. int errors = 0;
  108. if (ssid->passphrase) {
  109. if (ssid->psk_set) {
  110. wpa_printf(MSG_ERROR, "Line %d: both PSK and "
  111. "passphrase configured.", line);
  112. errors++;
  113. }
  114. wpa_config_update_psk(ssid);
  115. }
  116. if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
  117. !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  118. !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
  119. /* Group cipher cannot be stronger than the pairwise cipher. */
  120. wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
  121. " list since it was not allowed for pairwise "
  122. "cipher", line);
  123. ssid->group_cipher &= ~WPA_CIPHER_CCMP;
  124. }
  125. return errors;
  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[2000], *pos, *pos2;
  132. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
  133. *line);
  134. ssid = os_zalloc(sizeof(*ssid));
  135. if (ssid == NULL)
  136. return NULL;
  137. ssid->id = id;
  138. wpa_config_set_network_defaults(ssid);
  139. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  140. if (os_strcmp(pos, "}") == 0) {
  141. end = 1;
  142. break;
  143. }
  144. pos2 = os_strchr(pos, '=');
  145. if (pos2 == NULL) {
  146. wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
  147. "'%s'.", *line, pos);
  148. errors++;
  149. continue;
  150. }
  151. *pos2++ = '\0';
  152. if (*pos2 == '"') {
  153. if (os_strchr(pos2 + 1, '"') == NULL) {
  154. wpa_printf(MSG_ERROR, "Line %d: invalid "
  155. "quotation '%s'.", *line, pos2);
  156. errors++;
  157. continue;
  158. }
  159. }
  160. if (wpa_config_set(ssid, pos, pos2, *line) < 0)
  161. errors++;
  162. }
  163. if (!end) {
  164. wpa_printf(MSG_ERROR, "Line %d: network block was not "
  165. "terminated properly.", *line);
  166. errors++;
  167. }
  168. errors += wpa_config_validate_network(ssid, *line);
  169. if (errors) {
  170. wpa_config_free_ssid(ssid);
  171. ssid = NULL;
  172. }
  173. return ssid;
  174. }
  175. static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
  176. {
  177. struct wpa_cred *cred;
  178. int errors = 0, end = 0;
  179. char buf[256], *pos, *pos2;
  180. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
  181. cred = os_zalloc(sizeof(*cred));
  182. if (cred == NULL)
  183. return NULL;
  184. cred->id = id;
  185. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  186. if (os_strcmp(pos, "}") == 0) {
  187. end = 1;
  188. break;
  189. }
  190. pos2 = os_strchr(pos, '=');
  191. if (pos2 == NULL) {
  192. wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
  193. "'%s'.", *line, pos);
  194. errors++;
  195. continue;
  196. }
  197. *pos2++ = '\0';
  198. if (*pos2 == '"') {
  199. if (os_strchr(pos2 + 1, '"') == NULL) {
  200. wpa_printf(MSG_ERROR, "Line %d: invalid "
  201. "quotation '%s'.", *line, pos2);
  202. errors++;
  203. continue;
  204. }
  205. }
  206. if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
  207. errors++;
  208. }
  209. if (!end) {
  210. wpa_printf(MSG_ERROR, "Line %d: cred block was not "
  211. "terminated properly.", *line);
  212. errors++;
  213. }
  214. if (errors) {
  215. wpa_config_free_cred(cred);
  216. cred = NULL;
  217. }
  218. return cred;
  219. }
  220. #ifndef CONFIG_NO_CONFIG_BLOBS
  221. static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
  222. const char *name)
  223. {
  224. struct wpa_config_blob *blob;
  225. char buf[256], *pos;
  226. unsigned char *encoded = NULL, *nencoded;
  227. int end = 0;
  228. size_t encoded_len = 0, len;
  229. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
  230. *line, name);
  231. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  232. if (os_strcmp(pos, "}") == 0) {
  233. end = 1;
  234. break;
  235. }
  236. len = os_strlen(pos);
  237. nencoded = os_realloc(encoded, encoded_len + len);
  238. if (nencoded == NULL) {
  239. wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
  240. "blob", *line);
  241. os_free(encoded);
  242. return NULL;
  243. }
  244. encoded = nencoded;
  245. os_memcpy(encoded + encoded_len, pos, len);
  246. encoded_len += len;
  247. }
  248. if (!end) {
  249. wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
  250. "properly", *line);
  251. os_free(encoded);
  252. return NULL;
  253. }
  254. blob = os_zalloc(sizeof(*blob));
  255. if (blob == NULL) {
  256. os_free(encoded);
  257. return NULL;
  258. }
  259. blob->name = os_strdup(name);
  260. blob->data = base64_decode(encoded, encoded_len, &blob->len);
  261. os_free(encoded);
  262. if (blob->name == NULL || blob->data == NULL) {
  263. wpa_config_free_blob(blob);
  264. return NULL;
  265. }
  266. return blob;
  267. }
  268. static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
  269. int *line, char *bname)
  270. {
  271. char *name_end;
  272. struct wpa_config_blob *blob;
  273. name_end = os_strchr(bname, '=');
  274. if (name_end == NULL) {
  275. wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
  276. *line);
  277. return -1;
  278. }
  279. *name_end = '\0';
  280. blob = wpa_config_read_blob(f, line, bname);
  281. if (blob == NULL) {
  282. wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
  283. *line, bname);
  284. return -1;
  285. }
  286. wpa_config_set_blob(config, blob);
  287. return 0;
  288. }
  289. #endif /* CONFIG_NO_CONFIG_BLOBS */
  290. struct wpa_config * wpa_config_read(const char *name)
  291. {
  292. FILE *f;
  293. char buf[512], *pos;
  294. int errors = 0, line = 0;
  295. struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
  296. struct wpa_cred *cred, *cred_tail = NULL, *cred_head = NULL;
  297. struct wpa_config *config;
  298. int id = 0;
  299. int cred_id = 0;
  300. config = wpa_config_alloc_empty(NULL, NULL);
  301. if (config == NULL) {
  302. wpa_printf(MSG_ERROR, "Failed to allocate config file "
  303. "structure");
  304. return NULL;
  305. }
  306. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  307. f = fopen(name, "r");
  308. if (f == NULL) {
  309. wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
  310. "error: %s", name, strerror(errno));
  311. os_free(config);
  312. return NULL;
  313. }
  314. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  315. if (os_strcmp(pos, "network={") == 0) {
  316. ssid = wpa_config_read_network(f, &line, id++);
  317. if (ssid == NULL) {
  318. wpa_printf(MSG_ERROR, "Line %d: failed to "
  319. "parse network block.", line);
  320. errors++;
  321. continue;
  322. }
  323. if (head == NULL) {
  324. head = tail = ssid;
  325. } else {
  326. tail->next = ssid;
  327. tail = ssid;
  328. }
  329. if (wpa_config_add_prio_network(config, ssid)) {
  330. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  331. "network block to priority list.",
  332. line);
  333. errors++;
  334. continue;
  335. }
  336. } else if (os_strcmp(pos, "cred={") == 0) {
  337. cred = wpa_config_read_cred(f, &line, cred_id++);
  338. if (cred == NULL) {
  339. wpa_printf(MSG_ERROR, "Line %d: failed to "
  340. "parse cred block.", line);
  341. errors++;
  342. continue;
  343. }
  344. if (cred_head == NULL) {
  345. cred_head = cred_tail = cred;
  346. } else {
  347. cred_tail->next = cred;
  348. cred_tail = cred;
  349. }
  350. #ifndef CONFIG_NO_CONFIG_BLOBS
  351. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  352. if (wpa_config_process_blob(config, f, &line, pos + 12)
  353. < 0) {
  354. wpa_printf(MSG_ERROR, "Line %d: failed to "
  355. "process blob.", line);
  356. errors++;
  357. continue;
  358. }
  359. #endif /* CONFIG_NO_CONFIG_BLOBS */
  360. } else if (wpa_config_process_global(config, pos, line) < 0) {
  361. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  362. "line '%s'.", line, pos);
  363. errors++;
  364. continue;
  365. }
  366. }
  367. fclose(f);
  368. config->ssid = head;
  369. wpa_config_debug_dump_networks(config);
  370. config->cred = cred_head;
  371. #ifndef WPA_IGNORE_CONFIG_ERRORS
  372. if (errors) {
  373. wpa_config_free(config);
  374. config = NULL;
  375. head = NULL;
  376. }
  377. #endif /* WPA_IGNORE_CONFIG_ERRORS */
  378. return config;
  379. }
  380. #ifndef CONFIG_NO_CONFIG_WRITE
  381. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  382. {
  383. char *value = wpa_config_get(ssid, field);
  384. if (value == NULL)
  385. return;
  386. fprintf(f, "\t%s=%s\n", field, value);
  387. os_free(value);
  388. }
  389. static void write_int(FILE *f, const char *field, int value, int def)
  390. {
  391. if (value == def)
  392. return;
  393. fprintf(f, "\t%s=%d\n", field, value);
  394. }
  395. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  396. {
  397. char *value = wpa_config_get(ssid, "bssid");
  398. if (value == NULL)
  399. return;
  400. fprintf(f, "\tbssid=%s\n", value);
  401. os_free(value);
  402. }
  403. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  404. {
  405. char *value = wpa_config_get(ssid, "psk");
  406. if (value == NULL)
  407. return;
  408. fprintf(f, "\tpsk=%s\n", value);
  409. os_free(value);
  410. }
  411. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  412. {
  413. char *value;
  414. if (ssid->proto == DEFAULT_PROTO)
  415. return;
  416. value = wpa_config_get(ssid, "proto");
  417. if (value == NULL)
  418. return;
  419. if (value[0])
  420. fprintf(f, "\tproto=%s\n", value);
  421. os_free(value);
  422. }
  423. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  424. {
  425. char *value;
  426. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  427. return;
  428. value = wpa_config_get(ssid, "key_mgmt");
  429. if (value == NULL)
  430. return;
  431. if (value[0])
  432. fprintf(f, "\tkey_mgmt=%s\n", value);
  433. os_free(value);
  434. }
  435. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  436. {
  437. char *value;
  438. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  439. return;
  440. value = wpa_config_get(ssid, "pairwise");
  441. if (value == NULL)
  442. return;
  443. if (value[0])
  444. fprintf(f, "\tpairwise=%s\n", value);
  445. os_free(value);
  446. }
  447. static void write_group(FILE *f, struct wpa_ssid *ssid)
  448. {
  449. char *value;
  450. if (ssid->group_cipher == DEFAULT_GROUP)
  451. return;
  452. value = wpa_config_get(ssid, "group");
  453. if (value == NULL)
  454. return;
  455. if (value[0])
  456. fprintf(f, "\tgroup=%s\n", value);
  457. os_free(value);
  458. }
  459. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  460. {
  461. char *value;
  462. if (ssid->auth_alg == 0)
  463. return;
  464. value = wpa_config_get(ssid, "auth_alg");
  465. if (value == NULL)
  466. return;
  467. if (value[0])
  468. fprintf(f, "\tauth_alg=%s\n", value);
  469. os_free(value);
  470. }
  471. #ifdef IEEE8021X_EAPOL
  472. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  473. {
  474. char *value;
  475. value = wpa_config_get(ssid, "eap");
  476. if (value == NULL)
  477. return;
  478. if (value[0])
  479. fprintf(f, "\teap=%s\n", value);
  480. os_free(value);
  481. }
  482. #endif /* IEEE8021X_EAPOL */
  483. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  484. {
  485. char field[20], *value;
  486. int res;
  487. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  488. if (res < 0 || (size_t) res >= sizeof(field))
  489. return;
  490. value = wpa_config_get(ssid, field);
  491. if (value) {
  492. fprintf(f, "\t%s=%s\n", field, value);
  493. os_free(value);
  494. }
  495. }
  496. #ifdef CONFIG_P2P
  497. static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
  498. {
  499. char *value = wpa_config_get(ssid, "p2p_client_list");
  500. if (value == NULL)
  501. return;
  502. fprintf(f, "\tp2p_client_list=%s\n", value);
  503. os_free(value);
  504. }
  505. #endif /* CONFIG_P2P */
  506. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  507. {
  508. int i;
  509. #define STR(t) write_str(f, #t, ssid)
  510. #define INT(t) write_int(f, #t, ssid->t, 0)
  511. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  512. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  513. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  514. STR(ssid);
  515. INT(scan_ssid);
  516. write_bssid(f, ssid);
  517. write_psk(f, ssid);
  518. write_proto(f, ssid);
  519. write_key_mgmt(f, ssid);
  520. INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
  521. write_pairwise(f, ssid);
  522. write_group(f, ssid);
  523. write_auth_alg(f, ssid);
  524. STR(bgscan);
  525. STR(autoscan);
  526. #ifdef IEEE8021X_EAPOL
  527. write_eap(f, ssid);
  528. STR(identity);
  529. STR(anonymous_identity);
  530. STR(password);
  531. STR(ca_cert);
  532. STR(ca_path);
  533. STR(client_cert);
  534. STR(private_key);
  535. STR(private_key_passwd);
  536. STR(dh_file);
  537. STR(subject_match);
  538. STR(altsubject_match);
  539. STR(ca_cert2);
  540. STR(ca_path2);
  541. STR(client_cert2);
  542. STR(private_key2);
  543. STR(private_key2_passwd);
  544. STR(dh_file2);
  545. STR(subject_match2);
  546. STR(altsubject_match2);
  547. STR(phase1);
  548. STR(phase2);
  549. STR(pcsc);
  550. STR(pin);
  551. STR(engine_id);
  552. STR(key_id);
  553. STR(cert_id);
  554. STR(ca_cert_id);
  555. STR(key2_id);
  556. STR(pin2);
  557. STR(engine2_id);
  558. STR(cert2_id);
  559. STR(ca_cert2_id);
  560. INTe(engine);
  561. INTe(engine2);
  562. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  563. #endif /* IEEE8021X_EAPOL */
  564. for (i = 0; i < 4; i++)
  565. write_wep_key(f, i, ssid);
  566. INT(wep_tx_keyidx);
  567. INT(priority);
  568. #ifdef IEEE8021X_EAPOL
  569. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  570. STR(pac_file);
  571. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  572. #endif /* IEEE8021X_EAPOL */
  573. INT(mode);
  574. INT(frequency);
  575. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  576. INT(disabled);
  577. INT(peerkey);
  578. #ifdef CONFIG_IEEE80211W
  579. write_int(f, "ieee80211w", ssid->ieee80211w,
  580. MGMT_FRAME_PROTECTION_DEFAULT);
  581. #endif /* CONFIG_IEEE80211W */
  582. STR(id_str);
  583. #ifdef CONFIG_P2P
  584. write_p2p_client_list(f, ssid);
  585. #endif /* CONFIG_P2P */
  586. #undef STR
  587. #undef INT
  588. #undef INT_DEF
  589. }
  590. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  591. {
  592. if (cred->priority)
  593. fprintf(f, "\tpriority=%d\n", cred->priority);
  594. if (cred->pcsc)
  595. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  596. if (cred->realm)
  597. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  598. if (cred->username)
  599. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  600. if (cred->password && cred->ext_password)
  601. fprintf(f, "\tpassword=ext:%s\n", cred->password);
  602. else if (cred->password)
  603. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  604. if (cred->ca_cert)
  605. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  606. if (cred->client_cert)
  607. fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
  608. if (cred->private_key)
  609. fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
  610. if (cred->private_key_passwd)
  611. fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
  612. cred->private_key_passwd);
  613. if (cred->imsi)
  614. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  615. if (cred->milenage)
  616. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  617. if (cred->domain)
  618. fprintf(f, "\tdomain=\"%s\"\n", cred->domain);
  619. if (cred->roaming_consortium_len) {
  620. size_t i;
  621. fprintf(f, "\troaming_consortium=");
  622. for (i = 0; i < cred->roaming_consortium_len; i++)
  623. fprintf(f, "%02x", cred->roaming_consortium[i]);
  624. fprintf(f, "\n");
  625. }
  626. if (cred->eap_method) {
  627. const char *name;
  628. name = eap_get_name(cred->eap_method[0].vendor,
  629. cred->eap_method[0].method);
  630. fprintf(f, "\teap=%s\n", name);
  631. }
  632. if (cred->phase1)
  633. fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
  634. if (cred->phase2)
  635. fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
  636. if (cred->excluded_ssid) {
  637. size_t i, j;
  638. for (i = 0; i < cred->num_excluded_ssid; i++) {
  639. struct excluded_ssid *e = &cred->excluded_ssid[i];
  640. fprintf(f, "\texcluded_ssid=");
  641. for (j = 0; j < e->ssid_len; j++)
  642. fprintf(f, "%02x", e->ssid[j]);
  643. fprintf(f, "\n");
  644. }
  645. }
  646. }
  647. #ifndef CONFIG_NO_CONFIG_BLOBS
  648. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  649. {
  650. unsigned char *encoded;
  651. encoded = base64_encode(blob->data, blob->len, NULL);
  652. if (encoded == NULL)
  653. return -1;
  654. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  655. os_free(encoded);
  656. return 0;
  657. }
  658. #endif /* CONFIG_NO_CONFIG_BLOBS */
  659. static void write_global_bin(FILE *f, const char *field,
  660. const struct wpabuf *val)
  661. {
  662. size_t i;
  663. const u8 *pos;
  664. if (val == NULL)
  665. return;
  666. fprintf(f, "%s=", field);
  667. pos = wpabuf_head(val);
  668. for (i = 0; i < wpabuf_len(val); i++)
  669. fprintf(f, "%02X", *pos++);
  670. fprintf(f, "\n");
  671. }
  672. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  673. {
  674. #ifdef CONFIG_CTRL_IFACE
  675. if (config->ctrl_interface)
  676. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  677. if (config->ctrl_interface_group)
  678. fprintf(f, "ctrl_interface_group=%s\n",
  679. config->ctrl_interface_group);
  680. #endif /* CONFIG_CTRL_IFACE */
  681. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  682. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  683. if (config->ap_scan != DEFAULT_AP_SCAN)
  684. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  685. if (config->disable_scan_offload)
  686. fprintf(f, "disable_scan_offload=%d\n",
  687. config->disable_scan_offload);
  688. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  689. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  690. if (config->opensc_engine_path)
  691. fprintf(f, "opensc_engine_path=%s\n",
  692. config->opensc_engine_path);
  693. if (config->pkcs11_engine_path)
  694. fprintf(f, "pkcs11_engine_path=%s\n",
  695. config->pkcs11_engine_path);
  696. if (config->pkcs11_module_path)
  697. fprintf(f, "pkcs11_module_path=%s\n",
  698. config->pkcs11_module_path);
  699. if (config->pcsc_reader)
  700. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  701. if (config->pcsc_pin)
  702. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  703. if (config->driver_param)
  704. fprintf(f, "driver_param=%s\n", config->driver_param);
  705. if (config->dot11RSNAConfigPMKLifetime)
  706. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  707. config->dot11RSNAConfigPMKLifetime);
  708. if (config->dot11RSNAConfigPMKReauthThreshold)
  709. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  710. config->dot11RSNAConfigPMKReauthThreshold);
  711. if (config->dot11RSNAConfigSATimeout)
  712. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  713. config->dot11RSNAConfigSATimeout);
  714. if (config->update_config)
  715. fprintf(f, "update_config=%d\n", config->update_config);
  716. #ifdef CONFIG_WPS
  717. if (!is_nil_uuid(config->uuid)) {
  718. char buf[40];
  719. uuid_bin2str(config->uuid, buf, sizeof(buf));
  720. fprintf(f, "uuid=%s\n", buf);
  721. }
  722. if (config->device_name)
  723. fprintf(f, "device_name=%s\n", config->device_name);
  724. if (config->manufacturer)
  725. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  726. if (config->model_name)
  727. fprintf(f, "model_name=%s\n", config->model_name);
  728. if (config->model_number)
  729. fprintf(f, "model_number=%s\n", config->model_number);
  730. if (config->serial_number)
  731. fprintf(f, "serial_number=%s\n", config->serial_number);
  732. {
  733. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  734. buf = wps_dev_type_bin2str(config->device_type,
  735. _buf, sizeof(_buf));
  736. if (os_strcmp(buf, "0-00000000-0") != 0)
  737. fprintf(f, "device_type=%s\n", buf);
  738. }
  739. if (WPA_GET_BE32(config->os_version))
  740. fprintf(f, "os_version=%08x\n",
  741. WPA_GET_BE32(config->os_version));
  742. if (config->config_methods)
  743. fprintf(f, "config_methods=%s\n", config->config_methods);
  744. if (config->wps_cred_processing)
  745. fprintf(f, "wps_cred_processing=%d\n",
  746. config->wps_cred_processing);
  747. if (config->wps_vendor_ext_m1) {
  748. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  749. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  750. if (len > 0) {
  751. fprintf(f, "wps_vendor_ext_m1=");
  752. for (i = 0; i < len; i++)
  753. fprintf(f, "%02x", *p++);
  754. fprintf(f, "\n");
  755. }
  756. }
  757. #endif /* CONFIG_WPS */
  758. #ifdef CONFIG_P2P
  759. if (config->p2p_listen_reg_class)
  760. fprintf(f, "p2p_listen_reg_class=%u\n",
  761. config->p2p_listen_reg_class);
  762. if (config->p2p_listen_channel)
  763. fprintf(f, "p2p_listen_channel=%u\n",
  764. config->p2p_listen_channel);
  765. if (config->p2p_oper_reg_class)
  766. fprintf(f, "p2p_oper_reg_class=%u\n",
  767. config->p2p_oper_reg_class);
  768. if (config->p2p_oper_channel)
  769. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  770. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  771. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  772. if (config->p2p_ssid_postfix)
  773. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  774. if (config->persistent_reconnect)
  775. fprintf(f, "persistent_reconnect=%u\n",
  776. config->persistent_reconnect);
  777. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  778. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  779. if (config->p2p_group_idle)
  780. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  781. if (config->p2p_pref_chan) {
  782. unsigned int i;
  783. fprintf(f, "p2p_pref_chan=");
  784. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  785. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  786. config->p2p_pref_chan[i].op_class,
  787. config->p2p_pref_chan[i].chan);
  788. }
  789. fprintf(f, "\n");
  790. }
  791. if (config->p2p_go_ht40)
  792. fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
  793. if (config->p2p_disabled)
  794. fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
  795. if (config->p2p_no_group_iface)
  796. fprintf(f, "p2p_no_group_iface=%u\n",
  797. config->p2p_no_group_iface);
  798. #endif /* CONFIG_P2P */
  799. if (config->country[0] && config->country[1]) {
  800. fprintf(f, "country=%c%c\n",
  801. config->country[0], config->country[1]);
  802. }
  803. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  804. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  805. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  806. fprintf(f, "bss_expiration_age=%u\n",
  807. config->bss_expiration_age);
  808. if (config->bss_expiration_scan_count !=
  809. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  810. fprintf(f, "bss_expiration_scan_count=%u\n",
  811. config->bss_expiration_scan_count);
  812. if (config->filter_ssids)
  813. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  814. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  815. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  816. if (config->disassoc_low_ack)
  817. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  818. #ifdef CONFIG_HS20
  819. if (config->hs20)
  820. fprintf(f, "hs20=1\n");
  821. #endif /* CONFIG_HS20 */
  822. #ifdef CONFIG_INTERWORKING
  823. if (config->interworking)
  824. fprintf(f, "interworking=%u\n", config->interworking);
  825. if (!is_zero_ether_addr(config->hessid))
  826. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  827. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  828. fprintf(f, "access_network_type=%d\n",
  829. config->access_network_type);
  830. #endif /* CONFIG_INTERWORKING */
  831. if (config->pbc_in_m1)
  832. fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
  833. if (config->wps_nfc_dev_pw_id)
  834. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  835. config->wps_nfc_dev_pw_id);
  836. write_global_bin(f, "wps_nfc_dh_pubkey", config->wps_nfc_dh_pubkey);
  837. write_global_bin(f, "wps_nfc_dh_privkey", config->wps_nfc_dh_privkey);
  838. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  839. if (config->ext_password_backend)
  840. fprintf(f, "ext_password_backend=%s\n",
  841. config->ext_password_backend);
  842. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  843. fprintf(f, "p2p_go_max_inactivity=%d\n",
  844. config->p2p_go_max_inactivity);
  845. if (config->auto_interworking)
  846. fprintf(f, "auto_interworking=%d\n",
  847. config->auto_interworking);
  848. if (config->okc)
  849. fprintf(f, "okc=%d\n", config->okc);
  850. if (config->pmf)
  851. fprintf(f, "pmf=%d\n", config->pmf);
  852. if (config->sae_groups) {
  853. int i;
  854. fprintf(f, "sae_groups=");
  855. for (i = 0; config->sae_groups[i] >= 0; i++) {
  856. fprintf(f, "%s%d", i > 0 ? " " : "",
  857. config->sae_groups[i]);
  858. }
  859. fprintf(f, "\n");
  860. }
  861. }
  862. #endif /* CONFIG_NO_CONFIG_WRITE */
  863. int wpa_config_write(const char *name, struct wpa_config *config)
  864. {
  865. #ifndef CONFIG_NO_CONFIG_WRITE
  866. FILE *f;
  867. struct wpa_ssid *ssid;
  868. struct wpa_cred *cred;
  869. #ifndef CONFIG_NO_CONFIG_BLOBS
  870. struct wpa_config_blob *blob;
  871. #endif /* CONFIG_NO_CONFIG_BLOBS */
  872. int ret = 0;
  873. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  874. f = fopen(name, "w");
  875. if (f == NULL) {
  876. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  877. return -1;
  878. }
  879. wpa_config_write_global(f, config);
  880. for (cred = config->cred; cred; cred = cred->next) {
  881. fprintf(f, "\ncred={\n");
  882. wpa_config_write_cred(f, cred);
  883. fprintf(f, "}\n");
  884. }
  885. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  886. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  887. continue; /* do not save temporary networks */
  888. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  889. !ssid->passphrase)
  890. continue; /* do not save invalid network */
  891. fprintf(f, "\nnetwork={\n");
  892. wpa_config_write_network(f, ssid);
  893. fprintf(f, "}\n");
  894. }
  895. #ifndef CONFIG_NO_CONFIG_BLOBS
  896. for (blob = config->blobs; blob; blob = blob->next) {
  897. ret = wpa_config_write_blob(f, blob);
  898. if (ret)
  899. break;
  900. }
  901. #endif /* CONFIG_NO_CONFIG_BLOBS */
  902. fclose(f);
  903. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  904. name, ret ? "un" : "");
  905. return ret;
  906. #else /* CONFIG_NO_CONFIG_WRITE */
  907. return -1;
  908. #endif /* CONFIG_NO_CONFIG_WRITE */
  909. }