main_winsvc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_timestamp"), NULL, NULL,
  140. (LPBYTE) &val, &buflen);
  141. if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
  142. params.wpa_debug_timestamp = val;
  143. }
  144. buflen = sizeof(val);
  145. ret = RegQueryValueEx(hk, TEXT("debug_use_file"), NULL, NULL,
  146. (LPBYTE) &val, &buflen);
  147. if (ret == ERROR_SUCCESS && buflen == sizeof(val) && val) {
  148. params.wpa_debug_file_path = "\\Temp\\wpa_supplicant-log.txt";
  149. }
  150. exitcode = 0;
  151. global = wpa_supplicant_init(&params);
  152. if (global == NULL) {
  153. printf("Failed to initialize wpa_supplicant\n");
  154. exitcode = -1;
  155. }
  156. ret = RegOpenKeyEx(hk, TEXT("interfaces"), 0, KEY_ENUMERATE_SUB_KEYS,
  157. &ihk);
  158. RegCloseKey(hk);
  159. if (ret != ERROR_SUCCESS) {
  160. printf("Could not open wpa_supplicant interfaces registry "
  161. "key\n");
  162. return -1;
  163. }
  164. for (i = 0; ; i++) {
  165. TCHAR name[255];
  166. DWORD namelen;
  167. namelen = 255;
  168. ret = RegEnumKeyEx(ihk, i, name, &namelen, NULL, NULL, NULL,
  169. NULL);
  170. if (ret == ERROR_NO_MORE_ITEMS)
  171. break;
  172. if (ret != ERROR_SUCCESS) {
  173. printf("RegEnumKeyEx failed: 0x%x\n",
  174. (unsigned int) ret);
  175. break;
  176. }
  177. if (namelen >= 255)
  178. namelen = 255 - 1;
  179. name[namelen] = '\0';
  180. wpa_printf(MSG_DEBUG, "interface %d: %s\n", (int) i, name);
  181. if (read_interface(global, ihk, name) < 0)
  182. exitcode = -1;
  183. }
  184. RegCloseKey(ihk);
  185. if (exitcode == 0)
  186. exitcode = wpa_supplicant_run(global);
  187. wpa_supplicant_deinit(global);
  188. os_program_deinit();
  189. return exitcode;
  190. }
  191. static DWORD svc_thread(LPDWORD param)
  192. {
  193. int ret = wpa_supplicant_thread();
  194. svc_status.dwCurrentState = SERVICE_STOPPED;
  195. svc_status.dwWaitHint = 0;
  196. if (!SetServiceStatus(svc_status_handle, &svc_status)) {
  197. printf("SetServiceStatus() failed: %d\n",
  198. (int) GetLastError());
  199. }
  200. return ret;
  201. }
  202. static int register_service(const TCHAR *exe)
  203. {
  204. SC_HANDLE svc, scm;
  205. SERVICE_DESCRIPTION sd;
  206. printf("Registering service: " TSTR "\n", WPASVC_NAME);
  207. scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
  208. if (!scm) {
  209. printf("OpenSCManager failed: %d\n", (int) GetLastError());
  210. return -1;
  211. }
  212. svc = CreateService(scm, WPASVC_NAME, WPASVC_DISPLAY_NAME,
  213. SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
  214. SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
  215. exe, NULL, NULL, NULL, NULL, NULL);
  216. if (!svc) {
  217. printf("CreateService failed: %d\n\n", (int) GetLastError());
  218. CloseServiceHandle(scm);
  219. return -1;
  220. }
  221. os_memset(&sd, 0, sizeof(sd));
  222. sd.lpDescription = WPASVC_DESCRIPTION;
  223. if (!ChangeServiceConfig2(svc, SERVICE_CONFIG_DESCRIPTION, &sd)) {
  224. printf("ChangeServiceConfig2 failed: %d\n",
  225. (int) GetLastError());
  226. /* This is not a fatal error, so continue anyway. */
  227. }
  228. CloseServiceHandle(svc);
  229. CloseServiceHandle(scm);
  230. printf("Service registered successfully.\n");
  231. return 0;
  232. }
  233. static int unregister_service(void)
  234. {
  235. SC_HANDLE svc, scm;
  236. SERVICE_STATUS status;
  237. printf("Unregistering service: " TSTR "\n", WPASVC_NAME);
  238. scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
  239. if (!scm) {
  240. printf("OpenSCManager failed: %d\n", (int) GetLastError());
  241. return -1;
  242. }
  243. svc = OpenService(scm, WPASVC_NAME, SERVICE_ALL_ACCESS | DELETE);
  244. if (!svc) {
  245. printf("OpenService failed: %d\n\n", (int) GetLastError());
  246. CloseServiceHandle(scm);
  247. return -1;
  248. }
  249. if (QueryServiceStatus(svc, &status)) {
  250. if (status.dwCurrentState != SERVICE_STOPPED) {
  251. printf("Service currently active - stopping "
  252. "service...\n");
  253. if (!ControlService(svc, SERVICE_CONTROL_STOP,
  254. &status)) {
  255. printf("ControlService failed: %d\n",
  256. (int) GetLastError());
  257. }
  258. Sleep(500);
  259. }
  260. }
  261. if (DeleteService(svc)) {
  262. printf("Service unregistered successfully.\n");
  263. } else {
  264. printf("DeleteService failed: %d\n", (int) GetLastError());
  265. }
  266. CloseServiceHandle(svc);
  267. CloseServiceHandle(scm);
  268. return 0;
  269. }
  270. static void WINAPI service_ctrl_handler(DWORD control_code)
  271. {
  272. switch (control_code) {
  273. case SERVICE_CONTROL_INTERROGATE:
  274. break;
  275. case SERVICE_CONTROL_SHUTDOWN:
  276. case SERVICE_CONTROL_STOP:
  277. svc_status.dwCurrentState = SERVICE_STOP_PENDING;
  278. svc_status.dwWaitHint = 2000;
  279. eloop_terminate();
  280. SetEvent(kill_svc);
  281. break;
  282. }
  283. if (!SetServiceStatus(svc_status_handle, &svc_status)) {
  284. printf("SetServiceStatus() failed: %d\n",
  285. (int) GetLastError());
  286. }
  287. }
  288. static void WINAPI service_start(DWORD argc, LPTSTR *argv)
  289. {
  290. DWORD id;
  291. svc_status_handle = RegisterServiceCtrlHandler(WPASVC_NAME,
  292. service_ctrl_handler);
  293. if (svc_status_handle == (SERVICE_STATUS_HANDLE) 0) {
  294. printf("RegisterServiceCtrlHandler failed: %d\n",
  295. (int) GetLastError());
  296. return;
  297. }
  298. os_memset(&svc_status, 0, sizeof(svc_status));
  299. svc_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  300. svc_status.dwCurrentState = SERVICE_START_PENDING;
  301. svc_status.dwWaitHint = 1000;
  302. if (!SetServiceStatus(svc_status_handle, &svc_status)) {
  303. printf("SetServiceStatus() failed: %d\n",
  304. (int) GetLastError());
  305. return;
  306. }
  307. kill_svc = CreateEvent(0, TRUE, FALSE, 0);
  308. if (!kill_svc) {
  309. printf("CreateEvent failed: %d\n", (int) GetLastError());
  310. return;
  311. }
  312. if (CreateThread(0, 0, (LPTHREAD_START_ROUTINE) svc_thread, 0, 0, &id)
  313. == 0) {
  314. printf("CreateThread failed: %d\n", (int) GetLastError());
  315. return;
  316. }
  317. if (svc_status.dwCurrentState == SERVICE_START_PENDING) {
  318. svc_status.dwCurrentState = SERVICE_RUNNING;
  319. svc_status.dwWaitHint = 0;
  320. svc_status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
  321. SERVICE_ACCEPT_SHUTDOWN;
  322. }
  323. if (!SetServiceStatus(svc_status_handle, &svc_status)) {
  324. printf("SetServiceStatus() failed: %d\n",
  325. (int) GetLastError());
  326. return;
  327. }
  328. /* wait until service gets killed */
  329. WaitForSingleObject(kill_svc, INFINITE);
  330. }
  331. int main(int argc, char *argv[])
  332. {
  333. SERVICE_TABLE_ENTRY dt[] = {
  334. { WPASVC_NAME, service_start },
  335. { NULL, NULL }
  336. };
  337. if (argc > 1) {
  338. if (os_strcmp(argv[1], "reg") == 0) {
  339. TCHAR *path;
  340. int ret;
  341. if (argc < 3) {
  342. path = os_malloc(MAX_PATH * sizeof(TCHAR));
  343. if (path == NULL)
  344. return -1;
  345. if (!GetModuleFileName(NULL, path, MAX_PATH)) {
  346. printf("GetModuleFileName failed: "
  347. "%d\n", (int) GetLastError());
  348. os_free(path);
  349. return -1;
  350. }
  351. } else {
  352. path = wpa_strdup_tchar(argv[2]);
  353. if (path == NULL)
  354. return -1;
  355. }
  356. ret = register_service(path);
  357. os_free(path);
  358. return ret;
  359. } else if (os_strcmp(argv[1], "unreg") == 0) {
  360. return unregister_service();
  361. } else if (os_strcmp(argv[1], "app") == 0) {
  362. return wpa_supplicant_thread();
  363. }
  364. }
  365. if (!StartServiceCtrlDispatcher(dt)) {
  366. printf("StartServiceCtrlDispatcher failed: %d\n",
  367. (int) GetLastError());
  368. }
  369. return 0;
  370. }