X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=blobdiff_plain;f=etc%2Fafpd%2Fenumerate.c;h=523223b109eba1ff20e4e5b428482524589acc5e;hp=66cfaa245c2bcbf2375b852d8ca0d28471f55363;hb=1b20936596f89b2706f1122ca2fabad6ffe00c98;hpb=6b814c857d98259bd82737ce8746c6c1d94e9cd5 diff --git a/etc/afpd/enumerate.c b/etc/afpd/enumerate.c index 66cfaa24..523223b1 100644 --- a/etc/afpd/enumerate.c +++ b/etc/afpd/enumerate.c @@ -1,5 +1,5 @@ /* - * $Id: enumerate.c,v 1.24 2002-10-11 14:18:28 didg Exp $ + * $Id: enumerate.c,v 1.47.2.1 2010-01-02 10:22:32 franklahm Exp $ * * Copyright (c) 1990,1993 Regents of The University of Michigan. * All Rights Reserved. See COPYRIGHT. @@ -13,19 +13,14 @@ #include #include #include - -#include -#include -#include #include #include -#include +#include #include #include -#ifdef CNID_DB +#include #include -#endif /* CNID_DB */ #include "desktop.h" #include "directory.h" #include "volume.h" @@ -36,74 +31,6 @@ #define min(a,b) ((a)<(b)?(a):(b)) -/* ---------------------------- */ -struct dir * - adddir( vol, dir, path) -struct vol *vol; -struct dir *dir; -struct path *path; -{ - struct dir *cdir, *edir; - int upathlen; - char *name; - char *upath; -#ifndef USE_LASTDID - struct stat lst, *lstp; -#endif /* USE_LASTDID */ - struct stat *st; - - upath = path->u_name; - name = path->m_name; - st = &path->st; - upathlen = strlen(upath); - if ((cdir = dirnew(name, upath)) == NULL) { - LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) ); - return NULL; - } - - cdir->d_did = 0; - -#ifdef CNID_DB - /* add to cnid db */ - cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath, - upathlen, cdir->d_did); - /* Fail out if things go bad with CNID. */ - if (cdir->d_did == CNID_INVALID) { - switch (errno) { - case CNID_ERR_PARAM: - LOG(log_error, logtype_afpd, "adddir: Incorrect parameters passed to cnid_add"); - return NULL; - case CNID_ERR_PATH: - case CNID_ERR_DB: - case CNID_ERR_MAX: - return NULL; - } - } -#endif /* CNID_DB */ - - if (cdir->d_did == 0) { -#ifdef USE_LASTDID - /* last way of doing DIDs */ - cdir->d_did = htonl( vol->v_lastdid++ ); -#else /* USE_LASTDID */ - lstp = lstat(upath, &lst) < 0 ? st : &lst; - /* the old way of doing DIDs (default) */ - cdir->d_did = htonl( CNID(lstp, 0) ); -#endif /* USE_LASTDID */ - } - - if ((edir = dirinsert( vol, cdir ))) { - edir->d_m_name = cdir->d_m_name; - edir->d_u_name = cdir->d_u_name; - free(cdir); - cdir = edir; - } - - /* parent/child directories */ - cdir->d_parent = dir; - dirchildadd(dir, cdir); - return( cdir ); -} /* * Struct to save directory reading context in. Used to prevent * O(n^2) searches on a directory. @@ -116,19 +43,19 @@ struct savedir { char *sd_last; unsigned int sd_sindex; }; -#define SDBUFBRK 1024 +#define SDBUFBRK 2048 -static int enumerate_loop(struct dirent *de, char *mname, void *data) +static int enumerate_loop(struct dirent *de, char *mname _U_, void *data) { struct savedir *sd = data; char *start, *end; - int len; + int len,lenm; end = sd->sd_buf + sd->sd_buflen; len = strlen(de->d_name); *(sd->sd_last)++ = len; - - if ( sd->sd_last + len + 2 > end ) { + lenm = 0; /* strlen(mname);*/ + if ( sd->sd_last + len +lenm + 4 > end ) { char *buf; start = sd->sd_buf; @@ -146,29 +73,51 @@ static int enumerate_loop(struct dirent *de, char *mname, void *data) memcpy( sd->sd_last, de->d_name, len + 1 ); sd->sd_last += len + 1; +#if 0 + *(sd->sd_last)++ = lenm; + memcpy( sd->sd_last, mname, lenm + 1 ); + sd->sd_last += lenm + 1; +#endif return 0; } -/* ----------------------------- */ +/* ----------------------------- + * FIXME: + * Doesn't work with dangling symlink + * ie: + * - Move a folder with a dangling symlink in the trash + * - empty the trash + * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams + * and the Mac doesn't try to call afp_delete! + * + * Another option for symlink + * cf: + * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642 + * +*/ char *check_dirent(const struct vol *vol, char *name) { - char *m_name = NULL; - if (!strcmp(name, "..") || !strcmp(name, ".")) return NULL; - if (!(validupath(vol, name))) + if (!vol->vfs->vfs_validupath(vol, name)) return NULL; /* check for vetoed filenames */ if (veto_file(vol->v_veto, name)) return NULL; +#if 0 + char *m_name = NULL; + + if (NULL == (m_name = utompath(vol, name, 0, utf8_encoding()))) + return NULL; + /* now check against too big a file */ - if (strlen(m_name = utompath(vol, name)) > MACFILELEN) + if (strlen(m_name) > vol->max_filename) return NULL; - - return m_name; +#endif + return name; } /* ----------------------------- */ @@ -180,7 +129,7 @@ for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data) char *m_name; int ret; - if (( dp = opendir( name)) == NULL ) { + if (NULL == ( dp = opendir( name)) ) { return -1; } ret = 0; @@ -198,24 +147,32 @@ for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data) return ret; } +/* This is the maximal length of a single entry for a file/dir in the reply + block if all bits in the file/dir bitmap are set: header(4) + params(104) + + macnamelength(1) + macname(31) + utf8(4) + utf8namelen(2) + utf8name(255) + + oddpadding(1) */ + +#define REPLY_PARAM_MAXLEN (4 + 104 + 1 + MACFILELEN + 4 + 2 + 255 + 1) + /* ----------------------------- */ -static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, is64 ) -AFPObj *obj; -char *ibuf, *rbuf; -unsigned int ibuflen, *rbuflen; -int is64; +static int enumerate(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, + char *rbuf, + size_t *rbuflen, + int ext) { static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 }; struct vol *vol; struct dir *dir; - int did, ret, esz, len, first = 1; + int did, ret, len, first = 1; + size_t esz; char *data, *start; u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0; u_int16_t temp16; u_int32_t sindex, maxsz, sz = 0; struct path *o_path; struct path s_path; - + int header; + if ( sd.sd_buflen == 0 ) { if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) { LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) ); @@ -230,7 +187,7 @@ int is64; memcpy( &vid, ibuf, sizeof( vid )); ibuf += sizeof( vid ); - if (( vol = getvolbyvid( vid )) == NULL ) { + if (NULL == ( vol = getvolbyvid( vid )) ) { *rbuflen = 0; return( AFPERR_PARAM ); } @@ -238,9 +195,9 @@ int is64; memcpy( &did, ibuf, sizeof( did )); ibuf += sizeof( did ); - if (( dir = dirlookup( vol, did )) == NULL ) { + if (NULL == ( dir = dirlookup( vol, did )) ) { *rbuflen = 0; - return( AFPERR_NODIR ); + return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno; } memcpy( &fbitmap, ibuf, sizeof( fbitmap )); @@ -264,9 +221,9 @@ int is64; reqcnt = ntohs( reqcnt ); ibuf += sizeof( reqcnt ); - if (is64) { + if (ext == 2) { memcpy( &sindex, ibuf, sizeof( sindex )); - sindex = ntohs( sindex ); + sindex = ntohl( sindex ); ibuf += sizeof( sindex ); } else { @@ -274,10 +231,15 @@ int is64; sindex = ntohs( temp16 ); ibuf += sizeof( temp16 ); } - - if (is64) { + + if (!sindex) { + *rbuflen = 0; + return AFPERR_PARAM ; + } + + if (ext == 2) { memcpy( &maxsz, ibuf, sizeof( maxsz )); - maxsz = ntohs( maxsz ); + maxsz = ntohl( maxsz ); ibuf += sizeof( maxsz ); } else { @@ -286,20 +248,26 @@ int is64; ibuf += sizeof( temp16 ); } - maxsz = min(maxsz, *rbuflen); + header = (ext)?4:2; + header *=sizeof( u_char ); + + maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN); + o_path = cname( vol, dir, &ibuf ); - if (( o_path = cname( vol, dir, &ibuf )) == NULL) { - *rbuflen = 0; - return( AFPERR_NODIR ); - } + if (afp_errno == AFPERR_NOOBJ) + afp_errno = AFPERR_NODIR; + *rbuflen = 0; + if (NULL == o_path ) { + return get_afp_errno(AFPERR_NOOBJ); + } if ( *o_path->m_name != '\0') { - *rbuflen = 0; - return( AFPERR_BADTYPE ); + /* it's a file or it's a dir and extendir() was unable to chdir in it */ + return path_error(o_path, AFPERR_NODIR ); } data = rbuf + 3 * sizeof( u_int16_t ); - sz = 3 * sizeof( u_int16_t ); + sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */ /* * Read the directory into a pre-malloced buffer, stored @@ -308,21 +276,10 @@ int is64; */ if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) { sd.sd_last = sd.sd_buf; - if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) { - switch (errno) { - case EACCES: - return AFPERR_ACCESS; - case ENOTDIR: - return AFPERR_BADTYPE; - case ENOMEM: - return AFPERR_MISC; - default: - return AFPERR_NODIR; - } - } - curdir->ctime = o_path->st.st_ctime; /* play safe */ - if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) { - *rbuflen = 0; + /* if dir was in the cache we don't have the inode */ + if (( !o_path->st_valid && lstat( ".", &o_path->st ) < 0 ) || + (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) + { switch (errno) { case EACCES: return AFPERR_ACCESS; @@ -334,14 +291,14 @@ int is64; return AFPERR_NODIR; } } - curdir->offcnt = ret; + setdiroffcnt(curdir, &o_path->st, ret); *sd.sd_last = 0; sd.sd_last = sd.sd_buf; sd.sd_sindex = 1; sd.sd_vid = vid; - sd.sd_did = did; + sd.sd_did = curdir->d_did; } /* @@ -352,17 +309,16 @@ int is64; sd.sd_last = sd.sd_buf; } while ( sd.sd_sindex < sindex ) { - len = *(sd.sd_last)++; + len = (unsigned char)*(sd.sd_last)++; if ( len == 0 ) { sd.sd_did = 0; /* invalidate sd struct to force re-read */ - *rbuflen = 0; return( AFPERR_NOOBJ ); } sd.sd_last += len + 1; sd.sd_sindex++; } - while (( len = *(sd.sd_last)) != 0 ) { + while (( len = (unsigned char)*(sd.sd_last)) != 0 ) { /* * If we've got all we need, send it. */ @@ -382,6 +338,7 @@ int is64; sd.sd_last += len + 1; continue; } + memset(&s_path, 0, sizeof(s_path)); s_path.u_name = sd.sd_last; if (of_stat( &s_path) < 0 ) { /* @@ -395,9 +352,12 @@ int is64; */ *sd.sd_last = 0; sd.sd_last += len + 1; + curdir->offcnt--; /* a little lie */ continue; } + sd.sd_last += len + 1; + s_path.m_name = NULL; /* * If a fil/dir is not a dir, it's a file. This is slightly * inaccurate, since that means /dev/null is a file, /dev/printer @@ -405,40 +365,23 @@ int is64; */ if ( S_ISDIR(s_path.st.st_mode)) { if ( dbitmap == 0 ) { - sd.sd_last += len + 1; continue; } - dir = curdir->d_child; - s_path.m_name = NULL; - while (dir) { - if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) { - break; - } - dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next; - } - if (!dir) { - s_path.m_name = utompath(vol, s_path.u_name); - if ((dir = adddir( vol, curdir, &s_path)) == NULL) { - *rbuflen = 0; + dir = dirsearch_byname(vol, curdir, s_path.u_name); + if (!dir && NULL == (dir = adddir( vol, curdir, &s_path) ) ) { return AFPERR_MISC; } - } - - if (( ret = getdirparams(vol, dbitmap, &s_path, dir, - data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) { - *rbuflen = 0; + if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir, + data + header , &esz ))) { return( ret ); } } else { if ( fbitmap == 0 ) { - sd.sd_last += len + 1; continue; } - s_path.m_name = utompath(vol, s_path.u_name); - if (( ret = getfilparams(vol, fbitmap, &s_path, curdir, - data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) { - *rbuflen = 0; + if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir, + data + header , &esz )) ) { return( ret ); } } @@ -447,17 +390,16 @@ int is64; * Make sure entry is an even length, possibly with a null * byte on the end. */ - if ( esz & 1 ) { - *(data + 2 * sizeof( u_char ) + esz ) = '\0'; + if ( (esz + header) & 1 ) { + *(data + header + esz ) = '\0'; esz++; } /* * Check if we've exceeded the size limit. */ - if ( maxsz < sz + esz + 2 * sizeof( u_char )) { + if ( maxsz < sz + esz + header) { if (first) { /* maxsz can't hold a single reply */ - *rbuflen = 0; return AFPERR_PARAM; } sd.sd_last = start; @@ -467,16 +409,26 @@ int is64; if (first) first = 0; - sz += esz + 2 * sizeof( u_char ); - *data++ = esz + 2 * sizeof( u_char ); + sz += esz + header; + if (ext) { + temp16 = htons( esz + header ); + memcpy( data, &temp16, sizeof( temp16 )); + data += sizeof(temp16); + } + else { + *data++ = esz + header; + } + *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE; + if (ext) { + *data++ = 0; + } data += esz; actcnt++; - sd.sd_last += len + 1; + /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */ } if ( actcnt == 0 ) { - *rbuflen = 0; sd.sd_did = 0; /* invalidate sd struct to force re-read */ return( AFPERR_NOOBJ ); } @@ -499,20 +451,26 @@ int is64; } /* ----------------------------- */ -int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen ) -AFPObj *obj; -char *ibuf, *rbuf; -unsigned int ibuflen, *rbuflen; +int afp_enumerate(AFPObj *obj, char *ibuf, size_t ibuflen, + char *rbuf, + size_t *rbuflen) { return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0); } /* ----------------------------- */ -int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen ) -AFPObj *obj; -char *ibuf, *rbuf; -unsigned int ibuflen, *rbuflen; +int afp_enumerate_ext(AFPObj *obj, char *ibuf, size_t ibuflen, + char *rbuf, + size_t *rbuflen) { return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1); } +/* ----------------------------- */ +int afp_enumerate_ext2(AFPObj *obj, char *ibuf, size_t ibuflen, + char *rbuf, + size_t *rbuflen) +{ + return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2); +} +