2 * $Id: filedir.c,v 1.73 2010-03-12 15:16:49 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
316 special care is needed for lock
318 static int moveandrename(const struct vol *vol,
328 struct stat *st, nst;
332 struct ofork *opened = NULL;
337 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
342 if ((p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding())) == NULL)
343 return AFPERR_PARAM; /* can't convert */
345 #ifndef HAVE_RENAMEAT
347 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
348 p = ctoupath( vol, sdir, oldname );
350 return AFPERR_PARAM; /* pathname too long */
351 #endif /* HAVE_RENAMEAT */
356 opened = of_findnameat(sdir_fd, &path);
358 opened = of_findname(&path);
359 #endif /* HAVE_RENAMEAT */
361 /* 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;
375 * p now points to either
376 * a) full pathname of the source fs object (if renameat is not available)
377 * b) the oldname (renameat is available)
378 * we are in the dest folder so we need to use
380 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
384 if ((cwd_fd = open(".", O_RDONLY)) == -1)
386 if (fchdir(sdir_fd) != 0)
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 (fchdir(cwd_fd) != 0) {
399 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
404 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
409 if (0 != (rc = check_name(vol, upath))) {
413 /* source == destination. we just silently accept this. */
414 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
415 if (strcmp(oldname, newname) == 0)
418 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
419 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
420 /* not the same file */
425 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
430 path.st_errno = errno;
431 if (of_findname(&path)) {
432 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
434 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
436 of_rename(vol, opened, sdir, oldname, curdir, newname);
439 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
441 if ( rc == AFP_OK && id ) {
442 /* renaming may have moved the file/dir across a filesystem */
443 if (stat(upath, st) < 0)
446 /* fix up the catalog entry */
447 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
453 /* -------------------------------------------- */
454 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
458 char *oldname, *newname;
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, -1, oldname, newname, isdir);
521 if ( rc == AFP_OK ) {
522 setvoltime(obj, vol );
528 /* ------------------------------- */
529 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
541 memcpy( &vid, ibuf, sizeof( vid ));
542 ibuf += sizeof( vid );
543 if (NULL == ( vol = getvolbyvid( vid )) ) {
544 return( AFPERR_PARAM );
547 if (vol->v_flags & AFPVOL_RO)
550 memcpy( &did, ibuf, sizeof( did ));
551 ibuf += sizeof( int );
552 if (NULL == ( dir = dirlookup( vol, did )) ) {
556 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
557 return get_afp_errno(AFPERR_NOOBJ);
560 upath = s_path->u_name;
561 if ( path_isadir( s_path) ) {
562 if (*s_path->m_name != '\0') {
566 rc = deletecurdir( vol);
568 } else if (of_findname(s_path)) {
571 /* it's a file st_valid should always be true
572 * only test for ENOENT because EACCES needs
573 * to read meta data in deletefile
575 if (s_path->st_valid && s_path->st_errno == ENOENT) {
579 rc = deletefile(vol, -1, upath, 1);
582 if ( rc == AFP_OK ) {
584 setvoltime(obj, vol );
589 /* ------------------------ */
590 char *absupath(const struct vol *vol, struct dir *dir, char *u)
593 static char path[ MAXPATHLEN + 1];
600 p = path + sizeof( path ) - 1;
605 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
608 if (p -len -1 < path) {
610 rather rare so LOG error and/or client message ?
618 len = strlen( vol->v_path );
619 if (p -len -1 < path) {
624 memcpy( p, vol->v_path, len );
629 /* ------------------------
630 * FIXME dir could be NULL
632 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
634 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
637 /* ------------------------- */
638 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
641 struct dir *sdir, *ddir;
643 char *oldname, *newname;
652 #endif /* DROPKLUDGE */
659 memcpy( &vid, ibuf, sizeof( vid ));
660 ibuf += sizeof( vid );
661 if (NULL == ( vol = getvolbyvid( vid )) ) {
662 return( AFPERR_PARAM );
665 if (vol->v_flags & AFPVOL_RO)
668 /* source did followed by dest did */
669 memcpy( &did, ibuf, sizeof( did ));
670 ibuf += sizeof( int );
671 if (NULL == ( sdir = dirlookup( vol, did )) ) {
672 return afp_errno; /* was AFPERR_PARAM */
675 memcpy( &did, ibuf, sizeof( did ));
676 ibuf += sizeof( int );
678 /* source pathname */
679 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
680 return get_afp_errno(AFPERR_NOOBJ);
684 newname = obj->newtmp;
685 oldname = obj->oldtmp;
687 isdir = path_isadir(path);
688 if ( *path->m_name != '\0' ) {
692 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
694 strcpy(oldname, sdir->d_m_name);
698 if ((sdir_fd = open(".", O_RDONLY)) == -1)
702 /* get the destination directory */
703 if (NULL == ( ddir = dirlookup( vol, did )) ) {
704 rc = afp_errno; /* was AFPERR_PARAM */
707 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
711 pdid = curdir->d_did;
712 if ( *path->m_name != '\0' ) {
713 rc = path_error(path, AFPERR_NOOBJ);
717 /* one more place where we know about path type */
718 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
724 strcpy(newname, oldname);
727 /* This does the work */
728 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
730 if ( rc == AFP_OK ) {
731 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
740 if (vol->v_flags & AFPVOL_DROPBOX) {
741 /* FIXME did is not always the source id */
742 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
748 #endif /* DROPKLUDGE */
749 /* if unix priv don't try to match perm with dest folder */
750 if (!isdir && !vol_unix_priv(vol)) {
751 int admode = ad_mode("", 0777) | vol->v_fperm;
753 setfilmode(upath, admode, NULL, vol->v_umask);
754 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
756 setvoltime(obj, vol );
768 int veto_file(const char*veto_str, const char*path)
769 /* given a veto_str like "abc/zxc/" and path "abc", return 1
770 * veto_str should be '/' delimited
771 * if path matches any one of the veto_str elements exactly, then 1 is returned
772 * otherwise, 0 is returned.
775 int i; /* index to veto_str */
776 int j; /* index to path */
778 if ((veto_str == NULL) || (path == NULL))
781 for(i=0, j=0; veto_str[i] != '\0'; i++) {
782 if (veto_str[i] == '/') {
783 if ((j>0) && (path[j] == '\0')) {
784 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
789 if (veto_str[i] != path[j]) {
790 while ((veto_str[i] != '/')
791 && (veto_str[i] != '\0'))