2 * $Id: filedir.c,v 1.69.2.3 2010-02-04 14:34:31 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"
55 int matchfile2dirperms(
56 /* Since it's kinda' big; I decided against an
61 /* The below code changes the way file ownership is determined in the name of
62 fixing dropboxes. It has known security problem. See the netatalk FAQ for
71 LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
74 if (stat(upath, &st ) < 0) {
75 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
79 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
80 /* FIXME dirsearch doesn't move cwd to did ! */
81 if (( dir = dirlookup( vol, did )) == NULL ) {
82 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
85 else if (stat(".", &sb) < 0) {
86 LOG(log_error, logtype_afpd,
87 "matchfile2dirperms: Error checking directory \"%s\": %s",
88 dir->d_m_name, strerror(errno));
93 if ( uid != sb.st_uid )
96 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
98 LOG(log_error, logtype_afpd,
99 "matchfile2dirperms(%s): Error changing owner/gid: %s",
100 upath, strerror(errno));
103 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
105 LOG(log_error, logtype_afpd,
106 "matchfile2dirperms(%s): Error adding file read permissions: %s",
107 upath, strerror(errno));
110 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
112 LOG(log_error, logtype_afpd,
113 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
114 adpath, strerror(errno));
117 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
119 LOG(log_error, logtype_afpd,
120 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
121 adpath, strerror(errno));
126 } /* end else if stat success */
129 LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
135 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
143 u_int16_t fbitmap, dbitmap, vid;
149 memcpy( &vid, ibuf, sizeof( vid ));
150 ibuf += sizeof( vid );
151 if (NULL == ( vol = getvolbyvid( vid )) ) {
152 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
155 return( AFPERR_ACCESS );
158 memcpy( &did, ibuf, sizeof( did ));
159 ibuf += sizeof( did );
161 if (NULL == ( dir = dirlookup( vol, did )) ) {
165 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
166 fbitmap = ntohs( fbitmap );
167 ibuf += sizeof( fbitmap );
168 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
169 dbitmap = ntohs( dbitmap );
170 ibuf += sizeof( dbitmap );
172 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
173 return get_afp_errno(AFPERR_NOOBJ);
176 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
177 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
178 ntohl(curdir->d_did), cfrombstring(curdir->d_fullpath), s_path->u_name);
181 if (!s_path->st_valid) {
182 /* it's a dir and it should be there
183 * because we chdir in it in cname or
184 * it's curdir (maybe deleted, but then we can't know).
185 * So we need to try harder.
187 of_statdir(vol, s_path);
189 if ( s_path->st_errno != 0 ) {
190 if (afp_errno != AFPERR_ACCESS) {
191 return( AFPERR_NOOBJ );
197 if (S_ISDIR(st->st_mode)) {
203 ret = getdirparams(vol, dbitmap, s_path, dir,
204 rbuf + 3 * sizeof( u_int16_t ), &buflen );
208 /* this is a directory */
209 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
211 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
212 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
216 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
218 *rbuflen = buflen + 3 * sizeof( u_int16_t );
219 fbitmap = htons( fbitmap );
220 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
221 rbuf += sizeof( fbitmap );
222 dbitmap = htons( dbitmap );
223 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
224 rbuf += sizeof( dbitmap ) + sizeof( u_char );
230 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
236 u_int16_t vid, bitmap;
241 memcpy( &vid, ibuf, sizeof(vid));
242 ibuf += sizeof( vid );
244 if (NULL == ( vol = getvolbyvid( vid )) ) {
245 return( AFPERR_PARAM );
248 if (vol->v_flags & AFPVOL_RO)
251 memcpy( &did, ibuf, sizeof( did));
252 ibuf += sizeof( did);
254 if (NULL == ( dir = dirlookup( vol, did )) ) {
258 memcpy( &bitmap, ibuf, sizeof( bitmap ));
259 bitmap = ntohs( bitmap );
260 ibuf += sizeof( bitmap );
262 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
263 return get_afp_errno(AFPERR_NOOBJ);
267 if (!path->st_valid) {
268 /* it's a dir and it should be there
269 * because we chdir in it in cname
271 of_statdir(vol, path);
274 if ( path->st_errno != 0 ) {
275 if (afp_errno != AFPERR_ACCESS)
276 return( AFPERR_NOOBJ );
279 * If ibuf is odd, make it even.
281 if ((u_long)ibuf & 1 ) {
285 if (S_ISDIR(st->st_mode)) {
286 rc = setdirparams(vol, path, bitmap, ibuf );
288 rc = setfilparams(vol, path, bitmap, ibuf );
290 if ( rc == AFP_OK ) {
291 setvoltime(obj, vol );
297 /* --------------------------------------------
298 Factorise some checks on a pathname
300 int check_name(const struct vol *vol, char *name)
302 /* check for illegal characters in the unix filename */
303 if (!wincheck(vol, name))
306 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
309 if (!vol->vfs->vfs_validupath(vol, name)) {
310 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
314 /* check for vetoed filenames */
315 if (veto_file(vol->v_veto, name))
320 /* -------------------------
321 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, struct dir *sdir, char *oldname, char *newname, int isdir)
330 struct stat *st, nst;
334 struct ofork *opened = NULL;
338 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
343 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
345 return AFPERR_PARAM; /* can't convert */
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 */
354 if ((opened = of_findname(&path))) {
355 /* reuse struct adouble so it won't break locks */
360 id = sdir->d_did; /* we already have the CNID */
361 p = ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname );
365 adflags = ADFLAGS_DIR;
368 * p now points to the full pathname of the source fs object.
370 * we are in the dest folder so we need to use p for ad_open
373 if (!ad_metadata(p, adflags, adp)) {
376 ad_getattr(adp, &bshort);
377 ad_close_metadata( adp);
378 if ((bshort & htons(ATTRBIT_NORENAME)))
379 return(AFPERR_OLOCK);
382 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
387 if (0 != (rc = check_name(vol, upath))) {
391 /* source == destination. we just silently accept this. */
392 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
393 if (strcmp(oldname, newname) == 0)
396 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
397 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
398 /* not the same file */
403 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
408 path.st_errno = errno;
409 if (of_findname(&path)) {
410 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
412 rc = renamefile(vol, p, upath, newname, adp );
414 of_rename(vol, opened, sdir, oldname, curdir, newname);
417 rc = renamedir(vol, p, upath, sdir, curdir, newname);
419 if ( rc == AFP_OK && id ) {
420 /* renaming may have moved the file/dir across a filesystem */
421 if (stat(upath, st) < 0)
424 /* fix up the catalog entry */
425 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
431 /* -------------------------------------------- */
432 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
436 char *oldname, *newname;
447 memcpy( &vid, ibuf, sizeof( vid ));
448 ibuf += sizeof( vid );
449 if (NULL == ( vol = getvolbyvid( vid )) ) {
450 return( AFPERR_PARAM );
453 if (vol->v_flags & AFPVOL_RO)
456 memcpy( &did, ibuf, sizeof( did ));
457 ibuf += sizeof( did );
458 if (NULL == ( sdir = dirlookup( vol, did )) ) {
462 /* source pathname */
463 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
464 return get_afp_errno(AFPERR_NOOBJ);
468 newname = obj->newtmp;
469 oldname = obj->oldtmp;
470 isdir = path_isadir(path);
471 if ( *path->m_name != '\0' ) {
472 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
474 /* curdir parent dir, need to move sdir back */
479 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
480 return( AFPERR_NORENAME );
482 /* move to destination dir */
483 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
486 memcpy(oldname, cfrombstring(sdir->d_m_name), blength(sdir->d_m_name) +1);
489 /* another place where we know about the path type */
490 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
491 return( AFPERR_PARAM );
495 return AFP_OK; /* newname == oldname same dir */
498 rc = moveandrename(vol, sdir, oldname, newname, isdir);
500 if ( rc == AFP_OK ) {
501 setvoltime(obj, vol );
507 /* ------------------------------- */
508 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
520 memcpy( &vid, ibuf, sizeof( vid ));
521 ibuf += sizeof( vid );
522 if (NULL == ( vol = getvolbyvid( vid )) ) {
523 return( AFPERR_PARAM );
526 if (vol->v_flags & AFPVOL_RO)
529 memcpy( &did, ibuf, sizeof( did ));
530 ibuf += sizeof( int );
532 if (NULL == ( dir = dirlookup( vol, did )) ) {
536 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
537 return get_afp_errno(AFPERR_NOOBJ);
540 upath = s_path->u_name;
541 if ( path_isadir( s_path) ) {
542 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
545 rc = deletecurdir( vol);
546 } else if (of_findname(s_path)) {
549 /* it's a file st_valid should always be true
550 * only test for ENOENT because EACCES needs
551 * to read meta data in deletefile
553 if (s_path->st_valid && s_path->st_errno == ENOENT) {
557 rc = deletefile(vol, upath, 1);
560 if ( rc == AFP_OK ) {
562 setvoltime(obj, vol );
567 /* ------------------------ */
568 char *absupath(const struct vol *vol, struct dir *dir, char *u)
570 static char pathbuf[MAXPATHLEN + 1];
573 if (u == NULL || dir == NULL || vol == NULL)
576 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
578 if (bcatcstr(path, "/") != BSTR_OK)
580 if (bcatcstr(path, u) != BSTR_OK)
582 if (path->slen > MAXPATHLEN)
585 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstring(path));
587 strncpy(pathbuf, cfrombstring(path), blength(path) + 1);
593 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
595 if (vol == NULL || dir == NULL || name == NULL)
597 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
600 /* ------------------------- */
601 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
604 struct dir *sdir, *ddir;
606 char *oldname, *newname;
615 #endif /* DROPKLUDGE */
620 memcpy( &vid, ibuf, sizeof( vid ));
621 ibuf += sizeof( vid );
622 if (NULL == ( vol = getvolbyvid( vid )) ) {
623 return( AFPERR_PARAM );
626 if (vol->v_flags & AFPVOL_RO)
629 /* source did followed by dest did */
630 memcpy( &did, ibuf, sizeof( did ));
631 ibuf += sizeof( int );
632 if (NULL == ( sdir = dirlookup( vol, did )) ) {
633 return afp_errno; /* was AFPERR_PARAM */
636 memcpy( &did, ibuf, sizeof( did ));
637 ibuf += sizeof( int );
639 /* source pathname */
640 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
641 return get_afp_errno(AFPERR_NOOBJ);
645 newname = obj->newtmp;
646 oldname = obj->oldtmp;
648 isdir = path_isadir(path);
649 if ( *path->m_name != '\0' ) {
653 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
655 memcpy(oldname, cfrombstring(sdir->d_m_name), blength(sdir->d_m_name) + 1);
658 /* get the destination directory */
659 if (NULL == ( ddir = dirlookup( vol, did )) ) {
660 return afp_errno; /* was AFPERR_PARAM */
662 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
663 return( AFPERR_NOOBJ );
665 pdid = curdir->d_did;
666 if ( *path->m_name != '\0' ) {
667 return path_error(path, AFPERR_NOOBJ);
670 /* one more place where we know about path type */
671 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
672 return( AFPERR_PARAM );
676 strcpy(newname, oldname);
679 LOG(log_debug, logtype_afpd, "afp_move: {oldname:'%s', newname:'%s', isdir:%u}",
680 oldname, newname, isdir);
681 rc = moveandrename(vol, sdir, oldname, newname, isdir);
683 if ( rc == AFP_OK ) {
684 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
692 if (vol->v_flags & AFPVOL_DROPBOX) {
693 /* FIXME did is not always the source id */
694 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
699 #endif /* DROPKLUDGE */
700 /* if unix priv don't try to match perm with dest folder */
701 if (!isdir && !vol_unix_priv(vol)) {
702 int admode = ad_mode("", 0777) | vol->v_fperm;
704 setfilmode(upath, admode, NULL, vol->v_umask);
705 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
707 setvoltime(obj, vol );
713 int veto_file(const char*veto_str, const char*path)
714 /* given a veto_str like "abc/zxc/" and path "abc", return 1
715 * veto_str should be '/' delimited
716 * if path matches any one of the veto_str elements exactly, then 1 is returned
717 * otherwise, 0 is returned.
720 int i; /* index to veto_str */
721 int j; /* index to path */
723 if ((veto_str == NULL) || (path == NULL))
726 for(i=0, j=0; veto_str[i] != '\0'; i++) {
727 if (veto_str[i] == '/') {
728 if ((j>0) && (path[j] == '\0')) {
729 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
734 if (veto_str[i] != path[j]) {
735 while ((veto_str[i] != '/')
736 && (veto_str[i] != '\0'))