]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/extattrs.c
1) try a better workaround for deadlocks when both the server and the client are...
[netatalk.git] / etc / afpd / extattrs.c
index 9b20e2335feaeb1e147acb532d58003ad3c261b7..126bf9cf98e1a5342ae1c8d9fb90c8bbcacd6e8a 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  $Id: extattrs.c,v 1.3 2009-08-31 14:50:46 franklahm Exp $
+  $Id: extattrs.c,v 1.10 2009-10-22 13:40:11 franklahm Exp $
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
 
   This program is free software; you can redistribute it and/or modify
   GNU General Public License for more details.
 */
 
-/* According to man fsattr.5 we must define _ATFILE_SOURCE */
-#define _ATFILE_SOURCE
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
-#ifdef HAVE_EXT_ATTRS
-
-#include <errno.h>
 #include <unistd.h>
+#include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <dirent.h>
 
 #include <atalk/adouble.h>
+#include <atalk/vfs.h>
 #include <atalk/afp.h>
 #include <atalk/logger.h>
+#include <atalk/ea.h>
 
 #include "globals.h"
 #include "volume.h"
@@ -46,9 +43,9 @@ static char *ea_finderinfo = "com.apple.FinderInfo";
 static char *ea_resourcefork = "com.apple.ResourceFork";
 
 /* This should be big enough to consecutively store the names of all attributes */
-#define ATTRNAMEBUFSIZ 4096
 static char attrnamebuf[ATTRNAMEBUFSIZ];
 
+#ifdef DEBUG
 static void hexdump(void *m, size_t l) {
     char *p = m;
     int count = 0, len;
@@ -66,187 +63,10 @@ static void hexdump(void *m, size_t l) {
         }
     }
 }
