X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=etc%2Fafpd%2Ffile.c;h=6b5664a4260732beb892b80f2e6940bfb05f3917;hb=f3c04e0d1c1cbf8754599578de07f7a5f1cf1083;hp=5341b687379754faa3e1c3e7ec2bbb417fe4efb4;hpb=ae1170c11a624cd35a8acde26917814ff6184853;p=netatalk.git diff --git a/etc/afpd/file.c b/etc/afpd/file.c index 5341b687..6b5664a4 100644 --- a/etc/afpd/file.c +++ b/etc/afpd/file.c @@ -1,5 +1,5 @@ /* - * $Id: file.c,v 1.43 2002-03-24 17:43:39 jmarcus Exp $ + * $Id: file.c,v 1.53 2002-08-30 19:32:41 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 * @@ -95,42 +90,28 @@ const u_char ufinderi[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; -int getfilparams(struct vol *vol, +int getmetadata(struct vol *vol, u_int16_t bitmap, char *path, struct dir *dir, struct stat *st, - char *buf, int *buflen ) + char *buf, int *buflen, struct adouble *adp, int attrbits ) { #ifndef USE_LASTDID - struct stat hst, lst, *lstp; -#else /* USE_LASTDID */ - struct stat hst; + struct stat lst, *lstp; #endif /* USE_LASTDID */ - struct adouble ad, *adp; - struct ofork *of; + struct stat hst; struct extmap *em; char *data, *nameoff = NULL, *upath; - int bit = 0, isad = 1; + int bit = 0; u_int32_t aint; u_int16_t ashort; u_char achar, fdType[4]; + struct maccess ma; #ifdef DEBUG - LOG(log_info, logtype_afpd, "begin getfilparams:"); + LOG(log_info, logtype_afpd, "begin getmetadata:"); #endif /* DEBUG */ upath = mtoupath(vol, path); - if ((of = of_findname(vol, curdir, path))) { - adp = of->of_ad; - } else { - memset(&ad, 0, sizeof(ad)); - adp = &ad; - } - - if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) { - isad = 0; - } else if ( fstat( ad_hfileno( adp ), &hst ) < 0 ) { - LOG(log_error, logtype_afpd, "getfilparams fstat: %s", strerror(errno) ); - } data = buf; while ( bitmap != 0 ) { @@ -141,14 +122,27 @@ int getfilparams(struct vol *vol, switch ( bit ) { case FILPBIT_ATTR : - if ( isad ) { + if ( adp ) { ad_getattr(adp, &ashort); } else if (*upath == '.') { 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 )); - data += sizeof( u_short ); + data += sizeof( ashort ); break; case FILPBIT_PDID : @@ -157,17 +151,16 @@ int getfilparams(struct vol *vol, break; case FILPBIT_CDATE : - if (!isad || (ad_getdate(adp, AD_DATE_CREATE, &aint) < 0)) + if (!adp || (ad_getdate(adp, AD_DATE_CREATE, &aint) < 0)) aint = AD_DATE_FROM_UNIX(st->st_mtime); memcpy(data, &aint, sizeof( aint )); data += sizeof( aint ); break; case FILPBIT_MDATE : - if ( isad && (ad_getdate(adp, AD_DATE_MODIFY, &aint) == 0)) { - if ((st->st_mtime > AD_DATE_TO_UNIX(aint)) && - (hst.st_mtime < st->st_mtime)) { - aint = AD_DATE_FROM_UNIX(st->st_mtime); + if ( adp && (ad_getdate(adp, AD_DATE_MODIFY, &aint) == 0)) { + if ((st->st_mtime > AD_DATE_TO_UNIX(aint))) { + aint = AD_DATE_FROM_UNIX(st->st_mtime); } } else { aint = AD_DATE_FROM_UNIX(st->st_mtime); @@ -177,14 +170,14 @@ int getfilparams(struct vol *vol, break; case FILPBIT_BDATE : - if (!isad || (ad_getdate(adp, AD_DATE_BACKUP, &aint) < 0)) + if (!adp || (ad_getdate(adp, AD_DATE_BACKUP, &aint) < 0)) aint = AD_DATE_START; memcpy(data, &aint, sizeof( int )); data += sizeof( int ); break; case FILPBIT_FINFO : - if (isad) + if (adp) memcpy(data, ad_entry(adp, ADEID_FINDERI), 32); else { memcpy(data, ufinderi, 32); @@ -194,9 +187,9 @@ int getfilparams(struct vol *vol, } } - if ((!isad || (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)); } @@ -217,7 +210,7 @@ int getfilparams(struct vol *vol, aint = 0; #if AD_VERSION > AD_VERSION1 /* look in AD v2 header */ - if (isad) + if (adp) memcpy(&aint, ad_entry(adp, ADEID_DID), sizeof(aint)); #endif /* AD_VERSION > AD_VERSION1 */ @@ -289,7 +282,7 @@ int getfilparams(struct vol *vol, break; case FILPBIT_RFLEN : - if ( isad ) { + if ( adp ) { aint = htonl( ad_getentrylen( adp, ADEID_RFORK )); } else { aint = 0; @@ -305,7 +298,7 @@ int getfilparams(struct vol *vol, to "pXYZ" when we created it. See IA, Ver 2. */ case FILPBIT_PDINFO : - if ( isad ) { + if ( adp ) { memcpy(fdType, ad_entry( adp, ADEID_FINDERI ), 4 ); if ( memcmp( fdType, "TEXT", 4 ) == 0 ) { @@ -347,9 +340,6 @@ int getfilparams(struct vol *vol, break; default : - if ( isad ) { - ad_close( adp, ADFLAGS_HF ); - } return( AFPERR_BITMAP ); } bitmap = bitmap>>1; @@ -364,18 +354,67 @@ int getfilparams(struct vol *vol, memcpy(data, path, aint ); data += aint; } - if ( isad ) { - ad_close( adp, ADFLAGS_HF ); - } *buflen = data - buf; + return (AFP_OK); +} + +/* ----------------------- */ +int getfilparams(struct vol *vol, + u_int16_t bitmap, + char *path, struct dir *dir, struct stat *st, + char *buf, int *buflen ) +{ + struct adouble ad, *adp; + struct ofork *of; + char *upath; + u_int16_t attrbits = 0; + int rc; +#ifdef DEBUG + LOG(log_info, logtype_default, "begin getfilparams:"); +#endif /* DEBUG */ + + upath = mtoupath(vol, path); + if ((of = of_findname(vol, dir, path))) { + 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); + } else { + memset(&ad, 0, sizeof(ad)); + adp = &ad; + } + + if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) { + adp = NULL; + } + else { +#if 0 + /* 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: + - bitmap is requested. + - we don't already have the answer! + */ + if ((bitmap & (1 << FILPBIT_ATTR))) { + if (!(attrbits & ATTRBIT_ROPEN)) { + } + if (!(attrbits & ATTRBIT_DOPEN)) { + } + } +#endif + } + rc = getmetadata(vol, bitmap, path, dir, st, buf, buflen, adp, attrbits); + if ( adp ) { + ad_close( adp, ADFLAGS_HF ); + } #ifdef DEBUG LOG(log_info, logtype_afpd, "end getfilparams:"); #endif /* DEBUG */ - return( AFP_OK ); + return( rc ); } +/* ----------------------------- */ int afp_createfile(obj, ibuf, ibuflen, rbuf, rbuflen ) AFPObj *obj; char *ibuf, *rbuf; @@ -389,9 +428,6 @@ 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 */ #ifdef DEBUG LOG(log_info, logtype_afpd, "begin afp_createfile:"); @@ -423,20 +459,11 @@ int ibuflen, *rbuflen; } upath = mtoupath(vol, path); - - /* check for illegal bits in the unix filename */ - if (!wincheck(vol, upath)) - return AFPERR_PARAM; - - if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/')) - return AFPERR_PARAM; - - if (!validupath(vol, upath)) - return AFPERR_EXIST; - - /* check for vetoed filenames */ - if (veto_file(vol->v_veto, upath)) - return AFPERR_EXIST; + { + int ret; + if (0 != (ret = check_name(vol, upath))) + return ret; + } if ((of = of_findname(vol, curdir, path))) { adp = of->of_ad; @@ -450,33 +477,21 @@ int ibuflen, *rbuflen; 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 + because open syscall is not called + */ + if (of) { + return AFPERR_EXIST; + } 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 */ @@ -484,10 +499,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 ); } } @@ -512,11 +523,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); } @@ -561,6 +567,10 @@ int ibuflen, *rbuflen; return( AFPERR_NOOBJ ); } + if ( *path == '\0' ) { + return( AFPERR_BADTYPE ); /* it's a directory */ + } + if ((u_long)ibuf & 1 ) { ibuf++; } @@ -576,6 +586,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 ) @@ -590,11 +604,11 @@ 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:"); @@ -608,18 +622,14 @@ int setfilparams(struct vol *vol, 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<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; @@ -734,20 +752,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<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)) { @@ -1582,6 +1637,12 @@ int ibuflen, *rbuflen; char *spath, temp[17], *path, *p; char *supath, *upath; int err; + struct adouble ads; + struct adouble add; + struct adouble *adsp; + struct adouble *addp; + struct ofork *opened; + #ifdef CNID_DB int slen, dlen; #endif /* CNID_DB */ @@ -1621,7 +1682,7 @@ int ibuflen, *rbuflen; } if ( *path == '\0' ) { - return( AFPERR_BADTYPE ); + return( AFPERR_BADTYPE ); /* it's a dir */ } upath = mtoupath(vol, path); @@ -1636,7 +1697,12 @@ int ibuflen, *rbuflen; return AFPERR_PARAM; } } - + memset(&ads, 0, sizeof(ads)); + adsp = &ads; + if ((opened = of_findname(vol, curdir, path))) { + /* reuse struct adouble so it won't break locks */ + adsp = opened->of_ad; + } /* save some stuff */ sdir = curdir; spath = obj->oldtmp; @@ -1681,7 +1747,12 @@ int ibuflen, *rbuflen; return AFPERR_PARAM; } } - + memset(&add, 0, sizeof(add)); + addp = &add; + if ((opened = of_findname(vol, curdir, path))) { + /* reuse struct adouble so it won't break locks */ + addp = opened->of_ad; + } #ifdef CNID_DB /* look for destination id. */ did = cnid_lookup(vol->v_db, &destst, curdir->d_did, upath, @@ -1696,17 +1767,17 @@ int ibuflen, *rbuflen; return AFPERR_MISC; /* now, quickly rename the file. we error if we can't. */ - if ((err = renamefile(p, temp, temp, vol_noadouble(vol))) < 0) + if ((err = renamefile(p, temp, temp, vol_noadouble(vol), adsp)) < 0) goto err_exchangefile; of_rename(vol, sdir, spath, curdir, temp); /* rename destination to source */ - if ((err = renamefile(path, p, spath, vol_noadouble(vol))) < 0) + if ((err = renamefile(upath, p, spath, vol_noadouble(vol), addp)) < 0) goto err_src_to_tmp; of_rename(vol, curdir, path, sdir, spath); /* rename temp to destination */ - if ((err = renamefile(temp, upath, path, vol_noadouble(vol))) < 0) + if ((err = renamefile(temp, upath, path, vol_noadouble(vol), adsp)) < 0) goto err_dest_to_src; of_rename(vol, curdir, temp, curdir, path); @@ -1751,19 +1822,21 @@ 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)); + renamefile(upath, temp, temp, vol_noadouble(vol), adsp); of_rename(vol, curdir, upath, curdir, temp); err_dest_to_src: /* rename source back to dest */ - renamefile(p, upath, path, vol_noadouble(vol)); + renamefile(p, upath, path, vol_noadouble(vol), addp); of_rename(vol, sdir, spath, curdir, path); err_src_to_tmp: /* rename temp back to source */ - renamefile(temp, p, spath, vol_noadouble(vol)); + renamefile(temp, p, spath, vol_noadouble(vol), adsp); of_rename(vol, curdir, temp, sdir, spath); err_exchangefile: