1From 64dd780905ae339a0a57e4aba541799016816a1a Mon Sep 17 00:00:00 2001
2From: Darik Horn <dajhorn@vanadac.com>
3Date: Fri, 3 Oct 2014 13:30:24 -0400
4Subject: [PATCH] Create a non-forking softetherd for upstart and systemd.
5
6Implement a daemon that expects to be invoked by a new-style init like upstart
7or systemd as:
8
9	/usr/sbin/softetherd [vpnbridge|vpnclient|vpnserver]
10
11Alternatively, if the command line argument is empty, then use the
12`SOFTETHER_MODE` environment variable instead.
13
14Conflicts:
15	src/bin/hamcore/strtable_en.stb
16
17Taken from Github at
18https://github.com/dajhorn/SoftEtherVPN/commit/64dd780905ae339a0a57e4aba541799016816a1a.
19
20Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
21Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
22---
23 configure.ac                    |   1 +
24 src/Makefile.am                 |   3 ++
25 src/bin/hamcore/strtable_en.stb |   1 +
26 src/softetherd/Makefile.am      |  28 ++++++++++
27 src/softetherd/softetherd.c     | 114 ++++++++++++++++++++++++++++++++++++++++
28 5 files changed, 147 insertions(+)
29 create mode 100644 src/softetherd/Makefile.am
30 create mode 100644 src/softetherd/softetherd.c
31
32Index: b/configure.ac
33===================================================================
34--- a/configure.ac
35+++ b/configure.ac
36@@ -36,6 +36,7 @@
37 	src/vpnclient/Makefile
38 	src/vpnbridge/Makefile
39 	src/vpncmd/Makefile
40+	src/softetherd/Makefile
41 ])
42
43
44Index: b/src/Makefile.am
45===================================================================
46--- a/src/Makefile.am
47+++ b/src/Makefile.am
48@@ -27,3 +27,6 @@
49
50 # These are the final build products.
51 SUBDIRS += vpnserver vpnclient vpnbridge vpncmd
52+
53+# This is a daemon for upstart and systemd.
54+SUBDIRS += softetherd
55Index: b/src/bin/hamcore/strtable_en.stb
56===================================================================
57--- a/src/bin/hamcore/strtable_en.stb
58+++ b/src/bin/hamcore/strtable_en.stb
59@@ -1062,6 +1062,7 @@
60
61
62 # Concerning services (UNIX)
63+UNIX_DAEMON_HELP		SoftEther VPN non-forking daemon for upstart and systemd.\nCommand Usage:\n %S vpnbridge  - Enable bridging features.\n %S vpnclient  - Enable client features.\n %S vpnserver  - Enable all features.\nThe parameter can be set in the SOFTETHER_MODE environment variable.\n\n
64 UNIX_SVC_HELP			%S service program\nCopyright (c) SoftEther VPN Project. All Rights Reserved.\n\n%S command usage:\n %S start  - Start the %S service.\n %S stop   - Stop the %S service if the service has been already started.\n\n
65 UNIX_SVC_STARTED		The %S service has been started.\n
66 UNIX_SVC_STOPPING		Stopping the %S service ...\n
67Index: b/src/softetherd/Makefile.am
68===================================================================
69--- /dev/null
70+++ b/src/softetherd/Makefile.am
71@@ -0,0 +1,28 @@
72+#  Copyright 2014 Darik Horn <dajhorn@vanadac.com>
73+#
74+#  This file is part of SoftEther.
75+#
76+#  SoftEther is free software: you can redistribute it and/or modify it under
77+#  the terms of the GNU General Public License as published by the Free
78+#  Software Foundation, either version 2 of the License, or (at your option)
79+#  any later version.
80+#
81+#  SoftEther is distributed in the hope that it will be useful, but WITHOUT ANY
82+#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
83+#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
84+#  details.
85+#
86+#  You should have received a copy of the GNU General Public License along with
87+#  SoftEther.  If not, see <http://www.gnu.org/licenses/>.
88+
89+
90+include $(top_srcdir)/autotools/softether.am
91+
92+sbin_PROGRAMS = \
93+	softetherd
94+
95+softetherd_SOURCES = \
96+	softetherd.c
97+
98+softetherd_LDADD = \
99+	$(top_builddir)/src/libsoftether/libsoftether.la
100Index: b/src/softetherd/softetherd.c
101===================================================================
102--- /dev/null
103+++ b/src/softetherd/softetherd.c
104@@ -0,0 +1,114 @@
105+//  SoftEther VPN daemon for upstart and systemd.
106+//
107+//  Copyright 2014 Darik Horn <dajhorn@vanadac.com>
108+//
109+//  This file is part of SoftEther.
110+//
111+//  SoftEther is free software: you can redistribute it and/or modify it under
112+//  the terms of the GNU General Public License as published by the Free
113+//  Software Foundation, either version 2 of the License, or (at your option)
114+//  any later version.
115+//
116+//  SoftEther is distributed in the hope that it will be useful, but WITHOUT ANY
117+//  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
118+//  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
119+//  details.
120+//
121+//  You should have received a copy of the GNU General Public License along with
122+//  SoftEther.  If not, see <http://www.gnu.org/licenses/>.
123+
124+
125+#include <GlobalConst.h>
126+
127+#define	VPN_EXE
128+
129+#include <stdio.h>
130+#include <stdlib.h>
131+#include <string.h>
132+#include <wchar.h>
133+#include <stdarg.h>
134+#include <time.h>
135+#include <Mayaqua/Mayaqua.h>
136+#include <Cedar/Cedar.h>
137+
138+void DaemonUsage(char *name)
139+{
140+	UniPrint(_UU("UNIX_DAEMON_HELP"), name, name, name);
141+}
142+
143+
144+void DaemonStartProcess()
145+{
146+	// This environment variable is exported by upstart.
147+	char *upstart_job = getenv("UPSTART_JOB");
148+
149+	InitCedar();
150+	StInit();
151+	StStartServer(false);
152+
153+	// Notify upstart that softetherd is ready.
154+	if (upstart_job != NULL)
155+	{
156+		unsetenv("UPSTART_JOB");
157+		raise(SIGSTOP);
158+	}
159+}
160+
161+
162+void DaemonStopProcess()
163+{
164+	StStopServer();
165+	StFree();
166+	FreeCedar();
167+}
168+
169+
170+int main(int argc, char *argv[])
171+{
172+	// This environment variable is sourced and exported by the init process from /etc/default/softether.
173+	char *softether_mode = getenv("SOFTETHER_MODE");
174+
175+	InitMayaqua(false, false, argc, argv);
176+
177+	// Check for an explicit invocation. (eg: "/usr/sbin/softetherd vpnserver")
178+	if (argc >= 2)
179+	{
180+		if (StrCmpi(argv[1], "vpnbridge") == 0
181+		 || StrCmpi(argv[1], "vpnclient") == 0
182+		 || StrCmpi(argv[1], "vpnserver") == 0)
183+		{
184+			UnixExecService(argv[1], DaemonStartProcess, DaemonStopProcess);
185+			FreeMayaqua();
186+			return 0;
187+		}
188+
189+		// Exit status codes 150..199 are reserved for the application by the LSB.
190+		fprintf(stderr, "Error: Unrecognized parameter: %s\n", argv[1]);
191+		fflush(stderr);
192+		FreeMayaqua();
193+		return 150;
194+	}
195+
196+	// Alternatively, use the environment variable.
197+	if (softether_mode != NULL)
198+	{
199+		if (StrCmpi(softether_mode, "vpnbridge") == 0
200+		 || StrCmpi(softether_mode, "vpnclient") == 0
201+		 || StrCmpi(softether_mode, "vpnserver") == 0)
202+		{
203+			UnixExecService(softether_mode, DaemonStartProcess, DaemonStopProcess);
204+			FreeMayaqua();
205+			return 0;
206+		}
207+
208+		// Exit status codes 150..199 are reserved for the application by the LSB.
209+		fprintf(stderr, "Error: Unrecognized environment variable: SOFTETHER_MODE=%s\n", softether_mode);
210+		fflush(stderr);
211+		FreeMayaqua();
212+		return 151;
213+	}
214+
215+	DaemonUsage(argv[0]);
216+	FreeMayaqua();
217+	return 3;
218+}
219