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>
32 #include "directory.h"
45 * FIXMEs, loose ends after the dircache rewrite:
46 * o merge dircache_search_by_name and dir_add ??
47 * o case-insensitivity is gone from cname
51 /*******************************************************************************************
53 ******************************************************************************************/
56 /* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
57 struct dir rootParent = {
58 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
59 NULL, 0, 0, /* qidx_node, ctime, d_flags */
60 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
62 struct dir *curdir = &rootParent;
63 struct path Cur_Path = {
68 NULL,/* struct dir * */
69 0, /* stat is not set */
75 * dir_remove queues struct dirs to be freed here. We can't just delete them immeidately
76 * eg in dircache_search_by_id, because a caller somewhere up the stack might be
79 * - we mark it as invalid by setting d_did to CNID_INVALID (ie 0)
80 * - queue it in "invalid_dircache_entries" queue
81 * - which is finally freed at the end of every AFP func in afp_dsi.c.
83 q_t *invalid_dircache_entries;
86 /*******************************************************************************************
88 ******************************************************************************************/
91 /* -------------------------
92 appledouble mkdir afp error code.
94 static int netatalk_mkdir(const struct vol *vol, const char *name)
99 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
100 if (lstat(".", &st) < 0)
102 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
103 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
104 name, st.st_mode, vol->v_umask);
106 ret = mkdir(name, mode);
108 ret = ad_mkdir(name, DIRBITS | 0777);
114 return( AFPERR_NOOBJ );
116 return( AFPERR_VLOCK );
119 return( AFPERR_ACCESS );
121 return( AFPERR_EXIST );
124 return( AFPERR_DFULL );
126 return( AFPERR_PARAM );
132 /* ------------------- */
133 static int deletedir(int dirfd, char *dir)
135 char path[MAXPATHLEN + 1];
143 if ((len = strlen(dir)) +2 > sizeof(path))
147 if ((dp = opendirat(dirfd, dir)) == NULL)
153 remain = sizeof(path) -len -1;
154 while ((de = readdir(dp)) && err == AFP_OK) {
155 /* skip this and previous directory */
156 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
159 if (strlen(de->d_name) > remain) {
163 strcpy(path + len, de->d_name);
164 if (lstatat(dirfd, path, &st)) {
167 if (S_ISDIR(st.st_mode)) {
168 err = deletedir(dirfd, path);
170 err = netatalk_unlinkat(dirfd, path);
175 /* okay. the directory is empty. delete it. note: we already got rid
178 err = netatalk_rmdir(dirfd, dir);
183 /* do a recursive copy. */
184 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
186 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
195 /* doesn't exist or the path is too long. */
196 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
197 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
198 ((dp = opendirat(dirfd, src)) == NULL))
201 /* try to create the destination directory */
202 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
207 /* set things up to copy */
211 srem = sizeof(spath) - slen -1;
216 drem = sizeof(dpath) - dlen -1;
219 while ((de = readdir(dp))) {
220 /* skip this and previous directory */
221 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
224 if (strlen(de->d_name) > srem) {
228 strcpy(spath + slen, de->d_name);
230 if (lstatat(dirfd, spath, &st) == 0) {
231 if (strlen(de->d_name) > drem) {
235 strcpy(dpath + dlen, de->d_name);
237 if (S_ISDIR(st.st_mode)) {
238 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
240 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
244 /* keep the same time stamp. */
245 ut.actime = ut.modtime = st.st_mtime;
251 /* keep the same time stamp. */
252 if (lstatat(dirfd, src, &st) == 0) {
253 ut.actime = ut.modtime = st.st_mtime;
262 /* ---------------------
263 * is our cached offspring count valid?
265 static int diroffcnt(struct dir *dir, struct stat *st)
267 return st->st_ctime == dir->ctime;
270 /* --------------------- */
271 static int invisible_dots(const struct vol *vol, const char *name)
273 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
276 /* ------------------ */
277 static int set_dir_errors(struct path *path, const char *where, int err)
282 return AFPERR_ACCESS;
286 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
291 * @brief Convert name in client encoding to server encoding
293 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
294 * This only gets called from cname().
296 * @returns 0 on success, -1 on error
298 * @note If the passed ret->m_name is mangled, we'll demangle it
300 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
302 static char temp[ MAXPATHLEN + 1];
306 if (afp_version >= 30) {
308 if (dir->d_did == DIRDID_ROOT_PARENT) {
310 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
311 * So we compare it with the longname from the current volume and if they match
312 * we overwrite the requested path with the utf8 volume name so that the following
315 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
316 if (strcasecmp(ret->m_name, temp) == 0)
317 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
320 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
321 afp_errno = AFPERR_PARAM;
324 strcpy(ret->m_name, temp);
328 /* check for OS X mangled filename :( */
329 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
330 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
331 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
333 if (t != ret->m_name) {
335 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
336 * flags weren't the same
338 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
339 /* at last got our view of mac name */
340 strcpy(ret->m_name, t);
343 } /* afp_version >= 30 */
345 /* If we haven't got it by now, get it */
346 if (ret->u_name == NULL) {
347 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
348 afp_errno = AFPERR_PARAM;
357 * @brief Build struct path from struct dir
359 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
360 * 1. move cwd into parent dir (we're often already there, but not always)
361 * 2. set struct path to the dirname
363 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
364 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
365 * 4. indicate there's no dir for this path
368 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
370 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
376 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
379 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
380 if (dir->d_m_name == dir->d_u_name) {
381 ret->u_name = ret->m_name;
383 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
384 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
389 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
390 cfrombstr(dir->d_fullpath),
391 cfrombstr(curdir->d_fullpath),
397 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
400 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
401 if (dir->d_m_name == dir->d_u_name) {
402 ret->u_name = ret->m_name;
404 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
405 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
408 ret->d_dir = NULL; /* 4 */
409 dir_remove(vol, dir); /* 5 */
416 /* DEADC0DE: never get here */
421 /*********************************************************************************************
423 ********************************************************************************************/
425 int get_afp_errno(const int param)
427 if (afp_errno != AFPERR_DID1)
433 * @brief Resolve a DID
435 * Resolve a DID, allocate a struct dir for it
436 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
437 * 2a. Check if the DID is in the cache.
438 * 2b. Check if it's really a dir (d_fullpath != NULL) because we cache files too.
439 * 3. If it's not in the cache resolve it via the database.
440 * 4. Build complete server-side path to the dir.
441 * 5. Check if it exists and is a directory.
442 * 6. Create the struct dir and populate it.
443 * 7. Add it to the cache.
445 * @param vol (r) pointer to struct vol
446 * @param did (r) DID to resolve
448 * @returns pointer to struct dir
450 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
451 * and we are really bad in this case.
453 struct dir *dirlookup(const struct vol *vol, cnid_t did)
455 static char buffer[12 + MAXPATHLEN + 1];
457 struct dir *ret = NULL, *pdir;
458 bstring fullpath = NULL;
459 char *upath = NULL, *mpath;
462 int buflen = 12 + MAXPATHLEN + 1;
466 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
468 /* check for did 0, 1 and 2 */
469 if (did == 0 || vol == NULL) { /* 1 */
470 afp_errno = AFPERR_PARAM;
472 } else if (did == DIRDID_ROOT_PARENT) {
473 rootParent.d_vid = vol->v_vid;
474 return (&rootParent);
475 } else if (did == DIRDID_ROOT) {
479 /* Search the cache */
480 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
481 if (ret->d_fullpath == NULL) { /* 2b */
482 afp_errno = AFPERR_BADTYPE;
485 if (lstat(cfrombstr(ret->d_fullpath), &st) != 0) {
486 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
490 /* It's not there anymore, so remove it */
491 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
492 dir_remove(vol, ret);
493 afp_errno = AFPERR_NOOBJ;
504 utf8 = utf8_encoding();
505 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
507 /* Get it from the database */
509 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL
510 || (upath = strdup(upath)) == NULL) { /* 3 */
511 afp_errno = AFPERR_NOOBJ;
518 * Recurse up the tree, terminates in dirlookup when either
519 * - DIRDID_ROOT is hit
520 * - a cached entry is found
522 if ((pdir = dirlookup(vol, pdid)) == NULL) {
527 /* build the fullpath */
528 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
529 || bconchar(fullpath, '/') != BSTR_OK
530 || bcatcstr(fullpath, upath) != BSTR_OK) {
535 /* stat it and check if it's a dir */
536 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstr(fullpath));
538 if (stat(cfrombstr(fullpath), &st) != 0) { /* 5a */
541 afp_errno = AFPERR_NOOBJ;
545 afp_errno = AFPERR_ACCESS;
549 afp_errno = AFPERR_MISC;
554 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
555 afp_errno = AFPERR_BADTYPE;
561 /* Get macname from unix name */
562 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
563 afp_errno = AFPERR_NOOBJ;
568 /* Create struct dir */
569 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath, st.st_ctime)) == NULL) { /* 6 */
570 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
575 /* Add it to the cache only if it's a dir */
576 if (dircache_add(ret) != 0) { /* 7 */
581 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
582 ntohl(did), ntohl(pdid), cfrombstr(ret->d_fullpath));
585 if (upath) free(upath);
587 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
588 ntohl(did), AfpErr2name(afp_errno));
599 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
601 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
606 static u_int32_t did = 0;
607 static char cname[MAXPATHLEN];
608 static char lname[MAXPATHLEN];
609 ucs2_t u2_path[MAXPATHLEN];
610 ucs2_t u2_dename[MAXPATHLEN];
611 char *tmp, *savepath;
613 if (!(vol->v_flags & AFPVOL_CASEINSEN))
616 if (veto_file(ENUMVETO, path->u_name))
619 savepath = path->u_name;
621 /* very simple cache */
622 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
623 path->u_name = cname;
625 if (of_stat( path ) == 0 ) {
628 /* something changed, we cannot stat ... */
632 if (NULL == ( dp = opendir( "." )) ) {
633 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
638 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
639 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
640 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
642 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
644 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
645 if (NULL == check_dirent(vol, de->d_name))
648 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
651 if (strcasecmp_w( u2_path, u2_dename) == 0) {
653 strlcpy(cname, de->d_name, sizeof(cname));
654 path->u_name = cname;
656 if (of_stat( path ) == 0 ) {
657 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
658 strlcpy(lname, tmp, sizeof(lname));
671 /* invalidate cache */
674 path->u_name = savepath;
676 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
682 * @brief Construct struct dir
684 * Construct struct dir from parameters.
686 * @param m_name (r) directory name in UTF8-dec
687 * @param u_name (r) directory name in server side encoding
688 * @param vol (r) pointer to struct vol
689 * @param pdid (r) Parent CNID
690 * @param did (r) CNID
691 * @param path (r) Full unix path to dir or NULL for files
692 * @param ctime (r) st_ctime from stat
694 * @returns pointer to new struct dir or NULL on error
696 * @note Most of the time mac name and unix name are the same.
698 struct dir *dir_new(const char *m_name,
700 const struct vol *vol,
708 dir = (struct dir *) calloc(1, sizeof( struct dir ));
712 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
717 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
720 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
721 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
722 dir->d_m_name_ucs2 = NULL;
725 if (m_name == u_name || !strcmp(m_name, u_name)) {
726 dir->d_u_name = dir->d_m_name;
728 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
729 bdestroy(dir->d_m_name);
736 dir->d_vid = vol->v_vid;
737 dir->d_fullpath = path;
738 dir->ctime_dircache = ctime;
743 * @brief Free a struct dir and all its members
745 * @param (rw) pointer to struct dir
747 void dir_free(struct dir *dir)
749 if (dir->d_u_name != dir->d_m_name) {
750 bdestroy(dir->d_u_name);
752 if (dir->d_m_name_ucs2)
753 free(dir->d_m_name_ucs2);
754 bdestroy(dir->d_m_name);
755 bdestroy(dir->d_fullpath);
760 * @brief Create struct dir from struct path
762 * Create a new struct dir from struct path. Then add it to the cache.
764 * 1. Open adouble file, get CNID from it.
765 * 2. Search the database, hinting with the CNID from (1).
766 * 3. Build fullpath and create struct dir.
767 * 4. Add it to the cache.
769 * @param vol (r) pointer to struct vol, possibly modified in callee
770 * @param dir (r) pointer to parrent directory
771 * @param path (rw) pointer to struct path with valid path->u_name
772 * @param len (r) strlen of path->u_name
774 * @returns Pointer to new struct dir or NULL on error.
776 * @note Function also assigns path->m_name from path->u_name.
778 struct dir *dir_add(struct vol *vol, const struct dir *dir, struct path *path, int len)
781 struct dir *cdir = NULL;
784 struct adouble *adp = NULL;
792 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name), path->st.st_ctime)) != NULL) {
793 /* there's a stray entry in the dircache */
794 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
795 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
796 ntohl(cdir->d_did), cfrombstr(dir->d_fullpath));
797 if (dir_remove(vol, cdir) != 0) {
799 AFP_PANIC("dir_add");
803 /* get_id needs adp for reading CNID from adouble file */
804 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
805 if ((ad_open(&ad, path->u_name, ADFLAGS_HF | ADFLAGS_DIR)) == 0) /* 1 */
809 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
815 ad_close_metadata(adp);
817 /* Get macname from unixname */
818 if (path->m_name == NULL) {
819 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
826 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
827 || (bconchar(fullpath, '/') != BSTR_OK)
828 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
829 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
834 /* Allocate and initialize struct dir */
835 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath, path->st.st_ctime)) == NULL) { /* 3 */
840 if ((dircache_add(cdir)) != 0) { /* 4 */
841 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstr(fullpath));
847 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
848 cfrombstr(dir->d_u_name), path->u_name, err);
851 ad_close_metadata(adp);
852 if (!cdir && fullpath)
859 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
860 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
861 ntohl(cdir->d_did), cfrombstr(cdir->d_fullpath));
868 * Free the queue with invalid struct dirs
870 * This gets called at the end of every AFP func.
872 void dir_free_invalid_q(void)
875 while (dir = (struct dir *)dequeue(invalid_dircache_entries))
880 * @brief Remove a dir from a cache and queue it for freeing
882 * 1. Check if the dir is locked or has opened forks
883 * 2. If it's a request to remove curdir, just chdir to volume root
884 * 3. Remove it from the cache
885 * 4. Queue it for removal
886 * 5. Mark it as invalid
888 * @param (r) pointer to struct vol
889 * @param (rw) pointer to struct dir
891 int dir_remove(const struct vol *vol, struct dir *dir)
896 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
899 if (dir->d_flags & DIRF_CACHELOCK) { /* 1 */
900 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): dir is locked",
901 ntohl(dir->d_did), cfrombstr(dir->d_u_name));
905 if (curdir == dir) { /* 2 */
906 if (movecwd(vol, vol->v_root) < 0) {
907 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
911 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
912 ntohl(dir->d_did), cfrombstr(dir->d_u_name));
914 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 3 */
915 enqueue(invalid_dircache_entries, dir); /* 4 */
916 dir->d_did = CNID_INVALID; /* 5 */
921 * @brief Modify a struct dir, adjust cache
923 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
924 * If given new_uname == new_mname, new_uname will point to new_mname.
926 * @param vol (r) pointer to struct vol
927 * @param dir (rw) pointer to struct dir
928 * @param pdid (r) new parent DID
929 * @param did (r) new DID
930 * @param new_mname (r) new mac-name
931 * @param new_uname (r) new unix-name
932 * @param pdir_fullpath (r) new fullpath of parent dir
934 int dir_modify(const struct vol *vol,
938 const char *new_mname,
939 const char *new_uname,
940 bstring pdir_fullpath)
944 /* Remove it from the cache */
945 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
953 /* free uname if it's not the same as mname */
954 if (dir->d_m_name != dir->d_u_name)
955 bdestroy(dir->d_u_name);
957 if (new_uname == NULL)
958 new_uname = new_mname;
960 /* assign new name */
961 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
962 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
966 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
967 dir->d_u_name = dir->d_m_name;
969 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
970 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
977 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
979 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
981 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
985 if (dir->d_m_name_ucs2)
986 free(dir->d_m_name_ucs2);
987 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))
988 dir->d_m_name_ucs2 = NULL;
990 /* Re-add it to the cache */
991 if ((dircache_add(dir)) != 0) {
993 AFP_PANIC("dir_modify");
1000 * @brief Resolve a catalog node name path
1002 * 1. Evaluate path type
1003 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
1004 * path from dirname, so eg getdirparams has sth it can chew on. curdir
1005 * is dir parent then. All this is done in path_from_dir().
1006 * 3. Parse next cnode name in path, cases:
1007 * 4. single "\0" -> do nothing
1008 * 5. two or more consecutive "\0" -> chdir("..") one or more times
1009 * 6. cnode name -> copy it to path.m_name
1010 * 7. Get unix name from mac name
1011 * 8. Special handling of request with did 1
1012 * 9. stat the cnode name
1013 * 10. If it's not there, it's probably an afp_createfile|dir,
1014 * return with curdir = dir parent, struct path = dirname
1015 * 11. If it's there and it's a file, it must should be the last element of the requested
1016 * path. Return with curdir = cnode name parent dir, struct path = filename
1017 * 12. Treat symlinks like files, dont follow them
1018 * 13. If it's a dir:
1019 * 14. Search the dircache for it
1020 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
1021 * 16. chdir into the dir and
1022 * 17. set m_name to the mac equivalent of "."
1025 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1027 static char path[ MAXPATHLEN + 1];
1028 static struct path ret;
1038 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstr(dir->d_fullpath));
1041 afp_errno = AFPERR_NOOBJ;
1042 memset(&ret, 0, sizeof(ret));
1044 switch (ret.m_type = *data) { /* 1 */
1047 len = (unsigned char) *data++;
1049 if (afp_version >= 30) {
1055 if (afp_version >= 30) {
1057 memcpy(&hint, data, sizeof(hint));
1059 data += sizeof(hint);
1061 memcpy(&len16, data, sizeof(len16));
1067 /* else it's an error */
1069 afp_errno = AFPERR_PARAM;
1072 *cpath += len + size;
1077 if (movecwd(vol, dir) < 0 ) {
1078 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1079 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1081 return path_from_dir(vol, dir, &ret);
1086 while (len) { /* 3 */
1087 if (*data == 0) { /* 4 or 5 */
1090 while (len > 0 && *data == 0) { /* 5 */
1091 /* chdir to parrent dir */
1092 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1094 if (movecwd( vol, dir ) < 0 ) {
1095 dir_remove(vol, dir);
1105 for ( p = path; *data != 0 && len > 0; len-- ) {
1107 if (p > &path[ MAXPATHLEN]) {
1108 afp_errno = AFPERR_PARAM;
1112 *p = 0; /* Terminate string */
1115 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1116 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1120 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstr(dir->d_fullpath), ret.u_name);
1122 /* Prevent access to our special folders like .AppleDouble */
1123 if (check_name(vol, ret.u_name)) {
1124 /* the name is illegal */
1125 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1126 afp_errno = AFPERR_PARAM;
1130 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1132 * Special case: CNID 1
1133 * root parent (did 1) has one child: the volume. Requests for did=1 with
1134 * some <name> must check against the volume name.
1136 if ((strcmp(cfrombstr(vol->v_root->d_m_name), ret.m_name)) == 0)
1142 * CNID != 1, eg. most of the times we take this way.
1143 * Now check if current path-part is a file or dir:
1144 * o if it's dir we have to step into it
1145 * o if it's a file we expect it to be the last part of the requested path
1146 * and thus call continue which should terminate the while loop because
1149 if (of_stat(&ret) != 0) { /* 9 */
1151 * ret.u_name doesn't exist, might be afp_createfile|dir
1152 * that means it should have been the last part
1155 /* it wasn't the last part, so we have a bogus path request */
1156 afp_errno = AFPERR_NOOBJ;
1160 * this will terminate clean in while (1) because len == 0,
1161 * probably afp_createfile|dir
1163 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}",
1164 cfrombstr(dir->d_fullpath), ret.u_name);
1168 switch (ret.st.st_mode & S_IFMT) {
1169 case S_IFREG: /* 11 */
1170 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}",
1171 cfrombstr(dir->d_fullpath), ret.u_name);
1173 /* it wasn't the last part, so we have a bogus path request */
1174 afp_errno = AFPERR_PARAM;
1177 continue; /* continues while loop */
1178 case S_IFLNK: /* 12 */
1179 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}",
1180 cfrombstr(dir->d_fullpath), ret.u_name);
1182 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}",
1183 cfrombstr(dir->d_fullpath), ret.u_name);
1184 afp_errno = AFPERR_PARAM;
1187 continue; /* continues while loop */
1188 case S_IFDIR: /* 13 */
1191 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1192 afp_errno = AFPERR_NODIR;
1196 /* Search the cache */
1197 int unamelen = strlen(ret.u_name);
1198 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen, ret.st.st_ctime); /* 14 */
1200 /* Not in cache, create one */
1201 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1202 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1203 ntohl(dir->d_did), ret.u_name, getcwdpath());
1207 } /* if/else cnid==1 */
1209 /* Now chdir to the evaluated dir */
1210 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1211 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1212 cfrombstr(curdir->d_fullpath), cfrombstr(cdir->d_fullpath), strerror(errno));
1214 return path_from_dir(vol, cdir, &ret);
1219 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1222 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1223 afp_errno = AFPERR_DID1;
1227 if (ret.m_name[0] == 0) {
1228 /* Last part was a dir */
1229 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1233 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1234 cfrombstr(dir->d_fullpath),
1235 cfrombstr(curdir->d_fullpath),
1242 * @brief chdir() to dir
1244 * @param vol (r) pointer to struct vol
1245 * @param dir (r) pointer to struct dir
1247 * @returns 0 on success, -1 on error with afp_errno set appropiately
1249 int movecwd(const struct vol *vol, struct dir *dir)
1256 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1257 cfrombstr(curdir->d_fullpath), getcwdpath());
1261 if (dir->d_did == DIRDID_ROOT_PARENT) {
1262 curdir = &rootParent;
1266 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')",
1267 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1269 if ((ret = lchdir(cfrombstr(dir->d_fullpath))) != 0 ) {
1270 LOG(log_debug, logtype_afpd, "movecwd('%s'): ret: %u, %s",
1271 cfrombstr(dir->d_fullpath), ret, strerror(errno));
1273 /* p is a symlink or getcwd failed */
1274 afp_errno = AFPERR_BADTYPE;
1276 if (chdir(vol->v_path ) < 0) {
1277 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1278 /* XXX what do we do here? */
1280 curdir = vol->v_root;
1287 afp_errno = AFPERR_ACCESS;
1290 afp_errno = AFPERR_NOOBJ;
1300 * We can't use unix file's perm to support Apple's inherited protection modes.
1301 * If we aren't the file's owner we can't change its perms when moving it and smb
1302 * nfs,... don't even try.
1304 #define AFP_CHECK_ACCESS
1306 int check_access(char *path, int mode)
1308 #ifdef AFP_CHECK_ACCESS
1316 accessmode(p, &ma, curdir, NULL);
1317 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1319 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1325 /* --------------------- */
1326 int file_access(struct path *path, int mode)
1330 accessmode(path->u_name, &ma, curdir, &path->st);
1331 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1333 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1339 /* --------------------- */
1340 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1342 dir->offcnt = count;
1343 dir->ctime = st->st_ctime;
1344 dir->d_flags &= ~DIRF_CNID;
1348 /* ---------------------
1349 * is our cached also for reenumerate id?
1351 int dirreenumerate(struct dir *dir, struct stat *st)
1353 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1356 /* ------------------------------
1358 (name, dir) with curdir:name == dir, from afp_enumerate
1361 int getdirparams(const struct vol *vol,
1362 u_int16_t bitmap, struct path *s_path,
1364 char *buf, size_t *buflen )
1368 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1369 int bit = 0, isad = 0;
1375 struct stat *st = &s_path->st;
1376 char *upath = s_path->u_name;
1378 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1379 (1 << DIRPBIT_CDATE) |
1380 (1 << DIRPBIT_MDATE) |
1381 (1 << DIRPBIT_BDATE) |
1382 (1 << DIRPBIT_FINFO)))) {
1384 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1385 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) )
1392 while ( bitmap != 0 ) {
1393 while (( bitmap & 1 ) == 0 ) {
1401 ad_getattr(&ad, &ashort);
1402 } else if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1403 ashort = htons(ATTRBIT_INVISIBLE);
1406 ashort |= htons(ATTRBIT_SHARED);
1407 memcpy( data, &ashort, sizeof( ashort ));
1408 data += sizeof( ashort );
1412 memcpy( data, &pdid, sizeof( pdid ));
1413 data += sizeof( pdid );
1414 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1415 s_path->u_name, ntohl(pdid));
1418 case DIRPBIT_CDATE :
1419 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1420 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1421 memcpy( data, &aint, sizeof( aint ));
1422 data += sizeof( aint );
1425 case DIRPBIT_MDATE :
1426 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1427 memcpy( data, &aint, sizeof( aint ));
1428 data += sizeof( aint );
1431 case DIRPBIT_BDATE :
1432 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1433 aint = AD_DATE_START;
1434 memcpy( data, &aint, sizeof( aint ));
1435 data += sizeof( aint );
1438 case DIRPBIT_FINFO :
1440 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1441 } else { /* no appledouble */
1442 memset( data, 0, 32 );
1443 /* set default view -- this also gets done in ad_open() */
1444 ashort = htons(FINDERINFO_CLOSEDVIEW);
1445 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1447 /* dot files are by default visible */
1448 if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1449 ashort = htons(FINDERINFO_INVISIBLE);
1450 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1456 case DIRPBIT_LNAME :
1457 if (dir->d_m_name) /* root of parent can have a null name */
1460 memset(data, 0, sizeof(u_int16_t));
1461 data += sizeof( u_int16_t );
1464 case DIRPBIT_SNAME :
1465 memset(data, 0, sizeof(u_int16_t));
1466 data += sizeof( u_int16_t );
1470 memcpy( data, &dir->d_did, sizeof( aint ));
1471 data += sizeof( aint );
1472 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1473 s_path->u_name, ntohl(dir->d_did));
1476 case DIRPBIT_OFFCNT :
1478 /* this needs to handle current directory access rights */
1479 if (diroffcnt(dir, st)) {
1480 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1482 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1483 setdiroffcnt(dir, st, ret);
1484 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1486 ashort = htons( ashort );
1487 memcpy( data, &ashort, sizeof( ashort ));
1488 data += sizeof( ashort );
1492 aint = htonl(st->st_uid);
1493 memcpy( data, &aint, sizeof( aint ));
1494 data += sizeof( aint );
1498 aint = htonl(st->st_gid);
1499 memcpy( data, &aint, sizeof( aint ));
1500 data += sizeof( aint );
1503 case DIRPBIT_ACCESS :
1504 accessmode( upath, &ma, dir , st);
1506 *data++ = ma.ma_user;
1507 *data++ = ma.ma_world;
1508 *data++ = ma.ma_group;
1509 *data++ = ma.ma_owner;
1512 /* Client has requested the ProDOS information block.
1513 Just pass back the same basic block for all
1514 directories. <shirsch@ibm.net> */
1515 case DIRPBIT_PDINFO :
1516 if (afp_version >= 30) { /* UTF8 name */
1517 utf8 = kTextEncodingUTF8;
1518 if (dir->d_m_name) /* root of parent can have a null name */
1521 memset(data, 0, sizeof(u_int16_t));
1522 data += sizeof( u_int16_t );
1524 memcpy(data, &aint, sizeof( aint ));
1525 data += sizeof( aint );
1527 else { /* ProDOS Info Block */
1530 ashort = htons( 0x0200 );
1531 memcpy( data, &ashort, sizeof( ashort ));
1532 data += sizeof( ashort );
1533 memset( data, 0, sizeof( ashort ));
1534 data += sizeof( ashort );
1538 case DIRPBIT_UNIXPR :
1539 aint = htonl(st->st_uid);
1540 memcpy( data, &aint, sizeof( aint ));
1541 data += sizeof( aint );
1542 aint = htonl(st->st_gid);
1543 memcpy( data, &aint, sizeof( aint ));
1544 data += sizeof( aint );
1547 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1548 memcpy( data, &aint, sizeof( aint ));
1549 data += sizeof( aint );
1551 accessmode( upath, &ma, dir , st);
1553 *data++ = ma.ma_user;
1554 *data++ = ma.ma_world;
1555 *data++ = ma.ma_group;
1556 *data++ = ma.ma_owner;
1561 ad_close_metadata( &ad );
1563 return( AFPERR_BITMAP );
1569 ashort = htons( data - buf );
1570 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1571 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, 0);
1573 if ( utf_nameoff ) {
1574 ashort = htons( data - buf );
1575 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1576 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, utf8);
1579 ad_close_metadata( &ad );
1581 *buflen = data - buf;
1585 /* ----------------------------- */
1586 int path_error(struct path *path, int error)
1588 /* - a dir with access error
1589 * - no error it's a file
1592 if (path_isadir(path))
1594 if (path->st_valid && path->st_errno)
1596 return AFPERR_BADTYPE ;
1599 /* ----------------------------- */
1600 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1605 u_int16_t vid, bitmap;
1611 memcpy( &vid, ibuf, sizeof( vid ));
1612 ibuf += sizeof( vid );
1614 if (NULL == ( vol = getvolbyvid( vid )) ) {
1615 return( AFPERR_PARAM );
1618 if (vol->v_flags & AFPVOL_RO)
1619 return AFPERR_VLOCK;
1621 memcpy( &did, ibuf, sizeof( did ));
1622 ibuf += sizeof( int );
1624 if (NULL == ( dir = dirlookup( vol, did )) ) {
1628 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1629 bitmap = ntohs( bitmap );
1630 ibuf += sizeof( bitmap );
1632 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1633 return get_afp_errno(AFPERR_NOOBJ);
1636 if ( *path->m_name != '\0' ) {
1637 rc = path_error(path, AFPERR_NOOBJ);
1638 /* maybe we are trying to set perms back */
1639 if (rc != AFPERR_ACCESS)
1644 * If ibuf is odd, make it even.
1646 if ((u_long)ibuf & 1 ) {
1650 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1651 setvoltime(obj, vol );
1657 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1659 * assume path == '\0' eg. it's a directory in canonical form
1661 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1673 u_int16_t ashort, bshort, oshort;
1675 int change_mdate = 0;
1676 int change_parent_mdate = 0;
1678 u_int16_t bitmap = d_bitmap;
1679 u_char finder_buf[32];
1682 u_int16_t upriv_bit = 0;
1685 upath = path->u_name;
1687 while ( bitmap != 0 ) {
1688 while (( bitmap & 1 ) == 0 ) {
1696 memcpy( &ashort, buf, sizeof( ashort ));
1697 buf += sizeof( ashort );
1699 case DIRPBIT_CDATE :
1701 memcpy(&cdate, buf, sizeof(cdate));
1702 buf += sizeof( cdate );
1704 case DIRPBIT_MDATE :
1705 memcpy(&newdate, buf, sizeof(newdate));
1706 buf += sizeof( newdate );
1708 case DIRPBIT_BDATE :
1710 memcpy(&bdate, buf, sizeof(bdate));
1711 buf += sizeof( bdate );
1713 case DIRPBIT_FINFO :
1715 memcpy( finder_buf, buf, 32 );
1718 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1719 change_parent_mdate = 1;
1720 memcpy( &owner, buf, sizeof(owner));
1721 buf += sizeof( owner );
1724 change_parent_mdate = 1;
1725 memcpy( &group, buf, sizeof( group ));
1726 buf += sizeof( group );
1728 case DIRPBIT_ACCESS :
1730 change_parent_mdate = 1;
1731 ma.ma_user = *buf++;
1732 ma.ma_world = *buf++;
1733 ma.ma_group = *buf++;
1734 ma.ma_owner = *buf++;
1735 mpriv = mtoumode( &ma ) | vol->v_dperm;
1736 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1737 err = set_dir_errors(path, "setdirmode", errno);
1741 /* Ignore what the client thinks we should do to the
1742 ProDOS information block. Skip over the data and
1743 report nothing amiss. <shirsch@ibm.net> */
1744 case DIRPBIT_PDINFO :
1745 if (afp_version < 30) {
1749 err = AFPERR_BITMAP;
1753 case DIRPBIT_UNIXPR :
1754 if (vol_unix_priv(vol)) {
1755 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1756 buf += sizeof( owner );
1757 memcpy( &group, buf, sizeof( group ));
1758 buf += sizeof( group );
1761 change_parent_mdate = 1;
1762 memcpy( &upriv, buf, sizeof( upriv ));
1763 buf += sizeof( upriv );
1764 upriv = ntohl (upriv) | vol->v_dperm;
1765 if (dir_rx_set(upriv)) {
1766 /* maybe we are trying to set perms back */
1767 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1769 err = set_dir_errors(path, "setdirunixmode", errno);
1780 err = AFPERR_BITMAP;
1788 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1790 if (ad_open(&ad, upath, ADFLAGS_HF | ADFLAGS_DIR, O_CREAT, 0777) != 0) {
1792 * Check to see what we're trying to set. If it's anything
1793 * but ACCESS, UID, or GID, give an error. If it's any of those
1794 * three, we don't need the ad to be open, so just continue.
1796 * note: we also don't need to worry about mdate. also, be quiet
1797 * if we're using the noadouble option.
1799 if (!vol_noadouble(vol) && (d_bitmap &
1800 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1801 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1802 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1803 return AFPERR_ACCESS;
1809 * Check to see if a create was necessary. If it was, we'll want
1810 * to set our name, etc.
1812 if ( (ad_get_MD_flags( &ad ) & O_CREAT)) {
1813 ad_setname(&ad, cfrombstr(curdir->d_m_name));
1819 while ( bitmap != 0 ) {
1820 while (( bitmap & 1 ) == 0 ) {
1828 ad_getattr(&ad, &bshort);
1830 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1831 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1835 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1836 change_parent_mdate = 1;
1837 ad_setattr(&ad, bshort);
1840 case DIRPBIT_CDATE :
1842 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1845 case DIRPBIT_MDATE :
1847 case DIRPBIT_BDATE :
1849 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1852 case DIRPBIT_FINFO :
1854 /* Fixes #2802236 */
1855 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1856 *fflags &= htons(~FINDERINFO_ISHARED);
1858 if ( dir->d_did == DIRDID_ROOT ) {
1860 * Alright, we admit it, this is *really* sick!
1861 * The 4 bytes that we don't copy, when we're dealing
1862 * with the root of a volume, are the directory's
1863 * location information. This eliminates that annoying
1864 * behavior one sees when mounting above another mount
1867 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1868 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1870 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1874 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1875 if ( (dir->d_did == DIRDID_ROOT) &&
1876 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1877 err = set_dir_errors(path, "setdeskowner", errno);
1878 if (isad && err == AFPERR_PARAM) {
1879 err = AFP_OK; /* ???*/
1882 goto setdirparam_done;
1885 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1886 err = set_dir_errors(path, "setdirowner", errno);
1887 goto setdirparam_done;
1891 if (dir->d_did == DIRDID_ROOT)
1892 setdeskowner( -1, ntohl(group) );
1893 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1894 err = set_dir_errors(path, "setdirowner", errno);
1895 goto setdirparam_done;
1898 case DIRPBIT_ACCESS :
1899 if (dir->d_did == DIRDID_ROOT) {
1901 if (!dir_rx_set(mpriv)) {
1902 /* we can't remove read and search for owner on volume root */
1903 err = AFPERR_ACCESS;
1904 goto setdirparam_done;
1908 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1909 err = set_dir_errors(path, "setdirmode", errno);
1910 goto setdirparam_done;
1913 case DIRPBIT_PDINFO :
1914 if (afp_version >= 30) {
1915 err = AFPERR_BITMAP;
1916 goto setdirparam_done;
1919 case DIRPBIT_UNIXPR :
1920 if (vol_unix_priv(vol)) {
1921 if (dir->d_did == DIRDID_ROOT) {
1922 if (!dir_rx_set(upriv)) {
1923 /* we can't remove read and search for owner on volume root */
1924 err = AFPERR_ACCESS;
1925 goto setdirparam_done;
1927 setdeskowner( -1, ntohl(group) );
1928 setdeskmode( upriv );
1930 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1931 err = set_dir_errors(path, "setdirowner", errno);
1932 goto setdirparam_done;
1935 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1936 err = set_dir_errors(path, "setdirunixmode", errno);
1937 goto setdirparam_done;
1941 err = AFPERR_BITMAP;
1942 goto setdirparam_done;
1946 err = AFPERR_BITMAP;
1947 goto setdirparam_done;
1956 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1957 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1961 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1962 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1967 if (path->st_valid && !path->st_errno) {
1968 struct stat *st = &path->st;
1970 if (dir && dir->d_pdid) {
1971 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1975 ad_close_metadata( &ad);
1978 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1979 && gettimeofday(&tv, NULL) == 0) {
1980 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1981 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1982 /* be careful with bitmap because now dir is null */
1983 bitmap = 1<<DIRPBIT_MDATE;
1984 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1985 /* should we reset curdir ?*/
1992 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2006 memcpy( &vid, ibuf, sizeof( vid ));
2007 ibuf += sizeof( vid );
2008 if (NULL == (vol = getvolbyvid( vid )) ) {
2009 return( AFPERR_PARAM );
2012 memcpy( &did, ibuf, sizeof( did ));
2013 ibuf += sizeof( did );
2017 * if it's CNID 2 our only choice to meet the specs is call sync.
2018 * For any other CNID just sync that dir. To my knowledge the
2019 * intended use of FPSyncDir is to sync the volume so all we're
2020 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2023 if ( ntohl(did) == 2 ) {
2026 if (NULL == ( dir = dirlookup( vol, did )) ) {
2027 return afp_errno; /* was AFPERR_NOOBJ */
2030 if (movecwd( vol, dir ) < 0 )
2031 return ( AFPERR_NOOBJ );
2034 * Assuming only OSens that have dirfd also may require fsyncing directories
2035 * in order to flush metadata e.g. Linux.
2039 if (NULL == ( dp = opendir( "." )) ) {
2042 return( AFPERR_NOOBJ );
2044 return( AFPERR_ACCESS );
2046 return( AFPERR_PARAM );
2050 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2053 if ( fsync ( dfd ) < 0 )
2054 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2055 dir->d_u_name, strerror(errno) );
2056 closedir(dp); /* closes dfd too */
2059 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2062 return( AFPERR_NOOBJ );
2064 return( AFPERR_ACCESS );
2066 return( AFPERR_PARAM );
2070 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2071 vol->ad_path(".", ADFLAGS_DIR) );
2073 if ( fsync(dfd) < 0 )
2074 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2075 vol->ad_path(cfrombstr(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2082 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2088 struct path *s_path;
2096 memcpy( &vid, ibuf, sizeof( vid ));
2097 ibuf += sizeof( vid );
2098 if (NULL == ( vol = getvolbyvid( vid )) ) {
2099 return( AFPERR_PARAM );
2102 if (vol->v_flags & AFPVOL_RO)
2103 return AFPERR_VLOCK;
2105 memcpy( &did, ibuf, sizeof( did ));
2106 ibuf += sizeof( did );
2107 if (NULL == ( dir = dirlookup( vol, did )) ) {
2108 return afp_errno; /* was AFPERR_NOOBJ */
2110 /* for concurrent access we need to be sure we are not in the
2111 * folder we want to create...
2115 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2116 return get_afp_errno(AFPERR_PARAM);
2118 /* cname was able to move curdir to it! */
2119 if (*s_path->m_name == '\0')
2120 return AFPERR_EXIST;
2122 upath = s_path->u_name;
2124 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2128 if (of_stat(s_path) < 0) {
2134 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2138 if ( movecwd( vol, dir ) < 0 ) {
2139 return( AFPERR_PARAM );
2142 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2143 if (ad_open(&ad, ".", ADFLAGS_HF | ADFLAGS_DIR, O_CREAT, 0777) < 0) {
2144 if (vol_noadouble(vol))
2145 goto createdir_done;
2146 return( AFPERR_ACCESS );
2148 ad_setname(&ad, s_path->m_name);
2149 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2152 ad_close_metadata( &ad);
2155 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2156 *rbuflen = sizeof( u_int32_t );
2157 setvoltime(obj, vol );
2162 * dst new unix filename (not a pathname)
2163 * newname new mac name
2165 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2167 int renamedir(const struct vol *vol,
2172 struct dir *newparent,
2178 /* existence check moved to afp_moveandrename */
2179 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2182 return( AFPERR_NOOBJ );
2184 return( AFPERR_ACCESS );
2186 return AFPERR_VLOCK;
2188 /* tried to move directory into a subdirectory of itself */
2189 return AFPERR_CANTMOVE;
2191 /* this needs to copy and delete. bleah. that means we have
2192 * to deal with entire directory hierarchies. */
2193 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2197 if ((err = deletedir(dirfd, src)) < 0)
2201 return( AFPERR_PARAM );
2205 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2207 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2209 if (ad_open(&ad, dst, ADFLAGS_HF | ADFLAGS_DIR) == 0) {
2210 ad_setname(&ad, newname);
2212 ad_close_metadata( &ad);
2218 /* delete an empty directory */
2219 int deletecurdir(struct vol *vol)
2229 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2230 return( AFPERR_ACCESS );
2235 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2236 /* we never want to create a resource fork here, we are going to delete it */
2237 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2239 ad_getattr(&ad, &ashort);
2240 ad_close_metadata(&ad);
2241 if ((ashort & htons(ATTRBIT_NODELETE))) {
2242 return AFPERR_OLOCK;
2245 err = vol->vfs->vfs_deletecurdir(vol);
2250 /* now get rid of dangling symlinks */
2251 if ((dp = opendir("."))) {
2252 while ((de = readdir(dp))) {
2253 /* skip this and previous directory */
2254 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2257 /* bail if it's not a symlink */
2258 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2260 return AFPERR_DIRNEMPT;
2263 if ((err = netatalk_unlink(de->d_name))) {
2270 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2275 err = netatalk_rmdir_all_errors(-1, cfrombstr(fdir->d_u_name));
2276 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2277 cnid_delete(vol->v_cdb, fdir->d_did);
2278 dir_remove( vol, fdir );
2282 /* inode is used as key for cnid.
2283 * Close the descriptor only after cnid_delete
2291 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2301 sfunc = (unsigned char) *ibuf++;
2304 if (sfunc >= 3 && sfunc <= 6) {
2305 if (afp_version < 30) {
2306 return( AFPERR_PARAM );
2313 case 3 :/* unicode */
2314 memcpy( &id, ibuf, sizeof( id ));
2317 if (( pw = getpwuid( id )) == NULL ) {
2318 return( AFPERR_NOITEM );
2320 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2321 pw->pw_name, -1, &name);
2328 case 4 : /* unicode */
2329 memcpy( &id, ibuf, sizeof( id ));
2332 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2333 return( AFPERR_NOITEM );
2335 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2336 gr->gr_name, -1, &name);
2343 case 5 : /* UUID -> username */
2344 case 6 : /* UUID -> groupname */
2345 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2346 return AFPERR_PARAM;
2347 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2349 len = getnamefromuuid((unsigned char*) ibuf, &name, &type);
2350 if (len != 0) /* its a error code, not len */
2351 return AFPERR_NOITEM;
2354 if (( pw = getpwnam( name )) == NULL )
2355 return( AFPERR_NOITEM );
2356 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2357 id = htonl(UUID_USER);
2358 memcpy( rbuf, &id, sizeof( id ));
2359 id = htonl( pw->pw_uid);
2360 rbuf += sizeof( id );
2361 memcpy( rbuf, &id, sizeof( id ));
2362 rbuf += sizeof( id );
2363 *rbuflen = 2 * sizeof( id );
2366 if (( gr = getgrnam( name )) == NULL )
2367 return( AFPERR_NOITEM );
2368 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2369 id = htonl(UUID_GROUP);
2370 memcpy( rbuf, &id, sizeof( id ));
2371 rbuf += sizeof( id );
2372 id = htonl( gr->gr_gid);
2373 memcpy( rbuf, &id, sizeof( id ));
2374 rbuf += sizeof( id );
2375 *rbuflen = 2 * sizeof( id );
2379 return (AFPERR_NOITEM);
2386 return( AFPERR_PARAM );
2390 len = strlen( name );
2393 u_int16_t tp = htons(len);
2394 memcpy(rbuf, &tp, sizeof(tp));
2403 memcpy( rbuf, name, len );
2411 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2420 sfunc = (unsigned char) *ibuf++;
2422 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2425 case 2 : /* unicode */
2426 if (afp_version < 30) {
2427 return( AFPERR_PARAM );
2429 memcpy(&ulen, ibuf, sizeof(ulen));
2432 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2436 len = (unsigned char) *ibuf++;
2438 case 5 : /* username -> UUID */
2439 case 6 : /* groupname -> UUID */
2440 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2441 return AFPERR_PARAM;
2442 memcpy(&ulen, ibuf, sizeof(ulen));
2447 return( AFPERR_PARAM );
2453 return AFPERR_PARAM;
2456 case 1 : /* unicode */
2458 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2459 return( AFPERR_NOITEM );
2463 memcpy( rbuf, &id, sizeof( id ));
2464 *rbuflen = sizeof( id );
2467 case 2 : /* unicode */
2469 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2470 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2471 return( AFPERR_NOITEM );
2474 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2476 memcpy( rbuf, &id, sizeof( id ));
2477 *rbuflen = sizeof( id );
2479 case 5 : /* username -> UUID */
2480 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2481 if (0 != getuuidfromname(ibuf, UUID_USER, (unsigned char *)rbuf))
2482 return AFPERR_NOITEM;
2483 *rbuflen = UUID_BINSIZE;
2485 case 6 : /* groupname -> UUID */
2486 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2487 if (0 != getuuidfromname(ibuf, UUID_GROUP, (unsigned char *)rbuf))
2488 return AFPERR_NOITEM;
2489 *rbuflen = UUID_BINSIZE;
2496 /* ------------------------------------
2497 variable DID support
2499 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2510 /* do nothing as dids are static for the life of the process. */
2514 memcpy(&vid, ibuf, sizeof( vid ));
2515 ibuf += sizeof( vid );
2516 if (( vol = getvolbyvid( vid )) == NULL ) {
2517 return( AFPERR_PARAM );
2520 memcpy( &did, ibuf, sizeof( did ));
2521 ibuf += sizeof( did );
2522 if (( dir = dirlookup( vol, did )) == NULL ) {
2523 return( AFPERR_PARAM );
2526 /* dir_remove -- deletedid */
2532 /* did creation gets done automatically
2533 * there's a pb again with case but move it to cname
2535 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2538 struct dir *parentdir;
2546 memcpy(&vid, ibuf, sizeof(vid));
2547 ibuf += sizeof( vid );
2549 if (NULL == ( vol = getvolbyvid( vid )) ) {
2550 return( AFPERR_PARAM );
2553 memcpy(&did, ibuf, sizeof(did));
2554 ibuf += sizeof(did);
2556 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2560 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2561 return get_afp_errno(AFPERR_PARAM);
2564 if ( *path->m_name != '\0' ) {
2565 return path_error(path, AFPERR_NOOBJ);
2568 if ( !path->st_valid && of_stat(path ) < 0 ) {
2569 return( AFPERR_NOOBJ );
2571 if ( path->st_errno ) {
2572 return( AFPERR_NOOBJ );
2575 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2576 *rbuflen = sizeof(curdir->d_did);