Index: servconf.c
--- servconf.c.orig	2009-06-21 12:26:17 +0200
+++ servconf.c	2009-10-02 13:12:31 +0200
@@ -128,6 +128,12 @@
 	options->adm_forced_command = NULL;
 	options->chroot_directory = NULL;
 	options->zero_knowledge_password_authentication = -1;
+ 	options->log_sftp = LOG_SFTP_NOT_SET;
+ 	options->sftp_log_facility = SYSLOG_FACILITY_NOT_SET;
+ 	options->sftp_log_level = SYSLOG_LEVEL_NOT_SET;
+ 	memset(options->sftp_umask, 0, SFTP_UMASK_LENGTH);
+ 	options->sftp_permit_chmod = SFTP_PERMIT_NOT_SET;
+ 	options->sftp_permit_chown = SFTP_PERMIT_NOT_SET;
 }
 
 void
@@ -262,6 +268,24 @@
 	if (options->zero_knowledge_password_authentication == -1)
 		options->zero_knowledge_password_authentication = 0;
 
+	/* Turn sftp-server logging off by default */
+	if (options->log_sftp == LOG_SFTP_NOT_SET)
+		options->log_sftp = LOG_SFTP_NO;
+        if (options->sftp_log_facility == SYSLOG_FACILITY_NOT_SET)
+                options->sftp_log_facility = SYSLOG_FACILITY_AUTH;
+        if (options->sftp_log_level == SYSLOG_LEVEL_NOT_SET)
+                options->sftp_log_level = SYSLOG_LEVEL_INFO;
+
+	/* Don't set sftp-server umask */
+	if (!options->sftp_umask)
+		memset(options->sftp_umask, 0, SFTP_UMASK_LENGTH);
+
+	/* allow sftp client to issue chmod, chown / chgrp commands */
+	if (options->sftp_permit_chmod == SFTP_PERMIT_NOT_SET)
+		options->sftp_permit_chmod = SFTP_PERMIT_YES;
+	if (options->sftp_permit_chown == SFTP_PERMIT_NOT_SET)
+		options->sftp_permit_chown = SFTP_PERMIT_YES;
+
 	/* Turn privilege separation on by default */
 	if (use_privsep == -1)
 		use_privsep = 1;
@@ -306,6 +330,9 @@
 	sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
 	sUsePrivilegeSeparation, sAllowAgentForwarding,
 	sZeroKnowledgePasswordAuthentication,
+	sLogSftp, sSftpLogFacility, sSftpLogLevel,
+	sSftpUmask,
+	sSftpPermitChown, sSftpPermitChmod,
 	sDeprecated, sUnsupported
 } ServerOpCodes;
 
