]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/extattrs.c
remove include already in adouble.h
[netatalk.git] / etc / afpd / extattrs.c
index 2900be3d2b7ab91da36d846fbb28069b685ef198..6c5a395dcd658aa647b0f47a60fcc7a0b23f23c3 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  $Id: extattrs.c,v 1.5 2009-10-14 15:04:00 franklahm Exp $
+  $Id: extattrs.c,v 1.16 2009-10-29 10:04:35 didg Exp $
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
 
   This program is free software; you can redistribute it and/or modify
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <dirent.h>
 
 #include <atalk/adouble.h>
 #include <atalk/vfs.h>
 #include "fork.h"
 #include "extattrs.h"
 
-static char *ea_finderinfo = "com.apple.FinderInfo";
-static char *ea_resourcefork = "com.apple.ResourceFork";
+static const char *ea_finderinfo = "com.apple.FinderInfo";
+static const char *ea_resourcefork = "com.apple.ResourceFork";
 
 /* This should be big enough to consecutively store the names of all attributes */
 static char attrnamebuf[ATTRNAMEBUFSIZ];
 
+#ifdef DEBUG
 static void hexdump(void *m, size_t l) {
     char *p = m;
     int count = 0, len;
@@ -62,6 +59,7 @@ static void hexdump(void *m, size_t l) {
         }
     }
 }
+#endif
 
 /***************************************
  * AFP funcs
@@ -72,24 +70,28 @@ static void hexdump(void *m, size_t l) {
   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;
+    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 };
+
+    static int          buf_valid = 0;
+    static size_t       attrbuflen = 0;
 
     *rbuflen = 0;
     ibuf += 2;
 
     /* Get MaxReplySize first */
-    memcpy( &maxreply, ibuf + 14, 4);
+    memcpy( &maxreply, ibuf + 14, sizeof (maxreply));
     maxreply = ntohl( maxreply );
 
     /*
@@ -100,23 +102,23 @@ int afp_listextattr(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *r
 
         attrbuflen = 0;
 
-        memcpy( &vid, ibuf, 2);
-        ibuf += 2;
+        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));
             return AFPERR_ACCESS;
         }
 
-        memcpy( &did, ibuf, 4);
-        ibuf += 4;
+        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));
             return afp_errno;
         }
 
-        memcpy( &bitmap, ibuf, 2);
+        memcpy( &bitmap, ibuf, sizeof(bitmap));
         bitmap = ntohs( bitmap );
-        ibuf += 2;
+        ibuf += sizeof(bitmap);
 
 #ifdef HAVE_SOLARIS_EAS
         if (bitmap & kXAttrNoFollow)
@@ -145,7 +147,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?",
@@ -158,21 +164,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 */
@@ -203,14 +212,14 @@ int afp_listextattr(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *r
 
     /* Start building reply packet */
     bitmap = htons(bitmap);
-    memcpy( rbuf, &bitmap, 2);
-    rbuf += 2;
-    *rbuflen += 2;
+    memcpy( rbuf, &bitmap, sizeof(bitmap));
+    rbuf += sizeof(bitmap);
+    *rbuflen += sizeof(bitmap);
 
     tmpattr = htonl(attrbuflen);
-    memcpy( rbuf, &tmpattr, 4);
-    rbuf += 4;
-    *rbuflen += 4;
+    memcpy( rbuf, &tmpattr, sizeof(tmpattr));
+    rbuf += sizeof(tmpattr);
+    *rbuflen += sizeof(tmpattr);
 
     /* Only copy buffer if the client asked for it (2nd request, maxreply>0)
        and we didnt have an error (buf_valid) */
@@ -231,11 +240,11 @@ exit:
     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;
-    uint32_t            did, maxreply, attrnamelen;
+    uint16_t            vid, bitmap, attrnamelen;
+    uint32_t            did, maxreply;
     char                attrmname[256], attruname[256];
     struct vol          *vol;
     struct dir          *dir;
@@ -245,23 +254,23 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     *rbuflen = 0;
     ibuf += 2;
 
-    memcpy( &vid, ibuf, 2);
-    ibuf += 2;
+    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));
         return AFPERR_ACCESS;
     }
 
-    memcpy( &did, ibuf, 4);
-    ibuf += 4;
+    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));
         return afp_errno;
     }
 
-    memcpy( &bitmap, ibuf, 2);
+    memcpy( &bitmap, ibuf, sizeof(bitmap));
     bitmap = ntohs( bitmap );
-    ibuf += 2;
+    ibuf += sizeof(bitmap);
 
 #ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
@@ -272,9 +281,9 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     ibuf += 16;
 
     /* Get MaxReply */
