您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

config.c 60KB

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