diff options
author | Arne Schwabe <arne@rfc2549.org> | 2012-04-16 19:21:14 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2012-04-16 19:21:14 +0200 |
commit | 3e4d8f433239c40311037616b1b8833a06651ae0 (patch) | |
tree | 98ab7fce0d011d34677b0beb762d389cb5c39199 /openvpn/src/openvpnserv |
Initial import
Diffstat (limited to 'openvpn/src/openvpnserv')
-rw-r--r-- | openvpn/src/openvpnserv/Makefile.am | 27 | ||||
-rwxr-xr-x | openvpn/src/openvpnserv/openvpnserv.c | 534 | ||||
-rw-r--r-- | openvpn/src/openvpnserv/openvpnserv.vcxproj | 112 | ||||
-rw-r--r-- | openvpn/src/openvpnserv/openvpnserv.vcxproj.filters | 35 | ||||
-rw-r--r-- | openvpn/src/openvpnserv/openvpnserv_resources.rc | 41 | ||||
-rw-r--r-- | openvpn/src/openvpnserv/service.c | 700 | ||||
-rw-r--r-- | openvpn/src/openvpnserv/service.h | 139 |
7 files changed, 1588 insertions, 0 deletions
diff --git a/openvpn/src/openvpnserv/Makefile.am b/openvpn/src/openvpnserv/Makefile.am new file mode 100644 index 00000000..a989c25c --- /dev/null +++ b/openvpn/src/openvpnserv/Makefile.am @@ -0,0 +1,27 @@ +# +# OpenVPN -- An application to securely tunnel IP networks +# over a single UDP port, with support for SSL/TLS-based +# session authentication and key exchange, +# packet encryption, packet authentication, and +# packet compression. +# +# Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net> +# Copyright (C) 2006-2012 Alon Bar-Lev <alon.barlev@gmail.com> +# + +include $(top_srcdir)/build/ltrc.inc + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +EXTRA_DIST = \ + openvpnserv.vcxproj \ + openvpnserv.vcxproj.filters + +if WIN32 +sbin_PROGRAMS = openvpnserv +endif + +openvpnserv_SOURCES = \ + openvpnserv.c \ + service.h service.c \ + openvpnserv_resources.rc diff --git a/openvpn/src/openvpnserv/openvpnserv.c b/openvpn/src/openvpnserv/openvpnserv.c new file mode 100755 index 00000000..a9a9441e --- /dev/null +++ b/openvpn/src/openvpnserv/openvpnserv.c @@ -0,0 +1,534 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single TCP/UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (see the file COPYING included with this + * distribution); if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * This program allows one or more OpenVPN processes to be started + * as a service. To build, you must get the service sample from the + * Platform SDK and replace Simple.c with this file. + * + * You should also apply service.patch to + * service.c and service.h from the Platform SDK service sample. + * + * This code is designed to be built with the mingw compiler. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#elif defined(_MSC_VER) +#include "config-msvc.h" +#endif +#include <windows.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <process.h> +#include "service.h" + +/* bool definitions */ +#define bool int +#define true 1 +#define false 0 + +/* These are new for 2000/XP, so they aren't in the mingw headers yet */ +#ifndef BELOW_NORMAL_PRIORITY_CLASS +#define BELOW_NORMAL_PRIORITY_CLASS 0x00004000 +#endif +#ifndef ABOVE_NORMAL_PRIORITY_CLASS +#define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000 +#endif + +struct security_attributes +{ + SECURITY_ATTRIBUTES sa; + SECURITY_DESCRIPTOR sd; +}; + +/* + * This event is initially created in the non-signaled + * state. It will transition to the signaled state when + * we have received a terminate signal from the Service + * Control Manager which will cause an asynchronous call + * of ServiceStop below. + */ +#define EXIT_EVENT_NAME PACKAGE "_exit_1" + +/* + * Which registry key in HKLM should + * we get config info from? + */ +#define REG_KEY "SOFTWARE\\" PACKAGE_NAME + +static HANDLE exit_event = NULL; + +/* clear an object */ +#define CLEAR(x) memset(&(x), 0, sizeof(x)) + +/* + * Message handling + */ +#define M_INFO (0) // informational +#define M_SYSERR (MSG_FLAGS_ERROR|MSG_FLAGS_SYS_CODE) // error + system code +#define M_ERR (MSG_FLAGS_ERROR) // error + +/* write error to event log */ +#define MSG(flags, ...) \ + { \ + char x_msg[256]; \ + openvpn_snprintf (x_msg, sizeof(x_msg), __VA_ARGS__); \ + AddToMessageLog ((flags), x_msg); \ + } + +/* get a registry string */ +#define QUERY_REG_STRING(name, data) \ + { \ + len = sizeof (data); \ + status = RegQueryValueEx(openvpn_key, name, NULL, &type, data, &len); \ + if (status != ERROR_SUCCESS || type != REG_SZ) \ + { \ + SetLastError (status); \ + MSG (M_SYSERR, error_format_str, name); \ + RegCloseKey (openvpn_key); \ + goto finish; \ + } \ + } + +/* get a registry string */ +#define QUERY_REG_DWORD(name, data) \ + { \ + len = sizeof (DWORD); \ + status = RegQueryValueEx(openvpn_key, name, NULL, &type, (LPBYTE)&data, &len); \ + if (status != ERROR_SUCCESS || type != REG_DWORD || len != sizeof (DWORD)) \ + { \ + SetLastError (status); \ + MSG (M_SYSERR, error_format_dword, name); \ + RegCloseKey (openvpn_key); \ + goto finish; \ + } \ + } + +/* + * This is necessary due to certain buggy implementations of snprintf, + * that don't guarantee null termination for size > 0. + * (copied from ../buffer.c, line 217) + * (git: 100644 blob e2f8caab0a5b2a870092c6cd508a1a50c21c3ba3 buffer.c) + */ + +int openvpn_snprintf(char *str, size_t size, const char *format, ...) +{ + va_list arglist; + int len = -1; + if (size > 0) + { + va_start (arglist, format); + len = vsnprintf (str, size, format, arglist); + va_end (arglist); + str[size - 1] = 0; + } + return (len >= 0 && len < size); +} + + +bool +init_security_attributes_allow_all (struct security_attributes *obj) +{ + CLEAR (*obj); + + obj->sa.nLength = sizeof (SECURITY_ATTRIBUTES); + obj->sa.lpSecurityDescriptor = &obj->sd; + obj->sa.bInheritHandle = TRUE; + if (!InitializeSecurityDescriptor (&obj->sd, SECURITY_DESCRIPTOR_REVISION)) + return false; + if (!SetSecurityDescriptorDacl (&obj->sd, TRUE, NULL, FALSE)) + return false; + return true; +} + +HANDLE +create_event (const char *name, bool allow_all, bool initial_state, bool manual_reset) +{ + if (allow_all) + { + struct security_attributes sa; + if (!init_security_attributes_allow_all (&sa)) + return NULL; + return CreateEvent (&sa.sa, (BOOL)manual_reset, (BOOL)initial_state, name); + } + else + return CreateEvent (NULL, (BOOL)manual_reset, (BOOL)initial_state, name); +} + +void +close_if_open (HANDLE h) +{ + if (h != NULL) + CloseHandle (h); +} + +static bool +match (const WIN32_FIND_DATA *find, const char *ext) +{ + int i; + + if (find->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + return false; + + if (!strlen (ext)) + return true; + + i = strlen (find->cFileName) - strlen (ext) - 1; + if (i < 1) + return false; + + return find->cFileName[i] == '.' && !_stricmp (find->cFileName + i + 1, ext); +} + +/* + * Modify the extension on a filename. + */ +static bool +modext (char *dest, int size, const char *src, const char *newext) +{ + int i; + + if (size > 0 && (strlen (src) + 1) <= size) + { + strcpy (dest, src); + dest [size - 1] = '\0'; + i = strlen (dest); + while (--i >= 0) + { + if (dest[i] == '\\') + break; + if (dest[i] == '.') + { + dest[i] = '\0'; + break; + } + } + if (strlen (dest) + strlen(newext) + 2 <= size) + { + strcat (dest, "."); + strcat (dest, newext); + return true; + } + dest [0] = '\0'; + } + return false; +} + +VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv) +{ + char exe_path[MAX_PATH]; + char config_dir[MAX_PATH]; + char ext_string[16]; + char log_dir[MAX_PATH]; + char priority_string[64]; + char append_string[2]; + + DWORD priority; + bool append; + + ResetError (); + + if (!ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000)) + { + MSG (M_ERR, "ReportStatusToSCMgr #1 failed"); + goto finish; + } + + /* + * Create our exit event + */ + exit_event = create_event (EXIT_EVENT_NAME, false, false, true); + if (!exit_event) + { + MSG (M_ERR, "CreateEvent failed"); + goto finish; + } + + /* + * If exit event is already signaled, it means we were not + * shut down properly. + */ + if (WaitForSingleObject (exit_event, 0) != WAIT_TIMEOUT) + { + MSG (M_ERR, "Exit event is already signaled -- we were not shut down properly"); + goto finish; + } + + if (!ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000)) + { + MSG (M_ERR, "ReportStatusToSCMgr #2 failed"); + goto finish; + } + + /* + * Read info from registry in key HKLM\SOFTWARE\OpenVPN + */ + { + HKEY openvpn_key; + LONG status; + DWORD len; + DWORD type; + + static const char error_format_str[] = + "Error querying registry key of type REG_SZ: HKLM\\" REG_KEY "\\%s"; + + static const char error_format_dword[] = + "Error querying registry key of type REG_DWORD: HKLM\\" REG_KEY "\\%s"; + + status = RegOpenKeyEx( + HKEY_LOCAL_MACHINE, + REG_KEY, + 0, + KEY_READ, + &openvpn_key); + + if (status != ERROR_SUCCESS) + { + SetLastError (status); + MSG (M_SYSERR, "Registry key HKLM\\" REG_KEY " not found"); + goto finish; + } + + /* get path to openvpn.exe */ + QUERY_REG_STRING ("exe_path", exe_path); + + /* get path to configuration directory */ + QUERY_REG_STRING ("config_dir", config_dir); + + /* get extension on configuration files */ + QUERY_REG_STRING ("config_ext", ext_string); + + /* get path to log directory */ + QUERY_REG_STRING ("log_dir", log_dir); + + /* get priority for spawned OpenVPN subprocesses */ + QUERY_REG_STRING ("priority", priority_string); + + /* should we truncate or append to logfile? */ + QUERY_REG_STRING ("log_append", append_string); + + RegCloseKey (openvpn_key); + } + + /* set process priority */ + priority = NORMAL_PRIORITY_CLASS; + if (!_stricmp (priority_string, "IDLE_PRIORITY_CLASS")) + priority = IDLE_PRIORITY_CLASS; + else if (!_stricmp (priority_string, "BELOW_NORMAL_PRIORITY_CLASS")) + priority = BELOW_NORMAL_PRIORITY_CLASS; + else if (!_stricmp (priority_string, "NORMAL_PRIORITY_CLASS")) + priority = NORMAL_PRIORITY_CLASS; + else if (!_stricmp (priority_string, "ABOVE_NORMAL_PRIORITY_CLASS")) + priority = ABOVE_NORMAL_PRIORITY_CLASS; + else if (!_stricmp (priority_string, "HIGH_PRIORITY_CLASS")) + priority = HIGH_PRIORITY_CLASS; + else + { + MSG (M_ERR, "Unknown priority name: %s", priority_string); + goto finish; + } + + /* set log file append/truncate flag */ + append = false; + if (append_string[0] == '0') + append = false; + else if (append_string[0] == '1') + append = true; + else + { + MSG (M_ERR, "Log file append flag (given as '%s') must be '0' or '1'", append_string); + goto finish; + } + + /* + * Instantiate an OpenVPN process for each configuration + * file found. + */ + { + WIN32_FIND_DATA find_obj; + HANDLE find_handle; + BOOL more_files; + char find_string[MAX_PATH]; + + openvpn_snprintf (find_string, MAX_PATH, "%s\\*", config_dir); + + find_handle = FindFirstFile (find_string, &find_obj); + if (find_handle == INVALID_HANDLE_VALUE) + { + MSG (M_ERR, "Cannot get configuration file list using: %s", find_string); + goto finish; + } + + /* + * Loop over each config file + */ + do { + HANDLE log_handle = NULL; + STARTUPINFO start_info; + PROCESS_INFORMATION proc_info; + struct security_attributes sa; + char log_file[MAX_PATH]; + char log_path[MAX_PATH]; + char command_line[256]; + + CLEAR (start_info); + CLEAR (proc_info); + CLEAR (sa); + + if (!ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000)) + { + MSG (M_ERR, "ReportStatusToSCMgr #3 failed"); + FindClose (find_handle); + goto finish; + } + + /* does file have the correct type and extension? */ + if (match (&find_obj, ext_string)) + { + /* get log file pathname */ + if (!modext (log_file, sizeof (log_file), find_obj.cFileName, "log")) + { + MSG (M_ERR, "Cannot construct logfile name based on: %s", find_obj.cFileName); + FindClose (find_handle); + goto finish; + } + openvpn_snprintf (log_path, sizeof(log_path), + "%s\\%s", log_dir, log_file); + + /* construct command line */ + openvpn_snprintf (command_line, sizeof(command_line), PACKAGE " --service %s 1 --config \"%s\"", + EXIT_EVENT_NAME, + find_obj.cFileName); + + /* Make security attributes struct for logfile handle so it can + be inherited. */ + if (!init_security_attributes_allow_all (&sa)) + { + MSG (M_SYSERR, "InitializeSecurityDescriptor start_" PACKAGE " failed"); + goto finish; + } + + /* open logfile as stdout/stderr for soon-to-be-spawned subprocess */ + log_handle = CreateFile (log_path, + GENERIC_WRITE, + FILE_SHARE_READ, + &sa.sa, + append ? OPEN_ALWAYS : CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if (log_handle == INVALID_HANDLE_VALUE) + { + MSG (M_SYSERR, "Cannot open logfile: %s", log_path); + FindClose (find_handle); + goto finish; + } + + /* append to logfile? */ + if (append) + { + if (SetFilePointer (log_handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER) + { + MSG (M_SYSERR, "Cannot seek to end of logfile: %s", log_path); + FindClose (find_handle); + goto finish; + } + } + + /* fill in STARTUPINFO struct */ + GetStartupInfo(&start_info); + start_info.cb = sizeof(start_info); + start_info.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; + start_info.wShowWindow = SW_HIDE; + start_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); + start_info.hStdOutput = start_info.hStdError = log_handle; + + /* create an OpenVPN process for one config file */ + if (!CreateProcess(exe_path, + command_line, + NULL, + NULL, + TRUE, + priority | CREATE_NEW_CONSOLE, + NULL, + config_dir, + &start_info, + &proc_info)) + { + MSG (M_SYSERR, "CreateProcess failed, exe='%s' cmdline='%s' dir='%s'", + exe_path, + command_line, + config_dir); + + FindClose (find_handle); + CloseHandle (log_handle); + goto finish; + } + + /* close unneeded handles */ + Sleep (1000); /* try to prevent race if we close logfile + handle before child process DUPs it */ + if (!CloseHandle (proc_info.hProcess) + || !CloseHandle (proc_info.hThread) + || !CloseHandle (log_handle)) + { + MSG (M_SYSERR, "CloseHandle failed"); + goto finish; + } + } + + /* more files to process? */ + more_files = FindNextFile (find_handle, &find_obj); + + } while (more_files); + + FindClose (find_handle); + } + + /* we are now fully started */ + if (!ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0)) + { + MSG (M_ERR, "ReportStatusToSCMgr SERVICE_RUNNING failed"); + goto finish; + } + + /* wait for our shutdown signal */ + if (WaitForSingleObject (exit_event, INFINITE) != WAIT_OBJECT_0) + { + MSG (M_ERR, "wait for shutdown signal failed"); + } + + finish: + ServiceStop (); + if (exit_event) + CloseHandle (exit_event); +} + +VOID ServiceStop() +{ + if (exit_event) + SetEvent(exit_event); +} diff --git a/openvpn/src/openvpnserv/openvpnserv.vcxproj b/openvpn/src/openvpnserv/openvpnserv.vcxproj new file mode 100644 index 00000000..0b75ed01 --- /dev/null +++ b/openvpn/src/openvpnserv/openvpnserv.vcxproj @@ -0,0 +1,112 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{9C91EE0B-817D-420A-A1E6-15A5A9D98BAD}</ProjectGuid> + <RootNamespace>openvpnserv</RootNamespace> + <Keyword>Win32Proj</Keyword> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <CharacterSet>MultiByte</CharacterSet> + <WholeProgramOptimization>true</WholeProgramOptimization> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <CharacterSet>MultiByte</CharacterSet> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup> + <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)$(Platform)-Output\$(Configuration)\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Configuration)\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)$(Platform)-Output\$(Configuration)\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Configuration)\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <Optimization>Disabled</Optimization> + <AdditionalIncludeDirectories>$(SOURCEBASE);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;$(CPPFLAGS);%(PreprocessorDefinitions)</PreprocessorDefinitions> + <MinimalRebuild>true</MinimalRebuild> + <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> + <PrecompiledHeader> + </PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <DebugInformationFormat>EditAndContinue</DebugInformationFormat> + </ClCompile> + <ResourceCompile> + <AdditionalIncludeDirectories>$(SOURCEBASE);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + </ResourceCompile> + <Link> + <GenerateDebugInformation>true</GenerateDebugInformation> + <SubSystem>Console</SubSystem> + <TargetMachine>MachineX86</TargetMachine> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <Optimization>MaxSpeed</Optimization> + <IntrinsicFunctions>true</IntrinsicFunctions> + <AdditionalIncludeDirectories>$(SOURCEBASE);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;$(CPPFLAGS);%(PreprocessorDefinitions)</PreprocessorDefinitions> + <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> + <FunctionLevelLinking>true</FunctionLevelLinking> + <PrecompiledHeader> + </PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> + </ClCompile> + <ResourceCompile> + <AdditionalIncludeDirectories>$(SOURCEBASE);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + </ResourceCompile> + <Link> + <GenerateDebugInformation>true</GenerateDebugInformation> + <SubSystem>Console</SubSystem> + <OptimizeReferences>true</OptimizeReferences> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <TargetMachine>MachineX86</TargetMachine> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <ClCompile Include="openvpnserv.c" /> + <ClCompile Include="service.c" /> + </ItemGroup> + <ItemGroup> + <ClInclude Include="service.h" /> + </ItemGroup> + <ItemGroup> + <ResourceCompile Include="openvpnserv_resources.rc" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\build\msvc\msvc-generate\msvc-generate.vcxproj"> + <Project>{8598c2c8-34c4-47a1-99b0-7c295a890615}</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/openvpn/src/openvpnserv/openvpnserv.vcxproj.filters b/openvpn/src/openvpnserv/openvpnserv.vcxproj.filters new file mode 100644 index 00000000..0c89b4f4 --- /dev/null +++ b/openvpn/src/openvpnserv/openvpnserv.vcxproj.filters @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> + <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> + </Filter> + <Filter Include="Header Files"> + <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> + <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions> + </Filter> + <Filter Include="Resource Files"> + <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="openvpnserv.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="service.c"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="service.h"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <ResourceCompile Include="openvpnserv_resources.rc"> + <Filter>Resource Files</Filter> + </ResourceCompile> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/openvpn/src/openvpnserv/openvpnserv_resources.rc b/openvpn/src/openvpnserv/openvpnserv_resources.rc new file mode 100644 index 00000000..9b79cb7c --- /dev/null +++ b/openvpn/src/openvpnserv/openvpnserv_resources.rc @@ -0,0 +1,41 @@ +#ifdef HAVE_CONFIG_H +#include <config.h> +#else +#include <config-msvc-version.h> +#endif +#include <winresrc.h> + +LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL + +VS_VERSION_INFO VERSIONINFO + FILEVERSION OPENVPN_VERSION_RESOURCE + PRODUCTVERSION OPENVPN_VERSION_RESOURCE + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x40004L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "CompanyName", "The OpenVPN Project" + VALUE "FileDescription", "OpenVPN Service" + VALUE "FileVersion", PACKAGE_VERSION ".0" + VALUE "InternalName", "OpenVPN" + VALUE "LegalCopyright", "Copyright © The OpenVPN Project" + VALUE "OriginalFilename", "openvpnserv.exe" + VALUE "ProductName", "OpenVPN" + VALUE "ProductVersion", PACKAGE_VERSION ".0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END diff --git a/openvpn/src/openvpnserv/service.c b/openvpn/src/openvpnserv/service.c new file mode 100644 index 00000000..d7562b38 --- /dev/null +++ b/openvpn/src/openvpnserv/service.c @@ -0,0 +1,700 @@ +/*--------------------------------------------------------------------------- +THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +PARTICULAR PURPOSE. + +Copyright (C) 1993 - 2000. Microsoft Corporation. All rights reserved. + +MODULE: service.c + +PURPOSE: Implements functions required by all Windows NT services + +FUNCTIONS: + main(int argc, char **argv); + service_ctrl(DWORD dwCtrlCode); + service_main(DWORD dwArgc, LPTSTR *lpszArgv); + CmdInstallService(); + CmdRemoveService(); + CmdStartService(); + CmdDebugService(int argc, char **argv); + ControlHandler ( DWORD dwCtrlType ); + GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize ); + +---------------------------------------------------------------------------*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#elif defined(_MSC_VER) +#include "config-msvc.h" +#endif +#include <windows.h> +#include <stdio.h> +#include <stdlib.h> +#include <process.h> +#include <tchar.h> + +#include "service.h" + +// internal variables +SERVICE_STATUS ssStatus; // current status of the service +SERVICE_STATUS_HANDLE sshStatusHandle; +DWORD dwErr = 0; +BOOL bDebug = FALSE; +TCHAR szErr[256]; + +// internal function prototypes +VOID WINAPI service_ctrl(DWORD dwCtrlCode); +VOID WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv); +int CmdInstallService(); +int CmdRemoveService(); +int CmdStartService(); +VOID CmdDebugService(int argc, char **argv); +BOOL WINAPI ControlHandler ( DWORD dwCtrlType ); +LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize ); + +// +// FUNCTION: main +// +// PURPOSE: entrypoint for service +// +// PARAMETERS: +// argc - number of command line arguments +// argv - array of command line arguments +// +// RETURN VALUE: +// none +// +// COMMENTS: +// main() either performs the command line task, or +// call StartServiceCtrlDispatcher to register the +// main service thread. When the this call returns, +// the service has stopped, so exit. +// +int __cdecl main(int argc, char **argv) +{ + SERVICE_TABLE_ENTRY dispatchTable[] = + { + { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main}, + { NULL, NULL} + }; + + if ( (argc > 1) && + ((*argv[1] == '-') || (*argv[1] == '/')) ) + { + if ( _stricmp( "install", argv[1]+1 ) == 0 ) + { + return CmdInstallService(); + } + else if ( _stricmp( "remove", argv[1]+1 ) == 0 ) + { + return CmdRemoveService(); + } + else if ( _stricmp( "start", argv[1]+1 ) == 0) + { + return CmdStartService(); + } + else if ( _stricmp( "debug", argv[1]+1 ) == 0 ) + { + bDebug = TRUE; + CmdDebugService(argc, argv); + } + else + { + goto dispatch; + } + return 0; + } + + // if it doesn't match any of the above parameters + // the service control manager may be starting the service + // so we must call StartServiceCtrlDispatcher + dispatch: + // this is just to be friendly + printf( "%s -install to install the service\n", SZAPPNAME ); + printf( "%s -start to start the service\n", SZAPPNAME ); + printf( "%s -remove to remove the service\n", SZAPPNAME ); + printf( "%s -debug <params> to run as a console app for debugging\n", SZAPPNAME ); + printf( "\nStartServiceCtrlDispatcher being called.\n" ); + printf( "This may take several seconds. Please wait.\n" ); + + if (!StartServiceCtrlDispatcher(dispatchTable)) + AddToMessageLog(MSG_FLAGS_ERROR, TEXT("StartServiceCtrlDispatcher failed.")); + + return 0; +} + + + +// +// FUNCTION: service_main +// +// PURPOSE: To perform actual initialization of the service +// +// PARAMETERS: +// dwArgc - number of command line arguments +// lpszArgv - array of command line arguments +// +// RETURN VALUE: +// none +// +// COMMENTS: +// This routine performs the service initialization and then calls +// the user defined ServiceStart() routine to perform majority +// of the work. +// +void WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv) +{ + + // register our service control handler: + // + sshStatusHandle = RegisterServiceCtrlHandler( TEXT(SZSERVICENAME), service_ctrl); + + if (!sshStatusHandle) + goto cleanup; + + // SERVICE_STATUS members that don't change in example + // + ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; + ssStatus.dwServiceSpecificExitCode = 0; + + + // report the status to the service control manager. + // + if (!ReportStatusToSCMgr( + SERVICE_START_PENDING, // service state + NO_ERROR, // exit code + 3000)) // wait hint + goto cleanup; + + + ServiceStart( dwArgc, lpszArgv ); + + cleanup: + + // try to report the stopped status to the service control manager. + // + if (sshStatusHandle) + (VOID)ReportStatusToSCMgr( + SERVICE_STOPPED, + dwErr, + 0); + + return; +} + + + +// +// FUNCTION: service_ctrl +// +// PURPOSE: This function is called by the SCM whenever +// ControlService() is called on this service. +// +// PARAMETERS: +// dwCtrlCode - type of control requested +// +// RETURN VALUE: +// none +// +// COMMENTS: +// +VOID WINAPI service_ctrl(DWORD dwCtrlCode) +{ + // Handle the requested control code. + // + switch (dwCtrlCode) + { + // Stop the service. + // + // SERVICE_STOP_PENDING should be reported before + // setting the Stop Event - hServerStopEvent - in + // ServiceStop(). This avoids a race condition + // which may result in a 1053 - The Service did not respond... + // error. + case SERVICE_CONTROL_STOP: + ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0); + ServiceStop(); + return; + + // Update the service status. + // + case SERVICE_CONTROL_INTERROGATE: + break; + + // invalid control code + // + default: + break; + + } + + ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0); +} + + + +// +// FUNCTION: ReportStatusToSCMgr() +// +// PURPOSE: Sets the current status of the service and +// reports it to the Service Control Manager +// +// PARAMETERS: +// dwCurrentState - the state of the service +// dwWin32ExitCode - error code to report +// dwWaitHint - worst case estimate to next checkpoint +// +// RETURN VALUE: +// TRUE - success +// FALSE - failure +// +// COMMENTS: +// +BOOL ReportStatusToSCMgr(DWORD dwCurrentState, + DWORD dwWin32ExitCode, + DWORD dwWaitHint) +{ + static DWORD dwCheckPoint = 1; + BOOL fResult = TRUE; + + + if ( !bDebug ) // when debugging we don't report to the SCM + { + if (dwCurrentState == SERVICE_START_PENDING) + ssStatus.dwControlsAccepted = 0; + else + ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; + + ssStatus.dwCurrentState = dwCurrentState; + ssStatus.dwWin32ExitCode = dwWin32ExitCode; + ssStatus.dwWaitHint = dwWaitHint; + + if ( ( dwCurrentState == SERVICE_RUNNING ) || + ( dwCurrentState == SERVICE_STOPPED ) ) + ssStatus.dwCheckPoint = 0; + else + ssStatus.dwCheckPoint = dwCheckPoint++; + + + // Report the status of the service to the service control manager. + // + if (!(fResult = SetServiceStatus( sshStatusHandle, &ssStatus))) + { + AddToMessageLog(MSG_FLAGS_ERROR, TEXT("SetServiceStatus")); + } + } + return fResult; +} + + + +// +// FUNCTION: AddToMessageLog(LPTSTR lpszMsg) +// +// PURPOSE: Allows any thread to log an error message +// +// PARAMETERS: +// lpszMsg - text for message +// +// RETURN VALUE: +// none +// +// COMMENTS: +// +void AddToMessageLog(DWORD flags, LPTSTR lpszMsg) +{ + TCHAR szMsg [(sizeof(SZSERVICENAME) / sizeof(TCHAR)) + 100 ]; + HANDLE hEventSource; + LPCSTR lpszStrings[2]; + + if ( !bDebug ) + { + if (flags & MSG_FLAGS_SYS_CODE) + dwErr = GetLastError(); + else + dwErr = 0; + + // Use event logging to log the error. + // + hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME)); + + _stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), (int)dwErr); + lpszStrings[0] = szMsg; + lpszStrings[1] = lpszMsg; + + if (hEventSource != NULL) + { + ReportEvent(hEventSource, // handle of event source + // event type + (flags & MSG_FLAGS_ERROR) + ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE, + 0, // event category + 0, // event ID + NULL, // current user's SID + 2, // strings in lpszStrings + 0, // no bytes of raw data + lpszStrings, // array of error strings + NULL); // no raw data + + (VOID) DeregisterEventSource(hEventSource); + } + } +} + +void ResetError (void) +{ + dwErr = 0; +} + +/////////////////////////////////////////////////////////////////// +// +// The following code handles service installation and removal +// + + +// +// FUNCTION: CmdInstallService() +// +// PURPOSE: Installs the service +// +// PARAMETERS: +// none +// +// RETURN VALUE: +// 0 if success +// +// COMMENTS: +// +int CmdInstallService() +{ + SC_HANDLE schService; + SC_HANDLE schSCManager; + + TCHAR szPath[512]; + + int ret = 0; + + if ( GetModuleFileName( NULL, szPath+1, 510 ) == 0 ) + { + _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256)); + return 1; + } + szPath[0] = '\"'; + strcat(szPath, "\""); + + schSCManager = OpenSCManager( + NULL, // machine (NULL == local) + NULL, // database (NULL == default) + SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE // access required + ); + if ( schSCManager ) + { + schService = CreateService( + schSCManager, // SCManager database + TEXT(SZSERVICENAME), // name of service + TEXT(SZSERVICEDISPLAYNAME), // name to display + SERVICE_QUERY_STATUS, // desired access + SERVICE_WIN32_OWN_PROCESS, // service type + SERVICE_DEMAND_START, // start type -- alternative: SERVICE_AUTO_START + SERVICE_ERROR_NORMAL, // error control type + szPath, // service's binary + NULL, // no load ordering group + NULL, // no tag identifier + TEXT(SZDEPENDENCIES), // dependencies + NULL, // LocalSystem account + NULL); // no password + + if ( schService ) + { + _tprintf(TEXT("%s installed.\n"), TEXT(SZSERVICEDISPLAYNAME) ); + CloseServiceHandle(schService); + } + else + { + _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256)); + ret = 1; + } + + CloseServiceHandle(schSCManager); + } + else + { + _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256)); + ret = 1; + } + return ret; +} + +// +// FUNCTION: CmdStartService() +// +// PURPOSE: Start the service +// +// PARAMETERS: +// none +// +// RETURN VALUE: +// 0 if success +// +// COMMENTS: + +int CmdStartService() +{ + int ret = 0; + + SC_HANDLE schSCManager; + SC_HANDLE schService; + + + // Open a handle to the SC Manager database. + schSCManager = OpenSCManager( + NULL, // local machine + NULL, // ServicesActive database + SC_MANAGER_ALL_ACCESS); // full access rights + + if (NULL == schSCManager) { + _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256)); + ret = 1; + } + + schService = OpenService( + schSCManager, // SCM database + SZSERVICENAME, // service name + SERVICE_ALL_ACCESS); + + if (schService == NULL) { + _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256)); + ret = 1; + } + + if (!StartService( + schService, // handle to service + 0, // number of arguments + NULL) ) // no arguments + { + _tprintf(TEXT("StartService failed - %s\n"), GetLastErrorText(szErr,256)); + ret = 1; + } + else + { + _tprintf(TEXT("Service Started\n")); + ret = 0; + } + CloseServiceHandle(schService); + CloseServiceHandle(schSCManager); + return ret; +} + +// +// FUNCTION: CmdRemoveService() +// +// PURPOSE: Stops and removes the service +// +// PARAMETERS: +// none +// +// RETURN VALUE: +// 0 if success +// +// COMMENTS: +// +int CmdRemoveService() +{ + SC_HANDLE schService; + SC_HANDLE schSCManager; + + int ret = 0; + + schSCManager = OpenSCManager( + NULL, // machine (NULL == local) + NULL, // database (NULL == default) + SC_MANAGER_CONNECT // access required + ); + if ( schSCManager ) + { + schService = OpenService(schSCManager, TEXT(SZSERVICENAME), DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS); + + if (schService) + { + // try to stop the service + if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) ) + { + _tprintf(TEXT("Stopping %s."), TEXT(SZSERVICEDISPLAYNAME)); + Sleep( 1000 ); + + while ( QueryServiceStatus( schService, &ssStatus ) ) + { + if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING ) + { + _tprintf(TEXT(".")); + Sleep( 1000 ); + } + else + break; + } + + if ( ssStatus.dwCurrentState == SERVICE_STOPPED ) + _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) ); + else + { + _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) ); + ret = 1; + } + + } + + // now remove the service + if ( DeleteService(schService) ) + _tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICEDISPLAYNAME) ); + else + { + _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256)); + ret = 1; + } + + + CloseServiceHandle(schService); + } + else + { + _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256)); + ret = 1; + } + + CloseServiceHandle(schSCManager); + } + else + { + _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256)); + ret = 1; + } + return ret; +} + + + + +/////////////////////////////////////////////////////////////////// +// +// The following code is for running the service as a console app +// + + +// +// FUNCTION: CmdDebugService(int argc, char ** argv) +// +// PURPOSE: Runs the service as a console application +// +// PARAMETERS: +// argc - number of command line arguments +// argv - array of command line arguments +// +// RETURN VALUE: +// none +// +// COMMENTS: +// +void CmdDebugService(int argc, char ** argv) +{ + DWORD dwArgc; + LPTSTR *lpszArgv; + +#ifdef UNICODE + lpszArgv = CommandLineToArgvW(GetCommandLineW(), &(dwArgc) ); + if (NULL == lpszArgv) + { + // CommandLineToArvW failed!! + _tprintf(TEXT("CmdDebugService CommandLineToArgvW returned NULL\n")); + return; + } +#else + dwArgc = (DWORD) argc; + lpszArgv = argv; +#endif + + _tprintf(TEXT("Debugging %s.\n"), TEXT(SZSERVICEDISPLAYNAME)); + + SetConsoleCtrlHandler( ControlHandler, TRUE ); + + ServiceStart( dwArgc, lpszArgv ); + +#ifdef UNICODE +// Must free memory allocated for arguments + + GlobalFree(lpszArgv); +#endif // UNICODE + +} + + +// +// FUNCTION: ControlHandler ( DWORD dwCtrlType ) +// +// PURPOSE: Handled console control events +// +// PARAMETERS: +// dwCtrlType - type of control event +// +// RETURN VALUE: +// True - handled +// False - unhandled +// +// COMMENTS: +// +BOOL WINAPI ControlHandler ( DWORD dwCtrlType ) +{ + switch ( dwCtrlType ) + { + case CTRL_BREAK_EVENT: // use Ctrl+C or Ctrl+Break to simulate + case CTRL_C_EVENT: // SERVICE_CONTROL_STOP in debug mode + _tprintf(TEXT("Stopping %s.\n"), TEXT(SZSERVICEDISPLAYNAME)); + ServiceStop(); + return TRUE; + break; + + } + return FALSE; +} + +// +// FUNCTION: GetLastErrorText +// +// PURPOSE: copies error message text to string +// +// PARAMETERS: +// lpszBuf - destination buffer +// dwSize - size of buffer +// +// RETURN VALUE: +// destination buffer +// +// COMMENTS: +// +LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize ) +{ + DWORD dwRet; + LPTSTR lpszTemp = NULL; + + dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY, + NULL, + GetLastError(), + LANG_NEUTRAL, + (LPTSTR)&lpszTemp, + 0, + NULL ); + + // supplied buffer is not long enough + if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) ) + lpszBuf[0] = TEXT('\0'); + else + { + lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character + _stprintf( lpszBuf, TEXT("%s (0x%x)"), lpszTemp, (int)GetLastError() ); + } + + if ( lpszTemp ) + LocalFree((HLOCAL) lpszTemp ); + + return lpszBuf; +} diff --git a/openvpn/src/openvpnserv/service.h b/openvpn/src/openvpnserv/service.h new file mode 100644 index 00000000..e89a89fb --- /dev/null +++ b/openvpn/src/openvpnserv/service.h @@ -0,0 +1,139 @@ +/*--------------------------------------------------------------------------- +THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +PARTICULAR PURPOSE. + +Copyright (C) 1993 - 2000. Microsoft Corporation. All rights reserved. + + MODULE: service.h + + Comments: The use of this header file and the accompanying service.c + file simplifies the process of writting a service. You as a developer + simply need to follow the TODO's outlined in this header file, and + implement the ServiceStart() and ServiceStop() functions. + + There is no need to modify the code in service.c. Just add service.c + to your project and link with the following libraries... + + libcmt.lib kernel32.lib advapi.lib shell32.lib + + This code also supports unicode. Be sure to compile both service.c and + and code #include "service.h" with the same Unicode setting. + + Upon completion, your code will have the following command line interface + + <service exe> -? to display this list + <service exe> -install to install the service + <service exe> -remove to remove the service + <service exe> -debug <params> to run as a console app for debugging + + Note: This code also implements Ctrl+C and Ctrl+Break handlers + when using the debug option. These console events cause + your ServiceStop routine to be called + + Also, this code only handles the OWN_SERVICE service type + running in the LOCAL_SYSTEM security context. + + To control your service ( start, stop, etc ) you may use the + Services control panel applet or the NET.EXE program. + + To aid in writing/debugging service, the + SDK contains a utility (MSTOOLS\BIN\SC.EXE) that + can be used to control, configure, or obtain service status. + SC displays complete status for any service/driver + in the service database, and allows any of the configuration + parameters to be easily changed at the command line. + For more information on SC.EXE, type SC at the command line. + + +------------------------------------------------------------------------------*/ + +#ifndef _SERVICE_H +#define _SERVICE_H + + +#ifdef __cplusplus +extern "C" { +#endif + +////////////////////////////////////////////////////////////////////////////// +//// todo: change to desired strings +//// +// name of the executable +#define SZAPPNAME PACKAGE "serv" +// internal name of the service +#define SZSERVICENAME PACKAGE_NAME "Service" +// displayed name of the service +#define SZSERVICEDISPLAYNAME PACKAGE_NAME " Service" +// list of service dependencies - "dep1\0dep2\0\0" +#define SZDEPENDENCIES TAP_WIN_COMPONENT_ID "\0Dhcp\0\0" +////////////////////////////////////////////////////////////////////////////// + + + +////////////////////////////////////////////////////////////////////////////// +//// todo: ServiceStart()must be defined by in your code. +//// The service should use ReportStatusToSCMgr to indicate +//// progress. This routine must also be used by StartService() +//// to report to the SCM when the service is running. +//// +//// If a ServiceStop procedure is going to take longer than +//// 3 seconds to execute, it should spawn a thread to +//// execute the stop code, and return. Otherwise, the +//// ServiceControlManager will believe that the service has +//// stopped responding +//// + VOID ServiceStart(DWORD dwArgc, LPTSTR *lpszArgv); + VOID ServiceStop(); +////////////////////////////////////////////////////////////////////////////// + + + +////////////////////////////////////////////////////////////////////////////// +//// The following are procedures which +//// may be useful to call within the above procedures, +//// but require no implementation by the user. +//// They are implemented in service.c + +// +// FUNCTION: ReportStatusToSCMgr() +// +// PURPOSE: Sets the current status of the service and +// reports it to the Service Control Manager +// +// PARAMETERS: +// dwCurrentState - the state of the service +// dwWin32ExitCode - error code to report +// dwWaitHint - worst case estimate to next checkpoint +// +// RETURN VALUE: +// TRUE - success +// FALSE - failure +// + BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint); + + +// +// FUNCTION: AddToMessageLog(LPTSTR lpszMsg) +// +// PURPOSE: Allows any thread to log an error message +// +// PARAMETERS: +// lpszMsg - text for message +// +// RETURN VALUE: +// none +// +# define MSG_FLAGS_ERROR (1<<0) +# define MSG_FLAGS_SYS_CODE (1<<1) + void AddToMessageLog(DWORD flags, LPTSTR lpszMsg); + void ResetError (void); +////////////////////////////////////////////////////////////////////////////// + + +#ifdef __cplusplus +} +#endif + +#endif |