2 * $Id: filedir.c,v 1.45.2.1 2003-05-10 10:33:16 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
13 #include <atalk/logger.h>
14 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <netatalk/endian.h>
18 #include <atalk/adouble.h>
19 #include <atalk/afp.h>
20 #include <atalk/util.h>
22 #include <atalk/cnid.h>
28 #endif /* HAVE_FCNTL_H */
34 #else /* STDC_HEADERS */
38 #endif /* HAVE_STRCHR */
39 char *strchr (), *strrchr ();
41 #define memcpy(d,s,n) bcopy ((s), (d), (n))
42 #define memmove(d,s,n) bcopy ((s), (d), (n))
43 #endif /* ! HAVE_MEMCPY */
44 #endif /* STDC_HEADERS */
48 #endif /* HAVE_UNISTD_H */
50 #include "directory.h"
60 int matchfile2dirperms(upath, vol, did)
61 /* Since it's kinda' big; I decided against an
66 /* The below code changes the way file ownership is determined in the name of
67 fixing dropboxes. It has known security problem. See the netatalk FAQ for
76 LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
79 if (stat(upath, &st ) < 0) {
80 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
84 adpath = ad_path( upath, ADFLAGS_HF );
85 /* FIXME dirsearch doesn't move cwd to did ! */
86 if (( dir = dirlookup( vol, did )) == NULL ) {
87 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
90 else if (stat(".", &sb) < 0) {
91 LOG(log_error, logtype_afpd,
92 "matchfile2dirperms: Error checking directory \"%s\": %s",
93 dir->d_m_name, strerror(errno));
98 if ( uid != sb.st_uid )
101 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
103 LOG(log_error, logtype_afpd,
104 "matchfile2dirperms: Error changing owner/gid of %s: %s",
105 upath, strerror(errno));
108 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
110 LOG(log_error, logtype_afpd,
111 "matchfile2dirperms: Error adding file read permissions: %s",
115 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
117 LOG(log_error, logtype_afpd,
118 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %s",
119 adpath, strerror(errno));
122 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
124 LOG(log_error, logtype_afpd,
125 "matchfile2dirperms: Error adding AD file read permissions: %s",
131 } /* end else if stat success */
134 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
140 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
143 int ibuflen, *rbuflen;
150 u_int16_t fbitmap, dbitmap, vid;
154 LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
160 memcpy( &vid, ibuf, sizeof( vid ));
161 ibuf += sizeof( vid );
162 if (NULL == ( vol = getvolbyvid( vid )) ) {
163 return( AFPERR_PARAM );
166 memcpy( &did, ibuf, sizeof( did ));
167 ibuf += sizeof( did );
169 if (NULL == ( dir = dirlookup( vol, did )) ) {
173 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
174 fbitmap = ntohs( fbitmap );
175 ibuf += sizeof( fbitmap );
176 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
177 dbitmap = ntohs( dbitmap );
178 ibuf += sizeof( dbitmap );
180 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
181 return get_afp_errno(AFPERR_NOOBJ);
185 if (!s_path->st_valid) {
186 /* it's a dir and it should be there
187 * because we chdir in it in cname or
188 * it's curdir (maybe deleted, but then we can't know)
193 if ( s_path->st_errno != 0 ) {
194 return( AFPERR_NOOBJ );
198 if (S_ISDIR(st->st_mode)) {
204 ret = getdirparams(vol, dbitmap, s_path, dir,
205 rbuf + 3 * sizeof( u_int16_t ), &buflen );
209 /* this is a directory */
210 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
212 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
213 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
217 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
219 *rbuflen = buflen + 3 * sizeof( u_int16_t );
220 fbitmap = htons( fbitmap );
221 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
222 rbuf += sizeof( fbitmap );
223 dbitmap = htons( dbitmap );
224 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
225 rbuf += sizeof( dbitmap ) + sizeof( u_char );
229 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
235 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
238 int ibuflen, *rbuflen;
244 u_int16_t vid, bitmap;
248 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
253 memcpy( &vid, ibuf, sizeof(vid));
254 ibuf += sizeof( vid );
256 if (NULL == ( vol = getvolbyvid( vid )) ) {
257 return( AFPERR_PARAM );
260 if (vol->v_flags & AFPVOL_RO)
263 memcpy( &did, ibuf, sizeof( did));
264 ibuf += sizeof( did);
266 if (NULL == ( dir = dirlookup( vol, did )) ) {
270 memcpy( &bitmap, ibuf, sizeof( bitmap ));
271 bitmap = ntohs( bitmap );
272 ibuf += sizeof( bitmap );
274 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
275 return get_afp_errno(AFPERR_NOOBJ);
279 if (!path->st_valid) {
280 /* it's a dir and it should be there
281 * because we chdir in it in cname
286 if ( path->st_errno != 0 ) {
287 return( AFPERR_NOOBJ );
290 * If ibuf is odd, make it even.
292 if ((u_long)ibuf & 1 ) {
296 if (S_ISDIR(st->st_mode)) {
297 rc = setdirparams(vol, path, bitmap, ibuf );
299 rc = setfilparams(vol, path, bitmap, ibuf );
301 if ( rc == AFP_OK ) {
302 setvoltime(obj, vol );
306 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
312 /* --------------------------------------------
313 Factorise some check on a pathname
315 int check_name(const struct vol *vol, char *name)
317 /* check for illegal characters in the unix filename */
318 if (!wincheck(vol, name))
321 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
324 if (!validupath(vol, name))
327 /* check for vetoed filenames */
328 if (veto_file(vol->v_veto, name))
333 /* -------------------------
334 move and rename sdir:oldname to curdir:newname in volume vol
336 special care is needed for lock
338 static int moveandrename(vol, sdir, oldname, newname, isdir)
339 const struct vol *vol;
352 struct ofork *opened = NULL;
358 memset(&ad, 0, sizeof(ad));
364 p = mtoupath(vol, oldname, utf8_encoding());
366 return AFPERR_PARAM; /* can't convert */
368 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
370 p = ctoupath( vol, sdir, oldname );
372 return AFPERR_PARAM; /* pathname too long */
376 if ((opened = of_findname(&path))) {
377 /* reuse struct adouble so it won't break locks */
383 id = sdir->d_did; /* we already have the CNID */
385 p = ctoupath( vol, sdir->d_parent, oldname );
389 adflags = ADFLAGS_DIR;
392 * p now points to the full pathname of the source fs object.
394 * we are in the dest folder so we need to use p for ad_open
397 if (!ad_open(p, ADFLAGS_HF |adflags, O_RDONLY, 0666, adp)) {
400 ad_getattr(adp, &bshort);
401 ad_close( adp, ADFLAGS_HF );
402 if ((bshort & htons(ATTRBIT_NORENAME)))
403 return(AFPERR_OLOCK);
406 if (NULL == (upath = mtoupath(vol, newname, utf8_encoding()))){
411 if (0 != (rc = check_name(vol, upath))) {
415 /* source == destination. we just silently accept this. */
416 if (curdir == sdir) {
417 if (strcmp(oldname, newname) == 0)
420 /* deal with case insensitive, case-preserving filesystems. */
421 if ((stat(upath, st) == 0) && strdiacasecmp(oldname, newname))
424 } else if (stat(upath, st ) == 0)
429 path.st_errno = errno;
430 if (of_findname(&path)) {
431 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
433 rc = renamefile( p, upath, newname,vol_noadouble(vol), adp );
435 of_rename(vol, opened, sdir, oldname, curdir, newname);
438 rc = renamedir(p, upath, sdir, curdir, newname, vol_noadouble(vol));
440 if ( rc == AFP_OK ) {
442 /* renaming may have moved the file/dir across a filesystem */
443 if (stat(upath, st) < 0)
446 /* fix up the catalog entry */
447 cnid_update(vol->v_db, 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 char *absupath( vol, dir, u )
604 const struct vol *vol;
609 static char path[ MAXPATHLEN + 1];
616 p = path + sizeof( path ) - 1;
620 strncpy( p, u, len );
621 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
624 if (p -len -1 < path) {
626 rather rare so LOG error and/or client message ?
632 strncpy( p, u, len );
634 len = strlen( vol->v_path );
635 if (p -len -1 < path) {
640 strncpy( p, vol->v_path, len );
645 char *ctoupath( vol, dir, name )
646 const struct vol *vol;
650 return absupath(vol, dir, mtoupath(vol, name, utf8_encoding()));
653 /* ------------------------- */
654 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
657 int ibuflen, *rbuflen;
660 struct dir *sdir, *ddir;
662 char *oldname, *newname;
670 #endif /* DROPKLUDGE */
673 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
679 memcpy( &vid, ibuf, sizeof( vid ));
680 ibuf += sizeof( vid );
681 if (NULL == ( vol = getvolbyvid( vid )) ) {
682 return( AFPERR_PARAM );
685 if (vol->v_flags & AFPVOL_RO)
688 /* source did followed by dest did */
689 memcpy( &did, ibuf, sizeof( did ));
690 ibuf += sizeof( int );
691 if (NULL == ( sdir = dirlookup( vol, did )) ) {
692 return afp_errno; /* was AFPERR_PARAM */
695 memcpy( &did, ibuf, sizeof( did ));
696 ibuf += sizeof( int );
698 /* source pathname */
699 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
700 return get_afp_errno(AFPERR_NOOBJ);
704 newname = obj->newtmp;
705 oldname = obj->oldtmp;
707 isdir = path_isadir(path);
708 if ( *path->m_name != '\0' ) {
712 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
714 strcpy(oldname, sdir->d_m_name);
717 /* get the destination directory */
718 if (NULL == ( ddir = dirlookup( vol, did )) ) {
719 return afp_errno; /* was AFPERR_PARAM */
721 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
722 return( AFPERR_NOOBJ );
724 if ( *path->m_name != '\0' ) {
725 return path_error(path, AFPERR_NOOBJ);
728 /* one more place where we know about path type */
729 if ((plen = copy_path_name(newname, ibuf)) < 0) {
730 return( AFPERR_PARAM );
734 strcpy(newname, oldname);
737 rc = moveandrename(vol, sdir, oldname, newname, isdir);
739 if ( rc == AFP_OK ) {
740 char *upath = mtoupath(vol, newname, utf8_encoding());
748 if (vol->v_flags & AFPVOL_DROPBOX) {
749 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
754 #endif /* DROPKLUDGE */
756 int admode = ad_mode("", 0777);
758 setfilmode(upath, admode, NULL);
759 setfilmode(ad_path( upath, ADFLAGS_HF ), ad_hf_mode(admode), NULL);
761 setvoltime(obj, vol );
765 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
771 int veto_file(const char*veto_str, const char*path)
772 /* given a veto_str like "abc/zxc/" and path "abc", return 1
773 * veto_str should be '/' delimited
774 * if path matches any one of the veto_str elements exactly, then 1 is returned
775 * otherwise, 0 is returned.
778 int i; /* index to veto_str */
779 int j; /* index to path */
781 if ((veto_str == NULL) || (path == NULL))
785 LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
788 for(i=0, j=0; veto_str[i] != '\0'; i++) {
789 if (veto_str[i] == '/') {
790 if ((j>0) && (path[j] == '\0'))
794 if (veto_str[i] != path[j]) {
795 while ((veto_str[i] != '/')
796 && (veto_str[i] != '\0'))