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 extern void addir_inherit_acl(const struct vol *vol);
49 * FIXMEs, loose ends after the dircache rewrite:
50 * o merge dircache_search_by_name and dir_add ??
51 * o case-insensitivity is gone from cname
55 /*******************************************************************************************
57 ******************************************************************************************/
60 /* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
61 struct dir rootParent = {
62 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
63 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
64 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
66 struct dir *curdir = &rootParent;
67 struct path Cur_Path = {
72 NULL,/* struct dir * */
73 0, /* stat is not set */
79 /*******************************************************************************************
81 ******************************************************************************************/
84 /* -------------------------
85 appledouble mkdir afp error code.
87 static int netatalk_mkdir(const struct vol *vol, const char *name)
92 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
93 if (lstat(".", &st) < 0)
95 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
96 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
97 name, st.st_mode, vol->v_umask);
99 ret = mkdir(name, mode);
101 ret = ad_mkdir(name, DIRBITS | 0777);
107 return( AFPERR_NOOBJ );
109 return( AFPERR_VLOCK );
112 return( AFPERR_ACCESS );
114 return( AFPERR_EXIST );
117 return( AFPERR_DFULL );
119 return( AFPERR_PARAM );
125 /* ------------------- */
126 static int deletedir(int dirfd, char *dir)
128 char path[MAXPATHLEN + 1];
136 if ((len = strlen(dir)) +2 > sizeof(path))
140 if ((dp = opendirat(dirfd, dir)) == NULL)
146 remain = sizeof(path) -len -1;
147 while ((de = readdir(dp)) && err == AFP_OK) {
148 /* skip this and previous directory */
149 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
152 if (strlen(de->d_name) > remain) {
156 strcpy(path + len, de->d_name);
157 if (lstatat(dirfd, path, &st)) {
160 if (S_ISDIR(st.st_mode)) {
161 err = deletedir(dirfd, path);
163 err = netatalk_unlinkat(dirfd, path);
168 /* okay. the directory is empty. delete it. note: we already got rid
171 err = netatalk_rmdir(dirfd, dir);
176 /* do a recursive copy. */
177 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
179 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
188 /* doesn't exist or the path is too long. */
189 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
190 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
191 ((dp = opendirat(dirfd, src)) == NULL))
194 /* try to create the destination directory */
195 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
200 /* set things up to copy */
204 srem = sizeof(spath) - slen -1;
209 drem = sizeof(dpath) - dlen -1;
212 while ((de = readdir(dp))) {
213 /* skip this and previous directory */
214 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
217 if (strlen(de->d_name) > srem) {
221 strcpy(spath + slen, de->d_name);
223 if (lstatat(dirfd, spath, &st) == 0) {
224 if (strlen(de->d_name) > drem) {
228 strcpy(dpath + dlen, de->d_name);
230 if (S_ISDIR(st.st_mode)) {
231 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
233 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
237 /* keep the same time stamp. */
238 ut.actime = ut.modtime = st.st_mtime;
244 /* keep the same time stamp. */
245 if (lstatat(dirfd, src, &st) == 0) {
246 ut.actime = ut.modtime = st.st_mtime;
255 /* ---------------------
256 * is our cached offspring count valid?
258 static int diroffcnt(struct dir *dir, struct stat *st)
260 return st->st_ctime == dir->ctime;
263 /* --------------------- */
264 static int invisible_dots(const struct vol *vol, const char *name)
266 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
269 /* ------------------ */
270 static int set_dir_errors(struct path *path, const char *where, int err)
275 return AFPERR_ACCESS;
279 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
284 * @brief Convert name in client encoding to server encoding
286 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
287 * This only gets called from cname().
289 * @returns 0 on success, -1 on error
291 * @note If the passed ret->m_name is mangled, we'll demangle it
293 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
295 static char temp[ MAXPATHLEN + 1];
299 if (afp_version >= 30) {
301 if (dir->d_did == DIRDID_ROOT_PARENT) {
303 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
304 * So we compare it with the longname from the current volume and if they match
305 * we overwrite the requested path with the utf8 volume name so that the following
308 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
309 if (strcasecmp(ret->m_name, temp) == 0)
310 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
313 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
314 afp_errno = AFPERR_PARAM;
317 strcpy(ret->m_name, temp);
321 /* check for OS X mangled filename :( */
322 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
323 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
324 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
326 if (t != ret->m_name) {
328 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
329 * flags weren't the same
331 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
332 /* at last got our view of mac name */
333 strcpy(ret->m_name, t);
336 } /* afp_version >= 30 */
338 /* If we haven't got it by now, get it */
339 if (ret->u_name == NULL) {
340 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
341 afp_errno = AFPERR_PARAM;
350 * @brief Build struct path from struct dir
352 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
353 * 1. move cwd into parent dir (we're often already there, but not always)
354 * 2. set struct path to the dirname
356 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
357 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
358 * 4. indicate there's no dir for this path
361 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
363 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
369 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
372 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
373 if (dir->d_m_name == dir->d_u_name) {
374 ret->u_name = ret->m_name;
376 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
377 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
382 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
383 cfrombstring(dir->d_fullpath),
384 cfrombstring(curdir->d_fullpath),
390 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
393 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
394 if (dir->d_m_name == dir->d_u_name) {
395 ret->u_name = ret->m_name;
397 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
398 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
401 ret->d_dir = NULL; /* 4 */
402 dir_remove(vol, dir); /* 5 */
409 /* DEADC0DE: never get here */
414 /*********************************************************************************************
416 ********************************************************************************************/
418 int get_afp_errno(const int param)
420 if (afp_errno != AFPERR_DID1)
426 * @brief Resolve a DID
428 * Resolve a DID, allocate a struct dir for it
429 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
430 * 2a. Check if the DID is in the cache.
431 * 2b. Check if it's really a dir (d_fullpath != NULL) because we cache files too.
432 * 3. If it's not in the cache resolve it via the database.
433 * 4. Build complete server-side path to the dir.
434 * 5. Check if it exists and is a directory.
435 * 6. Create the struct dir and populate it.
436 * 7. Add it to the cache.
438 * @param vol (r) pointer to struct vol
439 * @param did (r) DID to resolve
441 * @returns pointer to struct dir
443 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
444 * and we are really bad in this case.
446 struct dir *dirlookup(const struct vol *vol, cnid_t did)
448 static char buffer[12 + MAXPATHLEN + 1];
450 struct dir *ret = NULL, *pdir;
451 bstring fullpath = NULL;
452 char *upath = NULL, *mpath;
455 int buflen = 12 + MAXPATHLEN + 1;
459 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
461 /* check for did 0, 1 and 2 */
462 if (did == 0 || vol == NULL) { /* 1 */
463 afp_errno = AFPERR_PARAM;
465 } else if (did == DIRDID_ROOT_PARENT) {
466 rootParent.d_vid = vol->v_vid;
467 return (&rootParent);
468 } else if (did == DIRDID_ROOT) {
472 /* Search the cache */
473 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
474 if (ret->d_fullpath == NULL) { /* 2b */
475 afp_errno = AFPERR_BADTYPE;
478 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
479 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
483 /* It's not there anymore, so remove it */
484 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
485 dir_remove(vol, ret);
486 afp_errno = AFPERR_NOOBJ;
497 utf8 = utf8_encoding();
498 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
500 /* Get it from the database */
502 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL
503 || (upath = strdup(upath)) == NULL) { /* 3 */
504 afp_errno = AFPERR_NOOBJ;
511 * Recurse up the tree, terminates in dirlookup when either
512 * - DIRDID_ROOT is hit
513 * - a cached entry is found
515 if ((pdir = dirlookup(vol, pdid)) == NULL) {
520 /* build the fullpath */
521 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
522 || bconchar(fullpath, '/') != BSTR_OK
523 || bcatcstr(fullpath, upath) != BSTR_OK) {
528 /* stat it and check if it's a dir */
529 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
531 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
534 afp_errno = AFPERR_NOOBJ;
538 afp_errno = AFPERR_ACCESS;
542 afp_errno = AFPERR_MISC;
547 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
548 afp_errno = AFPERR_BADTYPE;
554 /* Get macname from unix name */
555 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
556 afp_errno = AFPERR_NOOBJ;
561 /* Create struct dir */
562 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
563 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
568 /* Add it to the cache only if it's a dir */
569 if (dircache_add(ret) != 0) { /* 7 */
574 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
575 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
579 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
580 ntohl(did), AfpErr2name(afp_errno));
592 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
594 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
599 static u_int32_t did = 0;
600 static char cname[MAXPATHLEN];
601 static char lname[MAXPATHLEN];
602 ucs2_t u2_path[MAXPATHLEN];
603 ucs2_t u2_dename[MAXPATHLEN];
604 char *tmp, *savepath;
606 if (!(vol->v_flags & AFPVOL_CASEINSEN))
609 if (veto_file(ENUMVETO, path->u_name))
612 savepath = path->u_name;
614 /* very simple cache */
615 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
616 path->u_name = cname;
618 if (of_stat( path ) == 0 ) {
621 /* something changed, we cannot stat ... */
625 if (NULL == ( dp = opendir( "." )) ) {
626 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
631 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
632 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
633 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
635 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
637 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
638 if (NULL == check_dirent(vol, de->d_name))
641 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
644 if (strcasecmp_w( u2_path, u2_dename) == 0) {
646 strlcpy(cname, de->d_name, sizeof(cname));
647 path->u_name = cname;
649 if (of_stat( path ) == 0 ) {
650 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
651 strlcpy(lname, tmp, sizeof(lname));
664 /* invalidate cache */
667 path->u_name = savepath;
669 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
675 * @brief Construct struct dir
677 * Construct struct dir from parameters.
679 * @param m_name (r) directory name in UTF8-dec
680 * @param u_name (r) directory name in server side encoding
681 * @param vol (r) pointer to struct vol
682 * @param pdid (r) Parent CNID
683 * @param did (r) CNID
684 * @param fullpath (r) Full unix path to dir or NULL for files
686 * @returns pointer to new struct dir or NULL on error
688 * @note Most of the time mac name and unix name are the same.
690 struct dir *dir_new(const char *m_name,
692 const struct vol *vol,
699 dir = (struct dir *) calloc(1, sizeof( struct dir ));
703 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
708 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
711 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
712 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
713 dir->d_m_name_ucs2 = NULL;
716 if (m_name == u_name || !strcmp(m_name, u_name)) {
717 dir->d_u_name = dir->d_m_name;
719 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
720 bdestroy(dir->d_m_name);
727 dir->d_vid = vol->v_vid;
728 dir->d_fullpath = path;
733 * @brief Free a struct dir and all its members
735 * @param (rw) pointer to struct dir
737 void dir_free(struct dir *dir)
739 if (dir->d_u_name != dir->d_m_name) {
740 bdestroy(dir->d_u_name);
742 if (dir->d_m_name_ucs2)
743 free(dir->d_m_name_ucs2);
744 bdestroy(dir->d_m_name);
745 bdestroy(dir->d_fullpath);
750 * @brief Create struct dir from struct path
752 * Create a new struct dir from struct path. Then add it to the cache.
753 * The caller must have assured that the dir is not already in the cache,
754 * cf theAFP_ASSERTion.
755 * 1. Open adouble file, get CNID from it.
756 * 2. Search the database, hinting with the CNID from (1).
757 * 3. Build fullpath and create struct dir.
758 * 4. Add it to the cache.
760 * @param vol (r) pointer to struct vol
761 * @param dir (r) pointer to parrent directory
762 * @param path (rw) pointer to struct path with valid path->u_name
763 * @param len (r) strlen of path->u_name
765 * @returns Pointer to new struct dir or NULL on error.
767 * @note Function also assigns path->m_name from path->u_name.
769 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
772 struct dir *cdir = NULL;
775 struct adouble *adp = NULL;
783 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
784 /* there's a stray entry in the dircache */
785 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
786 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
787 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
788 if (dir_remove(vol, cdir) != 0) {
790 AFP_PANIC("dir_add");
794 /* get_id needs adp for reading CNID from adouble file */
795 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
796 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
800 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
806 ad_close_metadata(adp);
808 /* Get macname from unixname */
809 if (path->m_name == NULL) {
810 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
817 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
818 || (bconchar(fullpath, '/') != BSTR_OK)
819 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
820 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
825 /* Allocate and initialize struct dir */
826 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
831 if ((dircache_add(cdir)) != 0) { /* 4 */
832 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
838 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
839 cfrombstring(dir->d_u_name), path->u_name, err);
842 ad_close_metadata(adp);
843 if (!cdir && fullpath)
850 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
851 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
852 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
859 * @brief Remove a dir from a cache and free it and any ressources with it
861 * 1. Check if the dir is locked or has opened forks
862 * 2. If it's a request to remove curdir, just chdir to volume root
863 * 3. Remove it from the cache
864 * 4. Remove the dir plus any allocated resources it references
866 * @param (r) pointer to struct vol
867 * @param (rw) pointer to struct dir
869 int dir_remove(const struct vol *vol, struct dir *dir)
874 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
877 if (dir->d_flags & DIRF_CACHELOCK || dir->d_ofork) { /* 1 */
878 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): dir is locked or has opened forks",
879 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
883 if (curdir == dir) { /* 2 */
884 if (movecwd(vol, vol->v_root) < 0) {
885 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
889 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
890 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
892 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 3 */
893 dir_free(dir); /* 4 */
899 * @brief Modify a struct dir, adjust cache
901 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
902 * If given new_uname == new_mname, new_uname will point to new_mname.
904 * @param vol (r) pointer to struct vol
905 * @param dir (rw) pointer to struct dir
906 * @param pdid (r) new parent DID
907 * @param did (r) new DID
908 * @param new_mname (r) new mac-name
909 * @param new_uname (r) new unix-name
911 int dir_modify(const struct vol *vol,
915 const char *new_mname,
916 const char *new_uname,
917 bstring pdir_fullpath)
921 /* Remove it from the cache */
922 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
930 /* free uname if it's not the same as mname */
931 if (dir->d_m_name != dir->d_u_name)
932 bdestroy(dir->d_u_name);
934 if (new_uname == NULL)
935 new_uname = new_mname;
937 /* assign new name */
938 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
939 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
943 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
944 dir->d_u_name = dir->d_m_name;
946 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
947 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
954 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
956 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
958 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
962 if (dir->d_m_name_ucs2)
963 free(dir->d_m_name_ucs2);
964 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))
965 dir->d_m_name_ucs2 = NULL;
967 /* Re-add it to the cache */
968 if ((dircache_add(dir)) != 0) {
970 AFP_PANIC("dir_modify");
977 * @brief Resolve a catalog node name path
979 * 1. Evaluate path type
980 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
981 * path from dirname, so eg getdirparams has sth it can chew on. curdir
982 * is dir parent then. All this is done in path_from_dir().
983 * 3. Parse next cnode name in path, cases:
984 * 4. single "\0" -> do nothing
985 * 5. two or more consecutive "\0" -> chdir("..") one or more times
986 * 6. cnode name -> copy it to path.m_name
987 * 7. Get unix name from mac name
988 * 8. Special handling of request with did 1
989 * 9. stat the cnode name
990 * 10. If it's not there, it's probably an afp_createfile|dir,
991 * return with curdir = dir parent, struct path = dirname
992 * 11. If it's there and it's a file, it must should be the last element of the requested
993 * path. Return with curdir = cnode name parent dir, struct path = filename
994 * 12. Treat symlinks like files, dont follow them
996 * 14. Search the dircache for it
997 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
998 * 16. chdir into the dir and
999 * 17. set m_name to the mac equivalent of "."
1002 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1004 static char path[ MAXPATHLEN + 1];
1005 static struct path ret;
1015 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1018 afp_errno = AFPERR_NOOBJ;
1019 memset(&ret, 0, sizeof(ret));
1021 switch (ret.m_type = *data) { /* 1 */
1024 len = (unsigned char) *data++;
1026 if (afp_version >= 30) {
1032 if (afp_version >= 30) {
1034 memcpy(&hint, data, sizeof(hint));
1036 data += sizeof(hint);
1038 memcpy(&len16, data, sizeof(len16));
1044 /* else it's an error */
1046 afp_errno = AFPERR_PARAM;
1049 *cpath += len + size;
1054 if (movecwd(vol, dir) < 0 ) {
1055 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1056 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1058 return path_from_dir(vol, dir, &ret);
1063 while (len) { /* 3 */
1064 if (*data == 0) { /* 4 or 5 */
1067 while (len > 0 && *data == 0) { /* 5 */
1068 /* chdir to parrent dir */
1069 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1071 if (movecwd( vol, dir ) < 0 ) {
1072 dir_remove(vol, dir);
1082 for ( p = path; *data != 0 && len > 0; len-- ) {
1084 if (p > &path[ MAXPATHLEN]) {
1085 afp_errno = AFPERR_PARAM;
1089 *p = 0; /* Terminate string */
1092 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1093 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1097 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1099 /* Prevent access to our special folders like .AppleDouble */
1100 if (check_name(vol, ret.u_name)) {
1101 /* the name is illegal */
1102 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1103 afp_errno = AFPERR_PARAM;
1107 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1109 * Special case: CNID 1
1110 * root parent (did 1) has one child: the volume. Requests for did=1 with
1111 * some <name> must check against the volume name.
1113 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1119 * CNID != 1, eg. most of the times we take this way.
1120 * Now check if current path-part is a file or dir:
1121 * o if it's dir we have to step into it
1122 * o if it's a file we expect it to be the last part of the requested path
1123 * and thus call continue which should terminate the while loop because
1126 if (of_stat(&ret) != 0) { /* 9 */
1128 * ret.u_name doesn't exist, might be afp_createfile|dir
1129 * that means it should have been the last part
1132 /* it wasn't the last part, so we have a bogus path request */
1133 afp_errno = AFPERR_NOOBJ;
1137 * this will terminate clean in while (1) because len == 0,
1138 * probably afp_createfile|dir
1140 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1144 switch (ret.st.st_mode & S_IFMT) {
1145 case S_IFREG: /* 11 */
1146 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1148 /* it wasn't the last part, so we have a bogus path request */
1149 afp_errno = AFPERR_PARAM;
1152 continue; /* continues while loop */
1153 case S_IFLNK: /* 12 */
1154 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1156 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1157 afp_errno = AFPERR_PARAM;
1160 continue; /* continues while loop */
1161 case S_IFDIR: /* 13 */
1164 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1165 afp_errno = AFPERR_NODIR;
1169 /* Search the cache */
1170 int unamelen = strlen(ret.u_name);
1171 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1173 /* Not in cache, create one */
1174 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1175 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1176 ntohl(dir->d_did), ret.u_name, getcwdpath());
1180 } /* if/else cnid==1 */
1182 /* Now chdir to the evaluated dir */
1183 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1184 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1185 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1187 return path_from_dir(vol, cdir, &ret);
1192 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1195 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1196 afp_errno = AFPERR_DID1;
1200 if (ret.m_name[0] == 0) {
1201 /* Last part was a dir */
1202 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1206 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1207 cfrombstring(dir->d_fullpath),
1208 cfrombstring(curdir->d_fullpath),
1215 * @brief chdir() to dir
1217 * @param vol (r) pointer to struct vol
1218 * @param dir (r) pointer to struct dir
1220 * @returns 0 on success, -1 on error with afp_errno set appropiately
1222 int movecwd(const struct vol *vol, struct dir *dir)
1229 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1230 cfrombstring(curdir->d_fullpath), getcwdpath());
1234 if (dir->d_did == DIRDID_ROOT_PARENT) {
1235 curdir = &rootParent;
1239 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1241 if ((ret = lchdir(cfrombstring(dir->d_fullpath))) != 0 ) {
1242 LOG(log_debug, logtype_afpd, "movecwd('%s'): ret: %u, %s",
1243 cfrombstring(dir->d_fullpath), ret, strerror(errno));
1245 /* p is a symlink or getcwd failed */
1246 afp_errno = AFPERR_BADTYPE;
1248 if (chdir(vol->v_path ) < 0) {
1249 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1250 /* XXX what do we do here? */
1252 curdir = vol->v_root;
1259 afp_errno = AFPERR_ACCESS;
1262 afp_errno = AFPERR_NOOBJ;
1272 * We can't use unix file's perm to support Apple's inherited protection modes.
1273 * If we aren't the file's owner we can't change its perms when moving it and smb
1274 * nfs,... don't even try.
1276 #define AFP_CHECK_ACCESS
1278 int check_access(char *path, int mode)
1280 #ifdef AFP_CHECK_ACCESS
1288 accessmode(p, &ma, curdir, NULL);
1289 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1291 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1297 /* --------------------- */
1298 int file_access(struct path *path, int mode)
1302 accessmode(path->u_name, &ma, curdir, &path->st);
1303 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1305 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1311 /* --------------------- */
1312 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1314 dir->offcnt = count;
1315 dir->ctime = st->st_ctime;
1316 dir->d_flags &= ~DIRF_CNID;
1320 /* ---------------------
1321 * is our cached also for reenumerate id?
1323 int dirreenumerate(struct dir *dir, struct stat *st)
1325 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1328 /* ------------------------------
1330 (name, dir) with curdir:name == dir, from afp_enumerate
1333 int getdirparams(const struct vol *vol,
1334 u_int16_t bitmap, struct path *s_path,
1336 char *buf, size_t *buflen )
1340 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1341 int bit = 0, isad = 0;
1347 struct stat *st = &s_path->st;
1348 char *upath = s_path->u_name;
1350 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1351 (1 << DIRPBIT_CDATE) |
1352 (1 << DIRPBIT_MDATE) |
1353 (1 << DIRPBIT_BDATE) |
1354 (1 << DIRPBIT_FINFO)))) {
1356 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1357 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1359 if (ad.ad_md->adf_flags & O_CREAT) {
1360 /* We just created it */
1361 ad_setname(&ad, s_path->m_name);
1376 while ( bitmap != 0 ) {
1377 while (( bitmap & 1 ) == 0 ) {
1385 ad_getattr(&ad, &ashort);
1386 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1387 ashort = htons(ATTRBIT_INVISIBLE);
1390 ashort |= htons(ATTRBIT_SHARED);
1391 memcpy( data, &ashort, sizeof( ashort ));
1392 data += sizeof( ashort );
1396 memcpy( data, &pdid, sizeof( pdid ));
1397 data += sizeof( pdid );
1398 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1399 s_path->u_name, ntohl(pdid));
1402 case DIRPBIT_CDATE :
1403 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1404 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1405 memcpy( data, &aint, sizeof( aint ));
1406 data += sizeof( aint );
1409 case DIRPBIT_MDATE :
1410 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1411 memcpy( data, &aint, sizeof( aint ));
1412 data += sizeof( aint );
1415 case DIRPBIT_BDATE :
1416 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1417 aint = AD_DATE_START;
1418 memcpy( data, &aint, sizeof( aint ));
1419 data += sizeof( aint );
1422 case DIRPBIT_FINFO :
1424 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1425 } else { /* no appledouble */
1426 memset( data, 0, 32 );
1427 /* set default view -- this also gets done in ad_open() */
1428 ashort = htons(FINDERINFO_CLOSEDVIEW);
1429 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1431 /* dot files are by default visible */
1432 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1433 ashort = htons(FINDERINFO_INVISIBLE);
1434 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1440 case DIRPBIT_LNAME :
1441 if (dir->d_m_name) /* root of parent can have a null name */
1444 memset(data, 0, sizeof(u_int16_t));
1445 data += sizeof( u_int16_t );
1448 case DIRPBIT_SNAME :
1449 memset(data, 0, sizeof(u_int16_t));
1450 data += sizeof( u_int16_t );
1454 memcpy( data, &dir->d_did, sizeof( aint ));
1455 data += sizeof( aint );
1456 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1457 s_path->u_name, ntohl(dir->d_did));
1460 case DIRPBIT_OFFCNT :
1462 /* this needs to handle current directory access rights */
1463 if (diroffcnt(dir, st)) {
1464 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1466 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1467 setdiroffcnt(dir, st, ret);
1468 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1470 ashort = htons( ashort );
1471 memcpy( data, &ashort, sizeof( ashort ));
1472 data += sizeof( ashort );
1476 aint = htonl(st->st_uid);
1477 memcpy( data, &aint, sizeof( aint ));
1478 data += sizeof( aint );
1482 aint = htonl(st->st_gid);
1483 memcpy( data, &aint, sizeof( aint ));
1484 data += sizeof( aint );
1487 case DIRPBIT_ACCESS :
1488 accessmode( upath, &ma, dir , st);
1490 *data++ = ma.ma_user;
1491 *data++ = ma.ma_world;
1492 *data++ = ma.ma_group;
1493 *data++ = ma.ma_owner;
1496 /* Client has requested the ProDOS information block.
1497 Just pass back the same basic block for all
1498 directories. <shirsch@ibm.net> */
1499 case DIRPBIT_PDINFO :
1500 if (afp_version >= 30) { /* UTF8 name */
1501 utf8 = kTextEncodingUTF8;
1502 if (dir->d_m_name) /* root of parent can have a null name */
1505 memset(data, 0, sizeof(u_int16_t));
1506 data += sizeof( u_int16_t );
1508 memcpy(data, &aint, sizeof( aint ));
1509 data += sizeof( aint );
1511 else { /* ProDOS Info Block */
1514 ashort = htons( 0x0200 );
1515 memcpy( data, &ashort, sizeof( ashort ));
1516 data += sizeof( ashort );
1517 memset( data, 0, sizeof( ashort ));
1518 data += sizeof( ashort );
1522 case DIRPBIT_UNIXPR :
1523 aint = htonl(st->st_uid);
1524 memcpy( data, &aint, sizeof( aint ));
1525 data += sizeof( aint );
1526 aint = htonl(st->st_gid);
1527 memcpy( data, &aint, sizeof( aint ));
1528 data += sizeof( aint );
1531 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1532 memcpy( data, &aint, sizeof( aint ));
1533 data += sizeof( aint );
1535 accessmode( upath, &ma, dir , st);
1537 *data++ = ma.ma_user;
1538 *data++ = ma.ma_world;
1539 *data++ = ma.ma_group;
1540 *data++ = ma.ma_owner;
1545 ad_close_metadata( &ad );
1547 return( AFPERR_BITMAP );
1553 ashort = htons( data - buf );
1554 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1555 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1557 if ( utf_nameoff ) {
1558 ashort = htons( data - buf );
1559 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1560 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1563 ad_close_metadata( &ad );
1565 *buflen = data - buf;
1569 /* ----------------------------- */
1570 int path_error(struct path *path, int error)
1572 /* - a dir with access error
1573 * - no error it's a file
1576 if (path_isadir(path))
1578 if (path->st_valid && path->st_errno)
1580 return AFPERR_BADTYPE ;
1583 /* ----------------------------- */
1584 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1589 u_int16_t vid, bitmap;
1595 memcpy( &vid, ibuf, sizeof( vid ));
1596 ibuf += sizeof( vid );
1598 if (NULL == ( vol = getvolbyvid( vid )) ) {
1599 return( AFPERR_PARAM );
1602 if (vol->v_flags & AFPVOL_RO)
1603 return AFPERR_VLOCK;
1605 memcpy( &did, ibuf, sizeof( did ));
1606 ibuf += sizeof( int );
1608 if (NULL == ( dir = dirlookup( vol, did )) ) {
1612 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1613 bitmap = ntohs( bitmap );
1614 ibuf += sizeof( bitmap );
1616 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1617 return get_afp_errno(AFPERR_NOOBJ);
1620 if ( *path->m_name != '\0' ) {
1621 rc = path_error(path, AFPERR_NOOBJ);
1622 /* maybe we are trying to set perms back */
1623 if (rc != AFPERR_ACCESS)
1628 * If ibuf is odd, make it even.
1630 if ((u_long)ibuf & 1 ) {
1634 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1635 setvoltime(obj, vol );
1641 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1643 * assume path == '\0' eg. it's a directory in canonical form
1645 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1657 u_int16_t ashort, bshort, oshort;
1659 int change_mdate = 0;
1660 int change_parent_mdate = 0;
1662 u_int16_t bitmap = d_bitmap;
1663 u_char finder_buf[32];
1666 u_int16_t upriv_bit = 0;
1669 upath = path->u_name;
1671 while ( bitmap != 0 ) {
1672 while (( bitmap & 1 ) == 0 ) {
1680 memcpy( &ashort, buf, sizeof( ashort ));
1681 buf += sizeof( ashort );
1683 case DIRPBIT_CDATE :
1685 memcpy(&cdate, buf, sizeof(cdate));
1686 buf += sizeof( cdate );
1688 case DIRPBIT_MDATE :
1689 memcpy(&newdate, buf, sizeof(newdate));
1690 buf += sizeof( newdate );
1692 case DIRPBIT_BDATE :
1694 memcpy(&bdate, buf, sizeof(bdate));
1695 buf += sizeof( bdate );
1697 case DIRPBIT_FINFO :
1699 memcpy( finder_buf, buf, 32 );
1702 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1703 change_parent_mdate = 1;
1704 memcpy( &owner, buf, sizeof(owner));
1705 buf += sizeof( owner );
1708 change_parent_mdate = 1;
1709 memcpy( &group, buf, sizeof( group ));
1710 buf += sizeof( group );
1712 case DIRPBIT_ACCESS :
1714 change_parent_mdate = 1;
1715 ma.ma_user = *buf++;
1716 ma.ma_world = *buf++;
1717 ma.ma_group = *buf++;
1718 ma.ma_owner = *buf++;
1719 mpriv = mtoumode( &ma ) | vol->v_dperm;
1720 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1721 err = set_dir_errors(path, "setdirmode", errno);
1725 /* Ignore what the client thinks we should do to the
1726 ProDOS information block. Skip over the data and
1727 report nothing amiss. <shirsch@ibm.net> */
1728 case DIRPBIT_PDINFO :
1729 if (afp_version < 30) {
1733 err = AFPERR_BITMAP;
1737 case DIRPBIT_UNIXPR :
1738 if (vol_unix_priv(vol)) {
1739 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1740 buf += sizeof( owner );
1741 memcpy( &group, buf, sizeof( group ));
1742 buf += sizeof( group );
1745 change_parent_mdate = 1;
1746 memcpy( &upriv, buf, sizeof( upriv ));
1747 buf += sizeof( upriv );
1748 upriv = ntohl (upriv) | vol->v_dperm;
1749 if (dir_rx_set(upriv)) {
1750 /* maybe we are trying to set perms back */
1751 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1753 err = set_dir_errors(path, "setdirunixmode", errno);
1764 err = AFPERR_BITMAP;
1772 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1774 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1776 * Check to see what we're trying to set. If it's anything
1777 * but ACCESS, UID, or GID, give an error. If it's any of those
1778 * three, we don't need the ad to be open, so just continue.
1780 * note: we also don't need to worry about mdate. also, be quiet
1781 * if we're using the noadouble option.
1783 if (!vol_noadouble(vol) && (d_bitmap &
1784 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1785 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1786 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1787 return AFPERR_ACCESS;
1793 * Check to see if a create was necessary. If it was, we'll want
1794 * to set our name, etc.
1796 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1797 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1803 while ( bitmap != 0 ) {
1804 while (( bitmap & 1 ) == 0 ) {
1812 ad_getattr(&ad, &bshort);
1814 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1815 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1819 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1820 change_parent_mdate = 1;
1821 ad_setattr(&ad, bshort);
1824 case DIRPBIT_CDATE :
1826 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1829 case DIRPBIT_MDATE :
1831 case DIRPBIT_BDATE :
1833 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1836 case DIRPBIT_FINFO :
1838 /* Fixes #2802236 */
1839 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1840 *fflags &= htons(~FINDERINFO_ISHARED);
1842 if ( dir->d_did == DIRDID_ROOT ) {
1844 * Alright, we admit it, this is *really* sick!
1845 * The 4 bytes that we don't copy, when we're dealing
1846 * with the root of a volume, are the directory's
1847 * location information. This eliminates that annoying
1848 * behavior one sees when mounting above another mount
1851 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1852 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1854 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1858 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1859 if ( (dir->d_did == DIRDID_ROOT) &&
1860 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1861 err = set_dir_errors(path, "setdeskowner", errno);
1862 if (isad && err == AFPERR_PARAM) {
1863 err = AFP_OK; /* ???*/
1866 goto setdirparam_done;
1869 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1870 err = set_dir_errors(path, "setdirowner", errno);
1871 goto setdirparam_done;
1875 if (dir->d_did == DIRDID_ROOT)
1876 setdeskowner( -1, ntohl(group) );
1877 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1878 err = set_dir_errors(path, "setdirowner", errno);
1879 goto setdirparam_done;
1882 case DIRPBIT_ACCESS :
1883 if (dir->d_did == DIRDID_ROOT) {
1885 if (!dir_rx_set(mpriv)) {
1886 /* we can't remove read and search for owner on volume root */
1887 err = AFPERR_ACCESS;
1888 goto setdirparam_done;
1892 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1893 err = set_dir_errors(path, "setdirmode", errno);
1894 goto setdirparam_done;
1897 case DIRPBIT_PDINFO :
1898 if (afp_version >= 30) {
1899 err = AFPERR_BITMAP;
1900 goto setdirparam_done;
1903 case DIRPBIT_UNIXPR :
1904 if (vol_unix_priv(vol)) {
1905 if (dir->d_did == DIRDID_ROOT) {
1906 if (!dir_rx_set(upriv)) {
1907 /* we can't remove read and search for owner on volume root */
1908 err = AFPERR_ACCESS;
1909 goto setdirparam_done;
1911 setdeskowner( -1, ntohl(group) );
1912 setdeskmode( upriv );
1914 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1915 err = set_dir_errors(path, "setdirowner", errno);
1916 goto setdirparam_done;
1919 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1920 err = set_dir_errors(path, "setdirunixmode", errno);
1921 goto setdirparam_done;
1925 err = AFPERR_BITMAP;
1926 goto setdirparam_done;
1930 err = AFPERR_BITMAP;
1931 goto setdirparam_done;
1940 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1941 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1945 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1946 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1951 if (path->st_valid && !path->st_errno) {
1952 struct stat *st = &path->st;
1954 if (dir && dir->d_pdid) {
1955 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1959 ad_close_metadata( &ad);
1962 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1963 && gettimeofday(&tv, NULL) == 0) {
1964 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1965 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1966 /* be careful with bitmap because now dir is null */
1967 bitmap = 1<<DIRPBIT_MDATE;
1968 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1969 /* should we reset curdir ?*/
1976 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1990 memcpy( &vid, ibuf, sizeof( vid ));
1991 ibuf += sizeof( vid );
1992 if (NULL == (vol = getvolbyvid( vid )) ) {
1993 return( AFPERR_PARAM );
1996 memcpy( &did, ibuf, sizeof( did ));
1997 ibuf += sizeof( did );
2001 * if it's CNID 2 our only choice to meet the specs is call sync.
2002 * For any other CNID just sync that dir. To my knowledge the
2003 * intended use of FPSyncDir is to sync the volume so all we're
2004 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2007 if ( ntohl(did) == 2 ) {
2010 if (NULL == ( dir = dirlookup( vol, did )) ) {
2011 return afp_errno; /* was AFPERR_NOOBJ */
2014 if (movecwd( vol, dir ) < 0 )
2015 return ( AFPERR_NOOBJ );
2018 * Assuming only OSens that have dirfd also may require fsyncing directories
2019 * in order to flush metadata e.g. Linux.
2023 if (NULL == ( dp = opendir( "." )) ) {
2026 return( AFPERR_NOOBJ );
2028 return( AFPERR_ACCESS );
2030 return( AFPERR_PARAM );
2034 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2037 if ( fsync ( dfd ) < 0 )
2038 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2039 dir->d_u_name, strerror(errno) );
2040 closedir(dp); /* closes dfd too */
2043 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2046 return( AFPERR_NOOBJ );
2048 return( AFPERR_ACCESS );
2050 return( AFPERR_PARAM );
2054 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2055 vol->ad_path(".", ADFLAGS_DIR) );
2057 if ( fsync(dfd) < 0 )
2058 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2059 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2066 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2072 struct path *s_path;
2080 memcpy( &vid, ibuf, sizeof( vid ));
2081 ibuf += sizeof( vid );
2082 if (NULL == ( vol = getvolbyvid( vid )) ) {
2083 return( AFPERR_PARAM );
2086 if (vol->v_flags & AFPVOL_RO)
2087 return AFPERR_VLOCK;
2089 memcpy( &did, ibuf, sizeof( did ));
2090 ibuf += sizeof( did );
2091 if (NULL == ( dir = dirlookup( vol, did )) ) {
2092 return afp_errno; /* was AFPERR_NOOBJ */
2094 /* for concurrent access we need to be sure we are not in the
2095 * folder we want to create...
2099 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2100 return get_afp_errno(AFPERR_PARAM);
2102 /* cname was able to move curdir to it! */
2103 if (*s_path->m_name == '\0')
2104 return AFPERR_EXIST;
2106 upath = s_path->u_name;
2108 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2112 if (of_stat(s_path) < 0) {
2118 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2122 if ( movecwd( vol, dir ) < 0 ) {
2123 return( AFPERR_PARAM );
2126 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2127 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2128 if (vol_noadouble(vol))
2129 goto createdir_done;
2130 return( AFPERR_ACCESS );
2132 ad_setname(&ad, s_path->m_name);
2133 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2136 ad_close_metadata( &ad);
2140 /* FIXME: are we really inside the created dir? */
2141 addir_inherit_acl(vol);
2142 #endif /* HAVE_ACLS */
2144 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2145 *rbuflen = sizeof( u_int32_t );
2146 setvoltime(obj, vol );
2151 * dst new unix filename (not a pathname)
2152 * newname new mac name
2154 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2156 int renamedir(const struct vol *vol,
2161 struct dir *newparent,
2167 /* existence check moved to afp_moveandrename */
2168 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2171 return( AFPERR_NOOBJ );
2173 return( AFPERR_ACCESS );
2175 return AFPERR_VLOCK;
2177 /* tried to move directory into a subdirectory of itself */
2178 return AFPERR_CANTMOVE;
2180 /* this needs to copy and delete. bleah. that means we have
2181 * to deal with entire directory hierarchies. */
2182 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2186 if ((err = deletedir(dirfd, src)) < 0)
2190 return( AFPERR_PARAM );
2194 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2196 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2198 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2199 ad_setname(&ad, newname);
2201 ad_close_metadata( &ad);
2204 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2205 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2212 /* delete an empty directory */
2213 int deletecurdir(struct vol *vol)
2223 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2224 return( AFPERR_ACCESS );
2229 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2230 /* we never want to create a resource fork here, we are going to delete it */
2231 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2233 ad_getattr(&ad, &ashort);
2234 ad_close_metadata(&ad);
2235 if ((ashort & htons(ATTRBIT_NODELETE))) {
2236 return AFPERR_OLOCK;
2239 err = vol->vfs->vfs_deletecurdir(vol);
2244 /* now get rid of dangling symlinks */
2245 if ((dp = opendir("."))) {
2246 while ((de = readdir(dp))) {
2247 /* skip this and previous directory */
2248 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2251 /* bail if it's not a symlink */
2252 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2254 return AFPERR_DIRNEMPT;
2257 if ((err = netatalk_unlink(de->d_name))) {
2264 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2269 err = netatalk_rmdir_all_errors(-1, cfrombstring(fdir->d_u_name));
2270 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2271 cnid_delete(vol->v_cdb, fdir->d_did);
2272 dir_remove( vol, fdir );
2276 /* inode is used as key for cnid.
2277 * Close the descriptor only after cnid_delete
2285 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2295 sfunc = (unsigned char) *ibuf++;
2298 if (sfunc >= 3 && sfunc <= 6) {
2299 if (afp_version < 30) {
2300 return( AFPERR_PARAM );
2307 case 3 :/* unicode */
2308 memcpy( &id, ibuf, sizeof( id ));
2311 if (( pw = getpwuid( id )) == NULL ) {
2312 return( AFPERR_NOITEM );
2314 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2315 pw->pw_name, -1, &name);
2322 case 4 : /* unicode */
2323 memcpy( &id, ibuf, sizeof( id ));
2326 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2327 return( AFPERR_NOITEM );
2329 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2330 gr->gr_name, -1, &name);
2337 case 5 : /* UUID -> username */
2338 case 6 : /* UUID -> groupname */
2339 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2340 return AFPERR_PARAM;
2341 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2343 len = getnamefromuuid( ibuf, &name, &type);
2344 if (len != 0) /* its a error code, not len */
2345 return AFPERR_NOITEM;
2346 if (type == UUID_USER) {
2347 if (( pw = getpwnam( name )) == NULL )
2348 return( AFPERR_NOITEM );
2349 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2350 id = htonl(UUID_USER);
2351 memcpy( rbuf, &id, sizeof( id ));
2352 id = htonl( pw->pw_uid);
2353 rbuf += sizeof( id );
2354 memcpy( rbuf, &id, sizeof( id ));
2355 rbuf += sizeof( id );
2356 *rbuflen = 2 * sizeof( id );
2357 } else { /* type == UUID_GROUP */
2358 if (( gr = getgrnam( name )) == NULL )
2359 return( AFPERR_NOITEM );
2360 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2361 id = htonl(UUID_GROUP);
2362 memcpy( rbuf, &id, sizeof( id ));
2363 rbuf += sizeof( id );
2364 id = htonl( gr->gr_gid);
2365 memcpy( rbuf, &id, sizeof( id ));
2366 rbuf += sizeof( id );
2367 *rbuflen = 2 * sizeof( id );
2370 #endif /* HAVE_ACLS */
2372 return( AFPERR_PARAM );
2376 len = strlen( name );
2379 u_int16_t tp = htons(len);
2380 memcpy(rbuf, &tp, sizeof(tp));
2389 memcpy( rbuf, name, len );
2397 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2406 sfunc = (unsigned char) *ibuf++;
2408 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2411 case 2 : /* unicode */
2412 if (afp_version < 30) {
2413 return( AFPERR_PARAM );
2415 memcpy(&ulen, ibuf, sizeof(ulen));
2418 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2422 len = (unsigned char) *ibuf++;
2425 case 5 : /* username -> UUID */
2426 case 6 : /* groupname -> UUID */
2427 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2428 return AFPERR_PARAM;
2429 memcpy(&ulen, ibuf, sizeof(ulen));
2433 #endif /* HAVE_ACLS */
2435 return( AFPERR_PARAM );
2441 return AFPERR_PARAM;
2444 case 1 : /* unicode */
2446 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2447 return( AFPERR_NOITEM );
2451 memcpy( rbuf, &id, sizeof( id ));
2452 *rbuflen = sizeof( id );
2455 case 2 : /* unicode */
2457 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2458 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2459 return( AFPERR_NOITEM );
2462 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2464 memcpy( rbuf, &id, sizeof( id ));
2465 *rbuflen = sizeof( id );
2468 case 5 : /* username -> UUID */
2469 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2470 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2471 return AFPERR_NOITEM;
2472 *rbuflen = UUID_BINSIZE;
2474 case 6 : /* groupname -> UUID */
2475 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2476 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2477 return AFPERR_NOITEM;
2478 *rbuflen = UUID_BINSIZE;
2480 #endif /* HAVE_ACLS */
2486 /* ------------------------------------
2487 variable DID support
2489 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2500 /* do nothing as dids are static for the life of the process. */
2504 memcpy(&vid, ibuf, sizeof( vid ));
2505 ibuf += sizeof( vid );
2506 if (( vol = getvolbyvid( vid )) == NULL ) {
2507 return( AFPERR_PARAM );
2510 memcpy( &did, ibuf, sizeof( did ));
2511 ibuf += sizeof( did );
2512 if (( dir = dirlookup( vol, did )) == NULL ) {
2513 return( AFPERR_PARAM );
2516 /* dir_remove -- deletedid */
2522 /* did creation gets done automatically
2523 * there's a pb again with case but move it to cname
2525 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2528 struct dir *parentdir;
2536 memcpy(&vid, ibuf, sizeof(vid));
2537 ibuf += sizeof( vid );
2539 if (NULL == ( vol = getvolbyvid( vid )) ) {
2540 return( AFPERR_PARAM );
2543 memcpy(&did, ibuf, sizeof(did));
2544 ibuf += sizeof(did);
2546 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2550 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2551 return get_afp_errno(AFPERR_PARAM);
2554 if ( *path->m_name != '\0' ) {
2555 return path_error(path, AFPERR_NOOBJ);
2558 if ( !path->st_valid && of_stat(path ) < 0 ) {
2559 return( AFPERR_NOOBJ );
2561 if ( path->st_errno ) {
2562 return( AFPERR_NOOBJ );
2565 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2566 *rbuflen = sizeof(curdir->d_did);