2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #else /* STDC_HEADERS */
19 #endif /* HAVE_STRCHR */
20 char *strchr (), *strrchr ();
22 #define memcpy(d,s,n) bcopy ((s), (d), (n))
23 #define memmove(d,s,n) bcopy ((s), (d), (n))
24 #endif /* ! HAVE_MEMCPY */
25 #endif /* STDC_HEADERS */
31 #include <sys/param.h>
33 #include <atalk/adouble.h>
34 #include <atalk/vfs.h>
35 #include <atalk/afp.h>
36 #include <atalk/util.h>
37 #include <atalk/cnid.h>
38 #include <atalk/logger.h>
39 #include <atalk/unix.h>
40 #include <atalk/bstrlib.h>
41 #include <atalk/bstradd.h>
42 #include <atalk/acl.h>
43 #include <atalk/globals.h>
44 #include <atalk/fce_api.h>
46 #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 (ochown(upath, sb.st_uid, sb.st_gid, vol_syml_opt(vol)) < 0)
99 LOG(log_error, logtype_afpd,
100 "matchfile2dirperms(%s): Error changing owner/gid: %s",
101 upath, strerror(errno));
104 else if (ochmod(upath,
105 (st.st_mode & ~default_options.umask) | S_IRGRP | S_IROTH,
107 vol_syml_opt(vol) | O_NETATALK_ACL) < 0) {
108 LOG(log_error, logtype_afpd,
109 "matchfile2dirperms(%s): Error adding file read permissions: %s",
110 upath, strerror(errno));
113 else if (ochown(adpath, sb.st_uid, sb.st_gid, vol_syml_opt(vol)) < 0)
115 LOG(log_error, logtype_afpd,
116 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
117 adpath, strerror(errno));
120 else if (ochmod(adpath,
121 (st.st_mode & ~default_options.umask) | S_IRGRP| S_IROTH,
123 vol_syml_opt(vol) | O_NETATALK_ACL) < 0)
125 LOG(log_error, logtype_afpd,
126 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
127 adpath, strerror(errno));
132 } /* end else if stat success */
135 LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
141 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
149 u_int16_t fbitmap, dbitmap, vid;
155 memcpy( &vid, ibuf, sizeof( vid ));
156 ibuf += sizeof( vid );
157 if (NULL == ( vol = getvolbyvid( vid )) ) {
158 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
161 return( AFPERR_ACCESS );
164 memcpy( &did, ibuf, sizeof( did ));
165 ibuf += sizeof( did );
167 if (NULL == ( dir = dirlookup( vol, did )) ) {
171 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
172 fbitmap = ntohs( fbitmap );
173 ibuf += sizeof( fbitmap );
174 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
175 dbitmap = ntohs( dbitmap );
176 ibuf += sizeof( dbitmap );
178 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
179 return get_afp_errno(AFPERR_NOOBJ);
182 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
183 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
184 ntohl(curdir->d_did), cfrombstr(curdir->d_fullpath), s_path->u_name);
187 if (!s_path->st_valid) {
188 /* it's a dir and it should be there
189 * because we chdir in it in cname or
190 * it's curdir (maybe deleted, but then we can't know).
191 * So we need to try harder.
193 of_statdir(vol, s_path);
195 if ( s_path->st_errno != 0 ) {
196 if (afp_errno != AFPERR_ACCESS) {
197 return( AFPERR_NOOBJ );
203 if (S_ISDIR(st->st_mode)) {
209 ret = getdirparams(vol, dbitmap, s_path, dir,
210 rbuf + 3 * sizeof( u_int16_t ), &buflen );
214 /* this is a directory */
215 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
217 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
218 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
222 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
224 *rbuflen = buflen + 3 * sizeof( u_int16_t );
225 fbitmap = htons( fbitmap );
226 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
227 rbuf += sizeof( fbitmap );
228 dbitmap = htons( dbitmap );
229 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
230 rbuf += sizeof( dbitmap ) + sizeof( u_char );
236 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
242 u_int16_t vid, bitmap;
247 memcpy( &vid, ibuf, sizeof(vid));
248 ibuf += sizeof( vid );
250 if (NULL == ( vol = getvolbyvid( vid )) ) {
251 return( AFPERR_PARAM );
254 if (vol->v_flags & AFPVOL_RO)
257 memcpy( &did, ibuf, sizeof( did));
258 ibuf += sizeof( did);
260 if (NULL == ( dir = dirlookup( vol, did )) ) {
264 memcpy( &bitmap, ibuf, sizeof( bitmap ));
265 bitmap = ntohs( bitmap );
266 ibuf += sizeof( bitmap );
268 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
269 return get_afp_errno(AFPERR_NOOBJ);
273 if (!path->st_valid) {
274 /* it's a dir and it should be there
275 * because we chdir in it in cname
277 of_statdir(vol, path);
280 if ( path->st_errno != 0 ) {
281 if (afp_errno != AFPERR_ACCESS)
282 return( AFPERR_NOOBJ );
285 * If ibuf is odd, make it even.
287 if ((u_long)ibuf & 1 ) {
291 if (S_ISDIR(st->st_mode)) {
292 rc = setdirparams(vol, path, bitmap, ibuf );
294 rc = setfilparams(vol, path, bitmap, ibuf );
296 if ( rc == AFP_OK ) {
297 setvoltime(obj, vol );
303 /* --------------------------------------------
304 Factorise some checks on a pathname
306 int check_name(const struct vol *vol, char *name)
308 /* check for illegal characters in the unix filename */
309 if (!wincheck(vol, name))
312 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
315 if (!vol->vfs->vfs_validupath(vol, name)) {
316 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
320 /* check for vetoed filenames */
321 if (veto_file(vol->v_veto, name))
326 /* -------------------------
327 move and rename sdir:oldname to curdir:newname in volume vol
328 special care is needed for lock
330 static int moveandrename(const struct vol *vol,
337 char *oldunixname = NULL;
340 struct stat *st, nst;
344 struct ofork *opened = NULL;
349 LOG(log_debug, logtype_afpd,
350 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
351 cfrombstr(sdir->d_u_name), oldname, newname);
353 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
358 if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding()))) == NULL)
359 return AFPERR_PARAM; /* can't convert */
360 id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
365 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
366 return AFPERR_PARAM; /* pathname too long */
367 #endif /* HAVE_ATFUNCS */
370 path.u_name = oldunixname;
373 opened = of_findnameat(sdir_fd, &path);
375 opened = of_findname(&path);
376 #endif /* HAVE_ATFUNCS */
379 /* reuse struct adouble so it won't break locks */
383 id = sdir->d_did; /* we already have the CNID */
384 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
386 adflags = ADFLAGS_DIR;
390 * oldunixname now points to either
391 * a) full pathname of the source fs object (if renameat is not available)
392 * b) the oldname (renameat is available)
393 * we are in the dest folder so we need to use
394 * a) oldunixname for ad_open
395 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
399 if ((cwd_fd = open(".", O_RDONLY)) == -1)
401 if (fchdir(sdir_fd) != 0) {
406 if (!ad_metadata(oldunixname, adflags, adp)) {
409 ad_getattr(adp, &bshort);
410 ad_close_metadata( adp);
411 if ((bshort & htons(ATTRBIT_NORENAME))) {
417 if (fchdir(cwd_fd) != 0) {
418 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
424 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
430 if (0 != (rc = check_name(vol, upath))) {
434 /* source == destination. we just silently accept this. */
435 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
436 if (strcmp(oldname, newname) == 0) {
441 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
442 if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
443 /* not the same file */
449 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
456 path.st_errno = errno;
457 if (of_findname(vol, &path)) {
458 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
460 rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
462 of_rename(vol, opened, sdir, oldname, curdir, newname);
465 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
467 if ( rc == AFP_OK && id ) {
468 /* renaming may have moved the file/dir across a filesystem */
469 if (stat(upath, st) < 0) {
474 /* Remove it from the cache */
475 struct dir *cacheddir = dircache_search_by_did(vol, id);
477 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
478 (void)dir_remove(vol, cacheddir);
481 /* Fixup adouble info */
482 if (!ad_metadata(upath, adflags, adp)) {
483 ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
485 ad_close_metadata(adp);
488 /* fix up the catalog entry */
489 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
500 /* -------------------------------------------- */
501 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
505 char *oldname, *newname;
516 memcpy( &vid, ibuf, sizeof( vid ));
517 ibuf += sizeof( vid );
518 if (NULL == ( vol = getvolbyvid( vid )) ) {
519 return( AFPERR_PARAM );
522 if (vol->v_flags & AFPVOL_RO)
525 memcpy( &did, ibuf, sizeof( did ));
526 ibuf += sizeof( did );
527 if (NULL == ( sdir = dirlookup( vol, did )) ) {
531 /* source pathname */
532 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
533 return get_afp_errno(AFPERR_NOOBJ);
537 newname = obj->newtmp;
538 oldname = obj->oldtmp;
539 isdir = path_isadir(path);
540 if ( *path->m_name != '\0' ) {
541 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
543 /* curdir parent dir, need to move sdir back */
548 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
549 return( AFPERR_NORENAME );
551 /* move to destination dir */
552 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
555 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
558 /* another place where we know about the path type */
559 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
560 return( AFPERR_PARAM );
564 return AFP_OK; /* newname == oldname same dir */
567 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
568 if ( rc == AFP_OK ) {
569 setvoltime(obj, vol );
575 /* ------------------------------- */
576 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
588 memcpy( &vid, ibuf, sizeof( vid ));
589 ibuf += sizeof( vid );
590 if (NULL == ( vol = getvolbyvid( vid )) ) {
591 return( AFPERR_PARAM );
594 if (vol->v_flags & AFPVOL_RO)
597 memcpy( &did, ibuf, sizeof( did ));
598 ibuf += sizeof( int );
600 if (NULL == ( dir = dirlookup( vol, did )) ) {
604 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
605 return get_afp_errno(AFPERR_NOOBJ);
608 upath = s_path->u_name;
609 if ( path_isadir( s_path) ) {
610 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT) {
613 /* we have to cache this, the structs are lost in deletcurdir*/
614 /* but we need the positive returncode to send our event */
616 if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
618 if ((rc = deletecurdir(vol)) == AFP_OK)
619 fce_register_delete_dir(cfrombstr(dname));
622 } else if (of_findname(vol, s_path)) {
625 /* it's a file st_valid should always be true
626 * only test for ENOENT because EACCES needs
627 * to read meta data in deletefile
629 if (s_path->st_valid && s_path->st_errno == ENOENT) {
632 if ((rc = deletefile(vol, -1, upath, 1)) == AFP_OK) {
633 fce_register_delete_file( s_path );
634 if (vol->v_tm_used < s_path->st.st_size)
637 vol->v_tm_used -= s_path->st.st_size;
639 struct dir *cachedfile;
640 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
641 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
642 dir_free(cachedfile);
646 if ( rc == AFP_OK ) {
648 setvoltime(obj, vol );
653 /* ------------------------ */
654 char *absupath(const struct vol *vol, struct dir *dir, char *u)
656 static char pathbuf[MAXPATHLEN + 1];
659 if (u == NULL || dir == NULL || vol == NULL)
662 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
664 if (bcatcstr(path, "/") != BSTR_OK)
666 if (bcatcstr(path, u) != BSTR_OK)
668 if (path->slen > MAXPATHLEN)
671 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
673 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
679 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
681 if (vol == NULL || dir == NULL || name == NULL)
683 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
686 /* ------------------------- */
687 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
690 struct dir *sdir, *ddir;
692 char *oldname, *newname;
701 #endif /* DROPKLUDGE */
708 memcpy( &vid, ibuf, sizeof( vid ));
709 ibuf += sizeof( vid );
710 if (NULL == ( vol = getvolbyvid( vid )) ) {
711 return( AFPERR_PARAM );
714 if (vol->v_flags & AFPVOL_RO)
717 /* source did followed by dest did */
718 memcpy( &did, ibuf, sizeof( did ));
719 ibuf += sizeof( int );
720 if (NULL == ( sdir = dirlookup( vol, did )) ) {
721 return afp_errno; /* was AFPERR_PARAM */
724 memcpy( &did, ibuf, sizeof( did ));
725 ibuf += sizeof( int );
727 /* source pathname */
728 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
729 return get_afp_errno(AFPERR_NOOBJ);
733 newname = obj->newtmp;
734 oldname = obj->oldtmp;
736 isdir = path_isadir(path);
737 if ( *path->m_name != '\0' ) {
741 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
743 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
747 if ((sdir_fd = open(".", O_RDONLY)) == -1)
751 /* get the destination directory */
752 if (NULL == ( ddir = dirlookup( vol, did )) ) {
753 rc = afp_errno; /* was AFPERR_PARAM */
756 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
760 pdid = curdir->d_did;
761 if ( *path->m_name != '\0' ) {
762 rc = path_error(path, AFPERR_NOOBJ);
766 /* one more place where we know about path type */
767 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
773 strcpy(newname, oldname);
776 /* This does the work */
777 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
778 oldname, newname, isdir);
779 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
781 if ( rc == AFP_OK ) {
782 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
791 if (vol->v_flags & AFPVOL_DROPBOX) {
792 /* FIXME did is not always the source id */
793 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
799 #endif /* DROPKLUDGE */
800 /* if unix priv don't try to match perm with dest folder */
801 if (!isdir && !vol_unix_priv(vol)) {
802 int admode = ad_mode("", 0777) | vol->v_fperm;
804 setfilmode(vol, upath, admode, path->st_valid ? &path->st : NULL);
805 vol->vfs->vfs_setfilmode(vol, upath, admode, path->st_valid ? &path->st : NULL);
807 setvoltime(obj, vol );
819 int veto_file(const char*veto_str, const char*path)
820 /* given a veto_str like "abc/zxc/" and path "abc", return 1
821 * veto_str should be '/' delimited
822 * if path matches any one of the veto_str elements exactly, then 1 is returned
823 * otherwise, 0 is returned.
826 int i; /* index to veto_str */
827 int j; /* index to path */
829 if ((veto_str == NULL) || (path == NULL))
832 for(i=0, j=0; veto_str[i] != '\0'; i++) {
833 if (veto_str[i] == '/') {
834 if ((j>0) && (path[j] == '\0')) {
835 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
840 if (veto_str[i] != path[j]) {
841 while ((veto_str[i] != '/')
842 && (veto_str[i] != '\0'))