~ubuntu-branches/ubuntu/vivid/wpasupplicant/vivid

« back to all changes in this revision

Viewing changes to config_winreg.c

  • Committer: Bazaar Package Importer
  • Author(s): Kel Modderman
  • Date: 2006-10-05 08:04:01 UTC
  • mfrom: (1.1.5 upstream) (3 etch)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20061005080401-r8lqlix4390yos7b
Tags: 0.5.5-2
* Update madwifi headers to latest SVN. (Closes: #388316)
* Remove failed attempt at action locking. [debian/functions.sh,
  debian/wpa_action.sh]
* Add hysteresis checking functions, to avoid "event loops" while
  using wpa-roam. [debian/functions.sh, debian/wpa_action.sh]
* Change of co-maintainer email address.
* Add ishex() function to functions.sh to determine wpa-psk value type in
  plaintext or hex. This effectively eliminates the need for the bogus and
  somewhat confusing wpa-passphrase contruct specific to our scripts and
  allows wpa-psk to work with either a 8 to 63 character long plaintext
  string or 64 character long hex string.
* Adjust README.modes to not refer to the redundant wpa-passphrase stuff.
* Add big fat NOTE about acceptable wpa-psk's to top of example gallery.
* Strip surrounding quotes from wpa-ssid if present, instead of just whining
  about them.
* Update email address in copyright blurb of functions.sh, ifupdown.sh and
  wpa_action.sh.  

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "config.h"
38
38
 
39
39
#define KEY_ROOT HKEY_LOCAL_MACHINE
40
 
#define KEY_PREFIX "SOFTWARE\\wpa_supplicant"
 
40
#define KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
 
41
 
 
42
#ifdef UNICODE
 
43
#define TSTR "%S"
 
44
#else /* UNICODE */
 
45
#define TSTR "%s"
 
46
#endif /* UNICODE */
41
47
 
42
48
 
43
49
static int wpa_config_read_blobs(struct wpa_config *config, HKEY hk)
48
54
        LONG ret;
49
55
        DWORD i;
50
56
 
51
 
        ret = RegOpenKeyEx(hk, "blobs", 0, KEY_QUERY_VALUE, &bhk);
 
57
        ret = RegOpenKeyEx(hk, TEXT("blobs"), 0, KEY_QUERY_VALUE, &bhk);
52
58
        if (ret != ERROR_SUCCESS) {
53
59
                wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
54
60
                           "blobs key");
56
62
        }
57
63
 
58
64
        for (i = 0; ; i++) {
59
 
                char name[255], data[4096];
 
65
#define TNAMELEN 255
 
66
                TCHAR name[TNAMELEN];
 
67
                char data[4096];
60
68
                DWORD namelen, datalen, type;
61
69
 
62
 
                namelen = sizeof(name);
 
70
                namelen = TNAMELEN;
63
71
                datalen = sizeof(data);
64
72
                ret = RegEnumValue(bhk, i, name, &namelen, NULL, &type,
65
 
                                   data, &datalen);
 
73
                                   (LPBYTE) data, &datalen);
66
74
 
67
75
                if (ret == ERROR_NO_MORE_ITEMS)
68
76
                        break;
73
81
                        break;
74
82
                }
75
83
 
76
 
                if (namelen >= sizeof(name))
77
 
                        namelen = sizeof(name) - 1;
78
 
                name[namelen] = '\0';
 
84
                if (namelen >= TNAMELEN)
 
85
                        namelen = TNAMELEN - 1;
 
86
                name[namelen] = TEXT('\0');
 
87
                wpa_unicode2ascii_inplace(name);
79
88
 
80
89
                if (datalen >= sizeof(data))
81
90
                        datalen = sizeof(data) - 1;
82
 
                data[datalen] = '\0';
83
91
 
84
92
                wpa_printf(MSG_MSGDUMP, "blob %d: field='%s' len %d",
85
93
                           (int) i, name, (int) datalen);
89
97
                        errors++;
90
98
                        break;
91
99
                }
92
 
                blob->name = strdup(name);
 
100
                blob->name = strdup((char *) name);
