]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/volume.c
allow more values in cnid_open flags parameter
[netatalk.git] / etc / afpd / volume.c
index fb61d38c5d957e78c64cd38a57899deaa0c81beb..c802f8173d7083af335fa9e5667149c87bfdb413 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: volume.c,v 1.84 2009-04-17 04:24:20 didg Exp $
+ * $Id: volume.c,v 1.105 2009-11-24 11:40:11 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -12,7 +12,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <dirent.h>
 #include <pwd.h>
 #include <grp.h>
 #include <utime.h>
@@ -43,7 +42,9 @@ char *strchr (), *strrchr ();
 #include <atalk/adouble.h>
 #include <atalk/afp.h>
 #include <atalk/util.h>
+#include <atalk/volinfo.h>
 #include <atalk/logger.h>
+#include <atalk/vfs.h>
 #ifdef CNID_DB
 #include <atalk/cnid.h>
 #endif /* CNID_DB*/
@@ -55,6 +56,7 @@ char *strchr (), *strrchr ();
 #include "unix.h"
 #include "mangle.h"
 #include "fork.h"
+#include "hash.h"
 
 extern int afprun(int root, char *cmd, int *outfd);
 
@@ -124,6 +126,7 @@ m=u -> map both ways
 #define VOLOPT_DPERM     24  /* dperm default directories perms */
 #define VOLOPT_FPERM     25  /* fperm default files perms */
 #define VOLOPT_DFLTPERM  26  /* perm */
+#define VOLOPT_EA_VFS    27  /* Extended Attributes vfs indirection */
 
 #define VOLOPT_MAX       (VOLOPT_DFLTPERM +1)
 
@@ -132,10 +135,11 @@ m=u -> map both ways
 #define VOLPASSLEN  8
 #define VOLOPT_DEFAULT     ":DEFAULT:"
 #define VOLOPT_DEFAULT_LEN 9
