2 * $Id: directory.c,v 1.131.2.2 2010-02-01 12:59:09 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
59 /*******************************************************************************************
61 ******************************************************************************************/
64 struct dir rootParent = {
65 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
66 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
67 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
69 struct dir *curdir = &rootParent;
70 struct path Cur_Path = {
76 0, /* stat is not set */
80 /*******************************************************************************************
82 ******************************************************************************************/
85 /* -------------------------
86 appledouble mkdir afp error code.
88 static int netatalk_mkdir(const char *name)
90 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
93 return( AFPERR_NOOBJ );
95 return( AFPERR_VLOCK );
98 return( AFPERR_ACCESS );
100 return( AFPERR_EXIST );
103 return( AFPERR_DFULL );
105 return( AFPERR_PARAM );
111 /* ------------------- */
112 static int deletedir(char *dir)
114 char path[MAXPATHLEN + 1];
122 if ((len = strlen(dir)) +2 > sizeof(path))
126 if ((dp = opendir(dir)) == NULL)
132 remain = sizeof(path) -len -1;
133 while ((de = readdir(dp)) && err == AFP_OK) {
134 /* skip this and previous directory */
135 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
138 if (strlen(de->d_name) > remain) {
142 strcpy(path + len, de->d_name);
143 if (stat(path, &st)) {
146 if (S_ISDIR(st.st_mode)) {
147 err = deletedir(path);
149 err = netatalk_unlink(path);
154 /* okay. the directory is empty. delete it. note: we already got rid
157 err = netatalk_rmdir(dir);
162 /* do a recursive copy. */
163 static int copydir(const struct vol *vol, char *src, char *dst)
165 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
174 /* doesn't exist or the path is too long. */
175 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
176 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
177 ((dp = opendir(src)) == NULL))
180 /* try to create the destination directory */
181 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
186 /* set things up to copy */
190 srem = sizeof(spath) - slen -1;
195 drem = sizeof(dpath) - dlen -1;
198 while ((de = readdir(dp))) {
199 /* skip this and previous directory */
200 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
203 if (strlen(de->d_name) > srem) {
207 strcpy(spath + slen, de->d_name);
209 if (stat(spath, &st) == 0) {
210 if (strlen(de->d_name) > drem) {
214 strcpy(dpath + dlen, de->d_name);
216 if (S_ISDIR(st.st_mode)) {
217 if (AFP_OK != (err = copydir(vol, spath, dpath)))
219 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
223 /* keep the same time stamp. */
224 ut.actime = ut.modtime = st.st_mtime;
230 /* keep the same time stamp. */
231 if (stat(src, &st) == 0) {
232 ut.actime = ut.modtime = st.st_mtime;
241 /* ---------------------
242 * is our cached offspring count valid?
244 static int diroffcnt(struct dir *dir, struct stat *st)
246 return st->st_ctime == dir->ctime;
249 /* --------------------- */
250 static int invisible_dots(const struct vol *vol, const char *name)
252 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
255 /* ------------------ */
256 static int set_dir_errors(struct path *path, const char *where, int err)
261 return AFPERR_ACCESS;
265 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
270 * @brief Convert name in client encoding to server encoding
272 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
273 * This only gets called from cname().
275 * @returns 0 on success, -1 on error
277 * @note If the passed ret->m_name is mangled, we'll demangle it
279 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
281 static char temp[ MAXPATHLEN + 1];
285 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s', toUTF8:%u)", ret->m_name, toUTF8);
287 if (afp_version >= 30) {
289 if (dir->d_did == DIRDID_ROOT_PARENT) {
291 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
292 * So we compare it with the longname from the current volume and if they match
293 * we overwrite the requested path with the utf8 volume name so that the following
296 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
297 if (strcasecmp(ret->m_name, temp) == 0)
298 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
301 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
302 afp_errno = AFPERR_PARAM;
305 strcpy(ret->m_name, temp);
309 /* check for OS X mangled filename :( */
310 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
311 if (t != ret->m_name) {
313 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
314 * flags weren't the same
316 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
317 /* at last got our view of mac name */
318 strcpy(ret->m_name, t);
321 } /* afp_version >= 30 */
323 /* If we haven't got it by now, get it */
324 if (ret->u_name == NULL) {
325 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
326 afp_errno = AFPERR_PARAM;
335 * @brief Build struct path from struct dir
337 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
338 * 1. move cwd into parent dir (we're often already there, but not always)
340 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
343 * it's tricky: movecwd failed some of dir path are not there anymore.
344 * FIXME: Is it true with other errors?
346 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
352 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 )
355 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
356 if (dir->d_m_name == dir->d_u_name) {
357 ret->u_name = ret->m_name;
359 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
360 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
365 ret->st_errno = EACCES;
369 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 )
372 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
373 if (dir->d_m_name == dir->d_u_name) {
374 ret->u_name = ret->m_name;
376 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
377 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
381 ret->st_errno = ENOENT;
383 dir_remove(vol, dir);
390 /* DEADC0DE: never get here */
395 /*********************************************************************************************
397 ********************************************************************************************/
399 int get_afp_errno(const int param)
401 if (afp_errno != AFPERR_DID1)
407 * @brief Resolve a DID
409 * Resolve a DID, allocate a struct dir for it
410 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
411 * 2. Check if the DID is in the cache.
412 * 3. If it's not in the cache resolve it via the database.
413 * 4. Build complete server-side path to the dir.
414 * 5. Check if it exists and is a directory.
415 * 6. Create the struct dir and populate it.
416 * 7. Add it to the cache.
418 * @param vol (r) pointer to struct vol
419 * @param did (r) DID to resolve
421 * @returns pointer to struct dir
423 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
424 * and we are really bad in this case.
426 struct dir *dirlookup(const struct vol *vol, cnid_t did)
428 static char buffer[12 + MAXPATHLEN + 1];
429 struct bstrList *pathlist = NULL;
430 bstring fullpath = NULL;
432 struct dir *ret = NULL;
436 int buflen = 12 + MAXPATHLEN + 1;
440 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
442 /* check for did 0, 1 and 2 */
443 if (did == 0 || vol == NULL) { /* 1 */
444 afp_errno = AFPERR_PARAM;
446 } else if (did == DIRDID_ROOT_PARENT) {
447 rootParent.d_vid = vol->v_vid;
448 return (&rootParent);
449 } else if (did == DIRDID_ROOT) {
453 /* Search the cache */
454 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
458 utf8 = utf8_encoding();
459 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
461 /* Create list for path elements, request 16 list elements for now*/
462 if ((pathlist = bstListCreateMin(16)) == NULL) { /* 4 */
463 LOG(log_error, logtype_afpd, "dirlookup(did: %u): OOM: %s", ntohl(did), strerror(errno));
467 /* Get it from the database */
469 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
470 afp_errno = AFPERR_NOOBJ;
476 /* construct path, copy already found uname to path element list*/
477 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
478 afp_errno = AFPERR_MISC;
483 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {%u, %s}", ntohl(did), ntohl(pdid), upath);
485 /* The stuff that follows is for building the full path to the directory */
487 /* work upwards until we reach volume root */
488 while (cnid != DIRDID_ROOT) {
489 /* construct path, copy already found uname to path element list*/
490 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
491 afp_errno = AFPERR_MISC;
497 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
498 afp_errno = AFPERR_NOOBJ;
504 if ((bstrListPush(pathlist, bfromcstr(vol->v_path))) != BSTR_OK) { /* 4 */
505 afp_errno = AFPERR_MISC;
510 if ((fullpath = bjoinInv(pathlist, bfromcstr("/"))) == NULL) { /* 4 */
511 afp_errno = AFPERR_MISC;
515 /* Finished building the fullpath */
517 /* stat it and check if it's a dir */
518 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
520 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
523 afp_errno = AFPERR_NOOBJ;
527 afp_errno = AFPERR_ACCESS;
531 afp_errno = AFPERR_MISC;
536 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
537 afp_errno = AFPERR_BADTYPE;
543 /* Get macname from unix name */
544 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
545 afp_errno = AFPERR_NOOBJ;
550 /* Create struct dir */
551 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
552 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
557 /* Add it to the cache only if it's a dir */
558 if (dircache_add(ret) != 0) { /* 7 */
563 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
564 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
568 bstrListDestroy(pathlist);
571 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
572 ntohl(did), AfpErr2name(afp_errno));
583 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
585 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
590 static u_int32_t did = 0;
591 static char cname[MAXPATHLEN];
592 static char lname[MAXPATHLEN];
593 ucs2_t u2_path[MAXPATHLEN];
594 ucs2_t u2_dename[MAXPATHLEN];
595 char *tmp, *savepath;
597 if (!(vol->v_flags & AFPVOL_CASEINSEN))
600 if (veto_file(ENUMVETO, path->u_name))
603 savepath = path->u_name;
605 /* very simple cache */
606 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
607 path->u_name = cname;
609 if (of_stat( path ) == 0 ) {
612 /* something changed, we cannot stat ... */
616 if (NULL == ( dp = opendir( "." )) ) {
617 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
622 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
623 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
624 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
626 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
628 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
629 if (NULL == check_dirent(vol, de->d_name))
632 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
635 if (strcasecmp_w( u2_path, u2_dename) == 0) {
637 strlcpy(cname, de->d_name, sizeof(cname));
638 path->u_name = cname;
640 if (of_stat( path ) == 0 ) {
641 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
642 strlcpy(lname, tmp, sizeof(lname));
655 /* invalidate cache */
658 path->u_name = savepath;
660 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
666 * @brief Construct struct dir
668 * Construct struct dir from parameters.
670 * @param m_name (r) directory name in UTF8-dec
671 * @param u_name (r) directory name in server side encoding
672 * @param vol (r) pointer to struct vol
673 * @param pdid (r) Parent CNID
674 * @param did (r) CNID
675 * @param fullpath (r) Full unix path to dir
677 * @returns pointer to new struct dir or NULL on error
679 * @note Most of the time mac name and unix name are the same.
681 struct dir *dir_new(const char *m_name,
683 const struct vol *vol,
690 dir = (struct dir *) calloc(1, sizeof( struct dir ));
694 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
699 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
702 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
703 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
704 dir->d_m_name_ucs2 = NULL;
707 if (m_name == u_name || !strcmp(m_name, u_name)) {
708 dir->d_u_name = dir->d_m_name;
710 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
711 bdestroy(dir->d_m_name);
718 dir->d_vid = vol->v_vid;
719 dir->d_fullpath = path;
724 * @brief Free a struct dir and all its members
726 * @param (rw) pointer to struct dir
728 void dir_free(struct dir *dir)
730 if (dir->d_u_name != dir->d_m_name) {
731 bdestroy(dir->d_u_name);
733 if (dir->d_m_name_ucs2)
734 free(dir->d_m_name_ucs2);
735 bdestroy(dir->d_m_name);
736 bdestroy(dir->d_fullpath);
741 * @brief Create struct dir from struct path
743 * Create a new struct dir from struct path. Then add it to the cache.
744 * The caller must have assured that the dir is not already in the cache,
746 * 1. Open adouble file, get CNID from it.
747 * 2. Search the database, hinting with the CNID from (1).
748 * 3. Build fullpath and create struct dir.
749 * 4. Add it to the cache.
751 * @param vol (r) pointer to struct vol
752 * @param dir (r) pointer to parrent directory
753 * @param path (rw) pointer to struct path with valid path->u_name
754 * @param len (r) strlen of path->u_name
756 * @returns Pointer to new struct dir or NULL on error.
758 * @note Function also assigns path->m_name from path->u_name.
760 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
763 struct dir *cdir = NULL;
766 struct adouble *adp = NULL;
773 assert(dircache_search_by_name(vol, dir->d_did, path->u_name, strlen(path->u_name)) == NULL);
775 /* get_id needs adp for reading CNID from adouble file */
776 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
777 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
781 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
787 ad_close_metadata(adp);
789 /* Get macname from unixname */
790 if (path->m_name == NULL) {
791 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
798 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
799 || (bconchar(fullpath, '/') != BSTR_OK)
800 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
801 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
806 /* Allocate and initialize struct dir */
807 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
812 if ((dircache_add(cdir)) != 0) { /* 4 */
813 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
820 ad_close_metadata(adp);
821 if (!cdir && fullpath)
832 * @brief Remove a dir from a cache and free it and any ressources with it
834 * @param (r) pointer to struct vol
835 * @param (rw) pointer to struct dir
837 int dir_remove(const struct vol *vol, struct dir *dir)
842 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
846 if (movecwd(vol, vol->v_root) < 0) {
847 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
851 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
858 * @brief Modify a struct dir, adust cache
860 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
861 * If given new_uname == new_mname, new_uname will point to new_mname.
863 * @param vol (r) pointer to struct vol
864 * @param dir (rw) pointer to struct dir
865 * @param pdid (r) new parent DID
866 * @param did (r) new DID
867 * @param new_mname (r) new mac-name
868 * @param new_uname (r) new unix-name
870 int dir_modify(const struct vol *vol,
874 const char *new_mname,
875 const char *new_uname,
876 bstring pdir_fullpath)
880 /* Remove it from the cache */
881 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
889 /* free uname if it's not the same as mname */
890 if (dir->d_m_name != dir->d_u_name)
891 bdestroy(dir->d_u_name);
893 if (new_uname == NULL)
894 new_uname = new_mname;
896 /* assign new name */
897 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
898 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
902 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
903 dir->d_u_name = dir->d_m_name;
905 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
906 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
913 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
915 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
917 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
921 if (dir->d_m_name_ucs2)
922 free(dir->d_m_name_ucs2);
923 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))
924 dir->d_m_name_ucs2 = NULL;
926 /* Re-add it to the cache */
927 if ((dircache_add(dir)) != 0) {
936 * @brief Resolve a catalog node name path
938 * If it's a filename:
939 * 1. compute unix name
940 * 2. stat the file, storing struct stat or errno in struct path
941 * 3. cwd (and curdir) is filename parent directory
942 * 4. return path with with filename
945 * 5. search the dircache
946 * 6. if not in the cache:
947 * 7. compute unix name
948 * 8. stat the dir, storing struct stat or errno in struct path
950 * 10. if chdir failed, return path with dirname
951 * cwd is dir parent directory
952 * if chdir succeeded, add to dircache
953 * return path with "" and "."
955 * 11. else in the cache:
956 * 12. stat the dir, storing struct stat or errno in struct path
958 * 14. if chdir failed
961 * if not last path part, return NULL
966 curdir: dir parent directory
971 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
973 static char path[ MAXPATHLEN + 1];
974 static struct path ret;
984 LOG(log_debug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
987 afp_errno = AFPERR_NOOBJ;
988 memset(&ret, 0, sizeof(ret));
990 switch (ret.m_type = *data) { /* path type */
993 len = (unsigned char) *data++;
995 if (afp_version >= 30) {
1001 if (afp_version >= 30) {
1003 memcpy(&hint, data, sizeof(hint));
1005 data += sizeof(hint);
1007 memcpy(&len16, data, sizeof(len16));
1013 /* else it's an error */
1015 afp_errno = AFPERR_PARAM;
1018 *cpath += len + size;
1023 if (movecwd(vol, dir) < 0 ) {
1024 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1025 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1027 return path_from_dir(vol, dir, &ret);
1035 * 1. single 0 -> delimiter
1036 * 2. additional 0 -> chdir(..)
1038 * a) name is a file, build struct path from it etc., exit while loop
1039 * b) name is a dir, add it to the dircache, chdir to it, continue
1041 if (*data == 0) { /* case 1 or 2 */
1044 while (len > 0 && *data == 0) { /* case 2 */
1045 /* chdir to parrent dir */
1046 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1048 if (movecwd( vol, dir ) < 0 ) {
1049 dir_remove(vol, dir);
1058 /* case 3: copy name from packet buffer to ret.m_name and process it */
1059 for ( p = path; *data != 0 && len > 0; len-- ) {
1061 if (p > &path[ MAXPATHLEN]) {
1062 afp_errno = AFPERR_PARAM;
1066 *p = 0; /* Terminate string */
1069 /* Get u_name from m_name */
1070 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) {
1071 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1075 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1077 /* Prevent access to our special folders like .AppleDouble */
1078 if (check_name(vol, ret.u_name)) {
1079 /* the name is illegal */
1080 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1081 afp_errno = AFPERR_PARAM;
1085 if (dir->d_did == DIRDID_ROOT_PARENT) {
1087 * Special case: CNID 1
1088 * root parent (did 1) has one child: the volume. Requests for did=1 with
1089 * some <name> must check against the volume name.
1091 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1097 * CNID != 1, eg. most of the times we take this way.
1098 * Now check if current path-part is a file or dir:
1099 * o if it's dir we have to step into it
1100 * o if it's a file we expect it to be the last part of the requested path
1101 * and thus call continue which should terminate the while loop because
1104 if (of_stat(&ret) != 0) {
1106 * ret.u_name doesn't exist, might be afp_createfile|dir
1107 * that means it should have been the last part
1110 /* it wasn't the last part, so we have a bogus path request */
1111 afp_errno = AFPERR_NOOBJ;
1115 * this will terminate clean in while (1) because len == 0,
1116 * probably afp_createfile|dir
1118 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1122 switch (ret.st.st_mode & S_IFMT) {
1124 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1126 /* it wasn't the last part, so we have a bogus path request */
1127 afp_errno = AFPERR_PARAM;
1130 continue; /* continues while loop */
1132 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1134 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1135 afp_errno = AFPERR_PARAM;
1138 continue; /* continues while loop */
1142 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1143 afp_errno = AFPERR_NODIR;
1147 /* Search the cache */
1148 int unamelen = strlen(ret.u_name);
1149 cdir = dircache_search_by_name(vol, dir->d_did, ret.u_name, unamelen);
1151 /* Not in cache, create one */
1152 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) {
1153 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1154 ntohl(dir->d_did), ret.u_name, getcwdpath());
1158 } /* if/else cnid==1 */
1160 /* Now chdir to the evaluated dir */
1161 if (movecwd( vol, cdir ) < 0 ) {
1162 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1163 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1165 return path_from_dir(vol, cdir, &ret);
1170 ret.m_name[0] = 0; /* so we later know last token was a dir */
1173 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1174 afp_errno = AFPERR_DID1;
1178 if (ret.m_name[0] == 0) {
1179 /* Last part was a dir */
1180 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1184 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1185 cfrombstring(dir->d_fullpath),
1186 cfrombstring(curdir->d_fullpath),
1193 * @brief chdir() to dir
1195 * @param vol (r) pointer to struct vol
1196 * @param dir (r) pointer to struct dir
1198 * @returns 0 on success, -1 on error with afp_errno set appropiately
1200 int movecwd(const struct vol *vol, struct dir *dir)
1207 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1208 cfrombstring(curdir->d_fullpath), getcwdpath());
1212 if (dir->d_did == DIRDID_ROOT_PARENT) {
1213 curdir = &rootParent;
1217 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1219 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1220 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1224 afp_errno = AFPERR_ACCESS;
1227 afp_errno = AFPERR_NOOBJ;
1237 * We can't use unix file's perm to support Apple's inherited protection modes.
1238 * If we aren't the file's owner we can't change its perms when moving it and smb
1239 * nfs,... don't even try.
1241 #define AFP_CHECK_ACCESS
1243 int check_access(char *path, int mode)
1245 #ifdef AFP_CHECK_ACCESS
1253 accessmode(p, &ma, curdir, NULL);
1254 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1256 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1262 /* --------------------- */
1263 int file_access(struct path *path, int mode)
1267 accessmode(path->u_name, &ma, curdir, &path->st);
1268 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1270 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1276 /* --------------------- */
1277 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1279 dir->offcnt = count;
1280 dir->ctime = st->st_ctime;
1281 dir->d_flags &= ~DIRF_CNID;
1285 /* ---------------------
1286 * is our cached also for reenumerate id?
1288 int dirreenumerate(struct dir *dir, struct stat *st)
1290 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1293 /* ------------------------------
1295 (name, dir) with curdir:name == dir, from afp_enumerate
1298 int getdirparams(const struct vol *vol,
1299 u_int16_t bitmap, struct path *s_path,
1301 char *buf, size_t *buflen )
1305 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1306 int bit = 0, isad = 0;
1312 struct stat *st = &s_path->st;
1313 char *upath = s_path->u_name;
1315 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1316 (1 << DIRPBIT_CDATE) |
1317 (1 << DIRPBIT_MDATE) |
1318 (1 << DIRPBIT_BDATE) |
1319 (1 << DIRPBIT_FINFO)))) {
1321 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1322 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1324 if (ad.ad_md->adf_flags & O_CREAT) {
1325 /* We just created it */
1326 ad_setname(&ad, s_path->m_name);
1341 while ( bitmap != 0 ) {
1342 while (( bitmap & 1 ) == 0 ) {
1350 ad_getattr(&ad, &ashort);
1351 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1352 ashort = htons(ATTRBIT_INVISIBLE);
1355 ashort |= htons(ATTRBIT_SHARED);
1356 memcpy( data, &ashort, sizeof( ashort ));
1357 data += sizeof( ashort );
1361 memcpy( data, &pdid, sizeof( pdid ));
1362 data += sizeof( pdid );
1365 case DIRPBIT_CDATE :
1366 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1367 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1368 memcpy( data, &aint, sizeof( aint ));
1369 data += sizeof( aint );
1372 case DIRPBIT_MDATE :
1373 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1374 memcpy( data, &aint, sizeof( aint ));
1375 data += sizeof( aint );
1378 case DIRPBIT_BDATE :
1379 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1380 aint = AD_DATE_START;
1381 memcpy( data, &aint, sizeof( aint ));
1382 data += sizeof( aint );
1385 case DIRPBIT_FINFO :
1387 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1388 } else { /* no appledouble */
1389 memset( data, 0, 32 );
1390 /* set default view -- this also gets done in ad_open() */
1391 ashort = htons(FINDERINFO_CLOSEDVIEW);
1392 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1394 /* dot files are by default visible */
1395 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1396 ashort = htons(FINDERINFO_INVISIBLE);
1397 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1403 case DIRPBIT_LNAME :
1404 if (dir->d_m_name) /* root of parent can have a null name */
1407 memset(data, 0, sizeof(u_int16_t));
1408 data += sizeof( u_int16_t );
1411 case DIRPBIT_SNAME :
1412 memset(data, 0, sizeof(u_int16_t));
1413 data += sizeof( u_int16_t );
1417 memcpy( data, &dir->d_did, sizeof( aint ));
1418 data += sizeof( aint );
1421 case DIRPBIT_OFFCNT :
1423 /* this needs to handle current directory access rights */
1424 if (diroffcnt(dir, st)) {
1425 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1427 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1428 setdiroffcnt(dir, st, ret);
1429 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1431 ashort = htons( ashort );
1432 memcpy( data, &ashort, sizeof( ashort ));
1433 data += sizeof( ashort );
1437 aint = htonl(st->st_uid);
1438 memcpy( data, &aint, sizeof( aint ));
1439 data += sizeof( aint );
1443 aint = htonl(st->st_gid);
1444 memcpy( data, &aint, sizeof( aint ));
1445 data += sizeof( aint );
1448 case DIRPBIT_ACCESS :
1449 accessmode( upath, &ma, dir , st);
1451 *data++ = ma.ma_user;
1452 *data++ = ma.ma_world;
1453 *data++ = ma.ma_group;
1454 *data++ = ma.ma_owner;
1457 /* Client has requested the ProDOS information block.
1458 Just pass back the same basic block for all
1459 directories. <shirsch@ibm.net> */
1460 case DIRPBIT_PDINFO :
1461 if (afp_version >= 30) { /* UTF8 name */
1462 utf8 = kTextEncodingUTF8;
1463 if (dir->d_m_name) /* root of parent can have a null name */
1466 memset(data, 0, sizeof(u_int16_t));
1467 data += sizeof( u_int16_t );
1469 memcpy(data, &aint, sizeof( aint ));
1470 data += sizeof( aint );
1472 else { /* ProDOS Info Block */
1475 ashort = htons( 0x0200 );
1476 memcpy( data, &ashort, sizeof( ashort ));
1477 data += sizeof( ashort );
1478 memset( data, 0, sizeof( ashort ));
1479 data += sizeof( ashort );
1483 case DIRPBIT_UNIXPR :
1484 aint = htonl(st->st_uid);
1485 memcpy( data, &aint, sizeof( aint ));
1486 data += sizeof( aint );
1487 aint = htonl(st->st_gid);
1488 memcpy( data, &aint, sizeof( aint ));
1489 data += sizeof( aint );
1492 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1493 memcpy( data, &aint, sizeof( aint ));
1494 data += sizeof( aint );
1496 accessmode( upath, &ma, dir , st);
1498 *data++ = ma.ma_user;
1499 *data++ = ma.ma_world;
1500 *data++ = ma.ma_group;
1501 *data++ = ma.ma_owner;
1506 ad_close_metadata( &ad );
1508 return( AFPERR_BITMAP );
1514 ashort = htons( data - buf );
1515 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1516 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1518 if ( utf_nameoff ) {
1519 ashort = htons( data - buf );
1520 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1521 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1524 ad_close_metadata( &ad );
1526 *buflen = data - buf;
1530 /* ----------------------------- */
1531 int path_error(struct path *path, int error)
1533 /* - a dir with access error
1534 * - no error it's a file
1537 if (path_isadir(path))
1539 if (path->st_valid && path->st_errno)
1541 return AFPERR_BADTYPE ;
1544 /* ----------------------------- */
1545 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1550 u_int16_t vid, bitmap;
1556 memcpy( &vid, ibuf, sizeof( vid ));
1557 ibuf += sizeof( vid );
1559 if (NULL == ( vol = getvolbyvid( vid )) ) {
1560 return( AFPERR_PARAM );
1563 if (vol->v_flags & AFPVOL_RO)
1564 return AFPERR_VLOCK;
1566 memcpy( &did, ibuf, sizeof( did ));
1567 ibuf += sizeof( int );
1569 if (NULL == ( dir = dirlookup( vol, did )) ) {
1573 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1574 bitmap = ntohs( bitmap );
1575 ibuf += sizeof( bitmap );
1577 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1578 return get_afp_errno(AFPERR_NOOBJ);
1581 if ( *path->m_name != '\0' ) {
1582 rc = path_error(path, AFPERR_NOOBJ);
1583 /* maybe we are trying to set perms back */
1584 if (rc != AFPERR_ACCESS)
1589 * If ibuf is odd, make it even.
1591 if ((u_long)ibuf & 1 ) {
1595 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1596 setvoltime(obj, vol );
1602 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1604 * assume path == '\0' eg. it's a directory in canonical form
1606 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1618 u_int16_t ashort, bshort, oshort;
1620 int change_mdate = 0;
1621 int change_parent_mdate = 0;
1623 u_int16_t bitmap = d_bitmap;
1624 u_char finder_buf[32];
1627 u_int16_t upriv_bit = 0;
1630 upath = path->u_name;
1632 while ( bitmap != 0 ) {
1633 while (( bitmap & 1 ) == 0 ) {
1641 memcpy( &ashort, buf, sizeof( ashort ));
1642 buf += sizeof( ashort );
1644 case DIRPBIT_CDATE :
1646 memcpy(&cdate, buf, sizeof(cdate));
1647 buf += sizeof( cdate );
1649 case DIRPBIT_MDATE :
1650 memcpy(&newdate, buf, sizeof(newdate));
1651 buf += sizeof( newdate );
1653 case DIRPBIT_BDATE :
1655 memcpy(&bdate, buf, sizeof(bdate));
1656 buf += sizeof( bdate );
1658 case DIRPBIT_FINFO :
1660 memcpy( finder_buf, buf, 32 );
1663 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1664 change_parent_mdate = 1;
1665 memcpy( &owner, buf, sizeof(owner));
1666 buf += sizeof( owner );
1669 change_parent_mdate = 1;
1670 memcpy( &group, buf, sizeof( group ));
1671 buf += sizeof( group );
1673 case DIRPBIT_ACCESS :
1675 change_parent_mdate = 1;
1676 ma.ma_user = *buf++;
1677 ma.ma_world = *buf++;
1678 ma.ma_group = *buf++;
1679 ma.ma_owner = *buf++;
1680 mpriv = mtoumode( &ma ) | vol->v_dperm;
1681 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1682 err = set_dir_errors(path, "setdirmode", errno);
1686 /* Ignore what the client thinks we should do to the
1687 ProDOS information block. Skip over the data and
1688 report nothing amiss. <shirsch@ibm.net> */
1689 case DIRPBIT_PDINFO :
1690 if (afp_version < 30) {
1694 err = AFPERR_BITMAP;
1698 case DIRPBIT_UNIXPR :
1699 if (vol_unix_priv(vol)) {
1700 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1701 buf += sizeof( owner );
1702 memcpy( &group, buf, sizeof( group ));
1703 buf += sizeof( group );
1706 change_parent_mdate = 1;
1707 memcpy( &upriv, buf, sizeof( upriv ));
1708 buf += sizeof( upriv );
1709 upriv = ntohl (upriv) | vol->v_dperm;
1710 if (dir_rx_set(upriv)) {
1711 /* maybe we are trying to set perms back */
1712 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1714 err = set_dir_errors(path, "setdirunixmode", errno);
1725 err = AFPERR_BITMAP;
1733 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1735 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1737 * Check to see what we're trying to set. If it's anything
1738 * but ACCESS, UID, or GID, give an error. If it's any of those
1739 * three, we don't need the ad to be open, so just continue.
1741 * note: we also don't need to worry about mdate. also, be quiet
1742 * if we're using the noadouble option.
1744 if (!vol_noadouble(vol) && (d_bitmap &
1745 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1746 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1747 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1748 return AFPERR_ACCESS;
1754 * Check to see if a create was necessary. If it was, we'll want
1755 * to set our name, etc.
1757 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1758 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1764 while ( bitmap != 0 ) {
1765 while (( bitmap & 1 ) == 0 ) {
1773 ad_getattr(&ad, &bshort);
1775 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1776 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1780 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1781 change_parent_mdate = 1;
1782 ad_setattr(&ad, bshort);
1785 case DIRPBIT_CDATE :
1787 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1790 case DIRPBIT_MDATE :
1792 case DIRPBIT_BDATE :
1794 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1797 case DIRPBIT_FINFO :
1799 /* Fixes #2802236 */
1800 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1801 *fflags &= htons(~FINDERINFO_ISHARED);
1803 if ( dir->d_did == DIRDID_ROOT ) {
1805 * Alright, we admit it, this is *really* sick!
1806 * The 4 bytes that we don't copy, when we're dealing
1807 * with the root of a volume, are the directory's
1808 * location information. This eliminates that annoying
1809 * behavior one sees when mounting above another mount
1812 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1813 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1815 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1819 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1820 if ( (dir->d_did == DIRDID_ROOT) &&
1821 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1822 err = set_dir_errors(path, "setdeskowner", errno);
1823 if (isad && err == AFPERR_PARAM) {
1824 err = AFP_OK; /* ???*/
1827 goto setdirparam_done;
1830 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1831 err = set_dir_errors(path, "setdirowner", errno);
1832 goto setdirparam_done;
1836 if (dir->d_did == DIRDID_ROOT)
1837 setdeskowner( -1, ntohl(group) );
1838 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1839 err = set_dir_errors(path, "setdirowner", errno);
1840 goto setdirparam_done;
1843 case DIRPBIT_ACCESS :
1844 if (dir->d_did == DIRDID_ROOT) {
1846 if (!dir_rx_set(mpriv)) {
1847 /* we can't remove read and search for owner on volume root */
1848 err = AFPERR_ACCESS;
1849 goto setdirparam_done;
1853 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1854 err = set_dir_errors(path, "setdirmode", errno);
1855 goto setdirparam_done;
1858 case DIRPBIT_PDINFO :
1859 if (afp_version >= 30) {
1860 err = AFPERR_BITMAP;
1861 goto setdirparam_done;
1864 case DIRPBIT_UNIXPR :
1865 if (vol_unix_priv(vol)) {
1866 if (dir->d_did == DIRDID_ROOT) {
1867 if (!dir_rx_set(upriv)) {
1868 /* we can't remove read and search for owner on volume root */
1869 err = AFPERR_ACCESS;
1870 goto setdirparam_done;
1872 setdeskowner( -1, ntohl(group) );
1873 setdeskmode( upriv );
1875 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1876 err = set_dir_errors(path, "setdirowner", errno);
1877 goto setdirparam_done;
1880 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1881 err = set_dir_errors(path, "setdirunixmode", errno);
1882 goto setdirparam_done;
1886 err = AFPERR_BITMAP;
1887 goto setdirparam_done;
1891 err = AFPERR_BITMAP;
1892 goto setdirparam_done;
1901 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1902 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1906 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1907 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1912 if (path->st_valid && !path->st_errno) {
1913 struct stat *st = &path->st;
1915 if (dir && dir->d_pdid) {
1916 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1920 ad_close_metadata( &ad);
1924 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1925 && gettimeofday(&tv, NULL) == 0) {
1926 if (!movecwd(vol, dir->d_parent)) {
1927 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1928 /* be careful with bitmap because now dir is null */
1929 bitmap = 1<<DIRPBIT_MDATE;
1930 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1931 /* should we reset curdir ?*/
1938 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1952 memcpy( &vid, ibuf, sizeof( vid ));
1953 ibuf += sizeof( vid );
1954 if (NULL == (vol = getvolbyvid( vid )) ) {
1955 return( AFPERR_PARAM );
1958 memcpy( &did, ibuf, sizeof( did ));
1959 ibuf += sizeof( did );
1963 * if it's CNID 2 our only choice to meet the specs is call sync.
1964 * For any other CNID just sync that dir. To my knowledge the
1965 * intended use of FPSyncDir is to sync the volume so all we're
1966 * ever going to see here is probably CNID 2. Anyway, we' prepared.
1969 if ( ntohl(did) == 2 ) {
1972 if (NULL == ( dir = dirlookup( vol, did )) ) {
1973 return afp_errno; /* was AFPERR_NOOBJ */
1976 if (movecwd( vol, dir ) < 0 )
1977 return ( AFPERR_NOOBJ );
1980 * Assuming only OSens that have dirfd also may require fsyncing directories
1981 * in order to flush metadata e.g. Linux.
1985 if (NULL == ( dp = opendir( "." )) ) {
1988 return( AFPERR_NOOBJ );
1990 return( AFPERR_ACCESS );
1992 return( AFPERR_PARAM );
1996 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
1999 if ( fsync ( dfd ) < 0 )
2000 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2001 dir->d_u_name, strerror(errno) );
2002 closedir(dp); /* closes dfd too */
2005 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2008 return( AFPERR_NOOBJ );
2010 return( AFPERR_ACCESS );
2012 return( AFPERR_PARAM );
2016 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2017 vol->ad_path(".", ADFLAGS_DIR) );
2019 if ( fsync(dfd) < 0 )
2020 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2021 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2028 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2034 struct path *s_path;
2042 memcpy( &vid, ibuf, sizeof( vid ));
2043 ibuf += sizeof( vid );
2044 if (NULL == ( vol = getvolbyvid( vid )) ) {
2045 return( AFPERR_PARAM );
2048 if (vol->v_flags & AFPVOL_RO)
2049 return AFPERR_VLOCK;
2051 memcpy( &did, ibuf, sizeof( did ));
2052 ibuf += sizeof( did );
2053 if (NULL == ( dir = dirlookup( vol, did )) ) {
2054 return afp_errno; /* was AFPERR_NOOBJ */
2056 /* for concurrent access we need to be sure we are not in the
2057 * folder we want to create...
2061 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2062 return get_afp_errno(AFPERR_PARAM);
2064 /* cname was able to move curdir to it! */
2065 if (*s_path->m_name == '\0')
2066 return AFPERR_EXIST;
2068 upath = s_path->u_name;
2070 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2074 LOG(log_debug, logtype_afpd, "afp_createdir: alive1");
2076 if (of_stat(s_path) < 0) {
2079 LOG(log_debug, logtype_afpd, "afp_createdir: alive2");
2081 LOG(log_debug, logtype_afpd, "afp_createdir: alive3");
2082 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2085 LOG(log_debug, logtype_afpd, "afp_createdir: alive4");
2086 if ( movecwd( vol, dir ) < 0 ) {
2087 return( AFPERR_PARAM );
2089 LOG(log_debug, logtype_afpd, "afp_createdir: alive5");
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);