2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #include <sys/param.h>
21 #include <atalk/adouble.h>
22 #include <atalk/vfs.h>
23 #include <atalk/afp.h>
24 #include <atalk/util.h>
25 #include <atalk/cnid.h>
26 #include <atalk/logger.h>
27 #include <atalk/uuid.h>
28 #include <atalk/unix.h>
29 #include <atalk/bstrlib.h>
30 #include <atalk/bstradd.h>
31 #include <atalk/errchk.h>
33 #include "directory.h"
47 * FIXMEs, loose ends after the dircache rewrite:
48 * o merge dircache_search_by_name and dir_add ??
49 * o case-insensitivity is gone from cname
53 /*******************************************************************************************
55 ******************************************************************************************/
58 /* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
59 struct dir rootParent = {
60 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
61 NULL, 0, 0, /* qidx_node, ctime, d_flags */
62 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
64 struct dir *curdir = &rootParent;
65 struct path Cur_Path = {
70 NULL,/* struct dir * */
71 0, /* stat is not set */
77 * dir_remove queues struct dirs to be freed here. We can't just delete them immeidately
78 * eg in dircache_search_by_id, because a caller somewhere up the stack might be
81 * - we mark it as invalid by setting d_did to CNID_INVALID (ie 0)
82 * - queue it in "invalid_dircache_entries" queue
83 * - which is finally freed at the end of every AFP func in afp_dsi.c.
85 q_t *invalid_dircache_entries;
88 /*******************************************************************************************
90 ******************************************************************************************/
93 /* -------------------------
94 appledouble mkdir afp error code.
96 static int netatalk_mkdir(const struct vol *vol, const char *name)
101 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
102 if (lstat(".", &st) < 0)
104 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
105 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
106 name, st.st_mode, vol->v_umask);
108 ret = mkdir(name, mode);
110 ret = ad_mkdir(name, DIRBITS | 0777);
116 return( AFPERR_NOOBJ );
118 return( AFPERR_VLOCK );
121 return( AFPERR_ACCESS );
123 return( AFPERR_EXIST );
126 return( AFPERR_DFULL );
128 return( AFPERR_PARAM );
134 /* ------------------- */
135 static int deletedir(int dirfd, char *dir)
137 char path[MAXPATHLEN + 1];
145 if ((len = strlen(dir)) +2 > sizeof(path))
149 if ((dp = opendirat(dirfd, dir)) == NULL)
155 remain = sizeof(path) -len -1;
156 while ((de = readdir(dp)) && err == AFP_OK) {
157 /* skip this and previous directory */
158 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
161 if (strlen(de->d_name) > remain) {
165 strcpy(path + len, de->d_name);
166 if (lstatat(dirfd, path, &st)) {
169 if (S_ISDIR(st.st_mode)) {
170 err = deletedir(dirfd, path);
172 err = netatalk_unlinkat(dirfd, path);
177 /* okay. the directory is empty. delete it. note: we already got rid
180 err = netatalk_rmdir(dirfd, dir);
185 /* do a recursive copy. */
186 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
188 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
197 /* doesn't exist or the path is too long. */
198 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
199 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
200 ((dp = opendirat(dirfd, src)) == NULL))
203 /* try to create the destination directory */
204 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
209 /* set things up to copy */
213 srem = sizeof(spath) - slen -1;
218 drem = sizeof(dpath) - dlen -1;
221 while ((de = readdir(dp))) {
222 /* skip this and previous directory */
223 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
226 if (strlen(de->d_name) > srem) {
230 strcpy(spath + slen, de->d_name);
232 if (lstatat(dirfd, spath, &st) == 0) {
233 if (strlen(de->d_name) > drem) {
237 strcpy(dpath + dlen, de->d_name);
239 if (S_ISDIR(st.st_mode)) {
240 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
242 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
246 /* keep the same time stamp. */
247 ut.actime = ut.modtime = st.st_mtime;
253 /* keep the same time stamp. */
254 if (lstatat(dirfd, src, &st) == 0) {
255 ut.actime = ut.modtime = st.st_mtime;
264 /* ---------------------
265 * is our cached offspring count valid?
267 static int diroffcnt(struct dir *dir, struct stat *st)
269 return st->st_ctime == dir->ctime;
272 /* --------------------- */
273 static int invisible_dots(const struct vol *vol, const char *name)
275 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
278 /* ------------------ */
279 static int set_dir_errors(struct path *path, const char *where, int err)
284 return AFPERR_ACCESS;
288 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
293 * @brief Convert name in client encoding to server encoding
295 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
296 * This only gets called from cname().
298 * @returns 0 on success, -1 on error
300 * @note If the passed ret->m_name is mangled, we'll demangle it
302 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
304 static char temp[ MAXPATHLEN + 1];
308 if (afp_version >= 30) {
310 if (dir->d_did == DIRDID_ROOT_PARENT) {
312 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
313 * So we compare it with the longname from the current volume and if they match
314 * we overwrite the requested path with the utf8 volume name so that the following
317 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
318 if (strcasecmp(ret->m_name, temp) == 0)
319 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
322 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
323 afp_errno = AFPERR_PARAM;
326 strcpy(ret->m_name, temp);
330 /* check for OS X mangled filename :( */
331 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
332 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
333 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
335 if (t != ret->m_name) {
337 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
338 * flags weren't the same
340 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
341 /* at last got our view of mac name */
342 strcpy(ret->m_name, t);
345 } /* afp_version >= 30 */
347 /* If we haven't got it by now, get it */
348 if (ret->u_name == NULL) {
349 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
350 afp_errno = AFPERR_PARAM;
359 * @brief Build struct path from struct dir
361 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
362 * 1. move cwd into parent dir (we're often already there, but not always)
363 * 2. set struct path to the dirname
365 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
366 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
367 * 4. indicate there's no dir for this path
370 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
372 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
378 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
381 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
382 if (dir->d_m_name == dir->d_u_name) {
383 ret->u_name = ret->m_name;
385 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
386 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
391 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
392 cfrombstr(dir->d_fullpath),
393 cfrombstr(curdir->d_fullpath),
399 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
402 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
403 if (dir->d_m_name == dir->d_u_name) {
404 ret->u_name = ret->m_name;
406 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
407 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
410 ret->d_dir = NULL; /* 4 */
411 dir_remove(vol, dir); /* 5 */
418 /* DEADC0DE: never get here */
423 /*********************************************************************************************
425 ********************************************************************************************/
427 int get_afp_errno(const int param)
429 if (afp_errno != AFPERR_DID1)
435 * Resolve struct dir for an absolute path
437 * Given a path like "/Volumes/volume/dir/subdir" in a volume "/Volumes/volume" return
438 * a pointer to struct dir of "subdir".
439 * 1. Remove volue path from absolute path
441 * 3. loop through all elements of the remaining path from 1.
442 * 4. we only allow dirs
444 * 6. if not found in the dircache query the CNID database for the DID
445 * 7. and use dirlookup to resolve the DID to a it's struct dir *
447 * @param vol (r) volume the path is in, must be known
448 * @param path (r) absoule path
450 * @returns pointer to struct dir or NULL on error
452 struct dir *dirlookup_bypath(const struct vol *vol, const char *path)
456 struct dir *dir = NULL;
458 bstring rpath = NULL;
459 bstring statpath = NULL;
460 struct bstrList *l = NULL;
466 EC_NULL(rpath = rel_path_in_vol(path, vol->v_path)); /* 1. */
467 EC_NULL(statpath = bfromcstr(vol->v_path)); /* 2. */
469 l = bsplit(rpath, '/');
470 for (int i = 0; i < l->qty ; i++) { /* 3. */
472 EC_ZERO(bconcat(statpath, l->entry[i]));
473 EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
474 "lstat(rpath: %s, elem: %s): %s: %s",
475 cfrombstr(rpath), cfrombstr(l->entry[i]),
476 cfrombstr(statpath), strerror(errno));
478 if (!(S_ISDIR(st.st_mode))) /* 4. */
481 if ((dir = dircache_search_by_name(vol, /* 5. */
483 cfrombstr(l->entry[i]),
484 blength(l->entry[i]),
485 st.st_ctime)) == NULL) {
486 if ((cnid = cnid_add(vol->v_cdb, /* 6. */
489 cfrombstr(l->entry[i]),
490 blength(l->entry[i]),
491 0)) == CNID_INVALID) {
495 if ((dir = dirlookup(vol, cnid)) == NULL) /* 7. */
499 EC_ZERO(bcatcstr(statpath, "/"));
513 * @brief Resolve a DID
515 * Resolve a DID, allocate a struct dir for it
516 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
517 * 2a. Check if the DID is in the cache.
518 * 2b. Check if it's really a dir (d_fullpath != NULL) because we cache files too.
519 * 3. If it's not in the cache resolve it via the database.
520 * 4. Build complete server-side path to the dir.
521 * 5. Check if it exists and is a directory.
522 * 6. Create the struct dir and populate it.
523 * 7. Add it to the cache.
525 * @param vol (r) pointer to struct vol
526 * @param did (r) DID to resolve
528 * @returns pointer to struct dir
530 struct dir *dirlookup(const struct vol *vol, cnid_t did)
532 static char buffer[12 + MAXPATHLEN + 1];
534 struct dir *ret = NULL, *pdir;
535 bstring fullpath = NULL;
536 char *upath = NULL, *mpath;
539 int buflen = 12 + MAXPATHLEN + 1;
543 LOG(log_debug, logtype_afpd, "dirlookup(did: %u)", ntohl(did));
545 /* check for did 0, 1 and 2 */
546 if (did == 0 || vol == NULL) { /* 1 */
547 afp_errno = AFPERR_PARAM;
550 } else if (did == DIRDID_ROOT_PARENT) {
551 rootParent.d_vid = vol->v_vid;
554 } else if (did == DIRDID_ROOT) {
559 /* Search the cache */
560 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
561 if (ret->d_fullpath == NULL) { /* 2b */
562 afp_errno = AFPERR_BADTYPE;
566 if (lstat(cfrombstr(ret->d_fullpath), &st) != 0) {
567 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
571 /* It's not there anymore, so remove it */
572 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
573 dir_remove(vol, ret);
574 afp_errno = AFPERR_NOOBJ;
589 utf8 = utf8_encoding();
590 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
592 /* Get it from the database */
594 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL) {
595 afp_errno = AFPERR_NOOBJ;
599 if ((upath = strdup(upath)) == NULL) { /* 3 */
600 afp_errno = AFPERR_NOOBJ;
607 * Recurse up the tree, terminates in dirlookup when either
608 * - DIRDID_ROOT is hit
609 * - a cached entry is found
611 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {recursion for did: %u}", ntohl(pdid));
612 if ((pdir = dirlookup(vol, pdid)) == NULL) {
617 /* build the fullpath */
618 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
619 || bconchar(fullpath, '/') != BSTR_OK
620 || bcatcstr(fullpath, upath) != BSTR_OK) {
625 /* stat it and check if it's a dir */
626 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstr(fullpath));
628 if (stat(cfrombstr(fullpath), &st) != 0) { /* 5a */
631 afp_errno = AFPERR_NOOBJ;
635 afp_errno = AFPERR_ACCESS;
639 afp_errno = AFPERR_MISC;
644 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
645 afp_errno = AFPERR_BADTYPE;
651 /* Get macname from unix name */
652 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
653 afp_errno = AFPERR_NOOBJ;
658 /* Create struct dir */
659 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath, st.st_ctime)) == NULL) { /* 6 */
660 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
665 /* Add it to the cache only if it's a dir */
666 if (dircache_add(vol, ret) != 0) { /* 7 */
672 if (upath) free(upath);
674 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
675 ntohl(did), AfpErr2name(afp_errno));
684 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): pdid: %u, \"%s\"",
685 ntohl(ret->d_did), ntohl(ret->d_pdid), cfrombstr(ret->d_fullpath));
690 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
692 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
697 static u_int32_t did = 0;
698 static char cname[MAXPATHLEN];
699 static char lname[MAXPATHLEN];
700 ucs2_t u2_path[MAXPATHLEN];
701 ucs2_t u2_dename[MAXPATHLEN];
702 char *tmp, *savepath;
704 if (!(vol->v_flags & AFPVOL_CASEINSEN))
707 if (veto_file(ENUMVETO, path->u_name))
710 savepath = path->u_name;
712 /* very simple cache */
713 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
714 path->u_name = cname;
716 if (of_stat( path ) == 0 ) {
719 /* something changed, we cannot stat ... */
723 if (NULL == ( dp = opendir( "." )) ) {
724 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
729 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
730 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
731 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
733 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
735 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
736 if (NULL == check_dirent(vol, de->d_name))
739 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
742 if (strcasecmp_w( u2_path, u2_dename) == 0) {
744 strlcpy(cname, de->d_name, sizeof(cname));
745 path->u_name = cname;
747 if (of_stat( path ) == 0 ) {
748 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
749 strlcpy(lname, tmp, sizeof(lname));
762 /* invalidate cache */
765 path->u_name = savepath;
767 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
773 * @brief Construct struct dir
775 * Construct struct dir from parameters.
777 * @param m_name (r) directory name in UTF8-dec
778 * @param u_name (r) directory name in server side encoding
779 * @param vol (r) pointer to struct vol
780 * @param pdid (r) Parent CNID
781 * @param did (r) CNID
782 * @param path (r) Full unix path to dir or NULL for files
783 * @param ctime (r) st_ctime from stat
785 * @returns pointer to new struct dir or NULL on error
787 * @note Most of the time mac name and unix name are the same.
789 struct dir *dir_new(const char *m_name,
791 const struct vol *vol,
799 dir = (struct dir *) calloc(1, sizeof( struct dir ));
803 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
808 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
811 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
812 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
813 dir->d_m_name_ucs2 = NULL;
816 if (m_name == u_name || !strcmp(m_name, u_name)) {
817 dir->d_u_name = dir->d_m_name;
819 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
820 bdestroy(dir->d_m_name);
827 dir->d_vid = vol->v_vid;
828 dir->d_fullpath = path;
829 dir->ctime_dircache = ctime;
834 * @brief Free a struct dir and all its members
836 * @param (rw) pointer to struct dir
838 void dir_free(struct dir *dir)
840 if (dir->d_u_name != dir->d_m_name) {
841 bdestroy(dir->d_u_name);
843 if (dir->d_m_name_ucs2)
844 free(dir->d_m_name_ucs2);
845 bdestroy(dir->d_m_name);
846 bdestroy(dir->d_fullpath);
851 * @brief Create struct dir from struct path
853 * Create a new struct dir from struct path. Then add it to the cache.
855 * 1. Open adouble file, get CNID from it.
856 * 2. Search the database, hinting with the CNID from (1).
857 * 3. Build fullpath and create struct dir.
858 * 4. Add it to the cache.
860 * @param vol (r) pointer to struct vol, possibly modified in callee
861 * @param dir (r) pointer to parrent directory
862 * @param path (rw) pointer to struct path with valid path->u_name
863 * @param len (r) strlen of path->u_name
865 * @returns Pointer to new struct dir or NULL on error.
867 * @note Function also assigns path->m_name from path->u_name.
869 struct dir *dir_add(struct vol *vol, const struct dir *dir, struct path *path, int len)
872 struct dir *cdir = NULL;
875 struct adouble *adp = NULL;
883 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name), path->st.st_ctime)) != NULL) {
884 /* there's a stray entry in the dircache */
885 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
886 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
887 ntohl(cdir->d_did), cfrombstr(dir->d_fullpath));
888 if (dir_remove(vol, cdir) != 0) {
890 AFP_PANIC("dir_add");
894 /* get_id needs adp for reading CNID from adouble file */
895 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
896 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
900 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
906 ad_close_metadata(adp);
908 /* Get macname from unixname */
909 if (path->m_name == NULL) {
910 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
911 LOG(log_error, logtype_afpd, "dir_add(\"%s\"): can't assign macname", path->u_name);
918 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
919 || (bconchar(fullpath, '/') != BSTR_OK)
920 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
921 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
926 /* Allocate and initialize struct dir */
927 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath, path->st.st_ctime)) == NULL) { /* 3 */
932 if ((dircache_add(vol, cdir)) != 0) { /* 4 */
933 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstr(fullpath));
939 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
940 cfrombstr(dir->d_u_name), path->u_name, err);
943 ad_close_metadata(adp);
944 if (!cdir && fullpath)
951 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
952 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
953 ntohl(cdir->d_did), cfrombstr(cdir->d_fullpath));
960 * Free the queue with invalid struct dirs
962 * This gets called at the end of every AFP func.
964 void dir_free_invalid_q(void)
967 while (dir = (struct dir *)dequeue(invalid_dircache_entries))
972 * @brief Remove a dir from a cache and queue it for freeing
974 * 1. Check if the dir is locked or has opened forks
975 * 2. Remove it from the cache
976 * 3. Queue it for removal
977 * 4. If it's a request to remove curdir, mark curdir as invalid
978 * 5. Mark it as invalid
980 * @param (r) pointer to struct vol
981 * @param (rw) pointer to struct dir
983 int dir_remove(const struct vol *vol, struct dir *dir)
988 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
991 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
992 ntohl(dir->d_did), cfrombstr(dir->d_u_name));
994 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 2 */
995 enqueue(invalid_dircache_entries, dir); /* 3 */
997 if (curdir == dir) /* 4 */
1000 dir->d_did = CNID_INVALID; /* 5 */
1007 * @brief Modify a struct dir, adjust cache
1009 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
1010 * If given new_uname == new_mname, new_uname will point to new_mname.
1012 * @param vol (r) pointer to struct vol
1013 * @param dir (rw) pointer to struct dir
1014 * @param pdid (r) new parent DID
1015 * @param did (r) new DID
1016 * @param new_mname (r) new mac-name
1017 * @param new_uname (r) new unix-name
1018 * @param pdir_fullpath (r) new fullpath of parent dir
1020 int dir_modify(const struct vol *vol,
1024 const char *new_mname,
1025 const char *new_uname,
1026 bstring pdir_fullpath)
1030 /* Remove it from the cache */
1031 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
1039 /* free uname if it's not the same as mname */
1040 if (dir->d_m_name != dir->d_u_name)
1041 bdestroy(dir->d_u_name);
1043 if (new_uname == NULL)
1044 new_uname = new_mname;
1046 /* assign new name */
1047 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
1048 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1052 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
1053 dir->d_u_name = dir->d_m_name;
1055 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
1056 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1062 if (pdir_fullpath) {
1063 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
1065 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
1067 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
1071 if (dir->d_m_name_ucs2)
1072 free(dir->d_m_name_ucs2);
1073 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))
1074 dir->d_m_name_ucs2 = NULL;
1076 /* Re-add it to the cache */
1077 if ((dircache_add(vol, dir)) != 0) {
1079 AFP_PANIC("dir_modify");
1087 * @brief Resolve a catalog node name path
1089 * 1. Evaluate path type
1090 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
1091 * path from dirname, so eg getdirparams has sth it can chew on. curdir
1092 * is dir parent then. All this is done in path_from_dir().
1093 * 3. Parse next cnode name in path, cases:
1094 * 4. single "\0" -> do nothing
1095 * 5. two or more consecutive "\0" -> chdir("..") one or more times
1096 * 6. cnode name -> copy it to path.m_name
1097 * 7. Get unix name from mac name
1098 * 8. Special handling of request with did 1
1099 * 9. stat the cnode name
1100 * 10. If it's not there, it's probably an afp_createfile|dir,
1101 * return with curdir = dir parent, struct path = dirname
1102 * 11. If it's there and it's a file, it must should be the last element of the requested
1103 * path. Return with curdir = cnode name parent dir, struct path = filename
1104 * 12. Treat symlinks like files, dont follow them
1105 * 13. If it's a dir:
1106 * 14. Search the dircache for it
1107 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
1108 * 16. chdir into the dir and
1109 * 17. set m_name to the mac equivalent of "."
1112 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1114 static char path[ MAXPATHLEN + 1];
1115 static struct path ret;
1125 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstr(dir->d_fullpath));
1128 afp_errno = AFPERR_NOOBJ;
1129 memset(&ret, 0, sizeof(ret));
1131 switch (ret.m_type = *data) { /* 1 */
1134 len = (unsigned char) *data++;
1136 if (afp_version >= 30) {
1142 if (afp_version >= 30) {
1144 memcpy(&hint, data, sizeof(hint));
1146 data += sizeof(hint);
1148 memcpy(&len16, data, sizeof(len16));
1154 /* else it's an error */
1156 afp_errno = AFPERR_PARAM;
1159 *cpath += len + size;
1164 if (movecwd(vol, dir) < 0 ) {
1165 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1166 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1168 return path_from_dir(vol, dir, &ret);
1173 while (len) { /* 3 */
1174 if (*data == 0) { /* 4 or 5 */
1177 while (len > 0 && *data == 0) { /* 5 */
1178 /* chdir to parrent dir */
1179 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1181 if (movecwd( vol, dir ) < 0 ) {
1182 dir_remove(vol, dir);
1192 for ( p = path; *data != 0 && len > 0; len-- ) {
1194 if (p > &path[UTF8FILELEN_EARLY]) { /* FIXME safeguard, limit of early Mac OS X */
1195 afp_errno = AFPERR_PARAM;
1199 *p = 0; /* Terminate string */
1202 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1203 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1207 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstr(dir->d_fullpath), ret.u_name);
1209 /* Prevent access to our special folders like .AppleDouble */
1210 if (check_name(vol, ret.u_name)) {
1211 /* the name is illegal */
1212 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1213 afp_errno = AFPERR_PARAM;
1217 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1219 * Special case: CNID 1
1220 * root parent (did 1) has one child: the volume. Requests for did=1 with
1221 * some <name> must check against the volume name.
1223 if ((strcmp(cfrombstr(vol->v_root->d_m_name), ret.m_name)) == 0)
1229 * CNID != 1, eg. most of the times we take this way.
1230 * Now check if current path-part is a file or dir:
1231 * o if it's dir we have to step into it
1232 * o if it's a file we expect it to be the last part of the requested path
1233 * and thus call continue which should terminate the while loop because
1236 if (of_stat(&ret) != 0) { /* 9 */
1238 * ret.u_name doesn't exist, might be afp_createfile|dir
1239 * that means it should have been the last part
1242 /* it wasn't the last part, so we have a bogus path request */
1243 afp_errno = AFPERR_NOOBJ;
1247 * this will terminate clean in while (1) because len == 0,
1248 * probably afp_createfile|dir
1250 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}",
1251 cfrombstr(dir->d_fullpath), ret.u_name);
1255 switch (ret.st.st_mode & S_IFMT) {
1256 case S_IFREG: /* 11 */
1257 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}",
1258 cfrombstr(dir->d_fullpath), ret.u_name);
1260 /* it wasn't the last part, so we have a bogus path request */
1261 afp_errno = AFPERR_PARAM;
1264 continue; /* continues while loop */
1265 case S_IFLNK: /* 12 */
1266 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}",
1267 cfrombstr(dir->d_fullpath), ret.u_name);
1269 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}",
1270 cfrombstr(dir->d_fullpath), ret.u_name);
1271 afp_errno = AFPERR_PARAM;
1274 continue; /* continues while loop */
1275 case S_IFDIR: /* 13 */
1278 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1279 afp_errno = AFPERR_NODIR;
1283 /* Search the cache */
1284 int unamelen = strlen(ret.u_name);
1285 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen, ret.st.st_ctime); /* 14 */
1287 /* Not in cache, create one */
1288 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1289 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1290 ntohl(dir->d_did), ret.u_name, getcwdpath());
1294 } /* if/else cnid==1 */
1296 /* Now chdir to the evaluated dir */
1297 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1298 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1299 cfrombstr(curdir->d_fullpath), cfrombstr(cdir->d_fullpath), strerror(errno));
1301 return path_from_dir(vol, cdir, &ret);
1306 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1309 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1310 afp_errno = AFPERR_DID1;
1314 if (ret.m_name[0] == 0) {
1315 /* Last part was a dir */
1316 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1320 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1321 cfrombstr(dir->d_fullpath),
1322 cfrombstr(curdir->d_fullpath),
1329 * @brief chdir() to dir
1331 * @param vol (r) pointer to struct vol
1332 * @param dir (r) pointer to struct dir
1334 * @returns 0 on success, -1 on error with afp_errno set appropiately
1336 int movecwd(const struct vol *vol, struct dir *dir)
1343 LOG(log_maxdebug, logtype_afpd, "movecwd: from: curdir:\"%s\", cwd:\"%s\"",
1344 curdir ? cfrombstr(curdir->d_fullpath) : "INVALID", getcwdpath());
1346 if (dir->d_did == DIRDID_ROOT_PARENT) {
1347 curdir = &rootParent;
1351 LOG(log_debug, logtype_afpd, "movecwd(to: did: %u, \"%s\")",
1352 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1354 if ((ret = lchdir(cfrombstr(dir->d_fullpath))) != 0 ) {
1355 LOG(log_debug, logtype_afpd, "movecwd(\"%s\"): ret: %u, %s",
1356 cfrombstr(dir->d_fullpath), ret, strerror(errno));
1358 /* p is a symlink or getcwd failed */
1359 afp_errno = AFPERR_BADTYPE;
1361 if (chdir(vol->v_path ) < 0) {
1362 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1363 /* XXX what do we do here? */
1365 curdir = vol->v_root;
1372 afp_errno = AFPERR_ACCESS;
1375 afp_errno = AFPERR_NOOBJ;
1385 * We can't use unix file's perm to support Apple's inherited protection modes.
1386 * If we aren't the file's owner we can't change its perms when moving it and smb
1387 * nfs,... don't even try.
1389 #define AFP_CHECK_ACCESS
1391 int check_access(char *path, int mode)
1393 #ifdef AFP_CHECK_ACCESS
1401 accessmode(p, &ma, curdir, NULL);
1402 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1404 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1410 /* --------------------- */
1411 int file_access(struct path *path, int mode)
1415 accessmode(path->u_name, &ma, curdir, &path->st);
1417 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): mapped user mode: 0x%02x",
1418 path->u_name, ma.ma_user);
1420 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE)) {
1421 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): write access denied", path->u_name);
1424 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD)) {
1425 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): read access denied", path->u_name);
1432 /* --------------------- */
1433 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1435 dir->offcnt = count;
1436 dir->ctime = st->st_ctime;
1437 dir->d_flags &= ~DIRF_CNID;
1441 /* ---------------------
1442 * is our cached also for reenumerate id?
1444 int dirreenumerate(struct dir *dir, struct stat *st)
1446 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1449 /* ------------------------------
1451 (name, dir) with curdir:name == dir, from afp_enumerate
1454 int getdirparams(const struct vol *vol,
1455 u_int16_t bitmap, struct path *s_path,
1457 char *buf, size_t *buflen )
1461 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1462 int bit = 0, isad = 0;
1468 struct stat *st = &s_path->st;
1469 char *upath = s_path->u_name;
1471 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1472 (1 << DIRPBIT_CDATE) |
1473 (1 << DIRPBIT_MDATE) |
1474 (1 << DIRPBIT_BDATE) |
1475 (1 << DIRPBIT_FINFO)))) {
1477 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1478 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1480 if (ad.ad_md->adf_flags & O_CREAT) {
1481 /* We just created it */
1482 ad_setname(&ad, s_path->m_name);
1497 while ( bitmap != 0 ) {
1498 while (( bitmap & 1 ) == 0 ) {
1506 ad_getattr(&ad, &ashort);
1507 } else if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1508 ashort = htons(ATTRBIT_INVISIBLE);
1511 ashort |= htons(ATTRBIT_SHARED);
1512 memcpy( data, &ashort, sizeof( ashort ));
1513 data += sizeof( ashort );
1517 memcpy( data, &pdid, sizeof( pdid ));
1518 data += sizeof( pdid );
1519 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1520 s_path->u_name, ntohl(pdid));
1523 case DIRPBIT_CDATE :
1524 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1525 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1526 memcpy( data, &aint, sizeof( aint ));
1527 data += sizeof( aint );
1530 case DIRPBIT_MDATE :
1531 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1532 memcpy( data, &aint, sizeof( aint ));
1533 data += sizeof( aint );
1536 case DIRPBIT_BDATE :
1537 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1538 aint = AD_DATE_START;
1539 memcpy( data, &aint, sizeof( aint ));
1540 data += sizeof( aint );
1543 case DIRPBIT_FINFO :
1545 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1546 } else { /* no appledouble */
1547 memset( data, 0, 32 );
1548 /* set default view -- this also gets done in ad_open() */
1549 ashort = htons(FINDERINFO_CLOSEDVIEW);
1550 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1552 /* dot files are by default visible */
1553 if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1554 ashort = htons(FINDERINFO_INVISIBLE);
1555 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1561 case DIRPBIT_LNAME :
1562 if (dir->d_m_name) /* root of parent can have a null name */
1565 memset(data, 0, sizeof(u_int16_t));
1566 data += sizeof( u_int16_t );
1569 case DIRPBIT_SNAME :
1570 memset(data, 0, sizeof(u_int16_t));
1571 data += sizeof( u_int16_t );
1575 memcpy( data, &dir->d_did, sizeof( aint ));
1576 data += sizeof( aint );
1577 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1578 s_path->u_name, ntohl(dir->d_did));
1581 case DIRPBIT_OFFCNT :
1583 /* this needs to handle current directory access rights */
1584 if (diroffcnt(dir, st)) {
1585 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1587 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1588 setdiroffcnt(dir, st, ret);
1589 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1591 ashort = htons( ashort );
1592 memcpy( data, &ashort, sizeof( ashort ));
1593 data += sizeof( ashort );
1597 aint = htonl(st->st_uid);
1598 memcpy( data, &aint, sizeof( aint ));
1599 data += sizeof( aint );
1603 aint = htonl(st->st_gid);
1604 memcpy( data, &aint, sizeof( aint ));
1605 data += sizeof( aint );
1608 case DIRPBIT_ACCESS :
1609 accessmode( upath, &ma, dir , st);
1611 *data++ = ma.ma_user;
1612 *data++ = ma.ma_world;
1613 *data++ = ma.ma_group;
1614 *data++ = ma.ma_owner;
1617 /* Client has requested the ProDOS information block.
1618 Just pass back the same basic block for all
1619 directories. <shirsch@ibm.net> */
1620 case DIRPBIT_PDINFO :
1621 if (afp_version >= 30) { /* UTF8 name */
1622 utf8 = kTextEncodingUTF8;
1623 if (dir->d_m_name) /* root of parent can have a null name */
1626 memset(data, 0, sizeof(u_int16_t));
1627 data += sizeof( u_int16_t );
1629 memcpy(data, &aint, sizeof( aint ));
1630 data += sizeof( aint );
1632 else { /* ProDOS Info Block */
1635 ashort = htons( 0x0200 );
1636 memcpy( data, &ashort, sizeof( ashort ));
1637 data += sizeof( ashort );
1638 memset( data, 0, sizeof( ashort ));
1639 data += sizeof( ashort );
1643 case DIRPBIT_UNIXPR :
1644 aint = htonl(st->st_uid);
1645 memcpy( data, &aint, sizeof( aint ));
1646 data += sizeof( aint );
1647 aint = htonl(st->st_gid);
1648 memcpy( data, &aint, sizeof( aint ));
1649 data += sizeof( aint );
1652 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1653 memcpy( data, &aint, sizeof( aint ));
1654 data += sizeof( aint );
1656 accessmode( upath, &ma, dir , st);
1658 *data++ = ma.ma_user;
1659 *data++ = ma.ma_world;
1660 *data++ = ma.ma_group;
1661 *data++ = ma.ma_owner;
1666 ad_close_metadata( &ad );
1668 return( AFPERR_BITMAP );
1674 ashort = htons( data - buf );
1675 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1676 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, 0);
1678 if ( utf_nameoff ) {
1679 ashort = htons( data - buf );
1680 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1681 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, utf8);
1684 ad_close_metadata( &ad );
1686 *buflen = data - buf;
1690 /* ----------------------------- */
1691 int path_error(struct path *path, int error)
1693 /* - a dir with access error
1694 * - no error it's a file
1697 if (path_isadir(path))
1699 if (path->st_valid && path->st_errno)
1701 return AFPERR_BADTYPE ;
1704 /* ----------------------------- */
1705 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1710 u_int16_t vid, bitmap;
1716 memcpy( &vid, ibuf, sizeof( vid ));
1717 ibuf += sizeof( vid );
1719 if (NULL == ( vol = getvolbyvid( vid )) ) {
1720 return( AFPERR_PARAM );
1723 if (vol->v_flags & AFPVOL_RO)
1724 return AFPERR_VLOCK;
1726 memcpy( &did, ibuf, sizeof( did ));
1727 ibuf += sizeof( int );
1729 if (NULL == ( dir = dirlookup( vol, did )) ) {
1733 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1734 bitmap = ntohs( bitmap );
1735 ibuf += sizeof( bitmap );
1737 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1738 return get_afp_errno(AFPERR_NOOBJ);
1741 if ( *path->m_name != '\0' ) {
1742 rc = path_error(path, AFPERR_NOOBJ);
1743 /* maybe we are trying to set perms back */
1744 if (rc != AFPERR_ACCESS)
1749 * If ibuf is odd, make it even.
1751 if ((u_long)ibuf & 1 ) {
1755 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1756 setvoltime(obj, vol );
1762 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1764 * assume path == '\0' eg. it's a directory in canonical form
1766 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1778 u_int16_t ashort, bshort, oshort;
1780 int change_mdate = 0;
1781 int change_parent_mdate = 0;
1783 u_int16_t bitmap = d_bitmap;
1784 u_char finder_buf[32];
1787 u_int16_t upriv_bit = 0;
1790 upath = path->u_name;
1792 while ( bitmap != 0 ) {
1793 while (( bitmap & 1 ) == 0 ) {
1801 memcpy( &ashort, buf, sizeof( ashort ));
1802 buf += sizeof( ashort );
1804 case DIRPBIT_CDATE :
1806 memcpy(&cdate, buf, sizeof(cdate));
1807 buf += sizeof( cdate );
1809 case DIRPBIT_MDATE :
1810 memcpy(&newdate, buf, sizeof(newdate));
1811 buf += sizeof( newdate );
1813 case DIRPBIT_BDATE :
1815 memcpy(&bdate, buf, sizeof(bdate));
1816 buf += sizeof( bdate );
1818 case DIRPBIT_FINFO :
1820 memcpy( finder_buf, buf, 32 );
1823 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1824 change_parent_mdate = 1;
1825 memcpy( &owner, buf, sizeof(owner));
1826 buf += sizeof( owner );
1829 change_parent_mdate = 1;
1830 memcpy( &group, buf, sizeof( group ));
1831 buf += sizeof( group );
1833 case DIRPBIT_ACCESS :
1835 change_parent_mdate = 1;
1836 ma.ma_user = *buf++;
1837 ma.ma_world = *buf++;
1838 ma.ma_group = *buf++;
1839 ma.ma_owner = *buf++;
1840 mpriv = mtoumode( &ma ) | vol->v_dperm;
1841 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1842 err = set_dir_errors(path, "setdirmode", errno);
1846 /* Ignore what the client thinks we should do to the
1847 ProDOS information block. Skip over the data and
1848 report nothing amiss. <shirsch@ibm.net> */
1849 case DIRPBIT_PDINFO :
1850 if (afp_version < 30) {
1854 err = AFPERR_BITMAP;
1858 case DIRPBIT_UNIXPR :
1859 if (vol_unix_priv(vol)) {
1860 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1861 buf += sizeof( owner );
1862 memcpy( &group, buf, sizeof( group ));
1863 buf += sizeof( group );
1866 change_parent_mdate = 1;
1867 memcpy( &upriv, buf, sizeof( upriv ));
1868 buf += sizeof( upriv );
1869 upriv = ntohl (upriv) | vol->v_dperm;
1870 if (dir_rx_set(upriv)) {
1871 /* maybe we are trying to set perms back */
1872 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1874 err = set_dir_errors(path, "setdirunixmode", errno);
1885 err = AFPERR_BITMAP;
1893 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1895 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1897 * Check to see what we're trying to set. If it's anything
1898 * but ACCESS, UID, or GID, give an error. If it's any of those
1899 * three, we don't need the ad to be open, so just continue.
1901 * note: we also don't need to worry about mdate. also, be quiet
1902 * if we're using the noadouble option.
1904 if (!vol_noadouble(vol) && (d_bitmap &
1905 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1906 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1907 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1908 return AFPERR_ACCESS;
1914 * Check to see if a create was necessary. If it was, we'll want
1915 * to set our name, etc.
1917 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1918 ad_setname(&ad, cfrombstr(curdir->d_m_name));
1924 while ( bitmap != 0 ) {
1925 while (( bitmap & 1 ) == 0 ) {
1933 ad_getattr(&ad, &bshort);
1935 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1936 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1940 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1941 change_parent_mdate = 1;
1942 ad_setattr(&ad, bshort);
1945 case DIRPBIT_CDATE :
1947 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1950 case DIRPBIT_MDATE :
1952 case DIRPBIT_BDATE :
1954 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1957 case DIRPBIT_FINFO :
1959 /* Fixes #2802236 */
1960 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1961 *fflags &= htons(~FINDERINFO_ISHARED);
1963 if ( dir->d_did == DIRDID_ROOT ) {
1965 * Alright, we admit it, this is *really* sick!
1966 * The 4 bytes that we don't copy, when we're dealing
1967 * with the root of a volume, are the directory's
1968 * location information. This eliminates that annoying
1969 * behavior one sees when mounting above another mount
1972 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1973 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1975 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1979 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1980 if ( (dir->d_did == DIRDID_ROOT) &&
1981 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1982 err = set_dir_errors(path, "setdeskowner", errno);
1983 if (isad && err == AFPERR_PARAM) {
1984 err = AFP_OK; /* ???*/
1987 goto setdirparam_done;
1990 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1991 err = set_dir_errors(path, "setdirowner", errno);
1992 goto setdirparam_done;
1996 if (dir->d_did == DIRDID_ROOT)
1997 setdeskowner( -1, ntohl(group) );
1998 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1999 err = set_dir_errors(path, "setdirowner", errno);
2000 goto setdirparam_done;
2003 case DIRPBIT_ACCESS :
2004 if (dir->d_did == DIRDID_ROOT) {
2006 if (!dir_rx_set(mpriv)) {
2007 /* we can't remove read and search for owner on volume root */
2008 err = AFPERR_ACCESS;
2009 goto setdirparam_done;
2013 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2014 err = set_dir_errors(path, "setdirmode", errno);
2015 goto setdirparam_done;
2018 case DIRPBIT_PDINFO :
2019 if (afp_version >= 30) {
2020 err = AFPERR_BITMAP;
2021 goto setdirparam_done;
2024 case DIRPBIT_UNIXPR :
2025 if (vol_unix_priv(vol)) {
2026 if (dir->d_did == DIRDID_ROOT) {
2027 if (!dir_rx_set(upriv)) {
2028 /* we can't remove read and search for owner on volume root */
2029 err = AFPERR_ACCESS;
2030 goto setdirparam_done;
2032 setdeskowner( -1, ntohl(group) );
2033 setdeskmode( upriv );
2035 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2036 err = set_dir_errors(path, "setdirowner", errno);
2037 goto setdirparam_done;
2040 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2041 err = set_dir_errors(path, "setdirunixmode", errno);
2042 goto setdirparam_done;
2046 err = AFPERR_BITMAP;
2047 goto setdirparam_done;
2051 err = AFPERR_BITMAP;
2052 goto setdirparam_done;
2061 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2062 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2066 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2067 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2072 if (path->st_valid && !path->st_errno) {
2073 struct stat *st = &path->st;
2075 if (dir && dir->d_pdid) {
2076 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
2080 ad_close_metadata( &ad);
2083 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2084 && gettimeofday(&tv, NULL) == 0) {
2085 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
2086 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2087 /* be careful with bitmap because now dir is null */
2088 bitmap = 1<<DIRPBIT_MDATE;
2089 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2090 /* should we reset curdir ?*/
2097 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2111 memcpy( &vid, ibuf, sizeof( vid ));
2112 ibuf += sizeof( vid );
2113 if (NULL == (vol = getvolbyvid( vid )) ) {
2114 return( AFPERR_PARAM );
2117 memcpy( &did, ibuf, sizeof( did ));
2118 ibuf += sizeof( did );
2122 * if it's CNID 2 our only choice to meet the specs is call sync.
2123 * For any other CNID just sync that dir. To my knowledge the
2124 * intended use of FPSyncDir is to sync the volume so all we're
2125 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2128 if ( ntohl(did) == 2 ) {
2131 if (NULL == ( dir = dirlookup( vol, did )) ) {
2132 return afp_errno; /* was AFPERR_NOOBJ */
2135 if (movecwd( vol, dir ) < 0 )
2136 return ( AFPERR_NOOBJ );
2139 * Assuming only OSens that have dirfd also may require fsyncing directories
2140 * in order to flush metadata e.g. Linux.
2144 if (NULL == ( dp = opendir( "." )) ) {
2147 return( AFPERR_NOOBJ );
2149 return( AFPERR_ACCESS );
2151 return( AFPERR_PARAM );
2155 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2158 if ( fsync ( dfd ) < 0 )
2159 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2160 dir->d_u_name, strerror(errno) );
2161 closedir(dp); /* closes dfd too */
2164 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2167 return( AFPERR_NOOBJ );
2169 return( AFPERR_ACCESS );
2171 return( AFPERR_PARAM );
2175 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2176 vol->ad_path(".", ADFLAGS_DIR) );
2178 if ( fsync(dfd) < 0 )
2179 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2180 vol->ad_path(cfrombstr(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2187 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2193 struct path *s_path;
2201 memcpy( &vid, ibuf, sizeof( vid ));
2202 ibuf += sizeof( vid );
2203 if (NULL == ( vol = getvolbyvid( vid )) ) {
2204 return( AFPERR_PARAM );
2207 if (vol->v_flags & AFPVOL_RO)
2208 return AFPERR_VLOCK;
2210 memcpy( &did, ibuf, sizeof( did ));
2211 ibuf += sizeof( did );
2212 if (NULL == ( dir = dirlookup( vol, did )) ) {
2213 return afp_errno; /* was AFPERR_NOOBJ */
2215 /* for concurrent access we need to be sure we are not in the
2216 * folder we want to create...
2220 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2221 return get_afp_errno(AFPERR_PARAM);
2223 /* cname was able to move curdir to it! */
2224 if (*s_path->m_name == '\0')
2225 return AFPERR_EXIST;
2227 upath = s_path->u_name;
2229 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2233 if (of_stat(s_path) < 0) {
2239 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2243 if ( movecwd( vol, dir ) < 0 ) {
2244 return( AFPERR_PARAM );
2247 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2248 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2249 if (vol_noadouble(vol))
2250 goto createdir_done;
2251 return( AFPERR_ACCESS );
2253 ad_setname(&ad, s_path->m_name);
2254 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2256 fce_register_new_dir(s_path);
2259 ad_close_metadata( &ad);
2262 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2263 *rbuflen = sizeof( u_int32_t );
2264 setvoltime(obj, vol );
2269 * dst new unix filename (not a pathname)
2270 * newname new mac name
2272 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2274 int renamedir(const struct vol *vol,
2279 struct dir *newparent,
2285 /* existence check moved to afp_moveandrename */
2286 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2289 return( AFPERR_NOOBJ );
2291 return( AFPERR_ACCESS );
2293 return AFPERR_VLOCK;
2295 /* tried to move directory into a subdirectory of itself */
2296 return AFPERR_CANTMOVE;
2298 /* this needs to copy and delete. bleah. that means we have
2299 * to deal with entire directory hierarchies. */
2300 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2304 if ((err = deletedir(dirfd, src)) < 0)
2308 return( AFPERR_PARAM );
2312 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2314 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2316 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2317 ad_setname(&ad, newname);
2319 ad_close_metadata( &ad);
2325 /* delete an empty directory */
2326 int deletecurdir(struct vol *vol)
2330 struct dir *fdir, *pdir;
2336 if ((pdir = dirlookup(vol, curdir->d_pdid)) == NULL) {
2337 return( AFPERR_ACCESS );
2342 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2343 /* we never want to create a resource fork here, we are going to delete it */
2344 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2346 ad_getattr(&ad, &ashort);
2347 ad_close_metadata(&ad);
2348 if ((ashort & htons(ATTRBIT_NODELETE))) {
2349 return AFPERR_OLOCK;
2352 err = vol->vfs->vfs_deletecurdir(vol);
2354 LOG(log_error, logtype_afpd, "deletecurdir: error deleting .AppleDouble in \"%s\"",
2355 curdir->d_fullpath);
2359 /* now get rid of dangling symlinks */
2360 if ((dp = opendir("."))) {
2361 while ((de = readdir(dp))) {
2362 /* skip this and previous directory */
2363 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2366 /* bail if it's not a symlink */
2367 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2368 LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): not empty",
2369 curdir->d_fullpath);
2371 return AFPERR_DIRNEMPT;
2374 if ((err = netatalk_unlink(de->d_name))) {
2381 if (movecwd(vol, pdir) < 0) {
2386 LOG(log_debug, logtype_afpd, "deletecurdir: moved to \"%s\"",
2387 cfrombstr(curdir->d_fullpath));
2389 err = netatalk_rmdir_all_errors(-1, cfrombstr(fdir->d_u_name));
2390 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2391 cnid_delete(vol->v_cdb, fdir->d_did);
2392 dir_remove( vol, fdir );
2394 LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): netatalk_rmdir_all_errors error",
2395 curdir->d_fullpath);
2400 /* inode is used as key for cnid.
2401 * Close the descriptor only after cnid_delete
2409 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2419 sfunc = (unsigned char) *ibuf++;
2422 if (sfunc >= 3 && sfunc <= 6) {
2423 if (afp_version < 30) {
2424 return( AFPERR_PARAM );
2431 case 3 :/* unicode */
2432 memcpy( &id, ibuf, sizeof( id ));
2435 if (( pw = getpwuid( id )) == NULL ) {
2436 return( AFPERR_NOITEM );
2438 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2439 pw->pw_name, -1, &name);
2446 case 4 : /* unicode */
2447 memcpy( &id, ibuf, sizeof( id ));
2450 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2451 return( AFPERR_NOITEM );
2453 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2454 gr->gr_name, -1, &name);
2461 case 5 : /* UUID -> username */
2462 case 6 : /* UUID -> groupname */
2463 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2464 return AFPERR_PARAM;
2465 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2467 len = getnamefromuuid((unsigned char*) ibuf, &name, &type);
2468 if (len != 0) /* its a error code, not len */
2469 return AFPERR_NOITEM;
2472 if (( pw = getpwnam( name )) == NULL )
2473 return( AFPERR_NOITEM );
2474 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2475 id = htonl(UUID_USER);
2476 memcpy( rbuf, &id, sizeof( id ));
2477 id = htonl( pw->pw_uid);
2478 rbuf += sizeof( id );
2479 memcpy( rbuf, &id, sizeof( id ));
2480 rbuf += sizeof( id );
2481 *rbuflen = 2 * sizeof( id );
2484 if (( gr = getgrnam( name )) == NULL )
2485 return( AFPERR_NOITEM );
2486 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2487 id = htonl(UUID_GROUP);
2488 memcpy( rbuf, &id, sizeof( id ));
2489 rbuf += sizeof( id );
2490 id = htonl( gr->gr_gid);
2491 memcpy( rbuf, &id, sizeof( id ));
2492 rbuf += sizeof( id );
2493 *rbuflen = 2 * sizeof( id );
2497 return (AFPERR_NOITEM);
2504 return( AFPERR_PARAM );
2508 len = strlen( name );
2511 u_int16_t tp = htons(len);
2512 memcpy(rbuf, &tp, sizeof(tp));
2521 memcpy( rbuf, name, len );
2529 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2538 sfunc = (unsigned char) *ibuf++;
2540 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2543 case 2 : /* unicode */
2544 if (afp_version < 30) {
2545 return( AFPERR_PARAM );
2547 memcpy(&ulen, ibuf, sizeof(ulen));
2550 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2554 len = (unsigned char) *ibuf++;
2556 case 5 : /* username -> UUID */
2557 case 6 : /* groupname -> UUID */
2558 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2559 return AFPERR_PARAM;
2560 memcpy(&ulen, ibuf, sizeof(ulen));
2565 return( AFPERR_PARAM );
2571 return AFPERR_PARAM;
2574 case 1 : /* unicode */
2576 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2577 return( AFPERR_NOITEM );
2581 memcpy( rbuf, &id, sizeof( id ));
2582 *rbuflen = sizeof( id );
2585 case 2 : /* unicode */
2587 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2588 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2589 return( AFPERR_NOITEM );
2592 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2594 memcpy( rbuf, &id, sizeof( id ));
2595 *rbuflen = sizeof( id );
2597 case 5 : /* username -> UUID */
2598 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2599 if (0 != getuuidfromname(ibuf, UUID_USER, (unsigned char *)rbuf))
2600 return AFPERR_NOITEM;
2601 *rbuflen = UUID_BINSIZE;
2603 case 6 : /* groupname -> UUID */
2604 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2605 if (0 != getuuidfromname(ibuf, UUID_GROUP, (unsigned char *)rbuf))
2606 return AFPERR_NOITEM;
2607 *rbuflen = UUID_BINSIZE;
2614 /* ------------------------------------
2615 variable DID support
2617 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2628 /* do nothing as dids are static for the life of the process. */
2632 memcpy(&vid, ibuf, sizeof( vid ));
2633 ibuf += sizeof( vid );
2634 if (( vol = getvolbyvid( vid )) == NULL ) {
2635 return( AFPERR_PARAM );
2638 memcpy( &did, ibuf, sizeof( did ));
2639 ibuf += sizeof( did );
2640 if (( dir = dirlookup( vol, did )) == NULL ) {
2641 return( AFPERR_PARAM );
2644 /* dir_remove -- deletedid */
2650 /* did creation gets done automatically
2651 * there's a pb again with case but move it to cname
2653 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2656 struct dir *parentdir;
2664 memcpy(&vid, ibuf, sizeof(vid));
2665 ibuf += sizeof( vid );
2667 if (NULL == ( vol = getvolbyvid( vid )) ) {
2668 return( AFPERR_PARAM );
2671 memcpy(&did, ibuf, sizeof(did));
2672 ibuf += sizeof(did);
2674 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2678 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2679 return get_afp_errno(AFPERR_PARAM);
2682 if ( *path->m_name != '\0' ) {
2683 return path_error(path, AFPERR_NOOBJ);
2686 if ( !path->st_valid && of_stat(path ) < 0 ) {
2687 return( AFPERR_NOOBJ );
2689 if ( path->st_errno ) {
2690 return( AFPERR_NOOBJ );
2693 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2694 *rbuflen = sizeof(curdir->d_did);