config.c 71 KB

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