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