2 * $Id: filedir.c,v 1.9 2001-06-06 19:04:25 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>
29 #include "directory.h"
37 int matchfile2dirperms(upath, vol, did)
38 /* Since it's kinda' big; I decided against an
43 /* The below code changes the way file ownership is determined in the name of
44 fixing dropboxes. It has known security problem. See the netatalk FAQ for
53 syslog (LOG_INFO, "begin matchfile2dirperms:");
56 if (stat(upath, &st ) < 0)
57 syslog(LOG_ERR, "Could not stat %s: %m", upath);
58 strcpy (adpath, "./.AppleDouble/");
59 strcat (adpath, upath);
60 if (( dir = dirsearch( vol, did )) == NULL ) {
61 syslog (LOG_ERR, "matchfile2dirperms: Unable to get directory info.");
62 return( AFPERR_NOOBJ );
64 else if (stat(".", &sb) < 0) {
66 "matchfile2dirperms: Error checking directory \"%s\": %m",
68 return(AFPERR_NOOBJ );
72 if ( uid != sb.st_uid )
75 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
78 "matchfile2dirperms: Error changing owner/gid of %s: %m", upath);
79 return (AFPERR_ACCESS);
81 if (chmod(upath,(st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
84 "matchfile2dirperms: Error adding file read permissions: %m");
85 return (AFPERR_ACCESS);
90 "matchfile2dirperms: Added S_IRGRP and S_IROTH: %m");
92 if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
95 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %m",
97 return (AFPERR_ACCESS);
99 if (chmod(adpath, (st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
102 "matchfile2dirperms: Error adding AD file read permissions: %m");
103 return (AFPERR_ACCESS);
108 "matchfile2dirperms: Added S_IRGRP and S_IROTH to AD: %m");
114 "matchfile2dirperms: No ownership change necessary.");
116 } /* end else if stat success */
117 seteuid(uid); /* Restore process ownership to normal */
119 syslog (LOG_INFO, "end matchfile2dirperms:");
127 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
130 int ibuflen, *rbuflen;
138 u_int16_t fbitmap, dbitmap, vid;
141 syslog(LOG_INFO, "begin afp_getfildirparams:");
147 memcpy( &vid, ibuf, sizeof( vid ));
148 ibuf += sizeof( vid );
149 if (( vol = getvolbyvid( vid )) == NULL ) {
150 return( AFPERR_PARAM );
153 memcpy( &did, ibuf, sizeof( did ));
154 ibuf += sizeof( did );
156 if (( dir = dirsearch( vol, did )) == NULL ) {
157 return( AFPERR_NOOBJ );
160 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
161 fbitmap = ntohs( fbitmap );
162 ibuf += sizeof( fbitmap );
163 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
164 dbitmap = ntohs( dbitmap );
165 ibuf += sizeof( dbitmap );
167 if (( path = cname( vol, dir, &ibuf )) == NULL) {
168 return( AFPERR_NOOBJ );
171 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
172 return( AFPERR_NOOBJ );
176 if (S_ISDIR(st.st_mode)) {
177 if (dbitmap && ( ret = getdirparams(vol, dbitmap, ".", curdir,
178 &st, rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
181 /* this is a directory */
182 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISDIR;
184 if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
185 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
189 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
191 *rbuflen = buflen + 3 * sizeof( u_int16_t );
192 fbitmap = htons( fbitmap );
193 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
194 rbuf += sizeof( fbitmap );
195 dbitmap = htons( dbitmap );
196 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
197 rbuf += sizeof( dbitmap ) + sizeof( u_char );
201 syslog(LOG_INFO, "end afp_getfildirparams:");
207 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
210 int ibuflen, *rbuflen;
216 u_int16_t vid, bitmap;
220 syslog(LOG_INFO, "begin afp_setfildirparams:");
225 memcpy( &vid, ibuf, sizeof(vid));
226 ibuf += sizeof( vid );
228 if (( vol = getvolbyvid( vid )) == NULL ) {
229 return( AFPERR_PARAM );
232 if (vol->v_flags & AFPVOL_RO)
235 memcpy( &did, ibuf, sizeof( did));
236 ibuf += sizeof( did);
238 if (( dir = dirsearch( vol, did )) == NULL ) {
239 return( AFPERR_NOOBJ );
242 memcpy( &bitmap, ibuf, sizeof( bitmap ));
243 bitmap = ntohs( bitmap );
244 ibuf += sizeof( bitmap );
246 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
247 return( AFPERR_NOOBJ );
250 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
251 return( AFPERR_NOOBJ );
255 * If ibuf is odd, make it even.
257 if ((u_long)ibuf & 1 ) {
261 if (S_ISDIR(st.st_mode)) {
262 rc = setdirparams(vol, path, bitmap, ibuf );
264 rc = setfilparams(vol, path, bitmap, ibuf );
266 if ( rc == AFP_OK ) {
267 setvoltime(obj, vol );
271 syslog(LOG_INFO, "end afp_setfildirparams:");
277 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
280 int ibuflen, *rbuflen;
285 struct dir *dir, *odir = NULL;
286 char *path, *buf, *upath, *newpath;
291 #if AD_VERSION > AD_VERSION1
296 syslog(LOG_INFO, "begin afp_rename:");
302 memcpy( &vid, ibuf, sizeof( vid ));
303 ibuf += sizeof( vid );
304 if (( vol = getvolbyvid( vid )) == NULL ) {
305 return( AFPERR_PARAM );
308 if (vol->v_flags & AFPVOL_RO)
311 memcpy( &did, ibuf, sizeof( did ));
312 ibuf += sizeof( did );
313 if (( dir = dirsearch( vol, did )) == NULL ) {
314 return( AFPERR_NOOBJ );
317 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
318 return( AFPERR_NOOBJ );
321 /* another place where we know about the path type */
322 if ( *ibuf++ != 2 ) {
323 return( AFPERR_PARAM );
325 plen = (unsigned char) *ibuf++;
326 *( ibuf + plen ) = '\0';
328 if ( *path == '\0' ) {
329 if ( curdir->d_parent == NULL ) { /* root directory */
330 return( AFPERR_NORENAME );
333 path = curdir->d_name;
334 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
335 return( AFPERR_NOOBJ );
340 if ( strcasecmp( path, ibuf ) == 0 ) {
345 /* if a curdir/newname ofork exists, return busy */
346 if (of_findname(vol, curdir, ibuf))
349 /* source == destination. just say okay. */
350 if (strcmp(path, ibuf) == 0)
353 /* check for illegal characters */
354 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
355 strpbrk(ibuf, MSWINDOWS_BADCHARS))
358 newpath = obj->oldtmp;
359 strcpy( newpath, mtoupath(vol, ibuf ));
361 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(newpath, '/'))
364 if (!validupath(vol, newpath))
367 /* the strdiacasecmp deals with case-insensitive, case preserving
369 if (stat( newpath, &st ) == 0 && strdiacasecmp(path, ibuf))
370 return( AFPERR_EXIST );
372 upath = mtoupath(vol, path);
374 #if AD_VERSION > AD_VERSION1
375 id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
378 if ( rename( upath, newpath ) < 0 ) {
381 return( AFPERR_NOOBJ );
383 return( AFPERR_ACCESS );
385 return( AFPERR_PARAM );
389 #if AD_VERSION > AD_VERSION1
390 if (stat(newpath, &st) < 0) /* this shouldn't fail */
392 cnid_update(vol->v_db, id, &st, curdir->d_did, newpath, strlen(newpath));
396 newadpath = obj->newtmp;
397 strcpy( newadpath, ad_path( newpath, 0 ));
398 if ( rename( ad_path( upath, 0 ), newadpath ) < 0 ) {
399 if ( errno == ENOENT ) { /* no adouble header file */
400 if (( unlink( newadpath ) < 0 ) && ( errno != ENOENT )) {
401 return( AFPERR_PARAM );
405 return( AFPERR_PARAM );
408 memset(&ad, 0, sizeof(ad));
409 if ( ad_open( newpath, ADFLAGS_HF, O_RDWR|O_CREAT, 0666,
411 return( AFPERR_PARAM );
416 memset(&ad, 0, sizeof(ad));
417 if ( ad_open( newpath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
418 O_RDWR|O_CREAT, 0666, &ad) < 0 ) {
419 if (!((errno == ENOENT) && vol_noadouble(vol)))
420 return( AFPERR_PARAM );
423 if ((buf = realloc( odir->d_name, plen + 1 )) == NULL ) {
424 syslog( LOG_ERR, "afp_rename: realloc: %m" );
426 ad_flush(&ad, ADFLAGS_HF); /* in case of create */
427 ad_close(&ad, ADFLAGS_HF);
432 strcpy( odir->d_name, ibuf );
437 ad_setentrylen( &ad, ADEID_NAME, plen );
438 memcpy( ad_entry( &ad, ADEID_NAME ), ibuf, plen );
439 ad_flush( &ad, ADFLAGS_HF );
440 ad_close( &ad, ADFLAGS_HF );
443 setvoltime(obj, vol );
445 /* if it's still open, rename the ofork as well. */
446 if (of_rename(vol, curdir, path, curdir, ibuf) < 0)
450 syslog(LOG_INFO, "end afp_rename:");
457 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
460 int ibuflen, *rbuflen;
469 syslog(LOG_INFO, "begin afp_delete:");
475 memcpy( &vid, ibuf, sizeof( vid ));
476 ibuf += sizeof( vid );
477 if (( vol = getvolbyvid( vid )) == NULL ) {
478 return( AFPERR_PARAM );
481 if (vol->v_flags & AFPVOL_RO)
484 memcpy( &did, ibuf, sizeof( did ));
485 ibuf += sizeof( int );
486 if (( dir = dirsearch( vol, did )) == NULL ) {
487 return( AFPERR_NOOBJ );
490 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
491 return( AFPERR_NOOBJ );
494 if ( *path == '\0' ) {
495 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
496 } else if (of_findname(vol, curdir, path)) {
498 } else if ((rc = deletefile( upath = mtoupath(vol, path ))) == AFP_OK) {
499 #if AD_VERSION > AD_VERSION1 /* get rid of entry */
500 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
501 cnid_delete(vol->v_db, id);
504 if ( rc == AFP_OK ) {
505 setvoltime(obj, vol );
509 syslog(LOG_INFO, "end afp_delete:");
515 char *ctoupath( vol, dir, name )
516 const struct vol *vol;
521 static char path[ MAXPATHLEN + 1];
525 p = path + sizeof( path ) - 1;
527 u = mtoupath(vol, name );
530 strncpy( p, u, len );
531 for ( d = dir; d->d_parent; d = d->d_parent ) {
533 u = mtoupath(vol, d->d_name );
536 strncpy( p, u, len );
539 len = strlen( vol->v_path );
541 strncpy( p, vol->v_path, len );
547 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
550 int ibuflen, *rbuflen;
553 struct dir *sdir, *ddir, *odir = NULL;
555 char *oldname, *newname;
556 char *path, *p, *upath;
560 #if AD_VERSION > AD_VERSION1
565 #endif /* DROPKLUDGE */
568 syslog(LOG_INFO, "begin afp_moveandrename:");
574 memcpy( &vid, ibuf, sizeof( vid ));
575 ibuf += sizeof( vid );
576 if (( vol = getvolbyvid( vid )) == NULL ) {
577 return( AFPERR_PARAM );
580 if (vol->v_flags & AFPVOL_RO)
583 /* source did followed by dest did */
584 memcpy( &did, ibuf, sizeof( did ));
585 ibuf += sizeof( int );
586 if (( sdir = dirsearch( vol, did )) == NULL ) {
587 return( AFPERR_PARAM );
590 memcpy( &did, ibuf, sizeof( did ));
591 ibuf += sizeof( int );
593 /* source pathname */
594 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
595 return( AFPERR_NOOBJ );
599 newname = obj->newtmp;
600 oldname = obj->oldtmp;
601 if ( *path != '\0' ) {
602 /* not a directory */
603 strcpy(newname, path);
604 strcpy(oldname, path); /* an extra copy for of_rename */
605 #if AD_VERSION > AD_VERSION1
606 p = mtoupath(vol, path);
607 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
609 p = ctoupath( vol, sdir, newname );
612 strcpy( newname, odir->d_name );
613 strcpy(oldname, odir->d_name);
614 p = ctoupath( vol, odir->d_parent, newname );
615 #if AD_VERSION > AD_VERSION1
616 id = curdir->d_did; /* we already have the CNID */
620 * p now points to the full pathname of the source fs object.
623 /* get the destination directory */
624 if (( ddir = dirsearch( vol, did )) == NULL ) {
625 return( AFPERR_PARAM );
627 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
628 return( AFPERR_NOOBJ );
630 if ( *path != '\0' ) {
631 return( AFPERR_BADTYPE );
634 /* one more place where we know about path type */
635 if ( *ibuf++ != 2 ) {
636 return( AFPERR_PARAM );
639 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
640 strncpy( newname, ibuf, plen );
641 newname[ plen ] = '\0';
644 /* check for illegal characters */
645 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
646 strpbrk(newname, MSWINDOWS_BADCHARS))
649 upath = mtoupath(vol, newname);
651 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
654 if (!validupath(vol, upath))
657 /* source == destination. we just silently accept this. */
658 if (curdir == sdir) {
659 if (strcmp(oldname, newname) == 0)
662 /* deal with case insensitive, case-preserving filesystems. */
663 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
666 } else if (stat(upath, &st ) == 0)
667 return( AFPERR_EXIST );
670 if (of_findname(vol, curdir, newname)) {
672 } else if ((rc = renamefile( p, upath, newname,
673 vol_noadouble(vol) )) == AFP_OK) {
674 /* if it's still open, rename the ofork as well. */
675 rc = of_rename(vol, sdir, oldname, curdir, newname);
678 rc = renamedir(p, upath, odir, curdir, newname, vol_noadouble(vol));
682 if (vol->v_flags & AFPVOL_DROPBOX) {
683 if (retvalue=matchfile2dirperms (newname, vol, did) != AFP_OK) {
687 #endif /* DROPKLUDGE */
689 if ( rc == AFP_OK ) {
690 #if AD_VERSION > AD_VERSION1
691 /* renaming may have moved the file/dir across a filesystem */
692 if (stat(upath, &st) < 0)
695 /* fix up the catalog entry */
696 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
698 setvoltime(obj, vol );
702 syslog(LOG_INFO, "end afp_moveandrename:");