]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/directory.c
Fix catsearch
[netatalk.git] / etc / afpd / directory.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <grp.h>
14 #include <pwd.h>
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <errno.h>
18 #include <utime.h>
19 #include <assert.h>
20
21 #include <atalk/adouble.h>
22 #include <atalk/vfs.h>
23 #include <atalk/afp.h>
24 #include <atalk/util.h>
25 #include <atalk/cnid.h>
26 #include <atalk/logger.h>
27 #include <atalk/uuid.h>
28 #include <atalk/unix.h>
29 #include <atalk/bstrlib.h>
30 #include <atalk/bstradd.h>
31 #include <atalk/errchk.h>
32 #include <atalk/globals.h>
33 #include <atalk/fce_api.h>
34
35 #include "directory.h"
36 #include "dircache.h"
37 #include "desktop.h"
38 #include "volume.h"
39 #include "fork.h"
40 #include "file.h"
41 #include "filedir.h"
42 #include "unix.h"
43 #include "mangle.h"
44 #include "hash.h"
45
46 /*
47  * FIXMEs, loose ends after the dircache rewrite:
48  * o merge dircache_search_by_name and dir_add ??
49  * o case-insensitivity is gone from cname
50  */
51
52
53 /*******************************************************************************************
54  * Globals
55  ******************************************************************************************/
56
57 int         afp_errno;
58 /* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
59 struct dir rootParent  = {
60     NULL, NULL, NULL, NULL,          /* path, d_m_name, d_u_name, d_m_name_ucs2 */
61     NULL, 0, 0,                      /* qidx_node, ctime, d_flags */
62     0, 0, 0, 0                       /* pdid, did, offcnt, d_vid */
63 };
64 struct dir  *curdir = &rootParent;
65 struct path Cur_Path = {
66     0,
67     "",  /* mac name */
68     ".", /* unix name */
69     0,   /* id */
70     NULL,/* struct dir * */
71     0,   /* stat is not set */
72     0,   /* errno */
73     {0} /* struct stat */
74 };
75
76 /*
77  * dir_remove queues struct dirs to be freed here. We can't just delete them immeidately
78  * eg in dircache_search_by_id, because a caller somewhere up the stack might be
79  * referencing it.
80  * So instead:
81  * - we mark it as invalid by setting d_did to CNID_INVALID (ie 0)
82  * - queue it in "invalid_dircache_entries" queue
83  * - which is finally freed at the end of every AFP func in afp_dsi.c.
84  */
85 q_t *invalid_dircache_entries;
86
87
88 /*******************************************************************************************
89  * Locals
90  ******************************************************************************************/
91
92
93 /* -------------------------
94    appledouble mkdir afp error code.
95 */
96 static int netatalk_mkdir(const struct vol *vol, const char *name)
97 {
98     int ret;
99     struct stat st;
100
101     if (vol->v_flags & AFPVOL_UNIX_PRIV) {
102         if (lstat(".", &st) < 0)
103             return AFPERR_MISC;
104         int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
105         LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
106             name, st.st_mode, vol->v_umask);
107
108         ret = mkdir(name, mode);
109     } else {
110         ret = ad_mkdir(name, DIRBITS | 0777);
111     }
112
113     if (ret < 0) {
114         switch ( errno ) {
115         case ENOENT :
116             return( AFPERR_NOOBJ );
117         case EROFS :
118             return( AFPERR_VLOCK );
119         case EPERM:
120         case EACCES :
121             return( AFPERR_ACCESS );
122         case EEXIST :
123             return( AFPERR_EXIST );
124         case ENOSPC :
125         case EDQUOT :
126             return( AFPERR_DFULL );
127         default :
128             return( AFPERR_PARAM );
129         }
130     }
131     return AFP_OK;
132 }
133
134 /* ------------------- */
135 static int deletedir(int dirfd, char *dir)
136 {
137     char path[MAXPATHLEN + 1];
138     DIR *dp;
139     struct dirent   *de;
140     struct stat st;
141     size_t len;
142     int err = AFP_OK;
143     size_t remain;
144
145     if ((len = strlen(dir)) +2 > sizeof(path))
146         return AFPERR_PARAM;
147
148     /* already gone */
149     if ((dp = opendirat(dirfd, dir)) == NULL)
150         return AFP_OK;
151
152     strcpy(path, dir);
153     strcat(path, "/");
154     len++;
155     remain = sizeof(path) -len -1;
156     while ((de = readdir(dp)) && err == AFP_OK) {
157         /* skip this and previous directory */
158         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
159             continue;
160
161         if (strlen(de->d_name) > remain) {
162             err = AFPERR_PARAM;
163             break;
164         }
165         strcpy(path + len, de->d_name);
166         if (lstatat(dirfd, path, &st)) {
167             continue;
168         }
169         if (S_ISDIR(st.st_mode)) {
170             err = deletedir(dirfd, path);
171         } else {
172             err = netatalk_unlinkat(dirfd, path);
173         }
174     }
175     closedir(dp);
176
177     /* okay. the directory is empty. delete it. note: we already got rid
178        of .AppleDouble.  */
179     if (err == AFP_OK) {
180         err = netatalk_rmdir(dirfd, dir);
181     }
182     return err;
183 }
184
185 /* do a recursive copy. */
186 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
187 {
188     char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
189     DIR *dp;
190     struct dirent   *de;
191     struct stat st;
192     struct utimbuf      ut;
193     size_t slen, dlen;
194     size_t srem, drem;
195     int err;
196
197     /* doesn't exist or the path is too long. */
198     if (((slen = strlen(src)) > sizeof(spath) - 2) ||
199         ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
200         ((dp = opendirat(dirfd, src)) == NULL))
201         return AFPERR_PARAM;
202
203     /* try to create the destination directory */
204     if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
205         closedir(dp);
206         return err;
207     }
208
209     /* set things up to copy */
210     strcpy(spath, src);
211     strcat(spath, "/");
212     slen++;
213     srem = sizeof(spath) - slen -1;
214
215     strcpy(dpath, dst);
216     strcat(dpath, "/");
217     dlen++;
218     drem = sizeof(dpath) - dlen -1;
219
220     err = AFP_OK;
221     while ((de = readdir(dp))) {
222         /* skip this and previous directory */
223         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
224             continue;
225
226         if (strlen(de->d_name) > srem) {
227             err = AFPERR_PARAM;
228             break;
229         }
230         strcpy(spath + slen, de->d_name);
231
232         if (lstatat(dirfd, spath, &st) == 0) {
233             if (strlen(de->d_name) > drem) {
234                 err = AFPERR_PARAM;
235                 break;
236             }
237             strcpy(dpath + dlen, de->d_name);
238
239             if (S_ISDIR(st.st_mode)) {
240                 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
241                     goto copydir_done;
242             } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
243                 goto copydir_done;
244
245             } else {
246                 /* keep the same time stamp. */
247                 ut.actime = ut.modtime = st.st_mtime;
248                 utime(dpath, &ut);
249             }
250         }
251     }
252
253     /* keep the same time stamp. */
254     if (lstatat(dirfd, src, &st) == 0) {
255         ut.actime = ut.modtime = st.st_mtime;
256         utime(dst, &ut);
257     }
258
259 copydir_done:
260     closedir(dp);
261     return err;
262 }
263
264 /* ---------------------
265  * is our cached offspring count valid?
266  */
267 static int diroffcnt(struct dir *dir, struct stat *st)
268 {
269     return st->st_ctime == dir->d_ctime;
270 }
271
272 /* --------------------- */
273 static int invisible_dots(const struct vol *vol, const char *name)
274 {
275     return vol_inv_dots(vol) && *name  == '.' && strcmp(name, ".") && strcmp(name, "..");
276 }
277
278 /* ------------------ */
279 static int set_dir_errors(struct path *path, const char *where, int err)
280 {
281     switch ( err ) {
282     case EPERM :
283     case EACCES :
284         return AFPERR_ACCESS;
285     case EROFS :
286         return AFPERR_VLOCK;
287     }
288     LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
289     return AFPERR_PARAM;
290 }
291
292 /*!
293  * @brief Convert name in client encoding to server encoding
294  *
295  * Convert ret->m_name to ret->u_name from client encoding to server encoding.
296  * This only gets called from cname().
297  *
298  * @returns 0 on success, -1 on error
299  *
300  * @note If the passed ret->m_name is mangled, we'll demangle it
301  */
302 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
303 {
304     static char temp[ MAXPATHLEN + 1];
305     char *t;
306     cnid_t fileid = 0;
307
308     if (afp_version >= 30) {
309         if (toUTF8) {
310             if (dir->d_did == DIRDID_ROOT_PARENT) {
311                 /*
312                  * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
313                  * So we compare it with the longname from the current volume and if they match
314                  * we overwrite the requested path with the utf8 volume name so that the following
315                  * strcmp can match.
316                  */
317                 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
318                 if (strcasecmp(ret->m_name, temp) == 0)
319                     ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
320             } else {
321                 /* toUTF8 */
322                 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
323                     afp_errno = AFPERR_PARAM;
324                     return -1;
325                 }
326                 strcpy(ret->m_name, temp);
327             }
328         }
329
330         /* check for OS X mangled filename :( */
331         t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
332         LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
333             ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
334
335         if (t != ret->m_name) {
336             ret->u_name = t;
337             /* duplicate work but we can't reuse all convert_char we did in demangle_osx
338              * flags weren't the same
339              */
340             if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
341                 /* at last got our view of mac name */
342                 strcpy(ret->m_name, t);
343             }
344         }
345     } /* afp_version >= 30 */
346
347     /* If we haven't got it by now, get it */
348     if (ret->u_name == NULL) {
349         if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
350             afp_errno = AFPERR_PARAM;
351             return -1;
352         }
353     }
354
355     return 0;
356 }
357
358 /*!
359  * @brief Build struct path from struct dir
360  *
361  * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
362  * 1. move cwd into parent dir (we're often already there, but not always)
363  * 2. set struct path to the dirname
364  * 3. in case of
365  *    AFPERR_ACCESS: the dir is there, we just cant chdir into it
366  *    AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
367  *                  4. indicate there's no dir for this path
368  *                  5. remove the dir
369  */
370 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
371 {
372     if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
373         return NULL;
374
375     switch (afp_errno) {
376
377     case AFPERR_ACCESS:
378         if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
379             return NULL;
380
381         memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
382         if (dir->d_m_name == dir->d_u_name) {
383             ret->u_name = ret->m_name;
384         } else {
385             ret->u_name =  ret->m_name + blength(dir->d_m_name) + 1;
386             memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
387         }
388
389         ret->d_dir = dir;
390
391         LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
392             cfrombstr(dir->d_fullpath),
393             cfrombstr(curdir->d_fullpath),
394             ret->u_name);
395
396         return ret;
397
398     case AFPERR_NOOBJ:
399         if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
400             return NULL;
401
402         memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
403         if (dir->d_m_name == dir->d_u_name) {
404             ret->u_name = ret->m_name;
405         } else {
406             ret->u_name =  ret->m_name + blength(dir->d_m_name) + 1;
407             memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
408         }
409
410         ret->d_dir = NULL;      /* 4 */
411         dir_remove(vol, dir);   /* 5 */
412         return ret;
413
414     default:
415         return NULL;
416     }
417
418     /* DEADC0DE: never get here */
419     return NULL;
420 }
421
422
423 /*********************************************************************************************
424  * Interface
425  ********************************************************************************************/
426
427 int get_afp_errno(const int param)
428 {
429     if (afp_errno != AFPERR_DID1)
430         return afp_errno;
431     return param;
432 }
433
434 /*!
435  * Resolve struct dir for an absolute path
436  *
437  * Given a path like "/Volumes/volume/dir/subdir" in a volume "/Volumes/volume" return
438  * a pointer to struct dir of "subdir".
439  * 1. Remove volue path from absolute path
440  * 2. start path
441  * 3. loop through all elements of the remaining path from 1.
442  * 4. we only allow dirs
443  * 5. search dircache
444  * 6. if not found in the dircache query the CNID database for the DID
445  * 7. and use dirlookup to resolve the DID to a it's struct dir *
446  *
447  * @param vol   (r) volume the path is in, must be known
448  * @param path  (r) absoule path
449  *
450  * @returns pointer to struct dir or NULL on error
451  */
452 struct dir *dirlookup_bypath(const struct vol *vol, const char *path)
453 {
454     EC_INIT;
455
456     struct dir *dir = NULL;
457     cnid_t cnid, did;
458     bstring rpath = NULL;
459     bstring statpath = NULL;
460     struct bstrList *l = NULL;
461     struct stat st;
462
463     cnid = htonl(2);
464     dir = vol->v_root;
465
466     LOG(log_debug, logtype_afpd, "dirlookup_bypath(\"%s\")", path);
467
468     if (strcmp(vol->v_path, path) == 0)
469         return dir;
470
471     EC_NULL(rpath = rel_path_in_vol(path, vol->v_path)); /* 1. */
472
473     LOG(log_debug, logtype_afpd, "dirlookup_bypath: rpath: \"%s\"", cfrombstr(rpath));
474
475     EC_NULL(statpath = bfromcstr(vol->v_path));          /* 2. */
476
477     l = bsplit(rpath, '/');
478     for (int i = 0; i < l->qty ; i++) {                  /* 3. */
479         did = cnid;
480         EC_ZERO(bcatcstr(statpath, "/"));
481         EC_ZERO(bconcat(statpath, l->entry[i]));
482
483         LOG(log_debug, logtype_afpd, "dirlookup_bypath: statpath: \"%s\"", cfrombstr(statpath));
484
485         EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
486                        "lstat(rpath: %s, elem: %s): %s: %s",
487                        cfrombstr(rpath), cfrombstr(l->entry[i]),
488                        cfrombstr(statpath), strerror(errno));
489
490         if (!(S_ISDIR(st.st_mode)))                      /* 4. */
491             EC_FAIL;
492
493         if ((dir = dircache_search_by_name(vol,          /* 5. */
494                                            dir,
495                                            cfrombstr(l->entry[i]),
496                                            blength(l->entry[i]))) == NULL) {
497
498             if ((cnid = cnid_add(vol->v_cdb,             /* 6. */
499                                  &st,
500                                  did,
501                                  cfrombstr(l->entry[i]),
502                                  blength(l->entry[i]),
503                                  0)) == CNID_INVALID)
504                 EC_FAIL;
505
506             if ((dir = dirlookup(vol, cnid)) == NULL) /* 7. */
507                 EC_FAIL;
508         }
509     }
510
511 EC_CLEANUP:
512     bdestroy(rpath);
513     bstrListDestroy(l);
514     bdestroy(statpath);
515     if (ret != 0)
516         return NULL;
517
518     LOG(log_debug, logtype_afpd, "dirlookup_bypath: result: \"%s\"",
519         cfrombstr(dir->d_fullpath));
520
521     return dir;
522 }
523
524 /*!
525  * @brief Resolve a DID
526  *
527  * Resolve a DID, allocate a struct dir for it
528  * 1. Check for special CNIDs 0 (invalid), 1 and 2.
529  * 2a. Check if the DID is in the cache.
530  * 2b. Check if it's really a dir  because we cache files too.
531  * 3. If it's not in the cache resolve it via the database.
532  * 4. Build complete server-side path to the dir.
533  * 5. Check if it exists and is a directory.
534  * 6. Create the struct dir and populate it.
535  * 7. Add it to the cache.
536  *
537  * @param vol   (r) pointer to struct vol
538  * @param did   (r) DID to resolve
539  *
540  * @returns pointer to struct dir
541  */
542 struct dir *dirlookup(const struct vol *vol, cnid_t did)
543 {
544     static char  buffer[12 + MAXPATHLEN + 1];
545     struct stat  st;
546     struct dir   *ret = NULL, *pdir;
547     bstring      fullpath = NULL;
548     char         *upath = NULL, *mpath;
549     cnid_t       cnid, pdid;
550     size_t       maxpath;
551     int          buflen = 12 + MAXPATHLEN + 1;
552     int          utf8;
553     int          err = 0;
554
555     LOG(log_debug, logtype_afpd, "dirlookup(did: %u): START", ntohl(did));
556
557     /* check for did 0, 1 and 2 */
558     if (did == 0 || vol == NULL) { /* 1 */
559         afp_errno = AFPERR_PARAM;
560         ret = NULL;
561         goto exit;
562     } else if (did == DIRDID_ROOT_PARENT) {
563         rootParent.d_vid = vol->v_vid;
564         ret = &rootParent;
565         goto exit;
566     } else if (did == DIRDID_ROOT) {
567         ret = vol->v_root;
568         goto exit;
569     }
570
571     /* Search the cache */
572     if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
573         if (ret->d_flags & DIRF_ISFILE) {                   /* 2b */
574             afp_errno = AFPERR_BADTYPE;
575             ret = NULL;
576             goto exit;
577         }
578         if (lstat(cfrombstr(ret->d_fullpath), &st) != 0) {
579             LOG(log_debug, logtype_afpd, "dirlookup(did: %u, path: \"%s\"): lstat: %s",
580                 ntohl(did), cfrombstr(ret->d_fullpath), strerror(errno));
581             switch (errno) {
582             case ENOENT:
583             case ENOTDIR:
584                 /* It's not there anymore, so remove it */
585                 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): calling dir_remove", ntohl(did));
586                 dir_remove(vol, ret);
587                 afp_errno = AFPERR_NOOBJ;
588                 ret = NULL;
589                 goto exit;
590             default:
591                 ret = ret;
592                 goto exit;
593             }
594             /* DEADC0DE */
595             ret = NULL;
596             goto exit;            
597         }
598         ret = ret;
599         goto exit;
600     }
601
602     utf8 = utf8_encoding();
603     maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
604
605     /* Get it from the database */
606     cnid = did;
607     LOG(log_debug, logtype_afpd, "dirlookup(did: %u): querying CNID database", ntohl(did));
608     if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL) {
609         afp_errno = AFPERR_NOOBJ;
610         err = 1;
611         goto exit;
612     }
613     if ((upath = strdup(upath)) == NULL) { /* 3 */
614         afp_errno = AFPERR_NOOBJ;
615         err = 1;
616         goto exit;
617     }
618     pdid = cnid;
619
620     /*
621      * Recurse up the tree, terminates in dirlookup when either
622      * - DIRDID_ROOT is hit
623      * - a cached entry is found
624      */
625     LOG(log_debug, logtype_afpd, "dirlookup(did: %u): recursion for did: %u",
626         ntohl(did), ntohl(pdid));
627     if ((pdir = dirlookup(vol, pdid)) == NULL) {
628         err = 1;
629         goto exit;
630     }
631
632     /* build the fullpath */
633     if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
634         || bconchar(fullpath, '/') != BSTR_OK
635         || bcatcstr(fullpath, upath) != BSTR_OK) {
636         err = 1;
637         goto exit;
638     }
639
640     /* stat it and check if it's a dir */
641     LOG(log_debug, logtype_afpd, "dirlookup(did: %u): stating \"%s\"",
642         ntohl(did), cfrombstr(fullpath));
643
644     if (lstat(cfrombstr(fullpath), &st) != 0) { /* 5a */
645         switch (errno) {
646         case ENOENT:
647             afp_errno = AFPERR_NOOBJ;
648             err = 1;
649             goto exit;
650         case EPERM:
651             afp_errno = AFPERR_ACCESS;
652             err = 1;
653             goto exit;
654         default:
655             afp_errno = AFPERR_MISC;
656             err = 1;
657             goto exit;
658         }
659     } else {
660         if ( ! S_ISDIR(st.st_mode)) { /* 5b */
661             afp_errno = AFPERR_BADTYPE;
662             err = 1;
663             goto exit;
664         }
665     }
666
667     /* Get macname from unix name */
668     if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
669         afp_errno = AFPERR_NOOBJ;
670         err = 1;
671         goto exit;
672     }
673
674     /* Create struct dir */
675     if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath, &st)) == NULL) { /* 6 */
676         LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
677         err = 1;
678         goto exit;
679     }
680     
681     /* Add it to the cache only if it's a dir */
682     if (dircache_add(vol, ret) != 0) { /* 7 */
683         err = 1;
684         goto exit;
685     }
686
687 exit:
688     if (upath) free(upath);
689     if (err) {
690         LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
691             ntohl(did), AfpErr2name(afp_errno));
692         if (fullpath)
693             bdestroy(fullpath);
694         if (ret) {
695             dir_free(ret);
696             ret = NULL;
697         }
698     }
699     if (ret)
700         LOG(log_debug, logtype_afpd, "dirlookup(did: %u): RESULT: pdid: %u, path: \"%s\"",
701             ntohl(ret->d_did), ntohl(ret->d_pdid), cfrombstr(ret->d_fullpath));
702
703     return ret;
704 }
705
706 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
707
708 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
709 {
710     DIR               *dp;
711     struct dirent     *de;
712     int               ret;
713     static u_int32_t  did = 0;
714     static char       cname[MAXPATHLEN];
715     static char       lname[MAXPATHLEN];
716     ucs2_t        u2_path[MAXPATHLEN];
717     ucs2_t        u2_dename[MAXPATHLEN];
718     char          *tmp, *savepath;
719
720     if (!(vol->v_flags & AFPVOL_CASEINSEN))
721         return -1;
722
723     if (veto_file(ENUMVETO, path->u_name))
724         return -1;
725
726     savepath = path->u_name;
727
728     /* very simple cache */
729     if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
730         path->u_name = cname;
731         path->d_dir = NULL;
732         if (of_stat( path ) == 0 ) {
733             return 0;
734         }
735         /* something changed, we cannot stat ... */
736         did = 0;
737     }
738
739     if (NULL == ( dp = opendir( "." )) ) {
740         LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
741         return -1;
742     }
743
744
745     /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
746     if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
747         LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
748
749     /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
750     ret = -1;
751     for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
752         if (NULL == check_dirent(vol, de->d_name))
753             continue;
754
755         if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
756             continue;
757
758         if (strcasecmp_w( u2_path, u2_dename) == 0) {
759             tmp = path->u_name;
760             strlcpy(cname, de->d_name, sizeof(cname));
761             path->u_name = cname;
762             path->d_dir = NULL;
763             if (of_stat( path ) == 0 ) {
764                 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
765                 strlcpy(lname, tmp, sizeof(lname));
766                 did = dir->d_did;
767                 ret = 0;
768                 break;
769             }
770             else
771                 path->u_name = tmp;
772         }
773
774     }
775     closedir(dp);
776
777     if (ret) {
778         /* invalidate cache */
779         cname[0] = 0;
780         did = 0;
781         path->u_name = savepath;
782     }
783     /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
784     return ret;
785 }
786
787
788 /*!
789  * @brief Construct struct dir
790  *
791  * Construct struct dir from parameters.
792  *
793  * @param m_name   (r) directory name in UTF8-dec
794  * @param u_name   (r) directory name in server side encoding
795  * @param vol      (r) pointer to struct vol
796  * @param pdid     (r) Parent CNID
797  * @param did      (r) CNID
798  * @param path     (r) Full unix path to object
799  * @param st       (r) struct stat of object
800  *
801  * @returns pointer to new struct dir or NULL on error
802  *
803  * @note Most of the time mac name and unix name are the same.
804  */
805 struct dir *dir_new(const char *m_name,
806                     const char *u_name,
807                     const struct vol *vol,
808                     cnid_t pdid,
809                     cnid_t did,
810                     bstring path,
811                     struct stat *st)
812 {
813     struct dir *dir;
814
815     dir = (struct dir *) calloc(1, sizeof( struct dir ));
816     if (!dir)
817         return NULL;
818
819     if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
820         free(dir);
821         return NULL;
822     }
823
824     if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
825                                  CH_UCS2,
826                                  m_name,
827                                  -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
828         LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
829         dir->d_m_name_ucs2 = NULL;
830     }
831
832     if (m_name == u_name || !strcmp(m_name, u_name)) {
833         dir->d_u_name = dir->d_m_name;
834     }
835     else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
836         bdestroy(dir->d_m_name);
837         free(dir);
838         return NULL;
839     }
840
841     dir->d_did = did;
842     dir->d_pdid = pdid;
843     dir->d_vid = vol->v_vid;
844     dir->d_fullpath = path;
845     dir->dcache_ctime = st->st_ctime;
846     dir->dcache_ino = st->st_ino;
847     if (!S_ISDIR(st->st_mode))
848         dir->d_flags = DIRF_ISFILE;
849     dir->d_rights_cache = 0xffffffff;
850     return dir;
851 }
852
853 /*!
854  * @brief Free a struct dir and all its members
855  *
856  * @param (rw) pointer to struct dir
857  */
858 void dir_free(struct dir *dir)
859 {
860     if (dir->d_u_name != dir->d_m_name) {
861         bdestroy(dir->d_u_name);
862     }
863     if (dir->d_m_name_ucs2)
864         free(dir->d_m_name_ucs2);
865     bdestroy(dir->d_m_name);
866     bdestroy(dir->d_fullpath);
867     free(dir);
868 }
869
870 /*!
871  * @brief Create struct dir from struct path
872  *
873  * Create a new struct dir from struct path. Then add it to the cache.
874  *
875  * 1. Open adouble file, get CNID from it.
876  * 2. Search the database, hinting with the CNID from (1).
877  * 3. Build fullpath and create struct dir.
878  * 4. Add it to the cache.
879  *
880  * @param vol   (r) pointer to struct vol, possibly modified in callee
881  * @param dir   (r) pointer to parrent directory
882  * @param path  (rw) pointer to struct path with valid path->u_name
883  * @param len   (r) strlen of path->u_name
884  *
885  * @returns Pointer to new struct dir or NULL on error.
886  *
887  * @note Function also assigns path->m_name from path->u_name.
888  */
889 struct dir *dir_add(struct vol *vol, const struct dir *dir, struct path *path, int len)
890 {
891     int err = 0;
892     struct dir  *cdir = NULL;
893     cnid_t      id;
894     struct adouble  ad;
895     struct adouble *adp = NULL;
896     bstring fullpath;
897
898     AFP_ASSERT(vol);
899     AFP_ASSERT(dir);
900     AFP_ASSERT(path);
901     AFP_ASSERT(len > 0);
902
903     if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
904         /* there's a stray entry in the dircache */
905         LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
906             ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
907             ntohl(cdir->d_did), cfrombstr(dir->d_fullpath));
908         if (dir_remove(vol, cdir) != 0) {
909             dircache_dump();
910             AFP_PANIC("dir_add");
911         }
912     }
913
914     /* get_id needs adp for reading CNID from adouble file */
915     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
916     if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
917         adp = &ad;
918
919     /* Get CNID */
920     if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
921         err = 1;
922         goto exit;
923     }
924
925     if (adp)
926         ad_close_metadata(adp);
927
928     /* Get macname from unixname */
929     if (path->m_name == NULL) {
930         if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
931             LOG(log_error, logtype_afpd, "dir_add(\"%s\"): can't assign macname", path->u_name);
932             err = 2;
933             goto exit;
934         }
935     }
936
937     /* Build fullpath */
938     if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
939          || (bconchar(fullpath, '/') != BSTR_OK)
940          || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
941         LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
942         err = 3;
943         goto exit;
944     }
945
946     /* Allocate and initialize struct dir */
947     if ((cdir = dir_new(path->m_name,
948                         path->u_name,
949                         vol,
950                         dir->d_did,
951                         id,
952                         fullpath,
953                         &path->st)) == NULL) { /* 3 */
954         err = 4;
955         goto exit;
956     }
957
958     if ((dircache_add(vol, cdir)) != 0) { /* 4 */
959         LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstr(fullpath));
960         exit(EXITERR_SYS);
961     }
962
963 exit:
964     if (err != 0) {
965         LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
966             cfrombstr(dir->d_u_name), path->u_name, err);
967
968         if (adp)
969             ad_close_metadata(adp);
970         if (!cdir && fullpath)
971             bdestroy(fullpath);
972         if (cdir)
973             dir_free(cdir);
974         cdir = NULL;
975     } else {
976         /* no error */
977         LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
978             ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
979             ntohl(cdir->d_did), cfrombstr(cdir->d_fullpath));
980     }
981
982     return(cdir);
983 }
984
985 /*!
986  * Free the queue with invalid struct dirs
987  *
988  * This gets called at the end of every AFP func.
989  */
990 void dir_free_invalid_q(void)
991 {
992     struct dir *dir;
993     while (dir = (struct dir *)dequeue(invalid_dircache_entries))
994         dir_free(dir);
995 }
996
997 /*!
998  * @brief Remove a dir from a cache and queue it for freeing
999  *
1000  * 1. Check if the dir is locked or has opened forks
1001  * 2. Remove it from the cache
1002  * 3. Queue it for removal
1003  * 4. If it's a request to remove curdir, mark curdir as invalid
1004  * 5. Mark it as invalid
1005  *
1006  * @param (r) pointer to struct vol
1007  * @param (rw) pointer to struct dir
1008  */
1009 int dir_remove(const struct vol *vol, struct dir *dir)
1010 {
1011     AFP_ASSERT(vol);
1012     AFP_ASSERT(dir);
1013
1014     if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
1015         return 0;
1016
1017     LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
1018         ntohl(dir->d_did), cfrombstr(dir->d_u_name));
1019
1020     dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 2 */
1021     enqueue(invalid_dircache_entries, dir); /* 3 */
1022
1023     if (curdir == dir)                      /* 4 */
1024         curdir = NULL;
1025
1026     dir->d_did = CNID_INVALID;              /* 5 */
1027
1028     return 0;
1029 }
1030
1031 #if 0 /* unused */
1032 /*!
1033  * @brief Modify a struct dir, adjust cache
1034  *
1035  * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
1036  * If given new_uname == new_mname, new_uname will point to new_mname.
1037  *
1038  * @param vol       (r) pointer to struct vol
1039  * @param dir       (rw) pointer to struct dir
1040  * @param pdid      (r) new parent DID
1041  * @param did       (r) new DID
1042  * @param new_mname (r) new mac-name
1043  * @param new_uname (r) new unix-name
1044  * @param pdir_fullpath (r) new fullpath of parent dir
1045  */
1046 int dir_modify(const struct vol *vol,
1047                struct dir *dir,
1048                cnid_t pdid,
1049                cnid_t did,
1050                const char *new_mname,
1051                const char *new_uname,
1052                bstring pdir_fullpath)
1053 {
1054     int ret = 0;
1055
1056     /* Remove it from the cache */
1057     dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
1058
1059     if (pdid)
1060         dir->d_pdid = pdid;
1061     if (did)
1062         dir->d_did = did;
1063
1064     if (new_mname) {
1065         /* free uname if it's not the same as mname */
1066         if (dir->d_m_name != dir->d_u_name)
1067             bdestroy(dir->d_u_name);
1068
1069         if (new_uname == NULL)
1070             new_uname = new_mname;
1071
1072         /* assign new name */
1073         if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
1074             LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1075             return -1;
1076         }
1077
1078         if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
1079             dir->d_u_name = dir->d_m_name;
1080         } else {
1081             if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
1082                 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1083                 return -1;
1084             }
1085         }
1086     }
1087
1088     if (pdir_fullpath) {
1089         if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
1090             return -1;
1091         if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
1092             return -1;
1093         if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
1094             return -1;
1095     }
1096
1097     if (dir->d_m_name_ucs2)
1098         free(dir->d_m_name_ucs2);
1099     if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, dir->d_m_name, -1, (char**)&dir->d_m_name_ucs2))
1100         dir->d_m_name_ucs2 = NULL;
1101
1102     /* Re-add it to the cache */
1103     if ((dircache_add(vol, dir)) != 0) {
1104         dircache_dump();
1105         AFP_PANIC("dir_modify");
1106     }
1107
1108     return ret;
1109 }
1110 #endif
1111
1112 /*!
1113  * @brief Resolve a catalog node name path
1114  *
1115  * 1. Evaluate path type
1116  * 2. Move to start dir, if we cant, it might eg because of EACCES, build
1117  *    path from dirname, so eg getdirparams has sth it can chew on. curdir
1118  *    is dir parent then. All this is done in path_from_dir().
1119  * 3. Parse next cnode name in path, cases:
1120  * 4.   single "\0" -> do nothing
1121  * 5.   two or more consecutive "\0" -> chdir("..") one or more times
1122  * 6.   cnode name -> copy it to path.m_name
1123  * 7. Get unix name from mac name
1124  * 8. Special handling of request with did 1
1125  * 9. stat the cnode name
1126  * 10. If it's not there, it's probably an afp_createfile|dir,
1127  *     return with curdir = dir parent, struct path = dirname
1128  * 11. If it's there and it's a file, it must should be the last element of the requested
1129  *     path. Return with curdir = cnode name parent dir, struct path = filename
1130  * 12. Treat symlinks like files, dont follow them
1131  * 13. If it's a dir:
1132  * 14. Search the dircache for it
1133  * 15. If it's not in the cache, create a struct dir for it and add it to the cache
1134  * 16. chdir into the dir and
1135  * 17. set m_name to the mac equivalent of "."
1136  * 18. goto 3
1137  */
1138 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1139 {
1140     static char        path[ MAXPATHLEN + 1];
1141     static struct path ret;
1142
1143     struct dir  *cdir;
1144     char        *data, *p;
1145     int         len;
1146     u_int32_t   hint;
1147     u_int16_t   len16;
1148     int         size = 0;
1149     int         toUTF8 = 0;
1150
1151     LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstr(dir->d_fullpath));
1152
1153     data = *cpath;
1154     afp_errno = AFPERR_NOOBJ;
1155     memset(&ret, 0, sizeof(ret));
1156
1157     switch (ret.m_type = *data) { /* 1 */
1158     case 2:
1159         data++;
1160         len = (unsigned char) *data++;
1161         size = 2;
1162         if (afp_version >= 30) {
1163             ret.m_type = 3;
1164             toUTF8 = 1;
1165         }
1166         break;
1167     case 3:
1168         if (afp_version >= 30) {
1169             data++;
1170             memcpy(&hint, data, sizeof(hint));
1171             hint = ntohl(hint);
1172             data += sizeof(hint);
1173
1174             memcpy(&len16, data, sizeof(len16));
1175             len = ntohs(len16);
1176             data += 2;
1177             size = 7;
1178             break;
1179         }
1180         /* else it's an error */
1181     default:
1182         afp_errno = AFPERR_PARAM;
1183         return( NULL );
1184     }
1185     *cpath += len + size;
1186
1187     path[0] = 0;
1188     ret.m_name = path;
1189
1190     if (movecwd(vol, dir) < 0 ) {
1191         LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1192             ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1193         if (len == 0)
1194             return path_from_dir(vol, dir, &ret);
1195         else
1196             return NULL;
1197     }
1198
1199     while (len) {         /* 3 */
1200         if (*data == 0) { /* 4 or 5 */
1201             data++;
1202             len--;
1203             while (len > 0 && *data == 0) { /* 5 */
1204                 /* chdir to parrent dir */
1205                 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1206                     return NULL;
1207                 if (movecwd( vol, dir ) < 0 ) {
1208                     dir_remove(vol, dir);
1209                     return NULL;
1210                 }
1211                 data++;
1212                 len--;
1213             }
1214             continue;
1215         }
1216
1217         /* 6*/
1218         for ( p = path; *data != 0 && len > 0; len-- ) {
1219             *p++ = *data++;
1220             if (p > &path[UTF8FILELEN_EARLY]) {   /* FIXME safeguard, limit of early Mac OS X */
1221                 afp_errno = AFPERR_PARAM;
1222                 return NULL;
1223             }
1224         }
1225         *p = 0;            /* Terminate string */
1226         ret.u_name = NULL;
1227
1228         if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1229             LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1230             return NULL;
1231         }
1232
1233         LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstr(dir->d_fullpath), ret.u_name);
1234
1235         /* Prevent access to our special folders like .AppleDouble */
1236         if (check_name(vol, ret.u_name)) {
1237             /* the name is illegal */
1238             LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1239             afp_errno = AFPERR_PARAM;
1240             return NULL;
1241         }
1242
1243         if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1244             /*
1245              * Special case: CNID 1
1246              * root parent (did 1) has one child: the volume. Requests for did=1 with
1247              * some <name> must check against the volume name.
1248              */
1249             if ((strcmp(cfrombstr(vol->v_root->d_m_name), ret.m_name)) == 0)
1250                 cdir = vol->v_root;
1251             else
1252                 return NULL;
1253         } else {
1254             /*
1255              * CNID != 1, eg. most of the times we take this way.
1256              * Now check if current path-part is a file or dir:
1257              * o if it's dir we have to step into it
1258              * o if it's a file we expect it to be the last part of the requested path
1259              *   and thus call continue which should terminate the while loop because
1260              *   len = 0. Ok?
1261              */
1262             if (of_stat(&ret) != 0) { /* 9 */
1263                 /*
1264                  * ret.u_name doesn't exist, might be afp_createfile|dir
1265                  * that means it should have been the last part
1266                  */
1267                 if (len > 0) {
1268                     /* it wasn't the last part, so we have a bogus path request */
1269                     afp_errno = AFPERR_NOOBJ;
1270                     return NULL;
1271                 }
1272                 /*
1273                  * this will terminate clean in while (1) because len == 0,
1274                  * probably afp_createfile|dir
1275                  */
1276                 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}",
1277                     cfrombstr(dir->d_fullpath), ret.u_name);
1278                 continue; /* 10 */
1279             }
1280
1281             switch (ret.st.st_mode & S_IFMT) {
1282             case S_IFREG: /* 11 */
1283                 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}",
1284                     cfrombstr(dir->d_fullpath), ret.u_name);
1285                 if (len > 0) {
1286                     /* it wasn't the last part, so we have a bogus path request */
1287                     afp_errno = AFPERR_PARAM;
1288                     return NULL;
1289                 }
1290                 continue; /* continues while loop */
1291             case S_IFLNK: /* 12 */
1292                 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}",
1293                     cfrombstr(dir->d_fullpath), ret.u_name);
1294                 if (len > 0) {
1295                     LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}",
1296                         cfrombstr(dir->d_fullpath), ret.u_name);
1297                     afp_errno = AFPERR_PARAM;
1298                     return NULL;
1299                 }
1300                 continue; /* continues while loop */
1301             case S_IFDIR: /* 13 */
1302                 break;
1303             default:
1304                 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1305                 afp_errno = AFPERR_NODIR;
1306                 return NULL;
1307             }
1308
1309             /* Search the cache */
1310             int unamelen = strlen(ret.u_name);
1311             cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1312             if (cdir == NULL) {
1313                 /* Not in cache, create one */
1314                 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1315                     LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1316                         ntohl(dir->d_did), ret.u_name, getcwdpath());
1317                     return NULL;
1318                 }
1319             }
1320         } /* if/else cnid==1 */
1321
1322         /* Now chdir to the evaluated dir */
1323         if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1324             LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1325                 cfrombstr(curdir->d_fullpath), cfrombstr(cdir->d_fullpath), strerror(errno));
1326             if (len == 0)
1327                 return path_from_dir(vol, cdir, &ret);
1328             else
1329                 return NULL;
1330         }
1331         dir = cdir;
1332         ret.m_name[0] = 0;      /* 17, so we later know last token was a dir */
1333     } /* while (len) */
1334
1335     if (curdir->d_did == DIRDID_ROOT_PARENT) {
1336         afp_errno = AFPERR_DID1;
1337         return NULL;
1338     }
1339
1340     if (ret.m_name[0] == 0) {
1341         /* Last part was a dir */
1342         ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1343         ret.d_dir = dir;
1344     }
1345
1346     LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1347         cfrombstr(dir->d_fullpath),
1348         cfrombstr(curdir->d_fullpath),
1349         ret.u_name);
1350
1351     return &ret;
1352 }
1353
1354 /*
1355  * @brief chdir() to dir
1356  *
1357  * @param vol   (r) pointer to struct vol
1358  * @param dir   (r) pointer to struct dir
1359  *
1360  * @returns 0 on success, -1 on error with afp_errno set appropiately
1361  */
1362 int movecwd(const struct vol *vol, struct dir *dir)
1363 {
1364     int ret;
1365
1366     AFP_ASSERT(vol);
1367     AFP_ASSERT(dir);
1368
1369     LOG(log_maxdebug, logtype_afpd, "movecwd: from: curdir:\"%s\", cwd:\"%s\"",
1370         curdir ? cfrombstr(curdir->d_fullpath) : "INVALID", getcwdpath());
1371
1372     if (dir->d_did == DIRDID_ROOT_PARENT) {
1373         curdir = &rootParent;
1374         return 0;
1375     }
1376
1377     LOG(log_debug, logtype_afpd, "movecwd(to: did: %u, \"%s\")",
1378         ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1379
1380     if ((ret = lchdir(cfrombstr(dir->d_fullpath))) != 0 ) {
1381         LOG(log_debug, logtype_afpd, "movecwd(\"%s\"): %s",
1382             cfrombstr(dir->d_fullpath), strerror(errno));
1383         if (ret == 1) {
1384             /* p is a symlink or getcwd failed */
1385             afp_errno = AFPERR_BADTYPE;
1386
1387             if (chdir(vol->v_path ) < 0) {
1388                 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1389                 /* XXX what do we do here? */
1390             }
1391             curdir = vol->v_root;
1392             return -1;
1393         }
1394
1395         switch (errno) {
1396         case EACCES:
1397         case EPERM:
1398             afp_errno = AFPERR_ACCESS;
1399             break;
1400         default:
1401             afp_errno = AFPERR_NOOBJ;
1402         }
1403         return( -1 );
1404     }
1405
1406     curdir = dir;
1407     return( 0 );
1408 }
1409
1410 /*
1411  * We can't use unix file's perm to support Apple's inherited protection modes.
1412  * If we aren't the file's owner we can't change its perms when moving it and smb
1413  * nfs,... don't even try.
1414  */
1415 #define AFP_CHECK_ACCESS
1416
1417 int check_access(char *path, int mode)
1418 {
1419 #ifdef AFP_CHECK_ACCESS
1420     struct maccess ma;
1421     char *p;
1422
1423     p = ad_dir(path);
1424     if (!p)
1425         return -1;
1426
1427     accessmode(current_vol, p, &ma, curdir, NULL);
1428     if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1429         return -1;
1430     if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1431         return -1;
1432 #endif
1433     return 0;
1434 }
1435
1436 /* --------------------- */
1437 int file_access(struct path *path, int mode)
1438 {
1439     struct maccess ma;
1440
1441     accessmode(current_vol, path->u_name, &ma, curdir, &path->st);
1442
1443     LOG(log_debug, logtype_afpd, "file_access(\"%s\"): mapped user mode: 0x%02x",
1444         path->u_name, ma.ma_user);
1445
1446     if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE)) {
1447         LOG(log_debug, logtype_afpd, "file_access(\"%s\"): write access denied", path->u_name);
1448         return -1;
1449     }
1450     if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD)) {
1451         LOG(log_debug, logtype_afpd, "file_access(\"%s\"): read access denied", path->u_name);
1452         return -1;
1453     }
1454     return 0;
1455
1456 }
1457
1458 /* --------------------- */
1459 void setdiroffcnt(struct dir *dir, struct stat *st,  u_int32_t count)
1460 {
1461     dir->d_offcnt = count;
1462     dir->d_ctime = st->st_ctime;
1463     dir->d_flags &= ~DIRF_CNID;
1464 }
1465
1466
1467 /* ---------------------
1468  * is our cached also for reenumerate id?
1469  */
1470 int dirreenumerate(struct dir *dir, struct stat *st)
1471 {
1472     return st->st_ctime == dir->d_ctime && (dir->d_flags & DIRF_CNID);
1473 }
1474
1475 /* ------------------------------
1476    (".", curdir)
1477    (name, dir) with curdir:name == dir, from afp_enumerate
1478 */
1479
1480 int getdirparams(const struct vol *vol,
1481                  u_int16_t bitmap, struct path *s_path,
1482                  struct dir *dir,
1483                  char *buf, size_t *buflen )
1484 {
1485     struct maccess  ma;
1486     struct adouble  ad;
1487     char        *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1488     int         bit = 0, isad = 0;
1489     u_int32_t           aint;
1490     u_int16_t       ashort;
1491     int                 ret;
1492     u_int32_t           utf8 = 0;
1493     cnid_t              pdid;
1494     struct stat *st = &s_path->st;
1495     char *upath = s_path->u_name;
1496
1497     if ((bitmap & ((1 << DIRPBIT_ATTR)  |
1498                    (1 << DIRPBIT_CDATE) |
1499                    (1 << DIRPBIT_MDATE) |
1500                    (1 << DIRPBIT_BDATE) |
1501                    (1 << DIRPBIT_FINFO)))) {
1502
1503         ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1504         if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1505             isad = 1;
1506             if (ad.ad_md->adf_flags & O_CREAT) {
1507                 /* We just created it */
1508                 if (s_path->m_name == NULL) {
1509                     if ((s_path->m_name = utompath(vol,
1510                                                    upath,
1511                                                    dir->d_did,
1512                                                    utf8_encoding())) == NULL) {
1513                         LOG(log_error, logtype_afpd,
1514                             "getdirparams(\"%s\"): can't assign macname",
1515                             cfrombstr(dir->d_fullpath));
1516                         return AFPERR_MISC;
1517                     }
1518                 }
1519                 ad_setname(&ad, s_path->m_name);
1520                 ad_setid( &ad,
1521                           s_path->st.st_dev,
1522                           s_path->st.st_ino,
1523                           dir->d_did,
1524                           dir->d_pdid,
1525                           vol->v_stamp);
1526                 ad_flush( &ad);
1527             }
1528         }
1529     }
1530
1531     pdid = dir->d_pdid;
1532
1533     data = buf;
1534     while ( bitmap != 0 ) {
1535         while (( bitmap & 1 ) == 0 ) {
1536             bitmap = bitmap>>1;
1537             bit++;
1538         }
1539
1540         switch ( bit ) {
1541         case DIRPBIT_ATTR :
1542             if ( isad ) {
1543                 ad_getattr(&ad, &ashort);
1544             } else if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1545                 ashort = htons(ATTRBIT_INVISIBLE);
1546             } else
1547                 ashort = 0;
1548             ashort |= htons(ATTRBIT_SHARED);
1549             memcpy( data, &ashort, sizeof( ashort ));
1550             data += sizeof( ashort );
1551             break;
1552
1553         case DIRPBIT_PDID :
1554             memcpy( data, &pdid, sizeof( pdid ));
1555             data += sizeof( pdid );
1556             LOG(log_debug, logtype_afpd, "metadata('%s'):     Parent DID: %u",
1557                 s_path->u_name, ntohl(pdid));
1558             break;
1559
1560         case DIRPBIT_CDATE :
1561             if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1562                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1563             memcpy( data, &aint, sizeof( aint ));
1564             data += sizeof( aint );
1565             break;
1566
1567         case DIRPBIT_MDATE :
1568             aint = AD_DATE_FROM_UNIX(st->st_mtime);
1569             memcpy( data, &aint, sizeof( aint ));
1570             data += sizeof( aint );
1571             break;
1572
1573         case DIRPBIT_BDATE :
1574             if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1575                 aint = AD_DATE_START;
1576             memcpy( data, &aint, sizeof( aint ));
1577             data += sizeof( aint );
1578             break;
1579
1580         case DIRPBIT_FINFO :
1581             if ( isad ) {
1582                 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1583             } else { /* no appledouble */
1584                 memset( data, 0, 32 );
1585                 /* set default view -- this also gets done in ad_open() */
1586                 ashort = htons(FINDERINFO_CLOSEDVIEW);
1587                 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1588
1589                 /* dot files are by default visible */
1590                 if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1591                     ashort = htons(FINDERINFO_INVISIBLE);
1592                     memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1593                 }
1594             }
1595             data += 32;
1596             break;
1597
1598         case DIRPBIT_LNAME :
1599             if (dir->d_m_name) /* root of parent can have a null name */
1600                 l_nameoff = data;
1601             else
1602                 memset(data, 0, sizeof(u_int16_t));
1603             data += sizeof( u_int16_t );
1604             break;
1605
1606         case DIRPBIT_SNAME :
1607             memset(data, 0, sizeof(u_int16_t));
1608             data += sizeof( u_int16_t );
1609             break;
1610
1611         case DIRPBIT_DID :
1612             memcpy( data, &dir->d_did, sizeof( aint ));
1613             data += sizeof( aint );
1614             LOG(log_debug, logtype_afpd, "metadata('%s'):            DID: %u",
1615                 s_path->u_name, ntohl(dir->d_did));
1616             break;
1617
1618         case DIRPBIT_OFFCNT :
1619             ashort = 0;
1620             /* this needs to handle current directory access rights */
1621             if (diroffcnt(dir, st)) {
1622                 ashort = (dir->d_offcnt > 0xffff)?0xffff:dir->d_offcnt;
1623             }
1624             else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1625                 setdiroffcnt(dir, st,  ret);
1626                 ashort = (dir->d_offcnt > 0xffff)?0xffff:dir->d_offcnt;
1627             }
1628             ashort = htons( ashort );
1629             memcpy( data, &ashort, sizeof( ashort ));
1630             data += sizeof( ashort );
1631             break;
1632
1633         case DIRPBIT_UID :
1634             aint = htonl(st->st_uid);
1635             memcpy( data, &aint, sizeof( aint ));
1636             data += sizeof( aint );
1637             break;
1638
1639         case DIRPBIT_GID :
1640             aint = htonl(st->st_gid);
1641             memcpy( data, &aint, sizeof( aint ));
1642             data += sizeof( aint );
1643             break;
1644
1645         case DIRPBIT_ACCESS :
1646             accessmode(vol, upath, &ma, dir , st);
1647
1648             *data++ = ma.ma_user;
1649             *data++ = ma.ma_world;
1650             *data++ = ma.ma_group;
1651             *data++ = ma.ma_owner;
1652             break;
1653
1654             /* Client has requested the ProDOS information block.
1655                Just pass back the same basic block for all
1656                directories. <shirsch@ibm.net> */
1657         case DIRPBIT_PDINFO :
1658             if (afp_version >= 30) { /* UTF8 name */
1659                 utf8 = kTextEncodingUTF8;
1660                 if (dir->d_m_name) /* root of parent can have a null name */
1661                     utf_nameoff = data;
1662                 else
1663                     memset(data, 0, sizeof(u_int16_t));
1664                 data += sizeof( u_int16_t );
1665                 aint = 0;
1666                 memcpy(data, &aint, sizeof( aint ));
1667                 data += sizeof( aint );
1668             }
1669             else { /* ProDOS Info Block */
1670                 *data++ = 0x0f;
1671                 *data++ = 0;
1672                 ashort = htons( 0x0200 );
1673                 memcpy( data, &ashort, sizeof( ashort ));
1674                 data += sizeof( ashort );
1675                 memset( data, 0, sizeof( ashort ));
1676                 data += sizeof( ashort );
1677             }
1678             break;
1679
1680         case DIRPBIT_UNIXPR :
1681             /* accessmode may change st_mode with ACLs */
1682             accessmode(vol, upath, &ma, dir, st);
1683
1684             aint = htonl(st->st_uid);
1685             memcpy( data, &aint, sizeof( aint ));
1686             data += sizeof( aint );
1687             aint = htonl(st->st_gid);
1688             memcpy( data, &aint, sizeof( aint ));
1689             data += sizeof( aint );
1690
1691             aint = st->st_mode;
1692             aint = htonl ( aint & ~S_ISGID );  /* Remove SGID, OSX doesn't like it ... */
1693             memcpy( data, &aint, sizeof( aint ));
1694             data += sizeof( aint );
1695
1696             *data++ = ma.ma_user;
1697             *data++ = ma.ma_world;
1698             *data++ = ma.ma_group;
1699             *data++ = ma.ma_owner;
1700             break;
1701
1702         default :
1703             if ( isad ) {
1704                 ad_close_metadata( &ad );
1705             }
1706             return( AFPERR_BITMAP );
1707         }
1708         bitmap = bitmap>>1;
1709         bit++;
1710     }
1711     if ( l_nameoff ) {
1712         ashort = htons( data - buf );
1713         memcpy( l_nameoff, &ashort, sizeof( ashort ));
1714         data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, 0);
1715     }
1716     if ( utf_nameoff ) {
1717         ashort = htons( data - buf );
1718         memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1719         data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, utf8);
1720     }
1721     if ( isad ) {
1722         ad_close_metadata( &ad );
1723     }
1724     *buflen = data - buf;
1725     return( AFP_OK );
1726 }
1727
1728 /* ----------------------------- */
1729 int path_error(struct path *path, int error)
1730 {
1731 /* - a dir with access error
1732  * - no error it's a file
1733  * - file not found
1734  */
1735     if (path_isadir(path))
1736         return afp_errno;
1737     if (path->st_valid && path->st_errno)
1738         return error;
1739     return AFPERR_BADTYPE ;
1740 }
1741
1742 /* ----------------------------- */
1743 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1744 {
1745     struct vol  *vol;
1746     struct dir  *dir;
1747     struct path *path;
1748     u_int16_t   vid, bitmap;
1749     u_int32_t   did;
1750     int     rc;
1751
1752     *rbuflen = 0;
1753     ibuf += 2;
1754     memcpy( &vid, ibuf, sizeof( vid ));
1755     ibuf += sizeof( vid );
1756
1757     if (NULL == ( vol = getvolbyvid( vid )) ) {
1758         return( AFPERR_PARAM );
1759     }
1760
1761     if (vol->v_flags & AFPVOL_RO)
1762         return AFPERR_VLOCK;
1763
1764     memcpy( &did, ibuf, sizeof( did ));
1765     ibuf += sizeof( int );
1766
1767     if (NULL == ( dir = dirlookup( vol, did )) ) {
1768         return afp_errno;
1769     }
1770
1771     memcpy( &bitmap, ibuf, sizeof( bitmap ));
1772     bitmap = ntohs( bitmap );
1773     ibuf += sizeof( bitmap );
1774
1775     if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1776         return get_afp_errno(AFPERR_NOOBJ);
1777     }
1778
1779     if ( *path->m_name != '\0' ) {
1780         rc = path_error(path, AFPERR_NOOBJ);
1781         /* maybe we are trying to set perms back */
1782         if (rc != AFPERR_ACCESS)
1783             return rc;
1784     }
1785
1786     /*
1787      * If ibuf is odd, make it even.
1788      */
1789     if ((u_long)ibuf & 1 ) {
1790         ibuf++;
1791     }
1792
1793     if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1794         setvoltime(obj, vol );
1795     }
1796     return( rc );
1797 }
1798
1799 /*
1800  * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1801  *
1802  * assume path == '\0' eg. it's a directory in canonical form
1803  */
1804 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1805 {
1806     struct maccess  ma;
1807     struct adouble  ad;
1808     struct utimbuf      ut;
1809     struct timeval      tv;
1810
1811     char                *upath;
1812     struct dir          *dir;
1813     int         bit, isad = 1;
1814     int                 cdate, bdate;
1815     int                 owner, group;
1816     u_int16_t       ashort, bshort, oshort;
1817     int                 err = AFP_OK;
1818     int                 change_mdate = 0;
1819     int                 change_parent_mdate = 0;
1820     int                 newdate = 0;
1821     u_int16_t           bitmap = d_bitmap;
1822     u_char              finder_buf[32];
1823     u_int32_t       upriv;
1824     mode_t              mpriv = 0;
1825     u_int16_t           upriv_bit = 0;
1826
1827     bit = 0;
1828     upath = path->u_name;
1829     dir   = path->d_dir;
1830     while ( bitmap != 0 ) {
1831         while (( bitmap & 1 ) == 0 ) {
1832             bitmap = bitmap>>1;
1833             bit++;
1834         }
1835
1836         switch( bit ) {
1837         case DIRPBIT_ATTR :
1838             change_mdate = 1;
1839             memcpy( &ashort, buf, sizeof( ashort ));
1840             buf += sizeof( ashort );
1841             break;
1842         case DIRPBIT_CDATE :
1843             change_mdate = 1;
1844             memcpy(&cdate, buf, sizeof(cdate));
1845             buf += sizeof( cdate );
1846             break;
1847         case DIRPBIT_MDATE :
1848             memcpy(&newdate, buf, sizeof(newdate));
1849             buf += sizeof( newdate );
1850             break;
1851         case DIRPBIT_BDATE :
1852             change_mdate = 1;
1853             memcpy(&bdate, buf, sizeof(bdate));
1854             buf += sizeof( bdate );
1855             break;
1856         case DIRPBIT_FINFO :
1857             change_mdate = 1;
1858             memcpy( finder_buf, buf, 32 );
1859             buf += 32;
1860             break;
1861         case DIRPBIT_UID :  /* What kind of loser mounts as root? */
1862             change_parent_mdate = 1;
1863             memcpy( &owner, buf, sizeof(owner));
1864             buf += sizeof( owner );
1865             break;
1866         case DIRPBIT_GID :
1867             change_parent_mdate = 1;
1868             memcpy( &group, buf, sizeof( group ));
1869             buf += sizeof( group );
1870             break;
1871         case DIRPBIT_ACCESS :
1872             change_mdate = 1;
1873             change_parent_mdate = 1;
1874             ma.ma_user = *buf++;
1875             ma.ma_world = *buf++;
1876             ma.ma_group = *buf++;
1877             ma.ma_owner = *buf++;
1878             mpriv = mtoumode( &ma ) | vol->v_dperm;
1879             if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1880                 err = set_dir_errors(path, "setdirmode", errno);
1881                 bitmap = 0;
1882             }
1883             break;
1884             /* Ignore what the client thinks we should do to the
1885                ProDOS information block.  Skip over the data and
1886                report nothing amiss. <shirsch@ibm.net> */
1887         case DIRPBIT_PDINFO :
1888             if (afp_version < 30) {
1889                 buf += 6;
1890             }
1891             else {
1892                 err = AFPERR_BITMAP;
1893                 bitmap = 0;
1894             }
1895             break;
1896         case DIRPBIT_UNIXPR :
1897             if (vol_unix_priv(vol)) {
1898                 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1899                 buf += sizeof( owner );
1900                 memcpy( &group, buf, sizeof( group ));
1901                 buf += sizeof( group );
1902
1903                 change_mdate = 1;
1904                 change_parent_mdate = 1;
1905                 memcpy( &upriv, buf, sizeof( upriv ));
1906                 buf += sizeof( upriv );
1907                 upriv = ntohl (upriv) | vol->v_dperm;
1908                 if (dir_rx_set(upriv)) {
1909                     /* maybe we are trying to set perms back */
1910                     if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1911                         bitmap = 0;
1912                         err = set_dir_errors(path, "setdirunixmode", errno);
1913                     }
1914                 }
1915                 else {
1916                     /* do it later */
1917                     upriv_bit = 1;
1918                 }
1919                 break;
1920             }
1921             /* fall through */
1922         default :
1923             err = AFPERR_BITMAP;
1924             bitmap = 0;
1925             break;
1926         }
1927
1928         bitmap = bitmap>>1;
1929         bit++;
1930     }
1931     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1932
1933     if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1934         /*
1935          * Check to see what we're trying to set.  If it's anything
1936          * but ACCESS, UID, or GID, give an error.  If it's any of those
1937          * three, we don't need the ad to be open, so just continue.
1938          *
1939          * note: we also don't need to worry about mdate. also, be quiet
1940          *       if we're using the noadouble option.
1941          */
1942         if (!vol_noadouble(vol) && (d_bitmap &
1943                                     ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1944                                       (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1945                                       (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1946             return AFPERR_ACCESS;
1947         }
1948
1949         isad = 0;
1950     } else {
1951         /*
1952          * Check to see if a create was necessary. If it was, we'll want
1953          * to set our name, etc.
1954          */
1955         if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1956             ad_setname(&ad, cfrombstr(curdir->d_m_name));
1957         }
1958     }
1959
1960     bit = 0;
1961     bitmap = d_bitmap;
1962     while ( bitmap != 0 ) {
1963         while (( bitmap & 1 ) == 0 ) {
1964             bitmap = bitmap>>1;
1965             bit++;
1966         }
1967
1968         switch( bit ) {
1969         case DIRPBIT_ATTR :
1970             if (isad) {
1971                 ad_getattr(&ad, &bshort);
1972                 oshort = bshort;
1973                 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1974                     bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1975                 } else {
1976                     bshort &= ~ashort;
1977                 }
1978                 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1979                     change_parent_mdate = 1;
1980                 ad_setattr(&ad, bshort);
1981             }
1982             break;
1983         case DIRPBIT_CDATE :
1984             if (isad) {
1985                 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1986             }
1987             break;
1988         case DIRPBIT_MDATE :
1989             break;
1990         case DIRPBIT_BDATE :
1991             if (isad) {
1992                 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1993             }
1994             break;
1995         case DIRPBIT_FINFO :
1996             if (isad) {
1997                 /* Fixes #2802236 */
1998                 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1999                 *fflags &= htons(~FINDERINFO_ISHARED);
2000                 /* #2802236 end */
2001                 if (  dir->d_did == DIRDID_ROOT ) {
2002                     /*
2003                      * Alright, we admit it, this is *really* sick!
2004                      * The 4 bytes that we don't copy, when we're dealing
2005                      * with the root of a volume, are the directory's
2006                      * location information. This eliminates that annoying
2007                      * behavior one sees when mounting above another mount
2008                      * point.
2009                      */
2010                     memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
2011                     memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
2012                 } else {
2013                     memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
2014                 }
2015             }
2016             break;
2017         case DIRPBIT_UID :  /* What kind of loser mounts as root? */
2018             if ( (dir->d_did == DIRDID_ROOT) &&
2019                  (setdeskowner( ntohl(owner), -1 ) < 0)) {
2020                 err = set_dir_errors(path, "setdeskowner", errno);
2021                 if (isad && err == AFPERR_PARAM) {
2022                     err = AFP_OK; /* ???*/
2023                 }
2024                 else {
2025                     goto setdirparam_done;
2026                 }
2027             }
2028             if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
2029                 err = set_dir_errors(path, "setdirowner", errno);
2030                 goto setdirparam_done;
2031             }
2032             break;
2033         case DIRPBIT_GID :
2034             if (dir->d_did == DIRDID_ROOT)
2035                 setdeskowner( -1, ntohl(group) );
2036             if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2037                 err = set_dir_errors(path, "setdirowner", errno);
2038                 goto setdirparam_done;
2039             }
2040             break;
2041         case DIRPBIT_ACCESS :
2042             if (dir->d_did == DIRDID_ROOT) {
2043                 setdeskmode(mpriv);
2044                 if (!dir_rx_set(mpriv)) {
2045                     /* we can't remove read and search for owner on volume root */
2046                     err = AFPERR_ACCESS;
2047                     goto setdirparam_done;
2048                 }
2049             }
2050
2051             if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2052                 err = set_dir_errors(path, "setdirmode", errno);
2053                 goto setdirparam_done;
2054             }
2055             break;
2056         case DIRPBIT_PDINFO :
2057             if (afp_version >= 30) {
2058                 err = AFPERR_BITMAP;
2059                 goto setdirparam_done;
2060             }
2061             break;
2062         case DIRPBIT_UNIXPR :
2063             if (vol_unix_priv(vol)) {
2064                 if (dir->d_did == DIRDID_ROOT) {
2065                     if (!dir_rx_set(upriv)) {
2066                         /* we can't remove read and search for owner on volume root */
2067                         err = AFPERR_ACCESS;
2068                         goto setdirparam_done;
2069                     }
2070                     setdeskowner( -1, ntohl(group) );
2071                     setdeskmode( upriv );
2072                 }
2073                 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2074                     err = set_dir_errors(path, "setdirowner", errno);
2075                     goto setdirparam_done;
2076                 }
2077
2078                 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2079                     err = set_dir_errors(path, "setdirunixmode", errno);
2080                     goto setdirparam_done;
2081                 }
2082             }
2083             else {
2084                 err = AFPERR_BITMAP;
2085                 goto setdirparam_done;
2086             }
2087             break;
2088         default :
2089             err = AFPERR_BITMAP;
2090             goto setdirparam_done;
2091             break;
2092         }
2093
2094         bitmap = bitmap>>1;
2095         bit++;
2096     }
2097
2098 setdirparam_done:
2099     if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2100         newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2101     }
2102     if (newdate) {
2103         if (isad)
2104             ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2105         ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2106         utime(upath, &ut);
2107     }
2108
2109     if ( isad ) {
2110         if (path->st_valid && !path->st_errno) {
2111             struct stat *st = &path->st;
2112
2113             if (dir && dir->d_pdid) {
2114                 ad_setid(&ad, st->st_dev, st->st_ino,  dir->d_did, dir->d_pdid, vol->v_stamp);
2115             }
2116         }
2117         ad_flush( &ad);
2118         ad_close_metadata( &ad);
2119     }
2120
2121     if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2122         && gettimeofday(&tv, NULL) == 0) {
2123         if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
2124             newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2125             /* be careful with bitmap because now dir is null */
2126             bitmap = 1<<DIRPBIT_MDATE;
2127             setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2128             /* should we reset curdir ?*/
2129         }
2130     }
2131
2132     return err;
2133 }
2134
2135 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2136 {
2137 #ifdef HAVE_DIRFD
2138     DIR                  *dp;
2139 #endif
2140     int                  dfd;
2141     struct vol           *vol;
2142     struct dir           *dir;
2143     u_int32_t            did;
2144     u_int16_t            vid;
2145
2146     *rbuflen = 0;
2147     ibuf += 2;
2148
2149     memcpy( &vid, ibuf, sizeof( vid ));
2150     ibuf += sizeof( vid );
2151     if (NULL == (vol = getvolbyvid( vid )) ) {
2152         return( AFPERR_PARAM );
2153     }
2154
2155     memcpy( &did, ibuf, sizeof( did ));
2156     ibuf += sizeof( did );
2157
2158     /*
2159      * Here's the deal:
2160      * if it's CNID 2 our only choice to meet the specs is call sync.
2161      * For any other CNID just sync that dir. To my knowledge the
2162      * intended use of FPSyncDir is to sync the volume so all we're
2163      * ever going to see here is probably CNID 2. Anyway, we' prepared.
2164      */
2165
2166     if ( ntohl(did) == 2 ) {
2167         sync();
2168     } else {
2169         if (NULL == ( dir = dirlookup( vol, did )) ) {
2170             return afp_errno; /* was AFPERR_NOOBJ */
2171         }
2172
2173         if (movecwd( vol, dir ) < 0 )
2174             return ( AFPERR_NOOBJ );
2175
2176         /*
2177          * Assuming only OSens that have dirfd also may require fsyncing directories
2178          * in order to flush metadata e.g. Linux.
2179          */
2180
2181 #ifdef HAVE_DIRFD
2182         if (NULL == ( dp = opendir( "." )) ) {
2183             switch( errno ) {
2184             case ENOENT :
2185                 return( AFPERR_NOOBJ );
2186             case EACCES :
2187                 return( AFPERR_ACCESS );
2188             default :
2189                 return( AFPERR_PARAM );
2190             }
2191         }
2192
2193         LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2194
2195         dfd = dirfd( dp );
2196         if ( fsync ( dfd ) < 0 )
2197             LOG(log_error, logtype_afpd, "afp_syncdir(%s):  %s",
2198                 dir->d_u_name, strerror(errno) );
2199         closedir(dp); /* closes dfd too */
2200 #endif
2201
2202         if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2203             switch( errno ) {
2204             case ENOENT:
2205                 return( AFPERR_NOOBJ );
2206             case EACCES:
2207                 return( AFPERR_ACCESS );
2208             default:
2209                 return( AFPERR_PARAM );
2210             }
2211         }
2212
2213         LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2214             vol->ad_path(".", ADFLAGS_DIR) );
2215
2216         if ( fsync(dfd) < 0 )
2217             LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2218                 vol->ad_path(cfrombstr(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2219         close(dfd);
2220     }
2221
2222     return ( AFP_OK );
2223 }
2224
2225 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2226 {
2227     struct adouble  ad;
2228     struct vol      *vol;
2229     struct dir      *dir;
2230     char        *upath;
2231     struct path         *s_path;
2232     u_int32_t       did;
2233     u_int16_t       vid;
2234     int                 err;
2235
2236     *rbuflen = 0;
2237     ibuf += 2;
2238
2239     memcpy( &vid, ibuf, sizeof( vid ));
2240     ibuf += sizeof( vid );
2241     if (NULL == ( vol = getvolbyvid( vid )) ) {
2242         return( AFPERR_PARAM );
2243     }
2244
2245     if (vol->v_flags & AFPVOL_RO)
2246         return AFPERR_VLOCK;
2247
2248     memcpy( &did, ibuf, sizeof( did ));
2249     ibuf += sizeof( did );
2250     if (NULL == ( dir = dirlookup( vol, did )) ) {
2251         return afp_errno; /* was AFPERR_NOOBJ */
2252     }
2253     /* for concurrent access we need to be sure we are not in the
2254      * folder we want to create...
2255      */
2256     movecwd(vol, dir);
2257
2258     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2259         return get_afp_errno(AFPERR_PARAM);
2260     }
2261     /* cname was able to move curdir to it! */
2262     if (*s_path->m_name == '\0')
2263         return AFPERR_EXIST;
2264
2265     upath = s_path->u_name;
2266
2267     if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2268         return err;
2269     }
2270
2271     if (of_stat(s_path) < 0) {
2272         return AFPERR_MISC;
2273     }
2274
2275     curdir->d_offcnt++;
2276
2277     if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2278         return AFPERR_MISC;
2279     }
2280
2281     if ( movecwd( vol, dir ) < 0 ) {
2282         return( AFPERR_PARAM );
2283     }
2284
2285     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2286     if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0)  {
2287         if (vol_noadouble(vol))
2288             goto createdir_done;
2289         return( AFPERR_ACCESS );
2290     }
2291     ad_setname(&ad, s_path->m_name);
2292     ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2293
2294     fce_register_new_dir(s_path);
2295
2296     ad_flush( &ad);
2297     ad_close_metadata( &ad);
2298
2299 createdir_done:
2300     memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2301     *rbuflen = sizeof( u_int32_t );
2302     setvoltime(obj, vol );
2303     return( AFP_OK );
2304 }
2305
2306 /*
2307  * dst       new unix filename (not a pathname)
2308  * newname   new mac name
2309  * newparent curdir
2310  * dirfd     -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2311  */
2312 int renamedir(const struct vol *vol,
2313               int dirfd,
2314               char *src,
2315               char *dst,
2316               struct dir *dir,
2317               struct dir *newparent,
2318               char *newname)
2319 {
2320     struct adouble  ad;
2321     int             err;
2322
2323     /* existence check moved to afp_moveandrename */
2324     if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2325         switch ( errno ) {
2326         case ENOENT :
2327             return( AFPERR_NOOBJ );
2328         case EACCES :
2329             return( AFPERR_ACCESS );
2330         case EROFS:
2331             return AFPERR_VLOCK;
2332         case EINVAL:
2333             /* tried to move directory into a subdirectory of itself */
2334             return AFPERR_CANTMOVE;
2335         case EXDEV:
2336             /* this needs to copy and delete. bleah. that means we have
2337              * to deal with entire directory hierarchies. */
2338             if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2339                 deletedir(-1, dst);
2340                 return err;
2341             }
2342             if ((err = deletedir(dirfd, src)) < 0)
2343                 return err;
2344             break;
2345         default :
2346             return( AFPERR_PARAM );
2347         }
2348     }
2349
2350     vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2351
2352     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2353
2354     if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2355         ad_setname(&ad, newname);
2356         ad_flush( &ad);
2357         ad_close_metadata( &ad);
2358     }
2359
2360     return( AFP_OK );
2361 }
2362
2363 /* delete an empty directory */
2364 int deletecurdir(struct vol *vol)
2365 {
2366     struct dirent *de;
2367     struct stat st;
2368     struct dir  *fdir, *pdir;
2369     DIR *dp;
2370     struct adouble  ad;
2371     u_int16_t       ashort;
2372     int err;
2373
2374     if ((pdir = dirlookup(vol, curdir->d_pdid)) == NULL) {
2375         return( AFPERR_ACCESS );
2376     }
2377
2378     fdir = curdir;
2379
2380     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2381     /* we never want to create a resource fork here, we are going to delete it */
2382     if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2383
2384         ad_getattr(&ad, &ashort);
2385         ad_close_metadata(&ad);
2386         if ((ashort & htons(ATTRBIT_NODELETE))) {
2387             return  AFPERR_OLOCK;
2388         }
2389     }
2390     err = vol->vfs->vfs_deletecurdir(vol);
2391     if (err) {
2392         LOG(log_error, logtype_afpd, "deletecurdir: error deleting .AppleDouble in \"%s\"",
2393             curdir->d_fullpath);
2394         return err;
2395     }
2396
2397     /* now get rid of dangling symlinks */
2398     if ((dp = opendir("."))) {
2399         while ((de = readdir(dp))) {
2400             /* skip this and previous directory */
2401             if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2402                 continue;
2403
2404             /* bail if it's not a symlink */
2405             if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2406                 LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): not empty",
2407                     curdir->d_fullpath);
2408                 closedir(dp);
2409                 return AFPERR_DIRNEMPT;
2410             }
2411
2412             if ((err = netatalk_unlink(de->d_name))) {
2413                 closedir(dp);
2414                 return err;
2415             }
2416         }
2417     }
2418
2419     if (movecwd(vol, pdir) < 0) {
2420         err = afp_errno;
2421         goto delete_done;
2422     }
2423
2424     LOG(log_debug, logtype_afpd, "deletecurdir: moved to \"%s\"",
2425         cfrombstr(curdir->d_fullpath));
2426
2427     err = netatalk_rmdir_all_errors(-1, cfrombstr(fdir->d_u_name));
2428     if ( err ==  AFP_OK || err == AFPERR_NOOBJ) {
2429         cnid_delete(vol->v_cdb, fdir->d_did);
2430         dir_remove( vol, fdir );
2431     } else {
2432         LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): netatalk_rmdir_all_errors error",
2433             curdir->d_fullpath);
2434     }
2435
2436 delete_done:
2437     if (dp) {
2438         /* inode is used as key for cnid.
2439          * Close the descriptor only after cnid_delete
2440          * has been called.
2441          */
2442         closedir(dp);
2443     }
2444     return err;
2445 }
2446
2447 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2448 {
2449     struct passwd   *pw;
2450     struct group    *gr;
2451     char        *name;
2452     u_int32_t           id;
2453     int         len, sfunc;
2454     int         utf8 = 0;
2455
2456     ibuf++;
2457     sfunc = (unsigned char) *ibuf++;
2458     *rbuflen = 0;
2459
2460     if (sfunc >= 3 && sfunc <= 6) {
2461         if (afp_version < 30) {
2462             return( AFPERR_PARAM );
2463         }
2464         utf8 = 1;
2465     }
2466
2467     switch ( sfunc ) {
2468     case 1 :
2469     case 3 :/* unicode */
2470         memcpy( &id, ibuf, sizeof( id ));
2471         id = ntohl(id);
2472         if ( id != 0 ) {
2473             if (( pw = getpwuid( id )) == NULL ) {
2474                 return( AFPERR_NOITEM );
2475             }
2476             len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2477                                            pw->pw_name, -1, &name);
2478         } else {
2479             len = 0;
2480             name = NULL;
2481         }
2482         break;
2483     case 2 :
2484     case 4 : /* unicode */
2485         memcpy( &id, ibuf, sizeof( id ));
2486         id = ntohl(id);
2487         if ( id != 0 ) {
2488             if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2489                 return( AFPERR_NOITEM );
2490             }
2491             len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2492                                            gr->gr_name, -1, &name);
2493         } else {
2494             len = 0;
2495             name = NULL;
2496         }
2497         break;
2498
2499     case 5 : /* UUID -> username */
2500     case 6 : /* UUID -> groupname */
2501         if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2502             return AFPERR_PARAM;
2503         LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2504         uuidtype_t type;
2505         len = getnamefromuuid((unsigned char*) ibuf, &name, &type);
2506         if (len != 0)       /* its a error code, not len */
2507             return AFPERR_NOITEM;
2508         switch (type) {
2509         case UUID_USER:
2510             if (( pw = getpwnam( name )) == NULL )
2511                 return( AFPERR_NOITEM );
2512             LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2513             id = htonl(UUID_USER);
2514             memcpy( rbuf, &id, sizeof( id ));
2515             id = htonl( pw->pw_uid);
2516             rbuf += sizeof( id );
2517             memcpy( rbuf, &id, sizeof( id ));
2518             rbuf += sizeof( id );
2519             *rbuflen = 2 * sizeof( id );
2520             break;
2521         case UUID_GROUP:
2522             if (( gr = getgrnam( name )) == NULL )
2523                 return( AFPERR_NOITEM );
2524             LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2525             id = htonl(UUID_GROUP);
2526             memcpy( rbuf, &id, sizeof( id ));
2527             rbuf += sizeof( id );
2528             id = htonl( gr->gr_gid);
2529             memcpy( rbuf, &id, sizeof( id ));
2530             rbuf += sizeof( id );
2531             *rbuflen = 2 * sizeof( id );
2532             break;
2533         default:
2534             return AFPERR_MISC;
2535         }
2536         break;
2537
2538     default :
2539         return( AFPERR_PARAM );
2540     }
2541
2542     if (name)
2543         len = strlen( name );
2544
2545     if (utf8) {
2546         u_int16_t tp = htons(len);
2547         memcpy(rbuf, &tp, sizeof(tp));
2548         rbuf += sizeof(tp);
2549         *rbuflen += 2;
2550     }
2551     else {
2552         *rbuf++ = len;
2553         *rbuflen += 1;
2554     }
2555     if ( len > 0 ) {
2556         memcpy( rbuf, name, len );
2557     }
2558     *rbuflen += len;
2559     if (name)
2560         free(name);
2561     return( AFP_OK );
2562 }
2563
2564 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2565 {
2566     struct passwd   *pw;
2567     struct group    *gr;
2568     int             len, sfunc;
2569     u_int32_t       id;
2570     u_int16_t       ulen;
2571
2572     ibuf++;
2573     sfunc = (unsigned char) *ibuf++;
2574     *rbuflen = 0;
2575     LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2576     switch ( sfunc ) {
2577     case 1 :
2578     case 2 : /* unicode */
2579         if (afp_version < 30) {
2580             return( AFPERR_PARAM );
2581         }
2582         memcpy(&ulen, ibuf, sizeof(ulen));
2583         len = ntohs(ulen);
2584         ibuf += 2;
2585         LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2586         break;
2587     case 3 :
2588     case 4 :
2589         len = (unsigned char) *ibuf++;
2590         break;
2591     case 5 : /* username -> UUID  */
2592     case 6 : /* groupname -> UUID */
2593         if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2594             return AFPERR_PARAM;
2595         memcpy(&ulen, ibuf, sizeof(ulen));
2596         len = ntohs(ulen);
2597         ibuf += 2;
2598         break;
2599     default :
2600         return( AFPERR_PARAM );
2601     }
2602
2603     ibuf[ len ] = '\0';
2604
2605     if ( len == 0 )
2606         return AFPERR_PARAM;
2607     else {
2608         switch ( sfunc ) {
2609         case 1 : /* unicode */
2610         case 3 :
2611             if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2612                 return( AFPERR_NOITEM );
2613             }
2614             id = pw->pw_uid;
2615             id = htonl(id);
2616             memcpy( rbuf, &id, sizeof( id ));
2617             *rbuflen = sizeof( id );
2618             break;
2619
2620         case 2 : /* unicode */
2621         case 4 :
2622             LOG(log_debug, logtype_afpd, "afp_mapname: getgrnam for name: %s",ibuf);
2623             if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2624                 return( AFPERR_NOITEM );
2625             }
2626             id = gr->gr_gid;
2627             LOG(log_debug, logtype_afpd, "afp_mapname: getgrnam for name: %s -> id: %d",ibuf, id);
2628             id = htonl(id);
2629             memcpy( rbuf, &id, sizeof( id ));
2630             *rbuflen = sizeof( id );
2631             break;
2632         case 5 :        /* username -> UUID */
2633             LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2634             if (0 != getuuidfromname(ibuf, UUID_USER, (unsigned char *)rbuf))
2635                 return AFPERR_NOITEM;
2636             *rbuflen = UUID_BINSIZE;
2637             break;
2638         case 6 :        /* groupname -> UUID */
2639             LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2640             if (0 != getuuidfromname(ibuf, UUID_GROUP, (unsigned char *)rbuf))
2641                 return AFPERR_NOITEM;
2642             *rbuflen = UUID_BINSIZE;
2643             break;
2644         }
2645     }
2646     return( AFP_OK );
2647 }
2648
2649 /* ------------------------------------
2650    variable DID support
2651 */
2652 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2653 {
2654 #if 0
2655     struct vol   *vol;
2656     struct dir   *dir;
2657     u_int16_t    vid;
2658     u_int32_t    did;
2659 #endif /* 0 */
2660
2661     *rbuflen = 0;
2662
2663     /* do nothing as dids are static for the life of the process. */
2664 #if 0
2665     ibuf += 2;
2666
2667     memcpy(&vid,  ibuf, sizeof( vid ));
2668     ibuf += sizeof( vid );
2669     if (( vol = getvolbyvid( vid )) == NULL ) {
2670         return( AFPERR_PARAM );
2671     }
2672
2673     memcpy( &did, ibuf, sizeof( did ));
2674     ibuf += sizeof( did );
2675     if (( dir = dirlookup( vol, did )) == NULL ) {
2676         return( AFPERR_PARAM );
2677     }
2678
2679     /* dir_remove -- deletedid */
2680 #endif /* 0 */
2681
2682     return AFP_OK;
2683 }
2684
2685 /* did creation gets done automatically
2686  * there's a pb again with case but move it to cname
2687  */
2688 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen  _U_, char *rbuf, size_t *rbuflen)
2689 {
2690     struct vol      *vol;
2691     struct dir      *parentdir;
2692     struct path     *path;
2693     u_int32_t       did;
2694     u_int16_t       vid;
2695
2696     *rbuflen = 0;
2697     ibuf += 2;
2698
2699     memcpy(&vid, ibuf, sizeof(vid));
2700     ibuf += sizeof( vid );
2701
2702     if (NULL == ( vol = getvolbyvid( vid )) ) {
2703         return( AFPERR_PARAM );
2704     }
2705
2706     memcpy(&did, ibuf, sizeof(did));
2707     ibuf += sizeof(did);
2708
2709     if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2710         return afp_errno;
2711     }
2712
2713     if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2714         return get_afp_errno(AFPERR_PARAM);
2715     }
2716
2717     if ( *path->m_name != '\0' ) {
2718         return path_error(path, AFPERR_NOOBJ);
2719     }
2720
2721     if ( !path->st_valid && of_stat(path ) < 0 ) {
2722         return( AFPERR_NOOBJ );
2723     }
2724     if ( path->st_errno ) {
2725         return( AFPERR_NOOBJ );
2726     }
2727
2728     memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2729     *rbuflen = sizeof(curdir->d_did);
2730     return AFP_OK;
2731 }