Added debug_timestamp option to Windows registry
[mech_eap.git] / wpa_supplicant / main_winsvc.c
1 /*
2  * WPA Supplicant / main() function for Win32 service
3  * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
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.
19  *
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
26  * 'wpasvc.exe unreg'.
27  */
28
29 #include "includes.h"
30 #include <windows.h>
31
32 #include "common.h"
33 #include "wpa_supplicant_i.h"
34 #include "eloop.h"
35
36 #ifndef WPASVC_NAME
37 #define WPASVC_NAME TEXT("wpasvc")
38 #endif
39 #ifndef WPASVC_DISPLAY_NAME
40 #define WPASVC_DISPLAY_NAME TEXT("wpa_supplicant service")
41 #endif
42 #ifndef WPASVC_DESCRIPTION
43 #define WPASVC_DESCRIPTION \
44 TEXT("Provides IEEE 802.1X and WPA/WPA2 supplicant functionality")
45 #endif
46
47 static HANDLE kill_svc;
48
49 static SERVICE_STATUS_HANDLE svc_status_handle;
50 static SERVICE_STATUS svc_status;
51
52
53 #ifndef WPA_KEY_ROOT
54 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
55 #endif
56 #ifndef WPA_KEY_PREFIX
57 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
58 #endif
59
60 #ifdef UNICODE
61 #define TSTR "%S"
62 #else /* UNICODE */
63 #define TSTR "%s"
64 #endif /* UNICODE */
65
66
67 static int read_interface(struct wpa_global *global, HKEY _hk,
68                           const TCHAR *name)
69 {
70         HKEY hk;
71 #define TBUFLEN 255
72         TCHAR adapter[TBUFLEN], config[TBUFLEN], ctrl_interface[TBUFLEN];
73         DWORD buflen;
74         LONG ret;
75         struct wpa_interface iface;
76
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");
80                 return -1;
81         }
82
83         os_memset(&iface, 0, sizeof(iface));
84         iface.driver = "ndis";
85
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;
95         }
96
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;
106         }
107
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;
117         }
118
119         RegCloseKey(hk);
120
121         if (wpa_supplicant_add_iface(global, &iface) == NULL)
122                 return -1;
123
124         return 0;
125 }
126
127
128 static int wpa_supplicant_thread(void)
129 {
130         int exitcode;
131         struct wpa_params params;
132         struct wpa_global *global;
133         HKEY hk, ihk;
134         DWORD val, buflen, i;
135         LONG ret;
136
137         if (os_program_init())
138                 return -1;
139
140         os_memset(&params, 0, sizeof(params));
141         params.wpa_debug_level = MSG_INFO;
142
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");
147                 return -1;
148         }
149
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;
155         }
156
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;
162         }
163
164         buflen = sizeof(val);
165         ret = RegQueryValueEx(hk, TEXT("debug_timestamp"), NULL, NULL,
166                               (LPBYTE) &val, &buflen);
167         if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
168                 params.wpa_debug_timestamp = val;
169         }
170
171         buflen = sizeof(val);
172         ret = RegQueryValueEx(hk, TEXT("debug_use_file"), NULL, NULL,
173                               (LPBYTE) &val, &buflen);
174         if (ret == ERROR_SUCCESS && buflen == sizeof(val) && val) {
175                 params.wpa_debug_file_path = "\\Temp\\wpa_supplicant-log.txt";
176         }
177
178         exitcode = 0;
179         global = wpa_supplicant_init(&params);
180         if (global == NULL) {
181                 printf("Failed to initialize wpa_supplicant\n");
182                 exitcode = -1;
183         }
184
185         ret = RegOpenKeyEx(hk, TEXT("interfaces"), 0, KEY_ENUMERATE_SUB_KEYS,
186                            &ihk);
187         RegCloseKey(hk);
188         if (ret != ERROR_SUCCESS) {
189                 printf("Could not open wpa_supplicant interfaces registry "
190                        "key\n");
191                 return -1;
192         }
193
194         for (i = 0; ; i++) {
195                 TCHAR name[255];
196                 DWORD namelen;
197
198                 namelen = 255;
199                 ret = RegEnumKeyEx(ihk, i, name, &namelen, NULL, NULL, NULL,
200                                    NULL);
201
202                 if (ret == ERROR_NO_MORE_ITEMS)
203                         break;
204
205                 if (ret != ERROR_SUCCESS) {
206                         printf("RegEnumKeyEx failed: 0x%x\n",
207                                (unsigned int) ret);
208                         break;
209                 }
210
211                 if (namelen >= 255)
212                         namelen = 255 - 1;
213                 name[namelen] = '\0';
214
215                 wpa_printf(MSG_DEBUG, "interface %d: %s\n", (int) i, name);
216                 if (read_interface(global, ihk, name) < 0)
217                         exitcode = -1;
218         }
219
220         RegCloseKey(ihk);
221
222         if (exitcode == 0)
223                 exitcode = wpa_supplicant_run(global);
224
225         wpa_supplicant_deinit(global);
226
227         os_program_deinit();
228
229         return exitcode;
230 }
231
232
233 static DWORD svc_thread(LPDWORD param)
234 {
235         int ret = wpa_supplicant_thread();
236
237         svc_status.dwCurrentState = SERVICE_STOPPED;
238         svc_status.dwWaitHint = 0;
239         if (!SetServiceStatus(svc_status_handle, &svc_status)) {
240                 printf("SetServiceStatus() failed: %d\n",
241                        (int) GetLastError());
242         }
243
244         return ret;
245 }
246
247
248 static int register_service(const TCHAR *exe)
249 {
250         SC_HANDLE svc, scm;
251         SERVICE_DESCRIPTION sd;
252
253         printf("Registering service: " TSTR "\n", WPASVC_NAME);
254
255         scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
256         if (!scm) {
257                 printf("OpenSCManager failed: %d\n", (int) GetLastError());
258                 return -1;
259         }
260
261         svc = CreateService(scm, WPASVC_NAME, WPASVC_DISPLAY_NAME,
262                             SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
263                             SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
264                             exe, NULL, NULL, NULL, NULL, NULL);
265
266         if (!svc) {
267                 printf("CreateService failed: %d\n\n", (int) GetLastError());
268                 CloseServiceHandle(scm);
269                 return -1;
270         }
271
272         os_memset(&sd, 0, sizeof(sd));
273         sd.lpDescription = WPASVC_DESCRIPTION;
274         if (!ChangeServiceConfig2(svc, SERVICE_CONFIG_DESCRIPTION, &sd)) {
275                 printf("ChangeServiceConfig2 failed: %d\n",
276                        (int) GetLastError());
277                 /* This is not a fatal error, so continue anyway. */
278         }
279
280         CloseServiceHandle(svc);
281         CloseServiceHandle(scm);
282
283         printf("Service registered successfully.\n");
284
285         return 0;
286 }
287
288
289 static int unregister_service(void)
290 {
291         SC_HANDLE svc, scm;
292         SERVICE_STATUS status;
293
294         printf("Unregistering service: " TSTR "\n", WPASVC_NAME);
295
296         scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
297         if (!scm) {
298                 printf("OpenSCManager failed: %d\n", (int) GetLastError());
299                 return -1;
300         }
301
302         svc = OpenService(scm, WPASVC_NAME, SERVICE_ALL_ACCESS | DELETE);
303         if (!svc) {
304                 printf("OpenService failed: %d\n\n", (int) GetLastError());
305                 CloseServiceHandle(scm);
306                 return -1;
307         }
308
309         if (QueryServiceStatus(svc, &status)) {
310                 if (status.dwCurrentState != SERVICE_STOPPED) {
311                         printf("Service currently active - stopping "
312                                "service...\n");
313                         if (!ControlService(svc, SERVICE_CONTROL_STOP,
314                                             &status)) {
315                                 printf("ControlService failed: %d\n",
316                                        (int) GetLastError());
317                         }
318                         Sleep(500);
319                 }
320         }
321
322         if (DeleteService(svc)) {
323                 printf("Service unregistered successfully.\n");
324         } else {
325                 printf("DeleteService failed: %d\n", (int) GetLastError());
326         }
327
328         CloseServiceHandle(svc);
329         CloseServiceHandle(scm);
330
331         return 0;
332 }
333
334
335 static void WINAPI service_ctrl_handler(DWORD control_code)
336 {
337         switch (control_code) {
338         case SERVICE_CONTROL_INTERROGATE:
339                 break;
340         case SERVICE_CONTROL_SHUTDOWN:
341         case SERVICE_CONTROL_STOP:
342                 svc_status.dwCurrentState = SERVICE_STOP_PENDING;
343                 svc_status.dwWaitHint = 2000;
344                 eloop_terminate();
345                 SetEvent(kill_svc);
346                 break;
347         }
348
349         if (!SetServiceStatus(svc_status_handle, &svc_status)) {
350                 printf("SetServiceStatus() failed: %d\n",
351                        (int) GetLastError());
352         }
353 }
354
355
356 static void WINAPI service_start(DWORD argc, LPTSTR *argv)
357 {
358         DWORD id;
359
360         svc_status_handle = RegisterServiceCtrlHandler(WPASVC_NAME,
361                                                        service_ctrl_handler);
362         if (svc_status_handle == (SERVICE_STATUS_HANDLE) 0) {
363                 printf("RegisterServiceCtrlHandler failed: %d\n",
364                        (int) GetLastError());
365                 return;
366         }
367
368         os_memset(&svc_status, 0, sizeof(svc_status));
369         svc_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
370         svc_status.dwCurrentState = SERVICE_START_PENDING;
371         svc_status.dwWaitHint = 1000;
372
373         if (!SetServiceStatus(svc_status_handle, &svc_status)) {
374                 printf("SetServiceStatus() failed: %d\n",
375                        (int) GetLastError());
376                 return;
377         }
378
379         kill_svc = CreateEvent(0, TRUE, FALSE, 0);
380         if (!kill_svc) {
381                 printf("CreateEvent failed: %d\n", (int) GetLastError());
382                 return;
383         }
384
385         if (CreateThread(0, 0, (LPTHREAD_START_ROUTINE) svc_thread, 0, 0, &id)
386             == 0) {
387                 printf("CreateThread failed: %d\n", (int) GetLastError());
388                 return;
389         }
390
391         if (svc_status.dwCurrentState == SERVICE_START_PENDING) {
392                 svc_status.dwCurrentState = SERVICE_RUNNING;
393                 svc_status.dwWaitHint = 0;
394                 svc_status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
395                         SERVICE_ACCEPT_SHUTDOWN;
396         }
397
398         if (!SetServiceStatus(svc_status_handle, &svc_status)) {
399                 printf("SetServiceStatus() failed: %d\n",
400                        (int) GetLastError());
401                 return;
402         }
403
404         /* wait until service gets killed */
405         WaitForSingleObject(kill_svc, INFINITE);
406 }
407
408
409 int main(int argc, char *argv[])
410 {
411         SERVICE_TABLE_ENTRY dt[] = {
412                 { WPASVC_NAME, service_start },
413                 { NULL, NULL }
414         };
415
416         if (argc > 1) {
417                 if (os_strcmp(argv[1], "reg") == 0) {
418                         TCHAR *path;
419                         int ret;
420
421                         if (argc < 3) {
422                                 path = os_malloc(MAX_PATH * sizeof(TCHAR));
423                                 if (path == NULL)
424                                         return -1;
425                                 if (!GetModuleFileName(NULL, path, MAX_PATH)) {
426                                         printf("GetModuleFileName failed: "
427                                                "%d\n", (int) GetLastError());
428                                         os_free(path);
429                                         return -1;
430                                 }
431                         } else {
432                                 path = wpa_strdup_tchar(argv[2]);
433                                 if (path == NULL)
434                                         return -1;
435                         }
436                         ret = register_service(path);
437                         os_free(path);
438                         return ret;
439                 } else if (os_strcmp(argv[1], "unreg") == 0) {
440                         return unregister_service();
441                 } else if (os_strcmp(argv[1], "app") == 0) {
442                         return wpa_supplicant_thread();
443                 }
444         }
445
446         if (!StartServiceCtrlDispatcher(dt)) {
447                 printf("StartServiceCtrlDispatcher failed: %d\n",
448                        (int) GetLastError());
449         }
450
451         return 0;
452 }