jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone

Blame SOURCES/openssh-6.7p1-sftp-force-permission.patch

3a2fd7
diff --color -ru a/sftp-server.8 b/sftp-server.8
3a2fd7
--- a/sftp-server.8	2019-04-18 00:52:57.000000000 +0200
3a2fd7
+++ b/sftp-server.8	2022-06-20 16:03:47.892540068 +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
3a2fd7
@@ -138,6 +139,12 @@
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:
3a2fd7
+777, 755, 750, 666, 644, 640, etc.  Using both -m and -u switches makes the
3a2fd7
+umask (-u) effective only for newly created directories and explicit mode (-m)
3a2fd7
+for newly created files.
f5835d
 .El
f5835d
 .Pp
f5835d
 On some systems,
3a2fd7
diff --color -ru a/sftp-server.c b/sftp-server.c
3a2fd7
--- a/sftp-server.c	2022-06-20 16:01:26.183793633 +0200
3a2fd7
+++ b/sftp-server.c	2022-06-20 16:02:12.442690608 +0200
3a2fd7
@@ -65,6 +65,10 @@
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
 
3a2fd7
@@ -683,6 +687,7 @@
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 */
3a2fd7
@@ -692,6 +697,10 @@
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 &&
3a2fd7
@@ -713,6 +722,8 @@
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);
3a2fd7
@@ -1555,7 +1566,7 @@
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);
3a2fd7
@@ -1581,7 +1592,7 @@
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) {
3a2fd7
@@ -1643,6 +1654,15 @@
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();