2 * $Id: directory.c,v 1.140 2010/03/12 15:16:49 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
23 #include <atalk/adouble.h>
24 #include <atalk/vfs.h>
25 #include <atalk/afp.h>
26 #include <atalk/util.h>
27 #include <atalk/cnid.h>
28 #include <atalk/logger.h>
29 #include <atalk/uuid.h>
30 #include <atalk/unix.h>
31 #include <atalk/bstrlib.h>
32 #include <atalk/bstradd.h>
34 #include "directory.h"
46 #ifdef HAVE_NFSv4_ACLS
47 extern void addir_inherit_acl(const struct vol *vol);
51 * FIXMEs, loose ends after the dircache rewrite:
52 * o merge dircache_search_by_name and dir_add ??
53 * o case-insensitivity is gone from cname
57 /*******************************************************************************************
59 ******************************************************************************************/
62 struct dir rootParent = {
63 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
64 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
65 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
67 struct dir *curdir = &rootParent;
68 struct path Cur_Path = {
73 NULL,/* struct dir * */
74 0, /* stat is not set */
80 /*******************************************************************************************
82 ******************************************************************************************/
85 /* -------------------------
86 appledouble mkdir afp error code.
88 static int netatalk_mkdir(const struct vol *vol, const char *name)
93 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
94 if (lstat(".", &st) < 0)
96 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
97 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
98 name, st.st_mode, vol->v_umask);
100 ret = mkdir(name, mode);
102 ret = ad_mkdir(name, DIRBITS | 0777);
108 return( AFPERR_NOOBJ );
110 return( AFPERR_VLOCK );
113 return( AFPERR_ACCESS );
115 return( AFPERR_EXIST );
118 return( AFPERR_DFULL );
120 return( AFPERR_PARAM );
126 /* ------------------- */
127 static int deletedir(int dirfd, char *dir)
129 char path[MAXPATHLEN + 1];
137 if ((len = strlen(dir)) +2 > sizeof(path))
141 if ((dp = opendirat(dirfd, dir)) == NULL)
147 remain = sizeof(path) -len -1;
148 while ((de = readdir(dp)) && err == AFP_OK) {
149 /* skip this and previous directory */
150 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
153 if (strlen(de->d_name) > remain) {
157 strcpy(path + len, de->d_name);
158 if (lstatat(dirfd, path, &st)) {
161 if (S_ISDIR(st.st_mode)) {
162 err = deletedir(dirfd, path);
164 err = netatalk_unlinkat(dirfd, path);
169 /* okay. the directory is empty. delete it. note: we already got rid
172 err = netatalk_rmdir(dirfd, dir);
177 /* do a recursive copy. */
178 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
180 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
189 /* doesn't exist or the path is too long. */
190 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
191 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
192 ((dp = opendirat(dirfd, src)) == NULL))
195 /* try to create the destination directory */
196 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
201 /* set things up to copy */
205 srem = sizeof(spath) - slen -1;
210 drem = sizeof(dpath) - dlen -1;
213 while ((de = readdir(dp))) {
214 /* skip this and previous directory */
215 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
218 if (strlen(de->d_name) > srem) {
222 strcpy(spath + slen, de->d_name);
224 if (lstatat(dirfd, spath, &st) == 0) {
225 if (strlen(de->d_name) > drem) {
229 strcpy(dpath + dlen, de->d_name);
231 if (S_ISDIR(st.st_mode)) {
232 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
234 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
238 /* keep the same time stamp. */
239 ut.actime = ut.modtime = st.st_mtime;
245 /* keep the same time stamp. */
246 if (lstatat(dirfd, src, &st) == 0) {
247 ut.actime = ut.modtime = st.st_mtime;
256 /* ---------------------
257 * is our cached offspring count valid?
259 static int diroffcnt(struct dir *dir, struct stat *st)
261 return st->st_ctime == dir->ctime;
264 /* --------------------- */
265 static int invisible_dots(const struct vol *vol, const char *name)
267 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
270 /* ------------------ */
271 static int set_dir_errors(struct path *path, const char *where, int err)
276 return AFPERR_ACCESS;
280 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
285 * @brief Convert name in client encoding to server encoding
287 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
288 * This only gets called from cname().
290 * @returns 0 on success, -1 on error
292 * @note If the passed ret->m_name is mangled, we'll demangle it
294 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
296 static char temp[ MAXPATHLEN + 1];
300 if (afp_version >= 30) {
302 if (dir->d_did == DIRDID_ROOT_PARENT) {
304 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
305 * So we compare it with the longname from the current volume and if they match
306 * we overwrite the requested path with the utf8 volume name so that the following
309 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
310 if (strcasecmp(ret->m_name, temp) == 0)
311 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
314 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
315 afp_errno = AFPERR_PARAM;
318 strcpy(ret->m_name, temp);
322 /* check for OS X mangled filename :( */
323 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
324 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
325 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
327 if (t != ret->m_name) {
329 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
330 * flags weren't the same
332 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
333 /* at last got our view of mac name */
334 strcpy(ret->m_name, t);
337 } /* afp_version >= 30 */
339 /* If we haven't got it by now, get it */
340 if (ret->u_name == NULL) {
341 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
342 afp_errno = AFPERR_PARAM;
351 * @brief Build struct path from struct dir
353 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
354 * 1. move cwd into parent dir (we're often already there, but not always)
355 * 2. set struct path to the dirname
357 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
358 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
359 * 4. indicate there's no dir for this path
362 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
364 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
370 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
373 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
374 if (dir->d_m_name == dir->d_u_name) {
375 ret->u_name = ret->m_name;
377 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
378 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
383 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
384 cfrombstring(dir->d_fullpath),
385 cfrombstring(curdir->d_fullpath),
391 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
394 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
395 if (dir->d_m_name == dir->d_u_name) {
396 ret->u_name = ret->m_name;
398 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
399 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
402 ret->d_dir = NULL; /* 4 */
403 dir_remove(vol, dir); /* 5 */
410 /* DEADC0DE: never get here */
415 /*********************************************************************************************
417 ********************************************************************************************/
419 int get_afp_errno(const int param)
421 if (afp_errno != AFPERR_DID1)
427 * @brief Resolve a DID
429 * Resolve a DID, allocate a struct dir for it
430 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
431 * 2. Check if the DID is in the cache.
432 * 3. If it's not in the cache resolve it via the database.
433 * 4. Build complete server-side path to the dir.
434 * 5. Check if it exists and is a directory.
435 * 6. Create the struct dir and populate it.
436 * 7. Add it to the cache.
438 * @param vol (r) pointer to struct vol
439 * @param did (r) DID to resolve
441 * @returns pointer to struct dir
443 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
444 * and we are really bad in this case.
446 struct dir *dirlookup(const struct vol *vol, cnid_t did)
448 static char buffer[12 + MAXPATHLEN + 1];
450 struct dir *ret = NULL, *pdir;
451 bstring fullpath = NULL;
452 char *upath = NULL, *mpath;
455 int buflen = 12 + MAXPATHLEN + 1;
459 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
461 /* check for did 0, 1 and 2 */
462 if (did == 0 || vol == NULL) { /* 1 */
463 afp_errno = AFPERR_PARAM;
465 } else if (did == DIRDID_ROOT_PARENT) {
466 rootParent.d_vid = vol->v_vid;
467 return (&rootParent);
468 } else if (did == DIRDID_ROOT) {
472 /* Search the cache */
473 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
474 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
475 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
479 /* It's not there anymore, so remove it */
480 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
481 dir_remove(vol, ret);
482 afp_errno = AFPERR_NOOBJ;
493 utf8 = utf8_encoding();
494 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
496 /* Get it from the database */
498 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL
499 || (upath = strdup(upath)) == NULL) { /* 3 */
500 afp_errno = AFPERR_NOOBJ;
507 * Recurse up the tree, terminates in dirlookup when either
508 * - DIRDID_ROOT is hit
509 * - a cached entry is found
511 if ((pdir = dirlookup(vol, pdid)) == NULL) {
516 /* build the fullpath */
517 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
518 || bconchar(fullpath, '/') != BSTR_OK
519 || bcatcstr(fullpath, upath) != BSTR_OK) {
524 /* stat it and check if it's a dir */
525 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
527 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
530 afp_errno = AFPERR_NOOBJ;
534 afp_errno = AFPERR_ACCESS;
538 afp_errno = AFPERR_MISC;
543 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
544 afp_errno = AFPERR_BADTYPE;
550 /* Get macname from unix name */
551 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
552 afp_errno = AFPERR_NOOBJ;
557 /* Create struct dir */
558 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
559 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
564 /* Add it to the cache only if it's a dir */
565 if (dircache_add(ret) != 0) { /* 7 */
570 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
571 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
575 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
576 ntohl(did), AfpErr2name(afp_errno));
588 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
590 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
595 static u_int32_t did = 0;
596 static char cname[MAXPATHLEN];
597 static char lname[MAXPATHLEN];
598 ucs2_t u2_path[MAXPATHLEN];
599 ucs2_t u2_dename[MAXPATHLEN];
600 char *tmp, *savepath;
602 if (!(vol->v_flags & AFPVOL_CASEINSEN))
605 if (veto_file(ENUMVETO, path->u_name))
608 savepath = path->u_name;
610 /* very simple cache */
611 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
612 path->u_name = cname;
614 if (of_stat( path ) == 0 ) {
617 /* something changed, we cannot stat ... */
621 if (NULL == ( dp = opendir( "." )) ) {
622 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
627 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
628 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
629 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
631 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
633 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
634 if (NULL == check_dirent(vol, de->d_name))
637 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
640 if (strcasecmp_w( u2_path, u2_dename) == 0) {
642 strlcpy(cname, de->d_name, sizeof(cname));
643 path->u_name = cname;
645 if (of_stat( path ) == 0 ) {
646 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
647 strlcpy(lname, tmp, sizeof(lname));
660 /* invalidate cache */
663 path->u_name = savepath;
665 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
671 * @brief Construct struct dir
673 * Construct struct dir from parameters.
675 * @param m_name (r) directory name in UTF8-dec
676 * @param u_name (r) directory name in server side encoding
677 * @param vol (r) pointer to struct vol
678 * @param pdid (r) Parent CNID
679 * @param did (r) CNID
680 * @param fullpath (r) Full unix path to dir
682 * @returns pointer to new struct dir or NULL on error
684 * @note Most of the time mac name and unix name are the same.
686 struct dir *dir_new(const char *m_name,
688 const struct vol *vol,
695 dir = (struct dir *) calloc(1, sizeof( struct dir ));
699 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
704 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
707 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
708 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
709 dir->d_m_name_ucs2 = NULL;
712 if (m_name == u_name || !strcmp(m_name, u_name)) {
713 dir->d_u_name = dir->d_m_name;
715 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
716 bdestroy(dir->d_m_name);
723 dir->d_vid = vol->v_vid;
724 dir->d_fullpath = path;
729 * @brief Free a struct dir and all its members
731 * @param (rw) pointer to struct dir
733 void dir_free(struct dir *dir)
735 if (dir->d_u_name != dir->d_m_name) {
736 bdestroy(dir->d_u_name);
738 if (dir->d_m_name_ucs2)
739 free(dir->d_m_name_ucs2);
740 bdestroy(dir->d_m_name);
741 bdestroy(dir->d_fullpath);
746 * @brief Create struct dir from struct path
748 * Create a new struct dir from struct path. Then add it to the cache.
749 * The caller must have assured that the dir is not already in the cache,
750 * cf theAFP_ASSERTion.
751 * 1. Open adouble file, get CNID from it.
752 * 2. Search the database, hinting with the CNID from (1).
753 * 3. Build fullpath and create struct dir.
754 * 4. Add it to the cache.
756 * @param vol (r) pointer to struct vol
757 * @param dir (r) pointer to parrent directory
758 * @param path (rw) pointer to struct path with valid path->u_name
759 * @param len (r) strlen of path->u_name
761 * @returns Pointer to new struct dir or NULL on error.
763 * @note Function also assigns path->m_name from path->u_name.
765 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
768 struct dir *cdir = NULL;
771 struct adouble *adp = NULL;
779 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
780 /* there's a stray entry in the dircache */
781 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
782 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
783 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
784 if (dir_remove(vol, cdir) != 0) {
790 /* get_id needs adp for reading CNID from adouble file */
791 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
792 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
796 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
802 ad_close_metadata(adp);
804 /* Get macname from unixname */
805 if (path->m_name == NULL) {
806 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
813 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
814 || (bconchar(fullpath, '/') != BSTR_OK)
815 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
816 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
821 /* Allocate and initialize struct dir */
822 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
827 if ((dircache_add(cdir)) != 0) { /* 4 */
828 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
834 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
835 cfrombstring(dir->d_u_name), path->u_name, err);
838 ad_close_metadata(adp);
839 if (!cdir && fullpath)
846 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
847 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
848 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
855 * @brief Remove a dir from a cache and free it and any ressources with it
857 * 1. Check if the dir is locked or has opened forks
858 * 2. If it's a request to remove curdir, just chdir to volume root
859 * 3. Remove it from the cache
860 * 4. Remove the dir plus any allocated resources it references
862 * @param (r) pointer to struct vol
863 * @param (rw) pointer to struct dir
865 int dir_remove(const struct vol *vol, struct dir *dir)
870 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
873 if (dir->d_flags & DIRF_CACHELOCK || dir->d_ofork) { /* 1 */
874 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): dir is locked or has opened forks",
875 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
879 if (curdir == dir) { /* 2 */
880 if (movecwd(vol, vol->v_root) < 0) {
881 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
885 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
886 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
888 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 3 */
889 dir_free(dir); /* 4 */
895 * @brief Modify a struct dir, adjust cache
897 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
898 * If given new_uname == new_mname, new_uname will point to new_mname.
900 * @param vol (r) pointer to struct vol
901 * @param dir (rw) pointer to struct dir
902 * @param pdid (r) new parent DID
903 * @param did (r) new DID
904 * @param new_mname (r) new mac-name
905 * @param new_uname (r) new unix-name
907 int dir_modify(const struct vol *vol,
911 const char *new_mname,
912 const char *new_uname,
913 bstring pdir_fullpath)
917 /* Remove it from the cache */
918 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
926 /* free uname if it's not the same as mname */
927 if (dir->d_m_name != dir->d_u_name)
928 bdestroy(dir->d_u_name);
930 if (new_uname == NULL)
931 new_uname = new_mname;
933 /* assign new name */
934 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
935 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
939 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
940 dir->d_u_name = dir->d_m_name;
942 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
943 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
950 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
952 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
954 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
958 if (dir->d_m_name_ucs2)
959 free(dir->d_m_name_ucs2);
960 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))
961 dir->d_m_name_ucs2 = NULL;
963 /* Re-add it to the cache */
964 if ((dircache_add(dir)) != 0) {
973 * @brief Resolve a catalog node name path
975 * 1. Evaluate path type
976 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
977 * path from dirname, so eg getdirparams has sth it can chew on. curdir
978 * is dir parent then. All this is done in path_from_dir().
979 * 3. Parse next cnode name in path, cases:
980 * 4. single "\0" -> do nothing
981 * 5. two or more consecutive "\0" -> chdir("..") one or more times
982 * 6. cnode name -> copy it to path.m_name
983 * 7. Get unix name from mac name
984 * 8. Special handling of request with did 1
985 * 9. stat the cnode name
986 * 10. If it's not there, it's probably an afp_createfile|dir,
987 * return with curdir = dir parent, struct path = dirname
988 * 11. If it's there and it's a file, it must should be the last element of the requested
989 * path. Return with curdir = cnode name parent dir, struct path = filename
990 * 12. Treat symlinks like files, dont follow them
992 * 14. Search the dircache for it
993 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
994 * 16. chdir into the dir and
995 * 17. set m_name to the mac equivalent of "."
998 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1000 static char path[ MAXPATHLEN + 1];
1001 static struct path ret;
1011 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1014 afp_errno = AFPERR_NOOBJ;
1015 memset(&ret, 0, sizeof(ret));
1017 switch (ret.m_type = *data) { /* 1 */
1020 len = (unsigned char) *data++;
1022 if (afp_version >= 30) {
1028 if (afp_version >= 30) {
1030 memcpy(&hint, data, sizeof(hint));
1032 data += sizeof(hint);
1034 memcpy(&len16, data, sizeof(len16));
1040 /* else it's an error */
1042 afp_errno = AFPERR_PARAM;
1045 *cpath += len + size;
1050 if (movecwd(vol, dir) < 0 ) {
1051 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1052 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1054 return path_from_dir(vol, dir, &ret);
1059 while (len) { /* 3 */
1060 if (*data == 0) { /* 4 or 5 */
1063 while (len > 0 && *data == 0) { /* 5 */
1064 /* chdir to parrent dir */
1065 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1067 if (movecwd( vol, dir ) < 0 ) {
1068 dir_remove(vol, dir);
1078 for ( p = path; *data != 0 && len > 0; len-- ) {
1080 if (p > &path[ MAXPATHLEN]) {
1081 afp_errno = AFPERR_PARAM;
1085 *p = 0; /* Terminate string */
1088 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1089 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1093 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1095 /* Prevent access to our special folders like .AppleDouble */
1096 if (check_name(vol, ret.u_name)) {
1097 /* the name is illegal */
1098 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1099 afp_errno = AFPERR_PARAM;
1103 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1105 * Special case: CNID 1
1106 * root parent (did 1) has one child: the volume. Requests for did=1 with
1107 * some <name> must check against the volume name.
1109 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1115 * CNID != 1, eg. most of the times we take this way.
1116 * Now check if current path-part is a file or dir:
1117 * o if it's dir we have to step into it
1118 * o if it's a file we expect it to be the last part of the requested path
1119 * and thus call continue which should terminate the while loop because
1122 if (of_stat(&ret) != 0) { /* 9 */
1124 * ret.u_name doesn't exist, might be afp_createfile|dir
1125 * that means it should have been the last part
1128 /* it wasn't the last part, so we have a bogus path request */
1129 afp_errno = AFPERR_NOOBJ;
1133 * this will terminate clean in while (1) because len == 0,
1134 * probably afp_createfile|dir
1136 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1140 switch (ret.st.st_mode & S_IFMT) {
1141 case S_IFREG: /* 11 */
1142 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1144 /* it wasn't the last part, so we have a bogus path request */
1145 afp_errno = AFPERR_PARAM;
1148 continue; /* continues while loop */
1149 case S_IFLNK: /* 12 */
1150 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1152 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1153 afp_errno = AFPERR_PARAM;
1156 continue; /* continues while loop */
1157 case S_IFDIR: /* 13 */
1160 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1161 afp_errno = AFPERR_NODIR;
1165 /* Search the cache */
1166 int unamelen = strlen(ret.u_name);
1167 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1169 /* Not in cache, create one */
1170 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1171 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1172 ntohl(dir->d_did), ret.u_name, getcwdpath());
1176 } /* if/else cnid==1 */
1178 /* Now chdir to the evaluated dir */
1179 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1180 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1181 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1183 return path_from_dir(vol, cdir, &ret);
1188 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1191 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1192 afp_errno = AFPERR_DID1;
1196 if (ret.m_name[0] == 0) {
1197 /* Last part was a dir */
1198 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1202 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1203 cfrombstring(dir->d_fullpath),
1204 cfrombstring(curdir->d_fullpath),
1211 * @brief chdir() to dir
1213 * @param vol (r) pointer to struct vol
1214 * @param dir (r) pointer to struct dir
1216 * @returns 0 on success, -1 on error with afp_errno set appropiately
1218 int movecwd(const struct vol *vol, struct dir *dir)
1225 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1226 cfrombstring(curdir->d_fullpath), getcwdpath());
1230 if (dir->d_did == DIRDID_ROOT_PARENT) {
1231 curdir = &rootParent;
1235 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1237 if ((ret = lchdir(cfrombstring(dir->d_fullpath))) != 0 ) {
1238 LOG(log_debug, logtype_afpd, "movecwd('%s'): ret: %u, %s",
1239 cfrombstring(dir->d_fullpath), ret, strerror(errno));
1241 /* p is a symlink or getcwd failed */
1242 afp_errno = AFPERR_BADTYPE;
1244 if (chdir(vol->v_path ) < 0) {
1245 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1246 /* XXX what do we do here? */
1248 curdir = vol->v_root;
1255 afp_errno = AFPERR_ACCESS;
1258 afp_errno = AFPERR_NOOBJ;
1268 * We can't use unix file's perm to support Apple's inherited protection modes.
1269 * If we aren't the file's owner we can't change its perms when moving it and smb
1270 * nfs,... don't even try.
1272 #define AFP_CHECK_ACCESS
1274 int check_access(char *path, int mode)
1276 #ifdef AFP_CHECK_ACCESS
1284 accessmode(p, &ma, curdir, NULL);
1285 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1287 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1293 /* --------------------- */
1294 int file_access(struct path *path, int mode)
1298 accessmode(path->u_name, &ma, curdir, &path->st);
1299 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1301 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1307 /* --------------------- */
1308 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1310 dir->offcnt = count;
1311 dir->ctime = st->st_ctime;
1312 dir->d_flags &= ~DIRF_CNID;
1316 /* ---------------------
1317 * is our cached also for reenumerate id?
1319 int dirreenumerate(struct dir *dir, struct stat *st)
1321 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1324 /* ------------------------------
1326 (name, dir) with curdir:name == dir, from afp_enumerate
1329 int getdirparams(const struct vol *vol,
1330 u_int16_t bitmap, struct path *s_path,
1332 char *buf, size_t *buflen )
1336 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1337 int bit = 0, isad = 0;
1343 struct stat *st = &s_path->st;
1344 char *upath = s_path->u_name;
1346 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1347 (1 << DIRPBIT_CDATE) |
1348 (1 << DIRPBIT_MDATE) |
1349 (1 << DIRPBIT_BDATE) |
1350 (1 << DIRPBIT_FINFO)))) {
1352 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1353 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1355 if (ad.ad_md->adf_flags & O_CREAT) {
1356 /* We just created it */
1357 ad_setname(&ad, s_path->m_name);
1372 while ( bitmap != 0 ) {
1373 while (( bitmap & 1 ) == 0 ) {
1381 ad_getattr(&ad, &ashort);
1382 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1383 ashort = htons(ATTRBIT_INVISIBLE);
1386 ashort |= htons(ATTRBIT_SHARED);
1387 memcpy( data, &ashort, sizeof( ashort ));
1388 data += sizeof( ashort );
1392 memcpy( data, &pdid, sizeof( pdid ));
1393 data += sizeof( pdid );
1394 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1395 s_path->u_name, ntohl(pdid));
1398 case DIRPBIT_CDATE :
1399 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1400 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1401 memcpy( data, &aint, sizeof( aint ));
1402 data += sizeof( aint );
1405 case DIRPBIT_MDATE :
1406 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1407 memcpy( data, &aint, sizeof( aint ));
1408 data += sizeof( aint );
1411 case DIRPBIT_BDATE :
1412 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1413 aint = AD_DATE_START;
1414 memcpy( data, &aint, sizeof( aint ));
1415 data += sizeof( aint );
1418 case DIRPBIT_FINFO :
1420 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1421 } else { /* no appledouble */
1422 memset( data, 0, 32 );
1423 /* set default view -- this also gets done in ad_open() */
1424 ashort = htons(FINDERINFO_CLOSEDVIEW);
1425 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1427 /* dot files are by default visible */
1428 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1429 ashort = htons(FINDERINFO_INVISIBLE);
1430 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1436 case DIRPBIT_LNAME :
1437 if (dir->d_m_name) /* root of parent can have a null name */
1440 memset(data, 0, sizeof(u_int16_t));
1441 data += sizeof( u_int16_t );
1444 case DIRPBIT_SNAME :
1445 memset(data, 0, sizeof(u_int16_t));
1446 data += sizeof( u_int16_t );
1450 memcpy( data, &dir->d_did, sizeof( aint ));
1451 data += sizeof( aint );
1452 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1453 s_path->u_name, ntohl(dir->d_did));
1456 case DIRPBIT_OFFCNT :
1458 /* this needs to handle current directory access rights */
1459 if (diroffcnt(dir, st)) {
1460 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1462 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1463 setdiroffcnt(dir, st, ret);
1464 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1466 ashort = htons( ashort );
1467 memcpy( data, &ashort, sizeof( ashort ));
1468 data += sizeof( ashort );
1472 aint = htonl(st->st_uid);
1473 memcpy( data, &aint, sizeof( aint ));
1474 data += sizeof( aint );
1478 aint = htonl(st->st_gid);
1479 memcpy( data, &aint, sizeof( aint ));
1480 data += sizeof( aint );
1483 case DIRPBIT_ACCESS :
1484 accessmode( upath, &ma, dir , st);
1486 *data++ = ma.ma_user;
1487 *data++ = ma.ma_world;
1488 *data++ = ma.ma_group;
1489 *data++ = ma.ma_owner;
1492 /* Client has requested the ProDOS information block.
1493 Just pass back the same basic block for all
1494 directories. <shirsch@ibm.net> */
1495 case DIRPBIT_PDINFO :
1496 if (afp_version >= 30) { /* UTF8 name */
1497 utf8 = kTextEncodingUTF8;
1498 if (dir->d_m_name) /* root of parent can have a null name */
1501 memset(data, 0, sizeof(u_int16_t));
1502 data += sizeof( u_int16_t );
1504 memcpy(data, &aint, sizeof( aint ));
1505 data += sizeof( aint );
1507 else { /* ProDOS Info Block */
1510 ashort = htons( 0x0200 );
1511 memcpy( data, &ashort, sizeof( ashort ));
1512 data += sizeof( ashort );
1513 memset( data, 0, sizeof( ashort ));
1514 data += sizeof( ashort );
1518 case DIRPBIT_UNIXPR :
1519 aint = htonl(st->st_uid);
1520 memcpy( data, &aint, sizeof( aint ));
1521 data += sizeof( aint );
1522 aint = htonl(st->st_gid);
1523 memcpy( data, &aint, sizeof( aint ));
1524 data += sizeof( aint );
1527 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1528 memcpy( data, &aint, sizeof( aint ));
1529 data += sizeof( aint );
1531 accessmode( upath, &ma, dir , st);
1533 *data++ = ma.ma_user;
1534 *data++ = ma.ma_world;
1535 *data++ = ma.ma_group;
1536 *data++ = ma.ma_owner;
1541 ad_close_metadata( &ad );
1543 return( AFPERR_BITMAP );
1549 ashort = htons( data - buf );
1550 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1551 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1553 if ( utf_nameoff ) {
1554 ashort = htons( data - buf );
1555 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1556 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1559 ad_close_metadata( &ad );
1561 *buflen = data - buf;
1565 /* ----------------------------- */
1566 int path_error(struct path *path, int error)
1568 /* - a dir with access error
1569 * - no error it's a file
1572 if (path_isadir(path))
1574 if (path->st_valid && path->st_errno)
1576 return AFPERR_BADTYPE ;
1579 /* ----------------------------- */
1580 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1585 u_int16_t vid, bitmap;
1591 memcpy( &vid, ibuf, sizeof( vid ));
1592 ibuf += sizeof( vid );
1594 if (NULL == ( vol = getvolbyvid( vid )) ) {
1595 return( AFPERR_PARAM );
1598 if (vol->v_flags & AFPVOL_RO)
1599 return AFPERR_VLOCK;
1601 memcpy( &did, ibuf, sizeof( did ));
1602 ibuf += sizeof( int );
1604 if (NULL == ( dir = dirlookup( vol, did )) ) {
1608 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1609 bitmap = ntohs( bitmap );
1610 ibuf += sizeof( bitmap );
1612 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1613 return get_afp_errno(AFPERR_NOOBJ);
1616 if ( *path->m_name != '\0' ) {
1617 rc = path_error(path, AFPERR_NOOBJ);
1618 /* maybe we are trying to set perms back */
1619 if (rc != AFPERR_ACCESS)
1624 * If ibuf is odd, make it even.
1626 if ((u_long)ibuf & 1 ) {
1630 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1631 setvoltime(obj, vol );
1637 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1639 * assume path == '\0' eg. it's a directory in canonical form
1641 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1653 u_int16_t ashort, bshort, oshort;
1655 int change_mdate = 0;
1656 int change_parent_mdate = 0;
1658 u_int16_t bitmap = d_bitmap;
1659 u_char finder_buf[32];
1662 u_int16_t upriv_bit = 0;
1665 upath = path->u_name;
1667 while ( bitmap != 0 ) {
1668 while (( bitmap & 1 ) == 0 ) {
1676 memcpy( &ashort, buf, sizeof( ashort ));
1677 buf += sizeof( ashort );
1679 case DIRPBIT_CDATE :
1681 memcpy(&cdate, buf, sizeof(cdate));
1682 buf += sizeof( cdate );
1684 case DIRPBIT_MDATE :
1685 memcpy(&newdate, buf, sizeof(newdate));
1686 buf += sizeof( newdate );
1688 case DIRPBIT_BDATE :
1690 memcpy(&bdate, buf, sizeof(bdate));
1691 buf += sizeof( bdate );
1693 case DIRPBIT_FINFO :
1695 memcpy( finder_buf, buf, 32 );
1698 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1699 change_parent_mdate = 1;
1700 memcpy( &owner, buf, sizeof(owner));
1701 buf += sizeof( owner );
1704 change_parent_mdate = 1;
1705 memcpy( &group, buf, sizeof( group ));
1706 buf += sizeof( group );
1708 case DIRPBIT_ACCESS :
1710 change_parent_mdate = 1;
1711 ma.ma_user = *buf++;
1712 ma.ma_world = *buf++;
1713 ma.ma_group = *buf++;
1714 ma.ma_owner = *buf++;
1715 mpriv = mtoumode( &ma ) | vol->v_dperm;
1716 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1717 err = set_dir_errors(path, "setdirmode", errno);
1721 /* Ignore what the client thinks we should do to the
1722 ProDOS information block. Skip over the data and
1723 report nothing amiss. <shirsch@ibm.net> */
1724 case DIRPBIT_PDINFO :
1725 if (afp_version < 30) {
1729 err = AFPERR_BITMAP;
1733 case DIRPBIT_UNIXPR :
1734 if (vol_unix_priv(vol)) {
1735 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1736 buf += sizeof( owner );
1737 memcpy( &group, buf, sizeof( group ));
1738 buf += sizeof( group );
1741 change_parent_mdate = 1;
1742 memcpy( &upriv, buf, sizeof( upriv ));
1743 buf += sizeof( upriv );
1744 upriv = ntohl (upriv) | vol->v_dperm;
1745 if (dir_rx_set(upriv)) {
1746 /* maybe we are trying to set perms back */
1747 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1749 err = set_dir_errors(path, "setdirunixmode", errno);
1760 err = AFPERR_BITMAP;
1768 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1770 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1772 * Check to see what we're trying to set. If it's anything
1773 * but ACCESS, UID, or GID, give an error. If it's any of those
1774 * three, we don't need the ad to be open, so just continue.
1776 * note: we also don't need to worry about mdate. also, be quiet
1777 * if we're using the noadouble option.
1779 if (!vol_noadouble(vol) && (d_bitmap &
1780 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1781 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1782 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1783 return AFPERR_ACCESS;
1789 * Check to see if a create was necessary. If it was, we'll want
1790 * to set our name, etc.
1792 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1793 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1799 while ( bitmap != 0 ) {
1800 while (( bitmap & 1 ) == 0 ) {
1808 ad_getattr(&ad, &bshort);
1810 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1811 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1815 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1816 change_parent_mdate = 1;
1817 ad_setattr(&ad, bshort);
1820 case DIRPBIT_CDATE :
1822 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1825 case DIRPBIT_MDATE :
1827 case DIRPBIT_BDATE :
1829 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1832 case DIRPBIT_FINFO :
1834 /* Fixes #2802236 */
1835 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1836 *fflags &= htons(~FINDERINFO_ISHARED);
1838 if ( dir->d_did == DIRDID_ROOT ) {
1840 * Alright, we admit it, this is *really* sick!
1841 * The 4 bytes that we don't copy, when we're dealing
1842 * with the root of a volume, are the directory's
1843 * location information. This eliminates that annoying
1844 * behavior one sees when mounting above another mount
1847 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1848 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1850 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1854 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1855 if ( (dir->d_did == DIRDID_ROOT) &&
1856 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1857 err = set_dir_errors(path, "setdeskowner", errno);
1858 if (isad && err == AFPERR_PARAM) {
1859 err = AFP_OK; /* ???*/
1862 goto setdirparam_done;
1865 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1866 err = set_dir_errors(path, "setdirowner", errno);
1867 goto setdirparam_done;
1871 if (dir->d_did == DIRDID_ROOT)
1872 setdeskowner( -1, ntohl(group) );
1873 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1874 err = set_dir_errors(path, "setdirowner", errno);
1875 goto setdirparam_done;
1878 case DIRPBIT_ACCESS :
1879 if (dir->d_did == DIRDID_ROOT) {
1881 if (!dir_rx_set(mpriv)) {
1882 /* we can't remove read and search for owner on volume root */
1883 err = AFPERR_ACCESS;
1884 goto setdirparam_done;
1888 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1889 err = set_dir_errors(path, "setdirmode", errno);
1890 goto setdirparam_done;
1893 case DIRPBIT_PDINFO :
1894 if (afp_version >= 30) {
1895 err = AFPERR_BITMAP;
1896 goto setdirparam_done;
1899 case DIRPBIT_UNIXPR :
1900 if (vol_unix_priv(vol)) {
1901 if (dir->d_did == DIRDID_ROOT) {
1902 if (!dir_rx_set(upriv)) {
1903 /* we can't remove read and search for owner on volume root */
1904 err = AFPERR_ACCESS;
1905 goto setdirparam_done;
1907 setdeskowner( -1, ntohl(group) );
1908 setdeskmode( upriv );
1910 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1911 err = set_dir_errors(path, "setdirowner", errno);
1912 goto setdirparam_done;
1915 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1916 err = set_dir_errors(path, "setdirunixmode", errno);
1917 goto setdirparam_done;
1921 err = AFPERR_BITMAP;
1922 goto setdirparam_done;
1926 err = AFPERR_BITMAP;
1927 goto setdirparam_done;
1936 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1937 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1941 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1942 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1947 if (path->st_valid && !path->st_errno) {
1948 struct stat *st = &path->st;
1950 if (dir && dir->d_pdid) {
1951 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1955 ad_close_metadata( &ad);
1958 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1959 && gettimeofday(&tv, NULL) == 0) {
1960 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1961 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1962 /* be careful with bitmap because now dir is null */
1963 bitmap = 1<<DIRPBIT_MDATE;
1964 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1965 /* should we reset curdir ?*/
1972 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1986 memcpy( &vid, ibuf, sizeof( vid ));
1987 ibuf += sizeof( vid );
1988 if (NULL == (vol = getvolbyvid( vid )) ) {
1989 return( AFPERR_PARAM );
1992 memcpy( &did, ibuf, sizeof( did ));
1993 ibuf += sizeof( did );
1997 * if it's CNID 2 our only choice to meet the specs is call sync.
1998 * For any other CNID just sync that dir. To my knowledge the
1999 * intended use of FPSyncDir is to sync the volume so all we're
2000 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2003 if ( ntohl(did) == 2 ) {
2006 if (NULL == ( dir = dirlookup( vol, did )) ) {
2007 return afp_errno; /* was AFPERR_NOOBJ */
2010 if (movecwd( vol, dir ) < 0 )
2011 return ( AFPERR_NOOBJ );
2014 * Assuming only OSens that have dirfd also may require fsyncing directories
2015 * in order to flush metadata e.g. Linux.
2019 if (NULL == ( dp = opendir( "." )) ) {
2022 return( AFPERR_NOOBJ );
2024 return( AFPERR_ACCESS );
2026 return( AFPERR_PARAM );
2030 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2033 if ( fsync ( dfd ) < 0 )
2034 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2035 dir->d_u_name, strerror(errno) );
2036 closedir(dp); /* closes dfd too */
2039 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2042 return( AFPERR_NOOBJ );
2044 return( AFPERR_ACCESS );
2046 return( AFPERR_PARAM );
2050 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2051 vol->ad_path(".", ADFLAGS_DIR) );
2053 if ( fsync(dfd) < 0 )
2054 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2055 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2062 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2068 struct path *s_path;
2076 memcpy( &vid, ibuf, sizeof( vid ));
2077 ibuf += sizeof( vid );
2078 if (NULL == ( vol = getvolbyvid( vid )) ) {
2079 return( AFPERR_PARAM );
2082 if (vol->v_flags & AFPVOL_RO)
2083 return AFPERR_VLOCK;
2085 memcpy( &did, ibuf, sizeof( did ));
2086 ibuf += sizeof( did );
2087 if (NULL == ( dir = dirlookup( vol, did )) ) {
2088 return afp_errno; /* was AFPERR_NOOBJ */
2090 /* for concurrent access we need to be sure we are not in the
2091 * folder we want to create...
2095 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2096 return get_afp_errno(AFPERR_PARAM);
2098 /* cname was able to move curdir to it! */
2099 if (*s_path->m_name == '\0')
2100 return AFPERR_EXIST;
2102 upath = s_path->u_name;
2104 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2108 if (of_stat(s_path) < 0) {
2114 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2118 if ( movecwd( vol, dir ) < 0 ) {
2119 return( AFPERR_PARAM );
2122 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2123 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2124 if (vol_noadouble(vol))
2125 goto createdir_done;
2126 return( AFPERR_ACCESS );
2128 ad_setname(&ad, s_path->m_name);
2129 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2132 ad_close_metadata( &ad);
2135 #ifdef HAVE_NFSv4_ACLS
2136 /* FIXME: are we really inside the created dir? */
2137 addir_inherit_acl(vol);
2140 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2141 *rbuflen = sizeof( u_int32_t );
2142 setvoltime(obj, vol );
2147 * dst new unix filename (not a pathname)
2148 * newname new mac name
2150 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2152 int renamedir(const struct vol *vol,
2157 struct dir *newparent,
2163 /* existence check moved to afp_moveandrename */
2164 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2167 return( AFPERR_NOOBJ );
2169 return( AFPERR_ACCESS );
2171 return AFPERR_VLOCK;
2173 /* tried to move directory into a subdirectory of itself */
2174 return AFPERR_CANTMOVE;
2176 /* this needs to copy and delete. bleah. that means we have
2177 * to deal with entire directory hierarchies. */
2178 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2182 if ((err = deletedir(dirfd, src)) < 0)
2186 return( AFPERR_PARAM );
2190 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2192 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2194 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2195 ad_setname(&ad, newname);
2197 ad_close_metadata( &ad);
2200 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2201 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2208 /* delete an empty directory */
2209 int deletecurdir(struct vol *vol)
2219 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2220 return( AFPERR_ACCESS );
2225 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2226 /* we never want to create a resource fork here, we are going to delete it */
2227 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2229 ad_getattr(&ad, &ashort);
2230 ad_close_metadata(&ad);
2231 if ((ashort & htons(ATTRBIT_NODELETE))) {
2232 return AFPERR_OLOCK;
2235 err = vol->vfs->vfs_deletecurdir(vol);
2240 /* now get rid of dangling symlinks */
2241 if ((dp = opendir("."))) {
2242 while ((de = readdir(dp))) {
2243 /* skip this and previous directory */
2244 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2247 /* bail if it's not a symlink */
2248 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2250 return AFPERR_DIRNEMPT;
2253 if ((err = netatalk_unlink(de->d_name))) {
2260 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2265 err = netatalk_rmdir_all_errors(-1, cfrombstring(fdir->d_u_name));
2266 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2267 cnid_delete(vol->v_cdb, fdir->d_did);
2268 dir_remove( vol, fdir );
2272 /* inode is used as key for cnid.
2273 * Close the descriptor only after cnid_delete
2281 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2291 sfunc = (unsigned char) *ibuf++;
2295 if (sfunc >= 3 && sfunc <= 6) {
2296 if (afp_version < 30) {
2297 return( AFPERR_PARAM );
2304 case 3 :/* unicode */
2305 memcpy( &id, ibuf, sizeof( id ));
2308 if (( pw = getpwuid( id )) == NULL ) {
2309 return( AFPERR_NOITEM );
2311 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2312 pw->pw_name, -1, &name);
2319 case 4 : /* unicode */
2320 memcpy( &id, ibuf, sizeof( id ));
2323 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2324 return( AFPERR_NOITEM );
2326 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2327 gr->gr_name, -1, &name);
2333 #ifdef HAVE_NFSv4_ACLS
2334 case 5 : /* UUID -> username */
2335 case 6 : /* UUID -> groupname */
2336 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2337 return AFPERR_PARAM;
2338 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2340 len = getnamefromuuid( ibuf, &name, &type);
2341 if (len != 0) /* its a error code, not len */
2342 return AFPERR_NOITEM;
2343 if (type == UUID_USER) {
2344 if (( pw = getpwnam( name )) == NULL )
2345 return( AFPERR_NOITEM );
2346 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2347 id = htonl(UUID_USER);
2348 memcpy( rbuf, &id, sizeof( id ));
2349 id = htonl( pw->pw_uid);
2350 rbuf += sizeof( id );
2351 memcpy( rbuf, &id, sizeof( id ));
2352 rbuf += sizeof( id );
2353 *rbuflen = 2 * sizeof( id );
2354 } else { /* type == UUID_GROUP */
2355 if (( gr = getgrnam( name )) == NULL )
2356 return( AFPERR_NOITEM );
2357 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2358 id = htonl(UUID_GROUP);
2359 memcpy( rbuf, &id, sizeof( id ));
2360 rbuf += sizeof( id );
2361 id = htonl( gr->gr_gid);
2362 memcpy( rbuf, &id, sizeof( id ));
2363 rbuf += sizeof( id );
2364 *rbuflen = 2 * sizeof( id );
2369 return( AFPERR_PARAM );
2373 len = strlen( name );
2376 u_int16_t tp = htons(len);
2377 memcpy(rbuf, &tp, sizeof(tp));
2386 memcpy( rbuf, name, len );
2394 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2403 sfunc = (unsigned char) *ibuf++;
2405 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2408 case 2 : /* unicode */
2409 if (afp_version < 30) {
2410 return( AFPERR_PARAM );
2412 memcpy(&ulen, ibuf, sizeof(ulen));
2415 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2419 len = (unsigned char) *ibuf++;
2421 #ifdef HAVE_NFSv4_ACLS
2422 case 5 : /* username -> UUID */
2423 case 6 : /* groupname -> UUID */
2424 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2425 return AFPERR_PARAM;
2426 memcpy(&ulen, ibuf, sizeof(ulen));
2432 return( AFPERR_PARAM );
2438 return AFPERR_PARAM;
2441 case 1 : /* unicode */
2443 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2444 return( AFPERR_NOITEM );
2448 memcpy( rbuf, &id, sizeof( id ));
2449 *rbuflen = sizeof( id );
2452 case 2 : /* unicode */
2454 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2455 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2456 return( AFPERR_NOITEM );
2459 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2461 memcpy( rbuf, &id, sizeof( id ));
2462 *rbuflen = sizeof( id );
2464 #ifdef HAVE_NFSv4_ACLS
2465 case 5 : /* username -> UUID */
2466 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2467 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2468 return AFPERR_NOITEM;
2469 *rbuflen = UUID_BINSIZE;
2471 case 6 : /* groupname -> UUID */
2472 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2473 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2474 return AFPERR_NOITEM;
2475 *rbuflen = UUID_BINSIZE;
2483 /* ------------------------------------
2484 variable DID support
2486 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2497 /* do nothing as dids are static for the life of the process. */
2501 memcpy(&vid, ibuf, sizeof( vid ));
2502 ibuf += sizeof( vid );
2503 if (( vol = getvolbyvid( vid )) == NULL ) {
2504 return( AFPERR_PARAM );
2507 memcpy( &did, ibuf, sizeof( did ));
2508 ibuf += sizeof( did );
2509 if (( dir = dirlookup( vol, did )) == NULL ) {
2510 return( AFPERR_PARAM );
2513 /* dir_remove -- deletedid */
2519 /* did creation gets done automatically
2520 * there's a pb again with case but move it to cname
2522 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2525 struct dir *parentdir;
2533 memcpy(&vid, ibuf, sizeof(vid));
2534 ibuf += sizeof( vid );
2536 if (NULL == ( vol = getvolbyvid( vid )) ) {
2537 return( AFPERR_PARAM );
2540 memcpy(&did, ibuf, sizeof(did));
2541 ibuf += sizeof(did);
2543 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2547 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2548 return get_afp_errno(AFPERR_PARAM);
2551 if ( *path->m_name != '\0' ) {
2552 return path_error(path, AFPERR_NOOBJ);
2555 if ( !path->st_valid && of_stat(path ) < 0 ) {
2556 return( AFPERR_NOOBJ );
2558 if ( path->st_errno ) {
2559 return( AFPERR_NOOBJ );
2562 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2563 *rbuflen = sizeof(curdir->d_did);