X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=etc%2Fafpd%2Fappl.c;h=3189ab5fa8f81a9bb8ff37e12a88b1b10fb0b41e;hb=b4bf09bd448b2a32b2a0f21f442634e99f57a082;hp=c5d9ea06c817d360daa367ee133a68c6354a5950;hpb=458fcac4e6aee7eb54ba744f57169f70cce12505;p=netatalk.git diff --git a/etc/afpd/appl.c b/etc/afpd/appl.c index c5d9ea06..3189ab5f 100644 --- a/etc/afpd/appl.c +++ b/etc/afpd/appl.c @@ -1,25 +1,23 @@ /* + * $Id: appl.c,v 1.16 2008-12-03 18:35:44 didg 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 #include #include #include -#include -#include -#include -#include + #include -#include +#include #include -#include #include #include @@ -29,56 +27,58 @@ #include "file.h" #include "desktop.h" -static struct savedt sa = { { 0, 0, 0, 0 }, -1, 0 }; +static struct savedt sa = { { 0, 0, 0, 0 }, -1, 0, 0}; -static __inline__ int pathcmp( p, plen, q, qlen ) - char *p; - int plen; - char *q; - int qlen; +static int pathcmp( p, plen, q, qlen ) +char *p; +int plen; +char *q; +int qlen; { return (( plen == qlen && memcmp( p, q, plen ) == 0 ) ? 0 : 1 ); } static int applopen( vol, creator, flags, mode ) - struct vol *vol; - u_char creator[ 4 ]; +struct vol *vol; +u_char creator[ 4 ]; +int flags; +int mode; { char *dtf, *adt, *adts; if ( sa.sdt_fd != -1 ) { - if ( !(flags & ( O_RDWR | O_WRONLY )) && - memcmp( sa.sdt_creator, creator, sizeof( CreatorType )) == 0 && - sa.sdt_vid == vol->v_vid ) { - return( AFP_OK ); - } - close( sa.sdt_fd ); - sa.sdt_fd = -1; + if ( !(flags & ( O_RDWR | O_WRONLY )) && + memcmp( sa.sdt_creator, creator, sizeof( CreatorType )) == 0 && + sa.sdt_vid == vol->v_vid ) { + return( AFP_OK ); + } + close( sa.sdt_fd ); + sa.sdt_fd = -1; } dtf = dtfile( vol, creator, ".appl" ); if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) { - if ( errno == ENOENT && ( flags & O_CREAT )) { - if (( adts = strrchr( dtf, '/' )) == NULL ) { - return( AFPERR_PARAM ); - } - *adts = '\0'; - if (( adt = strrchr( dtf, '/' )) == NULL ) { - return( AFPERR_PARAM ); - } - *adt = '\0'; - (void) ad_mkdir( dtf, DIRBITS | 0777 ); - *adt = '/'; - (void) ad_mkdir( dtf, DIRBITS | 0777 ); - *adts = '/'; - - if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) { - return( AFPERR_PARAM ); - } - } else { - return( AFPERR_PARAM ); - } + if ( errno == ENOENT && ( flags & O_CREAT )) { + if (( adts = strrchr( dtf, '/' )) == NULL ) { + return( AFPERR_PARAM ); + } + *adts = '\0'; + if (( adt = strrchr( dtf, '/' )) == NULL ) { + return( AFPERR_PARAM ); + } + *adt = '\0'; + (void) ad_mkdir( dtf, DIRBITS | 0777 ); + *adt = '/'; + (void) ad_mkdir( dtf, DIRBITS | 0777 ); + *adts = '/'; + + if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) { + return( AFPERR_PARAM ); + } + } else { + return( AFPERR_PARAM ); + } } memcpy( sa.sdt_creator, creator, sizeof( CreatorType )); sa.sdt_vid = vol->v_vid; @@ -90,10 +90,10 @@ static int applopen( vol, creator, flags, mode ) * copy appls to new file, deleting any matching (old) appl entries */ static int copyapplfile( sfd, dfd, mpath, mplen ) - int sfd; - int dfd; - char *mpath; - u_short mplen; +int sfd; +int dfd; +char *mpath; +u_short mplen; { int cc; char *p; @@ -102,20 +102,20 @@ static int copyapplfile( sfd, dfd, mpath, mplen ) char buf[ MAXPATHLEN ]; while (( cc = read( sfd, buf, sizeof(appltag) + sizeof( u_short ))) > 0 ) { - p = buf + sizeof(appltag); - memcpy( &len, p, sizeof(len)); - len = ntohs( len ); - p += sizeof( len ); - if (( cc = read( sa.sdt_fd, p, len )) < len ) { - break; - } - if ( pathcmp( mpath, mplen, p, len ) != 0 ) { - p += len; - if ( write( dfd, buf, p - buf ) != p - buf ) { - cc = -1; - break; - } - } + p = buf + sizeof(appltag); + memcpy( &len, p, sizeof(len)); + len = ntohs( len ); + p += sizeof( len ); + if (( cc = read( sa.sdt_fd, p, len )) < len ) { + break; + } + if ( pathcmp( mpath, mplen, p, len ) != 0 ) { + p += len; + if ( write( dfd, buf, p - buf ) != p - buf ) { + cc = -1; + break; + } + } } return( cc ); } @@ -128,43 +128,49 @@ static int copyapplfile( sfd, dfd, mpath, mplen ) * but uses upaths instead of mac format paths. * * The new way: dir and path refer to an app, path is a mac format - * pathname. makemacpath() builds a cname. + * pathname. makemacpath() builds a cname. (zero is a path separator + * and it's not \0 terminated). * * See afp_getappl() for the backward compatiblity code. */ static char * makemacpath( mpath, mpathlen, dir, path ) - char *mpath; - int mpathlen; - struct dir *dir; - char *path; +char *mpath; +int mpathlen; +struct dir *dir; +char *path; { char *p; p = mpath + mpathlen; p -= strlen( path ); - strncpy( p, path, strlen( path )); + memcpy( p, path, strlen( path )); while ( dir->d_parent != NULL ) { - p -= strlen( dir->d_name ) + 1; - strcpy( p, dir->d_name ); - dir = dir->d_parent; - } - return( p ); + p -= strlen( dir->d_m_name ) + 1; + if (p < mpath) { + /* FIXME: pathname too long */ + return NULL; + } + strcpy( p, dir->d_m_name ); + dir = dir->d_parent; + } + return( p ); } int afp_addappl(obj, ibuf, ibuflen, rbuf, rbuflen ) - AFPObj *obj; - char *ibuf, *rbuf; - int ibuflen, *rbuflen; +AFPObj *obj; +char *ibuf, *rbuf _U_; +int ibuflen _U_, *rbuflen; { struct vol *vol; struct dir *dir; - int tfd, cc; + int tfd, cc; u_int32_t did; u_int16_t vid, mplen; - char *path, *dtf, *p, *mp; + struct path *path; + char *dtf, *p, *mp; u_char creator[ 4 ]; u_char appltag[ 4 ]; char *mpath, *tempfile; @@ -174,14 +180,14 @@ int afp_addappl(obj, ibuf, ibuflen, rbuf, rbuflen ) memcpy( &vid, ibuf, sizeof( vid )); ibuf += sizeof( vid ); - if (( vol = getvolbyvid( vid )) == NULL ) { - return( AFPERR_PARAM ); + if (NULL == ( vol = getvolbyvid( vid ))) { + return( AFPERR_PARAM ); } memcpy( &did, ibuf, sizeof( did )); ibuf += sizeof( did ); - if (( dir = dirsearch( vol, did )) == NULL ) { - return( AFPERR_NOOBJ ); + if (NULL == ( dir = dirlookup( vol, did )) ) { + return afp_errno; } memcpy( creator, ibuf, sizeof( creator )); @@ -190,27 +196,30 @@ int afp_addappl(obj, ibuf, ibuflen, rbuf, rbuflen ) memcpy( appltag, ibuf, sizeof( appltag )); ibuf += sizeof( appltag ); - if (( path = cname( vol, dir, &ibuf )) == NULL ) { - return( AFPERR_NOOBJ ); + if (NULL == ( path = cname( vol, dir, &ibuf )) ) { + return get_afp_errno(AFPERR_PARAM); } - if ( *path == '\0' ) { - return( AFPERR_BADTYPE ); + if ( path_isadir(path) ) { + return( AFPERR_BADTYPE ); } if ( applopen( vol, creator, O_RDWR|O_CREAT, 0666 ) != AFP_OK ) { - return( AFPERR_PARAM ); + return( AFPERR_PARAM ); } if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) { - return( AFPERR_PARAM ); + return( AFPERR_PARAM ); } dtf = dtfile( vol, creator, ".appl.temp" ); tempfile = obj->oldtmp; strcpy( tempfile, dtf ); if (( tfd = open( tempfile, O_RDWR|O_CREAT, 0666 )) < 0 ) { - return( AFPERR_PARAM ); + return( AFPERR_PARAM ); } mpath = obj->newtmp; - mp = makemacpath( mpath, AFPOBJ_TMPSIZ, curdir, path ); + mp = makemacpath( mpath, AFPOBJ_TMPSIZ, curdir, path->m_name ); + if (!mp) { + return AFPERR_PARAM; + } mplen = mpath + AFPOBJ_TMPSIZ - mp; /* write the new appl entry at start of temporary file */ @@ -222,8 +231,8 @@ int afp_addappl(obj, ibuf, ibuflen, rbuf, rbuflen ) memcpy(p, appltag, sizeof( appltag )); cc = mpath + AFPOBJ_TMPSIZ - p; if ( write( tfd, p, cc ) != cc ) { - unlink( tempfile ); - return( AFPERR_PARAM ); + unlink( tempfile ); + return( AFPERR_PARAM ); } cc = copyapplfile( sa.sdt_fd, tfd, mp, mplen ); close( tfd ); @@ -231,26 +240,27 @@ int afp_addappl(obj, ibuf, ibuflen, rbuf, rbuflen ) sa.sdt_fd = -1; if ( cc < 0 ) { - unlink( tempfile ); - return( AFPERR_PARAM ); + unlink( tempfile ); + return( AFPERR_PARAM ); } if ( rename( tempfile, dtfile( vol, creator, ".appl" )) < 0 ) { - return( AFPERR_PARAM ); + return( AFPERR_PARAM ); } return( AFP_OK ); } int afp_rmvappl(obj, ibuf, ibuflen, rbuf, rbuflen ) - AFPObj *obj; - char *ibuf, *rbuf; - int ibuflen, *rbuflen; +AFPObj *obj; +char *ibuf, *rbuf _U_; +int ibuflen _U_, *rbuflen; { struct vol *vol; struct dir *dir; int tfd, cc; u_int32_t did; u_int16_t vid, mplen; - char *path, *dtf, *mp; + struct path *path; + char *dtf, *mp; u_char creator[ 4 ]; char *tempfile, *mpath; @@ -259,40 +269,44 @@ int afp_rmvappl(obj, ibuf, ibuflen, rbuf, rbuflen ) memcpy( &vid, ibuf, sizeof( vid )); ibuf += sizeof( vid ); - if (( vol = getvolbyvid( vid )) == NULL ) { - return( AFPERR_PARAM ); + if (NULL == ( vol = getvolbyvid( vid ))) { + return( AFPERR_PARAM ); } memcpy( &did, ibuf, sizeof( did )); ibuf += sizeof( did ); - if (( dir = dirsearch( vol, did )) == NULL ) { - return( AFPERR_NOOBJ ); + if (NULL == ( dir = dirlookup( vol, did )) ) { + return afp_errno; } memcpy( creator, ibuf, sizeof( creator )); ibuf += sizeof( creator ); - if (( path = cname( vol, dir, &ibuf )) == NULL ) { - return( AFPERR_NOOBJ ); + if (NULL == ( path = cname( vol, dir, &ibuf )) ) { + return get_afp_errno(AFPERR_PARAM); } - if ( *path == '.' ) { - return( AFPERR_BADTYPE ); + if ( path_isadir(path) ) { + return( AFPERR_BADTYPE ); } if ( applopen( vol, creator, O_RDWR, 0666 ) != AFP_OK ) { - return( AFPERR_NOOBJ ); + return( AFPERR_NOOBJ ); } if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) { - return( AFPERR_PARAM ); + return( AFPERR_PARAM ); } dtf = dtfile( vol, creator, ".appl.temp" ); tempfile = obj->oldtmp; strcpy( tempfile, dtf ); if (( tfd = open( tempfile, O_RDWR|O_CREAT, 0666 )) < 0 ) { - return( AFPERR_PARAM ); + return( AFPERR_PARAM ); } mpath = obj->newtmp; - mp = makemacpath( mpath, AFPOBJ_TMPSIZ, curdir, path ); + mp = makemacpath( mpath, AFPOBJ_TMPSIZ, curdir, path->m_name ); + if (!mp) { + return AFPERR_PARAM ; + } + mplen = mpath + AFPOBJ_TMPSIZ - mp; cc = copyapplfile( sa.sdt_fd, tfd, mp, mplen ); close( tfd ); @@ -300,21 +314,20 @@ int afp_rmvappl(obj, ibuf, ibuflen, rbuf, rbuflen ) sa.sdt_fd = -1; if ( cc < 0 ) { - unlink( tempfile ); - return( AFPERR_PARAM ); + unlink( tempfile ); + return( AFPERR_PARAM ); } if ( rename( tempfile, dtfile( vol, creator, ".appl" )) < 0 ) { - return( AFPERR_PARAM ); + return( AFPERR_PARAM ); } return( AFP_OK ); } int afp_getappl(obj, ibuf, ibuflen, rbuf, rbuflen ) - AFPObj *obj; - char *ibuf, *rbuf; - int ibuflen, *rbuflen; +AFPObj *obj; +char *ibuf, *rbuf; +int ibuflen _U_, *rbuflen; { - struct stat st; struct vol *vol; char *p, *q; int cc, buflen; @@ -322,14 +335,15 @@ int afp_getappl(obj, ibuf, ibuflen, rbuf, rbuflen ) u_char creator[ 4 ]; u_char appltag[ 4 ]; char *buf, *cbuf; - + struct path *path; + ibuf += 2; memcpy( &vid, ibuf, sizeof( vid )); ibuf += sizeof( vid ); - if (( vol = getvolbyvid( vid )) == NULL ) { - *rbuflen = 0; - return( AFPERR_PARAM ); + if (NULL == ( vol = getvolbyvid( vid )) ) { + *rbuflen = 0; + return( AFPERR_PARAM ); } memcpy( creator, ibuf, sizeof( creator )); @@ -339,44 +353,44 @@ int afp_getappl(obj, ibuf, ibuflen, rbuf, rbuflen ) ibuf += sizeof( aindex ); aindex = ntohs( aindex ); if ( aindex ) { /* index 0 == index 1 */ - --aindex; + --aindex; } - + memcpy( &bitmap, ibuf, sizeof( bitmap )); bitmap = ntohs( bitmap ); ibuf += sizeof( bitmap ); if ( applopen( vol, creator, O_RDONLY, 0666 ) != AFP_OK ) { - *rbuflen = 0; - return( AFPERR_NOITEM ); + *rbuflen = 0; + return( AFPERR_NOITEM ); } if ( aindex < sa.sdt_index ) { - if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) { - *rbuflen = 0; - return( AFPERR_PARAM ); - } - sa.sdt_index = 0; + if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) { + *rbuflen = 0; + return( AFPERR_PARAM ); + } + sa.sdt_index = 0; } /* position to correct spot within appl file */ buf = obj->oldtmp; while (( cc = read( sa.sdt_fd, buf, sizeof( appltag ) - + sizeof( u_short ))) > 0 ) { - p = buf + sizeof( appltag ); - memcpy( &len, p, sizeof( len )); - len = ntohs( len ); - p += sizeof( u_short ); - if (( cc = read( sa.sdt_fd, p, len )) < len ) { - break; - } - if ( sa.sdt_index == aindex ) { - break; - } - sa.sdt_index++; + + sizeof( u_short ))) > 0 ) { + p = buf + sizeof( appltag ); + memcpy( &len, p, sizeof( len )); + len = ntohs( len ); + p += sizeof( u_short ); + if (( cc = read( sa.sdt_fd, p, len )) < len ) { + break; + } + if ( sa.sdt_index == aindex ) { + break; + } + sa.sdt_index++; } if ( cc <= 0 || sa.sdt_index != aindex ) { - *rbuflen = 0; - return( AFPERR_NOITEM ); + *rbuflen = 0; + return( AFPERR_NOITEM ); } sa.sdt_index++; @@ -390,35 +404,35 @@ int afp_getappl(obj, ibuf, ibuflen, rbuf, rbuflen ) #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'a' ) #define islxdigit(x) (!isupper(x)&&isxdigit(x)) - char utomname[ MAXPATHLEN + 1]; - char *u, *m; - int i, h; - - u = p; - m = utomname; - i = len; - while ( i ) { - if ( *u == ':' && *(u+1) != '\0' && islxdigit( *(u+1)) && - *(u+2) != '\0' && islxdigit( *(u+2))) { - ++u, --i; - h = hextoint( *u ) << 4; - ++u, --i; - h |= hextoint( *u ); - *m++ = h; - } else { - *m++ = *u; - } - ++u, --i; - } - - len = m - utomname; - p = utomname; - - if ( p[ len - 1 ] == '\0' ) { - len--; - } - } -#endif APPLCNAME + char utomname[ MAXPATHLEN + 1]; + char *u, *m; + int i, h; + + u = p; + m = utomname; + i = len; + while ( i ) { + if ( *u == ':' && *(u+1) != '\0' && islxdigit( *(u+1)) && + *(u+2) != '\0' && islxdigit( *(u+2))) { + ++u, --i; + h = hextoint( *u ) << 4; + ++u, --i; + h |= hextoint( *u ); + *m++ = h; + } else { + *m++ = *u; + } + ++u, --i; + } + + len = m - utomname; + p = utomname; + + if ( p[ len - 1 ] == '\0' ) { + len--; + } + } +#endif /* APPLCNAME */ /* fake up a cname */ cbuf = obj->newtmp; @@ -428,20 +442,20 @@ int afp_getappl(obj, ibuf, ibuflen, rbuf, rbuflen ) memcpy( q, p, len ); q = cbuf; - if (( p = cname( vol, vol->v_dir, &q )) == NULL ) { - *rbuflen = 0; - return( AFPERR_NOITEM ); + if (( path = cname( vol, vol->v_dir, &q )) == NULL ) { + *rbuflen = 0; + return( AFPERR_NOITEM ); } - if ( stat( mtoupath(vol, p), &st ) < 0 ) { - *rbuflen = 0; - return( AFPERR_NOITEM ); + if ( path_isadir(path) || path->st_errno ) { + *rbuflen = 0; + return( AFPERR_NOITEM ); } buflen = *rbuflen - sizeof( bitmap ) - sizeof( appltag ); - if ( getfilparams(vol, bitmap, p, curdir, &st, rbuf + sizeof( bitmap ) + - sizeof( appltag ), &buflen ) != AFP_OK ) { - *rbuflen = 0; - return( AFPERR_BITMAP ); + if ( getfilparams(vol, bitmap, path, curdir, rbuf + sizeof( bitmap ) + + sizeof( appltag ), &buflen ) != AFP_OK ) { + *rbuflen = 0; + return( AFPERR_BITMAP ); } *rbuflen = buflen + sizeof( bitmap ) + sizeof( appltag ); @@ -453,4 +467,3 @@ int afp_getappl(obj, ibuf, ibuflen, rbuf, rbuflen ) return( AFP_OK ); } -