2 * $Id: filedir.c,v 1.12 2001-08-15 01:37:34 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 <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: %s", upath, strerror(errno));
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\": %s",
73 dir->d_name, strerror(errno));
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: %s",
85 upath, strerror(errno));
86 return (AFPERR_ACCESS);
88 if (chmod(upath,(st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
91 "matchfile2dirperms: Error adding file read permissions: %s",
93 return (AFPERR_ACCESS);
98 "matchfile2dirperms: Added S_IRGRP and S_IROTH: %s",
101 if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
104 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %s",
105 adpath, strerror(errno));
106 return (AFPERR_ACCESS);
108 if (chmod(adpath, (st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
111 "matchfile2dirperms: Error adding AD file read permissions: %s",
113 return (AFPERR_ACCESS);
118 "matchfile2dirperms: Added S_IRGRP and S_IROTH to AD: %s",
125 "matchfile2dirperms: No ownership change necessary.");
127 } /* end else if stat success */
128 seteuid(uid); /* Restore process ownership to normal */
130 syslog (LOG_INFO, "end matchfile2dirperms:");
138 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
141 int ibuflen, *rbuflen;
149 u_int16_t fbitmap, dbitmap, vid;
152 syslog(LOG_INFO, "begin afp_getfildirparams:");
158 memcpy( &vid, ibuf, sizeof( vid ));
159 ibuf += sizeof( vid );
160 if (( vol = getvolbyvid( vid )) == NULL ) {
161 return( AFPERR_PARAM );
164 memcpy( &did, ibuf, sizeof( did ));
165 ibuf += sizeof( did );
167 if (( dir = dirsearch( vol, did )) == NULL ) {
168 return( AFPERR_NOOBJ );
171 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
172 fbitmap = ntohs( fbitmap );
173 ibuf += sizeof( fbitmap );
174 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
175 dbitmap = ntohs( dbitmap );
176 ibuf += sizeof( dbitmap );
178 if (( path = cname( vol, dir, &ibuf )) == NULL) {
179 return( AFPERR_NOOBJ );
182 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
183 return( AFPERR_NOOBJ );
187 if (S_ISDIR(st.st_mode)) {
189 ret = getdirparams(vol, dbitmap, ".", curdir,
190 &st, rbuf + 3 * sizeof( u_int16_t ), &buflen );
194 /* this is a directory */
195 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
197 if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
198 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
202 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
204 *rbuflen = buflen + 3 * sizeof( u_int16_t );
205 fbitmap = htons( fbitmap );
206 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
207 rbuf += sizeof( fbitmap );
208 dbitmap = htons( dbitmap );
209 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
210 rbuf += sizeof( dbitmap ) + sizeof( u_char );
214 syslog(LOG_INFO, "end afp_getfildirparams:");
220 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
223 int ibuflen, *rbuflen;
229 u_int16_t vid, bitmap;
233 syslog(LOG_INFO, "begin afp_setfildirparams:");
238 memcpy( &vid, ibuf, sizeof(vid));
239 ibuf += sizeof( vid );
241 if (( vol = getvolbyvid( vid )) == NULL ) {
242 return( AFPERR_PARAM );
245 if (vol->v_flags & AFPVOL_RO)
248 memcpy( &did, ibuf, sizeof( did));
249 ibuf += sizeof( did);
251 if (( dir = dirsearch( vol, did )) == NULL ) {
252 return( AFPERR_NOOBJ );
255 memcpy( &bitmap, ibuf, sizeof( bitmap ));
256 bitmap = ntohs( bitmap );
257 ibuf += sizeof( bitmap );
259 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
260 return( AFPERR_NOOBJ );
263 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
264 return( AFPERR_NOOBJ );
268 * If ibuf is odd, make it even.
270 if ((u_long)ibuf & 1 ) {
274 if (S_ISDIR(st.st_mode)) {
275 rc = setdirparams(vol, path, bitmap, ibuf );
277 rc = setfilparams(vol, path, bitmap, ibuf );
279 if ( rc == AFP_OK ) {
280 setvoltime(obj, vol );
284 syslog(LOG_INFO, "end afp_setfildirparams:");
290 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
293 int ibuflen, *rbuflen;
298 struct dir *dir, *odir = NULL;
299 char *path, *buf, *upath, *newpath;
309 syslog(LOG_INFO, "begin afp_rename:");
315 memcpy( &vid, ibuf, sizeof( vid ));
316 ibuf += sizeof( vid );
317 if (( vol = getvolbyvid( vid )) == NULL ) {
318 return( AFPERR_PARAM );
321 if (vol->v_flags & AFPVOL_RO)
324 memcpy( &did, ibuf, sizeof( did ));
325 ibuf += sizeof( did );
326 if (( dir = dirsearch( vol, did )) == NULL ) {
327 return( AFPERR_NOOBJ );
330 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
331 return( AFPERR_NOOBJ );
334 /* another place where we know about the path type */
335 if ( *ibuf++ != 2 ) {
336 return( AFPERR_PARAM );
338 plen = (unsigned char) *ibuf++;
339 *( ibuf + plen ) = '\0';
341 if ( *path == '\0' ) {
342 if ( curdir->d_parent == NULL ) { /* root directory */
343 return( AFPERR_NORENAME );
346 path = curdir->d_name;
347 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
348 return( AFPERR_NOOBJ );
353 if ( strcasecmp( path, ibuf ) == 0 ) {
358 /* if a curdir/newname ofork exists, return busy */
359 if (of_findname(vol, curdir, ibuf))
362 /* source == destination. just say okay. */
363 if (strcmp(path, ibuf) == 0)
366 /* check for illegal characters */
367 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
368 strpbrk(ibuf, MSWINDOWS_BADCHARS))
371 newpath = obj->oldtmp;
372 strcpy( newpath, mtoupath(vol, ibuf ));
374 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(newpath, '/'))
377 if (!validupath(vol, newpath))
380 /* the strdiacasecmp deals with case-insensitive, case preserving
382 if (stat( newpath, &st ) == 0 && strdiacasecmp(path, ibuf))
383 return( AFPERR_EXIST );
385 upath = mtoupath(vol, path);
388 id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
391 if ( rename( upath, newpath ) < 0 ) {
394 return( AFPERR_NOOBJ );
396 return( AFPERR_ACCESS );
398 return( AFPERR_PARAM );
403 if (stat(newpath, &st) < 0) /* this shouldn't fail */
405 cnid_update(vol->v_db, id, &st, curdir->d_did, newpath, strlen(newpath));
409 newadpath = obj->newtmp;
410 strcpy( newadpath, ad_path( newpath, 0 ));
411 if ( rename( ad_path( upath, 0 ), newadpath ) < 0 ) {
412 if ( errno == ENOENT ) { /* no adouble header file */
413 if (( unlink( newadpath ) < 0 ) && ( errno != ENOENT )) {
414 return( AFPERR_PARAM );
418 return( AFPERR_PARAM );
421 memset(&ad, 0, sizeof(ad));
422 if ( ad_open( newpath, ADFLAGS_HF, O_RDWR|O_CREAT, 0666,
424 return( AFPERR_PARAM );
429 memset(&ad, 0, sizeof(ad));
430 if ( ad_open( newpath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
431 O_RDWR|O_CREAT, 0666, &ad) < 0 ) {
432 if (!((errno == ENOENT) && vol_noadouble(vol)))
433 return( AFPERR_PARAM );
436 if ((buf = realloc( odir->d_name, plen + 1 )) == NULL ) {
437 syslog( LOG_ERR, "afp_rename: realloc: %s", strerror(errno) );
439 ad_flush(&ad, ADFLAGS_HF); /* in case of create */
440 ad_close(&ad, ADFLAGS_HF);
445 strcpy( odir->d_name, ibuf );
450 ad_setentrylen( &ad, ADEID_NAME, plen );
451 memcpy( ad_entry( &ad, ADEID_NAME ), ibuf, plen );
452 ad_flush( &ad, ADFLAGS_HF );
453 ad_close( &ad, ADFLAGS_HF );
456 setvoltime(obj, vol );
458 /* if it's still open, rename the ofork as well. */
459 if (of_rename(vol, curdir, path, curdir, ibuf) < 0)
463 syslog(LOG_INFO, "end afp_rename:");
470 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
473 int ibuflen, *rbuflen;
482 syslog(LOG_INFO, "begin afp_delete:");
488 memcpy( &vid, ibuf, sizeof( vid ));
489 ibuf += sizeof( vid );
490 if (( vol = getvolbyvid( vid )) == NULL ) {
491 return( AFPERR_PARAM );
494 if (vol->v_flags & AFPVOL_RO)
497 memcpy( &did, ibuf, sizeof( did ));
498 ibuf += sizeof( int );
499 if (( dir = dirsearch( vol, did )) == NULL ) {
500 return( AFPERR_NOOBJ );
503 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
504 return( AFPERR_NOOBJ );
507 if ( *path == '\0' ) {
508 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
509 } else if (of_findname(vol, curdir, path)) {
511 } else if ((rc = deletefile( upath = mtoupath(vol, path ))) == AFP_OK) {
512 #ifdef CNID_DB /* get rid of entry */
513 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
514 cnid_delete(vol->v_db, id);
517 if ( rc == AFP_OK ) {
518 setvoltime(obj, vol );
522 syslog(LOG_INFO, "end afp_delete:");
528 char *ctoupath( vol, dir, name )
529 const struct vol *vol;
534 static char path[ MAXPATHLEN + 1];
538 p = path + sizeof( path ) - 1;
540 u = mtoupath(vol, name );
543 strncpy( p, u, len );
544 for ( d = dir; d->d_parent; d = d->d_parent ) {
546 u = mtoupath(vol, d->d_name );
549 strncpy( p, u, len );
552 len = strlen( vol->v_path );
554 strncpy( p, vol->v_path, len );
560 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
563 int ibuflen, *rbuflen;
566 struct dir *sdir, *ddir, *odir = NULL;
568 char *oldname, *newname;
569 char *path, *p, *upath;
578 #endif /* DROPKLUDGE */
581 syslog(LOG_INFO, "begin afp_moveandrename:");
587 memcpy( &vid, ibuf, sizeof( vid ));
588 ibuf += sizeof( vid );
589 if (( vol = getvolbyvid( vid )) == NULL ) {
590 return( AFPERR_PARAM );
593 if (vol->v_flags & AFPVOL_RO)
596 /* source did followed by dest did */
597 memcpy( &did, ibuf, sizeof( did ));
598 ibuf += sizeof( int );
599 if (( sdir = dirsearch( vol, did )) == NULL ) {
600 return( AFPERR_PARAM );
603 memcpy( &did, ibuf, sizeof( did ));
604 ibuf += sizeof( int );
606 /* source pathname */
607 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
608 return( AFPERR_NOOBJ );
612 newname = obj->newtmp;
613 oldname = obj->oldtmp;
614 if ( *path != '\0' ) {
615 /* not a directory */
616 strcpy(newname, path);
617 strcpy(oldname, path); /* an extra copy for of_rename */
619 p = mtoupath(vol, path);
620 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
622 p = ctoupath( vol, sdir, newname );
625 strcpy( newname, odir->d_name );
626 strcpy(oldname, odir->d_name);
627 p = ctoupath( vol, odir->d_parent, newname );
629 id = curdir->d_did; /* we already have the CNID */
633 * p now points to the full pathname of the source fs object.
636 /* get the destination directory */
637 if (( ddir = dirsearch( vol, did )) == NULL ) {
638 return( AFPERR_PARAM );
640 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
641 return( AFPERR_NOOBJ );
643 if ( *path != '\0' ) {
644 return( AFPERR_BADTYPE );
647 /* one more place where we know about path type */
648 if ( *ibuf++ != 2 ) {
649 return( AFPERR_PARAM );
652 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
653 strncpy( newname, ibuf, plen );
654 newname[ plen ] = '\0';
657 /* check for illegal characters */
658 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
659 strpbrk(newname, MSWINDOWS_BADCHARS))
662 upath = mtoupath(vol, newname);
664 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
667 if (!validupath(vol, upath))
670 /* source == destination. we just silently accept this. */
671 if (curdir == sdir) {
672 if (strcmp(oldname, newname) == 0)
675 /* deal with case insensitive, case-preserving filesystems. */
676 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
679 } else if (stat(upath, &st ) == 0)
680 return( AFPERR_EXIST );
683 if (of_findname(vol, curdir, newname)) {
685 } else if ((rc = renamefile( p, upath, newname,
686 vol_noadouble(vol) )) == AFP_OK) {
687 /* if it's still open, rename the ofork as well. */
688 rc = of_rename(vol, sdir, oldname, curdir, newname);
691 rc = renamedir(p, upath, odir, curdir, newname, vol_noadouble(vol));
695 if (vol->v_flags & AFPVOL_DROPBOX) {
696 if (retvalue=matchfile2dirperms (newname, vol, did) != AFP_OK) {
700 #endif /* DROPKLUDGE */
702 if ( rc == AFP_OK ) {
704 /* renaming may have moved the file/dir across a filesystem */
705 if (stat(upath, &st) < 0)
708 /* fix up the catalog entry */
709 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
711 setvoltime(obj, vol );
715 syslog(LOG_INFO, "end afp_moveandrename:");