config_file.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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. #ifdef ANDROID
  14. #include <sys/stat.h>
  15. #endif /* ANDROID */
  16. #include "common.h"
  17. #include "config.h"
  18. #include "base64.h"
  19. #include "uuid.h"
  20. #include "p2p/p2p.h"
  21. #include "eap_peer/eap_methods.h"
  22. #include "eap_peer/eap.h"
  23. static int newline_terminated(const char *buf, size_t buflen)
  24. {
  25. size_t len = os_strlen(buf);
  26. if (len == 0)
  27. return 0;
  28. if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
  29. buf[len - 1] != '\n')
  30. return 0;
  31. return 1;
  32. }
  33. static void skip_line_end(FILE *stream)
  34. {
  35. char buf[100];
  36. while (fgets(buf, sizeof(buf), stream)) {
  37. buf[sizeof(buf) - 1] = '\0';
  38. if (newline_terminated(buf, sizeof(buf)))
  39. return;
  40. }
  41. }
  42. /**
  43. * wpa_config_get_line - Read the next configuration file line
  44. * @s: Buffer for the line
  45. * @size: The buffer length
  46. * @stream: File stream to read from
  47. * @line: Pointer to a variable storing the file line number
  48. * @_pos: Buffer for the pointer to the beginning of data on the text line or
  49. * %NULL if not needed (returned value used instead)
  50. * Returns: Pointer to the beginning of data on the text line or %NULL if no
  51. * more text lines are available.
  52. *
  53. * This function reads the next non-empty line from the configuration file and
  54. * removes comments. The returned string is guaranteed to be null-terminated.
  55. */
  56. static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
  57. char **_pos)
  58. {
  59. char *pos, *end, *sstart;
  60. while (fgets(s, size, stream)) {
  61. (*line)++;
  62. s[size - 1] = '\0';
  63. if (!newline_terminated(s, size)) {
  64. /*
  65. * The line was truncated - skip rest of it to avoid
  66. * confusing error messages.
  67. */
  68. wpa_printf(MSG_INFO, "Long line in configuration file "
  69. "truncated");
  70. skip_line_end(stream);
  71. }
  72. pos = s;
  73. /* Skip white space from the beginning of line. */
  74. while (*pos == ' ' || *pos == '\t' || *pos == '\r')
  75. pos++;
  76. /* Skip comment lines and empty lines */
  77. if (*pos == '#' || *pos == '\n' || *pos == '\0')
  78. continue;
  79. /*
  80. * Remove # comments unless they are within a double quoted
  81. * string.
  82. */
  83. sstart = os_strchr(pos, '"');
  84. if (sstart)
  85. sstart = os_strrchr(sstart + 1, '"');
  86. if (!sstart)
  87. sstart = pos;
  88. end = os_strchr(sstart, '#');
  89. if (end)
  90. *end-- = '\0';
  91. else
  92. end = pos + os_strlen(pos) - 1;
  93. /* Remove trailing white space. */
  94. while (end > pos &&
  95. (*end == '\n' || *end == ' ' || *end == '\t' ||
  96. *end == '\r'))
  97. *end-- = '\0';
  98. if (*pos == '\0')
  99. continue;
  100. if (_pos)
  101. *_pos = pos;
  102. return pos;
  103. }
  104. if (_pos)
  105. *_pos = NULL;
  106. return NULL;
  107. }
  108. static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
  109. {
  110. int errors = 0;
  111. if (ssid->passphrase) {
  112. if (ssid->psk_set) {
  113. wpa_printf(MSG_ERROR, "Line %d: both PSK and "
  114. "passphrase configured.", line);
  115. errors++;
  116. }
  117. wpa_config_update_psk(ssid);
  118. }
  119. if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
  120. !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  121. !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
  122. /* Group cipher cannot be stronger than the pairwise cipher. */
  123. wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
  124. " list since it was not allowed for pairwise "
  125. "cipher", line);
  126. ssid->group_cipher &= ~WPA_CIPHER_CCMP;
  127. }
  128. if (ssid->mode == WPAS_MODE_MESH &&
  129. (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
  130. ssid->key_mgmt != WPA_KEY_MGMT_SAE)) {
  131. wpa_printf(MSG_ERROR,
  132. "Line %d: key_mgmt for mesh network should be open or SAE",
  133. line);
  134. errors++;
  135. }
  136. return errors;
  137. }
  138. static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
  139. {
  140. struct wpa_ssid *ssid;
  141. int errors = 0, end = 0;
  142. char buf[2000], *pos, *pos2;
  143. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
  144. *line);
  145. ssid = os_zalloc(sizeof(*ssid));
  146. if (ssid == NULL)
  147. return NULL;
  148. dl_list_init(&ssid->psk_list);
  149. ssid->id = id;
  150. wpa_config_set_network_defaults(ssid);
  151. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  152. if (os_strcmp(pos, "}") == 0) {
  153. end = 1;
  154. break;
  155. }
  156. pos2 = os_strchr(pos, '=');
  157. if (pos2 == NULL) {
  158. wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
  159. "'%s'.", *line, pos);
  160. errors++;
  161. continue;
  162. }
  163. *pos2++ = '\0';
  164. if (*pos2 == '"') {
  165. if (os_strchr(pos2 + 1, '"') == NULL) {
  166. wpa_printf(MSG_ERROR, "Line %d: invalid "
  167. "quotation '%s'.", *line, pos2);
  168. errors++;
  169. continue;
  170. }
  171. }
  172. if (wpa_config_set(ssid, pos, pos2, *line) < 0)
  173. errors++;
  174. }
  175. if (!end) {
  176. wpa_printf(MSG_ERROR, "Line %d: network block was not "
  177. "terminated properly.", *line);
  178. errors++;
  179. }
  180. errors += wpa_config_validate_network(ssid, *line);
  181. if (errors) {
  182. wpa_config_free_ssid(ssid);
  183. ssid = NULL;
  184. }
  185. return ssid;
  186. }
  187. static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
  188. {
  189. struct wpa_cred *cred;
  190. int errors = 0, end = 0;
  191. char buf[256], *pos, *pos2;
  192. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
  193. cred = os_zalloc(sizeof(*cred));
  194. if (cred == NULL)
  195. return NULL;
  196. cred->id = id;
  197. cred->sim_num = DEFAULT_USER_SELECTED_SIM;
  198. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  199. if (os_strcmp(pos, "}") == 0) {
  200. end = 1;
  201. break;
  202. }
  203. pos2 = os_strchr(pos, '=');
  204. if (pos2 == NULL) {
  205. wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
  206. "'%s'.", *line, pos);
  207. errors++;
  208. continue;
  209. }
  210. *pos2++ = '\0';
  211. if (*pos2 == '"') {
  212. if (os_strchr(pos2 + 1, '"') == NULL) {
  213. wpa_printf(MSG_ERROR, "Line %d: invalid "
  214. "quotation '%s'.", *line, pos2);
  215. errors++;
  216. continue;
  217. }
  218. }
  219. if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
  220. errors++;
  221. }
  222. if (!end) {
  223. wpa_printf(MSG_ERROR, "Line %d: cred block was not "
  224. "terminated properly.", *line);
  225. errors++;
  226. }
  227. if (errors) {
  228. wpa_config_free_cred(cred);
  229. cred = NULL;
  230. }
  231. return cred;
  232. }
  233. #ifndef CONFIG_NO_CONFIG_BLOBS
  234. static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
  235. const char *name)
  236. {
  237. struct wpa_config_blob *blob;
  238. char buf[256], *pos;
  239. unsigned char *encoded = NULL, *nencoded;
  240. int end = 0;
  241. size_t encoded_len = 0, len;
  242. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
  243. *line, name);
  244. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  245. if (os_strcmp(pos, "}") == 0) {
  246. end = 1;
  247. break;
  248. }
  249. len = os_strlen(pos);
  250. nencoded = os_realloc(encoded, encoded_len + len);
  251. if (nencoded == NULL) {
  252. wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
  253. "blob", *line);
  254. os_free(encoded);
  255. return NULL;
  256. }
  257. encoded = nencoded;
  258. os_memcpy(encoded + encoded_len, pos, len);
  259. encoded_len += len;
  260. }
  261. if (!end) {
  262. wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
  263. "properly", *line);
  264. os_free(encoded);
  265. return NULL;
  266. }
  267. blob = os_zalloc(sizeof(*blob));
  268. if (blob == NULL) {
  269. os_free(encoded);
  270. return NULL;
  271. }
  272. blob->name = os_strdup(name);
  273. blob->data = base64_decode(encoded, encoded_len, &blob->len);
  274. os_free(encoded);
  275. if (blob->name == NULL || blob->data == NULL) {
  276. wpa_config_free_blob(blob);
  277. return NULL;
  278. }
  279. return blob;
  280. }
  281. static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
  282. int *line, char *bname)
  283. {
  284. char *name_end;
  285. struct wpa_config_blob *blob;
  286. name_end = os_strchr(bname, '=');
  287. if (name_end == NULL) {
  288. wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
  289. *line);
  290. return -1;
  291. }
  292. *name_end = '\0';
  293. blob = wpa_config_read_blob(f, line, bname);
  294. if (blob == NULL) {
  295. wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
  296. *line, bname);
  297. return -1;
  298. }
  299. wpa_config_set_blob(config, blob);
  300. return 0;
  301. }
  302. #endif /* CONFIG_NO_CONFIG_BLOBS */
  303. struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
  304. {
  305. FILE *f;
  306. char buf[512], *pos;
  307. int errors = 0, line = 0;
  308. struct wpa_ssid *ssid, *tail, *head;
  309. struct wpa_cred *cred, *cred_tail, *cred_head;
  310. struct wpa_config *config;
  311. int id = 0;
  312. int cred_id = 0;
  313. if (name == NULL)
  314. return NULL;
  315. if (cfgp)
  316. config = cfgp;
  317. else
  318. config = wpa_config_alloc_empty(NULL, NULL);
  319. if (config == NULL) {
  320. wpa_printf(MSG_ERROR, "Failed to allocate config file "
  321. "structure");
  322. return NULL;
  323. }
  324. tail = head = config->ssid;
  325. while (tail && tail->next)
  326. tail = tail->next;
  327. cred_tail = cred_head = config->cred;
  328. while (cred_tail && cred_tail->next)
  329. cred_tail = cred_tail->next;
  330. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  331. f = fopen(name, "r");
  332. if (f == NULL) {
  333. wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
  334. "error: %s", name, strerror(errno));
  335. os_free(config);
  336. return NULL;
  337. }
  338. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  339. if (os_strcmp(pos, "network={") == 0) {
  340. ssid = wpa_config_read_network(f, &line, id++);
  341. if (ssid == NULL) {
  342. wpa_printf(MSG_ERROR, "Line %d: failed to "
  343. "parse network block.", line);
  344. errors++;
  345. continue;
  346. }
  347. if (head == NULL) {
  348. head = tail = ssid;
  349. } else {
  350. tail->next = ssid;
  351. tail = ssid;
  352. }
  353. if (wpa_config_add_prio_network(config, ssid)) {
  354. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  355. "network block to priority list.",
  356. line);
  357. errors++;
  358. continue;
  359. }
  360. } else if (os_strcmp(pos, "cred={") == 0) {
  361. cred = wpa_config_read_cred(f, &line, cred_id++);
  362. if (cred == NULL) {
  363. wpa_printf(MSG_ERROR, "Line %d: failed to "
  364. "parse cred block.", line);
  365. errors++;
  366. continue;
  367. }
  368. if (cred_head == NULL) {
  369. cred_head = cred_tail = cred;
  370. } else {
  371. cred_tail->next = cred;
  372. cred_tail = cred;
  373. }
  374. #ifndef CONFIG_NO_CONFIG_BLOBS
  375. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  376. if (wpa_config_process_blob(config, f, &line, pos + 12)
  377. < 0) {
  378. wpa_printf(MSG_ERROR, "Line %d: failed to "
  379. "process blob.", line);
  380. errors++;
  381. continue;
  382. }
  383. #endif /* CONFIG_NO_CONFIG_BLOBS */
  384. } else if (wpa_config_process_global(config, pos, line) < 0) {
  385. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  386. "line '%s'.", line, pos);
  387. errors++;
  388. continue;
  389. }
  390. }
  391. fclose(f);
  392. config->ssid = head;
  393. wpa_config_debug_dump_networks(config);
  394. config->cred = cred_head;
  395. #ifndef WPA_IGNORE_CONFIG_ERRORS
  396. if (errors) {
  397. wpa_config_free(config);
  398. config = NULL;
  399. head = NULL;
  400. }
  401. #endif /* WPA_IGNORE_CONFIG_ERRORS */
  402. return config;
  403. }
  404. #ifndef CONFIG_NO_CONFIG_WRITE
  405. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  406. {
  407. char *value = wpa_config_get(ssid, field);
  408. if (value == NULL)
  409. return;
  410. fprintf(f, "\t%s=%s\n", field, value);
  411. os_free(value);
  412. }
  413. static void write_int(FILE *f, const char *field, int value, int def)
  414. {
  415. if (value == def)
  416. return;
  417. fprintf(f, "\t%s=%d\n", field, value);
  418. }
  419. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  420. {
  421. char *value = wpa_config_get(ssid, "bssid");
  422. if (value == NULL)
  423. return;
  424. fprintf(f, "\tbssid=%s\n", value);
  425. os_free(value);
  426. }
  427. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  428. {
  429. char *value = wpa_config_get(ssid, "psk");
  430. if (value == NULL)
  431. return;
  432. fprintf(f, "\tpsk=%s\n", value);
  433. os_free(value);
  434. }
  435. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  436. {
  437. char *value;
  438. if (ssid->proto == DEFAULT_PROTO)
  439. return;
  440. value = wpa_config_get(ssid, "proto");
  441. if (value == NULL)
  442. return;
  443. if (value[0])
  444. fprintf(f, "\tproto=%s\n", value);
  445. os_free(value);
  446. }
  447. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  448. {
  449. char *value;
  450. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  451. return;
  452. value = wpa_config_get(ssid, "key_mgmt");
  453. if (value == NULL)
  454. return;
  455. if (value[0])
  456. fprintf(f, "\tkey_mgmt=%s\n", value);
  457. os_free(value);
  458. }
  459. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  460. {
  461. char *value;
  462. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  463. return;
  464. value = wpa_config_get(ssid, "pairwise");
  465. if (value == NULL)
  466. return;
  467. if (value[0])
  468. fprintf(f, "\tpairwise=%s\n", value);
  469. os_free(value);
  470. }
  471. static void write_group(FILE *f, struct wpa_ssid *ssid)
  472. {
  473. char *value;
  474. if (ssid->group_cipher == DEFAULT_GROUP)
  475. return;
  476. value = wpa_config_get(ssid, "group");
  477. if (value == NULL)
  478. return;
  479. if (value[0])
  480. fprintf(f, "\tgroup=%s\n", value);
  481. os_free(value);
  482. }
  483. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  484. {
  485. char *value;
  486. if (ssid->auth_alg == 0)
  487. return;
  488. value = wpa_config_get(ssid, "auth_alg");
  489. if (value == NULL)
  490. return;
  491. if (value[0])
  492. fprintf(f, "\tauth_alg=%s\n", value);
  493. os_free(value);
  494. }
  495. #ifdef IEEE8021X_EAPOL
  496. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  497. {
  498. char *value;
  499. value = wpa_config_get(ssid, "eap");
  500. if (value == NULL)
  501. return;
  502. if (value[0])
  503. fprintf(f, "\teap=%s\n", value);
  504. os_free(value);
  505. }
  506. #endif /* IEEE8021X_EAPOL */
  507. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  508. {
  509. char field[20], *value;
  510. int res;
  511. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  512. if (os_snprintf_error(sizeof(field), res))
  513. return;
  514. value = wpa_config_get(ssid, field);
  515. if (value) {
  516. fprintf(f, "\t%s=%s\n", field, value);
  517. os_free(value);
  518. }
  519. }
  520. #ifdef CONFIG_P2P
  521. static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
  522. {
  523. char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
  524. if (value == NULL)
  525. return;
  526. fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
  527. os_free(value);
  528. }
  529. static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
  530. {
  531. char *value = wpa_config_get(ssid, "p2p_client_list");
  532. if (value == NULL)
  533. return;
  534. fprintf(f, "\tp2p_client_list=%s\n", value);
  535. os_free(value);
  536. }
  537. static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
  538. {
  539. struct psk_list_entry *psk;
  540. char hex[32 * 2 + 1];
  541. dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
  542. wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
  543. fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
  544. psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
  545. }
  546. }
  547. #endif /* CONFIG_P2P */
  548. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  549. {
  550. int i;
  551. #define STR(t) write_str(f, #t, ssid)
  552. #define INT(t) write_int(f, #t, ssid->t, 0)
  553. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  554. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  555. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  556. STR(ssid);
  557. INT(scan_ssid);
  558. write_bssid(f, ssid);
  559. write_psk(f, ssid);
  560. write_proto(f, ssid);
  561. write_key_mgmt(f, ssid);
  562. INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
  563. write_pairwise(f, ssid);
  564. write_group(f, ssid);
  565. write_auth_alg(f, ssid);
  566. STR(bgscan);
  567. STR(autoscan);
  568. STR(scan_freq);
  569. #ifdef IEEE8021X_EAPOL
  570. write_eap(f, ssid);
  571. STR(identity);
  572. STR(anonymous_identity);
  573. STR(password);
  574. STR(ca_cert);
  575. STR(ca_path);
  576. STR(client_cert);
  577. STR(private_key);
  578. STR(private_key_passwd);
  579. STR(dh_file);
  580. STR(subject_match);
  581. STR(altsubject_match);
  582. STR(domain_suffix_match);
  583. STR(domain_match);
  584. STR(ca_cert2);
  585. STR(ca_path2);
  586. STR(client_cert2);
  587. STR(private_key2);
  588. STR(private_key2_passwd);
  589. STR(dh_file2);
  590. STR(subject_match2);
  591. STR(altsubject_match2);
  592. STR(domain_suffix_match2);
  593. STR(domain_match2);
  594. STR(phase1);
  595. STR(phase2);
  596. STR(pcsc);
  597. STR(pin);
  598. STR(engine_id);
  599. STR(key_id);
  600. STR(cert_id);
  601. STR(ca_cert_id);
  602. STR(key2_id);
  603. STR(pin2);
  604. STR(engine2_id);
  605. STR(cert2_id);
  606. STR(ca_cert2_id);
  607. INTe(engine);
  608. INTe(engine2);
  609. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  610. INTe(erp);
  611. #endif /* IEEE8021X_EAPOL */
  612. for (i = 0; i < 4; i++)
  613. write_wep_key(f, i, ssid);
  614. INT(wep_tx_keyidx);
  615. INT(priority);
  616. #ifdef IEEE8021X_EAPOL
  617. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  618. STR(pac_file);
  619. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  620. INTe(ocsp);
  621. INT_DEFe(sim_num, DEFAULT_USER_SELECTED_SIM);
  622. #endif /* IEEE8021X_EAPOL */
  623. INT(mode);
  624. INT(frequency);
  625. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  626. INT(disabled);
  627. INT(peerkey);
  628. #ifdef CONFIG_IEEE80211W
  629. write_int(f, "ieee80211w", ssid->ieee80211w,
  630. MGMT_FRAME_PROTECTION_DEFAULT);
  631. #endif /* CONFIG_IEEE80211W */
  632. STR(id_str);
  633. #ifdef CONFIG_P2P
  634. write_go_p2p_dev_addr(f, ssid);
  635. write_p2p_client_list(f, ssid);
  636. write_psk_list(f, ssid);
  637. #endif /* CONFIG_P2P */
  638. INT(dtim_period);
  639. INT(beacon_int);
  640. #ifdef CONFIG_MACSEC
  641. INT(macsec_policy);
  642. #endif /* CONFIG_MACSEC */
  643. #ifdef CONFIG_HS20
  644. INT(update_identifier);
  645. #endif /* CONFIG_HS20 */
  646. write_int(f, "mac_addr", ssid->mac_addr, -1);
  647. #ifdef CONFIG_MESH
  648. STR(mesh_basic_rates);
  649. INT_DEF(dot11MeshMaxRetries, DEFAULT_MESH_MAX_RETRIES);
  650. INT_DEF(dot11MeshRetryTimeout, DEFAULT_MESH_RETRY_TIMEOUT);
  651. INT_DEF(dot11MeshConfirmTimeout, DEFAULT_MESH_CONFIRM_TIMEOUT);
  652. INT_DEF(dot11MeshHoldingTimeout, DEFAULT_MESH_HOLDING_TIMEOUT);
  653. #endif /* CONFIG_MESH */
  654. #undef STR
  655. #undef INT
  656. #undef INT_DEF
  657. }
  658. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  659. {
  660. size_t i;
  661. if (cred->priority)
  662. fprintf(f, "\tpriority=%d\n", cred->priority);
  663. if (cred->pcsc)
  664. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  665. if (cred->realm)
  666. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  667. if (cred->username)
  668. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  669. if (cred->password && cred->ext_password)
  670. fprintf(f, "\tpassword=ext:%s\n", cred->password);
  671. else if (cred->password)
  672. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  673. if (cred->ca_cert)
  674. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  675. if (cred->client_cert)
  676. fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
  677. if (cred->private_key)
  678. fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
  679. if (cred->private_key_passwd)
  680. fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
  681. cred->private_key_passwd);
  682. if (cred->imsi)
  683. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  684. if (cred->milenage)
  685. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  686. for (i = 0; i < cred->num_domain; i++)
  687. fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
  688. if (cred->domain_suffix_match)
  689. fprintf(f, "\tdomain_suffix_match=\"%s\"\n",
  690. cred->domain_suffix_match);
  691. if (cred->roaming_consortium_len) {
  692. fprintf(f, "\troaming_consortium=");
  693. for (i = 0; i < cred->roaming_consortium_len; i++)
  694. fprintf(f, "%02x", cred->roaming_consortium[i]);
  695. fprintf(f, "\n");
  696. }
  697. if (cred->eap_method) {
  698. const char *name;
  699. name = eap_get_name(cred->eap_method[0].vendor,
  700. cred->eap_method[0].method);
  701. if (name)
  702. fprintf(f, "\teap=%s\n", name);
  703. }
  704. if (cred->phase1)
  705. fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
  706. if (cred->phase2)
  707. fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
  708. if (cred->excluded_ssid) {
  709. size_t j;
  710. for (i = 0; i < cred->num_excluded_ssid; i++) {
  711. struct excluded_ssid *e = &cred->excluded_ssid[i];
  712. fprintf(f, "\texcluded_ssid=");
  713. for (j = 0; j < e->ssid_len; j++)
  714. fprintf(f, "%02x", e->ssid[j]);
  715. fprintf(f, "\n");
  716. }
  717. }
  718. if (cred->roaming_partner) {
  719. for (i = 0; i < cred->num_roaming_partner; i++) {
  720. struct roaming_partner *p = &cred->roaming_partner[i];
  721. fprintf(f, "\troaming_partner=\"%s,%d,%u,%s\"\n",
  722. p->fqdn, p->exact_match, p->priority,
  723. p->country);
  724. }
  725. }
  726. if (cred->update_identifier)
  727. fprintf(f, "\tupdate_identifier=%d\n", cred->update_identifier);
  728. if (cred->provisioning_sp)
  729. fprintf(f, "\tprovisioning_sp=\"%s\"\n", cred->provisioning_sp);
  730. if (cred->sp_priority)
  731. fprintf(f, "\tsp_priority=%d\n", cred->sp_priority);
  732. if (cred->min_dl_bandwidth_home)
  733. fprintf(f, "\tmin_dl_bandwidth_home=%u\n",
  734. cred->min_dl_bandwidth_home);
  735. if (cred->min_ul_bandwidth_home)
  736. fprintf(f, "\tmin_ul_bandwidth_home=%u\n",
  737. cred->min_ul_bandwidth_home);
  738. if (cred->min_dl_bandwidth_roaming)
  739. fprintf(f, "\tmin_dl_bandwidth_roaming=%u\n",
  740. cred->min_dl_bandwidth_roaming);
  741. if (cred->min_ul_bandwidth_roaming)
  742. fprintf(f, "\tmin_ul_bandwidth_roaming=%u\n",
  743. cred->min_ul_bandwidth_roaming);
  744. if (cred->max_bss_load)
  745. fprintf(f, "\tmax_bss_load=%u\n",
  746. cred->max_bss_load);
  747. if (cred->ocsp)
  748. fprintf(f, "\tocsp=%d\n", cred->ocsp);
  749. if (cred->num_req_conn_capab) {
  750. for (i = 0; i < cred->num_req_conn_capab; i++) {
  751. int *ports;
  752. fprintf(f, "\treq_conn_capab=%u",
  753. cred->req_conn_capab_proto[i]);
  754. ports = cred->req_conn_capab_port[i];
  755. if (ports) {
  756. int j;
  757. for (j = 0; ports[j] != -1; j++) {
  758. fprintf(f, "%s%d", j > 0 ? "," : ":",
  759. ports[j]);
  760. }
  761. }
  762. fprintf(f, "\n");
  763. }
  764. }
  765. if (cred->required_roaming_consortium_len) {
  766. fprintf(f, "\trequired_roaming_consortium=");
  767. for (i = 0; i < cred->required_roaming_consortium_len; i++)
  768. fprintf(f, "%02x",
  769. cred->required_roaming_consortium[i]);
  770. fprintf(f, "\n");
  771. }
  772. if (cred->sim_num != DEFAULT_USER_SELECTED_SIM)
  773. fprintf(f, "\tsim_num=%d\n", cred->sim_num);
  774. }
  775. #ifndef CONFIG_NO_CONFIG_BLOBS
  776. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  777. {
  778. unsigned char *encoded;
  779. encoded = base64_encode(blob->data, blob->len, NULL);
  780. if (encoded == NULL)
  781. return -1;
  782. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  783. os_free(encoded);
  784. return 0;
  785. }
  786. #endif /* CONFIG_NO_CONFIG_BLOBS */
  787. static void write_global_bin(FILE *f, const char *field,
  788. const struct wpabuf *val)
  789. {
  790. size_t i;
  791. const u8 *pos;
  792. if (val == NULL)
  793. return;
  794. fprintf(f, "%s=", field);
  795. pos = wpabuf_head(val);
  796. for (i = 0; i < wpabuf_len(val); i++)
  797. fprintf(f, "%02X", *pos++);
  798. fprintf(f, "\n");
  799. }
  800. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  801. {
  802. #ifdef CONFIG_CTRL_IFACE
  803. if (config->ctrl_interface)
  804. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  805. if (config->ctrl_interface_group)
  806. fprintf(f, "ctrl_interface_group=%s\n",
  807. config->ctrl_interface_group);
  808. #endif /* CONFIG_CTRL_IFACE */
  809. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  810. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  811. if (config->ap_scan != DEFAULT_AP_SCAN)
  812. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  813. if (config->disable_scan_offload)
  814. fprintf(f, "disable_scan_offload=%d\n",
  815. config->disable_scan_offload);
  816. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  817. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  818. if (config->opensc_engine_path)
  819. fprintf(f, "opensc_engine_path=%s\n",
  820. config->opensc_engine_path);
  821. if (config->pkcs11_engine_path)
  822. fprintf(f, "pkcs11_engine_path=%s\n",
  823. config->pkcs11_engine_path);
  824. if (config->pkcs11_module_path)
  825. fprintf(f, "pkcs11_module_path=%s\n",
  826. config->pkcs11_module_path);
  827. if (config->openssl_ciphers)
  828. fprintf(f, "openssl_ciphers=%s\n", config->openssl_ciphers);
  829. if (config->pcsc_reader)
  830. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  831. if (config->pcsc_pin)
  832. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  833. if (config->driver_param)
  834. fprintf(f, "driver_param=%s\n", config->driver_param);
  835. if (config->dot11RSNAConfigPMKLifetime)
  836. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  837. config->dot11RSNAConfigPMKLifetime);
  838. if (config->dot11RSNAConfigPMKReauthThreshold)
  839. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  840. config->dot11RSNAConfigPMKReauthThreshold);
  841. if (config->dot11RSNAConfigSATimeout)
  842. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  843. config->dot11RSNAConfigSATimeout);
  844. if (config->update_config)
  845. fprintf(f, "update_config=%d\n", config->update_config);
  846. #ifdef CONFIG_WPS
  847. if (!is_nil_uuid(config->uuid)) {
  848. char buf[40];
  849. uuid_bin2str(config->uuid, buf, sizeof(buf));
  850. fprintf(f, "uuid=%s\n", buf);
  851. }
  852. if (config->device_name)
  853. fprintf(f, "device_name=%s\n", config->device_name);
  854. if (config->manufacturer)
  855. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  856. if (config->model_name)
  857. fprintf(f, "model_name=%s\n", config->model_name);
  858. if (config->model_number)
  859. fprintf(f, "model_number=%s\n", config->model_number);
  860. if (config->serial_number)
  861. fprintf(f, "serial_number=%s\n", config->serial_number);
  862. {
  863. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  864. buf = wps_dev_type_bin2str(config->device_type,
  865. _buf, sizeof(_buf));
  866. if (os_strcmp(buf, "0-00000000-0") != 0)
  867. fprintf(f, "device_type=%s\n", buf);
  868. }
  869. if (WPA_GET_BE32(config->os_version))
  870. fprintf(f, "os_version=%08x\n",
  871. WPA_GET_BE32(config->os_version));
  872. if (config->config_methods)
  873. fprintf(f, "config_methods=%s\n", config->config_methods);
  874. if (config->wps_cred_processing)
  875. fprintf(f, "wps_cred_processing=%d\n",
  876. config->wps_cred_processing);
  877. if (config->wps_vendor_ext_m1) {
  878. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  879. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  880. if (len > 0) {
  881. fprintf(f, "wps_vendor_ext_m1=");
  882. for (i = 0; i < len; i++)
  883. fprintf(f, "%02x", *p++);
  884. fprintf(f, "\n");
  885. }
  886. }
  887. #endif /* CONFIG_WPS */
  888. #ifdef CONFIG_P2P
  889. if (config->p2p_listen_reg_class)
  890. fprintf(f, "p2p_listen_reg_class=%u\n",
  891. config->p2p_listen_reg_class);
  892. if (config->p2p_listen_channel)
  893. fprintf(f, "p2p_listen_channel=%u\n",
  894. config->p2p_listen_channel);
  895. if (config->p2p_oper_reg_class)
  896. fprintf(f, "p2p_oper_reg_class=%u\n",
  897. config->p2p_oper_reg_class);
  898. if (config->p2p_oper_channel)
  899. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  900. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  901. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  902. if (config->p2p_ssid_postfix)
  903. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  904. if (config->persistent_reconnect)
  905. fprintf(f, "persistent_reconnect=%u\n",
  906. config->persistent_reconnect);
  907. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  908. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  909. if (config->p2p_group_idle)
  910. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  911. if (config->p2p_passphrase_len)
  912. fprintf(f, "p2p_passphrase_len=%u\n",
  913. config->p2p_passphrase_len);
  914. if (config->p2p_pref_chan) {
  915. unsigned int i;
  916. fprintf(f, "p2p_pref_chan=");
  917. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  918. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  919. config->p2p_pref_chan[i].op_class,
  920. config->p2p_pref_chan[i].chan);
  921. }
  922. fprintf(f, "\n");
  923. }
  924. if (config->p2p_no_go_freq.num) {
  925. char *val = freq_range_list_str(&config->p2p_no_go_freq);
  926. if (val) {
  927. fprintf(f, "p2p_no_go_freq=%s\n", val);
  928. os_free(val);
  929. }
  930. }
  931. if (config->p2p_add_cli_chan)
  932. fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
  933. if (config->p2p_optimize_listen_chan !=
  934. DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN)
  935. fprintf(f, "p2p_optimize_listen_chan=%d\n",
  936. config->p2p_optimize_listen_chan);
  937. if (config->p2p_go_ht40)
  938. fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
  939. if (config->p2p_go_vht)
  940. fprintf(f, "p2p_go_vht=%u\n", config->p2p_go_vht);
  941. if (config->p2p_disabled)
  942. fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
  943. if (config->p2p_no_group_iface)
  944. fprintf(f, "p2p_no_group_iface=%u\n",
  945. config->p2p_no_group_iface);
  946. if (config->p2p_ignore_shared_freq)
  947. fprintf(f, "p2p_ignore_shared_freq=%u\n",
  948. config->p2p_ignore_shared_freq);
  949. #endif /* CONFIG_P2P */
  950. if (config->country[0] && config->country[1]) {
  951. fprintf(f, "country=%c%c\n",
  952. config->country[0], config->country[1]);
  953. }
  954. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  955. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  956. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  957. fprintf(f, "bss_expiration_age=%u\n",
  958. config->bss_expiration_age);
  959. if (config->bss_expiration_scan_count !=
  960. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  961. fprintf(f, "bss_expiration_scan_count=%u\n",
  962. config->bss_expiration_scan_count);
  963. if (config->filter_ssids)
  964. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  965. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  966. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  967. if (config->disassoc_low_ack)
  968. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  969. #ifdef CONFIG_HS20
  970. if (config->hs20)
  971. fprintf(f, "hs20=1\n");
  972. #endif /* CONFIG_HS20 */
  973. #ifdef CONFIG_INTERWORKING
  974. if (config->interworking)
  975. fprintf(f, "interworking=%u\n", config->interworking);
  976. if (!is_zero_ether_addr(config->hessid))
  977. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  978. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  979. fprintf(f, "access_network_type=%d\n",
  980. config->access_network_type);
  981. #endif /* CONFIG_INTERWORKING */
  982. if (config->pbc_in_m1)
  983. fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
  984. if (config->wps_nfc_pw_from_config) {
  985. if (config->wps_nfc_dev_pw_id)
  986. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  987. config->wps_nfc_dev_pw_id);
  988. write_global_bin(f, "wps_nfc_dh_pubkey",
  989. config->wps_nfc_dh_pubkey);
  990. write_global_bin(f, "wps_nfc_dh_privkey",
  991. config->wps_nfc_dh_privkey);
  992. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  993. }
  994. if (config->ext_password_backend)
  995. fprintf(f, "ext_password_backend=%s\n",
  996. config->ext_password_backend);
  997. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  998. fprintf(f, "p2p_go_max_inactivity=%d\n",
  999. config->p2p_go_max_inactivity);
  1000. if (config->auto_interworking)
  1001. fprintf(f, "auto_interworking=%d\n",
  1002. config->auto_interworking);
  1003. if (config->okc)
  1004. fprintf(f, "okc=%d\n", config->okc);
  1005. if (config->pmf)
  1006. fprintf(f, "pmf=%d\n", config->pmf);
  1007. if (config->dtim_period)
  1008. fprintf(f, "dtim_period=%d\n", config->dtim_period);
  1009. if (config->beacon_int)
  1010. fprintf(f, "beacon_int=%d\n", config->beacon_int);
  1011. if (config->sae_groups) {
  1012. int i;
  1013. fprintf(f, "sae_groups=");
  1014. for (i = 0; config->sae_groups[i] >= 0; i++) {
  1015. fprintf(f, "%s%d", i > 0 ? " " : "",
  1016. config->sae_groups[i]);
  1017. }
  1018. fprintf(f, "\n");
  1019. }
  1020. if (config->ap_vendor_elements) {
  1021. int i, len = wpabuf_len(config->ap_vendor_elements);
  1022. const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
  1023. if (len > 0) {
  1024. fprintf(f, "ap_vendor_elements=");
  1025. for (i = 0; i < len; i++)
  1026. fprintf(f, "%02x", *p++);
  1027. fprintf(f, "\n");
  1028. }
  1029. }
  1030. if (config->ignore_old_scan_res)
  1031. fprintf(f, "ignore_old_scan_res=%d\n",
  1032. config->ignore_old_scan_res);
  1033. if (config->freq_list && config->freq_list[0]) {
  1034. int i;
  1035. fprintf(f, "freq_list=");
  1036. for (i = 0; config->freq_list[i]; i++) {
  1037. fprintf(f, "%s%u", i > 0 ? " " : "",
  1038. config->freq_list[i]);
  1039. }
  1040. fprintf(f, "\n");
  1041. }
  1042. if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
  1043. fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
  1044. if (config->sched_scan_interval)
  1045. fprintf(f, "sched_scan_interval=%u\n",
  1046. config->sched_scan_interval);
  1047. if (config->external_sim)
  1048. fprintf(f, "external_sim=%d\n", config->external_sim);
  1049. if (config->tdls_external_control)
  1050. fprintf(f, "tdls_external_control=%d\n",
  1051. config->tdls_external_control);
  1052. if (config->wowlan_triggers)
  1053. fprintf(f, "wowlan_triggers=%s\n",
  1054. config->wowlan_triggers);
  1055. if (config->bgscan)
  1056. fprintf(f, "bgscan=\"%s\"\n", config->bgscan);
  1057. if (config->p2p_search_delay != DEFAULT_P2P_SEARCH_DELAY)
  1058. fprintf(f, "p2p_search_delay=%u\n",
  1059. config->p2p_search_delay);
  1060. if (config->mac_addr)
  1061. fprintf(f, "mac_addr=%d\n", config->mac_addr);
  1062. if (config->rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
  1063. fprintf(f, "rand_addr_lifetime=%u\n",
  1064. config->rand_addr_lifetime);
  1065. if (config->preassoc_mac_addr)
  1066. fprintf(f, "preassoc_mac_addr=%d\n", config->preassoc_mac_addr);
  1067. if (config->key_mgmt_offload != DEFAULT_KEY_MGMT_OFFLOAD)
  1068. fprintf(f, "key_mgmt_offload=%u\n", config->key_mgmt_offload);
  1069. if (config->user_mpm != DEFAULT_USER_MPM)
  1070. fprintf(f, "user_mpm=%d\n", config->user_mpm);
  1071. if (config->max_peer_links != DEFAULT_MAX_PEER_LINKS)
  1072. fprintf(f, "max_peer_links=%d\n", config->max_peer_links);
  1073. if (config->cert_in_cb != DEFAULT_CERT_IN_CB)
  1074. fprintf(f, "cert_in_cb=%d\n", config->cert_in_cb);
  1075. if (config->mesh_max_inactivity != DEFAULT_MESH_MAX_INACTIVITY)
  1076. fprintf(f, "mesh_max_inactivity=%d\n",
  1077. config->mesh_max_inactivity);
  1078. if (config->passive_scan)
  1079. fprintf(f, "cert_in_cb=%d\n", config->passive_scan);
  1080. }
  1081. #endif /* CONFIG_NO_CONFIG_WRITE */
  1082. int wpa_config_write(const char *name, struct wpa_config *config)
  1083. {
  1084. #ifndef CONFIG_NO_CONFIG_WRITE
  1085. FILE *f;
  1086. struct wpa_ssid *ssid;
  1087. struct wpa_cred *cred;
  1088. #ifndef CONFIG_NO_CONFIG_BLOBS
  1089. struct wpa_config_blob *blob;
  1090. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1091. int ret = 0;
  1092. const char *orig_name = name;
  1093. int tmp_len = os_strlen(name) + 5; /* allow space for .tmp suffix */
  1094. char *tmp_name = os_malloc(tmp_len);
  1095. if (tmp_name) {
  1096. os_snprintf(tmp_name, tmp_len, "%s.tmp", name);
  1097. name = tmp_name;
  1098. }
  1099. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  1100. f = fopen(name, "w");
  1101. if (f == NULL) {
  1102. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  1103. os_free(tmp_name);
  1104. return -1;
  1105. }
  1106. wpa_config_write_global(f, config);
  1107. for (cred = config->cred; cred; cred = cred->next) {
  1108. if (cred->temporary)
  1109. continue;
  1110. fprintf(f, "\ncred={\n");
  1111. wpa_config_write_cred(f, cred);
  1112. fprintf(f, "}\n");
  1113. }
  1114. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  1115. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  1116. continue; /* do not save temporary networks */
  1117. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  1118. !ssid->passphrase)
  1119. continue; /* do not save invalid network */
  1120. fprintf(f, "\nnetwork={\n");
  1121. wpa_config_write_network(f, ssid);
  1122. fprintf(f, "}\n");
  1123. }
  1124. #ifndef CONFIG_NO_CONFIG_BLOBS
  1125. for (blob = config->blobs; blob; blob = blob->next) {
  1126. ret = wpa_config_write_blob(f, blob);
  1127. if (ret)
  1128. break;
  1129. }
  1130. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1131. fclose(f);
  1132. if (tmp_name) {
  1133. int chmod_ret = 0;
  1134. #ifdef ANDROID
  1135. chmod_ret = chmod(tmp_name,
  1136. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  1137. #endif /* ANDROID */
  1138. if (chmod_ret != 0 || rename(tmp_name, orig_name) != 0)
  1139. ret = -1;
  1140. os_free(tmp_name);
  1141. }
  1142. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  1143. orig_name, ret ? "un" : "");
  1144. return ret;
  1145. #else /* CONFIG_NO_CONFIG_WRITE */
  1146. return -1;
  1147. #endif /* CONFIG_NO_CONFIG_WRITE */
  1148. }