config.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  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. { STRe(cert_id) },
  1101. { STRe(ca_cert_id) },
  1102. { STRe(key2_id) },
  1103. { STRe(cert2_id) },
  1104. { STRe(ca_cert2_id) },
  1105. { INTe(engine) },
  1106. { INT(eapol_flags) },
  1107. #endif /* IEEE8021X_EAPOL */
  1108. { FUNC_KEY(wep_key0) },
  1109. { FUNC_KEY(wep_key1) },
  1110. { FUNC_KEY(wep_key2) },
  1111. { FUNC_KEY(wep_key3) },
  1112. { INT(wep_tx_keyidx) },
  1113. { INT(priority) },
  1114. #ifdef IEEE8021X_EAPOL
  1115. { INT(eap_workaround) },
  1116. { STRe(pac_file) },
  1117. { INTe(fragment_size) },
  1118. #endif /* IEEE8021X_EAPOL */
  1119. { INT_RANGE(mode, 0, 1) },
  1120. { INT_RANGE(proactive_key_caching, 0, 1) },
  1121. { INT_RANGE(disabled, 0, 1) },
  1122. { STR(id_str) },
  1123. #ifdef CONFIG_IEEE80211W
  1124. { INT_RANGE(ieee80211w, 0, 2) },
  1125. #endif /* CONFIG_IEEE80211W */
  1126. { INT_RANGE(peerkey, 0, 1) },
  1127. { INT_RANGE(mixed_cell, 0, 1) },
  1128. { INT_RANGE(frequency, 0, 10000) }
  1129. };
  1130. #undef OFFSET
  1131. #undef _STR
  1132. #undef STR
  1133. #undef STR_KEY
  1134. #undef _STR_LEN
  1135. #undef STR_LEN
  1136. #undef STR_LEN_KEY
  1137. #undef _STR_RANGE
  1138. #undef STR_RANGE
  1139. #undef STR_RANGE_KEY
  1140. #undef _INT
  1141. #undef INT
  1142. #undef INT_RANGE
  1143. #undef _FUNC
  1144. #undef FUNC
  1145. #undef FUNC_KEY
  1146. #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
  1147. /**
  1148. * wpa_config_add_prio_network - Add a network to priority lists
  1149. * @config: Configuration data from wpa_config_read()
  1150. * @ssid: Pointer to the network configuration to be added to the list
  1151. * Returns: 0 on success, -1 on failure
  1152. *
  1153. * This function is used to add a network block to the priority list of
  1154. * networks. This must be called for each network when reading in the full
  1155. * configuration. In addition, this can be used indirectly when updating
  1156. * priorities by calling wpa_config_update_prio_list().
  1157. */
  1158. int wpa_config_add_prio_network(struct wpa_config *config,
  1159. struct wpa_ssid *ssid)
  1160. {
  1161. int prio;
  1162. struct wpa_ssid *prev, **nlist;
  1163. /*
  1164. * Add to an existing priority list if one is available for the
  1165. * configured priority level for this network.
  1166. */
  1167. for (prio = 0; prio < config->num_prio; prio++) {
  1168. prev = config->pssid[prio];
  1169. if (prev->priority == ssid->priority) {
  1170. while (prev->pnext)
  1171. prev = prev->pnext;
  1172. prev->pnext = ssid;
  1173. return 0;
  1174. }
  1175. }
  1176. /* First network for this priority - add a new priority list */
  1177. nlist = os_realloc(config->pssid,
  1178. (config->num_prio + 1) * sizeof(struct wpa_ssid *));
  1179. if (nlist == NULL)
  1180. return -1;
  1181. for (prio = 0; prio < config->num_prio; prio++) {
  1182. if (nlist[prio]->priority < ssid->priority)
  1183. break;
  1184. }
  1185. os_memmove(&nlist[prio + 1], &nlist[prio],
  1186. (config->num_prio - prio) * sizeof(struct wpa_ssid *));
  1187. nlist[prio] = ssid;
  1188. config->num_prio++;
  1189. config->pssid = nlist;
  1190. return 0;
  1191. }
  1192. /**
  1193. * wpa_config_update_prio_list - Update network priority list
  1194. * @config: Configuration data from wpa_config_read()
  1195. * Returns: 0 on success, -1 on failure
  1196. *
  1197. * This function is called to update the priority list of networks in the
  1198. * configuration when a network is being added or removed. This is also called
  1199. * if a priority for a network is changed.
  1200. */
  1201. static int wpa_config_update_prio_list(struct wpa_config *config)
  1202. {
  1203. struct wpa_ssid *ssid;
  1204. int ret = 0;
  1205. os_free(config->pssid);
  1206. config->pssid = NULL;
  1207. config->num_prio = 0;
  1208. ssid = config->ssid;
  1209. while (ssid) {
  1210. ssid->pnext = NULL;
  1211. if (wpa_config_add_prio_network(config, ssid) < 0)
  1212. ret = -1;
  1213. ssid = ssid->next;
  1214. }
  1215. return ret;
  1216. }
  1217. #ifdef IEEE8021X_EAPOL
  1218. static void eap_peer_config_free(struct eap_peer_config *eap)
  1219. {
  1220. os_free(eap->eap_methods);
  1221. os_free(eap->identity);
  1222. os_free(eap->anonymous_identity);
  1223. os_free(eap->password);
  1224. os_free(eap->ca_cert);
  1225. os_free(eap->ca_path);
  1226. os_free(eap->client_cert);
  1227. os_free(eap->private_key);
  1228. os_free(eap->private_key_passwd);
  1229. os_free(eap->dh_file);
  1230. os_free(eap->subject_match);
  1231. os_free(eap->altsubject_match);
  1232. os_free(eap->ca_cert2);
  1233. os_free(eap->ca_path2);
  1234. os_free(eap->client_cert2);
  1235. os_free(eap->private_key2);
  1236. os_free(eap->private_key2_passwd);
  1237. os_free(eap->dh_file2);
  1238. os_free(eap->subject_match2);
  1239. os_free(eap->altsubject_match2);
  1240. os_free(eap->phase1);
  1241. os_free(eap->phase2);
  1242. os_free(eap->pcsc);
  1243. os_free(eap->pin);
  1244. os_free(eap->engine_id);
  1245. os_free(eap->key_id);
  1246. os_free(eap->cert_id);
  1247. os_free(eap->ca_cert_id);
  1248. os_free(eap->key2_id);
  1249. os_free(eap->cert2_id);
  1250. os_free(eap->ca_cert2_id);
  1251. os_free(eap->otp);
  1252. os_free(eap->pending_req_otp);
  1253. os_free(eap->pac_file);
  1254. os_free(eap->new_password);
  1255. }
  1256. #endif /* IEEE8021X_EAPOL */
  1257. /**
  1258. * wpa_config_free_ssid - Free network/ssid configuration data
  1259. * @ssid: Configuration data for the network
  1260. *
  1261. * This function frees all resources allocated for the network configuration
  1262. * data.
  1263. */
  1264. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1265. {
  1266. os_free(ssid->ssid);
  1267. os_free(ssid->passphrase);
  1268. #ifdef IEEE8021X_EAPOL
  1269. eap_peer_config_free(&ssid->eap);
  1270. #endif /* IEEE8021X_EAPOL */
  1271. os_free(ssid->id_str);
  1272. os_free(ssid);
  1273. }
  1274. /**
  1275. * wpa_config_free - Free configuration data
  1276. * @config: Configuration data from wpa_config_read()
  1277. *
  1278. * This function frees all resources allocated for the configuration data by
  1279. * wpa_config_read().
  1280. */
  1281. void wpa_config_free(struct wpa_config *config)
  1282. {
  1283. #ifndef CONFIG_NO_CONFIG_BLOBS
  1284. struct wpa_config_blob *blob, *prevblob;
  1285. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1286. struct wpa_ssid *ssid, *prev = NULL;
  1287. ssid = config->ssid;
  1288. while (ssid) {
  1289. prev = ssid;
  1290. ssid = ssid->next;
  1291. wpa_config_free_ssid(prev);
  1292. }
  1293. #ifndef CONFIG_NO_CONFIG_BLOBS
  1294. blob = config->blobs;
  1295. prevblob = NULL;
  1296. while (blob) {
  1297. prevblob = blob;
  1298. blob = blob->next;
  1299. wpa_config_free_blob(prevblob);
  1300. }
  1301. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1302. os_free(config->ctrl_interface);
  1303. os_free(config->ctrl_interface_group);
  1304. #ifdef EAP_TLS_OPENSSL
  1305. os_free(config->opensc_engine_path);
  1306. os_free(config->pkcs11_engine_path);
  1307. os_free(config->pkcs11_module_path);
  1308. #endif /* EAP_TLS_OPENSSL */
  1309. os_free(config->driver_param);
  1310. os_free(config->pssid);
  1311. os_free(config);
  1312. }
  1313. /**
  1314. * wpa_config_get_network - Get configured network based on id
  1315. * @config: Configuration data from wpa_config_read()
  1316. * @id: Unique network id to search for
  1317. * Returns: Network configuration or %NULL if not found
  1318. */
  1319. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1320. {
  1321. struct wpa_ssid *ssid;
  1322. ssid = config->ssid;
  1323. while (ssid) {
  1324. if (id == ssid->id)
  1325. break;
  1326. ssid = ssid->next;
  1327. }
  1328. return ssid;
  1329. }
  1330. /**
  1331. * wpa_config_add_network - Add a new network with empty configuration
  1332. * @config: Configuration data from wpa_config_read()
  1333. * Returns: The new network configuration or %NULL if operation failed
  1334. */
  1335. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1336. {
  1337. int id;
  1338. struct wpa_ssid *ssid, *last = NULL;
  1339. id = -1;
  1340. ssid = config->ssid;
  1341. while (ssid) {
  1342. if (ssid->id > id)
  1343. id = ssid->id;
  1344. last = ssid;
  1345. ssid = ssid->next;
  1346. }
  1347. id++;
  1348. ssid = os_zalloc(sizeof(*ssid));
  1349. if (ssid == NULL)
  1350. return NULL;
  1351. ssid->id = id;
  1352. if (last)
  1353. last->next = ssid;
  1354. else
  1355. config->ssid = ssid;
  1356. wpa_config_update_prio_list(config);
  1357. return ssid;
  1358. }
  1359. /**
  1360. * wpa_config_remove_network - Remove a configured network based on id
  1361. * @config: Configuration data from wpa_config_read()
  1362. * @id: Unique network id to search for
  1363. * Returns: 0 on success, or -1 if the network was not found
  1364. */
  1365. int wpa_config_remove_network(struct wpa_config *config, int id)
  1366. {
  1367. struct wpa_ssid *ssid, *prev = NULL;
  1368. ssid = config->ssid;
  1369. while (ssid) {
  1370. if (id == ssid->id)
  1371. break;
  1372. prev = ssid;
  1373. ssid = ssid->next;
  1374. }
  1375. if (ssid == NULL)
  1376. return -1;
  1377. if (prev)
  1378. prev->next = ssid->next;
  1379. else
  1380. config->ssid = ssid->next;
  1381. wpa_config_update_prio_list(config);
  1382. wpa_config_free_ssid(ssid);
  1383. return 0;
  1384. }
  1385. /**
  1386. * wpa_config_set_network_defaults - Set network default values
  1387. * @ssid: Pointer to network configuration data
  1388. */
  1389. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1390. {
  1391. ssid->proto = DEFAULT_PROTO;
  1392. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1393. ssid->group_cipher = DEFAULT_GROUP;
  1394. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1395. #ifdef IEEE8021X_EAPOL
  1396. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1397. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1398. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1399. #endif /* IEEE8021X_EAPOL */
  1400. }
  1401. /**
  1402. * wpa_config_set - Set a variable in network configuration
  1403. * @ssid: Pointer to network configuration data
  1404. * @var: Variable name, e.g., "ssid"
  1405. * @value: Variable value
  1406. * @line: Line number in configuration file or 0 if not used
  1407. * Returns: 0 on success, -1 on failure
  1408. *
  1409. * This function can be used to set network configuration variables based on
  1410. * both the configuration file and management interface input. The value
  1411. * parameter must be in the same format as the text-based configuration file is
  1412. * using. For example, strings are using double quotation marks.
  1413. */
  1414. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1415. int line)
  1416. {
  1417. size_t i;
  1418. int ret = 0;
  1419. if (ssid == NULL || var == NULL || value == NULL)
  1420. return -1;
  1421. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1422. const struct parse_data *field = &ssid_fields[i];
  1423. if (os_strcmp(var, field->name) != 0)
  1424. continue;
  1425. if (field->parser(field, ssid, line, value)) {
  1426. if (line) {
  1427. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1428. "parse %s '%s'.", line, var, value);
  1429. }
  1430. ret = -1;
  1431. }
  1432. break;
  1433. }
  1434. if (i == NUM_SSID_FIELDS) {
  1435. if (line) {
  1436. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1437. "'%s'.", line, var);
  1438. }
  1439. ret = -1;
  1440. }
  1441. return ret;
  1442. }
  1443. #ifndef NO_CONFIG_WRITE
  1444. /**
  1445. * wpa_config_get - Get a variable in network configuration
  1446. * @ssid: Pointer to network configuration data
  1447. * @var: Variable name, e.g., "ssid"
  1448. * Returns: Value of the variable or %NULL on failure
  1449. *
  1450. * This function can be used to get network configuration variables. The
  1451. * returned value is a copy of the configuration variable in text format, i.e,.
  1452. * the same format that the text-based configuration file and wpa_config_set()
  1453. * are using for the value. The caller is responsible for freeing the returned
  1454. * value.
  1455. */
  1456. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1457. {
  1458. size_t i;
  1459. if (ssid == NULL || var == NULL)
  1460. return NULL;
  1461. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1462. const struct parse_data *field = &ssid_fields[i];
  1463. if (os_strcmp(var, field->name) == 0)
  1464. return field->writer(field, ssid);
  1465. }
  1466. return NULL;
  1467. }
  1468. /**
  1469. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1470. * @ssid: Pointer to network configuration data
  1471. * @var: Variable name, e.g., "ssid"
  1472. * Returns: Value of the variable or %NULL on failure
  1473. *
  1474. * This function can be used to get network configuration variable like
  1475. * wpa_config_get(). The only difference is that this functions does not expose
  1476. * key/password material from the configuration. In case a key/password field
  1477. * is requested, the returned value is an empty string or %NULL if the variable
  1478. * is not set or "*" if the variable is set (regardless of its value). The
  1479. * returned value is a copy of the configuration variable in text format, i.e,.
  1480. * the same format that the text-based configuration file and wpa_config_set()
  1481. * are using for the value. The caller is responsible for freeing the returned
  1482. * value.
  1483. */
  1484. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1485. {
  1486. size_t i;
  1487. if (ssid == NULL || var == NULL)
  1488. return NULL;
  1489. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1490. const struct parse_data *field = &ssid_fields[i];
  1491. if (os_strcmp(var, field->name) == 0) {
  1492. char *res = field->writer(field, ssid);
  1493. if (field->key_data) {
  1494. if (res && res[0]) {
  1495. wpa_printf(MSG_DEBUG, "Do not allow "
  1496. "key_data field to be "
  1497. "exposed");
  1498. os_free(res);
  1499. return os_strdup("*");
  1500. }
  1501. os_free(res);
  1502. return NULL;
  1503. }
  1504. return res;
  1505. }
  1506. }
  1507. return NULL;
  1508. }
  1509. #endif /* NO_CONFIG_WRITE */
  1510. /**
  1511. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  1512. * @ssid: Pointer to network configuration data
  1513. *
  1514. * This function must be called to update WPA PSK when either SSID or the
  1515. * passphrase has changed for the network configuration.
  1516. */
  1517. void wpa_config_update_psk(struct wpa_ssid *ssid)
  1518. {
  1519. #ifndef CONFIG_NO_PBKDF2
  1520. pbkdf2_sha1(ssid->passphrase,
  1521. (char *) ssid->ssid, ssid->ssid_len, 4096,
  1522. ssid->psk, PMK_LEN);
  1523. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1524. ssid->psk, PMK_LEN);
  1525. ssid->psk_set = 1;
  1526. #endif /* CONFIG_NO_PBKDF2 */
  1527. }
  1528. #ifndef CONFIG_NO_CONFIG_BLOBS
  1529. /**
  1530. * wpa_config_get_blob - Get a named configuration blob
  1531. * @config: Configuration data from wpa_config_read()
  1532. * @name: Name of the blob
  1533. * Returns: Pointer to blob data or %NULL if not found
  1534. */
  1535. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  1536. const char *name)
  1537. {
  1538. struct wpa_config_blob *blob = config->blobs;
  1539. while (blob) {
  1540. if (os_strcmp(blob->name, name) == 0)
  1541. return blob;
  1542. blob = blob->next;
  1543. }
  1544. return NULL;
  1545. }
  1546. /**
  1547. * wpa_config_set_blob - Set or add a named configuration blob
  1548. * @config: Configuration data from wpa_config_read()
  1549. * @blob: New value for the blob
  1550. *
  1551. * Adds a new configuration blob or replaces the current value of an existing
  1552. * blob.
  1553. */
  1554. void wpa_config_set_blob(struct wpa_config *config,
  1555. struct wpa_config_blob *blob)
  1556. {
  1557. wpa_config_remove_blob(config, blob->name);
  1558. blob->next = config->blobs;
  1559. config->blobs = blob;
  1560. }
  1561. /**
  1562. * wpa_config_free_blob - Free blob data
  1563. * @blob: Pointer to blob to be freed
  1564. */
  1565. void wpa_config_free_blob(struct wpa_config_blob *blob)
  1566. {
  1567. if (blob) {
  1568. os_free(blob->name);
  1569. os_free(blob->data);
  1570. os_free(blob);
  1571. }
  1572. }
  1573. /**
  1574. * wpa_config_remove_blob - Remove a named configuration blob
  1575. * @config: Configuration data from wpa_config_read()
  1576. * @name: Name of the blob to remove
  1577. * Returns: 0 if blob was removed or -1 if blob was not found
  1578. */
  1579. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  1580. {
  1581. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  1582. while (pos) {
  1583. if (os_strcmp(pos->name, name) == 0) {
  1584. if (prev)
  1585. prev->next = pos->next;
  1586. else
  1587. config->blobs = pos->next;
  1588. wpa_config_free_blob(pos);
  1589. return 0;
  1590. }
  1591. prev = pos;
  1592. pos = pos->next;
  1593. }
  1594. return -1;
  1595. }
  1596. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1597. /**
  1598. * wpa_config_alloc_empty - Allocate an empty configuration
  1599. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  1600. * socket
  1601. * @driver_param: Driver parameters
  1602. * Returns: Pointer to allocated configuration data or %NULL on failure
  1603. */
  1604. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  1605. const char *driver_param)
  1606. {
  1607. struct wpa_config *config;
  1608. config = os_zalloc(sizeof(*config));
  1609. if (config == NULL)
  1610. return NULL;
  1611. config->eapol_version = DEFAULT_EAPOL_VERSION;
  1612. config->ap_scan = DEFAULT_AP_SCAN;
  1613. config->fast_reauth = DEFAULT_FAST_REAUTH;
  1614. if (ctrl_interface)
  1615. config->ctrl_interface = os_strdup(ctrl_interface);
  1616. if (driver_param)
  1617. config->driver_param = os_strdup(driver_param);
  1618. return config;
  1619. }
  1620. #ifndef CONFIG_NO_STDOUT_DEBUG
  1621. /**
  1622. * wpa_config_debug_dump_networks - Debug dump of configured networks
  1623. * @config: Configuration data from wpa_config_read()
  1624. */
  1625. void wpa_config_debug_dump_networks(struct wpa_config *config)
  1626. {
  1627. int prio;
  1628. struct wpa_ssid *ssid;
  1629. for (prio = 0; prio < config->num_prio; prio++) {
  1630. ssid = config->pssid[prio];
  1631. wpa_printf(MSG_DEBUG, "Priority group %d",
  1632. ssid->priority);
  1633. while (ssid) {
  1634. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  1635. ssid->id,
  1636. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1637. ssid = ssid->pnext;
  1638. }
  1639. }
  1640. }
  1641. #endif /* CONFIG_NO_STDOUT_DEBUG */