]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/extattrs.c
afpd: Solaris locking problem, bug #559
[netatalk.git] / etc / afpd / extattrs.c
index aa6077ef8f2232f2bfbbe838b7475be5470cd1ed..18f5c787ffb6b9c1ef987e78dbe1769ce1b507ed 100644 (file)
@@ -1,5 +1,4 @@
 /*
-  $Id: extattrs.c,v 1.22 2009-11-09 05:45:06 didg Exp $
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
 
   This program is free software; you can redistribute it and/or modify
@@ -28,8 +27,9 @@
 #include <atalk/afp.h>
 #include <atalk/logger.h>
 #include <atalk/ea.h>
+#include <atalk/globals.h>
+#include <atalk/netatalk_conf.h>
 
-#include "globals.h"
 #include "volume.h"
 #include "desktop.h"
 #include "directory.h"
@@ -73,7 +73,7 @@ static void hexdump(void *m, size_t l) {
 */
 int afp_listextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
-    int                 ret, oflag = 0, adflags = 0;
+    int                 ret, oflag = 0, adflags = 0, fd = -1;
     uint16_t            vid, bitmap, uint16;
     uint32_t            did, maxreply, tmpattr;
     struct vol          *vol;
@@ -81,6 +81,7 @@ int afp_listextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     struct path         *s_path;
     struct stat         *st;
     struct adouble      ad, *adp = NULL;
+    struct ofork       *opened = NULL;
     char                *uname, *FinderInfo;
     char                emptyFinderInfo[32] = { 0 };
 
@@ -108,27 +109,25 @@ int afp_listextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
         memcpy( &vid, ibuf, sizeof(vid));
         ibuf += sizeof(vid);
         if (NULL == ( vol = getvolbyvid( vid )) ) {
-            LOG(log_error, logtype_afpd, "afp_listextattr: getvolbyvid error: %s", strerror(errno));
+            LOG(log_debug, logtype_afpd, "afp_listextattr: getvolbyvid error: %s", strerror(errno));
             return AFPERR_ACCESS;
         }
 
         memcpy( &did, ibuf, sizeof(did));
         ibuf += sizeof(did);
         if (NULL == ( dir = dirlookup( vol, did )) ) {
-            LOG(log_error, logtype_afpd, "afp_listextattr: dirlookup error: %s", strerror(errno));
+            LOG(log_debug, logtype_afpd, "afp_listextattr: dirlookup error: %s", strerror(errno));
             return afp_errno;
         }
 
-#ifdef HAVE_SOLARIS_EAS
         if (bitmap & kXAttrNoFollow)
             oflag = O_NOFOLLOW;
-#endif
         /* Skip Bitmap, ReqCount, StartIndex and maxreply*/
         ibuf += 12;
 
         /* get name */
         if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-            LOG(log_error, logtype_afpd, "afp_listextattr: cname error: %s", strerror(errno));
+            LOG(log_debug, logtype_afpd, "afp_listextattr: cname error: %s", strerror(errno));
             return AFPERR_NOOBJ;
         }
 
@@ -141,19 +140,33 @@ int afp_listextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
             return( AFPERR_NOOBJ );
         }
 
-        adp = of_ad(vol, s_path, &ad);
-        uname = s_path->u_name;        /*
+        uname = s_path->u_name;
+        /*
           We have to check the FinderInfo for the file, because if they aren't all 0
           we must return the synthetic attribute "com.apple.FinderInfo".
           Note: the client will never (never seen in traces) request that attribute
           via FPGetExtAttr !
         */
 
-        if (S_ISDIR(st->st_mode))
-            adflags = ADFLAGS_DIR;
+        adp = &ad;
+        ad_init(adp, vol);
 
