]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/directory.c
Initial checkin of ACLs patch
[netatalk.git] / etc / afpd / directory.c
index 0431c188eff24936332144b0a2967e2c4882e146..642eb9ebf35db9de62a6c578a102245c8958187e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: directory.c,v 1.78 2005-04-30 21:33:41 didg Exp $
+ * $Id: directory.c,v 1.91 2009-02-02 11:55:00 franklahm Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -44,6 +44,7 @@ char *strchr (), *strrchr ();
 #include <atalk/util.h>
 #include <atalk/cnid.h>
 #include <atalk/logger.h>
+#include <atalk/uuid.h>
 
 #include "directory.h"
 #include "desktop.h"
@@ -55,16 +56,20 @@ char *strchr (), *strrchr ();
 #include "unix.h"
 #include "mangle.h"
 
+#ifdef HAVE_NFSv4_ACLS
+extern void addir_inherit_acl(const struct vol *vol);
+#endif 
+
 struct dir     *curdir;
 int             afp_errno;
 
 #define SENTINEL (&sentinel)
 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
                                  NULL, NULL, NULL, NULL, NULL, 0, 0, 
-                                 0, 0, NULL, NULL, NULL};
+                                 0, 0, NULL, NULL};
 static struct dir rootpar  = { SENTINEL, SENTINEL, NULL, 0,
                                  NULL, NULL, NULL, NULL, NULL, 0, 0, 
-                                 0, 0, NULL, NULL, NULL};
+                                 0, 0, NULL, NULL};
 
 /* (from IM: Toolbox Essentials)
  * dirFinderInfo (DInfo) fields:
@@ -152,11 +157,7 @@ dirsearch_byname( const struct vol *vol, struct dir *cdir, char *name)
 {
 struct dir *dir = NULL;
 
-    if (cdir->d_did == DIRDID_ROOT_PARENT) {
-        if ( !strcmp(vol->v_dir->d_u_name, name)) {
-            dir = vol->v_dir;
-        }
-    } else if ( cdir->d_child) {
+    if ((cdir->d_did != DIRDID_ROOT_PARENT) && (cdir->d_child)) {
         struct dir key;
         hnode_t *hn;
         
@@ -443,9 +444,7 @@ static void dir_hash_del(const struct vol *vol, struct dir *dir)
 /* remove the node from the tree. this is just like insertion, but
  * different. actually, it has to worry about a bunch of things that
  * insertion doesn't care about. */
-static void dir_remove( vol, dir )
-struct vol     *vol;
-struct dir     *dir;
+static void dir_remove( const struct vol *vol _U_, struct dir  *dir)
 {
 #ifdef REMOVE_NODES
     struct ofork *of, *last;
@@ -461,7 +460,6 @@ struct dir  *dir;
     dirfreename(dir);
     dir->d_m_name = NULL;
     dir->d_u_name = NULL;
-    dir->d_m_name_ucs2 = NULL;
 #else /* ! REMOVE_NODES */
 
     /* go searching for a node with at most one child */
@@ -537,14 +535,11 @@ struct dir        *dir;
         /* set the node's d_name */
         node->d_m_name = save.d_m_name;
         node->d_u_name = save.d_u_name;
-        node->d_m_name_ucs2 = save.d_m_name_ucs2;
     }
 
     if (node->d_color == DIRTREE_COLOR_BLACK)
         dir_rmrecolor(vol, leaf);
 
-    if (node->d_m_name_ucs2)
-        free(node->d_u_name_ucs2);
     if (node->d_u_name != node->d_m_name) {
         free(node->d_u_name);
     }
@@ -605,86 +600,6 @@ struct dir *dir;
     return pdir;
 }
 
