/*
+ * $Id: file.c,v 1.23 2001-07-10 13:38:13 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>
#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
*
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;
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;
if (!(aint = cnid_add(vol->v_db, st, dir->d_did, upath,
strlen(upath), aint))) {
-#endif
+#endif /* AD_VERSION > AD_VERSION1 */
/*
* What a fucking mess. First thing: DID and FNUMs are
* in the same space for purposes of enumerate (and several
*
* 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));
+#endif /* DID_MTAB */
+#endif /* USE_LASTDID */
+
#if AD_VERSION > AD_VERSION1
}
-#endif
+#endif /* AD_VERSION > AD_VERSION1 */
memcpy(data, &aint, sizeof( aint ));
data += sizeof( aint );
break;
ad_close( adp, ADFLAGS_HF );
}
*buflen = data - buf;
+
+#ifdef DEBUG
+ syslog(LOG_INFO, "end getfilparams:");
+#endif /* DEBUG */
+
return( AFP_OK );
}
int ibuflen, *rbuflen;
{
struct stat st;
-#ifdef DROPKLUDGE
- struct stat sb;
- char adpath[50];
- int uid;
-#endif DROPKLUDGE
struct adouble ad, *adp;
struct vol *vol;
struct dir *dir;
struct ofork *of;
char *path, *upath;
- int creatf, did, openf;
+ 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++;
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 */
goto createfile_done;
/* fallthrough */
default :
+#ifdef FORCE_UIDGID
+ /* bring everything back to old euid, egid */
+ restore_uidgid ( uidgid );
+#endif /* FORCE_UIDGID */
return( AFPERR_PARAM );
}
}
createfile_done:
#ifdef DROPKLUDGE
-
-/* The below code changes the way file ownership is determined in the name of
-fixing dropboxes. It has known security problem. See the netatalk FAQ for
-more information */
- if (stat(".", &sb) == -1)
- syslog (LOG_ERR, "Error checking directory %s: %m", dir->d_name);
- else {
- uid=geteuid();
- if ( uid != sb.st_uid )
- {
- strcpy (adpath, "./.AppleDouble/");
- strcat (adpath, upath);
- seteuid(0); /* Become root to change the owner of the file */
- if (chown(upath, sb.st_uid, sb.st_gid) < 0)
- {
- syslog (LOG_ERR, "Error changing owner/gid: %m");
- return (-1);
- }
- /* In order to write information to the file, the Mac client needs
- to be able to read from it too, so read bits have to be turned on.
- Directory permissions remain unchanged */
- stat(upath, &st);
- if (chmod(upath,(st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
- {
- syslog (LOG_ERR, "Error adding file read permissions: %m");
- return (-1);
- }
- else syslog (LOG_DEBUG, "Added S_IRGRP and S_IROTH: %m");
- if (chown(adpath, sb.st_uid, sb.st_gid) < 0)
- {
- syslog (LOG_ERR, "Error changing AppleDouble owner/gid: %m");
- return (-1);
- }
- if (chmod(adpath, (st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
- {
- syslog (LOG_ERR, "Error adding AD file read permissions: %m");
- return (-1);
- }
- else syslog (LOG_DEBUG, "Added S_IRGRP and S_IROTH to AD: %m");
- syslog (LOG_DEBUG, "Changing afpd owner back to %d", uid);
- seteuid(uid); /* Restore process ownership to normal */
- }
+ if (vol->v_flags & AFPVOL_DROPBOX) {
+ retvalue = matchfile2dirperms(upath, vol, did);
}
-
-#endif DROPKLUDGE
+#endif /* DROPKLUDGE */
setvoltime(obj, vol );
- return AFP_OK;
+
+#ifdef DEBUG
+ syslog(LOG_INFO, "end afp_createfile");
+#endif /* DEBUG */
+
+#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 )
int did, rc;
u_int16_t vid, bitmap;
+#ifdef DEBUG
+ syslog(LOG_INFO, "begin afp_setfilparams:");
+#endif /* DEBUG */
+
*rbuflen = 0;
ibuf += 2;
setvoltime(obj, vol );
}
+#ifdef DEBUG
+ syslog(LOG_INFO, "end afp_setfilparams:");
+#endif /* DEBUG */
+
return( rc );
}
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;
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;
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;
}
/* existence check moved to afp_moveandrename */
+#ifdef DEBUG
+ syslog (LOG_INFO, "begin renamefile:");
+#endif /* DEBUG */
+
if ( rename( src, dst ) < 0 ) {
switch ( errno ) {
case ENOENT :
ad_flush( &ad, ADFLAGS_HF );
ad_close( &ad, ADFLAGS_HF );
+#ifdef DEBUG
+ syslog (LOG_INFO, "end renamefile:");
+#endif /* DEBUG */
+
return( AFP_OK );
}
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;
}
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 );
}
{
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) {
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
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 ) {
}
goto copyheader_done;
}
-#endif
+#endif /* SENDFILE_FLAVOR_LINUX */
while (1) {
if ((cc = read(sfd, filebuf, sizeof(filebuf))) < 0) {
if (errno == EINTR)
}
goto copydata_done;
}
-#endif
+#endif /* SENDFILE_FLAVOR_LINUX */
while (1) {
if ((cc = read( sfd, filebuf, sizeof( filebuf ))) < 0) {
ad_close( &ad, ADFLAGS_HF );
}
+#ifdef DEBUG
+ syslog(LOG_INFO, "end copyfile:");
+#endif /* DEBUG */
+
return( AFP_OK );
}
{
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;
}
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;
}
int len;
cnid_t did, id;
u_short vid;
+
+#ifdef DEBUG
+ syslog(LOG_INFO, "begin afp_createid:");
+#endif /* DEBUG */
*rbuflen = 0;
ibuf += 2;
id = 0;
else {
memcpy(&id, ad_entry(&ad, ADEID_DID), sizeof(id));
- ad_close(upath, ADFLAGS_HF);
+ ad_close(&ad, ADFLAGS_HF);
}
if (id = cnid_add(vol->v_db, &st, did, upath, len, id)) {
return AFP_OK;
}
+#ifdef DEBUG
+ syslog(LOG_INFO, "ending afp_createid...:");
+#endif /* DEBUG */
+
switch (errno) {
case EROFS:
return AFPERR_VLOCK;
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;
*rbuflen = buflen + sizeof(bitmap);
memcpy(rbuf, ibuf, sizeof(bitmap));
+
+#ifdef DEBUG
+ syslog(LOG_INFO, "end afp_resolveid:");
+#endif /* DEBUG */
+
return AFP_OK;
}
int err;
cnid_t id;
u_short vid;
-
+
+#ifdef DEBUG
+ syslog(LOG_INFO, "begin afp_deleteid:");
+#endif /* DEBUG */
+
*rbuflen = 0;
ibuf += 2;
}
}
+#ifdef DEBUG
+ syslog(LOG_INFO, "end afp_deleteid:");
+#endif /* DEBUG */
+
return err;
}
-#endif
+#endif /* AD_VERSION > AD_VERSION1 */
#define APPLETEMP ".AppleTempXXXXXX"
+
int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
AFPObj *obj;
char *ibuf, *rbuf;
int err;
#if AD_VERSION > AD_VERSION1
int slen, dlen;
-#endif
+#endif /* AD_VERSION > AD_VERSION1 */
cnid_t sid, did;
u_int16_t vid;
-
+
+#ifdef DEBUG
+ syslog(LOG_INFO, "begin afp_exchangefiles:");
+#endif /* DEBUG */
+
*rbuflen = 0;
ibuf += 2;
#if AD_VERSION > AD_VERSION1
sid = cnid_lookup(vol->v_db, &srcst, sdir->d_did, supath,
slen = strlen(supath));
-#endif
+#endif /* AD_VERSION > AD_VERSION1 */
if (( dir = dirsearch( vol, did )) == NULL ) {
return( AFPERR_PARAM );
/* look for destination id. */
did = cnid_lookup(vol->v_db, &destst, curdir->d_did, upath,
dlen = strlen(upath));
-#endif
+#endif /* AD_VERSION > AD_VERSION1 */
/* construct a temp name.
* NOTE: the temp file will be in the dest file's directory. it
cnid_update(vol->v_db, sid, &srcst, sdir->d_did, supath, slen);
goto err_temp_to_dest;
}
-#endif
+#endif /* AD_VERSION > AD_VERSION1 */
+
+#ifdef DEBUG
+ syslog(LOG_INFO, "ending afp_exchangefiles:");
+#endif /* DEBUG */
+
return AFP_OK;