b58e57
diff -up openssh-7.4p1/sftp-server.8.sftp-force-mode openssh-7.4p1/sftp-server.8
b58e57
--- openssh-7.4p1/sftp-server.8.sftp-force-mode	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/sftp-server.8	2017-02-09 10:35:41.926475399 +0100
b58e57
@@ -38,6 +38,7 @@
b58e57
 .Op Fl P Ar blacklisted_requests
b58e57
 .Op Fl p Ar whitelisted_requests
b58e57
 .Op Fl u Ar umask
b58e57
+.Op Fl m Ar force_file_perms
b58e57
 .Ek
b58e57
 .Nm
b58e57
 .Fl Q Ar protocol_feature
b58e57
@@ -138,6 +139,10 @@ Sets an explicit
b58e57
 .Xr umask 2
b58e57
 to be applied to newly-created files and directories, instead of the
b58e57
 user's default mask.
b58e57
+.It Fl m Ar force_file_perms
b58e57
+Sets explicit file permissions to be applied to newly-created files instead
b58e57
+of the default or client requested mode.  Numeric values include:
b58e57
+777, 755, 750, 666, 644, 640, etc.  Option -u is ineffective if -m is set.
b58e57
 .El
b58e57
 .Pp
b58e57
 On some systems,
b58e57
diff -up openssh-7.4p1/sftp-server.c.sftp-force-mode openssh-7.4p1/sftp-server.c
b58e57
--- openssh-7.4p1/sftp-server.c.sftp-force-mode	2017-02-09 10:22:36.498019921 +0100
b58e57
+++ openssh-7.4p1/sftp-server.c	2017-02-09 10:35:07.190520959 +0100
b58e57
@@ -65,6 +65,10 @@ struct sshbuf *oqueue;
b58e57
 /* Version of client */
b58e57
 static u_int version;
b58e57
 
b58e57
+/* Force file permissions */
b58e57
+int permforce = 0;
b58e57
+long permforcemode;
b58e57
+
b58e57
 /* SSH2_FXP_INIT received */
b58e57
 static int init_done;
b58e57
 
b58e57
@@ -679,6 +683,7 @@ process_open(u_int32_t id)
b58e57
 	Attrib a;
b58e57
 	char *name;
b58e57
 	int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE;
b58e57
+	mode_t old_umask = 0;
b58e57
 
b58e57
 	if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
b58e57
 	    (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */
b58e57
@@ -688,6 +693,10 @@ process_open(u_int32_t id)
b58e57
 	debug3("request %u: open flags %d", id, pflags);
b58e57
 	flags = flags_from_portable(pflags);
b58e57
 	mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666;
b58e57
+	if (permforce == 1) {   /* Force perm if -m is set */
b58e57
+		mode = permforcemode;
b58e57
+		old_umask = umask(0); /* so umask does not interfere */
b58e57
+	}
b58e57
 	logit("open \"%s\" flags %s mode 0%o",
b58e57
 	    name, string_from_portable(pflags), mode);
b58e57
 	if (readonly &&
b58e57
@@ -709,6 +718,8 @@ process_open(u_int32_t id)
b58e57
 			}
b58e57
 		}
b58e57
 	}
b58e57
+	if (permforce == 1)
b58e57
+		(void) umask(old_umask); /* restore umask to something sane */
b58e57
 	if (status != SSH2_FX_OK)
b58e57
 		send_status(id, status);
b58e57
 	free(name);
b58e57
@@ -1490,7 +1501,7 @@ sftp_server_usage(void)
b58e57
 	fprintf(stderr,
b58e57
 	    "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
b58e57
 	    "[-l log_level]\n\t[-P blacklisted_requests] "
b58e57
-	    "[-p whitelisted_requests] [-u umask]\n"
b58e57
+	    "[-p whitelisted_requests] [-u umask] [-m force_file_perms]\n"
b58e57
 	    "       %s -Q protocol_feature\n",
b58e57
 	    __progname, __progname);
b58e57
 	exit(1);
b58e57
@@ -1516,7 +1527,7 @@ sftp_server_main(int argc, char **argv,
b58e57
 	pw = pwcopy(user_pw);
b58e57
 
b58e57
 	while (!skipargs && (ch = getopt(argc, argv,
b58e57
-	    "d:f:l:P:p:Q:u:cehR")) != -1) {
b58e57
+	    "d:f:l:P:p:Q:u:m:cehR")) != -1) {
b58e57
 		switch (ch) {
b58e57
 		case 'Q':
b58e57
 			if (strcasecmp(optarg, "requests") != 0) {
b58e57
@@ -1576,6 +1587,15 @@ sftp_server_main(int argc, char **argv,
b58e57
 				fatal("Invalid umask \"%s\"", optarg);
b58e57
 			(void)umask((mode_t)mask);
b58e57
 			break;
b58e57
+		case 'm':
b58e57
+			/* Force permissions on file received via sftp */
b58e57
+			permforce = 1;
b58e57
+			permforcemode = strtol(optarg, &cp, 8);
b58e57
+			if (permforcemode < 0 || permforcemode > 0777 ||
b58e57
+			    *cp != '\0' || (permforcemode == 0 &&
b58e57
+			    errno != 0))
b58e57
+				fatal("Invalid file mode \"%s\"", optarg);
b58e57
+			break;
b58e57
 		case 'h':
b58e57
 		default:
b58e57
 			sftp_server_usage();