config.c 51 KB

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