@@ -320,6 +347,12 @@
 	u_int flags;
 } keywords[] = {
 	/* Portable-specific options */
+ 	{ "logsftp", sLogSftp},
+ 	{ "sftplogfacility", sSftpLogFacility},
+ 	{ "sftploglevel", sSftpLogLevel},
+ 	{ "sftpumask", sSftpUmask},
+ 	{ "sftppermitchmod", sSftpPermitChmod},
+ 	{ "sftppermitchown", sSftpPermitChown},
 #ifdef USE_PAM
 	{ "usepam", sUsePAM, SSHCFG_GLOBAL },
 #else
@@ -645,6 +678,8 @@
 	int port;
 	u_int i, flags = 0;
 	size_t len;
+	unsigned int umaskvalue = 0;
+	char *umaskptr;
 
 	cp = line;
 	if ((arg = strdelim(&cp)) == NULL)
@@ -1178,6 +1213,58 @@
 		charptr = &options->banner;
 		goto parse_filename;
 
+        case sLogSftp:
+                intptr = &options->log_sftp;
+                goto parse_flag;
+
+        case sSftpLogFacility:
+                intptr = (int *) &options->sftp_log_facility;
+                arg = strdelim(&cp);
+                value = log_facility_number(arg);
+                if (value == SYSLOG_FACILITY_NOT_SET)
+                        fatal("%.200s line %d: unsupported log facility '%s'",
+                            filename, linenum, arg ? arg : "<NONE>");
+                if (*intptr == -1)
+                        *intptr = (SyslogFacility) value;
+                break;
+
+        case sSftpLogLevel:
+                intptr = (int *) &options->sftp_log_level;
+                arg = strdelim(&cp);
+                value = log_level_number(arg);
+                if (value == SYSLOG_LEVEL_NOT_SET)
+                        fatal("%.200s line %d: unsupported log level '%s'",
+                            filename, linenum, arg ? arg : "<NONE>");
+                if (*intptr == -1)
+                        *intptr = (LogLevel) value;
+                break;
+
+        case sSftpUmask:
+                arg = strdelim(&cp);
+		umaskptr = arg;
+                while (*arg && *arg >= '0' && *arg <= '9')
+                    umaskvalue = umaskvalue * 8 + *arg++ - '0';
+                if (*arg || umaskvalue > 0777)
+                    fatal("%s line %d: bad value for umask",
+			    filename, linenum);
+		else {
+			while (*umaskptr && *umaskptr == '0')
+					*umaskptr++;
+			strncpy(options->sftp_umask, umaskptr,
+				SFTP_UMASK_LENGTH);
+		}
+
+                break;
+
+        case sSftpPermitChmod:
+                intptr = &options->sftp_permit_chmod;
+                goto parse_flag;
+
+        case sSftpPermitChown:
+                intptr = &options->sftp_permit_chown;
+                goto parse_flag;
+
+
 	/*
 	 * These options can contain %X options expanded at
 	 * connect time, so that you can specify paths like:
Index: servconf.h
--- servconf.h.orig	2009-01-28 06:31:23 +0100
+++ servconf.h	2009-10-02 13:12:31 +0200
@@ -34,6 +34,19 @@
 #define	PERMIT_NO_PASSWD	2
 #define	PERMIT_YES		3
 
+/* sftp-server logging */
+#define LOG_SFTP_NOT_SET	-1
+#define LOG_SFTP_NO		0
+#define LOG_SFTP_YES		1
+
+/* sftp-server umask control */
+#define SFTP_UMASK_LENGTH	5
+
+/* sftp-server client priviledge */
+#define SFTP_PERMIT_NOT_SET	-1
+#define SFTP_PERMIT_NO		0
+#define SFTP_PERMIT_YES		1
+
 #define DEFAULT_AUTH_FAIL_MAX	6	/* Default for MaxAuthTries */
 #define DEFAULT_SESSIONS_MAX	10	/* Default for MaxSessions */
 
@@ -151,6 +164,12 @@
 	int	num_permitted_opens;
 
 	char   *chroot_directory;
+	int	log_sftp;		/* perform sftp-server logging */
+	SyslogFacility sftp_log_facility;    /* Facility for sftp subsystem logging. */
+	LogLevel sftp_log_level;     /* Level for sftp subsystem logging. */
+	char	sftp_umask[SFTP_UMASK_LENGTH];		/* Sftp Umask */
+	int	sftp_permit_chmod;
+	int	sftp_permit_chown;
 }       ServerOptions;
 
 void	 initialize_server_options(ServerOptions *);
Index: session.c
--- session.c.orig	2009-08-20 08:20:50 +0200
+++ session.c	2009-10-02 13:12:31 +0200
@@ -152,6 +152,15 @@
 
 static int is_child = 0;
 
+/* so SFTP_LOG_FACILITY and SFTP_LOG_LEVEL can be passed through the 
+   environment to the sftp-server subsystem. */
+static const char *sysfac_to_int[] = { "0", "1", "2", "3", "4", "5", "6",
+	"7", "8", "9", "10", "11", "-1" };
+static const char *syslevel_to_int[] = { "0", "1", "2", "3", "4", "5", "6",
+	"7", "-1" };
+
+static char *sftpumask;
+
 /* Name and directory of socket for authentication agent forwarding. */
 static char *auth_sock_name = NULL;
 static char *auth_sock_dir = NULL;
