3d6b00a
diff -up openssh-5.2p1/session.c.sesftp openssh-5.2p1/session.c
56bb420
--- openssh-5.2p1/session.c.sesftp	2009-08-09 10:21:11.586827446 +0200
56bb420
+++ openssh-5.2p1/session.c	2009-08-09 10:39:30.475622699 +0200
56bb420
@@ -58,6 +58,9 @@
3d6b00a
 #include <stdlib.h>
3d6b00a
 #include <string.h>
3d6b00a
 #include <unistd.h>
56bb420
+#ifdef WITH_SELINUX
3d6b00a
+#include <selinux/selinux.h>
56bb420
+#endif
3d6b00a
 
3d6b00a
 #include "openbsd-compat/sys-queue.h"
3d6b00a
 #include "xmalloc.h"
56bb420
@@ -101,6 +104,9 @@
56bb420
 	  c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \
56bb420
 	  c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t'))
a827fee
 
56bb420
+#ifdef WITH_SELINUX
56bb420
+#define SFTPD_T		"sftpd_t"
56bb420
+#endif
56bb420
 /* func */
56bb420
 
56bb420
 Session *session_new(void);
56bb420
@@ -1789,6 +1795,10 @@ do_child(Session *s, const char *command
a827fee
 		extern int optind, optreset;
56bb420
 		int i;
56bb420
 		char *p, *args;
56bb420
+#ifdef WITH_SELINUX
56bb420
+		int L1, L2;
56bb420
+		char *c1, *c2, *cx;
56bb420
+#endif
a827fee
 
a827fee
 		setproctitle("%s@internal-sftp-server", s->pw->pw_name);
a827fee
 		args = xstrdup(command ? command : "sftp-server");
56bb420
@@ -1798,6 +1808,32 @@ do_child(Session *s, const char *command
3d6b00a
 		argv[i] = NULL;
3d6b00a
 		optind = optreset = 1;
3d6b00a
 		__progname = argv[0];
56bb420
+#ifdef WITH_SELINUX
56bb420
+		if (getcon ((security_context_t *) &c1) < 0) {
56bb420
+			logit("do_child: getcon failed with %s", strerror (errno));
a827fee
+		} else {
56bb420
+			L1 = strlen (c1) + sizeof (SFTPD_T);
56bb420
+			c2 = xmalloc (L1);
a827fee
+			if (!(cx = index (c1, ':')))
a827fee
+				goto badcontext;
a827fee
+			if (!(cx = index (cx + 1, ':'))) {
a827fee
+badcontext:
a827fee
+				logit ("do_child: unparseable context %s", c1);
a827fee
+			} else {
56bb420
+				L2 = cx - c1 + 1;
56bb420
+				memcpy (c2, c1, L2);
56bb420
+				strlcpy (c2 + L2, SFTPD_T, L1);
a827fee
+				if ((cx = index (cx + 1, ':')))
56bb420
+					strlcat (c2, cx, L1);
170a775
+				if (setcon (c2) < 0) 
56bb420
+					logit("do_child: setcon failed with %s", strerror (errno));
a827fee
+			
a827fee
+			}
56bb420
+			xfree (c1);
56bb420
+			xfree (c2);
a827fee
+		}		
56bb420
+#endif
f35d4ae
+			
3d6b00a
 		exit(sftp_server_main(i, argv, s->pw));
3d6b00a
 	}
3d6b00a