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