2 * $Id: filedir.c,v 1.54 2009-09-01 13:15:13 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>
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)) {
320 LOG(log_info, logtype_afpd, "check_name: illegal name: '%s'", name);
324 /* check for vetoed filenames */
325 if (veto_file(vol->v_veto, name))
330 /* -------------------------
331 move and rename sdir:oldname to curdir:newname in volume vol
333 special care is needed for lock
335 static int moveandrename(vol, sdir, oldname, newname, isdir)
336 const struct vol *vol;
345 struct stat *st, nst;
349 struct ofork *opened = NULL;
353 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
358 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
360 return AFPERR_PARAM; /* can't convert */
362 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
363 p = ctoupath( vol, sdir, oldname );
365 return AFPERR_PARAM; /* pathname too long */
369 if ((opened = of_findname(&path))) {
370 /* reuse struct adouble so it won't break locks */
375 id = sdir->d_did; /* we already have the CNID */
376 p = ctoupath( vol, sdir->d_parent, oldname );
380 adflags = ADFLAGS_DIR;
383 * p now points to the full pathname of the source fs object.
385 * we are in the dest folder so we need to use p for ad_open
388 if (!ad_metadata(p, adflags, adp)) {
391 ad_getattr(adp, &bshort);
392 ad_close_metadata( adp);
393 if ((bshort & htons(ATTRBIT_NORENAME)))
394 return(AFPERR_OLOCK);
397 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
402 if (0 != (rc = check_name(vol, upath))) {
406 /* source == destination. we just silently accept this. */
407 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
408 if (strcmp(oldname, newname) == 0)
411 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
412 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
413 /* not the same file */
418 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
423 path.st_errno = errno;
424 if (of_findname(&path)) {
425 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
427 rc = renamefile(vol, p, upath, newname, adp );
429 of_rename(vol, opened, sdir, oldname, curdir, newname);
432 rc = renamedir(vol, p, upath, sdir, curdir, newname);
434 if ( rc == AFP_OK && id ) {
435 /* renaming may have moved the file/dir across a filesystem */
436 if (stat(upath, st) < 0)
439 /* fix up the catalog entry */
440 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
446 /* -------------------------------------------- */
447 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
449 char *ibuf, *rbuf _U_;
450 int ibuflen _U_, *rbuflen;
454 char *oldname, *newname;
462 LOG(log_info, logtype_afpd, "begin afp_rename:");
468 memcpy( &vid, ibuf, sizeof( vid ));
469 ibuf += sizeof( vid );
470 if (NULL == ( vol = getvolbyvid( vid )) ) {
471 return( AFPERR_PARAM );
474 if (vol->v_flags & AFPVOL_RO)
477 memcpy( &did, ibuf, sizeof( did ));
478 ibuf += sizeof( did );
479 if (NULL == ( sdir = dirlookup( vol, did )) ) {
483 /* source pathname */
484 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
485 return get_afp_errno(AFPERR_NOOBJ);
489 newname = obj->newtmp;
490 oldname = obj->oldtmp;
491 isdir = path_isadir(path);
492 if ( *path->m_name != '\0' ) {
493 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
495 /* curdir parent dir, need to move sdir back */
500 if ( sdir->d_parent == NULL ) { /* root directory */
501 return( AFPERR_NORENAME );
503 /* move to destination dir */
504 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
507 strcpy(oldname, sdir->d_m_name);
510 /* another place where we know about the path type */
511 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
512 return( AFPERR_PARAM );
516 return AFP_OK; /* newname == oldname same dir */
519 rc = moveandrename(vol, sdir, oldname, newname, isdir);
521 if ( rc == AFP_OK ) {
522 setvoltime(obj, vol );
526 LOG(log_info, logtype_afpd, "end afp_rename:");
532 /* ------------------------------- */
533 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
535 char *ibuf, *rbuf _U_;
536 int ibuflen _U_, *rbuflen;
546 LOG(log_info, logtype_afpd, "begin afp_delete:");
552 memcpy( &vid, ibuf, sizeof( vid ));
553 ibuf += sizeof( vid );
554 if (NULL == ( vol = getvolbyvid( vid )) ) {
555 return( AFPERR_PARAM );
558 if (vol->v_flags & AFPVOL_RO)
561 memcpy( &did, ibuf, sizeof( did ));
562 ibuf += sizeof( int );
563 if (NULL == ( dir = dirlookup( vol, did )) ) {
567 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
568 return get_afp_errno(AFPERR_NOOBJ);
571 upath = s_path->u_name;
572 if ( path_isadir( s_path) ) {
573 if (*s_path->m_name != '\0') {
577 rc = deletecurdir( vol);
579 } else if (of_findname(s_path)) {
582 rc = deletefile(vol, upath, 1);
584 if ( rc == AFP_OK ) {
586 setvoltime(obj, vol );
590 LOG(log_info, logtype_afpd, "end afp_delete:");
595 /* ------------------------ */
596 char *absupath( vol, dir, u )
597 const struct vol *vol;
602 static char path[ MAXPATHLEN + 1];
609 p = path + sizeof( path ) - 1;
614 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
617 if (p -len -1 < path) {
619 rather rare so LOG error and/or client message ?
627 len = strlen( vol->v_path );
628 if (p -len -1 < path) {
633 memcpy( p, vol->v_path, len );
638 /* ------------------------
639 * FIXME dir could be NULL
641 char *ctoupath( vol, dir, name )
642 const struct vol *vol;
646 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
649 /* ------------------------- */
650 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
652 char *ibuf, *rbuf _U_;
653 int ibuflen _U_, *rbuflen;
656 struct dir *sdir, *ddir;
658 char *oldname, *newname;
667 #endif /* DROPKLUDGE */
670 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
676 memcpy( &vid, ibuf, sizeof( vid ));
677 ibuf += sizeof( vid );
678 if (NULL == ( vol = getvolbyvid( vid )) ) {
679 return( AFPERR_PARAM );
682 if (vol->v_flags & AFPVOL_RO)
685 /* source did followed by dest did */
686 memcpy( &did, ibuf, sizeof( did ));
687 ibuf += sizeof( int );
688 if (NULL == ( sdir = dirlookup( vol, did )) ) {
689 return afp_errno; /* was AFPERR_PARAM */
692 memcpy( &did, ibuf, sizeof( did ));
693 ibuf += sizeof( int );
695 /* source pathname */
696 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
697 return get_afp_errno(AFPERR_NOOBJ);
701 newname = obj->newtmp;
702 oldname = obj->oldtmp;
704 isdir = path_isadir(path);
705 if ( *path->m_name != '\0' ) {
709 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
711 strcpy(oldname, sdir->d_m_name);
714 /* get the destination directory */
715 if (NULL == ( ddir = dirlookup( vol, did )) ) {
716 return afp_errno; /* was AFPERR_PARAM */
718 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
719 return( AFPERR_NOOBJ );
721 pdid = curdir->d_did;
722 if ( *path->m_name != '\0' ) {
723 return path_error(path, AFPERR_NOOBJ);
726 /* one more place where we know about path type */
727 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
728 return( AFPERR_PARAM );
732 strcpy(newname, oldname);
735 rc = moveandrename(vol, sdir, oldname, newname, isdir);
737 if ( rc == AFP_OK ) {
738 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
746 if (vol->v_flags & AFPVOL_DROPBOX) {
747 /* FIXME did is not always the source id */
748 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
753 #endif /* DROPKLUDGE */
754 /* if unix priv don't try to match perm with dest folder */
755 if (!isdir && !vol_unix_priv(vol)) {
756 int admode = ad_mode("", 0777) | vol->v_fperm;
758 setfilmode(upath, admode, NULL);
759 vol->vfs->rf_setfilmode(vol, upath, admode, NULL);
761 setvoltime(obj, vol );
765 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
771 int veto_file(const char*veto_str, const char*path)
772 /* given a veto_str like "abc/zxc/" and path "abc", return 1
773 * veto_str should be '/' delimited
774 * if path matches any one of the veto_str elements exactly, then 1 is returned
775 * otherwise, 0 is returned.
778 int i; /* index to veto_str */
779 int j; /* index to path */
781 if ((veto_str == NULL) || (path == NULL))
784 for(i=0, j=0; veto_str[i] != '\0'; i++) {
785 if (veto_str[i] == '/') {
786 if ((j>0) && (path[j] == '\0')) {
787 LOG(log_info, logtype_afpd, "vetoed file:'%s'", path);
792 if (veto_str[i] != path[j]) {
793 while ((veto_str[i] != '/')
794 && (veto_str[i] != '\0'))