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)
388 if (!ad_metadata(p, adflags, adp)) {
391 ad_getattr(adp, &bshort);
392 ad_close_metadata( adp);
393 if ((bshort & htons(ATTRBIT_NORENAME)))
394 return(AFPERR_OLOCK);
397 if (fchdir(cwd_fd) != 0) {
398 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
403 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
408 if (0 != (rc = check_name(vol, upath))) {
412 /* source == destination. we just silently accept this. */
413 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
414 if (strcmp(oldname, newname) == 0)
417 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
418 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
419 /* not the same file */
424 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
429 path.st_errno = errno;
430 if (of_findname(&path)) {
431 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
433 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
435 of_rename(vol, opened, sdir, oldname, curdir, newname);
438 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
440 if ( rc == AFP_OK && id ) {
441 /* renaming may have moved the file/dir across a filesystem */
442 if (stat(upath, st) < 0)
445 /* fix up the catalog entry */
446 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
452 /* -------------------------------------------- */
453 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
457 char *oldname, *newname;
468 memcpy( &vid, ibuf, sizeof( vid ));
469 ibuf += sizeof( vid );
470 if (NULL == ( vol = getvolbyvid( vid )) ) {
471 return( AFPERR_PARAM );
474 if (vol->v_flags & AFPVOL_RO)
477 memcpy( &did, ibuf, sizeof( did ));
478 ibuf += sizeof( did );
479 if (NULL == ( sdir = dirlookup( vol, did )) ) {
483 /* source pathname */
484 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
485 return get_afp_errno(AFPERR_NOOBJ);
489 newname = obj->newtmp;
490 oldname = obj->oldtmp;
491 isdir = path_isadir(path);
492 if ( *path->m_name != '\0' ) {
493 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
495 /* curdir parent dir, need to move sdir back */
500 if ( sdir->d_parent == NULL ) { /* root directory */
501 return( AFPERR_NORENAME );
503 /* move to destination dir */
504 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
507 strcpy(oldname, sdir->d_m_name);
510 /* another place where we know about the path type */
511 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
512 return( AFPERR_PARAM );
516 return AFP_OK; /* newname == oldname same dir */
519 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
520 if ( rc == AFP_OK ) {
521 setvoltime(obj, vol );
527 /* ------------------------------- */
528 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
540 memcpy( &vid, ibuf, sizeof( vid ));
541 ibuf += sizeof( vid );
542 if (NULL == ( vol = getvolbyvid( vid )) ) {
543 return( AFPERR_PARAM );
546 if (vol->v_flags & AFPVOL_RO)
549 memcpy( &did, ibuf, sizeof( did ));
550 ibuf += sizeof( int );
551 if (NULL == ( dir = dirlookup( vol, did )) ) {
555 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
556 return get_afp_errno(AFPERR_NOOBJ);
559 upath = s_path->u_name;
560 if ( path_isadir( s_path) ) {
561 if (*s_path->m_name != '\0') {
565 rc = deletecurdir( vol);
567 } else if (of_findname(s_path)) {
570 /* it's a file st_valid should always be true
571 * only test for ENOENT because EACCES needs
572 * to read meta data in deletefile
574 if (s_path->st_valid && s_path->st_errno == ENOENT) {
578 rc = deletefile(vol, -1, upath, 1);
581 if ( rc == AFP_OK ) {
583 setvoltime(obj, vol );
588 /* ------------------------ */
589 char *absupath(const struct vol *vol, struct dir *dir, char *u)
592 static char path[ MAXPATHLEN + 1];
599 p = path + sizeof( path ) - 1;
604 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
607 if (p -len -1 < path) {
609 rather rare so LOG error and/or client message ?
617 len = strlen( vol->v_path );
618 if (p -len -1 < path) {
623 memcpy( p, vol->v_path, len );
628 /* ------------------------
629 * FIXME dir could be NULL
631 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
633 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
636 /* ------------------------- */
637 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
640 struct dir *sdir, *ddir;
642 char *oldname, *newname;
651 #endif /* DROPKLUDGE */
658 memcpy( &vid, ibuf, sizeof( vid ));
659 ibuf += sizeof( vid );
660 if (NULL == ( vol = getvolbyvid( vid )) ) {
661 return( AFPERR_PARAM );
664 if (vol->v_flags & AFPVOL_RO)
667 /* source did followed by dest did */
668 memcpy( &did, ibuf, sizeof( did ));
669 ibuf += sizeof( int );
670 if (NULL == ( sdir = dirlookup( vol, did )) ) {
671 return afp_errno; /* was AFPERR_PARAM */
674 memcpy( &did, ibuf, sizeof( did ));
675 ibuf += sizeof( int );
677 /* source pathname */
678 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
679 return get_afp_errno(AFPERR_NOOBJ);
683 newname = obj->newtmp;
684 oldname = obj->oldtmp;
686 isdir = path_isadir(path);
687 if ( *path->m_name != '\0' ) {
691 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
693 strcpy(oldname, sdir->d_m_name);
697 if ((sdir_fd = open(".", O_RDONLY)) == -1)
701 /* get the destination directory */
702 if (NULL == ( ddir = dirlookup( vol, did )) ) {
703 rc = afp_errno; /* was AFPERR_PARAM */
706 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
710 pdid = curdir->d_did;
711 if ( *path->m_name != '\0' ) {
712 rc = path_error(path, AFPERR_NOOBJ);
716 /* one more place where we know about path type */
717 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
723 strcpy(newname, oldname);
726 /* This does the work */
727 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
729 if ( rc == AFP_OK ) {
730 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
739 if (vol->v_flags & AFPVOL_DROPBOX) {
740 /* FIXME did is not always the source id */
741 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
747 #endif /* DROPKLUDGE */
748 /* if unix priv don't try to match perm with dest folder */
749 if (!isdir && !vol_unix_priv(vol)) {
750 int admode = ad_mode("", 0777) | vol->v_fperm;
752 setfilmode(upath, admode, NULL, vol->v_umask);
753 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
755 setvoltime(obj, vol );
767 int veto_file(const char*veto_str, const char*path)
768 /* given a veto_str like "abc/zxc/" and path "abc", return 1
769 * veto_str should be '/' delimited
770 * if path matches any one of the veto_str elements exactly, then 1 is returned
771 * otherwise, 0 is returned.
774 int i; /* index to veto_str */
775 int j; /* index to path */
777 if ((veto_str == NULL) || (path == NULL))
780 for(i=0, j=0; veto_str[i] != '\0'; i++) {
781 if (veto_str[i] == '/') {
782 if ((j>0) && (path[j] == '\0')) {
783 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
788 if (veto_str[i] != path[j]) {
789 while ((veto_str[i] != '/')
790 && (veto_str[i] != '\0'))