From 19814f04692d97e41c942e628034d7e89102716f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Moritz=20'Morty'=20Str=C3=BCbe?= <morty@gmx.net>
Date: Mon, 18 Jul 2011 11:35:23 +0200
Subject: [PATCH] Make wrapper more robust

---
 .../html/man8/x2gocleansessions.8.html             |    2 +-
 .../html/man8/x2gocmdexitmessage.8.html            |    2 +-
 .../html/man8/x2golistdesktops.8.html              |    2 +-
 .../html/man8/x2golistsessions.8.html              |    2 +-
 .../.build_man2html/html/man8/x2gomountdirs.8.html |    2 +-
 .../.build_man2html/html/man8/x2goprint.8.html     |    2 +-
 .../html/man8/x2goresume-session.8.html            |    2 +-
 .../html/man8/x2goruncommand.8.html                |    2 +-
 .../html/man8/x2goshowblocks.8.html                |    2 +-
 .../html/man8/x2gostartagent.8.html                |    2 +-
 .../html/man8/x2gosuspend-session.8.html           |    2 +-
 .../html/man8/x2goterminate-session.8.html         |    2 +-
 .../html/man8/x2goumount-session.8.html            |    2 +-
 x2goserver/x2gosqlitewrapper.c                     |   57 ++++++++++++++++----
 14 files changed, 60 insertions(+), 23 deletions(-)

diff --git a/x2goserver/.build_man2html/html/man8/x2gocleansessions.8.html b/x2goserver/.build_man2html/html/man8/x2gocleansessions.8.html
index 8d896e5..901eead 100644
--- a/x2goserver/.build_man2html/html/man8/x2gocleansessions.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2gocleansessions.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2gocmdexitmessage.8.html b/x2goserver/.build_man2html/html/man8/x2gocmdexitmessage.8.html
index 8c45019..245773f 100644
--- a/x2goserver/.build_man2html/html/man8/x2gocmdexitmessage.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2gocmdexitmessage.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2golistdesktops.8.html b/x2goserver/.build_man2html/html/man8/x2golistdesktops.8.html
index 535f75b..bab9b6a 100644
--- a/x2goserver/.build_man2html/html/man8/x2golistdesktops.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2golistdesktops.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2golistsessions.8.html b/x2goserver/.build_man2html/html/man8/x2golistsessions.8.html
index 9d42577..a761aae 100644
--- a/x2goserver/.build_man2html/html/man8/x2golistsessions.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2golistsessions.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2gomountdirs.8.html b/x2goserver/.build_man2html/html/man8/x2gomountdirs.8.html
index c0c64b5..d597152 100644
--- a/x2goserver/.build_man2html/html/man8/x2gomountdirs.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2gomountdirs.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2goprint.8.html b/x2goserver/.build_man2html/html/man8/x2goprint.8.html
index e42f484..52becbc 100644
--- a/x2goserver/.build_man2html/html/man8/x2goprint.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2goprint.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2goresume-session.8.html b/x2goserver/.build_man2html/html/man8/x2goresume-session.8.html
index 4c6c3e0..4fdc3f1 100644
--- a/x2goserver/.build_man2html/html/man8/x2goresume-session.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2goresume-session.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2goruncommand.8.html b/x2goserver/.build_man2html/html/man8/x2goruncommand.8.html
index c9aff9e..a0b4af3 100644
--- a/x2goserver/.build_man2html/html/man8/x2goruncommand.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2goruncommand.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2goshowblocks.8.html b/x2goserver/.build_man2html/html/man8/x2goshowblocks.8.html
index c9aff9e..a0b4af3 100644
--- a/x2goserver/.build_man2html/html/man8/x2goshowblocks.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2goshowblocks.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2gostartagent.8.html b/x2goserver/.build_man2html/html/man8/x2gostartagent.8.html
index a5c2f51..04eafa7 100644
--- a/x2goserver/.build_man2html/html/man8/x2gostartagent.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2gostartagent.8.html
@@ -60,6 +60,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2gosuspend-session.8.html b/x2goserver/.build_man2html/html/man8/x2gosuspend-session.8.html
index 2c28940..bd2b5f0 100644
--- a/x2goserver/.build_man2html/html/man8/x2gosuspend-session.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2gosuspend-session.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2goterminate-session.8.html b/x2goserver/.build_man2html/html/man8/x2goterminate-session.8.html
index 063b1fa..2c96b7d 100644
--- a/x2goserver/.build_man2html/html/man8/x2goterminate-session.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2goterminate-session.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/.build_man2html/html/man8/x2goumount-session.8.html b/x2goserver/.build_man2html/html/man8/x2goumount-session.8.html
index 3a4f259..745d261 100644
--- a/x2goserver/.build_man2html/html/man8/x2goumount-session.8.html
+++ b/x2goserver/.build_man2html/html/man8/x2goumount-session.8.html
@@ -44,6 +44,6 @@ This manual has been written by Mike Gabriel &lt;<A HREF="mailto:mike.gabriel@da
 This document was created by
 <A HREF="/cgi-bin/man/man2html">man2html</A>,
 using the manual pages.<BR>
