rcolebaugh / rpms / openssh

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