]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/catsearch.c
d85f25cbf995ea377c1f41a1ec92498173bbe2fb
[netatalk.git] / etc / afpd / catsearch.c
1 /* 
2  * Netatalk 2002 (c)
3  * Copyright (C) 1990, 1993 Regents of The University of Michigan
4  * All Rights Reserved. See COPYRIGHT
5  */
6
7
8 /*
9  * This file contains FPCatSearch implementation. FPCatSearch performs
10  * file/directory search based on specified criteria. It is used by client
11  * to perform fast searches on (propably) big volumes. So, it has to be
12  * pretty fast.
13  *
14  * This implementation bypasses most of adouble/libatalk stuff as long as
15  * possible and does a standard filesystem search. It calls higher-level
16  * libatalk/afpd functions only when it is really needed, mainly while
17  * returning some non-UNIX information or filtering by non-UNIX criteria.
18  *
19  * Initial version written by Rafal Lewczuk <rlewczuk@pronet.pl>
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif /* HAVE_CONFIG_H */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <time.h>
33
34 #if STDC_HEADERS
35 #include <string.h>
36 #else
37 #ifndef HAVE_MEMCPY
38 #define memcpy(d,s,n) bcopy ((s), (d), (n))
39 #define memmove(d,s,n) bcopy ((s), (d), (n))
40 #endif /* ! HAVE_MEMCPY */
41 #endif
42
43 #include <sys/file.h>
44 #include <netinet/in.h>
45
46 #include <atalk/afp.h>
47 #include <atalk/adouble.h>
48 #include <atalk/logger.h>
49 #ifdef CNID_DB
50 #include <atalk/cnid.h>
51 #endif /* CNID_DB */
52 #include "desktop.h"
53 #include "directory.h"
54 #include "dircache.h"
55 #include "file.h"
56 #include "volume.h"
57 #include "globals.h"
58 #include "filedir.h"
59 #include "fork.h"
60
61
62 struct finderinfo {
63         u_int32_t f_type;
64         u_int32_t creator;
65         u_int16_t attrs;    /* File attributes (high 8 bits)*/
66         u_int16_t label;    /* Label (low 8 bits )*/
67         char reserved[22]; /* Unknown (at least for now...) */
68 };
69
70 typedef char packed_finder[ADEDLEN_FINDERI];
71
72 /* Known attributes:
73  * 0x04 - has a custom icon
74  * 0x20 - name/icon is locked
75  * 0x40 - is invisible
76  * 0x80 - is alias
77  *
78  * Known labels:
79  * 0x02 - project 2
80  * 0x04 - project 1
81  * 0x06 - personal
82  * 0x08 - cool
83  * 0x0a - in progress
84  * 0x0c - hot
85  * 0x0e - essential
86  */
87
88 /* This is our search-criteria structure. */
89 struct scrit {
90         u_int32_t rbitmap;          /* Request bitmap - which values should we check ? */
91         u_int16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
92         u_int16_t attr;             /* File attributes */
93         time_t cdate;               /* Creation date */
94         time_t mdate;               /* Last modification date */
95         time_t bdate;               /* Last backup date */
96         u_int32_t pdid;             /* Parent DID */
97     u_int16_t offcnt;           /* Offspring count */
98         struct finderinfo finfo;    /* Finder info */
99         char lname[64];             /* Long name */ 
100         char utf8name[514];         /* UTF8 or UCS2 name */ /* for convert_charset dest_len parameter +2 */
101 };
102
103 /*
104  * Directory tree search is recursive by its nature. But AFP specification
105  * requires FPCatSearch to pause after returning n results and be able to
106  * resume the search later. So we have to do recursive search using flat
107  * (iterative) algorithm and remember all directories to look into in an
108  * stack-like structure. The structure below is one item of directory stack.
109  *
110  */
111 struct dsitem {
112         struct dir *dir; /* Structure describing this directory */
113         int pidx;        /* Parent's dsitem structure index. */
114         int checked;     /* Have we checked this directory ? */
115         int path_len;
116         char *path;      /* absolute UNIX path to this directory */
117 };
118  
119
120 #define DS_BSIZE 128
121 static u_int32_t cur_pos = 0;    /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
122 static DIR *dirpos = NULL; /* UNIX structure describing currently opened directory. */
123 static int save_cidx = -1; /* Saved index of currently scanned directory. */
124
125 static struct dsitem *dstack = NULL; /* Directory stack data... */
126 static int dssize = 0;               /* Directory stack (allocated) size... */
127 static int dsidx = 0;                /* First free item index... */
128
129 static struct scrit c1, c2;          /* search criteria */
130
131 /* Puts new item onto directory stack. */
132 static int addstack(char *uname, struct dir *dir, int pidx)
133 {
134         struct dsitem *ds;
135         size_t         l, u;
136
137         /* check if we have some space on stack... */
138         if (dsidx >= dssize) {
139                 dssize += DS_BSIZE;
140                 dstack = realloc(dstack, dssize * sizeof(struct dsitem));       
141                 if (dstack == NULL)
142                         return -1;
143         }
144
145         /* Put new element. Allocate and copy lname and path. */
146         ds = dstack + dsidx++;
147         ds->dir = dir;
148         ds->pidx = pidx;
149         ds->checked = 0;
150         if (pidx >= 0) {
151             l = dstack[pidx].path_len;
152             u = strlen(uname) +1;
153             if (!(ds->path = malloc(l + u + 1) ))
154                         return -1;
155                 memcpy(ds->path, dstack[pidx].path, l);
156                 ds->path[l] = '/';
157                 memcpy(&ds->path[l+1], uname, u);
158                 ds->path_len = l +u;
159         }
160         else {
161             ds->path = strdup(uname);
162                 ds->path_len = strlen(uname);
163         }
164         return 0;
165 }
166
167 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
168 static int reducestack(void)
169 {
170         int r;
171         if (save_cidx != -1) {
172                 r = save_cidx;
173                 save_cidx = -1;
174                 return r;
175         }
176
177         while (dsidx > 0) {
178                 if (dstack[dsidx-1].checked) {
179                         dsidx--;
180                         free(dstack[dsidx].path);
181                 } else
182                         return dsidx - 1;
183         } 
184         return -1;
185
186
187 /* Clears directory stack. */
188 static void clearstack(void) 
189 {
190         save_cidx = -1;
191         while (dsidx > 0) {
192                 dsidx--;
193                 free(dstack[dsidx].path);
194         }
195
196
197 /* Looks up for an opened adouble structure, opens resource fork of selected file. 
198  * FIXME What about noadouble?
199 */
200 static struct adouble *adl_lkup(struct vol *vol, struct path *path, struct adouble *adp)
201 {
202         static struct adouble ad;
203         
204         struct ofork *of;
205         int isdir;
206         
207         if (adp)
208             return adp;
209             
210         isdir  = S_ISDIR(path->st.st_mode);
211
212         if (!isdir && (of = of_findname(path))) {
213                 adp = of->of_ad;
214         } else {
215                 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
216                 adp = &ad;
217         } 
218
219     if ( ad_metadata( path->u_name, ((isdir) ? ADFLAGS_DIR : 0), adp) < 0 ) {
220         adp = NULL; /* FIXME without resource fork adl_lkup will be call again */
221     }
222     
223         return adp;     
224 }
225
226 /* -------------------- */
227 static struct finderinfo *unpack_buffer(struct finderinfo *finfo, char *buffer)
228 {
229         memcpy(&finfo->f_type,  buffer +FINDERINFO_FRTYPEOFF, sizeof(finfo->f_type));
230         memcpy(&finfo->creator, buffer +FINDERINFO_FRCREATOFF, sizeof(finfo->creator));
231         memcpy(&finfo->attrs,   buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->attrs));
232         memcpy(&finfo->label,   buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->label));
233         finfo->attrs &= 0xff00; /* high 8 bits */
234         finfo->label &= 0xff;   /* low 8 bits */
235
236         return finfo;
237 }
238
239 /* -------------------- */
240 static struct finderinfo *
241 unpack_finderinfo(struct vol *vol, struct path *path, struct adouble **adp, struct finderinfo *finfo)
242 {
243         packed_finder  buf;
244         void           *ptr;
245         
246     *adp = adl_lkup(vol, path, *adp);
247         ptr = get_finderinfo(vol, path->u_name, *adp, &buf);
248         return unpack_buffer(finfo, ptr);
249 }
250
251 /* -------------------- */
252 #define CATPBIT_PARTIAL 31
253 /* Criteria checker. This function returns a 2-bit value. */
254 /* bit 0 means if checked file meets given criteria. */
255 /* bit 1 means if it is a directory and we should descent into it. */
256 /* uname - UNIX name 
257  * fname - our fname (translated to UNIX)
258  * cidx - index in directory stack
259  */
260 static int crit_check(struct vol *vol, struct path *path) {
261         int result = 0;
262         u_int16_t attr, flags = CONV_PRECOMPOSE;
263         struct finderinfo *finfo = NULL, finderinfo;
264         struct adouble *adp = NULL;
265         time_t c_date, b_date;
266         u_int32_t ac_date, ab_date;
267         static char convbuf[514]; /* for convert_charset dest_len parameter +2 */
268         size_t len;
269
270         if (S_ISDIR(path->st.st_mode)) {
271                 if (!c1.dbitmap)
272                         return 0;
273         }
274         else {
275                 if (!c1.fbitmap)
276                         return 0;
277
278                 /* compute the Mac name 
279                  * first try without id (it's slow to find it)
280                  * An other option would be to call get_id in utompath but 
281                  * we need to pass parent dir
282                 */
283         if (!(path->m_name = utompath(vol, path->u_name, 0 , utf8_encoding()) )) {
284                 /*retry with the right id */
285        
286                 cnid_t id;
287                 
288                 adp = adl_lkup(vol, path, adp);
289                 id = get_id(vol, adp, &path->st, path->d_dir->d_did, path->u_name, strlen(path->u_name));
290                 if (!id) {
291                         /* FIXME */
292                         return 0;
293                 }
294                 /* save the id for getfilparm */
295                 path->id = id;
296                 if (!(path->m_name = utompath(vol, path->u_name, id , utf8_encoding()))) {
297                         return 0;
298                 }
299         }
300         }
301                 
302         /* Kind of optimization: 
303          * -- first check things we've already have - filename
304          * -- last check things we get from ad_open()
305          * FIXME strmcp strstr (icase)
306          */
307
308         /* Check for filename */
309         if ((c1.rbitmap & (1<<DIRPBIT_LNAME))) { 
310                 if ( (size_t)(-1) == (len = convert_string(vol->v_maccharset, CH_UCS2, path->m_name, -1, convbuf, 512)) )
311                         goto crit_check_ret;
312
313                 if ((c1.rbitmap & (1<<CATPBIT_PARTIAL))) {
314                         if (strcasestr_w( (ucs2_t*) convbuf, (ucs2_t*) c1.lname) == NULL)
315                                 goto crit_check_ret;
316                 } else
317                         if (strcasecmp_w((ucs2_t*) convbuf, (ucs2_t*) c1.lname) != 0)
318                                 goto crit_check_ret;
319         } 
320         
321         if ((c1.rbitmap & (1<<FILPBIT_PDINFO))) { 
322                 if ( (size_t)(-1) == (len = convert_charset( CH_UTF8_MAC, CH_UCS2, CH_UTF8, path->m_name, strlen(path->m_name), convbuf, 512, &flags))) {
323                         goto crit_check_ret;
324                 }
325
326                 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
327                         if (strcasestr_w((ucs2_t *) convbuf, (ucs2_t*)c1.utf8name) == NULL)
328                                 goto crit_check_ret;
329                 } else
330                         if (strcasecmp_w((ucs2_t *)convbuf, (ucs2_t*)c1.utf8name) != 0)
331                                 goto crit_check_ret;
332         } 
333
334
335         /* FIXME */
336         if ((unsigned)c2.mdate > 0x7fffffff)
337                 c2.mdate = 0x7fffffff;
338         if ((unsigned)c2.cdate > 0x7fffffff)
339                 c2.cdate = 0x7fffffff;
340         if ((unsigned)c2.bdate > 0x7fffffff)
341                 c2.bdate = 0x7fffffff;
342
343         /* Check for modification date */
344         if ((c1.rbitmap & (1<<DIRPBIT_MDATE))) {
345                 if (path->st.st_mtime < c1.mdate || path->st.st_mtime > c2.mdate)
346                         goto crit_check_ret;
347         }
348         
349         /* Check for creation date... */
350         if ((c1.rbitmap & (1<<DIRPBIT_CDATE))) {
351                 c_date = path->st.st_mtime;
352                 adp = adl_lkup(vol, path, adp);
353                 if (adp && ad_getdate(adp, AD_DATE_CREATE, &ac_date) >= 0)
354                     c_date = AD_DATE_TO_UNIX(ac_date);
355
356                 if (c_date < c1.cdate || c_date > c2.cdate)
357                         goto crit_check_ret;
358         }
359
360         /* Check for backup date... */
361         if ((c1.rbitmap & (1<<DIRPBIT_BDATE))) {
362                 b_date = path->st.st_mtime;
363                 adp = adl_lkup(vol, path, adp);
364                 if (adp && ad_getdate(adp, AD_DATE_BACKUP, &ab_date) >= 0)
365                         b_date = AD_DATE_TO_UNIX(ab_date);
366
367                 if (b_date < c1.bdate || b_date > c2.bdate)
368                         goto crit_check_ret;
369         }
370                                 
371         /* Check attributes */
372         if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0) {
373                 if ((adp = adl_lkup(vol, path, adp))) {
374                         ad_getattr(adp, &attr);
375                         if ((attr & c2.attr) != c1.attr)
376                                 goto crit_check_ret;
377                 } else 
378                         goto crit_check_ret;
379         }               
380
381         /* Check file type ID */
382         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0) {
383             finfo = unpack_finderinfo(vol, path, &adp, &finderinfo);
384                 if (finfo->f_type != c1.finfo.f_type)
385                         goto crit_check_ret;
386         }
387         
388         /* Check creator ID */
389         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0) {
390                 if (!finfo) {
391                         finfo = unpack_finderinfo(vol, path, &adp, &finderinfo);
392                 }
393                 if (finfo->creator != c1.finfo.creator)
394                         goto crit_check_ret;
395         }
396                 
397         /* Check finder info attributes */
398         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0) {
399                 if (!finfo) {
400                         finfo = unpack_finderinfo(vol, path, &adp, &finderinfo);
401                 }
402
403                 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
404                         goto crit_check_ret;
405         }
406         
407         /* Check label */
408         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0) {
409                 if (!finfo) {
410                         finfo = unpack_finderinfo(vol, path, &adp, &finderinfo);
411                 }
412                 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
413                         goto crit_check_ret;
414         }       
415         /* FIXME: Attributes check ! */
416         
417         /* All criteria are met. */
418         result |= 1;
419 crit_check_ret:
420         if (adp != NULL)
421                 ad_close_metadata(adp);
422         return result;
423 }  
424
425 /* ------------------------------ */
426 static int rslt_add ( struct vol *vol, struct path *path, char **buf, int ext)
427 {
428
429         char            *p = *buf;
430         int             ret;
431         size_t          tbuf =0;
432         u_int16_t       resultsize;
433         int             isdir = S_ISDIR(path->st.st_mode); 
434
435         /* Skip resultsize */
436         if (ext) {
437                 p += sizeof(resultsize); 
438         }
439         else {
440                 p++;
441         }
442         *p++ = isdir ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;    /* IsDir ? */
443
444         if (ext) {
445                 *p++ = 0;                  /* Pad */
446         }
447         
448         if ( isdir ) {
449         ret = getdirparams(vol, c1.dbitmap, path, path->d_dir, p , &tbuf ); 
450         }
451         else {
452             /* FIXME slow if we need the file ID, we already know it, done ? */
453                 ret = getfilparams ( vol, c1.fbitmap, path, path->d_dir, p, &tbuf);
454         }
455
456         if ( ret != AFP_OK )
457                 return 0;
458
459         /* Make sure entry length is even */
460         if ((tbuf & 1)) {
461            *p++ = 0;
462            tbuf++;
463         }
464
465         if (ext) {
466                 resultsize = htons(tbuf);
467                 memcpy ( *buf, &resultsize, sizeof(resultsize) );
468                 *buf += tbuf + 4;
469         }
470         else {
471                 **buf = tbuf;
472                 *buf += tbuf + 2;
473         }
474
475         return 1;
476
477         
478 #define VETO_STR \
479         "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
480
481 /* This function performs search. It is called directly from afp_catsearch 
482  * vol - volume we are searching on ...
483  * dir - directory we are starting from ...
484  * c1, c2 - search criteria
485  * rmatches - maximum number of matches we can return
486  * pos - position we've stopped recently
487  * rbuf - output buffer
488  * rbuflen - output buffer length
489  */
490 #define NUM_ROUNDS 200
491 static int catsearch(struct vol *vol, struct dir *dir,  
492                      int rmatches, u_int32_t *pos, char *rbuf, u_int32_t *nrecs, int *rsize, int ext)
493 {
494         int cidx, r;
495         struct dirent *entry;
496         int result = AFP_OK;
497         int ccr;
498     struct path path;
499         char *vpath = vol->v_path;
500         char *rrbuf = rbuf;
501     time_t start_time;
502     int num_rounds = NUM_ROUNDS;
503     int cached;
504     int cwd = -1;
505         
506         if (*pos != 0 && *pos != cur_pos) {
507                 result = AFPERR_CATCHNG;
508                 goto catsearch_end;
509         }
510
511         /* FIXME: Category "offspring count ! */
512
513         /* So we are beginning... */
514     start_time = time(NULL);
515
516         /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
517         if (*pos == 0) {
518                 clearstack();
519                 if (dirpos != NULL) {
520                         closedir(dirpos);
521                         dirpos = NULL;
522                 } 
523                 
524                 if (addstack(vpath, dir, -1) == -1) {
525                         result = AFPERR_MISC;
526                         goto catsearch_end;
527                 }
528                 /* FIXME: Sometimes DID is given by client ! (correct this one above !) */
529         }
530
531         /* Save current path */
532     if ((cwd = open(".", O_RDONLY)) < 0) {
533         result = AFPERR_MISC;
534         goto catsearch_end;
535     }
536         
537         while ((cidx = reducestack()) != -1) {
538                 cached = 1;
539                 if (dirpos == NULL) {
540                         dirpos = opendir(dstack[cidx].path);
541 // FIXME dircache rewrite
542 //                      cached = (dstack[cidx].dir->d_child != NULL);
543                 }
544                 if (dirpos == NULL) {
545                         switch (errno) {
546                         case EACCES:
547                                 dstack[cidx].checked = 1;
548                                 continue;
549                         case EMFILE:
550                         case ENFILE:
551                         case ENOENT:
552                                 result = AFPERR_NFILE;
553                                 break;
554                         case ENOMEM:
555                         case ENOTDIR:
556                         default:
557                                 result = AFPERR_MISC;
558                         } /* switch (errno) */
559                         goto catsearch_end;
560                 }
561                 /* FIXME error in chdir, what do we do? */
562                 chdir(dstack[cidx].path);
563                 
564
565                 while ((entry=readdir(dirpos)) != NULL) {
566                         (*pos)++;
567
568                         if (!check_dirent(vol, entry->d_name))
569                            continue;
570
571                         memset(&path, 0, sizeof(path));
572                         path.u_name = entry->d_name;
573                         if (of_stat(&path) != 0) {
574                                 switch (errno) {
575                                 case EACCES:
576                                 case ELOOP:
577                                 case ENOENT:
578                                         continue;
579                                 case ENOTDIR:
580                                 case EFAULT:
581                                 case ENOMEM:
582                                 case ENAMETOOLONG:
583                                 default:
584                                         result = AFPERR_MISC;
585                                         goto catsearch_end;
586                                 } 
587                         }
588                         if (S_ISDIR(path.st.st_mode)) {
589                                 /* here we can short cut 
590                                    ie if in the same loop the parent dir wasn't in the cache
591                                    ALL dirsearch_byname will fail.
592                                 */
593                 int unlen = strlen(path.u_name);
594                                 if (cached)
595                         path.d_dir = dircache_search_by_name(vol, dstack[cidx].dir, path.u_name, unlen);
596                 else
597                         path.d_dir = NULL;
598                 if (!path.d_dir) {
599                         /* path.m_name is set by adddir */
600                     if (NULL == (path.d_dir = dir_add( vol, dstack[cidx].dir, &path, unlen) ) ) {
601                                                 result = AFPERR_MISC;
602                                                 goto catsearch_end;
603                                         }
604                 }
605                 path.m_name = (char *)path.d_dir->d_m_name->data;
606                         
607                                 if (addstack(path.u_name, path.d_dir, cidx) == -1) {
608                                         result = AFPERR_MISC;
609                                         goto catsearch_end;
610                                 } 
611             }
612             else {
613                 /* yes it sucks for directory d_dir is the directory, for file it's the parent directory*/
614                 path.d_dir = dstack[cidx].dir;
615             }
616                         ccr = crit_check(vol, &path);
617
618                         /* bit 0 means that criteria has been met */
619                         if ((ccr & 1)) {
620                                 r = rslt_add ( vol, &path, &rrbuf, ext);
621                                 
622                                 if (r == 0) {
623                                         result = AFPERR_MISC;
624                                         goto catsearch_end;
625                                 } 
626                                 *nrecs += r;
627                                 /* Number of matches limit */
628                                 if (--rmatches == 0) 
629                                         goto catsearch_pause;
630                                 /* Block size limit */
631                                 if (rrbuf - rbuf >= 448)
632                                         goto catsearch_pause;
633                         }
634                         /* MacOS 9 doesn't like servers executing commands longer than few seconds */
635                         if (--num_rounds <= 0) {
636                             if (start_time != time(NULL)) {
637                                         result=AFP_OK;
638                                         goto catsearch_pause;
639                             }
640                             num_rounds = NUM_ROUNDS;
641                         }
642                 } /* while ((entry=readdir(dirpos)) != NULL) */
643                 closedir(dirpos);
644                 dirpos = NULL;
645                 dstack[cidx].checked = 1;
646         } /* while (current_idx = reducestack()) != -1) */
647
648         /* We have finished traversing our tree. Return EOF here. */
649         result = AFPERR_EOF;
650         goto catsearch_end;
651
652 catsearch_pause:
653         cur_pos = *pos; 
654         save_cidx = cidx;
655
656 catsearch_end: /* Exiting catsearch: error condition */
657         *rsize = rrbuf - rbuf;
658     if (cwd != -1) {
659         if ((fchdir(cwd)) != 0) {
660             LOG(log_debug, logtype_afpd, "error chdiring back: %s", strerror(errno));        
661         }
662         close(cwd);
663     }
664         return result;
665 } /* catsearch() */
666
667 /* -------------------------- */
668 static int catsearch_afp(AFPObj *obj _U_, char *ibuf, size_t ibuflen,
669                   char *rbuf, size_t *rbuflen, int ext)
670 {
671     struct vol *vol;
672     u_int16_t   vid;
673     u_int16_t   spec_len;
674     u_int32_t   rmatches, reserved;
675     u_int32_t   catpos[4];
676     u_int32_t   pdid = 0;
677     int ret, rsize;
678     u_int32_t nrecs = 0;
679     unsigned char *spec1, *spec2, *bspec1, *bspec2;
680     size_t      len;
681     u_int16_t   namelen;
682     u_int16_t   flags;
683     char        tmppath[256];
684
685     *rbuflen = 0;
686
687     /* min header size */
688     if (ibuflen < 32) {
689         return AFPERR_PARAM;
690     }
691
692     memset(&c1, 0, sizeof(c1));
693     memset(&c2, 0, sizeof(c2));
694
695     ibuf += 2;
696     memcpy(&vid, ibuf, sizeof(vid));
697     ibuf += sizeof(vid);
698
699     if ((vol = getvolbyvid(vid)) == NULL) {
700         return AFPERR_PARAM;
701     }
702     
703     memcpy(&rmatches, ibuf, sizeof(rmatches));
704     rmatches = ntohl(rmatches);
705     ibuf += sizeof(rmatches); 
706
707     /* FIXME: (rl) should we check if reserved == 0 ? */
708     ibuf += sizeof(reserved);
709
710     memcpy(catpos, ibuf, sizeof(catpos));
711     ibuf += sizeof(catpos);
712
713     memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
714     c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
715     ibuf += sizeof(c1.fbitmap);
716
717     memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
718     c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
719     ibuf += sizeof(c1.dbitmap);
720
721     memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
722     c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
723     ibuf += sizeof(c1.rbitmap);
724
725     if (! (c1.fbitmap || c1.dbitmap)) {
726             return AFPERR_BITMAP;
727     }
728
729     if ( ext) {
730         memcpy(&spec_len, ibuf, sizeof(spec_len));
731         spec_len = ntohs(spec_len);
732     }
733     else {
734         /* with catsearch only name and parent id are allowed */
735         c1.fbitmap &= (1<<FILPBIT_LNAME) | (1<<FILPBIT_PDID);
736         c1.dbitmap &= (1<<DIRPBIT_LNAME) | (1<<DIRPBIT_PDID);
737         spec_len = *(unsigned char*)ibuf;
738     }
739
740     /* Parse file specifications */
741     spec1 = (unsigned char*)ibuf;
742     spec2 = (unsigned char*)ibuf + spec_len + 2;
743
744     spec1 += 2; 
745     spec2 += 2; 
746
747     bspec1 = spec1;
748     bspec2 = spec2;
749     /* File attribute bits... */
750     if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
751             memcpy(&c1.attr, spec1, sizeof(c1.attr));
752             spec1 += sizeof(c1.attr);
753             memcpy(&c2.attr, spec2, sizeof(c2.attr));
754             spec2 += sizeof(c1.attr);
755     }
756
757     /* Parent DID */
758     if (c1.rbitmap & (1 << FILPBIT_PDID)) {
759             memcpy(&c1.pdid, spec1, sizeof(pdid));
760             spec1 += sizeof(c1.pdid);
761             memcpy(&c2.pdid, spec2, sizeof(pdid));
762             spec2 += sizeof(c2.pdid);
763     } /* FIXME: PDID - do we demarshall this argument ? */
764
765     /* Creation date */
766     if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
767             memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
768             spec1 += sizeof(c1.cdate);
769             c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
770             memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
771             spec2 += sizeof(c1.cdate);
772             ibuf += sizeof(c1.cdate);;
773             c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
774     }
775
776     /* Modification date */
777     if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
778             memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
779             c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
780             spec1 += sizeof(c1.mdate);
781             memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
782             c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
783             spec2 += sizeof(c1.mdate);
784     }
785     
786     /* Backup date */
787     if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
788             memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
789             spec1 += sizeof(c1.bdate);
790             c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
791             memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
792             spec2 += sizeof(c2.bdate);
793             c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
794     }
795
796     /* Finder info */
797     if (c1.rbitmap & (1 << FILPBIT_FINFO)) {
798         packed_finder buf;
799         
800             memcpy(buf, spec1, sizeof(buf));
801             unpack_buffer(&c1.finfo, buf);      
802             spec1 += sizeof(buf);
803
804             memcpy(buf, spec2, sizeof(buf));
805             unpack_buffer(&c2.finfo, buf);
806             spec2 += sizeof(buf);
807     } /* Finder info */
808
809     if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
810         /* Offspring count - only directories */
811                 if (c1.fbitmap == 0) {
812                 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
813                 spec1 += sizeof(c1.offcnt);
814                 c1.offcnt = ntohs(c1.offcnt);
815                 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
816                 spec2 += sizeof(c2.offcnt);
817                 c2.offcnt = ntohs(c2.offcnt);
818                 }
819                 else if (c1.dbitmap == 0) {
820                         /* ressource fork length */
821                 }
822                 else {
823                 return AFPERR_BITMAP;  /* error */
824                 }
825     } /* Offspring count/ressource fork length */
826
827     /* Long name */
828     if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
829         /* Get the long filename */     
830                 memcpy(tmppath, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
831                 tmppath[(bspec1 + spec1[1])[0]]= 0;
832                 len = convert_string ( vol->v_maccharset, CH_UCS2, tmppath, -1, c1.lname, sizeof(c1.lname));
833         if (len == (size_t)(-1))
834             return AFPERR_PARAM;
835
836 #if 0   
837                 /* FIXME: do we need it ? It's always null ! */
838                 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
839                 c2.lname[(bspec2 + spec2[1])[0]]= 0;
840 #endif
841     }
842         /* UTF8 Name */
843     if (c1.rbitmap & (1 << FILPBIT_PDINFO)) {
844
845                 /* offset */
846                 memcpy(&namelen, spec1, sizeof(namelen));
847                 namelen = ntohs (namelen);
848
849                 spec1 = bspec1+namelen+4; /* Skip Unicode Hint */
850
851                 /* length */
852                 memcpy(&namelen, spec1, sizeof(namelen));
853                 namelen = ntohs (namelen);
854                 if (namelen > 255)  /* Safeguard */
855                         namelen = 255;
856
857                 memcpy (c1.utf8name, spec1+2, namelen);
858                 c1.utf8name[(namelen+1)] =0;
859
860                 /* convert charset */
861                 flags = CONV_PRECOMPOSE;
862                 len = convert_charset(CH_UTF8_MAC, CH_UCS2, CH_UTF8, c1.utf8name, namelen, c1.utf8name, 512, &flags);
863         if (len == (size_t)(-1))
864             return AFPERR_PARAM;
865     }
866     
867     /* Call search */
868     *rbuflen = 24;
869     ret = catsearch(vol, vol->v_root, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize, ext);
870     memcpy(rbuf, catpos, sizeof(catpos));
871     rbuf += sizeof(catpos);
872
873     c1.fbitmap = htons(c1.fbitmap);
874     memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
875     rbuf += sizeof(c1.fbitmap);
876
877     c1.dbitmap = htons(c1.dbitmap);
878     memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
879     rbuf += sizeof(c1.dbitmap);
880
881     nrecs = htonl(nrecs);
882     memcpy(rbuf, &nrecs, sizeof(nrecs));
883     rbuf += sizeof(nrecs);
884     *rbuflen += rsize;
885
886     return ret;
887 } /* catsearch_afp */
888
889 /* -------------------------- */
890 int afp_catsearch (AFPObj *obj, char *ibuf, size_t ibuflen,
891                   char *rbuf, size_t *rbuflen)
892 {
893         return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 0);
894 }
895
896
897 int afp_catsearch_ext (AFPObj *obj, char *ibuf, size_t ibuflen,
898                   char *rbuf, size_t *rbuflen)
899 {
900         return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 1);
901 }
902
903 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
904 /* (so, all buffer packing/unpacking should be done in stub, everything else 
905    should be done in other functions) */