]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/file.c
"veto files" patch (Edmund Lam)
[netatalk.git] / etc / afpd / file.c
index e4f6025b1bc6f284a72fc25cab4d113ef66c43e0..a2afbab4c90fa0448d0ff60138b053e6501bbba0 100644 (file)
@@ -1,18 +1,24 @@
 /*
+ * $Id: file.c,v 1.28 2001-09-04 13:52:45 rufustfirefly Exp $
+ *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
+#endif /* HAVE_CONFIG_H */
 
 #include <stdio.h>
 #include <stdlib.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif /* HAVE_UNISTD_H */
 #include <string.h>
 #include <utime.h>
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
+#endif /* HAVE_FCNTL_H */
 #include <dirent.h>
 #include <sys/mman.h>
 #include <errno.h>
@@ -27,8 +33,9 @@
 #include <atalk/adouble.h>
 #include <atalk/afp.h>
 #include <atalk/util.h>
+#ifdef CNID_DB
 #include <atalk/cnid.h>
-
+#endif /* CNID_DB */
 #include "directory.h"
 #include "desktop.h"
 #include "volume.h"
 #include "filedir.h"
 #include "globals.h"
 
+/* check for mtab DID code */
+#ifdef DID_MTAB
+#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
  * 
  * files:
  * ioFlFndrInfo  16      ->       type    4  type field
  *                             creator    4  creator field
- *                               flags    2  finder flags: 
+ *                               flags    2  finder flags:
  *                                          alias, bundle, etc.
  *                            location    4  location in window
  *                              folder    2  window that contains file
@@ -63,24 +80,29 @@ const u_char ufinderi[] = {
     0, 0, 0, 0, 0, 0, 0, 0
 };
 
