rcolebaugh / rpms / openssh

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