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/acl.h>
42 #include "directory.h"
52 int matchfile2dirperms(
53 /* Since it's kinda' big; I decided against an
58 /* The below code changes the way file ownership is determined in the name of
59 fixing dropboxes. It has known security problem. See the netatalk FAQ for
68 LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
71 if (stat(upath, &st ) < 0) {
72 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
76 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
77 /* FIXME dirsearch doesn't move cwd to did ! */
78 if (( dir = dirlookup( vol, did )) == NULL ) {
79 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
82 else if (stat(".", &sb) < 0) {
83 LOG(log_error, logtype_afpd,
84 "matchfile2dirperms: Error checking directory \"%s\": %s",
85 dir->d_m_name, strerror(errno));
90 if ( uid != sb.st_uid )
93 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
95 LOG(log_error, logtype_afpd,
96 "matchfile2dirperms(%s): Error changing owner/gid: %s",
97 upath, strerror(errno));
100 else if ((!S_ISLNK(st->st_mode)) && (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0))
102 LOG(log_error, logtype_afpd,
103 "matchfile2dirperms(%s): Error adding file read permissions: %s",
104 upath, strerror(errno));
107 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
109 LOG(log_error, logtype_afpd,
110 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
111 adpath, strerror(errno));
114 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
116 LOG(log_error, logtype_afpd,
117 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
118 adpath, strerror(errno));
123 } /* end else if stat success */
126 LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
132 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
140 u_int16_t fbitmap, dbitmap, vid;
146 memcpy( &vid, ibuf, sizeof( vid ));
147 ibuf += sizeof( vid );
148 if (NULL == ( vol = getvolbyvid( vid )) ) {
149 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
152 return( AFPERR_ACCESS );
155 memcpy( &did, ibuf, sizeof( did ));
156 ibuf += sizeof( did );
158 if (NULL == ( dir = dirlookup( vol, did )) ) {
162 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
163 fbitmap = ntohs( fbitmap );
164 ibuf += sizeof( fbitmap );
165 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
166 dbitmap = ntohs( dbitmap );
167 ibuf += sizeof( dbitmap );
169 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
170 return get_afp_errno(AFPERR_NOOBJ);
173 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, name:'%s', f/d:%04x/%04x) {cwd: %s}",
174 ntohs(vid), ntohl(dir->d_did), s_path->u_name, fbitmap, dbitmap, getcwdpath());
177 if (!s_path->st_valid) {
178 /* it's a dir and it should be there
179 * because we chdir in it in cname or
180 * it's curdir (maybe deleted, but then we can't know).
181 * So we need to try harder.
183 of_statdir(vol, s_path);
185 if ( s_path->st_errno != 0 ) {
186 return( AFPERR_NOOBJ );
191 if (S_ISDIR(st->st_mode)) {
197 ret = getdirparams(vol, dbitmap, s_path, dir,
198 rbuf + 3 * sizeof( u_int16_t ), &buflen );
202 /* this is a directory */
203 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
205 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
206 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
210 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
212 *rbuflen = buflen + 3 * sizeof( u_int16_t );
213 fbitmap = htons( fbitmap );
214 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
215 rbuf += sizeof( fbitmap );
216 dbitmap = htons( dbitmap );
217 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
218 rbuf += sizeof( dbitmap ) + sizeof( u_char );
224 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
230 u_int16_t vid, bitmap;
235 memcpy( &vid, ibuf, sizeof(vid));
236 ibuf += sizeof( vid );
238 if (NULL == ( vol = getvolbyvid( vid )) ) {
239 return( AFPERR_PARAM );
242 if (vol->v_flags & AFPVOL_RO)
245 memcpy( &did, ibuf, sizeof( did));
246 ibuf += sizeof( did);
248 if (NULL == ( dir = dirlookup( vol, did )) ) {
252 memcpy( &bitmap, ibuf, sizeof( bitmap ));
253 bitmap = ntohs( bitmap );
254 ibuf += sizeof( bitmap );
256 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
257 return get_afp_errno(AFPERR_NOOBJ);
261 if (!path->st_valid) {
262 /* it's a dir and it should be there
263 * because we chdir in it in cname
265 of_statdir(vol, path);
268 if ( path->st_errno != 0 ) {
269 return( AFPERR_NOOBJ );
272 * If ibuf is odd, make it even.
274 if ((u_long)ibuf & 1 ) {
278 if (S_ISDIR(st->st_mode)) {
279 rc = setdirparams(vol, path, bitmap, ibuf );
281 rc = setfilparams(vol, path, bitmap, ibuf );
283 if ( rc == AFP_OK ) {
284 setvoltime(obj, vol );
290 /* --------------------------------------------
291 Factorise some checks on a pathname
293 int check_name(const struct vol *vol, char *name)
295 /* check for illegal characters in the unix filename */
296 if (!wincheck(vol, name))
299 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
302 if (!vol->vfs->vfs_validupath(vol, name)) {
303 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
307 /* check for vetoed filenames */
308 if (veto_file(vol->v_veto, name))
313 /* -------------------------
314 move and rename sdir:oldname to curdir:newname in volume vol
315 special care is needed for lock
317 static int moveandrename(const struct vol *vol,
327 struct stat *st, nst;
331 struct ofork *opened = NULL;
336 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
341 if ((p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding())) == NULL)
342 return AFPERR_PARAM; /* can't convert */
344 #ifndef HAVE_RENAMEAT
346 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
347 p = ctoupath( vol, sdir, oldname );
349 return AFPERR_PARAM; /* pathname too long */
350 #endif /* HAVE_RENAMEAT */
355 opened = of_findnameat(sdir_fd, &path);
357 opened = of_findname(&path);
358 #endif /* HAVE_RENAMEAT */
360 /* reuse struct adouble so it won't break locks */
364 id = sdir->d_did; /* we already have the CNID */
365 p = ctoupath( vol, sdir->d_parent, oldname );
369 adflags = ADFLAGS_DIR;
374 * p now points to either
375 * a) full pathname of the source fs object (if renameat is not available)
376 * b) the oldname (renameat is available)
377 * we are in the dest folder so we need to use
379 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
383 if ((cwd_fd = open(".", O_RDONLY)) == -1)
385 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))) {
401 if (fchdir(cwd_fd) != 0) {
402 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
408 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
414 if (0 != (rc = check_name(vol, upath))) {
418 /* source == destination. we just silently accept this. */
419 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
420 if (strcmp(oldname, newname) == 0) {
425 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
426 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
427 /* not the same file */
433 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
440 path.st_errno = errno;
441 if (of_findname(&path)) {
442 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
444 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
446 of_rename(vol, opened, sdir, oldname, curdir, newname);
449 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
451 if ( rc == AFP_OK && id ) {
452 /* renaming may have moved the file/dir across a filesystem */
453 if (stat(upath, st) < 0) {
457 /* fix up the catalog entry */
458 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
467 /* -------------------------------------------- */
468 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
472 char *oldname, *newname;
483 memcpy( &vid, ibuf, sizeof( vid ));
484 ibuf += sizeof( vid );
485 if (NULL == ( vol = getvolbyvid( vid )) ) {
486 return( AFPERR_PARAM );
489 if (vol->v_flags & AFPVOL_RO)
492 memcpy( &did, ibuf, sizeof( did ));
493 ibuf += sizeof( did );
494 if (NULL == ( sdir = dirlookup( vol, did )) ) {
498 /* source pathname */
499 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
500 return get_afp_errno(AFPERR_NOOBJ);
504 newname = obj->newtmp;
505 oldname = obj->oldtmp;
506 isdir = path_isadir(path);
507 if ( *path->m_name != '\0' ) {
508 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
510 /* curdir parent dir, need to move sdir back */
515 if ( sdir->d_parent == NULL ) { /* root directory */
516 return( AFPERR_NORENAME );
518 /* move to destination dir */
519 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
522 strcpy(oldname, sdir->d_m_name);
525 /* another place where we know about the path type */
526 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
527 return( AFPERR_PARAM );
531 return AFP_OK; /* newname == oldname same dir */
534 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
535 if ( rc == AFP_OK ) {
536 setvoltime(obj, vol );
542 /* ------------------------------- */
543 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
555 memcpy( &vid, ibuf, sizeof( vid ));
556 ibuf += sizeof( vid );
557 if (NULL == ( vol = getvolbyvid( vid )) ) {
558 return( AFPERR_PARAM );
561 if (vol->v_flags & AFPVOL_RO)
564 memcpy( &did, ibuf, sizeof( did ));
565 ibuf += sizeof( int );
566 if (NULL == ( dir = dirlookup( vol, did )) ) {
570 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
571 return get_afp_errno(AFPERR_NOOBJ);
574 upath = s_path->u_name;
575 if ( path_isadir( s_path) ) {
576 if (*s_path->m_name != '\0') {
580 rc = deletecurdir( vol);
582 } else if (of_findname(s_path)) {
585 /* it's a file st_valid should always be true
586 * only test for ENOENT because EACCES needs
587 * to read meta data in deletefile
589 if (s_path->st_valid && s_path->st_errno == ENOENT) {
593 rc = deletefile(vol, -1, upath, 1);
596 if ( rc == AFP_OK ) {
598 setvoltime(obj, vol );
603 /* ------------------------ */
604 char *absupath(const struct vol *vol, struct dir *dir, char *u)
607 static char path[ MAXPATHLEN + 1];
614 p = path + sizeof( path ) - 1;
619 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
622 if (p -len -1 < path) {
624 rather rare so LOG error and/or client message ?
632 len = strlen( vol->v_path );
633 if (p -len -1 < path) {
638 memcpy( p, vol->v_path, len );
643 /* ------------------------
644 * FIXME dir could be NULL
646 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
648 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
651 /* ------------------------- */
652 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
655 struct dir *sdir, *ddir;
657 char *oldname, *newname;
666 #endif /* DROPKLUDGE */
673 memcpy( &vid, ibuf, sizeof( vid ));
674 ibuf += sizeof( vid );
675 if (NULL == ( vol = getvolbyvid( vid )) ) {
676 return( AFPERR_PARAM );
679 if (vol->v_flags & AFPVOL_RO)
682 /* source did followed by dest did */
683 memcpy( &did, ibuf, sizeof( did ));
684 ibuf += sizeof( int );
685 if (NULL == ( sdir = dirlookup( vol, did )) ) {
686 return afp_errno; /* was AFPERR_PARAM */
689 memcpy( &did, ibuf, sizeof( did ));
690 ibuf += sizeof( int );
692 /* source pathname */
693 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
694 return get_afp_errno(AFPERR_NOOBJ);
698 newname = obj->newtmp;
699 oldname = obj->oldtmp;
701 isdir = path_isadir(path);
702 if ( *path->m_name != '\0' ) {
706 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
708 strcpy(oldname, sdir->d_m_name);
712 if ((sdir_fd = open(".", O_RDONLY)) == -1)
716 /* get the destination directory */
717 if (NULL == ( ddir = dirlookup( vol, did )) ) {
718 rc = afp_errno; /* was AFPERR_PARAM */
721 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
725 pdid = curdir->d_did;
726 if ( *path->m_name != '\0' ) {
727 rc = path_error(path, AFPERR_NOOBJ);
731 /* one more place where we know about path type */
732 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
738 strcpy(newname, oldname);
741 /* This does the work */
742 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
744 if ( rc == AFP_OK ) {
745 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
754 if (vol->v_flags & AFPVOL_DROPBOX) {
755 /* FIXME did is not always the source id */
756 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
762 #endif /* DROPKLUDGE */
763 /* if unix priv don't try to match perm with dest folder */
764 if (!isdir && !vol_unix_priv(vol)) {
765 int admode = ad_mode("", 0777) | vol->v_fperm;
767 setfilmode(upath, admode, NULL, vol->v_umask);
768 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
770 setvoltime(obj, vol );
782 int veto_file(const char*veto_str, const char*path)
783 /* given a veto_str like "abc/zxc/" and path "abc", return 1
784 * veto_str should be '/' delimited
785 * if path matches any one of the veto_str elements exactly, then 1 is returned
786 * otherwise, 0 is returned.
789 int i; /* index to veto_str */
790 int j; /* index to path */
792 if ((veto_str == NULL) || (path == NULL))
795 for(i=0, j=0; veto_str[i] != '\0'; i++) {
796 if (veto_str[i] == '/') {
797 if ((j>0) && (path[j] == '\0')) {
798 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
803 if (veto_str[i] != path[j]) {
804 while ((veto_str[i] != '/')
805 && (veto_str[i] != '\0'))