vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jan F. Chadima eca05f
diff -up /dev/null openssh-5.2p1/chrootenv.h
Jan F. Chadima aa8983
--- /dev/null	2009-07-23 14:57:23.604046842 +0200
Jan F. Chadima aa8983
+++ openssh-5.2p1/chrootenv.h	2009-07-24 07:11:29.000000000 +0200
Jan F. Chadima f4b0b4
@@ -0,0 +1,32 @@
Jan F. Chadima eca05f
+/* $OpenBSD: session.h,v 1.30 2008/05/08 12:21:16 djm Exp $ */
Jan F. Chadima eca05f
+
Jan F. Chadima eca05f
+/*
Jan F. Chadima eca05f
+ * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
Jan F. Chadima eca05f
+ *
Jan F. Chadima eca05f
+ * Redistribution and use in source and binary forms, with or without
Jan F. Chadima eca05f
+ * modification, are permitted provided that the following conditions
Jan F. Chadima eca05f
+ * are met:
Jan F. Chadima eca05f
+ * 1. Redistributions of source code must retain the above copyright
Jan F. Chadima eca05f
+ *    notice, this list of conditions and the following disclaimer.
Jan F. Chadima eca05f
+ * 2. Redistributions in binary form must reproduce the above copyright
Jan F. Chadima eca05f
+ *    notice, this list of conditions and the following disclaimer in the
Jan F. Chadima eca05f
+ *    documentation and/or other materials provided with the distribution.
Jan F. Chadima eca05f
+ *
Jan F. Chadima eca05f
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Jan F. Chadima eca05f
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Jan F. Chadima eca05f
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Jan F. Chadima eca05f
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Jan F. Chadima eca05f
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Jan F. Chadima eca05f
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Jan F. Chadima eca05f
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Jan F. Chadima eca05f
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Jan F. Chadima eca05f
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Jan F. Chadima eca05f
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jan F. Chadima eca05f
+ */
Jan F. Chadima eca05f
+#ifndef CHROOTENV_H
Jan F. Chadima eca05f
+#define CHROOTENV_H
Jan F. Chadima eca05f
+
Jan F. Chadima eca05f
+extern int	chroot_no_tree;
Jan F. Chadima eca05f
+
Jan F. Chadima eca05f
+#endif
Jan F. Chadima eca05f
+
Jan F. Chadima c1398b
diff -up openssh-5.2p1/session.c.homechroot openssh-5.2p1/session.c
Jan F. Chadima aa8983
--- openssh-5.2p1/session.c.homechroot	2009-07-24 07:11:22.000000000 +0200
Jan F. Chadima aa8983
+++ openssh-5.2p1/session.c	2009-07-24 07:33:14.000000000 +0200
Jan F. Chadima f4b0b4
@@ -119,6 +119,8 @@ void	do_child(Session *, const char *);
Jan F. Chadima eca05f
 void	do_motd(void);
Jan F. Chadima eca05f
 int	check_quietlogin(Session *, const char *);
Jan F. Chadima c1398b
 
Jan F. Chadima c1398b
+int	chroot_no_tree = 0;
Jan F. Chadima c1398b
+
Jan F. Chadima eca05f
 static void do_authenticated1(Authctxt *);
Jan F. Chadima eca05f
 static void do_authenticated2(Authctxt *);
Jan F. Chadima eca05f
 
Jan F. Chadima aa8983
@@ -802,6 +804,11 @@ do_exec(Session *s, const char *command)
Jan F. Chadima aa8983
 		debug("Forced command (key option) '%.900s'", command);
Jan F. Chadima aa8983
 	}
Jan F. Chadima eca05f
 
Jan F. Chadima eca05f
+	if ((s->is_subsystem != SUBSYSTEM_INT_SFTP) && chroot_no_tree) {
Jan F. Chadima eca05f
+		logit("You aren't welcomed, go away!");
Jan F. Chadima eca05f
+		exit (1);
Jan F. Chadima eca05f
+	}
Jan F. Chadima eca05f
+
Jan F. Chadima aa8983
 #ifdef SSH_AUDIT_EVENTS
Jan F. Chadima aa8983
 	if (command != NULL)
Jan F. Chadima aa8983
 		PRIVSEP(audit_run_command(command));
