config_file.c 25 KB

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