-#define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
-
-int
-caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
-{
-    DIR               *dp;
-    struct dirent     *de;
-    int               ret;
-    static u_int32_t  did = 0;
-    static char              cname[MAXPATHLEN];
-    static char              lname[MAXPATHLEN];
-    ucs2_t           u2_path[MAXPATHLEN];
-    ucs2_t           u2_dename[MAXPATHLEN];
-    char             *tmp, *savepath;
-
-    if (veto_file(ENUMVETO, path->u_name))
-       return -1;
-
-    savepath = path->u_name;
-
-    /* very simple cache */
-    if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
-        path->u_name = cname;
-        path->d_dir = NULL;
-        if (of_stat( path ) == 0 ) {
-            return 0;
-        }
-        /* something changed, we cannot stat ... */
-        did = 0;
-    }
-
-    if (NULL == ( dp = opendir( "." )) ) {
-        LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
-        return -1;
-    }
-
-
-    /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
-    if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, strlen(path->u_name), u2_path, sizeof(u2_path)) ) 
-        LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
-
-    /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
-    ret = -1;
-    for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
-        if (NULL == check_dirent(vol, de->d_name))
-            continue;
-
-        if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, strlen(de->d_name), u2_dename, sizeof(u2_dename)) )
-            continue;
-
-        if (strcasecmp_w( u2_path, u2_dename) == 0) {
-            tmp = path->u_name;
-            strlcpy(cname, de->d_name, sizeof(cname));
-            path->u_name = cname;
-            path->d_dir = NULL;
-            if (of_stat( path ) == 0 ) {
-                LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
-                strlcpy(lname, tmp, sizeof(lname));
-                did = dir->d_did;
-                ret = 0;
-                break;
-            }
-            else 
-                path->u_name = tmp;
-        }
-
-    }
-    closedir(dp);
-
-    if (ret) {
-        /* invalidate cache */
-        memset(cname, 0, sizeof(cname));
-        did = 0;
-        path->u_name = savepath;
-    }
-    /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
-    return ret;
-}
-
-
 /*
  * attempt to extend the current dir. tree to include path
  * as a side-effect, movecwd to that point and return the new dir
@@ -702,10 +617,7 @@ struct path *path;
         return NULL;
     }
     if (of_stat( path ) != 0 ) {
-        if (!(vol->v_flags & AFPVOL_CASEINSEN))
-            return NULL;
-       else if(caseenumerate(vol, path, dir) != 0)
-            return(NULL);
+        return NULL;
     }
 
     if (!S_ISDIR(path->st.st_mode)) {
@@ -1020,10 +932,6 @@ struct path     *path;
         LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
         return NULL;
     }
-    if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, path->m_name, strlen(path->m_name), &cdir->d_m_name_ucs2)) {
-       LOG(log_error, logtype_afpd, "Couldn't set UCS2 name for %s", name);
-       cdir->d_m_name_ucs2 = NULL;
-    }
 
     cdir->d_did = id;
 
@@ -1046,7 +954,6 @@ struct path     *path;
         dirfreename(edir);
         edir->d_m_name = cdir->d_m_name;
         edir->d_u_name = cdir->d_u_name;
-        edir->d_m_name_ucs2 = cdir->d_m_name_ucs2;
         free(cdir);
         cdir = edir;
         LOG(log_error, logtype_afpd, "adddir: insert %s", edir->d_m_name);
@@ -1073,8 +980,6 @@ void dirfreename(struct dir *dir)
     if (dir->d_u_name != dir->d_m_name) {
         free(dir->d_u_name);
     }
-    if (dir->d_m_name_ucs2)
-        free(dir->d_m_name_ucs2); 
     free(dir->d_m_name);
 }
 
@@ -1122,7 +1027,6 @@ struct dir *dirnew(const char *m_name, const char *u_name)
         return NULL;
     }
 
-    dir->d_m_name_ucs2 = NULL;
     dir->d_left = dir->d_right = SENTINEL;
     dir->d_next = dir->d_prev = dir;
     return dir;
@@ -1140,7 +1044,7 @@ const struct dir *k = key;
        0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
     };
 
-    const unsigned char *str = k->d_u_name;
+    const unsigned char *str = (unsigned char *)k->d_u_name;
     hash_val_t acc = 0;
 
     while (*str) {
@@ -1256,7 +1160,7 @@ const struct vol  *vol;
 struct dir     *dir;
 char   **cpath;
 {
-    struct dir            *cdir, *scdir=NULL;
+    struct dir            *cdir;
     static char                   path[ MAXPATHLEN + 1];
     static struct path ret;
 
@@ -1388,40 +1292,21 @@ char    **cpath;
             }
         }
         if ( !extend ) {
-            ucs2_t *tmpname;
-            cdir = dir->d_child;
-            scdir = NULL;
-           if ( cdir && (vol->v_flags & AFPVOL_CASEINSEN) &&
-                    (size_t)-1 != convert_string_allocate(((ret.m_type == 3)?CH_UTF8_MAC:vol->v_maccharset), 
-                                                          CH_UCS2, path, strlen(path), (char **)&tmpname) )
-            {
-                while (cdir) {
-                    if (!cdir->d_m_name_ucs2) {
-                        LOG(log_error, logtype_afpd, "cname: no UCS2 name for %s (did %u)!!!", cdir->d_m_name, ntohl(cdir->d_did) );
-                        /* this shouldn't happen !!!! */
-                        goto noucsfallback;
-                    }
-
-                    if ( strcmp_w( cdir->d_m_name_ucs2, tmpname ) == 0 ) {
-                         break;
-                    }
-                    if ( strcasecmp_w( cdir->d_m_name_ucs2, tmpname ) == 0 ) {
-                         scdir = cdir;
-                    }
-                    cdir = (cdir == dir->d_child->d_prev) ? NULL :cdir->d_next;
+            if (dir->d_did == DIRDID_ROOT_PARENT) {
+                    /* root parent has only one child and d_m_name is *NOT* utm (d_u_name)
+                     * d_m_name is the Mac volume name
+                     * d_u_name is the volume unix directory name
+                     *
+                    */
+                cdir = NULL;
+                if (!strcmp(vol->v_dir->d_m_name, ret.m_name)) {
+                       cdir = vol->v_dir;
                 }
-                free(tmpname);
             }
             else {
-noucsfallback:
                 cdir = dirsearch_byname(vol, dir, ret.u_name);
             }
 
