]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_open.c
Configurable symlink behaviour
[netatalk.git] / libatalk / adouble / ad_open.c
index 5b3f93c483b4dd3dbfad88969d2b3a5188194c4b..b2b291b659a2e1d3e7666fa53d5ef5123d490f60 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: ad_open.c,v 1.61 2010-01-04 13:49:48 franklahm Exp $
- *
  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
  * Copyright (c) 1990,1991 Regents of The University of Michigan.
  * All Rights Reserved.
@@ -283,6 +281,7 @@ static int ad_update(struct adouble *ad, const char *path)
 
     /* last place for failure. */
     if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
+        munmap(buf, st.st_size + shiftdata);
         goto bail_lock;
     }
 
@@ -1015,7 +1014,7 @@ int ad_stat(const char *path, struct stat *stbuf)
         return -1;
     }
 
-    return stat( p, stbuf );
+    return stat(p, stbuf);
 }
 
 /* ----------------
@@ -1037,7 +1036,7 @@ static int ad_chown(const char *path, struct stat *stbuf)
     if (default_uid != (uid_t)-1) {
         /* we are root (admin) */
         id = (default_uid)?default_uid:stbuf->st_uid;
-        ret = chown( path, id, stbuf->st_gid );
+        ret = chown(path, id, stbuf->st_gid);
     }
 #endif
     return ret;
@@ -1080,9 +1079,8 @@ ad_mkdir( const char *path, int mode)
     int st_invalid;
     struct stat stbuf;
 
-#ifdef DEBUG
-    LOG(log_debug9, logtype_default, "ad_mkdir: Creating directory with mode %d", mode);
-#endif
+    LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
+        path, mode, getcwdpath());
 
     st_invalid = ad_mode_st(path, &mode, &stbuf);
     ret = mkdir( path, mode );
@@ -1246,7 +1244,8 @@ void ad_init(struct adouble *ad, int flags, int options)
  * @returns 0 on success
  *
  * @note It's not possible to open the header file O_RDONLY -- the read
- *       will fail and return an error. this refcounts things now.
+ *       will fail and return an error. this refcounts things now.\n
+ *       metadata(ressource)-fork only gets created with O_CREAT.
  */
 int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble  *ad)
 {
@@ -1265,6 +1264,7 @@ int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble
         ad->ad_adflags = adflags;
         ad->ad_resource_fork.adf_refcount = 0;
         ad->ad_data_fork.adf_refcount = 0;
+        ad->ad_data_fork.adf_syml=0;
     }
     else {
         ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
@@ -1283,14 +1283,29 @@ int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble
                     admode = mode;
                 }
             }
-            ad->ad_data_fork.adf_fd =open( path, hoflags, admode );
-            if (ad->ad_data_fork.adf_fd < 0 ) {
+                
+            ad->ad_data_fork.adf_fd = open(path, hoflags | ad_get_syml_opt(ad), admode);
+            
+            if (ad->ad_data_fork.adf_fd == -1) {
                 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
                     hoflags = oflags;
-                    ad->ad_data_fork.adf_fd = open( path, hoflags, admode );
+                    ad->ad_data_fork.adf_fd = open( path, hoflags | ad_get_syml_opt(ad), admode );
+                }
+                if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
+                    int lsz;
+
+                    ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
+                    lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
+                    if (lsz <= 0) {
+                        free(ad->ad_data_fork.adf_syml);
+                        return -1;
+                    }
+                    ad->ad_data_fork.adf_syml[lsz] = 0;
+                    ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
                 }
             }
-            if ( ad->ad_data_fork.adf_fd < 0)
+
+            if ( ad->ad_data_fork.adf_fd == -1 )
                 return -1;
 
             AD_SET(ad->ad_data_fork.adf_off);
@@ -1337,6 +1352,8 @@ int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble
             return -1;
         }
         ad_refresh(ad);
+        /* it's not new anymore */
+        ad->ad_md->adf_flags &= ~( O_TRUNC | O_CREAT );
         ad->ad_md->adf_refcount++;
         goto sfm;
     }
@@ -1349,11 +1366,11 @@ int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble
     if (!(adflags & ADFLAGS_RDONLY)) {
         hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
     }
-    ad->ad_md->adf_fd = open( ad_p, hoflags, 0 );
+    ad->ad_md->adf_fd = open(ad_p, hoflags | ad_get_syml_opt(ad), 0);
     if (ad->ad_md->adf_fd < 0 ) {
         if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
             hoflags = oflags & ~(O_CREAT | O_EXCL);
-            ad->ad_md->adf_fd = open( ad_p, hoflags, 0 );
+            ad->ad_md->adf_fd = open(ad_p, hoflags | ad_get_syml_opt(ad), 0);
         }
     }
 
@@ -1364,6 +1381,8 @@ int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble
              * here.
              * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
              */
