2 * $Id: filedir.c,v 1.50 2005-06-02 12:32:17 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #else /* STDC_HEADERS */
21 #endif /* HAVE_STRCHR */
22 char *strchr (), *strrchr ();
24 #define memcpy(d,s,n) bcopy ((s), (d), (n))
25 #define memmove(d,s,n) bcopy ((s), (d), (n))
26 #endif /* ! HAVE_MEMCPY */
27 #endif /* STDC_HEADERS */
34 #include <sys/param.h>
35 #include <atalk/adouble.h>
37 #include <atalk/afp.h>
38 #include <atalk/util.h>
39 #include <atalk/cnid.h>
40 #include <atalk/logger.h>
42 #include "directory.h"
52 int matchfile2dirperms(upath, vol, did)
53 /* Since it's kinda' big; I decided against an
58 /* The below code changes the way file ownership is determined in the name of
59 fixing dropboxes. It has known security problem. See the netatalk FAQ for
68 LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
71 if (stat(upath, &st ) < 0) {
72 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
76 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
77 /* FIXME dirsearch doesn't move cwd to did ! */
78 if (( dir = dirlookup( vol, did )) == NULL ) {
79 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
82 else if (stat(".", &sb) < 0) {
83 LOG(log_error, logtype_afpd,
84 "matchfile2dirperms: Error checking directory \"%s\": %s",
85 dir->d_m_name, strerror(errno));
90 if ( uid != sb.st_uid )
93 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
95 LOG(log_error, logtype_afpd,
96 "matchfile2dirperms(%s): Error changing owner/gid: %s",
97 upath, strerror(errno));
100 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
102 LOG(log_error, logtype_afpd,
103 "matchfile2dirperms(%s): Error adding file read permissions: %s",
104 upath, strerror(errno));
107 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
109 LOG(log_error, logtype_afpd,
110 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
111 adpath, strerror(errno));
114 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
116 LOG(log_error, logtype_afpd,
117 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
118 adpath, strerror(errno));
123 } /* end else if stat success */
126 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
132 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
135 int ibuflen _U_, *rbuflen;
142 u_int16_t fbitmap, dbitmap, vid;
146 LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
152 memcpy( &vid, ibuf, sizeof( vid ));
153 ibuf += sizeof( vid );
154 if (NULL == ( vol = getvolbyvid( vid )) ) {
155 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
158 return( AFPERR_ACCESS );
161 memcpy( &did, ibuf, sizeof( did ));
162 ibuf += sizeof( did );
164 if (NULL == ( dir = dirlookup( vol, did )) ) {
168 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
169 fbitmap = ntohs( fbitmap );
170 ibuf += sizeof( fbitmap );
171 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
172 dbitmap = ntohs( dbitmap );
173 ibuf += sizeof( dbitmap );
175 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
176 return get_afp_errno(AFPERR_NOOBJ);
180 if (!s_path->st_valid) {
181 /* it's a dir and it should be there
182 * because we chdir in it in cname or
183 * it's curdir (maybe deleted, but then we can't know).
184 * So we need to try harder.
186 of_statdir(vol, s_path);
188 if ( s_path->st_errno != 0 ) {
189 return( AFPERR_NOOBJ );
193 if (S_ISDIR(st->st_mode)) {
199 ret = getdirparams(vol, dbitmap, s_path, dir,
200 rbuf + 3 * sizeof( u_int16_t ), &buflen );
204 /* this is a directory */
205 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
207 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
208 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
212 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
214 *rbuflen = buflen + 3 * sizeof( u_int16_t );
215 fbitmap = htons( fbitmap );
216 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
217 rbuf += sizeof( fbitmap );
218 dbitmap = htons( dbitmap );
219 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
220 rbuf += sizeof( dbitmap ) + sizeof( u_char );
224 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
230 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
232 char *ibuf, *rbuf _U_;
233 int ibuflen _U_, *rbuflen;
239 u_int16_t vid, bitmap;
243 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
248 memcpy( &vid, ibuf, sizeof(vid));
249 ibuf += sizeof( vid );
251 if (NULL == ( vol = getvolbyvid( vid )) ) {
252 return( AFPERR_PARAM );
255 if (vol->v_flags & AFPVOL_RO)
258 memcpy( &did, ibuf, sizeof( did));
259 ibuf += sizeof( did);
261 if (NULL == ( dir = dirlookup( vol, did )) ) {
265 memcpy( &bitmap, ibuf, sizeof( bitmap ));
266 bitmap = ntohs( bitmap );
267 ibuf += sizeof( bitmap );
269 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
270 return get_afp_errno(AFPERR_NOOBJ);
274 if (!path->st_valid) {
275 /* it's a dir and it should be there
276 * because we chdir in it in cname
278 of_statdir(vol, path);
281 if ( path->st_errno != 0 ) {
282 return( AFPERR_NOOBJ );
285 * If ibuf is odd, make it even.
287 if ((u_long)ibuf & 1 ) {
291 if (S_ISDIR(st->st_mode)) {
292 rc = setdirparams(vol, path, bitmap, ibuf );
294 rc = setfilparams(vol, path, bitmap, ibuf );
296 if ( rc == AFP_OK ) {
297 setvoltime(obj, vol );
301 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
307 /* --------------------------------------------
308 Factorise some checks on a pathname
310 int check_name(const struct vol *vol, char *name)
312 /* check for illegal characters in the unix filename */
313 if (!wincheck(vol, name))
316 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
319 if (!vol->vfs->validupath(vol, name))
322 /* check for vetoed filenames */
323 if (veto_file(vol->v_veto, name))
328 /* -------------------------
329 move and rename sdir:oldname to curdir:newname in volume vol
331 special care is needed for lock
333 static int moveandrename(vol, sdir, oldname, newname, isdir)
334 const struct vol *vol;
343 struct stat *st, nst;
347 struct ofork *opened = NULL;
351 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
356 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
358 return AFPERR_PARAM; /* can't convert */
360 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
361 p = ctoupath( vol, sdir, oldname );
363 return AFPERR_PARAM; /* pathname too long */
367 if ((opened = of_findname(&path))) {
368 /* reuse struct adouble so it won't break locks */
373 id = sdir->d_did; /* we already have the CNID */
374 p = ctoupath( vol, sdir->d_parent, oldname );
378 adflags = ADFLAGS_DIR;
381 * p now points to the full pathname of the source fs object.
383 * we are in the dest folder so we need to use p for ad_open
386 if (!ad_metadata(p, adflags, adp)) {
389 ad_getattr(adp, &bshort);
390 ad_close_metadata( adp);
391 if ((bshort & htons(ATTRBIT_NORENAME)))
392 return(AFPERR_OLOCK);
395 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
400 if (0 != (rc = check_name(vol, upath))) {
404 /* source == destination. we just silently accept this. */
405 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
406 if (strcmp(oldname, newname) == 0)
409 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
410 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
411 /* not the same file */
416 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
421 path.st_errno = errno;
422 if (of_findname(&path)) {
423 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
425 rc = renamefile(vol, p, upath, newname, adp );
427 of_rename(vol, opened, sdir, oldname, curdir, newname);
430 rc = renamedir(vol, p, upath, sdir, curdir, newname);
432 if ( rc == AFP_OK && id ) {
433 /* renaming may have moved the file/dir across a filesystem */
434 if (stat(upath, st) < 0)
437 /* fix up the catalog entry */
438 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
444 /* -------------------------------------------- */
445 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
447 char *ibuf, *rbuf _U_;
448 int ibuflen _U_, *rbuflen;
452 char *oldname, *newname;
460 LOG(log_info, logtype_afpd, "begin afp_rename:");
466 memcpy( &vid, ibuf, sizeof( vid ));
467 ibuf += sizeof( vid );
468 if (NULL == ( vol = getvolbyvid( vid )) ) {
469 return( AFPERR_PARAM );
472 if (vol->v_flags & AFPVOL_RO)
475 memcpy( &did, ibuf, sizeof( did ));
476 ibuf += sizeof( did );
477 if (NULL == ( sdir = dirlookup( vol, did )) ) {
481 /* source pathname */
482 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
483 return get_afp_errno(AFPERR_NOOBJ);
487 newname = obj->newtmp;
488 oldname = obj->oldtmp;
489 isdir = path_isadir(path);
490 if ( *path->m_name != '\0' ) {
491 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
493 /* curdir parent dir, need to move sdir back */
498 if ( sdir->d_parent == NULL ) { /* root directory */
499 return( AFPERR_NORENAME );
501 /* move to destination dir */
502 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
505 strcpy(oldname, sdir->d_m_name);
508 /* another place where we know about the path type */
509 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
510 return( AFPERR_PARAM );
514 return AFP_OK; /* newname == oldname same dir */
517 rc = moveandrename(vol, sdir, oldname, newname, isdir);
519 if ( rc == AFP_OK ) {
520 setvoltime(obj, vol );
524 LOG(log_info, logtype_afpd, "end afp_rename:");
530 /* ------------------------------- */
531 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
533 char *ibuf, *rbuf _U_;
534 int ibuflen _U_, *rbuflen;
544 LOG(log_info, logtype_afpd, "begin afp_delete:");
550 memcpy( &vid, ibuf, sizeof( vid ));
551 ibuf += sizeof( vid );
552 if (NULL == ( vol = getvolbyvid( vid )) ) {
553 return( AFPERR_PARAM );
556 if (vol->v_flags & AFPVOL_RO)
559 memcpy( &did, ibuf, sizeof( did ));
560 ibuf += sizeof( int );
561 if (NULL == ( dir = dirlookup( vol, did )) ) {
565 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
566 return get_afp_errno(AFPERR_NOOBJ);
569 upath = s_path->u_name;
570 if ( path_isadir( s_path) ) {
571 if (*s_path->m_name != '\0') {
575 rc = deletecurdir( vol);
577 } else if (of_findname(s_path)) {
580 rc = deletefile(vol, upath, 1);
582 if ( rc == AFP_OK ) {
584 setvoltime(obj, vol );
588 LOG(log_info, logtype_afpd, "end afp_delete:");
593 /* ------------------------ */
594 char *absupath( vol, dir, u )
595 const struct vol *vol;
600 static char path[ MAXPATHLEN + 1];
607 p = path + sizeof( path ) - 1;
612 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
615 if (p -len -1 < path) {
617 rather rare so LOG error and/or client message ?
625 len = strlen( vol->v_path );
626 if (p -len -1 < path) {
631 memcpy( p, vol->v_path, len );
636 /* ------------------------
637 * FIXME dir could be NULL
639 char *ctoupath( vol, dir, name )
640 const struct vol *vol;
644 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
647 /* ------------------------- */
648 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
650 char *ibuf, *rbuf _U_;
651 int ibuflen _U_, *rbuflen;
654 struct dir *sdir, *ddir;
656 char *oldname, *newname;
665 #endif /* DROPKLUDGE */
668 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
674 memcpy( &vid, ibuf, sizeof( vid ));
675 ibuf += sizeof( vid );
676 if (NULL == ( vol = getvolbyvid( vid )) ) {
677 return( AFPERR_PARAM );
680 if (vol->v_flags & AFPVOL_RO)
683 /* source did followed by dest did */
684 memcpy( &did, ibuf, sizeof( did ));
685 ibuf += sizeof( int );
686 if (NULL == ( sdir = dirlookup( vol, did )) ) {
687 return afp_errno; /* was AFPERR_PARAM */
690 memcpy( &did, ibuf, sizeof( did ));
691 ibuf += sizeof( int );
693 /* source pathname */
694 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
695 return get_afp_errno(AFPERR_NOOBJ);
699 newname = obj->newtmp;
700 oldname = obj->oldtmp;
702 isdir = path_isadir(path);
703 if ( *path->m_name != '\0' ) {
707 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
709 strcpy(oldname, sdir->d_m_name);
712 /* get the destination directory */
713 if (NULL == ( ddir = dirlookup( vol, did )) ) {
714 return afp_errno; /* was AFPERR_PARAM */
716 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
717 return( AFPERR_NOOBJ );
719 pdid = curdir->d_did;
720 if ( *path->m_name != '\0' ) {
721 return path_error(path, AFPERR_NOOBJ);
724 /* one more place where we know about path type */
725 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
726 return( AFPERR_PARAM );
730 strcpy(newname, oldname);
733 rc = moveandrename(vol, sdir, oldname, newname, isdir);
735 if ( rc == AFP_OK ) {
736 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
744 if (vol->v_flags & AFPVOL_DROPBOX) {
745 /* FIXME did is not always the source id */
746 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
751 #endif /* DROPKLUDGE */
752 /* if unix priv don't try to match perm with dest folder */
753 if (!isdir && !vol_unix_priv(vol)) {
754 int admode = ad_mode("", 0777) | vol->v_perm;
756 setfilmode(upath, admode, NULL);
757 vol->vfs->rf_setfilmode(vol, upath, admode, NULL);
759 setvoltime(obj, vol );
763 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
769 int veto_file(const char*veto_str, const char*path)
770 /* given a veto_str like "abc/zxc/" and path "abc", return 1
771 * veto_str should be '/' delimited
772 * if path matches any one of the veto_str elements exactly, then 1 is returned
773 * otherwise, 0 is returned.
776 int i; /* index to veto_str */
777 int j; /* index to path */
779 if ((veto_str == NULL) || (path == NULL))
783 LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
786 for(i=0, j=0; veto_str[i] != '\0'; i++) {
787 if (veto_str[i] == '/') {
788 if ((j>0) && (path[j] == '\0'))
792 if (veto_str[i] != path[j]) {
793 while ((veto_str[i] != '/')
794 && (veto_str[i] != '\0'))