-
-static int getextattr_size(char *rbuf, int *rbuflen, char *uname, int oflag, char *attruname)
-{
-    int                 ret, attrdirfd;
-    uint32_t            attrsize;
-    struct stat         st;
-
-    LOG(log_debug7, logtype_afpd, "getextattr_size(%s): attribute: \"%s\"", uname, attruname);
-
-    if ( -1 == (attrdirfd = attropen(uname, ".", O_RDONLY | oflag))) {
-        if (errno == ELOOP) {
-            /* its a symlink and client requested O_NOFOLLOW  */
-            LOG(log_debug, logtype_afpd, "getextattr_size(%s): encountered symlink with kXAttrNoFollow", uname);
-
-            memset(rbuf, 0, 4);
-            *rbuflen += 4;
-
-            return AFP_OK;
-        }
-        LOG(log_error, logtype_afpd, "getextattr_size: attropen error: %s", strerror(errno));
-        return AFPERR_MISC;
-    }
-
-    if ( -1 == (fstatat(attrdirfd, attruname, &st, 0))) {
-        LOG(log_error, logtype_afpd, "getextattr_size: fstatat error: %s", strerror(errno));
-        ret = AFPERR_MISC;
-        goto exit;
-    }
-    attrsize = (st.st_size > MAX_EA_SIZE) ? MAX_EA_SIZE : st.st_size;
-
-    /* Start building reply packet */
-
-    LOG(log_debug7, logtype_afpd, "getextattr_size(%s): attribute: \"%s\", size: %u", uname, attruname, attrsize);
-
-    /* length of attribute data */
-    attrsize = htonl(attrsize);
-    memcpy(rbuf, &attrsize, 4);
-    *rbuflen += 4;
-
-    ret = AFP_OK;
-
-exit:
-    close(attrdirfd);
-    return ret;
-}
-
-static int getextattr_content(char *rbuf, int *rbuflen, char *uname, int oflag, char *attruname, int maxreply)
-{
-    int                 ret, attrdirfd;
-    size_t              toread, okread = 0, len;
-    char                *datalength;
-    struct stat         st;
-
-    if ( -1 == (attrdirfd = attropen(uname, attruname, O_RDONLY | oflag))) {
-        if (errno == ELOOP) {
-            /* its a symlink and client requested O_NOFOLLOW  */
-            LOG(log_debug, logtype_afpd, "getextattr_content(%s): encountered symlink with kXAttrNoFollow", uname);
-
-            memset(rbuf, 0, 4);
-            *rbuflen += 4;
-
-            return AFP_OK;
-        }
-        LOG(log_error, logtype_afpd, "getextattr_content(%s): attropen error: %s", attruname, strerror(errno));
-        return AFPERR_MISC;
-    }
-
-    if ( -1 == (fstat(attrdirfd, &st))) {
-        LOG(log_error, logtype_afpd, "getextattr_content(%s): fstatat error: %s", attruname,strerror(errno));
-        ret = AFPERR_MISC;
-        goto exit;
-    }
-
-    /* Start building reply packet */
-
-    maxreply -= MAX_REPLY_EXTRA_BYTES;
-    if (maxreply > MAX_EA_SIZE)
-        maxreply = MAX_EA_SIZE;
-
-    /* But never send more than the client requested */
-    toread = (maxreply < st.st_size) ? maxreply : st.st_size;
-
-    LOG(log_debug7, logtype_afpd, "getextattr_content(%s): attribute: \"%s\", size: %u", uname, attruname, maxreply);
-
-    /* remember where we must store length of attribute data in rbuf */
-    datalength = rbuf;
-    rbuf += 4;
-    *rbuflen += 4;
-
-    while (1) {
-        len = read(attrdirfd, rbuf, toread);
-        if (len == -1) {
-            LOG(log_error, logtype_afpd, "getextattr_content(%s): read error: %s", attruname, strerror(errno));
-            ret = AFPERR_MISC;
-            goto exit;
-        }
-        okread += len;
-        rbuf += len;
-        *rbuflen += len;
-        if ((len == 0) || (okread == toread))
-            break;
-    }
-
-    okread = htonl((uint32_t)okread);
-    memcpy(datalength, &okread, 4);
-
-    ret = AFP_OK;
-
-exit:
-    close(attrdirfd);
-    return ret;
-}
-
-int list_extattr(AFPObj *obj, char *attrnamebuf, int *buflen, char *uname, int oflag)
-{
-    int ret, attrbuflen = *buflen, len, attrdirfd = 0;
-    struct dirent *dp;
-    DIR *dirp = NULL;
-
-    /* Now list file attribute dir */
-    if ( -1 == (attrdirfd = attropen( uname, ".", O_RDONLY | oflag))) {
-        if (errno == ELOOP) {
-            /* its a symlink and client requested O_NOFOLLOW */
-            ret = AFPERR_BADTYPE;
-            goto exit;
-        }
-        LOG(log_error, logtype_afpd, "list_extattr(%s): error opening atttribute dir: %s", uname, strerror(errno));
-        ret = AFPERR_MISC;
-        goto exit;
-    }
-
-    if (NULL == (dirp = fdopendir(attrdirfd))) {
-        LOG(log_error, logtype_afpd, "list_extattr(%s): error opening atttribute dir: %s", uname, strerror(errno));
-        ret = AFPERR_MISC;
-        goto exit;
-    }
-
-    while ((dp = readdir(dirp)))  {
-        /* check if its "." or ".." */
-        if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0) ||
-            (strcmp(dp->d_name, "SUNWattr_ro") == 0) || (strcmp(dp->d_name, "SUNWattr_rw") == 0))
-            continue;
-
-        len = strlen(dp->d_name);
-
-        if ( 0 >= ( len = convert_string(obj->options.unixcharset, CH_UTF8_MAC, dp->d_name, len, attrnamebuf + attrbuflen, 255)) ) {
-            ret = AFPERR_MISC;
-            goto exit;
-        }
-        if (len == 255)
-            /* convert_string didn't 0-terminate */
-            attrnamebuf[attrbuflen + 255] = 0;
-
-        LOG(log_debug7, logtype_afpd, "list_extattr(%s): attribute: %s", uname, dp->d_name);
-
-        attrbuflen += len + 1;
-        if (attrbuflen > (ATTRNAMEBUFSIZ - 256)) {
-            /* Next EA name could overflow, so bail out with error.
-               FIXME: evantually malloc/memcpy/realloc whatever.
-               Is it worth it ? */
-            LOG(log_warning, logtype_afpd, "list_extattr(%s): running out of buffer for EA names", uname);
-            ret = AFPERR_MISC;
-            goto exit;
-        }
-    }
-
-    ret = AFP_OK;
-
-exit:
-    if (dirp)
-        closedir(dirp);
-
-    if (attrdirfd > 0)
-        close(attrdirfd);
-
-    *buflen = attrbuflen;
-    return ret;
-}
+#endif
 
 /***************************************
- * Interface
+ * AFP funcs
  ****************************************/
 
 /*
@@ -254,22 +74,22 @@ exit:
   EA names, secondly it wants these names. In order to avoid scanning EAs twice
   we cache them in a static buffer.
 */
-int afp_listextattr(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *rbuflen)
+int afp_listextattr(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
-    int                 count, ret, oflag = 0;
-    uint16_t            vid, bitmap;
-    uint32_t            did, maxreply;
+    int                 ret, oflag = 0, adflags = 0;
+    uint16_t            vid, bitmap, uint16;
+    uint32_t            did, maxreply, tmpattr;
     struct vol          *vol;
     struct dir          *dir;
     struct path         *s_path;
+    struct stat         st;
     struct adouble      ad, *adp = NULL;
     struct ofork        *of;
     char                *uname, *FinderInfo;
-    static int          buf_valid = 0, attrbuflen = 0;
+    char                emptyFinderInfo[32] = { 0 };
 
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "afp_listextattr: BEGIN");
-#endif
+    static int          buf_valid = 0;
+    static size_t       attrbuflen = 0;
 
     *rbuflen = 0;
     ibuf += 2;