+            LOG(log_debug, logtype_default, "ad_open(\"%s\"): {cwd: \"%s\"} creating adouble file",
+                ad_p, getcwdpath());
             admode = mode;
             errno = 0;
             st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
@@ -1371,8 +1390,8 @@ int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble
                 admode = mode;
             }
             admode = ad_hf_mode(admode);
-            if ( errno == ENOENT && !(adflags & ADFLAGS_NOADOUBLE) && ad->ad_flags != AD_VERSION2_OSX) {
-                if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
+            if ((errno == ENOENT) && (ad->ad_flags != AD_VERSION2_OSX)) {
+                if (ad->ad_ops->ad_mkrf(ad_p) < 0) {
                     return ad_error(ad, adflags);
                 }
                 admode = mode;
@@ -1504,23 +1523,36 @@ sfm:
     return 0 ;
 }
 
-/* -----------------------------------
- * return only metadata but try very hard
+/*!
+ * @brief open metadata, possibly as root
+ *
+ * Return only metadata but try very hard ie at first try as user, then try as root.
+ *
+ * @param name  name of file/dir
+ * @param flags ADFLAGS_DIR: name is a directory \n
+ *              ADFLAGS_CREATE: force creation of header file, but only as user, not as root\n
+ *              ADFLAGS_OPENFORKS: test if name is open by another afpd process
+ *
+ * @param adp   pointer to struct adouble
+ *
+ * @note caller MUST pass ADFLAGS_DIR for directories. Whether ADFLAGS_CREATE really creates
+ *       a adouble file depends on various other volume options, eg. ADVOL_CACHE
  */
 int ad_metadata(const char *name, int flags, struct adouble *adp)
 {
     uid_t uid;
-    int   ret, err;
-    int   dir = flags & ADFLAGS_DIR;
-    int   adouble = 0;
-    
-    if (!(flags & ADFLAGS_NOADOUBLE)) {
-      adouble = O_CREAT;
-    }
-    
-    /* Open with O_CREAT, thus enumarating a dir will create missing adouble files, see: */
-    /* http://marc.info/?l=netatalk-devel&m=124039156832408&w=2 */
-    if ((ret = ad_open(name, ADFLAGS_HF | dir, O_RDWR | adouble, 0666, adp)) < 0 && errno == EACCES) {
+    int   ret, err, dir;
+    int   create = O_RDONLY;
+
+    dir = flags & ADFLAGS_DIR;
+
+    /* Check if we shall call ad_open with O_CREAT */
+    if ( (adp->ad_options & ADVOL_CACHE)
+         && ! (adp->ad_options & ADVOL_NOADOUBLE)
+         && (flags & ADFLAGS_CREATE) ) {
+        create = O_CREAT | O_RDWR;
+    }
+    if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
         uid = geteuid();
         if (seteuid(0)) {
             LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
@@ -1549,6 +1581,41 @@ int ad_metadata(const char *name, int flags, struct adouble *adp)
     return ret;
 }
 
+/*
+ * @brief openat like wrapper for ad_metadata
+ */
+int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
+{
+    int ret = 0;
+    int cwdfd = -1;
+
+    if (dirfd != -1) {
+        if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
+            ret = -1;
+            goto exit;
+        }
+    }
+
+    if (ad_metadata(name, flags, adp) < 0) {
+        ret = -1;
+        goto exit;
+    }
+
+    if (dirfd != -1) {
+        if (fchdir(cwdfd) != 0) {
+            LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
+            exit(EXITERR_SYS);
+        }
+    }
+
+exit:
+    if (cwdfd != -1)
+        close(cwdfd);
+
+    return ret;
+
+}
+
 /* ----------------------------------- */
 static int new_rfork(const char *path, struct adouble *ad, int adflags)
 {
@@ -1606,7 +1673,7 @@ static int new_rfork(const char *path, struct adouble *ad, int adflags)
         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
     }
 
-    if (stat(path, &st) < 0) {
+    if (ostat(path, &st, ad_get_syml_opt(ad)) < 0) {
         return -1;
     }
 
@@ -1628,3 +1695,39 @@ int ad_refresh(struct adouble *ad)
 
     return ad->ad_ops->ad_header_read(ad, NULL);
 }
+
+int ad_openat(int dirfd,  /* dir fd openat like */
+              const char *path,
+              int adflags,
+              int oflags,
+              int mode,
+              struct adouble  *ad)
+{
+    int ret = 0;
+    int cwdfd = -1;
+
+    if (dirfd != -1) {
+        if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
+            ret = -1;
+            goto exit;
+        }
+    }
+
+    if (ad_open(path, adflags, oflags, mode, ad) < 0) {
+        ret = -1;
+        goto exit;
+    }
+
+    if (dirfd != -1) {
+        if (fchdir(cwdfd) != 0) {
+            LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
+            exit(EXITERR_SYS);
+        }
+    }
+
+exit:
+    if (cwdfd != -1)
+        close(cwdfd);
+
+    return ret;
+}