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>
44 #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 ((!S_ISLNK(st->st_mode)) && (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), cfrombstr(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
322 special care is needed for lock
324 static int moveandrename(const struct vol *vol,
331 char *oldunixname = NULL;
334 struct stat *st, nst;
338 struct ofork *opened = NULL;
343 LOG(log_debug, logtype_afpd,
344 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
345 cfrombstr(sdir->d_u_name), oldname, newname);
347 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
352 if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding()))) == NULL)
353 return AFPERR_PARAM; /* can't convert */
357 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
358 p = ctoupath( vol, sdir, oldname );
360 return AFPERR_PARAM; /* pathname too long */
361 #endif /* HAVE_ATFUNCS */
364 path.u_name = oldunixname;
366 opened = of_findnameat(sdir_fd, &path);
368 opened = of_findname(&path);
369 #endif /* HAVE_ATFUNCS */
371 /* reuse struct adouble so it won't break locks */
375 id = sdir->d_did; /* we already have the CNID */
376 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
378 adflags = ADFLAGS_DIR;
381 LOG(log_debug, logtype_afpd, "oldunixname: \"%s\"", oldunixname);
384 * oldunixname now points to either
385 * a) full pathname of the source fs object (if renameat is not available)
386 * b) the oldname (renameat is available)
387 * we are in the dest folder so we need to use
388 * a) oldunixname for ad_open
389 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
393 if ((cwd_fd = open(".", O_RDONLY)) == -1)
395 if (fchdir(sdir_fd) != 0) {
400 if (!ad_metadata(oldunixname, adflags, adp)) {
403 ad_getattr(adp, &bshort);
404 ad_close_metadata( adp);
405 if ((bshort & htons(ATTRBIT_NORENAME))) {
411 if (fchdir(cwd_fd) != 0) {
412 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
418 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
424 if (0 != (rc = check_name(vol, upath))) {
428 /* source == destination. we just silently accept this. */
429 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
430 if (strcmp(oldname, newname) == 0) {
435 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
436 if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
437 /* not the same file */
443 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
448 LOG(log_debug, logtype_afpd, "oldunixname: \"%s\"", oldunixname);
452 path.st_errno = errno;
453 if (of_findname(&path)) {
454 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
456 rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
458 of_rename(vol, opened, sdir, oldname, curdir, newname);
461 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
463 if ( rc == AFP_OK && id ) {
464 /* renaming may have moved the file/dir across a filesystem */
465 if (stat(upath, st) < 0) {
470 /* Remove it from the cache */
471 struct dir *cacheddir = dircache_search_by_did(vol, id);
473 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
474 (void)dir_remove(vol, cacheddir);
477 /* fix up the catalog entry */
478 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
489 /* -------------------------------------------- */
490 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
494 char *oldname, *newname;
505 memcpy( &vid, ibuf, sizeof( vid ));
506 ibuf += sizeof( vid );
507 if (NULL == ( vol = getvolbyvid( vid )) ) {
508 return( AFPERR_PARAM );
511 if (vol->v_flags & AFPVOL_RO)
514 memcpy( &did, ibuf, sizeof( did ));
515 ibuf += sizeof( did );
516 if (NULL == ( sdir = dirlookup( vol, did )) ) {
520 /* source pathname */
521 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
522 return get_afp_errno(AFPERR_NOOBJ);
526 newname = obj->newtmp;
527 oldname = obj->oldtmp;
528 isdir = path_isadir(path);
529 if ( *path->m_name != '\0' ) {
530 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
532 /* curdir parent dir, need to move sdir back */
537 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
538 return( AFPERR_NORENAME );
540 /* move to destination dir */
541 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
544 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
547 /* another place where we know about the path type */
548 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
549 return( AFPERR_PARAM );
553 return AFP_OK; /* newname == oldname same dir */
556 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
557 if ( rc == AFP_OK ) {
558 setvoltime(obj, vol );
564 /* ------------------------------- */
565 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
577 memcpy( &vid, ibuf, sizeof( vid ));
578 ibuf += sizeof( vid );
579 if (NULL == ( vol = getvolbyvid( vid )) ) {
580 return( AFPERR_PARAM );
583 if (vol->v_flags & AFPVOL_RO)
586 memcpy( &did, ibuf, sizeof( did ));
587 ibuf += sizeof( int );
589 if (NULL == ( dir = dirlookup( vol, did )) ) {
593 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
594 return get_afp_errno(AFPERR_NOOBJ);
597 upath = s_path->u_name;
598 if ( path_isadir( s_path) ) {
599 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
602 rc = deletecurdir( vol);
603 } else if (of_findname(s_path)) {
606 /* it's a file st_valid should always be true
607 * only test for ENOENT because EACCES needs
608 * to read meta data in deletefile
610 if (s_path->st_valid && s_path->st_errno == ENOENT) {
614 rc = deletefile(vol, -1, upath, 1);
616 struct dir *cachedfile;
617 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath), s_path->st.st_ctime))) {
618 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
619 dir_free(cachedfile);
623 if ( rc == AFP_OK ) {
625 setvoltime(obj, vol );
630 /* ------------------------ */
631 char *absupath(const struct vol *vol, struct dir *dir, char *u)
633 static char pathbuf[MAXPATHLEN + 1];
636 if (u == NULL || dir == NULL || vol == NULL)
639 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
641 if (bcatcstr(path, "/") != BSTR_OK)
643 if (bcatcstr(path, u) != BSTR_OK)
645 if (path->slen > MAXPATHLEN)
648 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
650 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
656 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
658 if (vol == NULL || dir == NULL || name == NULL)
660 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
663 /* ------------------------- */
664 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
667 struct dir *sdir, *ddir;
669 char *oldname, *newname;
678 #endif /* DROPKLUDGE */
685 memcpy( &vid, ibuf, sizeof( vid ));
686 ibuf += sizeof( vid );
687 if (NULL == ( vol = getvolbyvid( vid )) ) {
688 return( AFPERR_PARAM );
691 if (vol->v_flags & AFPVOL_RO)
694 /* source did followed by dest did */
695 memcpy( &did, ibuf, sizeof( did ));
696 ibuf += sizeof( int );
697 if (NULL == ( sdir = dirlookup( vol, did )) ) {
698 return afp_errno; /* was AFPERR_PARAM */
701 memcpy( &did, ibuf, sizeof( did ));
702 ibuf += sizeof( int );
704 /* source pathname */
705 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
706 return get_afp_errno(AFPERR_NOOBJ);
710 newname = obj->newtmp;
711 oldname = obj->oldtmp;
713 isdir = path_isadir(path);
714 if ( *path->m_name != '\0' ) {
718 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
720 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
724 if ((sdir_fd = open(".", O_RDONLY)) == -1)
728 /* get the destination directory */
729 if (NULL == ( ddir = dirlookup( vol, did )) ) {
730 rc = afp_errno; /* was AFPERR_PARAM */
733 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
737 pdid = curdir->d_did;
738 if ( *path->m_name != '\0' ) {
739 rc = path_error(path, AFPERR_NOOBJ);
743 /* one more place where we know about path type */
744 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
750 strcpy(newname, oldname);
753 /* This does the work */
754 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
755 oldname, newname, isdir);
756 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
758 if ( rc == AFP_OK ) {
759 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
768 if (vol->v_flags & AFPVOL_DROPBOX) {
769 /* FIXME did is not always the source id */
770 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
776 #endif /* DROPKLUDGE */
777 /* if unix priv don't try to match perm with dest folder */
778 if (!isdir && !vol_unix_priv(vol)) {
779 int admode = ad_mode("", 0777) | vol->v_fperm;
781 setfilmode(upath, admode, NULL, vol->v_umask);
782 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
784 setvoltime(obj, vol );
796 int veto_file(const char*veto_str, const char*path)
797 /* given a veto_str like "abc/zxc/" and path "abc", return 1
798 * veto_str should be '/' delimited
799 * if path matches any one of the veto_str elements exactly, then 1 is returned
800 * otherwise, 0 is returned.
803 int i; /* index to veto_str */
804 int j; /* index to path */
806 if ((veto_str == NULL) || (path == NULL))
809 for(i=0, j=0; veto_str[i] != '\0'; i++) {
810 if (veto_str[i] == '/') {
811 if ((j>0) && (path[j] == '\0')) {
812 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
817 if (veto_str[i] != path[j]) {
818 while ((veto_str[i] != '/')
819 && (veto_str[i] != '\0'))