Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/ftp Improve method used in fileindir() to determine ...



details:   https://anonhg.NetBSD.org/src/rev/6338de496cc0
branches:  trunk
changeset: 581122:6338de496cc0
user:      lukem <lukem%NetBSD.org@localhost>
date:      Thu May 26 02:59:34 2005 +0000

description:
Improve method used in fileindir() to determine if `file' is in or under `dir':
realpath(3) on non-NetBSD systems may fail if the target filename doesn't
exist, so instead use realpath(3) on the parent directory of `file'.
Per discussion with Todd Eigenschink.

diffstat:

 usr.bin/ftp/util.c    |  27 +++++++++++++++++++--------
 usr.bin/ftp/version.h |   4 ++--
 2 files changed, 21 insertions(+), 10 deletions(-)

diffs (79 lines):

diff -r 05b5f4e23a21 -r 6338de496cc0 usr.bin/ftp/util.c
--- a/usr.bin/ftp/util.c        Wed May 25 20:58:00 2005 +0000
+++ b/usr.bin/ftp/util.c        Thu May 26 02:59:34 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.c,v 1.126 2005/05/19 03:14:52 lukem Exp $ */
+/*     $NetBSD: util.c,v 1.127 2005/05/26 02:59:34 lukem Exp $ */
 
 /*-
  * Copyright (c) 1997-2005 The NetBSD Foundation, Inc.
@@ -71,7 +71,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: util.c,v 1.126 2005/05/19 03:14:52 lukem Exp $");
+__RCSID("$NetBSD: util.c,v 1.127 2005/05/26 02:59:34 lukem Exp $");
 #endif /* not lint */
 
 /*
@@ -90,6 +90,7 @@
 #include <fcntl.h>
 #include <glob.h>
 #include <signal.h>
+#include <libgen.h>
 #include <limits.h>
 #include <netdb.h>
 #include <stdio.h>
@@ -835,20 +836,30 @@
 int
 fileindir(const char *file, const char *dir)
 {
-       char    realfile[PATH_MAX+1];
+       char    parentdirbuf[PATH_MAX+1], *parentdir;
+       char    realdir[PATH_MAX+1];
        size_t  dirlen;
 
-       if (realpath(file, realfile) == NULL) {
-               warn("Unable to determine real path of `%s'", file);
+                                       /* determine parent directory of file */
+       (void)strlcpy(parentdirbuf, file, sizeof(parentdirbuf));
+       parentdir = dirname(parentdirbuf);
+       if (strcmp(parentdir, ".") == 0)
+               return 1;               /* current directory is ok */
+
+                                       /* find the directory */
+       if (realpath(parentdir, realdir) == NULL) {
+               warn("Unable to determine real path of `%s'", parentdir);
                return 0;
        }
-       if (realfile[0] != '/')         /* relative result */
+       if (realdir[0] != '/')          /* relative result is ok */
                return 1;
        dirlen = strlen(dir);
 #if 0
-printf("file %s realfile %s dir %s [%d]\n", file, realfile, dir, dirlen);
+printf("file %s parent %s realdir %s dir %s [%d]\n",
+    file, parentdir, realdir, dir, dirlen);
 #endif
-       if (strncmp(realfile, dir, dirlen) == 0 && realfile[dirlen] == '/')
+       if (strncmp(realdir, dir, dirlen) == 0 &&
+           (realdir[dirlen] == '/' || realdir[dirlen] == '\0'))
                return 1;
        return 0;
 }
diff -r 05b5f4e23a21 -r 6338de496cc0 usr.bin/ftp/version.h
--- a/usr.bin/ftp/version.h     Wed May 25 20:58:00 2005 +0000
+++ b/usr.bin/ftp/version.h     Thu May 26 02:59:34 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: version.h,v 1.50 2005/05/14 15:26:43 lukem Exp $       */
+/*     $NetBSD: version.h,v 1.51 2005/05/26 02:59:34 lukem Exp $       */
 /*-
  * Copyright (c) 1999-2005 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -40,5 +40,5 @@
 #endif
 
 #ifndef FTP_VERSION
-#define        FTP_VERSION     "20050514"
+#define        FTP_VERSION     "20050526"
 #endif



Home | Main Index | Thread Index | Old Index