config.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  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, 4) },
  1268. { INT_RANGE(proactive_key_caching, 0, 1) },
  1269. { INT_RANGE(disabled, 0, 2) },
  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. int i;
  1443. ssid = config->ssid;
  1444. while (ssid) {
  1445. prev = ssid;
  1446. ssid = ssid->next;
  1447. wpa_config_free_ssid(prev);
  1448. }
  1449. #ifndef CONFIG_NO_CONFIG_BLOBS
  1450. blob = config->blobs;
  1451. prevblob = NULL;
  1452. while (blob) {
  1453. prevblob = blob;
  1454. blob = blob->next;
  1455. wpa_config_free_blob(prevblob);
  1456. }
  1457. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1458. os_free(config->ctrl_interface);
  1459. os_free(config->ctrl_interface_group);
  1460. os_free(config->opensc_engine_path);
  1461. os_free(config->pkcs11_engine_path);
  1462. os_free(config->pkcs11_module_path);
  1463. os_free(config->driver_param);
  1464. os_free(config->device_name);
  1465. os_free(config->manufacturer);
  1466. os_free(config->model_name);
  1467. os_free(config->model_number);
  1468. os_free(config->serial_number);
  1469. os_free(config->device_type);
  1470. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++)
  1471. os_free(config->sec_device_type[i]);
  1472. os_free(config->config_methods);
  1473. os_free(config->p2p_ssid_postfix);
  1474. os_free(config->pssid);
  1475. os_free(config);
  1476. }
  1477. /**
  1478. * wpa_config_get_network - Get configured network based on id
  1479. * @config: Configuration data from wpa_config_read()
  1480. * @id: Unique network id to search for
  1481. * Returns: Network configuration or %NULL if not found
  1482. */
  1483. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1484. {
  1485. struct wpa_ssid *ssid;
  1486. ssid = config->ssid;
  1487. while (ssid) {
  1488. if (id == ssid->id)
  1489. break;
  1490. ssid = ssid->next;
  1491. }
  1492. return ssid;
  1493. }
  1494. /**
  1495. * wpa_config_add_network - Add a new network with empty configuration
  1496. * @config: Configuration data from wpa_config_read()
  1497. * Returns: The new network configuration or %NULL if operation failed
  1498. */
  1499. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1500. {
  1501. int id;
  1502. struct wpa_ssid *ssid, *last = NULL;
  1503. id = -1;
  1504. ssid = config->ssid;
  1505. while (ssid) {
  1506. if (ssid->id > id)
  1507. id = ssid->id;
  1508. last = ssid;
  1509. ssid = ssid->next;
  1510. }
  1511. id++;
  1512. ssid = os_zalloc(sizeof(*ssid));
  1513. if (ssid == NULL)
  1514. return NULL;
  1515. ssid->id = id;
  1516. if (last)
  1517. last->next = ssid;
  1518. else
  1519. config->ssid = ssid;
  1520. wpa_config_update_prio_list(config);
  1521. return ssid;
  1522. }
  1523. /**
  1524. * wpa_config_remove_network - Remove a configured network based on id
  1525. * @config: Configuration data from wpa_config_read()
  1526. * @id: Unique network id to search for
  1527. * Returns: 0 on success, or -1 if the network was not found
  1528. */
  1529. int wpa_config_remove_network(struct wpa_config *config, int id)
  1530. {
  1531. struct wpa_ssid *ssid, *prev = NULL;
  1532. ssid = config->ssid;
  1533. while (ssid) {
  1534. if (id == ssid->id)
  1535. break;
  1536. prev = ssid;
  1537. ssid = ssid->next;
  1538. }
  1539. if (ssid == NULL)
  1540. return -1;
  1541. if (prev)
  1542. prev->next = ssid->next;
  1543. else
  1544. config->ssid = ssid->next;
  1545. wpa_config_update_prio_list(config);
  1546. wpa_config_free_ssid(ssid);
  1547. return 0;
  1548. }
  1549. /**
  1550. * wpa_config_set_network_defaults - Set network default values
  1551. * @ssid: Pointer to network configuration data
  1552. */
  1553. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1554. {
  1555. ssid->proto = DEFAULT_PROTO;
  1556. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1557. ssid->group_cipher = DEFAULT_GROUP;
  1558. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1559. #ifdef IEEE8021X_EAPOL
  1560. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1561. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1562. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1563. #endif /* IEEE8021X_EAPOL */
  1564. }
  1565. /**
  1566. * wpa_config_set - Set a variable in network configuration
  1567. * @ssid: Pointer to network configuration data
  1568. * @var: Variable name, e.g., "ssid"
  1569. * @value: Variable value
  1570. * @line: Line number in configuration file or 0 if not used
  1571. * Returns: 0 on success, -1 on failure
  1572. *
  1573. * This function can be used to set network configuration variables based on
  1574. * both the configuration file and management interface input. The value
  1575. * parameter must be in the same format as the text-based configuration file is
  1576. * using. For example, strings are using double quotation marks.
  1577. */
  1578. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1579. int line)
  1580. {
  1581. size_t i;
  1582. int ret = 0;
  1583. if (ssid == NULL || var == NULL || value == NULL)
  1584. return -1;
  1585. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1586. const struct parse_data *field = &ssid_fields[i];
  1587. if (os_strcmp(var, field->name) != 0)
  1588. continue;
  1589. if (field->parser(field, ssid, line, value)) {
  1590. if (line) {
  1591. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1592. "parse %s '%s'.", line, var, value);
  1593. }
  1594. ret = -1;
  1595. }
  1596. break;
  1597. }
  1598. if (i == NUM_SSID_FIELDS) {
  1599. if (line) {
  1600. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1601. "'%s'.", line, var);
  1602. }
  1603. ret = -1;
  1604. }
  1605. return ret;
  1606. }
  1607. /**
  1608. * wpa_config_get_all - Get all options from network configuration
  1609. * @ssid: Pointer to network configuration data
  1610. * @get_keys: Determines if keys/passwords will be included in returned list
  1611. * Returns: %NULL terminated list of all set keys and their values in the form
  1612. * of [key1, val1, key2, val2, ... , NULL]
  1613. *
  1614. * This function can be used to get list of all configured network properties.
  1615. * The caller is responsible for freeing the returned list and all its
  1616. * elements.
  1617. */
  1618. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  1619. {
  1620. const struct parse_data *field;
  1621. char *key, *value;
  1622. size_t i;
  1623. char **props;
  1624. int fields_num;
  1625. props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
  1626. if (!props)
  1627. return NULL;
  1628. fields_num = 0;
  1629. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1630. field = &ssid_fields[i];
  1631. if (field->key_data && !get_keys)
  1632. continue;
  1633. value = field->writer(field, ssid);
  1634. if (value == NULL)
  1635. continue;
  1636. if (os_strlen(value) == 0) {
  1637. os_free(value);
  1638. continue;
  1639. }
  1640. key = os_strdup(field->name);
  1641. if (key == NULL) {
  1642. os_free(value);
  1643. goto err;
  1644. }
  1645. props[fields_num * 2] = key;
  1646. props[fields_num * 2 + 1] = value;
  1647. fields_num++;
  1648. }
  1649. return props;
  1650. err:
  1651. value = *props;
  1652. while (value)
  1653. os_free(value++);
  1654. os_free(props);
  1655. return NULL;
  1656. }
  1657. #ifndef NO_CONFIG_WRITE
  1658. /**
  1659. * wpa_config_get - Get a variable in network configuration
  1660. * @ssid: Pointer to network configuration data
  1661. * @var: Variable name, e.g., "ssid"
  1662. * Returns: Value of the variable or %NULL on failure
  1663. *
  1664. * This function can be used to get network configuration variables. The
  1665. * returned value is a copy of the configuration variable in text format, i.e,.
  1666. * the same format that the text-based configuration file and wpa_config_set()
  1667. * are using for the value. The caller is responsible for freeing the returned
  1668. * value.
  1669. */
  1670. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1671. {
  1672. size_t i;
  1673. if (ssid == NULL || var == NULL)
  1674. return NULL;
  1675. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1676. const struct parse_data *field = &ssid_fields[i];
  1677. if (os_strcmp(var, field->name) == 0)
  1678. return field->writer(field, ssid);
  1679. }
  1680. return NULL;
  1681. }
  1682. /**
  1683. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1684. * @ssid: Pointer to network configuration data
  1685. * @var: Variable name, e.g., "ssid"
  1686. * Returns: Value of the variable or %NULL on failure
  1687. *
  1688. * This function can be used to get network configuration variable like
  1689. * wpa_config_get(). The only difference is that this functions does not expose
  1690. * key/password material from the configuration. In case a key/password field
  1691. * is requested, the returned value is an empty string or %NULL if the variable
  1692. * is not set or "*" if the variable is set (regardless of its value). The
  1693. * returned value is a copy of the configuration variable in text format, i.e,.
  1694. * the same format that the text-based configuration file and wpa_config_set()
  1695. * are using for the value. The caller is responsible for freeing the returned
  1696. * value.
  1697. */
  1698. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1699. {
  1700. size_t i;
  1701. if (ssid == NULL || var == NULL)
  1702. return NULL;
  1703. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1704. const struct parse_data *field = &ssid_fields[i];
  1705. if (os_strcmp(var, field->name) == 0) {
  1706. char *res = field->writer(field, ssid);
  1707. if (field->key_data) {
  1708. if (res && res[0]) {
  1709. wpa_printf(MSG_DEBUG, "Do not allow "
  1710. "key_data field to be "
  1711. "exposed");
  1712. os_free(res);
  1713. return os_strdup("*");
  1714. }
  1715. os_free(res);
  1716. return NULL;
  1717. }
  1718. return res;
  1719. }
  1720. }
  1721. return NULL;
  1722. }
  1723. #endif /* NO_CONFIG_WRITE */
  1724. /**
  1725. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  1726. * @ssid: Pointer to network configuration data
  1727. *
  1728. * This function must be called to update WPA PSK when either SSID or the
  1729. * passphrase has changed for the network configuration.
  1730. */
  1731. void wpa_config_update_psk(struct wpa_ssid *ssid)
  1732. {
  1733. #ifndef CONFIG_NO_PBKDF2
  1734. pbkdf2_sha1(ssid->passphrase,
  1735. (char *) ssid->ssid, ssid->ssid_len, 4096,
  1736. ssid->psk, PMK_LEN);
  1737. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1738. ssid->psk, PMK_LEN);
  1739. ssid->psk_set = 1;
  1740. #endif /* CONFIG_NO_PBKDF2 */
  1741. }
  1742. #ifndef CONFIG_NO_CONFIG_BLOBS
  1743. /**
  1744. * wpa_config_get_blob - Get a named configuration blob
  1745. * @config: Configuration data from wpa_config_read()
  1746. * @name: Name of the blob
  1747. * Returns: Pointer to blob data or %NULL if not found
  1748. */
  1749. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  1750. const char *name)
  1751. {
  1752. struct wpa_config_blob *blob = config->blobs;
  1753. while (blob) {
  1754. if (os_strcmp(blob->name, name) == 0)
  1755. return blob;
  1756. blob = blob->next;
  1757. }
  1758. return NULL;
  1759. }
  1760. /**
  1761. * wpa_config_set_blob - Set or add a named configuration blob
  1762. * @config: Configuration data from wpa_config_read()
  1763. * @blob: New value for the blob
  1764. *
  1765. * Adds a new configuration blob or replaces the current value of an existing
  1766. * blob.
  1767. */
  1768. void wpa_config_set_blob(struct wpa_config *config,
  1769. struct wpa_config_blob *blob)
  1770. {
  1771. wpa_config_remove_blob(config, blob->name);
  1772. blob->next = config->blobs;
  1773. config->blobs = blob;
  1774. }
  1775. /**
  1776. * wpa_config_free_blob - Free blob data
  1777. * @blob: Pointer to blob to be freed
  1778. */
  1779. void wpa_config_free_blob(struct wpa_config_blob *blob)
  1780. {
  1781. if (blob) {
  1782. os_free(blob->name);
  1783. os_free(blob->data);
  1784. os_free(blob);
  1785. }
  1786. }
  1787. /**
  1788. * wpa_config_remove_blob - Remove a named configuration blob
  1789. * @config: Configuration data from wpa_config_read()
  1790. * @name: Name of the blob to remove
  1791. * Returns: 0 if blob was removed or -1 if blob was not found
  1792. */
  1793. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  1794. {
  1795. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  1796. while (pos) {
  1797. if (os_strcmp(pos->name, name) == 0) {
  1798. if (prev)
  1799. prev->next = pos->next;
  1800. else
  1801. config->blobs = pos->next;
  1802. wpa_config_free_blob(pos);
  1803. return 0;
  1804. }
  1805. prev = pos;
  1806. pos = pos->next;
  1807. }
  1808. return -1;
  1809. }
  1810. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1811. /**
  1812. * wpa_config_alloc_empty - Allocate an empty configuration
  1813. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  1814. * socket
  1815. * @driver_param: Driver parameters
  1816. * Returns: Pointer to allocated configuration data or %NULL on failure
  1817. */
  1818. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  1819. const char *driver_param)
  1820. {
  1821. struct wpa_config *config;
  1822. config = os_zalloc(sizeof(*config));
  1823. if (config == NULL)
  1824. return NULL;
  1825. config->eapol_version = DEFAULT_EAPOL_VERSION;
  1826. config->ap_scan = DEFAULT_AP_SCAN;
  1827. config->fast_reauth = DEFAULT_FAST_REAUTH;
  1828. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  1829. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  1830. if (ctrl_interface)
  1831. config->ctrl_interface = os_strdup(ctrl_interface);
  1832. if (driver_param)
  1833. config->driver_param = os_strdup(driver_param);
  1834. return config;
  1835. }
  1836. #ifndef CONFIG_NO_STDOUT_DEBUG
  1837. /**
  1838. * wpa_config_debug_dump_networks - Debug dump of configured networks
  1839. * @config: Configuration data from wpa_config_read()
  1840. */
  1841. void wpa_config_debug_dump_networks(struct wpa_config *config)
  1842. {
  1843. int prio;
  1844. struct wpa_ssid *ssid;
  1845. for (prio = 0; prio < config->num_prio; prio++) {
  1846. ssid = config->pssid[prio];
  1847. wpa_printf(MSG_DEBUG, "Priority group %d",
  1848. ssid->priority);
  1849. while (ssid) {
  1850. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  1851. ssid->id,
  1852. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1853. ssid = ssid->pnext;
  1854. }
  1855. }
  1856. }
  1857. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1858. struct global_parse_data {
  1859. char *name;
  1860. int (*parser)(const struct global_parse_data *data,
  1861. struct wpa_config *config, int line, const char *value);
  1862. void *param1, *param2, *param3;
  1863. unsigned int changed_flag;
  1864. };
  1865. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  1866. struct wpa_config *config, int line,
  1867. const char *pos)
  1868. {
  1869. int *dst;
  1870. dst = (int *) (((u8 *) config) + (long) data->param1);
  1871. *dst = atoi(pos);
  1872. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  1873. if (data->param2 && *dst < (long) data->param2) {
  1874. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  1875. "min_value=%ld)", line, data->name, *dst,
  1876. (long) data->param2);
  1877. *dst = (long) data->param2;
  1878. return -1;
  1879. }
  1880. if (data->param3 && *dst > (long) data->param3) {
  1881. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  1882. "max_value=%ld)", line, data->name, *dst,
  1883. (long) data->param3);
  1884. *dst = (long) data->param3;
  1885. return -1;
  1886. }
  1887. return 0;
  1888. }
  1889. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  1890. struct wpa_config *config, int line,
  1891. const char *pos)
  1892. {
  1893. size_t len;
  1894. char **dst, *tmp;
  1895. len = os_strlen(pos);
  1896. if (data->param2 && len < (size_t) data->param2) {
  1897. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  1898. "min_len=%ld)", line, data->name,
  1899. (unsigned long) len, (long) data->param2);
  1900. return -1;
  1901. }
  1902. if (data->param3 && len > (size_t) data->param3) {
  1903. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  1904. "max_len=%ld)", line, data->name,
  1905. (unsigned long) len, (long) data->param3);
  1906. return -1;
  1907. }
  1908. tmp = os_strdup(pos);
  1909. if (tmp == NULL)
  1910. return -1;
  1911. dst = (char **) (((u8 *) config) + (long) data->param1);
  1912. os_free(*dst);
  1913. *dst = tmp;
  1914. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  1915. return 0;
  1916. }
  1917. static int wpa_config_process_country(const struct global_parse_data *data,
  1918. struct wpa_config *config, int line,
  1919. const char *pos)
  1920. {
  1921. if (!pos[0] || !pos[1]) {
  1922. wpa_printf(MSG_DEBUG, "Invalid country set");
  1923. return -1;
  1924. }
  1925. config->country[0] = pos[0];
  1926. config->country[1] = pos[1];
  1927. wpa_printf(MSG_DEBUG, "country='%c%c'",
  1928. config->country[0], config->country[1]);
  1929. return 0;
  1930. }
  1931. static int wpa_config_process_load_dynamic_eap(
  1932. const struct global_parse_data *data, struct wpa_config *config,
  1933. int line, const char *so)
  1934. {
  1935. int ret;
  1936. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  1937. ret = eap_peer_method_load(so);
  1938. if (ret == -2) {
  1939. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  1940. "reloading.");
  1941. } else if (ret) {
  1942. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  1943. "method '%s'.", line, so);
  1944. return -1;
  1945. }
  1946. return 0;
  1947. }
  1948. #ifdef CONFIG_WPS
  1949. static int wpa_config_process_uuid(const struct global_parse_data *data,
  1950. struct wpa_config *config, int line,
  1951. const char *pos)
  1952. {
  1953. char buf[40];
  1954. if (uuid_str2bin(pos, config->uuid)) {
  1955. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  1956. return -1;
  1957. }
  1958. uuid_bin2str(config->uuid, buf, sizeof(buf));
  1959. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  1960. return 0;
  1961. }
  1962. static int wpa_config_process_os_version(const struct global_parse_data *data,
  1963. struct wpa_config *config, int line,
  1964. const char *pos)
  1965. {
  1966. if (hexstr2bin(pos, config->os_version, 4)) {
  1967. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  1968. return -1;
  1969. }
  1970. wpa_printf(MSG_DEBUG, "os_version=%08x",
  1971. WPA_GET_BE32(config->os_version));
  1972. return 0;
  1973. }
  1974. #endif /* CONFIG_WPS */
  1975. #ifdef CONFIG_P2P
  1976. static int wpa_config_process_sec_device_type(
  1977. const struct global_parse_data *data,
  1978. struct wpa_config *config, int line, const char *pos)
  1979. {
  1980. int idx;
  1981. for (idx = 0; idx < MAX_SEC_DEVICE_TYPES; idx++)
  1982. if (config->sec_device_type[idx] == NULL)
  1983. break;
  1984. if (idx == MAX_SEC_DEVICE_TYPES) {
  1985. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  1986. "items", line);
  1987. return -1;
  1988. }
  1989. config->sec_device_type[idx] = os_strdup(pos);
  1990. if (config->sec_device_type[idx] == NULL)
  1991. return -1;
  1992. return 0;
  1993. }
  1994. #endif /* CONFIG_P2P */
  1995. #ifdef OFFSET
  1996. #undef OFFSET
  1997. #endif /* OFFSET */
  1998. /* OFFSET: Get offset of a variable within the wpa_config structure */
  1999. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  2000. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  2001. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  2002. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  2003. #define INT(f) _INT(f), NULL, NULL
  2004. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  2005. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  2006. #define STR(f) _STR(f), NULL, NULL
  2007. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  2008. static const struct global_parse_data global_fields[] = {
  2009. #ifdef CONFIG_CTRL_IFACE
  2010. { STR(ctrl_interface), 0 },
  2011. { STR(ctrl_interface_group), 0 } /* deprecated */,
  2012. #endif /* CONFIG_CTRL_IFACE */
  2013. { INT_RANGE(eapol_version, 1, 2), 0 },
  2014. { INT(ap_scan), 0 },
  2015. { INT(fast_reauth), 0 },
  2016. { STR(opensc_engine_path), 0 },
  2017. { STR(pkcs11_engine_path), 0 },
  2018. { STR(pkcs11_module_path), 0 },
  2019. { STR(driver_param), 0 },
  2020. { INT(dot11RSNAConfigPMKLifetime), 0 },
  2021. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  2022. { INT(dot11RSNAConfigSATimeout), 0 },
  2023. #ifndef CONFIG_NO_CONFIG_WRITE
  2024. { INT(update_config), 0 },
  2025. #endif /* CONFIG_NO_CONFIG_WRITE */
  2026. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  2027. #ifdef CONFIG_WPS
  2028. { FUNC(uuid), CFG_CHANGED_UUID },
  2029. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  2030. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  2031. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  2032. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2033. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2034. { STR(device_type), CFG_CHANGED_DEVICE_TYPE },
  2035. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  2036. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  2037. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  2038. #endif /* CONFIG_WPS */
  2039. #ifdef CONFIG_P2P
  2040. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  2041. { INT(p2p_listen_reg_class), 0 },
  2042. { INT(p2p_listen_channel), 0 },
  2043. { INT(p2p_oper_reg_class), 0 },
  2044. { INT(p2p_oper_channel), 0 },
  2045. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  2046. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  2047. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  2048. #endif /* CONFIG_P2P */
  2049. { FUNC(country), CFG_CHANGED_COUNTRY },
  2050. { INT(bss_max_count), 0 },
  2051. { INT_RANGE(filter_ssids, 0, 1), 0 }
  2052. };
  2053. #undef FUNC
  2054. #undef _INT
  2055. #undef INT
  2056. #undef INT_RANGE
  2057. #undef _STR
  2058. #undef STR
  2059. #undef STR_RANGE
  2060. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  2061. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  2062. {
  2063. size_t i;
  2064. int ret = 0;
  2065. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  2066. const struct global_parse_data *field = &global_fields[i];
  2067. size_t flen = os_strlen(field->name);
  2068. if (os_strncmp(pos, field->name, flen) != 0 ||
  2069. pos[flen] != '=')
  2070. continue;
  2071. if (field->parser(field, config, line, pos + flen + 1)) {
  2072. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2073. "parse '%s'.", line, pos);
  2074. ret = -1;
  2075. }
  2076. config->changed_parameters |= field->changed_flag;
  2077. break;
  2078. }
  2079. if (i == NUM_GLOBAL_FIELDS) {
  2080. if (line < 0)
  2081. return -1;
  2082. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  2083. line, pos);
  2084. ret = -1;
  2085. }
  2086. return ret;
  2087. }