]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/file.c
of_findname
[netatalk.git] / etc / afpd / file.c
index 39b61c1a92c081e789105271aecde74922f7aecc..b12c723f70e8789484784b2ad1653dc9f695e326 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: file.c,v 1.47 2002-08-16 00:42:56 didg Exp $
+ * $Id: file.c,v 1.56 2002-09-06 02:57:49 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -64,11 +64,6 @@ char *strchr (), *strrchr ();
 #include "parse_mtab.h"
 #endif /* DID_MTAB */
 
-#ifdef FORCE_UIDGID
-#warning UIDGID
-#include "uid.h"
-#endif /* FORCE_UIDGID */
-
 /* the format for the finderinfo fields (from IM: Toolbox Essentials):
  * field         bytes        subfield    bytes
  * 
@@ -110,6 +105,7 @@ int getmetadata(struct vol *vol,
     u_int32_t          aint;
     u_int16_t          ashort;
     u_char              achar, fdType[4];
+    struct maccess     ma;
 
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "begin getmetadata:");
@@ -132,6 +128,17 @@ int getmetadata(struct vol *vol,
                 ashort = htons(ATTRBIT_INVISIBLE);
             } else
                 ashort = 0;
+#if 0
+            /* FIXME do we want a visual clue if the file is read only
+             */
+            accessmode( ".", &ma, dir , NULL);
+            if ((ma.ma_user & AR_UWRITE)) {
+               accessmode( upath, &ma, dir , st);
+               if (!(ma.ma_user & AR_UWRITE)) {
+                       attrbits |= ATTRBIT_NOWRITE;
+                }
+            }
+#endif
             if (attrbits)
                 ashort = htons(ntohs(ashort) | attrbits);
             memcpy(data, &ashort, sizeof( ashort ));
