Coverage Report

Created: 2025-10-28 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/wpa_supplicant/config_file.c
Line
Count
Source
1
/*
2
 * WPA Supplicant / Configuration backend: text file
3
 * Copyright (c) 2003-2019, 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 "common/ieee802_1x_defs.h"
23
#include "p2p/p2p.h"
24
#include "eap_peer/eap_methods.h"
25
#include "eap_peer/eap.h"
26
#include "utils/config.h"
27
28
29
static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
30
0
{
31
0
  int errors = 0;
32
33
0
  if (ssid->passphrase) {
34
0
    if (ssid->psk_set) {
35
0
      wpa_printf(MSG_ERROR, "Line %d: both PSK and "
36
0
           "passphrase configured.", line);
37
0
      errors++;
38
0
    }
39
0
    wpa_config_update_psk(ssid);
40
0
  }
41
42
0
  if (ssid->disabled == 2)
43
0
    ssid->p2p_persistent_group = 1;
44
45
0
  if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
46
0
      !(ssid->pairwise_cipher & (WPA_CIPHER_CCMP | WPA_CIPHER_CCMP_256 |
47
0
               WPA_CIPHER_GCMP | WPA_CIPHER_GCMP_256 |
48
0
               WPA_CIPHER_NONE))) {
49
    /* Group cipher cannot be stronger than the pairwise cipher. */
50
0
    wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
51
0
         " list since it was not allowed for pairwise "
52
0
         "cipher", line);
53
0
    ssid->group_cipher &= ~WPA_CIPHER_CCMP;
54
0
  }
55
56
0
  if (is_6ghz_freq(ssid->frequency) && ssid->mode == WPAS_MODE_MESH &&
57
0
      ssid->key_mgmt == WPA_KEY_MGMT_NONE) {
58
0
    wpa_printf(MSG_ERROR,
59
0
         "Line %d: key_mgmt for mesh network in 6 GHz should be SAE",
60
0
         line);
61
0
    errors++;
62
0
  }
63
0
  if (ssid->mode == WPAS_MODE_MESH &&
64
0
      (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
65
0
      ssid->key_mgmt != WPA_KEY_MGMT_SAE)) {
66
0
    wpa_printf(MSG_ERROR,
67
0
         "Line %d: key_mgmt for mesh network should be open or SAE",
68
0
         line);
69
0
    errors++;
70
0
  }
71
72
#ifdef CONFIG_OCV
73
  if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
74
    wpa_printf(MSG_ERROR,
75
         "Line %d: PMF needs to be enabled whenever using OCV",
76
         line);
77
    errors++;
78
  }
79
#endif /* CONFIG_OCV */
80
81
0
  return errors;
82
0
}
83
84
85
static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
86
0
{
87
0
  struct wpa_ssid *ssid;
88
0
  int errors = 0, end = 0;
89
0
  char buf[2000], *pos, *pos2;
90
91
0
  wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
92
0
       *line);
93
0
  ssid = os_zalloc(sizeof(*ssid));
94
0
  if (ssid == NULL)
95
0
    return NULL;
96
0
  dl_list_init(&ssid->psk_list);
97
0
  ssid->id = id;
98
99
0
  wpa_config_set_network_defaults(ssid);
100
101
0
  while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
102
0
    if (os_strcmp(pos, "}") == 0) {
103
0
      end = 1;
104
0
      break;
105
0
    }
106
107
0
    pos2 = os_strchr(pos, '=');
108
0
    if (pos2 == NULL) {
109
0
      wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
110
0
           "'%s'.", *line, pos);
111
0
      errors++;
112
0
      continue;
113
0
    }
114
115
0
    *pos2++ = '\0';
116
0
    if (*pos2 == '"') {
117
0
      if (os_strchr(pos2 + 1, '"') == NULL) {
118
0
        wpa_printf(MSG_ERROR, "Line %d: invalid "
119
0
             "quotation '%s'.", *line, pos2);
120
0
        errors++;
121
0
        continue;
122
0
      }
123
0
    }
124
125
0
    if (wpa_config_set(ssid, pos, pos2, *line) < 0) {
126
0
#ifndef CONFIG_WEP
127
0
      if (os_strcmp(pos, "wep_key0") == 0 ||
128
0
          os_strcmp(pos, "wep_key1") == 0 ||
129
0
          os_strcmp(pos, "wep_key2") == 0 ||
130
0
          os_strcmp(pos, "wep_key3") == 0 ||
131
0
          os_strcmp(pos, "wep_tx_keyidx") == 0) {
132
0
        wpa_printf(MSG_ERROR,
133
0
             "Line %d: unsupported WEP parameter",
134
0
             *line);
135
0
        ssid->disabled = 1;
136
0
        continue;
137
0
      }
138
0
#endif /* CONFIG_WEP */
139
0
      errors++;
140
0
    }
141
0
  }
142
143
0
  if (!end) {
144
0
    wpa_printf(MSG_ERROR, "Line %d: network block was not "
145
0
         "terminated properly.", *line);
146
0
    errors++;
147
0
  }
148
149
0
  errors += wpa_config_validate_network(ssid, *line);
150
151
0
  if (errors) {
152
0
    wpa_config_free_ssid(ssid);
153
0
    ssid = NULL;
154
0
  }
155
156
0
  return ssid;
157
0
}
158
159
160
static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
161
0
{
162
0
  struct wpa_cred *cred;
163
0
  int errors = 0, end = 0;
164
0
  char buf[256], *pos, *pos2;
165
166
0
  wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
167
0
  cred = os_zalloc(sizeof(*cred));
168
0
  if (cred == NULL)
169
0
    return NULL;
170
0
  cred->id = id;
171
0
  cred->sim_num = DEFAULT_USER_SELECTED_SIM;
172
173
0
  while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
174
0
    if (os_strcmp(pos, "}") == 0) {
175
0
      end = 1;
176
0
      break;
177
0
    }
178
179
0
    pos2 = os_strchr(pos, '=');
180
0
    if (pos2 == NULL) {
181
0
      wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
182
0
           "'%s'.", *line, pos);
183
0
      errors++;
184
0
      continue;
185
0
    }
186
187
0
    *pos2++ = '\0';
188
0
    if (*pos2 == '"') {
189
0
      if (os_strchr(pos2 + 1, '"') == NULL) {
190
0
        wpa_printf(MSG_ERROR, "Line %d: invalid "
191
0
             "quotation '%s'.", *line, pos2);
192
0
        errors++;
193
0
        continue;
194
0
      }
195
0
    }
196
197
0
    if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
198
0
      errors++;
199
0
  }
200
201
0
  if (!end) {
202
0
    wpa_printf(MSG_ERROR, "Line %d: cred block was not "
203
0
         "terminated properly.", *line);
204
0
    errors++;
205
0
  }
206
207
0
  if (errors) {
208
0
    wpa_config_free_cred(cred);
209
0
    cred = NULL;
210
0
  }
211
212
0
  return cred;
213
0
}
214
215
216
#ifndef CONFIG_NO_CONFIG_BLOBS
217
static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
218
                 const char *name)
219
0
{
220
0
  struct wpa_config_blob *blob;
221
0
  char buf[256], *pos;
222
0
  char *encoded = NULL, *nencoded;
223
0
  int end = 0;
224
0
  size_t encoded_len = 0, len;
225
226
0
  wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
227
0
       *line, name);
228
229
0
  while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
230
0
    if (os_strcmp(pos, "}") == 0) {
231
0
      end = 1;
232
0
      break;
233
0
    }
234
235
0
    len = os_strlen(pos);
236
0
    nencoded = os_realloc(encoded, encoded_len + len);
237
0
    if (nencoded == NULL) {
238
0
      wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
239
0
           "blob", *line);
240
0
      os_free(encoded);
241
0
      return NULL;
242
0
    }
243
0
    encoded = nencoded;
244
0
    os_memcpy(encoded + encoded_len, pos, len);
245
0
    encoded_len += len;
246
0
  }
247
248
0
  if (!end || !encoded) {
249
0
    wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
250
0
         "properly", *line);
251
0
    os_free(encoded);
252
0
    return NULL;
253
0
  }
254
255
0
  blob = os_zalloc(sizeof(*blob));
256
0
  if (blob == NULL) {
257
0
    os_free(encoded);
258
0
    return NULL;
259
0
  }
260
0
  blob->name = os_strdup(name);
261
0
  blob->data = base64_decode(encoded, encoded_len, &blob->len);
262
0
  os_free(encoded);
263
264
0
  if (blob->name == NULL || blob->data == NULL) {
265
0
    wpa_config_free_blob(blob);
266
0
    return NULL;
267
0
  }
268
269
0
  return blob;
270
0
}
271
272
273
static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
274
           int *line, char *bname)
275
0
{
276
0
  char *name_end;
277
0
  struct wpa_config_blob *blob;
278
279
0
  name_end = os_strchr(bname, '=');
280
0
  if (name_end == NULL) {
281
0
    wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
282
0
         *line);
283
0
    return -1;
284
0
  }
285
0
  *name_end = '\0';
286
287
0
  blob = wpa_config_read_blob(f, line, bname);
288
0
  if (blob == NULL) {
289
0
    wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
290
0
         *line, bname);
291
0
    return -1;
292
0
  }
293
0
  wpa_config_set_blob(config, blob);
294
0
  return 0;
295
0
}
296
#endif /* CONFIG_NO_CONFIG_BLOBS */
297
298
299
static struct wpa_dev_ik * wpa_config_read_identity(FILE *f, int *line, int id)
300
0
{
301
0
  struct wpa_dev_ik *identity;
302
0
  int errors = 0, end = 0;
303
0
  char buf[256], *pos, *pos2;
304
305
0
  wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new identity block",
306
0
       *line);
307
0
  identity = os_zalloc(sizeof(*identity));
308
0
  if (!identity)
309
0
    return NULL;
310
0
  identity->id = id;
311
312
0
  while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
313
0
    if (os_strcmp(pos, "}") == 0) {
314
0
      end = 1;
315
0
      break;
316
0
    }
317
318
0
    pos2 = os_strchr(pos, '=');
