2 * $Id: filedir.c,v 1.70 2010-02-10 14:05:37 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 */
33 #include <sys/param.h>
35 #include <atalk/adouble.h>
36 #include <atalk/vfs.h>
37 #include <atalk/afp.h>
38 #include <atalk/util.h>
39 #include <atalk/cnid.h>
40 #include <atalk/logger.h>
41 #include <atalk/unix.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_debug9, 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 ((!S_ISLNK(st->st_mode)) && (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_debug9, logtype_afpd, "end matchfile2dirperms:");
133 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
141 u_int16_t fbitmap, dbitmap, vid;
147 memcpy( &vid, ibuf, sizeof( vid ));
148 ibuf += sizeof( vid );
149 if (NULL == ( vol = getvolbyvid( vid )) ) {
150 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
153 return( AFPERR_ACCESS );
156 memcpy( &did, ibuf, sizeof( did ));
157 ibuf += sizeof( did );
159 if (NULL == ( dir = dirlookup( vol, did )) ) {
163 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
164 fbitmap = ntohs( fbitmap );
165 ibuf += sizeof( fbitmap );
166 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
167 dbitmap = ntohs( dbitmap );
168 ibuf += sizeof( dbitmap );
170 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
171 return get_afp_errno(AFPERR_NOOBJ);
174 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, name:'%s', f/d:%04x/%04x) {cwd: %s}",
175 ntohs(vid), ntohl(dir->d_did), s_path->u_name, fbitmap, dbitmap, getcwdpath());
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 );
192 if (S_ISDIR(st->st_mode)) {
198 ret = getdirparams(vol, dbitmap, s_path, dir,
199 rbuf + 3 * sizeof( u_int16_t ), &buflen );
203 /* this is a directory */
204 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
206 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
207 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
211 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
213 *rbuflen = buflen + 3 * sizeof( u_int16_t );
214 fbitmap = htons( fbitmap );
215 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
216 rbuf += sizeof( fbitmap );
217 dbitmap = htons( dbitmap );
218 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
219 rbuf += sizeof( dbitmap ) + sizeof( u_char );
225 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
231 u_int16_t vid, bitmap;
236 memcpy( &vid, ibuf, sizeof(vid));
237 ibuf += sizeof( vid );
239 if (NULL == ( vol = getvolbyvid( vid )) ) {
240 return( AFPERR_PARAM );
243 if (vol->v_flags & AFPVOL_RO)
246 memcpy( &did, ibuf, sizeof( did));
247 ibuf += sizeof( did);
249 if (NULL == ( dir = dirlookup( vol, did )) ) {
253 memcpy( &bitmap, ibuf, sizeof( bitmap ));
254 bitmap = ntohs( bitmap );
255 ibuf += sizeof( bitmap );
257 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
258 return get_afp_errno(AFPERR_NOOBJ);
262 if (!path->st_valid) {
263 /* it's a dir and it should be there
264 * because we chdir in it in cname
266 of_statdir(vol, path);
269 if ( path->st_errno != 0 ) {
270 return( AFPERR_NOOBJ );
273 * If ibuf is odd, make it even.
275 if ((u_long)ibuf & 1 ) {
279 if (S_ISDIR(st->st_mode)) {
280 rc = setdirparams(vol, path, bitmap, ibuf );
282 rc = setfilparams(vol, path, bitmap, ibuf );
284 if ( rc == AFP_OK ) {
285 setvoltime(obj, vol );
291 /* --------------------------------------------
292 Factorise some checks on a pathname
294 int check_name(const struct vol *vol, char *name)
296 /* check for illegal characters in the unix filename */
297 if (!wincheck(vol, name))
300 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
303 if (!vol->vfs->vfs_validupath(vol, name)) {
304 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
308 /* check for vetoed filenames */
309 if (veto_file(vol->v_veto, name))
314 /* -------------------------
315 move and rename sdir:oldname to curdir:newname in volume vol
317 special care is needed for lock
319 static int moveandrename(const struct vol *vol, struct dir *sdir, char *oldname, char *newname, int isdir)
324 struct stat *st, nst;
328 struct ofork *opened = NULL;
332 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
337 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
339 return AFPERR_PARAM; /* can't convert */
341 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
342 p = ctoupath( vol, sdir, oldname );
344 return AFPERR_PARAM; /* pathname too long */
348 if ((opened = of_findname(&path))) {
349 /* reuse struct adouble so it won't break locks */
354 id = sdir->d_did; /* we already have the CNID */
355 p = ctoupath( vol, sdir->d_parent, oldname );
359 adflags = ADFLAGS_DIR;
362 * p now points to the full pathname of the source fs object.
364 * we are in the dest folder so we need to use p for ad_open
367 if (!ad_metadata(p, adflags, adp)) {
370 ad_getattr(adp, &bshort);
371 ad_close_metadata( adp);
372 if ((bshort & htons(ATTRBIT_NORENAME)))
373 return(AFPERR_OLOCK);
376 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
381 if (0 != (rc = check_name(vol, upath))) {
385 /* source == destination. we just silently accept this. */
386 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
387 if (strcmp(oldname, newname) == 0)
390 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
391 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
392 /* not the same file */
397 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
402 path.st_errno = errno;
403 if (of_findname(&path)) {
404 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
406 rc = renamefile(vol, p, upath, newname, adp );
408 of_rename(vol, opened, sdir, oldname, curdir, newname);
411 rc = renamedir(vol, p, upath, sdir, curdir, newname);
413 if ( rc == AFP_OK && id ) {
414 /* renaming may have moved the file/dir across a filesystem */
415 if (stat(upath, st) < 0)
418 /* fix up the catalog entry */
419 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
425 /* -------------------------------------------- */
426 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
430 char *oldname, *newname;
441 memcpy( &vid, ibuf, sizeof( vid ));
442 ibuf += sizeof( vid );
443 if (NULL == ( vol = getvolbyvid( vid )) ) {
444 return( AFPERR_PARAM );
447 if (vol->v_flags & AFPVOL_RO)
450 memcpy( &did, ibuf, sizeof( did ));
451 ibuf += sizeof( did );
452 if (NULL == ( sdir = dirlookup( vol, did )) ) {
456 /* source pathname */
457 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
458 return get_afp_errno(AFPERR_NOOBJ);
462 newname = obj->newtmp;
463 oldname = obj->oldtmp;
464 isdir = path_isadir(path);
465 if ( *path->m_name != '\0' ) {
466 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
468 /* curdir parent dir, need to move sdir back */
473 if ( sdir->d_parent == NULL ) { /* root directory */
474 return( AFPERR_NORENAME );
476 /* move to destination dir */
477 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
480 strcpy(oldname, sdir->d_m_name);
483 /* another place where we know about the path type */
484 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
485 return( AFPERR_PARAM );
489 return AFP_OK; /* newname == oldname same dir */
492 rc = moveandrename(vol, sdir, oldname, newname, isdir);
494 if ( rc == AFP_OK ) {
495 setvoltime(obj, vol );
501 /* ------------------------------- */
502 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
514 memcpy( &vid, ibuf, sizeof( vid ));
515 ibuf += sizeof( vid );
516 if (NULL == ( vol = getvolbyvid( vid )) ) {
517 return( AFPERR_PARAM );
520 if (vol->v_flags & AFPVOL_RO)
523 memcpy( &did, ibuf, sizeof( did ));
524 ibuf += sizeof( int );
525 if (NULL == ( dir = dirlookup( vol, did )) ) {
529 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
530 return get_afp_errno(AFPERR_NOOBJ);
533 upath = s_path->u_name;
534 if ( path_isadir( s_path) ) {
535 if (*s_path->m_name != '\0') {
539 rc = deletecurdir( vol);
541 } else if (of_findname(s_path)) {
544 /* it's a file st_valid should always be true
545 * only test for ENOENT because EACCES needs
546 * to read meta data in deletefile
548 if (s_path->st_valid && s_path->st_errno == ENOENT) {
552 rc = deletefile(vol, upath, 1);
555 if ( rc == AFP_OK ) {
557 setvoltime(obj, vol );
562 /* ------------------------ */
563 char *absupath(const struct vol *vol, struct dir *dir, char *u)
566 static char path[ MAXPATHLEN + 1];
573 p = path + sizeof( path ) - 1;
578 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
581 if (p -len -1 < path) {
583 rather rare so LOG error and/or client message ?
591 len = strlen( vol->v_path );
592 if (p -len -1 < path) {
597 memcpy( p, vol->v_path, len );
602 /* ------------------------
603 * FIXME dir could be NULL
605 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
607 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
610 /* ------------------------- */
611 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
614 struct dir *sdir, *ddir;
616 char *oldname, *newname;
625 #endif /* DROPKLUDGE */
630 memcpy( &vid, ibuf, sizeof( vid ));
631 ibuf += sizeof( vid );
632 if (NULL == ( vol = getvolbyvid( vid )) ) {
633 return( AFPERR_PARAM );
636 if (vol->v_flags & AFPVOL_RO)
639 /* source did followed by dest did */
640 memcpy( &did, ibuf, sizeof( did ));
641 ibuf += sizeof( int );
642 if (NULL == ( sdir = dirlookup( vol, did )) ) {
643 return afp_errno; /* was AFPERR_PARAM */
646 memcpy( &did, ibuf, sizeof( did ));
647 ibuf += sizeof( int );
649 /* source pathname */
650 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
651 return get_afp_errno(AFPERR_NOOBJ);
655 newname = obj->newtmp;
656 oldname = obj->oldtmp;
658 isdir = path_isadir(path);
659 if ( *path->m_name != '\0' ) {
663 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
665 strcpy(oldname, sdir->d_m_name);
668 /* get the destination directory */
669 if (NULL == ( ddir = dirlookup( vol, did )) ) {
670 return afp_errno; /* was AFPERR_PARAM */
672 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
673 return( AFPERR_NOOBJ );
675 pdid = curdir->d_did;
676 if ( *path->m_name != '\0' ) {
677 return path_error(path, AFPERR_NOOBJ);
680 /* one more place where we know about path type */
681 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
682 return( AFPERR_PARAM );
686 strcpy(newname, oldname);
689 rc = moveandrename(vol, sdir, oldname, newname, isdir);
691 if ( rc == AFP_OK ) {
692 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
700 if (vol->v_flags & AFPVOL_DROPBOX) {
701 /* FIXME did is not always the source id */
702 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
707 #endif /* DROPKLUDGE */
708 /* if unix priv don't try to match perm with dest folder */
709 if (!isdir && !vol_unix_priv(vol)) {
710 int admode = ad_mode("", 0777) | vol->v_fperm;
712 setfilmode(upath, admode, NULL, vol->v_umask);
713 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
715 setvoltime(obj, vol );
721 int veto_file(const char*veto_str, const char*path)
722 /* given a veto_str like "abc/zxc/" and path "abc", return 1
723 * veto_str should be '/' delimited
724 * if path matches any one of the veto_str elements exactly, then 1 is returned
725 * otherwise, 0 is returned.
728 int i; /* index to veto_str */
729 int j; /* index to path */
731 if ((veto_str == NULL) || (path == NULL))
734 for(i=0, j=0; veto_str[i] != '\0'; i++) {
735 if (veto_str[i] == '/') {
736 if ((j>0) && (path[j] == '\0')) {
737 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
742 if (veto_str[i] != path[j]) {
743 while ((veto_str[i] != '/')
744 && (veto_str[i] != '\0'))