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>
35 #include <atalk/netatalk_conf.h>
37 #include "directory.h"
49 * FIXMEs, loose ends after the dircache rewrite:
50 * o merge dircache_search_by_name and dir_add ??
51 * o case-insensitivity is gone from cname
55 /*******************************************************************************************
57 ******************************************************************************************/
60 /* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
61 struct dir rootParent = {
62 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
63 NULL, 0, 0, /* qidx_node, ctime, d_flags */
64 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
66 struct dir *curdir = &rootParent;
67 struct path Cur_Path = {
72 NULL,/* struct dir * */
73 0, /* stat is not set */
79 * dir_remove queues struct dirs to be freed here. We can't just delete them immeidately
80 * eg in dircache_search_by_id, because a caller somewhere up the stack might be
83 * - we mark it as invalid by setting d_did to CNID_INVALID (ie 0)
84 * - queue it in "invalid_dircache_entries" queue
85 * - which is finally freed at the end of every AFP func in afp_dsi.c.
87 q_t *invalid_dircache_entries;
90 /*******************************************************************************************
92 ******************************************************************************************/
95 /* -------------------------
96 appledouble mkdir afp error code.
98 static int netatalk_mkdir(const struct vol *vol, const char *name)
103 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
104 if (lstat(".", &st) < 0)
106 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
107 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
108 name, st.st_mode, vol->v_umask);
110 ret = mkdir(name, mode);
112 ret = ad_mkdir(name, DIRBITS | 0777);
118 return( AFPERR_NOOBJ );
120 return( AFPERR_VLOCK );
123 return( AFPERR_ACCESS );
125 return( AFPERR_EXIST );
128 return( AFPERR_DFULL );
130 return( AFPERR_PARAM );
136 /* ------------------- */
137 static int deletedir(const struct vol *vol, int dirfd, char *dir)
139 char path[MAXPATHLEN + 1];
147 if ((len = strlen(dir)) +2 > sizeof(path))
151 if ((dp = opendirat(dirfd, dir)) == NULL)
157 remain = sizeof(path) -len -1;
158 while ((de = readdir(dp)) && err == AFP_OK) {
159 /* skip this and previous directory */
160 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
163 if (strlen(de->d_name) > remain) {
167 strcpy(path + len, de->d_name);
168 if (ostatat(dirfd, path, &st, vol_syml_opt(vol))) {
171 if (S_ISDIR(st.st_mode)) {
172 err = deletedir(vol, dirfd, path);
174 err = netatalk_unlinkat(dirfd, path);
179 /* okay. the directory is empty. delete it. note: we already got rid
182 err = netatalk_rmdir(dirfd, dir);
187 /* do a recursive copy. */
188 static int copydir(struct vol *vol, struct dir *ddir, int dirfd, char *src, char *dst)
190 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
199 /* doesn't exist or the path is too long. */
200 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
201 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
202 ((dp = opendirat(dirfd, src)) == NULL))
205 /* try to create the destination directory */
206 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
211 /* set things up to copy */
215 srem = sizeof(spath) - slen -1;
220 drem = sizeof(dpath) - dlen -1;
223 while ((de = readdir(dp))) {
224 /* skip this and previous directory */
225 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
228 if (strlen(de->d_name) > srem) {
232 strcpy(spath + slen, de->d_name);
234 if (ostatat(dirfd, spath, &st, vol_syml_opt(vol)) == 0) {
235 if (strlen(de->d_name) > drem) {
239 strcpy(dpath + dlen, de->d_name);
241 if (S_ISDIR(st.st_mode)) {
242 if (AFP_OK != (err = copydir(vol, ddir, dirfd, spath, dpath)))
244 } else if (AFP_OK != (err = copyfile(vol, vol, ddir, dirfd, spath, dpath, NULL, NULL))) {
248 /* keep the same time stamp. */
249 ut.actime = ut.modtime = st.st_mtime;
255 /* keep the same time stamp. */
256 if (ostatat(dirfd, src, &st, vol_syml_opt(vol)) == 0) {
257 ut.actime = ut.modtime = st.st_mtime;
266 /* ---------------------
267 * is our cached offspring count valid?
269 static int diroffcnt(struct dir *dir, struct stat *st)
271 return st->st_ctime == dir->d_ctime;
274 /* --------------------- */
275 static int invisible_dots(const struct vol *vol, const char *name)
277 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
280 /* ------------------ */
281 static int set_dir_errors(struct path *path, const char *where, int err)
286 return AFPERR_ACCESS;
290 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
295 * @brief Convert name in client encoding to server encoding
297 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
298 * This only gets called from cname().
300 * @returns 0 on success, -1 on error
302 * @note If the passed ret->m_name is mangled, we'll demangle it
304 static int cname_mtouname(const struct vol *vol, struct dir *dir, struct path *ret, int toUTF8)
306 static char temp[ MAXPATHLEN + 1];
310 if (vol->v_obj->afp_version >= 30) {
312 if (dir->d_did == DIRDID_ROOT_PARENT) {
314 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
315 * So we compare it with the longname from the current volume and if they match
316 * we overwrite the requested path with the utf8 volume name so that the following
319 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
320 if (strcasecmp(ret->m_name, temp) == 0)
321 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
324 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
325 afp_errno = AFPERR_PARAM;
328 strcpy(ret->m_name, temp);
332 /* check for OS X mangled filename :( */
333 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
335 if (curdir == NULL) {
336 /* demangle_osx() calls dirlookup() which might have clobbered curdir */
340 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
341 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
343 if (t != ret->m_name) {
345 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
346 * flags weren't the same
348 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding(vol->v_obj))) ) {
349 /* at last got our view of mac name */
350 strcpy(ret->m_name, t);
353 } /* afp_version >= 30 */
355 /* If we haven't got it by now, get it */
356 if (ret->u_name == NULL) {
357 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding(vol->v_obj))) == NULL) {
358 afp_errno = AFPERR_PARAM;
367 * @brief Build struct path from struct dir
369 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
370 * 1. move cwd into parent dir (we're often already there, but not always)
371 * 2. set struct path to the dirname
373 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
374 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
375 * 4. indicate there's no dir for this path
378 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
380 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
386 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
389 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
390 if (dir->d_m_name == dir->d_u_name) {
391 ret->u_name = ret->m_name;
393 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
394 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
399 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
400 cfrombstr(dir->d_fullpath),
401 cfrombstr(curdir->d_fullpath),
407 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
410 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
411 if (dir->d_m_name == dir->d_u_name) {
412 ret->u_name = ret->m_name;
414 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
415 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
418 ret->d_dir = NULL; /* 4 */
419 dir_remove(vol, dir); /* 5 */
426 /* DEADC0DE: never get here */
431 /*********************************************************************************************
433 ********************************************************************************************/
435 int get_afp_errno(const int param)
437 if (afp_errno != AFPERR_DID1)
443 * Resolve struct dir for an absolute path
445 * Given a path like "/Volumes/volume/dir/subdir" in a volume "/Volumes/volume" return
446 * a pointer to struct dir of "subdir".
447 * 1. Remove volue path from absolute path
449 * 3. loop through all elements of the remaining path from 1.
450 * 4. we only allow dirs
452 * 6. if not found in the dircache query the CNID database for the DID
453 * 7. and use dirlookup to resolve the DID to a it's struct dir *
455 * @param vol (r) volume the path is in, must be known
456 * @param path (r) absoule path
458 * @returns pointer to struct dir or NULL on error
460 struct dir *dirlookup_bypath(const struct vol *vol, const char *path)
464 struct dir *dir = NULL;
466 bstring rpath = NULL;
467 bstring statpath = NULL;
468 struct bstrList *l = NULL;
474 LOG(log_debug, logtype_afpd, "dirlookup_bypath(\"%s\")", path);
476 if (strcmp(vol->v_path, path) == 0)
479 EC_NULL(rpath = rel_path_in_vol(path, vol->v_path)); /* 1. */
481 LOG(log_debug, logtype_afpd, "dirlookup_bypath: rpath: \"%s\"", cfrombstr(rpath));
483 EC_NULL(statpath = bfromcstr(vol->v_path)); /* 2. */
485 l = bsplit(rpath, '/');
486 for (int i = 0; i < l->qty ; i++) { /* 3. */
488 EC_ZERO(bcatcstr(statpath, "/"));
489 EC_ZERO(bconcat(statpath, l->entry[i]));
491 LOG(log_debug, logtype_afpd, "dirlookup_bypath: statpath: \"%s\"", cfrombstr(statpath));
493 EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
494 "lstat(rpath: %s, elem: %s): %s: %s",
495 cfrombstr(rpath), cfrombstr(l->entry[i]),
496 cfrombstr(statpath), strerror(errno));
498 if (!(S_ISDIR(st.st_mode))) /* 4. */
501 if ((dir = dircache_search_by_name(vol, /* 5. */
503 cfrombstr(l->entry[i]),
504 blength(l->entry[i]))) == NULL) {
506 AFP_CNID_START("cnid_add");
507 cnid = cnid_add(vol->v_cdb, /* 6. */
510 cfrombstr(l->entry[i]),
511 blength(l->entry[i]),
514 if (cnid == CNID_INVALID)
517 if ((dir = dirlookup(vol, cnid)) == NULL) /* 7. */
529 LOG(log_debug, logtype_afpd, "dirlookup_bypath: result: \"%s\"",
530 cfrombstr(dir->d_fullpath));
536 * @brief Resolve a DID
538 * Resolve a DID, allocate a struct dir for it
539 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
540 * 2a. Check if the DID is in the cache.
541 * 2b. Check if it's really a dir because we cache files too.
542 * 3. If it's not in the cache resolve it via the database.
543 * 4. Build complete server-side path to the dir.
544 * 5. Check if it exists and is a directory.
545 * 6. Create the struct dir and populate it.
546 * 7. Add it to the cache.
548 * @param vol (r) pointer to struct vol
549 * @param did (r) DID to resolve
551 * @returns pointer to struct dir
553 struct dir *dirlookup(const struct vol *vol, cnid_t did)
555 static char buffer[12 + MAXPATHLEN + 1];
557 struct dir *ret = NULL, *pdir;
558 bstring fullpath = NULL;
559 char *upath = NULL, *mpath;
562 int buflen = 12 + MAXPATHLEN + 1;
566 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): START", ntohl(did));
568 /* check for did 0, 1 and 2 */
569 if (did == 0 || vol == NULL) { /* 1 */
570 afp_errno = AFPERR_PARAM;
573 } else if (did == DIRDID_ROOT_PARENT) {
574 rootParent.d_vid = vol->v_vid;
577 } else if (did == DIRDID_ROOT) {
582 /* Search the cache */
583 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
584 if (ret->d_flags & DIRF_ISFILE) { /* 2b */
585 afp_errno = AFPERR_BADTYPE;
589 if (lstat(cfrombstr(ret->d_fullpath), &st) != 0) {
590 LOG(log_debug, logtype_afpd, "dirlookup(did: %u, path: \"%s\"): lstat: %s",
591 ntohl(did), cfrombstr(ret->d_fullpath), strerror(errno));
595 /* It's not there anymore, so remove it */
596 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): calling dir_remove", ntohl(did));
597 dir_remove(vol, ret);
598 afp_errno = AFPERR_NOOBJ;
613 utf8 = utf8_encoding(vol->v_obj);
614 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
616 /* Get it from the database */
618 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): querying CNID database", ntohl(did));
620 AFP_CNID_START("cnid_resolve");
621 upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen);
624 afp_errno = AFPERR_NOOBJ;
628 if ((upath = strdup(upath)) == NULL) { /* 3 */
629 afp_errno = AFPERR_NOOBJ;
636 * Recurse up the tree, terminates in dirlookup when either
637 * - DIRDID_ROOT is hit
638 * - a cached entry is found
640 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): recursion for did: %u",
641 ntohl(did), ntohl(pdid));
642 if ((pdir = dirlookup(vol, pdid)) == NULL) {
647 /* build the fullpath */
648 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
649 || bconchar(fullpath, '/') != BSTR_OK
650 || bcatcstr(fullpath, upath) != BSTR_OK) {
655 /* stat it and check if it's a dir */
656 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): stating \"%s\"",
657 ntohl(did), cfrombstr(fullpath));
659 if (ostat(cfrombstr(fullpath), &st, vol_syml_opt(vol)) != 0) { /* 5a */
662 afp_errno = AFPERR_NOOBJ;
666 afp_errno = AFPERR_ACCESS;
670 afp_errno = AFPERR_MISC;
675 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
676 afp_errno = AFPERR_BADTYPE;
682 /* Get macname from unix name */
683 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
684 afp_errno = AFPERR_NOOBJ;
689 /* Create struct dir */
690 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath, &st)) == NULL) { /* 6 */
691 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
696 /* Add it to the cache only if it's a dir */
697 if (dircache_add(vol, ret) != 0) { /* 7 */
703 if (upath) free(upath);
705 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
706 ntohl(did), AfpErr2name(afp_errno));
715 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): RESULT: pdid: %u, path: \"%s\"",
716 ntohl(ret->d_did), ntohl(ret->d_pdid), cfrombstr(ret->d_fullpath));
721 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
724 * @brief Construct struct dir
726 * Construct struct dir from parameters.
728 * @param m_name (r) directory name in UTF8-dec
729 * @param u_name (r) directory name in server side encoding
730 * @param vol (r) pointer to struct vol
731 * @param pdid (r) Parent CNID
732 * @param did (r) CNID
733 * @param path (r) Full unix path to object
734 * @param st (r) struct stat of object
736 * @returns pointer to new struct dir or NULL on error
738 * @note Most of the time mac name and unix name are the same.
740 struct dir *dir_new(const char *m_name,
742 const struct vol *vol,
750 dir = (struct dir *) calloc(1, sizeof( struct dir ));
754 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
759 if (convert_string_allocate( (utf8_encoding(vol->v_obj)) ? CH_UTF8_MAC : vol->v_maccharset,
762 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
763 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
764 dir->d_m_name_ucs2 = NULL;
767 if (m_name == u_name || !strcmp(m_name, u_name)) {
768 dir->d_u_name = dir->d_m_name;
770 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
771 bdestroy(dir->d_m_name);
778 dir->d_vid = vol->v_vid;
779 dir->d_fullpath = path;
780 dir->dcache_ctime = st->st_ctime;
781 dir->dcache_ino = st->st_ino;
782 if (!S_ISDIR(st->st_mode))
783 dir->d_flags = DIRF_ISFILE;
784 dir->d_rights_cache = 0xffffffff;
789 * @brief Free a struct dir and all its members
791 * @param (rw) pointer to struct dir
793 void dir_free(struct dir *dir)
795 if (dir->d_u_name != dir->d_m_name) {
796 bdestroy(dir->d_u_name);
798 if (dir->d_m_name_ucs2)
799 free(dir->d_m_name_ucs2);
800 bdestroy(dir->d_m_name);
801 bdestroy(dir->d_fullpath);
806 * @brief Create struct dir from struct path
808 * Create a new struct dir from struct path. Then add it to the cache.
810 * 1. Open adouble file, get CNID from it.
811 * 2. Search the database, hinting with the CNID from (1).
812 * 3. Build fullpath and create struct dir.
813 * 4. Add it to the cache.
815 * @param vol (r) pointer to struct vol, possibly modified in callee
816 * @param dir (r) pointer to parrent directory
817 * @param path (rw) pointer to struct path with valid path->u_name
818 * @param len (r) strlen of path->u_name
820 * @returns Pointer to new struct dir or NULL on error.
822 * @note Function also assigns path->m_name from path->u_name.
824 struct dir *dir_add(struct vol *vol, const struct dir *dir, struct path *path, int len)
827 struct dir *cdir = NULL;
830 struct adouble *adp = NULL;
831 bstring fullpath = NULL;
838 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
839 /* there's a stray entry in the dircache */
840 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
841 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
842 ntohl(cdir->d_did), cfrombstr(dir->d_fullpath));
843 if (dir_remove(vol, cdir) != 0) {
845 AFP_PANIC("dir_add");
849 /* get_id needs adp for reading CNID from adouble file */
851 if ((ad_open(&ad, path->u_name, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDONLY)) == 0) /* 1 */
855 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
861 ad_close(adp, ADFLAGS_HF);
863 /* Get macname from unixname */
864 if (path->m_name == NULL) {
865 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding(vol->v_obj))) == NULL) {
866 LOG(log_error, logtype_afpd, "dir_add(\"%s\"): can't assign macname", path->u_name);
873 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
874 || (bconchar(fullpath, '/') != BSTR_OK)
875 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
876 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
881 /* Allocate and initialize struct dir */
882 if ((cdir = dir_new(path->m_name,
888 &path->st)) == NULL) { /* 3 */
893 if ((dircache_add(vol, cdir)) != 0) { /* 4 */
894 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstr(fullpath));
900 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
901 cfrombstr(dir->d_u_name), path->u_name, err);
904 ad_close(adp, ADFLAGS_HF);
905 if (!cdir && fullpath)
912 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
913 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
914 ntohl(cdir->d_did), cfrombstr(cdir->d_fullpath));
921 * Free the queue with invalid struct dirs
923 * This gets called at the end of every AFP func.
925 void dir_free_invalid_q(void)
928 while ((dir = (struct dir *)dequeue(invalid_dircache_entries)))
933 * @brief Remove a dir from a cache and queue it for freeing
935 * 1. Check if the dir is locked or has opened forks
936 * 2. Remove it from the cache
937 * 3. Queue it for removal
938 * 4. If it's a request to remove curdir, mark curdir as invalid
939 * 5. Mark it as invalid
941 * @param (r) pointer to struct vol
942 * @param (rw) pointer to struct dir
944 int dir_remove(const struct vol *vol, struct dir *dir)
949 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
952 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
953 ntohl(dir->d_did), cfrombstr(dir->d_u_name));
955 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 2 */
956 enqueue(invalid_dircache_entries, dir); /* 3 */
958 if (curdir == dir) /* 4 */
961 dir->d_did = CNID_INVALID; /* 5 */
968 * @brief Modify a struct dir, adjust cache
970 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
971 * If given new_uname == new_mname, new_uname will point to new_mname.
973 * @param vol (r) pointer to struct vol
974 * @param dir (rw) pointer to struct dir
975 * @param pdid (r) new parent DID
976 * @param did (r) new DID
977 * @param new_mname (r) new mac-name
978 * @param new_uname (r) new unix-name
979 * @param pdir_fullpath (r) new fullpath of parent dir
981 int dir_modify(const struct vol *vol,
985 const char *new_mname,
986 const char *new_uname,
987 bstring pdir_fullpath)
991 /* Remove it from the cache */
992 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
1000 /* free uname if it's not the same as mname */
1001 if (dir->d_m_name != dir->d_u_name)
1002 bdestroy(dir->d_u_name);
1004 if (new_uname == NULL)
1005 new_uname = new_mname;
1007 /* assign new name */
1008 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
1009 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1013 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
1014 dir->d_u_name = dir->d_m_name;
1016 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
1017 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1023 if (pdir_fullpath) {
1024 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
1026 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
1028 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
1032 if (dir->d_m_name_ucs2)
1033 free(dir->d_m_name_ucs2);
1034 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))
1035 dir->d_m_name_ucs2 = NULL;
1037 /* Re-add it to the cache */
1038 if ((dircache_add(vol, dir)) != 0) {
1040 AFP_PANIC("dir_modify");
1048 * @brief Resolve a catalog node name path
1050 * 1. Evaluate path type
1051 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
1052 * path from dirname, so eg getdirparams has sth it can chew on. curdir
1053 * is dir parent then. All this is done in path_from_dir().
1054 * 3. Parse next cnode name in path, cases:
1055 * 4. single "\0" -> do nothing
1056 * 5. two or more consecutive "\0" -> chdir("..") one or more times
1057 * 6. cnode name -> copy it to path.m_name
1058 * 7. Get unix name from mac name
1059 * 8. Special handling of request with did 1
1060 * 9. stat the cnode name
1061 * 10. If it's not there, it's probably an afp_createfile|dir,
1062 * return with curdir = dir parent, struct path = dirname
1063 * 11. If it's there and it's a file, it must should be the last element of the requested
1064 * path. Return with curdir = cnode name parent dir, struct path = filename
1065 * 12. Treat symlinks like files, dont follow them
1066 * 13. If it's a dir:
1067 * 14. Search the dircache for it
1068 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
1069 * 16. chdir into the dir and
1070 * 17. set m_name to the mac equivalent of "."
1073 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1075 static char path[ MAXPATHLEN + 1];
1076 static struct path ret;
1086 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstr(dir->d_fullpath));
1089 afp_errno = AFPERR_NOOBJ;
1090 memset(&ret, 0, sizeof(ret));
1092 switch (ret.m_type = *data) { /* 1 */
1095 len = (unsigned char) *data++;
1097 if (vol->v_obj->afp_version >= 30) {
1103 if (vol->v_obj->afp_version >= 30) {
1105 memcpy(&hint, data, sizeof(hint));
1107 data += sizeof(hint);
1109 memcpy(&len16, data, sizeof(len16));
1115 /* else it's an error */
1117 afp_errno = AFPERR_PARAM;
1120 *cpath += len + size;
1125 if (movecwd(vol, dir) < 0 ) {
1126 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1127 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1129 return path_from_dir(vol, dir, &ret);
1134 while (len) { /* 3 */
1135 if (*data == 0) { /* 4 or 5 */
1138 while (len > 0 && *data == 0) { /* 5 */
1139 /* chdir to parrent dir */
1140 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1142 if (movecwd( vol, dir ) < 0 ) {
1143 dir_remove(vol, dir);
1153 for ( p = path; *data != 0 && len > 0; len-- ) {
1155 if (p > &path[UTF8FILELEN_EARLY]) { /* FIXME safeguard, limit of early Mac OS X */
1156 afp_errno = AFPERR_PARAM;
1160 *p = 0; /* Terminate string */
1163 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1164 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1168 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstr(dir->d_fullpath), ret.u_name);
1170 /* Prevent access to our special folders like .AppleDouble */
1171 if (check_name(vol, ret.u_name)) {
1172 /* the name is illegal */
1173 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1174 afp_errno = AFPERR_PARAM;
1175 if (vol->v_obj->options.flags & OPTION_VETOMSG) {
1176 bstring message = bformat("Attempt to access vetoed file or directory \"%s\" in directory \"%s\"",
1177 ret.u_name, bdata(dir->d_u_name));
1178 if (setmessage(bdata(message)) == 0)
1179 /* Client may make multiple attempts, only send the message the first time */
1180 kill(getpid(), SIGUSR2);
1186 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1188 * Special case: CNID 1
1189 * root parent (did 1) has one child: the volume. Requests for did=1 with
1190 * some <name> must check against the volume name.
1192 if ((strcmp(cfrombstr(vol->v_root->d_m_name), ret.m_name)) == 0)
1198 * CNID != 1, eg. most of the times we take this way.
1199 * Now check if current path-part is a file or dir:
1200 * o if it's dir we have to step into it
1201 * o if it's a file we expect it to be the last part of the requested path
1202 * and thus call continue which should terminate the while loop because
1205 if (of_stat(vol, &ret) != 0) { /* 9 */
1207 * ret.u_name doesn't exist, might be afp_createfile|dir
1208 * that means it should have been the last part
1211 /* it wasn't the last part, so we have a bogus path request */
1212 afp_errno = AFPERR_NOOBJ;
1216 * this will terminate clean in while (1) because len == 0,
1217 * probably afp_createfile|dir
1219 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}",
1220 cfrombstr(dir->d_fullpath), ret.u_name);
1224 switch (ret.st.st_mode & S_IFMT) {
1225 case S_IFREG: /* 11 */
1226 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}",
1227 cfrombstr(dir->d_fullpath), ret.u_name);
1229 /* it wasn't the last part, so we have a bogus path request */
1230 afp_errno = AFPERR_PARAM;
1233 continue; /* continues while loop */
1234 case S_IFLNK: /* 12 */
1235 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}",
1236 cfrombstr(dir->d_fullpath), ret.u_name);
1238 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}",
1239 cfrombstr(dir->d_fullpath), ret.u_name);
1240 afp_errno = AFPERR_PARAM;
1243 continue; /* continues while loop */
1244 case S_IFDIR: /* 13 */
1247 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1248 afp_errno = AFPERR_NODIR;
1252 /* Search the cache */
1253 int unamelen = strlen(ret.u_name);
1254 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1256 /* Not in cache, create one */
1257 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1258 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1259 ntohl(dir->d_did), ret.u_name, getcwdpath());
1263 } /* if/else cnid==1 */
1265 /* Now chdir to the evaluated dir */
1266 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1267 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1268 cfrombstr(curdir->d_fullpath), cfrombstr(cdir->d_fullpath), strerror(errno));
1270 return path_from_dir(vol, cdir, &ret);
1275 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1278 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1279 afp_errno = AFPERR_DID1;
1283 if (ret.m_name[0] == 0) {
1284 /* Last part was a dir */
1285 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1289 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1290 cfrombstr(dir->d_fullpath),
1291 cfrombstr(curdir->d_fullpath),
1298 * @brief chdir() to dir
1300 * @param vol (r) pointer to struct vol
1301 * @param dir (r) pointer to struct dir
1303 * @returns 0 on success, -1 on error with afp_errno set appropiately
1305 int movecwd(const struct vol *vol, struct dir *dir)
1312 LOG(log_maxdebug, logtype_afpd, "movecwd: from: curdir:\"%s\", cwd:\"%s\"",
1313 curdir ? cfrombstr(curdir->d_fullpath) : "INVALID", getcwdpath());
1315 if (dir->d_did == DIRDID_ROOT_PARENT) {
1316 curdir = &rootParent;
1320 LOG(log_debug, logtype_afpd, "movecwd(to: did: %u, \"%s\")",
1321 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1323 if ((ret = ochdir(cfrombstr(dir->d_fullpath), vol_syml_opt(vol))) != 0 ) {
1324 LOG(log_debug, logtype_afpd, "movecwd(\"%s\"): %s",
1325 cfrombstr(dir->d_fullpath), strerror(errno));
1327 /* p is a symlink or getcwd failed */
1328 afp_errno = AFPERR_BADTYPE;
1330 if (chdir(vol->v_path ) < 0) {
1331 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1332 /* XXX what do we do here? */
1334 curdir = vol->v_root;
1341 afp_errno = AFPERR_ACCESS;
1344 afp_errno = AFPERR_NOOBJ;
1354 * We can't use unix file's perm to support Apple's inherited protection modes.
1355 * If we aren't the file's owner we can't change its perms when moving it and smb
1356 * nfs,... don't even try.
1358 int check_access(const AFPObj *obj, struct vol *vol, char *path, int mode)
1367 accessmode(obj, vol, p, &ma, curdir, NULL);
1368 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1370 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1376 /* --------------------- */
1377 int file_access(const AFPObj *obj, struct vol *vol, struct path *path, int mode)
1381 accessmode(obj, vol, path->u_name, &ma, curdir, &path->st);
1383 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): mapped user mode: 0x%02x",
1384 path->u_name, ma.ma_user);
1386 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE)) {
1387 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): write access denied", path->u_name);
1390 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD)) {
1391 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): read access denied", path->u_name);
1398 /* --------------------- */
1399 void setdiroffcnt(struct dir *dir, struct stat *st, uint32_t count)
1401 dir->d_offcnt = count;
1402 dir->d_ctime = st->st_ctime;
1403 dir->d_flags &= ~DIRF_CNID;
1407 /* ---------------------
1408 * is our cached also for reenumerate id?
1410 int dirreenumerate(struct dir *dir, struct stat *st)
1412 return st->st_ctime == dir->d_ctime && (dir->d_flags & DIRF_CNID);
1415 /* ------------------------------
1417 (name, dir) with curdir:name == dir, from afp_enumerate
1420 int getdirparams(const AFPObj *obj,
1421 const struct vol *vol,
1422 uint16_t bitmap, struct path *s_path,
1424 char *buf, size_t *buflen )
1428 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1429 int bit = 0, isad = 0;
1435 struct stat *st = &s_path->st;
1436 char *upath = s_path->u_name;
1438 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1439 (1 << DIRPBIT_CDATE) |
1440 (1 << DIRPBIT_MDATE) |
1441 (1 << DIRPBIT_BDATE) |
1442 (1 << DIRPBIT_FINFO)))) {
1445 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1447 if (ad.ad_mdp->adf_flags & O_CREAT) {
1448 /* We just created it */
1449 if (s_path->m_name == NULL) {
1450 if ((s_path->m_name = utompath(vol,
1453 utf8_encoding(obj))) == NULL) {
1454 LOG(log_error, logtype_afpd,
1455 "getdirparams(\"%s\"): can't assign macname",
1456 cfrombstr(dir->d_fullpath));
1460 ad_setname(&ad, s_path->m_name);
1475 while ( bitmap != 0 ) {
1476 while (( bitmap & 1 ) == 0 ) {
1484 ad_getattr(&ad, &ashort);
1485 } else if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1486 ashort = htons(ATTRBIT_INVISIBLE);
1489 ashort &= ~htons(vol->v_ignattr);
1490 memcpy( data, &ashort, sizeof( ashort ));
1491 data += sizeof( ashort );
1495 memcpy( data, &pdid, sizeof( pdid ));
1496 data += sizeof( pdid );
1497 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1498 s_path->u_name, ntohl(pdid));
1501 case DIRPBIT_CDATE :
1502 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1503 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1504 memcpy( data, &aint, sizeof( aint ));
1505 data += sizeof( aint );
1508 case DIRPBIT_MDATE :
1509 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1510 memcpy( data, &aint, sizeof( aint ));
1511 data += sizeof( aint );
1514 case DIRPBIT_BDATE :
1515 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1516 aint = AD_DATE_START;
1517 memcpy( data, &aint, sizeof( aint ));
1518 data += sizeof( aint );
1521 case DIRPBIT_FINFO :
1523 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1524 } else { /* no appledouble */
1525 memset( data, 0, 32 );
1526 /* dot files are by default visible */
1527 if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1528 ashort = htons(FINDERINFO_INVISIBLE);
1529 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1535 case DIRPBIT_LNAME :
1536 if (dir->d_m_name) /* root of parent can have a null name */
1539 memset(data, 0, sizeof(uint16_t));
1540 data += sizeof( uint16_t );
1543 case DIRPBIT_SNAME :
1544 memset(data, 0, sizeof(uint16_t));
1545 data += sizeof( uint16_t );
1549 memcpy( data, &dir->d_did, sizeof( aint ));
1550 data += sizeof( aint );
1551 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1552 s_path->u_name, ntohl(dir->d_did));
1555 case DIRPBIT_OFFCNT :
1557 /* this needs to handle current directory access rights */
1558 if (diroffcnt(dir, st)) {
1559 ashort = (dir->d_offcnt > 0xffff)?0xffff:dir->d_offcnt;
1561 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1562 setdiroffcnt(dir, st, ret);
1563 ashort = (dir->d_offcnt > 0xffff)?0xffff:dir->d_offcnt;
1565 ashort = htons( ashort );
1566 memcpy( data, &ashort, sizeof( ashort ));
1567 data += sizeof( ashort );
1571 aint = htonl(st->st_uid);
1572 memcpy( data, &aint, sizeof( aint ));
1573 data += sizeof( aint );
1577 aint = htonl(st->st_gid);
1578 memcpy( data, &aint, sizeof( aint ));
1579 data += sizeof( aint );
1582 case DIRPBIT_ACCESS :
1583 accessmode(obj, vol, upath, &ma, dir , st);
1585 *data++ = ma.ma_user;
1586 *data++ = ma.ma_world;
1587 *data++ = ma.ma_group;
1588 *data++ = ma.ma_owner;
1591 /* Client has requested the ProDOS information block.
1592 Just pass back the same basic block for all
1593 directories. <shirsch@ibm.net> */
1594 case DIRPBIT_PDINFO :
1595 if (obj->afp_version >= 30) { /* UTF8 name */
1596 utf8 = kTextEncodingUTF8;
1597 if (dir->d_m_name) /* root of parent can have a null name */
1600 memset(data, 0, sizeof(uint16_t));
1601 data += sizeof( uint16_t );
1603 memcpy(data, &aint, sizeof( aint ));
1604 data += sizeof( aint );
1606 else { /* ProDOS Info Block */
1609 ashort = htons( 0x0200 );
1610 memcpy( data, &ashort, sizeof( ashort ));
1611 data += sizeof( ashort );
1612 memset( data, 0, sizeof( ashort ));
1613 data += sizeof( ashort );
1617 case DIRPBIT_UNIXPR :
1618 /* accessmode may change st_mode with ACLs */
1619 accessmode(obj, vol, upath, &ma, dir, st);
1621 aint = htonl(st->st_uid);
1622 memcpy( data, &aint, sizeof( aint ));
1623 data += sizeof( aint );
1624 aint = htonl(st->st_gid);
1625 memcpy( data, &aint, sizeof( aint ));
1626 data += sizeof( aint );
1629 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1630 memcpy( data, &aint, sizeof( aint ));
1631 data += sizeof( aint );
1633 *data++ = ma.ma_user;
1634 *data++ = ma.ma_world;
1635 *data++ = ma.ma_group;
1636 *data++ = ma.ma_owner;
1641 ad_close(&ad, ADFLAGS_HF);
1643 return( AFPERR_BITMAP );
1649 ashort = htons( data - buf );
1650 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1651 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, 0);
1653 if ( utf_nameoff ) {
1654 ashort = htons( data - buf );
1655 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1656 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, utf8);
1659 ad_close(&ad, ADFLAGS_HF);
1661 *buflen = data - buf;
1665 /* ----------------------------- */
1666 int path_error(struct path *path, int error)
1668 /* - a dir with access error
1669 * - no error it's a file
1672 if (path_isadir(path))
1674 if (path->st_valid && path->st_errno)
1676 return AFPERR_BADTYPE ;
1679 /* ----------------------------- */
1680 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1685 uint16_t vid, bitmap;
1691 memcpy( &vid, ibuf, sizeof( vid ));
1692 ibuf += sizeof( vid );
1694 if (NULL == ( vol = getvolbyvid( vid )) ) {
1695 return( AFPERR_PARAM );
1698 if (vol->v_flags & AFPVOL_RO)
1699 return AFPERR_VLOCK;
1701 memcpy( &did, ibuf, sizeof( did ));
1702 ibuf += sizeof( int );
1704 if (NULL == ( dir = dirlookup( vol, did )) ) {
1708 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1709 bitmap = ntohs( bitmap );
1710 ibuf += sizeof( bitmap );
1712 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1713 return get_afp_errno(AFPERR_NOOBJ);
1716 if ( *path->m_name != '\0' ) {
1717 rc = path_error(path, AFPERR_NOOBJ);
1718 /* maybe we are trying to set perms back */
1719 if (rc != AFPERR_ACCESS)
1724 * If ibuf is odd, make it even.
1726 if ((u_long)ibuf & 1 ) {
1730 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1731 setvoltime(obj, vol );
1737 * assume path == '\0' eg. it's a directory in canonical form
1739 int setdirparams(struct vol *vol, struct path *path, uint16_t d_bitmap, char *buf )
1751 uint16_t ashort, bshort, oshort;
1753 int change_mdate = 0;
1754 int change_parent_mdate = 0;
1756 uint16_t bitmap = d_bitmap;
1757 u_char finder_buf[32];
1760 bool set_upriv = false, set_maccess = false;
1762 LOG(log_debug, logtype_afpd, "setdirparams(\"%s\", bitmap: %02x)", path->u_name, bitmap);
1765 upath = path->u_name;
1767 while ( bitmap != 0 ) {
1768 while (( bitmap & 1 ) == 0 ) {
1776 memcpy( &ashort, buf, sizeof( ashort ));
1777 buf += sizeof( ashort );
1779 case DIRPBIT_CDATE :
1781 memcpy(&cdate, buf, sizeof(cdate));
1782 buf += sizeof( cdate );
1784 case DIRPBIT_MDATE :
1785 memcpy(&newdate, buf, sizeof(newdate));
1786 buf += sizeof( newdate );
1788 case DIRPBIT_BDATE :
1790 memcpy(&bdate, buf, sizeof(bdate));
1791 buf += sizeof( bdate );
1793 case DIRPBIT_FINFO :
1795 memcpy( finder_buf, buf, 32 );
1798 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1799 memcpy( &owner, buf, sizeof(owner));
1800 buf += sizeof( owner );
1803 memcpy( &group, buf, sizeof( group ));
1804 buf += sizeof( group );
1806 case DIRPBIT_ACCESS :
1809 ma.ma_user = *buf++;
1810 ma.ma_world = *buf++;
1811 ma.ma_group = *buf++;
1812 ma.ma_owner = *buf++;
1813 mpriv = mtoumode( &ma ) | vol->v_dperm;
1815 /* Ignore what the client thinks we should do to the
1816 ProDOS information block. Skip over the data and
1817 report nothing amiss. <shirsch@ibm.net> */
1818 case DIRPBIT_PDINFO :
1819 if (vol->v_obj->afp_version < 30) {
1823 err = AFPERR_BITMAP;
1827 case DIRPBIT_UNIXPR :
1828 if (vol_unix_priv(vol)) {
1830 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1831 buf += sizeof( owner );
1832 memcpy( &group, buf, sizeof( group ));
1833 buf += sizeof( group );
1836 memcpy( &upriv, buf, sizeof( upriv ));
1837 buf += sizeof( upriv );
1838 upriv = ntohl (upriv) | vol->v_dperm;
1843 err = AFPERR_BITMAP;
1852 if (d_bitmap & ((1<<DIRPBIT_ATTR) | (1<<DIRPBIT_CDATE) | (1<<DIRPBIT_BDATE) | (1<<DIRPBIT_FINFO))) {
1854 if (ad_open(&ad, upath, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) != 0) {
1855 LOG(log_debug, logtype_afpd, "setdirparams(\"%s\", bitmap: %02x): need adouble", path->u_name, d_bitmap);
1856 return AFPERR_ACCESS;
1858 if ((ad_get_MD_flags(&ad) & O_CREAT)) {
1859 ad_setname(&ad, cfrombstr(curdir->d_m_name));
1866 while ( bitmap != 0 ) {
1867 while (( bitmap & 1 ) == 0 ) {
1875 ad_getattr(&ad, &bshort);
1877 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1878 ashort &= ~htons(vol->v_ignattr);
1879 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1883 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1884 change_parent_mdate = 1;
1885 ad_setattr(&ad, bshort);
1888 case DIRPBIT_CDATE :
1890 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1893 case DIRPBIT_MDATE :
1895 case DIRPBIT_BDATE :
1897 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1900 case DIRPBIT_FINFO :
1902 /* Fixes #2802236 */
1904 memcpy(&fflags, finder_buf + FINDERINFO_FRFLAGOFF, sizeof(uint16_t));
1905 fflags &= htons(~FINDERINFO_ISHARED);
1906 memcpy(finder_buf + FINDERINFO_FRFLAGOFF, &fflags, sizeof(uint16_t));
1909 if ( dir->d_did == DIRDID_ROOT ) {
1911 * Alright, we admit it, this is *really* sick!
1912 * The 4 bytes that we don't copy, when we're dealing
1913 * with the root of a volume, are the directory's
1914 * location information. This eliminates that annoying
1915 * behavior one sees when mounting above another mount
1918 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1919 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1921 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1925 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1926 if ( (dir->d_did == DIRDID_ROOT) &&
1927 (setdeskowner(vol, ntohl(owner), -1 ) < 0)) {
1928 err = set_dir_errors(path, "setdeskowner", errno);
1929 if (isad && err == AFPERR_PARAM) {
1930 err = AFP_OK; /* ???*/
1933 goto setdirparam_done;
1936 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1937 err = set_dir_errors(path, "setdirowner", errno);
1938 goto setdirparam_done;
1942 if (dir->d_did == DIRDID_ROOT)
1943 setdeskowner(vol, -1, ntohl(group) );
1944 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1945 err = set_dir_errors(path, "setdirowner", errno);
1946 goto setdirparam_done;
1949 case DIRPBIT_ACCESS :
1951 case DIRPBIT_PDINFO :
1952 if (vol->v_obj->afp_version >= 30) {
1953 err = AFPERR_BITMAP;
1954 goto setdirparam_done;
1957 case DIRPBIT_UNIXPR :
1958 if (!vol_unix_priv(vol)) {
1959 err = AFPERR_BITMAP;
1960 goto setdirparam_done;
1964 err = AFPERR_BITMAP;
1965 goto setdirparam_done;
1974 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1975 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1979 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1980 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1985 if (path->st_valid && !path->st_errno) {
1986 struct stat *st = &path->st;
1987 if (dir && dir->d_pdid) {
1988 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1991 if (ad_flush(&ad) != 0) {
1994 err = AFPERR_ACCESS;
2001 ad_close(&ad, ADFLAGS_HF);
2004 if (err == AFP_OK) {
2005 if (set_maccess == true) {
2006 if (dir->d_did == DIRDID_ROOT) {
2007 setdeskmode(vol, mpriv);
2008 if (!dir_rx_set(mpriv)) {
2009 /* we can't remove read and search for owner on volume root */
2010 err = AFPERR_ACCESS;
2014 if (setdirunixmode(vol, upath, mpriv) < 0) {
2015 LOG(log_info, logtype_afpd, "setdirparams(\"%s\"): setdirunixmode: %s",
2016 fullpathname(upath), strerror(errno));
2017 err = set_dir_errors(path, "setdirmode", errno);
2020 if ((set_upriv == true) && vol_unix_priv(vol)) {
2021 if (dir->d_did == DIRDID_ROOT) {
2022 if (!dir_rx_set(upriv)) {
2023 /* we can't remove read and search for owner on volume root */
2024 err = AFPERR_ACCESS;
2027 setdeskowner(vol, -1, ntohl(group));
2028 setdeskmode(vol, upriv);
2031 if (setdirowner(vol, upath, -1, ntohl(group)) < 0) {
2032 LOG(log_info, logtype_afpd, "setdirparams(\"%s\"): setdirowner: %s",
2033 fullpathname(upath), strerror(errno));
2034 err = set_dir_errors(path, "setdirowner", errno);
2038 if (setdirunixmode(vol, upath, upriv) < 0) {
2039 LOG(log_info, logtype_afpd, "setdirparams(\"%s\"): setdirunixmode: %s",
2040 fullpathname(upath), strerror(errno));
2041 err = set_dir_errors(path, "setdirunixmode", errno);
2047 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2048 && gettimeofday(&tv, NULL) == 0) {
2049 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
2050 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2051 /* be careful with bitmap because now dir is null */
2052 bitmap = 1<<DIRPBIT_MDATE;
2053 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2054 /* should we reset curdir ?*/
2060 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2074 memcpy( &vid, ibuf, sizeof( vid ));
2075 ibuf += sizeof( vid );
2076 if (NULL == (vol = getvolbyvid( vid )) ) {
2077 return( AFPERR_PARAM );
2080 memcpy( &did, ibuf, sizeof( did ));
2081 ibuf += sizeof( did );
2085 * if it's CNID 2 our only choice to meet the specs is call sync.
2086 * For any other CNID just sync that dir. To my knowledge the
2087 * intended use of FPSyncDir is to sync the volume so all we're
2088 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2091 if ( ntohl(did) == 2 ) {
2094 if (NULL == ( dir = dirlookup( vol, did )) ) {
2095 return afp_errno; /* was AFPERR_NOOBJ */
2098 if (movecwd( vol, dir ) < 0 )
2099 return ( AFPERR_NOOBJ );
2102 * Assuming only OSens that have dirfd also may require fsyncing directories
2103 * in order to flush metadata e.g. Linux.
2107 if (NULL == ( dp = opendir( "." )) ) {
2110 return( AFPERR_NOOBJ );
2112 return( AFPERR_ACCESS );
2114 return( AFPERR_PARAM );
2118 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2121 if ( fsync ( dfd ) < 0 )
2122 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2123 dir->d_u_name, strerror(errno) );
2124 closedir(dp); /* closes dfd too */
2127 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2130 return( AFPERR_NOOBJ );
2132 return( AFPERR_ACCESS );
2134 return( AFPERR_PARAM );
2138 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2139 vol->ad_path(".", ADFLAGS_DIR) );
2141 if ( fsync(dfd) < 0 )
2142 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2143 vol->ad_path(cfrombstr(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2150 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2156 struct path *s_path;
2164 memcpy( &vid, ibuf, sizeof( vid ));
2165 ibuf += sizeof( vid );
2166 if (NULL == ( vol = getvolbyvid( vid )) ) {
2167 return( AFPERR_PARAM );
2170 if (vol->v_flags & AFPVOL_RO)
2171 return AFPERR_VLOCK;
2173 memcpy( &did, ibuf, sizeof( did ));
2174 ibuf += sizeof( did );
2175 if (NULL == ( dir = dirlookup( vol, did )) ) {
2176 return afp_errno; /* was AFPERR_NOOBJ */
2178 /* for concurrent access we need to be sure we are not in the
2179 * folder we want to create...
2183 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2184 return get_afp_errno(AFPERR_PARAM);
2186 /* cname was able to move curdir to it! */
2187 if (*s_path->m_name == '\0')
2188 return AFPERR_EXIST;
2190 upath = s_path->u_name;
2192 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2196 if (of_stat(vol, s_path) < 0) {
2202 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2206 if ( movecwd( vol, dir ) < 0 ) {
2207 return( AFPERR_PARAM );
2211 if (ad_open(&ad, ".", ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) < 0) {
2212 return( AFPERR_ACCESS );
2214 ad_setname(&ad, s_path->m_name);
2215 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2217 fce_register(FCE_DIR_CREATE, bdata(curdir->d_fullpath), NULL, fce_dir);
2220 ad_close(&ad, ADFLAGS_HF);
2222 memcpy( rbuf, &dir->d_did, sizeof( uint32_t ));
2223 *rbuflen = sizeof( uint32_t );
2224 setvoltime(obj, vol );
2229 * dst new unix filename (not a pathname)
2230 * newname new mac name
2232 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2234 int renamedir(struct vol *vol,
2239 struct dir *newparent,
2245 /* existence check moved to afp_moveandrename */
2246 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2249 return( AFPERR_NOOBJ );
2251 return( AFPERR_ACCESS );
2253 return AFPERR_VLOCK;
2255 /* tried to move directory into a subdirectory of itself */
2256 return AFPERR_CANTMOVE;
2258 /* this needs to copy and delete. bleah. that means we have
2259 * to deal with entire directory hierarchies. */
2260 if ((err = copydir(vol, newparent, dirfd, src, dst)) < 0) {
2261 deletedir(vol, -1, dst);
2264 if ((err = deletedir(vol, dirfd, src)) < 0)
2268 return( AFPERR_PARAM );
2272 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2276 if (ad_open(&ad, dst, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR) == 0) {
2277 ad_setname(&ad, newname);
2279 ad_close(&ad, ADFLAGS_HF);
2285 /* delete an empty directory */
2286 int deletecurdir(struct vol *vol)
2290 struct dir *fdir, *pdir;
2295 if ((pdir = dirlookup(vol, curdir->d_pdid)) == NULL) {
2296 return( AFPERR_ACCESS );
2302 /* we never want to create a resource fork here, we are going to delete it */
2303 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2305 ad_getattr(&ad, &ashort);
2306 ad_close(&ad, ADFLAGS_HF);
2307 if (!(vol->v_ignattr & ATTRBIT_NODELETE) && (ashort & htons(ATTRBIT_NODELETE))) {
2308 return AFPERR_OLOCK;
2311 err = vol->vfs->vfs_deletecurdir(vol);
2313 LOG(log_error, logtype_afpd, "deletecurdir: error deleting AppleDouble files in \"%s\"",
2314 cfrombstr(curdir->d_fullpath));
2318 if (movecwd(vol, pdir) < 0) {
2323 LOG(log_debug, logtype_afpd, "deletecurdir: moved to \"%s\"",
2324 cfrombstr(curdir->d_fullpath));
2326 err = netatalk_rmdir_all_errors(-1, cfrombstr(fdir->d_u_name));
2332 case AFPERR_DIRNEMPT:
2333 if (delete_vetoed_files(vol, bdata(fdir->d_u_name), false) != 0)
2338 LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): netatalk_rmdir_all_errors error",
2339 cfrombstr(curdir->d_fullpath));
2343 AFP_CNID_START("cnid_delete");
2344 cnid_delete(vol->v_cdb, fdir->d_did);
2347 dir_remove( vol, fdir );
2353 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2363 sfunc = (unsigned char) *ibuf++;
2366 if (sfunc >= 3 && sfunc <= 6) {
2367 if (obj->afp_version < 30) {
2368 return( AFPERR_PARAM );
2375 case 3 :/* unicode */
2376 memcpy( &id, ibuf, sizeof( id ));
2379 if (( pw = getpwuid( id )) == NULL ) {
2380 return( AFPERR_NOITEM );
2382 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2383 pw->pw_name, -1, &name);
2390 case 4 : /* unicode */
2391 memcpy( &id, ibuf, sizeof( id ));
2394 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2395 return( AFPERR_NOITEM );
2397 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2398 gr->gr_name, -1, &name);
2405 case 5 : /* UUID -> username */
2406 case 6 : /* UUID -> groupname */
2407 if ((obj->afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2408 return AFPERR_PARAM;
2409 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2411 len = getnamefromuuid((unsigned char*) ibuf, &name, &type);
2412 if (len != 0) /* its a error code, not len */
2413 return AFPERR_NOITEM;
2416 if (( pw = getpwnam( name )) == NULL )
2417 return( AFPERR_NOITEM );
2418 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2419 id = htonl(UUID_USER);
2420 memcpy( rbuf, &id, sizeof( id ));
2421 id = htonl( pw->pw_uid);
2422 rbuf += sizeof( id );
2423 memcpy( rbuf, &id, sizeof( id ));
2424 rbuf += sizeof( id );
2425 *rbuflen = 2 * sizeof( id );
2428 if (( gr = getgrnam( name )) == NULL )
2429 return( AFPERR_NOITEM );
2430 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2431 id = htonl(UUID_GROUP);
2432 memcpy( rbuf, &id, sizeof( id ));
2433 rbuf += sizeof( id );
2434 id = htonl( gr->gr_gid);
2435 memcpy( rbuf, &id, sizeof( id ));
2436 rbuf += sizeof( id );
2437 *rbuflen = 2 * sizeof( id );
2445 return( AFPERR_PARAM );
2449 len = strlen( name );
2452 uint16_t tp = htons(len);
2453 memcpy(rbuf, &tp, sizeof(tp));
2462 memcpy( rbuf, name, len );
2470 int afp_mapname(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2479 sfunc = (unsigned char) *ibuf++;
2481 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d", sfunc);
2484 case 2 : /* unicode */
2485 if (obj->afp_version < 30) {
2486 return( AFPERR_PARAM );
2488 memcpy(&ulen, ibuf, sizeof(ulen));
2491 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2495 len = (unsigned char) *ibuf++;
2497 case 5 : /* username -> UUID */
2498 case 6 : /* groupname -> UUID */
2499 if ((obj->afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2500 return AFPERR_PARAM;
2501 memcpy(&ulen, ibuf, sizeof(ulen));
2506 return( AFPERR_PARAM );
2512 return AFPERR_PARAM;
2515 case 1 : /* unicode */
2517 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2518 return( AFPERR_NOITEM );
2522 memcpy( rbuf, &id, sizeof( id ));
2523 *rbuflen = sizeof( id );
2526 case 2 : /* unicode */
2528 LOG(log_debug, logtype_afpd, "afp_mapname: getgrnam for name: %s",ibuf);
2529 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2530 return( AFPERR_NOITEM );
2533 LOG(log_debug, logtype_afpd, "afp_mapname: getgrnam for name: %s -> id: %d",ibuf, id);
2535 memcpy( rbuf, &id, sizeof( id ));
2536 *rbuflen = sizeof( id );
2538 case 5 : /* username -> UUID */
2539 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2540 if (0 != getuuidfromname(ibuf, UUID_USER, (unsigned char *)rbuf))
2541 return AFPERR_NOITEM;
2542 *rbuflen = UUID_BINSIZE;
2544 case 6 : /* groupname -> UUID */
2545 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2546 if (0 != getuuidfromname(ibuf, UUID_GROUP, (unsigned char *)rbuf))
2547 return AFPERR_NOITEM;
2548 *rbuflen = UUID_BINSIZE;
2555 /* ------------------------------------
2556 variable DID support
2558 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2569 /* do nothing as dids are static for the life of the process. */
2573 memcpy(&vid, ibuf, sizeof( vid ));
2574 ibuf += sizeof( vid );
2575 if (( vol = getvolbyvid( vid )) == NULL ) {
2576 return( AFPERR_PARAM );
2579 memcpy( &did, ibuf, sizeof( did ));
2580 ibuf += sizeof( did );
2581 if (( dir = dirlookup( vol, did )) == NULL ) {
2582 return( AFPERR_PARAM );
2585 /* dir_remove -- deletedid */
2591 /* did creation gets done automatically
2592 * there's a pb again with case but move it to cname
2594 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2597 struct dir *parentdir;
2605 memcpy(&vid, ibuf, sizeof(vid));
2606 ibuf += sizeof( vid );
2608 if (NULL == ( vol = getvolbyvid( vid )) ) {
2609 return( AFPERR_PARAM );
2612 memcpy(&did, ibuf, sizeof(did));
2613 ibuf += sizeof(did);
2615 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2619 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2620 return get_afp_errno(AFPERR_PARAM);
2623 if ( *path->m_name != '\0' ) {
2624 return path_error(path, AFPERR_NOOBJ);
2627 if ( !path->st_valid && of_stat(vol, path) < 0 ) {
2628 return( AFPERR_NOOBJ );
2630 if ( path->st_errno ) {
2631 return( AFPERR_NOOBJ );
2634 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2635 *rbuflen = sizeof(curdir->d_did);