319
0
    if (!pos2) {
320
0
      wpa_printf(MSG_ERROR,
321
0
           "Line %d: Invalid identity line '%s'.",
322
0
           *line, pos);
323
0
      errors++;
324
0
      continue;
325
0
    }
326
327
0
    *pos2++ = '\0';
328
0
    if (*pos2 == '"') {
329
0
      if (os_strchr(pos2 + 1, '"') == NULL) {
330
0
        wpa_printf(MSG_ERROR,
331
0
             "Line %d: invalid quotation '%s'.",
332
0
             *line, pos2);
333
0
        errors++;
334
0
        continue;
335
0
      }
336
0
    }
337
338
0
    if (wpa_config_set_identity(identity, pos, pos2, *line) < 0)
339
0
      errors++;
340
0
  }
341
342
0
  if (!end) {
343
0
    wpa_printf(MSG_ERROR,
344
0
         "Line %d: identity block was not terminated properly.",
345
0
         *line);
346
0
    errors++;
347
0
  }
348
349
0
  if (errors) {
350
0
    wpa_config_free_identity(identity);
351
0
    identity = NULL;
352
0
  }
353
354
0
  return identity;
355
0
}
356
357
358
struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp,
359
            bool ro, bool show_details)
360
0
{
361
0
  FILE *f;
362
0
  char buf[1024], *pos;
363
0
  int errors = 0, line = 0;
364
0
  struct wpa_ssid *ssid, *tail, *head;
365
0
  struct wpa_cred *cred, *cred_tail, *cred_head;
366
0
  struct wpa_dev_ik *identity, *identity_tail, *identity_head;
367
0
  struct wpa_config *config;
368
0
  static int id = 0;
369
0
  static int cred_id = 0;
370
0
  static int identity_id = 1;
371
372
0
  if (name == NULL)
373
0
    return NULL;
374
0
  if (cfgp)
375
0
    config = cfgp;
376
0
  else
377
0
    config = wpa_config_alloc_empty(NULL, NULL);
378
0
  if (config == NULL) {
379
0
    wpa_printf(MSG_ERROR, "Failed to allocate config file "
380
0
         "structure");
381
0
    return NULL;
382
0
  }
383
0
  tail = head = config->ssid;
384
0
  while (tail && tail->next)
385
0
    tail = tail->next;
386
0
  cred_tail = cred_head = config->cred;
387
0
  while (cred_tail && cred_tail->next)
388
0
    cred_tail = cred_tail->next;
389
0
  identity_tail = identity_head = config->identity;
390
0
  while (identity_tail && identity_tail->next)
391
0
    identity_tail = identity_tail->next;
392
393
0
  wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
394
0
  f = fopen(name, "r");
395
0
  if (f == NULL) {
396
0
    if (show_details)
397
0
      wpa_printf(MSG_ERROR,
398
0
           "Failed to open config file '%s', error: %s",
399
0
           name, strerror(errno));
400
0
    if (config != cfgp)
401
0
      os_free(config);
402
0
    return NULL;
403
0
  }
404
405
0
  while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
406
0
#ifndef WPA_IGNORE_CONFIG_ERRORS
407
0
    if (errors && !show_details)
408
0
      break;
409
0
#endif /* WPA_IGNORE_CONFIG_ERRORS */
410
411
0
    if (os_strcmp(pos, "network={") == 0) {
412
0
      ssid = wpa_config_read_network(f, &line, id++);
413
0
      if (ssid == NULL) {
414
0
        wpa_printf(MSG_ERROR, "Line %d: failed to "
415
0
             "parse network block.", line);
416
0
        errors++;
417
0
        continue;
418
0
      }
419
0
      ssid->ro = ro;
420
0
      if (head == NULL) {
421
0
        head = tail = ssid;
422
0
      } else {
423
0
        tail->next = ssid;
424
0
        tail = ssid;
425
0
      }
426
0
      if (wpa_config_add_prio_network(config, ssid)) {
427
0
        wpa_printf(MSG_ERROR, "Line %d: failed to add "
428
0
             "network block to priority list.",
429
0
             line);
430
0
        errors++;
431
0
        continue;
432
0
      }
433
0
    } else if (os_strcmp(pos, "cred={") == 0) {
434
0
      cred = wpa_config_read_cred(f, &line, cred_id++);
435
0
      if (cred == NULL) {
436
0
        wpa_printf(MSG_ERROR, "Line %d: failed to "
437
0
             "parse cred block.", line);
438
0
        errors++;
439
0
        continue;
440
0
      }
441
0
      if (cred_head == NULL) {
442
0
        cred_head = cred_tail = cred;
443
0
      } else {
444
0
        cred_tail->next = cred;
445
0
        cred_tail = cred;
446
0
      }
447
0
#ifndef CONFIG_NO_CONFIG_BLOBS
448
0
    } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
449
0
      if (wpa_config_process_blob(config, f, &line, pos + 12)
450
0
          < 0) {
451
0
        wpa_printf(MSG_ERROR, "Line %d: failed to "
452
0
             "process blob.", line);
453
0
        errors++;
454
0
        continue;
455
0
      }
456
0
#endif /* CONFIG_NO_CONFIG_BLOBS */
457
0
    } else if (os_strcmp(pos, "identity={") == 0) {
458
0
      identity = wpa_config_read_identity(f, &line,
459
0
                  identity_id++);
460
0
      if (!identity) {
461
0
        wpa_printf(MSG_ERROR,
462
0
             "Line %d: failed to parse identity block.",
463
0
             line);
464
0
        errors++;
465
0
        continue;
466
0
      }
467
0
      if (!identity_head) {
468
0
        identity_head = identity_tail = identity;
469
0
      } else {
470
0
        identity_tail->next = identity;
471
0
        identity_tail = identity;
472
0
      }
473
0
    } else if (wpa_config_process_global(config, pos, line,
474
0
                 show_details) < 0) {
475
0
      if (show_details)
476
0
        wpa_printf(MSG_ERROR,
477
0
             "Line %d: Invalid configuration line '%s'.",
478
0
             line, pos);
479
0
      errors++;
480
0
      continue;
481
0
    }
482
0
  }
483
484
0
  fclose(f);
485
486
0
  config->ssid = head;
487
0
  wpa_config_debug_dump_networks(config);
488
0
  config->cred = cred_head;
489
0
  config->identity = identity_head;
490
491
0
#ifndef WPA_IGNORE_CONFIG_ERRORS
492
0
  if (errors) {
493
0
    if (config != cfgp)
494
0
      wpa_config_free(config);
495
0
    config = NULL;
496
0
    head = NULL;
497
0
  }
498
0
#endif /* WPA_IGNORE_CONFIG_ERRORS */
499
500
0
  return config;
501
0
}
502
503
504
#ifndef CONFIG_NO_CONFIG_WRITE
505
506
static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
507
0
{
508
0
  char *value = wpa_config_get(ssid, field);
509
0
  if (value == NULL)
510
0
    return;
511
0
  fprintf(f, "\t%s=%s\n", field, value);
512
0
  str_clear_free(value);
513
0
}
514
515
516
static void write_int(FILE *f, const char *field, int value, int def)
517
0
{
518
0
  if (value == def)
519
0
    return;
520
0
  fprintf(f, "\t%s=%d\n", field, value);
521
0
}
522
523
524
static void write_bssid(FILE *f, struct wpa_ssid *ssid)
525
0
{
526
0
  char *value = wpa_config_get(ssid, "bssid");
527
0
  if (value == NULL)
528
0
    return;
529
0
  fprintf(f, "\tbssid=%s\n", value);
530
0
  os_free(value);
531
0
}
532
533
534
static void write_bssid_hint(FILE *f, struct wpa_ssid *ssid)
535
0
{
536
0
  char *value = wpa_config_get(ssid, "bssid_hint");
537
538
0
  if (!value)
539
0
    return;
540
0
  fprintf(f, "\tbssid_hint=%s\n", value);
541
0
  os_free(value);
542
0
}
543
544
545
static void write_psk(FILE *f, struct wpa_ssid *ssid)
546
0
{
547
0
  char *value;
548
549
0
  if (ssid->mem_only_psk)
550
0
    return;
551
552
0
  value = wpa_config_get(ssid, "psk");
553
0
  if (value == NULL)
554
0
    return;
555
0
  fprintf(f, "\tpsk=%s\n", value);
556
0
  os_free(value);
557
0
}
558
559
560
static void write_proto(FILE *f, struct wpa_ssid *ssid)
561
0
{
562
0
  char *value;
563
564
0
  if (ssid->proto == DEFAULT_PROTO)
565
0
    return;
566
567
0
  value = wpa_config_get(ssid, "proto");
568
0
  if (value == NULL)
569
0
    return;
570
0
  if (value[0])
571
0
    fprintf(f, "\tproto=%s\n", value);
572
0
  os_free(value);
573
0
}
574
575
576
static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
577
0
{
578
0
  char *value;
579
580
0
  if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
581
0
    return;
582
583
0
  value = wpa_config_get(ssid, "key_mgmt");
584
0
  if (value == NULL)
585
0
    return;
586
0
  if (value[0])
587
0
    fprintf(f, "\tkey_mgmt=%s\n", value);
588
0
  os_free(value);
589
0
}
590
591
592
static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
593
0
{
594
0
  char *value;
595
596
0
  if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
597
0
    return;
598
599
0
  value = wpa_config_get(ssid, "pairwise");
600
0
  if (value == NULL)
601
0
    return;
602
0
  if (value[0])
603
0
    fprintf(f, "\tpairwise=%s\n", value);
604
0
  os_free(value);
605
0
}
606
607
608
static void write_group(FILE *f, struct wpa_ssid *ssid)
609
0
{
610
0
  char *value;
611
612
0
  if (ssid->group_cipher == DEFAULT_GROUP)
613
0
    return;
614
615
0
  value = wpa_config_get(ssid, "group");
616
0
  if (value == NULL)
617
0
    return;
618
0
  if (value[0])
619
0
    fprintf(f, "\tgroup=%s\n", value);
620
0
  os_free(value);
621
0
}
622
623
624
static void write_group_mgmt(FILE *f, struct wpa_ssid *ssid)
625
0
{
626
0
  char *value;
627
628
0
  if (!ssid->group_mgmt_cipher)
629
0
    return;
630
631
0
  value = wpa_config_get(ssid, "group_mgmt");
632
0
  if (!value)
633
0
    return;
634
0
  if (value[0])
635
0
    fprintf(f, "\tgroup_mgmt=%s\n", value);
636
0
  os_free(value);
637
0
}
638
639
640
static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
641
0
{
642
0
  char *value;
643
644
0
  if (ssid->auth_alg == 0)
645
0
    return;
646
647
0
  value = wpa_config_get(ssid, "auth_alg");
648
0
  if (value == NULL)
649
0
    return;
650
0
  if (value[0])
651
0
    fprintf(f, "\tauth_alg=%s\n", value);
652
0
  os_free(value);
653
0
}
654
655
656
#ifdef IEEE8021X_EAPOL
657
static void write_eap(FILE *f, struct wpa_ssid *ssid)
658
0
{
659
0
  char *value;
660
661
0
  value = wpa_config_get(ssid, "eap");
662
0
  if (value == NULL)
663
0
    return;
664
665
0
  if (value[0])
666
0
    fprintf(f, "\teap=%s\n", value);
667
0
  os_free(value);
668
0
}
669
#endif /* IEEE8021X_EAPOL */
670
671
672
#ifdef CONFIG_WEP
673
static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
674
{
675
  char field[20], *value;
676
  int res;
677
678
  res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
679
  if (os_snprintf_error(sizeof(field), res))
680
    return;
681
  value = wpa_config_get(ssid, field);
682
  if (value) {
683
    fprintf(f, "\t%s=%s\n", field, value);
684
    os_free(value);
685
  }
686
}
687
#endif /* CONFIG_WEP */
688
689
690
#ifdef CONFIG_P2P
691
692
static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
693
{
694
  char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
695
  if (value == NULL)
696
    return;
697
  fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
698
  os_free(value);
699
}
700
701
static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
702
{
703
  char *value = wpa_config_get(ssid, "p2p_client_list");
704
  if (value == NULL)
705
    return;
706
  fprintf(f, "\tp2p_client_list=%s\n", value);
707
  os_free(value);
708
}
709
710
711
static void write_p2p2_client_list(FILE *f, struct wpa_ssid *ssid)
712
{
713
  char *value = wpa_config_get(ssid, "p2p2_client_list");
714
715
  if (!value)
716
    return;
717
  fprintf(f, "\tp2p2_client_list=%s\n", value);
718
  os_free(value);
719
}
720
721
722
static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
723
{
724
  struct psk_list_entry *psk;
725
  char hex[32 * 2 + 1];
726
727
  dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
728
    wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
729
    fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
730
      psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
731
  }
