kentpeacock / rpms / openssh

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