-            if (cdir == NULL && scdir != NULL) {
-                cdir = scdir;
-                /* LOG(log_debug, logtype_afpd, "cname: using casediff for %s, (%s = %s)", fullpathname(cdir->d_u_name), cdir->d_m_name, path ); */
-            }
-
             if ( cdir == NULL ) {
                 ++extend;
                 /* if dir == curdir it always succeed,
@@ -1592,6 +1477,12 @@ int dirreenumerate(struct dir *dir, struct stat *st)
     return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
 }
 
+/* --------------------- */
+static int invisible_dots(const struct vol *vol, const char *name)
+{ 
+  return vol_inv_dots(vol) && *name  == '.' && strcmp(name, ".") && strcmp(name, "..");
+}
+
 /* ------------------------------ 
    (".", curdir)
    (name, dir) with curdir:name == dir, from afp_enumerate
@@ -1645,8 +1536,7 @@ int getdirparams(const struct vol *vol,
         case DIRPBIT_ATTR :
             if ( isad ) {
                 ad_getattr(&ad, &ashort);
-            } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".") 
-                        && strcmp(dir->d_u_name, "..")) {
+            } else if (invisible_dots(vol, dir->d_u_name)) {
                 ashort = htons(ATTRBIT_INVISIBLE);
             } else
                 ashort = 0;
@@ -1689,12 +1579,10 @@ int getdirparams(const struct vol *vol,
                 ashort = htons(FINDERINFO_CLOSEDVIEW);
                 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
 
-                /* dot files are by default invisible */
-                if (*dir->d_u_name  == '.' && strcmp(dir->d_u_name , ".") &&
-                        strcmp(dir->d_u_name , "..")) {
+                /* dot files are by default visible */
+                if (invisible_dots(vol, dir->d_u_name)) {
                     ashort = htons(FINDERINFO_INVISIBLE);
-                    memcpy(data + FINDERINFO_FRFLAGOFF,
-                           &ashort, sizeof(ashort));
+                    memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
                 }
             }
             data += 32;
@@ -1803,7 +1691,7 @@ int getdirparams(const struct vol *vol,
             
         default :
             if ( isad ) {
-                ad_close( &ad, ADFLAGS_HF );
+                ad_close_metadata( &ad );
             }
             return( AFPERR_BITMAP );
         }
@@ -1821,7 +1709,7 @@ int getdirparams(const struct vol *vol,
         data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
     }
     if ( isad ) {
-        ad_close( &ad, ADFLAGS_HF );
+        ad_close_metadata( &ad );
     }
     *buflen = data - buf;
     return( AFP_OK );
@@ -1941,7 +1829,7 @@ int setdirparams(const struct vol *vol,
 
     char                *upath;
     struct dir          *dir;
-    int                        bit, aint, isad = 1;
+    int                        bit, isad = 1;
     int                 cdate, bdate;
     int                 owner, group;
     u_int16_t          ashort, bshort;
@@ -1952,7 +1840,7 @@ int setdirparams(const struct vol *vol,
     u_int16_t           bitmap = d_bitmap;
     u_char              finder_buf[32];
     u_int32_t          upriv;
-    mode_t              mpriv;          /* uninitialized, OK 310105 */
+    mode_t              mpriv = 0;        
     u_int16_t           upriv_bit = 0;
 
     bit = 0;
@@ -2006,7 +1894,7 @@ int setdirparams(const struct vol *vol,
             ma.ma_world = *buf++;
             ma.ma_group = *buf++;
             ma.ma_owner = *buf++;
-            mpriv = mtoumode( &ma );
+            mpriv = mtoumode( &ma ) | vol->v_dperm;
             if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
                 err = set_dir_errors(path, "setdirmode", errno);
                 bitmap = 0;
@@ -2026,20 +1914,21 @@ int setdirparams(const struct vol *vol,
             break;
        case DIRPBIT_UNIXPR :
            if (vol_unix_priv(vol)) {
-               /* Skip UID and GID for now, there seems to be no way to set them from an OSX client anyway */
-                buf += sizeof( aint );
-                buf += sizeof( aint );
+                memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
+                buf += sizeof( owner );
+                memcpy( &group, buf, sizeof( group ));
+                buf += sizeof( group );
 
                 change_mdate = 1;
                 change_parent_mdate = 1;
                 memcpy( &upriv, buf, sizeof( upriv ));
                 buf += sizeof( upriv );
-                upriv = ntohl (upriv);
+                upriv = ntohl (upriv) | vol->v_dperm;
                 if (dir_rx_set(upriv)) {
                     /* maybe we are trying to set perms back */
                     if ( setdirunixmode(vol, upath, upriv) < 0 ) {
                         bitmap = 0;
-                        err = set_dir_errors(path, "setdirmode", errno);
+                        err = set_dir_errors(path, "setdirunixmode", errno);
                     }
                 }
                 else {
@@ -2060,8 +1949,7 @@ int setdirparams(const struct vol *vol,
     }
     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
 
-    if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
-                 O_RDWR|O_CREAT, 0666, &ad) < 0) {
+    if (ad_open_metadata( upath, vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad) < 0) {
         /*
          * Check to see what we're trying to set.  If it's anything
          * but ACCESS, UID, or GID, give an error.  If it's any of those
@@ -2157,27 +2045,14 @@ int setdirparams(const struct vol *vol,
                 goto setdirparam_done;
             }
             break;
-
         case DIRPBIT_GID :
             if (dir->d_did == DIRDID_ROOT)
                 setdeskowner( -1, ntohl(group) ); 
-
-#if 0       /* don't error if we can't set the desktop owner. */
-                err = set_dir_errors(path, "setdeskowner", errno);
-                if (isad && err == AFPERR_PARAM) {
-                    err = AFP_OK; /* ???*/
-                }
-                else {
-                    goto setdirparam_done;
-                }
-#endif /* 0 */
-
             if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
                 err = set_dir_errors(path, "setdirowner", errno);
                 goto setdirparam_done;
             }
             break;
-
         case DIRPBIT_ACCESS :
             if (dir->d_did == DIRDID_ROOT) {
                 setdeskmode(mpriv);
@@ -2202,21 +2077,29 @@ int setdirparams(const struct vol *vol,
        case DIRPBIT_UNIXPR :
            if (vol_unix_priv(vol)) {
                 if (dir->d_did == DIRDID_ROOT) {
-                    setdeskmode( upriv );
                     if (!dir_rx_set(upriv)) {
                         /* we can't remove read and search for owner on volume root */
                         err = AFPERR_ACCESS;
                         goto setdirparam_done;
                     }
+                    setdeskowner( -1, ntohl(group) ); 
+                    setdeskmode( upriv );
+                }
+                if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
+                    err = set_dir_errors(path, "setdirowner", errno);
+                    goto setdirparam_done;
                 }
 
                 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
-                    err = set_dir_errors(path, "setdirmode", errno);
+                    err = set_dir_errors(path, "setdirunixmode", errno);
                     goto setdirparam_done;
                 }
-                break;
             }
-            /* fall through */
+            else {
+                err = AFPERR_BITMAP;
+                goto setdirparam_done;
+            }
+            break;
         default :
             err = AFPERR_BITMAP;
             goto setdirparam_done;
@@ -2246,8 +2129,8 @@ setdirparam_done:
                 ad_setid(&ad, st->st_dev, st->st_ino,  dir->d_did, dir->d_parent->d_did, vol->v_stamp);
             }
         }
-        ad_flush( &ad, ADFLAGS_HF );
-        ad_close( &ad, ADFLAGS_HF );
+        ad_flush( &ad);
+        ad_close_metadata( &ad);
     }
 
     if (change_parent_mdate && dir->d_did != DIRDID_ROOT
@@ -2264,6 +2147,54 @@ setdirparam_done:
     return err;
 }
 
+
+int afp_syncdir(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj  *obj _U_;
+char    *ibuf, *rbuf _U_;
+int     ibuflen _U_, *rbuflen;
+{
+    DIR                  *dp;
+    int                  dfd;
+    struct vol           *vol;
+    struct dir           *dir;
+    u_int32_t            did;
+    u_int16_t            vid;
+
+    *rbuflen = 0;
+    ibuf += 2;
+
+    memcpy( &vid, ibuf, sizeof( vid ));
+    ibuf += sizeof( vid );
+    if (NULL == (vol = getvolbyvid( vid )) ) {
+        return( AFPERR_PARAM );
+    }
+
+    memcpy( &did, ibuf, sizeof( did ));
+    ibuf += sizeof( did );
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
+        return afp_errno; /* was AFPERR_NOOBJ */
+    }
+
+    if (NULL == ( dp = opendir( "." )) ) {
+        switch( errno ) {
+        case ENOENT :
+            return( AFPERR_NOOBJ );
+        case EACCES :
+            return( AFPERR_ACCESS );
+        default :
+            return( AFPERR_PARAM );
+        }
+    }
+
+    dfd = dirfd( dp );
+    if ( fsync ( dfd ) < 0 ) {
+        LOG(log_error, logtype_afpd, "syncdir(%s): ddir(%d) %s", dir->d_u_name, dfd, strerror(errno) );
+    }
+    closedir(dp);
+    
+    return ( AFP_OK );
+}
+
 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
 AFPObj  *obj;
 char   *ibuf, *rbuf;
@@ -2329,8 +2260,7 @@ int       ibuflen _U_, *rbuflen;
     }
 
     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
-    if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
-                 O_RDWR|O_CREAT, 0666, &ad ) < 0)  {
+    if (ad_open_metadata( ".", vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad ) < 0)  {
         if (vol_noadouble(vol))
             goto createdir_done;
         return( AFPERR_ACCESS );
@@ -2338,10 +2268,15 @@ int     ibuflen _U_, *rbuflen;
     ad_setname(&ad, s_path->m_name);
     ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
 
-    ad_flush( &ad, ADFLAGS_HF );
-    ad_close( &ad, ADFLAGS_HF );
+    ad_flush( &ad);
+    ad_close_metadata( &ad);
 
 createdir_done:
+#ifdef HAVE_NFSv4_ACLS
+    /* FIXME: are we really inside the created dir? */
+    addir_inherit_acl(vol);
+#endif
+
     memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
     *rbuflen = sizeof( u_int32_t );
     setvoltime(obj, vol );
@@ -2400,10 +2335,10 @@ struct dir      *dir, *newparent;
     
     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
 
-    if (!ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
+    if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
         ad_setname(&ad, newname);
-        ad_flush( &ad, ADFLAGS_HF );
-        ad_close( &ad, ADFLAGS_HF );
+        ad_flush( &ad);
+        ad_close_metadata( &ad);
     }
 
     dir_hash_del(vol, dir);
@@ -2431,13 +2366,6 @@ struct dir       *dir, *newparent;
         strcpy( dir->d_u_name, dst );
     }
 
-    if (dir->d_m_name_ucs2)
-       free(dir->d_m_name_ucs2);
-
-    dir->d_m_name_ucs2 = NULL;
-    if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, dir->d_m_name, strlen(dir->d_m_name), (char**)&dir->d_m_name_ucs2))
-        dir->d_m_name_ucs2 = NULL;
-
     if (( parent = dir->d_parent ) == NULL ) {
         return( AFP_OK );
     }
