2 * $Id: filedir.c,v 1.55 2009-10-02 09:32:40 franklahm 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>
36 #include <atalk/adouble.h>
37 #include <atalk/vfs.h>
38 #include <atalk/afp.h>
39 #include <atalk/util.h>
40 #include <atalk/cnid.h>
41 #include <atalk/logger.h>
43 #include "directory.h"
53 int matchfile2dirperms(upath, vol, did)
54 /* Since it's kinda' big; I decided against an
59 /* The below code changes the way file ownership is determined in the name of
60 fixing dropboxes. It has known security problem. See the netatalk FAQ for
69 LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
72 if (stat(upath, &st ) < 0) {
73 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
77 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
78 /* FIXME dirsearch doesn't move cwd to did ! */
79 if (( dir = dirlookup( vol, did )) == NULL ) {
80 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
83 else if (stat(".", &sb) < 0) {
84 LOG(log_error, logtype_afpd,
85 "matchfile2dirperms: Error checking directory \"%s\": %s",
86 dir->d_m_name, strerror(errno));
91 if ( uid != sb.st_uid )
94 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
96 LOG(log_error, logtype_afpd,
97 "matchfile2dirperms(%s): Error changing owner/gid: %s",
98 upath, strerror(errno));
101 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
103 LOG(log_error, logtype_afpd,
104 "matchfile2dirperms(%s): Error adding file read permissions: %s",
105 upath, strerror(errno));
108 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
110 LOG(log_error, logtype_afpd,
111 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
112 adpath, strerror(errno));
115 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
117 LOG(log_error, logtype_afpd,
118 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
119 adpath, strerror(errno));
124 } /* end else if stat success */
127 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
133 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
136 int ibuflen _U_, *rbuflen;
143 u_int16_t fbitmap, dbitmap, vid;
147 LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
153 memcpy( &vid, ibuf, sizeof( vid ));
154 ibuf += sizeof( vid );
155 if (NULL == ( vol = getvolbyvid( vid )) ) {
156 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
159 return( AFPERR_ACCESS );
162 memcpy( &did, ibuf, sizeof( did ));
163 ibuf += sizeof( did );
165 if (NULL == ( dir = dirlookup( vol, did )) ) {
169 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
170 fbitmap = ntohs( fbitmap );
171 ibuf += sizeof( fbitmap );
172 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
173 dbitmap = ntohs( dbitmap );
174 ibuf += sizeof( dbitmap );
176 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
177 return get_afp_errno(AFPERR_NOOBJ);
181 if (!s_path->st_valid) {
182 /* it's a dir and it should be there
183 * because we chdir in it in cname or
184 * it's curdir (maybe deleted, but then we can't know).
185 * So we need to try harder.
187 of_statdir(vol, s_path);
189 if ( s_path->st_errno != 0 ) {
190 return( AFPERR_NOOBJ );
194 if (S_ISDIR(st->st_mode)) {
200 ret = getdirparams(vol, dbitmap, s_path, dir,
201 rbuf + 3 * sizeof( u_int16_t ), &buflen );
205 /* this is a directory */
206 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
208 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
209 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
213 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
215 *rbuflen = buflen + 3 * sizeof( u_int16_t );
216 fbitmap = htons( fbitmap );
217 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
218 rbuf += sizeof( fbitmap );
219 dbitmap = htons( dbitmap );
220 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
221 rbuf += sizeof( dbitmap ) + sizeof( u_char );
225 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
231 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
233 char *ibuf, *rbuf _U_;
234 int ibuflen _U_, *rbuflen;
240 u_int16_t vid, bitmap;
244 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
249 memcpy( &vid, ibuf, sizeof(vid));
250 ibuf += sizeof( vid );
252 if (NULL == ( vol = getvolbyvid( vid )) ) {
253 return( AFPERR_PARAM );
256 if (vol->v_flags & AFPVOL_RO)
259 memcpy( &did, ibuf, sizeof( did));
260 ibuf += sizeof( did);
262 if (NULL == ( dir = dirlookup( vol, did )) ) {
266 memcpy( &bitmap, ibuf, sizeof( bitmap ));
267 bitmap = ntohs( bitmap );
268 ibuf += sizeof( bitmap );
270 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
271 return get_afp_errno(AFPERR_NOOBJ);
275 if (!path->st_valid) {
276 /* it's a dir and it should be there
277 * because we chdir in it in cname
279 of_statdir(vol, path);
282 if ( path->st_errno != 0 ) {
283 return( AFPERR_NOOBJ );
286 * If ibuf is odd, make it even.
288 if ((u_long)ibuf & 1 ) {
292 if (S_ISDIR(st->st_mode)) {
293 rc = setdirparams(vol, path, bitmap, ibuf );
295 rc = setfilparams(vol, path, bitmap, ibuf );
297 if ( rc == AFP_OK ) {
298 setvoltime(obj, vol );
302 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
308 /* --------------------------------------------
309 Factorise some checks on a pathname
311 int check_name(const struct vol *vol, char *name)
313 /* check for illegal characters in the unix filename */
314 if (!wincheck(vol, name))
317 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
320 if (!vol->vfs->validupath(vol, name)) {
321 LOG(log_info, logtype_afpd, "check_name: illegal name: '%s'", name);
325 /* check for vetoed filenames */
326 if (veto_file(vol->v_veto, name))
331 /* -------------------------
332 move and rename sdir:oldname to curdir:newname in volume vol
334 special care is needed for lock
336 static int moveandrename(vol, sdir, oldname, newname, isdir)
337 const struct vol *vol;
346 struct stat *st, nst;
350 struct ofork *opened = NULL;
354 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
359 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
361 return AFPERR_PARAM; /* can't convert */
363 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
364 p = ctoupath( vol, sdir, oldname );
366 return AFPERR_PARAM; /* pathname too long */
370 if ((opened = of_findname(&path))) {
371 /* reuse struct adouble so it won't break locks */
376 id = sdir->d_did; /* we already have the CNID */
377 p = ctoupath( vol, sdir->d_parent, oldname );
381 adflags = ADFLAGS_DIR;
384 * p now points to the full pathname of the source fs object.
386 * we are in the dest folder so we need to use p for ad_open
389 if (!ad_metadata(p, adflags, adp)) {
392 ad_getattr(adp, &bshort);
393 ad_close_metadata( adp);
394 if ((bshort & htons(ATTRBIT_NORENAME)))
395 return(AFPERR_OLOCK);
398 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
403 if (0 != (rc = check_name(vol, upath))) {
407 /* source == destination. we just silently accept this. */
408 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
409 if (strcmp(oldname, newname) == 0)
412 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
413 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
414 /* not the same file */
419 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
424 path.st_errno = errno;
425 if (of_findname(&path)) {
426 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
428 rc = renamefile(vol, p, upath, newname, adp );
430 of_rename(vol, opened, sdir, oldname, curdir, newname);
433 rc = renamedir(vol, p, upath, sdir, curdir, newname);
435 if ( rc == AFP_OK && id ) {
436 /* renaming may have moved the file/dir across a filesystem */
437 if (stat(upath, st) < 0)
440 /* fix up the catalog entry */
441 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
447 /* -------------------------------------------- */
448 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
450 char *ibuf, *rbuf _U_;
451 int ibuflen _U_, *rbuflen;
455 char *oldname, *newname;
463 LOG(log_info, logtype_afpd, "begin afp_rename:");
469 memcpy( &vid, ibuf, sizeof( vid ));
470 ibuf += sizeof( vid );
471 if (NULL == ( vol = getvolbyvid( vid )) ) {
472 return( AFPERR_PARAM );
475 if (vol->v_flags & AFPVOL_RO)
478 memcpy( &did, ibuf, sizeof( did ));
479 ibuf += sizeof( did );
480 if (NULL == ( sdir = dirlookup( vol, did )) ) {
484 /* source pathname */
485 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
486 return get_afp_errno(AFPERR_NOOBJ);
490 newname = obj->newtmp;
491 oldname = obj->oldtmp;
492 isdir = path_isadir(path);
493 if ( *path->m_name != '\0' ) {
494 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
496 /* curdir parent dir, need to move sdir back */
501 if ( sdir->d_parent == NULL ) { /* root directory */
502 return( AFPERR_NORENAME );
504 /* move to destination dir */
505 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
508 strcpy(oldname, sdir->d_m_name);
511 /* another place where we know about the path type */
512 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
513 return( AFPERR_PARAM );
517 return AFP_OK; /* newname == oldname same dir */
520 rc = moveandrename(vol, sdir, oldname, newname, isdir);
522 if ( rc == AFP_OK ) {
523 setvoltime(obj, vol );
527 LOG(log_info, logtype_afpd, "end afp_rename:");
533 /* ------------------------------- */
534 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
536 char *ibuf, *rbuf _U_;
537 int ibuflen _U_, *rbuflen;
547 LOG(log_info, logtype_afpd, "begin afp_delete:");
553 memcpy( &vid, ibuf, sizeof( vid ));
554 ibuf += sizeof( vid );
555 if (NULL == ( vol = getvolbyvid( vid )) ) {
556 return( AFPERR_PARAM );
559 if (vol->v_flags & AFPVOL_RO)
562 memcpy( &did, ibuf, sizeof( did ));
563 ibuf += sizeof( int );
564 if (NULL == ( dir = dirlookup( vol, did )) ) {
568 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
569 return get_afp_errno(AFPERR_NOOBJ);
572 upath = s_path->u_name;
573 if ( path_isadir( s_path) ) {
574 if (*s_path->m_name != '\0') {
578 rc = deletecurdir( vol);
580 } else if (of_findname(s_path)) {
583 rc = deletefile(vol, upath, 1);
585 if ( rc == AFP_OK ) {
587 setvoltime(obj, vol );
591 LOG(log_info, logtype_afpd, "end afp_delete:");
596 /* ------------------------ */
597 char *absupath( vol, dir, u )
598 const struct vol *vol;
603 static char path[ MAXPATHLEN + 1];
610 p = path + sizeof( path ) - 1;
615 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
618 if (p -len -1 < path) {
620 rather rare so LOG error and/or client message ?
628 len = strlen( vol->v_path );
629 if (p -len -1 < path) {
634 memcpy( p, vol->v_path, len );
639 /* ------------------------
640 * FIXME dir could be NULL
642 char *ctoupath( vol, dir, name )
643 const struct vol *vol;
647 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
650 /* ------------------------- */
651 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
653 char *ibuf, *rbuf _U_;
654 int ibuflen _U_, *rbuflen;
657 struct dir *sdir, *ddir;
659 char *oldname, *newname;
668 #endif /* DROPKLUDGE */
671 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
677 memcpy( &vid, ibuf, sizeof( vid ));
678 ibuf += sizeof( vid );
679 if (NULL == ( vol = getvolbyvid( vid )) ) {
680 return( AFPERR_PARAM );
683 if (vol->v_flags & AFPVOL_RO)
686 /* source did followed by dest did */
687 memcpy( &did, ibuf, sizeof( did ));
688 ibuf += sizeof( int );
689 if (NULL == ( sdir = dirlookup( vol, did )) ) {
690 return afp_errno; /* was AFPERR_PARAM */
693 memcpy( &did, ibuf, sizeof( did ));
694 ibuf += sizeof( int );
696 /* source pathname */
697 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
698 return get_afp_errno(AFPERR_NOOBJ);
702 newname = obj->newtmp;
703 oldname = obj->oldtmp;
705 isdir = path_isadir(path);
706 if ( *path->m_name != '\0' ) {
710 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
712 strcpy(oldname, sdir->d_m_name);
715 /* get the destination directory */
716 if (NULL == ( ddir = dirlookup( vol, did )) ) {
717 return afp_errno; /* was AFPERR_PARAM */
719 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
720 return( AFPERR_NOOBJ );
722 pdid = curdir->d_did;
723 if ( *path->m_name != '\0' ) {
724 return path_error(path, AFPERR_NOOBJ);
727 /* one more place where we know about path type */
728 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
729 return( AFPERR_PARAM );
733 strcpy(newname, oldname);
736 rc = moveandrename(vol, sdir, oldname, newname, isdir);
738 if ( rc == AFP_OK ) {
739 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
747 if (vol->v_flags & AFPVOL_DROPBOX) {
748 /* FIXME did is not always the source id */
749 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
754 #endif /* DROPKLUDGE */
755 /* if unix priv don't try to match perm with dest folder */
756 if (!isdir && !vol_unix_priv(vol)) {
757 int admode = ad_mode("", 0777) | vol->v_fperm;
759 setfilmode(upath, admode, NULL, vol->v_umask);
760 vol->vfs->rf_setfilmode(vol, upath, admode, NULL);
762 setvoltime(obj, vol );
766 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
772 int veto_file(const char*veto_str, const char*path)
773 /* given a veto_str like "abc/zxc/" and path "abc", return 1
774 * veto_str should be '/' delimited
775 * if path matches any one of the veto_str elements exactly, then 1 is returned
776 * otherwise, 0 is returned.
779 int i; /* index to veto_str */
780 int j; /* index to path */
782 if ((veto_str == NULL) || (path == NULL))
785 for(i=0, j=0; veto_str[i] != '\0'; i++) {
786 if (veto_str[i] == '/') {
787 if ((j>0) && (path[j] == '\0')) {
788 LOG(log_info, logtype_afpd, "vetoed file:'%s'", path);
793 if (veto_str[i] != path[j]) {
794 while ((veto_str[i] != '/')
795 && (veto_str[i] != '\0'))