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

Generated by: LCOV version 1.10