--- /dev/null
+From b9420c3bfc2a8b9d35d0c8e5f6849007c2bc21fa Mon Sep 17 00:00:00 2001
+From: Darik Horn <dajhorn@vanadac.com>
+Date: Mon, 14 Apr 2014 13:22:24 -0400
+Subject: [PATCH] Use FHS installation directories.
+
+Install to `/usr/sbin`, `/usr/lib`, and `/var/lib` according to the Linux
+filesystem hierarchy standard if SoftEther is built through autotools.
+
+In a managed installation, the FHS stipulates that the application must
+accomodate a read-only installation path. This requires a new `GetStateDir`
+function that substitues `GetExeDir` in some parts of the code.
+
+Taken from Github at
+https://github.com/dajhorn/SoftEtherVPN/commit/b9420c3bfc2a8b9d35d0c8e5f6849007c2bc21fa.
+
+Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
+Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
+---
+ autotools/softether.am | 1 +
+ src/Cedar/Admin.c | 2 +-
+ src/Cedar/Command.c | 6 +++++-
+ src/Cedar/Logging.c | 2 +-
+ src/Cedar/Server.c | 4 ++--
+ src/Mayaqua/Encrypt.c | 2 +-
+ src/Mayaqua/FileIO.c | 39 ++++++++++++++++++++++++++++++++++++---
+ src/Mayaqua/FileIO.h | 2 ++
+ src/Mayaqua/Mayaqua.c | 4 ++++
+ src/Mayaqua/Table.c | 6 +++++-
+ src/Mayaqua/Unix.c | 6 +++---
+ src/bin/hamcore/Makefile.am | 8 ++++++--
+ 12 files changed, 67 insertions(+), 15 deletions(-)
+
+Index: b/autotools/softether.am
+===================================================================
+--- a/autotools/softether.am
++++ b/autotools/softether.am
+@@ -27,6 +27,7 @@
+ -I$(top_srcdir)/src \
+ -I$(top_srcdir)/src/Mayaqua \
+ -I$(top_srcdir)/src/Cedar \
++ -DSTATE_DIR='"@localstatedir@/lib/softether"' \
+ -DUNIX \
+ -DUNIX_LINUX \
+ -D_REENTRANT \
+Index: b/src/Cedar/Admin.c
+===================================================================
+--- a/src/Cedar/Admin.c
++++ b/src/Cedar/Admin.c
+@@ -10334,7 +10334,7 @@
+ \r
+ Zero(t, sizeof(RPC_READ_LOG_FILE));\r
+ \r
+- GetExeDir(exe_dir, sizeof(exe_dir));\r
++ GetStateDir(exe_dir, sizeof(exe_dir));\r
+ Format(full_path, sizeof(full_path), "%s/%s", exe_dir, filepath);\r
+ \r
+ // Read file\r
+Index: b/src/Cedar/Command.c
+===================================================================
+--- a/src/Cedar/Command.c
++++ b/src/Cedar/Command.c
+@@ -527,7 +527,7 @@
+ UINT i;\r
+ \r
+ GetExeName(exe, sizeof(exe));\r
+- GetExeDir(exe_dir, sizeof(exe_dir));\r
++ GetStateDir(exe_dir, sizeof(exe_dir));\r
+ \r
+ ok = false;\r
+ dirs = EnumDir(exe_dir);\r
+@@ -552,7 +552,11 @@
+ UCHAR *buf;\r
+ IO *io;\r
+ #ifndef OS_WIN32\r
++#ifdef STATE_DIR\r
++ wchar_t *filename = L"" STATE_DIR L"/vpn_checker_tmp";\r
++#else\r
+ wchar_t *filename = L"/tmp/vpn_checker_tmp";\r
++#endif\r
+ #else // OS_WIN32\r
+ wchar_t filename[MAX_PATH];\r
+ CombinePathW(filename, sizeof(filename), MsGetMyTempDirW(), L"vpn_checker_tmp");\r
+Index: b/src/Cedar/Logging.c
+===================================================================
+--- a/src/Cedar/Logging.c
++++ b/src/Cedar/Logging.c
+@@ -508,7 +508,7 @@
+ \r
+ e = ZeroMalloc(sizeof(ERASER));\r
+ \r
+- GetExeDir(dir, sizeof(dir));\r
++ GetStateDir(dir, sizeof(dir));\r
+ \r
+ e->Log = log;\r
+ e->MinFreeSpace = min_size;\r
+Index: b/src/Cedar/Server.c
+===================================================================
+--- a/src/Cedar/Server.c
++++ b/src/Cedar/Server.c
+@@ -1060,7 +1060,7 @@
+ hubname = NULL;\r
+ }\r
+ \r
+- GetExeDir(exe_dir, sizeof(exe_dir));\r
++ GetStateDir(exe_dir, sizeof(exe_dir));\r
+ \r
+ // Enumerate in the server_log\r
+ if (hubname == NULL)\r
+@@ -1134,7 +1134,7 @@
+ return;\r
+ }\r
+ \r
+- GetExeDir(exe_dir, sizeof(exe_dir));\r
++ GetStateDir(exe_dir, sizeof(exe_dir));\r
+ Format(dir_full_path, sizeof(dir_full_path), "%s/%s", exe_dir, dirname);\r
+ \r
+ dir = EnumDir(dir_full_path);\r
+Index: b/src/Mayaqua/Encrypt.c
+===================================================================
+--- a/src/Mayaqua/Encrypt.c
++++ b/src/Mayaqua/Encrypt.c
+@@ -579,7 +579,7 @@
+ return false;\r
+ }\r
+ \r
+- GetExeDir(dirname, sizeof(dirname));\r
++ GetStateDir(dirname, sizeof(dirname));\r
+ \r
+ // Search the CRL file\r
+ t = EnumDir(dirname);\r
+Index: b/src/Mayaqua/FileIO.c
+===================================================================
+--- a/src/Mayaqua/FileIO.c
++++ b/src/Mayaqua/FileIO.c
+@@ -122,8 +122,14 @@
+ #include <errno.h>\r
+ #include <Mayaqua/Mayaqua.h>\r
+ \r
++#ifdef STATE_DIR\r
++static char exe_file_name[MAX_SIZE] = STATE_DIR "/a.out";\r
++static wchar_t exe_file_name_w[MAX_SIZE] = L"" STATE_DIR L"/a.out";\r
++#else\r
+ static char exe_file_name[MAX_SIZE] = "/tmp/a.out";\r
+ static wchar_t exe_file_name_w[MAX_SIZE] = L"/tmp/a.out";\r
++#endif\r
++\r
+ static LIST *hamcore = NULL;\r
+ static IO *hamcore_io = NULL;\r
+ \r
+@@ -1038,7 +1044,7 @@
+ }\r
+ \r
+ // If the file exist in hamcore/ directory on the local disk, read it\r
+- GetExeDirW(exe_dir, sizeof(exe_dir));\r
++ GetStateDirW(exe_dir, sizeof(exe_dir));\r
+ \r
+ UniFormat(tmp, sizeof(tmp), L"%s/%S/%S", exe_dir, HAMCORE_DIR_NAME, filename);\r
+ \r
+@@ -1154,7 +1160,7 @@
+ return;\r
+ }\r
+ \r
+- GetExeDirW(exe_dir, sizeof(exe_dir));\r
++ GetStateDirW(exe_dir, sizeof(exe_dir));\r
+ UniFormat(tmp, sizeof(tmp), L"%s/%S", exe_dir, HAMCORE_FILE_NAME);\r
+ \r
+ UniFormat(tmp2, sizeof(tmp2), L"%s/%S", exe_dir, HAMCORE_FILE_NAME_2);\r
+@@ -1438,6 +1444,33 @@
+ GetDirNameFromFilePathW(name, size, exe_file_name_w);\r
+ }\r
+ \r
++void GetStateDir(char *name, UINT size)\r
++{\r
++ // Validate arguments\r
++ if (name == NULL)\r
++ {\r
++ return;\r
++ }\r
++#ifdef STATE_DIR\r
++ StrCpy(name, size, STATE_DIR);\r
++#else\r
++ GetExeDir(name, size)\r
++#endif\r
++}\r
++void GetStateDirW(wchar_t *name, UINT size)\r
++{\r
++ // Validate arguments\r
++ if (name == NULL)\r
++ {\r
++ return;\r
++ }\r
++#ifdef STATE_DIR\r
++ UniStrCpy(name, size, L"" STATE_DIR L"");\r
++#else\r
++ GetExeDirW(name, size)\r
++#endif\r
++}\r
++\r
+ // Get the EXE file name\r
+ void GetExeName(char *name, UINT size)\r
+ {\r
+@@ -2389,7 +2422,7 @@
+ else\r
+ {\r
+ wchar_t dir[MAX_SIZE];\r
+- GetExeDirW(dir, sizeof(dir));\r
++ GetStateDirW(dir, sizeof(dir));\r
+ ConbinePathW(dst, size, dir, &src[1]);\r
+ }\r
+ }\r
+Index: b/src/Mayaqua/FileIO.h
+===================================================================
+--- a/src/Mayaqua/FileIO.h
++++ b/src/Mayaqua/FileIO.h
+@@ -349,6 +349,8 @@
+ void GetExeNameW(wchar_t *name, UINT size);\r
+ void GetExeDir(char *name, UINT size);\r
+ void GetExeDirW(wchar_t *name, UINT size);\r
++void GetStateDir(char *name, UINT size);\r
++void GetStateDirW(wchar_t *name, UINT size);\r
+ void BuildHamcore(char *dst_filename, char *src_dir, bool unix_only);\r
+ int CompareHamcore(void *p1, void *p2);\r
+ void InitHamcore();\r
+Index: b/src/Mayaqua/Mayaqua.c
+===================================================================
+--- a/src/Mayaqua/Mayaqua.c
++++ b/src/Mayaqua/Mayaqua.c
+@@ -611,7 +611,11 @@
+ _exit(0);\r
+ }\r
+ \r
++#ifndef STATE_DIR\r
++ // This check causes hamcorebuilder to fail in an unprivileged\r
++ // environment, and is unnecessary for a managed installation.\r
+ CheckUnixTempDir();\r
++#endif\r
+ \r
+ // Initialization of Probe\r
+ InitProbe();\r
+Index: b/src/Mayaqua/Table.c
+===================================================================
+--- a/src/Mayaqua/Table.c
++++ b/src/Mayaqua/Table.c
+@@ -1191,7 +1191,7 @@
+ return;\r
+ }\r
+ \r
+- GetExeDirW(exe, sizeof(exe));\r
++ GetStateDirW(exe, sizeof(exe));\r
+ UniStrCpy(hashtemp, sizeof(hashtemp), strfilename);\r
+ BinToStrW(tmp, sizeof(tmp), filehash, MD5_SIZE);\r
+ UniStrCat(hashtemp, sizeof(hashtemp), tmp);\r
+@@ -1204,7 +1204,11 @@
+ UniStrLower(tmp);\r
+ \r
+ #ifndef OS_WIN32\r
++#ifdef STATE_DIR\r
++ UniStrCpy(exe, sizeof(exe), L"" STATE_DIR L"");\r
++#else\r
+ UniStrCpy(exe, sizeof(exe), L"/tmp");\r
++#endif\r
+ #else // OS_WIN32\r
+ StrToUni(exe, sizeof(exe), MsGetTempDir());\r
+ #endif // OS_WIN32\r
+Index: b/src/Mayaqua/Unix.c
+===================================================================
+--- a/src/Mayaqua/Unix.c
++++ b/src/Mayaqua/Unix.c
+@@ -928,7 +928,7 @@
+ StrCpy(tmp, sizeof(tmp), instance_name);\r
+ }\r
+ \r
+- GetExeDir(dir, sizeof(dir));\r
++ GetStateDir(dir, sizeof(dir));\r
+ \r
+ // File name generation\r
+ Format(name, sizeof(name), "%s/.%s", dir, tmp);\r
+@@ -2260,7 +2260,7 @@
+ return;\r
+ }\r
+ \r
+- GetExeDir(dir, sizeof(dir));\r
++ GetStateDir(dir, sizeof(dir));\r
+ \r
+ GetExeName(exe_name, sizeof(exe_name));\r
+ StrCat(exe_name, sizeof(exe_name), ":pid_hash");\r
+@@ -2305,7 +2305,7 @@
+ return;\r
+ }\r
+ \r
+- GetExeDir(dir, sizeof(dir));\r
++ GetStateDir(dir, sizeof(dir));\r
+ \r
+ GetExeName(exe_name, sizeof(exe_name));\r
+ StrCat(exe_name, sizeof(exe_name), ":pid_hash");\r
+Index: b/src/bin/hamcore/Makefile.am
+===================================================================
+--- a/src/bin/hamcore/Makefile.am
++++ b/src/bin/hamcore/Makefile.am
+@@ -18,12 +18,16 @@
+
+ include $(top_srcdir)/autotools/softether.am
+
+-# This is required to use a custom build rule with -Wall and -Werror enabled.
++# An empty EXEEXT required for overrides with -Wall and -Werror enabled.
+ AUTOMAKE_OPTIONS = -Wno-override
+ EXEEXT =
++
+ HAMCOREBUILDER = $(top_builddir)/src/hamcorebuilder/hamcorebuilder
+
+-sbin_PROGRAMS = hamcore.se2
++noinst_PROGRAMS = hamcore.se2
+
+ hamcore.se2$(EXEEXT): $(HAMCOREBUILDER)
+ $(HAMCOREBUILDER) $(top_srcdir)/src/bin/hamcore $@
++
++install-exec-local: hamcore.se2$(EXEEXT)
++ $(INSTALL_DATA) -D hamcore.se2$(EXEEXT) $(DESTDIR)$(localstatedir)/lib/softether/hamcore.se2