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>
42 #include <atalk/bstrlib.h>
43 #include <atalk/bstradd.h>
45 #include "directory.h"
56 int matchfile2dirperms(
57 /* Since it's kinda' big; I decided against an
62 /* The below code changes the way file ownership is determined in the name of
63 fixing dropboxes. It has known security problem. See the netatalk FAQ for
72 LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
75 if (stat(upath, &st ) < 0) {
76 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
80 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
81 /* FIXME dirsearch doesn't move cwd to did ! */
82 if (( dir = dirlookup( vol, did )) == NULL ) {
83 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
86 else if (stat(".", &sb) < 0) {
87 LOG(log_error, logtype_afpd,
88 "matchfile2dirperms: Error checking directory \"%s\": %s",
89 dir->d_m_name, strerror(errno));
94 if ( uid != sb.st_uid )
97 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
99 LOG(log_error, logtype_afpd,
100 "matchfile2dirperms(%s): Error changing owner/gid: %s",
101 upath, strerror(errno));
104 else if ((!S_ISLNK(st->st_mode)) && (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0))
106 LOG(log_error, logtype_afpd,
107 "matchfile2dirperms(%s): Error adding file read permissions: %s",
108 upath, strerror(errno));
111 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
113 LOG(log_error, logtype_afpd,
114 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
115 adpath, strerror(errno));
118 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
120 LOG(log_error, logtype_afpd,
121 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
122 adpath, strerror(errno));
127 } /* end else if stat success */
130 LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
136 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
144 u_int16_t fbitmap, dbitmap, vid;
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);
177 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
178 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
179 ntohl(curdir->d_did), cfrombstr(curdir->d_fullpath), s_path->u_name);
182 if (!s_path->st_valid) {
183 /* it's a dir and it should be there
184 * because we chdir in it in cname or
185 * it's curdir (maybe deleted, but then we can't know).
186 * So we need to try harder.
188 of_statdir(vol, s_path);
190 if ( s_path->st_errno != 0 ) {
191 if (afp_errno != AFPERR_ACCESS) {
192 return( AFPERR_NOOBJ );
198 if (S_ISDIR(st->st_mode)) {
204 ret = getdirparams(vol, dbitmap, s_path, dir,
205 rbuf + 3 * sizeof( u_int16_t ), &buflen );
209 /* this is a directory */
210 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
212 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
213 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
217 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
219 *rbuflen = buflen + 3 * sizeof( u_int16_t );
220 fbitmap = htons( fbitmap );
221 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
222 rbuf += sizeof( fbitmap );
223 dbitmap = htons( dbitmap );
224 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
225 rbuf += sizeof( dbitmap ) + sizeof( u_char );
231 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
237 u_int16_t vid, bitmap;
242 memcpy( &vid, ibuf, sizeof(vid));
243 ibuf += sizeof( vid );
245 if (NULL == ( vol = getvolbyvid( vid )) ) {
246 return( AFPERR_PARAM );
249 if (vol->v_flags & AFPVOL_RO)
252 memcpy( &did, ibuf, sizeof( did));
253 ibuf += sizeof( did);
255 if (NULL == ( dir = dirlookup( vol, did )) ) {
259 memcpy( &bitmap, ibuf, sizeof( bitmap ));
260 bitmap = ntohs( bitmap );
261 ibuf += sizeof( bitmap );
263 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
264 return get_afp_errno(AFPERR_NOOBJ);
268 if (!path->st_valid) {
269 /* it's a dir and it should be there
270 * because we chdir in it in cname
272 of_statdir(vol, path);
275 if ( path->st_errno != 0 ) {
276 if (afp_errno != AFPERR_ACCESS)
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 );
298 /* --------------------------------------------
299 Factorise some checks on a pathname
301 int check_name(const struct vol *vol, char *name)
303 /* check for illegal characters in the unix filename */
304 if (!wincheck(vol, name))
307 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
310 if (!vol->vfs->vfs_validupath(vol, name)) {
311 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
315 /* check for vetoed filenames */
316 if (veto_file(vol->v_veto, name))
321 /* -------------------------
322 move and rename sdir:oldname to curdir:newname in volume vol
323 special care is needed for lock
325 static int moveandrename(const struct vol *vol,
335 struct stat *st, nst;
339 struct ofork *opened = NULL;
344 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
349 if ((p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding())) == NULL)
350 return AFPERR_PARAM; /* can't convert */
352 #ifndef HAVE_RENAMEAT
354 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
355 p = ctoupath( vol, sdir, oldname );
357 return AFPERR_PARAM; /* pathname too long */
358 #endif /* HAVE_RENAMEAT */
363 opened = of_findnameat(sdir_fd, &path);
365 opened = of_findname(&path);
366 #endif /* HAVE_RENAMEAT */
368 /* reuse struct adouble so it won't break locks */
372 id = sdir->d_did; /* we already have the CNID */
373 p = ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname );
377 adflags = ADFLAGS_DIR;
381 * p now points to either
382 * a) full pathname of the source fs object (if renameat is not available)
383 * b) the oldname (renameat is available)
384 * we are in the dest folder so we need to use
386 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
390 if ((cwd_fd = open(".", O_RDONLY)) == -1)
392 if (fchdir(sdir_fd) != 0)
395 if (!ad_metadata(p, adflags, adp)) {
398 ad_getattr(adp, &bshort);
399 ad_close_metadata( adp);
400 if ((bshort & htons(ATTRBIT_NORENAME)))
401 return(AFPERR_OLOCK);
404 if (fchdir(cwd_fd) != 0) {
405 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
410 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
415 if (0 != (rc = check_name(vol, upath))) {
419 /* source == destination. we just silently accept this. */
420 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
421 if (strcmp(oldname, newname) == 0)
424 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
425 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
426 /* not the same file */
431 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
436 path.st_errno = errno;
437 if (of_findname(&path)) {
438 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
440 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
442 of_rename(vol, opened, sdir, oldname, curdir, newname);
445 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
447 if ( rc == AFP_OK && id ) {
448 /* renaming may have moved the file/dir across a filesystem */
449 if (stat(upath, st) < 0)
458 S_ISDIR(st->st_mode) ? curdir->d_fullpath : NULL) != 0) {
459 LOG(log_error, logtype_afpd, "moveandrename: dir_modify error: %s -> %s",
464 /* fix up the catalog entry */
465 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
471 /* -------------------------------------------- */
472 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
476 char *oldname, *newname;
487 memcpy( &vid, ibuf, sizeof( vid ));
488 ibuf += sizeof( vid );
489 if (NULL == ( vol = getvolbyvid( vid )) ) {
490 return( AFPERR_PARAM );
493 if (vol->v_flags & AFPVOL_RO)
496 memcpy( &did, ibuf, sizeof( did ));
497 ibuf += sizeof( did );
498 if (NULL == ( sdir = dirlookup( vol, did )) ) {
502 /* source pathname */
503 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
504 return get_afp_errno(AFPERR_NOOBJ);
508 newname = obj->newtmp;
509 oldname = obj->oldtmp;
510 isdir = path_isadir(path);
511 if ( *path->m_name != '\0' ) {
512 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
514 /* curdir parent dir, need to move sdir back */
519 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
520 return( AFPERR_NORENAME );
522 /* move to destination dir */
523 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
526 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
529 /* another place where we know about the path type */
530 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
531 return( AFPERR_PARAM );
535 return AFP_OK; /* newname == oldname same dir */
538 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
539 if ( rc == AFP_OK ) {
540 setvoltime(obj, vol );
546 /* ------------------------------- */
547 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
559 memcpy( &vid, ibuf, sizeof( vid ));
560 ibuf += sizeof( vid );
561 if (NULL == ( vol = getvolbyvid( vid )) ) {
562 return( AFPERR_PARAM );
565 if (vol->v_flags & AFPVOL_RO)
568 memcpy( &did, ibuf, sizeof( did ));
569 ibuf += sizeof( int );
571 if (NULL == ( dir = dirlookup( vol, did )) ) {
575 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
576 return get_afp_errno(AFPERR_NOOBJ);
579 upath = s_path->u_name;
580 if ( path_isadir( s_path) ) {
581 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
584 rc = deletecurdir( vol);
585 } else if (of_findname(s_path)) {
588 /* it's a file st_valid should always be true
589 * only test for ENOENT because EACCES needs
590 * to read meta data in deletefile
592 if (s_path->st_valid && s_path->st_errno == ENOENT) {
596 rc = deletefile(vol, -1, upath, 1);
598 struct dir *cachedfile;
599 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
600 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
601 dir_free(cachedfile);
605 if ( rc == AFP_OK ) {
607 setvoltime(obj, vol );
612 /* ------------------------ */
613 char *absupath(const struct vol *vol, struct dir *dir, char *u)
615 static char pathbuf[MAXPATHLEN + 1];
618 if (u == NULL || dir == NULL || vol == NULL)
621 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
623 if (bcatcstr(path, "/") != BSTR_OK)
625 if (bcatcstr(path, u) != BSTR_OK)
627 if (path->slen > MAXPATHLEN)
630 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
632 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
638 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
640 if (vol == NULL || dir == NULL || name == NULL)
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 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
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 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
737 oldname, newname, isdir);
738 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
740 if ( rc == AFP_OK ) {
741 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
750 if (vol->v_flags & AFPVOL_DROPBOX) {
751 /* FIXME did is not always the source id */
752 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
758 #endif /* DROPKLUDGE */
759 /* if unix priv don't try to match perm with dest folder */
760 if (!isdir && !vol_unix_priv(vol)) {
761 int admode = ad_mode("", 0777) | vol->v_fperm;
763 setfilmode(upath, admode, NULL, vol->v_umask);
764 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
766 setvoltime(obj, vol );
778 int veto_file(const char*veto_str, const char*path)
779 /* given a veto_str like "abc/zxc/" and path "abc", return 1
780 * veto_str should be '/' delimited
781 * if path matches any one of the veto_str elements exactly, then 1 is returned
782 * otherwise, 0 is returned.
785 int i; /* index to veto_str */
786 int j; /* index to path */
788 if ((veto_str == NULL) || (path == NULL))
791 for(i=0, j=0; veto_str[i] != '\0'; i++) {
792 if (veto_str[i] == '/') {
793 if ((j>0) && (path[j] == '\0')) {
794 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
799 if (veto_str[i] != path[j]) {
800 while ((veto_str[i] != '/')
801 && (veto_str[i] != '\0'))