2 * $Id: filedir.c,v 1.45.2.2.2.9 2004-03-11 02:02:01 didg 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 */
34 #include <sys/param.h>
35 #include <atalk/adouble.h>
37 #include <atalk/afp.h>
38 #include <atalk/util.h>
39 #include <atalk/cnid.h>
40 #include <atalk/logger.h>
42 #include "directory.h"
52 int matchfile2dirperms(upath, vol, did)
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_info, 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->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: Error changing owner/gid of %s: %s",
97 upath, strerror(errno));
100 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
102 LOG(log_error, logtype_afpd,
103 "matchfile2dirperms: Error adding file read permissions: %s",
107 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
109 LOG(log_error, logtype_afpd,
110 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %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: Error adding AD file read permissions: %s",
123 } /* end else if stat success */
126 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
132 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
135 int ibuflen, *rbuflen;
142 u_int16_t fbitmap, dbitmap, vid;
146 LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
152 memcpy( &vid, ibuf, sizeof( vid ));
153 ibuf += sizeof( vid );
154 if (NULL == ( vol = getvolbyvid( vid )) ) {
155 return( AFPERR_PARAM );
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);
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.
185 if ( s_path->st_errno != 0 ) {
186 return( AFPERR_NOOBJ );
190 if (S_ISDIR(st->st_mode)) {
196 ret = getdirparams(vol, dbitmap, s_path, dir,
197 rbuf + 3 * sizeof( u_int16_t ), &buflen );
201 /* this is a directory */
202 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
204 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
205 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
209 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
211 *rbuflen = buflen + 3 * sizeof( u_int16_t );
212 fbitmap = htons( fbitmap );
213 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
214 rbuf += sizeof( fbitmap );
215 dbitmap = htons( dbitmap );
216 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
217 rbuf += sizeof( dbitmap ) + sizeof( u_char );
221 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
227 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
230 int ibuflen, *rbuflen;
236 u_int16_t vid, bitmap;
240 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
245 memcpy( &vid, ibuf, sizeof(vid));
246 ibuf += sizeof( vid );
248 if (NULL == ( vol = getvolbyvid( vid )) ) {
249 return( AFPERR_PARAM );
252 if (vol->v_flags & AFPVOL_RO)
255 memcpy( &did, ibuf, sizeof( did));
256 ibuf += sizeof( did);
258 if (NULL == ( dir = dirlookup( vol, did )) ) {
262 memcpy( &bitmap, ibuf, sizeof( bitmap ));
263 bitmap = ntohs( bitmap );
264 ibuf += sizeof( bitmap );
266 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
267 return get_afp_errno(AFPERR_NOOBJ);
271 if (!path->st_valid) {
272 /* it's a dir and it should be there
273 * because we chdir in it in cname
278 if ( path->st_errno != 0 ) {
279 return( AFPERR_NOOBJ );
282 * If ibuf is odd, make it even.
284 if ((u_long)ibuf & 1 ) {
288 if (S_ISDIR(st->st_mode)) {
289 rc = setdirparams(vol, path, bitmap, ibuf );
291 rc = setfilparams(vol, path, bitmap, ibuf );
293 if ( rc == AFP_OK ) {
294 setvoltime(obj, vol );
298 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
304 /* --------------------------------------------
305 Factorise some checks on a pathname
307 int check_name(const struct vol *vol, char *name)
309 /* check for illegal characters in the unix filename */
310 if (!wincheck(vol, name))
313 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
316 if (!vol->validupath(vol, name))
319 /* check for vetoed filenames */
320 if (veto_file(vol->v_veto, name))
325 /* -------------------------
326 move and rename sdir:oldname to curdir:newname in volume vol
328 special care is needed for lock
330 static int moveandrename(vol, sdir, oldname, newname, isdir)
331 const struct vol *vol;
344 struct ofork *opened = NULL;
347 ucs2_t *oldname_w, *newname_w;
349 ad_init(&ad, vol->v_adouble);
354 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
356 return AFPERR_PARAM; /* can't convert */
358 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
359 p = ctoupath( vol, sdir, oldname );
361 return AFPERR_PARAM; /* pathname too long */
365 if ((opened = of_findname(&path))) {
366 /* reuse struct adouble so it won't break locks */
371 id = sdir->d_did; /* we already have the CNID */
372 p = ctoupath( vol, sdir->d_parent, oldname );
376 adflags = ADFLAGS_DIR;
379 * p now points to the full pathname of the source fs object.
381 * we are in the dest folder so we need to use p for ad_open
384 if (!ad_open(p, ADFLAGS_HF |adflags, O_RDONLY, 0666, adp)) {
387 ad_getattr(adp, &bshort);
388 ad_close( adp, ADFLAGS_HF );
389 if ((bshort & htons(ATTRBIT_NORENAME)))
390 return(AFPERR_OLOCK);
393 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
398 if (0 != (rc = check_name(vol, upath))) {
402 /* source == destination. we just silently accept this. */
403 if (curdir == sdir) {
404 if (strcmp(oldname, newname) == 0)
407 /* deal with case insensitive, case-preserving filesystems. */
408 if ((stat(upath, st) == 0)) {
409 if ((size_t)-1 == (convert_string_allocate(vol->v_volcharset, CH_UCS2, oldname,
410 strlen(oldname), (char**) &oldname_w)) ) {
411 return AFPERR_MISC; /* conversion error has already been logged */
413 if ((size_t)-1 == (convert_string_allocate(vol->v_volcharset, CH_UCS2, newname,
414 strlen(newname), (char**) &newname_w)) ) {
416 return AFPERR_MISC; /* conversion error has already been logged */
418 if (!strcasecmp_w(oldname_w, newname_w)) {
426 } else if (stat(upath, st ) == 0)
431 path.st_errno = errno;
432 if (of_findname(&path)) {
433 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
435 rc = renamefile(vol, p, upath, newname, adp );
437 of_rename(vol, opened, sdir, oldname, curdir, newname);
440 rc = renamedir(vol, p, upath, sdir, curdir, newname);
442 if ( rc == AFP_OK && id ) {
443 /* renaming may have moved the file/dir across a filesystem */
444 if (stat(upath, st) < 0)
447 /* fix up the catalog entry */
448 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
454 /* -------------------------------------------- */
455 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
458 int ibuflen, *rbuflen;
462 char *oldname, *newname;
470 LOG(log_info, logtype_afpd, "begin afp_rename:");
476 memcpy( &vid, ibuf, sizeof( vid ));
477 ibuf += sizeof( vid );
478 if (NULL == ( vol = getvolbyvid( vid )) ) {
479 return( AFPERR_PARAM );
482 if (vol->v_flags & AFPVOL_RO)
485 memcpy( &did, ibuf, sizeof( did ));
486 ibuf += sizeof( did );
487 if (NULL == ( sdir = dirlookup( vol, did )) ) {
491 /* source pathname */
492 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
493 return get_afp_errno(AFPERR_NOOBJ);
497 newname = obj->newtmp;
498 oldname = obj->oldtmp;
499 isdir = path_isadir(path);
500 if ( *path->m_name != '\0' ) {
501 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
503 /* curdir parent dir, need to move sdir back */
508 if ( sdir->d_parent == NULL ) { /* root directory */
509 return( AFPERR_NORENAME );
511 /* move to destination dir */
512 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
515 strcpy(oldname, sdir->d_m_name);
518 /* another place where we know about the path type */
519 if ((plen = copy_path_name(newname, ibuf)) < 0) {
520 return( AFPERR_PARAM );
524 return AFP_OK; /* newname == oldname same dir */
527 rc = moveandrename(vol, sdir, oldname, newname, isdir);
529 if ( rc == AFP_OK ) {
530 setvoltime(obj, vol );
534 LOG(log_info, logtype_afpd, "end afp_rename:");
540 /* ------------------------------- */
541 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
544 int ibuflen, *rbuflen;
554 LOG(log_info, logtype_afpd, "begin afp_delete:");
560 memcpy( &vid, ibuf, sizeof( vid ));
561 ibuf += sizeof( vid );
562 if (NULL == ( vol = getvolbyvid( vid )) ) {
563 return( AFPERR_PARAM );
566 if (vol->v_flags & AFPVOL_RO)
569 memcpy( &did, ibuf, sizeof( did ));
570 ibuf += sizeof( int );
571 if (NULL == ( dir = dirlookup( vol, did )) ) {
575 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
576 return get_afp_errno(AFPERR_NOOBJ);
579 upath = s_path->u_name;
580 if ( path_isadir( s_path) ) {
581 if (*s_path->m_name != '\0') {
585 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
587 } else if (of_findname(s_path)) {
590 rc = deletefile(vol, upath, 1);
592 if ( rc == AFP_OK ) {
594 setvoltime(obj, vol );
598 LOG(log_info, logtype_afpd, "end afp_delete:");
603 /* ------------------------ */
604 char *absupath( vol, dir, u )
605 const struct vol *vol;
610 static char path[ MAXPATHLEN + 1];
617 p = path + sizeof( path ) - 1;
622 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
625 if (p -len -1 < path) {
627 rather rare so LOG error and/or client message ?
635 len = strlen( vol->v_path );
636 if (p -len -1 < path) {
641 memcpy( p, vol->v_path, len );
646 /* ------------------------
647 * FIXME dir could be NULL
649 char *ctoupath( vol, dir, name )
650 const struct vol *vol;
654 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
657 /* ------------------------- */
658 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
661 int ibuflen, *rbuflen;
664 struct dir *sdir, *ddir;
666 char *oldname, *newname;
675 #endif /* DROPKLUDGE */
678 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
684 memcpy( &vid, ibuf, sizeof( vid ));
685 ibuf += sizeof( vid );
686 if (NULL == ( vol = getvolbyvid( vid )) ) {
687 return( AFPERR_PARAM );
690 if (vol->v_flags & AFPVOL_RO)
693 /* source did followed by dest did */
694 memcpy( &did, ibuf, sizeof( did ));
695 ibuf += sizeof( int );
696 if (NULL == ( sdir = dirlookup( vol, did )) ) {
697 return afp_errno; /* was AFPERR_PARAM */
700 memcpy( &did, ibuf, sizeof( did ));
701 ibuf += sizeof( int );
703 /* source pathname */
704 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
705 return get_afp_errno(AFPERR_NOOBJ);
709 newname = obj->newtmp;
710 oldname = obj->oldtmp;
712 isdir = path_isadir(path);
713 if ( *path->m_name != '\0' ) {
717 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
719 strcpy(oldname, sdir->d_m_name);
722 /* get the destination directory */
723 if (NULL == ( ddir = dirlookup( vol, did )) ) {
724 return afp_errno; /* was AFPERR_PARAM */
726 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
727 return( AFPERR_NOOBJ );
729 pdid = curdir->d_did;
730 if ( *path->m_name != '\0' ) {
731 return path_error(path, AFPERR_NOOBJ);
734 /* one more place where we know about path type */
735 if ((plen = copy_path_name(newname, ibuf)) < 0) {
736 return( AFPERR_PARAM );
740 strcpy(newname, oldname);
743 rc = moveandrename(vol, sdir, oldname, newname, isdir);
745 if ( rc == AFP_OK ) {
746 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) {
761 #endif /* DROPKLUDGE */
762 /* if unix priv don't try to match perm with dest folder */
763 if (!isdir && !vol_unix_priv(vol)) {
764 int admode = ad_mode("", 0777);
766 setfilmode(upath, admode, NULL);
767 setfilmode(vol->ad_path( upath, ADFLAGS_HF ), ad_hf_mode(admode), NULL);
769 setvoltime(obj, vol );
773 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
779 int veto_file(const char*veto_str, const char*path)
780 /* given a veto_str like "abc/zxc/" and path "abc", return 1
781 * veto_str should be '/' delimited
782 * if path matches any one of the veto_str elements exactly, then 1 is returned
783 * otherwise, 0 is returned.
786 int i; /* index to veto_str */
787 int j; /* index to path */
789 if ((veto_str == NULL) || (path == NULL))
793 LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
796 for(i=0, j=0; veto_str[i] != '\0'; i++) {
797 if (veto_str[i] == '/') {
798 if ((j>0) && (path[j] == '\0'))
802 if (veto_str[i] != path[j]) {
803 while ((veto_str[i] != '/')
804 && (veto_str[i] != '\0'))