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