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