]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_open.c
Resource fork offset in adouble header can be after the end of the header, cf.
[netatalk.git] / libatalk / adouble / ad_open.c
index 09741757216ce8d364c0e7a016bce11f407629ed..19627794ebf18610ee50861f3ffd586710ec67b1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: ad_open.c,v 1.30.6.8 2004-02-20 19:57:14 didg Exp $
+ * $Id: ad_open.c,v 1.30.6.18.2.1 2005-01-25 14:32:00 didg Exp $
  *
  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
  * Copyright (c) 1990,1991 Regents of The University of Michigan.
@@ -41,6 +41,7 @@
 #include <string.h>
 
 #include "ad_private.h"
+#include <stdlib.h>
 
 #ifndef MAX
 #define MAX(a, b)  ((a) < (b) ? (b) : (a))
 
 #define ADEDOFF_RFORK_V2     (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
 
+#define ADEID_NUM_OSX        2
+#define ADEDOFF_FINDERI_OSX  (AD_HEADER_LEN + ADEID_NUM_OSX*AD_ENTRY_LEN)
+#define ADEDOFF_RFORK_OSX    (ADEDOFF_FINDERI_OSX + ADEDLEN_FINDERI)
+
 /* we keep local copies of a bunch of stuff so that we can initialize things 
  * correctly. */
 
@@ -191,6 +196,16 @@ static const struct entry entry_order2[ADEID_NUM_V2 +1] = {
 
   {0, 0, 0}
 };
+
+/* OS X adouble finder info and resource fork only
+*/
+static const struct entry entry_order_osx[ADEID_NUM_OSX +1] = {
+  {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
+  {ADEID_RFORK, ADEDOFF_RFORK_OSX, ADEDLEN_INIT},
+
+  {0, 0, 0}
+};
+
 #endif /* AD_VERSION == AD_VERSION2 */
 
 #if AD_VERSION == AD_VERSION2
@@ -206,9 +221,15 @@ static int ad_update(struct adouble *ad, const char *path)
   int fd;
 
   /* check to see if we should convert this header. */
-  if (!path || (ad->ad_version != AD_VERSION2))
+  if (!path || (ad->ad_flags != AD_VERSION2))
     return 0;
   
+  if (!(ad->ad_hf.adf_flags & ( O_RDWR | O_WRONLY))) {
+      /* we were unable to open the file read write the last time
+      */
+      return 0;
+  }
+
   if (ad->ad_eid[ADEID_RFORK].ade_off)  
     shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
 
@@ -236,6 +257,20 @@ static int ad_update(struct adouble *ad, const char *path)
     LOG(log_debug, logtype_default, "ad_update: file '%s' too big for update.", path);
     goto bail_truncate;
   }