@@ -1283,6 +1292,67 @@
 		child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
 		    auth_sock_name);
 
+	/* LOG_SFTP */
+	if (options.log_sftp == -1 )
+		child_set_env(&env, &envsize, "LOG_SFTP", "-1");
+	else if (options.log_sftp == 0)
+		child_set_env(&env, &envsize, "LOG_SFTP", "0");
+	else
+		child_set_env(&env, &envsize, "LOG_SFTP", "1");
+
+	/* SFTP_LOG_FACILITY */
+	if (options.sftp_log_facility < 0)
+		child_set_env(&env, &envsize, "SFTP_LOG_FACILITY",
+			"-1");
+	else
+		child_set_env(&env, &envsize, "SFTP_LOG_FACILITY", 
+			sysfac_to_int[options.sftp_log_facility]);
+
+	/* SFTP_LOG_LEVEL */
+        if (options.sftp_log_level < 0)
+                child_set_env(&env, &envsize, "SFTP_LOG_LEVEL",
+                        "-1");
+        else
+                child_set_env(&env, &envsize, "SFTP_LOG_LEVEL",
+                        syslevel_to_int[options.sftp_log_level]);
+
+	/* SFTP_UMASK */
+
+	if (options.sftp_umask[0] == '\0')
+		child_set_env(&env, &envsize, "SFTP_UMASK", 
+			"" );
+	else {
+		if (!(sftpumask = calloc(SFTP_UMASK_LENGTH,1))) {
+
+logit("session.c: unabled to allocate memory for SftpUmask. SftpUmask control \
+will be turned off.");
+
+		child_set_env(&env, &envsize, "SFTP_UMASK", 
+			"" );
+		} else {
+			strncpy(sftpumask, options.sftp_umask,
+				SFTP_UMASK_LENGTH);
+			child_set_env(&env, &envsize, "SFTP_UMASK", 
+				sftpumask );
+		}
+	}
+
+        /* SFTP_PERMIT_CHMOD */
+        if (options.sftp_permit_chmod == -1 )
+                child_set_env(&env, &envsize, "SFTP_PERMIT_CHMOD", "-1");
+        else if (options.sftp_permit_chmod == 0)
+                child_set_env(&env, &envsize, "SFTP_PERMIT_CHMOD", "0");
+        else
+                child_set_env(&env, &envsize, "SFTP_PERMIT_CHMOD", "1");
+
+        /* SFTP_PERMIT_CHOWN */
+        if (options.sftp_permit_chown == -1 )
+                child_set_env(&env, &envsize, "SFTP_PERMIT_CHOWN", "-1");
+        else if (options.sftp_permit_chown == 0)
+                child_set_env(&env, &envsize, "SFTP_PERMIT_CHOWN", "0");
+        else
+                child_set_env(&env, &envsize, "SFTP_PERMIT_CHOWN", "1");
+
 	/* read $HOME/.ssh/environment. */
 	if (options.permit_user_env && !options.use_login) {
 		snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Index: sftp-server.8
--- sftp-server.8.orig	2009-06-21 09:52:28 +0200
+++ sftp-server.8	2009-10-02 13:12:31 +0200
@@ -49,6 +49,20 @@
 .Cm Subsystem
 declaration.
 See
+.Xr sshd 8
+for more information. Sftp-server transactions may be logged
+using the
+.Cm LogSftp , 
+.Cm SftpLogFacility ,
+and
+.Cm SftpLogLevel
+options. The administrator may exert control over the file and directory
+permission and ownership, with
+.Cm SftpUmask ,
+.Cm SftpPermitChmod ,
+and
+.Cm SftpPermitChown
+. See
 .Xr sshd_config 5
 for more information.
 .Pp
Index: sftp-server.c
--- sftp-server.c.orig	2009-08-28 02:43:13 +0200
+++ sftp-server.c	2009-10-02 13:12:31 +0200
@@ -59,6 +59,12 @@
 /* Our verbosity */
 LogLevel log_level = SYSLOG_LEVEL_ERROR;
 
+/* SFTP_UMASK */
+static mode_t setumask = 0;
+static int permit_chmod = 1;
+static int permit_chown = 1;
+static int permit_logging = 0;
+
 /* Our client */
 struct passwd *pw = NULL;
 char *client_addr = NULL;
@@ -551,6 +557,12 @@
 	a = get_attrib();
 	flags = flags_from_portable(pflags);
 	mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
+	if (setumask != 0) {
+		if (permit_logging == 0)
+			logit("setting file creation mode to 0666 and umask to %o", setumask);
+		mode = 0666;
+		umask(setumask);
+	}
 	logit("open \"%s\" flags %s mode 0%o",
 	    name, string_from_portable(pflags), mode);
 	fd = open(name, flags, mode);
@@ -565,6 +577,8 @@
 			status = SSH2_FX_OK;
 		}
 	}