93
101
                blob->data = malloc(datalen);
94
102
                if (blob->name == NULL || blob->data == NULL) {
95
103
                        wpa_config_free_blob(blob);
108
116
}
109
117
 
110
118
 
111
 
static int wpa_config_read_reg_dword(HKEY hk, const char *name, int *_val)
 
119
static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val)
112
120
{
113
121
        DWORD val, buflen;
114
122
        LONG ret;
125
133
}
126
134
 
127
135
 
128
 
static char * wpa_config_read_reg_string(HKEY hk, const char *name)
 
136
static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name)
129
137
{
130
138
        DWORD buflen;
131
139
        LONG ret;
132
 
        char *val;
 
140
        TCHAR *val;
133
141
 
134
142
        buflen = 0;
135
143
        ret = RegQueryValueEx(hk, name, NULL, NULL, NULL, &buflen);
136
144
        if (ret != ERROR_SUCCESS)
137
145
                return NULL;
138
 
        val = malloc(buflen + 1);
 
146
        val = malloc(buflen);
139
147
        if (val == NULL)
140
148
                return NULL;
141
 
        val[buflen] = '\0';
142
149
 
143
150
        ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen);
144
151
        if (ret != ERROR_SUCCESS) {
146
153
                return NULL;
147
154
        }
148
155
 
149
 
        wpa_printf(MSG_DEBUG, "%s=%s", name, val);
150
 
        return val;
 
156
        wpa_unicode2ascii_inplace(val);
 
157
        wpa_printf(MSG_DEBUG, "%s=%s", name, (char *) val);
 
158
        return (char *) val;
151
159
}
152
160
 
153
161
 
155
163
{
156
164
        int errors = 0;
157
165
 
158
 
        wpa_config_read_reg_dword(hk, "ap_scan", &config->ap_scan);
159
 
        wpa_config_read_reg_dword(hk, "fast_reauth", &config->fast_reauth);
160
 
        wpa_config_read_reg_dword(hk, "dot11RSNAConfigPMKLifetime",
 
166
        wpa_config_read_reg_dword(hk, TEXT("ap_scan"), &config->ap_scan);
 
167
        wpa_config_read_reg_dword(hk, TEXT("fast_reauth"),
 
168
                                  &config->fast_reauth);
 
169
        wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
161
170
                                  &config->dot11RSNAConfigPMKLifetime);
162
 
        wpa_config_read_reg_dword(hk, "dot11RSNAConfigPMKReauthThreshold",
 
171
        wpa_config_read_reg_dword(hk,
 
172
                                  TEXT("dot11RSNAConfigPMKReauthThreshold"),
163
173
                                  &config->dot11RSNAConfigPMKReauthThreshold);
164
 
        wpa_config_read_reg_dword(hk, "dot11RSNAConfigSATimeout",
 
174
        wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
165
175
                                  &config->dot11RSNAConfigSATimeout);
166
 
        wpa_config_read_reg_dword(hk, "update_config", &config->update_config);
 
176
        wpa_config_read_reg_dword(hk, TEXT("update_config"),
 
177
                                  &config->update_config);
167
178
 
168
 
        if (wpa_config_read_reg_dword(hk, "eapol_version",
 
179
        if (wpa_config_read_reg_dword(hk, TEXT("eapol_version"),
169
180
                                      &config->eapol_version) == 0) {
170
181
                if (config->eapol_version < 1 ||
171
182
                    config->eapol_version > 2) {
175
186
                }
176
187
        }
177
188
 
178
 
        config->ctrl_interface = wpa_config_read_reg_string(hk,
179
 
                                                            "ctrl_interface");
 
189
        config->ctrl_interface = wpa_config_read_reg_string(
 
190
                hk, TEXT("ctrl_interface"));
180
191
 
181
192
        return errors ? -1 : 0;
182
193
}
183
194
 
184
195
 
185
 
static struct wpa_ssid * wpa_config_read_network(HKEY hk, const char *netw,
 
196
static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
186
197
                                                 int id)
187
198
{
188
199
        HKEY nhk;
194
205
        ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
195
206
        if (ret != ERROR_SUCCESS) {
196
207
                wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
197
 
                           "network '%s'", netw);
 
208
                           "network '" TSTR "'", netw);
198
209
                return NULL;
199
210
        }
200
211
 
201
 
        wpa_printf(MSG_MSGDUMP, "Start of a new network '%s'", netw);
 
212
        wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
202
213
        ssid = wpa_zalloc(sizeof(*ssid));
203
214
        if (ssid == NULL) {
204
215
                RegCloseKey(nhk);
213
224
                DWORD namelen, datalen, type;
214
225
 
215
226
                namelen = 255;
216
 
                datalen = 1024;
 
227
                datalen = sizeof(data);
217
228
                ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
218
 
                                   data, &datalen);
 
229
                                   (LPBYTE) data, &datalen);
219
230
 
220
231
                if (ret == ERROR_NO_MORE_ITEMS)
221
232
                        break;
228
239
 
229
240
                if (namelen >= 255)
230
241
                        namelen = 255 - 1;
231
 
                name[namelen] = '\0';
 
242
                name[namelen] = TEXT('\0');
232
243
 
233
244
                if (datalen >= 1024)
234
245
                        datalen = 1024 - 1;
235
 
                data[datalen] = '\0';
 
246
                data[datalen] = TEXT('\0');
236
247
 
237
 
                if (wpa_config_set(ssid, name, data, 0) < 0)
 
248
                wpa_unicode2ascii_inplace(name);
 
249
                wpa_unicode2ascii_inplace(data);
 
250
                if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
238
251
                        errors++;
239
252
        }
