2 * $Id: filedir.c,v 1.29 2002-09-04 17:28:08 didg 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 <atalk/logger.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 LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
77 if (stat(upath, &st ) < 0)
78 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
79 strcpy (adpath, "./.AppleDouble/");
80 strcat (adpath, upath);
81 if (( dir = dirsearch( vol, did )) == NULL ) {
82 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
83 return( AFPERR_NOOBJ );
85 else if (stat(".", &sb) < 0) {
86 LOG(log_error, logtype_afpd,
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)
98 LOG(log_error, logtype_afpd,
99 "matchfile2dirperms: Error changing owner/gid of %s: %s",
100 upath, strerror(errno));
101 return (AFPERR_ACCESS);
103 if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
105 LOG(log_error, logtype_afpd,
106 "matchfile2dirperms: Error adding file read permissions: %s",
108 return (AFPERR_ACCESS);
112 LOG(log_info, logtype_afpd,
113 "matchfile2dirperms: Added S_IRGRP and S_IROTH: %s",
116 if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
118 LOG(log_error, logtype_afpd,
119 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %s",
120 adpath, strerror(errno));
121 return (AFPERR_ACCESS);
123 if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
125 LOG(log_error, logtype_afpd,
126 "matchfile2dirperms: Error adding AD file read permissions: %s",
128 return (AFPERR_ACCESS);
132 LOG(log_info, logtype_afpd,
133 "matchfile2dirperms: Added S_IRGRP and S_IROTH to AD: %s",
139 LOG(log_info, logtype_afpd,
140 "matchfile2dirperms: No ownership change necessary.");
142 } /* end else if stat success */
143 seteuid(uid); /* Restore process ownership to normal */
145 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
153 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
156 int ibuflen, *rbuflen;
164 u_int16_t fbitmap, dbitmap, vid;
167 LOG(log_info, logtype_afpd, "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 = dirlookup( 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 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
236 * We can't use unix file's perm to support Apple's inherited protection modes.
237 * If we aren't the file's owner we can't change its perms when moving it and smb
238 * nfs,... don't even try.
240 #define AFP_CHECK_ACCESS
242 int check_access(char *path, int mode)
244 #ifdef AFP_CHECK_ACCESS
252 accessmode(p, &ma, curdir, NULL);
253 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
255 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
261 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
264 int ibuflen, *rbuflen;
270 u_int16_t vid, bitmap;
274 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
279 memcpy( &vid, ibuf, sizeof(vid));
280 ibuf += sizeof( vid );
282 if (( vol = getvolbyvid( vid )) == NULL ) {
283 return( AFPERR_PARAM );
286 if (vol->v_flags & AFPVOL_RO)
289 memcpy( &did, ibuf, sizeof( did));
290 ibuf += sizeof( did);
292 if (( dir = dirsearch( vol, did )) == NULL ) {
293 return( AFPERR_NOOBJ );
296 memcpy( &bitmap, ibuf, sizeof( bitmap ));
297 bitmap = ntohs( bitmap );
298 ibuf += sizeof( bitmap );
300 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
301 return( AFPERR_NOOBJ );
304 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
305 return( AFPERR_NOOBJ );
309 * If ibuf is odd, make it even.
311 if ((u_long)ibuf & 1 ) {
315 if (S_ISDIR(st.st_mode)) {
316 rc = setdirparams(vol, path, bitmap, ibuf );
318 rc = setfilparams(vol, path, bitmap, ibuf );
320 if ( rc == AFP_OK ) {
321 setvoltime(obj, vol );
325 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
331 /* --------------------------------------------
332 Factorise some check on a pathname
334 int check_name(const struct vol *vol, char *name)
336 /* check for illegal characters in the unix filename */
337 if (!wincheck(vol, name))
340 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
343 if (!validupath(vol, name))
346 /* check for vetoed filenames */
347 if (veto_file(vol->v_veto, name))
352 /* -------------------------
353 move and rename sdir:oldname to curdir:newname in volume vol
355 special care is needed for lock
357 static int moveandrename(vol, sdir, oldname, newname, isdir)
358 const struct vol *vol;
371 struct ofork *opened;
377 memset(&ad, 0, sizeof(ad));
383 p = mtoupath(vol, oldname);
384 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
386 p = ctoupath( vol, sdir, oldname );
387 if ((opened = of_findname(vol, sdir, p, NULL))) {
388 /* reuse struct adouble so it won't break locks */
394 id = sdir->d_did; /* we already have the CNID */
396 p = ctoupath( vol, sdir->d_parent, oldname );
397 adflags = ADFLAGS_DIR;
400 * p now points to the full pathname of the source fs object.
402 * we are in the dest folder so we need to use p for ad_open
405 if (!ad_open(p, ADFLAGS_HF |adflags, O_RDONLY, 0666, adp)) {
408 ad_getattr(adp, &bshort);
409 ad_close( adp, ADFLAGS_HF );
410 if ((bshort & htons(ATTRBIT_NORENAME)))
411 return(AFPERR_OLOCK);
414 upath = mtoupath(vol, newname);
415 if (0 != (rc = check_name(vol, upath))) {
419 /* source == destination. we just silently accept this. */
420 if (curdir == sdir) {
421 if (strcmp(oldname, newname) == 0)
424 /* deal with case insensitive, case-preserving filesystems. */
425 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
428 } else if (stat(upath, &st ) == 0)
432 if (of_findname(vol, curdir, upath, &st)) {
433 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
435 rc = renamefile( p, upath, newname,vol_noadouble(vol), adp );
437 of_rename(vol, opened, sdir, oldname, curdir, newname);
440 rc = renamedir(p, upath, sdir, curdir, newname, vol_noadouble(vol));
442 if ( rc == AFP_OK ) {
444 /* renaming may have moved the file/dir across a filesystem */
445 if (stat(upath, &st) < 0)
448 /* fix up the catalog entry */
449 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
456 /* -------------------------------------------- */
457 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
460 int ibuflen, *rbuflen;
464 char *path, *oldname, *newname;
471 LOG(log_info, logtype_afpd, "begin afp_rename:");
477 memcpy( &vid, ibuf, sizeof( vid ));
478 ibuf += sizeof( vid );
479 if (( vol = getvolbyvid( vid )) == NULL ) {
480 return( AFPERR_PARAM );
483 if (vol->v_flags & AFPVOL_RO)
486 memcpy( &did, ibuf, sizeof( did ));
487 ibuf += sizeof( did );
488 if (( sdir = dirsearch( vol, did )) == NULL ) {
489 return( AFPERR_NOOBJ );
492 /* source pathname */
493 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
494 return( AFPERR_NOOBJ );
498 newname = obj->newtmp;
499 oldname = obj->oldtmp;
500 if ( *path != '\0' ) {
501 strcpy(oldname, path); /* an extra copy for of_rename */
504 if ( sdir->d_parent == NULL ) { /* root directory */
505 return( AFPERR_NORENAME );
507 /* move to destination dir */
508 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
509 return( AFPERR_NOOBJ );
512 strcpy(oldname, sdir->d_name);
515 /* another place where we know about the path type */
516 if ( *ibuf++ != 2 ) {
517 return( AFPERR_PARAM );
520 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
521 strncpy( newname, ibuf, plen );
522 newname[ plen ] = '\0';
523 if (strlen(newname) != plen) {
524 return( AFPERR_PARAM );
528 return AFP_OK; /* newname == oldname same dir */
531 rc = moveandrename(vol, sdir, oldname, newname, isdir);
533 if ( rc == AFP_OK ) {
534 setvoltime(obj, vol );
538 LOG(log_info, logtype_afpd, "end afp_rename:");
544 /* ------------------------------- */
545 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
548 int ibuflen, *rbuflen;
557 LOG(log_info, logtype_afpd, "begin afp_delete:");
563 memcpy( &vid, ibuf, sizeof( vid ));
564 ibuf += sizeof( vid );
565 if (( vol = getvolbyvid( vid )) == NULL ) {
566 return( AFPERR_PARAM );
569 if (vol->v_flags & AFPVOL_RO)
572 memcpy( &did, ibuf, sizeof( did ));
573 ibuf += sizeof( int );
574 if (( dir = dirsearch( vol, did )) == NULL ) {
575 return( AFPERR_NOOBJ );
578 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
579 return( AFPERR_NOOBJ );
582 upath = mtoupath(vol, path );
583 if ( *path == '\0' ) {
584 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
585 } else if (of_findname(vol, curdir, upath, NULL)) {
587 } else if ((rc = deletefile( upath, 1)) == AFP_OK) {
588 #ifdef CNID_DB /* get rid of entry */
589 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
590 cnid_delete(vol->v_db, id);
593 if ( rc == AFP_OK ) {
594 setvoltime(obj, vol );
598 LOG(log_info, logtype_afpd, "end afp_delete:");
604 char *ctoupath( vol, dir, name )
605 const struct vol *vol;
610 static char path[ MAXPATHLEN + 1];
614 p = path + sizeof( path ) - 1;
616 u = mtoupath(vol, name );
619 strncpy( p, u, len );
620 for ( d = dir; d->d_parent; d = d->d_parent ) {
622 u = mtoupath(vol, d->d_name );
625 strncpy( p, u, len );
628 len = strlen( vol->v_path );
630 strncpy( p, vol->v_path, len );
635 /* ------------------------- */
636 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
639 int ibuflen, *rbuflen;
642 struct dir *sdir, *ddir;
644 char *oldname, *newname;
652 #endif /* DROPKLUDGE */
655 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
661 memcpy( &vid, ibuf, sizeof( vid ));
662 ibuf += sizeof( vid );
663 if (( vol = getvolbyvid( vid )) == NULL ) {
664 return( AFPERR_PARAM );
667 if (vol->v_flags & AFPVOL_RO)
670 /* source did followed by dest did */
671 memcpy( &did, ibuf, sizeof( did ));
672 ibuf += sizeof( int );
673 if (( sdir = dirsearch( vol, did )) == NULL ) {
674 return( AFPERR_PARAM );
677 memcpy( &did, ibuf, sizeof( did ));
678 ibuf += sizeof( int );
680 /* source pathname */
681 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
682 return( AFPERR_NOOBJ );
686 newname = obj->newtmp;
687 oldname = obj->oldtmp;
688 if ( *path != '\0' ) {
689 /* not a directory */
690 strcpy(oldname, path); /* an extra copy for of_rename */
693 strcpy(oldname, sdir->d_name);
696 /* get the destination directory */
697 if (( ddir = dirsearch( vol, did )) == NULL ) {
698 return( AFPERR_PARAM );
700 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
701 return( AFPERR_NOOBJ );
703 if ( *path != '\0' ) {
704 return( AFPERR_BADTYPE );
707 /* one more place where we know about path type */
708 if ( *ibuf++ != 2 ) {
709 return( AFPERR_PARAM );
712 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
713 strncpy( newname, ibuf, plen );
714 newname[ plen ] = '\0';
715 if (strlen(newname) != plen) {
716 return( AFPERR_PARAM );
720 strcpy(newname, oldname);
723 rc = moveandrename(vol, sdir, oldname, newname, isdir);
725 if ( rc == AFP_OK ) {
727 if (vol->v_flags & AFPVOL_DROPBOX) {
728 if (retvalue=matchfile2dirperms (newname, vol, did) != AFP_OK) {
733 #endif /* DROPKLUDGE */
735 char *upath = mtoupath(vol, newname);
736 int admode = ad_mode("", 0777);
738 setfilmode(upath, admode, NULL);
739 setfilmode(ad_path( upath, ADFLAGS_HF ), ad_hf_mode(admode), NULL);
741 setvoltime(obj, vol );
745 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
751 int veto_file(const char*veto_str, const char*path)
752 /* given a veto_str like "abc/zxc/" and path "abc", return 1
753 * veto_str should be '/' delimited
754 * if path matches any one of the veto_str elements exactly, then 1 is returned
755 * otherwise, 0 is returned.
758 int i; /* index to veto_str */
759 int j; /* index to path */
761 if ((veto_str == NULL) || (path == NULL))
765 LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
768 for(i=0, j=0; veto_str[i] != '\0'; i++) {
769 if (veto_str[i] == '/') {
770 if ((j>0) && (path[j] == '\0'))
774 if (veto_str[i] != path[j]) {
775 while ((veto_str[i] != '/')
776 && (veto_str[i] != '\0'))