@@ -304,9 +124,10 @@ int afp_listextattr(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *r
         bitmap = ntohs( bitmap );
         ibuf += 2;
 
+#ifdef HAVE_SOLARIS_EAS
         if (bitmap & kXAttrNoFollow)
             oflag = O_NOFOLLOW;
-
+#endif
         /* Skip ReqCount, StartIndex and maxreply*/
         ibuf += 10;
 
@@ -330,7 +151,11 @@ int afp_listextattr(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *r
             adp = &ad;
         }
 
-        if ( ad_metadata( uname, 0, adp) < 0 ) {
+        stat(uname, &st);
+        if (S_ISDIR(st.st_mode))
+            adflags = ADFLAGS_DIR;
+
+        if ( ad_metadata( uname, adflags, adp) < 0 ) {
             switch (errno) {
             case EACCES:
                 LOG(log_error, logtype_afpd, "afp_listextattr(%s): %s: check resource fork permission?",
@@ -343,21 +168,24 @@ int afp_listextattr(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *r
         }
 
         FinderInfo = ad_entry(adp, ADEID_FINDERI);
+
 #ifdef DEBUG
-        LOG(log_debug9, logtype_afpd, "afp_listextattr(%s): FinderInfo:", uname);
+        LOG(log_maxdebug, logtype_afpd, "afp_listextattr(%s): FinderInfo:", uname);
         hexdump( FinderInfo, 32);
 #endif
 
-        /* Now scan FinderInfo if its all 0 */
-        count = 32;
-        while (count--) {
-            if (*FinderInfo++) {
-                /* 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);
-                break;
-            }
+        if ((adflags & ADFLAGS_DIR)) {
+            /* set default view */
+            uint16 = htons(FINDERINFO_CLOSEDVIEW);
+            memcpy(emptyFinderInfo + FINDERINFO_FRVIEWOFF, &uint16, 2);
+        }
+
+        /* 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 */
@@ -368,7 +196,8 @@ int afp_listextattr(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *r
             attrbuflen += strlen(ea_resourcefork) + 1;
         }
 
-        ret = list_extattr(obj, attrnamebuf, &attrbuflen, uname, oflag);
+        ret = vol->vfs->vfs_ea_list(vol, attrnamebuf, &attrbuflen, uname, oflag);
+
         switch (ret) {
         case AFPERR_BADTYPE:
             /* its a symlink and client requested O_NOFOLLOW */
@@ -391,8 +220,8 @@ int afp_listextattr(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *r
     rbuf += 2;
     *rbuflen += 2;
 
-    attrbuflen = htonl(attrbuflen);
-    memcpy( rbuf, &attrbuflen, 4);
+    tmpattr = htonl(attrbuflen);
+    memcpy( rbuf, &tmpattr, 4);
     rbuf += 4;
     *rbuflen += 4;
 
@@ -412,11 +241,10 @@ exit:
     if (adp)
         ad_close_metadata( adp);
 
-    LOG(log_debug9, logtype_afpd, "afp_listextattr: END");
     return ret;
 }
 
-int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int *rbuflen)
+int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     int                 ret, oflag = 0;
     uint16_t            vid, bitmap;
@@ -427,10 +255,6 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     struct path         *s_path;
 
 
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "afp_getextattr: BEGIN");
-#endif
-
     *rbuflen = 0;
     ibuf += 2;
 
@@ -451,8 +275,11 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     memcpy( &bitmap, ibuf, 2);
     bitmap = ntohs( bitmap );
     ibuf += 2;
+
+#ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
-        oflag = AT_SYMLINK_NOFOLLOW;
+        oflag = O_NOFOLLOW;
+#endif
 
     /* Skip Offset and ReqCount */
     ibuf += 16;
@@ -505,20 +332,16 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
       if its non 0 we must return the attribute.
     */
     if (maxreply == 0)
-        ret = getextattr_size(rbuf, rbuflen, s_path->u_name, oflag, attruname);
+        ret = vol->vfs->vfs_ea_getsize(vol, rbuf, rbuflen, s_path->u_name, oflag, attruname);
     else
-        ret = getextattr_content(rbuf, rbuflen, s_path->u_name, oflag, attruname, maxreply);
-
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "afp_getextattr: END");
-#endif
+        ret = vol->vfs->vfs_ea_getcontent(vol, rbuf, rbuflen, s_path->u_name, oflag, attruname, maxreply);
 
     return ret;
 }
 
-int afp_setextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int *rbuflen)
+int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
-    int                 len, oflag = O_CREAT | O_WRONLY, attrdirfd;
+    int                 oflag = O_CREAT | O_WRONLY, ret;
     uint16_t            vid, bitmap;
     uint32_t            did, attrnamelen, attrsize;
     char                attrmname[256], attruname[256];
@@ -526,10 +349,6 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     struct dir          *dir;
     struct path         *s_path;
 
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "afp_setextattr: BEGIN");
-#endif
-
     *rbuflen = 0;
     ibuf += 2;
 
@@ -550,8 +369,12 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     memcpy( &bitmap, ibuf, 2);
     bitmap = ntohs( bitmap );
     ibuf += 2;
+
+#ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
         oflag |= AT_SYMLINK_NOFOLLOW;
+#endif
+
     if (bitmap & kXAttrCreate)
         oflag |= O_EXCL;
     else if (bitmap & kXAttrReplace)
@@ -599,38 +422,14 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
 
     LOG(log_debug, logtype_afpd, "afp_setextattr(%s): EA: %s, size: %u", s_path->u_name, attrmname, attrsize);
 
-    if ( -1 == (attrdirfd = attropen(s_path->u_name, attruname, oflag, 0666))) {
-        if (errno == ELOOP) {
-            /* its a symlink and client requested O_NOFOLLOW  */
-            LOG(log_debug, logtype_afpd, "afp_setextattr(%s): encountered symlink with kXAttrNoFollow", s_path->u_name);
-            return AFP_OK;
-        }
-        LOG(log_error, logtype_afpd, "afp_setextattr(%s): attropen error: %s", s_path->u_name, strerror(errno));
-        return AFPERR_MISC;
-    }
-
-    while (1) {
-        len = write(attrdirfd, ibuf, attrsize);
-        if (len == -1) {
-            LOG(log_error, logtype_afpd, "afp_setextattr(%s): read error: %s", attruname, strerror(errno));
-            return AFPERR_MISC;
-        }
-        attrsize -= len;
-        ibuf += len;
-        if (attrsize == 0)
-            break;
-    }
-
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "afp_setextattr: END");
-#endif
+    ret = vol->vfs->vfs_ea_set(vol, s_path->u_name, attruname, ibuf, attrsize, oflag);
 