-  struct vol_option {
-      char *c_value;
-      int i_value;
-  };
+
+struct vol_option {
+    char *c_value;
+    int i_value;
+};
 
 typedef struct _special_folder {
         const char *name;
@@ -159,6 +163,8 @@ typedef struct _volopt_name {
        const char      *name;
 } _vol_opt_name;
 
+
+/* Keep in sync with libatalk/util/volinfo.c ! */
 static const _vol_opt_name vol_opt_names[] = {
     {AFPVOL_A2VOL,      "PRODOS"},      /* prodos volume */
     {AFPVOL_CRLF,       "CRLF"},        /* cr/lf translation */
@@ -177,9 +183,9 @@ static const _vol_opt_name vol_opt_names[] = {
     {AFPVOL_NODEV,      "NODEV"},       /* always use 0 for device number in cnid calls */
     {AFPVOL_CASEINSEN,  "CASEINSENSITIVE"}, /* volume is case insensitive */
     {AFPVOL_EILSEQ,     "ILLEGALSEQ"},     /* encode illegal sequence */
-    {AFPVOL_CACHE,      "CACHEID"},     /* Use adouble v2 CNID caching, default don't use it */
-    {AFPVOL_EXT_ATTRS,  "EXT_ATTRS"},   /* Vol supports Extened Attributes */
+    {AFPVOL_CACHE,      "CACHEID"},     /* Use adouble v2 CNID caching. Default: yes */
     {AFPVOL_ACLS,       "ACLS"},        /* Vol supports ACLs */
+    {AFPVOL_TM,         "TM"},          /* Set "kSupportsTMLockSteal" is volume attributes */
     {0, NULL}
 };
 
@@ -236,7 +242,8 @@ static void volfree(struct vol_option *options,
 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
                      char *src, struct passwd *pwd, char *path, char *volname)
 {
-    char *p, *q;
+    char *p, *r;
+    const char *q;
     int len;
     char *ret;
     
@@ -282,17 +289,17 @@ static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
 
             } else if (obj->proto == AFPPROTO_DSI) {
                 DSI *dsi = obj->handle;
-
-                len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
-                              ntohs(dsi->client.sin_port));
+                len = sprintf(dest, "%s:%u",
+                              getip_string((struct sockaddr *)&dsi->client),
+                              getip_port((struct sockaddr *)&dsi->client));
                 dest += len;
                 destlen -= len;
             }
         } else if (is_var(p, "$d")) {
              q = path;
         } else if (is_var(p, "$f")) {
-            if ((q = strchr(pwd->pw_gecos, ',')))
-                *q = '\0';
+            if ((r = strchr(pwd->pw_gecos, ',')))
+                *r = '\0';
             q = pwd->pw_gecos;
         } else if (is_var(p, "$g")) {
             struct group *grp = getgrgid(pwd->pw_gid);
@@ -310,9 +317,8 @@ static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
  
             } else if (obj->proto == AFPPROTO_DSI) {
                 DSI *dsi = obj->handle;
-                q = inet_ntoa(dsi->client.sin_addr);
-            }
+                q = getip_string((struct sockaddr *)&dsi->client);
+           }
         } else if (is_var(p, "$s")) {
             if (obj->Obj)
                 q = obj->Obj;
@@ -444,8 +450,6 @@ static void volset(struct vol_option *options, struct vol_option *save,
             options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
         else if (strcasecmp(val + 1, "osx") == 0)
             options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
-        else if (strcasecmp(val + 1, "ads") == 0)
-            options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_ADS;
         else if (strcasecmp(val + 1, "sfm") == 0)
             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
 #endif
@@ -489,8 +493,6 @@ static void volset(struct vol_option *options, struct vol_option *save,
                options[VOLOPT_ROOTPREEXEC].i_value = 1;
             else if (strcasecmp(p, "upriv") == 0)
                 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
-            else if (strcasecmp(p, "extattrs") == 0)
-                options[VOLOPT_FLAGS].i_value |= AFPVOL_EXT_ATTRS;
             else if (strcasecmp(p, "acls") == 0)
                 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
             else if (strcasecmp(p, "nodev") == 0)
@@ -499,8 +501,10 @@ static void volset(struct vol_option *options, struct vol_option *save,
                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
             else if (strcasecmp(p, "illegalseq") == 0)
                 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
-            else if (strcasecmp(p, "cachecnid") == 0)
-                options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
+            else if (strcasecmp(p, "nocnidcache") == 0)
+                options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
+            else if (strcasecmp(p, "tm") == 0)
+                options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
 
             p = strtok(NULL, ",");
         }
@@ -549,6 +553,12 @@ static void volset(struct vol_option *options, struct vol_option *save,
     } else if (optionok(tmp, "denied_hosts:", val)) {
         setoption(options, save, VOLOPT_DENIED_HOSTS, val);
 
+    } else if (optionok(tmp, "ea:", val)) {
+        if (strcasecmp(val + 1, "ad") == 0) /* the default anyway */
+            options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
+        else if (strcasecmp(val + 1, "sys") == 0)
+            options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
+
     } else {
         /* ignore unknown options */
         LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
@@ -613,7 +623,7 @@ static int creatvol(AFPObj *obj, struct passwd *pwd,
         if (tmpvlen + suffixlen > obj->options.volnamelen) {
             flags = CONV_FORCE;
             tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
-            tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
+            tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
         }
         strcat(tmpname, suffix);
         tmpvlen = strlen(tmpname);
@@ -639,7 +649,7 @@ static int creatvol(AFPObj *obj, struct passwd *pwd,
         if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
             flags = CONV_FORCE;
             tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
-            tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
+            tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
         }
         strcat(tmpname, suffix);
         tmpvlen = strlen(tmpname);
@@ -702,6 +712,7 @@ static int creatvol(AFPObj *obj, struct passwd *pwd,
     /* os X start at 1 and use network order ie. 1 2 3 */
     volume->v_vid = ++lastvid;
     volume->v_vid = htons(volume->v_vid);
+    volume->v_vfs_ea = AFPVOL_EA_AD;
 
     /* handle options */
     if (options) {
@@ -710,6 +721,9 @@ static int creatvol(AFPObj *obj, struct passwd *pwd,
 
         /* shift in some flags */
         volume->v_flags = options[VOLOPT_FLAGS].i_value;
+        
+        if (options[VOLOPT_EA_VFS].i_value != AFPVOL_EA_AD)
+            volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
 
         volume->v_ad_options = 0;
         if ((volume->v_flags & AFPVOL_NODEV))
@@ -756,7 +770,32 @@ static int creatvol(AFPObj *obj, struct passwd *pwd,
        else 
            volume->v_adouble = AD_VERSION;
 
+        /* Mac to Unix conversion flags*/
+        volume->v_mtou_flags = 0;
+        if (!(volume->v_flags & AFPVOL_NOHEX))
+            volume->v_mtou_flags |= CONV_ESCAPEHEX;
+        if (!(volume->v_flags & AFPVOL_USEDOTS))
+            volume->v_mtou_flags |= CONV_ESCAPEDOTS;
+        if ((volume->v_flags & AFPVOL_EILSEQ))
+            volume->v_mtou_flags |= CONV__EILSEQ;
+
+        if ((volume->v_casefold & AFPVOL_MTOUUPPER))
+            volume->v_mtou_flags |= CONV_TOUPPER;
+        else if ((volume->v_casefold & AFPVOL_MTOULOWER))
+            volume->v_mtou_flags |= CONV_TOLOWER;
+
+        /* Unix to Mac conversion flags*/
+        volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
+        if ((volume->v_casefold & AFPVOL_UTOMUPPER))
+            volume->v_utom_flags |= CONV_TOUPPER;
+        else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
+            volume->v_utom_flags |= CONV_TOLOWER;
+
+        if ((volume->v_flags & AFPVOL_EILSEQ)) 
+            volume->v_utom_flags |= CONV__EILSEQ;
+
        initvol_vfs(volume);
+
 #ifdef FORCE_UIDGID
         if (options[VOLOPT_FORCEUID].c_value) {
             volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
@@ -795,10 +834,7 @@ static int creatvol(AFPObj *obj, struct passwd *pwd,
 }
 
 /* ---------------- */
-static char *myfgets( buf, size, fp )
-char   *buf;
-int            size;
-FILE   *fp;
+static char *myfgets( char *buf, int size, FILE *fp)
 {
     char       *p;
     int                c;
@@ -850,9 +886,7 @@ FILE        *fp;
 
 #endif
 
-static int accessvol(args, name)
-const char *args;
-const char *name;
+static int accessvol(const char *args, const char *name)
 {
     char buf[MAXPATHLEN + 1], *p;
     struct group *gr;
@@ -876,14 +910,12 @@ const char *name;
     return 0;
 }
 
-static int hostaccessvol(type, volname, args, obj)
-int type;
-char *volname;
-const char *args;
-const AFPObj *obj;
+static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
 {
+    int mask_int;
     char buf[MAXPATHLEN + 1], *p, *b;
     DSI *dsi = obj->handle;
+    struct sockaddr_storage client;
 
     if (!args)
         return -1;
@@ -892,42 +924,61 @@ const AFPObj *obj;
     if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
         return -1;
 
+    if (obj->proto != AFPPROTO_DSI)
+        return -1;
+
     while (p) {
-        if (obj->proto == AFPPROTO_DSI) {
-            struct in_addr mask, net;
-            char *net_char, *mask_char;
-            int mask_int;
-
-            net_char = strtok(p, "/");
-            mask_char = strtok(NULL,"/");
-            if (mask_char == NULL) {
+        int ret;
+        char *ipaddr, *mask_char;
+        struct addrinfo hints, *ai;
+
+        ipaddr = strtok(p, "/");
+        mask_char = strtok(NULL,"/");
+
+        /* Get address from string with getaddrinfo */
+        memset(&hints, 0, sizeof hints);
+        hints.ai_family = AF_UNSPEC;
+        hints.ai_socktype = SOCK_STREAM;
+        if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
+            LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
+            continue;
+        }        
+
+        /* netmask */
+        if (mask_char != NULL)
+            mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
+        else {
+            if (ai->ai_family == AF_INET) /* IPv4 */
                 mask_int = 32;
-            } else {
-                mask_int = atoi(mask_char);
-            }
-           
-            // convert the integer netmask to a bitmask in network order
-            mask.s_addr = htonl(-1 - ((1 << (32 - mask_int)) - 1));
-            net.s_addr = inet_addr(net_char) & mask.s_addr;
-
-            if ((dsi->client.sin_addr.s_addr & mask.s_addr) == net.s_addr) {
-                   if (type == VOLOPT_DENIED_HOSTS)
-                       LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
-                           inet_ntoa(dsi->client.sin_addr), volname);
-                   return 1;
-           }
+            else                          /* IPv6 */
+                mask_int = 128;
         }
+
+        /* Apply mask to addresses */
+        client = dsi->client;
+        apply_ip_mask((struct sockaddr *)&client, mask_int);
+        apply_ip_mask(ai->ai_addr, mask_int);
+
+        if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
+            if (type == VOLOPT_DENIED_HOSTS)
+                LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
+                    getip_string((struct sockaddr *)&client), volname);
+            freeaddrinfo(ai);
+            return 1;
+        }
+
+        /* next address */
+        freeaddrinfo(ai);
         p = strtok_r(NULL, ",", &b);
     }
+
     if (type == VOLOPT_ALLOWED_HOSTS)
-       LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
-           inet_ntoa(dsi->client.sin_addr), volname);
+        LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
+            getip_string((struct sockaddr *)&dsi->client), volname);
     return 0;
 }
 
-static void setextmap( ext, type, creator, user)
-char           *ext, *type, *creator;
-int                    user;
+static void setextmap(char *ext, char *type, char *creator, int user)
 {
     struct extmap      *em;
     int                 cnt;
@@ -1053,12 +1104,7 @@ static int volfile_changed(struct afp_volume_name *p)
  *                           [codepage:<file>] [casefold:<num>]
  *             <extension> TYPE [CREATOR]
  */
-static int readvolfile(obj, p1, p2, user, pwent)
-AFPObj      *obj;
-struct afp_volume_name         *p1;
-char        *p2;
-int            user;
-struct passwd *pwent;
+static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
 {
     FILE               *fp;
     char               path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
@@ -1093,6 +1139,10 @@ struct passwd *pwent;
     }
 
     memset(save_options, 0, sizeof(save_options));
+
+    /* Enable some default options for all volumes */
+    save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
+
     while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
         initline( strlen( buf ), buf );
         parseline( sizeof( path ) - 1, path );
@@ -1180,9 +1230,9 @@ struct passwd *pwent;
                allow -> either no list (-1), or in list (1)
                deny -> either no list (-1), or not in list (0) */
             if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
-               (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
-               hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
-               (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
+                (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
+                hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
+                (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
 
                 /* handle read-only behaviour. semantics:
                  * 1) neither the rolist nor the rwlist exist -> rw
@@ -1299,10 +1349,9 @@ struct vol *vol, *ovol, *nvol;
     }
 }
 
-static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
-struct vol     *vol;
-u_int32_t      *bfree, *btotal, *bsize;
-VolSpace    *xbfree, *xbtotal;
+static int getvolspace(struct vol *vol,
+  u_int32_t *bfree, u_int32_t *btotal,
+  VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
 {
     int                spaceflag, rc;
     u_int32_t   maxsize;
@@ -1372,12 +1421,7 @@ static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
 }
 
 /* ----------------------- */
-static int getvolparams( bitmap, vol, st, buf, buflen )
-u_int16_t      bitmap;
-struct vol     *vol;
-struct stat    *st;
-char   *buf;
-int            *buflen;
+static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
 {
     struct adouble     ad;
     int                        bit = 0, isad = 1;
@@ -1424,7 +1468,7 @@ int               *buflen;
                      (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
                      (1<<VOLPBIT_BSIZE)) ) != 0 ) {
         if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
-                          &bsize) < 0 ) {
+                          &bsize) != AFP_OK ) {
             if ( isad ) {
                 ad_close( &ad, ADFLAGS_HF );
             }
@@ -1455,16 +1499,17 @@ int             *buflen;
                 ashort |= VOLPBIT_ATTR_RO;
             }
             ashort |= VOLPBIT_ATTR_CATSEARCH;
+            ashort |= VOLPBIT_ATTR_EXT_ATTRS;
             if (afp_version >= 30) {
                 ashort |= VOLPBIT_ATTR_UTF8;
-               if (vol->v_flags & AFPVOL_UNIX_PRIV)
-                   ashort |= VOLPBIT_ATTR_UNIXPRIV;
+                if (vol->v_flags & AFPVOL_UNIX_PRIV)
+                    ashort |= VOLPBIT_ATTR_UNIXPRIV;
+                if (vol->v_flags & AFPVOL_TM)
+                    ashort |= VOLPBIT_ATTR_TM;
             }
             if (afp_version >= 32) {
-               if (vol->v_flags & AFPVOL_EXT_ATTRS)
-                   ashort |= VOLPBIT_ATTR_EXT_ATTRS;
                if (vol->v_flags & AFPVOL_ACLS)
-                   ashort |= VOLPBIT_ATTR_ACLS;
+                ashort |= VOLPBIT_ATTR_ACLS;
             }
             ashort = htons(ashort);
             memcpy(data, &ashort, sizeof( ashort ));
@@ -1579,10 +1624,11 @@ int             *buflen;
 }
 
 /* ------------------------- */
-static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
+static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
 {
     struct stat        st;
-    int                buflen, ret;
+    int                ret;
+    size_t     buflen;
 
     if ( stat( vol->v_path, &st ) < 0 ) {
         *rbuflen = 0;
@@ -1673,10 +1719,7 @@ void load_volumes(AFPObj *obj)
 }
 
 /* ------------------------------- */
-int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj;
-char   *ibuf _U_, *rbuf;
-int    ibuflen _U_, *rbuflen;
+int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     struct timeval     tv;
     struct stat                st;
@@ -1739,7 +1782,7 @@ int       ibuflen _U_, *rbuflen;
 
     *rbuflen = data - rbuf;
     data = rbuf;
-    if ( gettimeofday( &tv, 0 ) < 0 ) {
+    if ( gettimeofday( &tv, NULL ) < 0 ) {
         LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
         *rbuflen = 0;
         return AFPERR_PARAM;
@@ -1751,17 +1794,69 @@ int     ibuflen _U_, *rbuflen;
     return( AFP_OK );
 }
 
+/* ------------------------- */
+static int volume_codepage(AFPObj *obj, struct vol *volume)
+{
+    struct charset_functions *charset;
+    /* Codepages */
+
+    if (!volume->v_volcodepage)
+       volume->v_volcodepage = strdup("UTF8");
+
+    if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
+       LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
+       return -1;
+    }
+
+    if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
+       LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
+    }  
+
+    if (!volume->v_maccodepage)
+       volume->v_maccodepage = strdup(obj->options.maccodepage);
+
+    if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
+       LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
+       return -1;
+    }
+
+    if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
+       LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
+       return -1;
+    }
+    volume->v_kTextEncoding = htonl(charset->kTextEncoding);
+    return 0;
+}
+
+/* ------------------------- */
+static int volume_openDB(struct vol *volume)
+{
+    int flags = 0;
+
+    if ((volume->v_flags & AFPVOL_NODEV)) {
+        flags |= CNID_FLAG_NODEV;
+    }
+
+    if (volume->v_cnidscheme == NULL) {
+        volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
+        LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
+    }
+    if (volume->v_dbpath)
+        volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, flags);
+    else
+        volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, flags);
+    return (!volume->v_cdb)?-1:0;
+}
+
 /* ------------------------- 
  * we are the user here
 */
-int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj;
-char   *ibuf, *rbuf;
-int    ibuflen _U_, *rbuflen;
+int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     struct stat        st;
     char       *volname;
     char        *p;
+    
     struct vol *volume;
     struct dir *dir;
     int                len, ret;
@@ -1771,13 +1866,14 @@ int     ibuflen _U_, *rbuflen;
     char        *vol_uname;
     char        *vol_mname;
     char        *volname_tmp;
-
+    
     ibuf += 2;
     memcpy(&bitmap, ibuf, sizeof( bitmap ));
     bitmap = ntohs( bitmap );
     ibuf += sizeof( bitmap );
+
+    *rbuflen = 0;
     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
-        *rbuflen = 0;
         return AFPERR_BITMAP;
     }
 
@@ -1793,8 +1889,7 @@ int       ibuflen _U_, *rbuflen;
       namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
     }
 
-    if ( namelen <= 0){
-        *rbuflen = 0;
+    if ( namelen <= 0) {
         return AFPERR_PARAM;
     }
 
@@ -1811,13 +1906,11 @@ int     ibuflen _U_, *rbuflen;
     }
 
     if ( volume == NULL ) {
-        *rbuflen = 0;
         return AFPERR_PARAM;
     }
 
     /* check for a volume password */
     if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
-        *rbuflen = 0;
         return AFPERR_ACCESS;
     }
 