-        if ( ad_metadata( uname, adflags, adp) < 0 ) {
+        if (path_isadir(s_path)) {
+           LOG(log_debug, logtype_afpd, "afp_listextattr(%s): is a dir", uname);
+            adflags = ADFLAGS_DIR;
+       } else {
+           LOG(log_debug, logtype_afpd, "afp_listextattr(%s): is a file", uname);
+           opened = of_findname(vol, s_path);
+           if (opened) {
+               adp = opened->of_ad;
+               fd = ad_meta_fileno(adp);
+           }
+       }
+
+        if (ad_metadata(uname, adflags, adp) != 0 ) {
             switch (errno) {
+            case ENOENT:
+                break;
             case EACCES:
                 LOG(log_error, logtype_afpd, "afp_listextattr(%s): %s: check resource fork permission?",
                     uname, strerror(errno));
@@ -162,38 +175,27 @@ int afp_listextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
                 LOG(log_error, logtype_afpd, "afp_listextattr(%s): error getting metadata: %s", uname, strerror(errno));
                 return AFPERR_MISC;
             }
-        }
-
-        FinderInfo = ad_entry(adp, ADEID_FINDERI);
-
-#ifdef DEBUG
-        LOG(log_maxdebug, logtype_afpd, "afp_listextattr(%s): FinderInfo:", uname);
-        hexdump( FinderInfo, 32);
-#endif
-
-        if ((adflags & ADFLAGS_DIR)) {
-            /* set default view */
-            uint16 = htons(FINDERINFO_CLOSEDVIEW);
-            memcpy(emptyFinderInfo + FINDERINFO_FRVIEWOFF, &uint16, 2);
-        }
+        } else {
+            FinderInfo = ad_entry(adp, ADEID_FINDERI);
+            /* Check if FinderInfo equals default and empty FinderInfo*/
+            if (memcmp(FinderInfo, emptyFinderInfo, 32) != 0) {
+                /* FinderInfo contains some non 0 bytes -> include "com.apple.FinderInfo" */
+                strcpy(attrnamebuf, ea_finderinfo);
+                attrbuflen += strlen(ea_finderinfo) + 1;
+                LOG(log_debug7, logtype_afpd, "afp_listextattr(%s): sending com.apple.FinderInfo", uname);
+            }
 
-        /* Check if FinderInfo equals default and empty FinderInfo*/
-        if ((memcmp(FinderInfo, emptyFinderInfo, 32)) != 0) {
-            /* FinderInfo contains some non 0 bytes -> include "com.apple.FinderInfo" */
-            strcpy(attrnamebuf, ea_finderinfo);
-            attrbuflen += strlen(ea_finderinfo) + 1;
-            LOG(log_debug7, logtype_afpd, "afp_listextattr(%s): sending com.apple.FinderInfo", uname);
-        }
+            /* Now check for Ressource fork and add virtual EA "com.apple.ResourceFork" if size > 0 */
+            LOG(log_debug7, logtype_afpd, "afp_listextattr(%s): Ressourcefork size: %llu", uname, adp->ad_rlen);
 
-        /* Now check for Ressource fork and add virtual EA "com.apple.ResourceFork" if size > 0 */
-        LOG(log_debug7, logtype_afpd, "afp_listextattr(%s): Ressourcefork size: %u", uname, adp->ad_eid[ADEID_RFORK].ade_len);
-        if (adp->ad_eid[ADEID_RFORK].ade_len > 0) {
-            LOG(log_debug7, logtype_afpd, "afp_listextattr(%s): sending com.apple.RessourceFork.", uname);
-            strcpy(attrnamebuf + attrbuflen, ea_resourcefork);
-            attrbuflen += strlen(ea_resourcefork) + 1;
+            if (adp->ad_rlen > 0) {
+                LOG(log_debug7, logtype_afpd, "afp_listextattr(%s): sending com.apple.RessourceFork.", uname);
+                strcpy(attrnamebuf + attrbuflen, ea_resourcefork);
+                attrbuflen += strlen(ea_resourcefork) + 1;
+            }
         }
-
-        ret = vol->vfs->vfs_ea_list(vol, attrnamebuf, &attrbuflen, uname, oflag);
+       
+        ret = vol->vfs->vfs_ea_list(vol, attrnamebuf, &attrbuflen, uname, oflag, fd);
 
         switch (ret) {
         case AFPERR_BADTYPE:
@@ -209,7 +211,7 @@ int afp_listextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
         default:
             buf_valid = 1;
         }
-    }
+    } /* if ((maxreply == 0) || (buf_valid == 0)) */
 
     /* Start building reply packet */
     bitmap = htons(bitmap);
@@ -235,8 +237,9 @@ int afp_listextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
 exit:
     if (ret != AFP_OK)
         buf_valid = 0;
+
     if (adp)
-        ad_close_metadata( adp);
+        ad_close(adp, ADFLAGS_HF);
 
     return ret;
 }
@@ -245,24 +248,26 @@ static char *to_stringz(char *ibuf, uint16_t len)
 {
 static char attrmname[256];
 
-    if (len > 256)
+    if (len > 255)
         /* dont fool with us */
-        len = 256;
+        len = 255;
 
     /* we must copy the name as its not 0-terminated and I DONT WANT TO WRITE to ibuf */
-    strlcpy(attrmname, ibuf, len);
+    strlcpy(attrmname, ibuf, len + 1);
     return attrmname;
 }
 
 int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