240
253
 
243
256
        if (ssid->passphrase) {
244
257
                if (ssid->psk_set) {
245
258
                        wpa_printf(MSG_ERROR, "Both PSK and passphrase "
246
 
                                   "configured for network '%s'.", netw);
 
259
                                   "configured for network '" TSTR "'.", netw);
247
260
                        errors++;
248
261
                }
249
262
                wpa_config_update_psk(ssid);
251
264
 
252
265
        if ((ssid->key_mgmt & WPA_KEY_MGMT_PSK) && !ssid->psk_set) {
253
266
                wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
254
 
                           "but no PSK configured for network '%s'.", netw);
 
267
                           "but no PSK configured for network '" TSTR "'.",
 
268
                           netw);
255
269
                errors++;
256
270
        }
257
271
 
261
275
                /* Group cipher cannot be stronger than the pairwise cipher. */
262
276
                wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
263
277
                           "list since it was not allowed for pairwise "
264
 
                           "cipher for network '%s'.", netw);
 
278
                           "cipher for network '" TSTR "'.", netw);
265
279
                ssid->group_cipher &= ~WPA_CIPHER_CCMP;
266
280
        }
267
281
 
282
296
        LONG ret;
283
297
        DWORD i;
284
298
 
285
 
        ret = RegOpenKeyEx(hk, "networks", 0, KEY_ENUMERATE_SUB_KEYS, &nhk);
 
299
        ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
 
300
                           &nhk);
286
301
        if (ret != ERROR_SUCCESS) {
287
302
                wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
288
303
                           "registry key");
341
356
 
