]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/file.c
fix a bug in default CREATOR/TYPE handling.
[netatalk.git] / etc / afpd / file.c
index f3b6cf3094902112e2a4287dfc972d7b8d3c99b0..6a543dc6607b0c3ae3ee3ed02fef766c16e1019b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: file.c,v 1.92.2.2.2.20 2004-03-11 16:16:40 didg Exp $
+ * $Id: file.c,v 1.92.2.2.2.31.2.2 2004-10-20 20:05:27 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -69,8 +69,8 @@ char *strchr (), *strrchr ();
  */
 
 const u_char ufinderi[] = {
-                              'T', 'E', 'X', 'T', 'U', 'N', 'I', 'X',
                               0, 0, 0, 0, 0, 0, 0, 0,
+                              1, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0
                           };
@@ -79,18 +79,24 @@ const u_char ufinderi[] = {
 void *get_finderinfo(const char *mpath, struct adouble *adp, void *data)
 {
     struct extmap      *em;
-    void                *ad_finder;
+    void                *ad_finder = NULL;
+    int                 chk_ext = 0;
+    
+    if (adp)
+        ad_finder = ad_entry(adp, ADEID_FINDERI);
 
-    if (adp && (ad_finder = ad_entry(adp, ADEID_FINDERI))) {
+    if ((ad_finder != NULL)) {
         memcpy(data, ad_finder, 32);
+        /* default type ? */
+        if (!memcmp(ad_finder, ufinderi, 8))
+            chk_ext = 1;
     }
     else {
         memcpy(data, ufinderi, 32);
+        chk_ext = 1;
     }
-
-    if ((!adp  || !memcmp(ad_entry(adp, ADEID_FINDERI),ufinderi , 8 )) 
-               && (em = getextmap( mpath ))
-    ) {
+    /** Only enter if no appledouble information and no finder information found. */
+    if (chk_ext && (em = getextmap( mpath ))) {
         memcpy(data, em->em_type, sizeof( em->em_type ));
         memcpy((char *)data + 4, em->em_creator, sizeof(em->em_creator));
     }
@@ -218,12 +224,13 @@ char   stamp[ADEDLEN_PRIVSYN];
             }
         }
 #if AD_VERSION > AD_VERSION1
-        else if (adp && sizeof(dev_t) == ADEDLEN_PRIVDEV && sizeof(ino_t) == ADEDLEN_PRIVINO) {
+        else if (adp ) {
             /* update the ressource fork
              * for a folder adp is always null
              */
-            ad_setid(adp,(vol->v_flags & AFPVOL_NODEV)?0:st->st_dev, st->st_ino, aint, did, vol->v_stamp);
-            ad_flush(adp, ADFLAGS_HF);
+            if (ad_setid(adp,(vol->v_flags & AFPVOL_NODEV)?0:st->st_dev, st->st_ino, aint, did, vol->v_stamp)) {
+                ad_flush(adp, ADFLAGS_HF);
+            }
         }
 #endif    
     }
@@ -454,7 +461,10 @@ int getmetadata(struct vol *vol,
             memcpy(data, &aint, sizeof( aint ));
             data += sizeof( aint );
             break;
-        case FILPBIT_UNIXPR :
+        case FILPBIT_UNIXPR :            
+            /* accessmode may change st_mode with ACLs */
+            accessmode( upath, &ma, dir , st);
+
             aint = htonl(st->st_uid);
             memcpy( data, &aint, sizeof( aint ));
             data += sizeof( aint );
@@ -466,14 +476,12 @@ int getmetadata(struct vol *vol,
             memcpy( data, &aint, sizeof( aint ));
             data += sizeof( aint );
 
-            accessmode( upath, &ma, dir , st);
-
             *data++ = ma.ma_user;
             *data++ = ma.ma_world;
             *data++ = ma.ma_group;
             *data++ = ma.ma_owner;
             break;
-
+            
         default :
             return( AFPERR_BITMAP );
         }
@@ -524,10 +532,22 @@ int getfilparams(struct vol *vol,
             adp = &ad;
         }
 
-        if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) {
-             adp = NULL;
+        if ( ad_metadata( upath, 0, adp) < 0 ) {
+            switch (errno) {
+            case EACCES:
+                LOG(log_error, logtype_afpd, "getfilparams(%s): %s: check resource fork permission?",
+                upath, strerror(errno));
+                return AFPERR_ACCESS;
+            case EIO:
+                LOG(log_error, logtype_afpd, "getfilparams(%s): bad resource fork", upath);
+                /* fall through */
+            case ENOENT:
+            default:
+                adp = NULL;
+                break;
+            }
         }
-        else {
+        if (adp) {
            /* FIXME 
               we need to check if the file is open by another process.
               it's slow so we only do it if we have to:
@@ -750,28 +770,35 @@ int               ibuflen, *rbuflen;
 
 /*
  * cf AFP3.0.pdf page 252 for change_mdate and change_parent_mdate logic  
- *
+ * 
 */
 extern struct path Cur_Path;
 
 int setfilparams(struct vol *vol,
-                 struct path *path, u_int16_t bitmap, char *buf )
+                 struct path *path, u_int16_t f_bitmap, char *buf )
 {
     struct adouble     ad, *adp;
     struct ofork        *of;
     struct extmap      *em;
-    int                        bit = 0, isad = 1, err = AFP_OK;
+    int                        bit, isad = 1, err = AFP_OK;
     char                *upath;
     u_char              achar, *fdType, xyy[4];
     u_int16_t          ashort, bshort;
     u_int32_t          aint;
+    u_int32_t          upriv;
+    u_int16_t           upriv_bit = 0;
+    
     struct utimbuf     ut;
 
     int                 change_mdate = 0;
     int                 change_parent_mdate = 0;
     int                 newdate = 0;
     struct timeval      tv;
-
+    uid_t              f_uid;
+    gid_t              f_gid;
+    u_int16_t           bitmap = f_bitmap;
+    u_int32_t           cdate,bdate;
+    u_char              finder_buf[32];
 
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "begin setfilparams:");
@@ -789,95 +816,67 @@ int setfilparams(struct vol *vol,
         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)) {
-            return vol_noadouble(vol) ? AFP_OK : AFPERR_ACCESS;
-        }
-        isad = 0;
-    } else if ((ad_get_HF_flags( adp ) & O_CREAT) ) {
-        ad_setname(adp, path->m_name);
-    }
-
+    /* with unix priv maybe we have to change adouble file priv first */
+    bit = 0;
     while ( bitmap != 0 ) {
         while (( bitmap & 1 ) == 0 ) {
             bitmap = bitmap>>1;
             bit++;
         }
-
         switch(  bit ) {
         case FILPBIT_ATTR :
             change_mdate = 1;
             memcpy(&ashort, buf, sizeof( ashort ));
-            ad_getattr(adp, &bshort);
-            if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
-                bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
-            } 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 );
+            memcpy(&cdate, buf, sizeof(cdate));
+            buf += sizeof( cdate );
             break;
-
         case FILPBIT_MDATE :
             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 );
+            memcpy(&bdate, buf, sizeof( bdate));
+            buf += sizeof( bdate );
             break;
-
         case FILPBIT_FINFO :
             change_mdate = 1;
-
-            if (!memcmp( ad_entry( adp, ADEID_FINDERI ), ufinderi, 8 )
-                    && ( 
-                     ((em = getextmap( path->m_name )) &&
-                      !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 );
+            memcpy(finder_buf, buf, 32 );
             buf += 32;
             break;
-
         case FILPBIT_UNIXPR :
-           /* Skip the UIG/GID, no way to set them from OSX clients */
-            buf += sizeof( aint );
-            buf += sizeof( aint );
-
+            if (!vol_unix_priv(vol)) {
+               /* this volume doesn't use unix priv */
+               err = AFPERR_BITMAP;
+               bitmap = 0;
+               break;
+            }
             change_mdate = 1;
             change_parent_mdate = 1;
+
+            memcpy( &aint, buf, sizeof( aint ));
+            f_uid = ntohl (aint);
+            buf += sizeof( aint );
             memcpy( &aint, buf, sizeof( aint ));
+            f_gid = ntohl (aint);
             buf += sizeof( aint );
-            aint = ntohl (aint);
+            setfilowner(vol, f_uid, f_gid, path);
 
-            setfilunixmode(vol, path, aint);
+            memcpy( &upriv, buf, sizeof( upriv ));
+            buf += sizeof( upriv );
+            upriv = ntohl (upriv);
+            if ((upriv & S_IWUSR)) {
+               setfilunixmode(vol, path, upriv);
+            }
+            else {
+               /* do it later */
+               upriv_bit = 1;
+            }
             break;
-            /* Client needs to set the ProDOS file info for this file.
-               Use a defined string for TEXT to support crlf
-               translations and convert all else into pXYY per Inside
-               Appletalk.  Always set the creator as "pdos".  Changes
-               from original by Marsha Jackson. */
         case FILPBIT_PDINFO :
             if (afp_version < 30) { /* else it's UTF8 name */
                 achar = *buf;
@@ -893,6 +892,83 @@ int setfilparams(struct vol *vol,
                    xyy[2] = *buf++;
                    fdType = xyy;
                }
+                break;
+            }
+            /* fallthrough */
+        default :
+            err = AFPERR_BITMAP;
+            /* break while loop */
+            bitmap = 0;
+            break;
+        }
+
+        bitmap = bitmap>>1;
+        bit++;
+    }
+
+    /* second try with adouble open 
+    */
+    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 (f_bitmap & ~(1<<FILPBIT_MDATE)) {
+            return vol_noadouble(vol) ? AFP_OK : AFPERR_ACCESS;
+        }
+        isad = 0;
+    } else if ((ad_get_HF_flags( adp ) & O_CREAT) ) {
+        ad_setname(adp, path->m_name);
+    }
+    
+    bit = 0;
+    bitmap = f_bitmap;
+    while ( bitmap != 0 ) {
+        while (( bitmap & 1 ) == 0 ) {
+            bitmap = bitmap>>1;
+            bit++;
+        }
+
+        switch(  bit ) {
+        case FILPBIT_ATTR :
+            ad_getattr(adp, &bshort);
+            if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
+                (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
+                change_parent_mdate = 1;
+            if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
+                bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
+            } else {
+                bshort &= ~ashort;
+            }
+            ad_setattr(adp, bshort);
+            break;
+        case FILPBIT_CDATE :
+            ad_setdate(adp, AD_DATE_CREATE, cdate);
+            break;
+        case FILPBIT_MDATE :
+            break;
+        case FILPBIT_BDATE :
+            ad_setdate(adp, AD_DATE_BACKUP, bdate);
+            break;
+        case FILPBIT_FINFO :
+            if (!memcmp( ad_entry( adp, ADEID_FINDERI ), ufinderi, 8 )
+                    && ( 
+                     ((em = getextmap( path->m_name )) &&
+                      !memcmp(finder_buf, em->em_type, sizeof( em->em_type )) &&
+                      !memcmp(finder_buf + 4, em->em_creator,sizeof( em->em_creator)))
+                     || ((em = getdefextmap()) &&
+                      !memcmp(finder_buf, em->em_type, sizeof( em->em_type )) &&
+                      !memcmp(finder_buf + 4, em->em_creator,sizeof( em->em_creator)))
+            )) {
+                memcpy(finder_buf, ufinderi, 8 );
+            }
+            memcpy(ad_entry( adp, ADEID_FINDERI ), finder_buf, 32 );
+            break;
+        case FILPBIT_UNIXPR :
+            if (upriv_bit) {
+               setfilunixmode(vol, path, upriv);
+            }
+            break;
+        case FILPBIT_PDINFO :
+            if (afp_version < 30) { /* else it's UTF8 name */
                 memcpy(ad_entry( adp, ADEID_FINDERI ), fdType, 4 );
                 memcpy(ad_entry( adp, ADEID_FINDERI ) + 4, "pdos", 4 );
                 break;
@@ -902,7 +978,6 @@ int setfilparams(struct vol *vol,
             err = AFPERR_BITMAP;
             goto setfilparam_done;
         }
-
         bitmap = bitmap>>1;
         bit++;
     }
@@ -1180,9 +1255,6 @@ int               ibuflen, *rbuflen;
         return get_afp_errno(AFPERR_NOOBJ); 
     }
     if ( *s_path->m_name != '\0' ) {
-#if 0
-        return (path_isadir( s_path))? AFPERR_PARAM:AFPERR_BADTYPE ;
-#endif        
        path_error(s_path, AFPERR_PARAM);
     }
 
@@ -1252,7 +1324,8 @@ static int copy_fd(int dfd, int sfd)
     int     err = 0;
     char    filebuf[8192];
     
-#ifdef SENDFILE_FLAVOR_LINUX
+#if 0 /* ifdef SENDFILE_FLAVOR_LINUX */
+    /* doesn't work With 2.6 FIXME, only check for EBADFD ? */
     off_t   offset = 0;
     size_t  size;
     struct stat         st;
@@ -1326,6 +1399,11 @@ char     *src, *dst, *newname;
         goto done;
     }
 
