2 * $Id: filedir.c,v 1.58 2009-10-15 10:43:13 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>
43 #include "directory.h"
53 int matchfile2dirperms(
54 /* Since it's kinda' big; I decided against an
59 /* The below code changes the way file ownership is determined in the name of
60 fixing dropboxes. It has known security problem. See the netatalk FAQ for
69 LOG(log_info, logtype_afpd, "begin matchfile2dirperms:");
72 if (stat(upath, &st ) < 0) {
73 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
77 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
78 /* FIXME dirsearch doesn't move cwd to did ! */
79 if (( dir = dirlookup( vol, did )) == NULL ) {
80 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
83 else if (stat(".", &sb) < 0) {
84 LOG(log_error, logtype_afpd,
85 "matchfile2dirperms: Error checking directory \"%s\": %s",
86 dir->d_m_name, strerror(errno));
91 if ( uid != sb.st_uid )
94 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
96 LOG(log_error, logtype_afpd,
97 "matchfile2dirperms(%s): Error changing owner/gid: %s",
98 upath, strerror(errno));
101 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
103 LOG(log_error, logtype_afpd,
104 "matchfile2dirperms(%s): Error adding file read permissions: %s",
105 upath, strerror(errno));
108 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
110 LOG(log_error, logtype_afpd,
111 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
112 adpath, strerror(errno));
115 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
117 LOG(log_error, logtype_afpd,
118 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
119 adpath, strerror(errno));
124 } /* end else if stat success */
127 LOG(log_info, logtype_afpd, "end matchfile2dirperms:");
133 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
141 u_int16_t fbitmap, dbitmap, vid;
145 LOG(log_info, logtype_afpd, "begin afp_getfildirparams:");
151 memcpy( &vid, ibuf, sizeof( vid ));
152 ibuf += sizeof( vid );
153 if (NULL == ( vol = getvolbyvid( vid )) ) {
154 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
157 return( AFPERR_ACCESS );
160 memcpy( &did, ibuf, sizeof( did ));
161 ibuf += sizeof( did );
163 if (NULL == ( dir = dirlookup( vol, did )) ) {
167 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
168 fbitmap = ntohs( fbitmap );
169 ibuf += sizeof( fbitmap );
170 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
171 dbitmap = ntohs( dbitmap );
172 ibuf += sizeof( dbitmap );
174 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
175 return get_afp_errno(AFPERR_NOOBJ);
179 if (!s_path->st_valid) {
180 /* it's a dir and it should be there
181 * because we chdir in it in cname or
182 * it's curdir (maybe deleted, but then we can't know).
183 * So we need to try harder.
185 of_statdir(vol, s_path);
187 if ( s_path->st_errno != 0 ) {
188 return( AFPERR_NOOBJ );
192 if (S_ISDIR(st->st_mode)) {
198 ret = getdirparams(vol, dbitmap, s_path, dir,
199 rbuf + 3 * sizeof( u_int16_t ), &buflen );
203 /* this is a directory */
204 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
206 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
207 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
211 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
213 *rbuflen = buflen + 3 * sizeof( u_int16_t );
214 fbitmap = htons( fbitmap );
215 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
216 rbuf += sizeof( fbitmap );
217 dbitmap = htons( dbitmap );
218 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
219 rbuf += sizeof( dbitmap ) + sizeof( u_char );
223 LOG(log_info, logtype_afpd, "end afp_getfildirparams:");
229 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
235 u_int16_t vid, bitmap;
239 LOG(log_info, logtype_afpd, "begin afp_setfildirparams:");
244 memcpy( &vid, ibuf, sizeof(vid));
245 ibuf += sizeof( vid );
247 if (NULL == ( vol = getvolbyvid( vid )) ) {
248 return( AFPERR_PARAM );
251 if (vol->v_flags & AFPVOL_RO)
254 memcpy( &did, ibuf, sizeof( did));
255 ibuf += sizeof( did);
257 if (NULL == ( dir = dirlookup( vol, did )) ) {
261 memcpy( &bitmap, ibuf, sizeof( bitmap ));
262 bitmap = ntohs( bitmap );
263 ibuf += sizeof( bitmap );
265 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
266 return get_afp_errno(AFPERR_NOOBJ);
270 if (!path->st_valid) {
271 /* it's a dir and it should be there
272 * because we chdir in it in cname
274 of_statdir(vol, path);
277 if ( path->st_errno != 0 ) {
278 return( AFPERR_NOOBJ );
281 * If ibuf is odd, make it even.
283 if ((u_long)ibuf & 1 ) {
287 if (S_ISDIR(st->st_mode)) {
288 rc = setdirparams(vol, path, bitmap, ibuf );
290 rc = setfilparams(vol, path, bitmap, ibuf );
292 if ( rc == AFP_OK ) {
293 setvoltime(obj, vol );
297 LOG(log_info, logtype_afpd, "end afp_setfildirparams:");
303 /* --------------------------------------------
304 Factorise some checks on a pathname
306 int check_name(const struct vol *vol, char *name)
308 /* check for illegal characters in the unix filename */
309 if (!wincheck(vol, name))
312 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
315 if (!vol->vfs->vfs_validupath(vol, name)) {
316 LOG(log_info, logtype_afpd, "check_name: illegal name: '%s'", name);
320 /* check for vetoed filenames */
321 if (veto_file(vol->v_veto, name))
326 /* -------------------------
327 move and rename sdir:oldname to curdir:newname in volume vol
329 special care is needed for lock
331 static int moveandrename(const struct vol *vol, struct dir *sdir, char *oldname, char *newname, int isdir)
336 struct stat *st, nst;
340 struct ofork *opened = NULL;
344 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
349 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
351 return AFPERR_PARAM; /* can't convert */
353 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
354 p = ctoupath( vol, sdir, oldname );
356 return AFPERR_PARAM; /* pathname too long */
360 if ((opened = of_findname(&path))) {
361 /* reuse struct adouble so it won't break locks */
366 id = sdir->d_did; /* we already have the CNID */
367 p = ctoupath( vol, sdir->d_parent, oldname );
371 adflags = ADFLAGS_DIR;
374 * p now points to the full pathname of the source fs object.
376 * we are in the dest folder so we need to use p for ad_open
379 if (!ad_metadata(p, adflags, adp)) {
382 ad_getattr(adp, &bshort);
383 ad_close_metadata( adp);
384 if ((bshort & htons(ATTRBIT_NORENAME)))
385 return(AFPERR_OLOCK);
388 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
393 if (0 != (rc = check_name(vol, upath))) {
397 /* source == destination. we just silently accept this. */
398 if ((!isdir && curdir == sdir) || (isdir && curdir == sdir->d_parent)) {
399 if (strcmp(oldname, newname) == 0)
402 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
403 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
404 /* not the same file */
409 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
414 path.st_errno = errno;
415 if (of_findname(&path)) {
416 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
418 rc = renamefile(vol, p, upath, newname, adp );
420 of_rename(vol, opened, sdir, oldname, curdir, newname);
423 rc = renamedir(vol, p, upath, sdir, curdir, newname);
425 if ( rc == AFP_OK && id ) {
426 /* renaming may have moved the file/dir across a filesystem */
427 if (stat(upath, st) < 0)
430 /* fix up the catalog entry */
431 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
437 /* -------------------------------------------- */
438 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
442 char *oldname, *newname;
450 LOG(log_info, logtype_afpd, "begin afp_rename:");
456 memcpy( &vid, ibuf, sizeof( vid ));
457 ibuf += sizeof( vid );
458 if (NULL == ( vol = getvolbyvid( vid )) ) {
459 return( AFPERR_PARAM );
462 if (vol->v_flags & AFPVOL_RO)
465 memcpy( &did, ibuf, sizeof( did ));
466 ibuf += sizeof( did );
467 if (NULL == ( sdir = dirlookup( vol, did )) ) {
471 /* source pathname */
472 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
473 return get_afp_errno(AFPERR_NOOBJ);
477 newname = obj->newtmp;
478 oldname = obj->oldtmp;
479 isdir = path_isadir(path);
480 if ( *path->m_name != '\0' ) {
481 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
483 /* curdir parent dir, need to move sdir back */
488 if ( sdir->d_parent == NULL ) { /* root directory */
489 return( AFPERR_NORENAME );
491 /* move to destination dir */
492 if ( movecwd( vol, sdir->d_parent ) < 0 ) {
495 strcpy(oldname, sdir->d_m_name);
498 /* another place where we know about the path type */
499 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
500 return( AFPERR_PARAM );
504 return AFP_OK; /* newname == oldname same dir */
507 rc = moveandrename(vol, sdir, oldname, newname, isdir);
509 if ( rc == AFP_OK ) {
510 setvoltime(obj, vol );
514 LOG(log_info, logtype_afpd, "end afp_rename:");
520 /* ------------------------------- */
521 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
531 LOG(log_info, logtype_afpd, "begin afp_delete:");
537 memcpy( &vid, ibuf, sizeof( vid ));
538 ibuf += sizeof( vid );
539 if (NULL == ( vol = getvolbyvid( vid )) ) {
540 return( AFPERR_PARAM );
543 if (vol->v_flags & AFPVOL_RO)
546 memcpy( &did, ibuf, sizeof( did ));
547 ibuf += sizeof( int );
548 if (NULL == ( dir = dirlookup( vol, did )) ) {
552 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
553 return get_afp_errno(AFPERR_NOOBJ);
556 upath = s_path->u_name;
557 if ( path_isadir( s_path) ) {
558 if (*s_path->m_name != '\0') {
562 rc = deletecurdir( vol);
564 } else if (of_findname(s_path)) {
567 rc = deletefile(vol, upath, 1);
569 if ( rc == AFP_OK ) {
571 setvoltime(obj, vol );
575 LOG(log_info, logtype_afpd, "end afp_delete:");
580 /* ------------------------ */
581 char *absupath(const struct vol *vol, struct dir *dir, char *u)
584 static char path[ MAXPATHLEN + 1];
591 p = path + sizeof( path ) - 1;
596 if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
599 if (p -len -1 < path) {
601 rather rare so LOG error and/or client message ?
609 len = strlen( vol->v_path );
610 if (p -len -1 < path) {
615 memcpy( p, vol->v_path, len );
620 /* ------------------------
621 * FIXME dir could be NULL
623 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
625 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
628 /* ------------------------- */
629 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
632 struct dir *sdir, *ddir;
634 char *oldname, *newname;
643 #endif /* DROPKLUDGE */
646 LOG(log_info, logtype_afpd, "begin afp_moveandrename:");
652 memcpy( &vid, ibuf, sizeof( vid ));
653 ibuf += sizeof( vid );
654 if (NULL == ( vol = getvolbyvid( vid )) ) {
655 return( AFPERR_PARAM );
658 if (vol->v_flags & AFPVOL_RO)
661 /* source did followed by dest did */
662 memcpy( &did, ibuf, sizeof( did ));
663 ibuf += sizeof( int );
664 if (NULL == ( sdir = dirlookup( vol, did )) ) {
665 return afp_errno; /* was AFPERR_PARAM */
668 memcpy( &did, ibuf, sizeof( did ));
669 ibuf += sizeof( int );
671 /* source pathname */
672 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
673 return get_afp_errno(AFPERR_NOOBJ);
677 newname = obj->newtmp;
678 oldname = obj->oldtmp;
680 isdir = path_isadir(path);
681 if ( *path->m_name != '\0' ) {
685 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
687 strcpy(oldname, sdir->d_m_name);
690 /* get the destination directory */
691 if (NULL == ( ddir = dirlookup( vol, did )) ) {
692 return afp_errno; /* was AFPERR_PARAM */
694 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
695 return( AFPERR_NOOBJ );
697 pdid = curdir->d_did;
698 if ( *path->m_name != '\0' ) {
699 return path_error(path, AFPERR_NOOBJ);
702 /* one more place where we know about path type */
703 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
704 return( AFPERR_PARAM );
708 strcpy(newname, oldname);
711 rc = moveandrename(vol, sdir, oldname, newname, isdir);
713 if ( rc == AFP_OK ) {
714 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
722 if (vol->v_flags & AFPVOL_DROPBOX) {
723 /* FIXME did is not always the source id */
724 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
729 #endif /* DROPKLUDGE */
730 /* if unix priv don't try to match perm with dest folder */
731 if (!isdir && !vol_unix_priv(vol)) {
732 int admode = ad_mode("", 0777) | vol->v_fperm;
734 setfilmode(upath, admode, NULL, vol->v_umask);
735 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
737 setvoltime(obj, vol );
741 LOG(log_info, logtype_afpd, "end afp_moveandrename:");
747 int veto_file(const char*veto_str, const char*path)
748 /* given a veto_str like "abc/zxc/" and path "abc", return 1
749 * veto_str should be '/' delimited
750 * if path matches any one of the veto_str elements exactly, then 1 is returned
751 * otherwise, 0 is returned.
754 int i; /* index to veto_str */
755 int j; /* index to path */
757 if ((veto_str == NULL) || (path == NULL))
760 for(i=0, j=0; veto_str[i] != '\0'; i++) {
761 if (veto_str[i] == '/') {
762 if ((j>0) && (path[j] == '\0')) {
763 LOG(log_info, logtype_afpd, "vetoed file:'%s'", path);
768 if (veto_str[i] != path[j]) {
769 while ((veto_str[i] != '/')
770 && (veto_str[i] != '\0'))