Imported Upstream version 0.3.3
[anytun.git] / src / win32 / winService.cpp
1 /*\r
2  *  anytun\r
3  *\r
4  *  The secure anycast tunneling protocol (satp) defines a protocol used\r
5  *  for communication between any combination of unicast and anycast\r
6  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel\r
7  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.\r
8  *  ethernet, ip, arp ...). satp directly includes cryptography and\r
9  *  message authentication based on the methodes used by SRTP.  It is\r
10  *  intended to deliver a generic, scaleable and secure solution for\r
11  *  tunneling and relaying of packets of any protocol.\r
12  *\r
13  *\r
14  *  Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl, \r
15  *                          Christian Pointner <satp@wirdorange.org>\r
16  *\r
17  *  This file is part of Anytun.\r
18  *\r
19  *  Anytun is free software: you can redistribute it and/or modify\r
20  *  it under the terms of the GNU General Public License as published by\r
21  *  the Free Software Foundation, either version 3 of the License, or\r
22  *  any later version.\r
23  *\r
24  *  Anytun is distributed in the hope that it will be useful,\r
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
27  *  GNU General Public License for more details.\r
28  *\r
29  *  You should have received a copy of the GNU General Public License\r
30  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.\r
31  */\r
32 \r
33 #ifdef WIN_SERVICE\r
34 \r
35 #include <iostream>\r
36 \r
37 #include <windows.h>\r
38 \r
39 #include "winService.h"\r
40 #include "../log.h"\r
41 #include "../anytunError.h"\r
42 #include "../threadUtils.hpp"\r
43 \r
44 void WinService::install()\r
45 {\r
46   SC_HANDLE schSCManager;\r
47   SC_HANDLE schService;\r
48   char szPath[MAX_PATH];\r
49 \r
50   if(!GetModuleFileNameA(NULL, szPath, MAX_PATH))\r
51     AnytunError::throwErr() << "Error on GetModuleFileName: " << AnytunErrno(GetLastError());\r
52 \r
53   schSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);\r
54   if(NULL == schSCManager)\r
55     AnytunError::throwErr() << "Error on OpenSCManager: " << AnytunErrno(GetLastError());\r
56 \r
57   schService = CreateServiceA(schSCManager, SVC_NAME, SVC_NAME, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, \r
58                               SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szPath, NULL, NULL, NULL, NULL, NULL);\r
59   if(schService == NULL) {\r
60     CloseServiceHandle(schSCManager);\r
61     AnytunError::throwErr() << "Error on CreateService: " << AnytunErrno(GetLastError());\r
62   }\r
63 \r
64   std::cout << "Service installed successfully" << std::endl; \r
65 \r
66   CloseServiceHandle(schService); \r
67   CloseServiceHandle(schSCManager);\r
68 }\r
69 \r
70 void WinService::uninstall()\r
71 {\r
72   SC_HANDLE schSCManager;\r
73   SC_HANDLE schService;\r
74 \r
75   schSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);\r
76   if(NULL == schSCManager)\r
77     AnytunError::throwErr() << "Error on OpenSCManager: " << AnytunErrno(GetLastError());\r
78 \r
79   schService = OpenServiceA(schSCManager, SVC_NAME, SERVICE_ALL_ACCESS);\r
80   if(schService == NULL) {\r
81     CloseServiceHandle(schSCManager);\r
82     AnytunError::throwErr() << "Error on CreateService: " << AnytunErrno(GetLastError());\r
83   }\r
84 \r
85   if(!DeleteService(schService)) {\r
86     CloseServiceHandle(schService); \r
87     CloseServiceHandle(schSCManager);\r
88     AnytunError::throwErr() << "Error on DeleteService: " << AnytunErrno(GetLastError());\r
89   }\r
90 \r
91   std::cout << "Service uninstalled successfully" << std::endl; \r
92 \r
93   CloseServiceHandle(schService); \r
94   CloseServiceHandle(schSCManager);\r
95 }\r
96 \r
97 void WinService::start()\r
98 {\r
99   SERVICE_TABLE_ENTRY DispatchTable[] = {\r
100     {SVC_NAME, (LPSERVICE_MAIN_FUNCTION)WinService::main },\r
101     {NULL, NULL}\r
102   };\r
103 \r
104   if(!StartServiceCtrlDispatcherA(DispatchTable))\r
105     AnytunError::throwErr() << "Error on StartServiceCtrlDispatcher: " << AnytunErrno(GetLastError());\r
106 }\r
107 \r
108 int real_main(int argc, char* argv[], WinService& service);\r
109 \r
110 VOID WINAPI WinService::main(DWORD dwArgc, LPTSTR *lpszArgv)\r
111 {\r
112   WinService service;\r
113 \r
114   service.status_handle_ = RegisterServiceCtrlHandlerA(SVC_NAME, WinService::ctrlHandler);\r
115   if(!service.status_handle_) { \r
116     cLog.msg(Log::PRIO_ERROR) << "Error on RegisterServiceCtrlHandler: " << AnytunErrno(GetLastError());\r
117     return;\r
118   }\r
119   service.status_.dwServiceType = SERVICE_WIN32_OWN_PROCESS; \r
120   service.status_.dwServiceSpecificExitCode = 0;    \r
121   service.reportStatus(SERVICE_START_PENDING, NO_ERROR);\r
122   \r
123   real_main(dwArgc, lpszArgv, service);\r
124   \r
125   service.reportStatus(SERVICE_STOPPED, NO_ERROR);\r
126 }\r
127 \r
128 VOID WINAPI WinService::ctrlHandler(DWORD dwCtrl)\r
129 {\r
130   gSignalController.inject(dwCtrl);\r
131 }\r
132 \r
133 int WinService::handleCtrlSignal(int sig, const std::string& msg)\r
134 {\r
135   switch(sig) {\r
136     case SERVICE_CONTROL_STOP: {\r
137       reportStatus(SERVICE_STOP_PENDING, NO_ERROR);\r
138       cLog.msg(Log::PRIO_NOTICE) << "received service stop signal, exitting";\r
139       return 1;\r
140     }\r
141     case SERVICE_CONTROL_INTERROGATE: break;\r
142     default: break;\r
143   }\r
144   reportStatus(status_.dwCurrentState, NO_ERROR);\r
145 \r
146   return 0;\r
147 }\r
148 \r
149 void WinService::reportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode)\r
150 {\r
151   static DWORD dwCheckPoint = 1;\r
152 \r
153   status_.dwCurrentState = dwCurrentState;\r
154   status_.dwWin32ExitCode = dwWin32ExitCode;\r
155   status_.dwWaitHint = 0;\r
156 \r
157   if((dwCurrentState == SERVICE_START_PENDING) ||\r
158      (dwCurrentState == SERVICE_STOP_PENDING))\r
159     status_.dwControlsAccepted = 0;\r
160   else \r
161     status_.dwControlsAccepted = SERVICE_ACCEPT_STOP;\r
162 \r
163   if((dwCurrentState == SERVICE_RUNNING) ||\r
164      (dwCurrentState == SERVICE_STOPPED))\r
165     status_.dwCheckPoint = 0;\r
166   else\r
167     status_.dwCheckPoint = dwCheckPoint++;\r
168 \r
169   SetServiceStatus(status_handle_, &status_);\r
170 }\r
171 \r
172 void WinService::initPrivs(std::string const& username, std::string const& groupname)\r
173 {\r
174 // nothing here\r
175 }\r
176 \r
177 void WinService::dropPrivs()\r
178 {\r
179 // nothing here\r
180 }\r
181 \r
182 void WinService::chroot(std::string const& dir)\r
183 {\r
184 // nothing here\r
185 }\r
186 \r
187 void WinService::daemonize()\r
188 {\r
189 // nothing here\r
190 }\r
191 \r
192 bool WinService::isDaemonized()\r
193 {\r
194   return true;\r
195 }\r
196 \r
197 #endif\r