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