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 )) ) {
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->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
209 if (!vol->vfs->vfs_validupath(vol, name)) {
210 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
214 /* check for vetoed filenames */
215 if (veto_file(vol->v_veto, name))
220 /* -------------------------
221 move and rename sdir:oldname to curdir:newname in volume vol
222 special care is needed for lock
224 static int moveandrename(const struct vol *vol,
231 char *oldunixname = NULL;
234 struct stat *st, nst;
238 struct ofork *opened = NULL;
243 LOG(log_debug, logtype_afpd,
244 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
245 cfrombstr(sdir->d_u_name), oldname, newname);
252 if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding()))) == NULL)
253 return AFPERR_PARAM; /* can't convert */
254 id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
259 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
260 return AFPERR_PARAM; /* pathname too long */
261 #endif /* HAVE_ATFUNCS */
264 path.u_name = oldunixname;
267 opened = of_findnameat(sdir_fd, &path);
269 opened = of_findname(&path);
270 #endif /* HAVE_ATFUNCS */
273 /* reuse struct adouble so it won't break locks */
277 id = sdir->d_did; /* we already have the CNID */
278 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
280 adflags = ADFLAGS_DIR;
284 * oldunixname now points to either
285 * a) full pathname of the source fs object (if renameat is not available)
286 * b) the oldname (renameat is available)
287 * we are in the dest folder so we need to use
288 * a) oldunixname for ad_open
289 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
293 if ((cwd_fd = open(".", O_RDONLY)) == -1)
295 if (fchdir(sdir_fd) != 0) {
300 if (!ad_metadata(oldunixname, adflags, adp)) {
303 ad_getattr(adp, &bshort);
305 ad_close(adp, ADFLAGS_HF);
306 if ((bshort & htons(ATTRBIT_NORENAME))) {
312 if (fchdir(cwd_fd) != 0) {
313 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
319 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
325 if (0 != (rc = check_name(vol, upath))) {
329 /* source == destination. we just silently accept this. */
330 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
331 if (strcmp(oldname, newname) == 0) {
336 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
337 if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
338 /* not the same file */
344 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
351 path.st_errno = errno;
352 if (of_findname(&path)) {
353 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
355 rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
357 of_rename(vol, opened, sdir, oldname, curdir, newname);
360 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
362 if ( rc == AFP_OK && id ) {
363 /* renaming may have moved the file/dir across a filesystem */
364 if (stat(upath, st) < 0) {
369 /* Remove it from the cache */
370 struct dir *cacheddir = dircache_search_by_did(vol, id);
372 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
373 (void)dir_remove(vol, cacheddir);
376 /* Fixup adouble info */
377 if (!ad_metadata(upath, adflags, adp)) {
378 ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
380 ad_close(adp, ADFLAGS_HF);
383 /* fix up the catalog entry */
384 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
395 /* -------------------------------------------- */
396 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
400 char *oldname, *newname;
411 memcpy( &vid, ibuf, sizeof( vid ));
412 ibuf += sizeof( vid );
413 if (NULL == ( vol = getvolbyvid( vid )) ) {
414 return( AFPERR_PARAM );
417 if (vol->v_flags & AFPVOL_RO)
420 memcpy( &did, ibuf, sizeof( did ));
421 ibuf += sizeof( did );
422 if (NULL == ( sdir = dirlookup( vol, did )) ) {
426 /* source pathname */
427 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
428 return get_afp_errno(AFPERR_NOOBJ);
432 newname = obj->newtmp;
433 oldname = obj->oldtmp;
434 isdir = path_isadir(path);
435 if ( *path->m_name != '\0' ) {
436 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
438 /* curdir parent dir, need to move sdir back */
443 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
444 return( AFPERR_NORENAME );
446 /* move to destination dir */
447 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
450 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
453 /* another place where we know about the path type */
454 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
455 return( AFPERR_PARAM );
459 return AFP_OK; /* newname == oldname same dir */
462 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
463 if ( rc == AFP_OK ) {
464 setvoltime(obj, vol );
470 /* ------------------------------- */
471 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
484 memcpy( &vid, ibuf, sizeof( vid ));
485 ibuf += sizeof( vid );
486 if (NULL == ( vol = getvolbyvid( vid )) ) {
487 return( AFPERR_PARAM );
490 if (vol->v_flags & AFPVOL_RO)
493 memcpy( &did, ibuf, sizeof( did ));
494 ibuf += sizeof( int );
496 if (NULL == ( dir = dirlookup( vol, did )) ) {
500 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
501 return get_afp_errno(AFPERR_NOOBJ);
504 upath = s_path->u_name;
505 if (path_isadir(s_path)) {
506 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT) {
507 if (vol->v_adouble == AD_VERSION2)
508 return AFPERR_ACCESS;
509 if (*s_path->m_name == '\0' && curdir->d_did == DIRDID_ROOT)
510 return AFPERR_ACCESS;
511 if (rmdir(upath) != 0) {
514 return AFPERR_DIRNEMPT;
516 return AFPERR_ACCESS;
522 cnid_t delcnid = CNID_INVALID;
523 if ((deldir = dircache_search_by_name(vol, curdir, upath, strlen(upath)))) {
524 delcnid = deldir->d_did;
525 dir_remove(vol, deldir);
527 if (delcnid == CNID_INVALID)
528 delcnid = cnid_get(vol->v_cdb, curdir->d_did, upath, strlen(upath));
529 if (delcnid != CNID_INVALID)
530 cnid_delete(vol->v_cdb, delcnid);
531 fce_register_delete_dir(upath);
533 /* we have to cache this, the structs are lost in deletcurdir*/
534 /* but we need the positive returncode to send our event */
536 if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
538 if ((rc = deletecurdir(vol)) == AFP_OK)
539 fce_register_delete_dir(cfrombstr(dname));
542 } else if (of_findname(s_path)) {
545 /* it's a file st_valid should always be true
546 * only test for ENOENT because EACCES needs
547 * to read meta data in deletefile
549 if (s_path->st_valid && s_path->st_errno == ENOENT) {
552 if ((rc = deletefile(vol, -1, upath, 1)) == AFP_OK) {
553 fce_register_delete_file( s_path );
554 if (vol->v_tm_used < s_path->st.st_size)
557 vol->v_tm_used -= s_path->st.st_size;
559 struct dir *cachedfile;
560 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
561 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
562 dir_free(cachedfile);
566 if ( rc == AFP_OK ) {
568 setvoltime(obj, vol );
573 /* ------------------------ */
574 char *absupath(const struct vol *vol, struct dir *dir, char *u)
576 static char pathbuf[MAXPATHLEN + 1];
579 if (u == NULL || dir == NULL || vol == NULL)
582 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
584 if (bcatcstr(path, "/") != BSTR_OK)
586 if (bcatcstr(path, u) != BSTR_OK)
588 if (path->slen > MAXPATHLEN)
591 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
593 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
599 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
601 if (vol == NULL || dir == NULL || name == NULL)
603 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
606 /* ------------------------- */
607 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
610 struct dir *sdir, *ddir;
612 char *oldname, *newname;
625 memcpy( &vid, ibuf, sizeof( vid ));
626 ibuf += sizeof( vid );
627 if (NULL == ( vol = getvolbyvid( vid )) ) {
628 return( AFPERR_PARAM );
631 if (vol->v_flags & AFPVOL_RO)
634 /* source did followed by dest did */
635 memcpy( &did, ibuf, sizeof( did ));
636 ibuf += sizeof( int );
637 if (NULL == ( sdir = dirlookup( vol, did )) ) {
638 return afp_errno; /* was AFPERR_PARAM */
641 memcpy( &did, ibuf, sizeof( did ));
642 ibuf += sizeof( int );
644 /* source pathname */
645 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
646 return get_afp_errno(AFPERR_NOOBJ);
650 newname = obj->newtmp;
651 oldname = obj->oldtmp;
653 isdir = path_isadir(path);
654 if ( *path->m_name != '\0' ) {
658 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
660 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
664 if ((sdir_fd = open(".", O_RDONLY)) == -1)
668 /* get the destination directory */
669 if (NULL == ( ddir = dirlookup( vol, did )) ) {
670 rc = afp_errno; /* was AFPERR_PARAM */
673 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
677 pdid = curdir->d_did;
678 if ( *path->m_name != '\0' ) {
679 rc = path_error(path, AFPERR_NOOBJ);
683 /* one more place where we know about path type */
684 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
690 strcpy(newname, oldname);
693 /* This does the work */
694 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
695 oldname, newname, isdir);
696 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
698 if ( rc == AFP_OK ) {
699 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
705 /* if unix priv don't try to match perm with dest folder */
706 if (!isdir && !vol_unix_priv(vol)) {
707 int admode = ad_mode("", 0777) | vol->v_fperm;
709 setfilmode(upath, admode, NULL, vol->v_umask);
710 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
712 setvoltime(obj, vol );
724 int veto_file(const char*veto_str, const char*path)
725 /* given a veto_str like "abc/zxc/" and path "abc", return 1
726 * veto_str should be '/' delimited
727 * if path matches any one of the veto_str elements exactly, then 1 is returned
728 * otherwise, 0 is returned.
731 int i; /* index to veto_str */
732 int j; /* index to path */
734 if ((veto_str == NULL) || (path == NULL))
737 for(i=0, j=0; veto_str[i] != '\0'; i++) {
738 if (veto_str[i] == '/') {
739 if ((j>0) && (path[j] == '\0')) {
740 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
745 if (veto_str[i] != path[j]) {
746 while ((veto_str[i] != '/')
747 && (veto_str[i] != '\0'))