2 * WPA Supplicant / main() function for Win32 service
3 * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * The root of wpa_supplicant configuration in registry is
9 * HKEY_LOCAL_MACHINE\\SOFTWARE\\%wpa_supplicant. This level includes global
10 * parameters and a 'interfaces' subkey with all the interface configuration
11 * (adapter to confname mapping). Each such mapping is a subkey that has
12 * 'adapter' and 'config' values.
14 * This program can be run either as a normal command line application, e.g.,
15 * for debugging, with 'wpasvc.exe app' or as a Windows service. Service need
16 * to be registered with 'wpasvc.exe reg <full path to wpasvc.exe>'. After
17 * this, it can be started like any other Windows service (e.g., 'net start
18 * wpasvc') or it can be configured to start automatically through the Services
19 * tool in administrative tasks. The service can be unregistered with
27 #include "wpa_supplicant_i.h"
31 #define WPASVC_NAME TEXT("wpasvc")
33 #ifndef WPASVC_DISPLAY_NAME
34 #define WPASVC_DISPLAY_NAME TEXT("wpa_supplicant service")
36 #ifndef WPASVC_DESCRIPTION
37 #define WPASVC_DESCRIPTION \
38 TEXT("Provides IEEE 802.1X and WPA/WPA2 supplicant functionality")
41 static HANDLE kill_svc
;
43 static SERVICE_STATUS_HANDLE svc_status_handle
;
44 static SERVICE_STATUS svc_status
;
48 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
50 #ifndef WPA_KEY_PREFIX
51 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
61 static int read_interface(struct wpa_global
*global
, HKEY _hk
,
66 TCHAR adapter
[TBUFLEN
], config
[TBUFLEN
], ctrl_interface
[TBUFLEN
];
69 struct wpa_interface iface
;
70 int skip_on_error
= 0;
72 ret
= RegOpenKeyEx(_hk
, name
, 0, KEY_QUERY_VALUE
, &hk
);
73 if (ret
!= ERROR_SUCCESS
) {
74 printf("Could not open wpa_supplicant interface key\n");
78 os_memset(&iface
, 0, sizeof(iface
));
79 iface
.driver
= "ndis";
81 buflen
= sizeof(ctrl_interface
);
82 ret
= RegQueryValueEx(hk
, TEXT("ctrl_interface"), NULL
, NULL
,
83 (LPBYTE
) ctrl_interface
, &buflen
);
84 if (ret
== ERROR_SUCCESS
) {
85 ctrl_interface
[TBUFLEN
- 1] = TEXT('\0');
86 wpa_unicode2ascii_inplace(ctrl_interface
);
87 printf("ctrl_interface[len=%d] '%s'\n",
88 (int) buflen
, (char *) ctrl_interface
);
89 iface
.ctrl_interface
= (char *) ctrl_interface
;
92 buflen
= sizeof(adapter
);
93 ret
= RegQueryValueEx(hk
, TEXT("adapter"), NULL
, NULL
,
94 (LPBYTE
) adapter
, &buflen
);
95 if (ret
== ERROR_SUCCESS
) {
96 adapter
[TBUFLEN
- 1] = TEXT('\0');
97 wpa_unicode2ascii_inplace(adapter
);
98 printf("adapter[len=%d] '%s'\n",
99 (int) buflen
, (char *) adapter
);
100 iface
.ifname
= (char *) adapter
;
103 buflen
= sizeof(config
);
104 ret
= RegQueryValueEx(hk
, TEXT("config"), NULL
, NULL
,
105 (LPBYTE
) config
, &buflen
);
106 if (ret
== ERROR_SUCCESS
) {
107 config
[sizeof(config
) - 1] = '\0';
108 wpa_unicode2ascii_inplace(config
);
109 printf("config[len=%d] '%s'\n",
110 (int) buflen
, (char *) config
);
111 iface
.confname
= (char *) config
;
114 buflen
= sizeof(val
);
115 ret
= RegQueryValueEx(hk
, TEXT("skip_on_error"), NULL
, NULL
,
116 (LPBYTE
) &val
, &buflen
);
117 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
))
122 if (wpa_supplicant_add_iface(global
, &iface
) == NULL
) {
124 wpa_printf(MSG_DEBUG
, "Skipped interface '%s' due to "
125 "initialization failure", iface
.ifname
);
134 static int wpa_supplicant_thread(void)
137 struct wpa_params params
;
138 struct wpa_global
*global
;
140 DWORD val
, buflen
, i
;
143 if (os_program_init())
146 os_memset(¶ms
, 0, sizeof(params
));
147 params
.wpa_debug_level
= MSG_INFO
;
149 ret
= RegOpenKeyEx(WPA_KEY_ROOT
, WPA_KEY_PREFIX
,
150 0, KEY_QUERY_VALUE
, &hk
);
151 if (ret
!= ERROR_SUCCESS
) {
152 printf("Could not open wpa_supplicant registry key\n");
156 buflen
= sizeof(val
);
157 ret
= RegQueryValueEx(hk
, TEXT("debug_level"), NULL
, NULL
,
158 (LPBYTE
) &val
, &buflen
);
159 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
)) {
160 params
.wpa_debug_level
= val
;
163 buflen
= sizeof(val
);
164 ret
= RegQueryValueEx(hk
, TEXT("debug_show_keys"), NULL
, NULL
,
165 (LPBYTE
) &val
, &buflen
);
166 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
)) {
167 params
.wpa_debug_show_keys
= val
;
170 buflen
= sizeof(val
);
171 ret
= RegQueryValueEx(hk
, TEXT("debug_timestamp"), NULL
, NULL
,
172 (LPBYTE
) &val
, &buflen
);
173 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
)) {
174 params
.wpa_debug_timestamp
= val
;
177 buflen
= sizeof(val
);
178 ret
= RegQueryValueEx(hk
, TEXT("debug_use_file"), NULL
, NULL
,
179 (LPBYTE
) &val
, &buflen
);
180 if (ret
== ERROR_SUCCESS
&& buflen
== sizeof(val
) && val
) {
181 params
.wpa_debug_file_path
= "\\Temp\\wpa_supplicant-log.txt";
185 global
= wpa_supplicant_init(¶ms
);
186 if (global
== NULL
) {
187 printf("Failed to initialize wpa_supplicant\n");
191 ret
= RegOpenKeyEx(hk
, TEXT("interfaces"), 0, KEY_ENUMERATE_SUB_KEYS
,
194 if (ret
!= ERROR_SUCCESS
) {
195 printf("Could not open wpa_supplicant interfaces registry "
205 ret
= RegEnumKeyEx(ihk
, i
, name
, &namelen
, NULL
, NULL
, NULL
,
208 if (ret
== ERROR_NO_MORE_ITEMS
)
211 if (ret
!= ERROR_SUCCESS
) {
212 printf("RegEnumKeyEx failed: 0x%x\n",
219 name
[namelen
] = '\0';
221 wpa_printf(MSG_DEBUG
, "interface %d: %s\n", (int) i
, name
);
222 if (read_interface(global
, ihk
, name
) < 0)
229 exitcode
= wpa_supplicant_run(global
);
231 wpa_supplicant_deinit(global
);
239 static DWORD
svc_thread(LPDWORD param
)
241 int ret
= wpa_supplicant_thread();
243 svc_status
.dwCurrentState
= SERVICE_STOPPED
;
244 svc_status
.dwWaitHint
= 0;
245 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
246 printf("SetServiceStatus() failed: %d\n",
247 (int) GetLastError());
254 static int register_service(const TCHAR
*exe
)
257 SERVICE_DESCRIPTION sd
;
259 printf("Registering service: " TSTR
"\n", WPASVC_NAME
);
261 scm
= OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE
);
263 printf("OpenSCManager failed: %d\n", (int) GetLastError());
267 svc
= CreateService(scm
, WPASVC_NAME
, WPASVC_DISPLAY_NAME
,
268 SERVICE_ALL_ACCESS
, SERVICE_WIN32_OWN_PROCESS
,
269 SERVICE_DEMAND_START
, SERVICE_ERROR_NORMAL
,
270 exe
, NULL
, NULL
, NULL
, NULL
, NULL
);
273 printf("CreateService failed: %d\n\n", (int) GetLastError());
274 CloseServiceHandle(scm
);
278 os_memset(&sd
, 0, sizeof(sd
));
279 sd
.lpDescription
= WPASVC_DESCRIPTION
;
280 if (!ChangeServiceConfig2(svc
, SERVICE_CONFIG_DESCRIPTION
, &sd
)) {
281 printf("ChangeServiceConfig2 failed: %d\n",
282 (int) GetLastError());
283 /* This is not a fatal error, so continue anyway. */
286 CloseServiceHandle(svc
);
287 CloseServiceHandle(scm
);
289 printf("Service registered successfully.\n");
295 static int unregister_service(void)
298 SERVICE_STATUS status
;
300 printf("Unregistering service: " TSTR
"\n", WPASVC_NAME
);
302 scm
= OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE
);
304 printf("OpenSCManager failed: %d\n", (int) GetLastError());
308 svc
= OpenService(scm
, WPASVC_NAME
, SERVICE_ALL_ACCESS
| DELETE
);
310 printf("OpenService failed: %d\n\n", (int) GetLastError());
311 CloseServiceHandle(scm
);
315 if (QueryServiceStatus(svc
, &status
)) {
316 if (status
.dwCurrentState
!= SERVICE_STOPPED
) {
317 printf("Service currently active - stopping "
319 if (!ControlService(svc
, SERVICE_CONTROL_STOP
,
321 printf("ControlService failed: %d\n",
322 (int) GetLastError());
328 if (DeleteService(svc
)) {
329 printf("Service unregistered successfully.\n");
331 printf("DeleteService failed: %d\n", (int) GetLastError());
334 CloseServiceHandle(svc
);
335 CloseServiceHandle(scm
);
341 static void WINAPI
service_ctrl_handler(DWORD control_code
)
343 switch (control_code
) {
344 case SERVICE_CONTROL_INTERROGATE
:
346 case SERVICE_CONTROL_SHUTDOWN
:
347 case SERVICE_CONTROL_STOP
:
348 svc_status
.dwCurrentState
= SERVICE_STOP_PENDING
;
349 svc_status
.dwWaitHint
= 2000;
355 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
356 printf("SetServiceStatus() failed: %d\n",
357 (int) GetLastError());
362 static void WINAPI
service_start(DWORD argc
, LPTSTR
*argv
)
366 svc_status_handle
= RegisterServiceCtrlHandler(WPASVC_NAME
,
367 service_ctrl_handler
);
368 if (svc_status_handle
== (SERVICE_STATUS_HANDLE
) 0) {
369 printf("RegisterServiceCtrlHandler failed: %d\n",
370 (int) GetLastError());
374 os_memset(&svc_status
, 0, sizeof(svc_status
));
375 svc_status
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
376 svc_status
.dwCurrentState
= SERVICE_START_PENDING
;
377 svc_status
.dwWaitHint
= 1000;
379 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
380 printf("SetServiceStatus() failed: %d\n",
381 (int) GetLastError());
385 kill_svc
= CreateEvent(0, TRUE
, FALSE
, 0);
387 printf("CreateEvent failed: %d\n", (int) GetLastError());
391 if (CreateThread(0, 0, (LPTHREAD_START_ROUTINE
) svc_thread
, 0, 0, &id
)
393 printf("CreateThread failed: %d\n", (int) GetLastError());
397 if (svc_status
.dwCurrentState
== SERVICE_START_PENDING
) {
398 svc_status
.dwCurrentState
= SERVICE_RUNNING
;
399 svc_status
.dwWaitHint
= 0;
400 svc_status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
|
401 SERVICE_ACCEPT_SHUTDOWN
;
404 if (!SetServiceStatus(svc_status_handle
, &svc_status
)) {
405 printf("SetServiceStatus() failed: %d\n",
406 (int) GetLastError());
410 /* wait until service gets killed */
411 WaitForSingleObject(kill_svc
, INFINITE
);
415 int main(int argc
, char *argv
[])
417 SERVICE_TABLE_ENTRY dt
[] = {
418 { WPASVC_NAME
, service_start
},
423 if (os_strcmp(argv
[1], "reg") == 0) {
428 path
= os_malloc(MAX_PATH
* sizeof(TCHAR
));
431 if (!GetModuleFileName(NULL
, path
, MAX_PATH
)) {
432 printf("GetModuleFileName failed: "
433 "%d\n", (int) GetLastError());
438 path
= wpa_strdup_tchar(argv
[2]);
442 ret
= register_service(path
);
445 } else if (os_strcmp(argv
[1], "unreg") == 0) {
446 return unregister_service();
447 } else if (os_strcmp(argv
[1], "app") == 0) {
448 return wpa_supplicant_thread();
452 if (!StartServiceCtrlDispatcher(dt
)) {
453 printf("StartServiceCtrlDispatcher failed: %d\n",
454 (int) GetLastError());