2 * $Id: filedir.c,v 1.64 2009-10-29 13:38:15 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 */
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 (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);
175 if (!s_path->st_valid) {
176 /* it's a dir and it should be there
177 * because we chdir in it in cname or
178 * it's curdir (maybe deleted, but then we can't know).
179 * So we need to try harder.
181 of_statdir(vol, s_path);
183 if ( s_path->st_errno != 0 ) {
184 return( AFPERR_NOOBJ );
188 if (S_ISDIR(st->st_mode)) {
194 ret = getdirparams(vol, dbitmap, s_path, dir,
195 rbuf + 3 * sizeof( u_int16_t ), &buflen );
199 /* this is a directory */
200 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
202 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
203 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
207 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
209 *rbuflen = buflen + 3 * sizeof( u_int16_t );
210 fbitmap = htons( fbitmap );
211 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
212 rbuf += sizeof( fbitmap );
213 dbitmap = htons( dbitmap );
214 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
215 rbuf += sizeof( dbitmap ) + sizeof( u_char );
221 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
227 u_int16_t vid, bitmap;
232 memcpy( &vid, ibuf, sizeof(vid));
233 ibuf += sizeof( vid );
235 if (NULL == ( vol = getvolbyvid( vid )) ) {
236 return( AFPERR_PARAM );
239 if (vol->v_flags & AFPVOL_RO)
242 memcpy( &did, ibuf, sizeof( did));
243 ibuf += sizeof( did);
245 if (NULL == ( dir = dirlookup( vol, did )) ) {
249 memcpy( &bitmap, ibuf, sizeof( bitmap ));
250 bitmap = ntohs( bitmap );
251 ibuf += sizeof( bitmap );
253 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
254 return get_afp_errno(AFPERR_NOOBJ);
258 if (!path->st_valid) {
259 /* it's a dir and it should be there
260 * because we chdir in it in cname
262 of_statdir(vol, path);
265 if ( path->st_errno != 0 ) {
266 return( AFPERR_NOOBJ );
269 * If ibuf is odd, make it even.
271 if ((u_long)ibuf & 1 ) {
275 if (S_ISDIR(st->st_mode)) {
276 rc = setdirparams(vol, path, bitmap, ibuf );
278 rc = setfilparams(vol, path, bitmap, ibuf );
280 if ( rc == AFP_OK ) {
281 setvoltime(obj, vol );
287 /* --------------------------------------------
288 Factorise some checks on a pathname
290 int check_name(const struct vol *vol, char *name)
292 /* check for illegal characters in the unix filename */
293 if (!wincheck(vol, name))
296 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
299 if (!vol->vfs->vfs_validupath(vol, name)) {
300 LOG(log_info, logtype_afpd, "check_name: illegal name: '%s'", name);
304 /* check for vetoed filenames */
305 if (veto_file(vol->v_veto, name))
310 /* -------------------------
311 move and rename sdir:oldname to curdir:newname in volume vol
313 special care is needed for lock
315 static int moveandrename(const struct vol *vol, struct dir *sdir, char *oldname, char *newname, int isdir)
320 struct stat *st, nst;
324 struct ofork *opened = NULL;
328 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
333 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
335 return AFPERR_PARAM; /* can't convert */
337 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
338 p = ctoupath( vol, sdir, oldname );
340 return AFPERR_PARAM; /* pathname too long */
344 if ((opened = of_findname(&path))) {
345 /* reuse struct adouble so it won't break locks */
350 id = sdir->d_did; /* we already have the CNID */
351 p = ctoupath( vol, sdir->d_parent, oldname );
355 adflags = ADFLAGS_DIR;
358 * p now points to the full pathname of the source fs object.
360 * we are in the dest folder so we need to use p for ad_open
363 if (!ad_metadata(p, adflags, adp)) {
366 ad_getattr(adp, &bshort);
367 ad_close_metadata( adp);
368 if ((bshort & htons(ATTRBIT_NORENAME)))
369 return(AFPERR_OLOCK);
372 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
377 if (0 != (rc = check_name(vol, upath))) {
381 /* source == destination. we just silently accept this. */
382 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
383 if (strcmp(oldname, newname) == 0)
386 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
387 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
388 /* not the same file */
393 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
398 path.st_errno = errno;
399 if (of_findname(&path)) {
400 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
402 rc = renamefile(vol, p, upath, newname, adp );
404 of_rename(vol, opened, sdir, oldname, curdir, newname);
407 rc = renamedir(vol, p, upath, sdir, curdir, newname);
409 if ( rc == AFP_OK && id ) {
410 /* renaming may have moved the file/dir across a filesystem */
411 if (stat(upath, st) < 0)
414 /* fix up the catalog entry */
415 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
421 /* -------------------------------------------- */
422 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
426 char *oldname, *newname;
437 memcpy( &vid, ibuf, sizeof( vid ));
438 ibuf += sizeof( vid );
439 if (NULL == ( vol = getvolbyvid( vid )) ) {
440 return( AFPERR_PARAM );
443 if (vol->v_flags & AFPVOL_RO)
446 memcpy( &did, ibuf, sizeof( did ));
447 ibuf += sizeof( did );
448 if (NULL == ( sdir = dirlookup( vol, did )) ) {
452 /* source pathname */
453 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
454 return get_afp_errno(AFPERR_NOOBJ);
458 newname = obj->newtmp;
459 oldname = obj->oldtmp;
460 isdir = path_isadir(path);
461 if ( *path->m_name != '\0' ) {
462 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
464 /* curdir parent dir, need to move sdir back */
469 if ( sdir->d_parent == NULL ) { /* root directory */
470 return( AFPERR_NORENAME );
472 /* move to destination dir */
473 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
476 strcpy(oldname, sdir->d_m_name);
479 /* another place where we know about the path type */
480 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
481 return( AFPERR_PARAM );
485 return AFP_OK; /* newname == oldname same dir */
488 rc = moveandrename(vol, sdir, oldname, newname, isdir);
490 if ( rc == AFP_OK ) {
491 setvoltime(obj, vol );
497 /* ------------------------------- */
498 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
510 memcpy( &vid, ibuf, sizeof( vid ));
511 ibuf += sizeof( vid );
512 if (NULL == ( vol = getvolbyvid( vid )) ) {
513 return( AFPERR_PARAM );
516 if (vol->v_flags & AFPVOL_RO)
519 memcpy( &did, ibuf, sizeof( did ));
520 ibuf += sizeof( int );
521 if (NULL == ( dir = dirlookup( vol, did )) ) {
525 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
526 return get_afp_errno(AFPERR_NOOBJ);
529 upath = s_path->u_name;
530 if ( path_isadir( s_path) ) {
531 if (*s_path->m_name != '\0') {
535 rc = deletecurdir( vol);
537 } else if (of_findname(s_path)) {
540 /* it's a file st_valid should always be true
541 * only test for ENOENT because EACCES needs
542 * to read meta data in deletefile
544 if (s_path->st_valid && s_path->st_errno == ENOENT) {
548 rc = deletefile(vol, upath, 1);
551 if ( rc == AFP_OK ) {
553 setvoltime(obj, vol );
558 /* ------------------------ */
559 char *absupath(const struct vol *vol, struct dir *dir, char *u)
562 static char path[ MAXPATHLEN + 1];
569 p = path + sizeof( path ) - 1;
574 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
577 if (p -len -1 < path) {
579 rather rare so LOG error and/or client message ?
587 len = strlen( vol->v_path );
588 if (p -len -1 < path) {
593 memcpy( p, vol->v_path, len );
598 /* ------------------------
599 * FIXME dir could be NULL
601 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
603 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
606 /* ------------------------- */
607 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
610 struct dir *sdir, *ddir;
612 char *oldname, *newname;
621 #endif /* DROPKLUDGE */
626 memcpy( &vid, ibuf, sizeof( vid ));
627 ibuf += sizeof( vid );
628 if (NULL == ( vol = getvolbyvid( vid )) ) {
629 return( AFPERR_PARAM );
632 if (vol->v_flags & AFPVOL_RO)
635 /* source did followed by dest did */
636 memcpy( &did, ibuf, sizeof( did ));
637 ibuf += sizeof( int );
638 if (NULL == ( sdir = dirlookup( vol, did )) ) {
639 return afp_errno; /* was AFPERR_PARAM */
642 memcpy( &did, ibuf, sizeof( did ));
643 ibuf += sizeof( int );
645 /* source pathname */
646 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
647 return get_afp_errno(AFPERR_NOOBJ);
651 newname = obj->newtmp;
652 oldname = obj->oldtmp;
654 isdir = path_isadir(path);
655 if ( *path->m_name != '\0' ) {
659 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
661 strcpy(oldname, sdir->d_m_name);
664 /* get the destination directory */
665 if (NULL == ( ddir = dirlookup( vol, did )) ) {
666 return afp_errno; /* was AFPERR_PARAM */
668 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
669 return( AFPERR_NOOBJ );
671 pdid = curdir->d_did;
672 if ( *path->m_name != '\0' ) {
673 return path_error(path, AFPERR_NOOBJ);
676 /* one more place where we know about path type */
677 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
678 return( AFPERR_PARAM );
682 strcpy(newname, oldname);
685 rc = moveandrename(vol, sdir, oldname, newname, isdir);
687 if ( rc == AFP_OK ) {
688 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
696 if (vol->v_flags & AFPVOL_DROPBOX) {
697 /* FIXME did is not always the source id */
698 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
703 #endif /* DROPKLUDGE */
704 /* if unix priv don't try to match perm with dest folder */
705 if (!isdir && !vol_unix_priv(vol)) {
706 int admode = ad_mode("", 0777) | vol->v_fperm;
708 setfilmode(upath, admode, NULL, vol->v_umask);
709 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
711 setvoltime(obj, vol );
717 int veto_file(const char*veto_str, const char*path)
718 /* given a veto_str like "abc/zxc/" and path "abc", return 1
719 * veto_str should be '/' delimited
720 * if path matches any one of the veto_str elements exactly, then 1 is returned
721 * otherwise, 0 is returned.
724 int i; /* index to veto_str */
725 int j; /* index to path */
727 if ((veto_str == NULL) || (path == NULL))
730 for(i=0, j=0; veto_str[i] != '\0'; i++) {
731 if (veto_str[i] == '/') {
732 if ((j>0) && (path[j] == '\0')) {
733 LOG(log_info, logtype_afpd, "vetoed file:'%s'", path);
738 if (veto_str[i] != path[j]) {
739 while ((veto_str[i] != '/')
740 && (veto_str[i] != '\0'))