Vidalia  0.3.1
TorService.h
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file torservice.h
13 ** \brief Starts, stops, installs, and uninstalls a Tor service (Win32).
14 */
15 
16 #ifndef _TORSERVICE_H
17 #define _TORSERVICE_H
18 
19 #include <QObject>
20 #include <QProcess>
21 
22 #include <windows.h>
23 #define TOR_SERVICE_NAME "tor"
24 #define TOR_SERVICE_DISP "Tor Win32 Service"
25 #define TOR_SERVICE_DESC \
26  TEXT("Provides an anonymous Internet communication system.")
27 #define TOR_SERVICE_ACCESS SERVICE_ALL_ACCESS
28 #define SERVICE_ERROR 8
29 
30 /* NT service function prototypes. This code is adapted from Tor's
31  * nt_service_load_library() in main.c. See LICENSE for details on
32  * Tor's license. */
33 typedef BOOL (WINAPI *ChangeServiceConfig2A_fn)(
34  SC_HANDLE hService,
35  DWORD dwInfoLevel,
36  LPVOID lpInfo);
37 typedef BOOL (WINAPI *CloseServiceHandle_fn)(
38  SC_HANDLE hSCObject);
39 typedef BOOL (WINAPI *ControlService_fn)(
40  SC_HANDLE hService,
41  DWORD dwControl,
42  LPSERVICE_STATUS lpServiceStatus);
43 typedef SC_HANDLE (WINAPI *CreateServiceA_fn)(
44  SC_HANDLE hSCManager,
45  LPCTSTR lpServiceName,
46  LPCTSTR lpDisplayName,
47  DWORD dwDesiredAccess,
48  DWORD dwServiceType,
49  DWORD dwStartType,
50  DWORD dwErrorControl,
51  LPCTSTR lpBinaryPathName,
52  LPCTSTR lpLoadOrderGroup,
53  LPDWORD lpdwTagId,
54  LPCTSTR lpDependencies,
55  LPCTSTR lpServiceStartName,
56  LPCTSTR lpPassword);
57 typedef BOOL (WINAPI *DeleteService_fn)(
58  SC_HANDLE hService);
59 typedef SC_HANDLE (WINAPI *OpenSCManagerA_fn)(
60  LPCTSTR lpMachineName,
61  LPCTSTR lpDatabaseName,
62  DWORD dwDesiredAccess);
63 typedef SC_HANDLE (WINAPI *OpenServiceA_fn)(
64  SC_HANDLE hSCManager,
65  LPCTSTR lpServiceName,
66  DWORD dwDesiredAccess);
67 typedef BOOL (WINAPI *QueryServiceStatus_fn)(
68  SC_HANDLE hService,
69  LPSERVICE_STATUS lpServiceStatus);
70 typedef BOOL (WINAPI *SetServiceStatus_fn)(SERVICE_STATUS_HANDLE,
71  LPSERVICE_STATUS);
72 typedef BOOL (WINAPI *StartServiceA_fn)(
73  SC_HANDLE hService,
74  DWORD dwNumServiceArgs,
75  LPCTSTR* lpServiceArgVectors);
76 
77 /** Table of NT service related functions. */
79  bool loaded;
90 };
91 
92 
93 class TorService : public QObject
94 {
95  Q_OBJECT
96 
97 public:
98  /** Returns if services are supported. */
99  static bool isSupported();
100  /** Dynamically loads NT service related functions from advapi32.dll. */
101  static bool loadServiceFunctions();
102 
103  /** Default ctor. */
104  TorService(QObject* parent = 0);
105  /** Default dtor. */
106  ~TorService();
107 
108  /** Returns true if the Tor service is installed. */
109  bool isInstalled();
110  /** Returns true if the Tor service is running. */
111  bool isRunning();
112  /** Starts the Tor service. Emits started on success. */
113  void start();
114  /** Stops the Tor service. Emits finished on success. */
115  bool stop();
116  /** Returns the exit code of the last Tor service that finished. */
117  int exitCode();
118  /** Returns the exit status of the last Tor service that finished. */
119  QProcess::ExitStatus exitStatus();
120  /** Installs the Tor service. */
121  bool install(const QString &torPath, const QString &torrc,
122  quint16 controlPort);
123  /** Removes the Tor service. */
124  bool remove();
125 
126 signals:
127  /** Called when the service gets started. */
128  void started();
129  /** Called when the service gets stopped. */
130  void finished(int exitCode, QProcess::ExitStatus);
131  /** Called when there is an error in starting the service. */
132  void startFailed(QString error);
133 
134 private:
135  /** Opens a handle to the Tor service. Returns NULL on error. */
136  SC_HANDLE openService();
137  /** Opens a handle to the service control manager. Returns NULL on error. */
138  static SC_HANDLE openSCM();
139  /** Closes the service <b>handle</b>. */
140  static void closeHandle(SC_HANDLE handle);
141  /** Gets the status of the Tor service. */
142  DWORD status();
143 
144  /** Handle to the service control manager. */
145  SC_HANDLE _scm;
146  /** List of dynamically loaded NT service functions. */
148 };
149 
150 #endif
151 
ServiceFunctions::OpenSCManagerA
OpenSCManagerA_fn OpenSCManagerA
Definition: TorService.h:85
TorService::_scm
SC_HANDLE _scm
Definition: TorService.h:145
TorService::isSupported
static bool isSupported()
Definition: TorService.cpp:48
CloseServiceHandle_fn
BOOL(WINAPI * CloseServiceHandle_fn)(SC_HANDLE hSCObject)
Definition: TorService.h:37
ServiceFunctions::ChangeServiceConfig2A
ChangeServiceConfig2A_fn ChangeServiceConfig2A
Definition: TorService.h:80
TorService::finished
void finished(int exitCode, QProcess::ExitStatus)
ServiceFunctions::SetServiceStatus
SetServiceStatus_fn SetServiceStatus
Definition: TorService.h:88
TorService::openSCM
static SC_HANDLE openSCM()
Definition: TorService.cpp:101
TorService::remove
bool remove()
Definition: TorService.cpp:280
ServiceFunctions::loaded
bool loaded
Definition: TorService.h:79
TorService::status
DWORD status()
Definition: TorService.cpp:300
TorService::exitStatus
QProcess::ExitStatus exitStatus()
Definition: TorService.cpp:225
SetServiceStatus_fn
BOOL(WINAPI * SetServiceStatus_fn)(SERVICE_STATUS_HANDLE, LPSERVICE_STATUS)
Definition: TorService.h:70
TorService::openService
SC_HANDLE openService()
Definition: TorService.cpp:88
TorService::started
void started()
TorService::loadServiceFunctions
static bool loadServiceFunctions()
Definition: TorService.cpp:57
TorService
Definition: TorService.h:93
StartServiceA_fn
BOOL(WINAPI * StartServiceA_fn)(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCTSTR *lpServiceArgVectors)
Definition: TorService.h:72
CreateServiceA_fn
SC_HANDLE(WINAPI * CreateServiceA_fn)(SC_HANDLE hSCManager, LPCTSTR lpServiceName, LPCTSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCTSTR lpBinaryPathName, LPCTSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCTSTR lpDependencies, LPCTSTR lpServiceStartName, LPCTSTR lpPassword)
Definition: TorService.h:43
QueryServiceStatus_fn
BOOL(WINAPI * QueryServiceStatus_fn)(SC_HANDLE hService, LPSERVICE_STATUS lpServiceStatus)
Definition: TorService.h:67
TorService::TorService
TorService(QObject *parent=0)
Definition: TorService.cpp:34
TorService::~TorService
~TorService()
Definition: TorService.cpp:41
TorService::start
void start()
Definition: TorService.cpp:137
TorService::stop
bool stop()
Definition: TorService.cpp:168
TorService::isRunning
bool isRunning()
Definition: TorService.cpp:130
TorService::exitCode
int exitCode()
Definition: TorService.cpp:203
ServiceFunctions::CreateServiceA
CreateServiceA_fn CreateServiceA
Definition: TorService.h:83
TorService::install
bool install(const QString &torPath, const QString &torrc, quint16 controlPort)
Definition: TorService.cpp:239
ServiceFunctions::CloseServiceHandle
CloseServiceHandle_fn CloseServiceHandle
Definition: TorService.h:81
OpenServiceA_fn
SC_HANDLE(WINAPI * OpenServiceA_fn)(SC_HANDLE hSCManager, LPCTSTR lpServiceName, DWORD dwDesiredAccess)
Definition: TorService.h:63
TorService::startFailed
void startFailed(QString error)
TorService::isInstalled
bool isInstalled()
Definition: TorService.cpp:119
OpenSCManagerA_fn
SC_HANDLE(WINAPI * OpenSCManagerA_fn)(LPCTSTR lpMachineName, LPCTSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: TorService.h:59
DeleteService_fn
BOOL(WINAPI * DeleteService_fn)(SC_HANDLE hService)
Definition: TorService.h:57
ServiceFunctions
Definition: TorService.h:78
ServiceFunctions::OpenServiceA
OpenServiceA_fn OpenServiceA
Definition: TorService.h:86
tc::error
DebugMessage error(const QString &fmt)
Definition: tcglobal.cpp:40
ServiceFunctions::StartServiceA
StartServiceA_fn StartServiceA
Definition: TorService.h:89
ServiceFunctions::ControlService
ControlService_fn ControlService
Definition: TorService.h:82
ChangeServiceConfig2A_fn
BOOL(WINAPI * ChangeServiceConfig2A_fn)(SC_HANDLE hService, DWORD dwInfoLevel, LPVOID lpInfo)
Definition: TorService.h:33
ServiceFunctions::QueryServiceStatus
QueryServiceStatus_fn QueryServiceStatus
Definition: TorService.h:87
ServiceFunctions::DeleteService
DeleteService_fn DeleteService
Definition: TorService.h:84
TorService::closeHandle
static void closeHandle(SC_HANDLE handle)
Definition: TorService.cpp:110
ControlService_fn
BOOL(WINAPI * ControlService_fn)(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: TorService.h:39
TorService::_service_fns
static ServiceFunctions _service_fns
Definition: TorService.h:147