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