2 * $Id: filedir.c,v 1.36 2002-10-25 11:26:49 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"
59 int matchfile2dirperms(upath, vol, did)
60 /* Since it's kinda' big; I decided against an
65 /* The below code changes the way file ownership is determined in the name of
66 fixing dropboxes. It has known security problem. See the netatalk FAQ for
75 LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
78 if (stat(upath, &st ) < 0) {
79 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
83 adpath = ad_path( upath, ADFLAGS_HF );
84 /* FIXME dirsearch doesn't move cwd to did ! */
85 if (( dir = dirlookup( vol, did )) == NULL ) {
86 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
89 else if (stat(".", &sb) < 0) {
90 LOG(log_error, logtype_afpd,
91 "matchfile2dirperms: Error checking directory \"%s\": %s",
92 dir->d_m_name, strerror(errno));
97 if ( uid != sb.st_uid )
100 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
102 LOG(log_error, logtype_afpd,
103 "matchfile2dirperms: Error changing owner/gid of %s: %s",
104 upath, strerror(errno));
107 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
109 LOG(log_error, logtype_afpd,
110 "matchfile2dirperms: Error adding file read permissions: %s",
114 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
116 LOG(log_error, logtype_afpd,
117 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %s",
118 adpath, strerror(errno));
121 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
123 LOG(log_error, logtype_afpd,
124 "matchfile2dirperms: Error adding AD file read permissions: %s",
130 } /* end else if stat success */
133 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
139 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
142 int ibuflen, *rbuflen;
149 u_int16_t fbitmap, dbitmap, vid;
153 LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
159 memcpy( &vid, ibuf, sizeof( vid ));
160 ibuf += sizeof( vid );
161 if (( vol = getvolbyvid( vid )) == NULL ) {
162 return( AFPERR_PARAM );
165 memcpy( &did, ibuf, sizeof( did ));
166 ibuf += sizeof( did );
168 if (( dir = dirlookup( vol, did )) == NULL ) {
169 return( AFPERR_NOOBJ );
172 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
173 fbitmap = ntohs( fbitmap );
174 ibuf += sizeof( fbitmap );
175 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
176 dbitmap = ntohs( dbitmap );
177 ibuf += sizeof( dbitmap );
179 if (( s_path = cname( vol, dir, &ibuf )) == NULL) {
180 return( AFPERR_NOOBJ );
184 if (!s_path->st_valid) {
185 /* it's a dir and it should be there
186 * because we chdir in it in cname
190 if ( s_path->st_errno != 0 ) {
191 return( AFPERR_NOOBJ );
195 if (S_ISDIR(st->st_mode)) {
197 ret = getdirparams(vol, dbitmap, s_path, curdir,
198 rbuf + 3 * sizeof( u_int16_t ), &buflen );
202 /* this is a directory */
203 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
205 if (fbitmap && ( ret = getfilparams(vol, fbitmap, s_path, curdir,
206 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
210 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
212 *rbuflen = buflen + 3 * sizeof( u_int16_t );
213 fbitmap = htons( fbitmap );
214 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
215 rbuf += sizeof( fbitmap );
216 dbitmap = htons( dbitmap );
217 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
218 rbuf += sizeof( dbitmap ) + sizeof( u_char );
222 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
228 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
231 int ibuflen, *rbuflen;
237 u_int16_t vid, bitmap;
241 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
246 memcpy( &vid, ibuf, sizeof(vid));
247 ibuf += sizeof( vid );
249 if (( vol = getvolbyvid( vid )) == NULL ) {
250 return( AFPERR_PARAM );
253 if (vol->v_flags & AFPVOL_RO)
256 memcpy( &did, ibuf, sizeof( did));
257 ibuf += sizeof( did);
259 if (( dir = dirlookup( vol, did )) == NULL ) {
260 return( AFPERR_NOOBJ );
263 memcpy( &bitmap, ibuf, sizeof( bitmap ));
264 bitmap = ntohs( bitmap );
265 ibuf += sizeof( bitmap );
267 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
268 return( AFPERR_NOOBJ );
272 if (!path->st_valid) {
273 /* it's a dir and it should be there
274 * because we chdir in it in cname
279 if ( path->st_errno != 0 ) {
280 return( AFPERR_NOOBJ );
283 * If ibuf is odd, make it even.
285 if ((u_long)ibuf & 1 ) {
289 if (S_ISDIR(st->st_mode)) {
290 rc = setdirparams(vol, path, bitmap, ibuf );
292 rc = setfilparams(vol, path, bitmap, ibuf );
294 if ( rc == AFP_OK ) {
295 setvoltime(obj, vol );
299 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
305 /* --------------------------------------------
306 Factorise some check on a pathname
308 int check_name(const struct vol *vol, char *name)
310 /* check for illegal characters in the unix filename */
311 if (!wincheck(vol, name))
314 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
317 if (!validupath(vol, name))
320 /* check for vetoed filenames */
321 if (veto_file(vol->v_veto, name))
326 /* -------------------------
327 move and rename sdir:oldname to curdir:newname in volume vol
329 special care is needed for lock
331 static int moveandrename(vol, sdir, oldname, newname, isdir)
332 const struct vol *vol;
345 struct ofork *opened = NULL;
351 memset(&ad, 0, sizeof(ad));
357 p = mtoupath(vol, oldname);
358 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
360 p = ctoupath( vol, sdir, oldname );
363 if ((opened = of_findname(&path))) {
364 /* reuse struct adouble so it won't break locks */
370 id = sdir->d_did; /* we already have the CNID */
372 p = ctoupath( vol, sdir->d_parent, oldname );
373 adflags = ADFLAGS_DIR;
376 * p now points to the full pathname of the source fs object.
378 * we are in the dest folder so we need to use p for ad_open
381 if (!ad_open(p, ADFLAGS_HF |adflags, O_RDONLY, 0666, adp)) {
384 ad_getattr(adp, &bshort);
385 ad_close( adp, ADFLAGS_HF );
386 if ((bshort & htons(ATTRBIT_NORENAME)))
387 return(AFPERR_OLOCK);
390 upath = mtoupath(vol, newname);
393 if (0 != (rc = check_name(vol, upath))) {
397 /* source == destination. we just silently accept this. */
398 if (curdir == sdir) {
399 if (strcmp(oldname, newname) == 0)
402 /* deal with case insensitive, case-preserving filesystems. */
403 if ((stat(upath, st) == 0) && strdiacasecmp(oldname, newname))
406 } else if (stat(upath, st ) == 0)
411 path.st_errno = errno;
412 if (of_findname(&path)) {
413 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
415 rc = renamefile( p, upath, newname,vol_noadouble(vol), adp );
417 of_rename(vol, opened, sdir, oldname, curdir, newname);
420 rc = renamedir(p, upath, sdir, curdir, newname, vol_noadouble(vol));
422 if ( rc == AFP_OK ) {
424 /* renaming may have moved the file/dir across a filesystem */
425 if (stat(upath, st) < 0)
428 /* fix up the catalog entry */
429 cnid_update(vol->v_db, id, st, curdir->d_did, upath, strlen(upath));
436 /* -------------------------------------------- */
437 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
440 int ibuflen, *rbuflen;
444 char *oldname, *newname;
452 LOG(log_info, logtype_afpd, "begin afp_rename:");
458 memcpy( &vid, ibuf, sizeof( vid ));
459 ibuf += sizeof( vid );
460 if (( vol = getvolbyvid( vid )) == NULL ) {
461 return( AFPERR_PARAM );
464 if (vol->v_flags & AFPVOL_RO)
467 memcpy( &did, ibuf, sizeof( did ));
468 ibuf += sizeof( did );
469 if (( sdir = dirlookup( vol, did )) == NULL ) {
470 return( AFPERR_NOOBJ );
473 /* source pathname */
474 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
475 return( AFPERR_NOOBJ );
479 newname = obj->newtmp;
480 oldname = obj->oldtmp;
481 if ( *path->m_name != '\0' ) {
482 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
485 if ( sdir->d_parent == NULL ) { /* root directory */
486 return( AFPERR_NORENAME );
488 /* move to destination dir */
489 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
490 return( AFPERR_NOOBJ );
493 strcpy(oldname, sdir->d_m_name);
496 /* another place where we know about the path type */
497 if ((plen = copy_path_name(newname, ibuf)) < 0) {
498 return( AFPERR_PARAM );
502 return AFP_OK; /* newname == oldname same dir */
505 rc = moveandrename(vol, sdir, oldname, newname, isdir);
507 if ( rc == AFP_OK ) {
508 setvoltime(obj, vol );
512 LOG(log_info, logtype_afpd, "end afp_rename:");
518 /* ------------------------------- */
519 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
522 int ibuflen, *rbuflen;
532 LOG(log_info, logtype_afpd, "begin afp_delete:");
538 memcpy( &vid, ibuf, sizeof( vid ));
539 ibuf += sizeof( vid );
540 if (( vol = getvolbyvid( vid )) == NULL ) {
541 return( AFPERR_PARAM );
544 if (vol->v_flags & AFPVOL_RO)
547 memcpy( &did, ibuf, sizeof( did ));
548 ibuf += sizeof( int );
549 if (( dir = dirlookup( vol, did )) == NULL ) {
550 return( AFPERR_NOOBJ );
553 if (( s_path = cname( vol, dir, &ibuf )) == NULL ) {
554 return( AFPERR_NOOBJ );
557 upath = s_path->u_name;
558 if ( *s_path->m_name == '\0' ) {
559 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
560 } else if (of_findname(s_path)) {
562 } else if ((rc = deletefile( upath, 1)) == AFP_OK) {
563 #ifdef CNID_DB /* get rid of entry */
564 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
565 cnid_delete(vol->v_db, id);
568 if ( rc == AFP_OK ) {
570 setvoltime(obj, vol );
574 LOG(log_info, logtype_afpd, "end afp_delete:");
579 char *absupath( vol, dir, u )
580 const struct vol *vol;
585 static char path[ MAXPATHLEN + 1];
589 p = path + sizeof( path ) - 1;
593 strncpy( p, u, len );
594 for ( d = dir; d->d_parent; d = d->d_parent ) {
599 strncpy( p, u, len );
602 len = strlen( vol->v_path );
604 strncpy( p, vol->v_path, len );
609 char *ctoupath( vol, dir, name )
610 const struct vol *vol;
614 return absupath(vol, dir, mtoupath(vol, name));
617 /* ------------------------- */
618 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
621 int ibuflen, *rbuflen;
624 struct dir *sdir, *ddir;
626 char *oldname, *newname;
634 #endif /* DROPKLUDGE */
637 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
643 memcpy( &vid, ibuf, sizeof( vid ));
644 ibuf += sizeof( vid );
645 if (( vol = getvolbyvid( vid )) == NULL ) {
646 return( AFPERR_PARAM );
649 if (vol->v_flags & AFPVOL_RO)
652 /* source did followed by dest did */
653 memcpy( &did, ibuf, sizeof( did ));
654 ibuf += sizeof( int );
655 if (( sdir = dirlookup( vol, did )) == NULL ) {
656 return( AFPERR_PARAM );
659 memcpy( &did, ibuf, sizeof( did ));
660 ibuf += sizeof( int );
662 /* source pathname */
663 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
664 return( AFPERR_NOOBJ );
668 newname = obj->newtmp;
669 oldname = obj->oldtmp;
670 if ( *path->m_name != '\0' ) {
671 /* not a directory */
672 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
675 strcpy(oldname, sdir->d_m_name);
678 /* get the destination directory */
679 if (( ddir = dirlookup( vol, did )) == NULL ) {
680 return( AFPERR_PARAM );
682 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
683 return( AFPERR_NOOBJ );
685 if ( *path->m_name != '\0' ) {
686 return( AFPERR_BADTYPE );
689 /* one more place where we know about path type */
690 if ((plen = copy_path_name(newname, ibuf)) < 0) {
691 return( AFPERR_PARAM );
695 strcpy(newname, oldname);
698 rc = moveandrename(vol, sdir, oldname, newname, isdir);
700 if ( rc == AFP_OK ) {
701 char *upath = mtoupath(vol, newname);
706 if (vol->v_flags & AFPVOL_DROPBOX) {
707 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
712 #endif /* DROPKLUDGE */
714 int admode = ad_mode("", 0777);
716 setfilmode(upath, admode, NULL);
717 setfilmode(ad_path( upath, ADFLAGS_HF ), ad_hf_mode(admode), NULL);
719 setvoltime(obj, vol );
723 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
729 int veto_file(const char*veto_str, const char*path)
730 /* given a veto_str like "abc/zxc/" and path "abc", return 1
731 * veto_str should be '/' delimited
732 * if path matches any one of the veto_str elements exactly, then 1 is returned
733 * otherwise, 0 is returned.
736 int i; /* index to veto_str */
737 int j; /* index to path */
739 if ((veto_str == NULL) || (path == NULL))
743 LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
746 for(i=0, j=0; veto_str[i] != '\0'; i++) {
747 if (veto_str[i] == '/') {
748 if ((j>0) && (path[j] == '\0'))
752 if (veto_str[i] != path[j]) {
753 while ((veto_str[i] != '/')
754 && (veto_str[i] != '\0'))