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>
29 #include "directory.h"
38 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
46 uint16_t fbitmap, dbitmap, vid;
52 memcpy( &vid, ibuf, sizeof( vid ));
53 ibuf += sizeof( vid );
54 if (NULL == ( vol = getvolbyvid( vid )) ) {
55 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
58 return( AFPERR_ACCESS );
61 memcpy( &did, ibuf, sizeof( did ));
62 ibuf += sizeof( did );
64 if (NULL == ( dir = dirlookup( vol, did )) ) {
68 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
69 fbitmap = ntohs( fbitmap );
70 ibuf += sizeof( fbitmap );
71 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
72 dbitmap = ntohs( dbitmap );
73 ibuf += sizeof( dbitmap );
75 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
76 return get_afp_errno(AFPERR_NOOBJ);
79 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
80 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
81 ntohl(curdir->d_did), cfrombstr(curdir->d_fullpath), s_path->u_name);
84 if (!s_path->st_valid) {
85 /* it's a dir and it should be there
86 * because we chdir in it in cname or
87 * it's curdir (maybe deleted, but then we can't know).
88 * So we need to try harder.
90 of_statdir(vol, s_path);
92 if ( s_path->st_errno != 0 ) {
93 if (afp_errno != AFPERR_ACCESS) {
94 return( AFPERR_NOOBJ );
100 if (S_ISDIR(st->st_mode)) {
106 ret = getdirparams(vol, dbitmap, s_path, dir,
107 rbuf + 3 * sizeof( uint16_t ), &buflen );
111 /* this is a directory */
112 *(rbuf + 2 * sizeof( uint16_t )) = (char) FILDIRBIT_ISDIR;
114 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
115 rbuf + 3 * sizeof( uint16_t ), &buflen )) ) {
119 *(rbuf + 2 * sizeof( uint16_t )) = FILDIRBIT_ISFILE;
121 *rbuflen = buflen + 3 * sizeof( uint16_t );
122 fbitmap = htons( fbitmap );
123 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
124 rbuf += sizeof( fbitmap );
125 dbitmap = htons( dbitmap );
126 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
127 rbuf += sizeof( dbitmap ) + sizeof( u_char );
133 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
139 uint16_t vid, bitmap;
144 memcpy( &vid, ibuf, sizeof(vid));
145 ibuf += sizeof( vid );
147 if (NULL == ( vol = getvolbyvid( vid )) ) {
148 return( AFPERR_PARAM );
151 if (vol->v_flags & AFPVOL_RO)
154 memcpy( &did, ibuf, sizeof( did));
155 ibuf += sizeof( did);
157 if (NULL == ( dir = dirlookup( vol, did )) ) {
161 memcpy( &bitmap, ibuf, sizeof( bitmap ));
162 bitmap = ntohs( bitmap );
163 ibuf += sizeof( bitmap );
165 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
166 return get_afp_errno(AFPERR_NOOBJ);
170 if (!path->st_valid) {
171 /* it's a dir and it should be there
172 * because we chdir in it in cname
174 of_statdir(vol, path);
177 if ( path->st_errno != 0 ) {
178 if (afp_errno != AFPERR_ACCESS)
179 return( AFPERR_NOOBJ );
182 * If ibuf is odd, make it even.
184 if ((u_long)ibuf & 1 ) {
188 if (S_ISDIR(st->st_mode)) {
189 rc = setdirparams(vol, path, bitmap, ibuf );
191 rc = setfilparams(vol, path, bitmap, ibuf );
193 if ( rc == AFP_OK ) {
194 setvoltime(obj, vol );
200 /* --------------------------------------------
201 Factorise some checks on a pathname
203 int check_name(const struct vol *vol, char *name)
205 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
208 if (!vol->vfs->vfs_validupath(vol, name)) {
209 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
213 /* check for vetoed filenames */
214 if (veto_file(vol->v_veto, name))
219 /* -------------------------
220 move and rename sdir:oldname to curdir:newname in volume vol
221 special care is needed for lock
223 static int moveandrename(const struct vol *vol,
230 char *oldunixname = NULL;
233 struct stat *st, nst;
237 struct ofork *opened = NULL;
242 LOG(log_debug, logtype_afpd,
243 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
244 cfrombstr(sdir->d_u_name), oldname, newname);
251 if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding()))) == NULL)
252 return AFPERR_PARAM; /* can't convert */
253 id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
258 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
259 return AFPERR_PARAM; /* pathname too long */
260 #endif /* HAVE_ATFUNCS */
263 path.u_name = oldunixname;
266 opened = of_findnameat(sdir_fd, &path);
268 opened = of_findname(&path);
269 #endif /* HAVE_ATFUNCS */
272 /* reuse struct adouble so it won't break locks */
276 id = sdir->d_did; /* we already have the CNID */
277 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
279 adflags = ADFLAGS_DIR;
283 * oldunixname now points to either
284 * a) full pathname of the source fs object (if renameat is not available)
285 * b) the oldname (renameat is available)
286 * we are in the dest folder so we need to use
287 * a) oldunixname for ad_open
288 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
292 if ((cwd_fd = open(".", O_RDONLY)) == -1)
294 if (fchdir(sdir_fd) != 0) {
299 if (!ad_metadata(oldunixname, adflags, adp)) {
302 ad_getattr(adp, &bshort);
304 ad_close(adp, ADFLAGS_HF);
305 if ((bshort & htons(ATTRBIT_NORENAME))) {
311 if (fchdir(cwd_fd) != 0) {
312 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
318 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
324 if (0 != (rc = check_name(vol, upath))) {
328 /* source == destination. we just silently accept this. */
329 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
330 if (strcmp(oldname, newname) == 0) {
335 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
336 if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
337 /* not the same file */
343 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
350 path.st_errno = errno;
351 if (of_findname(&path)) {
352 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
354 rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
356 of_rename(vol, opened, sdir, oldname, curdir, newname);
359 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
361 if ( rc == AFP_OK && id ) {
362 /* renaming may have moved the file/dir across a filesystem */
363 if (stat(upath, st) < 0) {
368 /* Remove it from the cache */
369 struct dir *cacheddir = dircache_search_by_did(vol, id);
371 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
372 (void)dir_remove(vol, cacheddir);
375 /* Fixup adouble info */
376 if (!ad_metadata(upath, adflags, adp)) {
377 ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
379 ad_close(adp, ADFLAGS_HF);
382 /* fix up the catalog entry */
383 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
394 /* -------------------------------------------- */
395 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
399 char *oldname, *newname;
410 memcpy( &vid, ibuf, sizeof( vid ));
411 ibuf += sizeof( vid );
412 if (NULL == ( vol = getvolbyvid( vid )) ) {
413 return( AFPERR_PARAM );
416 if (vol->v_flags & AFPVOL_RO)
419 memcpy( &did, ibuf, sizeof( did ));
420 ibuf += sizeof( did );
421 if (NULL == ( sdir = dirlookup( vol, did )) ) {
425 /* source pathname */
426 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
427 return get_afp_errno(AFPERR_NOOBJ);
431 newname = obj->newtmp;
432 oldname = obj->oldtmp;
433 isdir = path_isadir(path);
434 if ( *path->m_name != '\0' ) {
435 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
437 /* curdir parent dir, need to move sdir back */
442 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
443 return( AFPERR_NORENAME );
445 /* move to destination dir */
446 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
449 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
452 /* another place where we know about the path type */
453 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
454 return( AFPERR_PARAM );
458 return AFP_OK; /* newname == oldname same dir */
461 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
462 if ( rc == AFP_OK ) {
463 setvoltime(obj, vol );
469 /* ------------------------------- */
470 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
483 memcpy( &vid, ibuf, sizeof( vid ));
484 ibuf += sizeof( vid );
485 if (NULL == ( vol = getvolbyvid( vid )) ) {
486 return( AFPERR_PARAM );
489 if (vol->v_flags & AFPVOL_RO)
492 memcpy( &did, ibuf, sizeof( did ));
493 ibuf += sizeof( int );
495 if (NULL == ( dir = dirlookup( vol, did )) ) {
499 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
500 return get_afp_errno(AFPERR_NOOBJ);
503 upath = s_path->u_name;
504 if (path_isadir(s_path)) {
505 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT) {
506 if (vol->v_adouble == AD_VERSION2)
507 return AFPERR_ACCESS;
508 if (*s_path->m_name == '\0' && curdir->d_did == DIRDID_ROOT)
509 return AFPERR_ACCESS;
510 if (rmdir(upath) != 0) {
513 return AFPERR_DIRNEMPT;
515 return AFPERR_ACCESS;
521 cnid_t delcnid = CNID_INVALID;
522 if ((deldir = dircache_search_by_name(vol, curdir, upath, strlen(upath)))) {
523 delcnid = deldir->d_did;
524 dir_remove(vol, deldir);
526 if (delcnid == CNID_INVALID)
527 delcnid = cnid_get(vol->v_cdb, curdir->d_did, upath, strlen(upath));
528 if (delcnid != CNID_INVALID)
529 cnid_delete(vol->v_cdb, delcnid);
530 fce_register_delete_dir(upath);
532 /* we have to cache this, the structs are lost in deletcurdir*/
533 /* but we need the positive returncode to send our event */
535 if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
537 if ((rc = deletecurdir(vol)) == AFP_OK)
538 fce_register_delete_dir(cfrombstr(dname));
541 } else if (of_findname(s_path)) {
544 /* it's a file st_valid should always be true
545 * only test for ENOENT because EACCES needs
546 * to read meta data in deletefile
548 if (s_path->st_valid && s_path->st_errno == ENOENT) {
551 if ((rc = deletefile(vol, -1, upath, 1)) == AFP_OK) {
552 fce_register_delete_file( s_path );
553 if (vol->v_tm_used < s_path->st.st_size)
556 vol->v_tm_used -= s_path->st.st_size;
558 struct dir *cachedfile;
559 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
560 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
561 dir_free(cachedfile);
565 if ( rc == AFP_OK ) {
567 setvoltime(obj, vol );
572 /* ------------------------ */
573 char *absupath(const struct vol *vol, struct dir *dir, char *u)
575 static char pathbuf[MAXPATHLEN + 1];
578 if (u == NULL || dir == NULL || vol == NULL)
581 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
583 if (bcatcstr(path, "/") != BSTR_OK)
585 if (bcatcstr(path, u) != BSTR_OK)
587 if (path->slen > MAXPATHLEN)
590 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
592 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
598 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
600 if (vol == NULL || dir == NULL || name == NULL)
602 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
605 /* ------------------------- */
606 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
609 struct dir *sdir, *ddir;
611 char *oldname, *newname;
624 memcpy( &vid, ibuf, sizeof( vid ));
625 ibuf += sizeof( vid );
626 if (NULL == ( vol = getvolbyvid( vid )) ) {
627 return( AFPERR_PARAM );
630 if (vol->v_flags & AFPVOL_RO)
633 /* source did followed by dest did */
634 memcpy( &did, ibuf, sizeof( did ));
635 ibuf += sizeof( int );
636 if (NULL == ( sdir = dirlookup( vol, did )) ) {
637 return afp_errno; /* was AFPERR_PARAM */
640 memcpy( &did, ibuf, sizeof( did ));
641 ibuf += sizeof( int );
643 /* source pathname */
644 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
645 return get_afp_errno(AFPERR_NOOBJ);
649 newname = obj->newtmp;
650 oldname = obj->oldtmp;
652 isdir = path_isadir(path);
653 if ( *path->m_name != '\0' ) {
657 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
659 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
663 if ((sdir_fd = open(".", O_RDONLY)) == -1)
667 /* get the destination directory */
668 if (NULL == ( ddir = dirlookup( vol, did )) ) {
669 rc = afp_errno; /* was AFPERR_PARAM */
672 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
676 pdid = curdir->d_did;
677 if ( *path->m_name != '\0' ) {
678 rc = path_error(path, AFPERR_NOOBJ);
682 /* one more place where we know about path type */
683 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
689 strcpy(newname, oldname);
692 /* This does the work */
693 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
694 oldname, newname, isdir);
695 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
697 if ( rc == AFP_OK ) {
698 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
704 /* if unix priv don't try to match perm with dest folder */
705 if (!isdir && !vol_unix_priv(vol)) {
706 int admode = ad_mode("", 0777) | vol->v_fperm;
708 setfilmode(upath, admode, NULL, vol->v_umask);
709 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
711 setvoltime(obj, vol );
723 int veto_file(const char*veto_str, const char*path)
724 /* given a veto_str like "abc/zxc/" and path "abc", return 1
725 * veto_str should be '/' delimited
726 * if path matches any one of the veto_str elements exactly, then 1 is returned
727 * otherwise, 0 is returned.
730 int i; /* index to veto_str */
731 int j; /* index to path */
733 if ((veto_str == NULL) || (path == NULL))
736 for(i=0, j=0; veto_str[i] != '\0'; i++) {
737 if (veto_str[i] == '/') {
738 if ((j>0) && (path[j] == '\0')) {
739 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
744 if (veto_str[i] != path[j]) {
745 while ((veto_str[i] != '/')
746 && (veto_str[i] != '\0'))