732
}
733
734
#endif /* CONFIG_P2P */
735
736
737
#ifdef CONFIG_MACSEC
738
739
static void write_mka_cak(FILE *f, struct wpa_ssid *ssid)
740
{
741
  char *value;
742
743
  if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
744
    return;
745
746
  value = wpa_config_get(ssid, "mka_cak");
747
  if (!value)
748
    return;
749
  fprintf(f, "\tmka_cak=%s\n", value);
750
  os_free(value);
751
}
752
753
754
static void write_mka_ckn(FILE *f, struct wpa_ssid *ssid)
755
{
756
  char *value;
757
758
  if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
759
    return;
760
761
  value = wpa_config_get(ssid, "mka_ckn");
762
  if (!value)
763
    return;
764
  fprintf(f, "\tmka_ckn=%s\n", value);
765
  os_free(value);
766
}
767
768
#endif /* CONFIG_MACSEC */
769
770
771
static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
772
0
{
773
0
#define STR(t) write_str(f, #t, ssid)
774
0
#define INT(t) write_int(f, #t, ssid->t, 0)
775
0
#define INTe(t, m) write_int(f, #t, ssid->eap.m, 0)
776
0
#define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
777
0
#define INT_DEFe(t, m, def) write_int(f, #t, ssid->eap.m, def)
778
779
0
  STR(ssid);
780
0
  INT(scan_ssid);
781
0
  write_bssid(f, ssid);
782
0
  write_bssid_hint(f, ssid);
783
0
  write_str(f, "bssid_ignore", ssid);
784
0
  write_str(f, "bssid_accept", ssid);
785
0
  write_psk(f, ssid);
786
0
  INT(mem_only_psk);
787
0
  STR(sae_password);
788
0
  STR(sae_password_id);
789
0
  write_int(f, "sae_pwe", ssid->sae_pwe, DEFAULT_SAE_PWE);
790
0
  INT(sae_password_id_change);
791
0
  write_proto(f, ssid);
792
0
  write_key_mgmt(f, ssid);
793
0
  INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
794
0
  write_pairwise(f, ssid);
795
0
  write_group(f, ssid);
796
0
  write_group_mgmt(f, ssid);
797
0
  write_auth_alg(f, ssid);
798
0
  STR(bgscan);
799
0
  STR(autoscan);
800
0
  STR(scan_freq);
801
0
  STR(freq_list);
802
0
#ifdef IEEE8021X_EAPOL
803
0
  write_eap(f, ssid);
804
0
  STR(identity);
805
0
  STR(anonymous_identity);
806
0
  STR(imsi_identity);
807
0
  STR(machine_identity);
808
0
  STR(password);
809
0
  STR(machine_password);
810
0
  STR(ca_cert);
811
0
  STR(ca_path);
812
0
  STR(client_cert);
813
0
  STR(private_key);
814
0
  STR(private_key_passwd);
815
0
  STR(subject_match);
816
0
  STR(check_cert_subject);
817
0
  STR(altsubject_match);
818
0
  STR(domain_suffix_match);
819
0
  STR(domain_match);
820
0
  STR(ca_cert2);
821
0
  STR(ca_path2);
822
0
  STR(client_cert2);
823
0
  STR(private_key2);
824
0
  STR(private_key2_passwd);
825
0
  STR(subject_match2);
826
0
  STR(check_cert_subject2);
827
0
  STR(altsubject_match2);
828
0
  STR(domain_suffix_match2);
829
0
  STR(domain_match2);
830
0
  STR(machine_ca_cert);
831
0
  STR(machine_ca_path);
832
0
  STR(machine_client_cert);
833
0
  STR(machine_private_key);
834
0
  STR(machine_private_key_passwd);
835
0
  STR(machine_subject_match);
836
0
  STR(machine_check_cert_subject);
837
0
  STR(machine_altsubject_match);
838
0
  STR(machine_domain_suffix_match);
839
0
  STR(machine_domain_match);
840
0
  STR(phase1);
841
0
  STR(phase2);
842
0
  STR(machine_phase2);
843
0
  STR(pcsc);
844
0
  STR(pin);
845
0
  STR(engine_id);
846
0
  STR(key_id);
847
0
  STR(cert_id);
848
0
  STR(ca_cert_id);
849
0
  STR(key2_id);
850
0
  STR(pin2);
851
0
  STR(engine2_id);
852
0
  STR(cert2_id);
853
0
  STR(ca_cert2_id);
854
0
  INTe(engine, cert.engine);
855
0
  INTe(engine2, phase2_cert.engine);
856
0
  INTe(machine_engine, machine_cert.engine);
857
0
  INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
858
0
  STR(openssl_ciphers);
859
0
  INTe(erp, erp);
860
0
#endif /* IEEE8021X_EAPOL */
861
#ifdef CONFIG_WEP
862
  {
863
    int i;
864
865
    for (i = 0; i < 4; i++)
866
      write_wep_key(f, i, ssid);
867
    INT(wep_tx_keyidx);
868
  }
869
#endif /* CONFIG_WEP */
870
0
  INT(priority);
871
0
#ifdef IEEE8021X_EAPOL
872
0
  INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
873
0
  STR(pac_file);
874
0
  INT_DEFe(fragment_size, fragment_size, DEFAULT_FRAGMENT_SIZE);
875
0
  INTe(ocsp, cert.ocsp);
876
0
  INTe(ocsp2, phase2_cert.ocsp);
877
0
  INTe(machine_ocsp, machine_cert.ocsp);
878
0
  INT_DEFe(sim_num, sim_num, DEFAULT_USER_SELECTED_SIM);
879
0
#endif /* IEEE8021X_EAPOL */
880
0
  INT(mode);
881
#ifdef CONFIG_MESH
882
  INT(no_auto_peer);
883
  INT_DEF(mesh_fwding, DEFAULT_MESH_FWDING);
884
#endif /* CONFIG_MESH */
885
0
  INT(frequency);
886
0
  INT(enable_edmg);
887
0
  INT(edmg_channel);
888
0
  INT(fixed_freq);
889
#ifdef CONFIG_ACS
890
  INT(acs);
891
#endif /* CONFIG_ACS */
892
0
  write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
893
0
  INT(disabled);
894
0
  INT(mixed_cell);
895
0
  INT_DEF(vht, 1);
896
0
  INT_DEF(ht, 1);
897
0
  INT(ht40);
898
0
  INT_DEF(he, 1);
899
0
  INT_DEF(max_oper_chwidth, DEFAULT_MAX_OPER_CHWIDTH);
900
0
  INT(vht_center_freq1);
901
0
  INT(vht_center_freq2);
902
0
  INT(pbss);
903
0
  INT(wps_disabled);
904
0
  INT(fils_dh_group);
905
0
  write_int(f, "ieee80211w", ssid->ieee80211w,
906
0
      MGMT_FRAME_PROTECTION_DEFAULT);
907
0
  STR(id_str);
908
#ifdef CONFIG_P2P
909
  write_go_p2p_dev_addr(f, ssid);
910
  write_p2p_client_list(f, ssid);
911
  write_p2p2_client_list(f, ssid);
912
  write_psk_list(f, ssid);
913
  INT(go_dik_id);
914
#endif /* CONFIG_P2P */
915
0
  INT(ap_max_inactivity);
916
0
  INT(dtim_period);
917
0
  INT(beacon_int);
918
#ifdef CONFIG_MACSEC
919
  INT(macsec_policy);
920
  write_mka_cak(f, ssid);
921
  write_mka_ckn(f, ssid);
922
  INT(macsec_integ_only);
923
  INT(macsec_replay_protect);
924
  INT(macsec_replay_window);
925
  INT(macsec_offload);
926
  INT(macsec_port);
927
  INT_DEF(mka_priority, DEFAULT_PRIO_NOT_KEY_SERVER);
928
  INT(macsec_csindex);
929
  INT(macsec_icv_indicator);
930
#endif /* CONFIG_MACSEC */
931
0
#ifdef CONFIG_HS20
932
0
  INT(update_identifier);
933
0
  STR(roaming_consortium_selection);
934
0
#endif /* CONFIG_HS20 */
935
0
  write_int(f, "mac_addr", ssid->mac_addr, -1);
936
#ifdef CONFIG_MESH
937
  STR(mesh_basic_rates);
938
  INT_DEF(dot11MeshMaxRetries, DEFAULT_MESH_MAX_RETRIES);
939
  INT_DEF(dot11MeshRetryTimeout, DEFAULT_MESH_RETRY_TIMEOUT);
940
  INT_DEF(dot11MeshConfirmTimeout, DEFAULT_MESH_CONFIRM_TIMEOUT);
941
  INT_DEF(dot11MeshHoldingTimeout, DEFAULT_MESH_HOLDING_TIMEOUT);
942
  INT_DEF(mesh_rssi_threshold, DEFAULT_MESH_RSSI_THRESHOLD);
943
#endif /* CONFIG_MESH */
944
0
  INT(wpa_ptk_rekey);
945
0
  INT(wpa_deny_ptk0_rekey);
946
0
  INT(group_rekey);
947
0
  INT(ignore_broadcast_ssid);
948
#ifdef CONFIG_DPP
949
  STR(dpp_connector);
950
  STR(dpp_netaccesskey);
951
  INT(dpp_netaccesskey_expiry);
952
  STR(dpp_csign);
953
  STR(dpp_pp_key);
954
  INT(dpp_pfs);
955
  INT(dpp_connector_privacy);
956
#endif /* CONFIG_DPP */
957
0
  INT(owe_group);
958
0
  INT(owe_only);
959
0
  INT(owe_ptk_workaround);
960
0
  INT(multi_ap_backhaul_sta);
961
0
  INT(ft_eap_pmksa_caching);
962
0
  INT(multi_ap_profile);
963
0
  INT(beacon_prot);
964
0
  INT(transition_disable);
965
0
  INT(sae_pk);
966
#ifdef CONFIG_HT_OVERRIDES
967
  INT_DEF(disable_ht, DEFAULT_DISABLE_HT);
968
  INT_DEF(disable_ht40, DEFAULT_DISABLE_HT40);
969
  INT_DEF(disable_sgi, DEFAULT_DISABLE_SGI);
970
  INT_DEF(disable_ldpc, DEFAULT_DISABLE_LDPC);
971
  INT(ht40_intolerant);
972
  INT_DEF(tx_stbc, DEFAULT_TX_STBC);
973
  INT_DEF(rx_stbc, DEFAULT_RX_STBC);
974
  INT_DEF(disable_max_amsdu, DEFAULT_DISABLE_MAX_AMSDU);
975
  INT_DEF(ampdu_factor, DEFAULT_AMPDU_FACTOR);
976
  INT_DEF(ampdu_density, DEFAULT_AMPDU_DENSITY);
977
  STR(ht_mcs);
978
#endif /* CONFIG_HT_OVERRIDES */
979
#ifdef CONFIG_VHT_OVERRIDES
980
  INT(disable_vht);
981
  INT(vht_capa);
982
  INT(vht_capa_mask);
983
  INT_DEF(vht_rx_mcs_nss_1, -1);
984
  INT_DEF(vht_rx_mcs_nss_2, -1);
985
  INT_DEF(vht_rx_mcs_nss_3, -1);
986
  INT_DEF(vht_rx_mcs_nss_4, -1);
987
  INT_DEF(vht_rx_mcs_nss_5, -1);
988
  INT_DEF(vht_rx_mcs_nss_6, -1);
989
  INT_DEF(vht_rx_mcs_nss_7, -1);
990
  INT_DEF(vht_rx_mcs_nss_8, -1);
991
  INT_DEF(vht_tx_mcs_nss_1, -1);
992
  INT_DEF(vht_tx_mcs_nss_2, -1);
993
  INT_DEF(vht_tx_mcs_nss_3, -1);
994
  INT_DEF(vht_tx_mcs_nss_4, -1);
995
  INT_DEF(vht_tx_mcs_nss_5, -1);
996
  INT_DEF(vht_tx_mcs_nss_6, -1);
997
  INT_DEF(vht_tx_mcs_nss_7, -1);
998
  INT_DEF(vht_tx_mcs_nss_8, -1);
999
#endif /* CONFIG_VHT_OVERRIDES */
1000
#ifdef CONFIG_HE_OVERRIDES
1001
  INT(disable_he);
1002
#endif /* CONFIG_HE_OVERRIDES */
1003
0
  INT(disable_eht);
1004
0
  INT(enable_4addr_mode);
1005
0
  INT(max_idle);
1006
0
  INT(ssid_protection);
1007
0
  INT_DEF(rsn_overriding, RSN_OVERRIDING_NOT_SET);
1008
#ifdef CONFIG_SAE
1009
  if (ssid->alt_sae_password_ids) {
1010
    struct wpabuf_array *ids = ssid->alt_sae_password_ids;
1011
    unsigned int idx;
1012
    char hex[255 * 2 + 1];
1013
1014
    for (idx = 0; idx < ids->num; idx++) {
1015
      wpa_snprintf_hex(hex, sizeof(hex),
1016
           wpabuf_head(ids->buf[idx]),
1017
           wpabuf_len(ids->buf[idx]));
1018
      fprintf(f, "\talt_sae_password_ids=%s\n", hex);
1019
    }
1020
  }
1021
#endif /* CONFIG_SAE */
1022
0
#undef STR
1023
0
#undef INT
1024
0
#undef INT_DEF
1025
0
}
1026
1027
1028
static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
1029
0
{
1030
0
  size_t i;
1031
1032
0
  if (cred->priority)
1033
0
    fprintf(f, "\tpriority=%d\n", cred->priority);
1034
0
  if (cred->pcsc)
1035
0
    fprintf(f, "\tpcsc=%d\n", cred->pcsc);
1036
0
  if (cred->realm)
1037
0
    fprintf(f, "\trealm=\"%s\"\n", cred->realm);
1038
0
  if (cred->username)
1039
0
    fprintf(f, "\tusername=\"%s\"\n", cred->username);
1040
0
  if (cred->password && cred->ext_password)
1041
0
    fprintf(f, "\tpassword=ext:%s\n", cred->password);
1042
0
  else if (cred->password)
1043
0
    fprintf(f, "\tpassword=\"%s\"\n", cred->password);
1044
0
  if (cred->ca_cert)
1045
0
    fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
1046
0
  if (cred->client_cert)
1047
0
    fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
1048
0
  if (cred->private_key)
1049
0
    fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
1050
0
  if (cred->private_key_passwd)
1051
0
    fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
1052
0
      cred->private_key_passwd);