-Time: 19:49:43 GMT, July 17, 2011
+Time: 09:33:13 GMT, July 18, 2011
 </BODY>
 </HTML>
diff --git a/x2goserver/x2gosqlitewrapper.c b/x2goserver/x2gosqlitewrapper.c
index b702aa7..b4bb43e 100644
--- a/x2goserver/x2gosqlitewrapper.c
+++ b/x2goserver/x2gosqlitewrapper.c
@@ -24,22 +24,59 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <libgen.h>
+#include <errno.h>
+
 
-int main( int argc, char *argv[] ) {
 
-	char buffer[BUFSIZ];
+
+int main( int argc, char *argv[] ) {
 	char * x2gosqlitewrapper = NULL;
+	size_t path_max;
+	
+/*
+	The following snippet is taken from the realpath manpage
+*/
+#ifdef PATH_MAX
+	path_max = PATH_MAX;
+#else
+	path_max = pathconf (".", _PC_PATH_MAX);
+	if (path_max <= 0){
+		path_max = 4096;
+	}
+#endif
+	{
+		// allocate dynamic buffer in stack: this needs C99 or gnu??
+		char buffer[path_max];
+		ssize_t rvrl;
+		int rvap;
+
+		// resolve link of /proc/self/exe
+		rvrl = readlink("/proc/self/exe", buffer, path_max);
+		if(rvrl == -1){
+			perror("readlink(\"/proc/self/exe\",buffer,path_max)");
+			exit(EXIT_FAILURE);
+		}
+		if(rvrl >= path_max){
+			fprintf(stderr, "Could not resolve the path of this file using \"/proc/self/exe\". The path is to long (> %i)", path_max);
+			exit(EXIT_FAILURE);
+		}
+
+
+		// derive the full path of x2gosqlitewrapper.pl from path of this binary
+		rvap = asprintf(&x2gosqlitewrapper, "%s/%s", dirname(dirname(buffer)), "lib/x2go/x2gosqlitewrapper.pl");
+		if(rvap == -1){
+			fprintf(stderr, "Failed to allocate memory calling asprintf\n");
+			exit(EXIT_FAILURE);
+		}
 
-	// resolve link of /proc/self/exe
-	readlink("/proc/self/exe", buffer, BUFSIZ);
 
-	// derive the full path of x2gosqlitewrapper.pl from path of this binary
-	asprintf(&x2gosqlitewrapper, "%s/%s", dirname(dirname(buffer)), "lib/x2go/x2gosqlitewrapper.pl");
+		// execute the script, taking setuid bit into consideration if set...
+		execv(x2gosqlitewrapper, argv);
 
-	// execute the script, taking setuid bit into consideration if set...
-	execv(x2gosqlitewrapper, argv);
+	}
 
-	// fake a successful return value
-	return 0;
+	// ...fail
+	fprintf(stderr, "Failed to execute %s: %s\n", x2gosqlitewrapper, strerror(errno));
+	return EXIT_FAILURE;
 
 }
-- 
1.7.2.5

