2 * WPA Supplicant / main() function for Win32 service
3 * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * The root of wpa_supplicant configuration in registry is
15 * HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant. This level includes global
16 * parameters and a 'interfaces' subkey with all the interface configuration
17 * (adapter to confname mapping). Each such mapping is a subkey that has
18 * 'adapter' and 'config' values.
20 * This program can be run either as a normal command line application, e.g.,
21 * for debugging, with 'wpasvc.exe app' or as a Windows service. Service need
22 * to be registered with 'wpasvc.exe reg <full path to wpasvc.exe>'. After
23 * this, it can be started like any other Windows service (e.g., 'net start
24 * wpasvc') or it can be configured to start automatically through the Services
25 * tool in administrative tasks. The service can be unregistered with
33 #include "wpa_supplicant_i.h"
37 #define WPASVC_NAME TEXT("wpasvc")
39 #ifndef WPASVC_DISPLAY_NAME
40 #define WPASVC_DISPLAY_NAME TEXT("wpa_supplicant service")
42 #ifndef WPASVC_DESCRIPTION
43 #define WPASVC_DESCRIPTION \
44 TEXT("Provides IEEE 802.1X and WPA/WPA2 supplicant functionality")
47 static HANDLE kill_svc
;
49 static SERVICE_STATUS_HANDLE svc_status_handle
;
50 static SERVICE_STATUS svc_status
;
54 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
56 #ifndef WPA_KEY_PREFIX
57 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
67 static int read_interface(struct wpa_global
*global
, HKEY _hk
,
72 TCHAR adapter
[TBUFLEN
], config
[TBUFLEN
], ctrl_interface
[TBUFLEN
];
75 struct wpa_interface iface
;
77 ret
= RegOpenKeyEx(_hk
, name
, 0, KEY_QUERY_VALUE
, &hk
);
78 if (ret
!= ERROR_SUCCESS
) {
79 printf("Could not open wpa_supplicant interface key\n");
83 os_memset(&iface
, 0, sizeof(iface
));
84 iface
.driver
= "ndis";
86 buflen
= sizeof(ctrl_interface
);
87 ret
= RegQueryValueEx(hk
, TEXT("ctrl_interface"), NULL
, NULL
,
88 (LPBYTE
) ctrl_interface
, &buflen
);
89 if (ret
== ERROR_SUCCESS
) {
90 ctrl_interface
[TBUFLEN
- 1] = TEXT('\0');
91 wpa_unicode2ascii_inplace(ctrl_interface
);
92 printf("ctrl_interface[len=%d] '%s'\n",
93 (int) buflen
, (char *) ctrl_interface
);
94 iface
.ctrl_interface
= (char *) ctrl_interface
;
97 buflen
= sizeof(adapter
);
98 ret
= RegQueryValueEx(hk
, TEXT("adapter"), NULL
, NULL
,
99 (LPBYTE
) adapter
, &buflen
);
100 if (ret
== ERROR_SUCCESS
) {
101 adapter
[TBUFLEN
- 1] = TEXT('\0');
102 wpa_unicode2ascii_inplace(adapter
);
103 printf("adapter[len=%d] '%s'\n",
104 (int) buflen
, (char *) adapter
);
105 iface
.ifname
= (char *) adapter
;
108 buflen
= sizeof(config
);
109 ret
= RegQueryValueEx(hk
, TEXT("config"), NULL
, NULL
,
110 (LPBYTE
) config
, &buflen
);
111 if (ret
== ERROR_SUCCESS
) {
112 config
[sizeof(config
) - 1] = '\0';
113 wpa_unicode2ascii_inplace(config
);
114 printf("config[len=%d] '%s'\n",
115 (int) buflen
, (char *) config
);
116 iface
.confname
= (char *) config
;
121 if (wpa_supplicant_add_iface(global
, &iface
) == NULL
)
128 static int wpa_supplicant_thread(void)
131 struct wpa_params params
;
132 struct wpa_global
*global
;
134 DWORD val
, buflen
, i
;
137 if (os_program_init())
140 os_memset(¶ms
, 0, sizeof(params
));
141 params
.wpa_debug_level
= MSG_INFO
;
143 ret
= RegOpenKeyEx(WPA_KEY_ROOT
, WPA_KEY_PREFIX
,
144 0, KEY_QUERY_VALUE
, &hk
);
145 if (ret
!= ERROR_SUCCESS
) {
146 printf("Could not open wpa_supplicant registry key\n");
150 buflen
= sizeof(val
);
151 ret
= RegQueryValueEx(hk
, TEXT("debug_level"), NULL
, NULL
,
152 (LPBYTE
) &val
, &buflen
);
153 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
)) {
154 params
.wpa_debug_level
= val
;
157 buflen
= sizeof(val
);
158 ret
= RegQueryValueEx(hk
, TEXT("debug_show_keys"), NULL
, NULL
,
159 (LPBYTE
) &val
, &buflen
);
160 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
)) {
161 params
.wpa_debug_show_keys
= val
;
164 buflen
= sizeof(val
);
165 ret
= RegQueryValueEx(hk
, TEXT("debug_use_file"), NULL
, NULL
,
166 (LPBYTE
) &val
, &buflen
);
167 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
) && val
) {
168 params
.wpa_debug_file_path
= "\\Temp\\wpa_supplicant-log.txt";
172 global
= wpa_supplicant_init(¶ms
);
173 if (global
== NULL
) {
174 printf("Failed to initialize wpa_supplicant\n");
178 ret
= RegOpenKeyEx(hk
, TEXT("interfaces"), 0, KEY_ENUMERATE_SUB_KEYS
,
181 if (ret
!= ERROR_SUCCESS
) {
182 printf("Could not open wpa_supplicant interfaces registry "
192 ret
= RegEnumKeyEx(ihk
, i
, name
, &namelen
, NULL
, NULL
, NULL
,
195 if (ret
== ERROR_NO_MORE_ITEMS
)
198 if (ret
!= ERROR_SUCCESS
) {
199 printf("RegEnumKeyEx failed: 0x%x\n",
206 name
[namelen
] = '\0';
208 wpa_printf(MSG_DEBUG
, "interface %d: %s\n", (int) i
, name
);
209 if (read_interface(global
, ihk
, name
) < 0)
216 exitcode
= wpa_supplicant_run(global
);
218 wpa_supplicant_deinit(global
);
226 static DWORD
svc_thread(LPDWORD param
)
228 int ret
= wpa_supplicant_thread();
230 svc_status
.dwCurrentState
= SERVICE_STOPPED
;
231 svc_status
.dwWaitHint
= 0;
232 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
233 printf("SetServiceStatus() failed: %d\n",
234 (int) GetLastError());
241 static int register_service(const TCHAR
*exe
)
244 SERVICE_DESCRIPTION sd
;
246 printf("Registering service: " TSTR
"\n", WPASVC_NAME
);
248 scm
= OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE
);
250 printf("OpenSCManager failed: %d\n", (int) GetLastError());
254 svc
= CreateService(scm
, WPASVC_NAME
, WPASVC_DISPLAY_NAME
,
255 SERVICE_ALL_ACCESS
, SERVICE_WIN32_OWN_PROCESS
,
256 SERVICE_DEMAND_START
, SERVICE_ERROR_NORMAL
,
257 exe
, NULL
, NULL
, NULL
, NULL
, NULL
);
260 printf("CreateService failed: %d\n\n", (int) GetLastError());
261 CloseServiceHandle(scm
);
265 os_memset(&sd
, 0, sizeof(sd
));
266 sd
.lpDescription
= WPASVC_DESCRIPTION
;
267 if (!ChangeServiceConfig2(svc
, SERVICE_CONFIG_DESCRIPTION
, &sd
)) {
268 printf("ChangeServiceConfig2 failed: %d\n",
269 (int) GetLastError());
270 /* This is not a fatal error, so continue anyway. */
273 CloseServiceHandle(svc
);
274 CloseServiceHandle(scm
);
276 printf("Service registered successfully.\n");
282 static int unregister_service(void)
285 SERVICE_STATUS status
;
287 printf("Unregistering service: " TSTR
"\n", WPASVC_NAME
);
289 scm
= OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE
);
291 printf("OpenSCManager failed: %d\n", (int) GetLastError());
295 svc
= OpenService(scm
, WPASVC_NAME
, SERVICE_ALL_ACCESS
| DELETE
);
297 printf("OpenService failed: %d\n\n", (int) GetLastError());
298 CloseServiceHandle(scm
);
302 if (QueryServiceStatus(svc
, &status
)) {
303 if (status
.dwCurrentState
!= SERVICE_STOPPED
) {
304 printf("Service currently active - stopping "
306 if (!ControlService(svc
, SERVICE_CONTROL_STOP
,
308 printf("ControlService failed: %d\n",
309 (int) GetLastError());
315 if (DeleteService(svc
)) {
316 printf("Service unregistered successfully.\n");
318 printf("DeleteService failed: %d\n", (int) GetLastError());
321 CloseServiceHandle(svc
);
322 CloseServiceHandle(scm
);
328 static void WINAPI
service_ctrl_handler(DWORD control_code
)
330 switch (control_code
) {
331 case SERVICE_CONTROL_INTERROGATE
:
333 case SERVICE_CONTROL_SHUTDOWN
:
334 case SERVICE_CONTROL_STOP
:
335 svc_status
.dwCurrentState
= SERVICE_STOP_PENDING
;
336 svc_status
.dwWaitHint
= 2000;
342 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
343 printf("SetServiceStatus() failed: %d\n",
344 (int) GetLastError());
349 static void WINAPI
service_start(DWORD argc
, LPTSTR
*argv
)
353 svc_status_handle
= RegisterServiceCtrlHandler(WPASVC_NAME
,
354 service_ctrl_handler
);
355 if (svc_status_handle
== (SERVICE_STATUS_HANDLE
) 0) {
356 printf("RegisterServiceCtrlHandler failed: %d\n",
357 (int) GetLastError());
361 os_memset(&svc_status
, 0, sizeof(svc_status
));
362 svc_status
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
363 svc_status
.dwCurrentState
= SERVICE_START_PENDING
;
364 svc_status
.dwWaitHint
= 1000;
366 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
367 printf("SetServiceStatus() failed: %d\n",
368 (int) GetLastError());
372 kill_svc
= CreateEvent(0, TRUE
, FALSE
, 0);
374 printf("CreateEvent failed: %d\n", (int) GetLastError());
378 if (CreateThread(0, 0, (LPTHREAD_START_ROUTINE
) svc_thread
, 0, 0, &id
)
380 printf("CreateThread failed: %d\n", (int) GetLastError());
384 if (svc_status
.dwCurrentState
== SERVICE_START_PENDING
) {
385 svc_status
.dwCurrentState
= SERVICE_RUNNING
;
386 svc_status
.dwWaitHint
= 0;
387 svc_status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
|
388 SERVICE_ACCEPT_SHUTDOWN
;
391 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
392 printf("SetServiceStatus() failed: %d\n",
393 (int) GetLastError());
397 /* wait until service gets killed */
398 WaitForSingleObject(kill_svc
, INFINITE
);
402 int main(int argc
, char *argv
[])
404 SERVICE_TABLE_ENTRY dt
[] = {
405 { WPASVC_NAME
, service_start
},
410 if (os_strcmp(argv
[1], "reg") == 0) {
415 path
= os_malloc(MAX_PATH
* sizeof(TCHAR
));
418 if (!GetModuleFileName(NULL
, path
, MAX_PATH
)) {
419 printf("GetModuleFileName failed: "
420 "%d\n", (int) GetLastError());
425 path
= wpa_strdup_tchar(argv
[2]);
429 ret
= register_service(path
);
432 } else if (os_strcmp(argv
[1], "unreg") == 0) {
433 return unregister_service();
434 } else if (os_strcmp(argv
[1], "app") == 0) {
435 return wpa_supplicant_thread();
439 if (!StartServiceCtrlDispatcher(dt
)) {
440 printf("StartServiceCtrlDispatcher failed: %d\n",
441 (int) GetLastError());