1053
0
  if (cred->imsi)
1054
0
    fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
1055
0
  if (cred->milenage)
1056
0
    fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
1057
0
  for (i = 0; i < cred->num_domain; i++)
1058
0
    fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
1059
0
  if (cred->domain_suffix_match)
1060
0
    fprintf(f, "\tdomain_suffix_match=\"%s\"\n",
1061
0
      cred->domain_suffix_match);
1062
0
  if (cred->eap_method) {
1063
0
    const char *name;
1064
0
    name = eap_get_name(cred->eap_method[0].vendor,
1065
0
            cred->eap_method[0].method);
1066
0
    if (name)
1067
0
      fprintf(f, "\teap=%s\n", name);
1068
0
  }
1069
0
  if (cred->phase1)
1070
0
    fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
1071
0
  if (cred->phase2)
1072
0
    fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
1073
0
  if (cred->excluded_ssid) {
1074
0
    size_t j;
1075
0
    for (i = 0; i < cred->num_excluded_ssid; i++) {
1076
0
      struct excluded_ssid *e = &cred->excluded_ssid[i];
1077
0
      fprintf(f, "\texcluded_ssid=");
1078
0
      for (j = 0; j < e->ssid_len; j++)
1079
0
        fprintf(f, "%02x", e->ssid[j]);
1080
0
      fprintf(f, "\n");
1081
0
    }
1082
0
  }
1083
0
  if (cred->roaming_partner) {
1084
0
    for (i = 0; i < cred->num_roaming_partner; i++) {
1085
0
      struct roaming_partner *p = &cred->roaming_partner[i];
1086
0
      fprintf(f, "\troaming_partner=\"%s,%d,%u,%s\"\n",
1087
0
        p->fqdn, p->exact_match, p->priority,
1088
0
        p->country);
1089
0
    }
1090
0
  }
1091
0
  if (cred->update_identifier)
1092
0
    fprintf(f, "\tupdate_identifier=%d\n", cred->update_identifier);
1093
1094
0
  if (cred->provisioning_sp)
1095
0
    fprintf(f, "\tprovisioning_sp=\"%s\"\n", cred->provisioning_sp);
1096
0
  if (cred->sp_priority)
1097
0
    fprintf(f, "\tsp_priority=%d\n", cred->sp_priority);
1098
1099
0
  if (cred->min_dl_bandwidth_home)
1100
0
    fprintf(f, "\tmin_dl_bandwidth_home=%u\n",
1101
0
      cred->min_dl_bandwidth_home);
1102
0
  if (cred->min_ul_bandwidth_home)
1103
0
    fprintf(f, "\tmin_ul_bandwidth_home=%u\n",
1104
0
      cred->min_ul_bandwidth_home);
1105
0
  if (cred->min_dl_bandwidth_roaming)
1106
0
    fprintf(f, "\tmin_dl_bandwidth_roaming=%u\n",
1107
0
      cred->min_dl_bandwidth_roaming);
1108
0
  if (cred->min_ul_bandwidth_roaming)
1109
0
    fprintf(f, "\tmin_ul_bandwidth_roaming=%u\n",
1110
0
      cred->min_ul_bandwidth_roaming);
1111
1112
0
  if (cred->max_bss_load)
1113
0
    fprintf(f, "\tmax_bss_load=%u\n",
1114
0
      cred->max_bss_load);
