]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/volume.c
/tmp/log
[netatalk.git] / etc / afpd / volume.c
index 3119f90b296a3d5fa5ac23eb4cc76b8abef79f13..f5ef2d5acd6547d0bdbc94015995cca2afc02ee5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: volume.c,v 1.29 2002-08-20 05:36:08 jmarcus Exp $
+ * $Id: volume.c,v 1.46 2003-01-12 14:40:04 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -81,6 +81,7 @@ static int            lastvid = 0;
 static char            *Trash = "\02\024Network Trash Folder";
 #endif /* CNID_DB */
 static struct extmap   *extmap = NULL, *defextmap = NULL;
+static int              extmap_cnt;
 
 #define VOLOPT_ALLOW      0  /* user allow list */
 #define VOLOPT_DENY       1  /* user deny list */
@@ -104,11 +105,16 @@ m=u -> map both ways
 #define VOLOPT_VETO      10  /* list of veto filespec */
 
 #ifdef FORCE_UIDGID
+#warning UIDGID
+#include "uid.h"
+
 #define VOLOPT_FORCEUID  11  /* force uid for username x */
 #define VOLOPT_FORCEGID  12  /* force gid for group x */
-#define VOLOPT_MAX       12
+#define VOLOPT_UMASK     13
+#define VOLOPT_MAX       13
 #else /* normally, there are only 9 possible options */
-#define VOLOPT_MAX       10
+#define VOLOPT_UMASK     11
+#define VOLOPT_MAX       11
 #endif /* FORCE_UIDGID */
 
 #define VOLOPT_NUM        (VOLOPT_MAX + 1)