@@ -1829,27 +1922,10 @@ int     ibuflen _U_, *rbuflen;
         return stat_vol(bitmap, volume, rbuf, rbuflen);
     }
 
-    /* initialize volume variables
-     * FIXME file size
-    */
-    if (utf8_encoding()) {
-        volume->max_filename = 255;
-    }
-    else {
-        volume->max_filename = MACFILELEN;
-    }
-
-    volume->v_dir = volume->v_root = NULL;
-    volume->v_hash = NULL;
-
-    volume->v_flags |= AFPVOL_OPEN;
-    volume->v_cdb = NULL;  
-
     if (volume->v_root_preexec) {
        if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
             LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
-            ret = AFPERR_MISC;
-            goto openvol_err;
+            return AFPERR_MISC;
        }
     }
 
@@ -1860,21 +1936,45 @@ int     ibuflen _U_, *rbuflen;
     if (volume->v_preexec) {
        if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
             LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
-            ret = AFPERR_MISC;
-            goto openvol_err;
+            return AFPERR_MISC;
        }
     }
 
     if ( stat( volume->v_path, &st ) < 0 ) {
-        ret = AFPERR_PARAM;
-        goto openvol_err;
+        return AFPERR_PARAM;
     }
 
     if ( chdir( volume->v_path ) < 0 ) {
-        ret = AFPERR_PARAM;
-        goto openvol_err;
+        return AFPERR_PARAM;
+    }
+
+    if ( NULL == getcwd(path, MAXPATHLEN)) {
+        /* shouldn't be fatal but it will fail later */
+        LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
+        return AFPERR_MISC;
     }
 