+	if ( permit_logging == 1 )
+	logit("open %s", name);
 	if (status != SSH2_FX_OK)
 		send_status(id, status);
 	xfree(name);
@@ -622,6 +636,8 @@
 			}
 		}
 	}
+	if ( permit_logging == 1 )
+	logit("reading file");
 	if (status != SSH2_FX_OK)
 		send_status(id, status);
 }
@@ -661,6 +677,8 @@
 			}
 		}
 	}
+	if (permit_logging == 1)
+		logit("writing file");
 	send_status(id, status);
 	xfree(data);
 }
@@ -762,10 +780,19 @@
 			status = errno_to_portable(errno);
 	}
 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
+		if (permit_chmod == 1) {
 		logit("set \"%s\" mode %04o", name, a->perm);
 		ret = chmod(name, a->perm & 07777);
 		if (ret == -1)
 			status = errno_to_portable(errno);
+		else
+			if (permit_logging == 1)
+				logit("chmod'ed %s", name);
+		} else {
+			status = SSH2_FX_PERMISSION_DENIED;
+			if (permit_logging == 1)
+				logit("chmod %s: operation prohibited by sftp-server configuration.", name);
+		}
 	}
 	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
 		char buf[64];
@@ -779,11 +806,20 @@
 			status = errno_to_portable(errno);
 	}
 	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
+		if (permit_chown == 1) {
 		logit("set \"%s\" owner %lu group %lu", name,
 		    (u_long)a->uid, (u_long)a->gid);
 		ret = chown(name, a->uid, a->gid);
 		if (ret == -1)
 			status = errno_to_portable(errno);
+		else
+			if (permit_logging == 1)
+				logit("chown'ed %s.", name);
+		} else {
+			status = SSH2_FX_PERMISSION_DENIED;
+			if (permit_logging == 1)
+				logit("chown %s: operation prohibited by sftp-server configuration.", name);
+		}
 	}
 	send_status(id, status);
 	xfree(name);
@@ -797,6 +833,9 @@
 	int handle, fd, ret;
 	int status = SSH2_FX_OK;
 
+if ( permit_logging == 1 )
+logit("process_fsetstat");
+
 	id = get_int();
 	handle = get_handle();
 	a = get_attrib();
@@ -815,6 +854,7 @@
 				status = errno_to_portable(errno);
 		}
 		if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
+			if (permit_chmod == 1) {
 			logit("set \"%s\" mode %04o", name, a->perm);
 #ifdef HAVE_FCHMOD
 			ret = fchmod(fd, a->perm & 07777);
@@ -823,6 +863,14 @@
 #endif
 			if (ret == -1)
 				status = errno_to_portable(errno);
+			else
+				if (permit_logging == 1)
+					logit("chmod: succeeded.");
+			} else {
+	                        status = SSH2_FX_PERMISSION_DENIED;
+				if (permit_logging == 1)
+					logit("chmod: operation prohibited by sftp-server configuration.");
+			}
 		}
 		if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
 			char buf[64];
@@ -840,6 +888,7 @@
 				status = errno_to_portable(errno);
 		}
 		if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
