LCOV - code coverage report
Current view: top level - wpa_supplicant - config_file.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 499 737 67.7 %
Date: 2014-05-28 Functions: 28 29 96.6 %

          Line data    Source code
       1             : /*
       2             :  * WPA Supplicant / Configuration backend: text file
       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             :  * This file implements a configuration backend for text files. All the
       9             :  * configuration information is stored in a text file that uses a format
      10             :  * described in the sample configuration file, wpa_supplicant.conf.
      11             :  */
      12             : 
      13             : #include "includes.h"
      14             : 
      15             : #include "common.h"
      16             : #include "config.h"
      17             : #include "base64.h"
      18             : #include "uuid.h"
      19             : #include "p2p/p2p.h"
      20             : #include "eap_peer/eap_methods.h"
      21             : #include "eap_peer/eap.h"
      22             : 
      23             : 
      24          69 : static int newline_terminated(const char *buf, size_t buflen)
      25             : {
      26          69 :         size_t len = os_strlen(buf);
      27          69 :         if (len == 0)
      28           0 :                 return 0;
      29          85 :         if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
      30          16 :             buf[len - 1] != '\n')
      31          16 :                 return 0;
      32          53 :         return 1;
      33             : }
      34             : 
      35             : 
      36           1 : static void skip_line_end(FILE *stream)
      37             : {
      38             :         char buf[100];
      39          17 :         while (fgets(buf, sizeof(buf), stream)) {
      40          16 :                 buf[sizeof(buf) - 1] = '\0';
      41          16 :                 if (newline_terminated(buf, sizeof(buf)))
      42           2 :                         return;
      43             :         }
      44             : }
      45             : 
      46             : 
      47             : /**
      48             :  * wpa_config_get_line - Read the next configuration file line
      49             :  * @s: Buffer for the line
      50             :  * @size: The buffer length
      51             :  * @stream: File stream to read from
      52             :  * @line: Pointer to a variable storing the file line number
      53             :  * @_pos: Buffer for the pointer to the beginning of data on the text line or
      54             :  * %NULL if not needed (returned value used instead)
      55             :  * Returns: Pointer to the beginning of data on the text line or %NULL if no
      56             :  * more text lines are available.
      57             :  *
      58             :  * This function reads the next non-empty line from the configuration file and
      59             :  * removes comments. The returned string is guaranteed to be null-terminated.
      60             :  */
      61          53 : static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
      62             :                                   char **_pos)
      63             : {
      64             :         char *pos, *end, *sstart;
      65             : 
      66         111 :         while (fgets(s, size, stream)) {
      67          53 :                 (*line)++;
      68          53 :                 s[size - 1] = '\0';
      69          53 :                 if (!newline_terminated(s, size)) {
      70             :                         /*
      71             :                          * The line was truncated - skip rest of it to avoid
      72             :                          * confusing error messages.
      73             :                          */
      74           1 :                         wpa_printf(MSG_INFO, "Long line in configuration file "
      75             :                                    "truncated");
      76           1 :                         skip_line_end(stream);
      77             :                 }
      78          53 :                 pos = s;
      79             : 
      80             :                 /* Skip white space from the beginning of line. */
      81         134 :                 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
      82          28 :                         pos++;
      83             : 
      84             :                 /* Skip comment lines and empty lines */
      85          53 :                 if (*pos == '#' || *pos == '\n' || *pos == '\0')
      86           5 :                         continue;
      87             : 
      88             :                 /*
      89             :                  * Remove # comments unless they are within a double quoted
      90             :                  * string.
      91             :                  */
      92          48 :                 sstart = os_strchr(pos, '"');
      93          48 :                 if (sstart)
      94          10 :                         sstart = os_strrchr(sstart + 1, '"');
      95          48 :                 if (!sstart)
      96          38 :                         sstart = pos;
      97          48 :                 end = os_strchr(sstart, '#');
      98          48 :                 if (end)
      99           1 :                         *end-- = '\0';
     100             :                 else
     101          47 :                         end = pos + os_strlen(pos) - 1;
     102             : 
     103             :                 /* Remove trailing white space. */
     104        1225 :                 while (end > pos &&
     105        1174 :                        (*end == '\n' || *end == ' ' || *end == '\t' ||
     106          46 :                         *end == '\r'))
     107         542 :                         *end-- = '\0';
     108             : 
     109          48 :                 if (*pos == '\0')
     110           0 :                         continue;
     111             : 
     112          48 :                 if (_pos)
     113          48 :                         *_pos = pos;
     114          48 :                 return pos;
     115             :         }
     116             : 
     117           5 :         if (_pos)
     118           5 :                 *_pos = NULL;
     119           5 :         return NULL;
     120             : }
     121             : 
     122             : 
     123           1 : static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
     124             : {
     125           1 :         int errors = 0;
     126             : 
     127           1 :         if (ssid->passphrase) {
     128           1 :                 if (ssid->psk_set) {
     129           0 :                         wpa_printf(MSG_ERROR, "Line %d: both PSK and "
     130             :                                    "passphrase configured.", line);
     131           0 :                         errors++;
     132             :                 }
     133           1 :                 wpa_config_update_psk(ssid);
     134             :         }
     135             : 
     136           2 :         if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
     137           1 :             !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
     138           0 :             !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
     139             :                 /* Group cipher cannot be stronger than the pairwise cipher. */
     140           0 :                 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
     141             :                            " list since it was not allowed for pairwise "
     142             :                            "cipher", line);
     143           0 :                 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
     144             :         }
     145             : 
     146           1 :         return errors;
     147             : }
     148             : 
     149             : 
     150           1 : static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
     151             : {
     152             :         struct wpa_ssid *ssid;
     153           1 :         int errors = 0, end = 0;
     154             :         char buf[2000], *pos, *pos2;
     155             : 
     156           1 :         wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
     157             :                    *line);
     158           1 :         ssid = os_zalloc(sizeof(*ssid));
     159           1 :         if (ssid == NULL)
     160           0 :                 return NULL;
     161           1 :         dl_list_init(&ssid->psk_list);
     162           1 :         ssid->id = id;
     163             : 
     164           1 :         wpa_config_set_network_defaults(ssid);
     165             : 
     166          11 :         while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
     167          10 :                 if (os_strcmp(pos, "}") == 0) {
     168           1 :                         end = 1;
     169           1 :                         break;
     170             :                 }
     171             : 
     172           9 :                 pos2 = os_strchr(pos, '=');
     173           9 :                 if (pos2 == NULL) {
     174           0 :                         wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
     175             :                                    "'%s'.", *line, pos);
     176           0 :                         errors++;
     177           0 :                         continue;
     178             :                 }
     179             : 
     180           9 :                 *pos2++ = '\0';
     181           9 :                 if (*pos2 == '"') {
     182           2 :                         if (os_strchr(pos2 + 1, '"') == NULL) {
     183           0 :                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
     184             :                                            "quotation '%s'.", *line, pos2);
     185           0 :                                 errors++;
     186           0 :                                 continue;
     187             :                         }
     188             :                 }
     189             : 
     190           9 :                 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
     191           0 :                         errors++;
     192             :         }
     193             : 
     194           1 :         if (!end) {
     195           0 :                 wpa_printf(MSG_ERROR, "Line %d: network block was not "
     196             :                            "terminated properly.", *line);
     197           0 :                 errors++;
     198             :         }
     199             : 
     200           1 :         errors += wpa_config_validate_network(ssid, *line);
     201             : 
     202           1 :         if (errors) {
     203           0 :                 wpa_config_free_ssid(ssid);
     204           0 :                 ssid = NULL;
     205             :         }
     206             : 
     207           1 :         return ssid;
     208             : }
     209             : 
     210             : 
     211           1 : static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
     212             : {
     213             :         struct wpa_cred *cred;
     214           1 :         int errors = 0, end = 0;
     215             :         char buf[256], *pos, *pos2;
     216             : 
     217           1 :         wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
     218           1 :         cred = os_zalloc(sizeof(*cred));
     219           1 :         if (cred == NULL)
     220           0 :                 return NULL;
     221           1 :         cred->id = id;
     222           1 :         cred->sim_num = DEFAULT_USER_SELECTED_SIM;
     223             : 
     224          18 :         while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
     225          17 :                 if (os_strcmp(pos, "}") == 0) {
     226           1 :                         end = 1;
     227           1 :                         break;
     228             :                 }
     229             : 
     230          16 :                 pos2 = os_strchr(pos, '=');
     231          16 :                 if (pos2 == NULL) {
     232           0 :                         wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
     233             :                                    "'%s'.", *line, pos);
     234           0 :                         errors++;
     235           0 :                         continue;
     236             :                 }
     237             : 
     238          16 :                 *pos2++ = '\0';
     239          16 :                 if (*pos2 == '"') {
     240           8 :                         if (os_strchr(pos2 + 1, '"') == NULL) {
     241           0 :                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
     242             :                                            "quotation '%s'.", *line, pos2);
     243           0 :                                 errors++;
     244           0 :                                 continue;
     245             :                         }
     246             :                 }
     247             : 
     248          16 :                 if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
     249           0 :                         errors++;
     250             :         }
     251             : 
     252           1 :         if (!end) {
     253           0 :                 wpa_printf(MSG_ERROR, "Line %d: cred block was not "
     254             :                            "terminated properly.", *line);
     255           0 :                 errors++;
     256             :         }
     257             : 
     258           1 :         if (errors) {
     259           0 :                 wpa_config_free_cred(cred);
     260           0 :                 cred = NULL;
     261             :         }
     262             : 
     263           1 :         return cred;
     264             : }
     265             : 
     266             : 
     267             : #ifndef CONFIG_NO_CONFIG_BLOBS
     268           1 : static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
     269             :                                                      const char *name)
     270             : {
     271             :         struct wpa_config_blob *blob;
     272             :         char buf[256], *pos;
     273           1 :         unsigned char *encoded = NULL, *nencoded;
     274           1 :         int end = 0;
     275           1 :         size_t encoded_len = 0, len;
     276             : 
     277           1 :         wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
     278             :                    *line, name);
     279             : 
     280           3 :         while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
     281           2 :                 if (os_strcmp(pos, "}") == 0) {
     282           1 :                         end = 1;
     283           1 :                         break;
     284             :                 }
     285             : 
     286           1 :                 len = os_strlen(pos);
     287           1 :                 nencoded = os_realloc(encoded, encoded_len + len);
     288           1 :                 if (nencoded == NULL) {
     289           0 :                         wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
     290             :                                    "blob", *line);
     291           0 :                         os_free(encoded);
     292           0 :                         return NULL;
     293             :                 }
     294           1 :                 encoded = nencoded;
     295           1 :                 os_memcpy(encoded + encoded_len, pos, len);
     296           1 :                 encoded_len += len;
     297             :         }
     298             : 
     299           1 :         if (!end) {
     300           0 :                 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
     301             :                            "properly", *line);
     302           0 :                 os_free(encoded);
     303           0 :                 return NULL;
     304             :         }
     305             : 
     306           1 :         blob = os_zalloc(sizeof(*blob));
     307           1 :         if (blob == NULL) {
     308           0 :                 os_free(encoded);
     309           0 :                 return NULL;
     310             :         }
     311           1 :         blob->name = os_strdup(name);
     312           1 :         blob->data = base64_decode(encoded, encoded_len, &blob->len);
     313           1 :         os_free(encoded);
     314             : 
     315           1 :         if (blob->name == NULL || blob->data == NULL) {
     316           0 :                 wpa_config_free_blob(blob);
     317           0 :                 return NULL;
     318             :         }
     319             : 
     320           1 :         return blob;
     321             : }
     322             : 
     323             : 
     324           1 : static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
     325             :                                    int *line, char *bname)
     326             : {
     327             :         char *name_end;
     328             :         struct wpa_config_blob *blob;
     329             : 
     330           1 :         name_end = os_strchr(bname, '=');
     331           1 :         if (name_end == NULL) {
     332           0 :                 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
     333             :                            *line);
     334           0 :                 return -1;
     335             :         }
     336           1 :         *name_end = '\0';
     337             : 
     338           1 :         blob = wpa_config_read_blob(f, line, bname);
     339           1 :         if (blob == NULL) {
     340           0 :                 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
     341             :                            *line, bname);
     342           0 :                 return -1;
     343             :         }
     344           1 :         wpa_config_set_blob(config, blob);
     345           1 :         return 0;
     346             : }
     347             : #endif /* CONFIG_NO_CONFIG_BLOBS */
     348             : 
     349             : 
     350          22 : struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
     351             : {
     352             :         FILE *f;
     353             :         char buf[512], *pos;
     354          22 :         int errors = 0, line = 0;
     355             :         struct wpa_ssid *ssid, *tail, *head;
     356             :         struct wpa_cred *cred, *cred_tail, *cred_head;
     357             :         struct wpa_config *config;
     358          22 :         int id = 0;
     359          22 :         int cred_id = 0;
     360             : 
     361          22 :         if (name == NULL)
     362          10 :                 return NULL;
     363          12 :         if (cfgp)
     364           0 :                 config = cfgp;
     365             :         else
     366          12 :                 config = wpa_config_alloc_empty(NULL, NULL);
     367          12 :         if (config == NULL) {
     368           0 :                 wpa_printf(MSG_ERROR, "Failed to allocate config file "
     369             :                            "structure");
     370           0 :                 return NULL;
     371             :         }
     372          12 :         tail = head = config->ssid;
     373          24 :         while (tail && tail->next)
     374           0 :                 tail = tail->next;
     375          12 :         cred_tail = cred_head = config->cred;
     376          24 :         while (cred_tail && cred_tail->next)
     377           0 :                 cred_tail = cred_tail->next;
     378             : 
     379          12 :         wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
     380          12 :         f = fopen(name, "r");
     381          12 :         if (f == NULL) {
     382           7 :                 wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
     383           7 :                            "error: %s", name, strerror(errno));
     384           7 :                 os_free(config);
     385           7 :                 return NULL;
     386             :         }
     387             : 
     388          29 :         while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
     389          19 :                 if (os_strcmp(pos, "network={") == 0) {
     390           1 :                         ssid = wpa_config_read_network(f, &line, id++);
     391           1 :                         if (ssid == NULL) {
     392           0 :                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
     393             :                                            "parse network block.", line);
     394           0 :                                 errors++;
     395           0 :                                 continue;
     396             :                         }
     397           1 :                         if (head == NULL) {
     398           1 :                                 head = tail = ssid;
     399             :                         } else {
     400           0 :                                 tail->next = ssid;
     401           0 :                                 tail = ssid;
     402             :                         }
     403           1 :                         if (wpa_config_add_prio_network(config, ssid)) {
     404           0 :                                 wpa_printf(MSG_ERROR, "Line %d: failed to add "
     405             :                                            "network block to priority list.",
     406             :                                            line);
     407           0 :                                 errors++;
     408           0 :                                 continue;
     409             :                         }
     410          18 :                 } else if (os_strcmp(pos, "cred={") == 0) {
     411           1 :                         cred = wpa_config_read_cred(f, &line, cred_id++);
     412           1 :                         if (cred == NULL) {
     413           0 :                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
     414             :                                            "parse cred block.", line);
     415           0 :                                 errors++;
     416           0 :                                 continue;
     417             :                         }
     418           1 :                         if (cred_head == NULL) {
     419           1 :                                 cred_head = cred_tail = cred;
     420             :                         } else {
     421           0 :                                 cred_tail->next = cred;
     422           0 :                                 cred_tail = cred;
     423             :                         }
     424             : #ifndef CONFIG_NO_CONFIG_BLOBS
     425          17 :                 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
     426           1 :                         if (wpa_config_process_blob(config, f, &line, pos + 12)
     427             :                             < 0) {
     428           0 :                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
     429             :                                            "process blob.", line);
     430           0 :                                 errors++;
     431           0 :                                 continue;
     432             :                         }
     433             : #endif /* CONFIG_NO_CONFIG_BLOBS */
     434          16 :                 } else if (wpa_config_process_global(config, pos, line) < 0) {
     435           0 :                         wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
     436             :                                    "line '%s'.", line, pos);
     437           0 :                         errors++;
     438           0 :                         continue;
     439             :                 }
     440             :         }
     441             : 
     442           5 :         fclose(f);
     443             : 
     444           5 :         config->ssid = head;
     445           5 :         wpa_config_debug_dump_networks(config);
     446           5 :         config->cred = cred_head;
     447             : 
     448             : #ifndef WPA_IGNORE_CONFIG_ERRORS
     449           5 :         if (errors) {
     450           0 :                 wpa_config_free(config);
     451           0 :                 config = NULL;
     452           0 :                 head = NULL;
     453             :         }
     454             : #endif /* WPA_IGNORE_CONFIG_ERRORS */
     455             : 
     456           5 :         return config;
     457             : }
     458             : 
     459             : 
     460             : #ifndef CONFIG_NO_CONFIG_WRITE
     461             : 
     462         120 : static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
     463             : {
     464         120 :         char *value = wpa_config_get(ssid, field);
     465         120 :         if (value == NULL)
     466         237 :                 return;
     467           3 :         fprintf(f, "\t%s=%s\n", field, value);
     468           3 :         os_free(value);
     469             : }
     470             : 
     471             : 
     472          57 : static void write_int(FILE *f, const char *field, int value, int def)
     473             : {
     474          57 :         if (value == def)
     475         111 :                 return;
     476           3 :         fprintf(f, "\t%s=%d\n", field, value);
     477             : }
     478             : 
     479             : 
     480           3 : static void write_bssid(FILE *f, struct wpa_ssid *ssid)
     481             : {
     482           3 :         char *value = wpa_config_get(ssid, "bssid");
     483           3 :         if (value == NULL)
     484           3 :                 return;
     485           3 :         fprintf(f, "\tbssid=%s\n", value);
     486           3 :         os_free(value);
     487             : }
     488             : 
     489             : 
     490           3 : static void write_psk(FILE *f, struct wpa_ssid *ssid)
     491             : {
     492           3 :         char *value = wpa_config_get(ssid, "psk");
     493           3 :         if (value == NULL)
     494           3 :                 return;
     495           3 :         fprintf(f, "\tpsk=%s\n", value);
     496           3 :         os_free(value);
     497             : }
     498             : 
     499             : 
     500           3 : static void write_proto(FILE *f, struct wpa_ssid *ssid)
     501             : {
     502             :         char *value;
     503             : 
     504           3 :         if (ssid->proto == DEFAULT_PROTO)
     505           0 :                 return;
     506             : 
     507           3 :         value = wpa_config_get(ssid, "proto");
     508           3 :         if (value == NULL)
     509           0 :                 return;
     510           3 :         if (value[0])
     511           3 :                 fprintf(f, "\tproto=%s\n", value);
     512           3 :         os_free(value);
     513             : }
     514             : 
     515             : 
     516           3 : static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
     517             : {
     518             :         char *value;
     519             : 
     520           3 :         if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
     521           0 :                 return;
     522             : 
     523           3 :         value = wpa_config_get(ssid, "key_mgmt");
     524           3 :         if (value == NULL)
     525           0 :                 return;
     526           3 :         if (value[0])
     527           3 :                 fprintf(f, "\tkey_mgmt=%s\n", value);
     528           3 :         os_free(value);
     529             : }
     530             : 
     531             : 
     532           3 : static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
     533             : {
     534             :         char *value;
     535             : 
     536           3 :         if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
     537           0 :                 return;
     538             : 
     539           3 :         value = wpa_config_get(ssid, "pairwise");
     540           3 :         if (value == NULL)
     541           0 :                 return;
     542           3 :         if (value[0])
     543           3 :                 fprintf(f, "\tpairwise=%s\n", value);
     544           3 :         os_free(value);
     545             : }
     546             : 
     547             : 
     548           3 : static void write_group(FILE *f, struct wpa_ssid *ssid)
     549             : {
     550             :         char *value;
     551             : 
     552           3 :         if (ssid->group_cipher == DEFAULT_GROUP)
     553           0 :                 return;
     554             : 
     555           3 :         value = wpa_config_get(ssid, "group");
     556           3 :         if (value == NULL)
     557           0 :                 return;
     558           3 :         if (value[0])
     559           3 :                 fprintf(f, "\tgroup=%s\n", value);
     560           3 :         os_free(value);
     561             : }
     562             : 
     563             : 
     564           3 : static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
     565             : {
     566             :         char *value;
     567             : 
     568           3 :         if (ssid->auth_alg == 0)
     569           0 :                 return;
     570             : 
     571           3 :         value = wpa_config_get(ssid, "auth_alg");
     572           3 :         if (value == NULL)
     573           0 :                 return;
     574           3 :         if (value[0])
     575           3 :                 fprintf(f, "\tauth_alg=%s\n", value);
     576           3 :         os_free(value);
     577             : }
     578             : 
     579             : 
     580             : #ifdef IEEE8021X_EAPOL
     581           3 : static void write_eap(FILE *f, struct wpa_ssid *ssid)
     582             : {
     583             :         char *value;
     584             : 
     585           3 :         value = wpa_config_get(ssid, "eap");
     586           3 :         if (value == NULL)
     587           6 :                 return;
     588             : 
     589           0 :         if (value[0])
     590           0 :                 fprintf(f, "\teap=%s\n", value);
     591           0 :         os_free(value);
     592             : }
     593             : #endif /* IEEE8021X_EAPOL */
     594             : 
     595             : 
     596          12 : static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
     597             : {
     598             :         char field[20], *value;
     599             :         int res;
     600             : 
     601          12 :         res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
     602          12 :         if (res < 0 || (size_t) res >= sizeof(field))
     603          12 :                 return;
     604          12 :         value = wpa_config_get(ssid, field);
     605          12 :         if (value) {
     606           0 :                 fprintf(f, "\t%s=%s\n", field, value);
     607           0 :                 os_free(value);
     608             :         }
     609             : }
     610             : 
     611             : 
     612             : #ifdef CONFIG_P2P
     613             : 
     614           3 : static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
     615             : {
     616           3 :         char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
     617           3 :         if (value == NULL)
     618           6 :                 return;
     619           0 :         fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
     620           0 :         os_free(value);
     621             : }
     622             : 
     623           3 : static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
     624             : {
     625           3 :         char *value = wpa_config_get(ssid, "p2p_client_list");
     626           3 :         if (value == NULL)
     627           6 :                 return;
     628           0 :         fprintf(f, "\tp2p_client_list=%s\n", value);
     629           0 :         os_free(value);
     630             : }
     631             : 
     632             : 
     633           3 : static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
     634             : {
     635             :         struct psk_list_entry *psk;
     636             :         char hex[32 * 2 + 1];
     637             : 
     638           3 :         dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
     639           0 :                 wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
     640           0 :                 fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
     641           0 :                         psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
     642             :         }
     643           3 : }
     644             : 
     645             : #endif /* CONFIG_P2P */
     646             : 
     647             : 
     648           3 : static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
     649             : {
     650             :         int i;
     651             : 
     652             : #define STR(t) write_str(f, #t, ssid)
     653             : #define INT(t) write_int(f, #t, ssid->t, 0)
     654             : #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
     655             : #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
     656             : #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
     657             : 
     658           3 :         STR(ssid);
     659           3 :         INT(scan_ssid);
     660           3 :         write_bssid(f, ssid);
     661           3 :         write_psk(f, ssid);
     662           3 :         write_proto(f, ssid);
     663           3 :         write_key_mgmt(f, ssid);
     664           3 :         INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
     665           3 :         write_pairwise(f, ssid);
     666           3 :         write_group(f, ssid);
     667           3 :         write_auth_alg(f, ssid);
     668           3 :         STR(bgscan);
     669           3 :         STR(autoscan);
     670           3 :         STR(scan_freq);
     671             : #ifdef IEEE8021X_EAPOL
     672           3 :         write_eap(f, ssid);
     673           3 :         STR(identity);
     674           3 :         STR(anonymous_identity);
     675           3 :         STR(password);
     676           3 :         STR(ca_cert);
     677           3 :         STR(ca_path);
     678           3 :         STR(client_cert);
     679           3 :         STR(private_key);
     680           3 :         STR(private_key_passwd);
     681           3 :         STR(dh_file);
     682           3 :         STR(subject_match);
     683           3 :         STR(altsubject_match);
     684           3 :         STR(domain_suffix_match);
     685           3 :         STR(ca_cert2);
     686           3 :         STR(ca_path2);
     687           3 :         STR(client_cert2);
     688           3 :         STR(private_key2);
     689           3 :         STR(private_key2_passwd);
     690           3 :         STR(dh_file2);
     691           3 :         STR(subject_match2);
     692           3 :         STR(altsubject_match2);
     693           3 :         STR(domain_suffix_match2);
     694           3 :         STR(phase1);
     695           3 :         STR(phase2);
     696           3 :         STR(pcsc);
     697           3 :         STR(pin);
     698           3 :         STR(engine_id);
     699           3 :         STR(key_id);
     700           3 :         STR(cert_id);
     701           3 :         STR(ca_cert_id);
     702           3 :         STR(key2_id);
     703           3 :         STR(pin2);
     704           3 :         STR(engine2_id);
     705           3 :         STR(cert2_id);
     706           3 :         STR(ca_cert2_id);
     707           3 :         INTe(engine);
     708           3 :         INTe(engine2);
     709           3 :         INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
     710             : #endif /* IEEE8021X_EAPOL */
     711          15 :         for (i = 0; i < 4; i++)
     712          12 :                 write_wep_key(f, i, ssid);
     713           3 :         INT(wep_tx_keyidx);
     714           3 :         INT(priority);
     715             : #ifdef IEEE8021X_EAPOL
     716           3 :         INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
     717           3 :         STR(pac_file);
     718           3 :         INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
     719           3 :         INTe(ocsp);
     720           3 :         INT_DEFe(sim_num, DEFAULT_USER_SELECTED_SIM);
     721             : #endif /* IEEE8021X_EAPOL */
     722           3 :         INT(mode);
     723           3 :         INT(frequency);
     724           3 :         write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
     725           3 :         INT(disabled);
     726           3 :         INT(peerkey);
     727             : #ifdef CONFIG_IEEE80211W
     728           3 :         write_int(f, "ieee80211w", ssid->ieee80211w,
     729             :                   MGMT_FRAME_PROTECTION_DEFAULT);
     730             : #endif /* CONFIG_IEEE80211W */
     731           3 :         STR(id_str);
     732             : #ifdef CONFIG_P2P
     733           3 :         write_go_p2p_dev_addr(f, ssid);
     734           3 :         write_p2p_client_list(f, ssid);
     735           3 :         write_psk_list(f, ssid);
     736             : #endif /* CONFIG_P2P */
     737           3 :         INT(dtim_period);
     738           3 :         INT(beacon_int);
     739             : #ifdef CONFIG_MACSEC
     740             :         INT(macsec_policy);
     741             : #endif /* CONFIG_MACSEC */
     742             : 
     743             : #undef STR
     744             : #undef INT
     745             : #undef INT_DEF
     746           3 : }
     747             : 
     748             : 
     749           3 : static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
     750             : {
     751             :         size_t i;
     752             : 
     753           3 :         if (cred->priority)
     754           3 :                 fprintf(f, "\tpriority=%d\n", cred->priority);
     755           3 :         if (cred->pcsc)
     756           0 :                 fprintf(f, "\tpcsc=%d\n", cred->pcsc);
     757           3 :         if (cred->realm)
     758           3 :                 fprintf(f, "\trealm=\"%s\"\n", cred->realm);
     759           3 :         if (cred->username)
     760           3 :                 fprintf(f, "\tusername=\"%s\"\n", cred->username);
     761           3 :         if (cred->password && cred->ext_password)
     762           0 :                 fprintf(f, "\tpassword=ext:%s\n", cred->password);
     763           3 :         else if (cred->password)
     764           3 :                 fprintf(f, "\tpassword=\"%s\"\n", cred->password);
     765           3 :         if (cred->ca_cert)
     766           3 :                 fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
     767           3 :         if (cred->client_cert)
     768           0 :                 fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
     769           3 :         if (cred->private_key)
     770           0 :                 fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
     771           3 :         if (cred->private_key_passwd)
     772           0 :                 fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
     773             :                         cred->private_key_passwd);
     774           3 :         if (cred->imsi)
     775           0 :                 fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
     776           3 :         if (cred->milenage)
     777           0 :                 fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
     778           6 :         for (i = 0; i < cred->num_domain; i++)
     779           3 :                 fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
     780           3 :         if (cred->domain_suffix_match)
     781           3 :                 fprintf(f, "\tdomain_suffix_match=\"%s\"\n",
     782             :                         cred->domain_suffix_match);
     783           3 :         if (cred->roaming_consortium_len) {
     784           3 :                 fprintf(f, "\troaming_consortium=");
     785          12 :                 for (i = 0; i < cred->roaming_consortium_len; i++)
     786           9 :                         fprintf(f, "%02x", cred->roaming_consortium[i]);
     787           3 :                 fprintf(f, "\n");
     788             :         }
     789           3 :         if (cred->eap_method) {
     790             :                 const char *name;
     791           3 :                 name = eap_get_name(cred->eap_method[0].vendor,
     792           3 :                                     cred->eap_method[0].method);
     793           3 :                 fprintf(f, "\teap=%s\n", name);
     794             :         }
     795           3 :         if (cred->phase1)
     796           0 :                 fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
     797           3 :         if (cred->phase2)
     798           0 :                 fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
     799           3 :         if (cred->excluded_ssid) {
     800             :                 size_t j;
     801           0 :                 for (i = 0; i < cred->num_excluded_ssid; i++) {
     802           0 :                         struct excluded_ssid *e = &cred->excluded_ssid[i];
     803           0 :                         fprintf(f, "\texcluded_ssid=");
     804           0 :                         for (j = 0; j < e->ssid_len; j++)
     805           0 :                                 fprintf(f, "%02x", e->ssid[j]);
     806           0 :                         fprintf(f, "\n");
     807             :                 }
     808             :         }
     809           3 :         if (cred->roaming_partner) {
     810           6 :                 for (i = 0; i < cred->num_roaming_partner; i++) {
     811           3 :                         struct roaming_partner *p = &cred->roaming_partner[i];
     812           6 :                         fprintf(f, "\troaming_partner=\"%s,%d,%u,%s\"\n",
     813           6 :                                 p->fqdn, p->exact_match, p->priority,
     814           3 :                                 p->country);
     815             :                 }
     816             :         }
     817           3 :         if (cred->update_identifier)
     818           3 :                 fprintf(f, "\tupdate_identifier=%d\n", cred->update_identifier);
     819             : 
     820           3 :         if (cred->provisioning_sp)
     821           3 :                 fprintf(f, "\tprovisioning_sp=\"%s\"\n", cred->provisioning_sp);
     822           3 :         if (cred->sp_priority)
     823           3 :                 fprintf(f, "\tsp_priority=%d\n", cred->sp_priority);
     824             : 
     825           3 :         if (cred->min_dl_bandwidth_home)
     826           0 :                 fprintf(f, "\tmin_dl_bandwidth_home=%u\n",
     827             :                         cred->min_dl_bandwidth_home);
     828           3 :         if (cred->min_ul_bandwidth_home)
     829           0 :                 fprintf(f, "\tmin_ul_bandwidth_home=%u\n",
     830             :                         cred->min_ul_bandwidth_home);
     831           3 :         if (cred->min_dl_bandwidth_roaming)
     832           0 :                 fprintf(f, "\tmin_dl_bandwidth_roaming=%u\n",
     833             :                         cred->min_dl_bandwidth_roaming);
     834           3 :         if (cred->min_ul_bandwidth_roaming)
     835           0 :                 fprintf(f, "\tmin_ul_bandwidth_roaming=%u\n",
     836             :                         cred->min_ul_bandwidth_roaming);
     837             : 
     838           3 :         if (cred->max_bss_load)
     839           0 :                 fprintf(f, "\tmax_bss_load=%u\n",
     840             :                         cred->max_bss_load);
     841             : 
     842           3 :         if (cred->ocsp)
     843           3 :                 fprintf(f, "\tocsp=%d\n", cred->ocsp);
     844             : 
     845           3 :         if (cred->num_req_conn_capab) {
     846           6 :                 for (i = 0; i < cred->num_req_conn_capab; i++) {
     847             :                         int *ports;
     848             : 
     849           3 :                         fprintf(f, "\treq_conn_capab=%u",
     850           3 :                                 cred->req_conn_capab_proto[i]);
     851           3 :                         ports = cred->req_conn_capab_port[i];
     852           3 :                         if (ports) {
     853             :                                 int j;
     854           6 :                                 for (j = 0; ports[j] != -1; j++) {
     855           3 :                                         fprintf(f, "%s%d", j > 0 ? "," : ":",
     856           3 :                                                 ports[j]);
     857             :                                 }
     858             :                         }
     859           3 :                         fprintf(f, "\n");
     860             :                 }
     861             :         }
     862             : 
     863           3 :         if (cred->required_roaming_consortium_len) {
     864           3 :                 fprintf(f, "\trequired_roaming_consortium=");
     865          12 :                 for (i = 0; i < cred->required_roaming_consortium_len; i++)
     866           9 :                         fprintf(f, "%02x",
     867           9 :                                 cred->required_roaming_consortium[i]);
     868           3 :                 fprintf(f, "\n");
     869             :         }
     870             : 
     871           3 :         if (cred->sim_num != DEFAULT_USER_SELECTED_SIM)
     872           0 :                 fprintf(f, "\tsim_num=%d\n", cred->sim_num);
     873           3 : }
     874             : 
     875             : 
     876             : #ifndef CONFIG_NO_CONFIG_BLOBS
     877           3 : static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
     878             : {
     879             :         unsigned char *encoded;
     880             : 
     881           3 :         encoded = base64_encode(blob->data, blob->len, NULL);
     882           3 :         if (encoded == NULL)
     883           0 :                 return -1;
     884             : 
     885           3 :         fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
     886           3 :         os_free(encoded);
     887           3 :         return 0;
     888             : }
     889             : #endif /* CONFIG_NO_CONFIG_BLOBS */
     890             : 
     891             : 
     892           0 : static void write_global_bin(FILE *f, const char *field,
     893             :                              const struct wpabuf *val)
     894             : {
     895             :         size_t i;
     896             :         const u8 *pos;
     897             : 
     898           0 :         if (val == NULL)
     899           0 :                 return;
     900             : 
     901           0 :         fprintf(f, "%s=", field);
     902           0 :         pos = wpabuf_head(val);
     903           0 :         for (i = 0; i < wpabuf_len(val); i++)
     904           0 :                 fprintf(f, "%02X", *pos++);
     905           0 :         fprintf(f, "\n");
     906             : }
     907             : 
     908             : 
     909           3 : static void wpa_config_write_global(FILE *f, struct wpa_config *config)
     910             : {
     911             : #ifdef CONFIG_CTRL_IFACE
     912           3 :         if (config->ctrl_interface)
     913           3 :                 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
     914           3 :         if (config->ctrl_interface_group)
     915           0 :                 fprintf(f, "ctrl_interface_group=%s\n",
     916             :                         config->ctrl_interface_group);
     917             : #endif /* CONFIG_CTRL_IFACE */
     918           3 :         if (config->eapol_version != DEFAULT_EAPOL_VERSION)
     919           3 :                 fprintf(f, "eapol_version=%d\n", config->eapol_version);
     920           3 :         if (config->ap_scan != DEFAULT_AP_SCAN)
     921           0 :                 fprintf(f, "ap_scan=%d\n", config->ap_scan);
     922           3 :         if (config->disable_scan_offload)
     923           0 :                 fprintf(f, "disable_scan_offload=%d\n",
     924             :                         config->disable_scan_offload);
     925           3 :         if (config->fast_reauth != DEFAULT_FAST_REAUTH)
     926           0 :                 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
     927           3 :         if (config->opensc_engine_path)
     928           0 :                 fprintf(f, "opensc_engine_path=%s\n",
     929             :                         config->opensc_engine_path);
     930           3 :         if (config->pkcs11_engine_path)
     931           0 :                 fprintf(f, "pkcs11_engine_path=%s\n",
     932             :                         config->pkcs11_engine_path);
     933           3 :         if (config->pkcs11_module_path)
     934           0 :                 fprintf(f, "pkcs11_module_path=%s\n",
     935             :                         config->pkcs11_module_path);
     936           3 :         if (config->pcsc_reader)
     937           0 :                 fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
     938           3 :         if (config->pcsc_pin)
     939           0 :                 fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
     940           3 :         if (config->driver_param)
     941           0 :                 fprintf(f, "driver_param=%s\n", config->driver_param);
     942           3 :         if (config->dot11RSNAConfigPMKLifetime)
     943           0 :                 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
     944             :                         config->dot11RSNAConfigPMKLifetime);
     945           3 :         if (config->dot11RSNAConfigPMKReauthThreshold)
     946           0 :                 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
     947             :                         config->dot11RSNAConfigPMKReauthThreshold);
     948           3 :         if (config->dot11RSNAConfigSATimeout)
     949           0 :                 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
     950             :                         config->dot11RSNAConfigSATimeout);
     951           3 :         if (config->update_config)
     952           3 :                 fprintf(f, "update_config=%d\n", config->update_config);
     953             : #ifdef CONFIG_WPS
     954           3 :         if (!is_nil_uuid(config->uuid)) {
     955             :                 char buf[40];
     956           0 :                 uuid_bin2str(config->uuid, buf, sizeof(buf));
     957           0 :                 fprintf(f, "uuid=%s\n", buf);
     958             :         }
     959           3 :         if (config->device_name)
     960           3 :                 fprintf(f, "device_name=%s\n", config->device_name);
     961           3 :         if (config->manufacturer)
     962           0 :                 fprintf(f, "manufacturer=%s\n", config->manufacturer);
     963           3 :         if (config->model_name)
     964           0 :                 fprintf(f, "model_name=%s\n", config->model_name);
     965           3 :         if (config->model_number)
     966           0 :                 fprintf(f, "model_number=%s\n", config->model_number);
     967           3 :         if (config->serial_number)
     968           0 :                 fprintf(f, "serial_number=%s\n", config->serial_number);
     969             :         {
     970             :                 char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
     971           3 :                 buf = wps_dev_type_bin2str(config->device_type,
     972             :                                            _buf, sizeof(_buf));
     973           3 :                 if (os_strcmp(buf, "0-00000000-0") != 0)
     974           0 :                         fprintf(f, "device_type=%s\n", buf);
     975             :         }
     976           3 :         if (WPA_GET_BE32(config->os_version))
     977           0 :                 fprintf(f, "os_version=%08x\n",
     978           0 :                         WPA_GET_BE32(config->os_version));
     979           3 :         if (config->config_methods)
     980           0 :                 fprintf(f, "config_methods=%s\n", config->config_methods);
     981           3 :         if (config->wps_cred_processing)
     982           0 :                 fprintf(f, "wps_cred_processing=%d\n",
     983             :                         config->wps_cred_processing);
     984           3 :         if (config->wps_vendor_ext_m1) {
     985           0 :                 int i, len = wpabuf_len(config->wps_vendor_ext_m1);
     986           0 :                 const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
     987           0 :                 if (len > 0) {
     988           0 :                         fprintf(f, "wps_vendor_ext_m1=");
     989           0 :                         for (i = 0; i < len; i++)
     990           0 :                                 fprintf(f, "%02x", *p++);
     991           0 :                         fprintf(f, "\n");
     992             :                 }
     993             :         }
     994             : #endif /* CONFIG_WPS */
     995             : #ifdef CONFIG_P2P
     996           3 :         if (config->p2p_listen_reg_class)
     997           0 :                 fprintf(f, "p2p_listen_reg_class=%u\n",
     998             :                         config->p2p_listen_reg_class);
     999           3 :         if (config->p2p_listen_channel)
    1000           0 :                 fprintf(f, "p2p_listen_channel=%u\n",
    1001             :                         config->p2p_listen_channel);
    1002           3 :         if (config->p2p_oper_reg_class)
    1003           0 :                 fprintf(f, "p2p_oper_reg_class=%u\n",
    1004             :                         config->p2p_oper_reg_class);
    1005           3 :         if (config->p2p_oper_channel)
    1006           0 :                 fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
    1007           3 :         if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
    1008           0 :                 fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
    1009           3 :         if (config->p2p_ssid_postfix)
    1010           0 :                 fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
    1011           3 :         if (config->persistent_reconnect)
    1012           0 :                 fprintf(f, "persistent_reconnect=%u\n",
    1013             :                         config->persistent_reconnect);
    1014           3 :         if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
    1015           0 :                 fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
    1016           3 :         if (config->p2p_group_idle)
    1017           0 :                 fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
    1018           3 :         if (config->p2p_pref_chan) {
    1019             :                 unsigned int i;
    1020           0 :                 fprintf(f, "p2p_pref_chan=");
    1021           0 :                 for (i = 0; i < config->num_p2p_pref_chan; i++) {
    1022           0 :                         fprintf(f, "%s%u:%u", i > 0 ? "," : "",
    1023           0 :                                 config->p2p_pref_chan[i].op_class,
    1024           0 :                                 config->p2p_pref_chan[i].chan);
    1025             :                 }
    1026           0 :                 fprintf(f, "\n");
    1027             :         }
    1028           3 :         if (config->p2p_no_go_freq.num) {
    1029           0 :                 char *val = freq_range_list_str(&config->p2p_no_go_freq);
    1030           0 :                 if (val) {
    1031           0 :                         fprintf(f, "p2p_no_go_freq=%s\n", val);
    1032           0 :                         os_free(val);
    1033             :                 }
    1034             :         }
    1035           3 :         if (config->p2p_add_cli_chan)
    1036           0 :                 fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
    1037           3 :         if (config->p2p_go_ht40)
    1038           0 :                 fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
    1039           3 :         if (config->p2p_go_vht)
    1040           0 :                 fprintf(f, "p2p_go_vht=%u\n", config->p2p_go_vht);
    1041           3 :         if (config->p2p_disabled)
    1042           0 :                 fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
    1043           3 :         if (config->p2p_no_group_iface)
    1044           0 :                 fprintf(f, "p2p_no_group_iface=%u\n",
    1045             :                         config->p2p_no_group_iface);
    1046           3 :         if (config->p2p_ignore_shared_freq)
    1047           0 :                 fprintf(f, "p2p_ignore_shared_freq=%u\n",
    1048             :                         config->p2p_ignore_shared_freq);
    1049             : #endif /* CONFIG_P2P */
    1050           3 :         if (config->country[0] && config->country[1]) {
    1051           0 :                 fprintf(f, "country=%c%c\n",
    1052           0 :                         config->country[0], config->country[1]);
    1053             :         }
    1054           3 :         if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
    1055           0 :                 fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
    1056           3 :         if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
    1057           0 :                 fprintf(f, "bss_expiration_age=%u\n",
    1058             :                         config->bss_expiration_age);
    1059           3 :         if (config->bss_expiration_scan_count !=
    1060             :             DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
    1061           0 :                 fprintf(f, "bss_expiration_scan_count=%u\n",
    1062             :                         config->bss_expiration_scan_count);
    1063           3 :         if (config->filter_ssids)
    1064           0 :                 fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
    1065           3 :         if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
    1066           0 :                 fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
    1067           3 :         if (config->disassoc_low_ack)
    1068           0 :                 fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
    1069             : #ifdef CONFIG_HS20
    1070           3 :         if (config->hs20)
    1071           0 :                 fprintf(f, "hs20=1\n");
    1072             : #endif /* CONFIG_HS20 */
    1073             : #ifdef CONFIG_INTERWORKING
    1074           3 :         if (config->interworking)
    1075           0 :                 fprintf(f, "interworking=%u\n", config->interworking);
    1076           3 :         if (!is_zero_ether_addr(config->hessid))
    1077           0 :                 fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
    1078           3 :         if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
    1079           0 :                 fprintf(f, "access_network_type=%d\n",
    1080             :                         config->access_network_type);
    1081             : #endif /* CONFIG_INTERWORKING */
    1082           3 :         if (config->pbc_in_m1)
    1083           0 :                 fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
    1084           3 :         if (config->wps_nfc_pw_from_config) {
    1085           0 :                 if (config->wps_nfc_dev_pw_id)
    1086           0 :                         fprintf(f, "wps_nfc_dev_pw_id=%d\n",
    1087             :                                 config->wps_nfc_dev_pw_id);
    1088           0 :                 write_global_bin(f, "wps_nfc_dh_pubkey",
    1089           0 :                                  config->wps_nfc_dh_pubkey);
    1090           0 :                 write_global_bin(f, "wps_nfc_dh_privkey",
    1091           0 :                                  config->wps_nfc_dh_privkey);
    1092           0 :                 write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
    1093             :         }
    1094             : 
    1095           3 :         if (config->ext_password_backend)
    1096           0 :                 fprintf(f, "ext_password_backend=%s\n",
    1097             :                         config->ext_password_backend);
    1098           3 :         if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
    1099           0 :                 fprintf(f, "p2p_go_max_inactivity=%d\n",
    1100             :                         config->p2p_go_max_inactivity);
    1101           3 :         if (config->auto_interworking)
    1102           0 :                 fprintf(f, "auto_interworking=%d\n",
    1103             :                         config->auto_interworking);
    1104           3 :         if (config->okc)
    1105           0 :                 fprintf(f, "okc=%d\n", config->okc);
    1106           3 :         if (config->pmf)
    1107           0 :                 fprintf(f, "pmf=%d\n", config->pmf);
    1108           3 :         if (config->dtim_period)
    1109           0 :                 fprintf(f, "dtim_period=%d\n", config->dtim_period);
    1110           3 :         if (config->beacon_int)
    1111           0 :                 fprintf(f, "beacon_int=%d\n", config->beacon_int);
    1112             : 
    1113           3 :         if (config->sae_groups) {
    1114             :                 int i;
    1115           0 :                 fprintf(f, "sae_groups=");
    1116           0 :                 for (i = 0; config->sae_groups[i] >= 0; i++) {
    1117           0 :                         fprintf(f, "%s%d", i > 0 ? " " : "",
    1118           0 :                                 config->sae_groups[i]);
    1119             :                 }
    1120           0 :                 fprintf(f, "\n");
    1121             :         }
    1122             : 
    1123           3 :         if (config->ap_vendor_elements) {
    1124           0 :                 int i, len = wpabuf_len(config->ap_vendor_elements);
    1125           0 :                 const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
    1126           0 :                 if (len > 0) {
    1127           0 :                         fprintf(f, "ap_vendor_elements=");
    1128           0 :                         for (i = 0; i < len; i++)
    1129           0 :                                 fprintf(f, "%02x", *p++);
    1130           0 :                         fprintf(f, "\n");
    1131             :                 }
    1132             :         }
    1133             : 
    1134           3 :         if (config->ignore_old_scan_res)
    1135           0 :                 fprintf(f, "ignore_old_scan_res=%d\n",
    1136             :                         config->ignore_old_scan_res);
    1137             : 
    1138           3 :         if (config->freq_list && config->freq_list[0]) {
    1139             :                 int i;
    1140           0 :                 fprintf(f, "freq_list=");
    1141           0 :                 for (i = 0; config->freq_list[i]; i++) {
    1142           0 :                         fprintf(f, "%s%u", i > 0 ? " " : "",
    1143           0 :                                 config->freq_list[i]);
    1144             :                 }
    1145           0 :                 fprintf(f, "\n");
    1146             :         }
    1147           3 :         if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
    1148           0 :                 fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
    1149             : 
    1150           3 :         if (config->sched_scan_interval)
    1151           0 :                 fprintf(f, "sched_scan_interval=%u\n",
    1152             :                         config->sched_scan_interval);
    1153             : 
    1154           3 :         if (config->external_sim)
    1155           0 :                 fprintf(f, "external_sim=%d\n", config->external_sim);
    1156             : 
    1157           3 :         if (config->tdls_external_control)
    1158           0 :                 fprintf(f, "tdls_external_control=%d\n",
    1159             :                         config->tdls_external_control);
    1160             : 
    1161           3 :         if (config->wowlan_triggers)
    1162           0 :                 fprintf(f, "wowlan_triggers=\"%s\"\n",
    1163             :                         config->wowlan_triggers);
    1164             : 
    1165           3 :         if (config->bgscan)
    1166           0 :                 fprintf(f, "bgscan=\"%s\"\n", config->bgscan);
    1167           3 : }
    1168             : 
    1169             : #endif /* CONFIG_NO_CONFIG_WRITE */
    1170             : 
    1171             : 
    1172           3 : int wpa_config_write(const char *name, struct wpa_config *config)
    1173             : {
    1174             : #ifndef CONFIG_NO_CONFIG_WRITE
    1175             :         FILE *f;
    1176             :         struct wpa_ssid *ssid;
    1177             :         struct wpa_cred *cred;
    1178             : #ifndef CONFIG_NO_CONFIG_BLOBS
    1179             :         struct wpa_config_blob *blob;
    1180             : #endif /* CONFIG_NO_CONFIG_BLOBS */
    1181           3 :         int ret = 0;
    1182             : 
    1183           3 :         wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
    1184             : 
    1185           3 :         f = fopen(name, "w");
    1186           3 :         if (f == NULL) {
    1187           0 :                 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
    1188           0 :                 return -1;
    1189             :         }
    1190             : 
    1191           3 :         wpa_config_write_global(f, config);
    1192             : 
    1193           6 :         for (cred = config->cred; cred; cred = cred->next) {
    1194           3 :                 if (cred->temporary)
    1195           0 :                         continue;
    1196           3 :                 fprintf(f, "\ncred={\n");
    1197           3 :                 wpa_config_write_cred(f, cred);
    1198           3 :                 fprintf(f, "}\n");
    1199             :         }
    1200             : 
    1201           6 :         for (ssid = config->ssid; ssid; ssid = ssid->next) {
    1202           3 :                 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
    1203           0 :                         continue; /* do not save temporary networks */
    1204           3 :                 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
    1205           0 :                     !ssid->passphrase)
    1206           0 :                         continue; /* do not save invalid network */
    1207           3 :                 fprintf(f, "\nnetwork={\n");
    1208           3 :                 wpa_config_write_network(f, ssid);
    1209           3 :                 fprintf(f, "}\n");
    1210             :         }
    1211             : 
    1212             : #ifndef CONFIG_NO_CONFIG_BLOBS
    1213           6 :         for (blob = config->blobs; blob; blob = blob->next) {
    1214           3 :                 ret = wpa_config_write_blob(f, blob);
    1215           3 :                 if (ret)
    1216           0 :                         break;
    1217             :         }
    1218             : #endif /* CONFIG_NO_CONFIG_BLOBS */
    1219             : 
    1220           3 :         fclose(f);
    1221             : 
    1222           3 :         wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
    1223             :                    name, ret ? "un" : "");
    1224           3 :         return ret;
    1225             : #else /* CONFIG_NO_CONFIG_WRITE */
    1226             :         return -1;
    1227             : #endif /* CONFIG_NO_CONFIG_WRITE */
    1228             : }

Generated by: LCOV version 1.10