rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
Blob Blame History Raw
diff -up openssh-6.6p1/sftp-server.8.sftp-force-mode openssh-6.6p1/sftp-server.8
--- openssh-6.6p1/sftp-server.8.sftp-force-mode	2013-10-15 03:07:05.000000000 +0200
+++ openssh-6.6p1/sftp-server.8	2015-04-20 14:04:47.427562510 +0200
@@ -38,6 +38,7 @@
 .Op Fl P Ar blacklisted_requests
 .Op Fl p Ar whitelisted_requests
 .Op Fl u Ar umask
+.Op Fl m Ar force_file_perms
 .Ek
 .Nm
 .Fl Q Ar protocol_feature
@@ -138,6 +139,10 @@ Sets an explicit
 .Xr umask 2
 to be applied to newly-created files and directories, instead of the
 user's default mask.
+.It Fl m Ar force_file_perms
+Sets explicit file permissions to be applied to newly-created files instead
+of the default or client requested mode.  Numeric values include:
+777, 755, 750, 666, 644, 640, etc.  Option -u is ineffective if -m is set.
 .El
 .Pp
 For logging to work,
diff -up openssh-6.6p1/sftp-server.c.sftp-force-mode openssh-6.6p1/sftp-server.c
--- openssh-6.6p1/sftp-server.c.sftp-force-mode	2015-04-20 14:04:47.420562526 +0200
+++ openssh-6.6p1/sftp-server.c	2015-04-20 14:07:13.799231025 +0200
@@ -71,6 +71,10 @@ static Buffer oqueue;
 /* Version of client */
 static u_int version;
 
+/* Force file permissions */
+int permforce = 0;
+long permforcemode;
+
 /* SSH2_FXP_INIT received */
 static int init_done;
 
@@ -668,6 +672,7 @@ process_open(u_int32_t id)
 	Attrib *a;
 	char *name;
 	int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
+	mode_t old_umask;
 
 	name = get_string(NULL);
 	pflags = get_int();		/* portable flags */
@@ -675,6 +680,10 @@ process_open(u_int32_t id)
 	a = get_attrib();
 	flags = flags_from_portable(pflags);
 	mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
+	if (permforce == 1) {   /* Force perm if -m is set */
+		mode = permforcemode;
+		old_umask = umask(0); /* so umask does not interfere */
+	}
 	logit("open \"%s\" flags %s mode 0%o",
 	    name, string_from_portable(pflags), mode);
 	if (readonly &&
@@ -696,6 +705,8 @@ process_open(u_int32_t id)
 			}
 		}
 	}
+	if (permforce == 1)
+		(void) umask(old_umask); /* restore umask to something sane */
 	if (status != SSH2_FX_OK)
 		send_status(id, status);
 	free(name);
@@ -1430,7 +1441,7 @@ sftp_server_usage(void)
 	fprintf(stderr,
 	    "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
 	    "[-l log_level]\n\t[-P blacklisted_requests] "
-	    "[-p whitelisted_requests] [-u umask]\n"
+	    "[-p whitelisted_requests] [-u umask] [-m force_file_perms]\n"
 	    "       %s -Q protocol_feature\n",
 	    __progname, __progname);
 	exit(1);
@@ -1455,7 +1463,7 @@ sftp_server_main(int argc, char **argv,
 	pw = pwcopy(user_pw);
 
 	while (!skipargs && (ch = getopt(argc, argv,
-	    "d:f:l:P:p:Q:u:cehR")) != -1) {
+	    "d:f:l:P:p:Q:u:m:cehR")) != -1) {
 		switch (ch) {
 		case 'Q':
 			if (strcasecmp(optarg, "requests") != 0) {
@@ -1515,6 +1523,15 @@ sftp_server_main(int argc, char **argv,
 				fatal("Invalid umask \"%s\"", optarg);
 			(void)umask((mode_t)mask);
 			break;
+		case 'm':
+			/* Force permissions on file received via sftp */
+			permforce = 1;
+			permforcemode = strtol(optarg, &cp, 8);
+			if (permforcemode < 0 || permforcemode > 0777 ||
+			    *cp != '\0' || (permforcemode == 0 &&
+			    errno != 0))
+				fatal("Invalid file mode \"%s\"", optarg);
+			break;
 		case 'h':
 		default:
 			sftp_server_usage();