aboutsummaryrefslogtreecommitdiffstats
path: root/wpa_supplicant/main_winsvc.c
blob: 6d416fd4104613b643e7c227aaf351b60d274e25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * WPA Supplicant / main() function for Win32 service
 * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Alternatively, this software may be distributed under the terms of BSD
 * license.
 *
 * See README and COPYING for more details.
 *
 * The root of wpa_supplicant configuration in registry is
 * HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant. This level includes global
 * parameters and a 'interfaces' subkey with all the interface configuration
 * (adapter to confname mapping). Each such mapping is a subkey that has
 * 'adapter' and 'config' values.
 *
 * This program can be run either as a normal command line application, e.g.,
 * for debugging, with 'wpasvc.exe app' or as a Windows service. Service need
 * to be registered with 'wpasvc.exe reg <full path to wpasvc.exe>'. After
 * this, it can be started like any other Windows service (e.g., 'net start
 * wpasvc') or it can be configured to start automatically through the Services
 * tool in administrative tasks. The service can be unregistered with
 * 'wpasvc.exe unreg'.
 */

#include "includes.h"
#include <windows.h>

#include "common.h"
#include "wpa_supplicant_i.h"
#include "eloop.h"

#ifndef WPASVC_NAME
#define WPASVC_NAME TEXT("wpasvc")
#endif
#ifndef WPASVC_DISPLAY_NAME
#define WPASVC_DISPLAY_NAME TEXT("wpa_supplicant service")
#endif
#ifndef WPASVC_DESCRIPTION
#define WPASVC_DESCRIPTION \
TEXT("Provides IEEE 802.1X and WPA/WPA2 supplicant functionality")
#endif

static HANDLE kill_svc;

static SERVICE_STATUS_HANDLE svc_status_handle;
static SERVICE_STATUS svc_status;


#ifndef WPA_KEY_ROOT
#define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
#endif
#ifndef WPA_KEY_PREFIX
#define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
#endif

#ifdef UNICODE
#define TSTR "%S"
#else /* UNICODE */
#define TSTR "%s"
#endif /* UNICODE */


static int read_interface(struct wpa_global *global, HKEY _hk,
			  const TCHAR *name)
{
	HKEY hk;
#define TBUFLEN 255
	TCHAR adapter[TBUFLEN], config[TBUFLEN], ctrl_interface[TBUFLEN];
	DWORD buflen;
	LONG ret;
	struct wpa_interface iface;

	ret = RegOpenKeyEx(_hk, name, 0, KEY_QUERY_VALUE, &hk);
	if (ret != ERROR_SUCCESS) {
		printf("Could not open wpa_supplicant interface key\n");
		return -1;
	}

	os_memset(&iface, 0, sizeof(iface));
	iface.driver = "ndis";

	buflen = sizeof(ctrl_interface);
	ret = RegQueryValueEx(hk, TEXT("ctrl_interface"), NULL, NULL,
			      (LPBYTE) ctrl_interface, &buflen);
	if (ret == ERROR_SUCCESS) {
		ctrl_interface[TBUFLEN - 1] = TEXT('\0');
		wpa_unicode2ascii_inplace(ctrl_interface);
		printf("ctrl_interface[len=%d] '%s'\n",
		       (int) buflen, (char *) ctrl_interface);
		iface.ctrl_interface = (char *) ctrl_interface;
	}

	buflen = sizeof(adapter);
	ret = RegQueryValueEx(hk, TEXT("adapter"), NULL, NULL,
			      (LPBYTE) adapter, &buflen);
	if (ret == ERROR_SUCCESS) {
		adapter[TBUFLEN - 1] = TEXT('\0');
		wpa_unicode2ascii_inplace(adapter);
		printf("adapter[len=%d] '%s'\n",
		       (int) buflen, (char *) adapter);
		iface.ifname = (char *) adapter;
	}

	buflen = sizeof(config);
	ret = RegQueryValueEx(hk, TEXT("config"), NULL, NULL,
			      (LPBYTE) config, &buflen);
	if (ret == ERROR_SUCCESS) {
		config[sizeof(config) - 1] = '\0';
		wpa_unicode2ascii_inplace(config);
		printf("config[len=%d] '%s'\n",
		       (int) buflen, (char *) config);
		iface.confname = (char *) config;
	}

	RegCloseKey(hk);

	if (wpa_supplicant_add_iface(global, &iface) == NULL)
		return -1;

	return 0;
}


static int wpa_supplicant_thread(void)
{
	int exitcode;
	struct wpa_params params;
	struct wpa_global *global;
	HKEY hk, ihk;
	DWORD val, buflen, i;
	LONG ret;

	if (os_program_init())
		return -1;

	os_memset(&params, 0, sizeof(params));
	params.wpa_debug_level = MSG_INFO;

	ret = RegOpenKeyEx(WPA_KEY_ROOT, WPA_KEY_PREFIX,
			   0, KEY_QUERY_VALUE, &hk);
	if (ret != ERROR_SUCCESS) {
		printf("Could not open wpa_supplicant registry key\n");
		return -1;
	}

	buflen = sizeof(val);
	ret = RegQueryValueEx(hk, TEXT("debug_level"), NULL, NULL,
			      (LPBYTE) &val, &buflen);
	if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
		params.wpa_debug_level = val;
	}

	buflen = sizeof(val);
	ret = RegQueryValueEx(hk, TEXT("debug_show_keys"), NULL, NULL,
			      (LPBYTE) &val, &buflen);
	if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
		params.wpa_debug_show_keys = val;
	}

	buflen = sizeof(val);
	ret = RegQueryValueEx(hk, TEXT("debug_timestamp"), NULL, NULL,
			      (LPBYTE) &val, &buflen);
	if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
		params.wpa_debug_timestamp = val;
	}

	buflen = sizeof(val);
	ret = RegQueryValueEx(hk, TEXT("debug_use_file"), NULL, NULL,
			      (LPBYTE) &val, &buflen);
	if (ret == ERROR_SUCCESS && buflen == sizeof(val) && val) {
		params.wpa_debug_file_path = "\\Temp\\wpa_supplicant-log.txt";
	}

	exitcode = 0;
	global = wpa_supplicant_init(&params);
	if (global == NULL) {
		printf("Failed to initialize wpa_supplicant\n");
		exitcode = -1;
	}

	ret = RegOpenKeyEx(hk, TEXT("interfaces"), 0, KEY_ENUMERATE_SUB_KEYS,
			   &ihk);
	RegCloseKey(hk);
	if (ret != ERROR_SUCCESS) {
		printf("Could not open wpa_supplicant interfaces registry "
		       "key\n");
		return -1;
	}

	for (i = 0; ; i++) {
		TCHAR name[255];
		DWORD namelen;

		namelen = 255;
		ret = RegEnumKeyEx(ihk, i, name, &namelen, NULL, NULL, NULL,
				   NULL);

		if (ret == ERROR_NO_MORE_ITEMS)
			break;

		if (ret != ERROR_SUCCESS) {
			printf("RegEnumKeyEx failed: 0x%x\n",
			       (unsigned int) ret);
			break;
		}

		if (namelen >= 255)
			namelen = 255 - 1;
		name[namelen] = '\0';

		wpa_printf(MSG_DEBUG, "interface %d: %s\n", (int) i, name);
		if (read_interface(global, ihk, name) < 0)
			exitcode = -1;
	}

	RegCloseKey(ihk);

	if (exitcode == 0)
		exitcode = wpa_supplicant_run(global);

	wpa_supplicant_deinit(global);

	os_program_deinit();

	return exitcode;
}


