]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/volume.c
MFH: Set the volume creation time from the first stat call done in openvol -before...
[netatalk.git] / etc / afpd / volume.c
index 20b03f473faf29f4d47ba0fba02324df3093430e..68fb8351b3cd744458e63d49525d396073e1d1e8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: volume.c,v 1.30 2002-08-20 19:40:44 srittau Exp $
+ * $Id: volume.c,v 1.36.2.5 2003-09-18 19:24:54 bfernhomberg 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,12 +293,19 @@ 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;
 
+    LOG(log_debug, logtype_afpd, "Parsing volset %s", tmp);
+
     val = strchr(tmp, ':');
-    if (optionok(tmp, "allow:", val)) {
+    if (!val) {
+        /* we'll assume it's a volume name. */
+        strncpy(volname, tmp, vlen);
+
+    } else if (optionok(tmp, "allow:", val)) {
         if (options[VOLOPT_ALLOW].c_value)
             free(options[VOLOPT_ALLOW].c_value);
         options[VOLOPT_ALLOW].c_value = strdup(val + 1);
@@ -372,11 +385,15 @@ static void volset(struct vol_option *options, char *volname, int vlen,
 
 #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);
@@ -401,13 +418,10 @@ static void volset(struct vol_option *options, char *volname, int vlen,
 
 #endif /* FORCE_UIDGID */
 
-    } else if (val) {
+    } else {
         /* ignore unknown options */
         LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
 
-    } else {
-        /* we'll assume it's a volume name. */
-        strncpy(volname, tmp, vlen);
     }
 }
 
@@ -489,6 +503,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 +538,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 +598,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 +641,35 @@ int                       user;
     } else {
         memcpy(em->em_creator, creator, sizeof( em->em_creator ));
     }
+}
 
-    if ( strcmp( ext, "." ) == 0 ) {
-        defextmap = em;
+/* -------------------------- */
+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;
+
+    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 +705,7 @@ struct passwd *pwent;
         strcat( path, p2 );
     }
 
-    if (( fp = fopen( path, "r" )) == NULL ) {
+    if (NULL == ( fp = fopen( path, "r" )) ) {
         return( -1 );
     }
 
@@ -675,7 +727,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 +750,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 +790,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 +828,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 +952,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);
     }
 
@@ -931,6 +990,7 @@ int         *buflen;
             if ((vol->v_flags & AFPVOL_RO) ||
                     ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
                 ashort |= VOLPBIT_ATTR_RO;
+                ashort |= VOLPBIT_ATTR_CATSEARCH;
             ashort = htons(ashort);
             memcpy(data, &ashort, sizeof( ashort ));
             data += sizeof( ashort );
@@ -1165,6 +1225,9 @@ int               ibuflen, *rbuflen;
         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 +1251,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 +1286,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 +1302,6 @@ int               ibuflen, *rbuflen;
             curdir = ovol->v_dir;
         }
     }
-
     dirfree( vol->v_root );
     vol->v_dir = NULL;
 #ifdef CNID_DB
@@ -1262,30 +1324,73 @@ 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;
+                obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
+                return 1;
+            }
+        }
+    }
+    return 0;
+}
+
 void setvoltime(obj, vol )
 AFPObj *obj;
 struct vol     *vol;
@@ -1333,7 +1438,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 );
     }