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