2 * $Id: filedir.c,v 1.31 2002-09-07 19:18:59 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));
82 adpath = ad_path( upath, ADFLAGS_HF );
83 /* FIXME dirsearch doesn't move cwd to did ! */
84 if (( dir = dirsearch( vol, did )) == NULL ) {
85 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
88 else if (stat(".", &sb) < 0) {
89 LOG(log_error, logtype_afpd,
90 "matchfile2dirperms: Error checking directory \"%s\": %s",
91 dir->d_name, strerror(errno));
96 if ( uid != sb.st_uid )
99 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
101 LOG(log_error, logtype_afpd,
102 "matchfile2dirperms: Error changing owner/gid of %s: %s",
103 upath, strerror(errno));
106 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
108 LOG(log_error, logtype_afpd,
109 "matchfile2dirperms: Error adding file read permissions: %s",
113 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
115 LOG(log_error, logtype_afpd,
116 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %s",
117 adpath, strerror(errno));
120 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
122 LOG(log_error, logtype_afpd,
123 "matchfile2dirperms: Error adding AD file read permissions: %s",
129 } /* end else if stat success */
132 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
138 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
141 int ibuflen, *rbuflen;
149 u_int16_t fbitmap, dbitmap, vid;
152 LOG(log_info, logtype_afpd, "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 = dirlookup( 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 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
221 * We can't use unix file's perm to support Apple's inherited protection modes.
222 * If we aren't the file's owner we can't change its perms when moving it and smb
223 * nfs,... don't even try.
225 #define AFP_CHECK_ACCESS
227 int check_access(char *path, int mode)
229 #ifdef AFP_CHECK_ACCESS
237 accessmode(p, &ma, curdir, NULL);
238 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
240 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
246 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
249 int ibuflen, *rbuflen;
255 u_int16_t vid, bitmap;
259 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
264 memcpy( &vid, ibuf, sizeof(vid));
265 ibuf += sizeof( vid );
267 if (( vol = getvolbyvid( vid )) == NULL ) {
268 return( AFPERR_PARAM );
271 if (vol->v_flags & AFPVOL_RO)
274 memcpy( &did, ibuf, sizeof( did));
275 ibuf += sizeof( did);
277 if (( dir = dirsearch( vol, did )) == NULL ) {
278 return( AFPERR_NOOBJ );
281 memcpy( &bitmap, ibuf, sizeof( bitmap ));
282 bitmap = ntohs( bitmap );
283 ibuf += sizeof( bitmap );
285 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
286 return( AFPERR_NOOBJ );
289 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
290 return( AFPERR_NOOBJ );
294 * If ibuf is odd, make it even.
296 if ((u_long)ibuf & 1 ) {
300 if (S_ISDIR(st.st_mode)) {
301 rc = setdirparams(vol, path, bitmap, ibuf );
303 rc = setfilparams(vol, path, bitmap, ibuf );
305 if ( rc == AFP_OK ) {
306 setvoltime(obj, vol );
310 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
316 /* --------------------------------------------
317 Factorise some check on a pathname
319 int check_name(const struct vol *vol, char *name)
321 /* check for illegal characters in the unix filename */
322 if (!wincheck(vol, name))
325 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
328 if (!validupath(vol, name))
331 /* check for vetoed filenames */
332 if (veto_file(vol->v_veto, name))
337 /* -------------------------
338 move and rename sdir:oldname to curdir:newname in volume vol
340 special care is needed for lock
342 static int moveandrename(vol, sdir, oldname, newname, isdir)
343 const struct vol *vol;
356 struct ofork *opened;
362 memset(&ad, 0, sizeof(ad));
368 p = mtoupath(vol, oldname);
369 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
371 p = ctoupath( vol, sdir, oldname );
372 if ((opened = of_findname(p, NULL))) {
373 /* reuse struct adouble so it won't break locks */
379 id = sdir->d_did; /* we already have the CNID */
381 p = ctoupath( vol, sdir->d_parent, oldname );
382 adflags = ADFLAGS_DIR;
385 * p now points to the full pathname of the source fs object.
387 * we are in the dest folder so we need to use p for ad_open
390 if (!ad_open(p, ADFLAGS_HF |adflags, O_RDONLY, 0666, adp)) {
393 ad_getattr(adp, &bshort);
394 ad_close( adp, ADFLAGS_HF );
395 if ((bshort & htons(ATTRBIT_NORENAME)))
396 return(AFPERR_OLOCK);
399 upath = mtoupath(vol, newname);
400 if (0 != (rc = check_name(vol, upath))) {
404 /* source == destination. we just silently accept this. */
405 if (curdir == sdir) {
406 if (strcmp(oldname, newname) == 0)
409 /* deal with case insensitive, case-preserving filesystems. */
410 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
413 } else if (stat(upath, &st ) == 0)
417 if (of_findname(upath, &st)) {
418 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
420 rc = renamefile( p, upath, newname,vol_noadouble(vol), adp );
422 of_rename(vol, opened, sdir, oldname, curdir, newname);
425 rc = renamedir(p, upath, sdir, curdir, newname, vol_noadouble(vol));
427 if ( rc == AFP_OK ) {
429 /* renaming may have moved the file/dir across a filesystem */
430 if (stat(upath, &st) < 0)
433 /* fix up the catalog entry */
434 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
441 /* -------------------------------------------- */
442 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
445 int ibuflen, *rbuflen;
449 char *path, *oldname, *newname;
456 LOG(log_info, logtype_afpd, "begin afp_rename:");
462 memcpy( &vid, ibuf, sizeof( vid ));
463 ibuf += sizeof( vid );
464 if (( vol = getvolbyvid( vid )) == NULL ) {
465 return( AFPERR_PARAM );
468 if (vol->v_flags & AFPVOL_RO)
471 memcpy( &did, ibuf, sizeof( did ));
472 ibuf += sizeof( did );
473 if (( sdir = dirsearch( vol, did )) == NULL ) {
474 return( AFPERR_NOOBJ );
477 /* source pathname */
478 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
479 return( AFPERR_NOOBJ );
483 newname = obj->newtmp;
484 oldname = obj->oldtmp;
485 if ( *path != '\0' ) {
486 strcpy(oldname, path); /* an extra copy for of_rename */
489 if ( sdir->d_parent == NULL ) { /* root directory */
490 return( AFPERR_NORENAME );
492 /* move to destination dir */
493 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
494 return( AFPERR_NOOBJ );
497 strcpy(oldname, sdir->d_name);
500 /* another place where we know about the path type */
501 if ( *ibuf++ != 2 ) {
502 return( AFPERR_PARAM );
505 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
506 strncpy( newname, ibuf, plen );
507 newname[ plen ] = '\0';
508 if (strlen(newname) != plen) {
509 return( AFPERR_PARAM );
513 return AFP_OK; /* newname == oldname same dir */
516 rc = moveandrename(vol, sdir, oldname, newname, isdir);
518 if ( rc == AFP_OK ) {
519 setvoltime(obj, vol );
523 LOG(log_info, logtype_afpd, "end afp_rename:");
529 /* ------------------------------- */
530 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
533 int ibuflen, *rbuflen;
542 LOG(log_info, logtype_afpd, "begin afp_delete:");
548 memcpy( &vid, ibuf, sizeof( vid ));
549 ibuf += sizeof( vid );
550 if (( vol = getvolbyvid( vid )) == NULL ) {
551 return( AFPERR_PARAM );
554 if (vol->v_flags & AFPVOL_RO)
557 memcpy( &did, ibuf, sizeof( did ));
558 ibuf += sizeof( int );
559 if (( dir = dirsearch( vol, did )) == NULL ) {
560 return( AFPERR_NOOBJ );
563 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
564 return( AFPERR_NOOBJ );
567 upath = mtoupath(vol, path );
568 if ( *path == '\0' ) {
569 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
570 } else if (of_findname(upath, NULL)) {
572 } else if ((rc = deletefile( upath, 1)) == AFP_OK) {
573 #ifdef CNID_DB /* get rid of entry */
574 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
575 cnid_delete(vol->v_db, id);
578 if ( rc == AFP_OK ) {
579 setvoltime(obj, vol );
583 LOG(log_info, logtype_afpd, "end afp_delete:");
589 char *ctoupath( vol, dir, name )
590 const struct vol *vol;
595 static char path[ MAXPATHLEN + 1];
599 p = path + sizeof( path ) - 1;
601 u = mtoupath(vol, name );
604 strncpy( p, u, len );
605 for ( d = dir; d->d_parent; d = d->d_parent ) {
607 u = mtoupath(vol, d->d_name );
610 strncpy( p, u, len );
613 len = strlen( vol->v_path );
615 strncpy( p, vol->v_path, len );
620 /* ------------------------- */
621 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
624 int ibuflen, *rbuflen;
627 struct dir *sdir, *ddir;
629 char *oldname, *newname;
637 #endif /* DROPKLUDGE */
640 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
646 memcpy( &vid, ibuf, sizeof( vid ));
647 ibuf += sizeof( vid );
648 if (( vol = getvolbyvid( vid )) == NULL ) {
649 return( AFPERR_PARAM );
652 if (vol->v_flags & AFPVOL_RO)
655 /* source did followed by dest did */
656 memcpy( &did, ibuf, sizeof( did ));
657 ibuf += sizeof( int );
658 if (( sdir = dirsearch( vol, did )) == NULL ) {
659 return( AFPERR_PARAM );
662 memcpy( &did, ibuf, sizeof( did ));
663 ibuf += sizeof( int );
665 /* source pathname */
666 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
667 return( AFPERR_NOOBJ );
671 newname = obj->newtmp;
672 oldname = obj->oldtmp;
673 if ( *path != '\0' ) {
674 /* not a directory */
675 strcpy(oldname, path); /* an extra copy for of_rename */
678 strcpy(oldname, sdir->d_name);
681 /* get the destination directory */
682 if (( ddir = dirsearch( vol, did )) == NULL ) {
683 return( AFPERR_PARAM );
685 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
686 return( AFPERR_NOOBJ );
688 if ( *path != '\0' ) {
689 return( AFPERR_BADTYPE );
692 /* one more place where we know about path type */
693 if ( *ibuf++ != 2 ) {
694 return( AFPERR_PARAM );
697 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
698 strncpy( newname, ibuf, plen );
699 newname[ plen ] = '\0';
700 if (strlen(newname) != plen) {
701 return( AFPERR_PARAM );
705 strcpy(newname, oldname);
708 rc = moveandrename(vol, sdir, oldname, newname, isdir);
710 if ( rc == AFP_OK ) {
711 char *upath = mtoupath(vol, newname);
713 if (vol->v_flags & AFPVOL_DROPBOX) {
714 if (retvalue=matchfile2dirperms (upath, vol, did) != AFP_OK) {
719 #endif /* DROPKLUDGE */
721 int admode = ad_mode("", 0777);
723 setfilmode(upath, admode, NULL);
724 setfilmode(ad_path( upath, ADFLAGS_HF ), ad_hf_mode(admode), NULL);
726 setvoltime(obj, vol );
730 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
736 int veto_file(const char*veto_str, const char*path)
737 /* given a veto_str like "abc/zxc/" and path "abc", return 1
738 * veto_str should be '/' delimited
739 * if path matches any one of the veto_str elements exactly, then 1 is returned
740 * otherwise, 0 is returned.
743 int i; /* index to veto_str */
744 int j; /* index to path */
746 if ((veto_str == NULL) || (path == NULL))
750 LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
753 for(i=0, j=0; veto_str[i] != '\0'; i++) {
754 if (veto_str[i] == '/') {
755 if ((j>0) && (path[j] == '\0'))
759 if (veto_str[i] != path[j]) {
760 while ((veto_str[i] != '/')
761 && (veto_str[i] != '\0'))