@@ -153,13 +160,7 @@ int getmetadata(struct vol *vol,
         case FILPBIT_MDATE :
             if ( adp && (ad_getdate(adp, AD_DATE_MODIFY, &aint) == 0)) {
                 if ((st->st_mtime > AD_DATE_TO_UNIX(aint))) {
-                        if ( fstat( ad_hfileno( adp ), &hst ) < 0 ) {
-                            LOG(log_error, logtype_default, "getfilparams fstat: %s", strerror(errno) );
-                        }
-                        else if (hst.st_mtime < st->st_mtime) 
-                            aint = AD_DATE_FROM_UNIX(st->st_mtime);
-                        else 
-                            aint = AD_DATE_FROM_UNIX(hst.st_mtime);
+                   aint = AD_DATE_FROM_UNIX(st->st_mtime);
                 }
             } else {
                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
@@ -186,9 +187,9 @@ int getmetadata(struct vol *vol,
                 }
             }
 
-            if ((!adp || (memcmp(ad_entry(adp, ADEID_FINDERI),
-                                  ufinderi, 8 ) == 0)) &&
-                    (em = getextmap( path ))) {
+            if ((!adp  || !memcmp(ad_entry(adp, ADEID_FINDERI),ufinderi , 8 )) 
+               && (em = getextmap( path ))
+            ) {
                 memcpy(data, em->em_type, sizeof( em->em_type ));
                 memcpy(data + 4, em->em_creator, sizeof(em->em_creator));
             }
@@ -373,7 +374,7 @@ int getfilparams(struct vol *vol,
 #endif /* DEBUG */
 
     upath = mtoupath(vol, path);
-    if ((of = of_findname(vol, dir, path))) {
+    if ((of = of_findname(upath, st))) {
         adp = of->of_ad;
        attrbits = ((of->of_ad->ad_df.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
        attrbits |= ((of->of_ad->ad_hf.adf_refcount > of->of_ad->ad_df.adf_refcount)? ATTRBIT_ROPEN : 0);
@@ -427,10 +428,7 @@ int                ibuflen, *rbuflen;
     char               *path, *upath;
     int                        creatf, did, openf, retvalue = AFP_OK;
     u_int16_t          vid;
-#ifdef FORCE_UIDGID
-    uidgidset          *uidgid;
-#endif /* FORCE_UIDGID */
-
+    int                 ret;
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "begin afp_createfile:");
 #endif /* DEBUG */
@@ -461,13 +459,12 @@ int               ibuflen, *rbuflen;
     }
 
     upath = mtoupath(vol, path);
-    {
-    int ret;
-        if (0 != (ret = check_name(vol, upath))) 
-            return  ret;
-    }
+    if (0 != (ret = check_name(vol, upath))) 
+       return  ret;
 
-    if ((of = of_findname(vol, curdir, path))) {
+    ret = stat(upath, &st);
+    /* if upath is deleted we already in trouble anyway */
+    if (!ret && (of = of_findname(upath, &st))) {
         adp = of->of_ad;
     } else {
         memset(&ad, 0, sizeof(ad));
@@ -475,11 +472,11 @@ int               ibuflen, *rbuflen;
     }
     if ( creatf) {
         /* on a hard create, fail if file exists and is open */
-        if ((stat(upath, &st) == 0) && of)
+        if (!ret && of)
             return AFPERR_BUSY;
         openf = O_RDWR|O_CREAT|O_TRUNC;
     } else {
-       /* on a soft create, if the file is open then ad_open won't failed 
+       /* on a soft create, if the file is open then ad_open won't fail
           because open syscall is not called
        */
        if (of) {
@@ -488,30 +485,12 @@ int               ibuflen, *rbuflen;
         openf = O_RDWR|O_CREAT|O_EXCL;
     }
 
-#ifdef FORCE_UIDGID
-
-    /* preserve current euid, egid */
-    save_uidgid ( uidgid );
-
-    /* perform all switching of users */
-    set_uidgid ( vol );
-
-#endif /* FORCE_UIDGID */
-
     if ( ad_open( upath, vol_noadouble(vol)|ADFLAGS_DF|ADFLAGS_HF,
                   openf, 0666, adp) < 0 ) {
         switch ( errno ) {
         case EEXIST :
-#ifdef FORCE_UIDGID
-            /* bring everything back to old euid, egid */
-            restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
             return( AFPERR_EXIST );
         case EACCES :
-#ifdef FORCE_UIDGID
-            /* bring everything back to old euid, egid */
-            restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
             return( AFPERR_ACCESS );
         case ENOENT:
             /* on noadouble volumes, just creating the data fork is ok */
@@ -519,10 +498,6 @@ int                ibuflen, *rbuflen;
                 goto createfile_done;
             /* fallthrough */
         default :
-#ifdef FORCE_UIDGID
-            /* bring everything back to old euid, egid */
-            restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
             return( AFPERR_PARAM );
         }
     }
@@ -547,11 +522,6 @@ createfile_done:
     LOG(log_info, logtype_afpd, "end afp_createfile");
 #endif /* DEBUG */
 
-#ifdef FORCE_UIDGID
-    /* bring everything back to old euid, egid */
-    restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
-
     return (retvalue);
 }
 
@@ -615,6 +585,10 @@ int                ibuflen, *rbuflen;
     return( rc );
 }
 
+/*
+ * cf AFP3.0.pdf page 252 for change_mdate and change_parent_mdate logic  
+ *
+*/
 
 int setfilparams(struct vol *vol,
                  char *path, u_int16_t bitmap, char *buf )
@@ -629,36 +603,32 @@ int setfilparams(struct vol *vol,
     u_int32_t          aint;
     struct utimbuf     ut;
 
-#ifdef FORCE_UIDGID
-    uidgidset          *uidgid;
+    int                 change_mdate = 0;
+    int                 change_parent_mdate = 0;
+    int                 newdate = 0;
+    struct timeval      tv;
 
-    uidgid = malloc(sizeof(uidgidset));
-#endif /* FORCE_UIDGID */
 
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "begin setfilparams:");
 #endif /* DEBUG */
 
     upath = mtoupath(vol, path);
-    if ((of = of_findname(vol, curdir, path))) {
+    if ((of = of_findname(upath, NULL))) {
         adp = of->of_ad;
     } else {
         memset(&ad, 0, sizeof(ad));
         adp = &ad;
     }
 
-#ifdef FORCE_UIDGID
-    save_uidgid ( uidgid );
-    set_uidgid ( vol );
-#endif /* FORCE_UIDGID */
+    if (check_access(upath, OPENACC_WR ) < 0) {
+        return AFPERR_ACCESS;
+    }
 
     if (ad_open( upath, vol_noadouble(vol) | ADFLAGS_HF,
                  O_RDWR|O_CREAT, 0666, adp) < 0) {
         /* for some things, we don't need an adouble header */
         if (bitmap & ~(1<<FILPBIT_MDATE)) {
-#ifdef FORCE_UIDGID
-            restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
             return vol_noadouble(vol) ? AFP_OK : AFPERR_ACCESS;
         }
         isad = 0;
@@ -676,6 +646,7 @@ int setfilparams(struct vol *vol,
 
         switch(  bit ) {
         case FILPBIT_ATTR :
+            change_mdate = 1;
             memcpy(&ashort, buf, sizeof( ashort ));
             ad_getattr(adp, &bshort);
             if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
@@ -683,39 +654,46 @@ int setfilparams(struct vol *vol,
             } else {
                 bshort &= ~ashort;
             }
+            if ((ashort & htons(ATTRBIT_INVISIBLE)))
+                change_parent_mdate = 1;
             ad_setattr(adp, bshort);
             buf += sizeof( ashort );
             break;
 
         case FILPBIT_CDATE :
+            change_mdate = 1;
             memcpy(&aint, buf, sizeof(aint));
             ad_setdate(adp, AD_DATE_CREATE, aint);
             buf += sizeof( aint );
             break;
 
         case FILPBIT_MDATE :
-            memcpy(&aint, buf, sizeof( aint ));
-            if (isad)
-                ad_setdate(adp, AD_DATE_MODIFY, aint);
-            ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
-            utime(upath, &ut);
-            buf += sizeof( aint );
+            memcpy(&newdate, buf, sizeof( newdate ));
+            buf += sizeof( newdate );
             break;
 
         case FILPBIT_BDATE :
+            change_mdate = 1;
             memcpy(&aint, buf, sizeof(aint));
             ad_setdate(adp, AD_DATE_BACKUP, aint);
             buf += sizeof( aint );
             break;
 
         case FILPBIT_FINFO :
-            if ((memcmp( ad_entry( adp, ADEID_FINDERI ), ufinderi, 8 ) == 0)
-                    && (em = getextmap( path )) &&
-                    (memcmp(buf, em->em_type, sizeof( em->em_type )) == 0) &&
-                    (memcmp(buf + 4, em->em_creator,
-                            sizeof( em->em_creator )) == 0)) {
+            change_mdate = 1;
+
+            if (!memcmp( ad_entry( adp, ADEID_FINDERI ), ufinderi, 8 )
+                    && ( 
+                     ((em = getextmap( path )) &&
+                      !memcmp(buf, em->em_type, sizeof( em->em_type )) &&
+                      !memcmp(buf + 4, em->em_creator,sizeof( em->em_creator)))
+                     || ((em = getdefextmap()) &&
+                      !memcmp(buf, em->em_type, sizeof( em->em_type )) &&
+                      !memcmp(buf + 4, em->em_creator,sizeof( em->em_creator)))
+            )) {
                 memcpy(buf, ufinderi, 8 );
             }
+
             memcpy(ad_entry( adp, ADEID_FINDERI ), buf, 32 );
             buf += 32;
             break;
@@ -773,20 +751,31 @@ int setfilparams(struct vol *vol,
     }
 
 setfilparam_done:
+    if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
+       newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
+    }
+    if (newdate) {
+       if (isad)
+          ad_setdate(adp, AD_DATE_MODIFY, newdate);
+       ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
+       utime(upath, &ut);
+    }
+
     if (isad) {
         ad_flush( adp, ADFLAGS_HF );
         ad_close( adp, ADFLAGS_HF );
 
-#ifdef FORCE_UIDGID
-        restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
+    }
 
+    if (change_parent_mdate && gettimeofday(&tv, NULL) == 0) {
+        newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
+        bitmap = 1<<FILPBIT_MDATE;
+        setdirparams(vol, "", bitmap, (char *)&newdate);
     }
 
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "end setfilparams:");
 #endif /* DEBUG */
-
     return err;
 }
 
@@ -794,10 +783,9 @@ setfilparam_done:
  * renamefile and copyfile take the old and new unix pathnames
  * and the new mac name.
  * NOTE: if we have to copy a file instead of renaming it, locks
- *       will break.
- * FIXME: locks on ressource fork will always break thanks to ad_close, done ?
+ *       will break. Anyway it's an error because then we have 2 files.
  *
- * src         the full source absolute path 
+ * src         the source path 
  * dst         the dest filename in current dir
  * newname     the dest mac name
  * adp         adouble struct of src file, if open, or & zeroed one
@@ -808,7 +796,6 @@ char        *src, *dst, *newname;
 const int         noadouble;
 struct adouble    *adp;
 {
-    struct ofork       *opened;
     char               adsrc[ MAXPATHLEN + 1];
     int                        len, rc;
 
@@ -904,7 +891,7 @@ int         ibuflen, *rbuflen;
 {
     struct vol *vol;
     struct dir *dir;
-    char       *newname, *path, *p;
+    char       *newname, *path, *p, *upath;
     u_int32_t  sdid, ddid;
     int                plen, err, retvalue = AFP_OK;
     u_int16_t  svid, dvid;
@@ -945,14 +932,18 @@ int               ibuflen, *rbuflen;
      *      however, copyfile doesn't have any of that info,
      *      and locks need to stay coherent. as a result,
      *      we just balk if the file is opened already. */
-    if (of_findname(vol, curdir, path))
+
+    upath = mtoupath(vol, newname );
+    if (of_findname(upath, NULL))
         return AFPERR_DENYCONF;
 
     newname = obj->newtmp;
     strcpy( newname, path );
 
     p = ctoupath( vol, curdir, newname );
-
+#ifdef FORCE_UIDGID
+    /* FIXME svid != dvid && dvid's user can't read svid */
+#endif
     if (( vol = getvolbyvid( dvid )) == NULL ) {
         return( AFPERR_PARAM );
     }
@@ -968,7 +959,7 @@ int         ibuflen, *rbuflen;
         return( AFPERR_NOOBJ );
     }
     if ( *path != '\0' ) {
-        return( AFPERR_BADTYPE );
+        return( AFPERR_BADTYPE ); /* not a directory. AFPERR_PARAM? */
     }
 
     /* one of the handful of places that knows about the path type */
@@ -978,21 +969,27 @@ int               ibuflen, *rbuflen;
     if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
         strncpy( newname, ibuf, plen );
         newname[ plen ] = '\0';
+        if (strlen(newname) != plen) {
+            /* there's \0 in newname, e.g. it's a pathname not
+             * only a filename. 
+            */
+            return( AFPERR_PARAM );
+        }
     }
 
-    if ( (err = copyfile(p, mtoupath(vol, newname ), newname,
-                         vol_noadouble(vol))) < 0 ) {
+    if ( (err = copyfile(p, mtoupath(vol, newname), 
+                            newname, vol_noadouble(vol))) < 0 ) {
         return err;
     }
 
-    setvoltime(obj, vol );
-
 #ifdef DROPKLUDGE
     if (vol->v_flags & AFPVOL_DROPBOX) {
         retvalue=matchfile2dirperms(newname, vol, sdid);
     }
 #endif /* DROPKLUDGE */
 
+    setvoltime(obj, vol );
+
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "end afp_copyfile:");
 #endif /* DEBUG */
@@ -1046,11 +1043,14 @@ const int   noadouble;
     char               filebuf[8192];
     int                        sfd, dfd, len, err = AFP_OK;
     ssize_t             cc;
-
+    char                *dpath;
+    int                 admode;
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "begin copyfile:");
 #endif /* DEBUG */
 
+    dpath = ad_path( dst, ADFLAGS_HF );
+    admode = ad_mode( dst, 0666 );
     if (newname) {
         if ((sfd = open( ad_path( src, ADFLAGS_HF ), O_RDONLY, 0 )) < 0 ) {
             switch ( errno ) {
@@ -1062,8 +1062,7 @@ const int   noadouble;
                 return( AFPERR_PARAM );
             }
         } else {
-            if (( dfd = open( ad_path( dst, ADFLAGS_HF ), O_WRONLY|O_CREAT,
-                              ad_mode( ad_path( dst, ADFLAGS_HF ), 0666 ))) < 0 ) {
+            if (( dfd = open( dpath, O_WRONLY|O_CREAT,ad_hf_mode(admode))) < 0 ) {
                 close( sfd );
                 switch ( errno ) {
                 case ENOENT :
@@ -1113,7 +1112,7 @@ copyheader_done:
             close(sfd);
             close(dfd);
             if (err < 0) {
-                unlink(ad_path(dst, ADFLAGS_HF));
+                unlink(dpath);
                 return err;
             }
         }
@@ -1131,7 +1130,7 @@ copyheader_done:
         }
     }
 
-    if (( dfd = open( dst, O_WRONLY|O_CREAT, ad_mode( dst, 0666 ))) < 0 ) {
+    if (( dfd = open( dst, O_WRONLY|O_CREAT, admode)) < 0 ) {
         close( sfd );
         switch ( errno ) {
         case ENOENT :
@@ -1180,7 +1179,7 @@ copydata_done:
     close(sfd);
     close(dfd);
     if (err < 0) {
-        unlink(ad_path(dst, ADFLAGS_HF));
+        unlink(dpath);
         unlink(dst);
         return err;
     }
@@ -1221,6 +1220,7 @@ copydata_done:
    ie deletfile called by afp_delete
 
    when deletefile is called we don't have lock on it, file is closed (for us)
+   untrue if called by renamefile
 */
 int deletefile( file, checkAttrib )
 char           *file;
@@ -1501,11 +1501,11 @@ int             ibuflen, *rbuflen;
     ibuf += sizeof(id);
 
     if ((upath = cnid_resolve(vol->v_db, &id, buffer, len)) == NULL) {
-        return AFPERR_BADID;
+        return AFPERR_NOID; /* was AFPERR_BADID, but help older Macs */
     }
 
     if (( dir = dirlookup( vol, id )) == NULL ) {
-        return( AFPERR_PARAM );
+        return AFPERR_NOID; /* idem AFPERR_PARAM */
     }
 
     if ((movecwd(vol, dir) < 0) || (stat(upath, &st) < 0)) {
@@ -1642,7 +1642,8 @@ int               ibuflen, *rbuflen;
     struct adouble     add;
     struct adouble     *adsp;
     struct adouble     *addp;
-    struct ofork       *opened;
+    struct ofork       *s_of;
+    struct ofork       *d_of;
     
 #ifdef CNID_DB
     int                 slen, dlen;
@@ -1700,9 +1701,9 @@ int               ibuflen, *rbuflen;
     }
     memset(&ads, 0, sizeof(ads));
     adsp = &ads;
-    if ((opened = of_findname(vol, curdir, path))) {
+    if ((s_of = of_findname(upath, &srcst))) {
             /* reuse struct adouble so it won't break locks */
-            adsp = opened->of_ad;
+            adsp = s_of->of_ad;
     }
     /* save some stuff */
     sdir = curdir;
@@ -1750,10 +1751,16 @@ int             ibuflen, *rbuflen;
     }
     memset(&add, 0, sizeof(add));
     addp = &add;
-    if ((opened = of_findname(vol, curdir, path))) {
+    if ((d_of = of_findname( upath, &destst))) {
             /* reuse struct adouble so it won't break locks */
-            addp = opened->of_ad;
+            addp = d_of->of_ad;
     }
+
+    /* they are not on the same device and at least one is open
+    */
+    if ((d_of || s_of)  && srcst.st_dev != destst.st_dev)
+        return AFPERR_MISC;
+    
 #ifdef CNID_DB
     /* look for destination id. */
     did = cnid_lookup(vol->v_db, &destst, curdir->d_did, upath,
@@ -1770,17 +1777,17 @@ int             ibuflen, *rbuflen;
     /* now, quickly rename the file. we error if we can't. */
     if ((err = renamefile(p, temp, temp, vol_noadouble(vol), adsp)) < 0)
         goto err_exchangefile;
-    of_rename(vol, sdir, spath, curdir, temp);
+    of_rename(vol, s_of, sdir, spath, curdir, temp);
 
     /* rename destination to source */
-    if ((err = renamefile(path, p, spath, vol_noadouble(vol), addp)) < 0)
+    if ((err = renamefile(upath, p, spath, vol_noadouble(vol), addp)) < 0)
         goto err_src_to_tmp;
-    of_rename(vol, curdir, path, sdir, spath);
+    of_rename(vol, d_of, curdir, path, sdir, spath);
 
     /* rename temp to destination */
     if ((err = renamefile(temp, upath, path, vol_noadouble(vol), adsp)) < 0)
         goto err_dest_to_src;
-    of_rename(vol, curdir, temp, curdir, path);
+    of_rename(vol, s_of, curdir, temp, curdir, path);
 
 #ifdef CNID_DB
     /* id's need switching. src -> dest and dest -> src. */
@@ -1823,20 +1830,22 @@ int             ibuflen, *rbuflen;
 
     /* all this stuff is so that we can unwind a failed operation
      * properly. */
+#ifdef CNID_DB
 err_temp_to_dest:
+#endif
     /* rename dest to temp */
     renamefile(upath, temp, temp, vol_noadouble(vol), adsp);
-    of_rename(vol, curdir, upath, curdir, temp);
+    of_rename(vol, s_of, curdir, upath, curdir, temp);
 
 err_dest_to_src:
     /* rename source back to dest */
     renamefile(p, upath, path, vol_noadouble(vol), addp);
-    of_rename(vol, sdir, spath, curdir, path);
+    of_rename(vol, d_of, sdir, spath, curdir, path);
 
 err_src_to_tmp:
     /* rename temp back to source */
     renamefile(temp, p, spath, vol_noadouble(vol), adsp);
-    of_rename(vol, curdir, temp, sdir, spath);
+    of_rename(vol, s_of, curdir, temp, sdir, spath);
 
 err_exchangefile:
     return err;