kentpeacock / rpms / openssh

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