]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/enumerate.c
66cfaa245c2bcbf2375b852d8ca0d28471f55363
[netatalk.git] / etc / afpd / enumerate.c
1 /*
2  * $Id: enumerate.c,v 1.24 2002-10-11 14:18:28 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 char *check_dirent(const struct vol *vol, char *name)
154 {
155     char *m_name = NULL;
156
157     if (!strcmp(name, "..") || !strcmp(name, "."))
158         return NULL;
159
160     if (!(validupath(vol, name)))
161         return NULL;
162
163     /* check for vetoed filenames */
164     if (veto_file(vol->v_veto, name))
165         return NULL;
166
167     /* now check against too big a file */
168     if (strlen(m_name = utompath(vol, name)) > MACFILELEN)
169         return NULL;
170
171     return m_name;
172 }
173
174 /* ----------------------------- */
175 int 
176 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
177 {
178     DIR             *dp;
179     struct dirent       *de;
180     char            *m_name;
181     int             ret;
182     
183     if (( dp = opendir( name)) == NULL ) {
184         return -1;
185     }
186     ret = 0;
187     for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
188         if (!(m_name = check_dirent(vol, de->d_name)))
189             continue;
190
191         ret++;
192         if (fn && fn(de,m_name, data) < 0) {
193            closedir(dp);
194            return -1;
195         }
196     }
197     closedir(dp);
198     return ret;
199 }
200
201 /* ----------------------------- */
202 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, is64 )
203 AFPObj       *obj;
204 char         *ibuf, *rbuf;
205 unsigned int ibuflen, *rbuflen;
206 int     is64;
207 {
208     static struct savedir       sd = { 0, 0, 0, NULL, NULL, 0 };
209     struct vol                  *vol;
210     struct dir                  *dir;
211     int                         did, ret, esz, len, first = 1;
212     char                        *data, *start;
213     u_int16_t                   vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
214     u_int16_t                   temp16;
215     u_int32_t                   sindex, maxsz, sz = 0;
216     struct path                 *o_path;
217     struct path                 s_path;
218     
219     if ( sd.sd_buflen == 0 ) {
220         if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
221             LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
222             *rbuflen = 0;
223             return AFPERR_MISC;
224         }
225         sd.sd_buflen = SDBUFBRK;
226     }
227
228     ibuf += 2;
229
230     memcpy( &vid, ibuf, sizeof( vid ));
231     ibuf += sizeof( vid );
232
233     if (( vol = getvolbyvid( vid )) == NULL ) {
234         *rbuflen = 0;
235         return( AFPERR_PARAM );
236     }
237
238     memcpy( &did, ibuf, sizeof( did ));
239     ibuf += sizeof( did );
240
241     if (( dir = dirlookup( vol, did )) == NULL ) {
242         *rbuflen = 0;
243         return( AFPERR_NODIR );
244     }
245
246     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
247     fbitmap = ntohs( fbitmap );
248     ibuf += sizeof( fbitmap );
249
250     memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
251     dbitmap = ntohs( dbitmap );
252     ibuf += sizeof( dbitmap );
253
254     /* check for proper bitmaps -- the stuff in comments is for
255      * variable directory ids. */
256     if (!(fbitmap || dbitmap)
257             /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
258               (dbitmap & (1 << DIRPBIT_PDID))*/) {
259         *rbuflen = 0;
260         return AFPERR_BITMAP;
261     }
262
263     memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
264     reqcnt = ntohs( reqcnt );
265     ibuf += sizeof( reqcnt );
266
267     if (is64) {
268         memcpy( &sindex, ibuf, sizeof( sindex ));
269         sindex = ntohs( sindex );
270         ibuf += sizeof( sindex );
271     }
272     else {
273         memcpy( &temp16, ibuf, sizeof( temp16 ));
274         sindex = ntohs( temp16 );
275         ibuf += sizeof( temp16 );
276     }
277     
278     if (is64) {
279         memcpy( &maxsz, ibuf, sizeof( maxsz ));
280         maxsz = ntohs( maxsz );
281         ibuf += sizeof( maxsz );
282     }
283     else {
284         memcpy( &temp16, ibuf, sizeof( temp16 ));
285         maxsz = ntohs( temp16 );
286         ibuf += sizeof( temp16 );
287     }
288     
289     maxsz = min(maxsz, *rbuflen);
290
291     if (( o_path = cname( vol, dir, &ibuf )) == NULL) {
292         *rbuflen = 0;
293         return( AFPERR_NODIR );
294     }
295
296     if ( *o_path->m_name != '\0') {
297         *rbuflen = 0;
298         return( AFPERR_BADTYPE );
299     }
300
301     data = rbuf + 3 * sizeof( u_int16_t );
302     sz = 3 * sizeof( u_int16_t );
303
304     /*
305      * Read the directory into a pre-malloced buffer, stored
306      *          len <name> \0
307      * The end is indicated by a len of 0.
308      */
309     if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
310         sd.sd_last = sd.sd_buf;
311         if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
312             switch (errno) {
313             case EACCES:
314                 return AFPERR_ACCESS;
315             case ENOTDIR:
316                 return AFPERR_BADTYPE;
317             case ENOMEM:
318                 return AFPERR_MISC;
319             default:
320                 return AFPERR_NODIR;
321             }
322         }
323         curdir->ctime  = o_path->st.st_ctime; /* play safe */
324         if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
325             *rbuflen = 0;
326             switch (errno) {
327             case EACCES:
328                 return AFPERR_ACCESS;
329             case ENOTDIR:
330                 return AFPERR_BADTYPE;
331             case ENOMEM:
332                 return AFPERR_MISC;
333             default:
334                 return AFPERR_NODIR;
335             }
336         }
337         curdir->offcnt = ret;
338         *sd.sd_last = 0;
339
340         sd.sd_last = sd.sd_buf;
341         sd.sd_sindex = 1;
342
343         sd.sd_vid = vid;
344         sd.sd_did = did;
345     }
346
347     /*
348      * Position sd_last as dictated by sindex.
349      */
350     if ( sindex < sd.sd_sindex ) {
351         sd.sd_sindex = 1;
352         sd.sd_last = sd.sd_buf;
353     }
354     while ( sd.sd_sindex < sindex ) {
355         len = *(sd.sd_last)++;
356         if ( len == 0 ) {
357             sd.sd_did = 0;      /* invalidate sd struct to force re-read */
358             *rbuflen = 0;
359             return( AFPERR_NOOBJ );
360         }
361         sd.sd_last += len + 1;
362         sd.sd_sindex++;
363     }
364
365     while (( len = *(sd.sd_last)) != 0 ) {
366         /*
367          * If we've got all we need, send it.
368          */
369         if ( actcnt == reqcnt ) {
370             break;
371         }
372
373         /*
374          * Save the start position, in case we exceed the buffer
375          * limitation, and have to back up one.
376          */
377         start = sd.sd_last;
378         sd.sd_last++;
379
380         if (*sd.sd_last == 0) {
381             /* stat() already failed on this one */
382             sd.sd_last += len + 1;
383             continue;
384         }
385         s_path.u_name = sd.sd_last;
386         if (of_stat( &s_path) < 0 ) {
387             /*
388              * Somebody else plays with the dir, well it can be us with 
389             * "Empty Trash..."
390             */
391
392             /* so the next time it won't try to stat it again
393              * another solution would be to invalidate the cache with 
394              * sd.sd_did = 0 but if it's not ENOENT error it will start again
395              */
396             *sd.sd_last = 0;
397             sd.sd_last += len + 1;
398             continue;
399         }
400
401         /*
402          * If a fil/dir is not a dir, it's a file. This is slightly
403          * inaccurate, since that means /dev/null is a file, /dev/printer
404          * is a file, etc.
405          */
406         if ( S_ISDIR(s_path.st.st_mode)) {
407             if ( dbitmap == 0 ) {
408                 sd.sd_last += len + 1;
409                 continue;
410             }
411             dir = curdir->d_child;
412             s_path.m_name = NULL;
413             while (dir) {
414                 if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
415                     break;
416                 }
417                 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
418             }
419             if (!dir) {
420                 s_path.m_name = utompath(vol, s_path.u_name);
421                 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
422                     *rbuflen = 0;
423                     return AFPERR_MISC;
424                 }
425             }
426
427             if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
428                                      data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
429                 *rbuflen = 0;
430                 return( ret );
431             }
432
433         } else {
434             if ( fbitmap == 0 ) {
435                 sd.sd_last += len + 1;
436                 continue;
437             }
438             s_path.m_name = utompath(vol, s_path.u_name);
439             if (( ret = getfilparams(vol, fbitmap, &s_path, curdir, 
440                                      data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
441                 *rbuflen = 0;
442                 return( ret );
443             }
444         }
445
446         /*
447          * Make sure entry is an even length, possibly with a null
448          * byte on the end.
449          */
450         if ( esz & 1 ) {
451             *(data + 2 * sizeof( u_char ) + esz ) = '\0';
452             esz++;
453         }
454
455         /*
456          * Check if we've exceeded the size limit.
457          */
458         if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
459             if (first) { /* maxsz can't hold a single reply */
460                 *rbuflen = 0;
461                 return AFPERR_PARAM;
462             }
463             sd.sd_last = start;
464             break;
465         }
466
467         if (first)
468             first = 0;
469
470         sz += esz + 2 * sizeof( u_char );
471         *data++ = esz + 2 * sizeof( u_char );
472         *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
473         data += esz;
474         actcnt++;
475         sd.sd_last += len + 1;
476     }
477
478     if ( actcnt == 0 ) {
479         *rbuflen = 0;
480         sd.sd_did = 0;          /* invalidate sd struct to force re-read */
481         return( AFPERR_NOOBJ );
482     }
483     sd.sd_sindex = sindex + actcnt;
484
485     /*
486      * All done, fill in misc junk in rbuf
487      */
488     fbitmap = htons( fbitmap );
489     memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
490     rbuf += sizeof( fbitmap );
491     dbitmap = htons( dbitmap );
492     memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
493     rbuf += sizeof( dbitmap );
494     actcnt = htons( actcnt );
495     memcpy( rbuf, &actcnt, sizeof( actcnt ));
496     rbuf += sizeof( actcnt );
497     *rbuflen = sz;
498     return( AFP_OK );
499 }
500
501 /* ----------------------------- */
502 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
503 AFPObj       *obj;
504 char         *ibuf, *rbuf;
505 unsigned int ibuflen, *rbuflen;
506 {
507     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
508 }
509
510 /* ----------------------------- */
511 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
512 AFPObj       *obj;
513 char         *ibuf, *rbuf;
514 unsigned int ibuflen, *rbuflen;
515 {
516     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
517 }
518