+    if (ad_hfileno(&ads) == -1) {
+        /* no resource fork, don't create one for dst file */
+        adflags &= ~ADFLAGS_HF;
+    }
+
     if (ad_open(dst , adflags | noadouble, O_RDWR|O_CREAT|O_EXCL, 0666, &add) < 0) {
         ret_err = errno;
         ad_close( &ads, adflags );
@@ -1369,13 +1447,15 @@ char    *src, *dst, *newname;
     if (ret_err) {
         deletefile(d_vol, dst, 0);
     }
-
-    /* set dest modification date to src date */
-    if (!stat(src, &st)) {
+    else if (!stat(src, &st)) {
+        /* set dest modification date to src date */
         struct utimbuf ut;
 
        ut.actime = ut.modtime = st.st_mtime;
        utime(dst, &ut);
+       /* FIXME netatalk doesn't use resource fork file date
+        * but maybe we should set its modtime too.
+       */
     }
 
 #ifdef DEBUG
@@ -1417,6 +1497,7 @@ char              *file;
 int         checkAttrib;
 {
     struct adouble     ad;
+    struct adouble      *adp = &ad;
     int                        adflags, err = AFP_OK;
 
 #ifdef DEBUG
@@ -1438,7 +1519,8 @@ int         checkAttrib;
                 continue;
 
             case EACCES:
-                return AFPERR_ACCESS;
+                adp = NULL; /* maybe it's a file we no rw mode for us */
+                break;      /* was return AFPERR_ACCESS;*/
             case EROFS:
                 return AFPERR_VLOCK;
             default:
@@ -1450,17 +1532,31 @@ int         checkAttrib;
     /*
      * Does kFPDeleteInhibitBit (bit 8) set?
      */
-    if (checkAttrib && (adflags & ADFLAGS_HF)) {
+    if (checkAttrib) {
         u_int16_t   bshort;
+        
+        if (adp && (adflags & ADFLAGS_HF)) {
 
-        ad_getattr(&ad, &bshort);
-        if ((bshort & htons(ATTRBIT_NODELETE))) {
-            ad_close( &ad, adflags );
-            return(AFPERR_OLOCK);
+            ad_getattr(&ad, &bshort);
+            if ((bshort & htons(ATTRBIT_NODELETE))) {
+                ad_close( &ad, adflags );
+                return(AFPERR_OLOCK);
+            }
+        }
+        else if (!adp) {
+            /* was EACCESS error try to get only metadata */
+            ad_init(&ad, vol->v_adouble);  /* OK */
+            if ( ad_metadata( file , 0, &ad) == 0 ) {
+                ad_getattr(&ad, &bshort);
+                ad_close( &ad, ADFLAGS_HF );
+                if ((bshort & htons(ATTRBIT_NODELETE))) {
+                    return  AFPERR_OLOCK;
+                }
+            }
         }
     }
     
-    if ((adflags & ADFLAGS_HF) ) {
+    if (adp && (adflags & ADFLAGS_HF) ) {
         /* FIXME we have a pb here because we want to know if a file is open 
          * there's a 'priority inversion' if you can't open the ressource fork RW
          * you can delete it if it's open because you can't get a write lock.
@@ -1476,7 +1572,7 @@ int         checkAttrib;
         }
     }
 
-    if (ad_tmplock( &ad, ADEID_DFORK, ADLOCK_WR, 0, 0, 0 ) < 0) {
+    if (adp && ad_tmplock( &ad, ADEID_DFORK, ADLOCK_WR, 0, 0, 0 ) < 0) {
         err = AFPERR_BUSY;
     }
     else if (!(err = netatalk_unlink( vol->ad_path( file, ADFLAGS_HF)) ) &&
@@ -1488,7 +1584,8 @@ int         checkAttrib;
         }
 
     }
-    ad_close( &ad, adflags );  /* ad_close removes locks if any */
+    if (adp)
+        ad_close( &ad, adflags );  /* ad_close removes locks if any */
 
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "end deletefile:");
@@ -1587,11 +1684,10 @@ reenumerate_id(const struct vol *vol, char *name, cnid_t did)
     DIR             *dp;
     struct dirent   *de;
     int             ret;
-    struct stat     st;
     cnid_t         aint;
-    struct adouble  ad;
-       
-
+    struct path     path;
+    
+    memset(&path, 0, sizeof(path));
     if (vol->v_cdb == NULL) {
        return -1;
     }
@@ -1603,23 +1699,34 @@ reenumerate_id(const struct vol *vol, char *name, cnid_t did)
         if (NULL == check_dirent(vol, de->d_name))
             continue;
 
-        if ( stat(de->d_name, &st)<0 )
+        if ( stat(de->d_name, &path.st)<0 )
             continue;
        
        /* update or add to cnid */
-        aint = cnid_add(vol->v_cdb, &st, did, de->d_name, strlen(de->d_name), 0); /* ignore errors */
+        aint = cnid_add(vol->v_cdb, &path.st, did, de->d_name, strlen(de->d_name), 0); /* ignore errors */
 
 #if AD_VERSION > AD_VERSION1
-        if (aint != CNID_INVALID && !S_ISDIR(st.st_mode)) {
-            ad_init(&ad, 0);  /* OK */
-            if ( ad_open( de->d_name, ADFLAGS_HF, O_RDWR, 0, &ad ) < 0 ) {
+        if (aint != CNID_INVALID && !S_ISDIR(path.st.st_mode)) {
+            struct ofork    *of;
+            struct adouble  ad, *adp;
+
+            path.st_errno = 0;
+            path.st_valid = 1;
+            path.u_name = de->d_name;
+            
+            if (!(of = of_findname(&path))) {
+                ad_init(&ad, vol->v_adouble);
+                adp = &ad;
+            } else
+                adp = of->of_ad;
+            
+            if ( ad_open( de->d_name, ADFLAGS_HF, O_RDWR, 0, adp ) < 0 ) {
                 continue;
             }
-            else {
-                ad_setid(&ad,(vol->v_flags & AFPVOL_NODEV)?0:st.st_dev, st.st_ino, aint, did, vol->v_stamp);
-                ad_flush(&ad, ADFLAGS_HF);
-                ad_close(&ad, ADFLAGS_HF);
-           }
+            if (ad_setid(adp,(vol->v_flags & AFPVOL_NODEV)?0:path.st.st_dev, path.st.st_ino, aint, did, vol->v_stamp)) {
+               ad_flush(adp, ADFLAGS_HF);
+            }
+            ad_close(adp, ADFLAGS_HF);
         }
 #endif /* AD_VERSION > AD_VERSION1 */
 
@@ -1692,6 +1799,12 @@ retry:
     }
 
     if ( of_stat(&path) < 0 ) {
+#ifdef ESTALE
+        /* with nfs and our working directory is deleted */
+       if (errno == ESTALE) {
+           errno = ENOENT;
+       }
+#endif 
        if ( errno == ENOENT && !retry) {
            /* cnid db is out of sync, reenumerate the directory and updated ids */
            reenumerate_id(vol, ".", id);
@@ -1789,6 +1902,9 @@ int               ibuflen, *rbuflen;
         case EACCES:
         case EPERM:
             return AFPERR_ACCESS;
+#ifdef ESTALE
+       case ESTALE:
+#endif 
         case ENOENT:
             /* still try to delete the id */
             err = AFPERR_NOOBJ;
@@ -1819,6 +1935,52 @@ int              ibuflen, *rbuflen;
     return err;
 }
 
+/* ------------------------------ */
+static struct adouble *find_adouble(struct path *path, struct ofork **of, struct adouble *adp)
+{
+    int             ret;
+
+    if (path->st_errno) {
+        switch (path->st_errno) {
+        case ENOENT:
+            afp_errno = AFPERR_NOID;
+            break;
+        case EPERM:
+        case EACCES:
+            afp_errno = AFPERR_ACCESS;
+            break;
+        default:
+            afp_errno = AFPERR_PARAM;
+            break;
+        }
+        return NULL;
+    }
+    /* we use file_access both for legacy Mac perm and
+     * for unix privilege, rename will take care of folder perms
+    */
+    if (file_access(path, OPENACC_WR ) < 0) {
+        afp_errno = AFPERR_ACCESS;
+        return NULL;
+    }
+    
+    if ((*of = of_findname(path))) {
+        /* reuse struct adouble so it won't break locks */
+        adp = (*of)->of_ad;
+    }
+    else {
+        ret = ad_open( path->u_name, ADFLAGS_HF, O_RDONLY, 0, adp);
+        if ( !ret && ad_hfileno(adp) != -1 && !(adp->ad_hf.adf_flags & ( O_RDWR | O_WRONLY))) {
+            /* from AFP spec.
+             * The user must have the Read & Write privilege for both files in order to use this command.
+             */
+            ad_close(adp, ADFLAGS_HF);
+            afp_errno = AFPERR_ACCESS;
+            return NULL;
+        }
+    }
+    return adp;
+}
+
 #define APPLETEMP ".AppleTempXXXXXX"
 
 int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
@@ -1835,16 +1997,19 @@ int             ibuflen, *rbuflen;
     int                 err;
     struct adouble     ads;
     struct adouble     add;
-    struct adouble     *adsp;
-    struct adouble     *addp;
-    struct ofork       *s_of;
-    struct ofork       *d_of;
+    struct adouble     *adsp = NULL;
+    struct adouble     *addp = NULL;
+    struct ofork       *s_of = NULL;
+    struct ofork       *d_of = NULL;
     int                 crossdev;
     
     int                 slen, dlen;
     u_int32_t          sid, did;
     u_int16_t          vid;
 
+    uid_t              uid;
+    gid_t              gid;
+
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "begin afp_exchangefiles:");
 #endif /* DEBUG */
@@ -1859,7 +2024,7 @@ int               ibuflen, *rbuflen;
         return( AFPERR_PARAM);
     }
 
-    if (vol->v_flags & AFPVOL_RO)
+    if ((vol->v_flags & AFPVOL_RO))
         return AFPERR_VLOCK;
 
     /* source and destination dids */
@@ -1878,109 +2043,103 @@ int           ibuflen, *rbuflen;
     }
 
     if ( path_isadir(path) ) {
-        return( AFPERR_BADTYPE );   /* it's a dir */
+        return AFPERR_BADTYPE;   /* it's a dir */
     }
 
-    upath = path->u_name;
-    switch (path->st_errno) {
-        case 0:
-             break;
-        case ENOENT:
-            return AFPERR_NOID;
-        case EPERM:
-        case EACCES:
-            return AFPERR_ACCESS;
-        default:
-            return AFPERR_PARAM;
-    }
-    ad_init(&ads, vol->v_adouble);
-    adsp = &ads;
-    if ((s_of = of_findname(path))) {
-            /* reuse struct adouble so it won't break locks */
-            adsp = s_of->of_ad;
-    }
-    memcpy(&srcst, &path->st, sizeof(struct stat));
     /* save some stuff */
+    srcst = path->st;
     sdir = curdir;
     spath = obj->oldtmp;
     supath = obj->newtmp;
     strcpy(spath, path->m_name);
-    strcpy(supath, upath); /* this is for the cnid changing */
-    p = absupath( vol, sdir, upath);
+    strcpy(supath, path->u_name); /* this is for the cnid changing */
+    p = absupath( vol, sdir, supath);
     if (!p) {
         /* pathname too long */
         return AFPERR_PARAM ;
     }
+    
+    ad_init(&ads, vol->v_adouble);
+    if (!(adsp = find_adouble( path, &s_of, &ads))) {
+        return afp_errno;
+    }
 
+    /* ***** from here we may have resource fork open **** */
+    
     /* look for the source cnid. if it doesn't exist, don't worry about
      * it. */
     sid = cnid_lookup(vol->v_cdb, &srcst, sdir->d_did, supath,slen = strlen(supath));
 
     if (NULL == ( dir = dirlookup( vol, did )) ) {
-        return afp_errno; /* was AFPERR_PARAM */
+        err = afp_errno; /* was AFPERR_PARAM */
+        goto err_exchangefile;
     }
 
     if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
-        return get_afp_errno(AFPERR_NOOBJ); 
+        err = get_afp_errno(AFPERR_NOOBJ); 
+        goto err_exchangefile;
     }
 
     if ( path_isadir(path) ) {
-        return( AFPERR_BADTYPE );
+        err = AFPERR_BADTYPE;
+        goto err_exchangefile;
     }
 
     /* FPExchangeFiles is the only call that can return the SameObj
      * error */
-    if ((curdir == sdir) && strcmp(spath, path->m_name) == 0)
-        return AFPERR_SAMEOBJ;
-
-    switch (path->st_errno) {
-        case 0:
-             break;
-        case ENOENT:
-            return AFPERR_NOID;
-        case EPERM:
-        case EACCES:
-            return AFPERR_ACCESS;
-        default:
-            return AFPERR_PARAM;
+    if ((curdir == sdir) && strcmp(spath, path->m_name) == 0) {
+        err = AFPERR_SAMEOBJ;
+        goto err_exchangefile;
     }
+
     ad_init(&add, vol->v_adouble);
-    addp = &add;
-    if ((d_of = of_findname( path))) {
-            /* reuse struct adouble so it won't break locks */
-            addp = d_of->of_ad;
+    if (!(addp = find_adouble( path, &d_of, &add))) {
+        err = afp_errno;
+        goto err_exchangefile;
     }
-    memcpy(&destst, &path->st, sizeof(struct stat));
+    destst = path->st;
 
     /* they are not on the same device and at least one is open
+     * FIXME broken for for crossdev and adouble v2
+     * return an error 
     */
     crossdev = (srcst.st_dev != destst.st_dev);
-    if ((d_of || s_of)  && crossdev)
-        return AFPERR_MISC;
-    
-    upath = path->u_name;
+    if (/* (d_of || s_of)  && */ crossdev) {
+        err = AFPERR_MISC;
+        goto err_exchangefile;
+    }
+
     /* look for destination id. */
+    upath = path->u_name;
     did = cnid_lookup(vol->v_cdb, &destst, curdir->d_did, upath, dlen = strlen(upath));
 
     /* construct a temp name.
      * NOTE: the temp file will be in the dest file's directory. it
      * will also be inaccessible from AFP. */
     memcpy(temp, APPLETEMP, sizeof(APPLETEMP));
-    if (!mktemp(temp))
-        return AFPERR_MISC;
+    if (!mktemp(temp)) {
+        err = AFPERR_MISC;
+        goto err_exchangefile;
+    }
+    
+    if (crossdev) {
+        /* we need to close fork for copy, both s_of and d_of are null */
+       ad_close(adsp, ADFLAGS_HF);
+       ad_close(addp, ADFLAGS_HF);
+    }
 
     /* now, quickly rename the file. we error if we can't. */
-    if ((err = renamefile(vol, p, temp, temp, adsp)) < 0)
+    if ((err = renamefile(vol, p, temp, temp, adsp)) != AFP_OK)
         goto err_exchangefile;
     of_rename(vol, s_of, sdir, spath, curdir, temp);
 
     /* rename destination to source */
-    if ((err = renamefile(vol, upath, p, spath, addp)) < 0)
+    if ((err = renamefile(vol, upath, p, spath, addp)) != AFP_OK)
         goto err_src_to_tmp;
     of_rename(vol, d_of, curdir, path->m_name, sdir, spath);
 
     /* rename temp to destination */
-    if ((err = renamefile(vol, temp, upath, path->m_name, adsp)) < 0)
+    if ((err = renamefile(vol, temp, upath, path->m_name, adsp)) != AFP_OK)
         goto err_dest_to_src;
     of_rename(vol, s_of, curdir, temp, curdir, path->m_name);
 
@@ -2009,13 +2168,56 @@ int             ibuflen, *rbuflen;
         }
         goto err_temp_to_dest;
     }
+    
+    /* here we need to reopen if crossdev */
+    if (sid)
+        ad_setid(addp,(vol->v_flags & AFPVOL_NODEV)?0:destst.st_dev, destst.st_ino,  sid, sdir->d_did, vol->v_stamp);
+    if (did)
+        ad_setid(adsp,(vol->v_flags & AFPVOL_NODEV)?0:srcst.st_dev, srcst.st_ino,  did, curdir->d_did, vol->v_stamp);
+
+    /* change perms, src gets dest perm and vice versa */
+
+    uid = geteuid();
+    gid = getegid();
+    if (seteuid(0)) {
+        LOG(log_error, logtype_afpd, "seteuid failed %s", strerror(errno));
+        err = AFP_OK; /* ignore error */
+        goto err_temp_to_dest;
+    }
+
+    /*
+     * we need to exchange ACL entries as well
+     */
+    /* exchange_acls(vol, p, upath); */
+
+    path->st = srcst;
+    path->st_valid = 1;
+    path->st_errno = 0;
+    path->m_name = NULL;
+    path->u_name = upath;
+
+    setfilunixmode(vol, path, destst.st_mode);
+    setfilowner(vol, destst.st_uid, destst.st_gid, path);
+
+    path->st = destst;
+    path->st_valid = 1;
+    path->st_errno = 0;
+    path->u_name = p;
+
+    setfilunixmode(vol, path, srcst.st_mode);
+    setfilowner(vol, srcst.st_uid, srcst.st_gid, path);
+
+    if ( setegid(gid) < 0 || seteuid(uid) < 0) {
+        LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
+        exit(EXITERR_SYS);
+    }
 
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "ending afp_exchangefiles:");
 #endif /* DEBUG */
 
-    return AFP_OK;
-
+    err = AFP_OK;
+    goto err_exchangefile;
 
     /* all this stuff is so that we can unwind a failed operation
      * properly. */
@@ -2035,5 +2237,14 @@ err_src_to_tmp:
     of_rename(vol, s_of, curdir, temp, sdir, spath);
 
 err_exchangefile:
+    if ( !s_of && adsp && ad_hfileno(adsp) != -1 ) {
+       ad_flush( adsp, ADFLAGS_HF );
+       ad_close(adsp, ADFLAGS_HF);
+    }
+    if ( !d_of && addp && ad_hfileno(addp) != -1 ) {
+       ad_flush( addp, ADFLAGS_HF );
+       ad_close(addp, ADFLAGS_HF);
+    }
+
     return err;
 }