2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
16 #include <sys/param.h>
22 #include <atalk/adouble.h>
23 #include <atalk/vfs.h>
24 #include <atalk/afp.h>
25 #include <atalk/util.h>
26 #include <atalk/cnid.h>
27 #include <atalk/logger.h>
28 #include <atalk/uuid.h>
29 #include <atalk/unix.h>
30 #include <atalk/bstrlib.h>
31 #include <atalk/bstradd.h>
32 #include <atalk/errchk.h>
33 #include <atalk/globals.h>
34 #include <atalk/fce_api.h>
36 #include "directory.h"
48 * FIXMEs, loose ends after the dircache rewrite:
49 * o merge dircache_search_by_name and dir_add ??
50 * o case-insensitivity is gone from cname
54 /*******************************************************************************************
56 ******************************************************************************************/
59 /* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
60 struct dir rootParent = {
61 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
62 NULL, 0, 0, /* qidx_node, ctime, d_flags */
63 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
65 struct dir *curdir = &rootParent;
66 struct path Cur_Path = {
71 NULL,/* struct dir * */
72 0, /* stat is not set */
78 * dir_remove queues struct dirs to be freed here. We can't just delete them immeidately
79 * eg in dircache_search_by_id, because a caller somewhere up the stack might be
82 * - we mark it as invalid by setting d_did to CNID_INVALID (ie 0)
83 * - queue it in "invalid_dircache_entries" queue
84 * - which is finally freed at the end of every AFP func in afp_dsi.c.
86 q_t *invalid_dircache_entries;
89 /*******************************************************************************************
91 ******************************************************************************************/
94 /* -------------------------
95 appledouble mkdir afp error code.
97 static int netatalk_mkdir(const struct vol *vol, const char *name)
102 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
103 if (lstat(".", &st) < 0)
105 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
106 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
107 name, st.st_mode, vol->v_umask);
109 ret = mkdir(name, mode);
111 ret = ad_mkdir(name, DIRBITS | 0777);
117 return( AFPERR_NOOBJ );
119 return( AFPERR_VLOCK );
122 return( AFPERR_ACCESS );
124 return( AFPERR_EXIST );
127 return( AFPERR_DFULL );
129 return( AFPERR_PARAM );
135 /* ------------------- */
136 static int deletedir(int dirfd, char *dir)
138 char path[MAXPATHLEN + 1];
146 if ((len = strlen(dir)) +2 > sizeof(path))
150 if ((dp = opendirat(dirfd, dir)) == NULL)
156 remain = sizeof(path) -len -1;
157 while ((de = readdir(dp)) && err == AFP_OK) {
158 /* skip this and previous directory */
159 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
162 if (strlen(de->d_name) > remain) {
166 strcpy(path + len, de->d_name);
167 if (lstatat(dirfd, path, &st)) {
170 if (S_ISDIR(st.st_mode)) {
171 err = deletedir(dirfd, path);
173 err = netatalk_unlinkat(dirfd, path);
178 /* okay. the directory is empty. delete it. note: we already got rid
181 err = netatalk_rmdir(dirfd, dir);
186 /* do a recursive copy. */
187 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
189 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
198 /* doesn't exist or the path is too long. */
199 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
200 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
201 ((dp = opendirat(dirfd, src)) == NULL))
204 /* try to create the destination directory */
205 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
210 /* set things up to copy */
214 srem = sizeof(spath) - slen -1;
219 drem = sizeof(dpath) - dlen -1;
222 while ((de = readdir(dp))) {
223 /* skip this and previous directory */
224 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
227 if (strlen(de->d_name) > srem) {
231 strcpy(spath + slen, de->d_name);
233 if (lstatat(dirfd, spath, &st) == 0) {
234 if (strlen(de->d_name) > drem) {
238 strcpy(dpath + dlen, de->d_name);
240 if (S_ISDIR(st.st_mode)) {
241 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
243 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
247 /* keep the same time stamp. */
248 ut.actime = ut.modtime = st.st_mtime;
254 /* keep the same time stamp. */
255 if (lstatat(dirfd, src, &st) == 0) {
256 ut.actime = ut.modtime = st.st_mtime;
265 /* ---------------------
266 * is our cached offspring count valid?
268 static int diroffcnt(struct dir *dir, struct stat *st)
270 return st->st_ctime == dir->d_ctime;
273 /* --------------------- */
274 static int invisible_dots(const struct vol *vol, const char *name)
276 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
279 /* ------------------ */
280 static int set_dir_errors(struct path *path, const char *where, int err)
285 return AFPERR_ACCESS;
289 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
294 * @brief Convert name in client encoding to server encoding
296 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
297 * This only gets called from cname().
299 * @returns 0 on success, -1 on error
301 * @note If the passed ret->m_name is mangled, we'll demangle it
303 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
305 static char temp[ MAXPATHLEN + 1];
309 if (afp_version >= 30) {
311 if (dir->d_did == DIRDID_ROOT_PARENT) {
313 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
314 * So we compare it with the longname from the current volume and if they match
315 * we overwrite the requested path with the utf8 volume name so that the following
318 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
319 if (strcasecmp(ret->m_name, temp) == 0)
320 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
323 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
324 afp_errno = AFPERR_PARAM;
327 strcpy(ret->m_name, temp);
331 /* check for OS X mangled filename :( */
332 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
333 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
334 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
336 if (t != ret->m_name) {
338 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
339 * flags weren't the same
341 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
342 /* at last got our view of mac name */
343 strcpy(ret->m_name, t);
346 } /* afp_version >= 30 */
348 /* If we haven't got it by now, get it */
349 if (ret->u_name == NULL) {
350 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
351 afp_errno = AFPERR_PARAM;
360 * @brief Build struct path from struct dir
362 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
363 * 1. move cwd into parent dir (we're often already there, but not always)
364 * 2. set struct path to the dirname
366 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
367 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
368 * 4. indicate there's no dir for this path
371 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
373 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
379 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
382 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
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, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
392 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
393 cfrombstr(dir->d_fullpath),
394 cfrombstr(curdir->d_fullpath),
400 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
403 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
404 if (dir->d_m_name == dir->d_u_name) {
405 ret->u_name = ret->m_name;
407 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
408 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
411 ret->d_dir = NULL; /* 4 */
412 dir_remove(vol, dir); /* 5 */
419 /* DEADC0DE: never get here */
424 /*********************************************************************************************
426 ********************************************************************************************/
428 int get_afp_errno(const int param)
430 if (afp_errno != AFPERR_DID1)
436 * Resolve struct dir for an absolute path
438 * Given a path like "/Volumes/volume/dir/subdir" in a volume "/Volumes/volume" return
439 * a pointer to struct dir of "subdir".
440 * 1. Remove volue path from absolute path
442 * 3. loop through all elements of the remaining path from 1.
443 * 4. we only allow dirs
445 * 6. if not found in the dircache query the CNID database for the DID
446 * 7. and use dirlookup to resolve the DID to a it's struct dir *
448 * @param vol (r) volume the path is in, must be known
449 * @param path (r) absoule path
451 * @returns pointer to struct dir or NULL on error
453 struct dir *dirlookup_bypath(const struct vol *vol, const char *path)
457 struct dir *dir = NULL;
459 bstring rpath = NULL;
460 bstring statpath = NULL;
461 struct bstrList *l = NULL;
467 LOG(log_debug, logtype_afpd, "dirlookup_bypath(\"%s\")", path);
469 if (strcmp(vol->v_path, path) == 0)
472 EC_NULL(rpath = rel_path_in_vol(path, vol->v_path)); /* 1. */
474 LOG(log_debug, logtype_afpd, "dirlookup_bypath: rpath: \"%s\"", cfrombstr(rpath));
476 EC_NULL(statpath = bfromcstr(vol->v_path)); /* 2. */
478 l = bsplit(rpath, '/');
479 for (int i = 0; i < l->qty ; i++) { /* 3. */
481 EC_ZERO(bcatcstr(statpath, "/"));
482 EC_ZERO(bconcat(statpath, l->entry[i]));
484 LOG(log_debug, logtype_afpd, "dirlookup_bypath: statpath: \"%s\"", cfrombstr(statpath));
486 EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
487 "lstat(rpath: %s, elem: %s): %s: %s",
488 cfrombstr(rpath), cfrombstr(l->entry[i]),
489 cfrombstr(statpath), strerror(errno));
491 if (!(S_ISDIR(st.st_mode))) /* 4. */
494 if ((dir = dircache_search_by_name(vol, /* 5. */
496 cfrombstr(l->entry[i]),
497 blength(l->entry[i]))) == NULL) {
499 if ((cnid = cnid_add(vol->v_cdb, /* 6. */
502 cfrombstr(l->entry[i]),
503 blength(l->entry[i]),
507 if ((dir = dirlookup(vol, cnid)) == NULL) /* 7. */
519 LOG(log_debug, logtype_afpd, "dirlookup_bypath: result: \"%s\"",
520 cfrombstr(dir->d_fullpath));
526 * @brief Resolve a DID
528 * Resolve a DID, allocate a struct dir for it
529 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
530 * 2a. Check if the DID is in the cache.
531 * 2b. Check if it's really a dir because we cache files too.
532 * 3. If it's not in the cache resolve it via the database.
533 * 4. Build complete server-side path to the dir.
534 * 5. Check if it exists and is a directory.
535 * 6. Create the struct dir and populate it.
536 * 7. Add it to the cache.
538 * @param vol (r) pointer to struct vol
539 * @param did (r) DID to resolve
541 * @returns pointer to struct dir
543 struct dir *dirlookup(const struct vol *vol, cnid_t did)
545 static char buffer[12 + MAXPATHLEN + 1];
547 struct dir *ret = NULL, *pdir;
548 bstring fullpath = NULL;
549 char *upath = NULL, *mpath;
552 int buflen = 12 + MAXPATHLEN + 1;
556 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): START", ntohl(did));
558 /* check for did 0, 1 and 2 */
559 if (did == 0 || vol == NULL) { /* 1 */
560 afp_errno = AFPERR_PARAM;
563 } else if (did == DIRDID_ROOT_PARENT) {
564 rootParent.d_vid = vol->v_vid;
567 } else if (did == DIRDID_ROOT) {
572 /* Search the cache */
573 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
574 if (ret->d_flags & DIRF_ISFILE) { /* 2b */
575 afp_errno = AFPERR_BADTYPE;
579 if (lstat(cfrombstr(ret->d_fullpath), &st) != 0) {
580 LOG(log_debug, logtype_afpd, "dirlookup(did: %u, path: \"%s\"): lstat: %s",
581 ntohl(did), cfrombstr(ret->d_fullpath), strerror(errno));
585 /* It's not there anymore, so remove it */
586 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): calling dir_remove", ntohl(did));
587 dir_remove(vol, ret);
588 afp_errno = AFPERR_NOOBJ;
603 utf8 = utf8_encoding();
604 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
606 /* Get it from the database */
608 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): querying CNID database", ntohl(did));
609 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL) {
610 afp_errno = AFPERR_NOOBJ;
614 if ((upath = strdup(upath)) == NULL) { /* 3 */
615 afp_errno = AFPERR_NOOBJ;
622 * Recurse up the tree, terminates in dirlookup when either
623 * - DIRDID_ROOT is hit
624 * - a cached entry is found
626 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): recursion for did: %u",
627 ntohl(did), ntohl(pdid));
628 if ((pdir = dirlookup(vol, pdid)) == NULL) {
633 /* build the fullpath */
634 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
635 || bconchar(fullpath, '/') != BSTR_OK
636 || bcatcstr(fullpath, upath) != BSTR_OK) {
641 /* stat it and check if it's a dir */
642 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): stating \"%s\"",
643 ntohl(did), cfrombstr(fullpath));
645 if (lstat(cfrombstr(fullpath), &st) != 0) { /* 5a */
648 afp_errno = AFPERR_NOOBJ;
652 afp_errno = AFPERR_ACCESS;
656 afp_errno = AFPERR_MISC;
661 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
662 afp_errno = AFPERR_BADTYPE;
668 /* Get macname from unix name */
669 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
670 afp_errno = AFPERR_NOOBJ;
675 /* Create struct dir */
676 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath, &st)) == NULL) { /* 6 */
677 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
682 /* Add it to the cache only if it's a dir */
683 if (dircache_add(vol, ret) != 0) { /* 7 */
689 if (upath) free(upath);
691 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
692 ntohl(did), AfpErr2name(afp_errno));
701 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): RESULT: pdid: %u, path: \"%s\"",
702 ntohl(ret->d_did), ntohl(ret->d_pdid), cfrombstr(ret->d_fullpath));
707 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
709 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
714 static uint32_t did = 0;
715 static char cname[MAXPATHLEN];
716 static char lname[MAXPATHLEN];
717 ucs2_t u2_path[MAXPATHLEN];
718 ucs2_t u2_dename[MAXPATHLEN];
719 char *tmp, *savepath;
721 if (!(vol->v_flags & AFPVOL_CASEINSEN))
724 if (veto_file(ENUMVETO, path->u_name))
727 savepath = path->u_name;
729 /* very simple cache */
730 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
731 path->u_name = cname;
733 if (of_stat( path ) == 0 ) {
736 /* something changed, we cannot stat ... */
740 if (NULL == ( dp = opendir( "." )) ) {
741 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
746 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
747 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
748 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
750 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
752 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
753 if (NULL == check_dirent(vol, de->d_name))
756 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
759 if (strcasecmp_w( u2_path, u2_dename) == 0) {
761 strlcpy(cname, de->d_name, sizeof(cname));
762 path->u_name = cname;
764 if (of_stat( path ) == 0 ) {
765 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
766 strlcpy(lname, tmp, sizeof(lname));
779 /* invalidate cache */
782 path->u_name = savepath;
784 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
790 * @brief Construct struct dir
792 * Construct struct dir from parameters.
794 * @param m_name (r) directory name in UTF8-dec
795 * @param u_name (r) directory name in server side encoding
796 * @param vol (r) pointer to struct vol
797 * @param pdid (r) Parent CNID
798 * @param did (r) CNID
799 * @param path (r) Full unix path to object
800 * @param st (r) struct stat of object
802 * @returns pointer to new struct dir or NULL on error
804 * @note Most of the time mac name and unix name are the same.
806 struct dir *dir_new(const char *m_name,
808 const struct vol *vol,
816 dir = (struct dir *) calloc(1, sizeof( struct dir ));
820 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
825 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
828 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
829 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
830 dir->d_m_name_ucs2 = NULL;
833 if (m_name == u_name || !strcmp(m_name, u_name)) {
834 dir->d_u_name = dir->d_m_name;
836 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
837 bdestroy(dir->d_m_name);
844 dir->d_vid = vol->v_vid;
845 dir->d_fullpath = path;
846 dir->dcache_ctime = st->st_ctime;
847 dir->dcache_ino = st->st_ino;
848 if (!S_ISDIR(st->st_mode))
849 dir->d_flags = DIRF_ISFILE;
850 dir->d_rights_cache = 0xffffffff;
855 * @brief Free a struct dir and all its members
857 * @param (rw) pointer to struct dir
859 void dir_free(struct dir *dir)
861 if (dir->d_u_name != dir->d_m_name) {
862 bdestroy(dir->d_u_name);
864 if (dir->d_m_name_ucs2)
865 free(dir->d_m_name_ucs2);
866 bdestroy(dir->d_m_name);
867 bdestroy(dir->d_fullpath);
872 * @brief Create struct dir from struct path
874 * Create a new struct dir from struct path. Then add it to the cache.
876 * 1. Open adouble file, get CNID from it.
877 * 2. Search the database, hinting with the CNID from (1).
878 * 3. Build fullpath and create struct dir.
879 * 4. Add it to the cache.
881 * @param vol (r) pointer to struct vol, possibly modified in callee
882 * @param dir (r) pointer to parrent directory
883 * @param path (rw) pointer to struct path with valid path->u_name
884 * @param len (r) strlen of path->u_name
886 * @returns Pointer to new struct dir or NULL on error.
888 * @note Function also assigns path->m_name from path->u_name.
890 struct dir *dir_add(struct vol *vol, const struct dir *dir, struct path *path, int len)
893 struct dir *cdir = NULL;
896 struct adouble *adp = NULL;
904 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
905 /* there's a stray entry in the dircache */
906 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
907 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
908 ntohl(cdir->d_did), cfrombstr(dir->d_fullpath));
909 if (dir_remove(vol, cdir) != 0) {
911 AFP_PANIC("dir_add");
915 /* get_id needs adp for reading CNID from adouble file */
917 if ((ad_open(&ad, path->u_name, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDONLY)) == 0) /* 1 */
921 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
927 ad_close(adp, ADFLAGS_HF);
929 /* Get macname from unixname */
930 if (path->m_name == NULL) {
931 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
932 LOG(log_error, logtype_afpd, "dir_add(\"%s\"): can't assign macname", path->u_name);
939 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
940 || (bconchar(fullpath, '/') != BSTR_OK)
941 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
942 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
947 /* Allocate and initialize struct dir */
948 if ((cdir = dir_new(path->m_name,
954 &path->st)) == NULL) { /* 3 */
959 if ((dircache_add(vol, cdir)) != 0) { /* 4 */
960 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstr(fullpath));
966 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
967 cfrombstr(dir->d_u_name), path->u_name, err);
970 ad_close(adp, ADFLAGS_HF);
971 if (!cdir && fullpath)
978 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
979 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
980 ntohl(cdir->d_did), cfrombstr(cdir->d_fullpath));
987 * Free the queue with invalid struct dirs
989 * This gets called at the end of every AFP func.
991 void dir_free_invalid_q(void)
994 while (dir = (struct dir *)dequeue(invalid_dircache_entries))
999 * @brief Remove a dir from a cache and queue it for freeing
1001 * 1. Check if the dir is locked or has opened forks
1002 * 2. Remove it from the cache
1003 * 3. Queue it for removal
1004 * 4. If it's a request to remove curdir, mark curdir as invalid
1005 * 5. Mark it as invalid
1007 * @param (r) pointer to struct vol
1008 * @param (rw) pointer to struct dir
1010 int dir_remove(const struct vol *vol, struct dir *dir)
1015 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
1018 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
1019 ntohl(dir->d_did), cfrombstr(dir->d_u_name));
1021 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 2 */
1022 enqueue(invalid_dircache_entries, dir); /* 3 */
1024 if (curdir == dir) /* 4 */
1027 dir->d_did = CNID_INVALID; /* 5 */
1034 * @brief Modify a struct dir, adjust cache
1036 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
1037 * If given new_uname == new_mname, new_uname will point to new_mname.
1039 * @param vol (r) pointer to struct vol
1040 * @param dir (rw) pointer to struct dir
1041 * @param pdid (r) new parent DID
1042 * @param did (r) new DID
1043 * @param new_mname (r) new mac-name
1044 * @param new_uname (r) new unix-name
1045 * @param pdir_fullpath (r) new fullpath of parent dir
1047 int dir_modify(const struct vol *vol,
1051 const char *new_mname,
1052 const char *new_uname,
1053 bstring pdir_fullpath)
1057 /* Remove it from the cache */
1058 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
1066 /* free uname if it's not the same as mname */
1067 if (dir->d_m_name != dir->d_u_name)
1068 bdestroy(dir->d_u_name);
1070 if (new_uname == NULL)
1071 new_uname = new_mname;
1073 /* assign new name */
1074 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
1075 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1079 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
1080 dir->d_u_name = dir->d_m_name;
1082 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
1083 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1089 if (pdir_fullpath) {
1090 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
1092 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
1094 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
1098 if (dir->d_m_name_ucs2)
1099 free(dir->d_m_name_ucs2);
1100 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))
1101 dir->d_m_name_ucs2 = NULL;
1103 /* Re-add it to the cache */
1104 if ((dircache_add(vol, dir)) != 0) {
1106 AFP_PANIC("dir_modify");
1114 * @brief Resolve a catalog node name path
1116 * 1. Evaluate path type
1117 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
1118 * path from dirname, so eg getdirparams has sth it can chew on. curdir
1119 * is dir parent then. All this is done in path_from_dir().
1120 * 3. Parse next cnode name in path, cases:
1121 * 4. single "\0" -> do nothing
1122 * 5. two or more consecutive "\0" -> chdir("..") one or more times
1123 * 6. cnode name -> copy it to path.m_name
1124 * 7. Get unix name from mac name
1125 * 8. Special handling of request with did 1
1126 * 9. stat the cnode name
1127 * 10. If it's not there, it's probably an afp_createfile|dir,
1128 * return with curdir = dir parent, struct path = dirname
1129 * 11. If it's there and it's a file, it must should be the last element of the requested
1130 * path. Return with curdir = cnode name parent dir, struct path = filename
1131 * 12. Treat symlinks like files, dont follow them
1132 * 13. If it's a dir:
1133 * 14. Search the dircache for it
1134 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
1135 * 16. chdir into the dir and
1136 * 17. set m_name to the mac equivalent of "."
1139 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1141 static char path[ MAXPATHLEN + 1];
1142 static struct path ret;
1152 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstr(dir->d_fullpath));
1155 afp_errno = AFPERR_NOOBJ;
1156 memset(&ret, 0, sizeof(ret));
1158 switch (ret.m_type = *data) { /* 1 */
1161 len = (unsigned char) *data++;
1163 if (afp_version >= 30) {
1169 if (afp_version >= 30) {
1171 memcpy(&hint, data, sizeof(hint));
1173 data += sizeof(hint);
1175 memcpy(&len16, data, sizeof(len16));
1181 /* else it's an error */
1183 afp_errno = AFPERR_PARAM;
1186 *cpath += len + size;
1191 if (movecwd(vol, dir) < 0 ) {
1192 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1193 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1195 return path_from_dir(vol, dir, &ret);
1200 while (len) { /* 3 */
1201 if (*data == 0) { /* 4 or 5 */
1204 while (len > 0 && *data == 0) { /* 5 */
1205 /* chdir to parrent dir */
1206 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1208 if (movecwd( vol, dir ) < 0 ) {
1209 dir_remove(vol, dir);
1219 for ( p = path; *data != 0 && len > 0; len-- ) {
1221 if (p > &path[UTF8FILELEN_EARLY]) { /* FIXME safeguard, limit of early Mac OS X */
1222 afp_errno = AFPERR_PARAM;
1226 *p = 0; /* Terminate string */
1229 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1230 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1234 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstr(dir->d_fullpath), ret.u_name);
1236 /* Prevent access to our special folders like .AppleDouble */
1237 if (check_name(vol, ret.u_name)) {
1238 /* the name is illegal */
1239 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1240 afp_errno = AFPERR_PARAM;
1244 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1246 * Special case: CNID 1
1247 * root parent (did 1) has one child: the volume. Requests for did=1 with
1248 * some <name> must check against the volume name.
1250 if ((strcmp(cfrombstr(vol->v_root->d_m_name), ret.m_name)) == 0)
1256 * CNID != 1, eg. most of the times we take this way.
1257 * Now check if current path-part is a file or dir:
1258 * o if it's dir we have to step into it
1259 * o if it's a file we expect it to be the last part of the requested path
1260 * and thus call continue which should terminate the while loop because
1263 if (of_stat(&ret) != 0) { /* 9 */
1265 * ret.u_name doesn't exist, might be afp_createfile|dir
1266 * that means it should have been the last part
1269 /* it wasn't the last part, so we have a bogus path request */
1270 afp_errno = AFPERR_NOOBJ;
1274 * this will terminate clean in while (1) because len == 0,
1275 * probably afp_createfile|dir
1277 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}",
1278 cfrombstr(dir->d_fullpath), ret.u_name);
1282 switch (ret.st.st_mode & S_IFMT) {
1283 case S_IFREG: /* 11 */
1284 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}",
1285 cfrombstr(dir->d_fullpath), ret.u_name);
1287 /* it wasn't the last part, so we have a bogus path request */
1288 afp_errno = AFPERR_PARAM;
1291 continue; /* continues while loop */
1292 case S_IFLNK: /* 12 */
1293 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}",
1294 cfrombstr(dir->d_fullpath), ret.u_name);
1296 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}",
1297 cfrombstr(dir->d_fullpath), ret.u_name);
1298 afp_errno = AFPERR_PARAM;
1301 continue; /* continues while loop */
1302 case S_IFDIR: /* 13 */
1305 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1306 afp_errno = AFPERR_NODIR;
1310 /* Search the cache */
1311 int unamelen = strlen(ret.u_name);
1312 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1314 /* Not in cache, create one */
1315 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1316 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1317 ntohl(dir->d_did), ret.u_name, getcwdpath());
1321 } /* if/else cnid==1 */
1323 /* Now chdir to the evaluated dir */
1324 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1325 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1326 cfrombstr(curdir->d_fullpath), cfrombstr(cdir->d_fullpath), strerror(errno));
1328 return path_from_dir(vol, cdir, &ret);
1333 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1336 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1337 afp_errno = AFPERR_DID1;
1341 if (ret.m_name[0] == 0) {
1342 /* Last part was a dir */
1343 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1347 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1348 cfrombstr(dir->d_fullpath),
1349 cfrombstr(curdir->d_fullpath),
1356 * @brief chdir() to dir
1358 * @param vol (r) pointer to struct vol
1359 * @param dir (r) pointer to struct dir
1361 * @returns 0 on success, -1 on error with afp_errno set appropiately
1363 int movecwd(const struct vol *vol, struct dir *dir)
1370 LOG(log_maxdebug, logtype_afpd, "movecwd: from: curdir:\"%s\", cwd:\"%s\"",
1371 curdir ? cfrombstr(curdir->d_fullpath) : "INVALID", getcwdpath());
1373 if (dir->d_did == DIRDID_ROOT_PARENT) {
1374 curdir = &rootParent;
1378 LOG(log_debug, logtype_afpd, "movecwd(to: did: %u, \"%s\")",
1379 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1381 if ((ret = lchdir(cfrombstr(dir->d_fullpath))) != 0 ) {
1382 LOG(log_debug, logtype_afpd, "movecwd(\"%s\"): %s",
1383 cfrombstr(dir->d_fullpath), strerror(errno));
1385 /* p is a symlink or getcwd failed */
1386 afp_errno = AFPERR_BADTYPE;
1388 if (chdir(vol->v_path ) < 0) {
1389 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1390 /* XXX what do we do here? */
1392 curdir = vol->v_root;
1399 afp_errno = AFPERR_ACCESS;
1402 afp_errno = AFPERR_NOOBJ;
1412 * We can't use unix file's perm to support Apple's inherited protection modes.
1413 * If we aren't the file's owner we can't change its perms when moving it and smb
1414 * nfs,... don't even try.
1416 #define AFP_CHECK_ACCESS
1418 int check_access(char *path, int mode)
1420 #ifdef AFP_CHECK_ACCESS
1428 accessmode(current_vol, p, &ma, curdir, NULL);
1429 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1431 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1437 /* --------------------- */
1438 int file_access(struct path *path, int mode)
1442 accessmode(current_vol, path->u_name, &ma, curdir, &path->st);
1444 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): mapped user mode: 0x%02x",
1445 path->u_name, ma.ma_user);
1447 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE)) {
1448 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): write access denied", path->u_name);
1451 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD)) {
1452 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): read access denied", path->u_name);
1459 /* --------------------- */
1460 void setdiroffcnt(struct dir *dir, struct stat *st, uint32_t count)
1462 dir->d_offcnt = count;
1463 dir->d_ctime = st->st_ctime;
1464 dir->d_flags &= ~DIRF_CNID;
1468 /* ---------------------
1469 * is our cached also for reenumerate id?
1471 int dirreenumerate(struct dir *dir, struct stat *st)
1473 return st->st_ctime == dir->d_ctime && (dir->d_flags & DIRF_CNID);
1476 /* ------------------------------
1478 (name, dir) with curdir:name == dir, from afp_enumerate
1481 int getdirparams(const struct vol *vol,
1482 uint16_t bitmap, struct path *s_path,
1484 char *buf, size_t *buflen )
1488 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1489 int bit = 0, isad = 0;
1495 struct stat *st = &s_path->st;
1496 char *upath = s_path->u_name;
1498 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1499 (1 << DIRPBIT_CDATE) |
1500 (1 << DIRPBIT_MDATE) |
1501 (1 << DIRPBIT_BDATE) |
1502 (1 << DIRPBIT_FINFO)))) {
1505 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1507 if (ad.ad_mdp->adf_flags & O_CREAT) {
1508 /* We just created it */
1509 if (s_path->m_name == NULL) {
1510 if ((s_path->m_name = utompath(vol,
1513 utf8_encoding())) == NULL) {
1514 LOG(log_error, logtype_afpd,
1515 "getdirparams(\"%s\"): can't assign macname",
1516 cfrombstr(dir->d_fullpath));
1520 ad_setname(&ad, s_path->m_name);
1535 while ( bitmap != 0 ) {
1536 while (( bitmap & 1 ) == 0 ) {
1544 ad_getattr(&ad, &ashort);
1545 } else if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1546 ashort = htons(ATTRBIT_INVISIBLE);
1549 memcpy( data, &ashort, sizeof( ashort ));
1550 data += sizeof( ashort );
1554 memcpy( data, &pdid, sizeof( pdid ));
1555 data += sizeof( pdid );
1556 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1557 s_path->u_name, ntohl(pdid));
1560 case DIRPBIT_CDATE :
1561 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1562 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1563 memcpy( data, &aint, sizeof( aint ));
1564 data += sizeof( aint );
1567 case DIRPBIT_MDATE :
1568 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1569 memcpy( data, &aint, sizeof( aint ));
1570 data += sizeof( aint );
1573 case DIRPBIT_BDATE :
1574 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1575 aint = AD_DATE_START;
1576 memcpy( data, &aint, sizeof( aint ));
1577 data += sizeof( aint );
1580 case DIRPBIT_FINFO :
1582 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1583 } else { /* no appledouble */
1584 memset( data, 0, 32 );
1585 /* set default view -- this also gets done in ad_open() */
1586 ashort = htons(FINDERINFO_CLOSEDVIEW);
1587 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1589 /* dot files are by default visible */
1590 if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1591 ashort = htons(FINDERINFO_INVISIBLE);
1592 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1598 case DIRPBIT_LNAME :
1599 if (dir->d_m_name) /* root of parent can have a null name */
1602 memset(data, 0, sizeof(uint16_t));
1603 data += sizeof( uint16_t );
1606 case DIRPBIT_SNAME :
1607 memset(data, 0, sizeof(uint16_t));
1608 data += sizeof( uint16_t );
1612 memcpy( data, &dir->d_did, sizeof( aint ));
1613 data += sizeof( aint );
1614 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1615 s_path->u_name, ntohl(dir->d_did));
1618 case DIRPBIT_OFFCNT :
1620 /* this needs to handle current directory access rights */
1621 if (diroffcnt(dir, st)) {
1622 ashort = (dir->d_offcnt > 0xffff)?0xffff:dir->d_offcnt;
1624 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1625 setdiroffcnt(dir, st, ret);
1626 ashort = (dir->d_offcnt > 0xffff)?0xffff:dir->d_offcnt;
1628 ashort = htons( ashort );
1629 memcpy( data, &ashort, sizeof( ashort ));
1630 data += sizeof( ashort );
1634 aint = htonl(st->st_uid);
1635 memcpy( data, &aint, sizeof( aint ));
1636 data += sizeof( aint );
1640 aint = htonl(st->st_gid);
1641 memcpy( data, &aint, sizeof( aint ));
1642 data += sizeof( aint );
1645 case DIRPBIT_ACCESS :
1646 accessmode(vol, upath, &ma, dir , st);
1648 *data++ = ma.ma_user;
1649 *data++ = ma.ma_world;
1650 *data++ = ma.ma_group;
1651 *data++ = ma.ma_owner;
1654 /* Client has requested the ProDOS information block.
1655 Just pass back the same basic block for all
1656 directories. <shirsch@ibm.net> */
1657 case DIRPBIT_PDINFO :
1658 if (afp_version >= 30) { /* UTF8 name */
1659 utf8 = kTextEncodingUTF8;
1660 if (dir->d_m_name) /* root of parent can have a null name */
1663 memset(data, 0, sizeof(uint16_t));
1664 data += sizeof( uint16_t );
1666 memcpy(data, &aint, sizeof( aint ));
1667 data += sizeof( aint );
1669 else { /* ProDOS Info Block */
1672 ashort = htons( 0x0200 );
1673 memcpy( data, &ashort, sizeof( ashort ));
1674 data += sizeof( ashort );
1675 memset( data, 0, sizeof( ashort ));
1676 data += sizeof( ashort );
1680 case DIRPBIT_UNIXPR :
1681 /* accessmode may change st_mode with ACLs */
1682 accessmode(vol, upath, &ma, dir, st);
1684 aint = htonl(st->st_uid);
1685 memcpy( data, &aint, sizeof( aint ));
1686 data += sizeof( aint );
1687 aint = htonl(st->st_gid);
1688 memcpy( data, &aint, sizeof( aint ));
1689 data += sizeof( aint );
1692 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1693 memcpy( data, &aint, sizeof( aint ));
1694 data += sizeof( aint );
1696 *data++ = ma.ma_user;
1697 *data++ = ma.ma_world;
1698 *data++ = ma.ma_group;
1699 *data++ = ma.ma_owner;
1704 ad_close(&ad, ADFLAGS_HF);
1706 return( AFPERR_BITMAP );
1712 ashort = htons( data - buf );
1713 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1714 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, 0);
1716 if ( utf_nameoff ) {
1717 ashort = htons( data - buf );
1718 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1719 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, utf8);
1722 ad_close(&ad, ADFLAGS_HF);
1724 *buflen = data - buf;
1728 /* ----------------------------- */
1729 int path_error(struct path *path, int error)
1731 /* - a dir with access error
1732 * - no error it's a file
1735 if (path_isadir(path))
1737 if (path->st_valid && path->st_errno)
1739 return AFPERR_BADTYPE ;
1742 /* ----------------------------- */
1743 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1748 uint16_t vid, bitmap;
1754 memcpy( &vid, ibuf, sizeof( vid ));
1755 ibuf += sizeof( vid );
1757 if (NULL == ( vol = getvolbyvid( vid )) ) {
1758 return( AFPERR_PARAM );
1761 if (vol->v_flags & AFPVOL_RO)
1762 return AFPERR_VLOCK;
1764 memcpy( &did, ibuf, sizeof( did ));
1765 ibuf += sizeof( int );
1767 if (NULL == ( dir = dirlookup( vol, did )) ) {
1771 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1772 bitmap = ntohs( bitmap );
1773 ibuf += sizeof( bitmap );
1775 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1776 return get_afp_errno(AFPERR_NOOBJ);
1779 if ( *path->m_name != '\0' ) {
1780 rc = path_error(path, AFPERR_NOOBJ);
1781 /* maybe we are trying to set perms back */
1782 if (rc != AFPERR_ACCESS)
1787 * If ibuf is odd, make it even.
1789 if ((u_long)ibuf & 1 ) {
1793 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1794 setvoltime(obj, vol );
1800 * assume path == '\0' eg. it's a directory in canonical form
1802 int setdirparams(struct vol *vol, struct path *path, uint16_t d_bitmap, char *buf )
1814 uint16_t ashort, bshort, oshort;
1816 int change_mdate = 0;
1817 int change_parent_mdate = 0;
1819 uint16_t bitmap = d_bitmap;
1820 u_char finder_buf[32];
1823 bool set_upriv = false, set_maccess = false;
1825 LOG(log_debug, logtype_afpd, "setdirparams(%s)", path->u_name);
1828 upath = path->u_name;
1830 while ( bitmap != 0 ) {
1831 while (( bitmap & 1 ) == 0 ) {
1839 memcpy( &ashort, buf, sizeof( ashort ));
1840 buf += sizeof( ashort );
1842 case DIRPBIT_CDATE :
1844 memcpy(&cdate, buf, sizeof(cdate));
1845 buf += sizeof( cdate );
1847 case DIRPBIT_MDATE :
1848 memcpy(&newdate, buf, sizeof(newdate));
1849 buf += sizeof( newdate );
1851 case DIRPBIT_BDATE :
1853 memcpy(&bdate, buf, sizeof(bdate));
1854 buf += sizeof( bdate );
1856 case DIRPBIT_FINFO :
1858 memcpy( finder_buf, buf, 32 );
1861 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1862 memcpy( &owner, buf, sizeof(owner));
1863 buf += sizeof( owner );
1866 memcpy( &group, buf, sizeof( group ));
1867 buf += sizeof( group );
1869 case DIRPBIT_ACCESS :
1872 ma.ma_user = *buf++;
1873 ma.ma_world = *buf++;
1874 ma.ma_group = *buf++;
1875 ma.ma_owner = *buf++;
1876 mpriv = mtoumode( &ma ) | vol->v_dperm;
1878 /* Ignore what the client thinks we should do to the
1879 ProDOS information block. Skip over the data and
1880 report nothing amiss. <shirsch@ibm.net> */
1881 case DIRPBIT_PDINFO :
1882 if (afp_version < 30) {
1886 err = AFPERR_BITMAP;
1890 case DIRPBIT_UNIXPR :
1891 if (vol_unix_priv(vol)) {
1893 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1894 buf += sizeof( owner );
1895 memcpy( &group, buf, sizeof( group ));
1896 buf += sizeof( group );
1899 memcpy( &upriv, buf, sizeof( upriv ));
1900 buf += sizeof( upriv );
1901 upriv = ntohl (upriv) | vol->v_dperm;
1906 err = AFPERR_BITMAP;
1916 if (ad_open(&ad, upath, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) != 0) {
1918 * Check to see what we're trying to set. If it's anything
1919 * but ACCESS, UID, or GID, give an error. If it's any of those
1920 * three, we don't need the ad to be open, so just continue.
1922 * note: we also don't need to worry about mdate. also, be quiet
1923 * if we're using the noadouble option.
1926 if (!vol_noadouble(vol) && (d_bitmap &
1927 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1928 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1929 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1930 return AFPERR_ACCESS;
1936 * Check to see if a create was necessary. If it was, we'll want
1937 * to set our name, etc.
1939 if ( (ad_get_MD_flags( &ad ) & O_CREAT)) {
1940 ad_setname(&ad, cfrombstr(curdir->d_m_name));
1946 while ( bitmap != 0 ) {
1947 while (( bitmap & 1 ) == 0 ) {
1955 ad_getattr(&ad, &bshort);
1957 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1958 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1962 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1963 change_parent_mdate = 1;
1964 ad_setattr(&ad, bshort);
1967 case DIRPBIT_CDATE :
1969 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1972 case DIRPBIT_MDATE :
1974 case DIRPBIT_BDATE :
1976 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1979 case DIRPBIT_FINFO :
1981 /* Fixes #2802236 */
1982 uint16_t *fflags = (uint16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1983 *fflags &= htons(~FINDERINFO_ISHARED);
1985 if ( dir->d_did == DIRDID_ROOT ) {
1987 * Alright, we admit it, this is *really* sick!
1988 * The 4 bytes that we don't copy, when we're dealing
1989 * with the root of a volume, are the directory's
1990 * location information. This eliminates that annoying
1991 * behavior one sees when mounting above another mount
1994 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1995 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1997 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
2001 case DIRPBIT_UID : /* What kind of loser mounts as root? */
2002 if ( (dir->d_did == DIRDID_ROOT) &&
2003 (setdeskowner( ntohl(owner), -1 ) < 0)) {
2004 err = set_dir_errors(path, "setdeskowner", errno);
2005 if (isad && err == AFPERR_PARAM) {
2006 err = AFP_OK; /* ???*/
2009 goto setdirparam_done;
2012 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
2013 err = set_dir_errors(path, "setdirowner", errno);
2014 goto setdirparam_done;
2018 if (dir->d_did == DIRDID_ROOT)
2019 setdeskowner( -1, ntohl(group) );
2020 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2021 err = set_dir_errors(path, "setdirowner", errno);
2022 goto setdirparam_done;
2025 case DIRPBIT_ACCESS :
2027 case DIRPBIT_PDINFO :
2028 if (afp_version >= 30) {
2029 err = AFPERR_BITMAP;
2030 goto setdirparam_done;
2033 case DIRPBIT_UNIXPR :
2034 if (!vol_unix_priv(vol)) {
2035 err = AFPERR_BITMAP;
2036 goto setdirparam_done;
2040 err = AFPERR_BITMAP;
2041 goto setdirparam_done;
2050 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2051 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2055 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2056 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2061 if (path->st_valid && !path->st_errno) {
2062 struct stat *st = &path->st;
2064 if (dir && dir->d_pdid) {
2065 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
2068 if (ad_flush(&ad) != 0) {
2071 err = AFPERR_ACCESS;
2078 ad_close(&ad, ADFLAGS_HF);
2081 if (err == AFP_OK) {
2082 if (set_maccess == true) {
2083 if (dir->d_did == DIRDID_ROOT) {
2085 if (!dir_rx_set(mpriv)) {
2086 /* we can't remove read and search for owner on volume root */
2087 err = AFPERR_ACCESS;
2091 if (setdirmode(vol, upath, mpriv) < 0)
2092 err = set_dir_errors(path, "setdirmode", errno);
2094 if ((set_upriv == true) && vol_unix_priv(vol)) {
2095 if (dir->d_did == DIRDID_ROOT) {
2096 if (!dir_rx_set(upriv)) {
2097 /* we can't remove read and search for owner on volume root */
2098 err = AFPERR_ACCESS;
2101 setdeskowner(-1, ntohl(group));
2105 if (setdirowner(vol, upath, -1, ntohl(group)) < 0) {
2106 err = set_dir_errors(path, "setdirowner", errno);
2111 if (setdirunixmode(vol, upath, upriv) < 0)
2112 err = set_dir_errors(path, "setdirunixmode", errno);
2117 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2118 && gettimeofday(&tv, NULL) == 0) {
2119 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
2120 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2121 /* be careful with bitmap because now dir is null */
2122 bitmap = 1<<DIRPBIT_MDATE;
2123 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2124 /* should we reset curdir ?*/
2130 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2144 memcpy( &vid, ibuf, sizeof( vid ));
2145 ibuf += sizeof( vid );
2146 if (NULL == (vol = getvolbyvid( vid )) ) {
2147 return( AFPERR_PARAM );
2150 memcpy( &did, ibuf, sizeof( did ));
2151 ibuf += sizeof( did );
2155 * if it's CNID 2 our only choice to meet the specs is call sync.
2156 * For any other CNID just sync that dir. To my knowledge the
2157 * intended use of FPSyncDir is to sync the volume so all we're
2158 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2161 if ( ntohl(did) == 2 ) {
2164 if (NULL == ( dir = dirlookup( vol, did )) ) {
2165 return afp_errno; /* was AFPERR_NOOBJ */
2168 if (movecwd( vol, dir ) < 0 )
2169 return ( AFPERR_NOOBJ );
2172 * Assuming only OSens that have dirfd also may require fsyncing directories
2173 * in order to flush metadata e.g. Linux.
2177 if (NULL == ( dp = opendir( "." )) ) {
2180 return( AFPERR_NOOBJ );
2182 return( AFPERR_ACCESS );
2184 return( AFPERR_PARAM );
2188 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2191 if ( fsync ( dfd ) < 0 )
2192 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2193 dir->d_u_name, strerror(errno) );
2194 closedir(dp); /* closes dfd too */
2197 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2200 return( AFPERR_NOOBJ );
2202 return( AFPERR_ACCESS );
2204 return( AFPERR_PARAM );
2208 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2209 vol->ad_path(".", ADFLAGS_DIR) );
2211 if ( fsync(dfd) < 0 )
2212 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2213 vol->ad_path(cfrombstr(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2220 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2226 struct path *s_path;
2234 memcpy( &vid, ibuf, sizeof( vid ));
2235 ibuf += sizeof( vid );
2236 if (NULL == ( vol = getvolbyvid( vid )) ) {
2237 return( AFPERR_PARAM );
2240 if (vol->v_flags & AFPVOL_RO)
2241 return AFPERR_VLOCK;
2243 memcpy( &did, ibuf, sizeof( did ));
2244 ibuf += sizeof( did );
2245 if (NULL == ( dir = dirlookup( vol, did )) ) {
2246 return afp_errno; /* was AFPERR_NOOBJ */
2248 /* for concurrent access we need to be sure we are not in the
2249 * folder we want to create...
2253 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2254 return get_afp_errno(AFPERR_PARAM);
2256 /* cname was able to move curdir to it! */
2257 if (*s_path->m_name == '\0')
2258 return AFPERR_EXIST;
2260 upath = s_path->u_name;
2262 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2266 if (of_stat(s_path) < 0) {
2272 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2276 if ( movecwd( vol, dir ) < 0 ) {
2277 return( AFPERR_PARAM );
2281 if (ad_open(&ad, ".", ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) < 0) {
2282 if (vol_noadouble(vol))
2283 goto createdir_done;
2284 return( AFPERR_ACCESS );
2286 ad_setname(&ad, s_path->m_name);
2287 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2289 fce_register_new_dir(s_path);
2292 ad_close(&ad, ADFLAGS_HF);
2295 memcpy( rbuf, &dir->d_did, sizeof( uint32_t ));
2296 *rbuflen = sizeof( uint32_t );
2297 setvoltime(obj, vol );
2302 * dst new unix filename (not a pathname)
2303 * newname new mac name
2305 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2307 int renamedir(const struct vol *vol,
2312 struct dir *newparent,
2318 /* existence check moved to afp_moveandrename */
2319 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2322 return( AFPERR_NOOBJ );
2324 return( AFPERR_ACCESS );
2326 return AFPERR_VLOCK;
2328 /* tried to move directory into a subdirectory of itself */
2329 return AFPERR_CANTMOVE;
2331 /* this needs to copy and delete. bleah. that means we have
2332 * to deal with entire directory hierarchies. */
2333 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2337 if ((err = deletedir(dirfd, src)) < 0)
2341 return( AFPERR_PARAM );
2345 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2349 if (ad_open(&ad, dst, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR) == 0) {
2350 ad_setname(&ad, newname);
2352 ad_close(&ad, ADFLAGS_HF);
2358 /* delete an empty directory */
2359 int deletecurdir(struct vol *vol)
2363 struct dir *fdir, *pdir;
2369 if ((pdir = dirlookup(vol, curdir->d_pdid)) == NULL) {
2370 return( AFPERR_ACCESS );
2376 /* we never want to create a resource fork here, we are going to delete it */
2377 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2379 ad_getattr(&ad, &ashort);
2380 ad_close(&ad, ADFLAGS_HF);
2381 if ((ashort & htons(ATTRBIT_NODELETE))) {
2382 return AFPERR_OLOCK;
2385 err = vol->vfs->vfs_deletecurdir(vol);
2387 LOG(log_error, logtype_afpd, "deletecurdir: error deleting .AppleDouble in \"%s\"",
2388 curdir->d_fullpath);
2392 /* now get rid of dangling symlinks */
2393 if ((dp = opendir("."))) {
2394 while ((de = readdir(dp))) {
2395 /* skip this and previous directory */
2396 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2399 /* bail if it's not a symlink */
2400 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2401 LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): not empty",
2402 curdir->d_fullpath);
2404 return AFPERR_DIRNEMPT;
2407 if ((err = netatalk_unlink(de->d_name))) {
2414 if (movecwd(vol, pdir) < 0) {
2419 LOG(log_debug, logtype_afpd, "deletecurdir: moved to \"%s\"",
2420 cfrombstr(curdir->d_fullpath));
2422 err = netatalk_rmdir_all_errors(-1, cfrombstr(fdir->d_u_name));
2423 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2424 cnid_delete(vol->v_cdb, fdir->d_did);
2425 dir_remove( vol, fdir );
2427 LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): netatalk_rmdir_all_errors error",
2428 cfrombstr(curdir->d_fullpath));
2433 /* inode is used as key for cnid.
2434 * Close the descriptor only after cnid_delete
2442 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2452 sfunc = (unsigned char) *ibuf++;
2455 if (sfunc >= 3 && sfunc <= 6) {
2456 if (afp_version < 30) {
2457 return( AFPERR_PARAM );
2464 case 3 :/* unicode */
2465 memcpy( &id, ibuf, sizeof( id ));
2468 if (( pw = getpwuid( id )) == NULL ) {
2469 return( AFPERR_NOITEM );
2471 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2472 pw->pw_name, -1, &name);
2479 case 4 : /* unicode */
2480 memcpy( &id, ibuf, sizeof( id ));
2483 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2484 return( AFPERR_NOITEM );
2486 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2487 gr->gr_name, -1, &name);
2494 case 5 : /* UUID -> username */
2495 case 6 : /* UUID -> groupname */
2496 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2497 return AFPERR_PARAM;
2498 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2500 len = getnamefromuuid((unsigned char*) ibuf, &name, &type);
2501 if (len != 0) /* its a error code, not len */
2502 return AFPERR_NOITEM;
2505 if (( pw = getpwnam( name )) == NULL )
2506 return( AFPERR_NOITEM );
2507 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2508 id = htonl(UUID_USER);
2509 memcpy( rbuf, &id, sizeof( id ));
2510 id = htonl( pw->pw_uid);
2511 rbuf += sizeof( id );
2512 memcpy( rbuf, &id, sizeof( id ));
2513 rbuf += sizeof( id );
2514 *rbuflen = 2 * sizeof( id );
2517 if (( gr = getgrnam( name )) == NULL )
2518 return( AFPERR_NOITEM );
2519 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2520 id = htonl(UUID_GROUP);
2521 memcpy( rbuf, &id, sizeof( id ));
2522 rbuf += sizeof( id );
2523 id = htonl( gr->gr_gid);
2524 memcpy( rbuf, &id, sizeof( id ));
2525 rbuf += sizeof( id );
2526 *rbuflen = 2 * sizeof( id );
2534 return( AFPERR_PARAM );
2538 len = strlen( name );
2541 uint16_t tp = htons(len);
2542 memcpy(rbuf, &tp, sizeof(tp));
2551 memcpy( rbuf, name, len );
2559 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2568 sfunc = (unsigned char) *ibuf++;
2570 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2573 case 2 : /* unicode */
2574 if (afp_version < 30) {
2575 return( AFPERR_PARAM );
2577 memcpy(&ulen, ibuf, sizeof(ulen));
2580 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2584 len = (unsigned char) *ibuf++;
2586 case 5 : /* username -> UUID */
2587 case 6 : /* groupname -> UUID */
2588 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2589 return AFPERR_PARAM;
2590 memcpy(&ulen, ibuf, sizeof(ulen));
2595 return( AFPERR_PARAM );
2601 return AFPERR_PARAM;
2604 case 1 : /* unicode */
2606 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2607 return( AFPERR_NOITEM );
2611 memcpy( rbuf, &id, sizeof( id ));
2612 *rbuflen = sizeof( id );
2615 case 2 : /* unicode */
2617 LOG(log_debug, logtype_afpd, "afp_mapname: getgrnam for name: %s",ibuf);
2618 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2619 return( AFPERR_NOITEM );
2622 LOG(log_debug, logtype_afpd, "afp_mapname: getgrnam for name: %s -> id: %d",ibuf, id);
2624 memcpy( rbuf, &id, sizeof( id ));
2625 *rbuflen = sizeof( id );
2627 case 5 : /* username -> UUID */
2628 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2629 if (0 != getuuidfromname(ibuf, UUID_USER, (unsigned char *)rbuf))
2630 return AFPERR_NOITEM;
2631 *rbuflen = UUID_BINSIZE;
2633 case 6 : /* groupname -> UUID */
2634 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2635 if (0 != getuuidfromname(ibuf, UUID_GROUP, (unsigned char *)rbuf))
2636 return AFPERR_NOITEM;
2637 *rbuflen = UUID_BINSIZE;
2644 /* ------------------------------------
2645 variable DID support
2647 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2658 /* do nothing as dids are static for the life of the process. */
2662 memcpy(&vid, ibuf, sizeof( vid ));
2663 ibuf += sizeof( vid );
2664 if (( vol = getvolbyvid( vid )) == NULL ) {
2665 return( AFPERR_PARAM );
2668 memcpy( &did, ibuf, sizeof( did ));
2669 ibuf += sizeof( did );
2670 if (( dir = dirlookup( vol, did )) == NULL ) {
2671 return( AFPERR_PARAM );
2674 /* dir_remove -- deletedid */
2680 /* did creation gets done automatically
2681 * there's a pb again with case but move it to cname
2683 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2686 struct dir *parentdir;
2694 memcpy(&vid, ibuf, sizeof(vid));
2695 ibuf += sizeof( vid );
2697 if (NULL == ( vol = getvolbyvid( vid )) ) {
2698 return( AFPERR_PARAM );
2701 memcpy(&did, ibuf, sizeof(did));
2702 ibuf += sizeof(did);
2704 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2708 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2709 return get_afp_errno(AFPERR_PARAM);
2712 if ( *path->m_name != '\0' ) {
2713 return path_error(path, AFPERR_NOOBJ);
2716 if ( !path->st_valid && of_stat(path ) < 0 ) {
2717 return( AFPERR_NOOBJ );
2719 if ( path->st_errno ) {
2720 return( AFPERR_NOOBJ );
2723 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2724 *rbuflen = sizeof(curdir->d_did);