X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=etc%2Fafpd%2Fdirectory.c;h=7c7eae72cd9920a3d1876c016500f25fcf8a2bfb;hb=2c09a52ace844af8c2475b7221b8d69d8a81b53e;hp=a7362e0b8ab8f54eceb46a4191d2854e8d19bfad;hpb=ded40a0b6cdb144ae3fc59cea4cc3378d1541007;p=netatalk.git diff --git a/etc/afpd/directory.c b/etc/afpd/directory.c index a7362e0b..7c7eae72 100644 --- a/etc/afpd/directory.c +++ b/etc/afpd/directory.c @@ -1,5 +1,5 @@ /* - * $Id: directory.c,v 1.120 2009-11-26 18:17:12 franklahm Exp $ + * $Id: directory.c,v 1.133 2010-02-19 01:26:03 didg Exp $ * * Copyright (c) 1990,1993 Regents of The University of Michigan. * All Rights Reserved. See COPYRIGHT. @@ -62,16 +62,59 @@ char *strchr (), *strrchr (); extern void addir_inherit_acl(const struct vol *vol); #endif +/* + * Directory caches + * ================ + * + * There are currently two cache structures where afpd caches directory information + * a) a DID/dirname cache in a hashtable + * b) a (red-black) tree with CNIDs as key + * + * a) is for searching by DID/dirname + * b) is for searching by CNID + * + * Through additional parent, child, previous and next pointers, b) is also used to + * represent the on-disk layout of the filesystem. parent and child point to parent + * and child directory respectively, linking 2 or more subdirectories in one + * directory with previous and next pointers. + * + * Usage examples, highlighting the main functions: + * + * a) is eg used in enumerate(): + * if IS_DIR + * dir = dirsearch_byname() // search in cache + * if (dir == NULL) // not found + * dir = adddir() // add to cache + * getdirparams() + * + * b) is eg used in afp_getfildirparams() + * dirlookup() // wrapper for cache and db search + * => dir = dirsearch() // search in cache + * if (dir) // found + * return + * else // not found, + * cnid_resolve() // resolve with CNID database + * cname() // add to cache + */ + struct dir *curdir; int afp_errno; #define SENTINEL (&sentinel) -static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK, - NULL, NULL, NULL, NULL, NULL, 0, 0, - 0, 0, NULL, NULL, 0, NULL}; -static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0, - NULL, NULL, NULL, NULL, NULL, 0, 0, - 0, 0, NULL, NULL, 0, NULL}; +static struct dir sentinel = { SENTINEL, SENTINEL, NULL, /* left, right, back */ + DIRTREE_COLOR_BLACK, /* color */ + NULL, NULL, /* parent, child */ + NULL, NULL, /* previous, next */ + NULL, 0, 0, /* oforks, did, flags */ + 0, 0, /* ctime, offcnt */ + NULL, NULL, NULL}; /* mname, uname, ucs2-name */ +static struct dir rootpar = { SENTINEL, SENTINEL, NULL, + 0, + NULL, NULL, + NULL, NULL, + NULL, 0, 0, + 0, 0, + NULL, NULL, NULL}; /* (from IM: Toolbox Essentials) * dirFinderInfo (DInfo) fields: @@ -90,6 +133,76 @@ static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0, * 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) { @@ -157,7 +270,6 @@ dirsearch_byname( const struct vol *vol, struct dir *cdir, char *name) key.d_parent = cdir; key.d_u_name = name; - key.d_u_name_len = strlen(name); hn = hash_lookup(vol->v_hash, &key); if (hn) { dir = hnode_get(hn); @@ -776,7 +888,7 @@ static int deletedir(char *dir) break; } strcpy(path + len, de->d_name); - if (stat(path, &st)) { + if (lstat(path, &st)) { continue; } if (S_ISDIR(st.st_mode)) { @@ -842,7 +954,7 @@ static int copydir(const struct vol *vol, char *src, char *dst) } strcpy(spath + slen, de->d_name); - if (stat(spath, &st) == 0) { + if (lstat(spath, &st) == 0) { if (strlen(de->d_name) > drem) { err = AFPERR_PARAM; break; @@ -864,7 +976,7 @@ static int copydir(const struct vol *vol, char *src, char *dst) } /* keep the same time stamp. */ - if (stat(src, &st) == 0) { + if (lstat(src, &st) == 0) { ut.actime = ut.modtime = st.st_mtime; utime(dst, &ut); } @@ -944,13 +1056,24 @@ adddir(struct vol *vol, struct dir *dir, struct path *path) char *upath; struct stat *st; int deleted; + struct adouble ad; + struct adouble *adp = NULL; cnid_t id; upath = path->u_name; st = &path->st; upathlen = strlen(upath); - id = get_id(vol, NULL, st, dir->d_did, upath, upathlen); + /* get_id needs adp for reading CNID from adouble file */ + ad_init(&ad, vol->v_adouble, vol->v_ad_options); + if ((ad_open_metadata(upath, ADFLAGS_DIR, 0, &ad)) == 0) + adp = &ad; + + id = get_id(vol, adp, st, dir->d_did, upath, upathlen); + + if (adp) + ad_close_metadata(adp); + if (id == 0) { return NULL; } @@ -1063,7 +1186,6 @@ struct dir *dirnew(const char *m_name, const char *u_name) return NULL; } - dir->d_u_name_len = strlen(dir->d_u_name); dir->d_m_name_ucs2 = NULL; dir->d_left = dir->d_right = SENTINEL; dir->d_next = dir->d_prev = dir; @@ -1111,7 +1233,7 @@ static hash_val_t hash_fun2_dir(const void *key) { const struct dir *k = key; const char *data = k->d_u_name; - int len = k->d_u_name_len; + int len = strlen(k->d_u_name); hash_val_t hash = k->d_parent->d_did, tmp; int rem = len & 3; @@ -1490,6 +1612,7 @@ int movecwd(struct vol *vol, struct dir *dir) struct dir *d; char *p, *u; int n; + int ret; if ( dir == curdir ) { return( 0 ); @@ -1519,16 +1642,23 @@ int movecwd(struct vol *vol, struct dir *dir) memcpy( p, u, n ); } if ( d != curdir ) { - n = strlen( vol->v_path ); + n = strlen( vol->v_realpath ); if (p -n -1 < path) { afp_errno = AFPERR_PARAM; return -1; } *--p = '/'; p -= n; - memcpy( p, vol->v_path, n ); + memcpy( p, vol->v_realpath, n ); } - if ( chdir( 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 */ + afp_errno = AFPERR_BADTYPE; + return -1; + } switch (errno) { case EACCES: case EPERM: @@ -1645,8 +1775,19 @@ int getdirparams(const struct vol *vol, (1 << DIRPBIT_FINFO)))) { ad_init(&ad, vol->v_adouble, vol->v_ad_options); - if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) { + if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) { isad = 1; + if (ad.ad_md->adf_flags & O_CREAT) { + /* We just created it */ + ad_setname(&ad, s_path->m_name); + ad_setid( &ad, + s_path->st.st_dev, + s_path->st.st_ino, + dir->d_did, + dir->d_parent->d_did, + vol->v_stamp); + ad_flush( &ad); + } } } @@ -1962,7 +2103,7 @@ int setdirparams(struct vol *vol, int bit, isad = 1; int cdate, bdate; int owner, group; - u_int16_t ashort, bshort; + u_int16_t ashort, bshort, oshort; int err = AFP_OK; int change_mdate = 0; int change_parent_mdate = 0; @@ -2079,7 +2220,7 @@ int setdirparams(struct vol *vol, } ad_init(&ad, vol->v_adouble, vol->v_ad_options); - if (ad_open_metadata( upath, vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad) < 0) { + if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) { /* * Check to see what we're trying to set. If it's anything * but ACCESS, UID, or GID, give an error. If it's any of those @@ -2118,14 +2259,14 @@ int setdirparams(struct vol *vol, case DIRPBIT_ATTR : if (isad) { ad_getattr(&ad, &bshort); - if ((bshort & htons(ATTRBIT_INVISIBLE)) != - (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) ) - change_parent_mdate = 1; + oshort = bshort; if ( ntohs( ashort ) & ATTRBIT_SETCLR ) { bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR ); } else { bshort &= ~ashort; } + if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE))) + change_parent_mdate = 1; ad_setattr(&ad, bshort); } break; @@ -2430,7 +2571,7 @@ int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_ } ad_init(&ad, vol->v_adouble, vol->v_ad_options); - if (ad_open_metadata( ".", vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad ) < 0) { + if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) { if (vol_noadouble(vol)) goto createdir_done; return( AFPERR_ACCESS ); @@ -2576,6 +2717,7 @@ int deletecurdir(struct vol *vol) fdir = curdir; ad_init(&ad, vol->v_adouble, vol->v_ad_options); + /* we never want to create a resource fork here, we are going to delete it */ if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) { ad_getattr(&ad, &ashort); @@ -2614,11 +2756,11 @@ int deletecurdir(struct vol *vol) goto delete_done; } - if ( !(err = netatalk_rmdir(fdir->d_u_name))) { + err = netatalk_rmdir_all_errors(fdir->d_u_name); + if ( err == AFP_OK || err == AFPERR_NOOBJ) { dirchildremove(curdir, fdir); cnid_delete(vol->v_cdb, fdir->d_did); dir_remove( vol, fdir ); - err = AFP_OK; } delete_done: if (dp) {