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 u_int16_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( u_int16_t ), &buflen );
111 /* this is a directory */
112 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
114 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
115 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
119 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
121 *rbuflen = buflen + 3 * sizeof( u_int16_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 u_int16_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 /* check for illegal characters in the unix filename */
206 if (!wincheck(vol, name))
209 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
212 if (!vol->vfs->vfs_validupath(vol, name)) {
213 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
217 /* check for vetoed filenames */
218 if (veto_file(vol->v_veto, name))
223 /* -------------------------
224 move and rename sdir:oldname to curdir:newname in volume vol
225 special care is needed for lock
227 static int moveandrename(const struct vol *vol,
234 char *oldunixname = NULL;
237 struct stat *st, nst;
241 struct ofork *opened = NULL;
246 LOG(log_debug, logtype_afpd,
247 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
248 cfrombstr(sdir->d_u_name), oldname, newname);
250 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
255 if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding()))) == NULL)
256 return AFPERR_PARAM; /* can't convert */
257 id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
262 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
263 return AFPERR_PARAM; /* pathname too long */
264 #endif /* HAVE_ATFUNCS */
267 path.u_name = oldunixname;
270 opened = of_findnameat(sdir_fd, &path);
272 opened = of_findname(&path);
273 #endif /* HAVE_ATFUNCS */
276 /* reuse struct adouble so it won't break locks */
280 id = sdir->d_did; /* we already have the CNID */
281 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
283 adflags = ADFLAGS_DIR;
287 * oldunixname now points to either
288 * a) full pathname of the source fs object (if renameat is not available)
289 * b) the oldname (renameat is available)
290 * we are in the dest folder so we need to use
291 * a) oldunixname for ad_open
292 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
296 if ((cwd_fd = open(".", O_RDONLY)) == -1)
298 if (fchdir(sdir_fd) != 0) {
303 if (!ad_metadata(oldunixname, adflags, adp)) {
306 ad_getattr(adp, &bshort);
308 ad_close_metadata( adp);
309 if ((bshort & htons(ATTRBIT_NORENAME))) {
315 if (fchdir(cwd_fd) != 0) {
316 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
322 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
328 if (0 != (rc = check_name(vol, upath))) {
332 /* source == destination. we just silently accept this. */
333 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
334 if (strcmp(oldname, newname) == 0) {
339 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
340 if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
341 /* not the same file */
347 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
354 path.st_errno = errno;
355 if (of_findname(&path)) {
356 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
358 rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
360 of_rename(vol, opened, sdir, oldname, curdir, newname);
363 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
365 if ( rc == AFP_OK && id ) {
366 /* renaming may have moved the file/dir across a filesystem */
367 if (stat(upath, st) < 0) {
372 /* Remove it from the cache */
373 struct dir *cacheddir = dircache_search_by_did(vol, id);
375 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
376 (void)dir_remove(vol, cacheddir);
379 /* Fixup adouble info */
380 if (!ad_metadata(upath, adflags, adp)) {
381 ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
383 ad_close_metadata(adp);
386 /* fix up the catalog entry */
387 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
398 /* -------------------------------------------- */
399 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
403 char *oldname, *newname;
414 memcpy( &vid, ibuf, sizeof( vid ));
415 ibuf += sizeof( vid );
416 if (NULL == ( vol = getvolbyvid( vid )) ) {
417 return( AFPERR_PARAM );
420 if (vol->v_flags & AFPVOL_RO)
423 memcpy( &did, ibuf, sizeof( did ));
424 ibuf += sizeof( did );
425 if (NULL == ( sdir = dirlookup( vol, did )) ) {
429 /* source pathname */
430 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
431 return get_afp_errno(AFPERR_NOOBJ);
435 newname = obj->newtmp;
436 oldname = obj->oldtmp;
437 isdir = path_isadir(path);
438 if ( *path->m_name != '\0' ) {
439 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
441 /* curdir parent dir, need to move sdir back */
446 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
447 return( AFPERR_NORENAME );
449 /* move to destination dir */
450 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
453 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
456 /* another place where we know about the path type */
457 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
458 return( AFPERR_PARAM );
462 return AFP_OK; /* newname == oldname same dir */
465 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
466 if ( rc == AFP_OK ) {
467 setvoltime(obj, vol );
473 /* ------------------------------- */
474 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
486 memcpy( &vid, ibuf, sizeof( vid ));
487 ibuf += sizeof( vid );
488 if (NULL == ( vol = getvolbyvid( vid )) ) {
489 return( AFPERR_PARAM );
492 if (vol->v_flags & AFPVOL_RO)
495 memcpy( &did, ibuf, sizeof( did ));
496 ibuf += sizeof( int );
498 if (NULL == ( dir = dirlookup( vol, did )) ) {
502 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
503 return get_afp_errno(AFPERR_NOOBJ);
506 upath = s_path->u_name;
507 if ( path_isadir( s_path) ) {
508 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT) {
511 /* we have to cache this, the structs are lost in deletcurdir*/
512 /* but we need the positive returncode to send our event */
514 if ((dname = bstrcpy(curdir->d_u_name)) == NULL)
516 if ((rc = deletecurdir(vol)) == AFP_OK)
517 fce_register_delete_dir(cfrombstr(dname));
520 } else if (of_findname(s_path)) {
523 /* it's a file st_valid should always be true
524 * only test for ENOENT because EACCES needs
525 * to read meta data in deletefile
527 if (s_path->st_valid && s_path->st_errno == ENOENT) {
530 if ((rc = deletefile(vol, -1, upath, 1)) == AFP_OK) {
531 fce_register_delete_file( s_path );
532 if (vol->v_tm_used < s_path->st.st_size)
535 vol->v_tm_used -= s_path->st.st_size;
537 struct dir *cachedfile;
538 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath)))) {
539 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
540 dir_free(cachedfile);
544 if ( rc == AFP_OK ) {
546 setvoltime(obj, vol );
551 /* ------------------------ */
552 char *absupath(const struct vol *vol, struct dir *dir, char *u)
554 static char pathbuf[MAXPATHLEN + 1];
557 if (u == NULL || dir == NULL || vol == NULL)
560 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
562 if (bcatcstr(path, "/") != BSTR_OK)
564 if (bcatcstr(path, u) != BSTR_OK)
566 if (path->slen > MAXPATHLEN)
569 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
571 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
577 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
579 if (vol == NULL || dir == NULL || name == NULL)
581 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
584 /* ------------------------- */
585 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
588 struct dir *sdir, *ddir;
590 char *oldname, *newname;
603 memcpy( &vid, ibuf, sizeof( vid ));
604 ibuf += sizeof( vid );
605 if (NULL == ( vol = getvolbyvid( vid )) ) {
606 return( AFPERR_PARAM );
609 if (vol->v_flags & AFPVOL_RO)
612 /* source did followed by dest did */
613 memcpy( &did, ibuf, sizeof( did ));
614 ibuf += sizeof( int );
615 if (NULL == ( sdir = dirlookup( vol, did )) ) {
616 return afp_errno; /* was AFPERR_PARAM */
619 memcpy( &did, ibuf, sizeof( did ));
620 ibuf += sizeof( int );
622 /* source pathname */
623 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
624 return get_afp_errno(AFPERR_NOOBJ);
628 newname = obj->newtmp;
629 oldname = obj->oldtmp;
631 isdir = path_isadir(path);
632 if ( *path->m_name != '\0' ) {
636 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
638 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
642 if ((sdir_fd = open(".", O_RDONLY)) == -1)
646 /* get the destination directory */
647 if (NULL == ( ddir = dirlookup( vol, did )) ) {
648 rc = afp_errno; /* was AFPERR_PARAM */
651 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
655 pdid = curdir->d_did;
656 if ( *path->m_name != '\0' ) {
657 rc = path_error(path, AFPERR_NOOBJ);
661 /* one more place where we know about path type */
662 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
668 strcpy(newname, oldname);
671 /* This does the work */
672 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
673 oldname, newname, isdir);
674 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
676 if ( rc == AFP_OK ) {
677 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
685 /* if unix priv don't try to match perm with dest folder */
686 if (!isdir && !vol_unix_priv(vol)) {
687 int admode = ad_mode("", 0777) | vol->v_fperm;
689 setfilmode(upath, admode, NULL, vol->v_umask);
690 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
692 setvoltime(obj, vol );
704 int veto_file(const char*veto_str, const char*path)
705 /* given a veto_str like "abc/zxc/" and path "abc", return 1
706 * veto_str should be '/' delimited
707 * if path matches any one of the veto_str elements exactly, then 1 is returned
708 * otherwise, 0 is returned.
711 int i; /* index to veto_str */
712 int j; /* index to path */
714 if ((veto_str == NULL) || (path == NULL))
717 for(i=0, j=0; veto_str[i] != '\0'; i++) {
718 if (veto_str[i] == '/') {
719 if ((j>0) && (path[j] == '\0')) {
720 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
725 if (veto_str[i] != path[j]) {
726 while ((veto_str[i] != '/')
727 && (veto_str[i] != '\0'))