@@ -2538,49 +2466,93 @@ int     ibuflen _U_, *rbuflen;
     u_int32_t           id;
     int                        len, sfunc;
     int         utf8 = 0;
+    uuidtype_t          type;
+
+    LOG(log_debug, logtype_afpd, "afp_mapid: BEGIN");
     
     ibuf++;
     sfunc = (unsigned char) *ibuf++;
-    memcpy( &id, ibuf, sizeof( id ));
-
-    id = ntohl(id);
     *rbuflen = 0;
 
-    if (sfunc == 3 || sfunc == 4) {
+
+    if (sfunc >= 3 && sfunc <= 6) {
         if (afp_version < 30) {
             return( AFPERR_PARAM );
         }
         utf8 = 1;
     }
-    if ( id != 0 ) {
+
         switch ( sfunc ) {
         case 1 :
         case 3 :/* unicode */
+       memcpy( &id, ibuf, sizeof( id ));
+       id = ntohl(id);
+       if ( id != 0 ) {
             if (( pw = getpwuid( id )) == NULL ) {
                 return( AFPERR_NOITEM );
             }
            len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
                                             pw->pw_name, strlen(pw->pw_name), &name);
+       } else {
+           len = 0;
+           name = NULL;
+       }
             break;
-
         case 2 :
         case 4 : /* unicode */
+       memcpy( &id, ibuf, sizeof( id ));
+       id = ntohl(id);
+       if ( id != 0 ) {
             if (NULL == ( gr = (struct group *)getgrgid( id ))) {
                 return( AFPERR_NOITEM );
             }
            len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
                                             gr->gr_name, strlen(gr->gr_name), &name);
+       } else {
+           len = 0;
+           name = NULL;
+       }
             break;
-
+#ifdef HAVE_NFSv4_ACLS
+    case 5 : /* username -> UUID  */
+    case 6 : /* groupname -> UUID */
+       if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
+           return AFPERR_PARAM;
+       LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
+       len = getnamefromuuid( ibuf, &name, &type);
+       if (len != 0)           /* its a error code, not len */
+           return AFPERR_NOITEM;
+       if (type == UUID_USER) {
+           if (( pw = getpwnam( name )) == NULL )
+               return( AFPERR_NOITEM );
+           LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
+           id = htonl(UUID_USER);
+           memcpy( rbuf, &id, sizeof( id ));
+           id = htonl( pw->pw_uid);
+           rbuf += sizeof( id );
+           memcpy( rbuf, &id, sizeof( id ));
+           rbuf += sizeof( id );
+           *rbuflen = 2 * sizeof( id );
+       } else {                /* type == UUID_GROUP */
+            if (( gr = getgrnam( name )) == NULL )
+                return( AFPERR_NOITEM );
+           LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
+           id = htonl(UUID_GROUP);
+           memcpy( rbuf, &id, sizeof( id ));
+           rbuf += sizeof( id );
+           id = htonl( gr->gr_gid);
+           memcpy( rbuf, &id, sizeof( id ));
+           rbuf += sizeof( id );
+           *rbuflen = 2 * sizeof( id );
+       }
+       break;
+#endif
         default :
             return( AFPERR_PARAM );
         }
