config_file.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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, struct wpa_config *cfgp)
  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. if (name == NULL)
  301. return NULL;
  302. if (cfgp)
  303. config = cfgp;
  304. else
  305. config = wpa_config_alloc_empty(NULL, NULL);
  306. if (config == NULL) {
  307. wpa_printf(MSG_ERROR, "Failed to allocate config file "
  308. "structure");
  309. return NULL;
  310. }
  311. head = config->ssid;
  312. cred_head = config->cred;
  313. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  314. f = fopen(name, "r");
  315. if (f == NULL) {
  316. wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
  317. "error: %s", name, strerror(errno));
  318. os_free(config);
  319. return NULL;
  320. }
  321. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  322. if (os_strcmp(pos, "network={") == 0) {
  323. ssid = wpa_config_read_network(f, &line, id++);
  324. if (ssid == NULL) {
  325. wpa_printf(MSG_ERROR, "Line %d: failed to "
  326. "parse network block.", line);
  327. errors++;
  328. continue;
  329. }
  330. if (head == NULL) {
  331. head = tail = ssid;
  332. } else {
  333. tail->next = ssid;
  334. tail = ssid;
  335. }
  336. if (wpa_config_add_prio_network(config, ssid)) {
  337. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  338. "network block to priority list.",
  339. line);
  340. errors++;
  341. continue;
  342. }
  343. } else if (os_strcmp(pos, "cred={") == 0) {
  344. cred = wpa_config_read_cred(f, &line, cred_id++);
  345. if (cred == NULL) {
  346. wpa_printf(MSG_ERROR, "Line %d: failed to "
  347. "parse cred block.", line);
  348. errors++;
  349. continue;
  350. }
  351. if (cred_head == NULL) {
  352. cred_head = cred_tail = cred;
  353. } else {
  354. cred_tail->next = cred;
  355. cred_tail = cred;
  356. }
  357. #ifndef CONFIG_NO_CONFIG_BLOBS
  358. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  359. if (wpa_config_process_blob(config, f, &line, pos + 12)
  360. < 0) {
  361. wpa_printf(MSG_ERROR, "Line %d: failed to "
  362. "process blob.", line);
  363. errors++;
  364. continue;
  365. }
  366. #endif /* CONFIG_NO_CONFIG_BLOBS */
  367. } else if (wpa_config_process_global(config, pos, line) < 0) {
  368. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  369. "line '%s'.", line, pos);
  370. errors++;
  371. continue;
  372. }
  373. }
  374. fclose(f);
  375. config->ssid = head;
  376. wpa_config_debug_dump_networks(config);
  377. config->cred = cred_head;
  378. #ifndef WPA_IGNORE_CONFIG_ERRORS
  379. if (errors) {
  380. wpa_config_free(config);
  381. config = NULL;
  382. head = NULL;
  383. }
  384. #endif /* WPA_IGNORE_CONFIG_ERRORS */
  385. return config;
  386. }
  387. #ifndef CONFIG_NO_CONFIG_WRITE
  388. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  389. {
  390. char *value = wpa_config_get(ssid, field);
  391. if (value == NULL)
  392. return;
  393. fprintf(f, "\t%s=%s\n", field, value);
  394. os_free(value);
  395. }
  396. static void write_int(FILE *f, const char *field, int value, int def)
  397. {
  398. if (value == def)
  399. return;
  400. fprintf(f, "\t%s=%d\n", field, value);
  401. }
  402. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  403. {
  404. char *value = wpa_config_get(ssid, "bssid");
  405. if (value == NULL)
  406. return;
  407. fprintf(f, "\tbssid=%s\n", value);
  408. os_free(value);
  409. }
  410. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  411. {
  412. char *value = wpa_config_get(ssid, "psk");
  413. if (value == NULL)
  414. return;
  415. fprintf(f, "\tpsk=%s\n", value);
  416. os_free(value);
  417. }
  418. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  419. {
  420. char *value;
  421. if (ssid->proto == DEFAULT_PROTO)
  422. return;
  423. value = wpa_config_get(ssid, "proto");
  424. if (value == NULL)
  425. return;
  426. if (value[0])
  427. fprintf(f, "\tproto=%s\n", value);
  428. os_free(value);
  429. }
  430. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  431. {
  432. char *value;
  433. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  434. return;
  435. value = wpa_config_get(ssid, "key_mgmt");
  436. if (value == NULL)
  437. return;
  438. if (value[0])
  439. fprintf(f, "\tkey_mgmt=%s\n", value);
  440. os_free(value);
  441. }
  442. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  443. {
  444. char *value;
  445. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  446. return;
  447. value = wpa_config_get(ssid, "pairwise");
  448. if (value == NULL)
  449. return;
  450. if (value[0])
  451. fprintf(f, "\tpairwise=%s\n", value);
  452. os_free(value);
  453. }
  454. static void write_group(FILE *f, struct wpa_ssid *ssid)
  455. {
  456. char *value;
  457. if (ssid->group_cipher == DEFAULT_GROUP)
  458. return;
  459. value = wpa_config_get(ssid, "group");
  460. if (value == NULL)
  461. return;
  462. if (value[0])
  463. fprintf(f, "\tgroup=%s\n", value);
  464. os_free(value);
  465. }
  466. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  467. {
  468. char *value;
  469. if (ssid->auth_alg == 0)
  470. return;
  471. value = wpa_config_get(ssid, "auth_alg");
  472. if (value == NULL)
  473. return;
  474. if (value[0])
  475. fprintf(f, "\tauth_alg=%s\n", value);
  476. os_free(value);
  477. }
  478. #ifdef IEEE8021X_EAPOL
  479. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  480. {
  481. char *value;
  482. value = wpa_config_get(ssid, "eap");
  483. if (value == NULL)
  484. return;
  485. if (value[0])
  486. fprintf(f, "\teap=%s\n", value);
  487. os_free(value);
  488. }
  489. #endif /* IEEE8021X_EAPOL */
  490. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  491. {
  492. char field[20], *value;
  493. int res;
  494. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  495. if (res < 0 || (size_t) res >= sizeof(field))
  496. return;
  497. value = wpa_config_get(ssid, field);
  498. if (value) {
  499. fprintf(f, "\t%s=%s\n", field, value);
  500. os_free(value);
  501. }
  502. }
  503. #ifdef CONFIG_P2P
  504. static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
  505. {
  506. char *value = wpa_config_get(ssid, "p2p_client_list");
  507. if (value == NULL)
  508. return;
  509. fprintf(f, "\tp2p_client_list=%s\n", value);
  510. os_free(value);
  511. }
  512. #endif /* CONFIG_P2P */
  513. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  514. {
  515. int i;
  516. #define STR(t) write_str(f, #t, ssid)
  517. #define INT(t) write_int(f, #t, ssid->t, 0)
  518. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  519. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  520. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  521. STR(ssid);
  522. INT(scan_ssid);
  523. write_bssid(f, ssid);
  524. write_psk(f, ssid);
  525. write_proto(f, ssid);
  526. write_key_mgmt(f, ssid);
  527. INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
  528. write_pairwise(f, ssid);
  529. write_group(f, ssid);
  530. write_auth_alg(f, ssid);
  531. STR(bgscan);
  532. STR(autoscan);
  533. #ifdef IEEE8021X_EAPOL
  534. write_eap(f, ssid);
  535. STR(identity);
  536. STR(anonymous_identity);
  537. STR(password);
  538. STR(ca_cert);
  539. STR(ca_path);
  540. STR(client_cert);
  541. STR(private_key);
  542. STR(private_key_passwd);
  543. STR(dh_file);
  544. STR(subject_match);
  545. STR(altsubject_match);
  546. STR(ca_cert2);
  547. STR(ca_path2);
  548. STR(client_cert2);
  549. STR(private_key2);
  550. STR(private_key2_passwd);
  551. STR(dh_file2);
  552. STR(subject_match2);
  553. STR(altsubject_match2);
  554. STR(phase1);
  555. STR(phase2);
  556. STR(pcsc);
  557. STR(pin);
  558. STR(engine_id);
  559. STR(key_id);
  560. STR(cert_id);
  561. STR(ca_cert_id);
  562. STR(key2_id);
  563. STR(pin2);
  564. STR(engine2_id);
  565. STR(cert2_id);
  566. STR(ca_cert2_id);
  567. INTe(engine);
  568. INTe(engine2);
  569. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  570. #endif /* IEEE8021X_EAPOL */
  571. for (i = 0; i < 4; i++)
  572. write_wep_key(f, i, ssid);
  573. INT(wep_tx_keyidx);
  574. INT(priority);
  575. #ifdef IEEE8021X_EAPOL
  576. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  577. STR(pac_file);
  578. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  579. #endif /* IEEE8021X_EAPOL */
  580. INT(mode);
  581. INT(frequency);
  582. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  583. INT(disabled);
  584. INT(peerkey);
  585. #ifdef CONFIG_IEEE80211W
  586. write_int(f, "ieee80211w", ssid->ieee80211w,
  587. MGMT_FRAME_PROTECTION_DEFAULT);
  588. #endif /* CONFIG_IEEE80211W */
  589. STR(id_str);
  590. #ifdef CONFIG_P2P
  591. write_p2p_client_list(f, ssid);
  592. #endif /* CONFIG_P2P */
  593. INT(dtim_period);
  594. INT(beacon_int);
  595. #undef STR
  596. #undef INT
  597. #undef INT_DEF
  598. }
  599. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  600. {
  601. if (cred->priority)
  602. fprintf(f, "\tpriority=%d\n", cred->priority);
  603. if (cred->pcsc)
  604. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  605. if (cred->realm)
  606. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  607. if (cred->username)
  608. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  609. if (cred->password && cred->ext_password)
  610. fprintf(f, "\tpassword=ext:%s\n", cred->password);
  611. else if (cred->password)
  612. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  613. if (cred->ca_cert)
  614. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  615. if (cred->client_cert)
  616. fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
  617. if (cred->private_key)
  618. fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
  619. if (cred->private_key_passwd)
  620. fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
  621. cred->private_key_passwd);
  622. if (cred->imsi)
  623. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  624. if (cred->milenage)
  625. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  626. if (cred->domain)
  627. fprintf(f, "\tdomain=\"%s\"\n", cred->domain);
  628. if (cred->roaming_consortium_len) {
  629. size_t i;
  630. fprintf(f, "\troaming_consortium=");
  631. for (i = 0; i < cred->roaming_consortium_len; i++)
  632. fprintf(f, "%02x", cred->roaming_consortium[i]);
  633. fprintf(f, "\n");
  634. }
  635. if (cred->eap_method) {
  636. const char *name;
  637. name = eap_get_name(cred->eap_method[0].vendor,
  638. cred->eap_method[0].method);
  639. fprintf(f, "\teap=%s\n", name);
  640. }
  641. if (cred->phase1)
  642. fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
  643. if (cred->phase2)
  644. fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
  645. if (cred->excluded_ssid) {
  646. size_t i, j;
  647. for (i = 0; i < cred->num_excluded_ssid; i++) {
  648. struct excluded_ssid *e = &cred->excluded_ssid[i];
  649. fprintf(f, "\texcluded_ssid=");
  650. for (j = 0; j < e->ssid_len; j++)
  651. fprintf(f, "%02x", e->ssid[j]);
  652. fprintf(f, "\n");
  653. }
  654. }
  655. }
  656. #ifndef CONFIG_NO_CONFIG_BLOBS
  657. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  658. {
  659. unsigned char *encoded;
  660. encoded = base64_encode(blob->data, blob->len, NULL);
  661. if (encoded == NULL)
  662. return -1;
  663. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  664. os_free(encoded);
  665. return 0;
  666. }
  667. #endif /* CONFIG_NO_CONFIG_BLOBS */
  668. static void write_global_bin(FILE *f, const char *field,
  669. const struct wpabuf *val)
  670. {
  671. size_t i;
  672. const u8 *pos;
  673. if (val == NULL)
  674. return;
  675. fprintf(f, "%s=", field);
  676. pos = wpabuf_head(val);
  677. for (i = 0; i < wpabuf_len(val); i++)
  678. fprintf(f, "%02X", *pos++);
  679. fprintf(f, "\n");
  680. }
  681. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  682. {
  683. #ifdef CONFIG_CTRL_IFACE
  684. if (config->ctrl_interface)
  685. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  686. if (config->ctrl_interface_group)
  687. fprintf(f, "ctrl_interface_group=%s\n",
  688. config->ctrl_interface_group);
  689. #endif /* CONFIG_CTRL_IFACE */
  690. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  691. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  692. if (config->ap_scan != DEFAULT_AP_SCAN)
  693. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  694. if (config->disable_scan_offload)
  695. fprintf(f, "disable_scan_offload=%d\n",
  696. config->disable_scan_offload);
  697. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  698. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  699. if (config->opensc_engine_path)
  700. fprintf(f, "opensc_engine_path=%s\n",
  701. config->opensc_engine_path);
  702. if (config->pkcs11_engine_path)
  703. fprintf(f, "pkcs11_engine_path=%s\n",
  704. config->pkcs11_engine_path);
  705. if (config->pkcs11_module_path)
  706. fprintf(f, "pkcs11_module_path=%s\n",
  707. config->pkcs11_module_path);
  708. if (config->pcsc_reader)
  709. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  710. if (config->pcsc_pin)
  711. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  712. if (config->driver_param)
  713. fprintf(f, "driver_param=%s\n", config->driver_param);
  714. if (config->dot11RSNAConfigPMKLifetime)
  715. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  716. config->dot11RSNAConfigPMKLifetime);
  717. if (config->dot11RSNAConfigPMKReauthThreshold)
  718. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  719. config->dot11RSNAConfigPMKReauthThreshold);
  720. if (config->dot11RSNAConfigSATimeout)
  721. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  722. config->dot11RSNAConfigSATimeout);
  723. if (config->update_config)
  724. fprintf(f, "update_config=%d\n", config->update_config);
  725. #ifdef CONFIG_WPS
  726. if (!is_nil_uuid(config->uuid)) {
  727. char buf[40];
  728. uuid_bin2str(config->uuid, buf, sizeof(buf));
  729. fprintf(f, "uuid=%s\n", buf);
  730. }
  731. if (config->device_name)
  732. fprintf(f, "device_name=%s\n", config->device_name);
  733. if (config->manufacturer)
  734. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  735. if (config->model_name)
  736. fprintf(f, "model_name=%s\n", config->model_name);
  737. if (config->model_number)
  738. fprintf(f, "model_number=%s\n", config->model_number);
  739. if (config->serial_number)
  740. fprintf(f, "serial_number=%s\n", config->serial_number);
  741. {
  742. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  743. buf = wps_dev_type_bin2str(config->device_type,
  744. _buf, sizeof(_buf));
  745. if (os_strcmp(buf, "0-00000000-0") != 0)
  746. fprintf(f, "device_type=%s\n", buf);
  747. }
  748. if (WPA_GET_BE32(config->os_version))
  749. fprintf(f, "os_version=%08x\n",
  750. WPA_GET_BE32(config->os_version));
  751. if (config->config_methods)
  752. fprintf(f, "config_methods=%s\n", config->config_methods);
  753. if (config->wps_cred_processing)
  754. fprintf(f, "wps_cred_processing=%d\n",
  755. config->wps_cred_processing);
  756. if (config->wps_vendor_ext_m1) {
  757. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  758. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  759. if (len > 0) {
  760. fprintf(f, "wps_vendor_ext_m1=");
  761. for (i = 0; i < len; i++)
  762. fprintf(f, "%02x", *p++);
  763. fprintf(f, "\n");
  764. }
  765. }
  766. #endif /* CONFIG_WPS */
  767. #ifdef CONFIG_P2P
  768. if (config->p2p_listen_reg_class)
  769. fprintf(f, "p2p_listen_reg_class=%u\n",
  770. config->p2p_listen_reg_class);
  771. if (config->p2p_listen_channel)
  772. fprintf(f, "p2p_listen_channel=%u\n",
  773. config->p2p_listen_channel);
  774. if (config->p2p_oper_reg_class)
  775. fprintf(f, "p2p_oper_reg_class=%u\n",
  776. config->p2p_oper_reg_class);
  777. if (config->p2p_oper_channel)
  778. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  779. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  780. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  781. if (config->p2p_ssid_postfix)
  782. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  783. if (config->persistent_reconnect)
  784. fprintf(f, "persistent_reconnect=%u\n",
  785. config->persistent_reconnect);
  786. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  787. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  788. if (config->p2p_group_idle)
  789. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  790. if (config->p2p_pref_chan) {
  791. unsigned int i;
  792. fprintf(f, "p2p_pref_chan=");
  793. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  794. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  795. config->p2p_pref_chan[i].op_class,
  796. config->p2p_pref_chan[i].chan);
  797. }
  798. fprintf(f, "\n");
  799. }
  800. if (config->p2p_go_ht40)
  801. fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
  802. if (config->p2p_disabled)
  803. fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
  804. if (config->p2p_no_group_iface)
  805. fprintf(f, "p2p_no_group_iface=%u\n",
  806. config->p2p_no_group_iface);
  807. if (config->p2p_ignore_shared_freq)
  808. fprintf(f, "p2p_ignore_shared_freq=%u\n",
  809. config->p2p_ignore_shared_freq);
  810. #endif /* CONFIG_P2P */
  811. if (config->country[0] && config->country[1]) {
  812. fprintf(f, "country=%c%c\n",
  813. config->country[0], config->country[1]);
  814. }
  815. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  816. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  817. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  818. fprintf(f, "bss_expiration_age=%u\n",
  819. config->bss_expiration_age);
  820. if (config->bss_expiration_scan_count !=
  821. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  822. fprintf(f, "bss_expiration_scan_count=%u\n",
  823. config->bss_expiration_scan_count);
  824. if (config->filter_ssids)
  825. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  826. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  827. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  828. if (config->disassoc_low_ack)
  829. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  830. #ifdef CONFIG_HS20
  831. if (config->hs20)
  832. fprintf(f, "hs20=1\n");
  833. #endif /* CONFIG_HS20 */
  834. #ifdef CONFIG_INTERWORKING
  835. if (config->interworking)
  836. fprintf(f, "interworking=%u\n", config->interworking);
  837. if (!is_zero_ether_addr(config->hessid))
  838. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  839. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  840. fprintf(f, "access_network_type=%d\n",
  841. config->access_network_type);
  842. #endif /* CONFIG_INTERWORKING */
  843. if (config->pbc_in_m1)
  844. fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
  845. if (config->wps_nfc_pw_from_config) {
  846. if (config->wps_nfc_dev_pw_id)
  847. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  848. config->wps_nfc_dev_pw_id);
  849. write_global_bin(f, "wps_nfc_dh_pubkey",
  850. config->wps_nfc_dh_pubkey);
  851. write_global_bin(f, "wps_nfc_dh_privkey",
  852. config->wps_nfc_dh_privkey);
  853. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  854. }
  855. if (config->ext_password_backend)
  856. fprintf(f, "ext_password_backend=%s\n",
  857. config->ext_password_backend);
  858. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  859. fprintf(f, "p2p_go_max_inactivity=%d\n",
  860. config->p2p_go_max_inactivity);
  861. if (config->auto_interworking)
  862. fprintf(f, "auto_interworking=%d\n",
  863. config->auto_interworking);
  864. if (config->okc)
  865. fprintf(f, "okc=%d\n", config->okc);
  866. if (config->pmf)
  867. fprintf(f, "pmf=%d\n", config->pmf);
  868. if (config->dtim_period)
  869. fprintf(f, "dtim_period=%d\n", config->dtim_period);
  870. if (config->beacon_int)
  871. fprintf(f, "beacon_int=%d\n", config->beacon_int);
  872. if (config->sae_groups) {
  873. int i;
  874. fprintf(f, "sae_groups=");
  875. for (i = 0; config->sae_groups[i] >= 0; i++) {
  876. fprintf(f, "%s%d", i > 0 ? " " : "",
  877. config->sae_groups[i]);
  878. }
  879. fprintf(f, "\n");
  880. }
  881. if (config->ap_vendor_elements) {
  882. int i, len = wpabuf_len(config->ap_vendor_elements);
  883. const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
  884. if (len > 0) {
  885. fprintf(f, "ap_vendor_elements=");
  886. for (i = 0; i < len; i++)
  887. fprintf(f, "%02x", *p++);
  888. fprintf(f, "\n");
  889. }
  890. }
  891. if (config->ignore_old_scan_res)
  892. fprintf(f, "ignore_old_scan_res=%d\n",
  893. config->ignore_old_scan_res);
  894. if (config->freq_list && config->freq_list[0]) {
  895. int i;
  896. fprintf(f, "freq_list=");
  897. for (i = 0; config->freq_list[i]; i++) {
  898. fprintf(f, "%s%u", i > 0 ? " " : "",
  899. config->freq_list[i]);
  900. }
  901. fprintf(f, "\n");
  902. }
  903. }
  904. #endif /* CONFIG_NO_CONFIG_WRITE */
  905. int wpa_config_write(const char *name, struct wpa_config *config)
  906. {
  907. #ifndef CONFIG_NO_CONFIG_WRITE
  908. FILE *f;
  909. struct wpa_ssid *ssid;
  910. struct wpa_cred *cred;
  911. #ifndef CONFIG_NO_CONFIG_BLOBS
  912. struct wpa_config_blob *blob;
  913. #endif /* CONFIG_NO_CONFIG_BLOBS */
  914. int ret = 0;
  915. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  916. f = fopen(name, "w");
  917. if (f == NULL) {
  918. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  919. return -1;
  920. }
  921. wpa_config_write_global(f, config);
  922. for (cred = config->cred; cred; cred = cred->next) {
  923. fprintf(f, "\ncred={\n");
  924. wpa_config_write_cred(f, cred);
  925. fprintf(f, "}\n");
  926. }
  927. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  928. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  929. continue; /* do not save temporary networks */
  930. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  931. !ssid->passphrase)
  932. continue; /* do not save invalid network */
  933. fprintf(f, "\nnetwork={\n");
  934. wpa_config_write_network(f, ssid);
  935. fprintf(f, "}\n");
  936. }
  937. #ifndef CONFIG_NO_CONFIG_BLOBS
  938. for (blob = config->blobs; blob; blob = blob->next) {
  939. ret = wpa_config_write_blob(f, blob);
  940. if (ret)
  941. break;
  942. }
  943. #endif /* CONFIG_NO_CONFIG_BLOBS */
  944. fclose(f);
  945. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  946. name, ret ? "un" : "");
  947. return ret;
  948. #else /* CONFIG_NO_CONFIG_WRITE */
  949. return -1;
  950. #endif /* CONFIG_NO_CONFIG_WRITE */
  951. }