2 * $Id: filedir.c,v 1.39 2003-01-21 09:58:58 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 )) ) {
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)) {
200 if (*s_path->m_name != '\0') {
201 /* the dir wasn't in the cache and we weren't able to chdir in it.
203 return AFPERR_ACCESS;
205 ret = getdirparams(vol, dbitmap, s_path, curdir,
206 rbuf + 3 * sizeof( u_int16_t ), &buflen );
210 /* this is a directory */
211 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
213 if (fbitmap && ( ret = getfilparams(vol, fbitmap, s_path, curdir,
214 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
218 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
220 *rbuflen = buflen + 3 * sizeof( u_int16_t );
221 fbitmap = htons( fbitmap );
222 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
223 rbuf += sizeof( fbitmap );
224 dbitmap = htons( dbitmap );
225 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
226 rbuf += sizeof( dbitmap ) + sizeof( u_char );
230 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
236 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
239 int ibuflen, *rbuflen;
245 u_int16_t vid, bitmap;
249 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
254 memcpy( &vid, ibuf, sizeof(vid));
255 ibuf += sizeof( vid );
257 if (NULL == ( vol = getvolbyvid( vid )) ) {
258 return( AFPERR_PARAM );
261 if (vol->v_flags & AFPVOL_RO)
264 memcpy( &did, ibuf, sizeof( did));
265 ibuf += sizeof( did);
267 if (( dir = dirlookup( vol, did )) == NULL ) {
271 memcpy( &bitmap, ibuf, sizeof( bitmap ));
272 bitmap = ntohs( bitmap );
273 ibuf += sizeof( bitmap );
275 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
280 if (!path->st_valid) {
281 /* it's a dir and it should be there
282 * because we chdir in it in cname
287 if ( path->st_errno != 0 ) {
288 return( AFPERR_NOOBJ );
291 * If ibuf is odd, make it even.
293 if ((u_long)ibuf & 1 ) {
297 if (S_ISDIR(st->st_mode)) {
298 rc = setdirparams(vol, path, bitmap, ibuf );
300 rc = setfilparams(vol, path, bitmap, ibuf );
302 if ( rc == AFP_OK ) {
303 setvoltime(obj, vol );
307 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
313 /* --------------------------------------------
314 Factorise some check on a pathname
316 int check_name(const struct vol *vol, char *name)
318 /* check for illegal characters in the unix filename */
319 if (!wincheck(vol, name))
322 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
325 if (!validupath(vol, name))
328 /* check for vetoed filenames */
329 if (veto_file(vol->v_veto, name))
334 /* -------------------------
335 move and rename sdir:oldname to curdir:newname in volume vol
337 special care is needed for lock
339 static int moveandrename(vol, sdir, oldname, newname, isdir)
340 const struct vol *vol;
353 struct ofork *opened = NULL;
359 memset(&ad, 0, sizeof(ad));
365 p = mtoupath(vol, oldname);
366 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
368 p = ctoupath( vol, sdir, oldname );
371 if ((opened = of_findname(&path))) {
372 /* reuse struct adouble so it won't break locks */
378 id = sdir->d_did; /* we already have the CNID */
380 p = ctoupath( vol, sdir->d_parent, oldname );
381 adflags = ADFLAGS_DIR;
384 * p now points to the full pathname of the source fs object.
386 * we are in the dest folder so we need to use p for ad_open
389 if (!ad_open(p, ADFLAGS_HF |adflags, O_RDONLY, 0666, adp)) {
392 ad_getattr(adp, &bshort);
393 ad_close( adp, ADFLAGS_HF );
394 if ((bshort & htons(ATTRBIT_NORENAME)))
395 return(AFPERR_OLOCK);
398 upath = mtoupath(vol, newname);
401 if (0 != (rc = check_name(vol, upath))) {
405 /* source == destination. we just silently accept this. */
406 if (curdir == sdir) {
407 if (strcmp(oldname, newname) == 0)
410 /* deal with case insensitive, case-preserving filesystems. */
411 if ((stat(upath, st) == 0) && strdiacasecmp(oldname, newname))
414 } else if (stat(upath, st ) == 0)
419 path.st_errno = errno;
420 if (of_findname(&path)) {
421 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
423 rc = renamefile( p, upath, newname,vol_noadouble(vol), adp );
425 of_rename(vol, opened, sdir, oldname, curdir, newname);
428 rc = renamedir(p, upath, sdir, curdir, newname, vol_noadouble(vol));
430 if ( rc == AFP_OK ) {
432 /* renaming may have moved the file/dir across a filesystem */
433 if (stat(upath, st) < 0)
436 /* fix up the catalog entry */
437 cnid_update(vol->v_db, id, st, curdir->d_did, upath, strlen(upath));
444 /* -------------------------------------------- */
445 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
448 int ibuflen, *rbuflen;
452 char *oldname, *newname;
460 LOG(log_info, logtype_afpd, "begin afp_rename:");
466 memcpy( &vid, ibuf, sizeof( vid ));
467 ibuf += sizeof( vid );
468 if (NULL == ( vol = getvolbyvid( vid )) ) {
469 return( AFPERR_PARAM );
472 if (vol->v_flags & AFPVOL_RO)
475 memcpy( &did, ibuf, sizeof( did ));
476 ibuf += sizeof( did );
477 if (NULL == ( sdir = dirlookup( vol, did )) ) {
481 /* source pathname */
482 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
487 newname = obj->newtmp;
488 oldname = obj->oldtmp;
489 if ( *path->m_name != '\0' ) {
490 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
493 if ( sdir->d_parent == NULL ) { /* root directory */
494 return( AFPERR_NORENAME );
496 /* move to destination dir */
497 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
501 strcpy(oldname, sdir->d_m_name);
504 /* another place where we know about the path type */
505 if ((plen = copy_path_name(newname, ibuf)) < 0) {
506 return( AFPERR_PARAM );
510 return AFP_OK; /* newname == oldname same dir */
513 rc = moveandrename(vol, sdir, oldname, newname, isdir);
515 if ( rc == AFP_OK ) {
516 setvoltime(obj, vol );
520 LOG(log_info, logtype_afpd, "end afp_rename:");
526 /* ------------------------------- */
527 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
530 int ibuflen, *rbuflen;
540 LOG(log_info, logtype_afpd, "begin afp_delete:");
546 memcpy( &vid, ibuf, sizeof( vid ));
547 ibuf += sizeof( vid );
548 if (NULL == ( vol = getvolbyvid( vid )) ) {
549 return( AFPERR_PARAM );
552 if (vol->v_flags & AFPVOL_RO)
555 memcpy( &did, ibuf, sizeof( did ));
556 ibuf += sizeof( int );
557 if (NULL == ( dir = dirlookup( vol, did )) ) {
561 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
565 upath = s_path->u_name;
566 if ( *s_path->m_name == '\0' ) {
567 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
568 } else if (of_findname(s_path)) {
570 } else if (AFP_OK == (rc = deletefile( upath, 1))) {
571 #ifdef CNID_DB /* get rid of entry */
572 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
573 cnid_delete(vol->v_db, id);
576 if ( rc == AFP_OK ) {
578 setvoltime(obj, vol );
582 LOG(log_info, logtype_afpd, "end afp_delete:");
587 char *absupath( vol, dir, u )
588 const struct vol *vol;
593 static char path[ MAXPATHLEN + 1];
597 p = path + sizeof( path ) - 1;
601 strncpy( p, u, len );
602 for ( d = dir; d->d_parent; d = d->d_parent ) {
607 strncpy( p, u, len );
610 len = strlen( vol->v_path );
612 strncpy( p, vol->v_path, len );
617 char *ctoupath( vol, dir, name )
618 const struct vol *vol;
622 return absupath(vol, dir, mtoupath(vol, name));
625 /* ------------------------- */
626 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
629 int ibuflen, *rbuflen;
632 struct dir *sdir, *ddir;
634 char *oldname, *newname;
642 #endif /* DROPKLUDGE */
645 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
651 memcpy( &vid, ibuf, sizeof( vid ));
652 ibuf += sizeof( vid );
653 if (NULL == ( vol = getvolbyvid( vid )) ) {
654 return( AFPERR_PARAM );
657 if (vol->v_flags & AFPVOL_RO)
660 /* source did followed by dest did */
661 memcpy( &did, ibuf, sizeof( did ));
662 ibuf += sizeof( int );
663 if (NULL == ( sdir = dirlookup( vol, did )) ) {
664 return( AFPERR_PARAM );
667 memcpy( &did, ibuf, sizeof( did ));
668 ibuf += sizeof( int );
670 /* source pathname */
671 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
672 return( AFPERR_NOOBJ );
676 newname = obj->newtmp;
677 oldname = obj->oldtmp;
678 if ( *path->m_name != '\0' ) {
679 /* not a directory */
680 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
683 strcpy(oldname, sdir->d_m_name);
686 /* get the destination directory */
687 if (NULL == ( ddir = dirlookup( vol, did )) ) {
688 return( AFPERR_PARAM );
690 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
691 return( AFPERR_NOOBJ );
693 if ( *path->m_name != '\0' ) {
694 return( AFPERR_BADTYPE );
697 /* one more place where we know about path type */
698 if ((plen = copy_path_name(newname, ibuf)) < 0) {
699 return( AFPERR_PARAM );
703 strcpy(newname, oldname);
706 rc = moveandrename(vol, sdir, oldname, newname, isdir);
708 if ( rc == AFP_OK ) {
709 char *upath = mtoupath(vol, newname);
714 if (vol->v_flags & AFPVOL_DROPBOX) {
715 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
720 #endif /* DROPKLUDGE */
722 int admode = ad_mode("", 0777);
724 setfilmode(upath, admode, NULL);
725 setfilmode(ad_path( upath, ADFLAGS_HF ), ad_hf_mode(admode), NULL);
727 setvoltime(obj, vol );
731 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
737 int veto_file(const char*veto_str, const char*path)
738 /* given a veto_str like "abc/zxc/" and path "abc", return 1
739 * veto_str should be '/' delimited
740 * if path matches any one of the veto_str elements exactly, then 1 is returned
741 * otherwise, 0 is returned.
744 int i; /* index to veto_str */
745 int j; /* index to path */
747 if ((veto_str == NULL) || (path == NULL))
751 LOG(log_debug, logtype_afpd, "veto_file \"%s\", \"%s\"", veto_str, path);
754 for(i=0, j=0; veto_str[i] != '\0'; i++) {
755 if (veto_str[i] == '/') {
756 if ((j>0) && (path[j] == '\0'))
760 if (veto_str[i] != path[j]) {
761 while ((veto_str[i] != '/')
762 && (veto_str[i] != '\0'))