-int getfilparams(vol, bitmap, path, dir, st, buf, buflen )
-    struct vol  *vol;
-    u_int16_t  bitmap;
-    char       *path;
-    struct dir *dir;
-    struct stat        *st;
-    char       *buf;
-    int                *buflen;
+int getfilparams(struct vol *vol,
+                u_int16_t bitmap,
+                char *path, struct dir *dir, struct stat *st,
+                char *buf, int *buflen )
 {
+#ifndef USE_LASTDID
     struct stat                hst, lst, *lstp;
+#else /* USE_LASTDID */
+    struct stat                hst;
+#endif /* USE_LASTDID */
     struct adouble     ad, *adp;
     struct ofork        *of;
     struct extmap      *em;
     char               *data, *nameoff = NULL, *upath;
-    int                        bit = 0, isad = 1, aint;
+    int                        bit = 0, isad = 1;
+    u_int32_t          aint;
     u_int16_t          ashort;
     u_char              achar, fdType[4];
 
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin getfilparams:");
+#endif /* DEBUG */
+
     upath = mtoupath(vol, path);
     if ((of = of_findname(vol, curdir, path))) {
       adp = of->of_ad;
@@ -92,7 +114,7 @@ int getfilparams(vol, bitmap, path, dir, st, buf, buflen )
     if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) {
        isad = 0;
     } else if ( fstat( ad_hfileno( adp ), &hst ) < 0 ) {
-           syslog( LOG_ERR, "getfilparams fstat: %m" );
+           syslog( LOG_ERR, "getfilparams fstat: %s", strerror(errno) );
     }
 
     data = buf;
@@ -115,8 +137,8 @@ int getfilparams(vol, bitmap, path, dir, st, buf, buflen )
            break;
 
        case FILPBIT_PDID :
-           memcpy(data, &dir->d_did, sizeof( int ));
-           data += sizeof( int );
+           memcpy(data, &dir->d_did, sizeof( u_int32_t ));
+           data += sizeof( u_int32_t );
            break;
 
        case FILPBIT_CDATE :
@@ -177,16 +199,19 @@ int getfilparams(vol, bitmap, path, dir, st, buf, buflen )
            break;
 
        case FILPBIT_FNUM :
+               aint = 0;
 #if AD_VERSION > AD_VERSION1
-         /* use the CNID database if we're using AD v2 */
-           if (isad)
-             memcpy(&aint, ad_entry(adp, ADEID_DID), sizeof(aint));
-           else
-             aint = 0;
+        /* look in AD v2 header */
+        if (isad)
+                       memcpy(&aint, ad_entry(adp, ADEID_DID), sizeof(aint));
+#endif /* AD_VERSION > AD_VERSION1 */
+
+#ifdef CNID_DB
+        aint = cnid_add(vol->v_db, st, dir->d_did, upath,
+                                                       strlen(upath), aint);
+#endif /* CNID_DB */
 
-           if (!(aint = cnid_add(vol->v_db, st, dir->d_did, upath, 
-                                 strlen(upath), aint))) {
-#endif
+               if (aint == 0) {
            /*
             * What a fucking mess.  First thing:  DID and FNUMs are
             * in the same space for purposes of enumerate (and several
@@ -203,7 +228,7 @@ int getfilparams(vol, bitmap, path, dir, st, buf, buflen )
             * new algorithm:
             * due to complaints over did's being non-persistent,
             * here's the current hack to provide semi-persistent
-            * did's: 
+            * did's:
             *      1) we reserve the first bit for file ids.
             *      2) the next 7 bits are for the device.
             *      3) the remaining 24 bits are for the inode.
@@ -213,12 +238,19 @@ int getfilparams(vol, bitmap, path, dir, st, buf, buflen )
             *
             * it should be okay to use lstat to deal with symlinks.
             */
-             lstp = (lstat(upath, &lst) < 0) ? st : &lst;
+#ifdef USE_LASTDID
+             aint = htonl(( st->st_dev << 16 ) | (st->st_ino & 0x0000ffff));
+#else /* USE_LASTDID */
+             lstp = lstat(upath, &lst) < 0 ? st : &lst;
+#ifdef DID_MTAB
+             aint = htonl( afpd_st_cnid ( lstp ) );
+#else /* DID_MTAB */
              aint = htonl(CNID(lstp, 1));
-#if AD_VERSION > AD_VERSION1
-           }
-#endif
-           memcpy(data, &aint, sizeof( aint ));
+#endif /* DID_MTAB */
+#endif /* USE_LASTDID */
+       }
+
+               memcpy(data, &aint, sizeof( aint ));
            data += sizeof( aint );
            break;
 
@@ -277,7 +309,7 @@ int getfilparams(vol, bitmap, path, dir, st, buf, buflen )
              achar = '\x00';
              ashort = 0x0000;
            }
-           
+
            *data++ = achar;
            *data++ = 0;
            memcpy(data, &ashort, sizeof( ashort ));
@@ -308,6 +340,11 @@ int getfilparams(vol, bitmap, path, dir, st, buf, buflen )
         ad_close( adp, ADFLAGS_HF );
     }
     *buflen = data - buf;
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "end getfilparams:");
+#endif /* DEBUG */
+
     return( AFP_OK );
 }
 
