2 * $Id: directory.c,v 1.131.2.14 2010-02-11 14:13:06 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];
433 struct dir *ret = NULL, *pdir;
434 bstring fullpath = NULL;
435 char *upath = NULL, *mpath;
438 int buflen = 12 + MAXPATHLEN + 1;
442 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
444 /* check for did 0, 1 and 2 */
445 if (did == 0 || vol == NULL) { /* 1 */
446 afp_errno = AFPERR_PARAM;
448 } else if (did == DIRDID_ROOT_PARENT) {
449 rootParent.d_vid = vol->v_vid;
450 return (&rootParent);
451 } else if (did == DIRDID_ROOT) {
455 /* Search the cache */
456 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
457 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
458 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
462 /* It's not there anymore, so remove it */
463 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
464 dir_remove(vol, ret);
465 afp_errno = AFPERR_NOOBJ;
476 utf8 = utf8_encoding();
477 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
479 /* Get it from the database */
481 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL
482 || (upath = strdup(upath)) == NULL) { /* 3 */
483 afp_errno = AFPERR_NOOBJ;
490 * Recurse up the tree, terminates in dirlookup when either
491 * - DIRDID_ROOT is hit
492 * - a cached entry is found
494 if ((pdir = dirlookup(vol, pdid)) == NULL) {
499 /* build the fullpath */
500 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
501 || bconchar(fullpath, '/') != BSTR_OK
502 || bcatcstr(fullpath, upath) != BSTR_OK) {
507 /* stat it and check if it's a dir */
508 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
510 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
513 afp_errno = AFPERR_NOOBJ;
517 afp_errno = AFPERR_ACCESS;
521 afp_errno = AFPERR_MISC;
526 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
527 afp_errno = AFPERR_BADTYPE;
533 /* Get macname from unix name */
534 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
535 afp_errno = AFPERR_NOOBJ;
540 /* Create struct dir */
541 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
542 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
547 /* Add it to the cache only if it's a dir */
548 if (dircache_add(ret) != 0) { /* 7 */
553 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
554 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
558 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
559 ntohl(did), AfpErr2name(afp_errno));
571 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
573 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
578 static u_int32_t did = 0;
579 static char cname[MAXPATHLEN];
580 static char lname[MAXPATHLEN];
581 ucs2_t u2_path[MAXPATHLEN];
582 ucs2_t u2_dename[MAXPATHLEN];
583 char *tmp, *savepath;
585 if (!(vol->v_flags & AFPVOL_CASEINSEN))
588 if (veto_file(ENUMVETO, path->u_name))
591 savepath = path->u_name;
593 /* very simple cache */
594 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
595 path->u_name = cname;
597 if (of_stat( path ) == 0 ) {
600 /* something changed, we cannot stat ... */
604 if (NULL == ( dp = opendir( "." )) ) {
605 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
610 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
611 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
612 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
614 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
616 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
617 if (NULL == check_dirent(vol, de->d_name))
620 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
623 if (strcasecmp_w( u2_path, u2_dename) == 0) {
625 strlcpy(cname, de->d_name, sizeof(cname));
626 path->u_name = cname;
628 if (of_stat( path ) == 0 ) {
629 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
630 strlcpy(lname, tmp, sizeof(lname));
643 /* invalidate cache */
646 path->u_name = savepath;
648 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
654 * @brief Construct struct dir
656 * Construct struct dir from parameters.
658 * @param m_name (r) directory name in UTF8-dec
659 * @param u_name (r) directory name in server side encoding
660 * @param vol (r) pointer to struct vol
661 * @param pdid (r) Parent CNID
662 * @param did (r) CNID
663 * @param fullpath (r) Full unix path to dir
665 * @returns pointer to new struct dir or NULL on error
667 * @note Most of the time mac name and unix name are the same.
669 struct dir *dir_new(const char *m_name,
671 const struct vol *vol,
678 dir = (struct dir *) calloc(1, sizeof( struct dir ));
682 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
687 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
690 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
691 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
692 dir->d_m_name_ucs2 = NULL;
695 if (m_name == u_name || !strcmp(m_name, u_name)) {
696 dir->d_u_name = dir->d_m_name;
698 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
699 bdestroy(dir->d_m_name);
706 dir->d_vid = vol->v_vid;
707 dir->d_fullpath = path;
712 * @brief Free a struct dir and all its members
714 * @param (rw) pointer to struct dir
716 void dir_free(struct dir *dir)
718 if (dir->d_u_name != dir->d_m_name) {
719 bdestroy(dir->d_u_name);
721 if (dir->d_m_name_ucs2)
722 free(dir->d_m_name_ucs2);
723 bdestroy(dir->d_m_name);
724 bdestroy(dir->d_fullpath);
729 * @brief Create struct dir from struct path
731 * Create a new struct dir from struct path. Then add it to the cache.
732 * The caller must have assured that the dir is not already in the cache,
733 * cf theAFP_ASSERTion.
734 * 1. Open adouble file, get CNID from it.
735 * 2. Search the database, hinting with the CNID from (1).
736 * 3. Build fullpath and create struct dir.
737 * 4. Add it to the cache.
739 * @param vol (r) pointer to struct vol
740 * @param dir (r) pointer to parrent directory
741 * @param path (rw) pointer to struct path with valid path->u_name
742 * @param len (r) strlen of path->u_name
744 * @returns Pointer to new struct dir or NULL on error.
746 * @note Function also assigns path->m_name from path->u_name.
748 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
751 struct dir *cdir = NULL;
754 struct adouble *adp = NULL;
762 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
763 /* there's a stray entry in the dircache */
764 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
765 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
766 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
767 if (dir_remove(vol, cdir) != 0) {
773 /* get_id needs adp for reading CNID from adouble file */
774 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
775 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
779 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
785 ad_close_metadata(adp);
787 /* Get macname from unixname */
788 if (path->m_name == NULL) {
789 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
796 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
797 || (bconchar(fullpath, '/') != BSTR_OK)
798 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
799 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
804 /* Allocate and initialize struct dir */
805 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
810 if ((dircache_add(cdir)) != 0) { /* 4 */
811 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
817 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
818 cfrombstring(dir->d_u_name), path->u_name, err);
821 ad_close_metadata(adp);
822 if (!cdir && fullpath)
829 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
830 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
831 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
838 * @brief Remove a dir from a cache and free it and any ressources with it
840 * 1. Check if the dir is locked or has opened forks
841 * 2. If it's a request to remove curdir, just chdir to volume root
842 * 3. Remove it from the cache
843 * 4. Remove the dir plus any allocated resources it references
845 * @param (r) pointer to struct vol
846 * @param (rw) pointer to struct dir
848 int dir_remove(const struct vol *vol, struct dir *dir)
853 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
856 if (dir->d_flags & DIRF_CACHELOCK || dir->d_ofork) { /* 1 */
857 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): dir is locked or has opened forks",
858 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
862 if (curdir == dir) { /* 2 */
863 if (movecwd(vol, vol->v_root) < 0) {
864 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
868 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
869 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
871 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 3 */
872 dir_free(dir); /* 4 */
878 * @brief Modify a struct dir, adjust cache
880 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
881 * If given new_uname == new_mname, new_uname will point to new_mname.
883 * @param vol (r) pointer to struct vol
884 * @param dir (rw) pointer to struct dir
885 * @param pdid (r) new parent DID
886 * @param did (r) new DID
887 * @param new_mname (r) new mac-name
888 * @param new_uname (r) new unix-name
890 int dir_modify(const struct vol *vol,
894 const char *new_mname,
895 const char *new_uname,
896 bstring pdir_fullpath)
900 /* Remove it from the cache */
901 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
909 /* free uname if it's not the same as mname */
910 if (dir->d_m_name != dir->d_u_name)
911 bdestroy(dir->d_u_name);
913 if (new_uname == NULL)
914 new_uname = new_mname;
916 /* assign new name */
917 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
918 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
922 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
923 dir->d_u_name = dir->d_m_name;
925 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
926 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
933 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
935 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
937 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
941 if (dir->d_m_name_ucs2)
942 free(dir->d_m_name_ucs2);
943 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))
944 dir->d_m_name_ucs2 = NULL;
946 /* Re-add it to the cache */
947 if ((dircache_add(dir)) != 0) {
956 * @brief Resolve a catalog node name path
958 * 1. Evaluate path type
959 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
960 * path from dirname, so eg getdirparams has sth it can chew on. curdir
961 * is dir parent then. All this is done in path_from_dir().
962 * 3. Parse next cnode name in path, cases:
963 * 4. single "\0" -> do nothing
964 * 5. two or more consecutive "\0" -> chdir("..") one or more times
965 * 6. cnode name -> copy it to path.m_name
966 * 7. Get unix name from mac name
967 * 8. Special handling of request with did 1
968 * 9. stat the cnode name
969 * 10. If it's not there, it's probably an afp_createfile|dir,
970 * return with curdir = dir parent, struct path = dirname
971 * 11. If it's there and it's a file, it must should be the last element of the requested
972 * path. Return with curdir = cnode name parent dir, struct path = filename
973 * 12. Treat symlinks like files, dont follow them
975 * 14. Search the dircache for it
976 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
977 * 16. chdir into the dir and
978 * 17. set m_name to the mac equivalent of "."
981 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
983 static char path[ MAXPATHLEN + 1];
984 static struct path ret;
994 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
997 afp_errno = AFPERR_NOOBJ;
998 memset(&ret, 0, sizeof(ret));
1000 switch (ret.m_type = *data) { /* 1 */
1003 len = (unsigned char) *data++;
1005 if (afp_version >= 30) {
1011 if (afp_version >= 30) {
1013 memcpy(&hint, data, sizeof(hint));
1015 data += sizeof(hint);
1017 memcpy(&len16, data, sizeof(len16));
1023 /* else it's an error */
1025 afp_errno = AFPERR_PARAM;
1028 *cpath += len + size;
1033 if (movecwd(vol, dir) < 0 ) {
1034 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1035 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1037 return path_from_dir(vol, dir, &ret);
1042 while (len) { /* 3 */
1043 if (*data == 0) { /* 4 or 5 */
1046 while (len > 0 && *data == 0) { /* 5 */
1047 /* chdir to parrent dir */
1048 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1050 if (movecwd( vol, dir ) < 0 ) {
1051 dir_remove(vol, dir);
1061 for ( p = path; *data != 0 && len > 0; len-- ) {
1063 if (p > &path[ MAXPATHLEN]) {
1064 afp_errno = AFPERR_PARAM;
1068 *p = 0; /* Terminate string */
1071 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1072 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1076 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1078 /* Prevent access to our special folders like .AppleDouble */
1079 if (check_name(vol, ret.u_name)) {
1080 /* the name is illegal */
1081 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1082 afp_errno = AFPERR_PARAM;
1086 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1088 * Special case: CNID 1
1089 * root parent (did 1) has one child: the volume. Requests for did=1 with
1090 * some <name> must check against the volume name.
1092 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1098 * CNID != 1, eg. most of the times we take this way.
1099 * Now check if current path-part is a file or dir:
1100 * o if it's dir we have to step into it
1101 * o if it's a file we expect it to be the last part of the requested path
1102 * and thus call continue which should terminate the while loop because
1105 if (of_stat(&ret) != 0) { /* 9 */
1107 * ret.u_name doesn't exist, might be afp_createfile|dir
1108 * that means it should have been the last part
1111 /* it wasn't the last part, so we have a bogus path request */
1112 afp_errno = AFPERR_NOOBJ;
1116 * this will terminate clean in while (1) because len == 0,
1117 * probably afp_createfile|dir
1119 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1123 switch (ret.st.st_mode & S_IFMT) {
1124 case S_IFREG: /* 11 */
1125 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1127 /* it wasn't the last part, so we have a bogus path request */
1128 afp_errno = AFPERR_PARAM;
1131 continue; /* continues while loop */
1132 case S_IFLNK: /* 12 */
1133 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1135 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1136 afp_errno = AFPERR_PARAM;
1139 continue; /* continues while loop */
1140 case S_IFDIR: /* 13 */
1143 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1144 afp_errno = AFPERR_NODIR;
1148 /* Search the cache */
1149 int unamelen = strlen(ret.u_name);
1150 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1152 /* Not in cache, create one */
1153 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1154 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1155 ntohl(dir->d_did), ret.u_name, getcwdpath());
1159 } /* if/else cnid==1 */
1161 /* Now chdir to the evaluated dir */
1162 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1163 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1164 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1166 return path_from_dir(vol, cdir, &ret);
1171 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1174 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1175 afp_errno = AFPERR_DID1;
1179 if (ret.m_name[0] == 0) {
1180 /* Last part was a dir */
1181 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1185 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1186 cfrombstring(dir->d_fullpath),
1187 cfrombstring(curdir->d_fullpath),
1194 * @brief chdir() to dir
1196 * @param vol (r) pointer to struct vol
1197 * @param dir (r) pointer to struct dir
1199 * @returns 0 on success, -1 on error with afp_errno set appropiately
1201 int movecwd(const struct vol *vol, struct dir *dir)
1208 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1209 cfrombstring(curdir->d_fullpath), getcwdpath());
1213 if (dir->d_did == DIRDID_ROOT_PARENT) {
1214 curdir = &rootParent;
1218 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1220 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1221 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1225 afp_errno = AFPERR_ACCESS;
1228 afp_errno = AFPERR_NOOBJ;
1238 * We can't use unix file's perm to support Apple's inherited protection modes.
1239 * If we aren't the file's owner we can't change its perms when moving it and smb
1240 * nfs,... don't even try.
1242 #define AFP_CHECK_ACCESS
1244 int check_access(char *path, int mode)
1246 #ifdef AFP_CHECK_ACCESS
1254 accessmode(p, &ma, curdir, NULL);
1255 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1257 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1263 /* --------------------- */
1264 int file_access(struct path *path, int mode)
1268 accessmode(path->u_name, &ma, curdir, &path->st);
1269 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1271 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1277 /* --------------------- */
1278 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1280 dir->offcnt = count;
1281 dir->ctime = st->st_ctime;
1282 dir->d_flags &= ~DIRF_CNID;
1286 /* ---------------------
1287 * is our cached also for reenumerate id?
1289 int dirreenumerate(struct dir *dir, struct stat *st)
1291 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1294 /* ------------------------------
1296 (name, dir) with curdir:name == dir, from afp_enumerate
1299 int getdirparams(const struct vol *vol,
1300 u_int16_t bitmap, struct path *s_path,
1302 char *buf, size_t *buflen )
1306 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1307 int bit = 0, isad = 0;
1313 struct stat *st = &s_path->st;
1314 char *upath = s_path->u_name;
1316 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1317 (1 << DIRPBIT_CDATE) |
1318 (1 << DIRPBIT_MDATE) |
1319 (1 << DIRPBIT_BDATE) |
1320 (1 << DIRPBIT_FINFO)))) {
1322 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1323 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1325 if (ad.ad_md->adf_flags & O_CREAT) {
1326 /* We just created it */
1327 ad_setname(&ad, s_path->m_name);
1342 while ( bitmap != 0 ) {
1343 while (( bitmap & 1 ) == 0 ) {
1351 ad_getattr(&ad, &ashort);
1352 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1353 ashort = htons(ATTRBIT_INVISIBLE);
1356 ashort |= htons(ATTRBIT_SHARED);
1357 memcpy( data, &ashort, sizeof( ashort ));
1358 data += sizeof( ashort );
1362 memcpy( data, &pdid, sizeof( pdid ));
1363 data += sizeof( pdid );
1364 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1365 s_path->u_name, ntohl(pdid));
1368 case DIRPBIT_CDATE :
1369 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1370 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1371 memcpy( data, &aint, sizeof( aint ));
1372 data += sizeof( aint );
1375 case DIRPBIT_MDATE :
1376 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1377 memcpy( data, &aint, sizeof( aint ));
1378 data += sizeof( aint );
1381 case DIRPBIT_BDATE :
1382 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1383 aint = AD_DATE_START;
1384 memcpy( data, &aint, sizeof( aint ));
1385 data += sizeof( aint );
1388 case DIRPBIT_FINFO :
1390 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1391 } else { /* no appledouble */
1392 memset( data, 0, 32 );
1393 /* set default view -- this also gets done in ad_open() */
1394 ashort = htons(FINDERINFO_CLOSEDVIEW);
1395 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1397 /* dot files are by default visible */
1398 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1399 ashort = htons(FINDERINFO_INVISIBLE);
1400 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1406 case DIRPBIT_LNAME :
1407 if (dir->d_m_name) /* root of parent can have a null name */
1410 memset(data, 0, sizeof(u_int16_t));
1411 data += sizeof( u_int16_t );
1414 case DIRPBIT_SNAME :
1415 memset(data, 0, sizeof(u_int16_t));
1416 data += sizeof( u_int16_t );
1420 memcpy( data, &dir->d_did, sizeof( aint ));
1421 data += sizeof( aint );
1422 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1423 s_path->u_name, ntohl(dir->d_did));
1426 case DIRPBIT_OFFCNT :
1428 /* this needs to handle current directory access rights */
1429 if (diroffcnt(dir, st)) {
1430 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1432 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1433 setdiroffcnt(dir, st, ret);
1434 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1436 ashort = htons( ashort );
1437 memcpy( data, &ashort, sizeof( ashort ));
1438 data += sizeof( ashort );
1442 aint = htonl(st->st_uid);
1443 memcpy( data, &aint, sizeof( aint ));
1444 data += sizeof( aint );
1448 aint = htonl(st->st_gid);
1449 memcpy( data, &aint, sizeof( aint ));
1450 data += sizeof( aint );
1453 case DIRPBIT_ACCESS :
1454 accessmode( upath, &ma, dir , st);
1456 *data++ = ma.ma_user;
1457 *data++ = ma.ma_world;
1458 *data++ = ma.ma_group;
1459 *data++ = ma.ma_owner;
1462 /* Client has requested the ProDOS information block.
1463 Just pass back the same basic block for all
1464 directories. <shirsch@ibm.net> */
1465 case DIRPBIT_PDINFO :
1466 if (afp_version >= 30) { /* UTF8 name */
1467 utf8 = kTextEncodingUTF8;
1468 if (dir->d_m_name) /* root of parent can have a null name */
1471 memset(data, 0, sizeof(u_int16_t));
1472 data += sizeof( u_int16_t );
1474 memcpy(data, &aint, sizeof( aint ));
1475 data += sizeof( aint );
1477 else { /* ProDOS Info Block */
1480 ashort = htons( 0x0200 );
1481 memcpy( data, &ashort, sizeof( ashort ));
1482 data += sizeof( ashort );
1483 memset( data, 0, sizeof( ashort ));
1484 data += sizeof( ashort );
1488 case DIRPBIT_UNIXPR :
1489 aint = htonl(st->st_uid);
1490 memcpy( data, &aint, sizeof( aint ));
1491 data += sizeof( aint );
1492 aint = htonl(st->st_gid);
1493 memcpy( data, &aint, sizeof( aint ));
1494 data += sizeof( aint );
1497 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1498 memcpy( data, &aint, sizeof( aint ));
1499 data += sizeof( aint );
1501 accessmode( upath, &ma, dir , st);
1503 *data++ = ma.ma_user;
1504 *data++ = ma.ma_world;
1505 *data++ = ma.ma_group;
1506 *data++ = ma.ma_owner;
1511 ad_close_metadata( &ad );
1513 return( AFPERR_BITMAP );
1519 ashort = htons( data - buf );
1520 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1521 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1523 if ( utf_nameoff ) {
1524 ashort = htons( data - buf );
1525 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1526 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1529 ad_close_metadata( &ad );
1531 *buflen = data - buf;
1535 /* ----------------------------- */
1536 int path_error(struct path *path, int error)
1538 /* - a dir with access error
1539 * - no error it's a file
1542 if (path_isadir(path))
1544 if (path->st_valid && path->st_errno)
1546 return AFPERR_BADTYPE ;
1549 /* ----------------------------- */
1550 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1555 u_int16_t vid, bitmap;
1561 memcpy( &vid, ibuf, sizeof( vid ));
1562 ibuf += sizeof( vid );
1564 if (NULL == ( vol = getvolbyvid( vid )) ) {
1565 return( AFPERR_PARAM );
1568 if (vol->v_flags & AFPVOL_RO)
1569 return AFPERR_VLOCK;
1571 memcpy( &did, ibuf, sizeof( did ));
1572 ibuf += sizeof( int );
1574 if (NULL == ( dir = dirlookup( vol, did )) ) {
1578 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1579 bitmap = ntohs( bitmap );
1580 ibuf += sizeof( bitmap );
1582 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1583 return get_afp_errno(AFPERR_NOOBJ);
1586 if ( *path->m_name != '\0' ) {
1587 rc = path_error(path, AFPERR_NOOBJ);
1588 /* maybe we are trying to set perms back */
1589 if (rc != AFPERR_ACCESS)
1594 * If ibuf is odd, make it even.
1596 if ((u_long)ibuf & 1 ) {
1600 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1601 setvoltime(obj, vol );
1607 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1609 * assume path == '\0' eg. it's a directory in canonical form
1611 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1623 u_int16_t ashort, bshort, oshort;
1625 int change_mdate = 0;
1626 int change_parent_mdate = 0;
1628 u_int16_t bitmap = d_bitmap;
1629 u_char finder_buf[32];
1632 u_int16_t upriv_bit = 0;
1635 upath = path->u_name;
1637 while ( bitmap != 0 ) {
1638 while (( bitmap & 1 ) == 0 ) {
1646 memcpy( &ashort, buf, sizeof( ashort ));
1647 buf += sizeof( ashort );
1649 case DIRPBIT_CDATE :
1651 memcpy(&cdate, buf, sizeof(cdate));
1652 buf += sizeof( cdate );
1654 case DIRPBIT_MDATE :
1655 memcpy(&newdate, buf, sizeof(newdate));
1656 buf += sizeof( newdate );
1658 case DIRPBIT_BDATE :
1660 memcpy(&bdate, buf, sizeof(bdate));
1661 buf += sizeof( bdate );
1663 case DIRPBIT_FINFO :
1665 memcpy( finder_buf, buf, 32 );
1668 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1669 change_parent_mdate = 1;
1670 memcpy( &owner, buf, sizeof(owner));
1671 buf += sizeof( owner );
1674 change_parent_mdate = 1;
1675 memcpy( &group, buf, sizeof( group ));
1676 buf += sizeof( group );
1678 case DIRPBIT_ACCESS :
1680 change_parent_mdate = 1;
1681 ma.ma_user = *buf++;
1682 ma.ma_world = *buf++;
1683 ma.ma_group = *buf++;
1684 ma.ma_owner = *buf++;
1685 mpriv = mtoumode( &ma ) | vol->v_dperm;
1686 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1687 err = set_dir_errors(path, "setdirmode", errno);
1691 /* Ignore what the client thinks we should do to the
1692 ProDOS information block. Skip over the data and
1693 report nothing amiss. <shirsch@ibm.net> */
1694 case DIRPBIT_PDINFO :
1695 if (afp_version < 30) {
1699 err = AFPERR_BITMAP;
1703 case DIRPBIT_UNIXPR :
1704 if (vol_unix_priv(vol)) {
1705 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1706 buf += sizeof( owner );
1707 memcpy( &group, buf, sizeof( group ));
1708 buf += sizeof( group );
1711 change_parent_mdate = 1;
1712 memcpy( &upriv, buf, sizeof( upriv ));
1713 buf += sizeof( upriv );
1714 upriv = ntohl (upriv) | vol->v_dperm;
1715 if (dir_rx_set(upriv)) {
1716 /* maybe we are trying to set perms back */
1717 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1719 err = set_dir_errors(path, "setdirunixmode", errno);
1730 err = AFPERR_BITMAP;
1738 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1740 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1742 * Check to see what we're trying to set. If it's anything
1743 * but ACCESS, UID, or GID, give an error. If it's any of those
1744 * three, we don't need the ad to be open, so just continue.
1746 * note: we also don't need to worry about mdate. also, be quiet
1747 * if we're using the noadouble option.
1749 if (!vol_noadouble(vol) && (d_bitmap &
1750 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1751 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1752 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1753 return AFPERR_ACCESS;
1759 * Check to see if a create was necessary. If it was, we'll want
1760 * to set our name, etc.
1762 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1763 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1769 while ( bitmap != 0 ) {
1770 while (( bitmap & 1 ) == 0 ) {
1778 ad_getattr(&ad, &bshort);
1780 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1781 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1785 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1786 change_parent_mdate = 1;
1787 ad_setattr(&ad, bshort);
1790 case DIRPBIT_CDATE :
1792 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1795 case DIRPBIT_MDATE :
1797 case DIRPBIT_BDATE :
1799 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1802 case DIRPBIT_FINFO :
1804 /* Fixes #2802236 */
1805 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1806 *fflags &= htons(~FINDERINFO_ISHARED);
1808 if ( dir->d_did == DIRDID_ROOT ) {
1810 * Alright, we admit it, this is *really* sick!
1811 * The 4 bytes that we don't copy, when we're dealing
1812 * with the root of a volume, are the directory's
1813 * location information. This eliminates that annoying
1814 * behavior one sees when mounting above another mount
1817 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1818 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1820 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1824 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1825 if ( (dir->d_did == DIRDID_ROOT) &&
1826 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1827 err = set_dir_errors(path, "setdeskowner", errno);
1828 if (isad && err == AFPERR_PARAM) {
1829 err = AFP_OK; /* ???*/
1832 goto setdirparam_done;
1835 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1836 err = set_dir_errors(path, "setdirowner", errno);
1837 goto setdirparam_done;
1841 if (dir->d_did == DIRDID_ROOT)
1842 setdeskowner( -1, ntohl(group) );
1843 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1844 err = set_dir_errors(path, "setdirowner", errno);
1845 goto setdirparam_done;
1848 case DIRPBIT_ACCESS :
1849 if (dir->d_did == DIRDID_ROOT) {
1851 if (!dir_rx_set(mpriv)) {
1852 /* we can't remove read and search for owner on volume root */
1853 err = AFPERR_ACCESS;
1854 goto setdirparam_done;
1858 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1859 err = set_dir_errors(path, "setdirmode", errno);
1860 goto setdirparam_done;
1863 case DIRPBIT_PDINFO :
1864 if (afp_version >= 30) {
1865 err = AFPERR_BITMAP;
1866 goto setdirparam_done;
1869 case DIRPBIT_UNIXPR :
1870 if (vol_unix_priv(vol)) {
1871 if (dir->d_did == DIRDID_ROOT) {
1872 if (!dir_rx_set(upriv)) {
1873 /* we can't remove read and search for owner on volume root */
1874 err = AFPERR_ACCESS;
1875 goto setdirparam_done;
1877 setdeskowner( -1, ntohl(group) );
1878 setdeskmode( upriv );
1880 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1881 err = set_dir_errors(path, "setdirowner", errno);
1882 goto setdirparam_done;
1885 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1886 err = set_dir_errors(path, "setdirunixmode", errno);
1887 goto setdirparam_done;
1891 err = AFPERR_BITMAP;
1892 goto setdirparam_done;
1896 err = AFPERR_BITMAP;
1897 goto setdirparam_done;
1906 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1907 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1911 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1912 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1917 if (path->st_valid && !path->st_errno) {
1918 struct stat *st = &path->st;
1920 if (dir && dir->d_pdid) {
1921 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1925 ad_close_metadata( &ad);
1928 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1929 && gettimeofday(&tv, NULL) == 0) {
1930 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1931 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1932 /* be careful with bitmap because now dir is null */
1933 bitmap = 1<<DIRPBIT_MDATE;
1934 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1935 /* should we reset curdir ?*/
1942 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1956 memcpy( &vid, ibuf, sizeof( vid ));
1957 ibuf += sizeof( vid );
1958 if (NULL == (vol = getvolbyvid( vid )) ) {
1959 return( AFPERR_PARAM );
1962 memcpy( &did, ibuf, sizeof( did ));
1963 ibuf += sizeof( did );
1967 * if it's CNID 2 our only choice to meet the specs is call sync.
1968 * For any other CNID just sync that dir. To my knowledge the
1969 * intended use of FPSyncDir is to sync the volume so all we're
1970 * ever going to see here is probably CNID 2. Anyway, we' prepared.
1973 if ( ntohl(did) == 2 ) {
1976 if (NULL == ( dir = dirlookup( vol, did )) ) {
1977 return afp_errno; /* was AFPERR_NOOBJ */
1980 if (movecwd( vol, dir ) < 0 )
1981 return ( AFPERR_NOOBJ );
1984 * Assuming only OSens that have dirfd also may require fsyncing directories
1985 * in order to flush metadata e.g. Linux.
1989 if (NULL == ( dp = opendir( "." )) ) {
1992 return( AFPERR_NOOBJ );
1994 return( AFPERR_ACCESS );
1996 return( AFPERR_PARAM );
2000 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2003 if ( fsync ( dfd ) < 0 )
2004 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2005 dir->d_u_name, strerror(errno) );
2006 closedir(dp); /* closes dfd too */
2009 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2012 return( AFPERR_NOOBJ );
2014 return( AFPERR_ACCESS );
2016 return( AFPERR_PARAM );
2020 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2021 vol->ad_path(".", ADFLAGS_DIR) );
2023 if ( fsync(dfd) < 0 )
2024 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2025 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2032 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2038 struct path *s_path;
2046 memcpy( &vid, ibuf, sizeof( vid ));
2047 ibuf += sizeof( vid );
2048 if (NULL == ( vol = getvolbyvid( vid )) ) {
2049 return( AFPERR_PARAM );
2052 if (vol->v_flags & AFPVOL_RO)
2053 return AFPERR_VLOCK;
2055 memcpy( &did, ibuf, sizeof( did ));
2056 ibuf += sizeof( did );
2057 if (NULL == ( dir = dirlookup( vol, did )) ) {
2058 return afp_errno; /* was AFPERR_NOOBJ */
2060 /* for concurrent access we need to be sure we are not in the
2061 * folder we want to create...
2065 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2066 return get_afp_errno(AFPERR_PARAM);
2068 /* cname was able to move curdir to it! */
2069 if (*s_path->m_name == '\0')
2070 return AFPERR_EXIST;
2072 upath = s_path->u_name;
2074 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2078 if (of_stat(s_path) < 0) {
2084 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2088 if ( movecwd( vol, dir ) < 0 ) {
2089 return( AFPERR_PARAM );
2092 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2093 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2094 if (vol_noadouble(vol))
2095 goto createdir_done;
2096 return( AFPERR_ACCESS );
2098 ad_setname(&ad, s_path->m_name);
2099 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2102 ad_close_metadata( &ad);
2105 #ifdef HAVE_NFSv4_ACLS
2106 /* FIXME: are we really inside the created dir? */
2107 addir_inherit_acl(vol);
2110 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2111 *rbuflen = sizeof( u_int32_t );
2112 setvoltime(obj, vol );
2117 * dst new unix filename (not a pathname)
2118 * newname new mac name
2121 int renamedir(const struct vol *vol, char *src, char *dst,
2123 struct dir *newparent,
2129 /* existence check moved to afp_moveandrename */
2130 if ( unix_rename( src, dst ) < 0 ) {
2133 return( AFPERR_NOOBJ );
2135 return( AFPERR_ACCESS );
2137 return AFPERR_VLOCK;
2139 /* tried to move directory into a subdirectory of itself */
2140 return AFPERR_CANTMOVE;
2142 /* this needs to copy and delete. bleah. that means we have
2143 * to deal with entire directory hierarchies. */
2144 if ((err = copydir(vol, src, dst)) < 0) {
2148 if ((err = deletedir(src)) < 0)
2152 return( AFPERR_PARAM );
2156 vol->vfs->vfs_renamedir(vol, src, dst);
2158 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2160 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2161 ad_setname(&ad, newname);
2163 ad_close_metadata( &ad);
2166 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2167 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2174 /* delete an empty directory */
2175 int deletecurdir(struct vol *vol)
2185 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2186 return( AFPERR_ACCESS );
2191 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2192 /* we never want to create a resource fork here, we are going to delete it */
2193 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2195 ad_getattr(&ad, &ashort);
2196 ad_close( &ad, ADFLAGS_HF );
2197 if ((ashort & htons(ATTRBIT_NODELETE))) {
2198 return AFPERR_OLOCK;
2201 err = vol->vfs->vfs_deletecurdir(vol);
2206 /* now get rid of dangling symlinks */
2207 if ((dp = opendir("."))) {
2208 while ((de = readdir(dp))) {
2209 /* skip this and previous directory */
2210 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2213 /* bail if it's not a symlink */
2214 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2216 return AFPERR_DIRNEMPT;
2219 if ((err = netatalk_unlink(de->d_name))) {
2226 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2231 err = netatalk_rmdir_all_errors(cfrombstring(fdir->d_u_name));
2232 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2233 cnid_delete(vol->v_cdb, fdir->d_did);
2234 dir_remove( vol, fdir );
2238 /* inode is used as key for cnid.
2239 * Close the descriptor only after cnid_delete
2247 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2257 sfunc = (unsigned char) *ibuf++;
2261 if (sfunc >= 3 && sfunc <= 6) {
2262 if (afp_version < 30) {
2263 return( AFPERR_PARAM );
2270 case 3 :/* unicode */
2271 memcpy( &id, ibuf, sizeof( id ));
2274 if (( pw = getpwuid( id )) == NULL ) {
2275 return( AFPERR_NOITEM );
2277 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2278 pw->pw_name, -1, &name);
2285 case 4 : /* unicode */
2286 memcpy( &id, ibuf, sizeof( id ));
2289 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2290 return( AFPERR_NOITEM );
2292 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2293 gr->gr_name, -1, &name);
2299 #ifdef HAVE_NFSv4_ACLS
2300 case 5 : /* UUID -> username */
2301 case 6 : /* UUID -> groupname */
2302 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2303 return AFPERR_PARAM;
2304 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2306 len = getnamefromuuid( ibuf, &name, &type);
2307 if (len != 0) /* its a error code, not len */
2308 return AFPERR_NOITEM;
2309 if (type == UUID_USER) {
2310 if (( pw = getpwnam( name )) == NULL )
2311 return( AFPERR_NOITEM );
2312 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2313 id = htonl(UUID_USER);
2314 memcpy( rbuf, &id, sizeof( id ));
2315 id = htonl( pw->pw_uid);
2316 rbuf += sizeof( id );
2317 memcpy( rbuf, &id, sizeof( id ));
2318 rbuf += sizeof( id );
2319 *rbuflen = 2 * sizeof( id );
2320 } else { /* type == UUID_GROUP */
2321 if (( gr = getgrnam( name )) == NULL )
2322 return( AFPERR_NOITEM );
2323 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2324 id = htonl(UUID_GROUP);
2325 memcpy( rbuf, &id, sizeof( id ));
2326 rbuf += sizeof( id );
2327 id = htonl( gr->gr_gid);
2328 memcpy( rbuf, &id, sizeof( id ));
2329 rbuf += sizeof( id );
2330 *rbuflen = 2 * sizeof( id );
2335 return( AFPERR_PARAM );
2339 len = strlen( name );
2342 u_int16_t tp = htons(len);
2343 memcpy(rbuf, &tp, sizeof(tp));
2352 memcpy( rbuf, name, len );
2360 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2369 sfunc = (unsigned char) *ibuf++;
2371 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2374 case 2 : /* unicode */
2375 if (afp_version < 30) {
2376 return( AFPERR_PARAM );
2378 memcpy(&ulen, ibuf, sizeof(ulen));
2381 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2385 len = (unsigned char) *ibuf++;
2387 #ifdef HAVE_NFSv4_ACLS
2388 case 5 : /* username -> UUID */
2389 case 6 : /* groupname -> UUID */
2390 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2391 return AFPERR_PARAM;
2392 memcpy(&ulen, ibuf, sizeof(ulen));
2398 return( AFPERR_PARAM );
2404 return AFPERR_PARAM;
2407 case 1 : /* unicode */
2409 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2410 return( AFPERR_NOITEM );
2414 memcpy( rbuf, &id, sizeof( id ));
2415 *rbuflen = sizeof( id );
2418 case 2 : /* unicode */
2420 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2421 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2422 return( AFPERR_NOITEM );
2425 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2427 memcpy( rbuf, &id, sizeof( id ));
2428 *rbuflen = sizeof( id );
2430 #ifdef HAVE_NFSv4_ACLS
2431 case 5 : /* username -> UUID */
2432 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2433 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2434 return AFPERR_NOITEM;
2435 *rbuflen = UUID_BINSIZE;
2437 case 6 : /* groupname -> UUID */
2438 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2439 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2440 return AFPERR_NOITEM;
2441 *rbuflen = UUID_BINSIZE;
2449 /* ------------------------------------
2450 variable DID support
2452 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2463 /* do nothing as dids are static for the life of the process. */
2467 memcpy(&vid, ibuf, sizeof( vid ));
2468 ibuf += sizeof( vid );
2469 if (( vol = getvolbyvid( vid )) == NULL ) {
2470 return( AFPERR_PARAM );
2473 memcpy( &did, ibuf, sizeof( did ));
2474 ibuf += sizeof( did );
2475 if (( dir = dirlookup( vol, did )) == NULL ) {
2476 return( AFPERR_PARAM );
2479 /* dir_remove -- deletedid */
2485 /* did creation gets done automatically
2486 * there's a pb again with case but move it to cname
2488 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2491 struct dir *parentdir;
2499 memcpy(&vid, ibuf, sizeof(vid));
2500 ibuf += sizeof( vid );
2502 if (NULL == ( vol = getvolbyvid( vid )) ) {
2503 return( AFPERR_PARAM );
2506 memcpy(&did, ibuf, sizeof(did));
2507 ibuf += sizeof(did);
2509 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2513 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2514 return get_afp_errno(AFPERR_PARAM);
2517 if ( *path->m_name != '\0' ) {
2518 return path_error(path, AFPERR_NOOBJ);
2521 if ( !path->st_valid && of_stat(path ) < 0 ) {
2522 return( AFPERR_NOOBJ );
2524 if ( path->st_errno ) {
2525 return( AFPERR_NOOBJ );
2528 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2529 *rbuflen = sizeof(curdir->d_did);