Subject: Re: bin/19377 new patch
To: None <tech-userlevel@netbsd.org>
From: Emmanuel Dreyfus <manu@netbsd.org>
List: tech-userlevel
Date: 12/23/2002 17:48:50
--9zSXsLTf0vkW971A
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Here is a new patch incoporating everything that has been suggested so far.
Is it okay to commit that?

-- 
Emmanuel Dreyfus
manu@netbsd.org

--9zSXsLTf0vkW971A
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="ftpd.diff"

Index: cmds.c
===================================================================
RCS file: /cvsroot/src/libexec/ftpd/cmds.c,v
retrieving revision 1.16.2.1
diff -U4 -r1.16.2.1 cmds.c
--- cmds.c	2002/11/01 08:23:39	1.16.2.1
+++ cmds.c	2002/12/23 16:41:02
@@ -170,18 +170,22 @@
 };
 
 #define FACTTABSIZE	(sizeof(facttab) / sizeof(struct ftpfact))
 
+static char cached_path[MAXPATHLEN + 1] = "/";
+static void discover_path(char *, const char *);
 
 void
 cwd(const char *path)
 {
-
 	if (chdir(path) < 0)
 		perror_reply(550, path);
 	else {
 		show_chdir_messages(250);
 		ack("CWD");
+		if (getcwd(cached_path, MAXPATHLEN) == NULL) {
+			discover_path(cached_path, path);
+		}
 	}
 }
 
 void
@@ -403,13 +407,17 @@
 pwd(void)
 {
 	char path[MAXPATHLEN];
 
-	if (getcwd(path, sizeof(path) - 1) == NULL)
-		reply(550, "Can't get the current directory: %s.",
-		    strerror(errno));
-	else
-		replydirname(path, "is the current directory.");
+	if (getcwd(path, sizeof(path) - 1) == NULL) {
+		if (chdir(cached_path) < 0) {
+			reply(550, "Can't get the current directory: %s.",
+			    strerror(errno));
+			return;
+		}
+		(void)strlcpy(path, cached_path, MAXPATHLEN);
+	}
+	replydirname(path, "is the current directory.");
 }
 
 void
 removedir(const char *name)
@@ -843,4 +851,113 @@
 	}
 	*p = '\0';
 	reply(257, "\"%s\" %s", npath, message);
 }
+
+static void
+discover_path(last_path, new_path) 
+	char *last_path;
+	const char *new_path;
+{
+	char tp[MAXPATHLEN + 1] = "";
+	char tq[MAXPATHLEN + 1] = "";
+	char *cp;
+	char *cq; 
+	int sz1, sz2;
+	struct stat st1, st2;
+	
+	if (new_path[0] != '/') {
+		(void)strlcpy(tp, last_path, MAXPATHLEN);
+		(void)strlcat(tp, "/", MAXPATHLEN);
+	}
+	(void)strlcat(tp, new_path, MAXPATHLEN);
+	(void)strlcat(tp, "/", MAXPATHLEN);
+
+	/* Collapse any // into / */
+	while ((cp = strstr(tp, "//")) != NULL)
+		(void)memmove(cp, cp + 1, strlen(cp) - 1 + 1);
+
+	/* Collapse any /./ into / */
+	while ((cp = strstr(tp, "/./")) != NULL)
+		(void)memmove(cp, cp + 2, strlen(cp) - 2 + 1);
+
+	/* resolve symlinks */
+	cp = tp;
+	while ((cp = strstr(++cp, "/")) != NULL) {
+		sz1 = (u_long)cp - (u_long)tp;
+		if (sz1 > MAXPATHLEN)
+			goto bad;
+		*cp = 0;
+		sz2 = readlink(tp, tq, MAXPATHLEN); 
+		*cp = '/';
+		if (sz2 <= 0)
+			continue;
+
+		/* Null terminate the string */
+		tq[sz2] = 0;
+
+		/* The symlink might introduce some /./ or //, remove them */
+		/* Collapse any // into / */
+		while ((cq = strstr(tq, "//")) != NULL)
+			(void)memmove(cq, cq + 1, strlen(cq) - 1 + 1);
+
+		/* Collapse any /./ into / */
+		while ((cq = strstr(tq, "//")) != NULL)
+			(void)memmove(cq, cq + 2, strlen(cq) - 2 + 1);
+
+		sz2 = strlen(tq);
+		if (tq[sz2 - 1] == '/') 
+			tq[--sz2] = 0;
+
+		(void)memmove(cp, cp + 2, strlen(cp) - 2 + 1);
+
+		/* Absolute link? */
+		if (tq[0] == '/') {
+			if (strlen(cp) + sz2 > MAXPATHLEN)
+				goto bad;
+			memmove(tp + sz2, cp, strlen(cp) + 1);
+			memcpy(tp, tq, sz2);
+			cp = tp;
+			continue;
+		}			
+
+		/* Relative link */
+		for (cq = cp - 1; *cq != '/'; cq--);
+		if (strlen(tp) - ((u_long)cq - (u_long)cp)
+		    + 1 + sz2 > MAXPATHLEN)
+			goto bad;
+		(void)memmove(cq + 1 + sz2, cp, strlen(cp) + 1);
+		(void)memcpy(cq + 1, tq, sz2);
+	}
+
+	/* Collapse any /foo/../ into /foo/ */
+	while ((cp = strstr(tp, "/../")) != NULL) {
+		/* ^/../foo/ becomes ^/foo/ */
+		if (cp == tp) {
+			(void)memmove(cp, cp + 3,
+			    strlen(cp) - 3 + 1);
+		} else {
+			for (cq = cp - 1; *cq != '/'; cq--);
+			(void)memmove(cq, cp + 3,
+			    strlen(cp) - 3 + 1);
+		}
+	}
+
+	/* strip strailing / */
+	if (strlen(tp) != 1)
+		tp[strlen(tp) - 1] = '\0';
+
+	/* check that the path is correct */
+	stat(tp, &st1);
+	stat(".", &st2);
+	if ((st1.st_dev != st2.st_dev) || (st1.st_ino != st2.st_ino))
+		goto bad;
+
+	(void)strlcpy(last_path, tp, MAXPATHLEN);
+	return;
+
+bad:
+	(void)strlcat(last_path, "/", MAXPATHLEN);
+	(void)strlcat(last_path, new_path, MAXPATHLEN);
+	return;
+}
+

--9zSXsLTf0vkW971A--