+			if (permit_chown == 1) {
 			logit("set \"%s\" owner %lu group %lu", name,
 			    (u_long)a->uid, (u_long)a->gid);
 #ifdef HAVE_FCHOWN
@@ -849,6 +898,14 @@
 #endif
 			if (ret == -1)
 				status = errno_to_portable(errno);
+			else
+				if (permit_logging == 1)
+					logit("chown: succeeded");
+			} else {
+				status = SSH2_FX_PERMISSION_DENIED;
+				if (permit_logging == 1)
+					logit("chown: operation prohibited by sftp-server configuration.");
+			}
 		}
 	}
 	send_status(id, status);
@@ -879,6 +936,8 @@
 		}
 
 	}
+	if ( permit_logging == 1 )
+	logit("opendir %s", path);
 	if (status != SSH2_FX_OK)
 		send_status(id, status);
 	xfree(path);
@@ -954,6 +1013,8 @@
 	logit("remove name \"%s\"", name);
 	ret = unlink(name);
 	status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
+	if ( permit_logging == 1 )
+	logit("remove file %s", name);
 	send_status(id, status);
 	xfree(name);
 }
@@ -971,6 +1032,12 @@
 	a = get_attrib();
 	mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
 	    a->perm & 07777 : 0777;
+        if (setumask != 0) {
+		if (permit_logging == 1)
+                	logit("setting directory creation mode to 0777 and umask to %o.", setumask);
+                mode = 0777;
+                umask(setumask);
+        }
 	debug3("request %u: mkdir", id);
 	logit("mkdir name \"%s\" mode 0%o", name, mode);
 	ret = mkdir(name, mode);
@@ -992,6 +1059,8 @@
 	logit("rmdir name \"%s\"", name);
 	ret = rmdir(name);
 	status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
+	if ( permit_logging == 1 )
+	logit("rmdir %s", name);
 	send_status(id, status);
 	xfree(name);
 }
@@ -1019,6 +1088,8 @@
 		s.name = s.long_name = resolvedname;
 		send_names(id, 1, &s);
 	}
+	if ( permit_logging == 1 )
+	logit("realpath %s", path);
 	xfree(path);
 }
 
@@ -1078,6 +1149,8 @@
 			status = SSH2_FX_OK;
 	}
 	send_status(id, status);
+	if ( permit_logging == 1 )
+	logit("rename old %s new %s", oldpath, newpath);
 	xfree(oldpath);
 	xfree(newpath);
 }
@@ -1104,6 +1177,8 @@
 		s.name = s.long_name = buf;
 		send_names(id, 1, &s);
 	}
+	if ( permit_logging == 1 )
+	logit("readlink %s", path);
 	xfree(path);
 }
 
@@ -1123,6 +1198,8 @@
 	ret = symlink(oldpath, newpath);
 	status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
 	send_status(id, status);
+	if ( permit_logging == 1 )
+	logit("symlink old %s new %s", oldpath, newpath);
 	xfree(oldpath);
 	xfree(newpath);
 }
@@ -1334,6 +1411,8 @@
 	ssize_t len, olen, set_size;
 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
 	char *cp, buf[4*4096];
+ 	unsigned int val = 0;
+ 	char *umask_env;
 
 	extern char *optarg;
 	extern char *__progname;
@@ -1369,6 +1448,12 @@
 		}
 	}
 