-    int                 ret, oflag = 0;
+    int                 ret, oflag = 0, fd = -1;
     uint16_t            vid, bitmap, attrnamelen;
     uint32_t            did, maxreply;
     char                attruname[256];
     struct vol          *vol;
     struct dir          *dir;
     struct path         *s_path;
+    struct adouble     ad, *adp = NULL;
+    struct ofork       *opened = NULL;
 
 
     *rbuflen = 0;
@@ -271,14 +276,14 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     memcpy( &vid, ibuf, sizeof(vid));
     ibuf += sizeof(vid);
     if (NULL == ( vol = getvolbyvid( vid )) ) {
-        LOG(log_error, logtype_afpd, "afp_getextattr: getvolbyvid error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_getextattr: getvolbyvid error: %s", strerror(errno));
         return AFPERR_ACCESS;
     }
 
     memcpy( &did, ibuf, sizeof(did));
     ibuf += sizeof(did);
     if (NULL == ( dir = dirlookup( vol, did )) ) {
-        LOG(log_error, logtype_afpd, "afp_getextattr: dirlookup error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_getextattr: dirlookup error: %s", strerror(errno));
         return afp_errno;
     }
 
@@ -286,10 +291,8 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     bitmap = ntohs( bitmap );
     ibuf += sizeof(bitmap);
 
-#ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
         oflag = O_NOFOLLOW;
-#endif
 
     /* Skip Offset and ReqCount */
     ibuf += 16;
@@ -301,7 +304,7 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
 
     /* get name */
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        LOG(log_error, logtype_afpd, "afp_getextattr: cname error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_getextattr: cname error: %s", strerror(errno));
         return AFPERR_NOOBJ;
     }
 
@@ -325,22 +328,33 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     rbuf += sizeof(bitmap);
     *rbuflen += sizeof(bitmap);
 
+    if (path_isadir(s_path)) {
+       LOG(log_debug, logtype_afpd, "afp_getextattr(%s): is a dir", s_path->u_name);
+    } else {
+       LOG(log_debug, logtype_afpd, "afp_getextattr(%s): is a file", s_path->u_name);
+       opened = of_findname(vol, s_path);
+       if (opened) {
+           adp = opened->of_ad;
+           fd = ad_meta_fileno(adp);
+       }
+    }
+
     /*
       Switch on maxreply:
       if its 0 we must return the size of the requested attribute,
       if its non 0 we must return the attribute.
     */
     if (maxreply == 0)
-        ret = vol->vfs->vfs_ea_getsize(vol, rbuf, rbuflen, s_path->u_name, oflag, attruname);
+        ret = vol->vfs->vfs_ea_getsize(vol, rbuf, rbuflen, s_path->u_name, oflag, attruname, fd);
     else
-        ret = vol->vfs->vfs_ea_getcontent(vol, rbuf, rbuflen, s_path->u_name, oflag, attruname, maxreply);
+        ret = vol->vfs->vfs_ea_getcontent(vol, rbuf, rbuflen, s_path->u_name, oflag, attruname, maxreply, fd);
 
     return ret;
 }
 
 int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
 {
-    int                 oflag = O_CREAT | O_WRONLY, ret;
+    int                 oflag = 0, ret, fd = -1;
     uint16_t            vid, bitmap, attrnamelen;
     uint32_t            did, attrsize;
     char                attruname[256];
@@ -348,6 +362,8 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _
     struct vol          *vol;
     struct dir          *dir;
     struct path         *s_path;
+    struct adouble     ad, *adp = NULL;
+    struct ofork       *opened = NULL;
 
     *rbuflen = 0;
     ibuf += 2;
@@ -355,14 +371,14 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _
     memcpy( &vid, ibuf, sizeof(vid));
     ibuf += sizeof(vid);
     if (NULL == ( vol = getvolbyvid( vid )) ) {
-        LOG(log_error, logtype_afpd, "afp_setextattr: getvolbyvid error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_setextattr: getvolbyvid error: %s", strerror(errno));
         return AFPERR_ACCESS;
     }
 
     memcpy( &did, ibuf, sizeof(did));
     ibuf += sizeof(did);
     if (NULL == ( dir = dirlookup( vol, did )) ) {
-        LOG(log_error, logtype_afpd, "afp_setextattr: dirlookup error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_setextattr: dirlookup error: %s", strerror(errno));
         return afp_errno;
     }
 
@@ -370,13 +386,11 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _
     bitmap = ntohs( bitmap );
     ibuf += sizeof(bitmap);
 
-#ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
-        oflag |= AT_SYMLINK_NOFOLLOW;
-#endif
+        oflag |= O_NOFOLLOW;
 
     if (bitmap & kXAttrCreate)
-        oflag |= O_EXCL;
+        oflag |= O_CREAT;
     else if (bitmap & kXAttrReplace)
         oflag |= O_TRUNC;
 
@@ -385,10 +399,22 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _
 
     /* get name */
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        LOG(log_error, logtype_afpd, "afp_setextattr: cname error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_setextattr: cname error: %s", strerror(errno));
         return AFPERR_NOOBJ;
     }
 
