您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

main_winsvc.c 11KB

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