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