static DWORD svc_thread(LPDWORD param)
{
	int ret = wpa_supplicant_thread();

	svc_status.dwCurrentState = SERVICE_STOPPED;
	svc_status.dwWaitHint = 0;
	if (!SetServiceStatus(svc_status_handle, &svc_status)) {
		printf("SetServiceStatus() failed: %d\n",
		       (int) GetLastError());
	}

	return ret;
}


static int register_service(const TCHAR *exe)
{
	SC_HANDLE svc, scm;
	SERVICE_DESCRIPTION sd;

	printf("Registering service: " TSTR "\n", WPASVC_NAME);

	scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
	if (!scm) {
		printf("OpenSCManager failed: %d\n", (int) GetLastError());
		return -1;
	}

	svc = CreateService(scm, WPASVC_NAME, WPASVC_DISPLAY_NAME,
			    SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
			    SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
			    exe, NULL, NULL, NULL, NULL, NULL);

	if (!svc) {
		printf("CreateService failed: %d\n\n", (int) GetLastError());
		CloseServiceHandle(scm);
		return -1;
	}

	os_memset(&sd, 0, sizeof(sd));
	sd.lpDescription = WPASVC_DESCRIPTION;
	if (!ChangeServiceConfig2(svc, SERVICE_CONFIG_DESCRIPTION, &sd)) {
		printf("ChangeServiceConfig2 failed: %d\n",
		       (int) GetLastError());
		/* This is not a fatal error, so continue anyway. */
	}

	CloseServiceHandle(svc);
	CloseServiceHandle(scm);

	printf("Service registered successfully.\n");

	return 0;
}


