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