2 * $Id: filedir.c,v 1.59 2009-10-16 00:15:53 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>
36 #include <atalk/adouble.h>
37 #include <atalk/vfs.h>
38 #include <atalk/afp.h>
39 #include <atalk/util.h>
40 #include <atalk/cnid.h>
41 #include <atalk/logger.h>
42 #include <atalk/unix.h>
44 #include "directory.h"
54 int matchfile2dirperms(
55 /* Since it's kinda' big; I decided against an
60 /* The below code changes the way file ownership is determined in the name of
61 fixing dropboxes. It has known security problem. See the netatalk FAQ for
70 LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
73 if (stat(upath, &st ) < 0) {
74 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
78 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
79 /* FIXME dirsearch doesn't move cwd to did ! */
80 if (( dir = dirlookup( vol, did )) == NULL ) {
81 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
84 else if (stat(".", &sb) < 0) {
85 LOG(log_error, logtype_afpd,
86 "matchfile2dirperms: Error checking directory \"%s\": %s",
87 dir->d_m_name, strerror(errno));
92 if ( uid != sb.st_uid )
95 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
97 LOG(log_error, logtype_afpd,
98 "matchfile2dirperms(%s): Error changing owner/gid: %s",
99 upath, strerror(errno));
102 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
104 LOG(log_error, logtype_afpd,
105 "matchfile2dirperms(%s): Error adding file read permissions: %s",
106 upath, strerror(errno));
109 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
111 LOG(log_error, logtype_afpd,
112 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
113 adpath, strerror(errno));
116 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
118 LOG(log_error, logtype_afpd,
119 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
120 adpath, strerror(errno));
125 } /* end else if stat success */
128 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
134 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *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 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
158 return( AFPERR_ACCESS );
161 memcpy( &did, ibuf, sizeof( did ));
162 ibuf += sizeof( did );
164 if (NULL == ( dir = dirlookup( vol, did )) ) {
168 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
169 fbitmap = ntohs( fbitmap );
170 ibuf += sizeof( fbitmap );
171 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
172 dbitmap = ntohs( dbitmap );
173 ibuf += sizeof( dbitmap );
175 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
176 return get_afp_errno(AFPERR_NOOBJ);
180 if (!s_path->st_valid) {
181 /* it's a dir and it should be there
182 * because we chdir in it in cname or
183 * it's curdir (maybe deleted, but then we can't know).
184 * So we need to try harder.
186 of_statdir(vol, s_path);
188 if ( s_path->st_errno != 0 ) {
189 return( AFPERR_NOOBJ );
193 if (S_ISDIR(st->st_mode)) {
199 ret = getdirparams(vol, dbitmap, s_path, dir,
200 rbuf + 3 * sizeof( u_int16_t ), &buflen );
204 /* this is a directory */
205 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
207 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
208 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
212 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
214 *rbuflen = buflen + 3 * sizeof( u_int16_t );
215 fbitmap = htons( fbitmap );
216 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
217 rbuf += sizeof( fbitmap );
218 dbitmap = htons( dbitmap );
219 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
220 rbuf += sizeof( dbitmap ) + sizeof( u_char );
224 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
230 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *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
275 of_statdir(vol, path);
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->vfs->vfs_validupath(vol, name)) {
317 LOG(log_info, logtype_afpd, "check_name: illegal name: '%s'", name);
321 /* check for vetoed filenames */
322 if (veto_file(vol->v_veto, name))
327 /* -------------------------
328 move and rename sdir:oldname to curdir:newname in volume vol
330 special care is needed for lock
332 static int moveandrename(const struct vol *vol, struct dir *sdir, char *oldname, char *newname, int isdir)
337 struct stat *st, nst;
341 struct ofork *opened = NULL;
345 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
350 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
352 return AFPERR_PARAM; /* can't convert */
354 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
355 p = ctoupath( vol, sdir, oldname );
357 return AFPERR_PARAM; /* pathname too long */
361 if ((opened = of_findname(&path))) {
362 /* reuse struct adouble so it won't break locks */
367 id = sdir->d_did; /* we already have the CNID */
368 p = ctoupath( vol, sdir->d_parent, oldname );
372 adflags = ADFLAGS_DIR;
375 * p now points to the full pathname of the source fs object.
377 * we are in the dest folder so we need to use p for ad_open
380 if (!ad_metadata(p, adflags, adp)) {
383 ad_getattr(adp, &bshort);
384 ad_close_metadata( adp);
385 if ((bshort & htons(ATTRBIT_NORENAME)))
386 return(AFPERR_OLOCK);
389 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
394 if (0 != (rc = check_name(vol, upath))) {
398 /* source == destination. we just silently accept this. */
399 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
400 if (strcmp(oldname, newname) == 0)
403 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
404 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
405 /* not the same file */
410 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
415 path.st_errno = errno;
416 if (of_findname(&path)) {
417 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
419 rc = renamefile(vol, p, upath, newname, adp );
421 of_rename(vol, opened, sdir, oldname, curdir, newname);
424 rc = renamedir(vol, p, upath, sdir, curdir, newname);
426 if ( rc == AFP_OK && id ) {
427 /* renaming may have moved the file/dir across a filesystem */
428 if (stat(upath, st) < 0)
431 /* fix up the catalog entry */
432 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
438 /* -------------------------------------------- */
439 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
443 char *oldname, *newname;
451 LOG(log_info, logtype_afpd, "begin afp_rename:");
457 memcpy( &vid, ibuf, sizeof( vid ));
458 ibuf += sizeof( vid );
459 if (NULL == ( vol = getvolbyvid( vid )) ) {
460 return( AFPERR_PARAM );
463 if (vol->v_flags & AFPVOL_RO)
466 memcpy( &did, ibuf, sizeof( did ));
467 ibuf += sizeof( did );
468 if (NULL == ( sdir = dirlookup( vol, did )) ) {
472 /* source pathname */
473 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
474 return get_afp_errno(AFPERR_NOOBJ);
478 newname = obj->newtmp;
479 oldname = obj->oldtmp;
480 isdir = path_isadir(path);
481 if ( *path->m_name != '\0' ) {
482 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
484 /* curdir parent dir, need to move sdir back */
489 if ( sdir->d_parent == NULL ) { /* root directory */
490 return( AFPERR_NORENAME );
492 /* move to destination dir */
493 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
496 strcpy(oldname, sdir->d_m_name);
499 /* another place where we know about the path type */
500 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
501 return( AFPERR_PARAM );
505 return AFP_OK; /* newname == oldname same dir */
508 rc = moveandrename(vol, sdir, oldname, newname, isdir);
510 if ( rc == AFP_OK ) {
511 setvoltime(obj, vol );
515 LOG(log_info, logtype_afpd, "end afp_rename:");
521 /* ------------------------------- */
522 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
532 LOG(log_info, logtype_afpd, "begin afp_delete:");
538 memcpy( &vid, ibuf, sizeof( vid ));
539 ibuf += sizeof( vid );
540 if (NULL == ( vol = getvolbyvid( vid )) ) {
541 return( AFPERR_PARAM );
544 if (vol->v_flags & AFPVOL_RO)
547 memcpy( &did, ibuf, sizeof( did ));
548 ibuf += sizeof( int );
549 if (NULL == ( dir = dirlookup( vol, did )) ) {
553 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
554 return get_afp_errno(AFPERR_NOOBJ);
557 upath = s_path->u_name;
558 if ( path_isadir( s_path) ) {
559 if (*s_path->m_name != '\0') {
563 rc = deletecurdir( vol);
565 } else if (of_findname(s_path)) {
568 rc = deletefile(vol, upath, 1);
570 if ( rc == AFP_OK ) {
572 setvoltime(obj, vol );
576 LOG(log_info, logtype_afpd, "end afp_delete:");
581 /* ------------------------ */
582 char *absupath(const struct vol *vol, struct dir *dir, char *u)
585 static char path[ MAXPATHLEN + 1];
592 p = path + sizeof( path ) - 1;
597 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
600 if (p -len -1 < path) {
602 rather rare so LOG error and/or client message ?
610 len = strlen( vol->v_path );
611 if (p -len -1 < path) {
616 memcpy( p, vol->v_path, len );
621 /* ------------------------
622 * FIXME dir could be NULL
624 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
626 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
629 /* ------------------------- */
630 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
633 struct dir *sdir, *ddir;
635 char *oldname, *newname;
644 #endif /* DROPKLUDGE */
647 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
653 memcpy( &vid, ibuf, sizeof( vid ));
654 ibuf += sizeof( vid );
655 if (NULL == ( vol = getvolbyvid( vid )) ) {
656 return( AFPERR_PARAM );
659 if (vol->v_flags & AFPVOL_RO)
662 /* source did followed by dest did */
663 memcpy( &did, ibuf, sizeof( did ));
664 ibuf += sizeof( int );
665 if (NULL == ( sdir = dirlookup( vol, did )) ) {
666 return afp_errno; /* was AFPERR_PARAM */
669 memcpy( &did, ibuf, sizeof( did ));
670 ibuf += sizeof( int );
672 /* source pathname */
673 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
674 return get_afp_errno(AFPERR_NOOBJ);
678 newname = obj->newtmp;
679 oldname = obj->oldtmp;
681 isdir = path_isadir(path);
682 if ( *path->m_name != '\0' ) {
686 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
688 strcpy(oldname, sdir->d_m_name);
691 /* get the destination directory */
692 if (NULL == ( ddir = dirlookup( vol, did )) ) {
693 return afp_errno; /* was AFPERR_PARAM */
695 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
696 return( AFPERR_NOOBJ );
698 pdid = curdir->d_did;
699 if ( *path->m_name != '\0' ) {
700 return path_error(path, AFPERR_NOOBJ);
703 /* one more place where we know about path type */
704 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
705 return( AFPERR_PARAM );
709 strcpy(newname, oldname);
712 rc = moveandrename(vol, sdir, oldname, newname, isdir);
714 if ( rc == AFP_OK ) {
715 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
723 if (vol->v_flags & AFPVOL_DROPBOX) {
724 /* FIXME did is not always the source id */
725 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
730 #endif /* DROPKLUDGE */
731 /* if unix priv don't try to match perm with dest folder */
732 if (!isdir && !vol_unix_priv(vol)) {
733 int admode = ad_mode("", 0777) | vol->v_fperm;
735 setfilmode(upath, admode, NULL, vol->v_umask);
736 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
738 setvoltime(obj, vol );
742 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
748 int veto_file(const char*veto_str, const char*path)
749 /* given a veto_str like "abc/zxc/" and path "abc", return 1
750 * veto_str should be '/' delimited
751 * if path matches any one of the veto_str elements exactly, then 1 is returned
752 * otherwise, 0 is returned.
755 int i; /* index to veto_str */
756 int j; /* index to path */
758 if ((veto_str == NULL) || (path == NULL))
761 for(i=0, j=0; veto_str[i] != '\0'; i++) {
762 if (veto_str[i] == '/') {
763 if ((j>0) && (path[j] == '\0')) {
764 LOG(log_info, logtype_afpd, "vetoed file:'%s'", path);
769 if (veto_str[i] != path[j]) {
770 while ((veto_str[i] != '/')
771 && (veto_str[i] != '\0'))