+    if (volume_codepage(obj, volume) < 0) {
+       ret = AFPERR_MISC;
+       goto openvol_err;
+    }    
+
+    /* initialize volume variables
+     * FIXME file size
+    */
+    if (utf8_encoding()) {
+        volume->max_filename = 255;
+    }
+    else {
+        volume->max_filename = MACFILELEN;
+    }
+
+    volume->v_dir = volume->v_root = NULL;
+    volume->v_hash = NULL;
+
+    volume->v_flags |= AFPVOL_OPEN;
+    volume->v_cdb = NULL;  
+
     if (utf8_encoding()) {
         len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
     } else {
@@ -1885,13 +1985,6 @@ int      ibuflen _U_, *rbuflen;
         goto openvol_err;
     }
     
-    if ( NULL == getcwd(path, MAXPATHLEN)) {
-        /* shouldn't be fatal but it will fail later */
-        LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
-        ret = AFPERR_MISC;
-        goto openvol_err;
-    }        
-    
     if ((vol_uname = strrchr(path, '/')) == NULL)
          vol_uname = path;
     else if (*(vol_uname + 1) != '\0')
@@ -1909,55 +2002,17 @@ int     ibuflen _U_, *rbuflen;
     dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
     dir->d_m_name_ucs2 = strdup_w(volume->v_name);
     volume->v_dir = volume->v_root = dir;
