]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/filedir.c
better error code for calls on a not existing file
[netatalk.git] / etc / afpd / filedir.c
index 3913b9e809fea7a765ec6e6d65d118024285226e..d2400262db06a3ce4ea8d83e913b9bfcef1b6036 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: filedir.c,v 1.33 2002-10-11 14:18:31 didg Exp $
+ * $Id: filedir.c,v 1.45 2003-04-20 06:53:40 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -56,6 +56,7 @@ char *strchr (), *strrchr ();
 #include "filedir.h"
 #include "unix.h"
 
+#ifdef DROPKLUDGE
 int matchfile2dirperms(upath, vol, did)
 /* Since it's kinda' big; I decided against an
 inline function */
@@ -134,7 +135,7 @@ more information */
 #endif /* DEBUG */
     return ret;
 }
-
+#endif
 
 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
 AFPObj      *obj;
@@ -158,15 +159,15 @@ int               ibuflen, *rbuflen;
 
     memcpy( &vid, ibuf, sizeof( vid ));
     ibuf += sizeof( vid );
-    if (( vol = getvolbyvid( vid )) == NULL ) {
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
         return( AFPERR_PARAM );
     }
 
     memcpy( &did, ibuf, sizeof( did ));
     ibuf += sizeof( did );
 
-    if (( dir = dirlookup( vol, did )) == NULL ) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
+        return afp_errno;
     }
 
     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
@@ -176,14 +177,16 @@ int               ibuflen, *rbuflen;
     dbitmap = ntohs( dbitmap );
     ibuf += sizeof( dbitmap );
 
-    if (( s_path = cname( vol, dir, &ibuf )) == NULL) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     st   = &s_path->st;
     if (!s_path->st_valid) {
         /* it's a dir and it should be there
-         * because we chdir in it in cname
+         * because we chdir in it in cname or
+         * it's curdir (maybe deleted, but then we can't know)
+         * 
          */
         of_stat(s_path);
     }
