2
* $Id: filedir.c,v 1.32.2.3 2003/06/09 14:53:15 srittau Exp $
4
* Copyright (c) 1990,1993 Regents of The University of Michigan.
5
* All Rights Reserved. See COPYRIGHT.
10
#endif /* HAVE_CONFIG_H */
13
#include <atalk/logger.h>
14
#include <sys/types.h>
16
#include <sys/param.h>
17
#include <netatalk/endian.h>
18
#include <atalk/adouble.h>
19
#include <atalk/afp.h>
20
#include <atalk/util.h>
22
#include <atalk/cnid.h>
28
#endif /* HAVE_FCNTL_H */
34
#else /* STDC_HEADERS */
38
#endif /* HAVE_STRCHR */
39
char *strchr (), *strrchr ();
41
#define memcpy(d,s,n) bcopy ((s), (d), (n))
42
#define memmove(d,s,n) bcopy ((s), (d), (n))
43
#endif /* ! HAVE_MEMCPY */
44
#endif /* STDC_HEADERS */
48
#endif /* HAVE_UNISTD_H */
50
#include "directory.h"
59
int matchfile2dirperms(upath, vol, did)
60
/* Since it's kinda' big; I decided against an
65
/* The below code changes the way file ownership is determined in the name of
66
fixing dropboxes. It has known security problem. See the netatalk FAQ for
75
LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
78
if (stat(upath, &st ) < 0) {
79
LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
83
adpath = ad_path( upath, ADFLAGS_HF );
84
/* FIXME dirsearch doesn't move cwd to did ! */
85
if (( dir = dirlookup( vol, did )) == NULL ) {
86
LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
89
else if (stat(".", &sb) < 0) {
90
LOG(log_error, logtype_afpd,
91
"matchfile2dirperms: Error checking directory \"%s\": %s",
92
dir->d_name, strerror(errno));
97
if ( uid != sb.st_uid )
100
if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
102
LOG(log_error, logtype_afpd,
103
"matchfile2dirperms: Error changing owner/gid of %s: %s",
104
upath, strerror(errno));
107
else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
109
LOG(log_error, logtype_afpd,
110
"matchfile2dirperms: Error adding file read permissions: %s",
114
else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
116
LOG(log_error, logtype_afpd,
117
"matchfile2dirperms: Error changing AppleDouble owner/gid %s: %s",
118
adpath, strerror(errno));
121
else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
123
LOG(log_error, logtype_afpd,
124
"matchfile2dirperms: Error adding AD file read permissions: %s",
130
} /* end else if stat success */
133
LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
139
int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
142
int ibuflen, *rbuflen;
150
u_int16_t fbitmap, dbitmap, vid;
153
LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
159
memcpy( &vid, ibuf, sizeof( vid ));
160
ibuf += sizeof( vid );
161
if (( vol = getvolbyvid( vid )) == NULL ) {
162
return( AFPERR_PARAM );
165
memcpy( &did, ibuf, sizeof( did ));
166
ibuf += sizeof( did );
168
if (( dir = dirlookup( vol, did )) == NULL ) {
170
return( AFPERR_NOOBJ );
173
memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
174
fbitmap = ntohs( fbitmap );
175
ibuf += sizeof( fbitmap );
176
memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
177
dbitmap = ntohs( dbitmap );
178
ibuf += sizeof( dbitmap );
180
if (( path = cname( vol, dir, &ibuf )) == NULL ||
181
stat( mtoupath(vol, path ), &st ) < 0 ) {
185
return AFPERR_ACCESS;
192
if (S_ISDIR(st.st_mode)) {
195
/* the dir wasn't in the cache and we weren't able to chdir in it.
197
return AFPERR_ACCESS;
199
ret = getdirparams(vol, dbitmap, ".", curdir,
200
&st, rbuf + 3 * sizeof( u_int16_t ), &buflen );
204
/* this is a directory */
205
*(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
207
if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
208
rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
212
*(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
214
*rbuflen = buflen + 3 * sizeof( u_int16_t );
215
fbitmap = htons( fbitmap );
216
memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
217
rbuf += sizeof( fbitmap );
218
dbitmap = htons( dbitmap );
219
memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
220
rbuf += sizeof( dbitmap ) + sizeof( u_char );
224
LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
231
* We can't use unix file's perm to support Apple's inherited protection modes.
232
* If we aren't the file's owner we can't change its perms when moving it and smb
233
* nfs,... don't even try.
235
#define AFP_CHECK_ACCESS
237
int check_access(char *path, int mode)
239
#ifdef AFP_CHECK_ACCESS
247
accessmode(p, &ma, curdir, NULL);
248
if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
250
if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
256
int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
259
int ibuflen, *rbuflen;
265
u_int16_t vid, bitmap;
269
LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
274
memcpy( &vid, ibuf, sizeof(vid));
275
ibuf += sizeof( vid );
277
if (( vol = getvolbyvid( vid )) == NULL ) {
278
return( AFPERR_PARAM );
281
if (vol->v_flags & AFPVOL_RO)
284
memcpy( &did, ibuf, sizeof( did));
285
ibuf += sizeof( did);
287
if (( dir = dirlookup( vol, did )) == NULL ) {
288
return( AFPERR_NOOBJ );
291
memcpy( &bitmap, ibuf, sizeof( bitmap ));
292
bitmap = ntohs( bitmap );
293
ibuf += sizeof( bitmap );
295
if (( path = cname( vol, dir, &ibuf )) == NULL ) {
296
return( AFPERR_NOOBJ );
299
if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
300
return( AFPERR_NOOBJ );
304
* If ibuf is odd, make it even.
306
if ((u_long)ibuf & 1 ) {
310
if (S_ISDIR(st.st_mode)) {
311
rc = setdirparams(vol, path, bitmap, ibuf );
313
rc = setfilparams(vol, path, bitmap, ibuf );
315
if ( rc == AFP_OK ) {
316
setvoltime(obj, vol );
320
LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
326
/* --------------------------------------------
327
Factorise some check on a pathname
329
int check_name(const struct vol *vol, char *name)
331
/* check for illegal characters in the unix filename */
332
if (!wincheck(vol, name))
335
if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
338
if (!validupath(vol, name))
341
/* check for vetoed filenames */
342
if (veto_file(vol->v_veto, name))
347
/* -------------------------
348
move and rename sdir:oldname to curdir:newname in volume vol
350
special care is needed for lock
352
static int moveandrename(vol, sdir, oldname, newname, isdir)
353
const struct vol *vol;
366
struct ofork *opened;
372
memset(&ad, 0, sizeof(ad));
378
p = mtoupath(vol, oldname);
379
id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
381
p = ctoupath( vol, sdir, oldname );
382
if ((opened = of_findname(p, NULL))) {
383
/* reuse struct adouble so it won't break locks */
389
id = sdir->d_did; /* we already have the CNID */
391
p = ctoupath( vol, sdir->d_parent, oldname );
392
adflags = ADFLAGS_DIR;
395
* p now points to the full pathname of the source fs object.
397
* we are in the dest folder so we need to use p for ad_open
400
if (!ad_open(p, ADFLAGS_HF |adflags, O_RDONLY, 0666, adp)) {
403
ad_getattr(adp, &bshort);
404
ad_close( adp, ADFLAGS_HF );
405
if ((bshort & htons(ATTRBIT_NORENAME)))
406
return(AFPERR_OLOCK);
409
upath = mtoupath(vol, newname);
410
if (0 != (rc = check_name(vol, upath))) {
414
/* source == destination. we just silently accept this. */
415
if (curdir == sdir) {
416
if (strcmp(oldname, newname) == 0)
419
/* deal with case insensitive, case-preserving filesystems. */
420
if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
423
} else if (stat(upath, &st ) == 0)
427
if (of_findname(upath, &st)) {
428
rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
430
rc = renamefile( p, upath, newname,vol_noadouble(vol), adp );
432
of_rename(vol, opened, sdir, oldname, curdir, newname);
435
rc = renamedir(p, upath, sdir, curdir, newname, vol_noadouble(vol));
437
if ( rc == AFP_OK ) {
439
/* renaming may have moved the file/dir across a filesystem */
440
if (stat(upath, &st) < 0)
443
/* fix up the catalog entry */
444
cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
451
/* -------------------------------------------- */
452
int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
455
int ibuflen, *rbuflen;
459
char *path, *oldname, *newname;
466
LOG(log_info, logtype_afpd, "begin afp_rename:");
472
memcpy( &vid, ibuf, sizeof( vid ));
473
ibuf += sizeof( vid );
474
if (( vol = getvolbyvid( vid )) == NULL ) {
475
return( AFPERR_PARAM );
478
if (vol->v_flags & AFPVOL_RO)
481
memcpy( &did, ibuf, sizeof( did ));
482
ibuf += sizeof( did );
483
if (( sdir = dirlookup( vol, did )) == NULL ) {
484
return( AFPERR_NOOBJ );
487
/* source pathname */
488
if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
489
return( AFPERR_NOOBJ );
493
newname = obj->newtmp;
494
oldname = obj->oldtmp;
495
if ( *path != '\0' ) {
496
strcpy(oldname, path); /* an extra copy for of_rename */
499
if ( sdir->d_parent == NULL ) { /* root directory */
500
return( AFPERR_NORENAME );
502
/* move to destination dir */
503
if ( movecwd( vol, sdir->d_parent ) < 0 ) {
504
return( AFPERR_NOOBJ );
507
strcpy(oldname, sdir->d_name);
510
/* another place where we know about the path type */
511
if ( *ibuf++ != 2 ) {
512
return( AFPERR_PARAM );
515
if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
516
strncpy( newname, ibuf, plen );
517
newname[ plen ] = '\0';
518
if (strlen(newname) != plen) {
519
return( AFPERR_PARAM );
523
return AFP_OK; /* newname == oldname same dir */
526
rc = moveandrename(vol, sdir, oldname, newname, isdir);
528
if ( rc == AFP_OK ) {
529
setvoltime(obj, vol );
533
LOG(log_info, logtype_afpd, "end afp_rename:");
539
/* ------------------------------- */
540
int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
543
int ibuflen, *rbuflen;
552
LOG(log_info, logtype_afpd, "begin afp_delete:");
558
memcpy( &vid, ibuf, sizeof( vid ));
559
ibuf += sizeof( vid );
560
if (( vol = getvolbyvid( vid )) == NULL ) {
561
return( AFPERR_PARAM );
564
if (vol->v_flags & AFPVOL_RO)
567
memcpy( &did, ibuf, sizeof( did ));
568
ibuf += sizeof( int );
569
if (( dir = dirlookup( vol, did )) == NULL ) {
570
return( AFPERR_NOOBJ );
573
if (( path = cname( vol, dir, &ibuf )) == NULL ) {
574
return( AFPERR_NOOBJ );
577
upath = mtoupath(vol, path );
578
if ( *path == '\0' ) {
579
rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
580
} else if (of_findname(upath, NULL)) {
582
} else if ((rc = deletefile( upath, 1)) == AFP_OK) {
583
#ifdef CNID_DB /* get rid of entry */
584
cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
585
cnid_delete(vol->v_db, id);
588
if ( rc == AFP_OK ) {
589
setvoltime(obj, vol );
593
LOG(log_info, logtype_afpd, "end afp_delete:");
599
char *ctoupath( vol, dir, name )
600
const struct vol *vol;
605
static char path[ MAXPATHLEN + 1];
609
p = path + sizeof( path ) - 1;
611
u = mtoupath(vol, name );
614
strncpy( p, u, len );
615
for ( d = dir; d->d_parent; d = d->d_parent ) {
617
u = mtoupath(vol, d->d_name );
620
strncpy( p, u, len );
623
len = strlen( vol->v_path );
625
strncpy( p, vol->v_path, len );
630
/* ------------------------- */
631
int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
634
int ibuflen, *rbuflen;
637
struct dir *sdir, *ddir;
639
char *oldname, *newname;
647
#endif /* DROPKLUDGE */
650
LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
656
memcpy( &vid, ibuf, sizeof( vid ));
657
ibuf += sizeof( vid );
658
if (( vol = getvolbyvid( vid )) == NULL ) {
659
return( AFPERR_PARAM );
662
if (vol->v_flags & AFPVOL_RO)
665
/* source did followed by dest did */
666
memcpy( &did, ibuf, sizeof( did ));
667
ibuf += sizeof( int );
668
if (( sdir = dirlookup( vol, did )) == NULL ) {
669
return( AFPERR_PARAM );
672
memcpy( &did, ibuf, sizeof( did ));
673
ibuf += sizeof( int );
675
/* source pathname */
676
if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
677
return( AFPERR_NOOBJ );
681
newname = obj->newtmp;
682
oldname = obj->oldtmp;
683
if ( *path != '\0' ) {
684
/* not a directory */
685
strcpy(oldname, path); /* an extra copy for of_rename */
688
strcpy(oldname, sdir->d_name);
691
/* get the destination directory */
692
if (( ddir = dirlookup( vol, did )) == NULL ) {
693
return( AFPERR_PARAM );
695
if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
696
return( AFPERR_NOOBJ );
698
if ( *path != '\0' ) {
699
return( AFPERR_BADTYPE );
702
/* one more place where we know about path type */
703
if ( *ibuf++ != 2 ) {
704
return( AFPERR_PARAM );
707
if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
708
strncpy( newname, ibuf, plen );
709
newname[ plen ] = '\0';
710
if (strlen(newname) != plen) {
711
return( AFPERR_PARAM );
715
strcpy(newname, oldname);
718
rc = moveandrename(vol, sdir, oldname, newname, isdir);
720
if ( rc == AFP_OK ) {
721
char *upath = mtoupath(vol, newname);
723
if (vol->v_flags & AFPVOL_DROPBOX) {
724
if (retvalue=matchfile2dirperms (upath, vol, did) != AFP_OK) {
729
#endif /* DROPKLUDGE */
731
int admode = ad_mode("", 0777);
733
setfilmode(upath, admode, NULL);
734
setfilmode(ad_path( upath, ADFLAGS_HF ), ad_hf_mode(admode), NULL);
736
setvoltime(obj, vol );
740
LOG(log_info, logtype_afpd, "end afp_moveandrename:");
746
int veto_file(const char*veto_str, const char*path)
747
/* given a veto_str like "abc/zxc/" and path "abc", return 1
748
* veto_str should be '/' delimited
749
* if path matches any one of the veto_str elements exactly, then 1 is returned
750
* otherwise, 0 is returned.
753
int i; /* index to veto_str */
754
int j; /* index to path */
756
if ((veto_str == NULL) || (path == NULL))
760
LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
763
for(i=0, j=0; veto_str[i] != '\0'; i++) {
764
if (veto_str[i] == '/') {
765
if ((j>0) && (path[j] == '\0'))
769
if (veto_str[i] != path[j]) {
770
while ((veto_str[i] != '/')
771
&& (veto_str[i] != '\0'))