jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone

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

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