]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/directory.c
previous commit 'lchdir: use chdir + getcwd' was only testing for symlink outside...
[netatalk.git] / etc / afpd / directory.c
index 438cc43f63ed707866a0dbb92a5228db42508765..8e41298f48c9df474cc48009b3812df2501e30a2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: directory.c,v 1.134 2010-02-19 10:51:59 franklahm Exp $
+ * $Id: directory.c,v 1.137 2010-02-28 22:29:15 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -133,76 +133,6 @@ static struct dir rootpar  = { SENTINEL, SENTINEL, NULL,
  * frPutAway:   4    home directory ID
  */
 
-/*!
- * @brief symlink safe chdir replacement
- *
- * Only chdirs to dir if it doesn't contain symlinks.
- *
- * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
- */
-static int lchdir(const char *dir)
-{
-    int ret = 0;
-    char buf[MAXPATHLEN+1];
-#ifdef REALPATH_TAKES_NULL
-    char *rpath = NULL;
-#else
-    char rpath[MAXPATHLEN+1];
-#endif
-
-    /* dir might be an relative or an absolute path */
-    if (dir[0] == '/') {
-        /* absolute path, just make sure buf is prepared for strlcat */
-        buf[0] = 0;
-    } else {
-        /* relative path, push cwd int buf */
-        if (getcwd(buf, MAXPATHLEN) == NULL)
-            return -1;
-        if (strlcat(buf, "/", MAXPATHLEN) >= MAXPATHLEN)
-            return -1;
-    }
-
-    if (strlcat(buf, dir, MAXPATHLEN) >= MAXPATHLEN)
-        return -1;
-
-#ifdef REALPATH_TAKES_NULL
-    if ((rpath = realpath(dir, NULL)) == NULL) {
-#else
-    if (realpath(dir, rpath) == NULL) {
-#endif
-        ret = -1;
-        goto exit;
-    }
-
-    /* 
-     * Cases:
-     * chdir request   | realpath result | ret
-     * (after getwcwd) |                 |
-     * =======================================
-     * /a/b/.          | /a/b            | 0
-     * /a/b/.          | /c              | 1
-     * /a/b/.          | /c/d/e/f        | 1
-     */
-    ret = 0;
-    for (int i = 0; rpath[i]; i++) {
-        if (buf[i] != rpath[i]) {
-            ret = 1;
-            goto exit;
-        }
-    }
-
-    if (chdir(dir) != 0) {
-        ret = -1;
-        goto exit;
-    }
-
-exit:
-#ifdef REALPATH_TAKES_NULL
-    free(rpath);
-#endif
-    return ret;
-}
-
 static struct dir *
 vol_tree_root(const struct vol *vol, u_int32_t did)
 {
@@ -1623,8 +1553,7 @@ int movecwd(struct vol *vol, struct dir *dir)
     }
 
     p = path + sizeof(path) - 1;
-    *p-- = '\0';
-    *p = '.';
+    *p = '\0';
     for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
         u = d->d_u_name;
         if (!u) {
@@ -1651,12 +1580,17 @@ int movecwd(struct vol *vol, struct dir *dir)
         p -= n;
         memcpy( p, vol->v_path, n );
     }
-    if ( (ret = lchdir( p )) != 0 ) {
+    if ( (ret = lchdir(p )) != 0 ) {
         LOG(log_debug, logtype_afpd, "movecwd('%s'): ret:%d, %u/%s", p, ret, errno, strerror(errno));
 
         if (ret == 1) {
-            /* p is a symlink */
+            /* p is a symlink or getcwd failed */
             afp_errno = AFPERR_BADTYPE;
+            vol->v_curdir = curdir = vol->v_dir;
+            if (chdir(vol->v_path ) < 0) {
+                LOG(log_debug, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
+                /* XXX what do we do here? */
+            }
             return -1;
         }
         switch (errno) {