config.c 72 KB

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