2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
14 #include <sys/param.h>
16 #include <atalk/adouble.h>
17 #include <atalk/vfs.h>
18 #include <atalk/afp.h>
19 #include <atalk/util.h>
20 #include <atalk/cnid.h>
21 #include <atalk/logger.h>
22 #include <atalk/unix.h>
23 #include <atalk/bstrlib.h>
24 #include <atalk/bstradd.h>
25 #include <atalk/acl.h>
26 #include <atalk/globals.h>
27 #include <atalk/fce_api.h>
28 #include <atalk/netatalk_conf.h>
30 #include "directory.h"
39 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
47 uint16_t fbitmap, dbitmap, vid;
53 memcpy( &vid, ibuf, sizeof( vid ));
54 ibuf += sizeof( vid );
55 if (NULL == ( vol = getvolbyvid( vid )) ) {
56 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
59 return( AFPERR_ACCESS );
62 memcpy( &did, ibuf, sizeof( did ));
63 ibuf += sizeof( did );
65 if (NULL == ( dir = dirlookup( vol, did )) ) {
69 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
70 fbitmap = ntohs( fbitmap );
71 ibuf += sizeof( fbitmap );
72 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
73 dbitmap = ntohs( dbitmap );
74 ibuf += sizeof( dbitmap );
76 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
77 return get_afp_errno(AFPERR_NOOBJ);
80 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
81 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
82 ntohl(curdir->d_did), cfrombstr(curdir->d_fullpath), s_path->u_name);
85 if (!s_path->st_valid) {
86 /* it's a dir and it should be there
87 * because we chdir in it in cname or
88 * it's curdir (maybe deleted, but then we can't know).
89 * So we need to try harder.
91 of_statdir(vol, s_path);
93 if ( s_path->st_errno != 0 ) {
94 if (afp_errno != AFPERR_ACCESS) {
95 return( AFPERR_NOOBJ );
101 if (S_ISDIR(st->st_mode)) {
107 ret = getdirparams(obj, vol, dbitmap, s_path, dir,
108 rbuf + 3 * sizeof( uint16_t ), &buflen );
112 /* this is a directory */
113 *(rbuf + 2 * sizeof( uint16_t )) = (char) FILDIRBIT_ISDIR;
115 if (fbitmap && AFP_OK != (ret = getfilparams(obj, vol, fbitmap, s_path, curdir,
116 rbuf + 3 * sizeof( uint16_t ), &buflen, 0)) ) {
120 *(rbuf + 2 * sizeof( uint16_t )) = FILDIRBIT_ISFILE;
122 *rbuflen = buflen + 3 * sizeof( uint16_t );
123 fbitmap = htons( fbitmap );
124 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
125 rbuf += sizeof( fbitmap );
126 dbitmap = htons( dbitmap );
127 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
128 rbuf += sizeof( dbitmap ) + sizeof( u_char );
134 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
140 uint16_t vid, bitmap;
145 memcpy( &vid, ibuf, sizeof(vid));
146 ibuf += sizeof( vid );
148 if (NULL == ( vol = getvolbyvid( vid )) ) {
149 return( AFPERR_PARAM );
152 if (vol->v_flags & AFPVOL_RO)
155 memcpy( &did, ibuf, sizeof( did));
156 ibuf += sizeof( did);
158 if (NULL == ( dir = dirlookup( vol, did )) ) {
162 memcpy( &bitmap, ibuf, sizeof( bitmap ));
163 bitmap = ntohs( bitmap );
164 ibuf += sizeof( bitmap );
166 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
167 return get_afp_errno(AFPERR_NOOBJ);
171 if (!path->st_valid) {
172 /* it's a dir and it should be there
173 * because we chdir in it in cname
175 of_statdir(vol, path);
178 if ( path->st_errno != 0 ) {
179 if (afp_errno != AFPERR_ACCESS)
180 return( AFPERR_NOOBJ );
183 * If ibuf is odd, make it even.
185 if ((u_long)ibuf & 1 ) {
189 if (S_ISDIR(st->st_mode)) {
190 rc = setdirparams(vol, path, bitmap, ibuf );
192 rc = setfilparams(obj, vol, path, bitmap, ibuf );
194 if ( rc == AFP_OK ) {
195 setvoltime(obj, vol );
201 /* --------------------------------------------
202 Factorise some checks on a pathname
204 int check_name(const struct vol *vol, char *name)
206 if (!vol->vfs->vfs_validupath(vol, name)) {
207 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
211 /* check for vetoed filenames */
212 if (veto_file(vol->v_veto, name))
217 /* -------------------------
218 move and rename sdir:oldname to curdir:newname in volume vol
219 special care is needed for lock
221 static int moveandrename(const struct vol *vol,
228 char *oldunixname = NULL;
231 struct stat *st, nst;
235 struct ofork *opened = NULL;
240 LOG(log_debug, logtype_afpd,
241 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
242 cfrombstr(sdir->d_u_name), oldname, newname);
249 if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding(vol->v_obj)))) == NULL)
250 return AFPERR_PARAM; /* can't convert */
251 id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
256 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
257 return AFPERR_PARAM; /* pathname too long */
258 #endif /* HAVE_ATFUNCS */
261 path.u_name = oldunixname;
264 opened = of_findnameat(sdir_fd, &path);
266 opened = of_findname(&path);
267 #endif /* HAVE_ATFUNCS */
270 /* reuse struct adouble so it won't break locks */
274 id = sdir->d_did; /* we already have the CNID */
275 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
277 adflags = ADFLAGS_DIR;
281 * oldunixname now points to either
282 * a) full pathname of the source fs object (if renameat is not available)
283 * b) the oldname (renameat is available)
284 * we are in the dest folder so we need to use
285 * a) oldunixname for ad_open
286 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
290 if ((cwd_fd = open(".", O_RDONLY)) == -1)
292 if (fchdir(sdir_fd) != 0) {
297 if (!ad_metadata(oldunixname, adflags, adp)) {
300 ad_getattr(adp, &bshort);
302 ad_close(adp, ADFLAGS_HF);
303 if ((bshort & htons(ATTRBIT_NORENAME))) {
309 if (fchdir(cwd_fd) != 0) {
310 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
316 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding(vol->v_obj)))){
322 if (0 != (rc = check_name(vol, upath))) {
326 /* source == destination. we just silently accept this. */
327 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
328 if (strcmp(oldname, newname) == 0) {
333 if (stat(upath, st) == 0) {
334 if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
335 /* not the same file */
341 } else if (stat(upath, st ) == 0) {
348 path.st_errno = errno;
349 if (of_findname(&path)) {
350 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
352 rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
354 of_rename(vol, opened, sdir, oldname, curdir, newname);
357 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
359 if ( rc == AFP_OK && id ) {
360 /* renaming may have moved the file/dir across a filesystem */
361 if (stat(upath, st) < 0) {
366 /* Remove it from the cache */
367 struct dir *cacheddir = dircache_search_by_did(vol, id);
369 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
370 (void)dir_remove(vol, cacheddir);
373 /* Fixup adouble info */
374 if (!ad_metadata(upath, adflags, adp)) {
375 ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
377 ad_close(adp, ADFLAGS_HF);
380 /* fix up the catalog entry */
381 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
392 /* -------------------------------------------- */
393 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
397 char *oldname, *newname;
408 memcpy( &vid, ibuf, sizeof( vid ));
409 ibuf += sizeof( vid );
410 if (NULL == ( vol = getvolbyvid( vid )) ) {
411 return( AFPERR_PARAM );
414 if (vol->v_flags & AFPVOL_RO)
417 memcpy( &did, ibuf, sizeof( did ));
418 ibuf += sizeof( did );
419 if (NULL == ( sdir = dirlookup( vol, did )) ) {
423 /* source pathname */
424 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
425 return get_afp_errno(AFPERR_NOOBJ);
429 newname = obj->newtmp;
430 oldname = obj->oldtmp;
431 isdir = path_isadir(path);
432 if ( *path->m_name != '\0' ) {
433 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
435 /* curdir parent dir, need to move sdir back */
440 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
441 return( AFPERR_NORENAME );
443 /* move to destination dir */
444 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
447 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
450 /* another place where we know about the path type */
451 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
452 return( AFPERR_PARAM );
456 return AFP_OK; /* newname == oldname same dir */
459 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
460 if ( rc == AFP_OK ) {
461 setvoltime(obj, vol );
467 /* ------------------------------- */
468 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
481 memcpy( &vid, ibuf, sizeof( vid ));
482 ibuf += sizeof( vid );
483 if (NULL == ( vol = getvolbyvid( vid )) ) {
484 return( AFPERR_PARAM );
487 if (vol->v_flags & AFPVOL_RO)
490 memcpy( &did, ibuf, sizeof( did ));
491 ibuf += sizeof( int );
493 if (NULL == ( dir = dirlookup( vol, did )) ) {
497 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
498 return get_afp_errno(AFPERR_NOOBJ);
501 upath = s_path->u_name;
502 if (path_isadir(s_path)) {
503 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT) {
504 if (vol->v_adouble == AD_VERSION2)
505 return AFPERR_ACCESS;
506 if (*s_path->m_name == '\0' && curdir->d_did == DIRDID_ROOT)
507 return AFPERR_ACCESS;
508 if (rmdir(upath) != 0) {
511 return AFPERR_DIRNEMPT;
513 return AFPERR_ACCESS;
519 cnid_t delcnid = CNID_INVALID;
520 if ((deldir = dircache_search_by_name(vol, curdir, upath, strlen(upath)))) {
521 delcnid = deldir->d_did;
522 dir_remove(vol, deldir);
524 if (delcnid == CNID_INVALID)
525 delcnid = cnid_get(vol->v_cdb, curdir->d_did, upath, strlen(upath));
526 if (delcnid != CNID_INVALID)
527 cnid_delete(vol->v_cdb, delcnid);
528 fce_register_delete_dir(upath);
530 /* we have to cache this, the structs are lost in deletcurdir*/
531 /* but we need the positive returncode to send our event */
533 if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
535 if ((rc = deletecurdir(vol)) == AFP_OK)
536 fce_register_delete_dir(cfrombstr(dname));
539 } else if (of_findname(s_path)) {
542 /* it's a file st_valid should always be true
543 * only test for ENOENT because EACCES needs
544 * to read meta data in deletefile
546 if (s_path->st_valid && s_path->st_errno == ENOENT) {
549 if ((rc = deletefile(vol, -1, upath, 1)) == AFP_OK) {
550 fce_register_delete_file( s_path );
551 if (vol->v_tm_used < s_path->st.st_size)
554 vol->v_tm_used -= s_path->st.st_size;
556 struct dir *cachedfile;
557 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
558 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
559 dir_free(cachedfile);
563 if ( rc == AFP_OK ) {
565 setvoltime(obj, vol );
570 /* ------------------------ */
571 char *absupath(const struct vol *vol, struct dir *dir, char *u)
573 static char pathbuf[MAXPATHLEN + 1];
576 if (u == NULL || dir == NULL || vol == NULL)
579 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
581 if (bcatcstr(path, "/") != BSTR_OK)
583 if (bcatcstr(path, u) != BSTR_OK)
585 if (path->slen > MAXPATHLEN)
588 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
590 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
596 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
598 if (vol == NULL || dir == NULL || name == NULL)
600 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding(vol->v_obj)));
603 /* ------------------------- */
604 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
607 struct dir *sdir, *ddir;
609 char *oldname, *newname;
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, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
661 if ((sdir_fd = open(".", O_RDONLY)) == -1)
665 /* get the destination directory */
666 if (NULL == ( ddir = dirlookup( vol, did )) ) {
667 rc = afp_errno; /* was AFPERR_PARAM */
670 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
674 pdid = curdir->d_did;
675 if ( *path->m_name != '\0' ) {
676 rc = path_error(path, AFPERR_NOOBJ);
680 /* one more place where we know about path type */
681 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
687 strcpy(newname, oldname);
690 /* This does the work */
691 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
692 oldname, newname, isdir);
693 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
695 if ( rc == AFP_OK ) {
696 char *upath = mtoupath(vol, newname, pdid, utf8_encoding(obj));
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 );
721 int veto_file(const char*veto_str, const char*path)
722 /* given a veto_str like "abc/zxc/" and path "abc", return 1
723 * veto_str should be '/' delimited
724 * if path matches any one of the veto_str elements exactly, then 1 is returned
725 * otherwise, 0 is returned.
728 int i; /* index to veto_str */
729 int j; /* index to path */
731 if ((veto_str == NULL) || (path == NULL))
734 for(i=0, j=0; veto_str[i] != '\0'; i++) {
735 if (veto_str[i] == '/') {
736 if ((j>0) && (path[j] == '\0')) {
737 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
742 if (veto_str[i] != path[j]) {
743 while ((veto_str[i] != '/')
744 && (veto_str[i] != '\0'))