-    return AFP_OK;
+    return ret;
 }
 
-int afp_remextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int *rbuflen)
+int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
-    int                 oflag = O_RDONLY, attrdirfd;
+    int                 oflag = O_RDONLY, ret;
     uint16_t            vid, bitmap;
     uint32_t            did, attrnamelen;
     char                attrmname[256], attruname[256];
@@ -638,10 +437,6 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     struct dir          *dir;
     struct path         *s_path;
 
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "afp_remextattr: BEGIN");
-#endif
-
     *rbuflen = 0;
     ibuf += 2;
 
@@ -662,8 +457,11 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     memcpy( &bitmap, ibuf, 2);
     bitmap = ntohs( bitmap );
     ibuf += 2;
+
+#ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
         oflag |= AT_SYMLINK_NOFOLLOW;
+#endif
 
     /* get name */
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
@@ -696,38 +494,8 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
 
     LOG(log_debug, logtype_afpd, "afp_remextattr(%s): EA: %s", s_path->u_name, attrmname);
 
-    if ( -1 == (attrdirfd = attropen(s_path->u_name, ".", oflag))) {
-        switch (errno) {
-        case ELOOP:
-            /* its a symlink and client requested O_NOFOLLOW  */
-            LOG(log_debug, logtype_afpd, "afp_remextattr(%s): encountered symlink with kXAttrNoFollow", s_path->u_name);
-            return AFP_OK;
-        case EACCES:
-            LOG(log_debug, logtype_afpd, "afp_remextattr(%s): unlinkat error: %s", s_path->u_name, strerror(errno));
-            return AFPERR_ACCESS;
-        default:
-            LOG(log_error, logtype_afpd, "afp_remextattr(%s): attropen error: %s", s_path->u_name, strerror(errno));
-            return AFPERR_MISC;
-        }
-    }
+    ret = vol->vfs->vfs_ea_remove(vol, s_path->u_name, attruname, oflag);
 
-    if ( -1 == (unlinkat(attrdirfd, attruname, 0)) ) {
-        if (errno == EACCES) {
-            LOG(log_debug, logtype_afpd, "afp_remextattr(%s): unlinkat error: %s", s_path->u_name, strerror(errno));
-            return AFPERR_ACCESS;
-        }
-        LOG(log_error, logtype_afpd, "afp_remextattr(%s): unlinkat error: %s", s_path->u_name, strerror(errno));
-        return AFPERR_MISC;
-    }
-
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "afp_remextattr: END");
-#endif
-
-    return AFP_OK;
+    return ret;
 }
 
-
-
-#endif /* HAVE_EXT_ATTRS */
-