+    volume->v_curdir = NULL;
     volume->v_hash = dirhash();
 
     curdir = volume->v_dir;
-    if (volume->v_cnidscheme == NULL) {
-        volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
-        LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
-               volume->v_path);
-    }
-    if (volume->v_dbpath)
-        volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
-    else
-        volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
-    if (volume->v_cdb == NULL) {
+    if (volume_openDB(volume) < 0) {
         LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s", 
            volume->v_path, volume->v_cnidscheme);
         ret = AFPERR_MISC;
         goto openvol_err;
     }
 
-    /* Codepages */
-
-    if (!volume->v_volcodepage)
-       volume->v_volcodepage = strdup("UTF8");
-
-    if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
-       LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
-       ret = AFPERR_MISC;
-       goto openvol_err;
-    }
-
-    if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
-       LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
-    }  
-
-    if (!volume->v_maccodepage)
-       volume->v_maccodepage = strdup(obj->options.maccodepage);
-
-    if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
-       LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
-       ret = AFPERR_MISC;
-       goto openvol_err;
-    }
-
-    if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
-       LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
-       ret = AFPERR_MISC;
-       goto openvol_err;
-    }  
-
     ret  = stat_vol(bitmap, volume, rbuf, rbuflen);
     if (ret == AFP_OK) {
 
@@ -2071,10 +2126,7 @@ static void deletevol(struct vol *vol)
 }
 
 /* ------------------------- */
