1/*
2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved.  See COPYRIGHT.
4 */
5
6#ifdef HAVE_CONFIG_H
7#include "config.h"
8#endif /* HAVE_CONFIG_H */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <errno.h>
14#include <sys/file.h>
15#include <sys/param.h>
16
17#include <atalk/logger.h>
18#include <atalk/afp.h>
19#include <atalk/adouble.h>
20#include <atalk/vfs.h>
21#include <atalk/cnid.h>
22#include <atalk/util.h>
23#include <atalk/bstrlib.h>
24#include <atalk/bstradd.h>
25#include <atalk/globals.h>
26
27#include "desktop.h"
28#include "directory.h"
29#include "dircache.h"
30#include "volume.h"
31#include "file.h"
32#include "fork.h"
33#include "filedir.h"
34
35#define min(a,b)	((a)<(b)?(a):(b))
36
37/* foxconn add start, improvemennt of time machine backup rate,
38   Jonathan 2012/08/22 */
39#define TIME_MACHINE_WA
40
41/*
42 * Struct to save directory reading context in. Used to prevent
43 * O(n^2) searches on a directory.
44 */
45struct savedir {
46    u_short	 sd_vid;
47    u_int32_t	 sd_did;
48    int		 sd_buflen;
49    char	 *sd_buf;
50    char	 *sd_last;
51    unsigned int sd_sindex;
52};
53#define SDBUFBRK	2048
54
55static int enumerate_loop(struct dirent *de, char *mname _U_, void *data)
56{
57    struct savedir *sd = data;
58    char *start, *end;
59    int  len,lenm;
60
61    end = sd->sd_buf + sd->sd_buflen;
62    len = strlen(de->d_name);
63    *(sd->sd_last)++ = len;
64    lenm = 0; /* strlen(mname);*/
65    if ( sd->sd_last + len +lenm + 4 > end ) {
66        char *buf;
67
68        start = sd->sd_buf;
69        if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
70            LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
71                        strerror(errno) );
72            errno = ENOMEM;
73            return -1;
74        }
75        sd->sd_buf = buf;
76        sd->sd_buflen += SDBUFBRK;
77        sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
78        end = sd->sd_buf + sd->sd_buflen;
79    }
80
81    memcpy( sd->sd_last, de->d_name, len + 1 );
82    sd->sd_last += len + 1;
83#if 0
84    *(sd->sd_last)++ = lenm;
85    memcpy( sd->sd_last, mname, lenm + 1 );
86    sd->sd_last += lenm + 1;
87#endif
88    return 0;
89}
90
91/* -----------------------------
92 * FIXME:
93 * Doesn't work with dangling symlink
94 * ie:
95 * - Move a folder with a dangling symlink in the trash
96 * - empty the trash
97 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
98 * and the Mac doesn't try to call afp_delete!
99 *
100 * Another option for symlink
101 * cf:
102 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
103 *
104*/
105char *check_dirent(const struct vol *vol, char *name)
106{
107    if (!strcmp(name, "..") || !strcmp(name, "."))
108        return NULL;
109
110    if (!vol->vfs->vfs_validupath(vol, name))
111        return NULL;
112
113    /* check for vetoed filenames */
114    if (veto_file(vol->v_veto, name))
115        return NULL;
116
117#if 0
118    char *m_name = NULL;
119
120    if (NULL == (m_name = utompath(vol, name, 0, utf8_encoding())))
121        return NULL;
122
123    /* now check against too big a file */
124    if (strlen(m_name) > vol->max_filename)
125        return NULL;
126#endif
127    return name;
128}
129
130/* ----------------------------- */
131int
132for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
133{
134    DIR             *dp;
135    struct dirent	*de;
136    char            *m_name;
137    int             ret;
138
139    if (NULL == ( dp = opendir( name)) ) {
140        return -1;
141    }
142    ret = 0;
143    for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
144        if (!(m_name = check_dirent(vol, de->d_name)))
145            continue;
146
147        ret++;
148        if (fn && fn(de,m_name, data) < 0) {
149           closedir(dp);
150           return -1;
151        }
152    }
153    closedir(dp);
154    return ret;
155}
156
157/* This is the maximal length of a single entry for a file/dir in the reply
158   block if all bits in the file/dir bitmap are set: header(4) + params(104) +
159   macnamelength(1) + macname(31) + utf8(4) + utf8namelen(2) + utf8name(255) +
160   oddpadding(1) */
161
162#define REPLY_PARAM_MAXLEN (4 + 104 + 1 + MACFILELEN + 4 + 2 + UTF8FILELEN_EARLY + 1)
163
164/* ----------------------------- */
165static int enumerate(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,
166    char *rbuf,
167    size_t *rbuflen,
168    int ext)
169{
170    static struct savedir	sd = { 0, 0, 0, NULL, NULL, 0 };
171    struct vol			*vol;
172    struct dir			*dir;
173    int				did, ret, len, first = 1;
174    size_t			esz;
175    char                        *data, *start;
176    u_int16_t			vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
177    u_int16_t			temp16;
178    u_int32_t			sindex, maxsz, sz = 0;
179    struct path                 *o_path;
180    struct path                 s_path;
181    int                         header;
182
183    if ( sd.sd_buflen == 0 ) {
184        if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
185            LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
186            *rbuflen = 0;
187            return AFPERR_MISC;
188        }
189        sd.sd_buflen = SDBUFBRK;
190    }
191
192    ibuf += 2;
193
194    memcpy( &vid, ibuf, sizeof( vid ));
195    ibuf += sizeof( vid );
196
197    if (NULL == ( vol = getvolbyvid( vid )) ) {
198        *rbuflen = 0;
199        return( AFPERR_PARAM );
200    }
201
202    memcpy( &did, ibuf, sizeof( did ));
203    ibuf += sizeof( did );
204
205    if (NULL == ( dir = dirlookup( vol, did )) ) {
206        *rbuflen = 0;
207        return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
208    }
209
210    memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
211    fbitmap = ntohs( fbitmap );
212    ibuf += sizeof( fbitmap );
213
214    memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
215    dbitmap = ntohs( dbitmap );
216    ibuf += sizeof( dbitmap );
217
218    /* check for proper bitmaps -- the stuff in comments is for
219     * variable directory ids. */
220    if (!(fbitmap || dbitmap)
221            /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
222              (dbitmap & (1 << DIRPBIT_PDID))*/) {
223        *rbuflen = 0;
224        return AFPERR_BITMAP;
225    }
226
227    memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
228    reqcnt = ntohs( reqcnt );
229    ibuf += sizeof( reqcnt );
230
231    if (ext == 2) {
232        memcpy( &sindex, ibuf, sizeof( sindex ));
233        sindex = ntohl( sindex );
234        ibuf += sizeof( sindex );
235    }
236    else {
237        memcpy( &temp16, ibuf, sizeof( temp16 ));
238        sindex = ntohs( temp16 );
239        ibuf += sizeof( temp16 );
240    }
241
242    if (!sindex) {
243        *rbuflen = 0;
244        return AFPERR_PARAM ;
245    }
246
247    if (ext == 2) {
248        memcpy( &maxsz, ibuf, sizeof( maxsz ));
249        maxsz = ntohl( maxsz );
250        ibuf += sizeof( maxsz );
251    }
252    else {
253        memcpy( &temp16, ibuf, sizeof( temp16 ));
254        maxsz = ntohs( temp16 );
255        ibuf += sizeof( temp16 );
256    }
257
258    header = (ext)?4:2;
259    header *=sizeof( u_char );
260
261    maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN);
262    o_path = cname( vol, dir, &ibuf );
263
264    if (afp_errno == AFPERR_NOOBJ)
265        afp_errno = AFPERR_NODIR;
266
267    *rbuflen = 0;
268    if (NULL == o_path ) {
269        return get_afp_errno(AFPERR_NOOBJ);
270    }
271    if ( *o_path->m_name != '\0') {
272        /* it's a file or it's a dir and extendir() was unable to chdir in it */
273        return path_error(o_path, AFPERR_NODIR );
274    }
275
276    LOG(log_debug, logtype_afpd, "enumerate(\"%s/%s\", f/d:%04x/%04x, rc:%u, i:%u, max:%u)",
277        getcwdpath(), o_path->u_name, fbitmap, dbitmap, reqcnt, sindex, maxsz);
278
279    data = rbuf + 3 * sizeof( u_int16_t );
280    sz = 3 * sizeof( u_int16_t );	/* fbitmap, dbitmap, reqcount */
281
282    /*
283     * Read the directory into a pre-malloced buffer, stored
284     *		len <name> \0
285     * The end is indicated by a len of 0.
286     */
287    if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
288        sd.sd_last = sd.sd_buf;
289        /* if dir was in the cache we don't have the inode */
290        if (( !o_path->st_valid && lstat( ".", &o_path->st ) < 0 ) ||
291            (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
292        {
293            LOG(log_error, logtype_afpd, "enumerate: loop error: %s (%d)", strerror(errno), errno);
294            switch (errno) {
295            case EACCES:
296                return AFPERR_ACCESS;
297            case ENOTDIR:
298                return AFPERR_BADTYPE;
299            case ENOMEM:
300                return AFPERR_MISC;
301            default:
302                return AFPERR_NODIR;
303            }
304        }
305        setdiroffcnt(curdir, &o_path->st,  ret);
306        *sd.sd_last = 0;
307
308        sd.sd_last = sd.sd_buf;
309        sd.sd_sindex = 1;
310
311        sd.sd_vid = vid;
312        sd.sd_did = curdir->d_did;
313    }
314
315    /*
316     * Position sd_last as dictated by sindex.
317     */
318    if ( sindex < sd.sd_sindex ) {
319        sd.sd_sindex = 1;
320        sd.sd_last = sd.sd_buf;
321    }
322    while ( sd.sd_sindex < sindex ) {
323        len = (unsigned char)*(sd.sd_last)++;
324        if ( len == 0 ) {
325            sd.sd_did = 0;	/* invalidate sd struct to force re-read */
326            return( AFPERR_NOOBJ );
327        }
328        sd.sd_last += len + 1;
329        sd.sd_sindex++;
330    }
331
332    while (( len = (unsigned char)*(sd.sd_last)) != 0 ) {
333        /*
334         * If we've got all we need, send it.
335         */
336        if ( actcnt == reqcnt ) {
337            break;
338        }
339
340        /*
341         * Save the start position, in case we exceed the buffer
342         * limitation, and have to back up one.
343         */
344        start = sd.sd_last;
345        sd.sd_last++;
346
347        if (*sd.sd_last == 0) {
348            /* stat() already failed on this one */
349            sd.sd_last += len + 1;
350            continue;
351        }
352        memset(&s_path, 0, sizeof(s_path));
353        s_path.u_name = sd.sd_last;
354        if (of_stat( &s_path) < 0 ) {
355            /*
356             * Somebody else plays with the dir, well it can be us with
357            * "Empty Trash..."
358            */
359
360            /* so the next time it won't try to stat it again
361             * another solution would be to invalidate the cache with
362             * sd.sd_did = 0 but if it's not ENOENT error it will start again
363             */
364            *sd.sd_last = 0;
365            sd.sd_last += len + 1;
366            curdir->d_offcnt--;		/* a little lie */
367/* foxconn add start, Jonathan 2012/08/22 */
368#ifdef TIME_MACHINE_WA
369			afp_bandsdid_decreaseOffcnt(curdir->d_did);
370#endif
371            continue;
372        }
373
374        sd.sd_last += len + 1;
375        s_path.m_name = NULL;
376        /*
377         * If a fil/dir is not a dir, it's a file. This is slightly
378         * inaccurate, since that means /dev/null is a file, /dev/printer
379         * is a file, etc.
380         */
381        if ( S_ISDIR(s_path.st.st_mode)) {
382            if ( dbitmap == 0 ) {
383                continue;
384            }
385            int len = strlen(s_path.u_name);
386            if ((dir = dircache_search_by_name(vol, curdir, s_path.u_name, len)) == NULL) {
387/* foxconn add start, Jonathan 2012/08/22
388   if HD have backup folder, "sparsbundle" or "bands", record did here */
389#ifdef TIME_MACHINE_WA
390		if (strstr(s_path.u_name,"sparsebundle" ) || strstr(s_path.u_name,"bands") )
391		{
392			afp_enablechk();
393		}
394#endif
395                if ((dir = dir_add(vol, curdir, &s_path, len)) == NULL) {
396                    LOG(log_error, logtype_afpd, "enumerate(vid:%u, did:%u, name:'%s'): error adding dir: '%s'",
397                        ntohs(vid), ntohl(did), o_path->u_name, s_path.u_name);
398                        #ifdef TIME_MACHINE_WA  /* foxconn add start, Jonathan 2012/08/22 */
399			afp_disablechk(); // jon_20120712, offcnt workaround
400                        #endif
401                    return AFPERR_MISC;
402                }
403#ifdef TIME_MACHINE_WA  /* foxconn add start, Jonathan 2012/08/22 */
404		if (strstr(s_path.u_name,"sparsebundle" ) || strstr(s_path.u_name,"bands") )
405		{
406			afp_disablechk();
407                }
408#endif
409            }
410            if ((ret = getdirparams(vol, dbitmap, &s_path, dir, data + header , &esz)) != AFP_OK)
411                return( ret );
412
413        } else {
414            if ( fbitmap == 0 ) {
415                continue;
416            }
417            /* files are added to the dircache in getfilparams() -> getmetadata() */
418            if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
419                                     data + header , &esz )) ) {
420                return( ret );
421            }
422        }
423
424        /*
425         * Make sure entry is an even length, possibly with a null
426         * byte on the end.
427         */
428        if ( (esz + header) & 1 ) {
429            *(data + header + esz ) = '\0';
430            esz++;
431        }
432
433        /*
434         * Check if we've exceeded the size limit.
435         */
436        if ( maxsz < sz + esz + header) {
437            if (first) { /* maxsz can't hold a single reply */
438                return AFPERR_PARAM;
439            }
440            sd.sd_last = start;
441            break;
442        }
443
444        if (first)
445            first = 0;
446
447        sz += esz + header;
448        if (ext) {
449            temp16 = htons( esz + header );
450            memcpy( data, &temp16, sizeof( temp16 ));
451            data += sizeof(temp16);
452        }
453        else {
454            *data++ = esz + header;
455        }
456
457        *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
458        if (ext) {
459             *data++ = 0;
460        }
461        data += esz;
462        actcnt++;
463        /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */
464    }
465
466    if ( actcnt == 0 ) {
467        sd.sd_did = 0;		/* invalidate sd struct to force re-read */
468        return( AFPERR_NOOBJ );
469    }
470    sd.sd_sindex = sindex + actcnt;
471
472    /*
473     * All done, fill in misc junk in rbuf
474     */
475    fbitmap = htons( fbitmap );
476    memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
477    rbuf += sizeof( fbitmap );
478    dbitmap = htons( dbitmap );
479    memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
480    rbuf += sizeof( dbitmap );
481    actcnt = htons( actcnt );
482    memcpy( rbuf, &actcnt, sizeof( actcnt ));
483    rbuf += sizeof( actcnt );
484    *rbuflen = sz;
485    return( AFP_OK );
486}
487
488/* ----------------------------- */
489int afp_enumerate(AFPObj *obj, char *ibuf, size_t ibuflen,
490    char *rbuf,
491    size_t *rbuflen)
492{
493    return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
494}
495
496/* ----------------------------- */
497int afp_enumerate_ext(AFPObj *obj, char *ibuf, size_t ibuflen,
498    char *rbuf,
499    size_t *rbuflen)
500{
501    return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
502}
503
504/* ----------------------------- */
505int afp_enumerate_ext2(AFPObj *obj, char *ibuf, size_t ibuflen,
506    char *rbuf,
507    size_t *rbuflen)
508{
509    return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);
510}
511
512