Blame SOURCES/openssh-6.7p1-sftp-force-permission.patch

aedd00
diff -up openssh-7.2p2/sftp-server.8.sftp-force-mode openssh-7.2p2/sftp-server.8
aedd00
--- openssh-7.2p2/sftp-server.8.sftp-force-mode	2016-03-09 19:04:48.000000000 +0100
aedd00
+++ openssh-7.2p2/sftp-server.8	2016-06-23 16:18:20.463854117 +0200
aedd00
@@ -38,6 +38,7 @@
aedd00
 .Op Fl P Ar blacklisted_requests
aedd00
 .Op Fl p Ar whitelisted_requests
aedd00
 .Op Fl u Ar umask
aedd00
+.Op Fl m Ar force_file_perms
aedd00
 .Ek
aedd00
 .Nm
aedd00
 .Fl Q Ar protocol_feature
aedd00
@@ -138,6 +139,10 @@ Sets an explicit
aedd00
 .Xr umask 2
aedd00
 to be applied to newly-created files and directories, instead of the
aedd00
 user's default mask.
aedd00
+.It Fl m Ar force_file_perms
aedd00
+Sets explicit file permissions to be applied to newly-created files instead
aedd00
+of the default or client requested mode.  Numeric values include:
aedd00
+777, 755, 750, 666, 644, 640, etc.  Option -u is ineffective if -m is set.
aedd00
 .El
aedd00
 .Pp
aedd00
 On some systems,
aedd00
diff -up openssh-7.2p2/sftp-server.c.sftp-force-mode openssh-7.2p2/sftp-server.c
aedd00
--- openssh-7.2p2/sftp-server.c.sftp-force-mode	2016-06-23 16:18:20.446854128 +0200
aedd00
+++ openssh-7.2p2/sftp-server.c	2016-06-23 16:20:37.950766082 +0200
aedd00
@@ -69,6 +69,10 @@ struct sshbuf *oqueue;
aedd00
 /* Version of client */
aedd00
 static u_int version;
aedd00
 
aedd00
+/* Force file permissions */
aedd00
+int permforce = 0;
aedd00
+long permforcemode;
aedd00
+
aedd00
 /* SSH2_FXP_INIT received */
aedd00
 static int init_done;
aedd00
 
aedd00
@@ -683,6 +687,7 @@ process_open(u_int32_t id)
aedd00
 	Attrib a;
aedd00
 	char *name;
aedd00
 	int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE;
aedd00
+	mode_t old_umask = 0;
aedd00
 
aedd00
 	if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
aedd00
 	    (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */
aedd00
@@ -692,6 +697,10 @@ process_open(u_int32_t id)
aedd00
 	debug3("request %u: open flags %d", id, pflags);
aedd00
 	flags = flags_from_portable(pflags);
aedd00
 	mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666;
aedd00
+	if (permforce == 1) {   /* Force perm if -m is set */
aedd00
+		mode = permforcemode;
aedd00
+		old_umask = umask(0); /* so umask does not interfere */
aedd00
+	}	
aedd00
 	logit("open \"%s\" flags %s mode 0%o",
aedd00
 	    name, string_from_portable(pflags), mode);
aedd00
 	if (readonly &&
aedd00
@@ -713,6 +722,8 @@ process_open(u_int32_t id)
aedd00
 			}
aedd00
 		}
aedd00
 	}
aedd00
+	if (permforce == 1)
aedd00
+		(void) umask(old_umask); /* restore umask to something sane */
aedd00
 	if (status != SSH2_FX_OK)
aedd00
 		send_status(id, status);
aedd00
 	free(name);
aedd00
@@ -1494,7 +1505,7 @@ sftp_server_usage(void)
aedd00
 	fprintf(stderr,
aedd00
 	    "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
aedd00
 	    "[-l log_level]\n\t[-P blacklisted_requests] "
aedd00
-	    "[-p whitelisted_requests] [-u umask]\n"
aedd00
+	    "[-p whitelisted_requests] [-u umask] [-m force_file_perms]\n"
aedd00
 	    "       %s -Q protocol_feature\n",
aedd00
 	    __progname, __progname);
aedd00
 	exit(1);
aedd00
@@ -1520,7 +1531,7 @@ sftp_server_main(int argc, char **argv,
aedd00
 	pw = pwcopy(user_pw);
aedd00
 
aedd00
 	while (!skipargs && (ch = getopt(argc, argv,
aedd00
-	    "d:f:l:P:p:Q:u:cehR")) != -1) {
aedd00
+	    "d:f:l:P:p:Q:u:m:cehR")) != -1) {
aedd00
 		switch (ch) {
aedd00
 		case 'Q':
aedd00
 			if (strcasecmp(optarg, "requests") != 0) {
aedd00
@@ -1580,6 +1591,15 @@ sftp_server_main(int argc, char **argv,
aedd00
 				fatal("Invalid umask \"%s\"", optarg);
aedd00
 			(void)umask((mode_t)mask);
aedd00
 			break;
aedd00
+		case 'm':
aedd00
+			/* Force permissions on file received via sftp */
aedd00
+			permforce = 1;
aedd00
+			permforcemode = strtol(optarg, &cp, 8);
aedd00
+			if (permforcemode < 0 || permforcemode > 0777 ||
aedd00
+			    *cp != '\0' || (permforcemode == 0 &&
aedd00
+			    errno != 0))
aedd00
+				fatal("Invalid file mode \"%s\"", optarg);
aedd00
+			break;
aedd00
 		case 'h':
aedd00
 		default:
aedd00
 			sftp_server_usage();