]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/extattrs.c
convert_string can deal with not 0 terminated strings thus move the copy outside...
[netatalk.git] / etc / afpd / extattrs.c
index af93228ddcbec983be0a32d1751293dba0abcb4c..ba452231b81608da47960fbd38cb6aec04ab7f4e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  $Id: extattrs.c,v 1.13 2009-10-28 01:31:57 didg Exp $
+  $Id: extattrs.c,v 1.20 2009-10-29 12:48:34 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 <atalk/adouble.h>
+#include <atalk/util.h>
 #include <atalk/vfs.h>
 #include <atalk/afp.h>
 #include <atalk/logger.h>
@@ -72,7 +71,7 @@ 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, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
+int afp_listextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     int                 ret, oflag = 0, adflags = 0;
     uint16_t            vid, bitmap, uint16;
@@ -80,7 +79,7 @@ int afp_listextattr(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, siz
     struct vol          *vol;
     struct dir          *dir;
     struct path         *s_path;
-    struct stat         st;
+    struct stat         *st;
     struct adouble      ad, *adp = NULL;
     struct ofork        *of;
     char                *uname, *FinderInfo;
@@ -93,7 +92,7 @@ int afp_listextattr(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, siz
     ibuf += 2;
 
     /* Get MaxReplySize first */
-    memcpy( &maxreply, ibuf + 14, 4);
+    memcpy( &maxreply, ibuf + 14, sizeof (maxreply));
     maxreply = ntohl( maxreply );
 
     /*
@@ -104,23 +103,23 @@ int afp_listextattr(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, siz
 
         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)
@@ -134,9 +133,17 @@ int afp_listextattr(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, siz
             LOG(log_error, logtype_afpd, "afp_listextattr: cname error: %s", strerror(errno));
             return AFPERR_NOOBJ;
         }
-        uname = s_path->u_name;
 
-        /*
+        st   = &s_path->st;
+        if (!s_path->st_valid) {
+            /* it's a dir in our cache, we didn't stat it, do it now */
+            of_statdir(vol, s_path);
+        }
+        if ( s_path->st_errno != 0 ) {
+            return( AFPERR_NOOBJ );
+        }
+
+        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
@@ -149,8 +156,7 @@ int afp_listextattr(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, siz
             adp = &ad;
         }
 
-        stat(uname, &st);
-        if (S_ISDIR(st.st_mode))
+        if (S_ISDIR(st->st_mode))
             adflags = ADFLAGS_DIR;
 
         if ( ad_metadata( uname, adflags, adp) < 0 ) {
@@ -214,14 +220,14 @@ int afp_listextattr(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, siz
 
     /* 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) */
@@ -242,12 +248,25 @@ exit:
     return ret;
 }
 
+static char *to_stringz(char *ibuf, uint16_t len)
+{
+static char attrmname[256];
+
+    if (len > 256)
+        /* dont fool with us */
+        len = 256;
+
+    /* we must copy the name as its not 0-terminated and I DONT WANT TO WRITE to ibuf */
+    strlcpy(attrmname, ibuf, len);
+    return attrmname;
+}
+
 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;
-    char                attrmname[256], attruname[256];
+    uint16_t            vid, bitmap, attrnamelen;
+    uint32_t            did, maxreply;
+    char                attruname[256];
     struct vol          *vol;
     struct dir          *dir;
     struct path         *s_path;
@@ -256,23 +275,23 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     *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)
@@ -283,9 +302,9 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     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 )) ) {
@@ -297,32 +316,21 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
         ibuf++;
 
     /* get length of EA name */
-    memcpy(&attrnamelen, ibuf, 2);
+    memcpy(&attrnamelen, ibuf, sizeof(attrnamelen));
     attrnamelen = ntohs(attrnamelen);
-    ibuf += 2;
-    if (attrnamelen > 255)
-        /* dont fool with us */
-        attrnamelen = 255;
+    ibuf += sizeof(attrnamelen);
 
-    /* we must copy the name as its not 0-terminated and I DONT WANT TO WRITE to ibuf */
-    strncpy(attrmname, ibuf, attrnamelen);
-    attrmname[attrnamelen] = 0;
-
-    LOG(log_debug, logtype_afpd, "afp_getextattr(%s): EA: %s", s_path->u_name, attrmname);
+    LOG(log_debug, logtype_afpd, "afp_getextattr(%s): EA: %s", s_path->u_name, to_stringz(ibuf, attrnamelen));
 
     /* Convert EA name in utf8 to unix charset */
-    if ( 0 >= ( attrnamelen = convert_string(CH_UTF8_MAC, obj->options.unixcharset,attrmname, attrnamelen, attruname, 255)) )
+    if ( 0 >= convert_string(CH_UTF8_MAC, obj->options.unixcharset, ibuf, attrnamelen, attruname, 256) )
         return AFPERR_MISC;
 
-    if (attrnamelen == 255)
-        /* convert_string didn't 0-terminate */
-        attruname[255] = 0;
-
     /* 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:
@@ -337,12 +345,13 @@ int afp_getextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     return ret;
 }
 
-int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
+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;
-    uint16_t            vid, bitmap;
-    uint32_t            did, attrnamelen, attrsize;
-    char                attrmname[256], attruname[256];
+    uint16_t            vid, bitmap, attrnamelen;
+    uint32_t            did, attrsize;
+    char                attruname[256];
+    char               *attrmname;
     struct vol          *vol;
     struct dir          *dir;
     struct path         *s_path;
@@ -350,23 +359,23 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     *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)
@@ -391,46 +400,39 @@ int afp_setextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
         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;
 
-    /* we must copy the name as its not 0-terminated and we cant write to ibuf */
-    strncpy(attrmname, ibuf, attrnamelen);
-    attrmname[attrnamelen] = 0;
-    ibuf += attrnamelen;
-
+    attrmname = ibuf;
     /* Convert EA name in utf8 to unix charset */
-    if ( 0 >= ( attrnamelen = convert_string(CH_UTF8_MAC, obj->options.unixcharset,attrmname, attrnamelen, attruname, 255)) )
+    if ( 0 >= convert_string(CH_UTF8_MAC, obj->options.unixcharset, attrmname, attrnamelen, attruname, 256))
         return AFPERR_MISC;
 
-    if (attrnamelen == 255)
-        /* convert_string didn't 0-terminate */
-        attruname[255] = 0;
-
+    ibuf += attrnamelen;
     /* 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;
 
-    LOG(log_debug, logtype_afpd, "afp_setextattr(%s): EA: %s, size: %u", s_path->u_name, attrmname, attrsize);
+    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);
 
     return ret;
 }
 
-int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
+int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
 {
     int                 oflag = O_RDONLY, ret;
-    uint16_t            vid, bitmap;
-    uint32_t            did, attrnamelen;
-    char                attrmname[256], attruname[256];
+    uint16_t            vid, bitmap, attrnamelen;
+    uint32_t            did;
+    char                attruname[256];
     struct vol          *vol;
     struct dir          *dir;
     struct path         *s_path;
@@ -438,23 +440,23 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
     *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)
@@ -471,26 +473,17 @@ int afp_remextattr(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf,
         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;
 
-    /* we must copy the name as its not 0-terminated and we cant write to ibuf */
-    strncpy(attrmname, ibuf, attrnamelen);
-    attrmname[attrnamelen] = 0;
-    ibuf += attrnamelen;
-
     /* Convert EA name in utf8 to unix charset */
-    if ( 0 >= ( attrnamelen = convert_string(CH_UTF8_MAC, obj->options.unixcharset,attrmname, attrnamelen, attruname, 255)) )
+    if ( 0 >= (convert_string(CH_UTF8_MAC, obj->options.unixcharset,ibuf, attrnamelen, attruname, 256)) )
         return AFPERR_MISC;
 
-    if (attrnamelen == 255)
-        /* convert_string didn't 0-terminate */
-        attruname[255] = 0;
-
-    LOG(log_debug, logtype_afpd, "afp_remextattr(%s): EA: %s", s_path->u_name, attrmname);
+    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);