-int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj _U_;
-char   *ibuf, *rbuf _U_;
-int    ibuflen _U_, *rbuflen;
+int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
 {
     struct vol *vol;
     u_int16_t  vid;
@@ -2124,11 +2176,11 @@ struct extmap *getextmap(const char *path)
     char         *p;
     struct extmap *em;
 
-    if (NULL == ( p = strrchr( path, '.' )) ) {
+    if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
         return( Defextmap );
     }
     p++;
-    if (!*p || !Extmap_cnt) {
+    if (!*p) {
         return( Defextmap );
     }
     em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
@@ -2147,9 +2199,18 @@ struct extmap *getdefextmap(void)
 
 /* --------------------------
    poll if a volume is changed by other processes.
+   return 
+    0 no attention msg sent
+    1 attention msg sent
+   -1 error (socket closed)
+   
+   Note: if attention return -1 no packet has been
+   sent because the buffer is full, we don't care
+   either there's no reader or there's a lot of
+   traffic and another pollvoltime will follow
 */
-int  pollvoltime(obj)
-AFPObj *obj;
+int  pollvoltime(AFPObj *obj)
+
 {
     struct vol      *vol;
     struct timeval   tv;
@@ -2158,7 +2219,7 @@ AFPObj *obj;
     if (!(afp_version > 21 && obj->options.server_notif)) 
          return 0;
 
-    if ( gettimeofday( &tv, 0 ) < 0 ) 
+    if ( gettimeofday( &tv, NULL ) < 0 ) 
          return 0;
 
     for ( vol = Volumes; vol; vol = vol->v_next ) {
@@ -2175,9 +2236,7 @@ AFPObj *obj;
 }
 
 /* ------------------------- */
-void setvoltime(obj, vol )
-AFPObj *obj;
-struct vol     *vol;
+void setvoltime(AFPObj *obj, struct vol *vol)
 {
     struct timeval     tv;
 
@@ -2187,7 +2246,7 @@ struct vol        *vol;
      * the afpd processes would come closer)
      * [RS] */
 
-    if ( gettimeofday( &tv, 0 ) < 0 ) {
+    if ( gettimeofday( &tv, NULL ) < 0 ) {
         LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
         return;
     }
@@ -2200,7 +2259,9 @@ struct vol        *vol;
     /* a little granularity */
     if (vol->v_mtime < tv.tv_sec) {
         vol->v_mtime = tv.tv_sec;
-        /* or finder doesn't update free space */
+        /* or finder doesn't update free space 
+         * XXX is it still true with newer OSX?
+        */
         if (afp_version > 21 && obj->options.server_notif) {
             obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
         }
@@ -2208,10 +2269,7 @@ struct vol       *vol;
 }
 
 /* ------------------------- */
-int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj _U_;
-char   *ibuf, *rbuf;
-int    ibuflen _U_, *rbuflen;
+int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
 {
     struct vol *vol;
     u_int16_t  vid, bitmap;
@@ -2231,10 +2289,7 @@ int      ibuflen _U_, *rbuflen;
 }
 
 /* ------------------------- */
-int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj _U_;
-char   *ibuf, *rbuf _U_;
-int    ibuflen _U_, *rbuflen;
+int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_,  size_t *rbuflen)
 {
     struct adouble ad;
     struct vol *vol;
@@ -2459,9 +2514,6 @@ static int savevoloptions (const struct vol *vol)
         case AD_VERSION2_OSX:
             strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
             break;
-        case AD_VERSION1_ADS:
-            strlcat(buf, "ADOUBLE_VER:ads\n", sizeof(buf));
-            break;
         case AD_VERSION1_SFM:
             strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
             break;
@@ -2475,8 +2527,9 @@ static int savevoloptions (const struct vol *vol)
     strlcat(buf, Cnid_srv, sizeof(buf));
     strlcat(buf, "\n", sizeof(buf));
 
-    snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
-    strlcat(buf, item, sizeof(buf));
+    strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
+    strlcat(buf, Cnid_port, sizeof(buf));
+    strlcat(buf, "\n", sizeof(buf));
 
     strcpy(item, "CNID_DBPATH:");
     if (vol->v_dbpath)