config_file.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. dl_list_init(&ssid->psk_list);
  138. ssid->id = id;
  139. wpa_config_set_network_defaults(ssid);
  140. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  141. if (os_strcmp(pos, "}") == 0) {
  142. end = 1;
  143. break;
  144. }
  145. pos2 = os_strchr(pos, '=');
  146. if (pos2 == NULL) {
  147. wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
  148. "'%s'.", *line, pos);
  149. errors++;
  150. continue;
  151. }
  152. *pos2++ = '\0';
  153. if (*pos2 == '"') {
  154. if (os_strchr(pos2 + 1, '"') == NULL) {
  155. wpa_printf(MSG_ERROR, "Line %d: invalid "
  156. "quotation '%s'.", *line, pos2);
  157. errors++;
  158. continue;
  159. }
  160. }
  161. if (wpa_config_set(ssid, pos, pos2, *line) < 0)
  162. errors++;
  163. }
  164. if (!end) {
  165. wpa_printf(MSG_ERROR, "Line %d: network block was not "
  166. "terminated properly.", *line);
  167. errors++;
  168. }
  169. errors += wpa_config_validate_network(ssid, *line);
  170. if (errors) {
  171. wpa_config_free_ssid(ssid);
  172. ssid = NULL;
  173. }
  174. return ssid;
  175. }
  176. static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
  177. {
  178. struct wpa_cred *cred;
  179. int errors = 0, end = 0;
  180. char buf[256], *pos, *pos2;
  181. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
  182. cred = os_zalloc(sizeof(*cred));
  183. if (cred == NULL)
  184. return NULL;
  185. cred->id = id;
  186. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  187. if (os_strcmp(pos, "}") == 0) {
  188. end = 1;
  189. break;
  190. }
  191. pos2 = os_strchr(pos, '=');
  192. if (pos2 == NULL) {
  193. wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
  194. "'%s'.", *line, pos);
  195. errors++;
  196. continue;
  197. }
  198. *pos2++ = '\0';
  199. if (*pos2 == '"') {
  200. if (os_strchr(pos2 + 1, '"') == NULL) {
  201. wpa_printf(MSG_ERROR, "Line %d: invalid "
  202. "quotation '%s'.", *line, pos2);
  203. errors++;
  204. continue;
  205. }
  206. }
  207. if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
  208. errors++;
  209. }
  210. if (!end) {
  211. wpa_printf(MSG_ERROR, "Line %d: cred block was not "
  212. "terminated properly.", *line);
  213. errors++;
  214. }
  215. if (errors) {
  216. wpa_config_free_cred(cred);
  217. cred = NULL;
  218. }
  219. return cred;
  220. }
  221. #ifndef CONFIG_NO_CONFIG_BLOBS
  222. static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
  223. const char *name)
  224. {
  225. struct wpa_config_blob *blob;
  226. char buf[256], *pos;
  227. unsigned char *encoded = NULL, *nencoded;
  228. int end = 0;
  229. size_t encoded_len = 0, len;
  230. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
  231. *line, name);
  232. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  233. if (os_strcmp(pos, "}") == 0) {
  234. end = 1;
  235. break;
  236. }
  237. len = os_strlen(pos);
  238. nencoded = os_realloc(encoded, encoded_len + len);
  239. if (nencoded == NULL) {
  240. wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
  241. "blob", *line);
  242. os_free(encoded);
  243. return NULL;
  244. }
  245. encoded = nencoded;
  246. os_memcpy(encoded + encoded_len, pos, len);
  247. encoded_len += len;
  248. }
  249. if (!end) {
  250. wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
  251. "properly", *line);
  252. os_free(encoded);
  253. return NULL;
  254. }
  255. blob = os_zalloc(sizeof(*blob));
  256. if (blob == NULL) {
  257. os_free(encoded);
  258. return NULL;
  259. }
  260. blob->name = os_strdup(name);
  261. blob->data = base64_decode(encoded, encoded_len, &blob->len);
  262. os_free(encoded);
  263. if (blob->name == NULL || blob->data == NULL) {
  264. wpa_config_free_blob(blob);
  265. return NULL;
  266. }
  267. return blob;
  268. }
  269. static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
  270. int *line, char *bname)
  271. {
  272. char *name_end;
  273. struct wpa_config_blob *blob;
  274. name_end = os_strchr(bname, '=');
  275. if (name_end == NULL) {
  276. wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
  277. *line);
  278. return -1;
  279. }
  280. *name_end = '\0';
  281. blob = wpa_config_read_blob(f, line, bname);
  282. if (blob == NULL) {
  283. wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
  284. *line, bname);
  285. return -1;
  286. }
  287. wpa_config_set_blob(config, blob);
  288. return 0;
  289. }
  290. #endif /* CONFIG_NO_CONFIG_BLOBS */
  291. struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
  292. {
  293. FILE *f;
  294. char buf[512], *pos;
  295. int errors = 0, line = 0;
  296. struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
  297. struct wpa_cred *cred, *cred_tail = NULL, *cred_head = NULL;
  298. struct wpa_config *config;
  299. int id = 0;
  300. int cred_id = 0;
  301. if (name == NULL)
  302. return NULL;
  303. if (cfgp)
  304. config = cfgp;
  305. else
  306. config = wpa_config_alloc_empty(NULL, NULL);
  307. if (config == NULL) {
  308. wpa_printf(MSG_ERROR, "Failed to allocate config file "
  309. "structure");
  310. return NULL;
  311. }
  312. head = config->ssid;
  313. cred_head = config->cred;
  314. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  315. f = fopen(name, "r");
  316. if (f == NULL) {
  317. wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
  318. "error: %s", name, strerror(errno));
  319. os_free(config);
  320. return NULL;
  321. }
  322. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  323. if (os_strcmp(pos, "network={") == 0) {
  324. ssid = wpa_config_read_network(f, &line, id++);
  325. if (ssid == NULL) {
  326. wpa_printf(MSG_ERROR, "Line %d: failed to "
  327. "parse network block.", line);
  328. errors++;
  329. continue;
  330. }
  331. if (head == NULL) {
  332. head = tail = ssid;
  333. } else {
  334. tail->next = ssid;
  335. tail = ssid;
  336. }
  337. if (wpa_config_add_prio_network(config, ssid)) {
  338. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  339. "network block to priority list.",
  340. line);
  341. errors++;
  342. continue;
  343. }
  344. } else if (os_strcmp(pos, "cred={") == 0) {
  345. cred = wpa_config_read_cred(f, &line, cred_id++);
  346. if (cred == NULL) {
  347. wpa_printf(MSG_ERROR, "Line %d: failed to "
  348. "parse cred block.", line);
  349. errors++;
  350. continue;
  351. }
  352. if (cred_head == NULL) {
  353. cred_head = cred_tail = cred;
  354. } else {
  355. cred_tail->next = cred;
  356. cred_tail = cred;
  357. }
  358. #ifndef CONFIG_NO_CONFIG_BLOBS
  359. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  360. if (wpa_config_process_blob(config, f, &line, pos + 12)
  361. < 0) {
  362. wpa_printf(MSG_ERROR, "Line %d: failed to "
  363. "process blob.", line);
  364. errors++;
  365. continue;
  366. }
  367. #endif /* CONFIG_NO_CONFIG_BLOBS */
  368. } else if (wpa_config_process_global(config, pos, line) < 0) {
  369. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  370. "line '%s'.", line, pos);
  371. errors++;
  372. continue;
  373. }
  374. }
  375. fclose(f);
  376. config->ssid = head;
  377. wpa_config_debug_dump_networks(config);
  378. config->cred = cred_head;
  379. #ifndef WPA_IGNORE_CONFIG_ERRORS
  380. if (errors) {
  381. wpa_config_free(config);
  382. config = NULL;
  383. head = NULL;
  384. }
  385. #endif /* WPA_IGNORE_CONFIG_ERRORS */
  386. return config;
  387. }
  388. #ifndef CONFIG_NO_CONFIG_WRITE
  389. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  390. {
  391. char *value = wpa_config_get(ssid, field);
  392. if (value == NULL)
  393. return;
  394. fprintf(f, "\t%s=%s\n", field, value);
  395. os_free(value);
  396. }
  397. static void write_int(FILE *f, const char *field, int value, int def)
  398. {
  399. if (value == def)
  400. return;
  401. fprintf(f, "\t%s=%d\n", field, value);
  402. }
  403. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  404. {
  405. char *value = wpa_config_get(ssid, "bssid");
  406. if (value == NULL)
  407. return;
  408. fprintf(f, "\tbssid=%s\n", value);
  409. os_free(value);
  410. }
  411. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  412. {
  413. char *value = wpa_config_get(ssid, "psk");
  414. if (value == NULL)
  415. return;
  416. fprintf(f, "\tpsk=%s\n", value);
  417. os_free(value);
  418. }
  419. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  420. {
  421. char *value;
  422. if (ssid->proto == DEFAULT_PROTO)
  423. return;
  424. value = wpa_config_get(ssid, "proto");
  425. if (value == NULL)
  426. return;
  427. if (value[0])
  428. fprintf(f, "\tproto=%s\n", value);
  429. os_free(value);
  430. }
  431. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  432. {
  433. char *value;
  434. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  435. return;
  436. value = wpa_config_get(ssid, "key_mgmt");
  437. if (value == NULL)
  438. return;
  439. if (value[0])
  440. fprintf(f, "\tkey_mgmt=%s\n", value);
  441. os_free(value);
  442. }
  443. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  444. {
  445. char *value;
  446. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  447. return;
  448. value = wpa_config_get(ssid, "pairwise");
  449. if (value == NULL)
  450. return;
  451. if (value[0])
  452. fprintf(f, "\tpairwise=%s\n", value);
  453. os_free(value);
  454. }
  455. static void write_group(FILE *f, struct wpa_ssid *ssid)
  456. {
  457. char *value;
  458. if (ssid->group_cipher == DEFAULT_GROUP)
  459. return;
  460. value = wpa_config_get(ssid, "group");
  461. if (value == NULL)
  462. return;
  463. if (value[0])
  464. fprintf(f, "\tgroup=%s\n", value);
  465. os_free(value);
  466. }
  467. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  468. {
  469. char *value;
  470. if (ssid->auth_alg == 0)
  471. return;
  472. value = wpa_config_get(ssid, "auth_alg");
  473. if (value == NULL)
  474. return;
  475. if (value[0])
  476. fprintf(f, "\tauth_alg=%s\n", value);
  477. os_free(value);
  478. }
  479. #ifdef IEEE8021X_EAPOL
  480. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  481. {
  482. char *value;
  483. value = wpa_config_get(ssid, "eap");
  484. if (value == NULL)
  485. return;
  486. if (value[0])
  487. fprintf(f, "\teap=%s\n", value);
  488. os_free(value);
  489. }
  490. #endif /* IEEE8021X_EAPOL */
  491. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  492. {
  493. char field[20], *value;
  494. int res;
  495. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  496. if (res < 0 || (size_t) res >= sizeof(field))
  497. return;
  498. value = wpa_config_get(ssid, field);
  499. if (value) {
  500. fprintf(f, "\t%s=%s\n", field, value);
  501. os_free(value);
  502. }
  503. }
  504. #ifdef CONFIG_P2P
  505. static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
  506. {
  507. char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
  508. if (value == NULL)
  509. return;
  510. fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
  511. os_free(value);
  512. }
  513. static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
  514. {
  515. char *value = wpa_config_get(ssid, "p2p_client_list");
  516. if (value == NULL)
  517. return;
  518. fprintf(f, "\tp2p_client_list=%s\n", value);
  519. os_free(value);
  520. }
  521. static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
  522. {
  523. struct psk_list_entry *psk;
  524. char hex[32 * 2 + 1];
  525. dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
  526. wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
  527. fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
  528. psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
  529. }
  530. }
  531. #endif /* CONFIG_P2P */
  532. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  533. {
  534. int i;
  535. #define STR(t) write_str(f, #t, ssid)
  536. #define INT(t) write_int(f, #t, ssid->t, 0)
  537. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  538. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  539. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  540. STR(ssid);
  541. INT(scan_ssid);
  542. write_bssid(f, ssid);
  543. write_psk(f, ssid);
  544. write_proto(f, ssid);
  545. write_key_mgmt(f, ssid);
  546. INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
  547. write_pairwise(f, ssid);
  548. write_group(f, ssid);
  549. write_auth_alg(f, ssid);
  550. STR(bgscan);
  551. STR(autoscan);
  552. STR(scan_freq);
  553. #ifdef IEEE8021X_EAPOL
  554. write_eap(f, ssid);
  555. STR(identity);
  556. STR(anonymous_identity);
  557. STR(password);
  558. STR(ca_cert);
  559. STR(ca_path);
  560. STR(client_cert);
  561. STR(private_key);
  562. STR(private_key_passwd);
  563. STR(dh_file);
  564. STR(subject_match);
  565. STR(altsubject_match);
  566. STR(domain_suffix_match);
  567. STR(ca_cert2);
  568. STR(ca_path2);
  569. STR(client_cert2);
  570. STR(private_key2);
  571. STR(private_key2_passwd);
  572. STR(dh_file2);
  573. STR(subject_match2);
  574. STR(altsubject_match2);
  575. STR(domain_suffix_match2);
  576. STR(phase1);
  577. STR(phase2);
  578. STR(pcsc);
  579. STR(pin);
  580. STR(engine_id);
  581. STR(key_id);
  582. STR(cert_id);
  583. STR(ca_cert_id);
  584. STR(key2_id);
  585. STR(pin2);
  586. STR(engine2_id);
  587. STR(cert2_id);
  588. STR(ca_cert2_id);
  589. INTe(engine);
  590. INTe(engine2);
  591. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  592. #endif /* IEEE8021X_EAPOL */
  593. for (i = 0; i < 4; i++)
  594. write_wep_key(f, i, ssid);
  595. INT(wep_tx_keyidx);
  596. INT(priority);
  597. #ifdef IEEE8021X_EAPOL
  598. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  599. STR(pac_file);
  600. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  601. #endif /* IEEE8021X_EAPOL */
  602. INT(mode);
  603. INT(frequency);
  604. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  605. INT(disabled);
  606. INT(peerkey);
  607. #ifdef CONFIG_IEEE80211W
  608. write_int(f, "ieee80211w", ssid->ieee80211w,
  609. MGMT_FRAME_PROTECTION_DEFAULT);
  610. #endif /* CONFIG_IEEE80211W */
  611. STR(id_str);
  612. #ifdef CONFIG_P2P
  613. write_go_p2p_dev_addr(f, ssid);
  614. write_p2p_client_list(f, ssid);
  615. write_psk_list(f, ssid);
  616. #endif /* CONFIG_P2P */
  617. INT(dtim_period);
  618. INT(beacon_int);
  619. #undef STR
  620. #undef INT
  621. #undef INT_DEF
  622. }
  623. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  624. {
  625. size_t i;
  626. if (cred->priority)
  627. fprintf(f, "\tpriority=%d\n", cred->priority);
  628. if (cred->pcsc)
  629. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  630. if (cred->realm)
  631. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  632. if (cred->username)
  633. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  634. if (cred->password && cred->ext_password)
  635. fprintf(f, "\tpassword=ext:%s\n", cred->password);
  636. else if (cred->password)
  637. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  638. if (cred->ca_cert)
  639. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  640. if (cred->client_cert)
  641. fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
  642. if (cred->private_key)
  643. fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
  644. if (cred->private_key_passwd)
  645. fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
  646. cred->private_key_passwd);
  647. if (cred->imsi)
  648. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  649. if (cred->milenage)
  650. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  651. for (i = 0; i < cred->num_domain; i++)
  652. fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
  653. if (cred->domain_suffix_match)
  654. fprintf(f, "\tdomain_suffix_match=\"%s\"",
  655. cred->domain_suffix_match);
  656. if (cred->roaming_consortium_len) {
  657. fprintf(f, "\troaming_consortium=");
  658. for (i = 0; i < cred->roaming_consortium_len; i++)
  659. fprintf(f, "%02x", cred->roaming_consortium[i]);
  660. fprintf(f, "\n");
  661. }
  662. if (cred->eap_method) {
  663. const char *name;
  664. name = eap_get_name(cred->eap_method[0].vendor,
  665. cred->eap_method[0].method);
  666. fprintf(f, "\teap=%s\n", name);
  667. }
  668. if (cred->phase1)
  669. fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
  670. if (cred->phase2)
  671. fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
  672. if (cred->excluded_ssid) {
  673. size_t j;
  674. for (i = 0; i < cred->num_excluded_ssid; i++) {
  675. struct excluded_ssid *e = &cred->excluded_ssid[i];
  676. fprintf(f, "\texcluded_ssid=");
  677. for (j = 0; j < e->ssid_len; j++)
  678. fprintf(f, "%02x", e->ssid[j]);
  679. fprintf(f, "\n");
  680. }
  681. }
  682. }
  683. #ifndef CONFIG_NO_CONFIG_BLOBS
  684. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  685. {
  686. unsigned char *encoded;
  687. encoded = base64_encode(blob->data, blob->len, NULL);
  688. if (encoded == NULL)
  689. return -1;
  690. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  691. os_free(encoded);
  692. return 0;
  693. }
  694. #endif /* CONFIG_NO_CONFIG_BLOBS */
  695. static void write_global_bin(FILE *f, const char *field,
  696. const struct wpabuf *val)
  697. {
  698. size_t i;
  699. const u8 *pos;
  700. if (val == NULL)
  701. return;
  702. fprintf(f, "%s=", field);
  703. pos = wpabuf_head(val);
  704. for (i = 0; i < wpabuf_len(val); i++)
  705. fprintf(f, "%02X", *pos++);
  706. fprintf(f, "\n");
  707. }
  708. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  709. {
  710. #ifdef CONFIG_CTRL_IFACE
  711. if (config->ctrl_interface)
  712. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  713. if (config->ctrl_interface_group)
  714. fprintf(f, "ctrl_interface_group=%s\n",
  715. config->ctrl_interface_group);
  716. #endif /* CONFIG_CTRL_IFACE */
  717. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  718. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  719. if (config->ap_scan != DEFAULT_AP_SCAN)
  720. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  721. if (config->disable_scan_offload)
  722. fprintf(f, "disable_scan_offload=%d\n",
  723. config->disable_scan_offload);
  724. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  725. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  726. if (config->opensc_engine_path)
  727. fprintf(f, "opensc_engine_path=%s\n",
  728. config->opensc_engine_path);
  729. if (config->pkcs11_engine_path)
  730. fprintf(f, "pkcs11_engine_path=%s\n",
  731. config->pkcs11_engine_path);
  732. if (config->pkcs11_module_path)
  733. fprintf(f, "pkcs11_module_path=%s\n",
  734. config->pkcs11_module_path);
  735. if (config->pcsc_reader)
  736. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  737. if (config->pcsc_pin)
  738. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  739. if (config->driver_param)
  740. fprintf(f, "driver_param=%s\n", config->driver_param);
  741. if (config->dot11RSNAConfigPMKLifetime)
  742. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  743. config->dot11RSNAConfigPMKLifetime);
  744. if (config->dot11RSNAConfigPMKReauthThreshold)
  745. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  746. config->dot11RSNAConfigPMKReauthThreshold);
  747. if (config->dot11RSNAConfigSATimeout)
  748. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  749. config->dot11RSNAConfigSATimeout);
  750. if (config->update_config)
  751. fprintf(f, "update_config=%d\n", config->update_config);
  752. #ifdef CONFIG_WPS
  753. if (!is_nil_uuid(config->uuid)) {
  754. char buf[40];
  755. uuid_bin2str(config->uuid, buf, sizeof(buf));
  756. fprintf(f, "uuid=%s\n", buf);
  757. }
  758. if (config->device_name)
  759. fprintf(f, "device_name=%s\n", config->device_name);
  760. if (config->manufacturer)
  761. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  762. if (config->model_name)
  763. fprintf(f, "model_name=%s\n", config->model_name);
  764. if (config->model_number)
  765. fprintf(f, "model_number=%s\n", config->model_number);
  766. if (config->serial_number)
  767. fprintf(f, "serial_number=%s\n", config->serial_number);
  768. {
  769. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  770. buf = wps_dev_type_bin2str(config->device_type,
  771. _buf, sizeof(_buf));
  772. if (os_strcmp(buf, "0-00000000-0") != 0)
  773. fprintf(f, "device_type=%s\n", buf);
  774. }
  775. if (WPA_GET_BE32(config->os_version))
  776. fprintf(f, "os_version=%08x\n",
  777. WPA_GET_BE32(config->os_version));
  778. if (config->config_methods)
  779. fprintf(f, "config_methods=%s\n", config->config_methods);
  780. if (config->wps_cred_processing)
  781. fprintf(f, "wps_cred_processing=%d\n",
  782. config->wps_cred_processing);
  783. if (config->wps_vendor_ext_m1) {
  784. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  785. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  786. if (len > 0) {
  787. fprintf(f, "wps_vendor_ext_m1=");
  788. for (i = 0; i < len; i++)
  789. fprintf(f, "%02x", *p++);
  790. fprintf(f, "\n");
  791. }
  792. }
  793. #endif /* CONFIG_WPS */
  794. #ifdef CONFIG_P2P
  795. if (config->p2p_listen_reg_class)
  796. fprintf(f, "p2p_listen_reg_class=%u\n",
  797. config->p2p_listen_reg_class);
  798. if (config->p2p_listen_channel)
  799. fprintf(f, "p2p_listen_channel=%u\n",
  800. config->p2p_listen_channel);
  801. if (config->p2p_oper_reg_class)
  802. fprintf(f, "p2p_oper_reg_class=%u\n",
  803. config->p2p_oper_reg_class);
  804. if (config->p2p_oper_channel)
  805. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  806. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  807. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  808. if (config->p2p_ssid_postfix)
  809. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  810. if (config->persistent_reconnect)
  811. fprintf(f, "persistent_reconnect=%u\n",
  812. config->persistent_reconnect);
  813. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  814. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  815. if (config->p2p_group_idle)
  816. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  817. if (config->p2p_pref_chan) {
  818. unsigned int i;
  819. fprintf(f, "p2p_pref_chan=");
  820. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  821. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  822. config->p2p_pref_chan[i].op_class,
  823. config->p2p_pref_chan[i].chan);
  824. }
  825. fprintf(f, "\n");
  826. }
  827. if (config->p2p_no_go_freq.num) {
  828. char *val = freq_range_list_str(&config->p2p_no_go_freq);
  829. if (val) {
  830. fprintf(f, "p2p_no_go_freq=%s\n", val);
  831. os_free(val);
  832. }
  833. }
  834. if (config->p2p_add_cli_chan)
  835. fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
  836. if (config->p2p_go_ht40)
  837. fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
  838. if (config->p2p_go_vht)
  839. fprintf(f, "p2p_go_vht=%u\n", config->p2p_go_vht);
  840. if (config->p2p_disabled)
  841. fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
  842. if (config->p2p_no_group_iface)
  843. fprintf(f, "p2p_no_group_iface=%u\n",
  844. config->p2p_no_group_iface);
  845. if (config->p2p_ignore_shared_freq)
  846. fprintf(f, "p2p_ignore_shared_freq=%u\n",
  847. config->p2p_ignore_shared_freq);
  848. #endif /* CONFIG_P2P */
  849. if (config->country[0] && config->country[1]) {
  850. fprintf(f, "country=%c%c\n",
  851. config->country[0], config->country[1]);
  852. }
  853. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  854. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  855. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  856. fprintf(f, "bss_expiration_age=%u\n",
  857. config->bss_expiration_age);
  858. if (config->bss_expiration_scan_count !=
  859. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  860. fprintf(f, "bss_expiration_scan_count=%u\n",
  861. config->bss_expiration_scan_count);
  862. if (config->filter_ssids)
  863. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  864. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  865. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  866. if (config->disassoc_low_ack)
  867. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  868. #ifdef CONFIG_HS20
  869. if (config->hs20)
  870. fprintf(f, "hs20=1\n");
  871. #endif /* CONFIG_HS20 */
  872. #ifdef CONFIG_INTERWORKING
  873. if (config->interworking)
  874. fprintf(f, "interworking=%u\n", config->interworking);
  875. if (!is_zero_ether_addr(config->hessid))
  876. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  877. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  878. fprintf(f, "access_network_type=%d\n",
  879. config->access_network_type);
  880. #endif /* CONFIG_INTERWORKING */
  881. if (config->pbc_in_m1)
  882. fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
  883. if (config->wps_nfc_pw_from_config) {
  884. if (config->wps_nfc_dev_pw_id)
  885. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  886. config->wps_nfc_dev_pw_id);
  887. write_global_bin(f, "wps_nfc_dh_pubkey",
  888. config->wps_nfc_dh_pubkey);
  889. write_global_bin(f, "wps_nfc_dh_privkey",
  890. config->wps_nfc_dh_privkey);
  891. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  892. }
  893. if (config->ext_password_backend)
  894. fprintf(f, "ext_password_backend=%s\n",
  895. config->ext_password_backend);
  896. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  897. fprintf(f, "p2p_go_max_inactivity=%d\n",
  898. config->p2p_go_max_inactivity);
  899. if (config->auto_interworking)
  900. fprintf(f, "auto_interworking=%d\n",
  901. config->auto_interworking);
  902. if (config->okc)
  903. fprintf(f, "okc=%d\n", config->okc);
  904. if (config->pmf)
  905. fprintf(f, "pmf=%d\n", config->pmf);
  906. if (config->dtim_period)
  907. fprintf(f, "dtim_period=%d\n", config->dtim_period);
  908. if (config->beacon_int)
  909. fprintf(f, "beacon_int=%d\n", config->beacon_int);
  910. if (config->sae_groups) {
  911. int i;
  912. fprintf(f, "sae_groups=");
  913. for (i = 0; config->sae_groups[i] >= 0; i++) {
  914. fprintf(f, "%s%d", i > 0 ? " " : "",
  915. config->sae_groups[i]);
  916. }
  917. fprintf(f, "\n");
  918. }
  919. if (config->ap_vendor_elements) {
  920. int i, len = wpabuf_len(config->ap_vendor_elements);
  921. const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
  922. if (len > 0) {
  923. fprintf(f, "ap_vendor_elements=");
  924. for (i = 0; i < len; i++)
  925. fprintf(f, "%02x", *p++);
  926. fprintf(f, "\n");
  927. }
  928. }
  929. if (config->ignore_old_scan_res)
  930. fprintf(f, "ignore_old_scan_res=%d\n",
  931. config->ignore_old_scan_res);
  932. if (config->freq_list && config->freq_list[0]) {
  933. int i;
  934. fprintf(f, "freq_list=");
  935. for (i = 0; config->freq_list[i]; i++) {
  936. fprintf(f, "%s%u", i > 0 ? " " : "",
  937. config->freq_list[i]);
  938. }
  939. fprintf(f, "\n");
  940. }
  941. if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
  942. fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
  943. if (config->sched_scan_interval)
  944. fprintf(f, "sched_scan_interval=%u\n",
  945. config->sched_scan_interval);
  946. if (config->external_sim)
  947. fprintf(f, "external_sim=%d\n", config->external_sim);
  948. if (config->tdls_external_control)
  949. fprintf(f, "tdls_external_control=%d\n",
  950. config->tdls_external_control);
  951. }
  952. #endif /* CONFIG_NO_CONFIG_WRITE */
  953. int wpa_config_write(const char *name, struct wpa_config *config)
  954. {
  955. #ifndef CONFIG_NO_CONFIG_WRITE
  956. FILE *f;
  957. struct wpa_ssid *ssid;
  958. struct wpa_cred *cred;
  959. #ifndef CONFIG_NO_CONFIG_BLOBS
  960. struct wpa_config_blob *blob;
  961. #endif /* CONFIG_NO_CONFIG_BLOBS */
  962. int ret = 0;
  963. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  964. f = fopen(name, "w");
  965. if (f == NULL) {
  966. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  967. return -1;
  968. }
  969. wpa_config_write_global(f, config);
  970. for (cred = config->cred; cred; cred = cred->next) {
  971. fprintf(f, "\ncred={\n");
  972. wpa_config_write_cred(f, cred);
  973. fprintf(f, "}\n");
  974. }
  975. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  976. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  977. continue; /* do not save temporary networks */
  978. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  979. !ssid->passphrase)
  980. continue; /* do not save invalid network */
  981. fprintf(f, "\nnetwork={\n");
  982. wpa_config_write_network(f, ssid);
  983. fprintf(f, "}\n");
  984. }
  985. #ifndef CONFIG_NO_CONFIG_BLOBS
  986. for (blob = config->blobs; blob; blob = blob->next) {
  987. ret = wpa_config_write_blob(f, blob);
  988. if (ret)
  989. break;
  990. }
  991. #endif /* CONFIG_NO_CONFIG_BLOBS */
  992. fclose(f);
  993. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  994. name, ret ? "un" : "");
  995. return ret;
  996. #else /* CONFIG_NO_CONFIG_WRITE */
  997. return -1;
  998. #endif /* CONFIG_NO_CONFIG_WRITE */
  999. }