6c1f4d
diff --color -ru a/sftp-server.8 b/sftp-server.8
6c1f4d
--- a/sftp-server.8	2019-04-18 00:52:57.000000000 +0200
6c1f4d
+++ b/sftp-server.8	2022-06-20 16:03:47.892540068 +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
6c1f4d
@@ -138,6 +139,12 @@
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:
6c1f4d
+777, 755, 750, 666, 644, 640, etc.  Using both -m and -u switches makes the
6c1f4d
+umask (-u) effective only for newly created directories and explicit mode (-m)
6c1f4d
+for newly created files.
aedd00
 .El
aedd00
 .Pp
aedd00
 On some systems,
6c1f4d
diff --color -ru a/sftp-server.c b/sftp-server.c
6c1f4d
--- a/sftp-server.c	2022-06-20 16:01:26.183793633 +0200
6c1f4d
+++ b/sftp-server.c	2022-06-20 16:02:12.442690608 +0200
6c1f4d
@@ -65,6 +65,10 @@
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
 
6c1f4d
@@ -683,6 +687,7 @@
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 */
6c1f4d
@@ -692,6 +697,10 @@
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 &&
6c1f4d
@@ -713,6 +722,8 @@
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);
6c1f4d
@@ -1555,7 +1566,7 @@
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);
6c1f4d
@@ -1581,7 +1592,7 @@
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) {
6c1f4d
@@ -1643,6 +1654,15 @@
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();