342
357
struct wpa_config * wpa_config_read(const char *name)
343
358
{
344
 
        char buf[256];
 
359
        TCHAR buf[256];
345
360
        int errors = 0;
346
361
        struct wpa_config *config;
347
362
        struct wpa_ssid *ssid;
354
369
                return NULL;
355
370
        wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
356
371
 
357
 
        snprintf(buf, sizeof(buf), KEY_PREFIX "\\configs\\%s", name);
 
372
#ifdef UNICODE
 
373
        _snwprintf(buf, 256, KEY_PREFIX TEXT("\\configs\\%S"), name);
 
374
#else /* UNICODE */
 
375
        snprintf(buf, 256, KEY_PREFIX TEXT("\\configs\\%s"), name);
 
376
#endif /* UNICODE */
 
377
 
358
378
        ret = RegOpenKeyEx(KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
359
379
        if (ret != ERROR_SUCCESS) {
360
380
                wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
395
415
}
396
416
 
397
417
 
398
 
static int wpa_config_write_reg_dword(HKEY hk, const char *name, int val,
 
418
static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
399
419
                                      int def)
400
420
{
401
421
        LONG ret;
422
442
                                       const char *val)
423
443
{
424
444
        LONG ret;
 
445
        TCHAR *_name, *_val;
 
446
 
 
447
        _name = wpa_strdup_tchar(name);
 
448
        if (_name == NULL)
 
449
                return -1;
425
450
 
426
451
        if (val == NULL) {
427
 
                RegDeleteValue(hk, name);
 
452
                RegDeleteValue(hk, _name);
 
453
                free(_name);
428
454
                return 0;
429
455
        }
430
456
 
431
 
        ret = RegSetValueEx(hk, name, 0, REG_SZ, val, strlen(val) + 1);
 
457
        _val = wpa_strdup_tchar(val);
 
458
        if (_val == NULL) {
 
459
                free(_name);
 
460
                return -1;
 
461
        }
 
462
        ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val, (strlen(val) + 1) * sizeof(TCHAR));
432
463
        if (ret != ERROR_SUCCESS) {
433
464
                wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
434
465
                           "error %d", name, val, (int) GetLastError());
 
466
                free(_name);
 
467
                free(_val);
435
468
                return -1;
436
469
        }
437
470
 
 
471
        free(_name);
 
472
        free(_val);
438
473
        return 0;
439
474
}
440
475
 
446
481
                                    config->ctrl_interface);
447
482
#endif /* CONFIG_CTRL_IFACE */
448
483
 
449
 
        wpa_config_write_reg_dword(hk, "eapol_version", config->eapol_version,
 
484
        wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
 
485
                                   config->eapol_version,
450
486
                                   DEFAULT_EAPOL_VERSION);
451
 
        wpa_config_write_reg_dword(hk, "ap_scan", config->ap_scan,
 
487
        wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
452
488
                                   DEFAULT_AP_SCAN);
453
 
        wpa_config_write_reg_dword(hk, "fast_reauth", config->fast_reauth,
454
 
                                   DEFAULT_FAST_REAUTH);
455
 
        wpa_config_write_reg_dword(hk, "dot11RSNAConfigPMKLifetime",
 
489
        wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
 
490
                                   config->fast_reauth, DEFAULT_FAST_REAUTH);
 
491
        wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
456
492
                                   config->dot11RSNAConfigPMKLifetime, 0);
