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
;
76 int skip_on_error
= 0;
78 ret
= RegOpenKeyEx(_hk
, name
, 0, KEY_QUERY_VALUE
, &hk
);
79 if (ret
!= ERROR_SUCCESS
) {
80 printf("Could not open wpa_supplicant interface key\n");
84 os_memset(&iface
, 0, sizeof(iface
));
85 iface
.driver
= "ndis";
87 buflen
= sizeof(ctrl_interface
);
88 ret
= RegQueryValueEx(hk
, TEXT("ctrl_interface"), NULL
, NULL
,
89 (LPBYTE
) ctrl_interface
, &buflen
);
90 if (ret
== ERROR_SUCCESS
) {
91 ctrl_interface
[TBUFLEN
- 1] = TEXT('\0');
92 wpa_unicode2ascii_inplace(ctrl_interface
);
93 printf("ctrl_interface[len=%d] '%s'\n",
94 (int) buflen
, (char *) ctrl_interface
);
95 iface
.ctrl_interface
= (char *) ctrl_interface
;
98 buflen
= sizeof(adapter
);
99 ret
= RegQueryValueEx(hk
, TEXT("adapter"), NULL
, NULL
,
100 (LPBYTE
) adapter
, &buflen
);
101 if (ret
== ERROR_SUCCESS
) {
102 adapter
[TBUFLEN
- 1] = TEXT('\0');
103 wpa_unicode2ascii_inplace(adapter
);
104 printf("adapter[len=%d] '%s'\n",
105 (int) buflen
, (char *) adapter
);
106 iface
.ifname
= (char *) adapter
;
109 buflen
= sizeof(config
);
110 ret
= RegQueryValueEx(hk
, TEXT("config"), NULL
, NULL
,
111 (LPBYTE
) config
, &buflen
);
112 if (ret
== ERROR_SUCCESS
) {
113 config
[sizeof(config
) - 1] = '\0';
114 wpa_unicode2ascii_inplace(config
);
115 printf("config[len=%d] '%s'\n",
116 (int) buflen
, (char *) config
);
117 iface
.confname
= (char *) config
;
120 buflen
= sizeof(val
);
121 ret
= RegQueryValueEx(hk
, TEXT("skip_on_error"), NULL
, NULL
,
122 (LPBYTE
) &val
, &buflen
);
123 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
))
128 if (wpa_supplicant_add_iface(global
, &iface
) == NULL
) {
130 wpa_printf(MSG_DEBUG
, "Skipped interface '%s' due to "
131 "initialization failure", iface
.ifname
);
140 static int wpa_supplicant_thread(void)
143 struct wpa_params params
;
144 struct wpa_global
*global
;
146 DWORD val
, buflen
, i
;
149 if (os_program_init())
152 os_memset(¶ms
, 0, sizeof(params
));
153 params
.wpa_debug_level
= MSG_INFO
;
155 ret
= RegOpenKeyEx(WPA_KEY_ROOT
, WPA_KEY_PREFIX
,
156 0, KEY_QUERY_VALUE
, &hk
);
157 if (ret
!= ERROR_SUCCESS
) {
158 printf("Could not open wpa_supplicant registry key\n");
162 buflen
= sizeof(val
);
163 ret
= RegQueryValueEx(hk
, TEXT("debug_level"), NULL
, NULL
,
164 (LPBYTE
) &val
, &buflen
);
165 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
)) {
166 params
.wpa_debug_level
= val
;
169 buflen
= sizeof(val
);
170 ret
= RegQueryValueEx(hk
, TEXT("debug_show_keys"), NULL
, NULL
,
171 (LPBYTE
) &val
, &buflen
);
172 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
)) {
173 params
.wpa_debug_show_keys
= val
;
176 buflen
= sizeof(val
);
177 ret
= RegQueryValueEx(hk
, TEXT("debug_timestamp"), NULL
, NULL
,
178 (LPBYTE
) &val
, &buflen
);
179 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
)) {
180 params
.wpa_debug_timestamp
= val
;
183 buflen
= sizeof(val
);
184 ret
= RegQueryValueEx(hk
, TEXT("debug_use_file"), NULL
, NULL
,
185 (LPBYTE
) &val
, &buflen
);
186 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
) && val
) {
187 params
.wpa_debug_file_path
= "\\Temp\\wpa_supplicant-log.txt";
191 global
= wpa_supplicant_init(¶ms
);
192 if (global
== NULL
) {
193 printf("Failed to initialize wpa_supplicant\n");
197 ret
= RegOpenKeyEx(hk
, TEXT("interfaces"), 0, KEY_ENUMERATE_SUB_KEYS
,
200 if (ret
!= ERROR_SUCCESS
) {
201 printf("Could not open wpa_supplicant interfaces registry "
211 ret
= RegEnumKeyEx(ihk
, i
, name
, &namelen
, NULL
, NULL
, NULL
,
214 if (ret
== ERROR_NO_MORE_ITEMS
)
217 if (ret
!= ERROR_SUCCESS
) {
218 printf("RegEnumKeyEx failed: 0x%x\n",
225 name
[namelen
] = '\0';
227 wpa_printf(MSG_DEBUG
, "interface %d: %s\n", (int) i
, name
);
228 if (read_interface(global
, ihk
, name
) < 0)
235 exitcode
= wpa_supplicant_run(global
);
237 wpa_supplicant_deinit(global
);
245 static DWORD
svc_thread(LPDWORD param
)
247 int ret
= wpa_supplicant_thread();
249 svc_status
.dwCurrentState
= SERVICE_STOPPED
;
250 svc_status
.dwWaitHint
= 0;
251 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
252 printf("SetServiceStatus() failed: %d\n",
253 (int) GetLastError());
260 static int register_service(const TCHAR
*exe
)
263 SERVICE_DESCRIPTION sd
;
265 printf("Registering service: " TSTR
"\n", WPASVC_NAME
);
267 scm
= OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE
);
269 printf("OpenSCManager failed: %d\n", (int) GetLastError());
273 svc
= CreateService(scm
, WPASVC_NAME
, WPASVC_DISPLAY_NAME
,
274 SERVICE_ALL_ACCESS
, SERVICE_WIN32_OWN_PROCESS
,
275 SERVICE_DEMAND_START
, SERVICE_ERROR_NORMAL
,
276 exe
, NULL
, NULL
, NULL
, NULL
, NULL
);
279 printf("CreateService failed: %d\n\n", (int) GetLastError());
280 CloseServiceHandle(scm
);
284 os_memset(&sd
, 0, sizeof(sd
));
285 sd
.lpDescription
= WPASVC_DESCRIPTION
;
286 if (!ChangeServiceConfig2(svc
, SERVICE_CONFIG_DESCRIPTION
, &sd
)) {
287 printf("ChangeServiceConfig2 failed: %d\n",
288 (int) GetLastError());
289 /* This is not a fatal error, so continue anyway. */
292 CloseServiceHandle(svc
);
293 CloseServiceHandle(scm
);
295 printf("Service registered successfully.\n");
301 static int unregister_service(void)
304 SERVICE_STATUS status
;
306 printf("Unregistering service: " TSTR
"\n", WPASVC_NAME
);
308 scm
= OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE
);
310 printf("OpenSCManager failed: %d\n", (int) GetLastError());
314 svc
= OpenService(scm
, WPASVC_NAME
, SERVICE_ALL_ACCESS
| DELETE
);
316 printf("OpenService failed: %d\n\n", (int) GetLastError());
317 CloseServiceHandle(scm
);
321 if (QueryServiceStatus(svc
, &status
)) {
322 if (status
.dwCurrentState
!= SERVICE_STOPPED
) {
323 printf("Service currently active - stopping "
325 if (!ControlService(svc
, SERVICE_CONTROL_STOP
,
327 printf("ControlService failed: %d\n",
328 (int) GetLastError());
334 if (DeleteService(svc
)) {
335 printf("Service unregistered successfully.\n");
337 printf("DeleteService failed: %d\n", (int) GetLastError());
340 CloseServiceHandle(svc
);
341 CloseServiceHandle(scm
);
347 static void WINAPI
service_ctrl_handler(DWORD control_code
)
349 switch (control_code
) {
350 case SERVICE_CONTROL_INTERROGATE
:
352 case SERVICE_CONTROL_SHUTDOWN
:
353 case SERVICE_CONTROL_STOP
:
354 svc_status
.dwCurrentState
= SERVICE_STOP_PENDING
;
355 svc_status
.dwWaitHint
= 2000;
361 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
362 printf("SetServiceStatus() failed: %d\n",
363 (int) GetLastError());
368 static void WINAPI
service_start(DWORD argc
, LPTSTR
*argv
)
372 svc_status_handle
= RegisterServiceCtrlHandler(WPASVC_NAME
,
373 service_ctrl_handler
);
374 if (svc_status_handle
== (SERVICE_STATUS_HANDLE
) 0) {
375 printf("RegisterServiceCtrlHandler failed: %d\n",
376 (int) GetLastError());
380 os_memset(&svc_status
, 0, sizeof(svc_status
));
381 svc_status
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
382 svc_status
.dwCurrentState
= SERVICE_START_PENDING
;
383 svc_status
.dwWaitHint
= 1000;
385 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
386 printf("SetServiceStatus() failed: %d\n",
387 (int) GetLastError());
391 kill_svc
= CreateEvent(0, TRUE
, FALSE
, 0);
393 printf("CreateEvent failed: %d\n", (int) GetLastError());
397 if (CreateThread(0, 0, (LPTHREAD_START_ROUTINE
) svc_thread
, 0, 0, &id
)
399 printf("CreateThread failed: %d\n", (int) GetLastError());
403 if (svc_status
.dwCurrentState
== SERVICE_START_PENDING
) {
404 svc_status
.dwCurrentState
= SERVICE_RUNNING
;
405 svc_status
.dwWaitHint
= 0;
406 svc_status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
|
407 SERVICE_ACCEPT_SHUTDOWN
;
410 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
411 printf("SetServiceStatus() failed: %d\n",
412 (int) GetLastError());
416 /* wait until service gets killed */
417 WaitForSingleObject(kill_svc
, INFINITE
);
421 int main(int argc
, char *argv
[])
423 SERVICE_TABLE_ENTRY dt
[] = {
424 { WPASVC_NAME
, service_start
},
429 if (os_strcmp(argv
[1], "reg") == 0) {
434 path
= os_malloc(MAX_PATH
* sizeof(TCHAR
));
437 if (!GetModuleFileName(NULL
, path
, MAX_PATH
)) {
438 printf("GetModuleFileName failed: "
439 "%d\n", (int) GetLastError());
444 path
= wpa_strdup_tchar(argv
[2]);
448 ret
= register_service(path
);
451 } else if (os_strcmp(argv
[1], "unreg") == 0) {
452 return unregister_service();
453 } else if (os_strcmp(argv
[1], "app") == 0) {
454 return wpa_supplicant_thread();
458 if (!StartServiceCtrlDispatcher(dt
)) {
459 printf("StartServiceCtrlDispatcher failed: %d\n",
460 (int) GetLastError());