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