1115
1116
0
  if (cred->ocsp)
1117
0
    fprintf(f, "\tocsp=%d\n", cred->ocsp);
1118
1119
0
  if (cred->num_req_conn_capab) {
1120
0
    for (i = 0; i < cred->num_req_conn_capab; i++) {
1121
0
      int *ports;
1122
1123
0
      fprintf(f, "\treq_conn_capab=%u",
1124
0
        cred->req_conn_capab_proto[i]);
1125
0
      ports = cred->req_conn_capab_port[i];
1126
0
      if (ports) {
1127
0
        int j;
1128
0
        for (j = 0; ports[j] != -1; j++) {
1129
0
          fprintf(f, "%s%d", j > 0 ? "," : ":",
1130
0
            ports[j]);
1131
0
        }
1132
0
      }
1133
0
      fprintf(f, "\n");
1134
0
    }
1135
0
  }
1136
1137
0
  if (cred->num_home_ois) {
1138
0
    size_t j;
1139
1140
0
    fprintf(f, "\thome_ois=\"");
1141
0
    for (i = 0; i < cred->num_home_ois; i++) {
1142
0
      if (i > 0)
1143
0
        fprintf(f, ",");
1144
0
      for (j = 0; j < cred->home_ois_len[i]; j++)
1145
0
        fprintf(f, "%02x",
1146
0
          cred->home_ois[i][j]);
1147
0
    }
1148
0
    fprintf(f, "\"\n");
1149
0
  }
1150
1151
0
  if (cred->num_required_home_ois) {
1152
0
    size_t j;
1153
1154
0
    fprintf(f, "\trequired_home_ois=\"");
1155
0
    for (i = 0; i < cred->num_required_home_ois; i++) {
1156
0
      if (i > 0)
1157
0
        fprintf(f, ",");
1158
0
      for (j = 0; j < cred->required_home_ois_len[i]; j++)
1159
0
        fprintf(f, "%02x",
1160
0
          cred->required_home_ois[i][j]);
1161
0
    }
1162
0
    fprintf(f, "\"\n");
1163
0
  }
1164
1165
0
  if (cred->num_roaming_consortiums) {
1166
0
    size_t j;
1167
1168
0
    fprintf(f, "\troaming_consortiums=\"");
1169
0
    for (i = 0; i < cred->num_roaming_consortiums; i++) {
1170
0
      if (i > 0)
1171
0
        fprintf(f, ",");
1172
0
      for (j = 0; j < cred->roaming_consortiums_len[i]; j++)
1173
0
        fprintf(f, "%02x",
1174
0
          cred->roaming_consortiums[i][j]);
1175
0
    }
1176
0
    fprintf(f, "\"\n");
1177
0
  }
1178
1179
0
  if (cred->sim_num != DEFAULT_USER_SELECTED_SIM)
1180
0
    fprintf(f, "\tsim_num=%d\n", cred->sim_num);
1181
1182
0
  if (cred->engine)
1183
0
    fprintf(f, "\tengine=%d\n", cred->engine);
1184
0
  if (cred->engine_id)
1185
0
    fprintf(f, "\tengine_id=\"%s\"\n", cred->engine_id);
1186
0
  if (cred->key_id)
1187
0
    fprintf(f, "\tkey_id=\"%s\"\n", cred->key_id);
1188
0
  if (cred->cert_id)
1189
0
    fprintf(f, "\tcert_id=\"%s\"\n", cred->cert_id);
1190
0
  if (cred->ca_cert_id)
1191
0
    fprintf(f, "\tca_cert_id=\"%s\"\n", cred->ca_cert_id);
1192
1193
0
  if (cred->imsi_privacy_cert)
1194
0
    fprintf(f, "\timsi_privacy_cert=\"%s\"\n",
1195
0
      cred->imsi_privacy_cert);
1196
0
  if (cred->imsi_privacy_attr)
1197
0
    fprintf(f, "\timsi_privacy_attr=\"%s\"\n",
1198
0
      cred->imsi_privacy_attr);
1199
0
}
1200
1201
1202
#ifndef CONFIG_NO_CONFIG_BLOBS
1203
static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
1204
0
{
1205
0
  char *encoded;
1206
1207
0
  encoded = base64_encode(blob->data, blob->len, NULL);
1208
0
  if (encoded == NULL)
1209
0
    return -1;
1210
1211
0
  fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
1212
0
  os_free(encoded);
1213
0
  return 0;
1214
0
}
1215
#endif /* CONFIG_NO_CONFIG_BLOBS */
1216
1217
1218
static void write_global_bin(FILE *f, const char *field,
1219
           const struct wpabuf *val)
1220
0
{
1221
0
  size_t i;
1222
0
  const u8 *pos;
1223
1224
0
  if (val == NULL)
1225
0
    return;
1226
1227
0
  fprintf(f, "%s=", field);
1228
0
  pos = wpabuf_head(val);
1229
0
  for (i = 0; i < wpabuf_len(val); i++)
1230
0
    fprintf(f, "%02X", *pos++);
1231
0
  fprintf(f, "\n");
1232
0
}
1233
1234
1235
static void wpa_config_write_global(FILE *f, struct wpa_config *config)
1236
0
{
1237
#ifdef CONFIG_CTRL_IFACE
1238
  if (config->ctrl_interface)
1239
    fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
1240
  if (config->ctrl_interface_group)
1241
    fprintf(f, "ctrl_interface_group=%s\n",
1242
      config->ctrl_interface_group);
1243
#endif /* CONFIG_CTRL_IFACE */
1244
0
  if (config->eapol_version != DEFAULT_EAPOL_VERSION)
1245
0
    fprintf(f, "eapol_version=%d\n", config->eapol_version);
1246
0
  if (config->ap_scan != DEFAULT_AP_SCAN)
1247
0
    fprintf(f, "ap_scan=%d\n", config->ap_scan);
1248
0
  if (config->disable_scan_offload)
1249
0
    fprintf(f, "disable_scan_offload=%d\n",
1250
0
      config->disable_scan_offload);
1251
0
  if (config->fast_reauth != DEFAULT_FAST_REAUTH)
1252
0
    fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
1253
0
#ifndef CONFIG_OPENSC_ENGINE_PATH
1254
0
  if (config->opensc_engine_path)
1255
0
    fprintf(f, "opensc_engine_path=%s\n",
1256
0
      config->opensc_engine_path);
1257
0
#endif /* CONFIG_OPENSC_ENGINE_PATH */
1258
0
#ifndef CONFIG_PKCS11_ENGINE_PATH
1259
0
  if (config->pkcs11_engine_path)
1260
0
    fprintf(f, "pkcs11_engine_path=%s\n",
1261
0
      config->pkcs11_engine_path);
1262
0
#endif /* CONFIG_PKCS11_ENGINE_PATH */
1263
0
#ifndef CONFIG_PKCS11_MODULE_PATH
1264
0
  if (config->pkcs11_module_path)
1265
0
    fprintf(f, "pkcs11_module_path=%s\n",
1266
0
      config->pkcs11_module_path);
1267
0
#endif /* CONFIG_PKCS11_MODULE_PATH */
1268
0
  if (config->openssl_ciphers)
1269
0
    fprintf(f, "openssl_ciphers=%s\n", config->openssl_ciphers);
1270
0
  if (config->pcsc_reader)
1271
0
    fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
1272
0
  if (config->pcsc_pin)
1273
0
    fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
1274
0
  if (config->driver_param)
1275
0
    fprintf(f, "driver_param=%s\n", config->driver_param);
1276
0
  if (config->dot11RSNAConfigPMKLifetime)
1277
0
    fprintf(f, "dot11RSNAConfigPMKLifetime=%u\n",
1278
0
      config->dot11RSNAConfigPMKLifetime);
1279
0
  if (config->dot11RSNAConfigPMKReauthThreshold)
1280
0
    fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%u\n",
1281
0
      config->dot11RSNAConfigPMKReauthThreshold);
1282
0
  if (config->dot11RSNAConfigSATimeout)
1283
0
    fprintf(f, "dot11RSNAConfigSATimeout=%u\n",
1284
0
      config->dot11RSNAConfigSATimeout);
1285
0
  if (config->update_config)
1286
0
    fprintf(f, "update_config=%d\n", config->update_config);
1287
#ifdef CONFIG_WPS
1288
  if (!is_nil_uuid(config->uuid)) {
1289
    char buf[40];
1290
    uuid_bin2str(config->uuid, buf, sizeof(buf));
1291
    fprintf(f, "uuid=%s\n", buf);
1292
  }
1293
  if (config->auto_uuid)
1294
    fprintf(f, "auto_uuid=%d\n", config->auto_uuid);
1295
  if (config->device_name)
1296
    fprintf(f, "device_name=%s\n", config->device_name);
1297
  if (config->manufacturer)
1298
    fprintf(f, "manufacturer=%s\n", config->manufacturer);
1299
  if (config->model_name)
1300
    fprintf(f, "model_name=%s\n", config->model_name);
1301
  if (config->model_number)
1302
    fprintf(f, "model_number=%s\n", config->model_number);
1303
  if (config->serial_number)
1304
    fprintf(f, "serial_number=%s\n", config->serial_number);
1305
  {
1306
    char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
1307
    buf = wps_dev_type_bin2str(config->device_type,
1308
             _buf, sizeof(_buf));
1309
    if (os_strcmp(buf, "0-00000000-0") != 0)
1310
      fprintf(f, "device_type=%s\n", buf);
1311
  }
1312
  if (WPA_GET_BE32(config->os_version))
1313
    fprintf(f, "os_version=%08x\n",
1314
      WPA_GET_BE32(config->os_version));
1315
  if (config->config_methods)
1316
    fprintf(f, "config_methods=%s\n", config->config_methods);
1317
  if (config->wps_cred_processing)
1318
    fprintf(f, "wps_cred_processing=%d\n",
1319
      config->wps_cred_processing);
1320
  if (config->wps_cred_add_sae)
1321
    fprintf(f, "wps_cred_add_sae=%d\n",
1322
      config->wps_cred_add_sae);
