config_file.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  575. INT(disabled);
  576. INT(peerkey);
  577. #ifdef CONFIG_IEEE80211W
  578. write_int(f, "ieee80211w", ssid->ieee80211w,
  579. MGMT_FRAME_PROTECTION_DEFAULT);
  580. #endif /* CONFIG_IEEE80211W */
  581. STR(id_str);
  582. #ifdef CONFIG_P2P
  583. write_p2p_client_list(f, ssid);
  584. #endif /* CONFIG_P2P */
  585. #undef STR
  586. #undef INT
  587. #undef INT_DEF
  588. }
  589. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  590. {
  591. if (cred->priority)
  592. fprintf(f, "\tpriority=%d\n", cred->priority);
  593. if (cred->pcsc)
  594. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  595. if (cred->realm)
  596. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  597. if (cred->username)
  598. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  599. if (cred->password && cred->ext_password)
  600. fprintf(f, "\tpassword=ext:%s\n", cred->password);
  601. else if (cred->password)
  602. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  603. if (cred->ca_cert)
  604. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  605. if (cred->client_cert)
  606. fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
  607. if (cred->private_key)
  608. fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
  609. if (cred->private_key_passwd)
  610. fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
  611. cred->private_key_passwd);
  612. if (cred->imsi)
  613. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  614. if (cred->milenage)
  615. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  616. if (cred->domain)
  617. fprintf(f, "\tdomain=\"%s\"\n", cred->domain);
  618. if (cred->roaming_consortium_len) {
  619. size_t i;
  620. fprintf(f, "\troaming_consortium=");
  621. for (i = 0; i < cred->roaming_consortium_len; i++)
  622. fprintf(f, "%02x", cred->roaming_consortium[i]);
  623. fprintf(f, "\n");
  624. }
  625. if (cred->eap_method) {
  626. const char *name;
  627. name = eap_get_name(cred->eap_method[0].vendor,
  628. cred->eap_method[0].method);
  629. fprintf(f, "\teap=%s\n", name);
  630. }
  631. if (cred->phase1)
  632. fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
  633. if (cred->phase2)
  634. fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
  635. if (cred->excluded_ssid) {
  636. size_t i, j;
  637. for (i = 0; i < cred->num_excluded_ssid; i++) {
  638. struct excluded_ssid *e = &cred->excluded_ssid[i];
  639. fprintf(f, "\texcluded_ssid=");
  640. for (j = 0; j < e->ssid_len; j++)
  641. fprintf(f, "%02x", e->ssid[j]);
  642. fprintf(f, "\n");
  643. }
  644. }
  645. }
  646. #ifndef CONFIG_NO_CONFIG_BLOBS
  647. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  648. {
  649. unsigned char *encoded;
  650. encoded = base64_encode(blob->data, blob->len, NULL);
  651. if (encoded == NULL)
  652. return -1;
  653. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  654. os_free(encoded);
  655. return 0;
  656. }
  657. #endif /* CONFIG_NO_CONFIG_BLOBS */
  658. static void write_global_bin(FILE *f, const char *field,
  659. const struct wpabuf *val)
  660. {
  661. size_t i;
  662. const u8 *pos;
  663. if (val == NULL)
  664. return;
  665. fprintf(f, "%s=", field);
  666. pos = wpabuf_head(val);
  667. for (i = 0; i < wpabuf_len(val); i++)
  668. fprintf(f, "%02X", *pos++);
  669. fprintf(f, "\n");
  670. }
  671. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  672. {
  673. #ifdef CONFIG_CTRL_IFACE
  674. if (config->ctrl_interface)
  675. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  676. if (config->ctrl_interface_group)
  677. fprintf(f, "ctrl_interface_group=%s\n",
  678. config->ctrl_interface_group);
  679. #endif /* CONFIG_CTRL_IFACE */
  680. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  681. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  682. if (config->ap_scan != DEFAULT_AP_SCAN)
  683. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  684. if (config->disable_scan_offload)
  685. fprintf(f, "disable_scan_offload=%d\n",
  686. config->disable_scan_offload);
  687. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  688. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  689. if (config->opensc_engine_path)
  690. fprintf(f, "opensc_engine_path=%s\n",
  691. config->opensc_engine_path);
  692. if (config->pkcs11_engine_path)
  693. fprintf(f, "pkcs11_engine_path=%s\n",
  694. config->pkcs11_engine_path);
  695. if (config->pkcs11_module_path)
  696. fprintf(f, "pkcs11_module_path=%s\n",
  697. config->pkcs11_module_path);
  698. if (config->pcsc_reader)
  699. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  700. if (config->pcsc_pin)
  701. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  702. if (config->driver_param)
  703. fprintf(f, "driver_param=%s\n", config->driver_param);
  704. if (config->dot11RSNAConfigPMKLifetime)
  705. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  706. config->dot11RSNAConfigPMKLifetime);
  707. if (config->dot11RSNAConfigPMKReauthThreshold)
  708. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  709. config->dot11RSNAConfigPMKReauthThreshold);
  710. if (config->dot11RSNAConfigSATimeout)
  711. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  712. config->dot11RSNAConfigSATimeout);
  713. if (config->update_config)
  714. fprintf(f, "update_config=%d\n", config->update_config);
  715. #ifdef CONFIG_WPS
  716. if (!is_nil_uuid(config->uuid)) {
  717. char buf[40];
  718. uuid_bin2str(config->uuid, buf, sizeof(buf));
  719. fprintf(f, "uuid=%s\n", buf);
  720. }
  721. if (config->device_name)
  722. fprintf(f, "device_name=%s\n", config->device_name);
  723. if (config->manufacturer)
  724. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  725. if (config->model_name)
  726. fprintf(f, "model_name=%s\n", config->model_name);
  727. if (config->model_number)
  728. fprintf(f, "model_number=%s\n", config->model_number);
  729. if (config->serial_number)
  730. fprintf(f, "serial_number=%s\n", config->serial_number);
  731. {
  732. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  733. buf = wps_dev_type_bin2str(config->device_type,
  734. _buf, sizeof(_buf));
  735. if (os_strcmp(buf, "0-00000000-0") != 0)
  736. fprintf(f, "device_type=%s\n", buf);
  737. }
  738. if (WPA_GET_BE32(config->os_version))
  739. fprintf(f, "os_version=%08x\n",
  740. WPA_GET_BE32(config->os_version));
  741. if (config->config_methods)
  742. fprintf(f, "config_methods=%s\n", config->config_methods);
  743. if (config->wps_cred_processing)
  744. fprintf(f, "wps_cred_processing=%d\n",
  745. config->wps_cred_processing);
  746. if (config->wps_vendor_ext_m1) {
  747. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  748. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  749. if (len > 0) {
  750. fprintf(f, "wps_vendor_ext_m1=");
  751. for (i = 0; i < len; i++)
  752. fprintf(f, "%02x", *p++);
  753. fprintf(f, "\n");
  754. }
  755. }
  756. #endif /* CONFIG_WPS */
  757. #ifdef CONFIG_P2P
  758. if (config->p2p_listen_reg_class)
  759. fprintf(f, "p2p_listen_reg_class=%u\n",
  760. config->p2p_listen_reg_class);
  761. if (config->p2p_listen_channel)
  762. fprintf(f, "p2p_listen_channel=%u\n",
  763. config->p2p_listen_channel);
  764. if (config->p2p_oper_reg_class)
  765. fprintf(f, "p2p_oper_reg_class=%u\n",
  766. config->p2p_oper_reg_class);
  767. if (config->p2p_oper_channel)
  768. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  769. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  770. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  771. if (config->p2p_ssid_postfix)
  772. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  773. if (config->persistent_reconnect)
  774. fprintf(f, "persistent_reconnect=%u\n",
  775. config->persistent_reconnect);
  776. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  777. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  778. if (config->p2p_group_idle)
  779. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  780. if (config->p2p_pref_chan) {
  781. unsigned int i;
  782. fprintf(f, "p2p_pref_chan=");
  783. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  784. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  785. config->p2p_pref_chan[i].op_class,
  786. config->p2p_pref_chan[i].chan);
  787. }
  788. fprintf(f, "\n");
  789. }
  790. if (config->p2p_go_ht40)
  791. fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
  792. if (config->p2p_disabled)
  793. fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
  794. if (config->p2p_no_group_iface)
  795. fprintf(f, "p2p_no_group_iface=%u\n",
  796. config->p2p_no_group_iface);
  797. #endif /* CONFIG_P2P */
  798. if (config->country[0] && config->country[1]) {
  799. fprintf(f, "country=%c%c\n",
  800. config->country[0], config->country[1]);
  801. }
  802. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  803. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  804. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  805. fprintf(f, "bss_expiration_age=%u\n",
  806. config->bss_expiration_age);
  807. if (config->bss_expiration_scan_count !=
  808. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  809. fprintf(f, "bss_expiration_scan_count=%u\n",
  810. config->bss_expiration_scan_count);
  811. if (config->filter_ssids)
  812. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  813. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  814. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  815. if (config->disassoc_low_ack)
  816. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  817. #ifdef CONFIG_HS20
  818. if (config->hs20)
  819. fprintf(f, "hs20=1\n");
  820. #endif /* CONFIG_HS20 */
  821. #ifdef CONFIG_INTERWORKING
  822. if (config->interworking)
  823. fprintf(f, "interworking=%u\n", config->interworking);
  824. if (!is_zero_ether_addr(config->hessid))
  825. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  826. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  827. fprintf(f, "access_network_type=%d\n",
  828. config->access_network_type);
  829. #endif /* CONFIG_INTERWORKING */
  830. if (config->pbc_in_m1)
  831. fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
  832. if (config->wps_nfc_dev_pw_id)
  833. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  834. config->wps_nfc_dev_pw_id);
  835. write_global_bin(f, "wps_nfc_dh_pubkey", config->wps_nfc_dh_pubkey);
  836. write_global_bin(f, "wps_nfc_dh_privkey", config->wps_nfc_dh_privkey);
  837. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  838. if (config->ext_password_backend)
  839. fprintf(f, "ext_password_backend=%s\n",
  840. config->ext_password_backend);
  841. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  842. fprintf(f, "p2p_go_max_inactivity=%d\n",
  843. config->p2p_go_max_inactivity);
  844. if (config->auto_interworking)
  845. fprintf(f, "auto_interworking=%d\n",
  846. config->auto_interworking);
  847. if (config->okc)
  848. fprintf(f, "okc=%d\n", config->okc);
  849. if (config->pmf)
  850. fprintf(f, "pmf=%d\n", config->pmf);
  851. }
  852. #endif /* CONFIG_NO_CONFIG_WRITE */
  853. int wpa_config_write(const char *name, struct wpa_config *config)
  854. {
  855. #ifndef CONFIG_NO_CONFIG_WRITE
  856. FILE *f;
  857. struct wpa_ssid *ssid;
  858. struct wpa_cred *cred;
  859. #ifndef CONFIG_NO_CONFIG_BLOBS
  860. struct wpa_config_blob *blob;
  861. #endif /* CONFIG_NO_CONFIG_BLOBS */
  862. int ret = 0;
  863. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  864. f = fopen(name, "w");
  865. if (f == NULL) {
  866. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  867. return -1;
  868. }
  869. wpa_config_write_global(f, config);
  870. for (cred = config->cred; cred; cred = cred->next) {
  871. fprintf(f, "\ncred={\n");
  872. wpa_config_write_cred(f, cred);
  873. fprintf(f, "}\n");
  874. }
  875. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  876. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  877. continue; /* do not save temporary networks */
  878. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  879. !ssid->passphrase)
  880. continue; /* do not save invalid network */
  881. fprintf(f, "\nnetwork={\n");
  882. wpa_config_write_network(f, ssid);
  883. fprintf(f, "}\n");
  884. }
  885. #ifndef CONFIG_NO_CONFIG_BLOBS
  886. for (blob = config->blobs; blob; blob = blob->next) {
  887. ret = wpa_config_write_blob(f, blob);
  888. if (ret)
  889. break;
  890. }
  891. #endif /* CONFIG_NO_CONFIG_BLOBS */
  892. fclose(f);
  893. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  894. name, ret ? "un" : "");
  895. return ret;
  896. #else /* CONFIG_NO_CONFIG_WRITE */
  897. return -1;
  898. #endif /* CONFIG_NO_CONFIG_WRITE */
  899. }