2 * $Id: directory.c,v 1.131.2.4 2010-02-01 16:13:52 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
23 #include <atalk/adouble.h>
24 #include <atalk/vfs.h>
25 #include <atalk/afp.h>
26 #include <atalk/util.h>
27 #include <atalk/cnid.h>
28 #include <atalk/logger.h>
29 #include <atalk/uuid.h>
30 #include <atalk/unix.h>
31 #include <atalk/bstrlib.h>
32 #include <atalk/bstradd.h>
34 #include "directory.h"
46 #ifdef HAVE_NFSv4_ACLS
47 extern void addir_inherit_acl(const struct vol *vol);
51 * FIXMEs, loose ends after the dircache rewrite:
52 * o case-insensitivity is gone
53 * o setdirparams doesn't change parent mdate anymore
54 * o catsearch doesn't work, see FIXMEs in catsearch.c
55 * o curdir per volume caching is gone
56 * o directory offspring count calculation probably broken
60 /*******************************************************************************************
62 ******************************************************************************************/
65 struct dir rootParent = {
66 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
67 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
68 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
70 struct dir *curdir = &rootParent;
71 struct path Cur_Path = {
77 0, /* stat is not set */
81 /*******************************************************************************************
83 ******************************************************************************************/
86 /* -------------------------
87 appledouble mkdir afp error code.
89 static int netatalk_mkdir(const char *name)
91 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
94 return( AFPERR_NOOBJ );
96 return( AFPERR_VLOCK );
99 return( AFPERR_ACCESS );
101 return( AFPERR_EXIST );
104 return( AFPERR_DFULL );
106 return( AFPERR_PARAM );
112 /* ------------------- */
113 static int deletedir(char *dir)
115 char path[MAXPATHLEN + 1];
123 if ((len = strlen(dir)) +2 > sizeof(path))
127 if ((dp = opendir(dir)) == NULL)
133 remain = sizeof(path) -len -1;
134 while ((de = readdir(dp)) && err == AFP_OK) {
135 /* skip this and previous directory */
136 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
139 if (strlen(de->d_name) > remain) {
143 strcpy(path + len, de->d_name);
144 if (stat(path, &st)) {
147 if (S_ISDIR(st.st_mode)) {
148 err = deletedir(path);
150 err = netatalk_unlink(path);
155 /* okay. the directory is empty. delete it. note: we already got rid
158 err = netatalk_rmdir(dir);
163 /* do a recursive copy. */
164 static int copydir(const struct vol *vol, char *src, char *dst)
166 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
175 /* doesn't exist or the path is too long. */
176 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
177 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
178 ((dp = opendir(src)) == NULL))
181 /* try to create the destination directory */
182 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
187 /* set things up to copy */
191 srem = sizeof(spath) - slen -1;
196 drem = sizeof(dpath) - dlen -1;
199 while ((de = readdir(dp))) {
200 /* skip this and previous directory */
201 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
204 if (strlen(de->d_name) > srem) {
208 strcpy(spath + slen, de->d_name);
210 if (stat(spath, &st) == 0) {
211 if (strlen(de->d_name) > drem) {
215 strcpy(dpath + dlen, de->d_name);
217 if (S_ISDIR(st.st_mode)) {
218 if (AFP_OK != (err = copydir(vol, spath, dpath)))
220 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
224 /* keep the same time stamp. */
225 ut.actime = ut.modtime = st.st_mtime;
231 /* keep the same time stamp. */
232 if (stat(src, &st) == 0) {
233 ut.actime = ut.modtime = st.st_mtime;
242 /* ---------------------
243 * is our cached offspring count valid?
245 static int diroffcnt(struct dir *dir, struct stat *st)
247 return st->st_ctime == dir->ctime;
250 /* --------------------- */
251 static int invisible_dots(const struct vol *vol, const char *name)
253 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
256 /* ------------------ */
257 static int set_dir_errors(struct path *path, const char *where, int err)
262 return AFPERR_ACCESS;
266 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
271 * @brief Convert name in client encoding to server encoding
273 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
274 * This only gets called from cname().
276 * @returns 0 on success, -1 on error
278 * @note If the passed ret->m_name is mangled, we'll demangle it
280 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
282 static char temp[ MAXPATHLEN + 1];
286 if (afp_version >= 30) {
288 if (dir->d_did == DIRDID_ROOT_PARENT) {
290 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
291 * So we compare it with the longname from the current volume and if they match
292 * we overwrite the requested path with the utf8 volume name so that the following
295 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
296 if (strcasecmp(ret->m_name, temp) == 0)
297 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
300 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
301 afp_errno = AFPERR_PARAM;
304 strcpy(ret->m_name, temp);
308 /* check for OS X mangled filename :( */
309 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
310 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
311 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
313 if (t != ret->m_name) {
315 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
316 * flags weren't the same
318 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
319 /* at last got our view of mac name */
320 strcpy(ret->m_name, t);
323 } /* afp_version >= 30 */
325 /* If we haven't got it by now, get it */
326 if (ret->u_name == NULL) {
327 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
328 afp_errno = AFPERR_PARAM;
337 * @brief Build struct path from struct dir
339 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
340 * 1. move cwd into parent dir (we're often already there, but not always)
342 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
345 * it's tricky: movecwd failed some of dir path are not there anymore.
346 * FIXME: Is it true with other errors?
348 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
354 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 )
357 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
358 if (dir->d_m_name == dir->d_u_name) {
359 ret->u_name = ret->m_name;
361 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
362 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
368 ret->st_errno = EACCES;
371 LOG(log_debug, logtype_afpd, "cname(AFPERR_ACCESS:'%s') {path-from-dir: curdir:'%s', path:'%s'}",
372 cfrombstring(dir->d_fullpath),
373 cfrombstring(curdir->d_fullpath),
379 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 )
382 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
383 if (dir->d_m_name == dir->d_u_name) {
384 ret->u_name = ret->m_name;
386 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
387 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
392 ret->st_errno = ENOENT;
395 dir_remove(vol, dir);
402 /* DEADC0DE: never get here */
407 /*********************************************************************************************
409 ********************************************************************************************/
411 int get_afp_errno(const int param)
413 if (afp_errno != AFPERR_DID1)
419 * @brief Resolve a DID
421 * Resolve a DID, allocate a struct dir for it
422 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
423 * 2. Check if the DID is in the cache.
424 * 3. If it's not in the cache resolve it via the database.
425 * 4. Build complete server-side path to the dir.
426 * 5. Check if it exists and is a directory.
427 * 6. Create the struct dir and populate it.
428 * 7. Add it to the cache.
430 * @param vol (r) pointer to struct vol
431 * @param did (r) DID to resolve
433 * @returns pointer to struct dir
435 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
436 * and we are really bad in this case.
438 struct dir *dirlookup(const struct vol *vol, cnid_t did)
440 static char buffer[12 + MAXPATHLEN + 1];
441 struct bstrList *pathlist = NULL;
442 bstring fullpath = NULL;
444 struct dir *ret = NULL;
448 int buflen = 12 + MAXPATHLEN + 1;
452 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
454 /* check for did 0, 1 and 2 */
455 if (did == 0 || vol == NULL) { /* 1 */
456 afp_errno = AFPERR_PARAM;
458 } else if (did == DIRDID_ROOT_PARENT) {
459 rootParent.d_vid = vol->v_vid;
460 return (&rootParent);
461 } else if (did == DIRDID_ROOT) {
465 /* Search the cache */
466 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
470 utf8 = utf8_encoding();
471 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
473 /* Create list for path elements, request 16 list elements for now*/
474 if ((pathlist = bstListCreateMin(16)) == NULL) { /* 4 */
475 LOG(log_error, logtype_afpd, "dirlookup(did: %u): OOM: %s", ntohl(did), strerror(errno));
479 /* Get it from the database */
481 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
482 afp_errno = AFPERR_NOOBJ;
488 /* construct path, copy already found uname to path element list*/
489 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
490 afp_errno = AFPERR_MISC;
495 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {%u, %s}", ntohl(did), ntohl(pdid), upath);
497 /* The stuff that follows is for building the full path to the directory */
499 /* work upwards until we reach volume root */
500 while (cnid != DIRDID_ROOT) {
501 /* construct path, copy already found uname to path element list*/
502 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
503 afp_errno = AFPERR_MISC;
509 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
510 afp_errno = AFPERR_NOOBJ;
516 if ((bstrListPush(pathlist, bfromcstr(vol->v_path))) != BSTR_OK) { /* 4 */
517 afp_errno = AFPERR_MISC;
522 if ((fullpath = bjoinInv(pathlist, bfromcstr("/"))) == NULL) { /* 4 */
523 afp_errno = AFPERR_MISC;
527 /* Finished building the fullpath */
529 /* stat it and check if it's a dir */
530 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
532 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
535 afp_errno = AFPERR_NOOBJ;
539 afp_errno = AFPERR_ACCESS;
543 afp_errno = AFPERR_MISC;
548 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
549 afp_errno = AFPERR_BADTYPE;
555 /* Get macname from unix name */
556 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
557 afp_errno = AFPERR_NOOBJ;
562 /* Create struct dir */
563 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
564 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
569 /* Add it to the cache only if it's a dir */
570 if (dircache_add(ret) != 0) { /* 7 */
575 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
576 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
580 bstrListDestroy(pathlist);
583 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
584 ntohl(did), AfpErr2name(afp_errno));
595 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
597 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
602 static u_int32_t did = 0;
603 static char cname[MAXPATHLEN];
604 static char lname[MAXPATHLEN];
605 ucs2_t u2_path[MAXPATHLEN];
606 ucs2_t u2_dename[MAXPATHLEN];
607 char *tmp, *savepath;
609 if (!(vol->v_flags & AFPVOL_CASEINSEN))
612 if (veto_file(ENUMVETO, path->u_name))
615 savepath = path->u_name;
617 /* very simple cache */
618 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
619 path->u_name = cname;
621 if (of_stat( path ) == 0 ) {
624 /* something changed, we cannot stat ... */
628 if (NULL == ( dp = opendir( "." )) ) {
629 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
634 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
635 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
636 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
638 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
640 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
641 if (NULL == check_dirent(vol, de->d_name))
644 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
647 if (strcasecmp_w( u2_path, u2_dename) == 0) {
649 strlcpy(cname, de->d_name, sizeof(cname));
650 path->u_name = cname;
652 if (of_stat( path ) == 0 ) {
653 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
654 strlcpy(lname, tmp, sizeof(lname));
667 /* invalidate cache */
670 path->u_name = savepath;
672 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
678 * @brief Construct struct dir
680 * Construct struct dir from parameters.
682 * @param m_name (r) directory name in UTF8-dec
683 * @param u_name (r) directory name in server side encoding
684 * @param vol (r) pointer to struct vol
685 * @param pdid (r) Parent CNID
686 * @param did (r) CNID
687 * @param fullpath (r) Full unix path to dir
689 * @returns pointer to new struct dir or NULL on error
691 * @note Most of the time mac name and unix name are the same.
693 struct dir *dir_new(const char *m_name,
695 const struct vol *vol,
702 dir = (struct dir *) calloc(1, sizeof( struct dir ));
706 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
711 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
714 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
715 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
716 dir->d_m_name_ucs2 = NULL;
719 if (m_name == u_name || !strcmp(m_name, u_name)) {
720 dir->d_u_name = dir->d_m_name;
722 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
723 bdestroy(dir->d_m_name);
730 dir->d_vid = vol->v_vid;
731 dir->d_fullpath = path;
736 * @brief Free a struct dir and all its members
738 * @param (rw) pointer to struct dir
740 void dir_free(struct dir *dir)
742 if (dir->d_u_name != dir->d_m_name) {
743 bdestroy(dir->d_u_name);
745 if (dir->d_m_name_ucs2)
746 free(dir->d_m_name_ucs2);
747 bdestroy(dir->d_m_name);
748 bdestroy(dir->d_fullpath);
753 * @brief Create struct dir from struct path
755 * Create a new struct dir from struct path. Then add it to the cache.
756 * The caller must have assured that the dir is not already in the cache,
758 * 1. Open adouble file, get CNID from it.
759 * 2. Search the database, hinting with the CNID from (1).
760 * 3. Build fullpath and create struct dir.
761 * 4. Add it to the cache.
763 * @param vol (r) pointer to struct vol
764 * @param dir (r) pointer to parrent directory
765 * @param path (rw) pointer to struct path with valid path->u_name
766 * @param len (r) strlen of path->u_name
768 * @returns Pointer to new struct dir or NULL on error.
770 * @note Function also assigns path->m_name from path->u_name.
772 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
775 struct dir *cdir = NULL;
778 struct adouble *adp = NULL;
785 assert(dircache_search_by_name(vol, dir->d_did, path->u_name, strlen(path->u_name)) == NULL);
787 /* get_id needs adp for reading CNID from adouble file */
788 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
789 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
793 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
799 ad_close_metadata(adp);
801 /* Get macname from unixname */
802 if (path->m_name == NULL) {
803 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
810 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
811 || (bconchar(fullpath, '/') != BSTR_OK)
812 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
813 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
818 /* Allocate and initialize struct dir */
819 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
824 if ((dircache_add(cdir)) != 0) { /* 4 */
825 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
832 ad_close_metadata(adp);
833 if (!cdir && fullpath)
844 * @brief Remove a dir from a cache and free it and any ressources with it
846 * @param (r) pointer to struct vol
847 * @param (rw) pointer to struct dir
849 int dir_remove(const struct vol *vol, struct dir *dir)
854 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
858 if (movecwd(vol, vol->v_root) < 0) {
859 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
863 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
870 * @brief Modify a struct dir, adust cache
872 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
873 * If given new_uname == new_mname, new_uname will point to new_mname.
875 * @param vol (r) pointer to struct vol
876 * @param dir (rw) pointer to struct dir
877 * @param pdid (r) new parent DID
878 * @param did (r) new DID
879 * @param new_mname (r) new mac-name
880 * @param new_uname (r) new unix-name
882 int dir_modify(const struct vol *vol,
886 const char *new_mname,
887 const char *new_uname,
888 bstring pdir_fullpath)
892 /* Remove it from the cache */
893 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
901 /* free uname if it's not the same as mname */
902 if (dir->d_m_name != dir->d_u_name)
903 bdestroy(dir->d_u_name);
905 if (new_uname == NULL)
906 new_uname = new_mname;
908 /* assign new name */
909 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
910 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
914 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
915 dir->d_u_name = dir->d_m_name;
917 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
918 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
925 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
927 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
929 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
933 if (dir->d_m_name_ucs2)
934 free(dir->d_m_name_ucs2);
935 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))
936 dir->d_m_name_ucs2 = NULL;
938 /* Re-add it to the cache */
939 if ((dircache_add(dir)) != 0) {
948 * @brief Resolve a catalog node name path
950 * If it's a filename:
951 * 1. compute unix name
952 * 2. stat the file, storing struct stat or errno in struct path
953 * 3. cwd (and curdir) is filename parent directory
954 * 4. return path with with filename
957 * 5. search the dircache
958 * 6. if not in the cache:
959 * 7. compute unix name
960 * 8. stat the dir, storing struct stat or errno in struct path
962 * 10. if chdir failed, return path with dirname
963 * cwd is dir parent directory
964 * if chdir succeeded, add to dircache
965 * return path with "" and "."
967 * 11. else in the cache:
968 * 12. stat the dir, storing struct stat or errno in struct path
970 * 14. if chdir failed
973 * if not last path part, return NULL
978 curdir: dir parent directory
983 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
985 static char path[ MAXPATHLEN + 1];
986 static struct path ret;
996 LOG(log_debug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
999 afp_errno = AFPERR_NOOBJ;
1000 memset(&ret, 0, sizeof(ret));
1002 switch (ret.m_type = *data) { /* path type */
1005 len = (unsigned char) *data++;
1007 if (afp_version >= 30) {
1013 if (afp_version >= 30) {
1015 memcpy(&hint, data, sizeof(hint));
1017 data += sizeof(hint);
1019 memcpy(&len16, data, sizeof(len16));
1025 /* else it's an error */
1027 afp_errno = AFPERR_PARAM;
1030 *cpath += len + size;
1035 if (movecwd(vol, dir) < 0 ) {
1036 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1037 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1039 return path_from_dir(vol, dir, &ret);
1047 * 1. single 0 -> delimiter
1048 * 2. additional 0 -> chdir(..)
1050 * a) name is a file, build struct path from it etc., exit while loop
1051 * b) name is a dir, add it to the dircache, chdir to it, continue
1053 if (*data == 0) { /* case 1 or 2 */
1056 while (len > 0 && *data == 0) { /* case 2 */
1057 /* chdir to parrent dir */
1058 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1060 if (movecwd( vol, dir ) < 0 ) {
1061 dir_remove(vol, dir);
1070 /* case 3: copy name from packet buffer to ret.m_name and process it */
1071 for ( p = path; *data != 0 && len > 0; len-- ) {
1073 if (p > &path[ MAXPATHLEN]) {
1074 afp_errno = AFPERR_PARAM;
1078 *p = 0; /* Terminate string */
1081 /* Get u_name from m_name */
1082 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) {
1083 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1087 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1089 /* Prevent access to our special folders like .AppleDouble */
1090 if (check_name(vol, ret.u_name)) {
1091 /* the name is illegal */
1092 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1093 afp_errno = AFPERR_PARAM;
1097 if (dir->d_did == DIRDID_ROOT_PARENT) {
1099 * Special case: CNID 1
1100 * root parent (did 1) has one child: the volume. Requests for did=1 with
1101 * some <name> must check against the volume name.
1103 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1109 * CNID != 1, eg. most of the times we take this way.
1110 * Now check if current path-part is a file or dir:
1111 * o if it's dir we have to step into it
1112 * o if it's a file we expect it to be the last part of the requested path
1113 * and thus call continue which should terminate the while loop because
1116 if (of_stat(&ret) != 0) {
1118 * ret.u_name doesn't exist, might be afp_createfile|dir
1119 * that means it should have been the last part
1122 /* it wasn't the last part, so we have a bogus path request */
1123 afp_errno = AFPERR_NOOBJ;
1127 * this will terminate clean in while (1) because len == 0,
1128 * probably afp_createfile|dir
1130 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1134 switch (ret.st.st_mode & S_IFMT) {
1136 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1138 /* it wasn't the last part, so we have a bogus path request */
1139 afp_errno = AFPERR_PARAM;
1142 continue; /* continues while loop */
1144 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1146 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1147 afp_errno = AFPERR_PARAM;
1150 continue; /* continues while loop */
1154 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1155 afp_errno = AFPERR_NODIR;
1159 /* Search the cache */
1160 int unamelen = strlen(ret.u_name);
1161 cdir = dircache_search_by_name(vol, dir->d_did, ret.u_name, unamelen);
1163 /* Not in cache, create one */
1164 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) {
1165 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1166 ntohl(dir->d_did), ret.u_name, getcwdpath());
1170 } /* if/else cnid==1 */
1172 /* Now chdir to the evaluated dir */
1173 if (movecwd( vol, cdir ) < 0 ) {
1174 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1175 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1177 return path_from_dir(vol, cdir, &ret);
1182 ret.m_name[0] = 0; /* so we later know last token was a dir */
1185 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1186 afp_errno = AFPERR_DID1;
1190 if (ret.m_name[0] == 0) {
1191 /* Last part was a dir */
1192 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1196 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1197 cfrombstring(dir->d_fullpath),
1198 cfrombstring(curdir->d_fullpath),
1205 * @brief chdir() to dir
1207 * @param vol (r) pointer to struct vol
1208 * @param dir (r) pointer to struct dir
1210 * @returns 0 on success, -1 on error with afp_errno set appropiately
1212 int movecwd(const struct vol *vol, struct dir *dir)
1219 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1220 cfrombstring(curdir->d_fullpath), getcwdpath());
1224 if (dir->d_did == DIRDID_ROOT_PARENT) {
1225 curdir = &rootParent;
1229 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1231 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1232 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1236 afp_errno = AFPERR_ACCESS;
1239 afp_errno = AFPERR_NOOBJ;
1249 * We can't use unix file's perm to support Apple's inherited protection modes.
1250 * If we aren't the file's owner we can't change its perms when moving it and smb
1251 * nfs,... don't even try.
1253 #define AFP_CHECK_ACCESS
1255 int check_access(char *path, int mode)
1257 #ifdef AFP_CHECK_ACCESS
1265 accessmode(p, &ma, curdir, NULL);
1266 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1268 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1274 /* --------------------- */
1275 int file_access(struct path *path, int mode)
1279 accessmode(path->u_name, &ma, curdir, &path->st);
1280 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1282 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1288 /* --------------------- */
1289 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1291 dir->offcnt = count;
1292 dir->ctime = st->st_ctime;
1293 dir->d_flags &= ~DIRF_CNID;
1297 /* ---------------------
1298 * is our cached also for reenumerate id?
1300 int dirreenumerate(struct dir *dir, struct stat *st)
1302 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1305 /* ------------------------------
1307 (name, dir) with curdir:name == dir, from afp_enumerate
1310 int getdirparams(const struct vol *vol,
1311 u_int16_t bitmap, struct path *s_path,
1313 char *buf, size_t *buflen )
1317 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1318 int bit = 0, isad = 0;
1324 struct stat *st = &s_path->st;
1325 char *upath = s_path->u_name;
1327 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1328 (1 << DIRPBIT_CDATE) |
1329 (1 << DIRPBIT_MDATE) |
1330 (1 << DIRPBIT_BDATE) |
1331 (1 << DIRPBIT_FINFO)))) {
1333 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1334 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1336 if (ad.ad_md->adf_flags & O_CREAT) {
1337 /* We just created it */
1338 ad_setname(&ad, s_path->m_name);
1353 while ( bitmap != 0 ) {
1354 while (( bitmap & 1 ) == 0 ) {
1362 ad_getattr(&ad, &ashort);
1363 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1364 ashort = htons(ATTRBIT_INVISIBLE);
1367 ashort |= htons(ATTRBIT_SHARED);
1368 memcpy( data, &ashort, sizeof( ashort ));
1369 data += sizeof( ashort );
1373 memcpy( data, &pdid, sizeof( pdid ));
1374 data += sizeof( pdid );
1377 case DIRPBIT_CDATE :
1378 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1379 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1380 memcpy( data, &aint, sizeof( aint ));
1381 data += sizeof( aint );
1384 case DIRPBIT_MDATE :
1385 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1386 memcpy( data, &aint, sizeof( aint ));
1387 data += sizeof( aint );
1390 case DIRPBIT_BDATE :
1391 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1392 aint = AD_DATE_START;
1393 memcpy( data, &aint, sizeof( aint ));
1394 data += sizeof( aint );
1397 case DIRPBIT_FINFO :
1399 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1400 } else { /* no appledouble */
1401 memset( data, 0, 32 );
1402 /* set default view -- this also gets done in ad_open() */
1403 ashort = htons(FINDERINFO_CLOSEDVIEW);
1404 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1406 /* dot files are by default visible */
1407 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1408 ashort = htons(FINDERINFO_INVISIBLE);
1409 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1415 case DIRPBIT_LNAME :
1416 if (dir->d_m_name) /* root of parent can have a null name */
1419 memset(data, 0, sizeof(u_int16_t));
1420 data += sizeof( u_int16_t );
1423 case DIRPBIT_SNAME :
1424 memset(data, 0, sizeof(u_int16_t));
1425 data += sizeof( u_int16_t );
1429 memcpy( data, &dir->d_did, sizeof( aint ));
1430 data += sizeof( aint );
1433 case DIRPBIT_OFFCNT :
1435 /* this needs to handle current directory access rights */
1436 if (diroffcnt(dir, st)) {
1437 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1439 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1440 setdiroffcnt(dir, st, ret);
1441 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1443 ashort = htons( ashort );
1444 memcpy( data, &ashort, sizeof( ashort ));
1445 data += sizeof( ashort );
1449 aint = htonl(st->st_uid);
1450 memcpy( data, &aint, sizeof( aint ));
1451 data += sizeof( aint );
1455 aint = htonl(st->st_gid);
1456 memcpy( data, &aint, sizeof( aint ));
1457 data += sizeof( aint );
1460 case DIRPBIT_ACCESS :
1461 accessmode( upath, &ma, dir , st);
1463 *data++ = ma.ma_user;
1464 *data++ = ma.ma_world;
1465 *data++ = ma.ma_group;
1466 *data++ = ma.ma_owner;
1469 /* Client has requested the ProDOS information block.
1470 Just pass back the same basic block for all
1471 directories. <shirsch@ibm.net> */
1472 case DIRPBIT_PDINFO :
1473 if (afp_version >= 30) { /* UTF8 name */
1474 utf8 = kTextEncodingUTF8;
1475 if (dir->d_m_name) /* root of parent can have a null name */
1478 memset(data, 0, sizeof(u_int16_t));
1479 data += sizeof( u_int16_t );
1481 memcpy(data, &aint, sizeof( aint ));
1482 data += sizeof( aint );
1484 else { /* ProDOS Info Block */
1487 ashort = htons( 0x0200 );
1488 memcpy( data, &ashort, sizeof( ashort ));
1489 data += sizeof( ashort );
1490 memset( data, 0, sizeof( ashort ));
1491 data += sizeof( ashort );
1495 case DIRPBIT_UNIXPR :
1496 aint = htonl(st->st_uid);
1497 memcpy( data, &aint, sizeof( aint ));
1498 data += sizeof( aint );
1499 aint = htonl(st->st_gid);
1500 memcpy( data, &aint, sizeof( aint ));
1501 data += sizeof( aint );
1504 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1505 memcpy( data, &aint, sizeof( aint ));
1506 data += sizeof( aint );
1508 accessmode( upath, &ma, dir , st);
1510 *data++ = ma.ma_user;
1511 *data++ = ma.ma_world;
1512 *data++ = ma.ma_group;
1513 *data++ = ma.ma_owner;
1518 ad_close_metadata( &ad );
1520 return( AFPERR_BITMAP );
1526 ashort = htons( data - buf );
1527 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1528 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1530 if ( utf_nameoff ) {
1531 ashort = htons( data - buf );
1532 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1533 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1536 ad_close_metadata( &ad );
1538 *buflen = data - buf;
1542 /* ----------------------------- */
1543 int path_error(struct path *path, int error)
1545 /* - a dir with access error
1546 * - no error it's a file
1549 if (path_isadir(path))
1551 if (path->st_valid && path->st_errno)
1553 return AFPERR_BADTYPE ;
1556 /* ----------------------------- */
1557 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1562 u_int16_t vid, bitmap;
1568 memcpy( &vid, ibuf, sizeof( vid ));
1569 ibuf += sizeof( vid );
1571 if (NULL == ( vol = getvolbyvid( vid )) ) {
1572 return( AFPERR_PARAM );
1575 if (vol->v_flags & AFPVOL_RO)
1576 return AFPERR_VLOCK;
1578 memcpy( &did, ibuf, sizeof( did ));
1579 ibuf += sizeof( int );
1581 if (NULL == ( dir = dirlookup( vol, did )) ) {
1585 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1586 bitmap = ntohs( bitmap );
1587 ibuf += sizeof( bitmap );
1589 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1590 return get_afp_errno(AFPERR_NOOBJ);
1593 if ( *path->m_name != '\0' ) {
1594 rc = path_error(path, AFPERR_NOOBJ);
1595 /* maybe we are trying to set perms back */
1596 if (rc != AFPERR_ACCESS)
1601 * If ibuf is odd, make it even.
1603 if ((u_long)ibuf & 1 ) {
1607 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1608 setvoltime(obj, vol );
1614 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1616 * assume path == '\0' eg. it's a directory in canonical form
1618 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1630 u_int16_t ashort, bshort, oshort;
1632 int change_mdate = 0;
1633 int change_parent_mdate = 0;
1635 u_int16_t bitmap = d_bitmap;
1636 u_char finder_buf[32];
1639 u_int16_t upriv_bit = 0;
1642 upath = path->u_name;
1644 while ( bitmap != 0 ) {
1645 while (( bitmap & 1 ) == 0 ) {
1653 memcpy( &ashort, buf, sizeof( ashort ));
1654 buf += sizeof( ashort );
1656 case DIRPBIT_CDATE :
1658 memcpy(&cdate, buf, sizeof(cdate));
1659 buf += sizeof( cdate );
1661 case DIRPBIT_MDATE :
1662 memcpy(&newdate, buf, sizeof(newdate));
1663 buf += sizeof( newdate );
1665 case DIRPBIT_BDATE :
1667 memcpy(&bdate, buf, sizeof(bdate));
1668 buf += sizeof( bdate );
1670 case DIRPBIT_FINFO :
1672 memcpy( finder_buf, buf, 32 );
1675 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1676 change_parent_mdate = 1;
1677 memcpy( &owner, buf, sizeof(owner));
1678 buf += sizeof( owner );
1681 change_parent_mdate = 1;
1682 memcpy( &group, buf, sizeof( group ));
1683 buf += sizeof( group );
1685 case DIRPBIT_ACCESS :
1687 change_parent_mdate = 1;
1688 ma.ma_user = *buf++;
1689 ma.ma_world = *buf++;
1690 ma.ma_group = *buf++;
1691 ma.ma_owner = *buf++;
1692 mpriv = mtoumode( &ma ) | vol->v_dperm;
1693 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1694 err = set_dir_errors(path, "setdirmode", errno);
1698 /* Ignore what the client thinks we should do to the
1699 ProDOS information block. Skip over the data and
1700 report nothing amiss. <shirsch@ibm.net> */
1701 case DIRPBIT_PDINFO :
1702 if (afp_version < 30) {
1706 err = AFPERR_BITMAP;
1710 case DIRPBIT_UNIXPR :
1711 if (vol_unix_priv(vol)) {
1712 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1713 buf += sizeof( owner );
1714 memcpy( &group, buf, sizeof( group ));
1715 buf += sizeof( group );
1718 change_parent_mdate = 1;
1719 memcpy( &upriv, buf, sizeof( upriv ));
1720 buf += sizeof( upriv );
1721 upriv = ntohl (upriv) | vol->v_dperm;
1722 if (dir_rx_set(upriv)) {
1723 /* maybe we are trying to set perms back */
1724 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1726 err = set_dir_errors(path, "setdirunixmode", errno);
1737 err = AFPERR_BITMAP;
1745 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1747 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1749 * Check to see what we're trying to set. If it's anything
1750 * but ACCESS, UID, or GID, give an error. If it's any of those
1751 * three, we don't need the ad to be open, so just continue.
1753 * note: we also don't need to worry about mdate. also, be quiet
1754 * if we're using the noadouble option.
1756 if (!vol_noadouble(vol) && (d_bitmap &
1757 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1758 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1759 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1760 return AFPERR_ACCESS;
1766 * Check to see if a create was necessary. If it was, we'll want
1767 * to set our name, etc.
1769 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1770 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1776 while ( bitmap != 0 ) {
1777 while (( bitmap & 1 ) == 0 ) {
1785 ad_getattr(&ad, &bshort);
1787 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1788 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1792 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1793 change_parent_mdate = 1;
1794 ad_setattr(&ad, bshort);
1797 case DIRPBIT_CDATE :
1799 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1802 case DIRPBIT_MDATE :
1804 case DIRPBIT_BDATE :
1806 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1809 case DIRPBIT_FINFO :
1811 /* Fixes #2802236 */
1812 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1813 *fflags &= htons(~FINDERINFO_ISHARED);
1815 if ( dir->d_did == DIRDID_ROOT ) {
1817 * Alright, we admit it, this is *really* sick!
1818 * The 4 bytes that we don't copy, when we're dealing
1819 * with the root of a volume, are the directory's
1820 * location information. This eliminates that annoying
1821 * behavior one sees when mounting above another mount
1824 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1825 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1827 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1831 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1832 if ( (dir->d_did == DIRDID_ROOT) &&
1833 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1834 err = set_dir_errors(path, "setdeskowner", errno);
1835 if (isad && err == AFPERR_PARAM) {
1836 err = AFP_OK; /* ???*/
1839 goto setdirparam_done;
1842 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1843 err = set_dir_errors(path, "setdirowner", errno);
1844 goto setdirparam_done;
1848 if (dir->d_did == DIRDID_ROOT)
1849 setdeskowner( -1, ntohl(group) );
1850 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1851 err = set_dir_errors(path, "setdirowner", errno);
1852 goto setdirparam_done;
1855 case DIRPBIT_ACCESS :
1856 if (dir->d_did == DIRDID_ROOT) {
1858 if (!dir_rx_set(mpriv)) {
1859 /* we can't remove read and search for owner on volume root */
1860 err = AFPERR_ACCESS;
1861 goto setdirparam_done;
1865 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1866 err = set_dir_errors(path, "setdirmode", errno);
1867 goto setdirparam_done;
1870 case DIRPBIT_PDINFO :
1871 if (afp_version >= 30) {
1872 err = AFPERR_BITMAP;
1873 goto setdirparam_done;
1876 case DIRPBIT_UNIXPR :
1877 if (vol_unix_priv(vol)) {
1878 if (dir->d_did == DIRDID_ROOT) {
1879 if (!dir_rx_set(upriv)) {
1880 /* we can't remove read and search for owner on volume root */
1881 err = AFPERR_ACCESS;
1882 goto setdirparam_done;
1884 setdeskowner( -1, ntohl(group) );
1885 setdeskmode( upriv );
1887 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1888 err = set_dir_errors(path, "setdirowner", errno);
1889 goto setdirparam_done;
1892 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1893 err = set_dir_errors(path, "setdirunixmode", errno);
1894 goto setdirparam_done;
1898 err = AFPERR_BITMAP;
1899 goto setdirparam_done;
1903 err = AFPERR_BITMAP;
1904 goto setdirparam_done;
1913 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1914 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1918 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1919 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1924 if (path->st_valid && !path->st_errno) {
1925 struct stat *st = &path->st;
1927 if (dir && dir->d_pdid) {
1928 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1932 ad_close_metadata( &ad);
1936 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1937 && gettimeofday(&tv, NULL) == 0) {
1938 if (!movecwd(vol, dir->d_parent)) {
1939 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1940 /* be careful with bitmap because now dir is null */
1941 bitmap = 1<<DIRPBIT_MDATE;
1942 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1943 /* should we reset curdir ?*/
1950 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1964 memcpy( &vid, ibuf, sizeof( vid ));
1965 ibuf += sizeof( vid );
1966 if (NULL == (vol = getvolbyvid( vid )) ) {
1967 return( AFPERR_PARAM );
1970 memcpy( &did, ibuf, sizeof( did ));
1971 ibuf += sizeof( did );
1975 * if it's CNID 2 our only choice to meet the specs is call sync.
1976 * For any other CNID just sync that dir. To my knowledge the
1977 * intended use of FPSyncDir is to sync the volume so all we're
1978 * ever going to see here is probably CNID 2. Anyway, we' prepared.
1981 if ( ntohl(did) == 2 ) {
1984 if (NULL == ( dir = dirlookup( vol, did )) ) {
1985 return afp_errno; /* was AFPERR_NOOBJ */
1988 if (movecwd( vol, dir ) < 0 )
1989 return ( AFPERR_NOOBJ );
1992 * Assuming only OSens that have dirfd also may require fsyncing directories
1993 * in order to flush metadata e.g. Linux.
1997 if (NULL == ( dp = opendir( "." )) ) {
2000 return( AFPERR_NOOBJ );
2002 return( AFPERR_ACCESS );
2004 return( AFPERR_PARAM );
2008 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2011 if ( fsync ( dfd ) < 0 )
2012 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2013 dir->d_u_name, strerror(errno) );
2014 closedir(dp); /* closes dfd too */
2017 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2020 return( AFPERR_NOOBJ );
2022 return( AFPERR_ACCESS );
2024 return( AFPERR_PARAM );
2028 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2029 vol->ad_path(".", ADFLAGS_DIR) );
2031 if ( fsync(dfd) < 0 )
2032 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2033 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2040 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2046 struct path *s_path;
2054 memcpy( &vid, ibuf, sizeof( vid ));
2055 ibuf += sizeof( vid );
2056 if (NULL == ( vol = getvolbyvid( vid )) ) {
2057 return( AFPERR_PARAM );
2060 if (vol->v_flags & AFPVOL_RO)
2061 return AFPERR_VLOCK;
2063 memcpy( &did, ibuf, sizeof( did ));
2064 ibuf += sizeof( did );
2065 if (NULL == ( dir = dirlookup( vol, did )) ) {
2066 return afp_errno; /* was AFPERR_NOOBJ */
2068 /* for concurrent access we need to be sure we are not in the
2069 * folder we want to create...
2073 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2074 return get_afp_errno(AFPERR_PARAM);
2076 /* cname was able to move curdir to it! */
2077 if (*s_path->m_name == '\0')
2078 return AFPERR_EXIST;
2080 upath = s_path->u_name;
2082 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2086 if (of_stat(s_path) < 0) {
2092 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2096 if ( movecwd( vol, dir ) < 0 ) {
2097 return( AFPERR_PARAM );
2100 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2101 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2102 if (vol_noadouble(vol))
2103 goto createdir_done;
2104 return( AFPERR_ACCESS );
2106 ad_setname(&ad, s_path->m_name);
2107 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2110 ad_close_metadata( &ad);
2113 #ifdef HAVE_NFSv4_ACLS
2114 /* FIXME: are we really inside the created dir? */
2115 addir_inherit_acl(vol);
2118 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2119 *rbuflen = sizeof( u_int32_t );
2120 setvoltime(obj, vol );
2125 * dst new unix filename (not a pathname)
2126 * newname new mac name
2129 int renamedir(const struct vol *vol, char *src, char *dst,
2131 struct dir *newparent,
2137 /* existence check moved to afp_moveandrename */
2138 if ( unix_rename( src, dst ) < 0 ) {
2141 return( AFPERR_NOOBJ );
2143 return( AFPERR_ACCESS );
2145 return AFPERR_VLOCK;
2147 /* tried to move directory into a subdirectory of itself */
2148 return AFPERR_CANTMOVE;
2150 /* this needs to copy and delete. bleah. that means we have
2151 * to deal with entire directory hierarchies. */
2152 if ((err = copydir(vol, src, dst)) < 0) {
2156 if ((err = deletedir(src)) < 0)
2160 return( AFPERR_PARAM );
2164 vol->vfs->vfs_renamedir(vol, src, dst);
2166 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2168 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2169 ad_setname(&ad, newname);
2171 ad_close_metadata( &ad);
2174 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2175 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2182 /* delete an empty directory */
2183 int deletecurdir(struct vol *vol)
2193 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2194 return( AFPERR_ACCESS );
2199 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2200 /* we never want to create a resource fork here, we are going to delete it */
2201 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2203 ad_getattr(&ad, &ashort);
2204 ad_close( &ad, ADFLAGS_HF );
2205 if ((ashort & htons(ATTRBIT_NODELETE))) {
2206 return AFPERR_OLOCK;
2209 err = vol->vfs->vfs_deletecurdir(vol);
2214 /* now get rid of dangling symlinks */
2215 if ((dp = opendir("."))) {
2216 while ((de = readdir(dp))) {
2217 /* skip this and previous directory */
2218 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2221 /* bail if it's not a symlink */
2222 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2224 return AFPERR_DIRNEMPT;
2227 if ((err = netatalk_unlink(de->d_name))) {
2234 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2239 err = netatalk_rmdir_all_errors(cfrombstring(fdir->d_u_name));
2240 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2241 cnid_delete(vol->v_cdb, fdir->d_did);
2242 dir_remove( vol, fdir );
2246 /* inode is used as key for cnid.
2247 * Close the descriptor only after cnid_delete
2255 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2265 sfunc = (unsigned char) *ibuf++;
2269 if (sfunc >= 3 && sfunc <= 6) {
2270 if (afp_version < 30) {
2271 return( AFPERR_PARAM );
2278 case 3 :/* unicode */
2279 memcpy( &id, ibuf, sizeof( id ));
2282 if (( pw = getpwuid( id )) == NULL ) {
2283 return( AFPERR_NOITEM );
2285 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2286 pw->pw_name, -1, &name);
2293 case 4 : /* unicode */
2294 memcpy( &id, ibuf, sizeof( id ));
2297 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2298 return( AFPERR_NOITEM );
2300 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2301 gr->gr_name, -1, &name);
2307 #ifdef HAVE_NFSv4_ACLS
2308 case 5 : /* UUID -> username */
2309 case 6 : /* UUID -> groupname */
2310 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2311 return AFPERR_PARAM;
2312 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2314 len = getnamefromuuid( ibuf, &name, &type);
2315 if (len != 0) /* its a error code, not len */
2316 return AFPERR_NOITEM;
2317 if (type == UUID_USER) {
2318 if (( pw = getpwnam( name )) == NULL )
2319 return( AFPERR_NOITEM );
2320 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2321 id = htonl(UUID_USER);
2322 memcpy( rbuf, &id, sizeof( id ));
2323 id = htonl( pw->pw_uid);
2324 rbuf += sizeof( id );
2325 memcpy( rbuf, &id, sizeof( id ));
2326 rbuf += sizeof( id );
2327 *rbuflen = 2 * sizeof( id );
2328 } else { /* type == UUID_GROUP */
2329 if (( gr = getgrnam( name )) == NULL )
2330 return( AFPERR_NOITEM );
2331 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2332 id = htonl(UUID_GROUP);
2333 memcpy( rbuf, &id, sizeof( id ));
2334 rbuf += sizeof( id );
2335 id = htonl( gr->gr_gid);
2336 memcpy( rbuf, &id, sizeof( id ));
2337 rbuf += sizeof( id );
2338 *rbuflen = 2 * sizeof( id );
2343 return( AFPERR_PARAM );
2347 len = strlen( name );
2350 u_int16_t tp = htons(len);
2351 memcpy(rbuf, &tp, sizeof(tp));
2360 memcpy( rbuf, name, len );
2368 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2377 sfunc = (unsigned char) *ibuf++;
2379 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2382 case 2 : /* unicode */
2383 if (afp_version < 30) {
2384 return( AFPERR_PARAM );
2386 memcpy(&ulen, ibuf, sizeof(ulen));
2389 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2393 len = (unsigned char) *ibuf++;
2395 #ifdef HAVE_NFSv4_ACLS
2396 case 5 : /* username -> UUID */
2397 case 6 : /* groupname -> UUID */
2398 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2399 return AFPERR_PARAM;
2400 memcpy(&ulen, ibuf, sizeof(ulen));
2406 return( AFPERR_PARAM );
2412 return AFPERR_PARAM;
2415 case 1 : /* unicode */
2417 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2418 return( AFPERR_NOITEM );
2422 memcpy( rbuf, &id, sizeof( id ));
2423 *rbuflen = sizeof( id );
2426 case 2 : /* unicode */
2428 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2429 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2430 return( AFPERR_NOITEM );
2433 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2435 memcpy( rbuf, &id, sizeof( id ));
2436 *rbuflen = sizeof( id );
2438 #ifdef HAVE_NFSv4_ACLS
2439 case 5 : /* username -> UUID */
2440 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2441 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2442 return AFPERR_NOITEM;
2443 *rbuflen = UUID_BINSIZE;
2445 case 6 : /* groupname -> UUID */
2446 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2447 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2448 return AFPERR_NOITEM;
2449 *rbuflen = UUID_BINSIZE;
2457 /* ------------------------------------
2458 variable DID support
2460 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2471 /* do nothing as dids are static for the life of the process. */
2475 memcpy(&vid, ibuf, sizeof( vid ));
2476 ibuf += sizeof( vid );
2477 if (( vol = getvolbyvid( vid )) == NULL ) {
2478 return( AFPERR_PARAM );
2481 memcpy( &did, ibuf, sizeof( did ));
2482 ibuf += sizeof( did );
2483 if (( dir = dirlookup( vol, did )) == NULL ) {
2484 return( AFPERR_PARAM );
2487 /* dir_remove -- deletedid */
2493 /* did creation gets done automatically
2494 * there's a pb again with case but move it to cname
2496 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2499 struct dir *parentdir;
2507 memcpy(&vid, ibuf, sizeof(vid));
2508 ibuf += sizeof( vid );
2510 if (NULL == ( vol = getvolbyvid( vid )) ) {
2511 return( AFPERR_PARAM );
2514 memcpy(&did, ibuf, sizeof(did));
2515 ibuf += sizeof(did);
2517 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2521 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2522 return get_afp_errno(AFPERR_PARAM);
2525 if ( *path->m_name != '\0' ) {
2526 return path_error(path, AFPERR_NOOBJ);
2529 if ( !path->st_valid && of_stat(path ) < 0 ) {
2530 return( AFPERR_NOOBJ );
2532 if ( path->st_errno ) {
2533 return( AFPERR_NOOBJ );
2536 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2537 *rbuflen = sizeof(curdir->d_did);