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