static int unregister_service(void)
{
	SC_HANDLE svc, scm;
	SERVICE_STATUS status;

	printf("Unregistering service: " TSTR "\n", WPASVC_NAME);

	scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
	if (!scm) {
		printf("OpenSCManager failed: %d\n", (int) GetLastError());
		return -1;
	}

	svc = OpenService(scm, WPASVC_NAME, SERVICE_ALL_ACCESS | DELETE);
	if (!svc) {
		printf("OpenService failed: %d\n\n", (int) GetLastError());
		CloseServiceHandle(scm);
		return -1;
	}

	if (QueryServiceStatus(svc, &status)) {
		if (status.dwCurrentState != SERVICE_STOPPED) {
			printf("Service currently active - stopping "
			       "service...\n");
			if (!ControlService(svc, SERVICE_CONTROL_STOP,
					    &status)) {
				printf("ControlService failed: %d\n",
				       (int) GetLastError());
			}
			Sleep(500);
		}
	}

	if (DeleteService(svc)) {
		printf("Service unregistered successfully.\n");
	} else {
		printf("DeleteService failed: %d\n", (int) GetLastError());
	}

	CloseServiceHandle(svc);
	CloseServiceHandle(scm);

	return 0;
}


static void WINAPI service_ctrl_handler(DWORD control_code)
{
	switch (control_code) {
	case SERVICE_CONTROL_INTERROGATE:
		break;
	case SERVICE_CONTROL_SHUTDOWN:
	case SERVICE_CONTROL_STOP:
		svc_status.dwCurrentState = SERVICE_STOP_PENDING;
		svc_status.dwWaitHint = 2000;
		eloop_terminate();
		SetEvent(kill_svc);
		break;
	}

	if (!SetServiceStatus(svc_status_handle, &svc_status)) {
		printf("SetServiceStatus() failed: %d\n",
		       (int) GetLastError());
	}
}


static void WINAPI service_start(DWORD argc, LPTSTR *argv)
{
	DWORD id;

	svc_status_handle = RegisterServiceCtrlHandler(WPASVC_NAME,
						       service_ctrl_handler);
	if (svc_status_handle == (SERVICE_STATUS_HANDLE) 0) {
		printf("RegisterServiceCtrlHandler failed: %d\n",
		       (int) GetLastError());
		return;
	}

	os_memset(&svc_status, 0, sizeof(svc_status));
	svc_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	svc_status.dwCurrentState = SERVICE_START_PENDING;
	svc_status.dwWaitHint = 1000;

	if (!SetServiceStatus(svc_status_handle, &svc_status)) {
		printf("SetServiceStatus() failed: %d\n",
		       (int) GetLastError());
		return;
	}

	kill_svc = CreateEvent(0, TRUE, FALSE, 0);
	if (!kill_svc) {
		printf("CreateEvent failed: %d\n", (int) GetLastError());
		return;
	}

	if (CreateThread(0, 0, (LPTHREAD_START_ROUTINE) svc_thread, 0, 0, &id)
	    == 0) {
		printf("CreateThread failed: %d\n", (int) GetLastError());
		return;
	}

	if (svc_status.dwCurrentState == SERVICE_START_PENDING) {
		svc_status.dwCurrentState = SERVICE_RUNNING;
		svc_status.dwWaitHint = 0;
		svc_status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
			SERVICE_ACCEPT_SHUTDOWN;
	}

	if (!SetServiceStatus(svc_status_handle, &svc_status)) {
		printf("SetServiceStatus() failed: %d\n",
		       (int) GetLastError());
		return;
	}

	/* wait until service gets killed */
	WaitForSingleObject(kill_svc, INFINITE);
}


int main(int argc, char *argv[])
{
	SERVICE_TABLE_ENTRY dt[] = {
		{ WPASVC_NAME, service_start },
		{ NULL, NULL }
	};

	if (argc > 1) {
		if (os_strcmp(argv[1], "reg") == 0) {
			TCHAR *path;
			int ret;

			if (argc < 3) {
				path = os_malloc(MAX_PATH * sizeof(TCHAR));
				if (path == NULL)
					return -1;
				if (!GetModuleFileName(NULL, path, MAX_PATH)) {
					printf("GetModuleFileName failed: "
					       "%d\n", (int) GetLastError());
					os_free(path);
					return -1;
				}
			} else {
				path = wpa_strdup_tchar(argv[2]);
				if (path == NULL)
					return -1;
			}
			ret = register_service(path);
			os_free(path);
			return ret;
		} else if (os_strcmp(argv[1], "unreg") == 0) {
			return unregister_service();
		} else if (os_strcmp(argv[1], "app") == 0) {
			return wpa_supplicant_thread();
		}
	}

	if (!StartServiceCtrlDispatcher(dt)) {
		printf("StartServiceCtrlDispatcher failed: %d\n",
		       (int) GetLastError());
	}

	return 0;
}