config.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  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. if (ctrl_interface)
  1861. config->ctrl_interface = os_strdup(ctrl_interface);
  1862. if (driver_param)
  1863. config->driver_param = os_strdup(driver_param);
  1864. return config;
  1865. }
  1866. #ifndef CONFIG_NO_STDOUT_DEBUG
  1867. /**
  1868. * wpa_config_debug_dump_networks - Debug dump of configured networks
  1869. * @config: Configuration data from wpa_config_read()
  1870. */
  1871. void wpa_config_debug_dump_networks(struct wpa_config *config)
  1872. {
  1873. int prio;
  1874. struct wpa_ssid *ssid;
  1875. for (prio = 0; prio < config->num_prio; prio++) {
  1876. ssid = config->pssid[prio];
  1877. wpa_printf(MSG_DEBUG, "Priority group %d",
  1878. ssid->priority);
  1879. while (ssid) {
  1880. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  1881. ssid->id,
  1882. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1883. ssid = ssid->pnext;
  1884. }
  1885. }
  1886. }
  1887. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1888. struct global_parse_data {
  1889. char *name;
  1890. int (*parser)(const struct global_parse_data *data,
  1891. struct wpa_config *config, int line, const char *value);
  1892. void *param1, *param2, *param3;
  1893. unsigned int changed_flag;
  1894. };
  1895. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  1896. struct wpa_config *config, int line,
  1897. const char *pos)
  1898. {
  1899. int *dst;
  1900. dst = (int *) (((u8 *) config) + (long) data->param1);
  1901. *dst = atoi(pos);
  1902. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  1903. if (data->param2 && *dst < (long) data->param2) {
  1904. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  1905. "min_value=%ld)", line, data->name, *dst,
  1906. (long) data->param2);
  1907. *dst = (long) data->param2;
  1908. return -1;
  1909. }
  1910. if (data->param3 && *dst > (long) data->param3) {
  1911. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  1912. "max_value=%ld)", line, data->name, *dst,
  1913. (long) data->param3);
  1914. *dst = (long) data->param3;
  1915. return -1;
  1916. }
  1917. return 0;
  1918. }
  1919. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  1920. struct wpa_config *config, int line,
  1921. const char *pos)
  1922. {
  1923. size_t len;
  1924. char **dst, *tmp;
  1925. len = os_strlen(pos);
  1926. if (data->param2 && len < (size_t) data->param2) {
  1927. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  1928. "min_len=%ld)", line, data->name,
  1929. (unsigned long) len, (long) data->param2);
  1930. return -1;
  1931. }
  1932. if (data->param3 && len > (size_t) data->param3) {
  1933. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  1934. "max_len=%ld)", line, data->name,
  1935. (unsigned long) len, (long) data->param3);
  1936. return -1;
  1937. }
  1938. tmp = os_strdup(pos);
  1939. if (tmp == NULL)
  1940. return -1;
  1941. dst = (char **) (((u8 *) config) + (long) data->param1);
  1942. os_free(*dst);
  1943. *dst = tmp;
  1944. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  1945. return 0;
  1946. }
  1947. static int wpa_config_process_country(const struct global_parse_data *data,
  1948. struct wpa_config *config, int line,
  1949. const char *pos)
  1950. {
  1951. if (!pos[0] || !pos[1]) {
  1952. wpa_printf(MSG_DEBUG, "Invalid country set");
  1953. return -1;
  1954. }
  1955. config->country[0] = pos[0];
  1956. config->country[1] = pos[1];
  1957. wpa_printf(MSG_DEBUG, "country='%c%c'",
  1958. config->country[0], config->country[1]);
  1959. return 0;
  1960. }
  1961. static int wpa_config_process_load_dynamic_eap(
  1962. const struct global_parse_data *data, struct wpa_config *config,
  1963. int line, const char *so)
  1964. {
  1965. int ret;
  1966. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  1967. ret = eap_peer_method_load(so);
  1968. if (ret == -2) {
  1969. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  1970. "reloading.");
  1971. } else if (ret) {
  1972. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  1973. "method '%s'.", line, so);
  1974. return -1;
  1975. }
  1976. return 0;
  1977. }
  1978. #ifdef CONFIG_WPS
  1979. static int wpa_config_process_uuid(const struct global_parse_data *data,
  1980. struct wpa_config *config, int line,
  1981. const char *pos)
  1982. {
  1983. char buf[40];
  1984. if (uuid_str2bin(pos, config->uuid)) {
  1985. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  1986. return -1;
  1987. }
  1988. uuid_bin2str(config->uuid, buf, sizeof(buf));
  1989. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  1990. return 0;
  1991. }
  1992. static int wpa_config_process_device_type(
  1993. const struct global_parse_data *data,
  1994. struct wpa_config *config, int line, const char *pos)
  1995. {
  1996. return wps_dev_type_str2bin(pos, config->device_type);
  1997. }
  1998. static int wpa_config_process_os_version(const struct global_parse_data *data,
  1999. struct wpa_config *config, int line,
  2000. const char *pos)
  2001. {
  2002. if (hexstr2bin(pos, config->os_version, 4)) {
  2003. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  2004. return -1;
  2005. }
  2006. wpa_printf(MSG_DEBUG, "os_version=%08x",
  2007. WPA_GET_BE32(config->os_version));
  2008. return 0;
  2009. }
  2010. #endif /* CONFIG_WPS */
  2011. #ifdef CONFIG_P2P
  2012. static int wpa_config_process_sec_device_type(
  2013. const struct global_parse_data *data,
  2014. struct wpa_config *config, int line, const char *pos)
  2015. {
  2016. int idx;
  2017. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  2018. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  2019. "items", line);
  2020. return -1;
  2021. }
  2022. idx = config->num_sec_device_types;
  2023. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  2024. return -1;
  2025. config->num_sec_device_types++;
  2026. return 0;
  2027. }
  2028. #endif /* CONFIG_P2P */
  2029. #ifdef OFFSET
  2030. #undef OFFSET
  2031. #endif /* OFFSET */
  2032. /* OFFSET: Get offset of a variable within the wpa_config structure */
  2033. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  2034. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  2035. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  2036. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  2037. #define INT(f) _INT(f), NULL, NULL
  2038. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  2039. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  2040. #define STR(f) _STR(f), NULL, NULL
  2041. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  2042. static const struct global_parse_data global_fields[] = {
  2043. #ifdef CONFIG_CTRL_IFACE
  2044. { STR(ctrl_interface), 0 },
  2045. { STR(ctrl_interface_group), 0 } /* deprecated */,
  2046. #endif /* CONFIG_CTRL_IFACE */
  2047. { INT_RANGE(eapol_version, 1, 2), 0 },
  2048. { INT(ap_scan), 0 },
  2049. { INT(fast_reauth), 0 },
  2050. { STR(opensc_engine_path), 0 },
  2051. { STR(pkcs11_engine_path), 0 },
  2052. { STR(pkcs11_module_path), 0 },
  2053. { STR(driver_param), 0 },
  2054. { INT(dot11RSNAConfigPMKLifetime), 0 },
  2055. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  2056. { INT(dot11RSNAConfigSATimeout), 0 },
  2057. #ifndef CONFIG_NO_CONFIG_WRITE
  2058. { INT(update_config), 0 },
  2059. #endif /* CONFIG_NO_CONFIG_WRITE */
  2060. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  2061. #ifdef CONFIG_WPS
  2062. { FUNC(uuid), CFG_CHANGED_UUID },
  2063. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  2064. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  2065. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  2066. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2067. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2068. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  2069. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  2070. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  2071. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  2072. #endif /* CONFIG_WPS */
  2073. #ifdef CONFIG_P2P
  2074. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  2075. { INT(p2p_listen_reg_class), 0 },
  2076. { INT(p2p_listen_channel), 0 },
  2077. { INT(p2p_oper_reg_class), 0 },
  2078. { INT(p2p_oper_channel), 0 },
  2079. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  2080. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  2081. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  2082. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  2083. { INT(p2p_group_idle), 0 },
  2084. #endif /* CONFIG_P2P */
  2085. { FUNC(country), CFG_CHANGED_COUNTRY },
  2086. { INT(bss_max_count), 0 },
  2087. { INT(bss_expiration_age), 0 },
  2088. { INT(bss_expiration_scan_count), 0 },
  2089. { INT_RANGE(filter_ssids, 0, 1), 0 },
  2090. { INT(max_num_sta), 0 },
  2091. { INT_RANGE(disassoc_low_ack, 0, 1), 0 }
  2092. };
  2093. #undef FUNC
  2094. #undef _INT
  2095. #undef INT
  2096. #undef INT_RANGE
  2097. #undef _STR
  2098. #undef STR
  2099. #undef STR_RANGE
  2100. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  2101. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  2102. {
  2103. size_t i;
  2104. int ret = 0;
  2105. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  2106. const struct global_parse_data *field = &global_fields[i];
  2107. size_t flen = os_strlen(field->name);
  2108. if (os_strncmp(pos, field->name, flen) != 0 ||
  2109. pos[flen] != '=')
  2110. continue;
  2111. if (field->parser(field, config, line, pos + flen + 1)) {
  2112. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2113. "parse '%s'.", line, pos);
  2114. ret = -1;
  2115. }
  2116. config->changed_parameters |= field->changed_flag;
  2117. break;
  2118. }
  2119. if (i == NUM_GLOBAL_FIELDS) {
  2120. if (line < 0)
  2121. return -1;
  2122. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  2123. line, pos);
  2124. ret = -1;
  2125. }
  2126. return ret;
  2127. }