config.c 60 KB

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