-    memcpy(&maxreply, ibuf, 4);
+    memcpy(&maxreply, ibuf, sizeof(maxreply));
     maxreply = ntohl(maxreply);
-    ibuf += 4;
+    ibuf += sizeof(maxreply);
 
     /* get name */
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
@@ -286,9 +295,9 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
         ibuf++;
 
     /* get length of EA name */
-    memcpy(&attrnamelen, ibuf, 2);
+    memcpy(&attrnamelen, ibuf, sizeof(attrnamelen));
     attrnamelen = ntohs(attrnamelen);
-    ibuf += 2;
+    ibuf += sizeof(attrnamelen);
     if (attrnamelen > 255)
         /* dont fool with us */
         attrnamelen = 255;
@@ -309,9 +318,9 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
 
     /* write bitmap now */
     bitmap = htons(bitmap);
-    memcpy(rbuf, &bitmap, 2);
-    rbuf += 2;
-    *rbuflen += 2;
+    memcpy(rbuf, &bitmap, sizeof(bitmap));
+    rbuf += sizeof(bitmap);
+    *rbuflen += sizeof(bitmap);
 
     /*
       Switch on maxreply:
@@ -326,11 +335,11 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     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                 oflag = O_CREAT | O_WRONLY, ret;
-    uint16_t            vid, bitmap;
-    uint32_t            did, attrnamelen, attrsize;
+    uint16_t            vid, bitmap, attrnamelen;
+    uint32_t            did, attrsize;
     char                attrmname[256], attruname[256];
     struct vol          *vol;
     struct dir          *dir;
@@ -339,23 +348,23 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     *rbuflen = 0;
     ibuf += 2;
 
-    memcpy( &vid, ibuf, 2);
-    ibuf += 2;
+    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));
         return AFPERR_ACCESS;
     }
 
-    memcpy( &did, ibuf, 4);
-    ibuf += 4;
+    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));
         return afp_errno;
     }
 
-    memcpy( &bitmap, ibuf, 2);
+    memcpy( &bitmap, ibuf, sizeof(bitmap));
     bitmap = ntohs( bitmap );
-    ibuf += 2;
+    ibuf += sizeof(bitmap);
 
 #ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
@@ -380,9 +389,9 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
         ibuf++;
 
     /* get length of EA name */
-    memcpy(&attrnamelen, ibuf, 2);
+    memcpy(&attrnamelen, ibuf, sizeof(attrnamelen));
     attrnamelen = ntohs(attrnamelen);
-    ibuf += 2;
+    ibuf += sizeof(attrnamelen);
     if (attrnamelen > 255)
         return AFPERR_PARAM;
 
@@ -400,9 +409,9 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
         attruname[255] = 0;
 
     /* get EA size */
-    memcpy(&attrsize, ibuf, 4);
+    memcpy(&attrsize, ibuf, sizeof(attrsize));
     attrsize = ntohl(attrsize);
-    ibuf += 4;
+    ibuf += sizeof(attrsize);
     if (attrsize > MAX_EA_SIZE)
         /* we arbitrarily make this fatal */
         return AFPERR_PARAM;
@@ -414,11 +423,11 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     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, ret;
-    uint16_t            vid, bitmap;
-    uint32_t            did, attrnamelen;
+    uint16_t            vid, bitmap, attrnamelen;
+    uint32_t            did;
     char                attrmname[256], attruname[256];
     struct vol          *vol;
     struct dir          *dir;
@@ -427,23 +436,23 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
     *rbuflen = 0;
     ibuf += 2;
 
-    memcpy( &vid, ibuf, 2);
-    ibuf += 2;
+    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));
         return AFPERR_ACCESS;
     }
 
-    memcpy( &did, ibuf, 4);
-    ibuf += 4;
+    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));
         return afp_errno;
     }
 
-    memcpy( &bitmap, ibuf, 2);
+    memcpy( &bitmap, ibuf, sizeof(bitmap));
     bitmap = ntohs( bitmap );
-    ibuf += 2;
+    ibuf += sizeof(bitmap);
 
 #ifdef HAVE_SOLARIS_EAS
     if (bitmap & kXAttrNoFollow)
@@ -460,9 +469,9 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int
         ibuf++;
 
     /* get length of EA name */
-    memcpy(&attrnamelen, ibuf, 2);
+    memcpy(&attrnamelen, ibuf, sizeof(attrnamelen));
     attrnamelen = ntohs(attrnamelen);
-    ibuf += 2;
+    ibuf += sizeof(attrnamelen);
     if (attrnamelen > 255)
         return AFPERR_PARAM;