]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/catsearch.c
73dd30a8f0f8ae24e2e955cbd9a015c7d08426c2
[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 <dirent.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <time.h>
34
35 #if STDC_HEADERS
36 #include <string.h>
37 #else
38 #ifndef HAVE_MEMCPY
39 #define memcpy(d,s,n) bcopy ((s), (d), (n))
40 #define memmove(d,s,n) bcopy ((s), (d), (n))
41 #endif /* ! HAVE_MEMCPY */
42 #endif
43
44 #include <sys/file.h>
45 #include <netinet/in.h>
46
47 #include <atalk/afp.h>
48 #include <atalk/adouble.h>
49 #include <atalk/logger.h>
50 #ifdef CNID_DB
51 #include <atalk/cnid.h>
52 #endif /* CNID_DB */
53 #include "desktop.h"
54 #include "directory.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[512];         /* UTF8 name */
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         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         int           l;
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         if (pidx >= 0) {
149             l = strlen(dstack[pidx].path);
150             if (!(ds->path = malloc(l + strlen(uname) + 2) ))
151                         return -1;
152                 strcpy(ds->path, dstack[pidx].path);
153                 strcat(ds->path, "/");
154                 strcat(ds->path, uname);
155         }
156
157         ds->checked = 0;
158
159         return 0;
160 }
161
162 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
163 static int reducestack()
164 {
165         int r;
166         if (save_cidx != -1) {
167                 r = save_cidx;
168                 save_cidx = -1;
169                 return r;
170         }
171
172         while (dsidx > 0) {
173                 if (dstack[dsidx-1].checked) {
174                         dsidx--;
175                         free(dstack[dsidx].path);
176                 } else
177                         return dsidx - 1;
178         } 
179         return -1;
180
181
182 /* Clears directory stack. */
183 static void clearstack() 
184 {
185         save_cidx = -1;
186         while (dsidx > 0) {
187                 dsidx--;
188                 free(dstack[dsidx].path);
189         }
190
191
192 /* Looks up for an opened adouble structure, opens resource fork of selected file. 
193  * FIXME What about noadouble?
194 */
195 static struct adouble *adl_lkup(struct vol *vol, struct path *path, struct adouble *adp)
196 {
197         static struct adouble ad;
198         
199         struct ofork *of;
200         int isdir;
201         
202         if (adp)
203             return adp;
204             
205         isdir  = S_ISDIR(path->st.st_mode);
206
207         if (!isdir && (of = of_findname(path))) {
208                 adp = of->of_ad;
209         } else {
210                 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
211                 adp = &ad;
212         } 
213
214     if ( ad_metadata( path->u_name, ((isdir)?ADFLAGS_DIR:0), adp) < 0 ) {
215         adp = NULL; /* FIXME without resource fork adl_lkup will be call again */
216     }
217     
218         return adp;     
219 }
220
221 /* -------------------- */
222 static struct finderinfo *unpack_buffer(struct finderinfo *finfo, char *buffer)
223 {
224         memcpy(&finfo->f_type,  buffer +FINDERINFO_FRTYPEOFF, sizeof(finfo->f_type));
225         memcpy(&finfo->creator, buffer +FINDERINFO_FRCREATOFF, sizeof(finfo->creator));
226         memcpy(&finfo->attrs,   buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->attrs));
227         memcpy(&finfo->label,   buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->label));
228         finfo->attrs &= 0xff00; /* high 8 bits */
229         finfo->label &= 0xff;   /* low 8 bits */
230
231         return finfo;
232 }
233
234 /* -------------------- */
235 static struct finderinfo *
236 unpack_finderinfo(char *upath, struct adouble *adp, struct finderinfo *finfo)
237 {
238         packed_finder  buf;
239         void           *ptr;
240         
241         ptr = get_finderinfo(upath, 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[512];
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, strlen(path->m_name), convbuf, 512)) )
305                         goto crit_check_ret;
306                 convbuf[len] = 0; 
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                 convbuf[len] = 0; 
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                 adp = adl_lkup(vol, path, adp);
378             finfo = unpack_finderinfo(path->u_name, adp, &finderinfo);
379                 if (finfo->f_type != c1.finfo.f_type)
380                         goto crit_check_ret;
381         }
382         
383         /* Check creator ID */
384         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0) {
385                 if (!finfo) {
386                 adp = adl_lkup(vol, path, adp);
387                         finfo = unpack_finderinfo(path->u_name, adp, &finderinfo);
388                 }
389                 if (finfo->creator != c1.finfo.creator)
390                         goto crit_check_ret;
391         }
392                 
393         /* Check finder info attributes */
394         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0) {
395                 if (!finfo) {
396                 adp = adl_lkup(vol, path, adp);
397                         finfo = unpack_finderinfo(path->u_name, adp, &finderinfo);
398                 }
399
400                 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
401                         goto crit_check_ret;
402         }
403         
404         /* Check label */
405         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0) {
406                 if (!finfo) {
407                 adp = adl_lkup(vol, path, adp);
408                         finfo = unpack_finderinfo(path->u_name, adp, &finderinfo);
409                 }
410                 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
411                         goto crit_check_ret;
412         }       
413         /* FIXME: Attributes check ! */
414         
415         /* All criteria are met. */
416         result |= 1;
417 crit_check_ret:
418         if (adp != NULL)
419                 ad_close(adp, ADFLAGS_HF);
420         return result;
421 }  
422
423 /* ------------------------------ */
424 static int rslt_add ( struct vol *vol, struct path *path, char **buf, int ext)
425 {
426
427         char            *p = *buf;
428         int             ret, tbuf =0;
429         u_int16_t       resultsize;
430         int             isdir = S_ISDIR(path->st.st_mode); 
431
432         /* Skip resultsize */
433         if (ext) {
434                 p += sizeof(resultsize); 
435         }
436         else {
437                 p++;
438         }
439         *p++ = isdir ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;    /* IsDir ? */
440
441         if (ext) {
442                 *p++ = 0;                  /* Pad */
443         }
444         
445         if ( isdir ) {
446         ret = getdirparams(vol, c1.dbitmap, path, path->d_dir, p , &tbuf ); 
447         }
448         else {
449             /* FIXME slow if we need the file ID, we already know it, done ? */
450                 ret = getfilparams ( vol, c1.fbitmap, path, path->d_dir, p, &tbuf);
451         }
452
453         if ( ret != AFP_OK )
454                 return 0;
455
456         /* Make sure entry length is even */
457         if ((tbuf & 1)) {
458            *p++ = 0;
459            tbuf++;
460         }
461
462         if (ext) {
463                 resultsize = htons(tbuf);
464                 memcpy ( *buf, &resultsize, sizeof(resultsize) );
465                 *buf += tbuf + 4;
466         }
467         else {
468                 **buf = tbuf;
469                 *buf += tbuf + 2;
470         }
471
472         return 1;
473
474         
475 #define VETO_STR \
476         "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
477
478 /* This function performs search. It is called directly from afp_catsearch 
479  * vol - volume we are searching on ...
480  * dir - directory we are starting from ...
481  * c1, c2 - search criteria
482  * rmatches - maximum number of matches we can return
483  * pos - position we've stopped recently
484  * rbuf - output buffer
485  * rbuflen - output buffer length
486  */
487 #define NUM_ROUNDS 100
488 static int catsearch(struct vol *vol, struct dir *dir,  
489                      int rmatches, u_int32_t *pos, char *rbuf, u_int32_t *nrecs, int *rsize, int ext)
490 {
491         int cidx, r;
492         struct dirent *entry;
493         int result = AFP_OK;
494         int ccr;
495     struct path path;
496         char *orig_dir = NULL;
497         int orig_dir_len = 128;
498         char *vpath = vol->v_path;
499         char *rrbuf = rbuf;
500     time_t start_time;
501     int num_rounds = NUM_ROUNDS;
502     int cached;
503         
504         if (*pos != 0 && *pos != cur_pos) {
505                 result = AFPERR_CATCHNG;
506                 goto catsearch_end;
507         }
508
509         /* FIXME: Category "offspring count ! */
510
511         /* So we are beginning... */
512     start_time = time(NULL);
513
514         /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
515         if (*pos == 0) {
516                 clearstack();
517                 if (dirpos != NULL) {
518                         closedir(dirpos);
519                         dirpos = NULL;
520                 } 
521                 
522                 if (addstack("", dir, -1) == -1) {
523                         result = AFPERR_MISC;
524                         goto catsearch_end;
525                 }
526                 dstack[0].path = strdup(vpath);
527                 /* FIXME: Sometimes DID is given by client ! (correct this one above !) */
528         }
529
530         /* Save current path */
531         orig_dir = (char*)malloc(orig_dir_len);
532         while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
533                 if (errno != ERANGE) {
534                         result = AFPERR_MISC;
535                         goto catsearch_end;
536                 }
537                 orig_dir_len += 128; 
538                 orig_dir = realloc(orig_dir, orig_dir_len);
539         } /* while() */
540         
541         while ((cidx = reducestack()) != -1) {
542                 cached = 1;
543                 if (dirpos == NULL) {
544                         dirpos = opendir(dstack[cidx].path);
545                         cached = (dstack[cidx].dir->d_child != NULL);
546                 }
547                 if (dirpos == NULL) {
548                         switch (errno) {
549                         case EACCES:
550                                 dstack[cidx].checked = 1;
551                                 continue;
552                         case EMFILE:
553                         case ENFILE:
554                         case ENOENT:
555                                 result = AFPERR_NFILE;
556                                 break;
557                         case ENOMEM:
558                         case ENOTDIR:
559                         default:
560                                 result = AFPERR_MISC;
561                         } /* switch (errno) */
562                         goto catsearch_end;
563                 }
564                 /* FIXME error in chdir, what do we do? */
565                 chdir(dstack[cidx].path);
566                 
567
568                 while ((entry=readdir(dirpos)) != NULL) {
569                         (*pos)++;
570
571                         if (!check_dirent(vol, entry->d_name))
572                            continue;
573
574                         memset(&path, 0, sizeof(path));
575                         path.u_name = entry->d_name;
576                         if (of_stat(&path) != 0) {
577                                 switch (errno) {
578                                 case EACCES:
579                                 case ELOOP:
580                                 case ENOENT:
581                                         continue;
582                                 case ENOTDIR:
583                                 case EFAULT:
584                                 case ENOMEM:
585                                 case ENAMETOOLONG:
586                                 default:
587                                         result = AFPERR_MISC;
588                                         goto catsearch_end;
589                                 } 
590                         }
591                         if (S_ISDIR(path.st.st_mode)) {
592                                 /* here we can short cut 
593                                    ie if in the same loop the parent dir wasn't in the cache
594                                    ALL dirsearch_byname will fail.
595                                 */
596                                 if (cached)
597                         path.d_dir = dirsearch_byname(vol, dstack[cidx].dir, path.u_name);
598                 else
599                         path.d_dir = NULL;
600                 if (!path.d_dir) {
601                         /* path.m_name is set by adddir */
602                     if (NULL == (path.d_dir = adddir( vol, dstack[cidx].dir, &path) ) ) {
603                                                 result = AFPERR_MISC;
604                                                 goto catsearch_end;
605                                         }
606                 }
607                 path.m_name = path.d_dir->d_m_name; 
608                         
609                                 if (addstack(path.u_name, path.d_dir, cidx) == -1) {
610                                         result = AFPERR_MISC;
611                                         goto catsearch_end;
612                                 } 
613             }
614             else {
615                 /* yes it sucks for directory d_dir is the directory, for file it's the parent directory*/
616                 path.d_dir = dstack[cidx].dir;
617             }
618                         ccr = crit_check(vol, &path);
619
620                         /* bit 0 means that criteria has been met */
621                         if ((ccr & 1)) {
622                                 r = rslt_add ( vol, &path, &rrbuf, ext);
623                                 
624                                 if (r == 0) {
625                                         result = AFPERR_MISC;
626                                         goto catsearch_end;
627                                 } 
628                                 *nrecs += r;
629                                 /* Number of matches limit */
630                                 if (--rmatches == 0) 
631                                         goto catsearch_pause;
632                                 /* Block size limit */
633                                 if (rrbuf - rbuf >= 448)
634                                         goto catsearch_pause;
635                         }
636                         /* MacOS 9 doesn't like servers executing commands longer than few seconds */
637                         if (--num_rounds <= 0) {
638                             if (start_time != time(NULL)) {
639                                         result=AFP_OK;
640                                         goto catsearch_pause;
641                             }
642                             num_rounds = NUM_ROUNDS;
643                         }
644                 } /* while ((entry=readdir(dirpos)) != NULL) */
645                 closedir(dirpos);
646                 dirpos = NULL;
647                 dstack[cidx].checked = 1;
648         } /* while (current_idx = reducestack()) != -1) */
649
650         /* We have finished traversing our tree. Return EOF here. */
651         result = AFPERR_EOF;
652         goto catsearch_end;
653
654 catsearch_pause:
655         cur_pos = *pos; 
656         save_cidx = cidx;
657
658 catsearch_end: /* Exiting catsearch: error condition */
659         *rsize = rrbuf - rbuf;
660         if (orig_dir != NULL) {
661                 chdir(orig_dir);
662                 free(orig_dir);
663         }
664         return result;
665 } /* catsearch() */
666
667 /* -------------------------- */
668 int catsearch_afp(AFPObj *obj _U_, char *ibuf, int ibuflen,
669                   char *rbuf, int *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 = ibuf;
742     spec2 = 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, strlen(tmppath), c1.lname, 64);
833         if (len == (size_t)(-1))
834             return AFPERR_PARAM;
835                 c1.lname[len] = 0;
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                 c1.utf8name[len]=0;
867     }
868     
869     /* Call search */
870     *rbuflen = 24;
871     ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize, ext);
872     memcpy(rbuf, catpos, sizeof(catpos));
873     rbuf += sizeof(catpos);
874
875     c1.fbitmap = htons(c1.fbitmap);
876     memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
877     rbuf += sizeof(c1.fbitmap);
878
879     c1.dbitmap = htons(c1.dbitmap);
880     memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
881     rbuf += sizeof(c1.dbitmap);
882
883     nrecs = htonl(nrecs);
884     memcpy(rbuf, &nrecs, sizeof(nrecs));
885     rbuf += sizeof(nrecs);
886     *rbuflen += rsize;
887
888     return ret;
889 } /* catsearch_afp */
890
891 /* -------------------------- */
892 int afp_catsearch (AFPObj *obj, char *ibuf, int ibuflen,
893                   char *rbuf, int *rbuflen)
894 {
895         return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 0);
896 }
897
898
899 int afp_catsearch_ext (AFPObj *obj, char *ibuf, int ibuflen,
900                   char *rbuf, int *rbuflen)
901 {
902         return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 1);
903 }
904
905 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
906 /* (so, all buffer packing/unpacking should be done in stub, everything else 
907    should be done in other functions) */