@@ -317,16 +354,20 @@ int afp_createfile(obj, ibuf, ibuflen, rbuf, rbuflen )
     int                ibuflen, *rbuflen;
 {
     struct stat         st;
-#ifdef DROPKLUDGE
-    struct stat                sb;
-#endif DROPKLUDGE
     struct adouble     ad, *adp;
     struct vol         *vol;
     struct dir         *dir;
     struct ofork        *of;
-    char               *path, *upath, *adpath;
-    int                        creatf, did, openf, uid;
+    char               *path, *upath;
+    int                        creatf, did, openf, retvalue = AFP_OK;
     u_int16_t          vid;
+#ifdef FORCE_UIDGID
+       uidgidset               *uidgid;
+#endif /* FORCE_UIDGID */
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin afp_createfile:");
+#endif /* DEBUG */
 
     *rbuflen = 0;
     ibuf++;
@@ -365,6 +406,10 @@ int afp_createfile(obj, ibuf, ibuflen, rbuf, rbuflen )
     if (!validupath(vol, upath))
       return AFPERR_EXIST;
 
+    /* check for vetoed filenames */
+    if (veto_file(vol->v_veto, upath))
+        return AFPERR_EXIST;
+
     if ((of = of_findname(vol, curdir, path))) {
       adp = of->of_ad;
     } else {
@@ -380,12 +425,30 @@ int afp_createfile(obj, ibuf, ibuflen, rbuf, 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 */
@@ -393,43 +456,40 @@ int afp_createfile(obj, ibuf, ibuflen, rbuf, 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 );
        }
     }
 
     ad_setentrylen( adp, ADEID_NAME, strlen( path ));
-    memcpy(ad_entry( adp, ADEID_NAME ), path, 
+    memcpy(ad_entry( adp, ADEID_NAME ), path,
           ad_getentrylen( adp, ADEID_NAME ));
     ad_flush( adp, ADFLAGS_DF|ADFLAGS_HF );
     ad_close( adp, ADFLAGS_DF|ADFLAGS_HF );
 
 createfile_done:
-    setvoltime(obj, vol );
 
 #ifdef DROPKLUDGE
+    if (vol->v_flags & AFPVOL_DROPBOX) {
+         retvalue = matchfile2dirperms(upath, vol, did);
+    }
+#endif /* DROPKLUDGE */
+
+    setvoltime(obj, vol );
 
-/* The below code is an experimental, untested, incomplete kludge which 
-provides better dropbox support.  It should NOT be turned on yet unless
-you are a developer who wants to try it out and fix it. */
-    if (stat(".", &sb) == -1) 
-      syslog (LOG_ERR, "Error checking directory %s: %m", dir->d_name);
-    else {
-      uid=geteuid();
-      strcpy (adpath, "./.AppleDouble/");
-      strcat (adpath, path);
-      seteuid(0); /* Become root to change the owner of the file */
-      syslog (LOG_INFO, "Changing %s to uid=%d gid=%d", path, sb.st_uid, sb.st_gid);
-      if (chown(path, sb.st_uid, sb.st_gid)==-1)
-        syslog (LOG_ERR, "Error changing permissions: %m");
-      if (chown(adpath, sb.st_uid, sb.st_gid)==-1)
-        syslog (LOG_ERR, "Error changing AppleDouble permissions: %m");
-      syslog (LOG_INFO, "Changing afpd owner back to %d", uid);
-      seteuid(uid); /* Restore process ownership to normal */
-    }
-
-#endif DROPKLUDGE
+#ifdef DEBUG
+    syslog(LOG_INFO, "end afp_createfile");
+#endif /* DEBUG */
 
-    return AFP_OK;
+#ifdef FORCE_UIDGID
+       /* bring everything back to old euid, egid */
+       restore_uidgid ( uidgid );
+#endif /* FORCE_UIDGID */
+
+    return (retvalue);
 }
 
 int afp_setfilparams(obj, ibuf, ibuflen, rbuf, rbuflen )
@@ -443,6 +503,10 @@ int afp_setfilparams(obj, ibuf, ibuflen, rbuf, rbuflen )
     int                did, rc;
     u_int16_t  vid, bitmap;
 
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin afp_setfilparams:");
+#endif /* DEBUG */
+
     *rbuflen = 0;
     ibuf += 2;
 
@@ -477,14 +541,16 @@ int afp_setfilparams(obj, ibuf, ibuflen, rbuf, rbuflen )
        setvoltime(obj, vol );
     }
 
+#ifdef DEBUG
+    syslog(LOG_INFO, "end afp_setfilparams:");
+#endif /* DEBUG */
+
     return( rc );
 }
 
 