Jan F. Chadima f4b0b4
@@ -1408,6 +1415,7 @@ safely_chroot(const char *path, uid_t ui
Jan F. Chadima 061e21
 	const char *cp;
Jan F. Chadima 061e21
 	char component[MAXPATHLEN];
Jan F. Chadima 061e21
 	struct stat st;
Jan F. Chadima 061e21
+	int last;
Jan F. Chadima 061e21
 
Jan F. Chadima 061e21
 	if (*path != '/')
Jan F. Chadima 061e21
 		fatal("chroot path does not begin at root");
Jan F. Chadima f4b0b4
@@ -1419,7 +1427,7 @@ safely_chroot(const char *path, uid_t ui
Jan F. Chadima 061e21
 	 * root-owned directory with strict permissions.
Jan F. Chadima 061e21
 	 */
Jan F. Chadima 061e21
 	for (cp = path; cp != NULL;) {
Jan F. Chadima 061e21
-		if ((cp = strchr(cp, '/')) == NULL)
Jan F. Chadima 061e21
+		if (((last = ((cp = strchr(cp, '/')) == NULL))))
Jan F. Chadima 061e21
 			strlcpy(component, path, sizeof(component));
Jan F. Chadima 061e21
 		else {
Jan F. Chadima 061e21
 			cp++;
Jan F. Chadima f4b0b4
@@ -1432,15 +1440,19 @@ safely_chroot(const char *path, uid_t ui
Jan F. Chadima 061e21
 		if (stat(component, &st) != 0)
Jan F. Chadima 061e21
 			fatal("%s: stat(\"%s\"): %s", __func__,
Jan F. Chadima 061e21
 			    component, strerror(errno));
Jan F. Chadima 061e21
-		if (st.st_uid != 0 || (st.st_mode & 022) != 0)
Jan F. Chadima 061e21
+		if ((st.st_uid != 0 || (st.st_mode & 022) != 0) && !(last && st.st_uid == uid))
Jan F. Chadima 061e21
 			fatal("bad ownership or modes for chroot "
Jan F. Chadima 061e21
 			    "directory %s\"%s\"", 
Jan F. Chadima 061e21
 			    cp == NULL ? "" : "component ", component);
Jan F. Chadima c1398b
 		if (!S_ISDIR(st.st_mode))
Jan F. Chadima c1398b
 			fatal("chroot path %s\"%s\" is not a directory",
Jan F. Chadima c1398b
 			    cp == NULL ? "" : "component ", component);
Jan F. Chadima f4b0b4
-
Jan F. Chadima c1398b
 	}
Jan F. Chadima f4b0b4
+	setenv ("TZ", "/etc/localtime", 0);
Jan F. Chadima f4b0b4
+	tzset ();
Jan F. Chadima f4b0b4
+
Jan F. Chadima eca05f
+	if (st.st_uid != uid)
Jan F. Chadima eca05f
+		++chroot_no_tree;
Jan F. Chadima f4b0b4
 
Jan F. Chadima eca05f
 	if (chdir(path) == -1)
Jan F. Chadima eca05f
 		fatal("Unable to chdir to chroot path \"%s\": "
Jan F. Chadima f4b0b4
@@ -1451,6 +1463,10 @@ safely_chroot(const char *path, uid_t ui
Jan F. Chadima eca05f
 	if (chdir("/") == -1)
Jan F. Chadima eca05f
 		fatal("%s: chdir(/) after chroot: %s",
Jan F. Chadima eca05f
 		    __func__, strerror(errno));
Jan F. Chadima eca05f
+
Jan F. Chadima eca05f
+	if (access ("/etc/localtime", R_OK) < 0)
Jan F. Chadima eca05f
+			++chroot_no_tree;
Jan F. Chadima eca05f
+
Jan F. Chadima eca05f
 	verbose("Changed root directory to \"%s\"", path);
Jan F. Chadima eca05f
 }
Jan F. Chadima eca05f
 
Jan F. Chadima eca05f
diff -up openssh-5.2p1/sftp.c.homechroot openssh-5.2p1/sftp.c
Jan F. Chadima eca05f
--- openssh-5.2p1/sftp.c.homechroot	2009-02-14 06:26:19.000000000 +0100
Jan F. Chadima aa8983
+++ openssh-5.2p1/sftp.c	2009-07-24 07:11:29.000000000 +0200
Jan F. Chadima f4b0b4
@@ -94,6 +94,8 @@ int remote_glob(struct sftp_conn *, cons
Jan F. Chadima eca05f
 
Jan F. Chadima eca05f
 extern char *__progname;
Jan F. Chadima eca05f
 
Jan F. Chadima eca05f
+int	chroot_no_tree = 0;
Jan F. Chadima eca05f
+
Jan F. Chadima eca05f
 /* Separators for interactive commands */
Jan F. Chadima eca05f
 #define WHITESPACE " \t\r\n"
Jan F. Chadima eca05f
 
Jan F. Chadima eca05f
diff -up openssh-5.2p1/sftp-common.c.homechroot openssh-5.2p1/sftp-common.c
Jan F. Chadima eca05f
--- openssh-5.2p1/sftp-common.c.homechroot	2006-08-05 04:39:40.000000000 +0200
Jan F. Chadima aa8983
+++ openssh-5.2p1/sftp-common.c	2009-07-24 07:11:29.000000000 +0200
Jan F. Chadima eca05f
@@ -40,6 +40,7 @@
Jan F. Chadima eca05f
 #include "xmalloc.h"
Jan F. Chadima eca05f
 #include "buffer.h"
Jan F. Chadima eca05f
 #include "log.h"
Jan F. Chadima eca05f
+#include "chrootenv.h"
Jan F. Chadima eca05f
 
Jan F. Chadima eca05f
 #include "sftp.h"
Jan F. Chadima eca05f
 #include "sftp-common.h"
Jan F. Chadima f4b0b4
@@ -194,13 +195,13 @@ ls_file(const char *name, const struct s
Jan F. Chadima eca05f
 	char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
Jan F. Chadima eca05f
 
Jan F. Chadima eca05f
 	strmode(st->st_mode, mode);
Jan F. Chadima eca05f
-	if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
Jan F. Chadima eca05f
+	if (!remote && !chroot_no_tree && (pw = getpwuid(st->st_uid)) != NULL) {
Jan F. Chadima eca05f
 		user = pw->pw_name;
Jan F. Chadima eca05f
 	} else {
Jan F. Chadima eca05f
 		snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
Jan F. Chadima eca05f
 		user = ubuf;
Jan F. Chadima eca05f
 	}
Jan F. Chadima eca05f
-	if (!remote && (gr = getgrgid(st->st_gid)) != NULL) {
Jan F. Chadima eca05f
+	if (!remote && !chroot_no_tree && (gr = getgrgid(st->st_gid)) != NULL) {
Jan F. Chadima eca05f
 		group = gr->gr_name;
Jan F. Chadima eca05f
 	} else {
Jan F. Chadima eca05f
 		snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
Jan F. Chadima c1398b
diff -up openssh-5.2p1/sftp-server-main.c.homechroot openssh-5.2p1/sftp-server-main.c
Jan F. Chadima c1398b
--- openssh-5.2p1/sftp-server-main.c.homechroot	2009-02-21 22:47:02.000000000 +0100
Jan F. Chadima aa8983
+++ openssh-5.2p1/sftp-server-main.c	2009-07-24 07:11:29.000000000 +0200
Jan F. Chadima f4b0b4
@@ -22,11 +22,14 @@
Jan F. Chadima eca05f
 #include <stdarg.h>
Jan F. Chadima eca05f
 #include <stdio.h>
Jan F. Chadima eca05f
 #include <unistd.h>
Jan F. Chadima eca05f
+#include <time.h>
Jan F. Chadima eca05f
 
Jan F. Chadima eca05f
 #include "log.h"
Jan F. Chadima c1398b
 #include "sftp.h"
Jan F. Chadima c1398b
 #include "misc.h"
Jan F. Chadima c1398b
 
Jan F. Chadima eca05f
+int	chroot_no_tree = 0;
Jan F. Chadima c1398b
+
Jan F. Chadima c1398b
 void
Jan F. Chadima c1398b
 cleanup_exit(int i)
Jan F. Chadima c1398b
 {