]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/enumerate.c
/tmp/log
[netatalk.git] / etc / afpd / enumerate.c
1 /*
2  * $Id: enumerate.c,v 1.30 2003-01-12 14:39:59 didg 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
17 #include <atalk/logger.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/file.h>
21 #include <sys/param.h>
22
23 #include <netatalk/endian.h>
24 #include <atalk/afp.h>
25 #include <atalk/adouble.h>
26 #ifdef CNID_DB
27 #include <atalk/cnid.h>
28 #endif /* CNID_DB */
29 #include "desktop.h"
30 #include "directory.h"
31 #include "volume.h"
32 #include "globals.h"
33 #include "file.h"
34 #include "fork.h"
35 #include "filedir.h"
36
37 #define min(a,b)        ((a)<(b)?(a):(b))
38
39 /* ---------------------------- */
40 struct dir *
41             adddir( vol, dir, path)
42 struct vol      *vol;
43 struct dir      *dir;
44 struct path     *path;
45 {
46     struct dir  *cdir, *edir;
47     int         upathlen;
48     char        *name;
49     char        *upath;
50 #ifndef USE_LASTDID
51     struct stat lst, *lstp;
52 #endif /* USE_LASTDID */
53     struct stat *st;
54
55     upath = path->u_name;
56     name  = path->m_name;    
57     st    = &path->st;
58     upathlen = strlen(upath);
59     if ((cdir = dirnew(name, upath)) == NULL) {
60         LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
61         return NULL;
62     }
63
64     cdir->d_did = 0;
65
66 #ifdef CNID_DB
67     /* add to cnid db */
68     cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
69                            upathlen, cdir->d_did);
70     /* Fail out if things go bad with CNID. */
71     if (cdir->d_did == CNID_INVALID) {
72         switch (errno) {
73         case CNID_ERR_PARAM:
74             LOG(log_error, logtype_afpd, "adddir: Incorrect parameters passed to cnid_add");
75             return NULL;
76         case CNID_ERR_PATH:
77         case CNID_ERR_DB:
78         case CNID_ERR_MAX:
79             return NULL;
80         }
81     }
82 #endif /* CNID_DB */
83
84     if (cdir->d_did == 0) {
85 #ifdef USE_LASTDID
86         /* last way of doing DIDs */
87         cdir->d_did = htonl( vol->v_lastdid++ );
88 #else /* USE_LASTDID */
89         lstp = lstat(upath, &lst) < 0 ? st : &lst;
90         /* the old way of doing DIDs (default) */
91         cdir->d_did = htonl( CNID(lstp, 0) );
92 #endif /* USE_LASTDID */
93     }
94
95     if ((edir = dirinsert( vol, cdir ))) {
96         edir->d_m_name = cdir->d_m_name;
97         edir->d_u_name = cdir->d_u_name;
98         free(cdir);
99         cdir = edir;
100     }
101
102     /* parent/child directories */
103     cdir->d_parent = dir;
104     dirchildadd(dir, cdir);
105     return( cdir );
106 }
107 /*
108  * Struct to save directory reading context in. Used to prevent
109  * O(n^2) searches on a directory.
110  */
111 struct savedir {
112     u_short      sd_vid;
113     u_int32_t    sd_did;
114     int          sd_buflen;
115     char         *sd_buf;
116     char         *sd_last;
117     unsigned int sd_sindex;
118 };
119 #define SDBUFBRK        1024
120
121 static int enumerate_loop(struct dirent *de, char *mname, void *data)
122 {
123     struct savedir *sd = data; 
124     char *start, *end;
125     int  len;
126     
127     end = sd->sd_buf + sd->sd_buflen;
128     len = strlen(de->d_name);
129     *(sd->sd_last)++ = len;
130
131     if ( sd->sd_last + len + 2 > end ) {
132         char *buf;
133
134         start = sd->sd_buf;
135         if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
136             LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
137                         strerror(errno) );
138             errno = ENOMEM;
139             return -1;
140         }
141         sd->sd_buf = buf;
142         sd->sd_buflen += SDBUFBRK;
143         sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
144         end = sd->sd_buf + sd->sd_buflen;
145     }
146
147     memcpy( sd->sd_last, de->d_name, len + 1 );
148     sd->sd_last += len + 1;
149     return 0;
150 }
151
152 /* ----------------------------- 
153  * FIXME: 
154  * Doesn't work with dangling symlink
155  * ie: 
156  * - Move a folder with a dangling symlink in the trash
157  * - empty the trash
158  * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams 
159  * and the Mac doesn't try to call afp_delete!
160  *
161  * Another option for symlink
162  * cf:
163  * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
164  * 
165 */
166 char *check_dirent(const struct vol *vol, char *name)
167 {
168     char *m_name = NULL;
169
170     if (!strcmp(name, "..") || !strcmp(name, "."))
171         return NULL;
172
173     if (!(validupath(vol, name)))
174         return NULL;
175
176     /* check for vetoed filenames */
177     if (veto_file(vol->v_veto, name))
178         return NULL;
179
180     /* now check against too big a file */
181     if (strlen(m_name = utompath(vol, name)) > vol->max_filename)
182         return NULL;
183
184     return m_name;
185 }
186
187 /* ----------------------------- */
188 int 
189 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
190 {
191     DIR             *dp;
192     struct dirent       *de;
193     char            *m_name;
194     int             ret;
195     
196     if (( dp = opendir( name)) == NULL ) {
197         return -1;
198     }
199     ret = 0;
200     for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
201         if (!(m_name = check_dirent(vol, de->d_name)))
202             continue;
203
204         ret++;
205         if (fn && fn(de,m_name, data) < 0) {
206            closedir(dp);
207            return -1;
208         }
209     }
210     closedir(dp);
211     return ret;
212 }
213
214 /* ----------------------------- */
215 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
216 AFPObj       *obj;
217 char         *ibuf, *rbuf;
218 unsigned int ibuflen, *rbuflen;
219 int     ext;
220 {
221     static struct savedir       sd = { 0, 0, 0, NULL, NULL, 0 };
222     struct vol                  *vol;
223     struct dir                  *dir;
224     int                         did, ret, esz, len, first = 1;
225     char                        *data, *start;
226     u_int16_t                   vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
227     u_int16_t                   temp16;
228     u_int32_t                   sindex, maxsz, sz = 0;
229     struct path                 *o_path;
230     struct path                 s_path;
231     int                         header;
232         
233     if ( sd.sd_buflen == 0 ) {
234         if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
235             LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
236             *rbuflen = 0;
237             return AFPERR_MISC;
238         }
239         sd.sd_buflen = SDBUFBRK;
240     }
241
242     ibuf += 2;
243
244     memcpy( &vid, ibuf, sizeof( vid ));
245     ibuf += sizeof( vid );
246
247     if (NULL == ( vol = getvolbyvid( vid )) ) {
248         *rbuflen = 0;
249         return( AFPERR_PARAM );
250     }
251
252     memcpy( &did, ibuf, sizeof( did ));
253     ibuf += sizeof( did );
254
255     if (NULL == ( dir = dirlookup( vol, did )) ) {
256         *rbuflen = 0;
257         return( AFPERR_NODIR );
258     }
259
260     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
261     fbitmap = ntohs( fbitmap );
262     ibuf += sizeof( fbitmap );
263
264     memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
265     dbitmap = ntohs( dbitmap );
266     ibuf += sizeof( dbitmap );
267
268     /* check for proper bitmaps -- the stuff in comments is for
269      * variable directory ids. */
270     if (!(fbitmap || dbitmap)
271             /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
272               (dbitmap & (1 << DIRPBIT_PDID))*/) {
273         *rbuflen = 0;
274         return AFPERR_BITMAP;
275     }
276
277     memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
278     reqcnt = ntohs( reqcnt );
279     ibuf += sizeof( reqcnt );
280
281     if (ext == 2) {
282         memcpy( &sindex, ibuf, sizeof( sindex ));
283         sindex = ntohl( sindex );
284         ibuf += sizeof( sindex );
285     }
286     else {
287         memcpy( &temp16, ibuf, sizeof( temp16 ));
288         sindex = ntohs( temp16 );
289         ibuf += sizeof( temp16 );
290     }
291
292     if (!sindex) {
293         *rbuflen = 0;
294         return AFPERR_PARAM ;
295     }
296
297     if (ext == 2) {
298         memcpy( &maxsz, ibuf, sizeof( maxsz ));
299         maxsz = ntohl( maxsz );
300         ibuf += sizeof( maxsz );
301     }
302     else {
303         memcpy( &temp16, ibuf, sizeof( temp16 ));
304         maxsz = ntohs( temp16 );
305         ibuf += sizeof( temp16 );
306     }
307     
308     header = (ext)?4:2;
309     header *=sizeof( u_char );
310     
311     maxsz = min(maxsz, *rbuflen);
312
313     if (NULL == ( o_path = cname( vol, dir, &ibuf )) ) {
314         *rbuflen = 0;
315         return( AFPERR_NODIR );
316     }
317
318     if ( *o_path->m_name != '\0') {
319         *rbuflen = 0;
320         return( AFPERR_BADTYPE );
321     }
322
323     data = rbuf + 3 * sizeof( u_int16_t );
324     sz = 3 * sizeof( u_int16_t );       /* fbitmap, dbitmap, reqcount */
325
326     /*
327      * Read the directory into a pre-malloced buffer, stored
328      *          len <name> \0
329      * The end is indicated by a len of 0.
330      */
331     if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
332         sd.sd_last = sd.sd_buf;
333         if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
334             switch (errno) {
335             case EACCES:
336                 return AFPERR_ACCESS;
337             case ENOTDIR:
338                 return AFPERR_BADTYPE;
339             case ENOMEM:
340                 return AFPERR_MISC;
341             default:
342                 return AFPERR_NODIR;
343             }
344         }
345         curdir->ctime  = o_path->st.st_ctime; /* play safe */
346         if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
347             *rbuflen = 0;
348             switch (errno) {
349             case EACCES:
350                 return AFPERR_ACCESS;
351             case ENOTDIR:
352                 return AFPERR_BADTYPE;
353             case ENOMEM:
354                 return AFPERR_MISC;
355             default:
356                 return AFPERR_NODIR;
357             }
358         }
359         curdir->offcnt = ret;
360         *sd.sd_last = 0;
361
362         sd.sd_last = sd.sd_buf;
363         sd.sd_sindex = 1;
364
365         sd.sd_vid = vid;
366         sd.sd_did = did;
367     }
368
369     /*
370      * Position sd_last as dictated by sindex.
371      */
372     if ( sindex < sd.sd_sindex ) {
373         sd.sd_sindex = 1;
374         sd.sd_last = sd.sd_buf;
375     }
376     while ( sd.sd_sindex < sindex ) {
377         len = *(sd.sd_last)++;
378         if ( len == 0 ) {
379             sd.sd_did = 0;      /* invalidate sd struct to force re-read */
380             *rbuflen = 0;
381             return( AFPERR_NOOBJ );
382         }
383         sd.sd_last += len + 1;
384         sd.sd_sindex++;
385     }
386
387     while (( len = *(sd.sd_last)) != 0 ) {
388         /*
389          * If we've got all we need, send it.
390          */
391         if ( actcnt == reqcnt ) {
392             break;
393         }
394
395         /*
396          * Save the start position, in case we exceed the buffer
397          * limitation, and have to back up one.
398          */
399         start = sd.sd_last;
400         sd.sd_last++;
401
402         if (*sd.sd_last == 0) {
403             /* stat() already failed on this one */
404             sd.sd_last += len + 1;
405             continue;
406         }
407         s_path.u_name = sd.sd_last;
408         if (of_stat( &s_path) < 0 ) {
409             /*
410              * Somebody else plays with the dir, well it can be us with 
411             * "Empty Trash..."
412             */
413
414             /* so the next time it won't try to stat it again
415              * another solution would be to invalidate the cache with 
416              * sd.sd_did = 0 but if it's not ENOENT error it will start again
417              */
418             *sd.sd_last = 0;
419             sd.sd_last += len + 1;
420             curdir->offcnt--;           /* a little lie */
421             continue;
422         }
423
424         /*
425          * If a fil/dir is not a dir, it's a file. This is slightly
426          * inaccurate, since that means /dev/null is a file, /dev/printer
427          * is a file, etc.
428          */
429         if ( S_ISDIR(s_path.st.st_mode)) {
430             if ( dbitmap == 0 ) {
431                 sd.sd_last += len + 1;
432                 continue;
433             }
434             dir = curdir->d_child;
435             s_path.m_name = NULL;
436             while (dir) {
437                 if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
438                     break;
439                 }
440                 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
441             }
442             if (!dir) {
443                 s_path.m_name = utompath(vol, s_path.u_name);
444                 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
445                     *rbuflen = 0;
446                     return AFPERR_MISC;
447                 }
448             }
449
450             if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
451                                      data + header , &esz )) != AFP_OK ) {
452                 *rbuflen = 0;
453                 return( ret );
454             }
455
456         } else {
457             if ( fbitmap == 0 ) {
458                 sd.sd_last += len + 1;
459                 continue;
460             }
461             s_path.m_name = utompath(vol, s_path.u_name);
462             if (( ret = getfilparams(vol, fbitmap, &s_path, curdir, 
463                                      data + header , &esz )) != AFP_OK ) {
464                 *rbuflen = 0;
465                 return( ret );
466             }
467         }
468
469         /*
470          * Make sure entry is an even length, possibly with a null
471          * byte on the end.
472          */
473         if ( (esz + header) & 1 ) {
474             *(data + header + esz ) = '\0';
475             esz++;
476         }
477
478         /*
479          * Check if we've exceeded the size limit.
480          */
481         if ( maxsz < sz + esz + header) {
482             if (first) { /* maxsz can't hold a single reply */
483                 *rbuflen = 0;
484                 return AFPERR_PARAM;
485             }
486             sd.sd_last = start;
487             break;
488         }
489
490         if (first)
491             first = 0;
492
493         sz += esz + header;
494         if (ext) {
495             temp16 = htons( esz + header );
496             memcpy( data, &temp16, sizeof( temp16 ));
497             data += sizeof(temp16);
498         }
499         else {
500             *data++ = esz + header;
501         }
502
503         *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
504         if (ext) {
505              *data++ = 0;
506         }
507         data += esz;
508         actcnt++;
509         sd.sd_last += len + 1;
510     }
511
512     if ( actcnt == 0 ) {
513         *rbuflen = 0;
514         sd.sd_did = 0;          /* invalidate sd struct to force re-read */
515         return( AFPERR_NOOBJ );
516     }
517     sd.sd_sindex = sindex + actcnt;
518
519     /*
520      * All done, fill in misc junk in rbuf
521      */
522     fbitmap = htons( fbitmap );
523     memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
524     rbuf += sizeof( fbitmap );
525     dbitmap = htons( dbitmap );
526     memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
527     rbuf += sizeof( dbitmap );
528     actcnt = htons( actcnt );
529     memcpy( rbuf, &actcnt, sizeof( actcnt ));
530     rbuf += sizeof( actcnt );
531     *rbuflen = sz;
532     return( AFP_OK );
533 }
534
535 /* ----------------------------- */
536 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
537 AFPObj       *obj;
538 char         *ibuf, *rbuf;
539 unsigned int ibuflen, *rbuflen;
540 {
541     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
542 }
543
544 /* ----------------------------- */
545 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
546 AFPObj       *obj;
547 char         *ibuf, *rbuf;
548 unsigned int ibuflen, *rbuflen;
549 {
550     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
551 }
552
553 /* ----------------------------- */
554 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
555 AFPObj       *obj;
556 char         *ibuf, *rbuf;
557 unsigned int ibuflen, *rbuflen;
558 {
559     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);
560 }
561