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"
54 int matchfile2dirperms(
55 /* Since it's kinda' big; I decided against an
60 /* The below code changes the way file ownership is determined in the name of
61 fixing dropboxes. It has known security problem. See the netatalk FAQ for
70 LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
73 if (stat(upath, &st ) < 0) {
74 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
78 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
79 /* FIXME dirsearch doesn't move cwd to did ! */
80 if (( dir = dirlookup( vol, did )) == NULL ) {
81 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
84 else if (stat(".", &sb) < 0) {
85 LOG(log_error, logtype_afpd,
86 "matchfile2dirperms: Error checking directory \"%s\": %s",
87 dir->d_m_name, strerror(errno));
92 if ( uid != sb.st_uid )
95 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
97 LOG(log_error, logtype_afpd,
98 "matchfile2dirperms(%s): Error changing owner/gid: %s",
99 upath, strerror(errno));
102 else if ((!S_ISLNK(st->st_mode)) && (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0))
104 LOG(log_error, logtype_afpd,
105 "matchfile2dirperms(%s): Error adding file read permissions: %s",
106 upath, strerror(errno));
109 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
111 LOG(log_error, logtype_afpd,
112 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
113 adpath, strerror(errno));
116 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
118 LOG(log_error, logtype_afpd,
119 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
120 adpath, strerror(errno));
125 } /* end else if stat success */
128 LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
134 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
142 u_int16_t fbitmap, dbitmap, vid;
148 memcpy( &vid, ibuf, sizeof( vid ));
149 ibuf += sizeof( vid );
150 if (NULL == ( vol = getvolbyvid( vid )) ) {
151 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
154 return( AFPERR_ACCESS );
157 memcpy( &did, ibuf, sizeof( did ));
158 ibuf += sizeof( did );
160 if (NULL == ( dir = dirlookup( vol, did )) ) {
164 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
165 fbitmap = ntohs( fbitmap );
166 ibuf += sizeof( fbitmap );
167 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
168 dbitmap = ntohs( dbitmap );
169 ibuf += sizeof( dbitmap );
171 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
172 return get_afp_errno(AFPERR_NOOBJ);
175 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, name:'%s', f/d:%04x/%04x) {cwd: %s}",
176 ntohs(vid), ntohl(dir->d_did), s_path->u_name, fbitmap, dbitmap, getcwdpath());
179 if (!s_path->st_valid) {
180 /* it's a dir and it should be there
181 * because we chdir in it in cname or
182 * it's curdir (maybe deleted, but then we can't know).
183 * So we need to try harder.
185 of_statdir(vol, s_path);
187 if ( s_path->st_errno != 0 ) {
188 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 );
226 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
232 u_int16_t vid, bitmap;
237 memcpy( &vid, ibuf, sizeof(vid));
238 ibuf += sizeof( vid );
240 if (NULL == ( vol = getvolbyvid( vid )) ) {
241 return( AFPERR_PARAM );
244 if (vol->v_flags & AFPVOL_RO)
247 memcpy( &did, ibuf, sizeof( did));
248 ibuf += sizeof( did);
250 if (NULL == ( dir = dirlookup( vol, did )) ) {
254 memcpy( &bitmap, ibuf, sizeof( bitmap ));
255 bitmap = ntohs( bitmap );
256 ibuf += sizeof( bitmap );
258 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
259 return get_afp_errno(AFPERR_NOOBJ);
263 if (!path->st_valid) {
264 /* it's a dir and it should be there
265 * because we chdir in it in cname
267 of_statdir(vol, path);
270 if ( path->st_errno != 0 ) {
271 return( AFPERR_NOOBJ );
274 * If ibuf is odd, make it even.
276 if ((u_long)ibuf & 1 ) {
280 if (S_ISDIR(st->st_mode)) {
281 rc = setdirparams(vol, path, bitmap, ibuf );
283 rc = setfilparams(vol, path, bitmap, ibuf );
285 if ( rc == AFP_OK ) {
286 setvoltime(obj, vol );
292 /* --------------------------------------------
293 Factorise some checks on a pathname
295 int check_name(const struct vol *vol, char *name)
297 /* check for illegal characters in the unix filename */
298 if (!wincheck(vol, name))
301 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
304 if (!vol->vfs->vfs_validupath(vol, name)) {
305 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
309 /* check for vetoed filenames */
310 if (veto_file(vol->v_veto, name))
315 /* -------------------------
316 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,
329 struct stat *st, nst;
333 struct ofork *opened = NULL;
338 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
343 if ((p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding())) == NULL)
344 return AFPERR_PARAM; /* can't convert */
346 #ifndef HAVE_RENAMEAT
348 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
349 p = ctoupath( vol, sdir, oldname );
351 return AFPERR_PARAM; /* pathname too long */
352 #endif /* HAVE_RENAMEAT */
357 opened = of_findnameat(sdir_fd, &path);
359 opened = of_findname(&path);
360 #endif /* HAVE_RENAMEAT */
362 /* reuse struct adouble so it won't break locks */
366 id = sdir->d_did; /* we already have the CNID */
367 p = ctoupath( vol, sdir->d_parent, oldname );
371 adflags = ADFLAGS_DIR;
376 * p now points to either
377 * a) full pathname of the source fs object (if renameat is not available)
378 * b) the oldname (renameat is available)
379 * we are in the dest folder so we need to use
381 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
385 if ((cwd_fd = open(".", O_RDONLY)) == -1)
387 if (fchdir(sdir_fd) != 0)
390 if (!ad_metadata(p, adflags, adp)) {
393 ad_getattr(adp, &bshort);
394 ad_close_metadata( adp);
395 if ((bshort & htons(ATTRBIT_NORENAME)))
396 return(AFPERR_OLOCK);
399 if (fchdir(cwd_fd) != 0) {
400 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
405 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
410 if (0 != (rc = check_name(vol, upath))) {
414 /* source == destination. we just silently accept this. */
415 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
416 if (strcmp(oldname, newname) == 0)
419 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
420 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
421 /* not the same file */
426 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
431 path.st_errno = errno;
432 if (of_findname(&path)) {
433 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
435 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
437 of_rename(vol, opened, sdir, oldname, curdir, newname);
440 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
442 if ( rc == AFP_OK && id ) {
443 /* renaming may have moved the file/dir across a filesystem */
444 if (stat(upath, st) < 0)
447 /* fix up the catalog entry */
448 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
454 /* -------------------------------------------- */
455 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
459 char *oldname, *newname;
470 memcpy( &vid, ibuf, sizeof( vid ));
471 ibuf += sizeof( vid );
472 if (NULL == ( vol = getvolbyvid( vid )) ) {
473 return( AFPERR_PARAM );
476 if (vol->v_flags & AFPVOL_RO)
479 memcpy( &did, ibuf, sizeof( did ));
480 ibuf += sizeof( did );
481 if (NULL == ( sdir = dirlookup( vol, did )) ) {
485 /* source pathname */
486 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
487 return get_afp_errno(AFPERR_NOOBJ);
491 newname = obj->newtmp;
492 oldname = obj->oldtmp;
493 isdir = path_isadir(path);
494 if ( *path->m_name != '\0' ) {
495 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
497 /* curdir parent dir, need to move sdir back */
502 if ( sdir->d_parent == NULL ) { /* root directory */
503 return( AFPERR_NORENAME );
505 /* move to destination dir */
506 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
509 strcpy(oldname, sdir->d_m_name);
512 /* another place where we know about the path type */
513 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
514 return( AFPERR_PARAM );
518 return AFP_OK; /* newname == oldname same dir */
521 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
522 if ( rc == AFP_OK ) {
523 setvoltime(obj, vol );
529 /* ------------------------------- */
530 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
542 memcpy( &vid, ibuf, sizeof( vid ));
543 ibuf += sizeof( vid );
544 if (NULL == ( vol = getvolbyvid( vid )) ) {
545 return( AFPERR_PARAM );
548 if (vol->v_flags & AFPVOL_RO)
551 memcpy( &did, ibuf, sizeof( did ));
552 ibuf += sizeof( int );
553 if (NULL == ( dir = dirlookup( vol, did )) ) {
557 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
558 return get_afp_errno(AFPERR_NOOBJ);
561 upath = s_path->u_name;
562 if ( path_isadir( s_path) ) {
563 if (*s_path->m_name != '\0') {
569 /* we have to cache this, the structs are lost in deletcurdir*/
570 /* but we need the positive returncode to send our event */
572 strncpy(dname, curdir->d_u_name, 255 );
573 if ((rc = deletecurdir(vol)) == AFP_OK)
574 fce_register_delete_dir(dname);
576 } else if (of_findname(s_path)) {
579 /* it's a file st_valid should always be true
580 * only test for ENOENT because EACCES needs
581 * to read meta data in deletefile
583 if (s_path->st_valid && s_path->st_errno == ENOENT) {
586 if ((rc = deletefile(vol, -1, upath, 1)) === AFP_OK)
587 fce_register_delete_file( s_path );
590 if ( rc == AFP_OK ) {
592 setvoltime(obj, vol );
597 /* ------------------------ */
598 char *absupath(const struct vol *vol, struct dir *dir, char *u)
601 static char path[ MAXPATHLEN + 1];
608 p = path + sizeof( path ) - 1;
613 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
616 if (p -len -1 < path) {
618 rather rare so LOG error and/or client message ?
626 len = strlen( vol->v_path );
627 if (p -len -1 < path) {
632 memcpy( p, vol->v_path, len );
637 /* ------------------------
638 * FIXME dir could be NULL
640 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
642 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
645 /* ------------------------- */
646 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
649 struct dir *sdir, *ddir;
651 char *oldname, *newname;
660 #endif /* DROPKLUDGE */
667 memcpy( &vid, ibuf, sizeof( vid ));
668 ibuf += sizeof( vid );
669 if (NULL == ( vol = getvolbyvid( vid )) ) {
670 return( AFPERR_PARAM );
673 if (vol->v_flags & AFPVOL_RO)
676 /* source did followed by dest did */
677 memcpy( &did, ibuf, sizeof( did ));
678 ibuf += sizeof( int );
679 if (NULL == ( sdir = dirlookup( vol, did )) ) {
680 return afp_errno; /* was AFPERR_PARAM */
683 memcpy( &did, ibuf, sizeof( did ));
684 ibuf += sizeof( int );
686 /* source pathname */
687 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
688 return get_afp_errno(AFPERR_NOOBJ);
692 newname = obj->newtmp;
693 oldname = obj->oldtmp;
695 isdir = path_isadir(path);
696 if ( *path->m_name != '\0' ) {
700 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
702 strcpy(oldname, sdir->d_m_name);
706 if ((sdir_fd = open(".", O_RDONLY)) == -1)
710 /* get the destination directory */
711 if (NULL == ( ddir = dirlookup( vol, did )) ) {
712 rc = afp_errno; /* was AFPERR_PARAM */
715 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
719 pdid = curdir->d_did;
720 if ( *path->m_name != '\0' ) {
721 rc = path_error(path, AFPERR_NOOBJ);
725 /* one more place where we know about path type */
726 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
732 strcpy(newname, oldname);
735 /* This does the work */
736 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
738 if ( rc == AFP_OK ) {
739 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
748 if (vol->v_flags & AFPVOL_DROPBOX) {
749 /* FIXME did is not always the source id */
750 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
756 #endif /* DROPKLUDGE */
757 /* if unix priv don't try to match perm with dest folder */
758 if (!isdir && !vol_unix_priv(vol)) {
759 int admode = ad_mode("", 0777) | vol->v_fperm;
761 setfilmode(upath, admode, NULL, vol->v_umask);
762 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
764 setvoltime(obj, vol );
776 int veto_file(const char*veto_str, const char*path)
777 /* given a veto_str like "abc/zxc/" and path "abc", return 1
778 * veto_str should be '/' delimited
779 * if path matches any one of the veto_str elements exactly, then 1 is returned
780 * otherwise, 0 is returned.
783 int i; /* index to veto_str */
784 int j; /* index to path */
786 if ((veto_str == NULL) || (path == NULL))
789 for(i=0, j=0; veto_str[i] != '\0'; i++) {
790 if (veto_str[i] == '/') {
791 if ((j>0) && (path[j] == '\0')) {
792 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
797 if (veto_str[i] != path[j]) {
798 while ((veto_str[i] != '/')
799 && (veto_str[i] != '\0'))