@@ -194,7 +197,11 @@ int                ibuflen, *rbuflen;
     buflen = 0;
     if (S_ISDIR(st->st_mode)) {
         if (dbitmap) {
-            ret = getdirparams(vol, dbitmap, s_path, curdir,
+            dir = s_path->dir;
+            if (!dir) 
+                return AFPERR_NOOBJ;
+
+            ret = getdirparams(vol, dbitmap, s_path, dir,
                                  rbuf + 3 * sizeof( u_int16_t ), &buflen );
             if (ret != AFP_OK )
                 return( ret );
@@ -202,8 +209,8 @@ int         ibuflen, *rbuflen;
         /* this is a directory */
         *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
     } else {
-        if (fbitmap && ret = getfilparams(vol, fbitmap, s_path, curdir, 
-                                            rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
+        if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir, 
+                                            rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
             return( ret );
         }
         /* this is a file */
@@ -246,7 +253,7 @@ int         ibuflen, *rbuflen;
     memcpy( &vid, ibuf, sizeof(vid));
     ibuf += sizeof( vid );
 
-    if (( vol = getvolbyvid( vid )) == NULL ) {
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
         return( AFPERR_PARAM );
     }
 
@@ -256,16 +263,16 @@ int               ibuflen, *rbuflen;
     memcpy( &did, ibuf, sizeof( did));
     ibuf += sizeof( did);
 
-    if (( dir = dirlookup( vol, did )) == NULL ) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
+       return afp_errno;    
     }
 
     memcpy( &bitmap, ibuf, sizeof( bitmap ));
     bitmap = ntohs( bitmap );
     ibuf += sizeof( bitmap );
 
-    if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( path = cname( vol, dir, &ibuf ))) {
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     st   = &path->st;
@@ -354,10 +361,16 @@ int         isdir;
     
     if (!isdir) {
 #ifdef CNID_DB
-        p = mtoupath(vol, oldname);
+        p = mtoupath(vol, oldname, utf8_encoding());
+        if (!p) { 
+            return AFPERR_PARAM; /* can't convert */
+        }
         id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
 #endif /* CNID_DB */
         p = ctoupath( vol, sdir, oldname );
+        if (!p) { 
+            return AFPERR_PARAM; /* pathname too long */
+        }
         path.st_valid = 0;
         path.u_name = p;
         if ((opened = of_findname(&path))) {
@@ -370,6 +383,9 @@ int         isdir;
         id = sdir->d_did; /* we already have the CNID */
 #endif /* CNID_DB */
         p = ctoupath( vol, sdir->d_parent, oldname );
+        if (!p) {
+            return AFPERR_PARAM;
+        }
         adflags = ADFLAGS_DIR;
     }
     /*
@@ -387,7 +403,9 @@ int         isdir;
             return(AFPERR_OLOCK);
     }
 
-    upath = mtoupath(vol, newname);
+    if (NULL == (upath = mtoupath(vol, newname, utf8_encoding()))){ 
+        return AFPERR_PARAM;
+    }
     path.u_name = upath;
     st = &path.st;    
     if (0 != (rc = check_name(vol, upath))) {
@@ -444,7 +462,7 @@ int         ibuflen, *rbuflen;
     char        *oldname, *newname;
     struct path *path;
     u_int32_t  did;
-    size_t      plen;
+    int         plen;
     u_int16_t  vid;
     int         isdir = 0;
     int         rc;
@@ -457,7 +475,7 @@ int         ibuflen, *rbuflen;
 
     memcpy( &vid, ibuf, sizeof( vid ));
     ibuf += sizeof( vid );
-    if (( vol = getvolbyvid( vid )) == NULL ) {
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
         return( AFPERR_PARAM );
     }
 
@@ -466,20 +484,25 @@ int               ibuflen, *rbuflen;
 
     memcpy( &did, ibuf, sizeof( did ));
     ibuf += sizeof( did );
-    if (( sdir = dirlookup( vol, did )) == NULL ) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( sdir = dirlookup( vol, did )) ) {
+       return afp_errno;    
     }
 
     /* source pathname */
-    if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     sdir = curdir;
     newname = obj->newtmp;
     oldname = obj->oldtmp;
+    isdir = path_isadir(path);
     if ( *path->m_name != '\0' ) {
         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
+        if (isdir) {
+            /* curdir parent dir, need to move sdir back */
+            sdir = path->dir;
+        }
     }
     else {
         if ( sdir->d_parent == NULL ) { /* root directory */
@@ -487,25 +510,17 @@ int               ibuflen, *rbuflen;
         }
         /* move to destination dir */
         if ( movecwd( vol, sdir->d_parent ) < 0 ) {
-            return( AFPERR_NOOBJ );
+            return afp_errno;
         }
-        isdir = 1;
         strcpy(oldname, sdir->d_m_name);
     }
 
     /* another place where we know about the path type */
-    if ( *ibuf++ != 2 ) {
+    if ((plen = copy_path_name(newname, ibuf)) < 0) {
         return( AFPERR_PARAM );
     }
 
-    if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
-        strncpy( newname, ibuf, plen );
-        newname[ plen ] = '\0';
-        if (strlen(newname) != plen) {
-            return( AFPERR_PARAM );
-        }
-    }
-    else {
+    if (!plen) {
         return AFP_OK; /* newname == oldname same dir */
     }
     
@@ -544,7 +559,7 @@ int         ibuflen, *rbuflen;
 
     memcpy( &vid, ibuf, sizeof( vid ));
     ibuf += sizeof( vid );
-    if (( vol = getvolbyvid( vid )) == NULL ) {
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
         return( AFPERR_PARAM );
     }
 
@@ -553,26 +568,29 @@ int               ibuflen, *rbuflen;
 
     memcpy( &did, ibuf, sizeof( did ));
     ibuf += sizeof( int );
-    if (( dir = dirlookup( vol, did )) == NULL ) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
+       return afp_errno;    
     }
 
-    if (( s_path = cname( vol, dir, &ibuf )) == NULL ) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     upath = s_path->u_name;
-    if ( *s_path->m_name == '\0' ) {
-        rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
+    if ( path_isadir( s_path) ) {
+       if (*s_path->m_name != '\0') {
+           rc = AFPERR_ACCESS;
+       }
+       else {
+            rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
+        }
     } else if (of_findname(s_path)) {
         rc = AFPERR_BUSY;
-    } else if ((rc = deletefile( upath, 1)) == AFP_OK) {
-#ifdef CNID_DB /* get rid of entry */
-        cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
-        cnid_delete(vol->v_db, id);
-#endif /* CNID_DB */
+    } else {
+        rc = deletefile(vol, upath, 1);
     }
     if ( rc == AFP_OK ) {
+       curdir->offcnt--;
         setvoltime(obj, vol );
     }
 
@@ -592,20 +610,32 @@ char      *u;
     char       *p;
     int                len;
 
+    if (u == NULL)
+        return NULL;
+        
     p = path + sizeof( path ) - 1;
     *p = '\0';
     len = strlen( u );
     p -= len;
     strncpy( p, u, len );
-    for ( d = dir; d->d_parent; d = d->d_parent ) {
-        *--p = '/';
+    if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
         u = d->d_u_name;
         len = strlen( u );
+        if (p -len -1 < path) {
+            /* FIXME 
+               rather rare so LOG error and/or client message ?
+            */
+            return NULL;
+        }
+        *--p = '/';
         p -= len;
         strncpy( p, u, len );
     }
-    *--p = '/';
     len = strlen( vol->v_path );
+    if (p -len -1 < path) {
+        return NULL;
+    }
+    *--p = '/';
     p -= len;
     strncpy( p, vol->v_path, len );
 
@@ -617,7 +647,7 @@ const struct vol    *vol;
 struct dir     *dir;
 char   *name;
 {
-    return absupath(vol, dir, mtoupath(vol, name));
+    return absupath(vol, dir, mtoupath(vol, name, utf8_encoding()));
 }
 
 /* ------------------------- */
@@ -628,11 +658,11 @@ int               ibuflen, *rbuflen;
 {
     struct vol *vol;
     struct dir *sdir, *ddir;
-    int         isdir = 0;
+    int         isdir;
     char       *oldname, *newname;
     struct path *path;
     int                did;
-    size_t      plen;
+    int         plen;
     u_int16_t  vid;
     int         rc;
 #ifdef DROPKLUDGE
@@ -648,7 +678,7 @@ int         ibuflen, *rbuflen;
 
     memcpy( &vid, ibuf, sizeof( vid ));
     ibuf += sizeof( vid );
-    if (( vol = getvolbyvid( vid )) == NULL ) {
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
         return( AFPERR_PARAM );
     }
 
@@ -658,60 +688,62 @@ int               ibuflen, *rbuflen;
     /* source did followed by dest did */
     memcpy( &did, ibuf, sizeof( did ));
     ibuf += sizeof( int );
-    if (( sdir = dirlookup( vol, did )) == NULL ) {
-        return( AFPERR_PARAM );
+    if (NULL == ( sdir = dirlookup( vol, did )) ) {
+        return afp_errno; /* was AFPERR_PARAM */
     }
 
     memcpy( &did, ibuf, sizeof( did ));
     ibuf += sizeof( int );
 
     /* source pathname */
-    if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
-        return( AFPERR_NOOBJ );
+    if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     sdir = curdir;
     newname = obj->newtmp;
     oldname = obj->oldtmp;
+    
+    isdir = path_isadir(path);
     if ( *path->m_name != '\0' ) {
-        /* not a directory */
+        if (isdir) {
+            sdir = path->dir;
+       }
         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
     } else {
-        isdir = 1;
         strcpy(oldname, sdir->d_m_name);
     }
 
     /* get the destination directory */
-    if (( ddir = dirlookup( vol, did )) == NULL ) {
-        return( AFPERR_PARAM );
+    if (NULL == ( ddir = dirlookup( vol, did )) ) {
+        return afp_errno; /*  was AFPERR_PARAM */
     }
     if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
         return( AFPERR_NOOBJ );
     }
     if ( *path->m_name != '\0' ) {
-        return( AFPERR_BADTYPE );
+        return path_error(path, AFPERR_NOOBJ);
     }
 
     /* one more place where we know about path type */
-    if ( *ibuf++ != 2 ) {
+    if ((plen = copy_path_name(newname, ibuf)) < 0) {
         return( AFPERR_PARAM );
     }
 
-    if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
-        strncpy( newname, ibuf, plen );
-        newname[ plen ] = '\0';
-        if (strlen(newname) != plen) {
-            return( AFPERR_PARAM );
-        }
-    }
-    else {
+    if (!plen) {
         strcpy(newname, oldname);
     }
-    
+
     rc = moveandrename(vol, sdir, oldname, newname, isdir);
 
     if ( rc == AFP_OK ) {
-        char *upath = mtoupath(vol, newname);
+        char *upath = mtoupath(vol, newname, utf8_encoding());
+        
+        if (NULL == upath) {
+            return AFPERR_PARAM;
+        }
+        curdir->offcnt++;
+        sdir->offcnt--;
 #ifdef DROPKLUDGE
         if (vol->v_flags & AFPVOL_DROPBOX) {
             if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {