2 * $Id: directory.c,v 1.131.2.12 2010-02-09 14:56:30 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 = {
74 0, /* stat is not set */
78 /*******************************************************************************************
80 ******************************************************************************************/
83 /* -------------------------
84 appledouble mkdir afp error code.
86 static int netatalk_mkdir(const char *name)
88 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
91 return( AFPERR_NOOBJ );
93 return( AFPERR_VLOCK );
96 return( AFPERR_ACCESS );
98 return( AFPERR_EXIST );
101 return( AFPERR_DFULL );
103 return( AFPERR_PARAM );
109 /* ------------------- */
110 static int deletedir(char *dir)
112 char path[MAXPATHLEN + 1];
120 if ((len = strlen(dir)) +2 > sizeof(path))
124 if ((dp = opendir(dir)) == NULL)
130 remain = sizeof(path) -len -1;
131 while ((de = readdir(dp)) && err == AFP_OK) {
132 /* skip this and previous directory */
133 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
136 if (strlen(de->d_name) > remain) {
140 strcpy(path + len, de->d_name);
141 if (stat(path, &st)) {
144 if (S_ISDIR(st.st_mode)) {
145 err = deletedir(path);
147 err = netatalk_unlink(path);
152 /* okay. the directory is empty. delete it. note: we already got rid
155 err = netatalk_rmdir(dir);
160 /* do a recursive copy. */
161 static int copydir(const struct vol *vol, char *src, char *dst)
163 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
172 /* doesn't exist or the path is too long. */
173 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
174 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
175 ((dp = opendir(src)) == NULL))
178 /* try to create the destination directory */
179 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
184 /* set things up to copy */
188 srem = sizeof(spath) - slen -1;
193 drem = sizeof(dpath) - dlen -1;
196 while ((de = readdir(dp))) {
197 /* skip this and previous directory */
198 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
201 if (strlen(de->d_name) > srem) {
205 strcpy(spath + slen, de->d_name);
207 if (stat(spath, &st) == 0) {
208 if (strlen(de->d_name) > drem) {
212 strcpy(dpath + dlen, de->d_name);
214 if (S_ISDIR(st.st_mode)) {
215 if (AFP_OK != (err = copydir(vol, spath, dpath)))
217 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
221 /* keep the same time stamp. */
222 ut.actime = ut.modtime = st.st_mtime;
228 /* keep the same time stamp. */
229 if (stat(src, &st) == 0) {
230 ut.actime = ut.modtime = st.st_mtime;
239 /* ---------------------
240 * is our cached offspring count valid?
242 static int diroffcnt(struct dir *dir, struct stat *st)
244 return st->st_ctime == dir->ctime;
247 /* --------------------- */
248 static int invisible_dots(const struct vol *vol, const char *name)
250 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
253 /* ------------------ */
254 static int set_dir_errors(struct path *path, const char *where, int err)
259 return AFPERR_ACCESS;
263 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
268 * @brief Convert name in client encoding to server encoding
270 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
271 * This only gets called from cname().
273 * @returns 0 on success, -1 on error
275 * @note If the passed ret->m_name is mangled, we'll demangle it
277 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
279 static char temp[ MAXPATHLEN + 1];
283 if (afp_version >= 30) {
285 if (dir->d_did == DIRDID_ROOT_PARENT) {
287 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
288 * So we compare it with the longname from the current volume and if they match
289 * we overwrite the requested path with the utf8 volume name so that the following
292 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
293 if (strcasecmp(ret->m_name, temp) == 0)
294 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
297 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
298 afp_errno = AFPERR_PARAM;
301 strcpy(ret->m_name, temp);
305 /* check for OS X mangled filename :( */
306 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
307 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
308 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
310 if (t != ret->m_name) {
312 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
313 * flags weren't the same
315 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
316 /* at last got our view of mac name */
317 strcpy(ret->m_name, t);
320 } /* afp_version >= 30 */
322 /* If we haven't got it by now, get it */
323 if (ret->u_name == NULL) {
324 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
325 afp_errno = AFPERR_PARAM;
334 * @brief Build struct path from struct dir
336 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
337 * 1. move cwd into parent dir (we're often already there, but not always)
338 * 2. set struct path to the dirname
340 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
341 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
342 * 4. indicate there's no dir for this path
345 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
347 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
353 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
356 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
357 if (dir->d_m_name == dir->d_u_name) {
358 ret->u_name = ret->m_name;
360 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
361 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
366 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
367 cfrombstring(dir->d_fullpath),
368 cfrombstring(curdir->d_fullpath),
374 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
377 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
378 if (dir->d_m_name == dir->d_u_name) {
379 ret->u_name = ret->m_name;
381 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
382 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
385 ret->d_dir = NULL; /* 4 */
386 dir_remove(vol, dir); /* 5 */
393 /* DEADC0DE: never get here */
398 /*********************************************************************************************
400 ********************************************************************************************/
402 int get_afp_errno(const int param)
404 if (afp_errno != AFPERR_DID1)
410 * @brief Resolve a DID
412 * Resolve a DID, allocate a struct dir for it
413 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
414 * 2. Check if the DID is in the cache.
415 * 3. If it's not in the cache resolve it via the database.
416 * 4. Build complete server-side path to the dir.
417 * 5. Check if it exists and is a directory.
418 * 6. Create the struct dir and populate it.
419 * 7. Add it to the cache.
421 * @param vol (r) pointer to struct vol
422 * @param did (r) DID to resolve
424 * @returns pointer to struct dir
426 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
427 * and we are really bad in this case.
429 struct dir *dirlookup(const struct vol *vol, cnid_t did)
431 static char buffer[12 + MAXPATHLEN + 1];
432 struct bstrList *pathlist = NULL;
433 bstring fullpath = NULL;
435 struct dir *ret = NULL;
439 int buflen = 12 + MAXPATHLEN + 1;
443 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
445 /* check for did 0, 1 and 2 */
446 if (did == 0 || vol == NULL) { /* 1 */
447 afp_errno = AFPERR_PARAM;
449 } else if (did == DIRDID_ROOT_PARENT) {
450 rootParent.d_vid = vol->v_vid;
451 return (&rootParent);
452 } else if (did == DIRDID_ROOT) {
456 /* Search the cache */
457 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
458 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
459 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
463 /* It's not there anymore, so remove it */
464 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
465 dir_remove(vol, ret);
466 afp_errno = AFPERR_NOOBJ;
477 utf8 = utf8_encoding();
478 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
480 /* Create list for path elements, request 16 list elements for now*/
481 if ((pathlist = bstListCreateMin(16)) == NULL) { /* 4 */
482 LOG(log_error, logtype_afpd, "dirlookup(did: %u): OOM: %s", ntohl(did), strerror(errno));
486 /* Get it from the database */
488 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
489 afp_errno = AFPERR_NOOBJ;
495 /* construct path, copy already found uname to path element list*/
496 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
497 afp_errno = AFPERR_MISC;
502 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {%u, %s}", ntohl(did), ntohl(pdid), upath);
504 /* The stuff that follows is for building the full path to the directory */
506 /* work upwards until we reach volume root */
507 while (cnid != DIRDID_ROOT) {
508 /* construct path, copy already found uname to path element list*/
509 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
510 afp_errno = AFPERR_MISC;
516 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
517 afp_errno = AFPERR_NOOBJ;
523 if ((bstrListPush(pathlist, bfromcstr(vol->v_path))) != BSTR_OK) { /* 4 */
524 afp_errno = AFPERR_MISC;
529 if ((fullpath = bjoinInv(pathlist, bfromcstr("/"))) == NULL) { /* 4 */
530 afp_errno = AFPERR_MISC;
534 /* Finished building the fullpath */
536 /* stat it and check if it's a dir */
537 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
539 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
542 afp_errno = AFPERR_NOOBJ;
546 afp_errno = AFPERR_ACCESS;
550 afp_errno = AFPERR_MISC;
555 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
556 afp_errno = AFPERR_BADTYPE;
562 /* Get macname from unix name */
563 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
564 afp_errno = AFPERR_NOOBJ;
569 /* Create struct dir */
570 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
571 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
576 /* Add it to the cache only if it's a dir */
577 if (dircache_add(ret) != 0) { /* 7 */
582 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
583 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
587 bstrListDestroy(pathlist);
590 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
591 ntohl(did), AfpErr2name(afp_errno));
602 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
604 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
609 static u_int32_t did = 0;
610 static char cname[MAXPATHLEN];
611 static char lname[MAXPATHLEN];
612 ucs2_t u2_path[MAXPATHLEN];
613 ucs2_t u2_dename[MAXPATHLEN];
614 char *tmp, *savepath;
616 if (!(vol->v_flags & AFPVOL_CASEINSEN))
619 if (veto_file(ENUMVETO, path->u_name))
622 savepath = path->u_name;
624 /* very simple cache */
625 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
626 path->u_name = cname;
628 if (of_stat( path ) == 0 ) {
631 /* something changed, we cannot stat ... */
635 if (NULL == ( dp = opendir( "." )) ) {
636 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
641 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
642 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
643 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
645 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
647 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
648 if (NULL == check_dirent(vol, de->d_name))
651 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
654 if (strcasecmp_w( u2_path, u2_dename) == 0) {
656 strlcpy(cname, de->d_name, sizeof(cname));
657 path->u_name = cname;
659 if (of_stat( path ) == 0 ) {
660 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
661 strlcpy(lname, tmp, sizeof(lname));
674 /* invalidate cache */
677 path->u_name = savepath;
679 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
685 * @brief Construct struct dir
687 * Construct struct dir from parameters.
689 * @param m_name (r) directory name in UTF8-dec
690 * @param u_name (r) directory name in server side encoding
691 * @param vol (r) pointer to struct vol
692 * @param pdid (r) Parent CNID
693 * @param did (r) CNID
694 * @param fullpath (r) Full unix path to dir
696 * @returns pointer to new struct dir or NULL on error
698 * @note Most of the time mac name and unix name are the same.
700 struct dir *dir_new(const char *m_name,
702 const struct vol *vol,
709 dir = (struct dir *) calloc(1, sizeof( struct dir ));
713 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
718 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
721 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
722 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
723 dir->d_m_name_ucs2 = NULL;
726 if (m_name == u_name || !strcmp(m_name, u_name)) {
727 dir->d_u_name = dir->d_m_name;
729 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
730 bdestroy(dir->d_m_name);
737 dir->d_vid = vol->v_vid;
738 dir->d_fullpath = path;
743 * @brief Free a struct dir and all its members
745 * @param (rw) pointer to struct dir
747 void dir_free(struct dir *dir)
749 if (dir->d_u_name != dir->d_m_name) {
750 bdestroy(dir->d_u_name);
752 if (dir->d_m_name_ucs2)
753 free(dir->d_m_name_ucs2);
754 bdestroy(dir->d_m_name);
755 bdestroy(dir->d_fullpath);
760 * @brief Create struct dir from struct path
762 * Create a new struct dir from struct path. Then add it to the cache.
763 * The caller must have assured that the dir is not already in the cache,
765 * 1. Open adouble file, get CNID from it.
766 * 2. Search the database, hinting with the CNID from (1).
767 * 3. Build fullpath and create struct dir.
768 * 4. Add it to the cache.
770 * @param vol (r) pointer to struct vol
771 * @param dir (r) pointer to parrent directory
772 * @param path (rw) pointer to struct path with valid path->u_name
773 * @param len (r) strlen of path->u_name
775 * @returns Pointer to new struct dir or NULL on error.
777 * @note Function also assigns path->m_name from path->u_name.
779 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
782 struct dir *cdir = NULL;
785 struct adouble *adp = NULL;
793 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
794 /* there's a stray entry in the dircache */
795 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
796 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
797 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
798 if (dir_remove(vol, cdir) != 0) {
804 /* get_id needs adp for reading CNID from adouble file */
805 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
806 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
810 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
816 ad_close_metadata(adp);
818 /* Get macname from unixname */
819 if (path->m_name == NULL) {
820 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
827 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
828 || (bconchar(fullpath, '/') != BSTR_OK)
829 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
830 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
835 /* Allocate and initialize struct dir */
836 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
841 if ((dircache_add(cdir)) != 0) { /* 4 */
842 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
848 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
849 cfrombstring(dir->d_u_name), path->u_name, err);
852 ad_close_metadata(adp);
853 if (!cdir && fullpath)
860 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
861 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
862 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
869 * @brief Remove a dir from a cache and free it and any ressources with it
871 * 1. Check if the dir is locked or has opened forks
872 * 2. If it's a request to remove curdir, just chdir to volume root
873 * 3. Remove it from the cache
874 * 4. Remove the dir plus any allocated resources it references
876 * @param (r) pointer to struct vol
877 * @param (rw) pointer to struct dir
879 int dir_remove(const struct vol *vol, struct dir *dir)
884 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
887 if (dir->d_flags & DIRF_CACHELOCK || dir->d_ofork) { /* 1 */
888 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): dir is locked or has opened forks",
889 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
893 if (curdir == dir) { /* 2 */
894 if (movecwd(vol, vol->v_root) < 0) {
895 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
899 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
900 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
902 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 3 */
903 dir_free(dir); /* 4 */
909 * @brief Modify a struct dir, adjust cache
911 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
912 * If given new_uname == new_mname, new_uname will point to new_mname.
914 * @param vol (r) pointer to struct vol
915 * @param dir (rw) pointer to struct dir
916 * @param pdid (r) new parent DID
917 * @param did (r) new DID
918 * @param new_mname (r) new mac-name
919 * @param new_uname (r) new unix-name
921 int dir_modify(const struct vol *vol,
925 const char *new_mname,
926 const char *new_uname,
927 bstring pdir_fullpath)
931 /* Remove it from the cache */
932 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
940 /* free uname if it's not the same as mname */
941 if (dir->d_m_name != dir->d_u_name)
942 bdestroy(dir->d_u_name);
944 if (new_uname == NULL)
945 new_uname = new_mname;
947 /* assign new name */
948 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
949 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
953 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
954 dir->d_u_name = dir->d_m_name;
956 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
957 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
964 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
966 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
968 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
972 if (dir->d_m_name_ucs2)
973 free(dir->d_m_name_ucs2);
974 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))
975 dir->d_m_name_ucs2 = NULL;
977 /* Re-add it to the cache */
978 if ((dircache_add(dir)) != 0) {
987 * @brief Resolve a catalog node name path
989 * 1. Evaluate path type
990 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
991 * path from dirname, so eg getdirparams has sth it can chew on. curdir
992 * is dir parent then. All this is done in path_from_dir().
993 * 3. Parse next cnode name in path, cases:
994 * 4. single "\0" -> do nothing
995 * 5. two or more consecutive "\0" -> chdir("..") one or more times
996 * 6. cnode name -> copy it to path.m_name
997 * 7. Get unix name from mac name
998 * 8. Special handling of request with did 1
999 * 9. stat the cnode name
1000 * 10. If it's not there, it's probably an afp_createfile|dir,
1001 * return with curdir = dir parent, struct path = dirname
1002 * 11. If it's there and it's a file, it must should be the last element of the requested
1003 * path. Return with curdir = cnode name parent dir, struct path = filename
1004 * 12. Treat symlinks like files, dont follow them
1005 * 13. If it's a dir:
1006 * 14. Search the dircache for it
1007 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
1008 * 16. chdir into the dir and
1009 * 17. set m_name to the mac equivalent of "."
1012 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1014 static char path[ MAXPATHLEN + 1];
1015 static struct path ret;
1025 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1028 afp_errno = AFPERR_NOOBJ;
1029 memset(&ret, 0, sizeof(ret));
1031 switch (ret.m_type = *data) { /* 1 */
1034 len = (unsigned char) *data++;
1036 if (afp_version >= 30) {
1042 if (afp_version >= 30) {
1044 memcpy(&hint, data, sizeof(hint));
1046 data += sizeof(hint);
1048 memcpy(&len16, data, sizeof(len16));
1054 /* else it's an error */
1056 afp_errno = AFPERR_PARAM;
1059 *cpath += len + size;
1064 if (movecwd(vol, dir) < 0 ) {
1065 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1066 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1068 return path_from_dir(vol, dir, &ret);
1073 while (len) { /* 3 */
1074 if (*data == 0) { /* 4 or 5 */
1077 while (len > 0 && *data == 0) { /* 5 */
1078 /* chdir to parrent dir */
1079 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1081 if (movecwd( vol, dir ) < 0 ) {
1082 dir_remove(vol, dir);
1092 for ( p = path; *data != 0 && len > 0; len-- ) {
1094 if (p > &path[ MAXPATHLEN]) {
1095 afp_errno = AFPERR_PARAM;
1099 *p = 0; /* Terminate string */
1102 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1103 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1107 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1109 /* Prevent access to our special folders like .AppleDouble */
1110 if (check_name(vol, ret.u_name)) {
1111 /* the name is illegal */
1112 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1113 afp_errno = AFPERR_PARAM;
1117 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1119 * Special case: CNID 1
1120 * root parent (did 1) has one child: the volume. Requests for did=1 with
1121 * some <name> must check against the volume name.
1123 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1129 * CNID != 1, eg. most of the times we take this way.
1130 * Now check if current path-part is a file or dir:
1131 * o if it's dir we have to step into it
1132 * o if it's a file we expect it to be the last part of the requested path
1133 * and thus call continue which should terminate the while loop because
1136 if (of_stat(&ret) != 0) { /* 9 */
1138 * ret.u_name doesn't exist, might be afp_createfile|dir
1139 * that means it should have been the last part
1142 /* it wasn't the last part, so we have a bogus path request */
1143 afp_errno = AFPERR_NOOBJ;
1147 * this will terminate clean in while (1) because len == 0,
1148 * probably afp_createfile|dir
1150 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1154 switch (ret.st.st_mode & S_IFMT) {
1155 case S_IFREG: /* 11 */
1156 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1158 /* it wasn't the last part, so we have a bogus path request */
1159 afp_errno = AFPERR_PARAM;
1162 continue; /* continues while loop */
1163 case S_IFLNK: /* 12 */
1164 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1166 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1167 afp_errno = AFPERR_PARAM;
1170 continue; /* continues while loop */
1171 case S_IFDIR: /* 13 */
1174 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1175 afp_errno = AFPERR_NODIR;
1179 /* Search the cache */
1180 int unamelen = strlen(ret.u_name);
1181 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1183 /* Not in cache, create one */
1184 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1185 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1186 ntohl(dir->d_did), ret.u_name, getcwdpath());
1190 } /* if/else cnid==1 */
1192 /* Now chdir to the evaluated dir */
1193 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1194 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1195 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1197 return path_from_dir(vol, cdir, &ret);
1202 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1205 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1206 afp_errno = AFPERR_DID1;
1210 if (ret.m_name[0] == 0) {
1211 /* Last part was a dir */
1212 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1216 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1217 cfrombstring(dir->d_fullpath),
1218 cfrombstring(curdir->d_fullpath),
1225 * @brief chdir() to dir
1227 * @param vol (r) pointer to struct vol
1228 * @param dir (r) pointer to struct dir
1230 * @returns 0 on success, -1 on error with afp_errno set appropiately
1232 int movecwd(const struct vol *vol, struct dir *dir)
1239 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1240 cfrombstring(curdir->d_fullpath), getcwdpath());
1244 if (dir->d_did == DIRDID_ROOT_PARENT) {
1245 curdir = &rootParent;
1249 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1251 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1252 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1256 afp_errno = AFPERR_ACCESS;
1259 afp_errno = AFPERR_NOOBJ;
1269 * We can't use unix file's perm to support Apple's inherited protection modes.
1270 * If we aren't the file's owner we can't change its perms when moving it and smb
1271 * nfs,... don't even try.
1273 #define AFP_CHECK_ACCESS
1275 int check_access(char *path, int mode)
1277 #ifdef AFP_CHECK_ACCESS
1285 accessmode(p, &ma, curdir, NULL);
1286 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1288 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1294 /* --------------------- */
1295 int file_access(struct path *path, int mode)
1299 accessmode(path->u_name, &ma, curdir, &path->st);
1300 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1302 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1308 /* --------------------- */
1309 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1311 dir->offcnt = count;
1312 dir->ctime = st->st_ctime;
1313 dir->d_flags &= ~DIRF_CNID;
1317 /* ---------------------
1318 * is our cached also for reenumerate id?
1320 int dirreenumerate(struct dir *dir, struct stat *st)
1322 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1325 /* ------------------------------
1327 (name, dir) with curdir:name == dir, from afp_enumerate
1330 int getdirparams(const struct vol *vol,
1331 u_int16_t bitmap, struct path *s_path,
1333 char *buf, size_t *buflen )
1337 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1338 int bit = 0, isad = 0;
1344 struct stat *st = &s_path->st;
1345 char *upath = s_path->u_name;
1347 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1348 (1 << DIRPBIT_CDATE) |
1349 (1 << DIRPBIT_MDATE) |
1350 (1 << DIRPBIT_BDATE) |
1351 (1 << DIRPBIT_FINFO)))) {
1353 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1354 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1356 if (ad.ad_md->adf_flags & O_CREAT) {
1357 /* We just created it */
1358 ad_setname(&ad, s_path->m_name);
1373 while ( bitmap != 0 ) {
1374 while (( bitmap & 1 ) == 0 ) {
1382 ad_getattr(&ad, &ashort);
1383 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1384 ashort = htons(ATTRBIT_INVISIBLE);
1387 ashort |= htons(ATTRBIT_SHARED);
1388 memcpy( data, &ashort, sizeof( ashort ));
1389 data += sizeof( ashort );
1393 memcpy( data, &pdid, sizeof( pdid ));
1394 data += sizeof( pdid );
1395 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1396 s_path->u_name, ntohl(pdid));
1399 case DIRPBIT_CDATE :
1400 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1401 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1402 memcpy( data, &aint, sizeof( aint ));
1403 data += sizeof( aint );
1406 case DIRPBIT_MDATE :
1407 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1408 memcpy( data, &aint, sizeof( aint ));
1409 data += sizeof( aint );
1412 case DIRPBIT_BDATE :
1413 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1414 aint = AD_DATE_START;
1415 memcpy( data, &aint, sizeof( aint ));
1416 data += sizeof( aint );
1419 case DIRPBIT_FINFO :
1421 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1422 } else { /* no appledouble */
1423 memset( data, 0, 32 );
1424 /* set default view -- this also gets done in ad_open() */
1425 ashort = htons(FINDERINFO_CLOSEDVIEW);
1426 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1428 /* dot files are by default visible */
1429 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1430 ashort = htons(FINDERINFO_INVISIBLE);
1431 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1437 case DIRPBIT_LNAME :
1438 if (dir->d_m_name) /* root of parent can have a null name */
1441 memset(data, 0, sizeof(u_int16_t));
1442 data += sizeof( u_int16_t );
1445 case DIRPBIT_SNAME :
1446 memset(data, 0, sizeof(u_int16_t));
1447 data += sizeof( u_int16_t );
1451 memcpy( data, &dir->d_did, sizeof( aint ));
1452 data += sizeof( aint );
1453 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1454 s_path->u_name, ntohl(dir->d_did));
1457 case DIRPBIT_OFFCNT :
1459 /* this needs to handle current directory access rights */
1460 if (diroffcnt(dir, st)) {
1461 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1463 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1464 setdiroffcnt(dir, st, ret);
1465 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1467 ashort = htons( ashort );
1468 memcpy( data, &ashort, sizeof( ashort ));
1469 data += sizeof( ashort );
1473 aint = htonl(st->st_uid);
1474 memcpy( data, &aint, sizeof( aint ));
1475 data += sizeof( aint );
1479 aint = htonl(st->st_gid);
1480 memcpy( data, &aint, sizeof( aint ));
1481 data += sizeof( aint );
1484 case DIRPBIT_ACCESS :
1485 accessmode( upath, &ma, dir , st);
1487 *data++ = ma.ma_user;
1488 *data++ = ma.ma_world;
1489 *data++ = ma.ma_group;
1490 *data++ = ma.ma_owner;
1493 /* Client has requested the ProDOS information block.
1494 Just pass back the same basic block for all
1495 directories. <shirsch@ibm.net> */
1496 case DIRPBIT_PDINFO :
1497 if (afp_version >= 30) { /* UTF8 name */
1498 utf8 = kTextEncodingUTF8;
1499 if (dir->d_m_name) /* root of parent can have a null name */
1502 memset(data, 0, sizeof(u_int16_t));
1503 data += sizeof( u_int16_t );
1505 memcpy(data, &aint, sizeof( aint ));
1506 data += sizeof( aint );
1508 else { /* ProDOS Info Block */
1511 ashort = htons( 0x0200 );
1512 memcpy( data, &ashort, sizeof( ashort ));
1513 data += sizeof( ashort );
1514 memset( data, 0, sizeof( ashort ));
1515 data += sizeof( ashort );
1519 case DIRPBIT_UNIXPR :
1520 aint = htonl(st->st_uid);
1521 memcpy( data, &aint, sizeof( aint ));
1522 data += sizeof( aint );
1523 aint = htonl(st->st_gid);
1524 memcpy( data, &aint, sizeof( aint ));
1525 data += sizeof( aint );
1528 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1529 memcpy( data, &aint, sizeof( aint ));
1530 data += sizeof( aint );
1532 accessmode( upath, &ma, dir , st);
1534 *data++ = ma.ma_user;
1535 *data++ = ma.ma_world;
1536 *data++ = ma.ma_group;
1537 *data++ = ma.ma_owner;
1542 ad_close_metadata( &ad );
1544 return( AFPERR_BITMAP );
1550 ashort = htons( data - buf );
1551 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1552 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1554 if ( utf_nameoff ) {
1555 ashort = htons( data - buf );
1556 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1557 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1560 ad_close_metadata( &ad );
1562 *buflen = data - buf;
1566 /* ----------------------------- */
1567 int path_error(struct path *path, int error)
1569 /* - a dir with access error
1570 * - no error it's a file
1573 if (path_isadir(path))
1575 if (path->st_valid && path->st_errno)
1577 return AFPERR_BADTYPE ;
1580 /* ----------------------------- */
1581 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1586 u_int16_t vid, bitmap;
1592 memcpy( &vid, ibuf, sizeof( vid ));
1593 ibuf += sizeof( vid );
1595 if (NULL == ( vol = getvolbyvid( vid )) ) {
1596 return( AFPERR_PARAM );
1599 if (vol->v_flags & AFPVOL_RO)
1600 return AFPERR_VLOCK;
1602 memcpy( &did, ibuf, sizeof( did ));
1603 ibuf += sizeof( int );
1605 if (NULL == ( dir = dirlookup( vol, did )) ) {
1609 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1610 bitmap = ntohs( bitmap );
1611 ibuf += sizeof( bitmap );
1613 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1614 return get_afp_errno(AFPERR_NOOBJ);
1617 if ( *path->m_name != '\0' ) {
1618 rc = path_error(path, AFPERR_NOOBJ);
1619 /* maybe we are trying to set perms back */
1620 if (rc != AFPERR_ACCESS)
1625 * If ibuf is odd, make it even.
1627 if ((u_long)ibuf & 1 ) {
1631 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1632 setvoltime(obj, vol );
1638 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1640 * assume path == '\0' eg. it's a directory in canonical form
1642 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1654 u_int16_t ashort, bshort, oshort;
1656 int change_mdate = 0;
1657 int change_parent_mdate = 0;
1659 u_int16_t bitmap = d_bitmap;
1660 u_char finder_buf[32];
1663 u_int16_t upriv_bit = 0;
1666 upath = path->u_name;
1668 while ( bitmap != 0 ) {
1669 while (( bitmap & 1 ) == 0 ) {
1677 memcpy( &ashort, buf, sizeof( ashort ));
1678 buf += sizeof( ashort );
1680 case DIRPBIT_CDATE :
1682 memcpy(&cdate, buf, sizeof(cdate));
1683 buf += sizeof( cdate );
1685 case DIRPBIT_MDATE :
1686 memcpy(&newdate, buf, sizeof(newdate));
1687 buf += sizeof( newdate );
1689 case DIRPBIT_BDATE :
1691 memcpy(&bdate, buf, sizeof(bdate));
1692 buf += sizeof( bdate );
1694 case DIRPBIT_FINFO :
1696 memcpy( finder_buf, buf, 32 );
1699 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1700 change_parent_mdate = 1;
1701 memcpy( &owner, buf, sizeof(owner));
1702 buf += sizeof( owner );
1705 change_parent_mdate = 1;
1706 memcpy( &group, buf, sizeof( group ));
1707 buf += sizeof( group );
1709 case DIRPBIT_ACCESS :
1711 change_parent_mdate = 1;
1712 ma.ma_user = *buf++;
1713 ma.ma_world = *buf++;
1714 ma.ma_group = *buf++;
1715 ma.ma_owner = *buf++;
1716 mpriv = mtoumode( &ma ) | vol->v_dperm;
1717 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1718 err = set_dir_errors(path, "setdirmode", errno);
1722 /* Ignore what the client thinks we should do to the
1723 ProDOS information block. Skip over the data and
1724 report nothing amiss. <shirsch@ibm.net> */
1725 case DIRPBIT_PDINFO :
1726 if (afp_version < 30) {
1730 err = AFPERR_BITMAP;
1734 case DIRPBIT_UNIXPR :
1735 if (vol_unix_priv(vol)) {
1736 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1737 buf += sizeof( owner );
1738 memcpy( &group, buf, sizeof( group ));
1739 buf += sizeof( group );
1742 change_parent_mdate = 1;
1743 memcpy( &upriv, buf, sizeof( upriv ));
1744 buf += sizeof( upriv );
1745 upriv = ntohl (upriv) | vol->v_dperm;
1746 if (dir_rx_set(upriv)) {
1747 /* maybe we are trying to set perms back */
1748 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1750 err = set_dir_errors(path, "setdirunixmode", errno);
1761 err = AFPERR_BITMAP;
1769 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1771 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1773 * Check to see what we're trying to set. If it's anything
1774 * but ACCESS, UID, or GID, give an error. If it's any of those
1775 * three, we don't need the ad to be open, so just continue.
1777 * note: we also don't need to worry about mdate. also, be quiet
1778 * if we're using the noadouble option.
1780 if (!vol_noadouble(vol) && (d_bitmap &
1781 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1782 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1783 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1784 return AFPERR_ACCESS;
1790 * Check to see if a create was necessary. If it was, we'll want
1791 * to set our name, etc.
1793 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1794 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1800 while ( bitmap != 0 ) {
1801 while (( bitmap & 1 ) == 0 ) {
1809 ad_getattr(&ad, &bshort);
1811 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1812 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1816 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1817 change_parent_mdate = 1;
1818 ad_setattr(&ad, bshort);
1821 case DIRPBIT_CDATE :
1823 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1826 case DIRPBIT_MDATE :
1828 case DIRPBIT_BDATE :
1830 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1833 case DIRPBIT_FINFO :
1835 /* Fixes #2802236 */
1836 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1837 *fflags &= htons(~FINDERINFO_ISHARED);
1839 if ( dir->d_did == DIRDID_ROOT ) {
1841 * Alright, we admit it, this is *really* sick!
1842 * The 4 bytes that we don't copy, when we're dealing
1843 * with the root of a volume, are the directory's
1844 * location information. This eliminates that annoying
1845 * behavior one sees when mounting above another mount
1848 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1849 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1851 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1855 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1856 if ( (dir->d_did == DIRDID_ROOT) &&
1857 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1858 err = set_dir_errors(path, "setdeskowner", errno);
1859 if (isad && err == AFPERR_PARAM) {
1860 err = AFP_OK; /* ???*/
1863 goto setdirparam_done;
1866 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1867 err = set_dir_errors(path, "setdirowner", errno);
1868 goto setdirparam_done;
1872 if (dir->d_did == DIRDID_ROOT)
1873 setdeskowner( -1, ntohl(group) );
1874 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1875 err = set_dir_errors(path, "setdirowner", errno);
1876 goto setdirparam_done;
1879 case DIRPBIT_ACCESS :
1880 if (dir->d_did == DIRDID_ROOT) {
1882 if (!dir_rx_set(mpriv)) {
1883 /* we can't remove read and search for owner on volume root */
1884 err = AFPERR_ACCESS;
1885 goto setdirparam_done;
1889 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1890 err = set_dir_errors(path, "setdirmode", errno);
1891 goto setdirparam_done;
1894 case DIRPBIT_PDINFO :
1895 if (afp_version >= 30) {
1896 err = AFPERR_BITMAP;
1897 goto setdirparam_done;
1900 case DIRPBIT_UNIXPR :
1901 if (vol_unix_priv(vol)) {
1902 if (dir->d_did == DIRDID_ROOT) {
1903 if (!dir_rx_set(upriv)) {
1904 /* we can't remove read and search for owner on volume root */
1905 err = AFPERR_ACCESS;
1906 goto setdirparam_done;
1908 setdeskowner( -1, ntohl(group) );
1909 setdeskmode( upriv );
1911 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1912 err = set_dir_errors(path, "setdirowner", errno);
1913 goto setdirparam_done;
1916 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1917 err = set_dir_errors(path, "setdirunixmode", errno);
1918 goto setdirparam_done;
1922 err = AFPERR_BITMAP;
1923 goto setdirparam_done;
1927 err = AFPERR_BITMAP;
1928 goto setdirparam_done;
1937 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1938 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1942 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1943 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1948 if (path->st_valid && !path->st_errno) {
1949 struct stat *st = &path->st;
1951 if (dir && dir->d_pdid) {
1952 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1956 ad_close_metadata( &ad);
1959 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1960 && gettimeofday(&tv, NULL) == 0) {
1961 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1962 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1963 /* be careful with bitmap because now dir is null */
1964 bitmap = 1<<DIRPBIT_MDATE;
1965 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1966 /* should we reset curdir ?*/
1973 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1987 memcpy( &vid, ibuf, sizeof( vid ));
1988 ibuf += sizeof( vid );
1989 if (NULL == (vol = getvolbyvid( vid )) ) {
1990 return( AFPERR_PARAM );
1993 memcpy( &did, ibuf, sizeof( did ));
1994 ibuf += sizeof( did );
1998 * if it's CNID 2 our only choice to meet the specs is call sync.
1999 * For any other CNID just sync that dir. To my knowledge the
2000 * intended use of FPSyncDir is to sync the volume so all we're
2001 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2004 if ( ntohl(did) == 2 ) {
2007 if (NULL == ( dir = dirlookup( vol, did )) ) {
2008 return afp_errno; /* was AFPERR_NOOBJ */
2011 if (movecwd( vol, dir ) < 0 )
2012 return ( AFPERR_NOOBJ );
2015 * Assuming only OSens that have dirfd also may require fsyncing directories
2016 * in order to flush metadata e.g. Linux.
2020 if (NULL == ( dp = opendir( "." )) ) {
2023 return( AFPERR_NOOBJ );
2025 return( AFPERR_ACCESS );
2027 return( AFPERR_PARAM );
2031 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2034 if ( fsync ( dfd ) < 0 )
2035 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2036 dir->d_u_name, strerror(errno) );
2037 closedir(dp); /* closes dfd too */
2040 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2043 return( AFPERR_NOOBJ );
2045 return( AFPERR_ACCESS );
2047 return( AFPERR_PARAM );
2051 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2052 vol->ad_path(".", ADFLAGS_DIR) );
2054 if ( fsync(dfd) < 0 )
2055 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2056 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2063 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2069 struct path *s_path;
2077 memcpy( &vid, ibuf, sizeof( vid ));
2078 ibuf += sizeof( vid );
2079 if (NULL == ( vol = getvolbyvid( vid )) ) {
2080 return( AFPERR_PARAM );
2083 if (vol->v_flags & AFPVOL_RO)
2084 return AFPERR_VLOCK;
2086 memcpy( &did, ibuf, sizeof( did ));
2087 ibuf += sizeof( did );
2088 if (NULL == ( dir = dirlookup( vol, did )) ) {
2089 return afp_errno; /* was AFPERR_NOOBJ */
2091 /* for concurrent access we need to be sure we are not in the
2092 * folder we want to create...
2096 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2097 return get_afp_errno(AFPERR_PARAM);
2099 /* cname was able to move curdir to it! */
2100 if (*s_path->m_name == '\0')
2101 return AFPERR_EXIST;
2103 upath = s_path->u_name;
2105 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2109 if (of_stat(s_path) < 0) {
2115 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2119 if ( movecwd( vol, dir ) < 0 ) {
2120 return( AFPERR_PARAM );
2123 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2124 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2125 if (vol_noadouble(vol))
2126 goto createdir_done;
2127 return( AFPERR_ACCESS );
2129 ad_setname(&ad, s_path->m_name);
2130 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2133 ad_close_metadata( &ad);
2136 #ifdef HAVE_NFSv4_ACLS
2137 /* FIXME: are we really inside the created dir? */
2138 addir_inherit_acl(vol);
2141 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2142 *rbuflen = sizeof( u_int32_t );
2143 setvoltime(obj, vol );
2148 * dst new unix filename (not a pathname)
2149 * newname new mac name
2152 int renamedir(const struct vol *vol, char *src, char *dst,
2154 struct dir *newparent,
2160 /* existence check moved to afp_moveandrename */
2161 if ( unix_rename( src, dst ) < 0 ) {
2164 return( AFPERR_NOOBJ );
2166 return( AFPERR_ACCESS );
2168 return AFPERR_VLOCK;
2170 /* tried to move directory into a subdirectory of itself */
2171 return AFPERR_CANTMOVE;
2173 /* this needs to copy and delete. bleah. that means we have
2174 * to deal with entire directory hierarchies. */
2175 if ((err = copydir(vol, src, dst)) < 0) {
2179 if ((err = deletedir(src)) < 0)
2183 return( AFPERR_PARAM );
2187 vol->vfs->vfs_renamedir(vol, src, dst);
2189 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2191 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2192 ad_setname(&ad, newname);
2194 ad_close_metadata( &ad);
2197 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2198 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2205 /* delete an empty directory */
2206 int deletecurdir(struct vol *vol)
2216 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2217 return( AFPERR_ACCESS );
2222 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2223 /* we never want to create a resource fork here, we are going to delete it */
2224 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2226 ad_getattr(&ad, &ashort);
2227 ad_close( &ad, ADFLAGS_HF );
2228 if ((ashort & htons(ATTRBIT_NODELETE))) {
2229 return AFPERR_OLOCK;
2232 err = vol->vfs->vfs_deletecurdir(vol);
2237 /* now get rid of dangling symlinks */
2238 if ((dp = opendir("."))) {
2239 while ((de = readdir(dp))) {
2240 /* skip this and previous directory */
2241 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2244 /* bail if it's not a symlink */
2245 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2247 return AFPERR_DIRNEMPT;
2250 if ((err = netatalk_unlink(de->d_name))) {
2257 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2262 err = netatalk_rmdir_all_errors(cfrombstring(fdir->d_u_name));
2263 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2264 cnid_delete(vol->v_cdb, fdir->d_did);
2265 dir_remove( vol, fdir );
2269 /* inode is used as key for cnid.
2270 * Close the descriptor only after cnid_delete
2278 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2288 sfunc = (unsigned char) *ibuf++;
2292 if (sfunc >= 3 && sfunc <= 6) {
2293 if (afp_version < 30) {
2294 return( AFPERR_PARAM );
2301 case 3 :/* unicode */
2302 memcpy( &id, ibuf, sizeof( id ));
2305 if (( pw = getpwuid( id )) == NULL ) {
2306 return( AFPERR_NOITEM );
2308 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2309 pw->pw_name, -1, &name);
2316 case 4 : /* unicode */
2317 memcpy( &id, ibuf, sizeof( id ));
2320 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2321 return( AFPERR_NOITEM );
2323 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2324 gr->gr_name, -1, &name);
2330 #ifdef HAVE_NFSv4_ACLS
2331 case 5 : /* UUID -> username */
2332 case 6 : /* UUID -> groupname */
2333 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2334 return AFPERR_PARAM;
2335 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2337 len = getnamefromuuid( ibuf, &name, &type);
2338 if (len != 0) /* its a error code, not len */
2339 return AFPERR_NOITEM;
2340 if (type == UUID_USER) {
2341 if (( pw = getpwnam( name )) == NULL )
2342 return( AFPERR_NOITEM );
2343 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2344 id = htonl(UUID_USER);
2345 memcpy( rbuf, &id, sizeof( id ));
2346 id = htonl( pw->pw_uid);
2347 rbuf += sizeof( id );
2348 memcpy( rbuf, &id, sizeof( id ));
2349 rbuf += sizeof( id );
2350 *rbuflen = 2 * sizeof( id );
2351 } else { /* type == UUID_GROUP */
2352 if (( gr = getgrnam( name )) == NULL )
2353 return( AFPERR_NOITEM );
2354 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2355 id = htonl(UUID_GROUP);
2356 memcpy( rbuf, &id, sizeof( id ));
2357 rbuf += sizeof( id );
2358 id = htonl( gr->gr_gid);
2359 memcpy( rbuf, &id, sizeof( id ));
2360 rbuf += sizeof( id );
2361 *rbuflen = 2 * sizeof( id );
2366 return( AFPERR_PARAM );
2370 len = strlen( name );
2373 u_int16_t tp = htons(len);
2374 memcpy(rbuf, &tp, sizeof(tp));
2383 memcpy( rbuf, name, len );
2391 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2400 sfunc = (unsigned char) *ibuf++;
2402 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2405 case 2 : /* unicode */
2406 if (afp_version < 30) {
2407 return( AFPERR_PARAM );
2409 memcpy(&ulen, ibuf, sizeof(ulen));
2412 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2416 len = (unsigned char) *ibuf++;
2418 #ifdef HAVE_NFSv4_ACLS
2419 case 5 : /* username -> UUID */
2420 case 6 : /* groupname -> UUID */
2421 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2422 return AFPERR_PARAM;
2423 memcpy(&ulen, ibuf, sizeof(ulen));
2429 return( AFPERR_PARAM );
2435 return AFPERR_PARAM;
2438 case 1 : /* unicode */
2440 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2441 return( AFPERR_NOITEM );
2445 memcpy( rbuf, &id, sizeof( id ));
2446 *rbuflen = sizeof( id );
2449 case 2 : /* unicode */
2451 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2452 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2453 return( AFPERR_NOITEM );
2456 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2458 memcpy( rbuf, &id, sizeof( id ));
2459 *rbuflen = sizeof( id );
2461 #ifdef HAVE_NFSv4_ACLS
2462 case 5 : /* username -> UUID */
2463 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2464 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2465 return AFPERR_NOITEM;
2466 *rbuflen = UUID_BINSIZE;
2468 case 6 : /* groupname -> UUID */
2469 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2470 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2471 return AFPERR_NOITEM;
2472 *rbuflen = UUID_BINSIZE;
2480 /* ------------------------------------
2481 variable DID support
2483 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2494 /* do nothing as dids are static for the life of the process. */
2498 memcpy(&vid, ibuf, sizeof( vid ));
2499 ibuf += sizeof( vid );
2500 if (( vol = getvolbyvid( vid )) == NULL ) {
2501 return( AFPERR_PARAM );
2504 memcpy( &did, ibuf, sizeof( did ));
2505 ibuf += sizeof( did );
2506 if (( dir = dirlookup( vol, did )) == NULL ) {
2507 return( AFPERR_PARAM );
2510 /* dir_remove -- deletedid */
2516 /* did creation gets done automatically
2517 * there's a pb again with case but move it to cname
2519 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2522 struct dir *parentdir;
2530 memcpy(&vid, ibuf, sizeof(vid));
2531 ibuf += sizeof( vid );
2533 if (NULL == ( vol = getvolbyvid( vid )) ) {
2534 return( AFPERR_PARAM );
2537 memcpy(&did, ibuf, sizeof(did));
2538 ibuf += sizeof(did);
2540 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2544 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2545 return get_afp_errno(AFPERR_PARAM);
2548 if ( *path->m_name != '\0' ) {
2549 return path_error(path, AFPERR_NOOBJ);
2552 if ( !path->st_valid && of_stat(path ) < 0 ) {
2553 return( AFPERR_NOOBJ );
2555 if ( path->st_errno ) {
2556 return( AFPERR_NOOBJ );
2559 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2560 *rbuflen = sizeof(curdir->d_did);