-int setfilparams(vol, path, bitmap, buf )
-    struct vol  *vol;
-    char       *path, *buf;
-    u_int16_t  bitmap;
+int setfilparams(struct vol *vol,
+                char *path, u_int16_t bitmap, char *buf )
 {
     struct adouble     ad, *adp;
     struct ofork        *of;
@@ -496,6 +562,16 @@ int setfilparams(vol, path, bitmap, buf )
     u_int32_t          aint;
     struct utimbuf     ut;
 
+#ifdef FORCE_UIDGID
+       uidgidset               *uidgid;
+
+       uidgid = malloc(sizeof(uidgidset));
+#endif /* FORCE_UIDGID */
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin setfilparams:");
+#endif /* DEBUG */
+
     upath = mtoupath(vol, path);
     if ((of = of_findname(vol, curdir, path))) {
       adp = of->of_ad;
@@ -503,10 +579,19 @@ int setfilparams(vol, path, bitmap, buf )
       memset(&ad, 0, sizeof(ad));
       adp = &ad;
     }
+
+#ifdef FORCE_UIDGID
+       save_uidgid ( uidgid );
+       set_uidgid ( vol );
+#endif /* FORCE_UIDGID */
+
     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;
@@ -558,7 +643,7 @@ int setfilparams(vol, path, bitmap, buf )
 
        case FILPBIT_FINFO :
            if ((memcmp( ad_entry( adp, ADEID_FINDERI ), ufinderi, 8 ) == 0)
-               && (em = getextmap( path )) && 
+               && (em = getextmap( path )) &&
                (memcmp(buf, em->em_type, sizeof( em->em_type )) == 0) &&
                (memcmp(buf + 4, em->em_creator,
                        sizeof( em->em_creator )) == 0)) {
@@ -624,7 +709,17 @@ setfilparam_done:
     if (isad) {
       ad_flush( adp, ADFLAGS_HF );
       ad_close( adp, ADFLAGS_HF );
+
+#ifdef FORCE_UIDGID
+       restore_uidgid ( uidgid );
+#endif /* FORCE_UIDGID */
+
     }
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "end setfilparams:");
+#endif /* DEBUG */
+
     return err;
 }
 
@@ -651,6 +746,10 @@ int renamefile(src, dst, newname, noadouble )
 
     /* existence check moved to afp_moveandrename */
 
+#ifdef DEBUG
+    syslog (LOG_INFO, "begin renamefile:");
+#endif /* DEBUG */
+
     if ( rename( src, dst ) < 0 ) {
        switch ( errno ) {
        case ENOENT :
@@ -718,6 +817,10 @@ rename_retry:
     ad_flush( &ad, ADFLAGS_HF );
     ad_close( &ad, ADFLAGS_HF );
 
+#ifdef DEBUG
+    syslog (LOG_INFO, "end renamefile:");
+#endif /* DEBUG */
+
     return( AFP_OK );
 }
 
@@ -730,9 +833,13 @@ int afp_copyfile(obj, ibuf, ibuflen, rbuf, rbuflen )
     struct dir *dir;
     char       *newname, *path, *p;
     u_int32_t  sdid, ddid;
-    int                plen, err;
+    int                plen, err, retvalue = AFP_OK;
     u_int16_t  svid, dvid;
 
+#ifdef DEBUG
+    syslog (LOG_INFO, "begin afp_copyfile:");
+#endif /* DEBUG */
+
     *rbuflen = 0;
     ibuf += 2;
 
@@ -806,7 +913,18 @@ int afp_copyfile(obj, ibuf, ibuflen, rbuf, rbuflen )
     }
 
     setvoltime(obj, vol );
-    return( AFP_OK );
+
+#ifdef DROPKLUDGE
+    if (vol->v_flags & AFPVOL_DROPBOX) {
+         retvalue=matchfile2dirperms(newname, vol, sdid);
+    }
+#endif /* DROPKLUDGE */
+
+#ifdef DEBUG
+    syslog (LOG_INFO, "end afp_copyfile:");
+#endif /* DEBUG */
+
+    return( retvalue );
 }
 
 
