config.c 57 KB

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