main_winsvc.c 11 KB

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