]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/enumerate.c
removed unneeded cnid_lookups; fixed a bug in afp_createid.
[netatalk.git] / etc / afpd / enumerate.c
1 /*
2  * $Id: enumerate.c,v 1.8 2001-08-27 15:26:16 uhees 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 <dirent.h>
16 #include <errno.h>
17
18 #include <sys/syslog.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/file.h>
22 #include <sys/param.h>
23
24 #include <netatalk/endian.h>
25 #include <atalk/afp.h>
26 #include <atalk/adouble.h>
27 #ifdef CNID_DB
28 #include <atalk/cnid.h>
29 #endif /* CNID_DB */
30 #include "desktop.h"
31 #include "directory.h"
32 #include "volume.h"
33 #include "globals.h"
34 #include "file.h"
35
36 /* check for mtab DID code */
37 #ifdef DID_MTAB
38 #include "parse_mtab.h"
39 #endif /* DID_MTAB */
40
41 struct dir *
42 adddir( vol, dir, name, namlen, upath, upathlen, st )
43     struct vol  *vol;
44     struct dir  *dir;
45     char        *name, *upath;
46     int         namlen, upathlen;
47     struct stat *st;
48 {
49     struct dir  *cdir, *edir;
50 #if AD_VERSION > AD_VERSION1
51     struct adouble ad;
52 #endif /* AD_VERSION > AD_VERSION1 */
53
54 #ifndef USE_LASTDID
55     struct stat lst, *lstp;
56 #endif /* USE_LASTDID */
57
58     if ((cdir = dirnew(namlen + 1)) == NULL) {
59         syslog( LOG_ERR, "adddir: malloc: %s", strerror(errno) );
60         return NULL;
61     }
62     strcpy( cdir->d_name, name );
63     cdir->d_name[namlen] = '\0';
64
65     cdir->d_did = 0;
66
67 #if AD_VERSION > AD_VERSION1
68     /* look in AD v2 header */
69     memset(&ad, 0, sizeof(ad));
70     if (ad_open(upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY, 0, &ad) >= 0) {
71         /* if we can parse the AppleDouble header, retrieve the DID entry into cdir->d_did */
72             memcpy(&cdir->d_did, ad_entry(&ad, ADEID_DID), sizeof(cdir->d_did));
73             ad_close(&ad, ADFLAGS_HF);
74         }
75 #endif /* AD_VERSION */
76
77 #ifdef CNID_DB
78         /* add to cnid db */
79     cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
80                                    upathlen, cdir->d_did);
81 #endif /* CNID_DB */
82
83         if (cdir->d_did == 0) {
84 #ifdef USE_LASTDID
85         /* last way of doing DIDs */
86         cdir->d_did = htonl( vol->v_lastdid++ );
87 #else /* USE_LASTDID */
88         lstp = lstat(upath, &lst) < 0 ? st : &lst;
89 #ifdef DID_MTAB
90         /* mtab way of doing DIDs */
91         cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
92 #else /* DID_MTAB */
93         /* the old way of doing DIDs (default) */
94         cdir->d_did = htonl( CNID(lstp, 0) );
95 #endif /* DID_MTAB */
96 #endif /* USE_LASTDID */
97     }
98
99     if ((edir = dirinsert( vol, cdir ))) {
100             if (edir->d_name) {
101                     if (strcmp(edir->d_name, cdir->d_name)) {
102                             syslog(LOG_INFO, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
103                     }
104                     free(cdir->d_name);
105                     free(cdir);
106                     return edir;
107             }
108             edir->d_name = cdir->d_name;
109             free(cdir);
110             cdir = edir;
111     }
112
113     /* parent/child directories */
114     cdir->d_parent = dir;
115     dirchildadd(dir, cdir);
116     return( cdir );
117 }
118
119 /*
120  * Struct to save directory reading context in. Used to prevent
121  * O(n^2) searches on a directory.
122  */
123 struct savedir {
124     u_short     sd_vid;
125     int         sd_did;
126     int         sd_buflen;
127     char        *sd_buf;
128     char        *sd_last;
129     int         sd_sindex;
130 };
131 #define SDBUFBRK        1024
132
133 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
134     AFPObj      *obj;
135     char        *ibuf, *rbuf;
136     int         ibuflen, *rbuflen;
137 {
138     struct stat                 st;
139     static struct savedir       sd = { 0, 0, 0, NULL, NULL, 0 };
140     struct vol                  *vol;
141     struct dir                  *dir;
142     struct dirent               *de;
143     DIR                         *dp;
144     int                         did, ret, esz, len, first = 1;
145     char                        *path, *data, *end, *start;
146     u_int16_t                   vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
147     u_int16_t                   sindex, maxsz, sz = 0;
148
149     if ( sd.sd_buflen == 0 ) {
150         if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
151             syslog( LOG_ERR, "afp_enumerate: malloc: %s", strerror(errno) );
152             *rbuflen = 0;
153             return AFPERR_MISC;
154         }
155         sd.sd_buflen = SDBUFBRK;
156     }
157
158     ibuf += 2;
159
160     memcpy( &vid, ibuf, sizeof( vid ));
161     ibuf += sizeof( vid );
162
163     if (( vol = getvolbyvid( vid )) == NULL ) {
164         *rbuflen = 0;
165         return( AFPERR_PARAM );
166     }
167
168     memcpy( &did, ibuf, sizeof( did ));
169     ibuf += sizeof( did );
170
171     if (( dir = dirsearch( vol, did )) == NULL ) {
172         *rbuflen = 0;
173         return( AFPERR_NODIR );
174     }
175
176     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
177     fbitmap = ntohs( fbitmap );
178     ibuf += sizeof( fbitmap );
179
180     memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
181     dbitmap = ntohs( dbitmap );
182     ibuf += sizeof( dbitmap );
183
184     /* check for proper bitmaps -- the stuff in comments is for
185      * variable directory ids. */
186     if (!(fbitmap || dbitmap) 
187         /*|| (fbitmap & (1 << FILPBIT_PDID)) || 
188           (dbitmap & (1 << DIRPBIT_PDID))*/) {
189       *rbuflen = 0;
190       return AFPERR_BITMAP;
191     }
192
193     memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
194     reqcnt = ntohs( reqcnt );
195     ibuf += sizeof( reqcnt );
196
197     memcpy( &sindex, ibuf, sizeof( sindex ));
198     sindex = ntohs( sindex );
199     ibuf += sizeof( sindex );
200
201     memcpy( &maxsz, ibuf, sizeof( maxsz ));
202     maxsz = ntohs( maxsz );
203     ibuf += sizeof( maxsz );
204
205     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
206         *rbuflen = 0;
207         return( AFPERR_NODIR );
208     }
209     data = rbuf + 3 * sizeof( u_int16_t );
210     sz = 3 * sizeof( u_int16_t );
211
212     /*
213      * Read the directory into a pre-malloced buffer, stored
214      *          len <name> \0
215      * The end is indicated by a len of 0.
216      */
217     if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
218         sd.sd_last = sd.sd_buf;
219
220         if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
221             *rbuflen = 0;
222             return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
223         }
224
225         end = sd.sd_buf + sd.sd_buflen;
226         for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
227             if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
228               continue;
229
230             if (!(validupath(vol, de->d_name)))
231               continue;
232
233             /* now check against too big a file */
234             if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
235               continue;
236
237             len = strlen(de->d_name);
238             *(sd.sd_last)++ = len;
239
240             if ( sd.sd_last + len + 2 > end ) {
241                 char *buf;
242
243                 start = sd.sd_buf;
244                 if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen + 
245                                              SDBUFBRK )) == NULL ) {
246                     syslog( LOG_ERR, "afp_enumerate: realloc: %s",
247                             strerror(errno) );
248                     closedir(dp);
249                     *rbuflen = 0;
250                     return AFPERR_MISC;
251                 }
252                 sd.sd_buf = buf;
253                 sd.sd_buflen += SDBUFBRK;
254                 sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
255                 end = sd.sd_buf + sd.sd_buflen;
256             }
257
258             memcpy( sd.sd_last, de->d_name, len + 1 );
259             sd.sd_last += len + 1;
260         }
261         *sd.sd_last = 0;
262
263         sd.sd_last = sd.sd_buf;
264         sd.sd_sindex = 1;
265
266         closedir( dp );
267         sd.sd_vid = vid;
268         sd.sd_did = did;
269     }
270
271     /*
272      * Position sd_last as dictated by sindex.
273      */
274     if ( sindex < sd.sd_sindex ) {
275         sd.sd_sindex = 1;
276         sd.sd_last = sd.sd_buf;
277     }
278     while ( sd.sd_sindex < sindex ) {
279         len = *(sd.sd_last)++;
280         if ( len == 0 ) {
281             sd.sd_did = -1;     /* invalidate sd struct to force re-read */
282             *rbuflen = 0;
283             return( AFPERR_NOOBJ );
284         }
285         sd.sd_last += len + 1;
286         sd.sd_sindex++;
287     }
288
289     while (( len = *(sd.sd_last)) != 0 ) {
290         /*
291          * If we've got all we need, send it.
292          */
293         if ( actcnt == reqcnt ) {
294             break;
295         }
296
297         /*
298          * Save the start position, in case we exceed the buffer
299          * limitation, and have to back up one.
300          */
301         start = sd.sd_last;
302         sd.sd_last++;
303
304         if ( stat( sd.sd_last, &st ) < 0 ) {
305             syslog( LOG_DEBUG, "afp_enumerate: stat %s: %s",
306                     sd.sd_last, strerror(errno) );
307             sd.sd_last += len + 1;
308             continue;
309         }
310
311         /*
312          * If a fil/dir is not a dir, it's a file. This is slightly
313          * inaccurate, since that means /dev/null is a file, /dev/printer
314          * is a file, etc.
315          */
316         if ( S_ISDIR(st.st_mode)) {
317             if ( dbitmap == 0 ) {
318                 sd.sd_last += len + 1;
319                 continue;
320             }
321             path = utompath(vol, sd.sd_last);
322             dir = curdir->d_child; 
323             while (dir) {
324                 if ( strcmp( dir->d_name, path ) == 0 ) {
325                     break;
326                 }
327                 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
328             }
329             if (!dir && ((dir = adddir( vol, curdir, path, strlen( path ),
330                                         sd.sd_last, len, &st)) == NULL)) {
331               *rbuflen = 0;
332               return AFPERR_MISC;
333             }
334               
335
336             if (( ret = getdirparams(vol, dbitmap, sd.sd_last, dir,
337                     &st, data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
338                 *rbuflen = 0;
339                 return( ret );
340             }
341
342         } else {
343             if ( fbitmap == 0 ) {
344                 sd.sd_last += len + 1;
345                 continue;
346             }
347             
348             if (( ret = getfilparams(vol, fbitmap, utompath(vol, sd.sd_last),
349                     curdir, &st, data + 2 * sizeof( u_char ), &esz )) !=
350                     AFP_OK ) {
351                 *rbuflen = 0;
352                 return( ret );
353             }
354         }
355
356         /*
357          * Make sure entry is an even length, possibly with a null
358          * byte on the end.
359          */
360         if ( esz & 1 ) {
361             *(data + 2 * sizeof( u_char ) + esz ) = '\0';
362             esz++;
363         }
364
365         /*
366          * Check if we've exceeded the size limit.
367          */
368         if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
369             if (first) { /* maxsz can't hold a single reply */
370               *rbuflen = 0; 
371               return AFPERR_PARAM;
372             }
373             sd.sd_last = start;
374             break;
375         }
376
377         if (first)
378           first = 0;
379
380         sz += esz + 2 * sizeof( u_char );
381         *data++ = esz + 2 * sizeof( u_char );
382         *data++ = S_ISDIR(st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
383         data += esz;
384         actcnt++;
385         sd.sd_last += len + 1;
386     }
387
388     if ( actcnt == 0 ) {
389         *rbuflen = 0;
390         sd.sd_did = -1;         /* invalidate sd struct to force re-read */
391         return( AFPERR_NOOBJ );
392     }
393     sd.sd_sindex = sindex + actcnt;
394
395     /*
396      * All done, fill in misc junk in rbuf
397      */
398     fbitmap = htons( fbitmap );
399     memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
400     rbuf += sizeof( fbitmap );
401     dbitmap = htons( dbitmap );
402     memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
403     rbuf += sizeof( dbitmap );
404     actcnt = htons( actcnt );
405     memcpy( rbuf, &actcnt, sizeof( actcnt ));
406     rbuf += sizeof( actcnt );
407     *rbuflen = sz;
408     return( AFP_OK );
409 }
410
411
412 /* why is this here? well, FPCatSearch is essentially an FPEnumerate
413  * with filters. */
414 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen, 
415                   char *rbuf, int *rbuflen)
416 {
417     struct vol *vol;
418     u_int16_t   vid;
419
420     ibuf += 2;
421     memcpy(&vid, ibuf, sizeof(vid));
422     ibuf += sizeof(vid);
423
424     *rbuflen = 0;
425     if ((vol = getvolbyvid(vid)) == NULL)
426        return AFPERR_PARAM;
427
428     /* the ritual:
429      * do a breadth-first search of directories:
430      *   lookup did/name info.
431      *   add to result if match
432      *   check to see if we've exceeded our timelimit
433      *     if yes, return current position
434      *     if not, continue
435      * 
436      *   we keep a copy of our current position in struct vol.
437      *   if the next catsearch request for that volume isn't at
438      *   at the current position, bail and return catchanged.
439      */
440
441     /* eof when done */
442     return AFPERR_EOF;
443 }