kentpeacock / rpms / openssh

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