2 * $Id: directory.c,v 1.131.2.5 2010-02-01 19:11:01 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 case-insensitivity is gone
53 * o setdirparams doesn't change parent mdate anymore
54 * o catsearch doesn't work, see FIXMEs in catsearch.c
55 * o curdir per volume caching is gone
56 * o directory offspring count calculation probably broken
60 /*******************************************************************************************
62 ******************************************************************************************/
65 struct dir rootParent = {
66 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
67 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
68 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
70 struct dir *curdir = &rootParent;
71 struct path Cur_Path = {
77 0, /* stat is not set */
81 /*******************************************************************************************
83 ******************************************************************************************/
86 /* -------------------------
87 appledouble mkdir afp error code.
89 static int netatalk_mkdir(const char *name)
91 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
94 return( AFPERR_NOOBJ );
96 return( AFPERR_VLOCK );
99 return( AFPERR_ACCESS );
101 return( AFPERR_EXIST );
104 return( AFPERR_DFULL );
106 return( AFPERR_PARAM );
112 /* ------------------- */
113 static int deletedir(char *dir)
115 char path[MAXPATHLEN + 1];
123 if ((len = strlen(dir)) +2 > sizeof(path))
127 if ((dp = opendir(dir)) == NULL)
133 remain = sizeof(path) -len -1;
134 while ((de = readdir(dp)) && err == AFP_OK) {
135 /* skip this and previous directory */
136 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
139 if (strlen(de->d_name) > remain) {
143 strcpy(path + len, de->d_name);
144 if (stat(path, &st)) {
147 if (S_ISDIR(st.st_mode)) {
148 err = deletedir(path);
150 err = netatalk_unlink(path);
155 /* okay. the directory is empty. delete it. note: we already got rid
158 err = netatalk_rmdir(dir);
163 /* do a recursive copy. */
164 static int copydir(const struct vol *vol, char *src, char *dst)
166 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
175 /* doesn't exist or the path is too long. */
176 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
177 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
178 ((dp = opendir(src)) == NULL))
181 /* try to create the destination directory */
182 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
187 /* set things up to copy */
191 srem = sizeof(spath) - slen -1;
196 drem = sizeof(dpath) - dlen -1;
199 while ((de = readdir(dp))) {
200 /* skip this and previous directory */
201 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
204 if (strlen(de->d_name) > srem) {
208 strcpy(spath + slen, de->d_name);
210 if (stat(spath, &st) == 0) {
211 if (strlen(de->d_name) > drem) {
215 strcpy(dpath + dlen, de->d_name);
217 if (S_ISDIR(st.st_mode)) {
218 if (AFP_OK != (err = copydir(vol, spath, dpath)))
220 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
224 /* keep the same time stamp. */
225 ut.actime = ut.modtime = st.st_mtime;
231 /* keep the same time stamp. */
232 if (stat(src, &st) == 0) {
233 ut.actime = ut.modtime = st.st_mtime;
242 /* ---------------------
243 * is our cached offspring count valid?
245 static int diroffcnt(struct dir *dir, struct stat *st)
247 return st->st_ctime == dir->ctime;
250 /* --------------------- */
251 static int invisible_dots(const struct vol *vol, const char *name)
253 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
256 /* ------------------ */
257 static int set_dir_errors(struct path *path, const char *where, int err)
262 return AFPERR_ACCESS;
266 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
271 * @brief Convert name in client encoding to server encoding
273 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
274 * This only gets called from cname().
276 * @returns 0 on success, -1 on error
278 * @note If the passed ret->m_name is mangled, we'll demangle it
280 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
282 static char temp[ MAXPATHLEN + 1];
286 if (afp_version >= 30) {
288 if (dir->d_did == DIRDID_ROOT_PARENT) {
290 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
291 * So we compare it with the longname from the current volume and if they match
292 * we overwrite the requested path with the utf8 volume name so that the following
295 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
296 if (strcasecmp(ret->m_name, temp) == 0)
297 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
300 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
301 afp_errno = AFPERR_PARAM;
304 strcpy(ret->m_name, temp);
308 /* check for OS X mangled filename :( */
309 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
310 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
311 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
313 if (t != ret->m_name) {
315 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
316 * flags weren't the same
318 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
319 /* at last got our view of mac name */
320 strcpy(ret->m_name, t);
323 } /* afp_version >= 30 */
325 /* If we haven't got it by now, get it */
326 if (ret->u_name == NULL) {
327 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
328 afp_errno = AFPERR_PARAM;
337 * @brief Build struct path from struct dir
339 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
340 * 1. move cwd into parent dir (we're often already there, but not always)
342 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
345 * it's tricky: movecwd failed some of dir path are not there anymore.
346 * FIXME: Is it true with other errors?
348 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
354 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 )
357 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
358 if (dir->d_m_name == dir->d_u_name) {
359 ret->u_name = ret->m_name;
361 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
362 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
368 ret->st_errno = EACCES;
371 LOG(log_debug, logtype_afpd, "cname(AFPERR_ACCESS:'%s') {path-from-dir: curdir:'%s', path:'%s'}",
372 cfrombstring(dir->d_fullpath),
373 cfrombstring(curdir->d_fullpath),
379 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 )
382 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
383 if (dir->d_m_name == dir->d_u_name) {
384 ret->u_name = ret->m_name;
386 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
387 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
392 ret->st_errno = ENOENT;
395 dir_remove(vol, dir);
402 /* DEADC0DE: never get here */
407 /*********************************************************************************************
409 ********************************************************************************************/
411 int get_afp_errno(const int param)
413 if (afp_errno != AFPERR_DID1)
419 * @brief Resolve a DID
421 * Resolve a DID, allocate a struct dir for it
422 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
423 * 2. Check if the DID is in the cache.
424 * 3. If it's not in the cache resolve it via the database.
425 * 4. Build complete server-side path to the dir.
426 * 5. Check if it exists and is a directory.
427 * 6. Create the struct dir and populate it.
428 * 7. Add it to the cache.
430 * @param vol (r) pointer to struct vol
431 * @param did (r) DID to resolve
433 * @returns pointer to struct dir
435 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
436 * and we are really bad in this case.
438 struct dir *dirlookup(const struct vol *vol, cnid_t did)
440 static char buffer[12 + MAXPATHLEN + 1];
441 struct bstrList *pathlist = NULL;
442 bstring fullpath = NULL;
444 struct dir *ret = NULL;
448 int buflen = 12 + MAXPATHLEN + 1;
452 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
454 /* check for did 0, 1 and 2 */
455 if (did == 0 || vol == NULL) { /* 1 */
456 afp_errno = AFPERR_PARAM;
458 } else if (did == DIRDID_ROOT_PARENT) {
459 rootParent.d_vid = vol->v_vid;
460 return (&rootParent);
461 } else if (did == DIRDID_ROOT) {
465 /* Search the cache */
466 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
470 utf8 = utf8_encoding();
471 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
473 /* Create list for path elements, request 16 list elements for now*/
474 if ((pathlist = bstListCreateMin(16)) == NULL) { /* 4 */
475 LOG(log_error, logtype_afpd, "dirlookup(did: %u): OOM: %s", ntohl(did), strerror(errno));
479 /* Get it from the database */
481 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
482 afp_errno = AFPERR_NOOBJ;
488 /* construct path, copy already found uname to path element list*/
489 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
490 afp_errno = AFPERR_MISC;
495 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {%u, %s}", ntohl(did), ntohl(pdid), upath);
497 /* The stuff that follows is for building the full path to the directory */
499 /* work upwards until we reach volume root */
500 while (cnid != DIRDID_ROOT) {
501 /* construct path, copy already found uname to path element list*/
502 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
503 afp_errno = AFPERR_MISC;
509 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
510 afp_errno = AFPERR_NOOBJ;
516 if ((bstrListPush(pathlist, bfromcstr(vol->v_path))) != BSTR_OK) { /* 4 */
517 afp_errno = AFPERR_MISC;
522 if ((fullpath = bjoinInv(pathlist, bfromcstr("/"))) == NULL) { /* 4 */
523 afp_errno = AFPERR_MISC;
527 /* Finished building the fullpath */
529 /* stat it and check if it's a dir */
530 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
532 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
535 afp_errno = AFPERR_NOOBJ;
539 afp_errno = AFPERR_ACCESS;
543 afp_errno = AFPERR_MISC;
548 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
549 afp_errno = AFPERR_BADTYPE;
555 /* Get macname from unix name */
556 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
557 afp_errno = AFPERR_NOOBJ;
562 /* Create struct dir */
563 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
564 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
569 /* Add it to the cache only if it's a dir */
570 if (dircache_add(ret) != 0) { /* 7 */
575 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
576 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
580 bstrListDestroy(pathlist);
583 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
584 ntohl(did), AfpErr2name(afp_errno));
595 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
597 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
602 static u_int32_t did = 0;
603 static char cname[MAXPATHLEN];
604 static char lname[MAXPATHLEN];
605 ucs2_t u2_path[MAXPATHLEN];
606 ucs2_t u2_dename[MAXPATHLEN];
607 char *tmp, *savepath;
609 if (!(vol->v_flags & AFPVOL_CASEINSEN))
612 if (veto_file(ENUMVETO, path->u_name))
615 savepath = path->u_name;
617 /* very simple cache */
618 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
619 path->u_name = cname;
621 if (of_stat( path ) == 0 ) {
624 /* something changed, we cannot stat ... */
628 if (NULL == ( dp = opendir( "." )) ) {
629 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
634 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
635 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
636 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
638 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
640 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
641 if (NULL == check_dirent(vol, de->d_name))
644 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
647 if (strcasecmp_w( u2_path, u2_dename) == 0) {
649 strlcpy(cname, de->d_name, sizeof(cname));
650 path->u_name = cname;
652 if (of_stat( path ) == 0 ) {
653 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
654 strlcpy(lname, tmp, sizeof(lname));
667 /* invalidate cache */
670 path->u_name = savepath;
672 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
678 * @brief Construct struct dir
680 * Construct struct dir from parameters.
682 * @param m_name (r) directory name in UTF8-dec
683 * @param u_name (r) directory name in server side encoding
684 * @param vol (r) pointer to struct vol
685 * @param pdid (r) Parent CNID
686 * @param did (r) CNID
687 * @param fullpath (r) Full unix path to dir
689 * @returns pointer to new struct dir or NULL on error
691 * @note Most of the time mac name and unix name are the same.
693 struct dir *dir_new(const char *m_name,
695 const struct vol *vol,
702 dir = (struct dir *) calloc(1, sizeof( struct dir ));
706 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
711 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
714 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
715 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
716 dir->d_m_name_ucs2 = NULL;
719 if (m_name == u_name || !strcmp(m_name, u_name)) {
720 dir->d_u_name = dir->d_m_name;
722 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
723 bdestroy(dir->d_m_name);
730 dir->d_vid = vol->v_vid;
731 dir->d_fullpath = path;
736 * @brief Free a struct dir and all its members
738 * @param (rw) pointer to struct dir
740 void dir_free(struct dir *dir)
742 if (dir->d_u_name != dir->d_m_name) {
743 bdestroy(dir->d_u_name);
745 if (dir->d_m_name_ucs2)
746 free(dir->d_m_name_ucs2);
747 bdestroy(dir->d_m_name);
748 bdestroy(dir->d_fullpath);
753 * @brief Create struct dir from struct path
755 * Create a new struct dir from struct path. Then add it to the cache.
756 * The caller must have assured that the dir is not already in the cache,
758 * 1. Open adouble file, get CNID from it.
759 * 2. Search the database, hinting with the CNID from (1).
760 * 3. Build fullpath and create struct dir.
761 * 4. Add it to the cache.
763 * @param vol (r) pointer to struct vol
764 * @param dir (r) pointer to parrent directory
765 * @param path (rw) pointer to struct path with valid path->u_name
766 * @param len (r) strlen of path->u_name
768 * @returns Pointer to new struct dir or NULL on error.
770 * @note Function also assigns path->m_name from path->u_name.
772 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
775 struct dir *cdir = NULL;
778 struct adouble *adp = NULL;
786 if ((cdir = dircache_search_by_name(vol, dir->d_did, path->u_name, strlen(path->u_name))) != NULL) {
787 /* there's a stray entry in the dircache */
788 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
789 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
790 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
791 if (dir_remove(vol, cdir) != 0) {
797 /* get_id needs adp for reading CNID from adouble file */
798 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
799 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
803 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
809 ad_close_metadata(adp);
811 /* Get macname from unixname */
812 if (path->m_name == NULL) {
813 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
820 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
821 || (bconchar(fullpath, '/') != BSTR_OK)
822 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
823 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
828 /* Allocate and initialize struct dir */
829 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
834 if ((dircache_add(cdir)) != 0) { /* 4 */
835 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
841 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
842 cfrombstring(dir->d_u_name), path->u_name, err);
845 ad_close_metadata(adp);
846 if (!cdir && fullpath)
853 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): now cached",
854 ntohl(dir->d_did), cfrombstring(dir->d_u_name), path->u_name);
861 * @brief Remove a dir from a cache and free it and any ressources with it
863 * @param (r) pointer to struct vol
864 * @param (rw) pointer to struct dir
866 int dir_remove(const struct vol *vol, struct dir *dir)
871 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
875 if (movecwd(vol, vol->v_root) < 0) {
876 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
881 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
882 ntohl(dir->d_did), cfrombstring(dir->d_u_name));
884 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
891 * @brief Modify a struct dir, adust cache
893 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
894 * If given new_uname == new_mname, new_uname will point to new_mname.
896 * @param vol (r) pointer to struct vol
897 * @param dir (rw) pointer to struct dir
898 * @param pdid (r) new parent DID
899 * @param did (r) new DID
900 * @param new_mname (r) new mac-name
901 * @param new_uname (r) new unix-name
903 int dir_modify(const struct vol *vol,
907 const char *new_mname,
908 const char *new_uname,
909 bstring pdir_fullpath)
913 /* Remove it from the cache */
914 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
922 /* free uname if it's not the same as mname */
923 if (dir->d_m_name != dir->d_u_name)
924 bdestroy(dir->d_u_name);
926 if (new_uname == NULL)
927 new_uname = new_mname;
929 /* assign new name */
930 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
931 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
935 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
936 dir->d_u_name = dir->d_m_name;
938 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
939 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
946 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
948 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
950 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
954 if (dir->d_m_name_ucs2)
955 free(dir->d_m_name_ucs2);
956 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))
957 dir->d_m_name_ucs2 = NULL;
959 /* Re-add it to the cache */
960 if ((dircache_add(dir)) != 0) {
969 * @brief Resolve a catalog node name path
971 * If it's a filename:
972 * 1. compute unix name
973 * 2. stat the file, storing struct stat or errno in struct path
974 * 3. cwd (and curdir) is filename parent directory
975 * 4. return path with with filename
978 * 5. search the dircache
979 * 6. if not in the cache:
980 * 7. compute unix name
981 * 8. stat the dir, storing struct stat or errno in struct path
983 * 10. if chdir failed, return path with dirname
984 * cwd is dir parent directory
985 * if chdir succeeded, add to dircache
986 * return path with "" and "."
988 * 11. else in the cache:
989 * 12. stat the dir, storing struct stat or errno in struct path
991 * 14. if chdir failed
994 * if not last path part, return NULL
999 curdir: dir parent directory
1004 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1006 static char path[ MAXPATHLEN + 1];
1007 static struct path ret;
1017 LOG(log_debug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1020 afp_errno = AFPERR_NOOBJ;
1021 memset(&ret, 0, sizeof(ret));
1023 switch (ret.m_type = *data) { /* path type */
1026 len = (unsigned char) *data++;
1028 if (afp_version >= 30) {
1034 if (afp_version >= 30) {
1036 memcpy(&hint, data, sizeof(hint));
1038 data += sizeof(hint);
1040 memcpy(&len16, data, sizeof(len16));
1046 /* else it's an error */
1048 afp_errno = AFPERR_PARAM;
1051 *cpath += len + size;
1056 if (movecwd(vol, dir) < 0 ) {
1057 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1058 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1060 return path_from_dir(vol, dir, &ret);
1068 * 1. single 0 -> delimiter
1069 * 2. additional 0 -> chdir(..)
1071 * a) name is a file, build struct path from it etc., exit while loop
1072 * b) name is a dir, add it to the dircache, chdir to it, continue
1074 if (*data == 0) { /* case 1 or 2 */
1077 while (len > 0 && *data == 0) { /* case 2 */
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);
1091 /* case 3: copy name from packet buffer to ret.m_name and process it */
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 /* Get u_name from m_name */
1103 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) {
1104 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1108 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1110 /* Prevent access to our special folders like .AppleDouble */
1111 if (check_name(vol, ret.u_name)) {
1112 /* the name is illegal */
1113 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1114 afp_errno = AFPERR_PARAM;
1118 if (dir->d_did == DIRDID_ROOT_PARENT) {
1120 * Special case: CNID 1
1121 * root parent (did 1) has one child: the volume. Requests for did=1 with
1122 * some <name> must check against the volume name.
1124 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1130 * CNID != 1, eg. most of the times we take this way.
1131 * Now check if current path-part is a file or dir:
1132 * o if it's dir we have to step into it
1133 * o if it's a file we expect it to be the last part of the requested path
1134 * and thus call continue which should terminate the while loop because
1137 if (of_stat(&ret) != 0) {
1139 * ret.u_name doesn't exist, might be afp_createfile|dir
1140 * that means it should have been the last part
1143 /* it wasn't the last part, so we have a bogus path request */
1144 afp_errno = AFPERR_NOOBJ;
1148 * this will terminate clean in while (1) because len == 0,
1149 * probably afp_createfile|dir
1151 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1155 switch (ret.st.st_mode & S_IFMT) {
1157 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1159 /* it wasn't the last part, so we have a bogus path request */
1160 afp_errno = AFPERR_PARAM;
1163 continue; /* continues while loop */
1165 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1167 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1168 afp_errno = AFPERR_PARAM;
1171 continue; /* continues while loop */
1175 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1176 afp_errno = AFPERR_NODIR;
1180 /* Search the cache */
1181 int unamelen = strlen(ret.u_name);
1182 cdir = dircache_search_by_name(vol, dir->d_did, ret.u_name, unamelen);
1184 /* Not in cache, create one */
1185 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) {
1186 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1187 ntohl(dir->d_did), ret.u_name, getcwdpath());
1191 } /* if/else cnid==1 */
1193 /* Now chdir to the evaluated dir */
1194 if (movecwd( vol, cdir ) < 0 ) {
1195 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1196 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1198 return path_from_dir(vol, cdir, &ret);
1203 ret.m_name[0] = 0; /* so we later know last token was a dir */
1206 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1207 afp_errno = AFPERR_DID1;
1211 if (ret.m_name[0] == 0) {
1212 /* Last part was a dir */
1213 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1217 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1218 cfrombstring(dir->d_fullpath),
1219 cfrombstring(curdir->d_fullpath),
1226 * @brief chdir() to dir
1228 * @param vol (r) pointer to struct vol
1229 * @param dir (r) pointer to struct dir
1231 * @returns 0 on success, -1 on error with afp_errno set appropiately
1233 int movecwd(const struct vol *vol, struct dir *dir)
1240 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1241 cfrombstring(curdir->d_fullpath), getcwdpath());
1245 if (dir->d_did == DIRDID_ROOT_PARENT) {
1246 curdir = &rootParent;
1250 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1252 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1253 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1257 afp_errno = AFPERR_ACCESS;
1260 afp_errno = AFPERR_NOOBJ;
1270 * We can't use unix file's perm to support Apple's inherited protection modes.
1271 * If we aren't the file's owner we can't change its perms when moving it and smb
1272 * nfs,... don't even try.
1274 #define AFP_CHECK_ACCESS
1276 int check_access(char *path, int mode)
1278 #ifdef AFP_CHECK_ACCESS
1286 accessmode(p, &ma, curdir, NULL);
1287 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1289 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1295 /* --------------------- */
1296 int file_access(struct path *path, int mode)
1300 accessmode(path->u_name, &ma, curdir, &path->st);
1301 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1303 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1309 /* --------------------- */
1310 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1312 dir->offcnt = count;
1313 dir->ctime = st->st_ctime;
1314 dir->d_flags &= ~DIRF_CNID;
1318 /* ---------------------
1319 * is our cached also for reenumerate id?
1321 int dirreenumerate(struct dir *dir, struct stat *st)
1323 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1326 /* ------------------------------
1328 (name, dir) with curdir:name == dir, from afp_enumerate
1331 int getdirparams(const struct vol *vol,
1332 u_int16_t bitmap, struct path *s_path,
1334 char *buf, size_t *buflen )
1338 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1339 int bit = 0, isad = 0;
1345 struct stat *st = &s_path->st;
1346 char *upath = s_path->u_name;
1348 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1349 (1 << DIRPBIT_CDATE) |
1350 (1 << DIRPBIT_MDATE) |
1351 (1 << DIRPBIT_BDATE) |
1352 (1 << DIRPBIT_FINFO)))) {
1354 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1355 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1357 if (ad.ad_md->adf_flags & O_CREAT) {
1358 /* We just created it */
1359 ad_setname(&ad, s_path->m_name);
1374 while ( bitmap != 0 ) {
1375 while (( bitmap & 1 ) == 0 ) {
1383 ad_getattr(&ad, &ashort);
1384 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1385 ashort = htons(ATTRBIT_INVISIBLE);
1388 ashort |= htons(ATTRBIT_SHARED);
1389 memcpy( data, &ashort, sizeof( ashort ));
1390 data += sizeof( ashort );
1394 memcpy( data, &pdid, sizeof( pdid ));
1395 data += sizeof( pdid );
1398 case DIRPBIT_CDATE :
1399 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1400 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1401 memcpy( data, &aint, sizeof( aint ));
1402 data += sizeof( aint );
1405 case DIRPBIT_MDATE :
1406 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1407 memcpy( data, &aint, sizeof( aint ));
1408 data += sizeof( aint );
1411 case DIRPBIT_BDATE :
1412 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1413 aint = AD_DATE_START;
1414 memcpy( data, &aint, sizeof( aint ));
1415 data += sizeof( aint );
1418 case DIRPBIT_FINFO :
1420 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1421 } else { /* no appledouble */
1422 memset( data, 0, 32 );
1423 /* set default view -- this also gets done in ad_open() */
1424 ashort = htons(FINDERINFO_CLOSEDVIEW);
1425 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1427 /* dot files are by default visible */
1428 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1429 ashort = htons(FINDERINFO_INVISIBLE);
1430 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1436 case DIRPBIT_LNAME :
1437 if (dir->d_m_name) /* root of parent can have a null name */
1440 memset(data, 0, sizeof(u_int16_t));
1441 data += sizeof( u_int16_t );
1444 case DIRPBIT_SNAME :
1445 memset(data, 0, sizeof(u_int16_t));
1446 data += sizeof( u_int16_t );
1450 memcpy( data, &dir->d_did, sizeof( aint ));
1451 data += sizeof( aint );
1454 case DIRPBIT_OFFCNT :
1456 /* this needs to handle current directory access rights */
1457 if (diroffcnt(dir, st)) {
1458 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1460 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1461 setdiroffcnt(dir, st, ret);
1462 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1464 ashort = htons( ashort );
1465 memcpy( data, &ashort, sizeof( ashort ));
1466 data += sizeof( ashort );
1470 aint = htonl(st->st_uid);
1471 memcpy( data, &aint, sizeof( aint ));
1472 data += sizeof( aint );
1476 aint = htonl(st->st_gid);
1477 memcpy( data, &aint, sizeof( aint ));
1478 data += sizeof( aint );
1481 case DIRPBIT_ACCESS :
1482 accessmode( upath, &ma, dir , st);
1484 *data++ = ma.ma_user;
1485 *data++ = ma.ma_world;
1486 *data++ = ma.ma_group;
1487 *data++ = ma.ma_owner;
1490 /* Client has requested the ProDOS information block.
1491 Just pass back the same basic block for all
1492 directories. <shirsch@ibm.net> */
1493 case DIRPBIT_PDINFO :
1494 if (afp_version >= 30) { /* UTF8 name */
1495 utf8 = kTextEncodingUTF8;
1496 if (dir->d_m_name) /* root of parent can have a null name */
1499 memset(data, 0, sizeof(u_int16_t));
1500 data += sizeof( u_int16_t );
1502 memcpy(data, &aint, sizeof( aint ));
1503 data += sizeof( aint );
1505 else { /* ProDOS Info Block */
1508 ashort = htons( 0x0200 );
1509 memcpy( data, &ashort, sizeof( ashort ));
1510 data += sizeof( ashort );
1511 memset( data, 0, sizeof( ashort ));
1512 data += sizeof( ashort );
1516 case DIRPBIT_UNIXPR :
1517 aint = htonl(st->st_uid);
1518 memcpy( data, &aint, sizeof( aint ));
1519 data += sizeof( aint );
1520 aint = htonl(st->st_gid);
1521 memcpy( data, &aint, sizeof( aint ));
1522 data += sizeof( aint );
1525 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1526 memcpy( data, &aint, sizeof( aint ));
1527 data += sizeof( aint );
1529 accessmode( upath, &ma, dir , st);
1531 *data++ = ma.ma_user;
1532 *data++ = ma.ma_world;
1533 *data++ = ma.ma_group;
1534 *data++ = ma.ma_owner;
1539 ad_close_metadata( &ad );
1541 return( AFPERR_BITMAP );
1547 ashort = htons( data - buf );
1548 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1549 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1551 if ( utf_nameoff ) {
1552 ashort = htons( data - buf );
1553 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1554 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1557 ad_close_metadata( &ad );
1559 *buflen = data - buf;
1563 /* ----------------------------- */
1564 int path_error(struct path *path, int error)
1566 /* - a dir with access error
1567 * - no error it's a file
1570 if (path_isadir(path))
1572 if (path->st_valid && path->st_errno)
1574 return AFPERR_BADTYPE ;
1577 /* ----------------------------- */
1578 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1583 u_int16_t vid, bitmap;
1589 memcpy( &vid, ibuf, sizeof( vid ));
1590 ibuf += sizeof( vid );
1592 if (NULL == ( vol = getvolbyvid( vid )) ) {
1593 return( AFPERR_PARAM );
1596 if (vol->v_flags & AFPVOL_RO)
1597 return AFPERR_VLOCK;
1599 memcpy( &did, ibuf, sizeof( did ));
1600 ibuf += sizeof( int );
1602 if (NULL == ( dir = dirlookup( vol, did )) ) {
1606 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1607 bitmap = ntohs( bitmap );
1608 ibuf += sizeof( bitmap );
1610 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1611 return get_afp_errno(AFPERR_NOOBJ);
1614 if ( *path->m_name != '\0' ) {
1615 rc = path_error(path, AFPERR_NOOBJ);
1616 /* maybe we are trying to set perms back */
1617 if (rc != AFPERR_ACCESS)
1622 * If ibuf is odd, make it even.
1624 if ((u_long)ibuf & 1 ) {
1628 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1629 setvoltime(obj, vol );
1635 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1637 * assume path == '\0' eg. it's a directory in canonical form
1639 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1651 u_int16_t ashort, bshort, oshort;
1653 int change_mdate = 0;
1654 int change_parent_mdate = 0;
1656 u_int16_t bitmap = d_bitmap;
1657 u_char finder_buf[32];
1660 u_int16_t upriv_bit = 0;
1663 upath = path->u_name;
1665 while ( bitmap != 0 ) {
1666 while (( bitmap & 1 ) == 0 ) {
1674 memcpy( &ashort, buf, sizeof( ashort ));
1675 buf += sizeof( ashort );
1677 case DIRPBIT_CDATE :
1679 memcpy(&cdate, buf, sizeof(cdate));
1680 buf += sizeof( cdate );
1682 case DIRPBIT_MDATE :
1683 memcpy(&newdate, buf, sizeof(newdate));
1684 buf += sizeof( newdate );
1686 case DIRPBIT_BDATE :
1688 memcpy(&bdate, buf, sizeof(bdate));
1689 buf += sizeof( bdate );
1691 case DIRPBIT_FINFO :
1693 memcpy( finder_buf, buf, 32 );
1696 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1697 change_parent_mdate = 1;
1698 memcpy( &owner, buf, sizeof(owner));
1699 buf += sizeof( owner );
1702 change_parent_mdate = 1;
1703 memcpy( &group, buf, sizeof( group ));
1704 buf += sizeof( group );
1706 case DIRPBIT_ACCESS :
1708 change_parent_mdate = 1;
1709 ma.ma_user = *buf++;
1710 ma.ma_world = *buf++;
1711 ma.ma_group = *buf++;
1712 ma.ma_owner = *buf++;
1713 mpriv = mtoumode( &ma ) | vol->v_dperm;
1714 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1715 err = set_dir_errors(path, "setdirmode", errno);
1719 /* Ignore what the client thinks we should do to the
1720 ProDOS information block. Skip over the data and
1721 report nothing amiss. <shirsch@ibm.net> */
1722 case DIRPBIT_PDINFO :
1723 if (afp_version < 30) {
1727 err = AFPERR_BITMAP;
1731 case DIRPBIT_UNIXPR :
1732 if (vol_unix_priv(vol)) {
1733 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1734 buf += sizeof( owner );
1735 memcpy( &group, buf, sizeof( group ));
1736 buf += sizeof( group );
1739 change_parent_mdate = 1;
1740 memcpy( &upriv, buf, sizeof( upriv ));
1741 buf += sizeof( upriv );
1742 upriv = ntohl (upriv) | vol->v_dperm;
1743 if (dir_rx_set(upriv)) {
1744 /* maybe we are trying to set perms back */
1745 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1747 err = set_dir_errors(path, "setdirunixmode", errno);
1758 err = AFPERR_BITMAP;
1766 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1768 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1770 * Check to see what we're trying to set. If it's anything
1771 * but ACCESS, UID, or GID, give an error. If it's any of those
1772 * three, we don't need the ad to be open, so just continue.
1774 * note: we also don't need to worry about mdate. also, be quiet
1775 * if we're using the noadouble option.
1777 if (!vol_noadouble(vol) && (d_bitmap &
1778 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1779 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1780 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1781 return AFPERR_ACCESS;
1787 * Check to see if a create was necessary. If it was, we'll want
1788 * to set our name, etc.
1790 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1791 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1797 while ( bitmap != 0 ) {
1798 while (( bitmap & 1 ) == 0 ) {
1806 ad_getattr(&ad, &bshort);
1808 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1809 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1813 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1814 change_parent_mdate = 1;
1815 ad_setattr(&ad, bshort);
1818 case DIRPBIT_CDATE :
1820 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1823 case DIRPBIT_MDATE :
1825 case DIRPBIT_BDATE :
1827 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1830 case DIRPBIT_FINFO :
1832 /* Fixes #2802236 */
1833 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1834 *fflags &= htons(~FINDERINFO_ISHARED);
1836 if ( dir->d_did == DIRDID_ROOT ) {
1838 * Alright, we admit it, this is *really* sick!
1839 * The 4 bytes that we don't copy, when we're dealing
1840 * with the root of a volume, are the directory's
1841 * location information. This eliminates that annoying
1842 * behavior one sees when mounting above another mount
1845 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1846 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1848 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1852 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1853 if ( (dir->d_did == DIRDID_ROOT) &&
1854 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1855 err = set_dir_errors(path, "setdeskowner", errno);
1856 if (isad && err == AFPERR_PARAM) {
1857 err = AFP_OK; /* ???*/
1860 goto setdirparam_done;
1863 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1864 err = set_dir_errors(path, "setdirowner", errno);
1865 goto setdirparam_done;
1869 if (dir->d_did == DIRDID_ROOT)
1870 setdeskowner( -1, ntohl(group) );
1871 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1872 err = set_dir_errors(path, "setdirowner", errno);
1873 goto setdirparam_done;
1876 case DIRPBIT_ACCESS :
1877 if (dir->d_did == DIRDID_ROOT) {
1879 if (!dir_rx_set(mpriv)) {
1880 /* we can't remove read and search for owner on volume root */
1881 err = AFPERR_ACCESS;
1882 goto setdirparam_done;
1886 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1887 err = set_dir_errors(path, "setdirmode", errno);
1888 goto setdirparam_done;
1891 case DIRPBIT_PDINFO :
1892 if (afp_version >= 30) {
1893 err = AFPERR_BITMAP;
1894 goto setdirparam_done;
1897 case DIRPBIT_UNIXPR :
1898 if (vol_unix_priv(vol)) {
1899 if (dir->d_did == DIRDID_ROOT) {
1900 if (!dir_rx_set(upriv)) {
1901 /* we can't remove read and search for owner on volume root */
1902 err = AFPERR_ACCESS;
1903 goto setdirparam_done;
1905 setdeskowner( -1, ntohl(group) );
1906 setdeskmode( upriv );
1908 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1909 err = set_dir_errors(path, "setdirowner", errno);
1910 goto setdirparam_done;
1913 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1914 err = set_dir_errors(path, "setdirunixmode", errno);
1915 goto setdirparam_done;
1919 err = AFPERR_BITMAP;
1920 goto setdirparam_done;
1924 err = AFPERR_BITMAP;
1925 goto setdirparam_done;
1934 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1935 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1939 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1940 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1945 if (path->st_valid && !path->st_errno) {
1946 struct stat *st = &path->st;
1948 if (dir && dir->d_pdid) {
1949 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1953 ad_close_metadata( &ad);
1957 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1958 && gettimeofday(&tv, NULL) == 0) {
1959 if (!movecwd(vol, dir->d_parent)) {
1960 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1961 /* be careful with bitmap because now dir is null */
1962 bitmap = 1<<DIRPBIT_MDATE;
1963 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1964 /* should we reset curdir ?*/
1971 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1985 memcpy( &vid, ibuf, sizeof( vid ));
1986 ibuf += sizeof( vid );
1987 if (NULL == (vol = getvolbyvid( vid )) ) {
1988 return( AFPERR_PARAM );
1991 memcpy( &did, ibuf, sizeof( did ));
1992 ibuf += sizeof( did );
1996 * if it's CNID 2 our only choice to meet the specs is call sync.
1997 * For any other CNID just sync that dir. To my knowledge the
1998 * intended use of FPSyncDir is to sync the volume so all we're
1999 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2002 if ( ntohl(did) == 2 ) {
2005 if (NULL == ( dir = dirlookup( vol, did )) ) {
2006 return afp_errno; /* was AFPERR_NOOBJ */
2009 if (movecwd( vol, dir ) < 0 )
2010 return ( AFPERR_NOOBJ );
2013 * Assuming only OSens that have dirfd also may require fsyncing directories
2014 * in order to flush metadata e.g. Linux.
2018 if (NULL == ( dp = opendir( "." )) ) {
2021 return( AFPERR_NOOBJ );
2023 return( AFPERR_ACCESS );
2025 return( AFPERR_PARAM );
2029 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2032 if ( fsync ( dfd ) < 0 )
2033 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2034 dir->d_u_name, strerror(errno) );
2035 closedir(dp); /* closes dfd too */
2038 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2041 return( AFPERR_NOOBJ );
2043 return( AFPERR_ACCESS );
2045 return( AFPERR_PARAM );
2049 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2050 vol->ad_path(".", ADFLAGS_DIR) );
2052 if ( fsync(dfd) < 0 )
2053 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2054 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2061 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2067 struct path *s_path;
2075 memcpy( &vid, ibuf, sizeof( vid ));
2076 ibuf += sizeof( vid );
2077 if (NULL == ( vol = getvolbyvid( vid )) ) {
2078 return( AFPERR_PARAM );
2081 if (vol->v_flags & AFPVOL_RO)
2082 return AFPERR_VLOCK;
2084 memcpy( &did, ibuf, sizeof( did ));
2085 ibuf += sizeof( did );
2086 if (NULL == ( dir = dirlookup( vol, did )) ) {
2087 return afp_errno; /* was AFPERR_NOOBJ */
2089 /* for concurrent access we need to be sure we are not in the
2090 * folder we want to create...
2094 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2095 return get_afp_errno(AFPERR_PARAM);
2097 /* cname was able to move curdir to it! */
2098 if (*s_path->m_name == '\0')
2099 return AFPERR_EXIST;
2101 upath = s_path->u_name;
2103 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2107 if (of_stat(s_path) < 0) {
2113 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2117 if ( movecwd( vol, dir ) < 0 ) {
2118 return( AFPERR_PARAM );
2121 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2122 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2123 if (vol_noadouble(vol))
2124 goto createdir_done;
2125 return( AFPERR_ACCESS );
2127 ad_setname(&ad, s_path->m_name);
2128 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2131 ad_close_metadata( &ad);
2134 #ifdef HAVE_NFSv4_ACLS
2135 /* FIXME: are we really inside the created dir? */
2136 addir_inherit_acl(vol);
2139 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2140 *rbuflen = sizeof( u_int32_t );
2141 setvoltime(obj, vol );
2146 * dst new unix filename (not a pathname)
2147 * newname new mac name
2150 int renamedir(const struct vol *vol, char *src, char *dst,
2152 struct dir *newparent,
2158 /* existence check moved to afp_moveandrename */
2159 if ( unix_rename( src, dst ) < 0 ) {
2162 return( AFPERR_NOOBJ );
2164 return( AFPERR_ACCESS );
2166 return AFPERR_VLOCK;
2168 /* tried to move directory into a subdirectory of itself */
2169 return AFPERR_CANTMOVE;
2171 /* this needs to copy and delete. bleah. that means we have
2172 * to deal with entire directory hierarchies. */
2173 if ((err = copydir(vol, src, dst)) < 0) {
2177 if ((err = deletedir(src)) < 0)
2181 return( AFPERR_PARAM );
2185 vol->vfs->vfs_renamedir(vol, src, dst);
2187 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2189 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2190 ad_setname(&ad, newname);
2192 ad_close_metadata( &ad);
2195 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2196 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2203 /* delete an empty directory */
2204 int deletecurdir(struct vol *vol)
2214 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2215 return( AFPERR_ACCESS );
2220 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2221 /* we never want to create a resource fork here, we are going to delete it */
2222 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2224 ad_getattr(&ad, &ashort);
2225 ad_close( &ad, ADFLAGS_HF );
2226 if ((ashort & htons(ATTRBIT_NODELETE))) {
2227 return AFPERR_OLOCK;
2230 err = vol->vfs->vfs_deletecurdir(vol);
2235 /* now get rid of dangling symlinks */
2236 if ((dp = opendir("."))) {
2237 while ((de = readdir(dp))) {
2238 /* skip this and previous directory */
2239 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2242 /* bail if it's not a symlink */
2243 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2245 return AFPERR_DIRNEMPT;
2248 if ((err = netatalk_unlink(de->d_name))) {
2255 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2260 err = netatalk_rmdir_all_errors(cfrombstring(fdir->d_u_name));
2261 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2262 cnid_delete(vol->v_cdb, fdir->d_did);
2263 dir_remove( vol, fdir );
2267 /* inode is used as key for cnid.
2268 * Close the descriptor only after cnid_delete
2276 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2286 sfunc = (unsigned char) *ibuf++;
2290 if (sfunc >= 3 && sfunc <= 6) {
2291 if (afp_version < 30) {
2292 return( AFPERR_PARAM );
2299 case 3 :/* unicode */
2300 memcpy( &id, ibuf, sizeof( id ));
2303 if (( pw = getpwuid( id )) == NULL ) {
2304 return( AFPERR_NOITEM );
2306 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2307 pw->pw_name, -1, &name);
2314 case 4 : /* unicode */
2315 memcpy( &id, ibuf, sizeof( id ));
2318 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2319 return( AFPERR_NOITEM );
2321 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2322 gr->gr_name, -1, &name);
2328 #ifdef HAVE_NFSv4_ACLS
2329 case 5 : /* UUID -> username */
2330 case 6 : /* UUID -> groupname */
2331 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2332 return AFPERR_PARAM;
2333 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2335 len = getnamefromuuid( ibuf, &name, &type);
2336 if (len != 0) /* its a error code, not len */
2337 return AFPERR_NOITEM;
2338 if (type == UUID_USER) {
2339 if (( pw = getpwnam( name )) == NULL )
2340 return( AFPERR_NOITEM );
2341 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2342 id = htonl(UUID_USER);
2343 memcpy( rbuf, &id, sizeof( id ));
2344 id = htonl( pw->pw_uid);
2345 rbuf += sizeof( id );
2346 memcpy( rbuf, &id, sizeof( id ));
2347 rbuf += sizeof( id );
2348 *rbuflen = 2 * sizeof( id );
2349 } else { /* type == UUID_GROUP */
2350 if (( gr = getgrnam( name )) == NULL )
2351 return( AFPERR_NOITEM );
2352 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2353 id = htonl(UUID_GROUP);
2354 memcpy( rbuf, &id, sizeof( id ));
2355 rbuf += sizeof( id );
2356 id = htonl( gr->gr_gid);
2357 memcpy( rbuf, &id, sizeof( id ));
2358 rbuf += sizeof( id );
2359 *rbuflen = 2 * sizeof( id );
2364 return( AFPERR_PARAM );
2368 len = strlen( name );
2371 u_int16_t tp = htons(len);
2372 memcpy(rbuf, &tp, sizeof(tp));
2381 memcpy( rbuf, name, len );
2389 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2398 sfunc = (unsigned char) *ibuf++;
2400 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2403 case 2 : /* unicode */
2404 if (afp_version < 30) {
2405 return( AFPERR_PARAM );
2407 memcpy(&ulen, ibuf, sizeof(ulen));
2410 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2414 len = (unsigned char) *ibuf++;
2416 #ifdef HAVE_NFSv4_ACLS
2417 case 5 : /* username -> UUID */
2418 case 6 : /* groupname -> UUID */
2419 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2420 return AFPERR_PARAM;
2421 memcpy(&ulen, ibuf, sizeof(ulen));
2427 return( AFPERR_PARAM );
2433 return AFPERR_PARAM;
2436 case 1 : /* unicode */
2438 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2439 return( AFPERR_NOITEM );
2443 memcpy( rbuf, &id, sizeof( id ));
2444 *rbuflen = sizeof( id );
2447 case 2 : /* unicode */
2449 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2450 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2451 return( AFPERR_NOITEM );
2454 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2456 memcpy( rbuf, &id, sizeof( id ));
2457 *rbuflen = sizeof( id );
2459 #ifdef HAVE_NFSv4_ACLS
2460 case 5 : /* username -> UUID */
2461 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2462 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2463 return AFPERR_NOITEM;
2464 *rbuflen = UUID_BINSIZE;
2466 case 6 : /* groupname -> UUID */
2467 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2468 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2469 return AFPERR_NOITEM;
2470 *rbuflen = UUID_BINSIZE;
2478 /* ------------------------------------
2479 variable DID support
2481 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2492 /* do nothing as dids are static for the life of the process. */
2496 memcpy(&vid, ibuf, sizeof( vid ));
2497 ibuf += sizeof( vid );
2498 if (( vol = getvolbyvid( vid )) == NULL ) {
2499 return( AFPERR_PARAM );
2502 memcpy( &did, ibuf, sizeof( did ));
2503 ibuf += sizeof( did );
2504 if (( dir = dirlookup( vol, did )) == NULL ) {
2505 return( AFPERR_PARAM );
2508 /* dir_remove -- deletedid */
2514 /* did creation gets done automatically
2515 * there's a pb again with case but move it to cname
2517 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2520 struct dir *parentdir;
2528 memcpy(&vid, ibuf, sizeof(vid));
2529 ibuf += sizeof( vid );
2531 if (NULL == ( vol = getvolbyvid( vid )) ) {
2532 return( AFPERR_PARAM );
2535 memcpy(&did, ibuf, sizeof(did));
2536 ibuf += sizeof(did);
2538 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2542 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2543 return get_afp_errno(AFPERR_PARAM);
2546 if ( *path->m_name != '\0' ) {
2547 return path_error(path, AFPERR_NOOBJ);
2550 if ( !path->st_valid && of_stat(path ) < 0 ) {
2551 return( AFPERR_NOOBJ );
2553 if ( path->st_errno ) {
2554 return( AFPERR_NOOBJ );
2557 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2558 *rbuflen = sizeof(curdir->d_did);