@@ -815,6 +933,10 @@ static __inline__ int copy_all(const int dfd, const void *buf,
 {
   ssize_t cc;
 
+#ifdef DEBUG
+  syslog(LOG_INFO, "begin copy_all:");
+#endif /* DEBUG */
+
   while (buflen > 0) {
     if ((cc = write(dfd, buf, buflen)) < 0) {
       switch (errno) {
@@ -833,7 +955,11 @@ static __inline__ int copy_all(const int dfd, const void *buf,
     buflen -= cc;
   }
 
-  return 0;
+#ifdef DEBUG
+  syslog(LOG_INFO, "end copy_all:");
+#endif /* DEBUG */
+
+  return AFP_OK;
 }
 
 /* XXX: this needs to use ad_open and ad_lock. so, we need to
@@ -848,6 +974,9 @@ int copyfile(src, dst, newname, noadouble )
     int                        sfd, dfd, len, err = AFP_OK;
     ssize_t             cc;
 
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin copyfile:");
+#endif /* DEBUG */
 
     if (newname) { 
       if ((sfd = open( ad_path( src, ADFLAGS_HF ), O_RDONLY, 0 )) < 0 ) {
@@ -894,10 +1023,10 @@ int copyfile(src, dst, newname, noadouble )
          }
          goto copyheader_done;
        }
-#endif
+#endif /* SENDFILE_FLAVOR_LINUX */
        while (1) {
          if ((cc = read(sfd, filebuf, sizeof(filebuf))) < 0) {
-           if (errno == EINTR) 
+           if (errno == EINTR)
              continue;
            err = AFPERR_PARAM;
            break;
@@ -958,7 +1087,7 @@ copyheader_done:
       }
       goto copydata_done;
     }
-#endif
+#endif /* SENDFILE_FLAVOR_LINUX */
 
     while (1) {
       if ((cc = read( sfd, filebuf, sizeof( filebuf ))) < 0) {
@@ -982,7 +1111,7 @@ copydata_done:
       unlink(dst);
       return err;
     }
-    
+
     if (newname) {
       memset(&ad, 0, sizeof(ad));
       if ( ad_open( dst, noadouble | ADFLAGS_HF, O_RDWR|O_CREAT,
@@ -1006,6 +1135,10 @@ copydata_done:
       ad_close( &ad, ADFLAGS_HF );
     }
     
+#ifdef DEBUG
+    syslog(LOG_INFO, "end copyfile:");
+#endif /* DEBUG */
+
     return( AFP_OK );
 }
 
@@ -1015,44 +1148,71 @@ int deletefile( file )
 {
     struct adouble     ad;
     int                        adflags, err = AFP_OK;
-
-    /* try to open both at once */
-    adflags = ADFLAGS_DF|ADFLAGS_HF;
-    memset(&ad, 0, sizeof(ad));
-    if ( ad_open( file, adflags, O_RDWR, 0, &ad ) < 0 ) {
-         switch (errno) {
-         case ENOENT:
-           adflags = ADFLAGS_DF;
-           /* that failed. now try to open just the data fork */
-           memset(&ad, 0, sizeof(ad));
-           if ( ad_open( file, adflags, O_RDWR, 0, &ad ) < 0 ) {
+    int                        locktype = ADLOCK_WR;
+    int                        openmode = O_RDWR;
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin deletefile:");
+#endif /* DEBUG */
+
+    while(1) {
+       /*
+        * If can't open read/write then try again read-only.  If it's open
+        * read-only, we must do a read lock instead of a write lock.
+        */
+       /* try to open both at once */
+       adflags = ADFLAGS_DF|ADFLAGS_HF;
+       memset(&ad, 0, sizeof(ad));
+       if ( ad_open( file, adflags, openmode, 0, &ad ) < 0 ) {
              switch (errno) {
              case ENOENT:
-               return AFPERR_NOOBJ;
+               adflags = ADFLAGS_DF;
+               /* that failed. now try to open just the data fork */
+               memset(&ad, 0, sizeof(ad));
+               if ( ad_open( file, adflags, openmode, 0, &ad ) < 0 ) {
+                 switch (errno) {
+                 case ENOENT:
+                   return AFPERR_NOOBJ;
+                 case EACCES:
+                   if(openmode == O_RDWR) {
+                       openmode = O_RDONLY;
+                       locktype = ADLOCK_RD;
+                       continue;
+                   } else {
+                       return AFPERR_ACCESS;
+                   }
+                 case EROFS:
+                   return AFPERR_VLOCK;
+                 default:
+                   return AFPERR_PARAM;
+                 }
+               }
+               break;
+
              case EACCES:
-               return AFPERR_ACCESS;
+               if(openmode == O_RDWR) {
+                   openmode = O_RDONLY;
+                   locktype = ADLOCK_RD;
+                   continue;
+               } else {
+                   return AFPERR_ACCESS;
+               }
+             case EROFS:
+               return AFPERR_VLOCK;
              default:
-               return AFPERR_PARAM;
+               return( AFPERR_PARAM );
              }
-           }
-           break;
-
-         case EACCES:
-           return( AFPERR_ACCESS );
-         case EROFS:
-           return AFPERR_VLOCK;
-         default:
-           return( AFPERR_PARAM );
-         }
+       }
+       break;  /* from the while */
     }
 
     if ((adflags & ADFLAGS_HF) &&
-       (ad_tmplock(&ad, ADEID_RFORK, ADLOCK_WR, 0, 0) < 0 )) {
+       (ad_tmplock(&ad, ADEID_RFORK, locktype, 0, 0) < 0 )) {
       ad_close( &ad, adflags );
       return( AFPERR_BUSY );
     }
 
-    if (ad_tmplock( &ad, ADEID_DFORK, ADLOCK_WR, 0, 0 ) < 0) {
+    if (ad_tmplock( &ad, ADEID_DFORK, locktype, 0, 0 ) < 0) {
       err = AFPERR_BUSY;
       goto delete_unlock;
     }
@@ -1096,11 +1256,16 @@ delete_unlock:
       ad_tmplock(&ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
     ad_tmplock(&ad, ADEID_DFORK, ADLOCK_CLR, 0, 0);
     ad_close( &ad, adflags );
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "end deletefile:");
+#endif /* DEBUG */
+
     return err;
 }
 
 
-#if AD_VERSION > AD_VERSION1
+#ifdef CNID_DB
 /* return a file id */
 int afp_createid(obj, ibuf, ibuflen, rbuf, rbuflen )
     AFPObj      *obj;
@@ -1115,7 +1280,10 @@ int afp_createid(obj, ibuf, ibuflen, rbuf, rbuflen )
     int                 len;
     cnid_t             did, id;
     u_short            vid;
-    
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin afp_createid:");
+#endif /* DEBUG */
 
     *rbuflen = 0;
     ibuf += 2;
@@ -1164,13 +1332,13 @@ int afp_createid(obj, ibuf, ibuflen, rbuf, rbuflen )
       return AFPERR_EXISTID;
     }
 
+#if AD_VERSION > AD_VERSION1
     memset(&ad, 0, sizeof(ad));
-    if (ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, &ad ) < 0)
-      id = 0;
-    else {
+    if (ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, &ad ) >= 0) {
       memcpy(&id, ad_entry(&ad, ADEID_DID), sizeof(id));
-      ad_close(upath, ADFLAGS_HF);
+      ad_close(&ad, ADFLAGS_HF);
     }
+#endif /* AD_VERSION > AD_VERSION1 */
 
     if (id = cnid_add(vol->v_db, &st, did, upath, len, id)) {
       memcpy(rbuf, &id, sizeof(id));
@@ -1178,6 +1346,10 @@ int afp_createid(obj, ibuf, ibuflen, rbuf, rbuflen )
       return AFP_OK;
     }
 
+#ifdef DEBUG
+    syslog(LOG_INFO, "ending afp_createid...:");
+#endif /* DEBUG */
+
     switch (errno) {
     case EROFS:
       return AFPERR_VLOCK;
@@ -1205,7 +1377,10 @@ int afp_resolveid(obj, ibuf, ibuflen, rbuf, rbuflen )
     int                 err, buflen;
     cnid_t             id;
     u_int16_t          vid, bitmap;
-    
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin afp_resolveid:");
+#endif /* DEBUG */
 
     *rbuflen = 0;
     ibuf += 2;
@@ -1253,6 +1428,11 @@ int afp_resolveid(obj, ibuf, ibuflen, rbuf, rbuflen )
 
     *rbuflen = buflen + sizeof(bitmap);
     memcpy(rbuf, ibuf, sizeof(bitmap));
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "end afp_resolveid:");
+#endif /* DEBUG */
+
     return AFP_OK;
 }
 
@@ -1268,7 +1448,10 @@ int afp_deleteid(obj, ibuf, ibuflen, rbuf, rbuflen )
     int                 err;
     cnid_t             id;
     u_short            vid;
-    
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin afp_deleteid:");
+#endif /* DEBUG */
 
     *rbuflen = 0;
     ibuf += 2;
@@ -1325,11 +1508,16 @@ int afp_deleteid(obj, ibuf, ibuflen, rbuf, rbuflen )
       }
     }
 
+#ifdef DEBUG
+    syslog(LOG_INFO, "end afp_deleteid:");
+#endif /* DEBUG */
+
     return err;
 }
-#endif
+#endif /* CNID_DB */
 
 #define APPLETEMP ".AppleTempXXXXXX"
+
 int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
     AFPObj      *obj;
     char       *ibuf, *rbuf;
@@ -1341,12 +1529,15 @@ int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
     char               *spath, temp[17], *path, *p;
     char                *supath, *upath;
     int                 err;
-#if AD_VERSION > AD_VERSION1
+#ifdef CNID_DB
     int                 slen, dlen;
-#endif
-    cnid_t             sid, did;
+#endif /* CNID_DB */
+    u_int32_t          sid, did;
     u_int16_t          vid;
-    
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "begin afp_exchangefiles:");
+#endif /* DEBUG */
 
     *rbuflen = 0;
     ibuf += 2;
@@ -1403,10 +1594,10 @@ int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
 
     /* look for the source cnid. if it doesn't exist, don't worry about
      * it. */
-#if AD_VERSION > AD_VERSION1
-    sid = cnid_lookup(vol->v_db, &srcst, sdir->d_did, supath, 
+#ifdef CNID_DB
+    sid = cnid_lookup(vol->v_db, &srcst, sdir->d_did, supath,
                      slen = strlen(supath));
-#endif
+#endif /* CNID_DB */
 
     if (( dir = dirsearch( vol, did )) == NULL ) {
        return( AFPERR_PARAM );
@@ -1438,13 +1629,13 @@ int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
       }
     }
 
-#if AD_VERSION > AD_VERSION1
+#ifdef CNID_DB
     /* look for destination id. */
-    did = cnid_lookup(vol->v_db, &destst, curdir->d_did, upath, 
+    did = cnid_lookup(vol->v_db, &destst, curdir->d_did, upath,
                      dlen = strlen(upath));
-#endif
+#endif /* CNID_DB */
 
-    /* construct a temp name. 
+    /* 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));
@@ -1457,7 +1648,7 @@ int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
     of_rename(vol, sdir, spath, curdir, temp);
 
     /* rename destination to source */
-    if ((err = renamefile(path, p, spath, vol_noadouble(vol))) < 0) 
+    if ((err = renamefile(path, p, spath, vol_noadouble(vol))) < 0)
       goto err_src_to_tmp;
     of_rename(vol, curdir, path, sdir, spath);
 
@@ -1466,9 +1657,9 @@ int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
       goto err_dest_to_src;
     of_rename(vol, curdir, temp, curdir, path);
     
-#if AD_VERSION > AD_VERSION1
+#ifdef CNID_DB
     /* id's need switching. src -> dest and dest -> src. */
-    if (sid && (cnid_update(vol->v_db, sid, &destst, curdir->d_did, 
+    if (sid && (cnid_update(vol->v_db, sid, &destst, curdir->d_did,
                            upath, dlen) < 0)) {
       switch (errno) {
       case EPERM:
@@ -1494,11 +1685,16 @@ int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
        cnid_update(vol->v_db, sid, &srcst, sdir->d_did, supath, slen);
       goto err_temp_to_dest;
     }
-#endif
+#endif /* CNID_DB */
+
+#ifdef DEBUG
+    syslog(LOG_INFO, "ending afp_exchangefiles:");
+#endif /* DEBUG */
+
     return AFP_OK;
 
 
-    /* all this stuff is so that we can unwind a failed operation 
+    /* all this stuff is so that we can unwind a failed operation
      * properly. */
 err_temp_to_dest:
     /* rename dest to temp */