kentpeacock / rpms / openssh

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