main_winsvc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. #include "includes.h"
  29. #include <windows.h>
  30. #include "common.h"
  31. #include "wpa_supplicant_i.h"
  32. #include "eloop.h"
  33. #ifndef WPASVC_NAME
  34. #define WPASVC_NAME TEXT("wpasvc")
  35. #endif
  36. #ifndef WPASVC_DISPLAY_NAME
  37. #define WPASVC_DISPLAY_NAME TEXT("wpa_supplicant service")
  38. #endif
  39. #ifndef WPASVC_DESCRIPTION
  40. #define WPASVC_DESCRIPTION \
  41. TEXT("Provides IEEE 802.1X and WPA/WPA2 supplicant functionality")
  42. #endif
  43. static HANDLE kill_svc;
  44. static SERVICE_STATUS_HANDLE svc_status_handle;
  45. static SERVICE_STATUS svc_status;
  46. #ifndef WPA_KEY_ROOT
  47. #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
  48. #endif
  49. #ifndef WPA_KEY_PREFIX
  50. #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
  51. #endif
  52. #ifdef UNICODE
  53. #define TSTR "%S"
  54. #else /* UNICODE */
  55. #define TSTR "%s"
  56. #endif /* UNICODE */
  57. static int read_interface(struct wpa_global *global, HKEY _hk,
  58. const TCHAR *name)
  59. {
  60. HKEY hk;
  61. #define TBUFLEN 255
  62. TCHAR adapter[TBUFLEN], config[TBUFLEN], ctrl_interface[TBUFLEN];
  63. DWORD buflen;
  64. LONG ret;
  65. struct wpa_interface iface;
  66. ret = RegOpenKeyEx(_hk, name, 0, KEY_QUERY_VALUE, &hk);
  67. if (ret != ERROR_SUCCESS) {
  68. printf("Could not open wpa_supplicant interface key\n");
  69. return -1;
  70. }
  71. os_memset(&iface, 0, sizeof(iface));
  72. iface.driver = "ndis";
  73. buflen = sizeof(ctrl_interface);
  74. ret = RegQueryValueEx(hk, TEXT("ctrl_interface"), NULL, NULL,
  75. (LPBYTE) ctrl_interface, &buflen);
  76. if (ret == ERROR_SUCCESS) {
  77. ctrl_interface[TBUFLEN - 1] = TEXT('\0');
  78. wpa_unicode2ascii_inplace(ctrl_interface);
  79. printf("ctrl_interface[len=%d] '%s'\n",
  80. (int) buflen, (char *) ctrl_interface);
  81. iface.ctrl_interface = (char *) ctrl_interface;
  82. }
  83. buflen = sizeof(adapter);
  84. ret = RegQueryValueEx(hk, TEXT("adapter"), NULL, NULL,
  85. (LPBYTE) adapter, &buflen);
  86. if (ret == ERROR_SUCCESS) {
  87. adapter[TBUFLEN - 1] = TEXT('\0');
  88. wpa_unicode2ascii_inplace(adapter);
  89. printf("adapter[len=%d] '%s'\n",
  90. (int) buflen, (char *) adapter);
  91. iface.ifname = (char *) adapter;
  92. }
  93. buflen = sizeof(config);
  94. ret = RegQueryValueEx(hk, TEXT("config"), NULL, NULL,
  95. (LPBYTE) config, &buflen);
  96. if (ret == ERROR_SUCCESS) {
  97. config[sizeof(config) - 1] = '\0';
  98. wpa_unicode2ascii_inplace(config);
  99. printf("config[len=%d] '%s'\n",
  100. (int) buflen, (char *) config);
  101. iface.confname = (char *) config;
  102. }
  103. RegCloseKey(hk);
  104. if (wpa_supplicant_add_iface(global, &iface) == NULL)
  105. return -1;
  106. return 0;
  107. }
  108. static int wpa_supplicant_thread(void)
  109. {
  110. int exitcode;
  111. struct wpa_params params;
  112. struct wpa_global *global;
  113. HKEY hk, ihk;
  114. DWORD val, buflen, i;
  115. LONG ret;
  116. if (os_program_init())
  117. return -1;
  118. os_memset(&params, 0, sizeof(params));
  119. params.wpa_debug_level = MSG_INFO;
  120. ret = RegOpenKeyEx(WPA_KEY_ROOT, WPA_KEY_PREFIX,
  121. 0, KEY_QUERY_VALUE, &hk);
  122. if (ret != ERROR_SUCCESS) {
  123. printf("Could not open wpa_supplicant registry key\n");
  124. return -1;
  125. }
  126. buflen = sizeof(val);
  127. ret = RegQueryValueEx(hk, TEXT("debug_level"), NULL, NULL,
  128. (LPBYTE) &val, &buflen);
  129. if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
  130. params.wpa_debug_level = val;
  131. }
  132. buflen = sizeof(val);
  133. ret = RegQueryValueEx(hk, TEXT("debug_show_keys"), NULL, NULL,
  134. (LPBYTE) &val, &buflen);
  135. if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
  136. params.wpa_debug_show_keys = val;
  137. }
  138. buflen = sizeof(val);
  139. ret = RegQueryValueEx(hk, TEXT("debug_use_file"), NULL, NULL,
  140. (LPBYTE) &val, &buflen);
  141. if (ret == ERROR_SUCCESS && buflen == sizeof(val) && val) {
  142. params.wpa_debug_file_path = "\\Temp\\wpa_supplicant-log.txt";
  143. }
  144. exitcode = 0;
  145. global = wpa_supplicant_init(&params);
  146. if (global == NULL) {
  147. printf("Failed to initialize wpa_supplicant\n");
  148. exitcode = -1;
  149. }
  150. ret = RegOpenKeyEx(hk, TEXT("interfaces"), 0, KEY_ENUMERATE_SUB_KEYS,
  151. &ihk);
  152. RegCloseKey(hk);
  153. if (ret != ERROR_SUCCESS) {
  154. printf("Could not open wpa_supplicant interfaces registry "
  155. "key\n");
  156. return -1;
  157. }
  158. for (i = 0; ; i++) {
  159. TCHAR name[255];
  160. DWORD namelen;
  161. namelen = 255;
  162. ret = RegEnumKeyEx(ihk, i, name, &namelen, NULL, NULL, NULL,
  163. NULL);
  164. if (ret == ERROR_NO_MORE_ITEMS)
  165. break;
  166. if (ret != ERROR_SUCCESS) {
  167. printf("RegEnumKeyEx failed: 0x%x\n",
  168. (unsigned int) ret);
  169. break;
  170. }
  171. if (namelen >= 255)
  172. namelen = 255 - 1;
  173. name[namelen] = '\0';
  174. wpa_printf(MSG_DEBUG, "interface %d: %s\n", (int) i, name);
  175. if (read_interface(global, ihk, name) < 0)
  176. exitcode = -1;
  177. }
  178. RegCloseKey(ihk);
  179. if (exitcode == 0)
  180. exitcode = wpa_supplicant_run(global);
  181. wpa_supplicant_deinit(global);
  182. os_program_deinit();
  183. return exitcode;
  184. }
  185. static DWORD svc_thread(LPDWORD param)
  186. {
  187. int ret = wpa_supplicant_thread();
  188. svc_status.dwCurrentState = SERVICE_STOPPED;
  189. svc_status.dwWaitHint = 0;
  190. if (!SetServiceStatus(svc_status_handle, &svc_status)) {
  191. printf("SetServiceStatus() failed: %d\n",
  192. (int) GetLastError());
  193. }
  194. return ret;
  195. }
  196. static int register_service(const TCHAR *exe)
  197. {
  198. SC_HANDLE svc, scm;
  199. SERVICE_DESCRIPTION sd;
  200. printf("Registering service: " TSTR "\n", WPASVC_NAME);
  201. scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
  202. if (!scm) {
  203. printf("OpenSCManager failed: %d\n", (int) GetLastError());
  204. return -1;
  205. }
  206. svc = CreateService(scm, WPASVC_NAME, WPASVC_DISPLAY_NAME,
  207. SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
  208. SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
  209. exe, NULL, NULL, NULL, NULL, NULL);
  210. if (!svc) {
  211. printf("CreateService failed: %d\n\n", (int) GetLastError());
  212. CloseServiceHandle(scm);
  213. return -1;
  214. }
  215. os_memset(&sd, 0, sizeof(sd));
  216. sd.lpDescription = WPASVC_DESCRIPTION;
  217. if (!ChangeServiceConfig2(svc, SERVICE_CONFIG_DESCRIPTION, &sd)) {
  218. printf("ChangeServiceConfig2 failed: %d\n",
  219. (int) GetLastError());
  220. /* This is not a fatal error, so continue anyway. */
  221. }
  222. CloseServiceHandle(svc);
  223. CloseServiceHandle(scm);
  224. printf("Service registered successfully.\n");
  225. return 0;
  226. }
  227. static int unregister_service(void)
  228. {
  229. SC_HANDLE svc, scm;
  230. SERVICE_STATUS status;
  231. printf("Unregistering service: " TSTR "\n", WPASVC_NAME);
  232. scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
  233. if (!scm) {
  234. printf("OpenSCManager failed: %d\n", (int) GetLastError());
  235. return -1;
  236. }
  237. svc = OpenService(scm, WPASVC_NAME, SERVICE_ALL_ACCESS | DELETE);
  238. if (!svc) {
  239. printf("OpenService failed: %d\n\n", (int) GetLastError());
  240. CloseServiceHandle(scm);
  241. return -1;
  242. }
  243. if (QueryServiceStatus(svc, &status)) {
  244. if (status.dwCurrentState != SERVICE_STOPPED) {
  245. printf("Service currently active - stopping "
  246. "service...\n");
  247. if (!ControlService(svc, SERVICE_CONTROL_STOP,
  248. &status)) {
  249. printf("ControlService failed: %d\n",
  250. (int) GetLastError());
  251. }
  252. Sleep(500);
  253. }
  254. }
  255. if (DeleteService(svc)) {
  256. printf("Service unregistered successfully.\n");
  257. } else {
  258. printf("DeleteService failed: %d\n", (int) GetLastError());
  259. }
  260. CloseServiceHandle(svc);
  261. CloseServiceHandle(scm);
  262. return 0;
  263. }
  264. static void WINAPI service_ctrl_handler(DWORD control_code)
  265. {
  266. switch (control_code) {
  267. case SERVICE_CONTROL_INTERROGATE:
  268. break;
  269. case SERVICE_CONTROL_SHUTDOWN:
  270. case SERVICE_CONTROL_STOP:
  271. svc_status.dwCurrentState = SERVICE_STOP_PENDING;
  272. svc_status.dwWaitHint = 2000;
  273. eloop_terminate();
  274. SetEvent(kill_svc);
  275. break;
  276. }
  277. if (!SetServiceStatus(svc_status_handle, &svc_status)) {
  278. printf("SetServiceStatus() failed: %d\n",
  279. (int) GetLastError());
  280. }
  281. }
  282. static void WINAPI service_start(DWORD argc, LPTSTR *argv)
  283. {
  284. DWORD id;
  285. svc_status_handle = RegisterServiceCtrlHandler(WPASVC_NAME,
  286. service_ctrl_handler);
  287. if (svc_status_handle == (SERVICE_STATUS_HANDLE) 0) {
  288. printf("RegisterServiceCtrlHandler failed: %d\n",
  289. (int) GetLastError());
  290. return;
  291. }
  292. os_memset(&svc_status, 0, sizeof(svc_status));
  293. svc_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  294. svc_status.dwCurrentState = SERVICE_START_PENDING;
  295. svc_status.dwWaitHint = 1000;
  296. if (!SetServiceStatus(svc_status_handle, &svc_status)) {
  297. printf("SetServiceStatus() failed: %d\n",
  298. (int) GetLastError());
  299. return;
  300. }
  301. kill_svc = CreateEvent(0, TRUE, FALSE, 0);
  302. if (!kill_svc) {
  303. printf("CreateEvent failed: %d\n", (int) GetLastError());
  304. return;
  305. }
  306. if (CreateThread(0, 0, (LPTHREAD_START_ROUTINE) svc_thread, 0, 0, &id)
  307. == 0) {
  308. printf("CreateThread failed: %d\n", (int) GetLastError());
  309. return;
  310. }
  311. if (svc_status.dwCurrentState == SERVICE_START_PENDING) {
  312. svc_status.dwCurrentState = SERVICE_RUNNING;
  313. svc_status.dwWaitHint = 0;
  314. svc_status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
  315. SERVICE_ACCEPT_SHUTDOWN;
  316. }
  317. if (!SetServiceStatus(svc_status_handle, &svc_status)) {
  318. printf("SetServiceStatus() failed: %d\n",
  319. (int) GetLastError());
  320. return;
  321. }
  322. /* wait until service gets killed */
  323. WaitForSingleObject(kill_svc, INFINITE);
  324. }
  325. int main(int argc, char *argv[])
  326. {
  327. SERVICE_TABLE_ENTRY dt[] = {
  328. { WPASVC_NAME, service_start },
  329. { NULL, NULL }
  330. };
  331. if (argc > 1) {
  332. if (os_strcmp(argv[1], "reg") == 0) {
  333. TCHAR *path;
  334. int ret;
  335. if (argc < 3) {
  336. path = os_malloc(MAX_PATH * sizeof(TCHAR));
  337. if (path == NULL)
  338. return -1;
  339. if (!GetModuleFileName(NULL, path, MAX_PATH)) {
  340. printf("GetModuleFileName failed: "
  341. "%d\n", (int) GetLastError());
  342. os_free(path);
  343. return -1;
  344. }
  345. } else {
  346. path = wpa_strdup_tchar(argv[2]);
  347. if (path == NULL)
  348. return -1;
  349. }
  350. ret = register_service(path);
  351. os_free(path);
  352. return ret;
  353. } else if (os_strcmp(argv[1], "unreg") == 0) {
  354. return unregister_service();
  355. } else if (os_strcmp(argv[1], "app") == 0) {
  356. return wpa_supplicant_thread();
  357. }
  358. }
  359. if (!StartServiceCtrlDispatcher(dt)) {
  360. printf("StartServiceCtrlDispatcher failed: %d\n",
  361. (int) GetLastError());
  362. }
  363. return 0;
  364. }