From 349cfa7acb95abe865209a28e417ec74b56f9bba Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Iniesta Date: Tue, 21 Feb 2012 15:53:40 +0100 Subject: Imported Upstream version 2.2.1 --- service-win32/Makefile.am | 41 +++ service-win32/Makefile.in | 509 ++++++++++++++++++++++++++++++++ service-win32/msvc.mak | 30 ++ service-win32/openvpnserv.c | 530 +++++++++++++++++++++++++++++++++ service-win32/service.c | 695 ++++++++++++++++++++++++++++++++++++++++++++ service-win32/service.h | 141 +++++++++ 6 files changed, 1946 insertions(+) create mode 100644 service-win32/Makefile.am create mode 100644 service-win32/Makefile.in create mode 100644 service-win32/msvc.mak create mode 100755 service-win32/openvpnserv.c create mode 100644 service-win32/service.c create mode 100644 service-win32/service.h (limited to 'service-win32') diff --git a/service-win32/Makefile.am b/service-win32/Makefile.am new file mode 100644 index 0000000..4e4f55e --- /dev/null +++ b/service-win32/Makefile.am @@ -0,0 +1,41 @@ +# +# 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. +# +# 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 +# + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +if WIN32 + +sbin_PROGRAMS = openvpnserv + +openvpnserv_SOURCES = \ + openvpnserv.c \ + service.h service.c + +else + +dist_noinst_DATA = \ + openvpnserv.c \ + service.h service.c + +endif diff --git a/service-win32/Makefile.in b/service-win32/Makefile.in new file mode 100644 index 0000000..88ef15b --- /dev/null +++ b/service-win32/Makefile.in @@ -0,0 +1,509 @@ +# Makefile.in generated by automake 1.11.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, +# Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +# +# 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. +# +# 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 +# + + +VPATH = @srcdir@ +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +@WIN32_TRUE@sbin_PROGRAMS = openvpnserv$(EXEEXT) +subdir = service-win32 +DIST_COMMON = $(am__dist_noinst_DATA_DIST) $(srcdir)/Makefile.am \ + $(srcdir)/Makefile.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \ + $(top_srcdir)/version.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am__installdirs = "$(DESTDIR)$(sbindir)" +PROGRAMS = $(sbin_PROGRAMS) +am__openvpnserv_SOURCES_DIST = openvpnserv.c service.h service.c +@WIN32_TRUE@am_openvpnserv_OBJECTS = openvpnserv.$(OBJEXT) \ +@WIN32_TRUE@ service.$(OBJEXT) +openvpnserv_OBJECTS = $(am_openvpnserv_OBJECTS) +openvpnserv_LDADD = $(LDADD) +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__depfiles_maybe = depfiles +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +CCLD = $(CC) +LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ +SOURCES = $(openvpnserv_SOURCES) +DIST_SOURCES = $(am__openvpnserv_SOURCES_DIST) +am__dist_noinst_DATA_DIST = openvpnserv.c service.h service.c +DATA = $(dist_noinst_DATA) +ETAGS = etags +CTAGS = ctags +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +GREP = @GREP@ +IFCONFIG = @IFCONFIG@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +IPROUTE = @IPROUTE@ +LDFLAGS = @LDFLAGS@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LTLIBOBJS = @LTLIBOBJS@ +MAKEINFO = @MAKEINFO@ +MAN2HTML = @MAN2HTML@ +MKDIR_P = @MKDIR_P@ +NETSTAT = @NETSTAT@ +OBJEXT = @OBJEXT@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +ROUTE = @ROUTE@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +TAP_ID = @TAP_ID@ +TAP_WIN32_MIN_MAJOR = @TAP_WIN32_MIN_MAJOR@ +TAP_WIN32_MIN_MINOR = @TAP_WIN32_MIN_MINOR@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_CC = @ac_ct_CC@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +win32datadir = @win32datadir@ +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in +@WIN32_TRUE@openvpnserv_SOURCES = \ +@WIN32_TRUE@ openvpnserv.c \ +@WIN32_TRUE@ service.h service.c + +@WIN32_FALSE@dist_noinst_DATA = \ +@WIN32_FALSE@ openvpnserv.c \ +@WIN32_FALSE@ service.h service.c + +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu service-win32/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --gnu service-win32/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): +install-sbinPROGRAMS: $(sbin_PROGRAMS) + @$(NORMAL_INSTALL) + test -z "$(sbindir)" || $(MKDIR_P) "$(DESTDIR)$(sbindir)" + @list='$(sbin_PROGRAMS)'; test -n "$(sbindir)" || list=; \ + for p in $$list; do echo "$$p $$p"; done | \ + sed 's/$(EXEEXT)$$//' | \ + while read p p1; do if test -f $$p; \ + then echo "$$p"; echo "$$p"; else :; fi; \ + done | \ + sed -e 'p;s,.*/,,;n;h' -e 's|.*|.|' \ + -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | \ + sed 'N;N;N;s,\n, ,g' | \ + $(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } \ + { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } \ + if ($$2 == $$4) files[d] = files[d] " " $$1; \ + else { print "f", $$3 "/" $$4, $$1; } } \ + END { for (d in files) print "f", d, files[d] }' | \ + while read type dir files; do \ + if test "$$dir" = .; then dir=; else dir=/$$dir; fi; \ + test -z "$$files" || { \ + echo " $(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(sbindir)$$dir'"; \ + $(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(sbindir)$$dir" || exit $$?; \ + } \ + ; done + +uninstall-sbinPROGRAMS: + @$(NORMAL_UNINSTALL) + @list='$(sbin_PROGRAMS)'; test -n "$(sbindir)" || list=; \ + files=`for p in $$list; do echo "$$p"; done | \ + sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' \ + -e 's/$$/$(EXEEXT)/' `; \ + test -n "$$list" || exit 0; \ + echo " ( cd '$(DESTDIR)$(sbindir)' && rm -f" $$files ")"; \ + cd "$(DESTDIR)$(sbindir)" && rm -f $$files + +clean-sbinPROGRAMS: + -test -z "$(sbin_PROGRAMS)" || rm -f $(sbin_PROGRAMS) +openvpnserv$(EXEEXT): $(openvpnserv_OBJECTS) $(openvpnserv_DEPENDENCIES) + @rm -f openvpnserv$(EXEEXT) + $(LINK) $(openvpnserv_OBJECTS) $(openvpnserv_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/openvpnserv.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/service.Po@am__quote@ + +.c.o: +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(COMPILE) -c $< + +.c.obj: +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(COMPILE) -c `$(CYGPATH_W) '$<'` + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + set x; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: CTAGS +CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(PROGRAMS) $(DATA) +installdirs: + for dir in "$(DESTDIR)$(sbindir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." + -test -z "$(MAINTAINERCLEANFILES)" || rm -f $(MAINTAINERCLEANFILES) +clean: clean-am + +clean-am: clean-generic clean-sbinPROGRAMS mostlyclean-am + +distclean: distclean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: install-sbinPROGRAMS + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-sbinPROGRAMS + +.MAKE: install-am install-strip + +.PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \ + clean-sbinPROGRAMS ctags distclean distclean-compile \ + distclean-generic distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-html install-html-am install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-sbinPROGRAMS install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \ + uninstall-am uninstall-sbinPROGRAMS + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/service-win32/msvc.mak b/service-win32/msvc.mak new file mode 100644 index 0000000..ba4bab7 --- /dev/null +++ b/service-win32/msvc.mak @@ -0,0 +1,30 @@ +# This makefile builds the OpenVPN service wrapper for Windows in the +# Visual Studio 2008 environment. + +# Some of these libs may not be needed +LIBS = ws2_32.lib crypt32.lib iphlpapi.lib winmm.lib user32.lib gdi32.lib advapi32.lib wininet.lib +EXE = openvpnserv.exe + +CPP=cl.exe +CPP_ARG_COMMON=/nologo /W3 -DWIN32 -DWIN32_LEAN_AND_MEAN -D_CONSOLE -D_MBCS -D_CRT_SECURE_NO_DEPRECATE /FD /c -I".." +CPP_PROJ=$(CPP_ARG_COMMON) /O2 /MD -DNDEBUG + +LINK32=link.exe +LINK32_FLAGS=/nologo /subsystem:console /incremental:no + +OBJS = \ + openvpnserv.obj \ + service.obj + +openvpnserv : $(OBJS) + $(LINK32) @<< + $(LINK32_FLAGS) "/out:$(EXE)" $(LIBS) $(OBJS) +<< + +clean : + del /Q $(OBJS) $(EXE) *.idb *.pdb + +.c.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< diff --git a/service-win32/openvpnserv.c b/service-win32/openvpnserv.c new file mode 100755 index 0000000..5b0eb6e --- /dev/null +++ b/service-win32/openvpnserv.c @@ -0,0 +1,530 @@ +/* + * 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. + * + * 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. + */ + +#include "config.h" +#include +#include +#include +#include +#include +#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 ret = 0; + if (size > 0) + { + va_start (arglist, format); + ret = vsnprintf (str, size, format, arglist); + va_end (arglist); + str[size - 1] = 0; + } + return ret; +} + + +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] == '.' && !strcasecmp (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 (!strcasecmp (priority_string, "IDLE_PRIORITY_CLASS")) + priority = IDLE_PRIORITY_CLASS; + else if (!strcasecmp (priority_string, "BELOW_NORMAL_PRIORITY_CLASS")) + priority = BELOW_NORMAL_PRIORITY_CLASS; + else if (!strcasecmp (priority_string, "NORMAL_PRIORITY_CLASS")) + priority = NORMAL_PRIORITY_CLASS; + else if (!strcasecmp (priority_string, "ABOVE_NORMAL_PRIORITY_CLASS")) + priority = ABOVE_NORMAL_PRIORITY_CLASS; + else if (!strcasecmp (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/service-win32/service.c b/service-win32/service.c new file mode 100644 index 0000000..91b5821 --- /dev/null +++ b/service-win32/service.c @@ -0,0 +1,695 @@ +/*--------------------------------------------------------------------------- +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 ); + +---------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include + +#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 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/service-win32/service.h b/service-win32/service.h new file mode 100644 index 0000000..028d075 --- /dev/null +++ b/service-win32/service.h @@ -0,0 +1,141 @@ +/*--------------------------------------------------------------------------- +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 + + -? to display this list + -install to install the service + -remove to remove the service + -debug 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 + +#include "config.h" + +////////////////////////////////////////////////////////////////////////////// +//// 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_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 -- cgit v1.2.3