+    if (path_isadir(s_path)) {
+       LOG(log_debug, logtype_afpd, "afp_setextattr(%s): is a dir", s_path->u_name);
+    } else {
+       LOG(log_debug, logtype_afpd, "afp_setextattr(%s): is a file", s_path->u_name);
+       opened = of_findname(vol, s_path);
+       if (opened) {
+           adp = opened->of_ad;
+           fd = ad_meta_fileno(adp);
+       }
+    }
+
+
     if ((unsigned long)ibuf & 1)
         ibuf++;
 
@@ -415,20 +441,22 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _
 
     LOG(log_debug, logtype_afpd, "afp_setextattr(%s): EA: %s, size: %u", s_path->u_name, to_stringz(attrmname, attrnamelen), attrsize);
 
-    ret = vol->vfs->vfs_ea_set(vol, s_path->u_name, attruname, ibuf, attrsize, oflag);
+    ret = vol->vfs->vfs_ea_set(vol, s_path->u_name, attruname, ibuf, attrsize, oflag, fd);
 
     return ret;
 }
 
 int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
 {
-    int                 oflag = O_RDONLY, ret;
+    int                 oflag = 0, ret, fd = -1;
     uint16_t            vid, bitmap, attrnamelen;
     uint32_t            did;
     char                attruname[256];
     struct vol          *vol;
     struct dir          *dir;
     struct path         *s_path;
+    struct adouble     ad, *adp = NULL;
+    struct ofork       *opened = NULL;
 
     *rbuflen = 0;
     ibuf += 2;
@@ -436,14 +464,14 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _
     memcpy( &vid, ibuf, sizeof(vid));
     ibuf += sizeof(vid);
     if (NULL == ( vol = getvolbyvid( vid )) ) {
-        LOG(log_error, logtype_afpd, "afp_remextattr: getvolbyvid error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_remextattr: getvolbyvid error: %s", strerror(errno));
         return AFPERR_ACCESS;
     }
 
     memcpy( &did, ibuf, sizeof(did));
     ibuf += sizeof(did);
     if (NULL == ( dir = dirlookup( vol, did )) ) {
-        LOG(log_error, logtype_afpd, "afp_remextattr: dirlookup error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_remextattr: dirlookup error: %s", strerror(errno));
         return afp_errno;
     }
 
@@ -451,17 +479,26 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _
     bitmap = ntohs( bitmap );
     ibuf += sizeof(bitmap);
 
-#ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
-        oflag |= AT_SYMLINK_NOFOLLOW;
-#endif
+        oflag |= O_NOFOLLOW;
 
     /* get name */
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        LOG(log_error, logtype_afpd, "afp_setextattr: cname error: %s", strerror(errno));
+        LOG(log_debug, logtype_afpd, "afp_remextattr: cname error: %s", strerror(errno));
         return AFPERR_NOOBJ;
     }
 
+    if (path_isadir(s_path)) {
+       LOG(log_debug, logtype_afpd, "afp_remextattr(%s): is a dir", s_path->u_name);
+    } else {
+       LOG(log_debug, logtype_afpd, "afp_remextattr(%s): is a file", s_path->u_name);
+       opened = of_findname(vol, s_path);
+       if (opened) {
+           adp = opened->of_ad;
+           fd = ad_meta_fileno(adp);
+       }
+    }
+
     if ((unsigned long)ibuf & 1)
         ibuf++;
 
@@ -478,7 +515,7 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _
 
     LOG(log_debug, logtype_afpd, "afp_remextattr(%s): EA: %s", s_path->u_name, to_stringz(ibuf, attrnamelen));
 
-    ret = vol->vfs->vfs_ea_remove(vol, s_path->u_name, attruname, oflag);
+    ret = vol->vfs->vfs_ea_remove(vol, s_path->u_name, attruname, oflag, fd);
 
     return ret;
 }