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>
27 #include "directory.h"
37 int afp_getfildirparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
45 u_int16_t fbitmap, dbitmap, vid;
51 memcpy( &vid, ibuf, sizeof( vid ));
52 ibuf += sizeof( vid );
53 if (NULL == ( vol = getvolbyvid( vid )) ) {
54 /* was AFPERR_PARAM but it helps OS 10.3 when a volume has been removed
57 return( AFPERR_ACCESS );
60 memcpy( &did, ibuf, sizeof( did ));
61 ibuf += sizeof( did );
63 if (NULL == ( dir = dirlookup( vol, did )) ) {
67 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
68 fbitmap = ntohs( fbitmap );
69 ibuf += sizeof( fbitmap );
70 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
71 dbitmap = ntohs( dbitmap );
72 ibuf += sizeof( dbitmap );
74 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
75 return get_afp_errno(AFPERR_NOOBJ);
78 LOG(log_debug, logtype_afpd, "getfildirparams(vid:%u, did:%u, f/d:%04x/%04x) {cwdid:%u, cwd: %s, name:'%s'}",
79 ntohs(vid), ntohl(dir->d_did), fbitmap, dbitmap,
80 ntohl(curdir->d_did), cfrombstr(curdir->d_fullpath), s_path->u_name);
83 if (!s_path->st_valid) {
84 /* it's a dir and it should be there
85 * because we chdir in it in cname or
86 * it's curdir (maybe deleted, but then we can't know).
87 * So we need to try harder.
89 of_statdir(vol, s_path);
91 if ( s_path->st_errno != 0 ) {
92 if (afp_errno != AFPERR_ACCESS) {
93 return( AFPERR_NOOBJ );
99 if (S_ISDIR(st->st_mode)) {
105 ret = getdirparams(vol, dbitmap, s_path, dir,
106 rbuf + 3 * sizeof( u_int16_t ), &buflen );
110 /* this is a directory */
111 *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
113 if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir,
114 rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
118 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
120 *rbuflen = buflen + 3 * sizeof( u_int16_t );
121 fbitmap = htons( fbitmap );
122 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
123 rbuf += sizeof( fbitmap );
124 dbitmap = htons( dbitmap );
125 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
126 rbuf += sizeof( dbitmap ) + sizeof( u_char );
132 int afp_setfildirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
138 u_int16_t vid, bitmap;
143 memcpy( &vid, ibuf, sizeof(vid));
144 ibuf += sizeof( vid );
146 if (NULL == ( vol = getvolbyvid( vid )) ) {
147 return( AFPERR_PARAM );
150 if (vol->v_flags & AFPVOL_RO)
153 memcpy( &did, ibuf, sizeof( did));
154 ibuf += sizeof( did);
156 if (NULL == ( dir = dirlookup( vol, did )) ) {
160 memcpy( &bitmap, ibuf, sizeof( bitmap ));
161 bitmap = ntohs( bitmap );
162 ibuf += sizeof( bitmap );
164 if (NULL == ( path = cname( vol, dir, &ibuf ))) {
165 return get_afp_errno(AFPERR_NOOBJ);
169 if (!path->st_valid) {
170 /* it's a dir and it should be there
171 * because we chdir in it in cname
173 of_statdir(vol, path);
176 if ( path->st_errno != 0 ) {
177 if (afp_errno != AFPERR_ACCESS)
178 return( AFPERR_NOOBJ );
181 * If ibuf is odd, make it even.
183 if ((u_long)ibuf & 1 ) {
187 if (S_ISDIR(st->st_mode)) {
188 rc = setdirparams(vol, path, bitmap, ibuf );
190 rc = setfilparams(vol, path, bitmap, ibuf );
192 if ( rc == AFP_OK ) {
193 setvoltime(obj, vol );
199 /* --------------------------------------------
200 Factorise some checks on a pathname
202 int check_name(const struct vol *vol, char *name)
204 /* check for illegal characters in the unix filename */
205 if (!wincheck(vol, name))
208 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(name, '/'))
211 if (!vol->vfs->vfs_validupath(vol, name)) {
212 LOG(log_error, logtype_afpd, "check_name: illegal name: '%s'", name);
216 /* check for vetoed filenames */
217 if (veto_file(vol->v_veto, name))
222 /* -------------------------
223 move and rename sdir:oldname to curdir:newname in volume vol
224 special care is needed for lock
226 static int moveandrename(const struct vol *vol,
233 char *oldunixname = NULL;
236 struct stat *st, nst;
240 struct ofork *opened = NULL;
245 LOG(log_debug, logtype_afpd,
246 "moveandrename: [\"%s\"/\"%s\"] -> \"%s\"",
247 cfrombstr(sdir->d_u_name), oldname, newname);
249 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
254 if ((oldunixname = strdup(mtoupath(vol, oldname, sdir->d_did, utf8_encoding()))) == NULL)
255 return AFPERR_PARAM; /* can't convert */
256 id = cnid_get(vol->v_cdb, sdir->d_did, oldunixname, strlen(oldunixname));
261 if ((oldunixname = strdup(ctoupath(vol, sdir, oldname))) == NULL)
262 return AFPERR_PARAM; /* pathname too long */
263 #endif /* HAVE_ATFUNCS */
266 path.u_name = oldunixname;
269 opened = of_findnameat(sdir_fd, &path);
271 opened = of_findname(&path);
272 #endif /* HAVE_ATFUNCS */
275 /* reuse struct adouble so it won't break locks */
279 id = sdir->d_did; /* we already have the CNID */
280 if ((oldunixname = strdup(ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname))) == NULL)
282 adflags = ADFLAGS_DIR;
286 * oldunixname now points to either
287 * a) full pathname of the source fs object (if renameat is not available)
288 * b) the oldname (renameat is available)
289 * we are in the dest folder so we need to use
290 * a) oldunixname for ad_open
291 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
295 if ((cwd_fd = open(".", O_RDONLY)) == -1)
297 if (fchdir(sdir_fd) != 0) {
302 if (!ad_metadata(oldunixname, adflags, adp)) {
305 ad_getattr(adp, &bshort);
307 ad_close_metadata( adp);
308 if ((bshort & htons(ATTRBIT_NORENAME))) {
314 if (fchdir(cwd_fd) != 0) {
315 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
321 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
327 if (0 != (rc = check_name(vol, upath))) {
331 /* source == destination. we just silently accept this. */
332 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
333 if (strcmp(oldname, newname) == 0) {
338 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
339 if (!stat(oldunixname, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
340 /* not the same file */
346 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
353 path.st_errno = errno;
354 if (of_findname(&path)) {
355 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
357 rc = renamefile(vol, sdir_fd, oldunixname, upath, newname, adp );
359 of_rename(vol, opened, sdir, oldname, curdir, newname);
362 rc = renamedir(vol, sdir_fd, oldunixname, upath, sdir, curdir, newname);
364 if ( rc == AFP_OK && id ) {
365 /* renaming may have moved the file/dir across a filesystem */
366 if (stat(upath, st) < 0) {
371 /* Remove it from the cache */
372 struct dir *cacheddir = dircache_search_by_did(vol, id);
374 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
375 (void)dir_remove(vol, cacheddir);
378 /* Fixup adouble info */
379 if (!ad_metadata(upath, adflags, adp)) {
380 ad_setid(adp, st->st_dev, st->st_ino, id, curdir->d_did, vol->v_stamp);
382 ad_close_metadata(adp);
385 /* fix up the catalog entry */
386 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
397 /* -------------------------------------------- */
398 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
402 char *oldname, *newname;
413 memcpy( &vid, ibuf, sizeof( vid ));
414 ibuf += sizeof( vid );
415 if (NULL == ( vol = getvolbyvid( vid )) ) {
416 return( AFPERR_PARAM );
419 if (vol->v_flags & AFPVOL_RO)
422 memcpy( &did, ibuf, sizeof( did ));
423 ibuf += sizeof( did );
424 if (NULL == ( sdir = dirlookup( vol, did )) ) {
428 /* source pathname */
429 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
430 return get_afp_errno(AFPERR_NOOBJ);
434 newname = obj->newtmp;
435 oldname = obj->oldtmp;
436 isdir = path_isadir(path);
437 if ( *path->m_name != '\0' ) {
438 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
440 /* curdir parent dir, need to move sdir back */
445 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
446 return( AFPERR_NORENAME );
448 /* move to destination dir */
449 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
452 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
455 /* another place where we know about the path type */
456 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
457 return( AFPERR_PARAM );
461 return AFP_OK; /* newname == oldname same dir */
464 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
465 if ( rc == AFP_OK ) {
466 setvoltime(obj, vol );
472 /* ------------------------------- */
473 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
485 memcpy( &vid, ibuf, sizeof( vid ));
486 ibuf += sizeof( vid );
487 if (NULL == ( vol = getvolbyvid( vid )) ) {
488 return( AFPERR_PARAM );
491 if (vol->v_flags & AFPVOL_RO)
494 memcpy( &did, ibuf, sizeof( did ));
495 ibuf += sizeof( int );
497 if (NULL == ( dir = dirlookup( vol, did )) ) {
501 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
502 return get_afp_errno(AFPERR_NOOBJ);
505 upath = s_path->u_name;
506 if ( path_isadir( s_path) ) {
507 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
510 rc = deletecurdir( vol);
511 } else if (of_findname(s_path)) {
514 /* it's a file st_valid should always be true
515 * only test for ENOENT because EACCES needs
516 * to read meta data in deletefile
518 if (s_path->st_valid && s_path->st_errno == ENOENT) {
522 rc = deletefile(vol, -1, upath, 1);
524 struct dir *cachedfile;
525 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath), s_path->st.st_ctime))) {
526 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
527 dir_free(cachedfile);
531 if ( rc == AFP_OK ) {
533 setvoltime(obj, vol );
538 /* ------------------------ */
539 char *absupath(const struct vol *vol, struct dir *dir, char *u)
541 static char pathbuf[MAXPATHLEN + 1];
544 if (u == NULL || dir == NULL || vol == NULL)
547 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
549 if (bcatcstr(path, "/") != BSTR_OK)
551 if (bcatcstr(path, u) != BSTR_OK)
553 if (path->slen > MAXPATHLEN)
556 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
558 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
564 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
566 if (vol == NULL || dir == NULL || name == NULL)
568 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
571 /* ------------------------- */
572 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
575 struct dir *sdir, *ddir;
577 char *oldname, *newname;
590 memcpy( &vid, ibuf, sizeof( vid ));
591 ibuf += sizeof( vid );
592 if (NULL == ( vol = getvolbyvid( vid )) ) {
593 return( AFPERR_PARAM );
596 if (vol->v_flags & AFPVOL_RO)
599 /* source did followed by dest did */
600 memcpy( &did, ibuf, sizeof( did ));
601 ibuf += sizeof( int );
602 if (NULL == ( sdir = dirlookup( vol, did )) ) {
603 return afp_errno; /* was AFPERR_PARAM */
606 memcpy( &did, ibuf, sizeof( did ));
607 ibuf += sizeof( int );
609 /* source pathname */
610 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
611 return get_afp_errno(AFPERR_NOOBJ);
615 newname = obj->newtmp;
616 oldname = obj->oldtmp;
618 isdir = path_isadir(path);
619 if ( *path->m_name != '\0' ) {
623 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
625 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
629 if ((sdir_fd = open(".", O_RDONLY)) == -1)
633 /* get the destination directory */
634 if (NULL == ( ddir = dirlookup( vol, did )) ) {
635 rc = afp_errno; /* was AFPERR_PARAM */
638 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
642 pdid = curdir->d_did;
643 if ( *path->m_name != '\0' ) {
644 rc = path_error(path, AFPERR_NOOBJ);
648 /* one more place where we know about path type */
649 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
655 strcpy(newname, oldname);
658 /* This does the work */
659 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
660 oldname, newname, isdir);
661 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
663 if ( rc == AFP_OK ) {
664 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
672 /* if unix priv don't try to match perm with dest folder */
673 if (!isdir && !vol_unix_priv(vol)) {
674 int admode = ad_mode("", 0777) | vol->v_fperm;
676 setfilmode(upath, admode, NULL, vol->v_umask);
677 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
679 setvoltime(obj, vol );
691 int veto_file(const char*veto_str, const char*path)
692 /* given a veto_str like "abc/zxc/" and path "abc", return 1
693 * veto_str should be '/' delimited
694 * if path matches any one of the veto_str elements exactly, then 1 is returned
695 * otherwise, 0 is returned.
698 int i; /* index to veto_str */
699 int j; /* index to path */
701 if ((veto_str == NULL) || (path == NULL))
704 for(i=0, j=0; veto_str[i] != '\0'; i++) {
705 if (veto_str[i] == '/') {
706 if ((j>0) && (path[j] == '\0')) {
707 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
712 if (veto_str[i] != path[j]) {
713 while ((veto_str[i] != '/')
714 && (veto_str[i] != '\0'))