config.c 45 KB

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