2 * $Id: filedir.c,v 1.10 2001-06-20 18:33:04 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>
21 #include <atalk/cnid.h>
26 #endif /* HAVE_FCNTL_H */
31 #endif /* HAVE_UNISTD_H */
33 #include "directory.h"
41 int matchfile2dirperms(upath, vol, did)
42 /* Since it's kinda' big; I decided against an
47 /* The below code changes the way file ownership is determined in the name of
48 fixing dropboxes. It has known security problem. See the netatalk FAQ for
57 syslog (LOG_INFO, "begin matchfile2dirperms:");
60 if (stat(upath, &st ) < 0)
61 syslog(LOG_ERR, "Could not stat %s: %m", upath);
62 strcpy (adpath, "./.AppleDouble/");
63 strcat (adpath, upath);
64 if (( dir = dirsearch( vol, did )) == NULL ) {
65 syslog (LOG_ERR, "matchfile2dirperms: Unable to get directory info.");
66 return( AFPERR_NOOBJ );
68 else if (stat(".", &sb) < 0) {
70 "matchfile2dirperms: Error checking directory \"%s\": %m",
72 return(AFPERR_NOOBJ );
76 if ( uid != sb.st_uid )
79 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
82 "matchfile2dirperms: Error changing owner/gid of %s: %m", upath);
83 return (AFPERR_ACCESS);
85 if (chmod(upath,(st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
88 "matchfile2dirperms: Error adding file read permissions: %m");
89 return (AFPERR_ACCESS);
94 "matchfile2dirperms: Added S_IRGRP and S_IROTH: %m");
96 if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
99 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %m",
101 return (AFPERR_ACCESS);
103 if (chmod(adpath, (st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
106 "matchfile2dirperms: Error adding AD file read permissions: %m");
107 return (AFPERR_ACCESS);
112 "matchfile2dirperms: Added S_IRGRP and S_IROTH to AD: %m");
118 "matchfile2dirperms: No ownership change necessary.");
120 } /* end else if stat success */
121 seteuid(uid); /* Restore process ownership to normal */
123 syslog (LOG_INFO, "end matchfile2dirperms:");
131 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
134 int ibuflen, *rbuflen;
142 u_int16_t fbitmap, dbitmap, vid;
145 syslog(LOG_INFO, "begin afp_getfildirparams:");
151 memcpy( &vid, ibuf, sizeof( vid ));
152 ibuf += sizeof( vid );
153 if (( vol = getvolbyvid( vid )) == NULL ) {
154 return( AFPERR_PARAM );
157 memcpy( &did, ibuf, sizeof( did ));
158 ibuf += sizeof( did );
160 if (( dir = dirsearch( vol, did )) == NULL ) {
161 return( AFPERR_NOOBJ );
164 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
165 fbitmap = ntohs( fbitmap );
166 ibuf += sizeof( fbitmap );
167 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
168 dbitmap = ntohs( dbitmap );
169 ibuf += sizeof( dbitmap );
171 if (( path = cname( vol, dir, &ibuf )) == NULL) {
172 return( AFPERR_NOOBJ );
175 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
176 return( AFPERR_NOOBJ );
180 if (S_ISDIR(st.st_mode)) {
181 if (dbitmap && ( ret = getdirparams(vol, dbitmap, ".", curdir,
182 &st, rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
185 /* this is a directory */
186 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISDIR;
188 if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
189 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
193 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
195 *rbuflen = buflen + 3 * sizeof( u_int16_t );
196 fbitmap = htons( fbitmap );
197 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
198 rbuf += sizeof( fbitmap );
199 dbitmap = htons( dbitmap );
200 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
201 rbuf += sizeof( dbitmap ) + sizeof( u_char );
205 syslog(LOG_INFO, "end afp_getfildirparams:");
211 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
214 int ibuflen, *rbuflen;
220 u_int16_t vid, bitmap;
224 syslog(LOG_INFO, "begin afp_setfildirparams:");
229 memcpy( &vid, ibuf, sizeof(vid));
230 ibuf += sizeof( vid );
232 if (( vol = getvolbyvid( vid )) == NULL ) {
233 return( AFPERR_PARAM );
236 if (vol->v_flags & AFPVOL_RO)
239 memcpy( &did, ibuf, sizeof( did));
240 ibuf += sizeof( did);
242 if (( dir = dirsearch( vol, did )) == NULL ) {
243 return( AFPERR_NOOBJ );
246 memcpy( &bitmap, ibuf, sizeof( bitmap ));
247 bitmap = ntohs( bitmap );
248 ibuf += sizeof( bitmap );
250 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
251 return( AFPERR_NOOBJ );
254 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
255 return( AFPERR_NOOBJ );
259 * If ibuf is odd, make it even.
261 if ((u_long)ibuf & 1 ) {
265 if (S_ISDIR(st.st_mode)) {
266 rc = setdirparams(vol, path, bitmap, ibuf );
268 rc = setfilparams(vol, path, bitmap, ibuf );
270 if ( rc == AFP_OK ) {
271 setvoltime(obj, vol );
275 syslog(LOG_INFO, "end afp_setfildirparams:");
281 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
284 int ibuflen, *rbuflen;
289 struct dir *dir, *odir = NULL;
290 char *path, *buf, *upath, *newpath;
295 #if AD_VERSION > AD_VERSION1
297 #endif /* AD_VERSION > AD_VERSION1 */
300 syslog(LOG_INFO, "begin afp_rename:");
306 memcpy( &vid, ibuf, sizeof( vid ));
307 ibuf += sizeof( vid );
308 if (( vol = getvolbyvid( vid )) == NULL ) {
309 return( AFPERR_PARAM );
312 if (vol->v_flags & AFPVOL_RO)
315 memcpy( &did, ibuf, sizeof( did ));
316 ibuf += sizeof( did );
317 if (( dir = dirsearch( vol, did )) == NULL ) {
318 return( AFPERR_NOOBJ );
321 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
322 return( AFPERR_NOOBJ );
325 /* another place where we know about the path type */
326 if ( *ibuf++ != 2 ) {
327 return( AFPERR_PARAM );
329 plen = (unsigned char) *ibuf++;
330 *( ibuf + plen ) = '\0';
332 if ( *path == '\0' ) {
333 if ( curdir->d_parent == NULL ) { /* root directory */
334 return( AFPERR_NORENAME );
337 path = curdir->d_name;
338 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
339 return( AFPERR_NOOBJ );
344 if ( strcasecmp( path, ibuf ) == 0 ) {
349 /* if a curdir/newname ofork exists, return busy */
350 if (of_findname(vol, curdir, ibuf))
353 /* source == destination. just say okay. */
354 if (strcmp(path, ibuf) == 0)
357 /* check for illegal characters */
358 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
359 strpbrk(ibuf, MSWINDOWS_BADCHARS))
362 newpath = obj->oldtmp;
363 strcpy( newpath, mtoupath(vol, ibuf ));
365 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(newpath, '/'))
368 if (!validupath(vol, newpath))
371 /* the strdiacasecmp deals with case-insensitive, case preserving
373 if (stat( newpath, &st ) == 0 && strdiacasecmp(path, ibuf))
374 return( AFPERR_EXIST );
376 upath = mtoupath(vol, path);
378 #if AD_VERSION > AD_VERSION1
379 id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
380 #endif /* AD_VERSION > AD_VERSION1 */
382 if ( rename( upath, newpath ) < 0 ) {
385 return( AFPERR_NOOBJ );
387 return( AFPERR_ACCESS );
389 return( AFPERR_PARAM );
393 #if AD_VERSION > AD_VERSION1
394 if (stat(newpath, &st) < 0) /* this shouldn't fail */
396 cnid_update(vol->v_db, id, &st, curdir->d_did, newpath, strlen(newpath));
397 #endif /* AD_VERSION > AD_VERSION1 */
400 newadpath = obj->newtmp;
401 strcpy( newadpath, ad_path( newpath, 0 ));
402 if ( rename( ad_path( upath, 0 ), newadpath ) < 0 ) {
403 if ( errno == ENOENT ) { /* no adouble header file */
404 if (( unlink( newadpath ) < 0 ) && ( errno != ENOENT )) {
405 return( AFPERR_PARAM );
409 return( AFPERR_PARAM );
412 memset(&ad, 0, sizeof(ad));
413 if ( ad_open( newpath, ADFLAGS_HF, O_RDWR|O_CREAT, 0666,
415 return( AFPERR_PARAM );
420 memset(&ad, 0, sizeof(ad));
421 if ( ad_open( newpath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
422 O_RDWR|O_CREAT, 0666, &ad) < 0 ) {
423 if (!((errno == ENOENT) && vol_noadouble(vol)))
424 return( AFPERR_PARAM );
427 if ((buf = realloc( odir->d_name, plen + 1 )) == NULL ) {
428 syslog( LOG_ERR, "afp_rename: realloc: %m" );
430 ad_flush(&ad, ADFLAGS_HF); /* in case of create */
431 ad_close(&ad, ADFLAGS_HF);
436 strcpy( odir->d_name, ibuf );
441 ad_setentrylen( &ad, ADEID_NAME, plen );
442 memcpy( ad_entry( &ad, ADEID_NAME ), ibuf, plen );
443 ad_flush( &ad, ADFLAGS_HF );
444 ad_close( &ad, ADFLAGS_HF );
447 setvoltime(obj, vol );
449 /* if it's still open, rename the ofork as well. */
450 if (of_rename(vol, curdir, path, curdir, ibuf) < 0)
454 syslog(LOG_INFO, "end afp_rename:");
461 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
464 int ibuflen, *rbuflen;
473 syslog(LOG_INFO, "begin afp_delete:");
479 memcpy( &vid, ibuf, sizeof( vid ));
480 ibuf += sizeof( vid );
481 if (( vol = getvolbyvid( vid )) == NULL ) {
482 return( AFPERR_PARAM );
485 if (vol->v_flags & AFPVOL_RO)
488 memcpy( &did, ibuf, sizeof( did ));
489 ibuf += sizeof( int );
490 if (( dir = dirsearch( vol, did )) == NULL ) {
491 return( AFPERR_NOOBJ );
494 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
495 return( AFPERR_NOOBJ );
498 if ( *path == '\0' ) {
499 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
500 } else if (of_findname(vol, curdir, path)) {
502 } else if ((rc = deletefile( upath = mtoupath(vol, path ))) == AFP_OK) {
503 #if AD_VERSION > AD_VERSION1 /* get rid of entry */
504 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
505 cnid_delete(vol->v_db, id);
506 #endif /* AD_VERSION > AD_VERSION1 */
508 if ( rc == AFP_OK ) {
509 setvoltime(obj, vol );
513 syslog(LOG_INFO, "end afp_delete:");
519 char *ctoupath( vol, dir, name )
520 const struct vol *vol;
525 static char path[ MAXPATHLEN + 1];
529 p = path + sizeof( path ) - 1;
531 u = mtoupath(vol, name );
534 strncpy( p, u, len );
535 for ( d = dir; d->d_parent; d = d->d_parent ) {
537 u = mtoupath(vol, d->d_name );
540 strncpy( p, u, len );
543 len = strlen( vol->v_path );
545 strncpy( p, vol->v_path, len );
551 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
554 int ibuflen, *rbuflen;
557 struct dir *sdir, *ddir, *odir = NULL;
559 char *oldname, *newname;
560 char *path, *p, *upath;
564 #if AD_VERSION > AD_VERSION1
566 #endif /* AD_VERSION > AD_VERSION1 */
569 #endif /* DROPKLUDGE */
572 syslog(LOG_INFO, "begin afp_moveandrename:");
578 memcpy( &vid, ibuf, sizeof( vid ));
579 ibuf += sizeof( vid );
580 if (( vol = getvolbyvid( vid )) == NULL ) {
581 return( AFPERR_PARAM );
584 if (vol->v_flags & AFPVOL_RO)
587 /* source did followed by dest did */
588 memcpy( &did, ibuf, sizeof( did ));
589 ibuf += sizeof( int );
590 if (( sdir = dirsearch( vol, did )) == NULL ) {
591 return( AFPERR_PARAM );
594 memcpy( &did, ibuf, sizeof( did ));
595 ibuf += sizeof( int );
597 /* source pathname */
598 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
599 return( AFPERR_NOOBJ );
603 newname = obj->newtmp;
604 oldname = obj->oldtmp;
605 if ( *path != '\0' ) {
606 /* not a directory */
607 strcpy(newname, path);
608 strcpy(oldname, path); /* an extra copy for of_rename */
609 #if AD_VERSION > AD_VERSION1
610 p = mtoupath(vol, path);
611 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
612 #endif /* AD_VERSION > AD_VERSION1 */
613 p = ctoupath( vol, sdir, newname );
616 strcpy( newname, odir->d_name );
617 strcpy(oldname, odir->d_name);
618 p = ctoupath( vol, odir->d_parent, newname );
619 #if AD_VERSION > AD_VERSION1
620 id = curdir->d_did; /* we already have the CNID */
621 #endif /* AD_VERSION > AD_VERSION1 */
624 * p now points to the full pathname of the source fs object.
627 /* get the destination directory */
628 if (( ddir = dirsearch( vol, did )) == NULL ) {
629 return( AFPERR_PARAM );
631 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
632 return( AFPERR_NOOBJ );
634 if ( *path != '\0' ) {
635 return( AFPERR_BADTYPE );
638 /* one more place where we know about path type */
639 if ( *ibuf++ != 2 ) {
640 return( AFPERR_PARAM );
643 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
644 strncpy( newname, ibuf, plen );
645 newname[ plen ] = '\0';
648 /* check for illegal characters */
649 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
650 strpbrk(newname, MSWINDOWS_BADCHARS))
653 upath = mtoupath(vol, newname);
655 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
658 if (!validupath(vol, upath))
661 /* source == destination. we just silently accept this. */
662 if (curdir == sdir) {
663 if (strcmp(oldname, newname) == 0)
666 /* deal with case insensitive, case-preserving filesystems. */
667 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
670 } else if (stat(upath, &st ) == 0)
671 return( AFPERR_EXIST );
674 if (of_findname(vol, curdir, newname)) {
676 } else if ((rc = renamefile( p, upath, newname,
677 vol_noadouble(vol) )) == AFP_OK) {
678 /* if it's still open, rename the ofork as well. */
679 rc = of_rename(vol, sdir, oldname, curdir, newname);
682 rc = renamedir(p, upath, odir, curdir, newname, vol_noadouble(vol));
686 if (vol->v_flags & AFPVOL_DROPBOX) {
687 if (retvalue=matchfile2dirperms (newname, vol, did) != AFP_OK) {
691 #endif /* DROPKLUDGE */
693 if ( rc == AFP_OK ) {
694 #if AD_VERSION > AD_VERSION1
695 /* renaming may have moved the file/dir across a filesystem */
696 if (stat(upath, &st) < 0)
699 /* fix up the catalog entry */
700 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
701 #endif /* AD_VERSION > AD_VERSION1 */
702 setvoltime(obj, vol );
706 syslog(LOG_INFO, "end afp_moveandrename:");