2 * $Id: filedir.c,v 1.69.2.2 2010-02-01 16:13:52 franklahm 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 */
33 #include <sys/param.h>
35 #include <atalk/adouble.h>
36 #include <atalk/vfs.h>
37 #include <atalk/afp.h>
38 #include <atalk/util.h>
39 #include <atalk/cnid.h>
40 #include <atalk/logger.h>
41 #include <atalk/unix.h>
42 #include <atalk/bstrlib.h>
43 #include <atalk/bstradd.h>
45 #include "directory.h"
55 int matchfile2dirperms(
56 /* Since it's kinda' big; I decided against an
61 /* The below code changes the way file ownership is determined in the name of
62 fixing dropboxes. It has known security problem. See the netatalk FAQ for
71 LOG(log_debug9, logtype_afpd, "begin matchfile2dirperms:");
74 if (stat(upath, &st ) < 0) {
75 LOG(log_error, logtype_afpd, "Could not stat %s: %s", upath, strerror(errno));
79 adpath = vol->vfs->ad_path( upath, ADFLAGS_HF );
80 /* FIXME dirsearch doesn't move cwd to did ! */
81 if (( dir = dirlookup( vol, did )) == NULL ) {
82 LOG(log_error, logtype_afpd, "matchfile2dirperms: Unable to get directory info.");
85 else if (stat(".", &sb) < 0) {
86 LOG(log_error, logtype_afpd,
87 "matchfile2dirperms: Error checking directory \"%s\": %s",
88 dir->d_m_name, strerror(errno));
93 if ( uid != sb.st_uid )
96 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
98 LOG(log_error, logtype_afpd,
99 "matchfile2dirperms(%s): Error changing owner/gid: %s",
100 upath, strerror(errno));
103 else if (chmod(upath,(st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
105 LOG(log_error, logtype_afpd,
106 "matchfile2dirperms(%s): Error adding file read permissions: %s",
107 upath, strerror(errno));
110 else if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
112 LOG(log_error, logtype_afpd,
113 "matchfile2dirperms(%s): Error changing AppleDouble owner/gid: %s",
114 adpath, strerror(errno));
117 else if (chmod(adpath, (st.st_mode&~default_options.umask)| S_IRGRP| S_IROTH) < 0)
119 LOG(log_error, logtype_afpd,
120 "matchfile2dirperms(%s): Error adding AD file read permissions: %s",
121 adpath, strerror(errno));
126 } /* end else if stat success */
129 LOG(log_debug9, logtype_afpd, "end matchfile2dirperms:");
135 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
143 u_int16_t fbitmap, dbitmap, vid;
149 memcpy( &vid, ibuf, sizeof( vid ));
150 ibuf += sizeof( vid );
151 if (NULL == ( vol = getvolbyvid( vid )) ) {
152 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
155 return( AFPERR_ACCESS );
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);
176 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
177 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
178 ntohl(curdir->d_did), cfrombstring(curdir->d_fullpath), s_path->u_name);
181 if (!s_path->st_valid) {
182 LOG(log_debug, logtype_afpd, "getfildirparams: !s_path->st_valid");
183 /* it's a dir and it should be there
184 * because we chdir in it in cname or
185 * it's curdir (maybe deleted, but then we can't know).
186 * So we need to try harder.
188 of_statdir(vol, s_path);
190 if ( s_path->st_errno != 0 ) {
191 if (afp_errno != AFPERR_ACCESS) {
192 LOG(log_debug, logtype_afpd, "getfildirparams: s_path->st_errno != 0");
193 return( AFPERR_NOOBJ );
199 if (S_ISDIR(st->st_mode)) {
205 ret = getdirparams(vol, dbitmap, s_path, dir,
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 && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
214 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
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 );
232 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
238 u_int16_t vid, bitmap;
243 memcpy( &vid, ibuf, sizeof(vid));
244 ibuf += sizeof( vid );
246 if (NULL == ( vol = getvolbyvid( vid )) ) {
247 return( AFPERR_PARAM );
250 if (vol->v_flags & AFPVOL_RO)
253 memcpy( &did, ibuf, sizeof( did));
254 ibuf += sizeof( did);
256 if (NULL == ( dir = dirlookup( vol, did )) ) {
260 memcpy( &bitmap, ibuf, sizeof( bitmap ));
261 bitmap = ntohs( bitmap );
262 ibuf += sizeof( bitmap );
264 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
265 return get_afp_errno(AFPERR_NOOBJ);
269 if (!path->st_valid) {
270 /* it's a dir and it should be there
271 * because we chdir in it in cname
273 of_statdir(vol, path);
276 if ( path->st_errno != 0 ) {
277 if (afp_errno != AFPERR_ACCESS)
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 );
299 /* --------------------------------------------
300 Factorise some checks on a pathname
302 int check_name(const struct vol *vol, char *name)
304 /* check for illegal characters in the unix filename */
305 if (!wincheck(vol, name))
308 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
311 if (!vol->vfs->vfs_validupath(vol, name)) {
312 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
316 /* check for vetoed filenames */
317 if (veto_file(vol->v_veto, name))
322 /* -------------------------
323 move and rename sdir:oldname to curdir:newname in volume vol
325 special care is needed for lock
327 static int moveandrename(const struct vol *vol, struct dir *sdir, char *oldname, char *newname, int isdir)
332 struct stat *st, nst;
336 struct ofork *opened = NULL;
340 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
345 p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding());
347 return AFPERR_PARAM; /* can't convert */
349 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
350 p = ctoupath( vol, sdir, oldname );
352 return AFPERR_PARAM; /* pathname too long */
356 if ((opened = of_findname(&path))) {
357 /* reuse struct adouble so it won't break locks */
362 id = sdir->d_did; /* we already have the CNID */
363 p = ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname );
367 adflags = ADFLAGS_DIR;
370 * p now points to the full pathname of the source fs object.
372 * we are in the dest folder so we need to use p for ad_open
375 if (!ad_metadata(p, adflags, adp)) {
378 ad_getattr(adp, &bshort);
379 ad_close_metadata( adp);
380 if ((bshort & htons(ATTRBIT_NORENAME)))
381 return(AFPERR_OLOCK);
384 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
389 if (0 != (rc = check_name(vol, upath))) {
393 /* source == destination. we just silently accept this. */
394 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
395 if (strcmp(oldname, newname) == 0)
398 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
399 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
400 /* not the same file */
405 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0)
410 path.st_errno = errno;
411 if (of_findname(&path)) {
412 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
414 rc = renamefile(vol, p, upath, newname, adp );
416 of_rename(vol, opened, sdir, oldname, curdir, newname);
419 rc = renamedir(vol, p, upath, sdir, curdir, newname);
421 if ( rc == AFP_OK && id ) {
422 /* renaming may have moved the file/dir across a filesystem */
423 if (stat(upath, st) < 0)
426 /* fix up the catalog entry */
427 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
433 /* -------------------------------------------- */
434 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
438 char *oldname, *newname;
449 memcpy( &vid, ibuf, sizeof( vid ));
450 ibuf += sizeof( vid );
451 if (NULL == ( vol = getvolbyvid( vid )) ) {
452 return( AFPERR_PARAM );
455 if (vol->v_flags & AFPVOL_RO)
458 memcpy( &did, ibuf, sizeof( did ));
459 ibuf += sizeof( did );
460 if (NULL == ( sdir = dirlookup( vol, did )) ) {
464 /* source pathname */
465 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
466 return get_afp_errno(AFPERR_NOOBJ);
470 newname = obj->newtmp;
471 oldname = obj->oldtmp;
472 isdir = path_isadir(path);
473 if ( *path->m_name != '\0' ) {
474 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
476 /* curdir parent dir, need to move sdir back */
481 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
482 return( AFPERR_NORENAME );
484 /* move to destination dir */
485 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
488 memcpy(oldname, cfrombstring(sdir->d_m_name), blength(sdir->d_m_name) +1);
491 /* another place where we know about the path type */
492 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
493 return( AFPERR_PARAM );
497 return AFP_OK; /* newname == oldname same dir */
500 rc = moveandrename(vol, sdir, oldname, newname, isdir);
502 if ( rc == AFP_OK ) {
503 setvoltime(obj, vol );
509 /* ------------------------------- */
510 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
522 memcpy( &vid, ibuf, sizeof( vid ));
523 ibuf += sizeof( vid );
524 if (NULL == ( vol = getvolbyvid( vid )) ) {
525 return( AFPERR_PARAM );
528 if (vol->v_flags & AFPVOL_RO)
531 memcpy( &did, ibuf, sizeof( did ));
532 ibuf += sizeof( int );
534 if (NULL == ( dir = dirlookup( vol, did )) ) {
538 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
539 return get_afp_errno(AFPERR_NOOBJ);
542 upath = s_path->u_name;
543 if ( path_isadir( s_path) ) {
544 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
547 rc = deletecurdir( vol);
548 } else if (of_findname(s_path)) {
551 /* it's a file st_valid should always be true
552 * only test for ENOENT because EACCES needs
553 * to read meta data in deletefile
555 if (s_path->st_valid && s_path->st_errno == ENOENT) {
559 rc = deletefile(vol, upath, 1);
562 if ( rc == AFP_OK ) {
564 setvoltime(obj, vol );
569 /* ------------------------ */
570 char *absupath(const struct vol *vol, struct dir *dir, char *u)
572 static char pathbuf[MAXPATHLEN + 1];
575 if (u == NULL || dir == NULL || vol == NULL)
578 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
580 if (bcatcstr(path, "/") != BSTR_OK)
582 if (bcatcstr(path, u) != BSTR_OK)
584 if (path->slen > MAXPATHLEN)
587 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstring(path));
589 strncpy(pathbuf, cfrombstring(path), blength(path) + 1);
595 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
597 if (vol == NULL || dir == NULL || name == NULL)
599 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
602 /* ------------------------- */
603 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
606 struct dir *sdir, *ddir;
608 char *oldname, *newname;
617 #endif /* DROPKLUDGE */
622 memcpy( &vid, ibuf, sizeof( vid ));
623 ibuf += sizeof( vid );
624 if (NULL == ( vol = getvolbyvid( vid )) ) {
625 return( AFPERR_PARAM );
628 if (vol->v_flags & AFPVOL_RO)
631 /* source did followed by dest did */
632 memcpy( &did, ibuf, sizeof( did ));
633 ibuf += sizeof( int );
634 if (NULL == ( sdir = dirlookup( vol, did )) ) {
635 return afp_errno; /* was AFPERR_PARAM */
638 memcpy( &did, ibuf, sizeof( did ));
639 ibuf += sizeof( int );
641 /* source pathname */
642 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
643 return get_afp_errno(AFPERR_NOOBJ);
647 newname = obj->newtmp;
648 oldname = obj->oldtmp;
650 isdir = path_isadir(path);
651 if ( *path->m_name != '\0' ) {
655 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
657 memcpy(oldname, cfrombstring(sdir->d_m_name), blength(sdir->d_m_name) + 1);
660 /* get the destination directory */
661 if (NULL == ( ddir = dirlookup( vol, did )) ) {
662 return afp_errno; /* was AFPERR_PARAM */
664 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
665 return( AFPERR_NOOBJ );
667 pdid = curdir->d_did;
668 if ( *path->m_name != '\0' ) {
669 return path_error(path, AFPERR_NOOBJ);
672 /* one more place where we know about path type */
673 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
674 return( AFPERR_PARAM );
678 strcpy(newname, oldname);
681 LOG(log_debug, logtype_afpd, "afp_move: {oldname:'%s', newname:'%s', isdir:%u}",
682 oldname, newname, isdir);
683 rc = moveandrename(vol, sdir, oldname, newname, isdir);
685 if ( rc == AFP_OK ) {
686 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
694 if (vol->v_flags & AFPVOL_DROPBOX) {
695 /* FIXME did is not always the source id */
696 if ((retvalue=matchfile2dirperms (upath, vol, did)) != AFP_OK) {
701 #endif /* DROPKLUDGE */
702 /* if unix priv don't try to match perm with dest folder */
703 if (!isdir && !vol_unix_priv(vol)) {
704 int admode = ad_mode("", 0777) | vol->v_fperm;
706 setfilmode(upath, admode, NULL, vol->v_umask);
707 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
709 setvoltime(obj, vol );
715 int veto_file(const char*veto_str, const char*path)
716 /* given a veto_str like "abc/zxc/" and path "abc", return 1
717 * veto_str should be '/' delimited
718 * if path matches any one of the veto_str elements exactly, then 1 is returned
719 * otherwise, 0 is returned.
722 int i; /* index to veto_str */
723 int j; /* index to path */
725 if ((veto_str == NULL) || (path == NULL))
728 for(i=0, j=0; veto_str[i] != '\0'; i++) {
729 if (veto_str[i] == '/') {
730 if ((j>0) && (path[j] == '\0')) {
731 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
736 if (veto_str[i] != path[j]) {
737 while ((veto_str[i] != '/')
738 && (veto_str[i] != '\0'))