1323
  if (config->wps_vendor_ext_m1) {
1324
    int i, len = wpabuf_len(config->wps_vendor_ext_m1);
1325
    const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
1326
    if (len > 0) {
1327
      fprintf(f, "wps_vendor_ext_m1=");
1328
      for (i = 0; i < len; i++)
1329
        fprintf(f, "%02x", *p++);
1330
      fprintf(f, "\n");
1331
    }
1332
  }
1333
#endif /* CONFIG_WPS */
1334
#ifdef CONFIG_P2P
1335
  {
1336
    int i;
1337
    char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
1338
1339
    for (i = 0; i < config->num_sec_device_types; i++) {
1340
      buf = wps_dev_type_bin2str(config->sec_device_type[i],
1341
               _buf, sizeof(_buf));
1342
      if (buf)
1343
        fprintf(f, "sec_device_type=%s\n", buf);
1344
    }
1345
  }
1346
  if (config->p2p_listen_reg_class)
1347
    fprintf(f, "p2p_listen_reg_class=%d\n",
1348
      config->p2p_listen_reg_class);
1349
  if (config->p2p_listen_channel)
1350
    fprintf(f, "p2p_listen_channel=%d\n",
1351
      config->p2p_listen_channel);
1352
  if (config->p2p_oper_reg_class)
1353
    fprintf(f, "p2p_oper_reg_class=%d\n",
1354
      config->p2p_oper_reg_class);
1355
  if (config->p2p_oper_channel)
1356
    fprintf(f, "p2p_oper_channel=%d\n", config->p2p_oper_channel);
1357
  if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
1358
    fprintf(f, "p2p_go_intent=%d\n", config->p2p_go_intent);
1359
  if (config->p2p_ssid_postfix)
1360
    fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
1361
  if (config->persistent_reconnect)
1362
    fprintf(f, "persistent_reconnect=%d\n",
1363
      config->persistent_reconnect);
1364
  if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
1365
    fprintf(f, "p2p_intra_bss=%d\n", config->p2p_intra_bss);
1366
  if (config->p2p_group_idle)
1367
    fprintf(f, "p2p_group_idle=%d\n", config->p2p_group_idle);
1368
  if (config->p2p_passphrase_len)
1369
    fprintf(f, "p2p_passphrase_len=%u\n",
1370
      config->p2p_passphrase_len);
1371
  if (config->p2p_pref_chan) {
1372
    unsigned int i;
1373
    fprintf(f, "p2p_pref_chan=");
1374
    for (i = 0; i < config->num_p2p_pref_chan; i++) {
1375
      fprintf(f, "%s%u:%u", i > 0 ? "," : "",
1376
        config->p2p_pref_chan[i].op_class,
1377
        config->p2p_pref_chan[i].chan);
1378
    }
1379
    fprintf(f, "\n");
1380
  }
1381
  if (config->p2p_no_go_freq.num) {
1382
    char *val = freq_range_list_str(&config->p2p_no_go_freq);
1383
    if (val) {
1384
      fprintf(f, "p2p_no_go_freq=%s\n", val);
1385
      os_free(val);
1386
    }
1387
  }
1388
  if (config->p2p_add_cli_chan)
1389
    fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
1390
  if (config->p2p_optimize_listen_chan !=
1391
      DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN)
1392
    fprintf(f, "p2p_optimize_listen_chan=%d\n",
1393
      config->p2p_optimize_listen_chan);
1394
  if (config->p2p_go_ht40)
1395
    fprintf(f, "p2p_go_ht40=%d\n", config->p2p_go_ht40);
1396
  if (config->p2p_go_vht)
1397
    fprintf(f, "p2p_go_vht=%d\n", config->p2p_go_vht);
1398
  if (config->p2p_go_he)
1399
    fprintf(f, "p2p_go_he=%d\n", config->p2p_go_he);
1400
  if (config->p2p_go_edmg)
1401
    fprintf(f, "p2p_go_edmg=%d\n", config->p2p_go_edmg);
1402
  if (config->p2p_go_ctwindow != DEFAULT_P2P_GO_CTWINDOW)
1403
    fprintf(f, "p2p_go_ctwindow=%d\n", config->p2p_go_ctwindow);
1404
  if (config->p2p_disabled)
1405
    fprintf(f, "p2p_disabled=%d\n", config->p2p_disabled);
1406
  if (config->p2p_no_group_iface)
1407
    fprintf(f, "p2p_no_group_iface=%d\n",
1408
      config->p2p_no_group_iface);
1409
  if (config->p2p_ignore_shared_freq)
1410
    fprintf(f, "p2p_ignore_shared_freq=%d\n",
1411
      config->p2p_ignore_shared_freq);
1412
  if (config->p2p_cli_probe)
1413
    fprintf(f, "p2p_cli_probe=%d\n", config->p2p_cli_probe);
1414
  if (config->p2p_go_freq_change_policy != DEFAULT_P2P_GO_FREQ_MOVE)
1415
    fprintf(f, "p2p_go_freq_change_policy=%u\n",
1416
      config->p2p_go_freq_change_policy);
1417
1418
  if (config->p2p_6ghz_disable)
1419
    fprintf(f, "p2p_6ghz_disable=%d\n", config->p2p_6ghz_disable);
1420
1421
  if (config->p2p_bootstrap_methods)
1422
    fprintf(f, "p2p_bootstrap_methods=%d\n",
1423
      config->p2p_bootstrap_methods);
1424
  if (config->p2p_pasn_type)
1425
    fprintf(f, "p2p_pasn_type=%d\n", config->p2p_pasn_type);
1426
  if (config->p2p_comeback_after)
1427
    fprintf(f, "p2p_comeback_after=%d\n",
1428
      config->p2p_comeback_after);
1429
  if (config->p2p_twt_power_mgmt)
1430
    fprintf(f, "p2p_twt_power_mgmt=%d\n",
1431
      config->p2p_twt_power_mgmt);
1432
  if (config->p2p_chan_switch_req_enable)
1433
    fprintf(f, "p2p_chan_switch_req_enable=%d\n",
1434
      config->p2p_chan_switch_req_enable);
1435
  if (config->p2p_reg_info)
1436
    fprintf(f, "p2p_reg_info=%d\n", config->p2p_reg_info);
1437
1438
  if (WPA_GET_BE32(config->ip_addr_go))
1439
    fprintf(f, "ip_addr_go=%u.%u.%u.%u\n",
1440
      config->ip_addr_go[0], config->ip_addr_go[1],
1441
      config->ip_addr_go[2], config->ip_addr_go[3]);
1442
  if (WPA_GET_BE32(config->ip_addr_mask))
1443
    fprintf(f, "ip_addr_mask=%u.%u.%u.%u\n",
1444
      config->ip_addr_mask[0], config->ip_addr_mask[1],
1445
      config->ip_addr_mask[2], config->ip_addr_mask[3]);
1446
  if (WPA_GET_BE32(config->ip_addr_start))
1447
    fprintf(f, "ip_addr_start=%u.%u.%u.%u\n",
1448
      config->ip_addr_start[0], config->ip_addr_start[1],
1449
      config->ip_addr_start[2], config->ip_addr_start[3]);
1450
  if (WPA_GET_BE32(config->ip_addr_end))
1451
    fprintf(f, "ip_addr_end=%u.%u.%u.%u\n",
1452
      config->ip_addr_end[0], config->ip_addr_end[1],
1453
      config->ip_addr_end[2], config->ip_addr_end[3]);
1454
#endif /* CONFIG_P2P */
1455
0
  if (config->country[0] && config->country[1]) {
1456
0
    fprintf(f, "country=%c%c\n",
1457
0
      config->country[0], config->country[1]);
1458
0
  }
1459
0
  if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
1460
0
    fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
1461
0
  if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
1462
0
    fprintf(f, "bss_expiration_age=%u\n",
1463
0
      config->bss_expiration_age);
