config.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /*
  2. * WPA Supplicant / Configuration parser and common functions
  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. #include "includes.h"
  15. #include "common.h"
  16. #include "wpa.h"
  17. #include "sha1.h"
  18. #include "eap_peer/eap.h"
  19. #include "config.h"
  20. #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
  21. #define NO_CONFIG_WRITE
  22. #endif
  23. /*
  24. * Structure for network configuration parsing. This data is used to implement
  25. * a generic parser for each network block variable. The table of configuration
  26. * variables is defined below in this file (ssid_fields[]).
  27. */
  28. struct parse_data {
  29. /* Configuration variable name */
  30. char *name;
  31. /* Parser function for this variable */
  32. int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
  33. int line, const char *value);
  34. #ifndef NO_CONFIG_WRITE
  35. /* Writer function (i.e., to get the variable in text format from
  36. * internal presentation). */
  37. char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
  38. #endif /* NO_CONFIG_WRITE */
  39. /* Variable specific parameters for the parser. */
  40. void *param1, *param2, *param3, *param4;
  41. /* 0 = this variable can be included in debug output and ctrl_iface
  42. * 1 = this variable contains key/private data and it must not be
  43. * included in debug output unless explicitly requested. In
  44. * addition, this variable will not be readable through the
  45. * ctrl_iface.
  46. */
  47. int key_data;
  48. };
  49. static char * wpa_config_parse_string(const char *value, size_t *len)
  50. {
  51. if (*value == '"') {
  52. const char *pos;
  53. char *str;
  54. value++;
  55. pos = os_strrchr(value, '"');
  56. if (pos == NULL || pos[1] != '\0')
  57. return NULL;
  58. *len = pos - value;
  59. str = os_malloc(*len + 1);
  60. if (str == NULL)
  61. return NULL;
  62. os_memcpy(str, value, *len);
  63. str[*len] = '\0';
  64. return str;
  65. } else {
  66. u8 *str;
  67. size_t tlen, hlen = os_strlen(value);
  68. if (hlen & 1)
  69. return NULL;
  70. tlen = hlen / 2;
  71. str = os_malloc(tlen + 1);
  72. if (str == NULL)
  73. return NULL;
  74. if (hexstr2bin(value, str, tlen)) {
  75. os_free(str);
  76. return NULL;
  77. }
  78. str[tlen] = '\0';
  79. *len = tlen;
  80. return (char *) str;
  81. }
  82. }
  83. static int wpa_config_parse_str(const struct parse_data *data,
  84. struct wpa_ssid *ssid,
  85. int line, const char *value)
  86. {
  87. size_t res_len, *dst_len;
  88. char **dst, *tmp;
  89. if (os_strcmp(value, "NULL") == 0) {
  90. wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
  91. data->name);
  92. tmp = NULL;
  93. res_len = 0;
  94. goto set;
  95. }
  96. tmp = wpa_config_parse_string(value, &res_len);
  97. if (tmp == NULL) {
  98. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
  99. line, data->name,
  100. data->key_data ? "[KEY DATA REMOVED]" : value);
  101. return -1;
  102. }
  103. if (data->key_data) {
  104. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  105. (u8 *) tmp, res_len);
  106. } else {
  107. wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
  108. (u8 *) tmp, res_len);
  109. }
  110. if (data->param3 && res_len < (size_t) data->param3) {
  111. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  112. "min_len=%ld)", line, data->name,
  113. (unsigned long) res_len, (long) data->param3);
  114. os_free(tmp);
  115. return -1;
  116. }
  117. if (data->param4 && res_len > (size_t) data->param4) {
  118. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  119. "max_len=%ld)", line, data->name,
  120. (unsigned long) res_len, (long) data->param4);
  121. os_free(tmp);
  122. return -1;
  123. }
  124. set:
  125. dst = (char **) (((u8 *) ssid) + (long) data->param1);
  126. dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
  127. os_free(*dst);
  128. *dst = tmp;
  129. if (data->param2)
  130. *dst_len = res_len;
  131. return 0;
  132. }
  133. #ifndef NO_CONFIG_WRITE
  134. static int is_hex(const u8 *data, size_t len)
  135. {
  136. size_t i;
  137. for (i = 0; i < len; i++) {
  138. if (data[i] < 32 || data[i] >= 127)
  139. return 1;
  140. }
  141. return 0;
  142. }
  143. static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
  144. {
  145. char *buf;
  146. buf = os_malloc(len + 3);
  147. if (buf == NULL)
  148. return NULL;
  149. buf[0] = '"';
  150. os_memcpy(buf + 1, value, len);
  151. buf[len + 1] = '"';
  152. buf[len + 2] = '\0';
  153. return buf;
  154. }
  155. static char * wpa_config_write_string_hex(const u8 *value, size_t len)
  156. {
  157. char *buf;
  158. buf = os_zalloc(2 * len + 1);
  159. if (buf == NULL)
  160. return NULL;
  161. wpa_snprintf_hex(buf, 2 * len + 1, value, len);
  162. return buf;
  163. }
  164. static char * wpa_config_write_string(const u8 *value, size_t len)
  165. {
  166. if (value == NULL)
  167. return NULL;
  168. if (is_hex(value, len))
  169. return wpa_config_write_string_hex(value, len);
  170. else
  171. return wpa_config_write_string_ascii(value, len);
  172. }
  173. static char * wpa_config_write_str(const struct parse_data *data,
  174. struct wpa_ssid *ssid)
  175. {
  176. size_t len;
  177. char **src;
  178. src = (char **) (((u8 *) ssid) + (long) data->param1);
  179. if (*src == NULL)
  180. return NULL;
  181. if (data->param2)
  182. len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
  183. else
  184. len = os_strlen(*src);
  185. return wpa_config_write_string((const u8 *) *src, len);
  186. }
  187. #endif /* NO_CONFIG_WRITE */
  188. static int wpa_config_parse_int(const struct parse_data *data,
  189. struct wpa_ssid *ssid,
  190. int line, const char *value)
  191. {
  192. int *dst;
  193. dst = (int *) (((u8 *) ssid) + (long) data->param1);
  194. *dst = atoi(value);
  195. wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
  196. if (data->param3 && *dst < (long) data->param3) {
  197. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  198. "min_value=%ld)", line, data->name, *dst,
  199. (long) data->param3);
  200. *dst = (long) data->param3;
  201. return -1;
  202. }
  203. if (data->param4 && *dst > (long) data->param4) {
  204. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  205. "max_value=%ld)", line, data->name, *dst,
  206. (long) data->param4);
  207. *dst = (long) data->param4;
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. #ifndef NO_CONFIG_WRITE
  213. static char * wpa_config_write_int(const struct parse_data *data,
  214. struct wpa_ssid *ssid)
  215. {
  216. int *src, res;
  217. char *value;
  218. src = (int *) (((u8 *) ssid) + (long) data->param1);
  219. value = os_malloc(20);
  220. if (value == NULL)
  221. return NULL;
  222. res = os_snprintf(value, 20, "%d", *src);
  223. if (res < 0 || res >= 20) {
  224. os_free(value);
  225. return NULL;
  226. }
  227. value[20 - 1] = '\0';
  228. return value;
  229. }
  230. #endif /* NO_CONFIG_WRITE */
  231. static int wpa_config_parse_bssid(const struct parse_data *data,
  232. struct wpa_ssid *ssid, int line,
  233. const char *value)
  234. {
  235. if (hwaddr_aton(value, ssid->bssid)) {
  236. wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
  237. line, value);
  238. return -1;
  239. }
  240. ssid->bssid_set = 1;
  241. wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
  242. return 0;
  243. }
  244. #ifndef NO_CONFIG_WRITE
  245. static char * wpa_config_write_bssid(const struct parse_data *data,
  246. struct wpa_ssid *ssid)
  247. {
  248. char *value;
  249. int res;
  250. if (!ssid->bssid_set)
  251. return NULL;
  252. value = os_malloc(20);
  253. if (value == NULL)
  254. return NULL;
  255. res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
  256. if (res < 0 || res >= 20) {
  257. os_free(value);
  258. return NULL;
  259. }
  260. value[20 - 1] = '\0';
  261. return value;
  262. }
  263. #endif /* NO_CONFIG_WRITE */
  264. static int wpa_config_parse_psk(const struct parse_data *data,
  265. struct wpa_ssid *ssid, int line,
  266. const char *value)
  267. {
  268. if (*value == '"') {
  269. #ifndef CONFIG_NO_PBKDF2
  270. const char *pos;
  271. size_t len;
  272. value++;
  273. pos = os_strrchr(value, '"');
  274. if (pos)
  275. len = pos - value;
  276. else
  277. len = os_strlen(value);
  278. if (len < 8 || len > 63) {
  279. wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
  280. "length %lu (expected: 8..63) '%s'.",
  281. line, (unsigned long) len, value);
  282. return -1;
  283. }
  284. wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
  285. (u8 *) value, len);
  286. if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
  287. os_memcmp(ssid->passphrase, value, len) == 0)
  288. return 0;
  289. ssid->psk_set = 0;
  290. os_free(ssid->passphrase);
  291. ssid->passphrase = os_malloc(len + 1);
  292. if (ssid->passphrase == NULL)
  293. return -1;
  294. os_memcpy(ssid->passphrase, value, len);
  295. ssid->passphrase[len] = '\0';
  296. return 0;
  297. #else /* CONFIG_NO_PBKDF2 */
  298. wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
  299. "supported.", line);
  300. return -1;
  301. #endif /* CONFIG_NO_PBKDF2 */
  302. }
  303. if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
  304. value[PMK_LEN * 2] != '\0') {
  305. wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
  306. line, value);
  307. return -1;
  308. }
  309. os_free(ssid->passphrase);
  310. ssid->passphrase = NULL;
  311. ssid->psk_set = 1;
  312. wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
  313. return 0;
  314. }
  315. #ifndef NO_CONFIG_WRITE
  316. static char * wpa_config_write_psk(const struct parse_data *data,
  317. struct wpa_ssid *ssid)
  318. {
  319. if (ssid->passphrase)
  320. return wpa_config_write_string_ascii(
  321. (const u8 *) ssid->passphrase,
  322. os_strlen(ssid->passphrase));
  323. if (ssid->psk_set)
  324. return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
  325. return NULL;
  326. }
  327. #endif /* NO_CONFIG_WRITE */
  328. static int wpa_config_parse_proto(const struct parse_data *data,
  329. struct wpa_ssid *ssid, int line,
  330. const char *value)
  331. {
  332. int val = 0, last, errors = 0;
  333. char *start, *end, *buf;
  334. buf = os_strdup(value);
  335. if (buf == NULL)
  336. return -1;
  337. start = buf;
  338. while (*start != '\0') {
  339. while (*start == ' ' || *start == '\t')
  340. start++;
  341. if (*start == '\0')
  342. break;
  343. end = start;
  344. while (*end != ' ' && *end != '\t' && *end != '\0')
  345. end++;
  346. last = *end == '\0';
  347. *end = '\0';
  348. if (os_strcmp(start, "WPA") == 0)
  349. val |= WPA_PROTO_WPA;
  350. else if (os_strcmp(start, "RSN") == 0 ||
  351. os_strcmp(start, "WPA2") == 0)
  352. val |= WPA_PROTO_RSN;
  353. else {
  354. wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
  355. line, start);
  356. errors++;
  357. }
  358. if (last)
  359. break;
  360. start = end + 1;
  361. }
  362. os_free(buf);
  363. if (val == 0) {
  364. wpa_printf(MSG_ERROR,
  365. "Line %d: no proto values configured.", line);
  366. errors++;
  367. }
  368. wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
  369. ssid->proto = val;
  370. return errors ? -1 : 0;
  371. }
  372. #ifndef NO_CONFIG_WRITE
  373. static char * wpa_config_write_proto(const struct parse_data *data,
  374. struct wpa_ssid *ssid)
  375. {
  376. int first = 1, ret;
  377. char *buf, *pos, *end;
  378. pos = buf = os_zalloc(10);
  379. if (buf == NULL)
  380. return NULL;
  381. end = buf + 10;
  382. if (ssid->proto & WPA_PROTO_WPA) {
  383. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  384. if (ret < 0 || ret >= end - pos)
  385. return buf;
  386. pos += ret;
  387. first = 0;
  388. }
  389. if (ssid->proto & WPA_PROTO_RSN) {
  390. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  391. if (ret < 0 || ret >= end - pos)
  392. return buf;
  393. pos += ret;
  394. first = 0;
  395. }
  396. return buf;
  397. }
  398. #endif /* NO_CONFIG_WRITE */
  399. static int wpa_config_parse_key_mgmt(const struct parse_data *data,
  400. struct wpa_ssid *ssid, int line,
  401. const char *value)
  402. {
  403. int val = 0, last, errors = 0;
  404. char *start, *end, *buf;
  405. buf = os_strdup(value);
  406. if (buf == NULL)
  407. return -1;
  408. start = buf;
  409. while (*start != '\0') {
  410. while (*start == ' ' || *start == '\t')
  411. start++;
  412. if (*start == '\0')
  413. break;
  414. end = start;
  415. while (*end != ' ' && *end != '\t' && *end != '\0')
  416. end++;
  417. last = *end == '\0';
  418. *end = '\0';
  419. if (os_strcmp(start, "WPA-PSK") == 0)
  420. val |= WPA_KEY_MGMT_PSK;
  421. else if (os_strcmp(start, "WPA-EAP") == 0)
  422. val |= WPA_KEY_MGMT_IEEE8021X;
  423. else if (os_strcmp(start, "IEEE8021X") == 0)
  424. val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  425. else if (os_strcmp(start, "NONE") == 0)
  426. val |= WPA_KEY_MGMT_NONE;
  427. else if (os_strcmp(start, "WPA-NONE") == 0)
  428. val |= WPA_KEY_MGMT_WPA_NONE;
  429. #ifdef CONFIG_IEEE80211R
  430. else if (os_strcmp(start, "FT-PSK") == 0)
  431. val |= WPA_KEY_MGMT_FT_PSK;
  432. else if (os_strcmp(start, "FT-EAP") == 0)
  433. val |= WPA_KEY_MGMT_FT_IEEE8021X;
  434. #endif /* CONFIG_IEEE80211R */
  435. #ifdef CONFIG_IEEE80211W
  436. else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
  437. val |= WPA_KEY_MGMT_PSK_SHA256;
  438. else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
  439. val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
  440. #endif /* CONFIG_IEEE80211W */
  441. #ifdef CONFIG_WPS
  442. else if (os_strcmp(start, "WPS") == 0)
  443. val |= WPA_KEY_MGMT_WPS;
  444. #endif /* CONFIG_WPS */
  445. else {
  446. wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
  447. line, start);
  448. errors++;
  449. }
  450. if (last)
  451. break;
  452. start = end + 1;
  453. }
  454. os_free(buf);
  455. if (val == 0) {
  456. wpa_printf(MSG_ERROR,
  457. "Line %d: no key_mgmt values configured.", line);
  458. errors++;
  459. }
  460. wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
  461. ssid->key_mgmt = val;
  462. return errors ? -1 : 0;
  463. }
  464. #ifndef NO_CONFIG_WRITE
  465. static char * wpa_config_write_key_mgmt(const struct parse_data *data,
  466. struct wpa_ssid *ssid)
  467. {
  468. char *buf, *pos, *end;
  469. int ret;
  470. pos = buf = os_zalloc(50);
  471. if (buf == NULL)
  472. return NULL;
  473. end = buf + 50;
  474. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  475. ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
  476. pos == buf ? "" : " ");
  477. if (ret < 0 || ret >= end - pos) {
  478. end[-1] = '\0';
  479. return buf;
  480. }
  481. pos += ret;
  482. }
  483. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  484. ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
  485. pos == buf ? "" : " ");
  486. if (ret < 0 || ret >= end - pos) {
  487. end[-1] = '\0';
  488. return buf;
  489. }
  490. pos += ret;
  491. }
  492. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  493. ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
  494. pos == buf ? "" : " ");
  495. if (ret < 0 || ret >= end - pos) {
  496. end[-1] = '\0';
  497. return buf;
  498. }
  499. pos += ret;
  500. }
  501. if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
  502. ret = os_snprintf(pos, end - pos, "%sNONE",
  503. pos == buf ? "" : " ");
  504. if (ret < 0 || ret >= end - pos) {
  505. end[-1] = '\0';
  506. return buf;
  507. }
  508. pos += ret;
  509. }
  510. if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  511. ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
  512. pos == buf ? "" : " ");
  513. if (ret < 0 || ret >= end - pos) {
  514. end[-1] = '\0';
  515. return buf;
  516. }
  517. pos += ret;
  518. }
  519. #ifdef CONFIG_IEEE80211R
  520. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  521. pos += os_snprintf(pos, end - pos, "%sFT-PSK",
  522. pos == buf ? "" : " ");
  523. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  524. pos += os_snprintf(pos, end - pos, "%sFT-EAP",
  525. pos == buf ? "" : " ");
  526. #endif /* CONFIG_IEEE80211R */
  527. #ifdef CONFIG_IEEE80211W
  528. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  529. pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
  530. pos == buf ? "" : " ");
  531. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  532. pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
  533. pos == buf ? "" : " ");
  534. #endif /* CONFIG_IEEE80211W */
  535. #ifdef CONFIG_WPS
  536. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  537. pos += os_snprintf(pos, end - pos, "%sWPS",
  538. pos == buf ? "" : " ");
  539. #endif /* CONFIG_WPS */
  540. return buf;
  541. }
  542. #endif /* NO_CONFIG_WRITE */
  543. static int wpa_config_parse_cipher(int line, const char *value)
  544. {
  545. int val = 0, last;
  546. char *start, *end, *buf;
  547. buf = os_strdup(value);
  548. if (buf == NULL)
  549. return -1;
  550. start = buf;
  551. while (*start != '\0') {
  552. while (*start == ' ' || *start == '\t')
  553. start++;
  554. if (*start == '\0')
  555. break;
  556. end = start;
  557. while (*end != ' ' && *end != '\t' && *end != '\0')
  558. end++;
  559. last = *end == '\0';
  560. *end = '\0';
  561. if (os_strcmp(start, "CCMP") == 0)
  562. val |= WPA_CIPHER_CCMP;
  563. else if (os_strcmp(start, "TKIP") == 0)
  564. val |= WPA_CIPHER_TKIP;
  565. else if (os_strcmp(start, "WEP104") == 0)
  566. val |= WPA_CIPHER_WEP104;
  567. else if (os_strcmp(start, "WEP40") == 0)
  568. val |= WPA_CIPHER_WEP40;
  569. else if (os_strcmp(start, "NONE") == 0)
  570. val |= WPA_CIPHER_NONE;
  571. else {
  572. wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
  573. line, start);
  574. os_free(buf);
  575. return -1;
  576. }
  577. if (last)
  578. break;
  579. start = end + 1;
  580. }
  581. os_free(buf);
  582. if (val == 0) {
  583. wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
  584. line);
  585. return -1;
  586. }
  587. return val;
  588. }
  589. #ifndef NO_CONFIG_WRITE
  590. static char * wpa_config_write_cipher(int cipher)
  591. {
  592. char *buf, *pos, *end;
  593. int ret;
  594. pos = buf = os_zalloc(50);
  595. if (buf == NULL)
  596. return NULL;
  597. end = buf + 50;
  598. if (cipher & WPA_CIPHER_CCMP) {
  599. ret = os_snprintf(pos, end - pos, "%sCCMP",
  600. pos == buf ? "" : " ");
  601. if (ret < 0 || ret >= end - pos) {
  602. end[-1] = '\0';
  603. return buf;
  604. }
  605. pos += ret;
  606. }
  607. if (cipher & WPA_CIPHER_TKIP) {
  608. ret = os_snprintf(pos, end - pos, "%sTKIP",
  609. pos == buf ? "" : " ");
  610. if (ret < 0 || ret >= end - pos) {
  611. end[-1] = '\0';
  612. return buf;
  613. }
  614. pos += ret;
  615. }
  616. if (cipher & WPA_CIPHER_WEP104) {
  617. ret = os_snprintf(pos, end - pos, "%sWEP104",
  618. pos == buf ? "" : " ");
  619. if (ret < 0 || ret >= end - pos) {
  620. end[-1] = '\0';
  621. return buf;
  622. }
  623. pos += ret;
  624. }
  625. if (cipher & WPA_CIPHER_WEP40) {
  626. ret = os_snprintf(pos, end - pos, "%sWEP40",
  627. pos == buf ? "" : " ");
  628. if (ret < 0 || ret >= end - pos) {
  629. end[-1] = '\0';
  630. return buf;
  631. }
  632. pos += ret;
  633. }
  634. if (cipher & WPA_CIPHER_NONE) {
  635. ret = os_snprintf(pos, end - pos, "%sNONE",
  636. pos == buf ? "" : " ");
  637. if (ret < 0 || ret >= end - pos) {
  638. end[-1] = '\0';
  639. return buf;
  640. }
  641. pos += ret;
  642. }
  643. return buf;
  644. }
  645. #endif /* NO_CONFIG_WRITE */
  646. static int wpa_config_parse_pairwise(const struct parse_data *data,
  647. struct wpa_ssid *ssid, int line,
  648. const char *value)
  649. {
  650. int val;
  651. val = wpa_config_parse_cipher(line, value);
  652. if (val == -1)
  653. return -1;
  654. if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
  655. wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
  656. "(0x%x).", line, val);
  657. return -1;
  658. }
  659. wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
  660. ssid->pairwise_cipher = val;
  661. return 0;
  662. }
  663. #ifndef NO_CONFIG_WRITE
  664. static char * wpa_config_write_pairwise(const struct parse_data *data,
  665. struct wpa_ssid *ssid)
  666. {
  667. return wpa_config_write_cipher(ssid->pairwise_cipher);
  668. }
  669. #endif /* NO_CONFIG_WRITE */
  670. static int wpa_config_parse_group(const struct parse_data *data,
  671. struct wpa_ssid *ssid, int line,
  672. const char *value)
  673. {
  674. int val;
  675. val = wpa_config_parse_cipher(line, value);
  676. if (val == -1)
  677. return -1;
  678. if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
  679. WPA_CIPHER_WEP40)) {
  680. wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
  681. "(0x%x).", line, val);
  682. return -1;
  683. }
  684. wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
  685. ssid->group_cipher = val;
  686. return 0;
  687. }
  688. #ifndef NO_CONFIG_WRITE
  689. static char * wpa_config_write_group(const struct parse_data *data,
  690. struct wpa_ssid *ssid)
  691. {
  692. return wpa_config_write_cipher(ssid->group_cipher);
  693. }
  694. #endif /* NO_CONFIG_WRITE */
  695. static int wpa_config_parse_auth_alg(const struct parse_data *data,
  696. struct wpa_ssid *ssid, int line,
  697. const char *value)
  698. {
  699. int val = 0, last, errors = 0;
  700. char *start, *end, *buf;
  701. buf = os_strdup(value);
  702. if (buf == NULL)
  703. return -1;
  704. start = buf;
  705. while (*start != '\0') {
  706. while (*start == ' ' || *start == '\t')
  707. start++;
  708. if (*start == '\0')
  709. break;
  710. end = start;
  711. while (*end != ' ' && *end != '\t' && *end != '\0')
  712. end++;
  713. last = *end == '\0';
  714. *end = '\0';
  715. if (os_strcmp(start, "OPEN") == 0)
  716. val |= WPA_AUTH_ALG_OPEN;
  717. else if (os_strcmp(start, "SHARED") == 0)
  718. val |= WPA_AUTH_ALG_SHARED;
  719. else if (os_strcmp(start, "LEAP") == 0)
  720. val |= WPA_AUTH_ALG_LEAP;
  721. else {
  722. wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
  723. line, start);
  724. errors++;
  725. }
  726. if (last)
  727. break;
  728. start = end + 1;
  729. }
  730. os_free(buf);
  731. if (val == 0) {
  732. wpa_printf(MSG_ERROR,
  733. "Line %d: no auth_alg values configured.", line);
  734. errors++;
  735. }
  736. wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
  737. ssid->auth_alg = val;
  738. return errors ? -1 : 0;
  739. }
  740. #ifndef NO_CONFIG_WRITE
  741. static char * wpa_config_write_auth_alg(const struct parse_data *data,
  742. struct wpa_ssid *ssid)
  743. {
  744. char *buf, *pos, *end;
  745. int ret;
  746. pos = buf = os_zalloc(30);
  747. if (buf == NULL)
  748. return NULL;
  749. end = buf + 30;
  750. if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
  751. ret = os_snprintf(pos, end - pos, "%sOPEN",
  752. pos == buf ? "" : " ");
  753. if (ret < 0 || ret >= end - pos) {
  754. end[-1] = '\0';
  755. return buf;
  756. }
  757. pos += ret;
  758. }
  759. if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
  760. ret = os_snprintf(pos, end - pos, "%sSHARED",
  761. pos == buf ? "" : " ");
  762. if (ret < 0 || ret >= end - pos) {
  763. end[-1] = '\0';
  764. return buf;
  765. }
  766. pos += ret;
  767. }
  768. if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
  769. ret = os_snprintf(pos, end - pos, "%sLEAP",
  770. pos == buf ? "" : " ");
  771. if (ret < 0 || ret >= end - pos) {
  772. end[-1] = '\0';
  773. return buf;
  774. }
  775. pos += ret;
  776. }
  777. return buf;
  778. }
  779. #endif /* NO_CONFIG_WRITE */
  780. static int wpa_config_parse_scan_freq(const struct parse_data *data,
  781. struct wpa_ssid *ssid, int line,
  782. const char *value)
  783. {
  784. int *freqs;
  785. size_t used, len;
  786. const char *pos;
  787. used = 0;
  788. len = 10;
  789. freqs = os_zalloc((len + 1) * sizeof(int));
  790. if (freqs == NULL)
  791. return -1;
  792. pos = value;
  793. while (pos) {
  794. while (*pos == ' ')
  795. pos++;
  796. if (used == len) {
  797. int *n;
  798. size_t i;
  799. n = os_realloc(freqs, (len * 2 + 1) * sizeof(int));
  800. if (n == NULL) {
  801. os_free(freqs);
  802. return -1;
  803. }
  804. for (i = len; i <= len * 2; i++)
  805. n[i] = 0;
  806. freqs = n;
  807. len *= 2;
  808. }
  809. freqs[used] = atoi(pos);
  810. if (freqs[used] == 0)
  811. break;
  812. used++;
  813. pos = os_strchr(pos + 1, ' ');
  814. }
  815. os_free(ssid->scan_freq);
  816. ssid->scan_freq = freqs;
  817. return 0;
  818. }
  819. #ifndef NO_CONFIG_WRITE
  820. static char * wpa_config_write_scan_freq(const struct parse_data *data,
  821. struct wpa_ssid *ssid)
  822. {
  823. char *buf, *pos, *end;
  824. int i, ret;
  825. size_t count;
  826. if (ssid->scan_freq == NULL)
  827. return NULL;
  828. count = 0;
  829. for (i = 0; ssid->scan_freq[i]; i++)
  830. count++;
  831. pos = buf = os_zalloc(10 * count + 1);
  832. if (buf == NULL)
  833. return NULL;
  834. end = buf + 10 * count + 1;
  835. for (i = 0; ssid->scan_freq[i]; i++) {
  836. ret = os_snprintf(pos, end - pos, "%s%u",
  837. i == 0 ? "" : " ", ssid->scan_freq[i]);
  838. if (ret < 0 || ret >= end - pos) {
  839. end[-1] = '\0';
  840. return buf;
  841. }
  842. pos += ret;
  843. }
  844. return buf;
  845. }
  846. #endif /* NO_CONFIG_WRITE */
  847. #ifdef IEEE8021X_EAPOL
  848. static int wpa_config_parse_eap(const struct parse_data *data,
  849. struct wpa_ssid *ssid, int line,
  850. const char *value)
  851. {
  852. int last, errors = 0;
  853. char *start, *end, *buf;
  854. struct eap_method_type *methods = NULL, *tmp;
  855. size_t num_methods = 0;
  856. buf = os_strdup(value);
  857. if (buf == NULL)
  858. return -1;
  859. start = buf;
  860. while (*start != '\0') {
  861. while (*start == ' ' || *start == '\t')
  862. start++;
  863. if (*start == '\0')
  864. break;
  865. end = start;
  866. while (*end != ' ' && *end != '\t' && *end != '\0')
  867. end++;
  868. last = *end == '\0';
  869. *end = '\0';
  870. tmp = methods;
  871. methods = os_realloc(methods,
  872. (num_methods + 1) * sizeof(*methods));
  873. if (methods == NULL) {
  874. os_free(tmp);
  875. os_free(buf);
  876. return -1;
  877. }
  878. methods[num_methods].method = eap_peer_get_type(
  879. start, &methods[num_methods].vendor);
  880. if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  881. methods[num_methods].method == EAP_TYPE_NONE) {
  882. wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
  883. "'%s'", line, start);
  884. wpa_printf(MSG_ERROR, "You may need to add support for"
  885. " this EAP method during wpa_supplicant\n"
  886. "build time configuration.\n"
  887. "See README for more information.");
  888. errors++;
  889. } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  890. methods[num_methods].method == EAP_TYPE_LEAP)
  891. ssid->leap++;
  892. else
  893. ssid->non_leap++;
  894. num_methods++;
  895. if (last)
  896. break;
  897. start = end + 1;
  898. }
  899. os_free(buf);
  900. tmp = methods;
  901. methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
  902. if (methods == NULL) {
  903. os_free(tmp);
  904. return -1;
  905. }
  906. methods[num_methods].vendor = EAP_VENDOR_IETF;
  907. methods[num_methods].method = EAP_TYPE_NONE;
  908. num_methods++;
  909. wpa_hexdump(MSG_MSGDUMP, "eap methods",
  910. (u8 *) methods, num_methods * sizeof(*methods));
  911. ssid->eap.eap_methods = methods;
  912. return errors ? -1 : 0;
  913. }
  914. static char * wpa_config_write_eap(const struct parse_data *data,
  915. struct wpa_ssid *ssid)
  916. {
  917. int i, ret;
  918. char *buf, *pos, *end;
  919. const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
  920. const char *name;
  921. if (eap_methods == NULL)
  922. return NULL;
  923. pos = buf = os_zalloc(100);
  924. if (buf == NULL)
  925. return NULL;
  926. end = buf + 100;
  927. for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
  928. eap_methods[i].method != EAP_TYPE_NONE; i++) {
  929. name = eap_get_name(eap_methods[i].vendor,
  930. eap_methods[i].method);
  931. if (name) {
  932. ret = os_snprintf(pos, end - pos, "%s%s",
  933. pos == buf ? "" : " ", name);
  934. if (ret < 0 || ret >= end - pos)
  935. break;
  936. pos += ret;
  937. }
  938. }
  939. end[-1] = '\0';
  940. return buf;
  941. }
  942. static int wpa_config_parse_password(const struct parse_data *data,
  943. struct wpa_ssid *ssid, int line,
  944. const char *value)
  945. {
  946. u8 *hash;
  947. if (os_strcmp(value, "NULL") == 0) {
  948. wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
  949. os_free(ssid->eap.password);
  950. ssid->eap.password = NULL;
  951. ssid->eap.password_len = 0;
  952. return 0;
  953. }
  954. if (os_strncmp(value, "hash:", 5) != 0) {
  955. char *tmp;
  956. size_t res_len;
  957. tmp = wpa_config_parse_string(value, &res_len);
  958. if (tmp == NULL) {
  959. wpa_printf(MSG_ERROR, "Line %d: failed to parse "
  960. "password.", line);
  961. return -1;
  962. }
  963. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  964. (u8 *) tmp, res_len);
  965. os_free(ssid->eap.password);
  966. ssid->eap.password = (u8 *) tmp;
  967. ssid->eap.password_len = res_len;
  968. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  969. return 0;
  970. }
  971. /* NtPasswordHash: hash:<32 hex digits> */
  972. if (os_strlen(value + 5) != 2 * 16) {
  973. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
  974. "(expected 32 hex digits)", line);
  975. return -1;
  976. }
  977. hash = os_malloc(16);
  978. if (hash == NULL)
  979. return -1;
  980. if (hexstr2bin(value + 5, hash, 16)) {
  981. os_free(hash);
  982. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
  983. return -1;
  984. }
  985. wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
  986. os_free(ssid->eap.password);
  987. ssid->eap.password = hash;
  988. ssid->eap.password_len = 16;
  989. ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  990. return 0;
  991. }
  992. static char * wpa_config_write_password(const struct parse_data *data,
  993. struct wpa_ssid *ssid)
  994. {
  995. char *buf;
  996. if (ssid->eap.password == NULL)
  997. return NULL;
  998. if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
  999. return wpa_config_write_string(
  1000. ssid->eap.password, ssid->eap.password_len);
  1001. }
  1002. buf = os_malloc(5 + 32 + 1);
  1003. if (buf == NULL)
  1004. return NULL;
  1005. os_memcpy(buf, "hash:", 5);
  1006. wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
  1007. return buf;
  1008. }
  1009. #endif /* IEEE8021X_EAPOL */
  1010. static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
  1011. const char *value, int idx)
  1012. {
  1013. char *buf, title[20];
  1014. int res;
  1015. buf = wpa_config_parse_string(value, len);
  1016. if (buf == NULL) {
  1017. wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
  1018. line, idx, value);
  1019. return -1;
  1020. }
  1021. if (*len > MAX_WEP_KEY_LEN) {
  1022. wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
  1023. line, idx, value);
  1024. os_free(buf);
  1025. return -1;
  1026. }
  1027. os_memcpy(key, buf, *len);
  1028. os_free(buf);
  1029. res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
  1030. if (res >= 0 && (size_t) res < sizeof(title))
  1031. wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
  1032. return 0;
  1033. }
  1034. static int wpa_config_parse_wep_key0(const struct parse_data *data,
  1035. struct wpa_ssid *ssid, int line,
  1036. const char *value)
  1037. {
  1038. return wpa_config_parse_wep_key(ssid->wep_key[0],
  1039. &ssid->wep_key_len[0], line,
  1040. value, 0);
  1041. }
  1042. static int wpa_config_parse_wep_key1(const struct parse_data *data,
  1043. struct wpa_ssid *ssid, int line,
  1044. const char *value)
  1045. {
  1046. return wpa_config_parse_wep_key(ssid->wep_key[1],
  1047. &ssid->wep_key_len[1], line,
  1048. value, 1);
  1049. }
  1050. static int wpa_config_parse_wep_key2(const struct parse_data *data,
  1051. struct wpa_ssid *ssid, int line,
  1052. const char *value)
  1053. {
  1054. return wpa_config_parse_wep_key(ssid->wep_key[2],
  1055. &ssid->wep_key_len[2], line,
  1056. value, 2);
  1057. }
  1058. static int wpa_config_parse_wep_key3(const struct parse_data *data,
  1059. struct wpa_ssid *ssid, int line,
  1060. const char *value)
  1061. {
  1062. return wpa_config_parse_wep_key(ssid->wep_key[3],
  1063. &ssid->wep_key_len[3], line,
  1064. value, 3);
  1065. }
  1066. #ifndef NO_CONFIG_WRITE
  1067. static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
  1068. {
  1069. if (ssid->wep_key_len[idx] == 0)
  1070. return NULL;
  1071. return wpa_config_write_string(ssid->wep_key[idx],
  1072. ssid->wep_key_len[idx]);
  1073. }
  1074. static char * wpa_config_write_wep_key0(const struct parse_data *data,
  1075. struct wpa_ssid *ssid)
  1076. {
  1077. return wpa_config_write_wep_key(ssid, 0);
  1078. }
  1079. static char * wpa_config_write_wep_key1(const struct parse_data *data,
  1080. struct wpa_ssid *ssid)
  1081. {
  1082. return wpa_config_write_wep_key(ssid, 1);
  1083. }
  1084. static char * wpa_config_write_wep_key2(const struct parse_data *data,
  1085. struct wpa_ssid *ssid)
  1086. {
  1087. return wpa_config_write_wep_key(ssid, 2);
  1088. }
  1089. static char * wpa_config_write_wep_key3(const struct parse_data *data,
  1090. struct wpa_ssid *ssid)
  1091. {
  1092. return wpa_config_write_wep_key(ssid, 3);
  1093. }
  1094. #endif /* NO_CONFIG_WRITE */
  1095. /* Helper macros for network block parser */
  1096. #ifdef OFFSET
  1097. #undef OFFSET
  1098. #endif /* OFFSET */
  1099. /* OFFSET: Get offset of a variable within the wpa_ssid structure */
  1100. #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
  1101. /* STR: Define a string variable for an ASCII string; f = field name */
  1102. #ifdef NO_CONFIG_WRITE
  1103. #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
  1104. #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
  1105. #else /* NO_CONFIG_WRITE */
  1106. #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
  1107. #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
  1108. #endif /* NO_CONFIG_WRITE */
  1109. #define STR(f) _STR(f), NULL, NULL, NULL, 0
  1110. #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
  1111. #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
  1112. #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
  1113. /* STR_LEN: Define a string variable with a separate variable for storing the
  1114. * data length. Unlike STR(), this can be used to store arbitrary binary data
  1115. * (i.e., even nul termination character). */
  1116. #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
  1117. #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
  1118. #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
  1119. #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
  1120. #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
  1121. /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
  1122. * explicitly specified. */
  1123. #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
  1124. #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
  1125. #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
  1126. #ifdef NO_CONFIG_WRITE
  1127. #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
  1128. #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
  1129. #else /* NO_CONFIG_WRITE */
  1130. #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1131. OFFSET(f), (void *) 0
  1132. #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1133. OFFSET(eap.f), (void *) 0
  1134. #endif /* NO_CONFIG_WRITE */
  1135. /* INT: Define an integer variable */
  1136. #define INT(f) _INT(f), NULL, NULL, 0
  1137. #define INTe(f) _INTe(f), NULL, NULL, 0
  1138. /* INT_RANGE: Define an integer variable with allowed value range */
  1139. #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
  1140. /* FUNC: Define a configuration variable that uses a custom function for
  1141. * parsing and writing the value. */
  1142. #ifdef NO_CONFIG_WRITE
  1143. #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
  1144. #else /* NO_CONFIG_WRITE */
  1145. #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
  1146. NULL, NULL, NULL, NULL
  1147. #endif /* NO_CONFIG_WRITE */
  1148. #define FUNC(f) _FUNC(f), 0
  1149. #define FUNC_KEY(f) _FUNC(f), 1
  1150. /*
  1151. * Table of network configuration variables. This table is used to parse each
  1152. * network configuration variable, e.g., each line in wpa_supplicant.conf file
  1153. * that is inside a network block.
  1154. *
  1155. * This table is generated using the helper macros defined above and with
  1156. * generous help from the C pre-processor. The field name is stored as a string
  1157. * into .name and for STR and INT types, the offset of the target buffer within
  1158. * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
  1159. * offset to the field containing the length of the configuration variable.
  1160. * .param3 and .param4 can be used to mark the allowed range (length for STR
  1161. * and value for INT).
  1162. *
  1163. * For each configuration line in wpa_supplicant.conf, the parser goes through
  1164. * this table and select the entry that matches with the field name. The parser
  1165. * function (.parser) is then called to parse the actual value of the field.
  1166. *
  1167. * This kind of mechanism makes it easy to add new configuration parameters,
  1168. * since only one line needs to be added into this table and into the
  1169. * struct wpa_ssid definition if the new variable is either a string or
  1170. * integer. More complex types will need to use their own parser and writer
  1171. * functions.
  1172. */
  1173. static const struct parse_data ssid_fields[] = {
  1174. { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
  1175. { INT_RANGE(scan_ssid, 0, 1) },
  1176. { FUNC(bssid) },
  1177. { FUNC_KEY(psk) },
  1178. { FUNC(proto) },
  1179. { FUNC(key_mgmt) },
  1180. { FUNC(pairwise) },
  1181. { FUNC(group) },
  1182. { FUNC(auth_alg) },
  1183. { FUNC(scan_freq) },
  1184. #ifdef IEEE8021X_EAPOL
  1185. { FUNC(eap) },
  1186. { STR_LENe(identity) },
  1187. { STR_LENe(anonymous_identity) },
  1188. { FUNC_KEY(password) },
  1189. { STRe(ca_cert) },
  1190. { STRe(ca_path) },
  1191. { STRe(client_cert) },
  1192. { STRe(private_key) },
  1193. { STR_KEYe(private_key_passwd) },
  1194. { STRe(dh_file) },
  1195. { STRe(subject_match) },
  1196. { STRe(altsubject_match) },
  1197. { STRe(ca_cert2) },
  1198. { STRe(ca_path2) },
  1199. { STRe(client_cert2) },
  1200. { STRe(private_key2) },
  1201. { STR_KEYe(private_key2_passwd) },
  1202. { STRe(dh_file2) },
  1203. { STRe(subject_match2) },
  1204. { STRe(altsubject_match2) },
  1205. { STRe(phase1) },
  1206. { STRe(phase2) },
  1207. { STRe(pcsc) },
  1208. { STR_KEYe(pin) },
  1209. { STRe(engine_id) },
  1210. { STRe(key_id) },
  1211. { STRe(cert_id) },
  1212. { STRe(ca_cert_id) },
  1213. { STR_KEYe(pin2) },
  1214. { STRe(engine2_id) },
  1215. { STRe(key2_id) },
  1216. { STRe(cert2_id) },
  1217. { STRe(ca_cert2_id) },
  1218. { INTe(engine) },
  1219. { INTe(engine2) },
  1220. { INT(eapol_flags) },
  1221. #endif /* IEEE8021X_EAPOL */
  1222. { FUNC_KEY(wep_key0) },
  1223. { FUNC_KEY(wep_key1) },
  1224. { FUNC_KEY(wep_key2) },
  1225. { FUNC_KEY(wep_key3) },
  1226. { INT(wep_tx_keyidx) },
  1227. { INT(priority) },
  1228. #ifdef IEEE8021X_EAPOL
  1229. { INT(eap_workaround) },
  1230. { STRe(pac_file) },
  1231. { INTe(fragment_size) },
  1232. #endif /* IEEE8021X_EAPOL */
  1233. { INT_RANGE(mode, 0, 2) },
  1234. { INT_RANGE(proactive_key_caching, 0, 1) },
  1235. { INT_RANGE(disabled, 0, 1) },
  1236. { STR(id_str) },
  1237. #ifdef CONFIG_IEEE80211W
  1238. { INT_RANGE(ieee80211w, 0, 2) },
  1239. #endif /* CONFIG_IEEE80211W */
  1240. { INT_RANGE(peerkey, 0, 1) },
  1241. { INT_RANGE(mixed_cell, 0, 1) },
  1242. { INT_RANGE(frequency, 0, 10000) },
  1243. { INT(wpa_ptk_rekey) },
  1244. { STR(bgscan) },
  1245. };
  1246. #undef OFFSET
  1247. #undef _STR
  1248. #undef STR
  1249. #undef STR_KEY
  1250. #undef _STR_LEN
  1251. #undef STR_LEN
  1252. #undef STR_LEN_KEY
  1253. #undef _STR_RANGE
  1254. #undef STR_RANGE
  1255. #undef STR_RANGE_KEY
  1256. #undef _INT
  1257. #undef INT
  1258. #undef INT_RANGE
  1259. #undef _FUNC
  1260. #undef FUNC
  1261. #undef FUNC_KEY
  1262. #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
  1263. /**
  1264. * wpa_config_add_prio_network - Add a network to priority lists
  1265. * @config: Configuration data from wpa_config_read()
  1266. * @ssid: Pointer to the network configuration to be added to the list
  1267. * Returns: 0 on success, -1 on failure
  1268. *
  1269. * This function is used to add a network block to the priority list of
  1270. * networks. This must be called for each network when reading in the full
  1271. * configuration. In addition, this can be used indirectly when updating
  1272. * priorities by calling wpa_config_update_prio_list().
  1273. */
  1274. int wpa_config_add_prio_network(struct wpa_config *config,
  1275. struct wpa_ssid *ssid)
  1276. {
  1277. int prio;
  1278. struct wpa_ssid *prev, **nlist;
  1279. /*
  1280. * Add to an existing priority list if one is available for the
  1281. * configured priority level for this network.
  1282. */
  1283. for (prio = 0; prio < config->num_prio; prio++) {
  1284. prev = config->pssid[prio];
  1285. if (prev->priority == ssid->priority) {
  1286. while (prev->pnext)
  1287. prev = prev->pnext;
  1288. prev->pnext = ssid;
  1289. return 0;
  1290. }
  1291. }
  1292. /* First network for this priority - add a new priority list */
  1293. nlist = os_realloc(config->pssid,
  1294. (config->num_prio + 1) * sizeof(struct wpa_ssid *));
  1295. if (nlist == NULL)
  1296. return -1;
  1297. for (prio = 0; prio < config->num_prio; prio++) {
  1298. if (nlist[prio]->priority < ssid->priority)
  1299. break;
  1300. }
  1301. os_memmove(&nlist[prio + 1], &nlist[prio],
  1302. (config->num_prio - prio) * sizeof(struct wpa_ssid *));
  1303. nlist[prio] = ssid;
  1304. config->num_prio++;
  1305. config->pssid = nlist;
  1306. return 0;
  1307. }
  1308. /**
  1309. * wpa_config_update_prio_list - Update network priority list
  1310. * @config: Configuration data from wpa_config_read()
  1311. * Returns: 0 on success, -1 on failure
  1312. *
  1313. * This function is called to update the priority list of networks in the
  1314. * configuration when a network is being added or removed. This is also called
  1315. * if a priority for a network is changed.
  1316. */
  1317. static int wpa_config_update_prio_list(struct wpa_config *config)
  1318. {
  1319. struct wpa_ssid *ssid;
  1320. int ret = 0;
  1321. os_free(config->pssid);
  1322. config->pssid = NULL;
  1323. config->num_prio = 0;
  1324. ssid = config->ssid;
  1325. while (ssid) {
  1326. ssid->pnext = NULL;
  1327. if (wpa_config_add_prio_network(config, ssid) < 0)
  1328. ret = -1;
  1329. ssid = ssid->next;
  1330. }
  1331. return ret;
  1332. }
  1333. #ifdef IEEE8021X_EAPOL
  1334. static void eap_peer_config_free(struct eap_peer_config *eap)
  1335. {
  1336. os_free(eap->eap_methods);
  1337. os_free(eap->identity);
  1338. os_free(eap->anonymous_identity);
  1339. os_free(eap->password);
  1340. os_free(eap->ca_cert);
  1341. os_free(eap->ca_path);
  1342. os_free(eap->client_cert);
  1343. os_free(eap->private_key);
  1344. os_free(eap->private_key_passwd);
  1345. os_free(eap->dh_file);
  1346. os_free(eap->subject_match);
  1347. os_free(eap->altsubject_match);
  1348. os_free(eap->ca_cert2);
  1349. os_free(eap->ca_path2);
  1350. os_free(eap->client_cert2);
  1351. os_free(eap->private_key2);
  1352. os_free(eap->private_key2_passwd);
  1353. os_free(eap->dh_file2);
  1354. os_free(eap->subject_match2);
  1355. os_free(eap->altsubject_match2);
  1356. os_free(eap->phase1);
  1357. os_free(eap->phase2);
  1358. os_free(eap->pcsc);
  1359. os_free(eap->pin);
  1360. os_free(eap->engine_id);
  1361. os_free(eap->key_id);
  1362. os_free(eap->cert_id);
  1363. os_free(eap->ca_cert_id);
  1364. os_free(eap->key2_id);
  1365. os_free(eap->cert2_id);
  1366. os_free(eap->ca_cert2_id);
  1367. os_free(eap->pin2);
  1368. os_free(eap->engine2_id);
  1369. os_free(eap->otp);
  1370. os_free(eap->pending_req_otp);
  1371. os_free(eap->pac_file);
  1372. os_free(eap->new_password);
  1373. }
  1374. #endif /* IEEE8021X_EAPOL */
  1375. /**
  1376. * wpa_config_free_ssid - Free network/ssid configuration data
  1377. * @ssid: Configuration data for the network
  1378. *
  1379. * This function frees all resources allocated for the network configuration
  1380. * data.
  1381. */
  1382. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1383. {
  1384. os_free(ssid->ssid);
  1385. os_free(ssid->passphrase);
  1386. #ifdef IEEE8021X_EAPOL
  1387. eap_peer_config_free(&ssid->eap);
  1388. #endif /* IEEE8021X_EAPOL */
  1389. os_free(ssid->id_str);
  1390. os_free(ssid->scan_freq);
  1391. os_free(ssid->bgscan);
  1392. os_free(ssid);
  1393. }
  1394. /**
  1395. * wpa_config_free - Free configuration data
  1396. * @config: Configuration data from wpa_config_read()
  1397. *
  1398. * This function frees all resources allocated for the configuration data by
  1399. * wpa_config_read().
  1400. */
  1401. void wpa_config_free(struct wpa_config *config)
  1402. {
  1403. #ifndef CONFIG_NO_CONFIG_BLOBS
  1404. struct wpa_config_blob *blob, *prevblob;
  1405. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1406. struct wpa_ssid *ssid, *prev = NULL;
  1407. ssid = config->ssid;
  1408. while (ssid) {
  1409. prev = ssid;
  1410. ssid = ssid->next;
  1411. wpa_config_free_ssid(prev);
  1412. }
  1413. #ifndef CONFIG_NO_CONFIG_BLOBS
  1414. blob = config->blobs;
  1415. prevblob = NULL;
  1416. while (blob) {
  1417. prevblob = blob;
  1418. blob = blob->next;
  1419. wpa_config_free_blob(prevblob);
  1420. }
  1421. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1422. os_free(config->ctrl_interface);
  1423. os_free(config->ctrl_interface_group);
  1424. #ifdef EAP_TLS_OPENSSL
  1425. os_free(config->opensc_engine_path);
  1426. os_free(config->pkcs11_engine_path);
  1427. os_free(config->pkcs11_module_path);
  1428. #endif /* EAP_TLS_OPENSSL */
  1429. os_free(config->driver_param);
  1430. os_free(config->device_name);
  1431. os_free(config->manufacturer);
  1432. os_free(config->model_name);
  1433. os_free(config->model_number);
  1434. os_free(config->serial_number);
  1435. os_free(config->device_type);
  1436. os_free(config->pssid);
  1437. os_free(config);
  1438. }
  1439. /**
  1440. * wpa_config_get_network - Get configured network based on id
  1441. * @config: Configuration data from wpa_config_read()
  1442. * @id: Unique network id to search for
  1443. * Returns: Network configuration or %NULL if not found
  1444. */
  1445. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1446. {
  1447. struct wpa_ssid *ssid;
  1448. ssid = config->ssid;
  1449. while (ssid) {
  1450. if (id == ssid->id)
  1451. break;
  1452. ssid = ssid->next;
  1453. }
  1454. return ssid;
  1455. }
  1456. /**
  1457. * wpa_config_add_network - Add a new network with empty configuration
  1458. * @config: Configuration data from wpa_config_read()
  1459. * Returns: The new network configuration or %NULL if operation failed
  1460. */
  1461. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1462. {
  1463. int id;
  1464. struct wpa_ssid *ssid, *last = NULL;
  1465. id = -1;
  1466. ssid = config->ssid;
  1467. while (ssid) {
  1468. if (ssid->id > id)
  1469. id = ssid->id;
  1470. last = ssid;
  1471. ssid = ssid->next;
  1472. }
  1473. id++;
  1474. ssid = os_zalloc(sizeof(*ssid));
  1475. if (ssid == NULL)
  1476. return NULL;
  1477. ssid->id = id;
  1478. if (last)
  1479. last->next = ssid;
  1480. else
  1481. config->ssid = ssid;
  1482. wpa_config_update_prio_list(config);
  1483. return ssid;
  1484. }
  1485. /**
  1486. * wpa_config_remove_network - Remove a configured network based on id
  1487. * @config: Configuration data from wpa_config_read()
  1488. * @id: Unique network id to search for
  1489. * Returns: 0 on success, or -1 if the network was not found
  1490. */
  1491. int wpa_config_remove_network(struct wpa_config *config, int id)
  1492. {
  1493. struct wpa_ssid *ssid, *prev = NULL;
  1494. ssid = config->ssid;
  1495. while (ssid) {
  1496. if (id == ssid->id)
  1497. break;
  1498. prev = ssid;
  1499. ssid = ssid->next;
  1500. }
  1501. if (ssid == NULL)
  1502. return -1;
  1503. if (prev)
  1504. prev->next = ssid->next;
  1505. else
  1506. config->ssid = ssid->next;
  1507. wpa_config_update_prio_list(config);
  1508. wpa_config_free_ssid(ssid);
  1509. return 0;
  1510. }
  1511. /**
  1512. * wpa_config_set_network_defaults - Set network default values
  1513. * @ssid: Pointer to network configuration data
  1514. */
  1515. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1516. {
  1517. ssid->proto = DEFAULT_PROTO;
  1518. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1519. ssid->group_cipher = DEFAULT_GROUP;
  1520. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1521. #ifdef IEEE8021X_EAPOL
  1522. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1523. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1524. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1525. #endif /* IEEE8021X_EAPOL */
  1526. }
  1527. /**
  1528. * wpa_config_set - Set a variable in network configuration
  1529. * @ssid: Pointer to network configuration data
  1530. * @var: Variable name, e.g., "ssid"
  1531. * @value: Variable value
  1532. * @line: Line number in configuration file or 0 if not used
  1533. * Returns: 0 on success, -1 on failure
  1534. *
  1535. * This function can be used to set network configuration variables based on
  1536. * both the configuration file and management interface input. The value
  1537. * parameter must be in the same format as the text-based configuration file is
  1538. * using. For example, strings are using double quotation marks.
  1539. */
  1540. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1541. int line)
  1542. {
  1543. size_t i;
  1544. int ret = 0;
  1545. if (ssid == NULL || var == NULL || value == NULL)
  1546. return -1;
  1547. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1548. const struct parse_data *field = &ssid_fields[i];
  1549. if (os_strcmp(var, field->name) != 0)
  1550. continue;
  1551. if (field->parser(field, ssid, line, value)) {
  1552. if (line) {
  1553. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1554. "parse %s '%s'.", line, var, value);
  1555. }
  1556. ret = -1;
  1557. }
  1558. break;
  1559. }
  1560. if (i == NUM_SSID_FIELDS) {
  1561. if (line) {
  1562. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1563. "'%s'.", line, var);
  1564. }
  1565. ret = -1;
  1566. }
  1567. return ret;
  1568. }
  1569. #ifndef NO_CONFIG_WRITE
  1570. /**
  1571. * wpa_config_get - Get a variable in network configuration
  1572. * @ssid: Pointer to network configuration data
  1573. * @var: Variable name, e.g., "ssid"
  1574. * Returns: Value of the variable or %NULL on failure
  1575. *
  1576. * This function can be used to get network configuration variables. The
  1577. * returned value is a copy of the configuration variable in text format, i.e,.
  1578. * the same format that the text-based configuration file and wpa_config_set()
  1579. * are using for the value. The caller is responsible for freeing the returned
  1580. * value.
  1581. */
  1582. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1583. {
  1584. size_t i;
  1585. if (ssid == NULL || var == NULL)
  1586. return NULL;
  1587. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1588. const struct parse_data *field = &ssid_fields[i];
  1589. if (os_strcmp(var, field->name) == 0)
  1590. return field->writer(field, ssid);
  1591. }
  1592. return NULL;
  1593. }
  1594. /**
  1595. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1596. * @ssid: Pointer to network configuration data
  1597. * @var: Variable name, e.g., "ssid"
  1598. * Returns: Value of the variable or %NULL on failure
  1599. *
  1600. * This function can be used to get network configuration variable like
  1601. * wpa_config_get(). The only difference is that this functions does not expose
  1602. * key/password material from the configuration. In case a key/password field
  1603. * is requested, the returned value is an empty string or %NULL if the variable
  1604. * is not set or "*" if the variable is set (regardless of its value). The
  1605. * returned value is a copy of the configuration variable in text format, i.e,.
  1606. * the same format that the text-based configuration file and wpa_config_set()
  1607. * are using for the value. The caller is responsible for freeing the returned
  1608. * value.
  1609. */
  1610. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1611. {
  1612. size_t i;
  1613. if (ssid == NULL || var == NULL)
  1614. return NULL;
  1615. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1616. const struct parse_data *field = &ssid_fields[i];
  1617. if (os_strcmp(var, field->name) == 0) {
  1618. char *res = field->writer(field, ssid);
  1619. if (field->key_data) {
  1620. if (res && res[0]) {
  1621. wpa_printf(MSG_DEBUG, "Do not allow "
  1622. "key_data field to be "
  1623. "exposed");
  1624. os_free(res);
  1625. return os_strdup("*");
  1626. }
  1627. os_free(res);
  1628. return NULL;
  1629. }
  1630. return res;
  1631. }
  1632. }
  1633. return NULL;
  1634. }
  1635. #endif /* NO_CONFIG_WRITE */
  1636. /**
  1637. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  1638. * @ssid: Pointer to network configuration data
  1639. *
  1640. * This function must be called to update WPA PSK when either SSID or the
  1641. * passphrase has changed for the network configuration.
  1642. */
  1643. void wpa_config_update_psk(struct wpa_ssid *ssid)
  1644. {
  1645. #ifndef CONFIG_NO_PBKDF2
  1646. pbkdf2_sha1(ssid->passphrase,
  1647. (char *) ssid->ssid, ssid->ssid_len, 4096,
  1648. ssid->psk, PMK_LEN);
  1649. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1650. ssid->psk, PMK_LEN);
  1651. ssid->psk_set = 1;
  1652. #endif /* CONFIG_NO_PBKDF2 */
  1653. }
  1654. #ifndef CONFIG_NO_CONFIG_BLOBS
  1655. /**
  1656. * wpa_config_get_blob - Get a named configuration blob
  1657. * @config: Configuration data from wpa_config_read()
  1658. * @name: Name of the blob
  1659. * Returns: Pointer to blob data or %NULL if not found
  1660. */
  1661. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  1662. const char *name)
  1663. {
  1664. struct wpa_config_blob *blob = config->blobs;
  1665. while (blob) {
  1666. if (os_strcmp(blob->name, name) == 0)
  1667. return blob;
  1668. blob = blob->next;
  1669. }
  1670. return NULL;
  1671. }
  1672. /**
  1673. * wpa_config_set_blob - Set or add a named configuration blob
  1674. * @config: Configuration data from wpa_config_read()
  1675. * @blob: New value for the blob
  1676. *
  1677. * Adds a new configuration blob or replaces the current value of an existing
  1678. * blob.
  1679. */
  1680. void wpa_config_set_blob(struct wpa_config *config,
  1681. struct wpa_config_blob *blob)
  1682. {
  1683. wpa_config_remove_blob(config, blob->name);
  1684. blob->next = config->blobs;
  1685. config->blobs = blob;
  1686. }
  1687. /**
  1688. * wpa_config_free_blob - Free blob data
  1689. * @blob: Pointer to blob to be freed
  1690. */
  1691. void wpa_config_free_blob(struct wpa_config_blob *blob)
  1692. {
  1693. if (blob) {
  1694. os_free(blob->name);
  1695. os_free(blob->data);
  1696. os_free(blob);
  1697. }
  1698. }
  1699. /**
  1700. * wpa_config_remove_blob - Remove a named configuration blob
  1701. * @config: Configuration data from wpa_config_read()
  1702. * @name: Name of the blob to remove
  1703. * Returns: 0 if blob was removed or -1 if blob was not found
  1704. */
  1705. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  1706. {
  1707. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  1708. while (pos) {
  1709. if (os_strcmp(pos->name, name) == 0) {
  1710. if (prev)
  1711. prev->next = pos->next;
  1712. else
  1713. config->blobs = pos->next;
  1714. wpa_config_free_blob(pos);
  1715. return 0;
  1716. }
  1717. prev = pos;
  1718. pos = pos->next;
  1719. }
  1720. return -1;
  1721. }
  1722. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1723. /**
  1724. * wpa_config_alloc_empty - Allocate an empty configuration
  1725. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  1726. * socket
  1727. * @driver_param: Driver parameters
  1728. * Returns: Pointer to allocated configuration data or %NULL on failure
  1729. */
  1730. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  1731. const char *driver_param)
  1732. {
  1733. struct wpa_config *config;
  1734. config = os_zalloc(sizeof(*config));
  1735. if (config == NULL)
  1736. return NULL;
  1737. config->eapol_version = DEFAULT_EAPOL_VERSION;
  1738. config->ap_scan = DEFAULT_AP_SCAN;
  1739. config->fast_reauth = DEFAULT_FAST_REAUTH;
  1740. if (ctrl_interface)
  1741. config->ctrl_interface = os_strdup(ctrl_interface);
  1742. if (driver_param)
  1743. config->driver_param = os_strdup(driver_param);
  1744. return config;
  1745. }
  1746. #ifndef CONFIG_NO_STDOUT_DEBUG
  1747. /**
  1748. * wpa_config_debug_dump_networks - Debug dump of configured networks
  1749. * @config: Configuration data from wpa_config_read()
  1750. */
  1751. void wpa_config_debug_dump_networks(struct wpa_config *config)
  1752. {
  1753. int prio;
  1754. struct wpa_ssid *ssid;
  1755. for (prio = 0; prio < config->num_prio; prio++) {
  1756. ssid = config->pssid[prio];
  1757. wpa_printf(MSG_DEBUG, "Priority group %d",
  1758. ssid->priority);
  1759. while (ssid) {
  1760. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  1761. ssid->id,
  1762. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1763. ssid = ssid->pnext;
  1764. }
  1765. }
  1766. }
  1767. #endif /* CONFIG_NO_STDOUT_DEBUG */