2 * $Id: filedir.c,v 1.19 2002-01-03 17:49:38 sibaz Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_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"
58 int matchfile2dirperms(upath, vol, did)
59 /* Since it's kinda' big; I decided against an
64 /* The below code changes the way file ownership is determined in the name of
65 fixing dropboxes. It has known security problem. See the netatalk FAQ for
74 syslog(LOG_INFO, "begin matchfile2dirperms:");
77 if (stat(upath, &st ) < 0)
78 syslog(LOG_ERR, "Could not stat %s: %s", upath, strerror(errno));
79 strcpy (adpath, "./.AppleDouble/");
80 strcat (adpath, upath);
81 if (( dir = dirsearch( vol, did )) == NULL ) {
82 syslog(LOG_ERR, "matchfile2dirperms: Unable to get directory info.");
83 return( AFPERR_NOOBJ );
85 else if (stat(".", &sb) < 0) {
87 "matchfile2dirperms: Error checking directory \"%s\": %s",
88 dir->d_name, strerror(errno));
89 return(AFPERR_NOOBJ );
93 if ( uid != sb.st_uid )
96 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
99 "matchfile2dirperms: Error changing owner/gid of %s: %s",
100 upath, strerror(errno));
101 return (AFPERR_ACCESS);
103 if (chmod(upath,(st.st_mode&0777&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
106 "matchfile2dirperms: Error adding file read permissions: %s",
108 return (AFPERR_ACCESS);
113 "matchfile2dirperms: Added S_IRGRP and S_IROTH: %s",
116 if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
119 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %s",
120 adpath, strerror(errno));
121 return (AFPERR_ACCESS);
123 if (chmod(adpath, (st.st_mode&0777&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
126 "matchfile2dirperms: Error adding AD file read permissions: %s",
128 return (AFPERR_ACCESS);
133 "matchfile2dirperms: Added S_IRGRP and S_IROTH to AD: %s",
140 "matchfile2dirperms: No ownership change necessary.");
142 } /* end else if stat success */
143 seteuid(uid); /* Restore process ownership to normal */
145 syslog(LOG_INFO, "end matchfile2dirperms:");
153 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
156 int ibuflen, *rbuflen;
164 u_int16_t fbitmap, dbitmap, vid;
167 syslog(LOG_INFO, "begin afp_getfildirparams:");
173 memcpy( &vid, ibuf, sizeof( vid ));
174 ibuf += sizeof( vid );
175 if (( vol = getvolbyvid( vid )) == NULL ) {
176 return( AFPERR_PARAM );
179 memcpy( &did, ibuf, sizeof( did ));
180 ibuf += sizeof( did );
182 if (( dir = dirsearch( vol, did )) == NULL ) {
183 return( AFPERR_NOOBJ );
186 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
187 fbitmap = ntohs( fbitmap );
188 ibuf += sizeof( fbitmap );
189 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
190 dbitmap = ntohs( dbitmap );
191 ibuf += sizeof( dbitmap );
193 if (( path = cname( vol, dir, &ibuf )) == NULL) {
194 return( AFPERR_NOOBJ );
197 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
198 return( AFPERR_NOOBJ );
202 if (S_ISDIR(st.st_mode)) {
204 ret = getdirparams(vol, dbitmap, ".", curdir,
205 &st, rbuf + 3 * sizeof( u_int16_t ), &buflen );
209 /* this is a directory */
210 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
212 if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
213 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
217 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
219 *rbuflen = buflen + 3 * sizeof( u_int16_t );
220 fbitmap = htons( fbitmap );
221 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
222 rbuf += sizeof( fbitmap );
223 dbitmap = htons( dbitmap );
224 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
225 rbuf += sizeof( dbitmap ) + sizeof( u_char );
229 syslog(LOG_INFO, "end afp_getfildirparams:");
235 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
238 int ibuflen, *rbuflen;
244 u_int16_t vid, bitmap;
248 syslog(LOG_INFO, "begin afp_setfildirparams:");
253 memcpy( &vid, ibuf, sizeof(vid));
254 ibuf += sizeof( vid );
256 if (( vol = getvolbyvid( vid )) == NULL ) {
257 return( AFPERR_PARAM );
260 if (vol->v_flags & AFPVOL_RO)
263 memcpy( &did, ibuf, sizeof( did));
264 ibuf += sizeof( did);
266 if (( dir = dirsearch( vol, did )) == NULL ) {
267 return( AFPERR_NOOBJ );
270 memcpy( &bitmap, ibuf, sizeof( bitmap ));
271 bitmap = ntohs( bitmap );
272 ibuf += sizeof( bitmap );
274 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
275 return( AFPERR_NOOBJ );
278 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
279 return( AFPERR_NOOBJ );
283 * If ibuf is odd, make it even.
285 if ((u_long)ibuf & 1 ) {
289 if (S_ISDIR(st.st_mode)) {
290 rc = setdirparams(vol, path, bitmap, ibuf );
292 rc = setfilparams(vol, path, bitmap, ibuf );
294 if ( rc == AFP_OK ) {
295 setvoltime(obj, vol );
299 syslog(LOG_INFO, "end afp_setfildirparams:");
305 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
308 int ibuflen, *rbuflen;
313 struct dir *dir, *odir = NULL;
314 char *path, *buf, *upath, *newpath;
324 syslog(LOG_INFO, "begin afp_rename:");
330 memcpy( &vid, ibuf, sizeof( vid ));
331 ibuf += sizeof( vid );
332 if (( vol = getvolbyvid( vid )) == NULL ) {
333 return( AFPERR_PARAM );
336 if (vol->v_flags & AFPVOL_RO)
339 memcpy( &did, ibuf, sizeof( did ));
340 ibuf += sizeof( did );
341 if (( dir = dirsearch( vol, did )) == NULL ) {
342 return( AFPERR_NOOBJ );
345 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
346 return( AFPERR_NOOBJ );
349 /* another place where we know about the path type */
350 if ( *ibuf++ != 2 ) {
351 return( AFPERR_PARAM );
353 plen = (unsigned char) *ibuf++;
354 *( ibuf + plen ) = '\0';
356 if ( *path == '\0' ) {
357 if ( curdir->d_parent == NULL ) { /* root directory */
358 return( AFPERR_NORENAME );
361 path = curdir->d_name;
362 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
363 return( AFPERR_NOOBJ );
368 if ( strcasecmp( path, ibuf ) == 0 ) {
373 /* if a curdir/newname ofork exists, return busy */
374 if (of_findname(vol, curdir, ibuf))
377 /* source == destination. just say okay. */
378 if (strcmp(path, ibuf) == 0)
381 /* check for illegal characters */
382 if (!wincheck(vol, ibuf))
385 newpath = obj->oldtmp;
386 strcpy( newpath, mtoupath(vol, ibuf ));
388 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(newpath, '/'))
391 if (!validupath(vol, newpath))
394 /* check for vetoed filenames */
395 if (veto_file(vol->v_veto, newpath))
398 /* the strdiacasecmp deals with case-insensitive, case preserving
400 if (stat( newpath, &st ) == 0 && strdiacasecmp(path, ibuf))
401 return( AFPERR_EXIST );
403 upath = mtoupath(vol, path);
406 id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
409 if ( rename( upath, newpath ) < 0 ) {
412 return( AFPERR_NOOBJ );
414 return( AFPERR_ACCESS );
416 return( AFPERR_PARAM );
421 if (stat(newpath, &st) < 0) /* this shouldn't fail */
423 cnid_update(vol->v_db, id, &st, curdir->d_did, newpath, strlen(newpath));
427 newadpath = obj->newtmp;
428 strcpy( newadpath, ad_path( newpath, 0 ));
429 if ( rename( ad_path( upath, 0 ), newadpath ) < 0 ) {
430 if ( errno == ENOENT ) { /* no adouble header file */
431 if (( unlink( newadpath ) < 0 ) && ( errno != ENOENT )) {
432 return( AFPERR_PARAM );
436 return( AFPERR_PARAM );
439 memset(&ad, 0, sizeof(ad));
440 if ( ad_open( newpath, ADFLAGS_HF, O_RDWR|O_CREAT, 0666,
442 return( AFPERR_PARAM );
447 memset(&ad, 0, sizeof(ad));
448 if ( ad_open( newpath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
449 O_RDWR|O_CREAT, 0666, &ad) < 0 ) {
450 if (!((errno == ENOENT) && vol_noadouble(vol)))
451 return( AFPERR_PARAM );
454 if ((buf = realloc( odir->d_name, plen + 1 )) == NULL ) {
455 syslog( LOG_ERR, "afp_rename: realloc: %s", strerror(errno) );
457 ad_flush(&ad, ADFLAGS_HF); /* in case of create */
458 ad_close(&ad, ADFLAGS_HF);
463 strcpy( odir->d_name, ibuf );
468 ad_setentrylen( &ad, ADEID_NAME, plen );
469 memcpy( ad_entry( &ad, ADEID_NAME ), ibuf, plen );
470 ad_flush( &ad, ADFLAGS_HF );
471 ad_close( &ad, ADFLAGS_HF );
474 setvoltime(obj, vol );
476 /* if it's still open, rename the ofork as well. */
477 if (of_rename(vol, curdir, path, curdir, ibuf) < 0)
481 syslog(LOG_INFO, "end afp_rename:");
488 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
491 int ibuflen, *rbuflen;
500 syslog(LOG_INFO, "begin afp_delete:");
506 memcpy( &vid, ibuf, sizeof( vid ));
507 ibuf += sizeof( vid );
508 if (( vol = getvolbyvid( vid )) == NULL ) {
509 return( AFPERR_PARAM );
512 if (vol->v_flags & AFPVOL_RO)
515 memcpy( &did, ibuf, sizeof( did ));
516 ibuf += sizeof( int );
517 if (( dir = dirsearch( vol, did )) == NULL ) {
518 return( AFPERR_NOOBJ );
521 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
522 return( AFPERR_NOOBJ );
525 if ( *path == '\0' ) {
526 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
527 } else if (of_findname(vol, curdir, path)) {
529 } else if ((rc = deletefile( upath = mtoupath(vol, path ))) == AFP_OK) {
530 #ifdef CNID_DB /* get rid of entry */
531 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
532 cnid_delete(vol->v_db, id);
535 if ( rc == AFP_OK ) {
536 setvoltime(obj, vol );
540 syslog(LOG_INFO, "end afp_delete:");
546 char *ctoupath( vol, dir, name )
547 const struct vol *vol;
552 static char path[ MAXPATHLEN + 1];
556 p = path + sizeof( path ) - 1;
558 u = mtoupath(vol, name );
561 strncpy( p, u, len );
562 for ( d = dir; d->d_parent; d = d->d_parent ) {
564 u = mtoupath(vol, d->d_name );
567 strncpy( p, u, len );
570 len = strlen( vol->v_path );
572 strncpy( p, vol->v_path, len );
578 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
581 int ibuflen, *rbuflen;
584 struct dir *sdir, *ddir, *odir = NULL;
586 char *oldname, *newname;
587 char *path, *p, *upath;
596 #endif /* DROPKLUDGE */
599 syslog(LOG_INFO, "begin afp_moveandrename:");
605 memcpy( &vid, ibuf, sizeof( vid ));
606 ibuf += sizeof( vid );
607 if (( vol = getvolbyvid( vid )) == NULL ) {
608 return( AFPERR_PARAM );
611 if (vol->v_flags & AFPVOL_RO)
614 /* source did followed by dest did */
615 memcpy( &did, ibuf, sizeof( did ));
616 ibuf += sizeof( int );
617 if (( sdir = dirsearch( vol, did )) == NULL ) {
618 return( AFPERR_PARAM );
621 memcpy( &did, ibuf, sizeof( did ));
622 ibuf += sizeof( int );
624 /* source pathname */
625 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
626 return( AFPERR_NOOBJ );
630 newname = obj->newtmp;
631 oldname = obj->oldtmp;
632 if ( *path != '\0' ) {
633 /* not a directory */
634 strcpy(newname, path);
635 strcpy(oldname, path); /* an extra copy for of_rename */
637 p = mtoupath(vol, path);
638 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
640 p = ctoupath( vol, sdir, newname );
643 strcpy( newname, odir->d_name );
644 strcpy(oldname, odir->d_name);
645 p = ctoupath( vol, odir->d_parent, newname );
647 id = curdir->d_did; /* we already have the CNID */
651 * p now points to the full pathname of the source fs object.
654 /* get the destination directory */
655 if (( ddir = dirsearch( vol, did )) == NULL ) {
656 return( AFPERR_PARAM );
658 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
659 return( AFPERR_NOOBJ );
661 if ( *path != '\0' ) {
662 return( AFPERR_BADTYPE );
665 /* one more place where we know about path type */
666 if ( *ibuf++ != 2 ) {
667 return( AFPERR_PARAM );
670 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
671 strncpy( newname, ibuf, plen );
672 newname[ plen ] = '\0';
675 /* check for illegal characters */
676 if (!wincheck(vol, newname))
679 upath = mtoupath(vol, newname);
681 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
684 if (!validupath(vol, upath))
687 /* check for vetoed filenames */
688 if (veto_file(vol->v_veto, upath))
691 /* source == destination. we just silently accept this. */
692 if (curdir == sdir) {
693 if (strcmp(oldname, newname) == 0)
696 /* deal with case insensitive, case-preserving filesystems. */
697 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
700 } else if (stat(upath, &st ) == 0)
701 return( AFPERR_EXIST );
704 if (of_findname(vol, curdir, newname)) {
706 } else if ((rc = renamefile( p, upath, newname,
707 vol_noadouble(vol) )) == AFP_OK) {
708 /* if it's still open, rename the ofork as well. */
709 rc = of_rename(vol, sdir, oldname, curdir, newname);
712 rc = renamedir(p, upath, odir, curdir, newname, vol_noadouble(vol));
716 if (vol->v_flags & AFPVOL_DROPBOX) {
717 if (retvalue=matchfile2dirperms (newname, vol, did) != AFP_OK) {
721 #endif /* DROPKLUDGE */
723 if ( rc == AFP_OK ) {
725 /* renaming may have moved the file/dir across a filesystem */
726 if (stat(upath, &st) < 0)
729 /* fix up the catalog entry */
730 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
732 setvoltime(obj, vol );
736 syslog(LOG_INFO, "end afp_moveandrename:");
742 int veto_file(const char*veto_str, const char*path)
743 /* given a veto_str like "abc/zxc/" and path "abc", return 1
744 * veto_str should be '/' delimited
745 * if path matches any one of the veto_str elements exactly, then 1 is returned
746 * otherwise, 0 is returned.
749 int i; /* index to veto_str */
750 int j; /* index to path */
752 if ((veto_str == NULL) || (path == NULL))
756 syslog(LOG_DEBUG, "veto_file \"%s\", \"%s\"", veto_str, path);
759 for(i=0, j=0; veto_str[i] != '\0'; i++) {
760 if (veto_str[i] == '/') {
761 if ((j>0) && (path[j] == '\0'))
765 if (veto_str[i] != path[j]) {
766 while ((veto_str[i] != '/')
767 && (veto_str[i] != '\0'))