1464
0
  if (config->bss_expiration_scan_count !=
1465
0
      DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
1466
0
    fprintf(f, "bss_expiration_scan_count=%u\n",
1467
0
      config->bss_expiration_scan_count);
1468
0
  if (config->filter_ssids)
1469
0
    fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
1470
0
  if (config->filter_rssi)
1471
0
    fprintf(f, "filter_rssi=%d\n", config->filter_rssi);
1472
0
  if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
1473
0
    fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
1474
0
  if (config->ap_isolate != DEFAULT_AP_ISOLATE)
1475
0
    fprintf(f, "ap_isolate=%u\n", config->ap_isolate);
1476
0
  if (config->disassoc_low_ack)
1477
0
    fprintf(f, "disassoc_low_ack=%d\n", config->disassoc_low_ack);
1478
0
#ifdef CONFIG_HS20
1479
0
  if (config->hs20)
1480
0
    fprintf(f, "hs20=1\n");
1481
0
#endif /* CONFIG_HS20 */
1482
0
#ifdef CONFIG_INTERWORKING
1483
0
  if (config->interworking)
1484
0
    fprintf(f, "interworking=%d\n", config->interworking);
1485
0
  if (!is_zero_ether_addr(config->hessid))
1486
0
    fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
1487
0
  if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
1488
0
    fprintf(f, "access_network_type=%d\n",
1489
0
      config->access_network_type);
1490
0
  if (config->go_interworking)
1491
0
    fprintf(f, "go_interworking=%d\n", config->go_interworking);
1492
0
  if (config->go_access_network_type)
1493
0
    fprintf(f, "go_access_network_type=%d\n",
1494
0
      config->go_access_network_type);
1495
0
  if (config->go_internet)
1496
0
    fprintf(f, "go_internet=%d\n", config->go_internet);
1497
0
  if (config->go_venue_group)
1498
0
    fprintf(f, "go_venue_group=%d\n", config->go_venue_group);
1499
0
  if (config->go_venue_type)
1500
0
    fprintf(f, "go_venue_type=%d\n", config->go_venue_type);
1501
0
#endif /* CONFIG_INTERWORKING */
1502
0
  if (config->pbc_in_m1)
1503
0
    fprintf(f, "pbc_in_m1=%d\n", config->pbc_in_m1);
1504
0
  if (config->wps_nfc_pw_from_config) {
1505
0
    if (config->wps_nfc_dev_pw_id)
1506
0
      fprintf(f, "wps_nfc_dev_pw_id=%d\n",
1507
0
        config->wps_nfc_dev_pw_id);
1508
0
    write_global_bin(f, "wps_nfc_dh_pubkey",
1509
0
         config->wps_nfc_dh_pubkey);
1510
0
    write_global_bin(f, "wps_nfc_dh_privkey",
1511
0
         config->wps_nfc_dh_privkey);
1512
0
    write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
1513
0
  }
1514
1515
0
  if (config->ext_password_backend)
1516
0
    fprintf(f, "ext_password_backend=%s\n",
1517
0
      config->ext_password_backend);
1518
0
  if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
1519
0
    fprintf(f, "p2p_go_max_inactivity=%d\n",
1520
0
      config->p2p_go_max_inactivity);
1521
0
  if (config->auto_interworking)
1522
0
    fprintf(f, "auto_interworking=%d\n",
1523
0
      config->auto_interworking);
1524
0
  if (config->okc)
1525
0
    fprintf(f, "okc=%d\n", config->okc);
1526
0
  if (config->pmf)
1527
0
    fprintf(f, "pmf=%d\n", config->pmf);
1528
0
  if (config->dtim_period)
1529
0
    fprintf(f, "dtim_period=%d\n", config->dtim_period);
1530
0
  if (config->beacon_int)
1531
0
    fprintf(f, "beacon_int=%d\n", config->beacon_int);
1532
1533
0
  if (config->sae_check_mfp)
1534
0
    fprintf(f, "sae_check_mfp=%d\n", config->sae_check_mfp);
1535
1536
0
  if (config->sae_groups) {
1537
0
    int i;
1538
0
    fprintf(f, "sae_groups=");
1539
0
    for (i = 0; config->sae_groups[i] > 0; i++) {
1540
0
      fprintf(f, "%s%d", i > 0 ? " " : "",
1541
0
        config->sae_groups[i]);
1542
0
    }
1543
0
    fprintf(f, "\n");
1544
0
  }
1545
1546
0
  if (config->sae_pwe)
1547
0
    fprintf(f, "sae_pwe=%d\n", config->sae_pwe);
1548
1549
0
  if (config->sae_pmkid_in_assoc)
1550
0
    fprintf(f, "sae_pmkid_in_assoc=%d\n",
1551
0
      config->sae_pmkid_in_assoc);
1552
1553
0
  if (config->ap_vendor_elements) {
1554
0
    int i, len = wpabuf_len(config->ap_vendor_elements);
1555
0
    const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
1556
0
    if (len > 0) {
1557
0
      fprintf(f, "ap_vendor_elements=");
1558
0
      for (i = 0; i < len; i++)
1559
0
        fprintf(f, "%02x", *p++);
1560
0
      fprintf(f, "\n");
1561
0
    }
1562
0
  }
1563
1564
0
  if (config->ap_assocresp_elements) {
1565
0
    int i, len = wpabuf_len(config->ap_assocresp_elements);
1566
0
    const u8 *p = wpabuf_head_u8(config->ap_assocresp_elements);
1567
1568
0
    if (len > 0) {
1569
0
      fprintf(f, "ap_assocresp_elements=");
1570
0
      for (i = 0; i < len; i++)
1571
0
        fprintf(f, "%02x", *p++);
1572
0
      fprintf(f, "\n");
1573
0
    }
1574
0
  }
1575
1576
0
  if (config->ignore_old_scan_res)
1577
0
    fprintf(f, "ignore_old_scan_res=%d\n",
1578
0
      config->ignore_old_scan_res);
1579
1580
0
  if (config->freq_list && config->freq_list[0]) {
1581
0
    int i;
1582
0
    fprintf(f, "freq_list=");
1583
0
    for (i = 0; config->freq_list[i]; i++) {
1584
0
      fprintf(f, "%s%d", i > 0 ? " " : "",
1585
0
        config->freq_list[i]);
1586
0
    }
1587
0
    fprintf(f, "\n");
1588
0
  }
1589
0
  if (config->initial_freq_list && config->initial_freq_list[0]) {
1590
0
    int i;
1591
0
    fprintf(f, "initial_freq_list=");
1592
0
    for (i = 0; config->initial_freq_list[i]; i++) {
1593
0
      fprintf(f, "%s%d", i > 0 ? " " : "",
1594
0
        config->initial_freq_list[i]);
1595
0
    }
1596
0
    fprintf(f, "\n");
1597
0
  }
1598
0
  if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
1599
0
    fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
1600
1601
0
  if (config->scan_res_valid_for_connect !=
1602
0
      DEFAULT_SCAN_RES_VALID_FOR_CONNECT)
1603
0
    fprintf(f, "scan_res_valid_for_connect=%d\n",
1604
0
      config->scan_res_valid_for_connect);
1605
1606
0
  if (config->sched_scan_interval)
1607
0
    fprintf(f, "sched_scan_interval=%u\n",
1608
0
      config->sched_scan_interval);
1609
1610
0
  if (config->sched_scan_start_delay)
1611
0
    fprintf(f, "sched_scan_start_delay=%u\n",
1612
0
      config->sched_scan_start_delay);
1613
1614
0
  if (config->external_sim)
1615
0
    fprintf(f, "external_sim=%d\n", config->external_sim);
1616
1617
0
  if (config->tdls_external_control)
1618
0
    fprintf(f, "tdls_external_control=%d\n",
1619
0
      config->tdls_external_control);
1620
1621
0
  if (config->wowlan_triggers)
1622
0
    fprintf(f, "wowlan_triggers=%s\n",
1623
0
      config->wowlan_triggers);
1624
1625
0
  if (config->bgscan)
1626
0
    fprintf(f, "bgscan=\"%s\"\n", config->bgscan);
1627
1628
0
  if (config->autoscan)
1629
0
    fprintf(f, "autoscan=%s\n", config->autoscan);
1630
1631
0
  if (config->p2p_search_delay != DEFAULT_P2P_SEARCH_DELAY)
1632
0
    fprintf(f, "p2p_search_delay=%u\n",
1633
0
      config->p2p_search_delay);
1634
1635
0
  if (config->mac_addr)
1636
0
    fprintf(f, "mac_addr=%d\n", config->mac_addr);
1637
1638
0
  if (config->rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
1639
0
    fprintf(f, "rand_addr_lifetime=%u\n",
1640
0
      config->rand_addr_lifetime);
1641
1642
0
  if (config->preassoc_mac_addr)
1643
0
    fprintf(f, "preassoc_mac_addr=%d\n", config->preassoc_mac_addr);
1644
1645
0
  if (config->key_mgmt_offload != DEFAULT_KEY_MGMT_OFFLOAD)
1646
0
    fprintf(f, "key_mgmt_offload=%d\n", config->key_mgmt_offload);
1647
1648
0
  if (config->user_mpm != DEFAULT_USER_MPM)
1649
0
    fprintf(f, "user_mpm=%d\n", config->user_mpm);
1650
1651
0
  if (config->max_peer_links != DEFAULT_MAX_PEER_LINKS)
1652
0
    fprintf(f, "max_peer_links=%d\n", config->max_peer_links);
1653
1654
0
  if (config->cert_in_cb != DEFAULT_CERT_IN_CB)
1655
0
    fprintf(f, "cert_in_cb=%d\n", config->cert_in_cb);
1656
1657
0
  if (config->mesh_max_inactivity != DEFAULT_MESH_MAX_INACTIVITY)
1658
0
    fprintf(f, "mesh_max_inactivity=%d\n",
1659
0
      config->mesh_max_inactivity);
1660
1661
0
  if (config->mesh_fwding != DEFAULT_MESH_FWDING)
1662
0
    fprintf(f, "mesh_fwding=%d\n", config->mesh_fwding);
1663
1664
0
  if (config->dot11RSNASAERetransPeriod !=
1665
0
      DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD)
1666
0
    fprintf(f, "dot11RSNASAERetransPeriod=%d\n",
1667
0
      config->dot11RSNASAERetransPeriod);
1668
1669
0
  if (config->passive_scan)
1670
0
    fprintf(f, "passive_scan=%d\n", config->passive_scan);
1671
1672
0
  if (config->reassoc_same_bss_optim)
1673
0
    fprintf(f, "reassoc_same_bss_optim=%d\n",
1674
0
      config->reassoc_same_bss_optim);
1675
1676
0
  if (config->wps_priority)
1677
0
    fprintf(f, "wps_priority=%d\n", config->wps_priority);
1678
1679
0
  if (config->wpa_rsc_relaxation != DEFAULT_WPA_RSC_RELAXATION)
1680
0
    fprintf(f, "wpa_rsc_relaxation=%d\n",
1681
0
      config->wpa_rsc_relaxation);
1682
1683
0
  if (config->sched_scan_plans)
1684
0
    fprintf(f, "sched_scan_plans=%s\n", config->sched_scan_plans);
1685
1686
#ifdef CONFIG_MBO
1687
  if (config->non_pref_chan)
1688
    fprintf(f, "non_pref_chan=%s\n", config->non_pref_chan);
1689
  if (config->mbo_cell_capa != DEFAULT_MBO_CELL_CAPA)
1690
    fprintf(f, "mbo_cell_capa=%u\n", config->mbo_cell_capa);
1691
  if (config->disassoc_imminent_rssi_threshold !=
1692
      DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD)
1693
    fprintf(f, "disassoc_imminent_rssi_threshold=%d\n",
1694
      config->disassoc_imminent_rssi_threshold);
1695
  if (config->oce != DEFAULT_OCE_SUPPORT)
1696
    fprintf(f, "oce=%u\n", config->oce);
1697
#endif /* CONFIG_MBO */
1698
1699
0
  if (config->gas_address3)
1700
0
    fprintf(f, "gas_address3=%d\n", config->gas_address3);
1701
1702
0
  if (config->ftm_responder)
1703
0
    fprintf(f, "ftm_responder=%d\n", config->ftm_responder);
1704
0
  if (config->ftm_initiator)
1705
0
    fprintf(f, "ftm_initiator=%d\n", config->ftm_initiator);
1706
1707
0
  if (config->twt_requester)
1708
0
    fprintf(f, "twt_requester=%d\n", config->twt_requester);
1709
1710
0
  if (config->fst_group_id)
1711
0
    fprintf(f, "fst_group_id=%s\n", config->fst_group_id);
1712
0
  if (config->fst_priority)
1713
0
    fprintf(f, "fst_priority=%d\n", config->fst_priority);
1714
0
  if (config->fst_llt)
1715
0
    fprintf(f, "fst_llt=%d\n", config->fst_llt);
1716
1717
0
  if (config->gas_rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
1718
0
    fprintf(f, "gas_rand_addr_lifetime=%u\n",
1719
0
      config->gas_rand_addr_lifetime);
1720
0
  if (config->gas_rand_mac_addr)
1721
0
    fprintf(f, "gas_rand_mac_addr=%d\n", config->gas_rand_mac_addr);
1722
0
  if (config->dpp_config_processing)
1723
0
    fprintf(f, "dpp_config_processing=%d\n",
1724
0
      config->dpp_config_processing);
1725
0
  if (config->dpp_name)
1726
0
    fprintf(f, "dpp_name=%s\n", config->dpp_name);
1727
0
  if (config->dpp_mud_url)
1728
0
    fprintf(f, "dpp_mud_url=%s\n", config->dpp_mud_url);
1729
0
  if (config->dpp_extra_conf_req_name)
1730
0
    fprintf(f, "dpp_extra_conf_req_name=%s\n",
1731
0
      config->dpp_extra_conf_req_name);
1732
0
  if (config->dpp_extra_conf_req_value)
1733
0
    fprintf(f, "dpp_extra_conf_req_value=%s\n",
1734
0
      config->dpp_extra_conf_req_value);
1735
0
  if (config->dpp_connector_privacy_default)
1736
0
    fprintf(f, "dpp_connector_privacy_default=%d\n",
1737
0
      config->dpp_connector_privacy_default);
1738
0
  if (config->coloc_intf_reporting)
1739
0
    fprintf(f, "coloc_intf_reporting=%d\n",
1740
0
      config->coloc_intf_reporting);
1741
0
  if (config->p2p_device_random_mac_addr)
1742
0
    fprintf(f, "p2p_device_random_mac_addr=%d\n",
1743
0
      config->p2p_device_random_mac_addr);
1744
0
  if (!is_zero_ether_addr(config->p2p_device_persistent_mac_addr))
1745
0
    fprintf(f, "p2p_device_persistent_mac_addr=" MACSTR "\n",
1746
0
      MAC2STR(config->p2p_device_persistent_mac_addr));
1747
0
  if (config->p2p_interface_random_mac_addr)
1748
0
    fprintf(f, "p2p_interface_random_mac_addr=%d\n",
1749
0
      config->p2p_interface_random_mac_addr);
1750
0
  if (config->disable_btm)
1751
0
    fprintf(f, "disable_btm=1\n");
1752
0
  if (config->extended_key_id != DEFAULT_EXTENDED_KEY_ID)
1753
0
    fprintf(f, "extended_key_id=%d\n",
1754
0
      config->extended_key_id);
1755
0
  if (config->wowlan_disconnect_on_deinit)
1756
0
    fprintf(f, "wowlan_disconnect_on_deinit=%d\n",
1757
0
      config->wowlan_disconnect_on_deinit);
1758
0
  if (config->rsn_overriding)
1759
0
    fprintf(f, "rsn_overriding=%d\n", config->rsn_overriding);
1760
#ifdef CONFIG_TESTING_OPTIONS
1761
  if (config->mld_force_single_link)
1762
    fprintf(f, "mld_force_single_link=1\n");
1763
  if (config->mld_connect_band_pref != MLD_CONNECT_BAND_PREF_AUTO)
1764
    fprintf(f, "mld_connect_band_pref=%d\n",
1765
      config->mld_connect_band_pref);
1766
  if (!is_zero_ether_addr(config->mld_connect_bssid_pref))
1767
    fprintf(f, "mld_connect_bssid_pref=" MACSTR "\n",
1768
      MAC2STR(config->mld_connect_bssid_pref));
1769
#endif /* CONFIG_TESTING_OPTIONS */
1770
0
  if (config->ft_prepend_pmkid)
1771
0
    fprintf(f, "ft_prepend_pmkid=%d\n", config->ft_prepend_pmkid);
1772
0
  if (config->dik) {
1773
0
    fprintf(f, "dik_cipher=%d\n", config->dik_cipher);
1774
0
    write_global_bin(f, "dik", config->dik);
1775
0
  }
1776
0
  if (config->wfa_gen_capa)
1777
0
    fprintf(f, "wfa_gen_capa=%d\n", config->wfa_gen_capa);
1778
0
  write_global_bin(f, "wfa_gen_capa_supp", config->wfa_gen_capa_supp);
1779
0
  write_global_bin(f, "wfa_gen_capa_cert", config->wfa_gen_capa_cert);
1780
0
  if (config->disable_op_classes_80_80_mhz)
1781
0
    fprintf(f, "disable_op_classes_80_80_mhz=%d\n",
1782
0
      config->disable_op_classes_80_80_mhz);
1783
0
  if (config->pr_pasn_type)
1784
0
    fprintf(f, "pr_pasn_type=%d\n", config->pr_pasn_type);
1785
0
  if (config->pr_preferred_role)
1786
0
    fprintf(f, "pr_preferred_role=%d\n", config->pr_preferred_role);
1787
0
}
1788
1789
static void wpa_config_write_identity(FILE *f, struct wpa_dev_ik *dev_ik)
1790
0
{
1791
0
  fprintf(f, "\tdik_cipher=%d\n", dev_ik->dik_cipher);
1792
1793
0
  if (dev_ik->dik)
1794
0
    write_global_bin(f, "\tdik", dev_ik->dik);
1795
1796
0
  if (dev_ik->pmk)
1797
0
    write_global_bin(f, "\tpmk", dev_ik->pmk);
1798
1799
0
  if (dev_ik->pmkid)
1800
0
    write_global_bin(f, "\tpmkid", dev_ik->pmkid);
1801
0
}
1802
1803
#endif /* CONFIG_NO_CONFIG_WRITE */
1804
1805
1806
int wpa_config_write(const char *name, struct wpa_config *config)
1807
0
{
1808
0
#ifndef CONFIG_NO_CONFIG_WRITE
1809
0
  FILE *f;
1810
0
  struct wpa_ssid *ssid;
1811
0
  struct wpa_cred *cred;
1812
0
  struct wpa_dev_ik *dev_ik;
1813
0
#ifndef CONFIG_NO_CONFIG_BLOBS
1814
0
  struct wpa_config_blob *blob;
1815
0
#endif /* CONFIG_NO_CONFIG_BLOBS */
1816
0
  int ret = 0;
1817
0
  const char *orig_name = name;
1818
0
  int tmp_len;
1819
0
  char *tmp_name;
1820
1821
0
  if (!name) {
1822
0
    wpa_printf(MSG_ERROR, "No configuration file for writing");
1823
0
    return -1;
1824
0
  }
1825
1826
0
  tmp_len = os_strlen(name) + 5; /* allow space for .tmp suffix */
1827
0
  tmp_name = os_malloc(tmp_len);
1828
0
  if (tmp_name) {
1829
0
    os_snprintf(tmp_name, tmp_len, "%s.tmp", name);
1830
0
    name = tmp_name;
1831
0
  }
1832
1833
0
  wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
1834
1835
0
  f = fopen(name, "w");
1836
0
  if (f == NULL) {
1837
0
    wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
1838
0
    os_free(tmp_name);
1839
0
    return -1;
1840
0
  }
1841
1842
0
  wpa_config_write_global(f, config);
1843
1844
0
  for (cred = config->cred; cred; cred = cred->next) {
1845
0
    if (cred->temporary)
1846
0
      continue;
1847
0
    fprintf(f, "\ncred={\n");
1848
0
    wpa_config_write_cred(f, cred);
1849
0
    fprintf(f, "}\n");
1850
0
  }
1851
1852
0
  for (ssid = config->ssid; ssid; ssid = ssid->next) {
1853
0
    if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary ||
1854
0
        ssid->ro)
1855
0
      continue; /* do not save temporary networks */
1856
0
    if (wpa_key_mgmt_wpa_psk_no_sae(ssid->key_mgmt) &&
1857
0
        !ssid->psk_set && !ssid->passphrase)
1858
0
      continue; /* do not save invalid network */
1859
0
    if (wpa_key_mgmt_sae(ssid->key_mgmt) &&
1860
0
        !ssid->passphrase && !ssid->sae_password &&
1861
0
        !ssid->pmk_valid)
1862
0
      continue; /* do not save invalid network */
1863
0
    fprintf(f, "\nnetwork={\n");
1864
0
    wpa_config_write_network(f, ssid);
1865
0
    fprintf(f, "}\n");
1866
0
  }
1867
1868
0
  for (dev_ik = config->identity; dev_ik; dev_ik = dev_ik->next) {
1869
0
    fprintf(f, "\nidentity={\n");
1870
0
    wpa_config_write_identity(f, dev_ik);
1871
0
    fprintf(f, "}\n");
1872
0
  }
1873
1874
0
#ifndef CONFIG_NO_CONFIG_BLOBS
1875
0
  for (blob = config->blobs; blob; blob = blob->next) {
1876
0
    ret = wpa_config_write_blob(f, blob);
1877
0
    if (ret)
1878
0
      break;
1879
0
  }
1880
0
#endif /* CONFIG_NO_CONFIG_BLOBS */
1881
1882
0
  os_fdatasync(f);
1883
1884
0
  fclose(f);
1885
1886
0
  if (tmp_name) {
1887
0
    int chmod_ret = 0;
1888
1889
#ifdef ANDROID
1890
    chmod_ret = chmod(tmp_name,
1891
          S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1892
#endif /* ANDROID */
1893
0
    if (chmod_ret != 0 || rename(tmp_name, orig_name) != 0)
1894
0
      ret = -1;
1895
1896
0
    os_free(tmp_name);
1897
0
  }
1898
1899
0
  wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
1900
0
       orig_name, ret ? "un" : "");
1901
0
  return ret;
1902
#else /* CONFIG_NO_CONFIG_WRITE */
1903
  return -1;
1904
#endif /* CONFIG_NO_CONFIG_WRITE */
1905
0
}