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