+
+  off = ad->ad_eid[ADEID_RFORK].ade_off;
+  if (off > st.st_size) {
+      LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork offset. (off: %u)", off); 
+      errno = EIO;
+      goto bail_truncate;
+  }
+
+  if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
+      LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len); 
+      errno = EIO;
+      goto bail_truncate;
+  }
+  
   /* last place for failure. */
   if ((void *) (buf = (char *)
                 mmap(NULL, st.st_size + shiftdata,
@@ -244,7 +279,6 @@ static int ad_update(struct adouble *ad, const char *path)
     goto bail_truncate;
   }
 
-  off = ad->ad_eid[ADEID_RFORK].ade_off;
 
   /* move the RFORK. this assumes that the RFORK is at the end */
   if (off) {
@@ -315,9 +349,14 @@ static int ad_v1tov2(struct adouble *ad, const char *path)
     return 0;
 
   /* we want version1 anyway */
-  if (ad->ad_flags == AD_VERSION1)
+  if (ad->ad_flags != AD_VERSION2)
       return 0;
 
+  if (!(ad->ad_hf.adf_flags & ( O_RDWR | O_WRONLY))) {
+      /* we were unable to open the file read write the last time
+      */
+      return 0;
+  }
 
   if (!ad->ad_flags) {
       /* we don't really know what we want */
@@ -355,6 +394,20 @@ static int ad_v1tov2(struct adouble *ad, const char *path)
       LOG(log_debug, logtype_default, "ad_v1tov2: file too big."); 
       goto bail_truncate;
   }
+
+  off = ad->ad_eid[ADEID_RFORK].ade_off;
+
+  if (off > st.st_size) {
+      LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork offset. (off: %u)", off); 
+      errno = EIO;
+      goto bail_truncate;
+  }
+
+  if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
+      LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len); 
+      errno = EIO;
+      goto bail_truncate;
+  }
   
   /* last place for failure. */
   if ((void *) (buf = (char *) 
@@ -364,8 +417,6 @@ static int ad_v1tov2(struct adouble *ad, const char *path)
     goto bail_truncate;
   }
   
-  off = ad->ad_eid[ADEID_RFORK].ade_off;
-
   /* move the RFORK. this assumes that the RFORK is at the end */
   if (off) {
       memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
@@ -445,6 +496,10 @@ bail_err:
 #ifdef ATACC
 mode_t ad_hf_mode (mode_t mode)
 {
+    /* we always need RW mode for file owner */
+#if 0
+    mode |= S_IRUSR;
+#endif    
     /* fnctl lock need write access */
     if ((mode & S_IRUSR))
         mode |= S_IWUSR;
@@ -486,12 +541,13 @@ static void parse_entries(struct adouble *ad, char *buf,
        len = ntohl( len );
        buf += sizeof( len );
 
-       if ( 0 < eid && eid < ADEID_MAX ) {
+       if (eid && eid < ADEID_MAX && ( (off < sizeof(ad->ad_data) && 
+                off +len <= sizeof(ad->ad_data)) || eid == ADEID_RFORK)) {
            ad->ad_eid[ eid ].ade_off = off;
            ad->ad_eid[ eid ].ade_len = len;
        } else if (!warning) {
            warning = 1;
-           LOG(log_debug, logtype_default, "ad_refresh: nentries %hd  eid %d\n",
+           LOG(log_debug, logtype_default, "ad_refresh: nentries %hd  eid %d",
                    nentries, eid );
        }
     }
@@ -585,8 +641,9 @@ static int ad_header_read(struct adouble *ad, struct stat *hst)
     if (!ad_getentryoff(ad, ADEID_RFORK)
        || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
        ) {
-      LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset."); 
-      return -1;
+        errno = EIO;
+        LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset."); 
+        return -1;
     }
 
     if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
@@ -622,8 +679,7 @@ static int ad_header_read(struct adouble *ad, struct stat *hst)
     return 0;
 }
 
-
-/*
+/* ---------------------------------------
  * Put the .AppleDouble where it needs to be:
  *
  *         /   a/.AppleDouble/b
@@ -666,6 +722,39 @@ ad_path( path, adflags )
     return( pathbuf );
 }
 
+/* ---------------------------------------
+ * Put the resource fork where it needs to be:
+ * ._name
+ */
+char *
+ad_path_osx( path, adflags )
+    const char *path;
+    int                adflags;
+{
+    static char        pathbuf[ MAXPATHLEN + 1];
+    char       c, *slash, buf[MAXPATHLEN + 1];
+    
+    if (!strcmp(path,".")) {
+            /* fixme */
+        getcwd(buf, MAXPATHLEN);
+    }
+    else {
+        strlcpy(buf, path, MAXPATHLEN +1);
+    }
+    if (NULL != ( slash = strrchr( buf, '/' )) ) {
+       c = *++slash;
+       *slash = '\0';
+       strlcpy( pathbuf, buf, MAXPATHLEN +1);
+       *slash = c;
+    } else {
+       pathbuf[ 0 ] = '\0';
+       slash = buf;
+    }
+    strlcat( pathbuf, "._", MAXPATHLEN  +1);  
+    strlcat( pathbuf, slash, MAXPATHLEN +1);
+    return pathbuf;
+}
+
 /*
  * Support inherited protection modes for AppleDouble files.  The supplied
  * mode is ANDed with the parent directory's mask value in lieu of "umask",
@@ -825,12 +914,14 @@ struct stat stbuf;
 /* ----------------- */
 static int ad_error(struct adouble *ad, int adflags)
 {
+int err = errno;
     if ((adflags & ADFLAGS_NOHF)) {
         /* FIXME double check : set header offset ?*/
         return 0;
     }
     if ((adflags & ADFLAGS_DF)) {
        ad_close( ad, ADFLAGS_DF );
+       err = errno;
     }
     return -1 ;
 }
@@ -847,6 +938,12 @@ void ad_init(struct adouble *ad, int flags)
 {
     memset( ad, 0, sizeof( struct adouble ) );
     ad->ad_flags = flags;
+    if (flags == AD_VERSION2_OSX) {
+        ad->ad_path     = ad_path_osx;
+    }
+    else {
+        ad->ad_path     = ad_path;
+    }
 }
 
 /* -------------------
@@ -933,10 +1030,12 @@ int ad_open( path, adflags, oflags, mode, ad )
        return 0;
     }
 
-    ad_p = ad_path( path, adflags );
+    ad_p = ad->ad_path( path, adflags );
 
     hoflags = oflags & ~O_CREAT;
-    hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
+    if (!(adflags & ADFLAGS_RDONLY)) {
+        hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
+    }
     ad->ad_hf.adf_fd = open( ad_p, hoflags, 0 );
     if (ad->ad_hf.adf_fd < 0 ) {
         if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
@@ -953,40 +1052,36 @@ int ad_open( path, adflags, oflags, mode, ad )
             * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
             */
            admode = mode;
+           errno = 0;
            st_invalid = ad_mode_st(ad_p, &admode, &st);
            admode = ad_hf_mode(admode); 
-           errno = 0;
-           ad->ad_hf.adf_fd = open( ad_p, oflags,admode );
-           if ( ad->ad_hf.adf_fd < 0 ) {
+           if ( errno == ENOENT && !(adflags & ADFLAGS_NOADOUBLE) && ad->ad_flags != AD_VERSION2_OSX) {
                /*
                 * Probably .AppleDouble doesn't exist, try to
                 * mkdir it.
                 */
-               if (errno == ENOENT && (adflags & ADFLAGS_NOADOUBLE) == 0) {
-                   if (NULL == ( slash = strrchr( ad_p, '/' )) ) {
-                       return ad_error(ad, adflags);
-                   }
-                   *slash = '\0';
-                   errno = 0;
-                   if ( ad_mkdir( ad_p, 0777 ) < 0 ) {
-                       return ad_error(ad, adflags);
-                   }
-                   *slash = '/';
-                   admode = mode;
-                   st_invalid = ad_mode_st(ad_p, &admode, &st);
-                   admode = ad_hf_mode(admode); 
-                   ad->ad_hf.adf_fd = open( ad_p, oflags, admode);
-                   if ( ad->ad_hf.adf_fd < 0 ) {
-                       return ad_error(ad, adflags);
-                   }
-               } else {
-                    return ad_error(ad, adflags);
+               if (NULL == ( slash = strrchr( ad_p, '/' )) ) {
+                   return ad_error(ad, adflags);
+               }
+               *slash = '\0';
+               errno = 0;
+               if ( ad_mkdir( ad_p, 0777 ) < 0 ) {
+                   return ad_error(ad, adflags);
                }
+               *slash = '/';
+               admode = mode;
+               st_invalid = ad_mode_st(ad_p, &admode, &st);
+               admode = ad_hf_mode(admode); 
+           }
+           /* retry with O_CREAT */
+           ad->ad_hf.adf_fd = open( ad_p, oflags,admode );
+           if ( ad->ad_hf.adf_fd < 0 ) {
+               return ad_error(ad, adflags);
            }
            ad->ad_hf.adf_flags = oflags;
            /* just created, set owner if admin owner (root) */
            if (!st_invalid) {
-               ad_chown(path, &st);
+               ad_chown(ad_p, &st);
            }
        }
        else {
@@ -1008,8 +1103,10 @@ int ad_open( path, adflags, oflags, mode, ad )
          * instead of reading it.
         */
         if (new_rfork(path, ad, adflags) < 0) {
+            int err = errno;
             /* the file is already deleted, perm, whatever, so return an error*/
             ad_close(ad, adflags);
+            errno = err;
            return -1;
        }
     } else {
@@ -1019,34 +1116,64 @@ int ad_open( path, adflags, oflags, mode, ad )
                || (ad_v1tov2(ad, ad_p) < 0) || (ad_update(ad, ad_p) < 0)
 #endif /* AD_VERSION == AD_VERSION2 */
         ) {
+            int err = errno;
+            
             ad_close( ad, adflags );
-           return( -1 );
+            errno = err;
+           return -1;
        }
     }
     return 0 ;
 }
 
+/* ----------------------------------- 
+ * return only metadata but try very hard
+*/
+int ad_metadata(const char *name, int flags, struct adouble *adp)
+{
+    uid_t uid;
+    int   ret, err;
+
+    if ((ret = ad_open(name, ADFLAGS_HF | (flags), O_RDONLY, 0, adp)) < 0 && errno == EACCES) {
+        uid = geteuid();
+        if (seteuid(0)) {
+            LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
+            errno = EACCES;
+            return -1;
+        }
+        /* we are root open read only */
+        ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY|(flags), O_RDONLY, 0, adp);
+        err = errno;
+        if ( seteuid(uid) < 0) {
+            LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
+            exit(EXITERR_SYS);
+        }
+        errno = err;
+    }
+    return ret;
+}
+
 /* ----------------------------------- */
 static int new_rfork(const char *path, struct adouble *ad, int adflags)
 {
-#if 0
-    struct timeval      tv;
-#endif    
     const struct entry  *eid;
     u_int16_t           ashort;
     struct stat         st;
 
     ad->ad_magic = AD_MAGIC;
-    ad->ad_version = ad->ad_flags;
-    if (!ad->ad_version)
-    ad->ad_version = AD_VERSION;
+    ad->ad_version = ad->ad_flags & 0x0f0000;
+    if (!ad->ad_version) {
+        ad->ad_version = AD_VERSION;
+    }
 
     memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
     memset(ad->ad_data, 0, sizeof(ad->ad_data));
 
 #if AD_VERSION == AD_VERSION2
-    if (ad->ad_version == AD_VERSION2)
+    if (ad->ad_flags == AD_VERSION2)
        eid = entry_order2;
+    else if (ad->ad_flags == AD_VERSION2_OSX)
+       eid = entry_order_osx;
     else
 #endif
        eid = entry_order1;
@@ -1065,8 +1192,8 @@ static int new_rfork(const char *path, struct adouble *ad, int adflags)
                     &ashort, sizeof(ashort));
     } else {
         /* set default creator/type fields */
-       memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"TEXT", 4);
-       memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"UNIX", 4);
+       memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
+       memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
     }
 
     /* make things invisible */
@@ -1078,12 +1205,6 @@ static int new_rfork(const char *path, struct adouble *ad, int adflags)
                     &ashort, sizeof(ashort));
     }
 
-#if 0
-    if (gettimeofday(&tv, NULL) < 0) {
-       return -1;
-    } 
-#endif
-    
     if (stat(path, &st) < 0) {
        return -1;
     }
@@ -1101,7 +1222,7 @@ static int new_rfork(const char *path, struct adouble *ad, int adflags)
 int ad_refresh(struct adouble *ad)
 {
 
-  if (ad->ad_hf.adf_fd < -1)
+  if (ad->ad_hf.adf_fd < 0)
     return -1;
 
   return ad_header_read(ad, NULL);