2 * $Id: filedir.c,v 1.11 2001-08-14 14:00:10 rufustfirefly 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 <sys/syslog.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 */
33 #endif /* HAVE_UNISTD_H */
35 #include "directory.h"
43 int matchfile2dirperms(upath, vol, did)
44 /* Since it's kinda' big; I decided against an
49 /* The below code changes the way file ownership is determined in the name of
50 fixing dropboxes. It has known security problem. See the netatalk FAQ for
59 syslog (LOG_INFO, "begin matchfile2dirperms:");
62 if (stat(upath, &st ) < 0)
63 syslog(LOG_ERR, "Could not stat %s: %m", upath);
64 strcpy (adpath, "./.AppleDouble/");
65 strcat (adpath, upath);
66 if (( dir = dirsearch( vol, did )) == NULL ) {
67 syslog (LOG_ERR, "matchfile2dirperms: Unable to get directory info.");
68 return( AFPERR_NOOBJ );
70 else if (stat(".", &sb) < 0) {
72 "matchfile2dirperms: Error checking directory \"%s\": %m",
74 return(AFPERR_NOOBJ );
78 if ( uid != sb.st_uid )
81 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
84 "matchfile2dirperms: Error changing owner/gid of %s: %m", upath);
85 return (AFPERR_ACCESS);
87 if (chmod(upath,(st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
90 "matchfile2dirperms: Error adding file read permissions: %m");
91 return (AFPERR_ACCESS);
96 "matchfile2dirperms: Added S_IRGRP and S_IROTH: %m");
98 if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
101 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %m",
103 return (AFPERR_ACCESS);
105 if (chmod(adpath, (st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
108 "matchfile2dirperms: Error adding AD file read permissions: %m");
109 return (AFPERR_ACCESS);
114 "matchfile2dirperms: Added S_IRGRP and S_IROTH to AD: %m");
120 "matchfile2dirperms: No ownership change necessary.");
122 } /* end else if stat success */
123 seteuid(uid); /* Restore process ownership to normal */
125 syslog (LOG_INFO, "end matchfile2dirperms:");
133 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
136 int ibuflen, *rbuflen;
144 u_int16_t fbitmap, dbitmap, vid;
147 syslog(LOG_INFO, "begin afp_getfildirparams:");
153 memcpy( &vid, ibuf, sizeof( vid ));
154 ibuf += sizeof( vid );
155 if (( vol = getvolbyvid( vid )) == NULL ) {
156 return( AFPERR_PARAM );
159 memcpy( &did, ibuf, sizeof( did ));
160 ibuf += sizeof( did );
162 if (( dir = dirsearch( vol, did )) == NULL ) {
163 return( AFPERR_NOOBJ );
166 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
167 fbitmap = ntohs( fbitmap );
168 ibuf += sizeof( fbitmap );
169 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
170 dbitmap = ntohs( dbitmap );
171 ibuf += sizeof( dbitmap );
173 if (( path = cname( vol, dir, &ibuf )) == NULL) {
174 return( AFPERR_NOOBJ );
177 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
178 return( AFPERR_NOOBJ );
182 if (S_ISDIR(st.st_mode)) {
183 if (dbitmap && ( ret = getdirparams(vol, dbitmap, ".", curdir,
184 &st, rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
187 /* this is a directory */
188 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISDIR;
190 if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
191 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
195 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
197 *rbuflen = buflen + 3 * sizeof( u_int16_t );
198 fbitmap = htons( fbitmap );
199 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
200 rbuf += sizeof( fbitmap );
201 dbitmap = htons( dbitmap );
202 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
203 rbuf += sizeof( dbitmap ) + sizeof( u_char );
207 syslog(LOG_INFO, "end afp_getfildirparams:");
213 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
216 int ibuflen, *rbuflen;
222 u_int16_t vid, bitmap;
226 syslog(LOG_INFO, "begin afp_setfildirparams:");
231 memcpy( &vid, ibuf, sizeof(vid));
232 ibuf += sizeof( vid );
234 if (( vol = getvolbyvid( vid )) == NULL ) {
235 return( AFPERR_PARAM );
238 if (vol->v_flags & AFPVOL_RO)
241 memcpy( &did, ibuf, sizeof( did));
242 ibuf += sizeof( did);
244 if (( dir = dirsearch( vol, did )) == NULL ) {
245 return( AFPERR_NOOBJ );
248 memcpy( &bitmap, ibuf, sizeof( bitmap ));
249 bitmap = ntohs( bitmap );
250 ibuf += sizeof( bitmap );
252 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
253 return( AFPERR_NOOBJ );
256 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
257 return( AFPERR_NOOBJ );
261 * If ibuf is odd, make it even.
263 if ((u_long)ibuf & 1 ) {
267 if (S_ISDIR(st.st_mode)) {
268 rc = setdirparams(vol, path, bitmap, ibuf );
270 rc = setfilparams(vol, path, bitmap, ibuf );
272 if ( rc == AFP_OK ) {
273 setvoltime(obj, vol );
277 syslog(LOG_INFO, "end afp_setfildirparams:");
283 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
286 int ibuflen, *rbuflen;
291 struct dir *dir, *odir = NULL;
292 char *path, *buf, *upath, *newpath;
302 syslog(LOG_INFO, "begin afp_rename:");
308 memcpy( &vid, ibuf, sizeof( vid ));
309 ibuf += sizeof( vid );
310 if (( vol = getvolbyvid( vid )) == NULL ) {
311 return( AFPERR_PARAM );
314 if (vol->v_flags & AFPVOL_RO)
317 memcpy( &did, ibuf, sizeof( did ));
318 ibuf += sizeof( did );
319 if (( dir = dirsearch( vol, did )) == NULL ) {
320 return( AFPERR_NOOBJ );
323 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
324 return( AFPERR_NOOBJ );
327 /* another place where we know about the path type */
328 if ( *ibuf++ != 2 ) {
329 return( AFPERR_PARAM );
331 plen = (unsigned char) *ibuf++;
332 *( ibuf + plen ) = '\0';
334 if ( *path == '\0' ) {
335 if ( curdir->d_parent == NULL ) { /* root directory */
336 return( AFPERR_NORENAME );
339 path = curdir->d_name;
340 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
341 return( AFPERR_NOOBJ );
346 if ( strcasecmp( path, ibuf ) == 0 ) {
351 /* if a curdir/newname ofork exists, return busy */
352 if (of_findname(vol, curdir, ibuf))
355 /* source == destination. just say okay. */
356 if (strcmp(path, ibuf) == 0)
359 /* check for illegal characters */
360 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
361 strpbrk(ibuf, MSWINDOWS_BADCHARS))
364 newpath = obj->oldtmp;
365 strcpy( newpath, mtoupath(vol, ibuf ));
367 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(newpath, '/'))
370 if (!validupath(vol, newpath))
373 /* the strdiacasecmp deals with case-insensitive, case preserving
375 if (stat( newpath, &st ) == 0 && strdiacasecmp(path, ibuf))
376 return( AFPERR_EXIST );
378 upath = mtoupath(vol, path);
381 id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
384 if ( rename( upath, newpath ) < 0 ) {
387 return( AFPERR_NOOBJ );
389 return( AFPERR_ACCESS );
391 return( AFPERR_PARAM );
396 if (stat(newpath, &st) < 0) /* this shouldn't fail */
398 cnid_update(vol->v_db, id, &st, curdir->d_did, newpath, strlen(newpath));
402 newadpath = obj->newtmp;
403 strcpy( newadpath, ad_path( newpath, 0 ));
404 if ( rename( ad_path( upath, 0 ), newadpath ) < 0 ) {
405 if ( errno == ENOENT ) { /* no adouble header file */
406 if (( unlink( newadpath ) < 0 ) && ( errno != ENOENT )) {
407 return( AFPERR_PARAM );
411 return( AFPERR_PARAM );
414 memset(&ad, 0, sizeof(ad));
415 if ( ad_open( newpath, ADFLAGS_HF, O_RDWR|O_CREAT, 0666,
417 return( AFPERR_PARAM );
422 memset(&ad, 0, sizeof(ad));
423 if ( ad_open( newpath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
424 O_RDWR|O_CREAT, 0666, &ad) < 0 ) {
425 if (!((errno == ENOENT) && vol_noadouble(vol)))
426 return( AFPERR_PARAM );
429 if ((buf = realloc( odir->d_name, plen + 1 )) == NULL ) {
430 syslog( LOG_ERR, "afp_rename: realloc: %m" );
432 ad_flush(&ad, ADFLAGS_HF); /* in case of create */
433 ad_close(&ad, ADFLAGS_HF);
438 strcpy( odir->d_name, ibuf );
443 ad_setentrylen( &ad, ADEID_NAME, plen );
444 memcpy( ad_entry( &ad, ADEID_NAME ), ibuf, plen );
445 ad_flush( &ad, ADFLAGS_HF );
446 ad_close( &ad, ADFLAGS_HF );
449 setvoltime(obj, vol );
451 /* if it's still open, rename the ofork as well. */
452 if (of_rename(vol, curdir, path, curdir, ibuf) < 0)
456 syslog(LOG_INFO, "end afp_rename:");
463 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
466 int ibuflen, *rbuflen;
475 syslog(LOG_INFO, "begin afp_delete:");
481 memcpy( &vid, ibuf, sizeof( vid ));
482 ibuf += sizeof( vid );
483 if (( vol = getvolbyvid( vid )) == NULL ) {
484 return( AFPERR_PARAM );
487 if (vol->v_flags & AFPVOL_RO)
490 memcpy( &did, ibuf, sizeof( did ));
491 ibuf += sizeof( int );
492 if (( dir = dirsearch( vol, did )) == NULL ) {
493 return( AFPERR_NOOBJ );
496 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
497 return( AFPERR_NOOBJ );
500 if ( *path == '\0' ) {
501 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
502 } else if (of_findname(vol, curdir, path)) {
504 } else if ((rc = deletefile( upath = mtoupath(vol, path ))) == AFP_OK) {
505 #ifdef CNID_DB /* get rid of entry */
506 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
507 cnid_delete(vol->v_db, id);
510 if ( rc == AFP_OK ) {
511 setvoltime(obj, vol );
515 syslog(LOG_INFO, "end afp_delete:");
521 char *ctoupath( vol, dir, name )
522 const struct vol *vol;
527 static char path[ MAXPATHLEN + 1];
531 p = path + sizeof( path ) - 1;
533 u = mtoupath(vol, name );
536 strncpy( p, u, len );
537 for ( d = dir; d->d_parent; d = d->d_parent ) {
539 u = mtoupath(vol, d->d_name );
542 strncpy( p, u, len );
545 len = strlen( vol->v_path );
547 strncpy( p, vol->v_path, len );
553 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
556 int ibuflen, *rbuflen;
559 struct dir *sdir, *ddir, *odir = NULL;
561 char *oldname, *newname;
562 char *path, *p, *upath;
571 #endif /* DROPKLUDGE */
574 syslog(LOG_INFO, "begin afp_moveandrename:");
580 memcpy( &vid, ibuf, sizeof( vid ));
581 ibuf += sizeof( vid );
582 if (( vol = getvolbyvid( vid )) == NULL ) {
583 return( AFPERR_PARAM );
586 if (vol->v_flags & AFPVOL_RO)
589 /* source did followed by dest did */
590 memcpy( &did, ibuf, sizeof( did ));
591 ibuf += sizeof( int );
592 if (( sdir = dirsearch( vol, did )) == NULL ) {
593 return( AFPERR_PARAM );
596 memcpy( &did, ibuf, sizeof( did ));
597 ibuf += sizeof( int );
599 /* source pathname */
600 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
601 return( AFPERR_NOOBJ );
605 newname = obj->newtmp;
606 oldname = obj->oldtmp;
607 if ( *path != '\0' ) {
608 /* not a directory */
609 strcpy(newname, path);
610 strcpy(oldname, path); /* an extra copy for of_rename */
612 p = mtoupath(vol, path);
613 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
615 p = ctoupath( vol, sdir, newname );
618 strcpy( newname, odir->d_name );
619 strcpy(oldname, odir->d_name);
620 p = ctoupath( vol, odir->d_parent, newname );
622 id = curdir->d_did; /* we already have the CNID */
626 * p now points to the full pathname of the source fs object.
629 /* get the destination directory */
630 if (( ddir = dirsearch( vol, did )) == NULL ) {
631 return( AFPERR_PARAM );
633 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
634 return( AFPERR_NOOBJ );
636 if ( *path != '\0' ) {
637 return( AFPERR_BADTYPE );
640 /* one more place where we know about path type */
641 if ( *ibuf++ != 2 ) {
642 return( AFPERR_PARAM );
645 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
646 strncpy( newname, ibuf, plen );
647 newname[ plen ] = '\0';
650 /* check for illegal characters */
651 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
652 strpbrk(newname, MSWINDOWS_BADCHARS))
655 upath = mtoupath(vol, newname);
657 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
660 if (!validupath(vol, upath))
663 /* source == destination. we just silently accept this. */
664 if (curdir == sdir) {
665 if (strcmp(oldname, newname) == 0)
668 /* deal with case insensitive, case-preserving filesystems. */
669 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
672 } else if (stat(upath, &st ) == 0)
673 return( AFPERR_EXIST );
676 if (of_findname(vol, curdir, newname)) {
678 } else if ((rc = renamefile( p, upath, newname,
679 vol_noadouble(vol) )) == AFP_OK) {
680 /* if it's still open, rename the ofork as well. */
681 rc = of_rename(vol, sdir, oldname, curdir, newname);
684 rc = renamedir(p, upath, odir, curdir, newname, vol_noadouble(vol));
688 if (vol->v_flags & AFPVOL_DROPBOX) {
689 if (retvalue=matchfile2dirperms (newname, vol, did) != AFP_OK) {
693 #endif /* DROPKLUDGE */
695 if ( rc == AFP_OK ) {
697 /* renaming may have moved the file/dir across a filesystem */
698 if (stat(upath, &st) < 0)
701 /* fix up the catalog entry */
702 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
704 setvoltime(obj, vol );
708 syslog(LOG_INFO, "end afp_moveandrename:");