457
 
        wpa_config_write_reg_dword(hk, "dot11RSNAConfigPMKReauthThreshold",
 
493
        wpa_config_write_reg_dword(hk,
 
494
                                   TEXT("dot11RSNAConfigPMKReauthThreshold"),
458
495
                                   config->dot11RSNAConfigPMKReauthThreshold,
459
496
                                   0);
460
 
        wpa_config_write_reg_dword(hk, "dot11RSNAConfigSATimeout",
 
497
        wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
461
498
                                   config->dot11RSNAConfigSATimeout, 0);
462
 
        wpa_config_write_reg_dword(hk, "update_config", config->update_config,
 
499
        wpa_config_write_reg_dword(hk, TEXT("update_config"),
 
500
                                   config->update_config,
463
501
                                   0);
464
502
 
465
503
        return 0;
466
504
}
467
505
 
468
506
 
469
 
static int wpa_config_delete_subkeys(HKEY hk, const char *key)
 
507
static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
470
508
{
471
509
        HKEY nhk;
472
510
        int i, errors = 0;
474
512
 
475
513
        ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
476
514
        if (ret != ERROR_SUCCESS) {
477
 
                wpa_printf(MSG_DEBUG, "WINREG: Could not open key '%s' for "
478
 
                           "subkey deletion: error 0x%x (%d)", key,
 
515
                wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
 
516
                           "' for subkey deletion: error 0x%x (%d)", key,
479
517
                           (unsigned int) ret, (int) GetLastError());
480
518
                return 0;
481
519
        }
499
537
 
500
538
                if (namelen >= 255)
501
539
                        namelen = 255 - 1;
502
 
                name[namelen] = '\0';
 
540
                name[namelen] = TEXT('\0');
503
541
 
504
542
                ret = RegDeleteKey(nhk, name);
505
543
                if (ret != ERROR_SUCCESS) {
669
707
        int i, errors = 0;
670
708
        HKEY nhk, netw;
671
709
        LONG ret;
672
 
        char name[5];
 
710
        TCHAR name[5];
673
711
 
674
 
        ret = RegOpenKeyEx(hk, "networks", 0, KEY_CREATE_SUB_KEY, &nhk);
 
712
        ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
675
713
        if (ret != ERROR_SUCCESS) {
676
714
                wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
677
715
                           "for subkey addition: error 0x%x (%d)",
679
717
                return 0;
680
718
        }
681
719
 
 
720
#ifdef UNICODE
 
721
        wsprintf(name, L"%04d", id);
 
722
#else /* UNICODE */
682
723
        snprintf(name, sizeof(name), "%04d", id);
 
724
#endif /* UNICODE */
683
725
        ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
684
726
                             NULL);
685
727
        RegCloseKey(nhk);
745
787
        INT(mode);
746
788
        INT(proactive_key_caching);
747
789
        INT(disabled);
 
790
        INT(stakey);
 
791
        INT(peerkey);
 
792
        STR(id_str);
748
793
 
749
794
#undef STR
750
795
#undef INT
760
805
{
761
806
        HKEY bhk;
762
807
        LONG ret;
 
808
        TCHAR *name;
763
809
 
764
 
        ret = RegCreateKeyEx(hk, "blobs", 0, NULL, 0, KEY_WRITE, NULL, &bhk,
765
 
                             NULL);
 
810
        ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
 
811
                             &bhk, NULL);
766
812
        if (ret != ERROR_SUCCESS) {
767
813
                wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
768
814
                           "error 0x%x (%d)",
770
816
                return -1;
771
817
        }
772
818
 
773
 
        ret = RegSetValueEx(bhk, blob->name, 0, REG_BINARY, blob->data,
 
819
        name = wpa_strdup_tchar(blob->name);
 
820
        ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
774
821
                            blob->len);
775
822
        if (ret != ERROR_SUCCESS) {
776
823
                wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
777
824
                           "error 0x%x (%d)", blob->name, (unsigned int) ret,
778
825
                           (int) GetLastError());
779
826
                RegCloseKey(bhk);
 
827
                free(name);
780
828
                return -1;
781
829
        }
 
830
        free(name);
782
831
 
783
832
        RegCloseKey(bhk);
784
833
 
788
837
 
789
838
int wpa_config_write(const char *name, struct wpa_config *config)
790
839
{
791
 
        char buf[256];
 
840
        TCHAR buf[256];
792
841
        HKEY hk;
793
842
        LONG ret;
794
843
        int errors = 0;
798
847
 
799
848
        wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
800
849
 
801
 
        snprintf(buf, sizeof(buf), KEY_PREFIX "\\configs\\%s", name);
 
850
#ifdef UNICODE
 
851
        _snwprintf(buf, 256, KEY_PREFIX TEXT("\\configs\\%S"), name);
 
852
#else /* UNICODE */
 
853
        snprintf(buf, 256, KEY_PREFIX TEXT("\\configs\\%s"), name);
 
854
#endif /* UNICODE */
 
855
 
802
856
        ret = RegOpenKeyEx(KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
803
857
        if (ret != ERROR_SUCCESS) {
804
858
                wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
813
867
                errors++;
814
868
        }
815
869
 
816
 
        wpa_config_delete_subkeys(hk, "networks");
 
870
        wpa_config_delete_subkeys(hk, TEXT("networks"));
817
871
        for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
818
872
                if (wpa_config_write_network(hk, ssid, id))
819
873
                        errors++;
820
874
        }
821
875
 
822
 
        RegDeleteKey(hk, "blobs");
 
876
        RegDeleteKey(hk, TEXT("blobs"));
823
877
        for (blob = config->blobs; blob; blob = blob->next) {
824
878
                if (wpa_config_write_blob(hk, blob))
825
879
                        errors++;