rcolebaugh / rpms / openssh

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