config.c 73 KB

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