2 * $Id: filedir.c,v 1.57 2009-10-14 15:04:01 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(
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(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int *rbuflen)
140 u_int16_t fbitmap, dbitmap, vid;
144 LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
150 memcpy( &vid, ibuf, sizeof( vid ));
151 ibuf += sizeof( vid );
152 if (NULL == ( vol = getvolbyvid( vid )) ) {
153 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
156 return( AFPERR_ACCESS );
159 memcpy( &did, ibuf, sizeof( did ));
160 ibuf += sizeof( did );
162 if (NULL == ( dir = dirlookup( vol, did )) ) {
166 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
167 fbitmap = ntohs( fbitmap );
168 ibuf += sizeof( fbitmap );
169 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
170 dbitmap = ntohs( dbitmap );
171 ibuf += sizeof( dbitmap );
173 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
174 return get_afp_errno(AFPERR_NOOBJ);
178 if (!s_path->st_valid) {
179 /* it's a dir and it should be there
180 * because we chdir in it in cname or
181 * it's curdir (maybe deleted, but then we can't know).
182 * So we need to try harder.
184 of_statdir(vol, s_path);
186 if ( s_path->st_errno != 0 ) {
187 return( AFPERR_NOOBJ );
191 if (S_ISDIR(st->st_mode)) {
197 ret = getdirparams(vol, dbitmap, s_path, dir,
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 && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
206 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
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(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
234 u_int16_t vid, bitmap;
238 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
243 memcpy( &vid, ibuf, sizeof(vid));
244 ibuf += sizeof( vid );
246 if (NULL == ( vol = getvolbyvid( vid )) ) {
247 return( AFPERR_PARAM );
250 if (vol->v_flags & AFPVOL_RO)
253 memcpy( &did, ibuf, sizeof( did));
254 ibuf += sizeof( did);
256 if (NULL == ( dir = dirlookup( vol, did )) ) {
260 memcpy( &bitmap, ibuf, sizeof( bitmap ));
261 bitmap = ntohs( bitmap );
262 ibuf += sizeof( bitmap );
264 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
265 return get_afp_errno(AFPERR_NOOBJ);
269 if (!path->st_valid) {
270 /* it's a dir and it should be there
271 * because we chdir in it in cname
273 of_statdir(vol, path);
276 if ( path->st_errno != 0 ) {
277 return( AFPERR_NOOBJ );
280 * If ibuf is odd, make it even.
282 if ((u_long)ibuf & 1 ) {
286 if (S_ISDIR(st->st_mode)) {
287 rc = setdirparams(vol, path, bitmap, ibuf );
289 rc = setfilparams(vol, path, bitmap, ibuf );
291 if ( rc == AFP_OK ) {
292 setvoltime(obj, vol );
296 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
302 /* --------------------------------------------
303 Factorise some checks on a pathname
305 int check_name(const struct vol *vol, char *name)
307 /* check for illegal characters in the unix filename */
308 if (!wincheck(vol, name))
311 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
314 if (!vol->vfs->vfs_validupath(vol, name)) {
315 LOG(log_info, logtype_afpd, "check_name: illegal name: '%s'", name);
319 /* check for vetoed filenames */
320 if (veto_file(vol->v_veto, name))
325 /* -------------------------
326 move and rename sdir:oldname to curdir:newname in volume vol
328 special care is needed for lock
330 static int moveandrename(const struct vol *vol, struct dir *sdir, char *oldname, char *newname, int isdir)
335 struct stat *st, nst;
339 struct ofork *opened = NULL;
343 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
348 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
350 return AFPERR_PARAM; /* can't convert */
352 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
353 p = ctoupath( vol, sdir, oldname );
355 return AFPERR_PARAM; /* pathname too long */
359 if ((opened = of_findname(&path))) {
360 /* reuse struct adouble so it won't break locks */
365 id = sdir->d_did; /* we already have the CNID */
366 p = ctoupath( vol, sdir->d_parent, oldname );
370 adflags = ADFLAGS_DIR;
373 * p now points to the full pathname of the source fs object.
375 * we are in the dest folder so we need to use p for ad_open
378 if (!ad_metadata(p, adflags, adp)) {
381 ad_getattr(adp, &bshort);
382 ad_close_metadata( adp);
383 if ((bshort & htons(ATTRBIT_NORENAME)))
384 return(AFPERR_OLOCK);
387 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
392 if (0 != (rc = check_name(vol, upath))) {
396 /* source == destination. we just silently accept this. */
397 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
398 if (strcmp(oldname, newname) == 0)
401 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
402 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
403 /* not the same file */
408 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
413 path.st_errno = errno;
414 if (of_findname(&path)) {
415 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
417 rc = renamefile(vol, p, upath, newname, adp );
419 of_rename(vol, opened, sdir, oldname, curdir, newname);
422 rc = renamedir(vol, p, upath, sdir, curdir, newname);
424 if ( rc == AFP_OK && id ) {
425 /* renaming may have moved the file/dir across a filesystem */
426 if (stat(upath, st) < 0)
429 /* fix up the catalog entry */
430 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
436 /* -------------------------------------------- */
437 int afp_rename(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
441 char *oldname, *newname;
449 LOG(log_info, logtype_afpd, "begin afp_rename:");
455 memcpy( &vid, ibuf, sizeof( vid ));
456 ibuf += sizeof( vid );
457 if (NULL == ( vol = getvolbyvid( vid )) ) {
458 return( AFPERR_PARAM );
461 if (vol->v_flags & AFPVOL_RO)
464 memcpy( &did, ibuf, sizeof( did ));
465 ibuf += sizeof( did );
466 if (NULL == ( sdir = dirlookup( vol, did )) ) {
470 /* source pathname */
471 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
472 return get_afp_errno(AFPERR_NOOBJ);
476 newname = obj->newtmp;
477 oldname = obj->oldtmp;
478 isdir = path_isadir(path);
479 if ( *path->m_name != '\0' ) {
480 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
482 /* curdir parent dir, need to move sdir back */
487 if ( sdir->d_parent == NULL ) { /* root directory */
488 return( AFPERR_NORENAME );
490 /* move to destination dir */
491 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
494 strcpy(oldname, sdir->d_m_name);
497 /* another place where we know about the path type */
498 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
499 return( AFPERR_PARAM );
503 return AFP_OK; /* newname == oldname same dir */
506 rc = moveandrename(vol, sdir, oldname, newname, isdir);
508 if ( rc == AFP_OK ) {
509 setvoltime(obj, vol );
513 LOG(log_info, logtype_afpd, "end afp_rename:");
519 /* ------------------------------- */
520 int afp_delete(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
530 LOG(log_info, logtype_afpd, "begin afp_delete:");
536 memcpy( &vid, ibuf, sizeof( vid ));
537 ibuf += sizeof( vid );
538 if (NULL == ( vol = getvolbyvid( vid )) ) {
539 return( AFPERR_PARAM );
542 if (vol->v_flags & AFPVOL_RO)
545 memcpy( &did, ibuf, sizeof( did ));
546 ibuf += sizeof( int );
547 if (NULL == ( dir = dirlookup( vol, did )) ) {
551 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
552 return get_afp_errno(AFPERR_NOOBJ);
555 upath = s_path->u_name;
556 if ( path_isadir( s_path) ) {
557 if (*s_path->m_name != '\0') {
561 rc = deletecurdir( vol);
563 } else if (of_findname(s_path)) {
566 rc = deletefile(vol, upath, 1);
568 if ( rc == AFP_OK ) {
570 setvoltime(obj, vol );
574 LOG(log_info, logtype_afpd, "end afp_delete:");
579 /* ------------------------ */
580 char *absupath(const struct vol *vol, struct dir *dir, char *u)
583 static char path[ MAXPATHLEN + 1];
590 p = path + sizeof( path ) - 1;
595 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
598 if (p -len -1 < path) {
600 rather rare so LOG error and/or client message ?
608 len = strlen( vol->v_path );
609 if (p -len -1 < path) {
614 memcpy( p, vol->v_path, len );
619 /* ------------------------
620 * FIXME dir could be NULL
622 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
624 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
627 /* ------------------------- */
628 int afp_moveandrename(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
631 struct dir *sdir, *ddir;
633 char *oldname, *newname;
642 #endif /* DROPKLUDGE */
645 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
651 memcpy( &vid, ibuf, sizeof( vid ));
652 ibuf += sizeof( vid );
653 if (NULL == ( vol = getvolbyvid( vid )) ) {
654 return( AFPERR_PARAM );
657 if (vol->v_flags & AFPVOL_RO)
660 /* source did followed by dest did */
661 memcpy( &did, ibuf, sizeof( did ));
662 ibuf += sizeof( int );
663 if (NULL == ( sdir = dirlookup( vol, did )) ) {
664 return afp_errno; /* was AFPERR_PARAM */
667 memcpy( &did, ibuf, sizeof( did ));
668 ibuf += sizeof( int );
670 /* source pathname */
671 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
672 return get_afp_errno(AFPERR_NOOBJ);
676 newname = obj->newtmp;
677 oldname = obj->oldtmp;
679 isdir = path_isadir(path);
680 if ( *path->m_name != '\0' ) {
684 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
686 strcpy(oldname, sdir->d_m_name);
689 /* get the destination directory */
690 if (NULL == ( ddir = dirlookup( vol, did )) ) {
691 return afp_errno; /* was AFPERR_PARAM */
693 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
694 return( AFPERR_NOOBJ );
696 pdid = curdir->d_did;
697 if ( *path->m_name != '\0' ) {
698 return path_error(path, AFPERR_NOOBJ);
701 /* one more place where we know about path type */
702 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
703 return( AFPERR_PARAM );
707 strcpy(newname, oldname);
710 rc = moveandrename(vol, sdir, oldname, newname, isdir);
712 if ( rc == AFP_OK ) {
713 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
721 if (vol->v_flags & AFPVOL_DROPBOX) {
722 /* FIXME did is not always the source id */
723 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
728 #endif /* DROPKLUDGE */
729 /* if unix priv don't try to match perm with dest folder */
730 if (!isdir && !vol_unix_priv(vol)) {
731 int admode = ad_mode("", 0777) | vol->v_fperm;
733 setfilmode(upath, admode, NULL, vol->v_umask);
734 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
736 setvoltime(obj, vol );
740 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
746 int veto_file(const char*veto_str, const char*path)
747 /* given a veto_str like "abc/zxc/" and path "abc", return 1
748 * veto_str should be '/' delimited
749 * if path matches any one of the veto_str elements exactly, then 1 is returned
750 * otherwise, 0 is returned.
753 int i; /* index to veto_str */
754 int j; /* index to path */
756 if ((veto_str == NULL) || (path == NULL))
759 for(i=0, j=0; veto_str[i] != '\0'; i++) {
760 if (veto_str[i] == '/') {
761 if ((j>0) && (path[j] == '\0')) {
762 LOG(log_info, logtype_afpd, "vetoed file:'%s'", path);
767 if (veto_str[i] != path[j]) {
768 while ((veto_str[i] != '/')
769 && (veto_str[i] != '\0'))