@@ -287,11 +293,14 @@ static __inline__ char *get_codepage_path(const char *path, const char *name)
 
 /* handle all the options. tmp can't be NULL. */
 static void volset(struct vol_option *options, char *volname, int vlen,
-                   const char *nlspath, const char *tmp)
+                   const char *nlspath, const char *tmp, AFPObj *obj,
+                  struct passwd *pwd)
 {
     char *val;
 
     val = strchr(tmp, ':');
+    LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
+
     if (optionok(tmp, "allow:", val)) {
         if (options[VOLOPT_ALLOW].c_value)
             free(options[VOLOPT_ALLOW].c_value);
@@ -366,17 +375,23 @@ static void volset(struct vol_option *options, char *volname, int vlen,
                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
             else if (strcasecmp(p, "nofileid") == 0)
                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
+            else if (strcasecmp(p, "utf8") == 0)
+                options[VOLOPT_FLAGS].i_value |= AFPVOL_UTF8;
 
             p = strtok(NULL, ",");
         }
 
 #ifdef CNID_DB
     } else if (optionok(tmp, "dbpath:", val)) {
+       char t[MAXPATHLEN + 1];
         if (options[VOLOPT_DBPATH].c_value)
             free(options[VOLOPT_DBPATH].c_value);
 
-        options[VOLOPT_DBPATH].c_value = strdup(val + 1);
+       volxlate(obj, t, MAXPATHLEN, val, pwd, NULL);
+        options[VOLOPT_DBPATH].c_value = strdup(t + 1);
 #endif /* CNID_DB */
+    } else if (optionok(tmp, "umask:", val)) {
+       options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
     } else if (optionok(tmp, "mapchars:",val)) {
         if (options[VOLOPT_MAPCHARS].c_value)
             free(options[VOLOPT_MAPCHARS].c_value);
@@ -489,6 +504,9 @@ static int creatvol(const char *path, char *name, struct vol_option *options)
             volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
 #endif /* CNID_DB */
 
+       if (options[VOLOPT_UMASK].i_value)
+           volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
+
 #ifdef FORCE_UIDGID
 
         if (options[VOLOPT_FORCEUID].c_value) {
@@ -521,7 +539,7 @@ FILE        *fp;
     int                c;
 
     p = buf;
-    while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
+    while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
         if ( c == '\n' || c == '\r' ) {
             *p++ = '\n';
             break;
@@ -581,26 +599,38 @@ char              *ext, *type, *creator;
 int                    user;
 {
     struct extmap      *em;
+    int                 cnt;
 
-    for ( em = extmap; em; em = em->em_next ) {
-        if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
+    if (extmap == NULL) {
+        if (( extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
+            LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
+            return;
+        }
+    }
+    ext++;
+    for ( em = extmap, cnt = 0; em->em_ext; em++, cnt++) {
+        if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
             break;
         }
     }
 
-    if ( em == NULL ) {
-        if (( em =
-                    (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
-            LOG(log_error, logtype_afpd, "setextmap: malloc: %s", strerror(errno) );
+    if ( em->em_ext == NULL ) {
+        if (!(extmap  = realloc( extmap, sizeof( struct extmap ) * (cnt +2))) ) {
+            LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
             return;
         }
-        em->em_next = extmap;
-        extmap = em;
+        (extmap +cnt +1)->em_ext = NULL;
+        em = extmap +cnt;
     } else if ( !user ) {
         return;
     }
+    if (em->em_ext)
+       free(em->em_ext);
 
-    strcpy( em->em_ext, ext );
+    if (!(em->em_ext = strdup(  ext))) {
+        LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
+        return;
+    }
 
     if ( *type == '\0' ) {
         memcpy(em->em_type, "????", sizeof( em->em_type ));
@@ -612,12 +642,35 @@ int                       user;
     } else {
         memcpy(em->em_creator, creator, sizeof( em->em_creator ));
     }
+}
+
+/* -------------------------- */
+static int extmap_cmp(const void *map1, const void *map2)
+{
+    const struct extmap *em1 = map1;
+    const struct extmap *em2 = map2;
+    return strdiacasecmp(em1->em_ext, em2->em_ext);
+}
+
+static void sortextmap( void)
+{
+    struct extmap      *em;
 
-    if ( strcmp( ext, "." ) == 0 ) {
-        defextmap = em;
+    extmap_cnt = 0;
+    if ((em = extmap) == NULL) {
+        return;
+    }
+    while (em->em_ext) {
+        em++;
+        extmap_cnt++;
+    }
+    if (extmap_cnt) {
+        qsort(extmap, extmap_cnt, sizeof(struct extmap), extmap_cmp);
+        defextmap = extmap;
     }
 }
 
+
 /*
  * Read a volume configuration file and add the volumes contained within to
  * the global volume list.  If p2 is non-NULL, the file that is opened is
@@ -653,7 +706,7 @@ struct passwd *pwent;
         strcat( path, p2 );
     }
 
-    if (( fp = fopen( path, "r" )) == NULL ) {
+    if (NULL == ( fp = fopen( path, "r" )) ) {
         return( -1 );
     }
 
@@ -675,7 +728,8 @@ struct passwd *pwent;
                                    path + VOLOPT_DEFAULT_LEN) < 0)
                         break;
                     volset(save_options, tmp, sizeof(tmp) - 1,
-                           obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
+                           obj->options.nlspath, path + VOLOPT_DEFAULT_LEN,
+                          obj, pwent);
                 }
             }
             break;
@@ -697,6 +751,10 @@ struct passwd *pwent;
                 strcat( tmp, "/" );
                 strcat( tmp, p );
             }
+           /* Tag a user's home directory with their umask.  Note, this will
+            * be overwritten if the user actually specifies a umask: option
+            * for a '~' volume. */
+           save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
             /* fall through */
 
         case '/' :
@@ -733,7 +791,7 @@ struct passwd *pwent;
                     break;
 
                 volset(options, volname, sizeof(volname) - 1,
-                       obj->options.nlspath, tmp);
+                       obj->options.nlspath, tmp, obj, pwent);
             }
 
             /* check allow/deny lists:
@@ -771,6 +829,7 @@ struct passwd *pwent;
         }
     }
     volfree(save_options, NULL);
+    sortextmap();
     if ( fclose( fp ) != 0 ) {
         LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
     }
@@ -894,6 +953,7 @@ int         *buflen;
         ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
         memcpy(ad_entry( &ad, ADEID_NAME ), slash,
                ad_getentrylen( &ad, ADEID_NAME ));
+       ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
         ad_flush(&ad, ADFLAGS_HF);
     }
 
@@ -929,8 +989,13 @@ int                *buflen;
              *       it's passed in that way as it's possible to mount
              *       a read-write filesystem under a read-only one. */
             if ((vol->v_flags & AFPVOL_RO) ||
-                    ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
+                    ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
                 ashort |= VOLPBIT_ATTR_RO;
+            }
+            ashort |= VOLPBIT_ATTR_CATSEARCH;
+            if (afp_version >= 30) {
+                ashort |= VOLPBIT_ATTR_UTF8;
+            }
             ashort = htons(ashort);
             memcpy(data, &ashort, sizeof( ashort ));
             data += sizeof( ashort );
@@ -1152,19 +1217,29 @@ int             ibuflen, *rbuflen;
         ret = AFPERR_ACCESS;
         goto openvol_err;
     }
-
+    /* FIXME 
+    */
+    if (afp_version >= 30) {
+        volume->max_filename = 255;
+    }
+    else {
+        volume->max_filename = MACFILELEN;
+    }
     if (( volume->v_flags & AFPVOL_OPEN  ) == 0 ) {
-        if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
+        /* FIXME unix name != mac name */
+        if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
             LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
             ret = AFPERR_MISC;
             goto openvol_err;
         }
         dir->d_did = DIRDID_ROOT;
         dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
-        strcpy( dir->d_name, volume->v_name );
         volume->v_dir = volume->v_root = dir;
         volume->v_flags |= AFPVOL_OPEN;
     }
+#ifdef FORCE_UIDGID
+    set_uidgid ( volume );
+#endif /* FORCE_UIDGID */
 
     if ( stat( volume->v_path, &st ) < 0 ) {
         ret = AFPERR_PARAM;
@@ -1188,9 +1263,9 @@ int               ibuflen, *rbuflen;
 
 #ifdef CNID_DB
     if (volume->v_dbpath)
-        volume->v_db = cnid_open (volume->v_dbpath);
+        volume->v_db = cnid_open (volume->v_dbpath, volume->v_umask);
     if (volume->v_db == NULL)
-        volume->v_db = cnid_open (volume->v_path);
+        volume->v_db = cnid_open (volume->v_path, volume->v_umask);
 #endif /* CNID_DB */
 
 #ifndef CNID_DB
@@ -1223,7 +1298,7 @@ int               ibuflen, *rbuflen;
     *rbuflen = 0;
     ibuf += 2;
     memcpy(&vid, ibuf, sizeof( vid ));
-    if (( vol = getvolbyvid( vid )) == NULL ) {
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
         return( AFPERR_PARAM );
     }
 
@@ -1239,7 +1314,6 @@ int               ibuflen, *rbuflen;
             curdir = ovol->v_dir;
         }
     }
-
     dirfree( vol->v_root );
     vol->v_dir = NULL;
 #ifdef CNID_DB
@@ -1262,28 +1336,72 @@ struct vol *getvolbyvid(const u_int16_t vid )
         return( NULL );
     }
 
+#ifdef FORCE_UIDGID
+    set_uidgid ( vol );
+#endif /* FORCE_UIDGID */
+
     return( vol );
 }
 
+/* ------------------------ */
+static int ext_cmp_key(const void *key, const void *obj)
+{
+    const char          *p = key;
+    const struct extmap *em = obj;
+    return strdiacasecmp(p, em->em_ext);
+}
 struct extmap *getextmap(const char *path)
 {
-    char       *p;
-    struct extmap      *em;
+    char         *p;
+    struct extmap *em;
 
-    if (( p = strrchr( path, '.' )) == NULL ) {
+    if (NULL == ( p = strrchr( path, '.' )) ) {
         return( defextmap );
     }
-
-    for ( em = extmap; em; em = em->em_next ) {
-        if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
-            break;
-        }
-    }
-    if ( em == NULL ) {
+    p++;
+    if (!*p || !extmap_cnt) {
         return( defextmap );
-    } else {
+    }
+    em = bsearch(p, extmap, extmap_cnt, sizeof(struct extmap), ext_cmp_key);
+    if (em) {
         return( em );
+    } else {
+        return( defextmap );
+    }
+}
+
+struct extmap *getdefextmap(void)
+{
+    return( defextmap );
+}
+
+/*
+   poll if a volume is changed by other processes.
+*/
+int  pollvoltime(obj)
+AFPObj *obj;
+{
+    struct vol      *vol;
+    struct timeval   tv;
+    struct stat      st;
+    
+    if (!(afp_version > 21 && obj->options.server_notif)) 
+         return 0;
+
+    if ( gettimeofday( &tv, 0 ) < 0 ) 
+         return 0;
+
+    for ( vol = volumes; vol; vol = vol->v_next ) {
+        if ( (vol->v_flags & AFPVOL_OPEN)  && vol->v_time + 30 < tv.tv_sec) {
+            if ( !stat( vol->v_path, &st ) && vol->v_time != st.st_mtime ) {
+                vol->v_time = st.st_mtime;
+                if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
+                    return -1;
+                return 1;
+            }
+        }
     }
+    return 0;
 }
 
 void setvoltime(obj, vol )
@@ -1333,7 +1451,7 @@ int               ibuflen, *rbuflen;
     memcpy(&bitmap, ibuf, sizeof( bitmap ));
     bitmap = ntohs( bitmap );
 
-    if (( vol = getvolbyvid( vid )) == NULL ) {
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
         *rbuflen = 0;
         return( AFPERR_PARAM );
     }
@@ -1402,7 +1520,7 @@ int               ibuflen, *rbuflen;
 }
 
 
-int wincheck(struct vol *vol, const char *path)
+int wincheck(const struct vol *vol, const char *path)
 {
     int len;