2 * $Id: directory.c,v 1.131.2.3 2010-02-01 14:25:45 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);
367 ret->st_errno = EACCES;
371 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 )
374 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
375 if (dir->d_m_name == dir->d_u_name) {
376 ret->u_name = ret->m_name;
378 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
379 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
383 ret->st_errno = ENOENT;
385 dir_remove(vol, dir);
392 /* DEADC0DE: never get here */
397 /*********************************************************************************************
399 ********************************************************************************************/
401 int get_afp_errno(const int param)
403 if (afp_errno != AFPERR_DID1)
409 * @brief Resolve a DID
411 * Resolve a DID, allocate a struct dir for it
412 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
413 * 2. Check if the DID is in the cache.
414 * 3. If it's not in the cache resolve it via the database.
415 * 4. Build complete server-side path to the dir.
416 * 5. Check if it exists and is a directory.
417 * 6. Create the struct dir and populate it.
418 * 7. Add it to the cache.
420 * @param vol (r) pointer to struct vol
421 * @param did (r) DID to resolve
423 * @returns pointer to struct dir
425 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
426 * and we are really bad in this case.
428 struct dir *dirlookup(const struct vol *vol, cnid_t did)
430 static char buffer[12 + MAXPATHLEN + 1];
431 struct bstrList *pathlist = NULL;
432 bstring fullpath = NULL;
434 struct dir *ret = NULL;
438 int buflen = 12 + MAXPATHLEN + 1;
442 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
444 /* check for did 0, 1 and 2 */
445 if (did == 0 || vol == NULL) { /* 1 */
446 afp_errno = AFPERR_PARAM;
448 } else if (did == DIRDID_ROOT_PARENT) {
449 rootParent.d_vid = vol->v_vid;
450 return (&rootParent);
451 } else if (did == DIRDID_ROOT) {
455 /* Search the cache */
456 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
460 utf8 = utf8_encoding();
461 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
463 /* Create list for path elements, request 16 list elements for now*/
464 if ((pathlist = bstListCreateMin(16)) == NULL) { /* 4 */
465 LOG(log_error, logtype_afpd, "dirlookup(did: %u): OOM: %s", ntohl(did), strerror(errno));
469 /* Get it from the database */
471 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
472 afp_errno = AFPERR_NOOBJ;
478 /* construct path, copy already found uname to path element list*/
479 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
480 afp_errno = AFPERR_MISC;
485 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {%u, %s}", ntohl(did), ntohl(pdid), upath);
487 /* The stuff that follows is for building the full path to the directory */
489 /* work upwards until we reach volume root */
490 while (cnid != DIRDID_ROOT) {
491 /* construct path, copy already found uname to path element list*/
492 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
493 afp_errno = AFPERR_MISC;
499 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
500 afp_errno = AFPERR_NOOBJ;
506 if ((bstrListPush(pathlist, bfromcstr(vol->v_path))) != BSTR_OK) { /* 4 */
507 afp_errno = AFPERR_MISC;
512 if ((fullpath = bjoinInv(pathlist, bfromcstr("/"))) == NULL) { /* 4 */
513 afp_errno = AFPERR_MISC;
517 /* Finished building the fullpath */
519 /* stat it and check if it's a dir */
520 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
522 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
525 afp_errno = AFPERR_NOOBJ;
529 afp_errno = AFPERR_ACCESS;
533 afp_errno = AFPERR_MISC;
538 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
539 afp_errno = AFPERR_BADTYPE;
545 /* Get macname from unix name */
546 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
547 afp_errno = AFPERR_NOOBJ;
552 /* Create struct dir */
553 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
554 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
559 /* Add it to the cache only if it's a dir */
560 if (dircache_add(ret) != 0) { /* 7 */
565 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
566 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
570 bstrListDestroy(pathlist);
573 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
574 ntohl(did), AfpErr2name(afp_errno));
585 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
587 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
592 static u_int32_t did = 0;
593 static char cname[MAXPATHLEN];
594 static char lname[MAXPATHLEN];
595 ucs2_t u2_path[MAXPATHLEN];
596 ucs2_t u2_dename[MAXPATHLEN];
597 char *tmp, *savepath;
599 if (!(vol->v_flags & AFPVOL_CASEINSEN))
602 if (veto_file(ENUMVETO, path->u_name))
605 savepath = path->u_name;
607 /* very simple cache */
608 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
609 path->u_name = cname;
611 if (of_stat( path ) == 0 ) {
614 /* something changed, we cannot stat ... */
618 if (NULL == ( dp = opendir( "." )) ) {
619 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
624 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
625 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
626 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
628 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
630 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
631 if (NULL == check_dirent(vol, de->d_name))
634 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
637 if (strcasecmp_w( u2_path, u2_dename) == 0) {
639 strlcpy(cname, de->d_name, sizeof(cname));
640 path->u_name = cname;
642 if (of_stat( path ) == 0 ) {
643 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
644 strlcpy(lname, tmp, sizeof(lname));
657 /* invalidate cache */
660 path->u_name = savepath;
662 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
668 * @brief Construct struct dir
670 * Construct struct dir from parameters.
672 * @param m_name (r) directory name in UTF8-dec
673 * @param u_name (r) directory name in server side encoding
674 * @param vol (r) pointer to struct vol
675 * @param pdid (r) Parent CNID
676 * @param did (r) CNID
677 * @param fullpath (r) Full unix path to dir
679 * @returns pointer to new struct dir or NULL on error
681 * @note Most of the time mac name and unix name are the same.
683 struct dir *dir_new(const char *m_name,
685 const struct vol *vol,
692 dir = (struct dir *) calloc(1, sizeof( struct dir ));
696 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
701 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
704 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
705 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
706 dir->d_m_name_ucs2 = NULL;
709 if (m_name == u_name || !strcmp(m_name, u_name)) {
710 dir->d_u_name = dir->d_m_name;
712 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
713 bdestroy(dir->d_m_name);
720 dir->d_vid = vol->v_vid;
721 dir->d_fullpath = path;
726 * @brief Free a struct dir and all its members
728 * @param (rw) pointer to struct dir
730 void dir_free(struct dir *dir)
732 if (dir->d_u_name != dir->d_m_name) {
733 bdestroy(dir->d_u_name);
735 if (dir->d_m_name_ucs2)
736 free(dir->d_m_name_ucs2);
737 bdestroy(dir->d_m_name);
738 bdestroy(dir->d_fullpath);
743 * @brief Create struct dir from struct path
745 * Create a new struct dir from struct path. Then add it to the cache.
746 * The caller must have assured that the dir is not already in the cache,
748 * 1. Open adouble file, get CNID from it.
749 * 2. Search the database, hinting with the CNID from (1).
750 * 3. Build fullpath and create struct dir.
751 * 4. Add it to the cache.
753 * @param vol (r) pointer to struct vol
754 * @param dir (r) pointer to parrent directory
755 * @param path (rw) pointer to struct path with valid path->u_name
756 * @param len (r) strlen of path->u_name
758 * @returns Pointer to new struct dir or NULL on error.
760 * @note Function also assigns path->m_name from path->u_name.
762 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
765 struct dir *cdir = NULL;
768 struct adouble *adp = NULL;
775 assert(dircache_search_by_name(vol, dir->d_did, path->u_name, strlen(path->u_name)) == NULL);
777 /* get_id needs adp for reading CNID from adouble file */
778 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
779 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
783 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
789 ad_close_metadata(adp);
791 /* Get macname from unixname */
792 if (path->m_name == NULL) {
793 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
800 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
801 || (bconchar(fullpath, '/') != BSTR_OK)
802 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
803 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
808 /* Allocate and initialize struct dir */
809 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
814 if ((dircache_add(cdir)) != 0) { /* 4 */
815 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
822 ad_close_metadata(adp);
823 if (!cdir && fullpath)
834 * @brief Remove a dir from a cache and free it and any ressources with it
836 * @param (r) pointer to struct vol
837 * @param (rw) pointer to struct dir
839 int dir_remove(const struct vol *vol, struct dir *dir)
844 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
848 if (movecwd(vol, vol->v_root) < 0) {
849 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
853 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
860 * @brief Modify a struct dir, adust cache
862 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
863 * If given new_uname == new_mname, new_uname will point to new_mname.
865 * @param vol (r) pointer to struct vol
866 * @param dir (rw) pointer to struct dir
867 * @param pdid (r) new parent DID
868 * @param did (r) new DID
869 * @param new_mname (r) new mac-name
870 * @param new_uname (r) new unix-name
872 int dir_modify(const struct vol *vol,
876 const char *new_mname,
877 const char *new_uname,
878 bstring pdir_fullpath)
882 /* Remove it from the cache */
883 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
891 /* free uname if it's not the same as mname */
892 if (dir->d_m_name != dir->d_u_name)
893 bdestroy(dir->d_u_name);
895 if (new_uname == NULL)
896 new_uname = new_mname;
898 /* assign new name */
899 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
900 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
904 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
905 dir->d_u_name = dir->d_m_name;
907 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
908 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
915 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
917 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
919 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
923 if (dir->d_m_name_ucs2)
924 free(dir->d_m_name_ucs2);
925 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))
926 dir->d_m_name_ucs2 = NULL;
928 /* Re-add it to the cache */
929 if ((dircache_add(dir)) != 0) {
938 * @brief Resolve a catalog node name path
940 * If it's a filename:
941 * 1. compute unix name
942 * 2. stat the file, storing struct stat or errno in struct path
943 * 3. cwd (and curdir) is filename parent directory
944 * 4. return path with with filename
947 * 5. search the dircache
948 * 6. if not in the cache:
949 * 7. compute unix name
950 * 8. stat the dir, storing struct stat or errno in struct path
952 * 10. if chdir failed, return path with dirname
953 * cwd is dir parent directory
954 * if chdir succeeded, add to dircache
955 * return path with "" and "."
957 * 11. else in the cache:
958 * 12. stat the dir, storing struct stat or errno in struct path
960 * 14. if chdir failed
963 * if not last path part, return NULL
968 curdir: dir parent directory
973 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
975 static char path[ MAXPATHLEN + 1];
976 static struct path ret;
986 LOG(log_debug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
989 afp_errno = AFPERR_NOOBJ;
990 memset(&ret, 0, sizeof(ret));
992 switch (ret.m_type = *data) { /* path type */
995 len = (unsigned char) *data++;
997 if (afp_version >= 30) {
1003 if (afp_version >= 30) {
1005 memcpy(&hint, data, sizeof(hint));
1007 data += sizeof(hint);
1009 memcpy(&len16, data, sizeof(len16));
1015 /* else it's an error */
1017 afp_errno = AFPERR_PARAM;
1020 *cpath += len + size;
1025 if (movecwd(vol, dir) < 0 ) {
1026 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1027 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1029 return path_from_dir(vol, dir, &ret);
1037 * 1. single 0 -> delimiter
1038 * 2. additional 0 -> chdir(..)
1040 * a) name is a file, build struct path from it etc., exit while loop
1041 * b) name is a dir, add it to the dircache, chdir to it, continue
1043 if (*data == 0) { /* case 1 or 2 */
1046 while (len > 0 && *data == 0) { /* case 2 */
1047 /* chdir to parrent dir */
1048 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1050 if (movecwd( vol, dir ) < 0 ) {
1051 dir_remove(vol, dir);
1060 /* case 3: copy name from packet buffer to ret.m_name and process it */
1061 for ( p = path; *data != 0 && len > 0; len-- ) {
1063 if (p > &path[ MAXPATHLEN]) {
1064 afp_errno = AFPERR_PARAM;
1068 *p = 0; /* Terminate string */
1071 /* Get u_name from m_name */
1072 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) {
1073 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1077 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1079 /* Prevent access to our special folders like .AppleDouble */
1080 if (check_name(vol, ret.u_name)) {
1081 /* the name is illegal */
1082 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1083 afp_errno = AFPERR_PARAM;
1087 if (dir->d_did == DIRDID_ROOT_PARENT) {
1089 * Special case: CNID 1
1090 * root parent (did 1) has one child: the volume. Requests for did=1 with
1091 * some <name> must check against the volume name.
1093 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1099 * CNID != 1, eg. most of the times we take this way.
1100 * Now check if current path-part is a file or dir:
1101 * o if it's dir we have to step into it
1102 * o if it's a file we expect it to be the last part of the requested path
1103 * and thus call continue which should terminate the while loop because
1106 if (of_stat(&ret) != 0) {
1108 * ret.u_name doesn't exist, might be afp_createfile|dir
1109 * that means it should have been the last part
1112 /* it wasn't the last part, so we have a bogus path request */
1113 afp_errno = AFPERR_NOOBJ;
1117 * this will terminate clean in while (1) because len == 0,
1118 * probably afp_createfile|dir
1120 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1124 switch (ret.st.st_mode & S_IFMT) {
1126 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1128 /* it wasn't the last part, so we have a bogus path request */
1129 afp_errno = AFPERR_PARAM;
1132 continue; /* continues while loop */
1134 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1136 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1137 afp_errno = AFPERR_PARAM;
1140 continue; /* continues while loop */
1144 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1145 afp_errno = AFPERR_NODIR;
1149 /* Search the cache */
1150 int unamelen = strlen(ret.u_name);
1151 cdir = dircache_search_by_name(vol, dir->d_did, ret.u_name, unamelen);
1153 /* Not in cache, create one */
1154 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) {
1155 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1156 ntohl(dir->d_did), ret.u_name, getcwdpath());
1160 } /* if/else cnid==1 */
1162 /* Now chdir to the evaluated dir */
1163 if (movecwd( vol, cdir ) < 0 ) {
1164 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1165 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1167 return path_from_dir(vol, cdir, &ret);
1172 ret.m_name[0] = 0; /* so we later know last token was a dir */
1175 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1176 afp_errno = AFPERR_DID1;
1180 if (ret.m_name[0] == 0) {
1181 /* Last part was a dir */
1182 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1186 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1187 cfrombstring(dir->d_fullpath),
1188 cfrombstring(curdir->d_fullpath),
1195 * @brief chdir() to dir
1197 * @param vol (r) pointer to struct vol
1198 * @param dir (r) pointer to struct dir
1200 * @returns 0 on success, -1 on error with afp_errno set appropiately
1202 int movecwd(const struct vol *vol, struct dir *dir)
1209 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1210 cfrombstring(curdir->d_fullpath), getcwdpath());
1214 if (dir->d_did == DIRDID_ROOT_PARENT) {
1215 curdir = &rootParent;
1219 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1221 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1222 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1226 afp_errno = AFPERR_ACCESS;
1229 afp_errno = AFPERR_NOOBJ;
1239 * We can't use unix file's perm to support Apple's inherited protection modes.
1240 * If we aren't the file's owner we can't change its perms when moving it and smb
1241 * nfs,... don't even try.
1243 #define AFP_CHECK_ACCESS
1245 int check_access(char *path, int mode)
1247 #ifdef AFP_CHECK_ACCESS
1255 accessmode(p, &ma, curdir, NULL);
1256 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1258 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1264 /* --------------------- */
1265 int file_access(struct path *path, int mode)
1269 accessmode(path->u_name, &ma, curdir, &path->st);
1270 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1272 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1278 /* --------------------- */
1279 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1281 dir->offcnt = count;
1282 dir->ctime = st->st_ctime;
1283 dir->d_flags &= ~DIRF_CNID;
1287 /* ---------------------
1288 * is our cached also for reenumerate id?
1290 int dirreenumerate(struct dir *dir, struct stat *st)
1292 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1295 /* ------------------------------
1297 (name, dir) with curdir:name == dir, from afp_enumerate
1300 int getdirparams(const struct vol *vol,
1301 u_int16_t bitmap, struct path *s_path,
1303 char *buf, size_t *buflen )
1307 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1308 int bit = 0, isad = 0;
1314 struct stat *st = &s_path->st;
1315 char *upath = s_path->u_name;
1317 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1318 (1 << DIRPBIT_CDATE) |
1319 (1 << DIRPBIT_MDATE) |
1320 (1 << DIRPBIT_BDATE) |
1321 (1 << DIRPBIT_FINFO)))) {
1323 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1324 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1326 if (ad.ad_md->adf_flags & O_CREAT) {
1327 /* We just created it */
1328 ad_setname(&ad, s_path->m_name);
1343 while ( bitmap != 0 ) {
1344 while (( bitmap & 1 ) == 0 ) {
1352 ad_getattr(&ad, &ashort);
1353 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1354 ashort = htons(ATTRBIT_INVISIBLE);
1357 ashort |= htons(ATTRBIT_SHARED);
1358 memcpy( data, &ashort, sizeof( ashort ));
1359 data += sizeof( ashort );
1363 memcpy( data, &pdid, sizeof( pdid ));
1364 data += sizeof( pdid );
1367 case DIRPBIT_CDATE :
1368 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1369 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1370 memcpy( data, &aint, sizeof( aint ));
1371 data += sizeof( aint );
1374 case DIRPBIT_MDATE :
1375 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1376 memcpy( data, &aint, sizeof( aint ));
1377 data += sizeof( aint );
1380 case DIRPBIT_BDATE :
1381 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1382 aint = AD_DATE_START;
1383 memcpy( data, &aint, sizeof( aint ));
1384 data += sizeof( aint );
1387 case DIRPBIT_FINFO :
1389 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1390 } else { /* no appledouble */
1391 memset( data, 0, 32 );
1392 /* set default view -- this also gets done in ad_open() */
1393 ashort = htons(FINDERINFO_CLOSEDVIEW);
1394 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1396 /* dot files are by default visible */
1397 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1398 ashort = htons(FINDERINFO_INVISIBLE);
1399 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1405 case DIRPBIT_LNAME :
1406 if (dir->d_m_name) /* root of parent can have a null name */
1409 memset(data, 0, sizeof(u_int16_t));
1410 data += sizeof( u_int16_t );
1413 case DIRPBIT_SNAME :
1414 memset(data, 0, sizeof(u_int16_t));
1415 data += sizeof( u_int16_t );
1419 memcpy( data, &dir->d_did, sizeof( aint ));
1420 data += sizeof( aint );
1423 case DIRPBIT_OFFCNT :
1425 /* this needs to handle current directory access rights */
1426 if (diroffcnt(dir, st)) {
1427 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1429 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1430 setdiroffcnt(dir, st, ret);
1431 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1433 ashort = htons( ashort );
1434 memcpy( data, &ashort, sizeof( ashort ));
1435 data += sizeof( ashort );
1439 aint = htonl(st->st_uid);
1440 memcpy( data, &aint, sizeof( aint ));
1441 data += sizeof( aint );
1445 aint = htonl(st->st_gid);
1446 memcpy( data, &aint, sizeof( aint ));
1447 data += sizeof( aint );
1450 case DIRPBIT_ACCESS :
1451 accessmode( upath, &ma, dir , st);
1453 *data++ = ma.ma_user;
1454 *data++ = ma.ma_world;
1455 *data++ = ma.ma_group;
1456 *data++ = ma.ma_owner;
1459 /* Client has requested the ProDOS information block.
1460 Just pass back the same basic block for all
1461 directories. <shirsch@ibm.net> */
1462 case DIRPBIT_PDINFO :
1463 if (afp_version >= 30) { /* UTF8 name */
1464 utf8 = kTextEncodingUTF8;
1465 if (dir->d_m_name) /* root of parent can have a null name */
1468 memset(data, 0, sizeof(u_int16_t));
1469 data += sizeof( u_int16_t );
1471 memcpy(data, &aint, sizeof( aint ));
1472 data += sizeof( aint );
1474 else { /* ProDOS Info Block */
1477 ashort = htons( 0x0200 );
1478 memcpy( data, &ashort, sizeof( ashort ));
1479 data += sizeof( ashort );
1480 memset( data, 0, sizeof( ashort ));
1481 data += sizeof( ashort );
1485 case DIRPBIT_UNIXPR :
1486 aint = htonl(st->st_uid);
1487 memcpy( data, &aint, sizeof( aint ));
1488 data += sizeof( aint );
1489 aint = htonl(st->st_gid);
1490 memcpy( data, &aint, sizeof( aint ));
1491 data += sizeof( aint );
1494 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1495 memcpy( data, &aint, sizeof( aint ));
1496 data += sizeof( aint );
1498 accessmode( upath, &ma, dir , st);
1500 *data++ = ma.ma_user;
1501 *data++ = ma.ma_world;
1502 *data++ = ma.ma_group;
1503 *data++ = ma.ma_owner;
1508 ad_close_metadata( &ad );
1510 return( AFPERR_BITMAP );
1516 ashort = htons( data - buf );
1517 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1518 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1520 if ( utf_nameoff ) {
1521 ashort = htons( data - buf );
1522 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1523 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1526 ad_close_metadata( &ad );
1528 *buflen = data - buf;
1532 /* ----------------------------- */
1533 int path_error(struct path *path, int error)
1535 /* - a dir with access error
1536 * - no error it's a file
1539 if (path_isadir(path))
1541 if (path->st_valid && path->st_errno)
1543 return AFPERR_BADTYPE ;
1546 /* ----------------------------- */
1547 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1552 u_int16_t vid, bitmap;
1558 memcpy( &vid, ibuf, sizeof( vid ));
1559 ibuf += sizeof( vid );
1561 if (NULL == ( vol = getvolbyvid( vid )) ) {
1562 return( AFPERR_PARAM );
1565 if (vol->v_flags & AFPVOL_RO)
1566 return AFPERR_VLOCK;
1568 memcpy( &did, ibuf, sizeof( did ));
1569 ibuf += sizeof( int );
1571 if (NULL == ( dir = dirlookup( vol, did )) ) {
1575 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1576 bitmap = ntohs( bitmap );
1577 ibuf += sizeof( bitmap );
1579 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1580 return get_afp_errno(AFPERR_NOOBJ);
1583 if ( *path->m_name != '\0' ) {
1584 rc = path_error(path, AFPERR_NOOBJ);
1585 /* maybe we are trying to set perms back */
1586 if (rc != AFPERR_ACCESS)
1591 * If ibuf is odd, make it even.
1593 if ((u_long)ibuf & 1 ) {
1597 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1598 setvoltime(obj, vol );
1604 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1606 * assume path == '\0' eg. it's a directory in canonical form
1608 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1620 u_int16_t ashort, bshort, oshort;
1622 int change_mdate = 0;
1623 int change_parent_mdate = 0;
1625 u_int16_t bitmap = d_bitmap;
1626 u_char finder_buf[32];
1629 u_int16_t upriv_bit = 0;
1632 upath = path->u_name;
1634 while ( bitmap != 0 ) {
1635 while (( bitmap & 1 ) == 0 ) {
1643 memcpy( &ashort, buf, sizeof( ashort ));
1644 buf += sizeof( ashort );
1646 case DIRPBIT_CDATE :
1648 memcpy(&cdate, buf, sizeof(cdate));
1649 buf += sizeof( cdate );
1651 case DIRPBIT_MDATE :
1652 memcpy(&newdate, buf, sizeof(newdate));
1653 buf += sizeof( newdate );
1655 case DIRPBIT_BDATE :
1657 memcpy(&bdate, buf, sizeof(bdate));
1658 buf += sizeof( bdate );
1660 case DIRPBIT_FINFO :
1662 memcpy( finder_buf, buf, 32 );
1665 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1666 change_parent_mdate = 1;
1667 memcpy( &owner, buf, sizeof(owner));
1668 buf += sizeof( owner );
1671 change_parent_mdate = 1;
1672 memcpy( &group, buf, sizeof( group ));
1673 buf += sizeof( group );
1675 case DIRPBIT_ACCESS :
1677 change_parent_mdate = 1;
1678 ma.ma_user = *buf++;
1679 ma.ma_world = *buf++;
1680 ma.ma_group = *buf++;
1681 ma.ma_owner = *buf++;
1682 mpriv = mtoumode( &ma ) | vol->v_dperm;
1683 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1684 err = set_dir_errors(path, "setdirmode", errno);
1688 /* Ignore what the client thinks we should do to the
1689 ProDOS information block. Skip over the data and
1690 report nothing amiss. <shirsch@ibm.net> */
1691 case DIRPBIT_PDINFO :
1692 if (afp_version < 30) {
1696 err = AFPERR_BITMAP;
1700 case DIRPBIT_UNIXPR :
1701 if (vol_unix_priv(vol)) {
1702 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1703 buf += sizeof( owner );
1704 memcpy( &group, buf, sizeof( group ));
1705 buf += sizeof( group );
1708 change_parent_mdate = 1;
1709 memcpy( &upriv, buf, sizeof( upriv ));
1710 buf += sizeof( upriv );
1711 upriv = ntohl (upriv) | vol->v_dperm;
1712 if (dir_rx_set(upriv)) {
1713 /* maybe we are trying to set perms back */
1714 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1716 err = set_dir_errors(path, "setdirunixmode", errno);
1727 err = AFPERR_BITMAP;
1735 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1737 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1739 * Check to see what we're trying to set. If it's anything
1740 * but ACCESS, UID, or GID, give an error. If it's any of those
1741 * three, we don't need the ad to be open, so just continue.
1743 * note: we also don't need to worry about mdate. also, be quiet
1744 * if we're using the noadouble option.
1746 if (!vol_noadouble(vol) && (d_bitmap &
1747 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1748 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1749 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1750 return AFPERR_ACCESS;
1756 * Check to see if a create was necessary. If it was, we'll want
1757 * to set our name, etc.
1759 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1760 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1766 while ( bitmap != 0 ) {
1767 while (( bitmap & 1 ) == 0 ) {
1775 ad_getattr(&ad, &bshort);
1777 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1778 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1782 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1783 change_parent_mdate = 1;
1784 ad_setattr(&ad, bshort);
1787 case DIRPBIT_CDATE :
1789 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1792 case DIRPBIT_MDATE :
1794 case DIRPBIT_BDATE :
1796 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1799 case DIRPBIT_FINFO :
1801 /* Fixes #2802236 */
1802 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1803 *fflags &= htons(~FINDERINFO_ISHARED);
1805 if ( dir->d_did == DIRDID_ROOT ) {
1807 * Alright, we admit it, this is *really* sick!
1808 * The 4 bytes that we don't copy, when we're dealing
1809 * with the root of a volume, are the directory's
1810 * location information. This eliminates that annoying
1811 * behavior one sees when mounting above another mount
1814 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1815 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1817 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1821 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1822 if ( (dir->d_did == DIRDID_ROOT) &&
1823 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1824 err = set_dir_errors(path, "setdeskowner", errno);
1825 if (isad && err == AFPERR_PARAM) {
1826 err = AFP_OK; /* ???*/
1829 goto setdirparam_done;
1832 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1833 err = set_dir_errors(path, "setdirowner", errno);
1834 goto setdirparam_done;
1838 if (dir->d_did == DIRDID_ROOT)
1839 setdeskowner( -1, ntohl(group) );
1840 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1841 err = set_dir_errors(path, "setdirowner", errno);
1842 goto setdirparam_done;
1845 case DIRPBIT_ACCESS :
1846 if (dir->d_did == DIRDID_ROOT) {
1848 if (!dir_rx_set(mpriv)) {
1849 /* we can't remove read and search for owner on volume root */
1850 err = AFPERR_ACCESS;
1851 goto setdirparam_done;
1855 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1856 err = set_dir_errors(path, "setdirmode", errno);
1857 goto setdirparam_done;
1860 case DIRPBIT_PDINFO :
1861 if (afp_version >= 30) {
1862 err = AFPERR_BITMAP;
1863 goto setdirparam_done;
1866 case DIRPBIT_UNIXPR :
1867 if (vol_unix_priv(vol)) {
1868 if (dir->d_did == DIRDID_ROOT) {
1869 if (!dir_rx_set(upriv)) {
1870 /* we can't remove read and search for owner on volume root */
1871 err = AFPERR_ACCESS;
1872 goto setdirparam_done;
1874 setdeskowner( -1, ntohl(group) );
1875 setdeskmode( upriv );
1877 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1878 err = set_dir_errors(path, "setdirowner", errno);
1879 goto setdirparam_done;
1882 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1883 err = set_dir_errors(path, "setdirunixmode", errno);
1884 goto setdirparam_done;
1888 err = AFPERR_BITMAP;
1889 goto setdirparam_done;
1893 err = AFPERR_BITMAP;
1894 goto setdirparam_done;
1903 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1904 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1908 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1909 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1914 if (path->st_valid && !path->st_errno) {
1915 struct stat *st = &path->st;
1917 if (dir && dir->d_pdid) {
1918 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1922 ad_close_metadata( &ad);
1926 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1927 && gettimeofday(&tv, NULL) == 0) {
1928 if (!movecwd(vol, dir->d_parent)) {
1929 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1930 /* be careful with bitmap because now dir is null */
1931 bitmap = 1<<DIRPBIT_MDATE;
1932 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1933 /* should we reset curdir ?*/
1940 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1954 memcpy( &vid, ibuf, sizeof( vid ));
1955 ibuf += sizeof( vid );
1956 if (NULL == (vol = getvolbyvid( vid )) ) {
1957 return( AFPERR_PARAM );
1960 memcpy( &did, ibuf, sizeof( did ));
1961 ibuf += sizeof( did );
1965 * if it's CNID 2 our only choice to meet the specs is call sync.
1966 * For any other CNID just sync that dir. To my knowledge the
1967 * intended use of FPSyncDir is to sync the volume so all we're
1968 * ever going to see here is probably CNID 2. Anyway, we' prepared.
1971 if ( ntohl(did) == 2 ) {
1974 if (NULL == ( dir = dirlookup( vol, did )) ) {
1975 return afp_errno; /* was AFPERR_NOOBJ */
1978 if (movecwd( vol, dir ) < 0 )
1979 return ( AFPERR_NOOBJ );
1982 * Assuming only OSens that have dirfd also may require fsyncing directories
1983 * in order to flush metadata e.g. Linux.
1987 if (NULL == ( dp = opendir( "." )) ) {
1990 return( AFPERR_NOOBJ );
1992 return( AFPERR_ACCESS );
1994 return( AFPERR_PARAM );
1998 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2001 if ( fsync ( dfd ) < 0 )
2002 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2003 dir->d_u_name, strerror(errno) );
2004 closedir(dp); /* closes dfd too */
2007 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2010 return( AFPERR_NOOBJ );
2012 return( AFPERR_ACCESS );
2014 return( AFPERR_PARAM );
2018 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2019 vol->ad_path(".", ADFLAGS_DIR) );
2021 if ( fsync(dfd) < 0 )
2022 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2023 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2030 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2036 struct path *s_path;
2044 memcpy( &vid, ibuf, sizeof( vid ));
2045 ibuf += sizeof( vid );
2046 if (NULL == ( vol = getvolbyvid( vid )) ) {
2047 return( AFPERR_PARAM );
2050 if (vol->v_flags & AFPVOL_RO)
2051 return AFPERR_VLOCK;
2053 memcpy( &did, ibuf, sizeof( did ));
2054 ibuf += sizeof( did );
2055 if (NULL == ( dir = dirlookup( vol, did )) ) {
2056 return afp_errno; /* was AFPERR_NOOBJ */
2058 /* for concurrent access we need to be sure we are not in the
2059 * folder we want to create...
2063 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2064 return get_afp_errno(AFPERR_PARAM);
2066 /* cname was able to move curdir to it! */
2067 if (*s_path->m_name == '\0')
2068 return AFPERR_EXIST;
2070 upath = s_path->u_name;
2072 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2076 if (of_stat(s_path) < 0) {
2082 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2086 if ( movecwd( vol, dir ) < 0 ) {
2087 return( AFPERR_PARAM );
2090 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2091 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2092 if (vol_noadouble(vol))
2093 goto createdir_done;
2094 return( AFPERR_ACCESS );
2096 ad_setname(&ad, s_path->m_name);
2097 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2100 ad_close_metadata( &ad);
2103 #ifdef HAVE_NFSv4_ACLS
2104 /* FIXME: are we really inside the created dir? */
2105 addir_inherit_acl(vol);
2108 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2109 *rbuflen = sizeof( u_int32_t );
2110 setvoltime(obj, vol );
2115 * dst new unix filename (not a pathname)
2116 * newname new mac name
2119 int renamedir(const struct vol *vol, char *src, char *dst,
2121 struct dir *newparent,
2127 /* existence check moved to afp_moveandrename */
2128 if ( unix_rename( src, dst ) < 0 ) {
2131 return( AFPERR_NOOBJ );
2133 return( AFPERR_ACCESS );
2135 return AFPERR_VLOCK;
2137 /* tried to move directory into a subdirectory of itself */
2138 return AFPERR_CANTMOVE;
2140 /* this needs to copy and delete. bleah. that means we have
2141 * to deal with entire directory hierarchies. */
2142 if ((err = copydir(vol, src, dst)) < 0) {
2146 if ((err = deletedir(src)) < 0)
2150 return( AFPERR_PARAM );
2154 vol->vfs->vfs_renamedir(vol, src, dst);
2156 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2158 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2159 ad_setname(&ad, newname);
2161 ad_close_metadata( &ad);
2164 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2165 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2172 /* delete an empty directory */
2173 int deletecurdir(struct vol *vol)
2183 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2184 return( AFPERR_ACCESS );
2189 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2190 /* we never want to create a resource fork here, we are going to delete it */
2191 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2193 ad_getattr(&ad, &ashort);
2194 ad_close( &ad, ADFLAGS_HF );
2195 if ((ashort & htons(ATTRBIT_NODELETE))) {
2196 return AFPERR_OLOCK;
2199 err = vol->vfs->vfs_deletecurdir(vol);
2204 /* now get rid of dangling symlinks */
2205 if ((dp = opendir("."))) {
2206 while ((de = readdir(dp))) {
2207 /* skip this and previous directory */
2208 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2211 /* bail if it's not a symlink */
2212 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2214 return AFPERR_DIRNEMPT;
2217 if ((err = netatalk_unlink(de->d_name))) {
2224 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2229 err = netatalk_rmdir_all_errors(cfrombstring(fdir->d_u_name));
2230 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2231 cnid_delete(vol->v_cdb, fdir->d_did);
2232 dir_remove( vol, fdir );
2236 /* inode is used as key for cnid.
2237 * Close the descriptor only after cnid_delete
2245 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2255 sfunc = (unsigned char) *ibuf++;
2259 if (sfunc >= 3 && sfunc <= 6) {
2260 if (afp_version < 30) {
2261 return( AFPERR_PARAM );
2268 case 3 :/* unicode */
2269 memcpy( &id, ibuf, sizeof( id ));
2272 if (( pw = getpwuid( id )) == NULL ) {
2273 return( AFPERR_NOITEM );
2275 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2276 pw->pw_name, -1, &name);
2283 case 4 : /* unicode */
2284 memcpy( &id, ibuf, sizeof( id ));
2287 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2288 return( AFPERR_NOITEM );
2290 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2291 gr->gr_name, -1, &name);
2297 #ifdef HAVE_NFSv4_ACLS
2298 case 5 : /* UUID -> username */
2299 case 6 : /* UUID -> groupname */
2300 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2301 return AFPERR_PARAM;
2302 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2304 len = getnamefromuuid( ibuf, &name, &type);
2305 if (len != 0) /* its a error code, not len */
2306 return AFPERR_NOITEM;
2307 if (type == UUID_USER) {
2308 if (( pw = getpwnam( name )) == NULL )
2309 return( AFPERR_NOITEM );
2310 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2311 id = htonl(UUID_USER);
2312 memcpy( rbuf, &id, sizeof( id ));
2313 id = htonl( pw->pw_uid);
2314 rbuf += sizeof( id );
2315 memcpy( rbuf, &id, sizeof( id ));
2316 rbuf += sizeof( id );
2317 *rbuflen = 2 * sizeof( id );
2318 } else { /* type == UUID_GROUP */
2319 if (( gr = getgrnam( name )) == NULL )
2320 return( AFPERR_NOITEM );
2321 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2322 id = htonl(UUID_GROUP);
2323 memcpy( rbuf, &id, sizeof( id ));
2324 rbuf += sizeof( id );
2325 id = htonl( gr->gr_gid);
2326 memcpy( rbuf, &id, sizeof( id ));
2327 rbuf += sizeof( id );
2328 *rbuflen = 2 * sizeof( id );
2333 return( AFPERR_PARAM );
2337 len = strlen( name );
2340 u_int16_t tp = htons(len);
2341 memcpy(rbuf, &tp, sizeof(tp));
2350 memcpy( rbuf, name, len );
2358 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2367 sfunc = (unsigned char) *ibuf++;
2369 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2372 case 2 : /* unicode */
2373 if (afp_version < 30) {
2374 return( AFPERR_PARAM );
2376 memcpy(&ulen, ibuf, sizeof(ulen));
2379 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2383 len = (unsigned char) *ibuf++;
2385 #ifdef HAVE_NFSv4_ACLS
2386 case 5 : /* username -> UUID */
2387 case 6 : /* groupname -> UUID */
2388 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2389 return AFPERR_PARAM;
2390 memcpy(&ulen, ibuf, sizeof(ulen));
2396 return( AFPERR_PARAM );
2402 return AFPERR_PARAM;
2405 case 1 : /* unicode */
2407 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2408 return( AFPERR_NOITEM );
2412 memcpy( rbuf, &id, sizeof( id ));
2413 *rbuflen = sizeof( id );
2416 case 2 : /* unicode */
2418 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2419 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2420 return( AFPERR_NOITEM );
2423 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2425 memcpy( rbuf, &id, sizeof( id ));
2426 *rbuflen = sizeof( id );
2428 #ifdef HAVE_NFSv4_ACLS
2429 case 5 : /* username -> UUID */
2430 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2431 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2432 return AFPERR_NOITEM;
2433 *rbuflen = UUID_BINSIZE;
2435 case 6 : /* groupname -> UUID */
2436 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2437 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2438 return AFPERR_NOITEM;
2439 *rbuflen = UUID_BINSIZE;
2447 /* ------------------------------------
2448 variable DID support
2450 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2461 /* do nothing as dids are static for the life of the process. */
2465 memcpy(&vid, ibuf, sizeof( vid ));
2466 ibuf += sizeof( vid );
2467 if (( vol = getvolbyvid( vid )) == NULL ) {
2468 return( AFPERR_PARAM );
2471 memcpy( &did, ibuf, sizeof( did ));
2472 ibuf += sizeof( did );
2473 if (( dir = dirlookup( vol, did )) == NULL ) {
2474 return( AFPERR_PARAM );
2477 /* dir_remove -- deletedid */
2483 /* did creation gets done automatically
2484 * there's a pb again with case but move it to cname
2486 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2489 struct dir *parentdir;
2497 memcpy(&vid, ibuf, sizeof(vid));
2498 ibuf += sizeof( vid );
2500 if (NULL == ( vol = getvolbyvid( vid )) ) {
2501 return( AFPERR_PARAM );
2504 memcpy(&did, ibuf, sizeof(did));
2505 ibuf += sizeof(did);
2507 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2511 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2512 return get_afp_errno(AFPERR_PARAM);
2515 if ( *path->m_name != '\0' ) {
2516 return path_error(path, AFPERR_NOOBJ);
2519 if ( !path->st_valid && of_stat(path ) < 0 ) {
2520 return( AFPERR_NOOBJ );
2522 if ( path->st_errno ) {
2523 return( AFPERR_NOOBJ );
2526 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2527 *rbuflen = sizeof(curdir->d_did);