+	if (atoi(getenv("LOG_SFTP")) == 1) {
+		permit_logging = 1;
+		log_init("sftp-server", atoi(getenv("SFTP_LOG_LEVEL")),
+			atoi(getenv("SFTP_LOG_FACILITY")), 0);
+	}
+        else
 	log_init(__progname, log_level, log_facility, log_stderr);
 
 	if ((cp = getenv("SSH_CONNECTION")) != NULL) {
@@ -1390,6 +1475,39 @@
 	in = dup(STDIN_FILENO);
 	out = dup(STDOUT_FILENO);
 
+	if ( permit_logging == 1 )
+	logit("Starting sftp-server logging for user %s.", getenv("USER"));
+
+	/* Umask control */
+
+	umask_env = getenv("SFTP_UMASK");
+	while (*umask_env && *umask_env >= '0' && *umask_env <= '9')
+		val = val * 8 + *umask_env++ - '0';
+
+	if (*umask_env || val > 0777 || val == 0) {
+		if ( permit_logging == 1 )
+		logit("bad value %o for SFTP_UMASK, turning umask control off.", val);
+		setumask = 0;
+	} else {
+		if ( permit_logging == 1 )
+		logit("umask control is on.");
+		setumask = val;
+	};
+
+
+	/* Sensitive client commands */
+	
+        if (atoi(getenv("SFTP_PERMIT_CHMOD")) != 1) {
+		permit_chmod = 0;
+		if ( permit_logging == 1 )
+                logit("client is not permitted to chmod.");
+	};
+        if (atoi(getenv("SFTP_PERMIT_CHOWN")) != 1) {
+		permit_chown = 0;
+		if ( permit_logging == 1 )
+                logit("client is not permitted to chown.");
+	};
+
 #ifdef HAVE_CYGWIN
 	setmode(in, O_BINARY);
 	setmode(out, O_BINARY);
Index: sshd_config.5
--- sshd_config.5.orig	2009-08-28 02:27:08 +0200
+++ sshd_config.5	2009-10-02 13:12:31 +0200
@@ -544,6 +544,10 @@
 DEBUG and DEBUG1 are equivalent.
 DEBUG2 and DEBUG3 each specify higher levels of debugging output.
 Logging with a DEBUG level violates the privacy of users and is not recommended.
+.It Cm LogSftp
+Specifies whether to perform logging of
+.Nm sftp-server
+subsystem transactions. Must be "yes" or "no." The default value is "no."
 .It Cm MACs
 Specifies the available MAC (message authentication code) algorithms.
 The MAC algorithm is used in protocol version 2
@@ -819,6 +823,37 @@
 .It Cm ServerKeyBits
 Defines the number of bits in the ephemeral protocol version 1 server key.
 The minimum value is 512, and the default is 1024.
+.It Cm SftpLogFacility
+Gives the facility code that is used when logging
+.Nm sftp-server .
+transactions. The possible values are: DAEMON, USER, AUTH, LOCAL0,
+LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7.
+The default is AUTH.
+.It Cm SftpLogLevel
+Gives the verbosity level that is used when logging messages from
+.Nm sftp-server .
+The possible values are:
+QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG, DEBUG1, DEBUG2 and DEBUG3.
+The default is INFO.  DEBUG and DEBUG1 are equivalent.  DEBUG2
+and DEBUG3 each specify higher levels of debugging output.
+Logging with a DEBUG level violates the privacy of users
+and is not recommended.
+.It Cm SftpPermitChmod
+Specifies whether the sftp-server allows the sftp client to execute chmod 
+commands on the server. The default is yes.
+.It Cm SftpPermitChown
+Specifies whether the sftp-server allows the sftp client to execute chown
+or chgrp commands on the server. Turning this value on means that the client
+is allowed to execute both chown and chgrp commands. Turning it off means that
+the client is prohibited from executing either chown or chgrp.
+ The default is yes.
+.It Cm SftpUmask
+Specifies an optional umask for 
+.Nm sftp-server
+subsystem transactions. If a umask is given, this umask will override all system, 
+environment or sftp client permission modes. If
+no umask or an invalid umask is given, file creation mode defaults to the permission
+mode specified by the sftp client. The default is for no umask.
 .It Cm StrictModes
 Specifies whether
 .Xr sshd 8
Index: sshd_config
--- sshd_config.orig	2008-07-02 14:35:43 +0200
+++ sshd_config	2009-10-02 13:12:31 +0200
@@ -112,6 +112,17 @@
 # override default of no subsystems
 Subsystem	sftp	/usr/libexec/sftp-server
 
+# sftp-server logging
+#LogSftp no
+#SftpLogFacility AUTH
+#SftpLogLevel INFO
+
+# sftp-server umask control
+#SftpUmask
+
+#SftpPermitChmod yes
+#SftpPermitChown yes
+
 # Example of overriding settings on a per-user basis
 #Match User anoncvs
 #	X11Forwarding no