+
         len = strlen( name );
 
-    } else {
-        len = 0;
-        name = NULL;
-    }
     if (utf8) {
         u_int16_t tp = htons(len);
         memcpy(rbuf, &tp, sizeof(tp));
@@ -2610,10 +2582,14 @@ int     ibuflen _U_, *rbuflen;
     int             len, sfunc;
     u_int32_t       id;
     u_int16_t       ulen;
+    char            *uuidstring;
+
+    LOG(log_debug, logtype_afpd, "afp_mapname: BEGIN");
 
     ibuf++;
     sfunc = (unsigned char) *ibuf++;
     *rbuflen = 0;
+    LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
     switch ( sfunc ) {
     case 1 : 
     case 2 : /* unicode */
@@ -2623,18 +2599,31 @@ int     ibuflen _U_, *rbuflen;
         memcpy(&ulen, ibuf, sizeof(ulen));
         len = ntohs(ulen);
         ibuf += 2;
+       LOG(log_debug, logtype_afpd, "afp_mapname: alive");
         break;
     case 3 :
     case 4 :
         len = (unsigned char) *ibuf++;
         break;
+#ifdef HAVE_NFSv4_ACLS
+    case 5 : /* username -> UUID  */
+    case 6 : /* groupname -> UUID */
+        if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
+            return AFPERR_PARAM;
+        memcpy(&ulen, ibuf, sizeof(ulen));
+        len = ntohs(ulen);
+        ibuf += 2;
+        break;
+#endif
     default :
         return( AFPERR_PARAM );
     }
 
     ibuf[ len ] = '\0';
 
-    if ( len != 0 ) {
+    if ( len == 0 )
+       return AFPERR_PARAM;
+    else {
         switch ( sfunc ) {
         case 1 : /* unicode */
         case 3 :
@@ -2642,22 +2631,39 @@ int     ibuflen _U_, *rbuflen;
                 return( AFPERR_NOITEM );
             }
             id = pw->pw_uid;
+           id = htonl(id);
+           memcpy( rbuf, &id, sizeof( id ));
+           *rbuflen = sizeof( id );
             break;
 
         case 2 : /* unicode */
         case 4 :
+           LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
             if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
                 return( AFPERR_NOITEM );
             }
             id = gr->gr_gid;
-            break;
-        }
-    } else {
-        id = 0;
-    }
+           LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
     id = htonl(id);
     memcpy( rbuf, &id, sizeof( id ));
     *rbuflen = sizeof( id );
+           break;
+#ifdef HAVE_NFSv4_ACLS
+       case 5 :                /* username -> UUID */
+           LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);    
+           if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
+               return AFPERR_NOITEM;
+           *rbuflen = UUID_BINSIZE;
+           break;
+       case 6 :                /* groupname -> UUID */
+           LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);    
+           if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
+               return AFPERR_NOITEM;
+           *rbuflen = UUID_BINSIZE;
+           break;
+#endif
+        }
+    }
     return( AFP_OK );
 }