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,
236 struct stat *st, nst;
240 struct ofork *opened = NULL;
245 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
250 if ((p = mtoupath(vol, oldname, sdir->d_did, utf8_encoding())) == NULL)
251 return AFPERR_PARAM; /* can't convert */
253 #ifndef HAVE_RENAMEAT
255 id = cnid_get(vol->v_cdb, sdir->d_did, p, strlen(p));
256 p = ctoupath( vol, sdir, oldname );
258 return AFPERR_PARAM; /* pathname too long */
259 #endif /* HAVE_RENAMEAT */
264 opened = of_findnameat(sdir_fd, &path);
266 opened = of_findname(&path);
267 #endif /* HAVE_RENAMEAT */
269 /* reuse struct adouble so it won't break locks */
273 id = sdir->d_did; /* we already have the CNID */
274 p = ctoupath( vol, dirlookup(vol, sdir->d_pdid), oldname );
278 adflags = ADFLAGS_DIR;
282 * p now points to either
283 * a) full pathname of the source fs object (if renameat is not available)
284 * b) the oldname (renameat is available)
285 * we are in the dest folder so we need to use
287 * b) fchdir sdir_fd before eg ad_open or use *at functions where appropiate
291 if ((cwd_fd = open(".", O_RDONLY)) == -1)
293 if (fchdir(sdir_fd) != 0) {
298 if (!ad_metadata(p, adflags, adp)) {
301 ad_getattr(adp, &bshort);
303 ad_close_metadata( adp);
304 if ((bshort & htons(ATTRBIT_NORENAME))) {
310 if (fchdir(cwd_fd) != 0) {
311 LOG(log_error, logtype_afpd, "moveandrename: %s", strerror(errno) );
317 if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))){
323 if (0 != (rc = check_name(vol, upath))) {
327 /* source == destination. we just silently accept this. */
328 if ((!isdir && curdir == sdir) || (isdir && curdir->d_did == sdir->d_pdid)) {
329 if (strcmp(oldname, newname) == 0) {
334 if (stat(upath, st) == 0 || caseenumerate(vol, &path, curdir) == 0) {
335 if (!stat(p, &nst) && !(nst.st_dev == st->st_dev && nst.st_ino == st->st_ino) ) {
336 /* not the same file */
342 } else if (stat(upath, st ) == 0 || caseenumerate(vol, &path, curdir) == 0) {
349 path.st_errno = errno;
350 if (of_findname(&path)) {
351 rc = AFPERR_EXIST; /* was AFPERR_BUSY; */
353 rc = renamefile(vol, sdir_fd, p, upath, newname, adp );
355 of_rename(vol, opened, sdir, oldname, curdir, newname);
358 rc = renamedir(vol, sdir_fd, p, upath, sdir, curdir, newname);
360 if ( rc == AFP_OK && id ) {
361 /* renaming may have moved the file/dir across a filesystem */
362 if (stat(upath, st) < 0) {
367 /* Remove it from the cache */
368 struct dir *cacheddir = dircache_search_by_did(vol, id);
370 LOG(log_warning, logtype_afpd,"Still cached: \"%s/%s\"", getcwdpath(), upath);
371 (void)dir_remove(vol, cacheddir);
374 /* fix up the catalog entry */
375 cnid_update(vol->v_cdb, id, st, curdir->d_did, upath, strlen(upath));
384 /* -------------------------------------------- */
385 int afp_rename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
389 char *oldname, *newname;
400 memcpy( &vid, ibuf, sizeof( vid ));
401 ibuf += sizeof( vid );
402 if (NULL == ( vol = getvolbyvid( vid )) ) {
403 return( AFPERR_PARAM );
406 if (vol->v_flags & AFPVOL_RO)
409 memcpy( &did, ibuf, sizeof( did ));
410 ibuf += sizeof( did );
411 if (NULL == ( sdir = dirlookup( vol, did )) ) {
415 /* source pathname */
416 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
417 return get_afp_errno(AFPERR_NOOBJ);
421 newname = obj->newtmp;
422 oldname = obj->oldtmp;
423 isdir = path_isadir(path);
424 if ( *path->m_name != '\0' ) {
425 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
427 /* curdir parent dir, need to move sdir back */
432 if ( sdir->d_did == DIRDID_ROOT ) { /* root directory */
433 return( AFPERR_NORENAME );
435 /* move to destination dir */
436 if ( movecwd( vol, dirlookup(vol, sdir->d_pdid) ) < 0 ) {
439 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) +1);
442 /* another place where we know about the path type */
443 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
444 return( AFPERR_PARAM );
448 return AFP_OK; /* newname == oldname same dir */
451 rc = moveandrename(vol, sdir, -1, oldname, newname, isdir);
452 if ( rc == AFP_OK ) {
453 setvoltime(obj, vol );
459 /* ------------------------------- */
460 int afp_delete(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
472 memcpy( &vid, ibuf, sizeof( vid ));
473 ibuf += sizeof( vid );
474 if (NULL == ( vol = getvolbyvid( vid )) ) {
475 return( AFPERR_PARAM );
478 if (vol->v_flags & AFPVOL_RO)
481 memcpy( &did, ibuf, sizeof( did ));
482 ibuf += sizeof( int );
484 if (NULL == ( dir = dirlookup( vol, did )) ) {
488 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
489 return get_afp_errno(AFPERR_NOOBJ);
492 upath = s_path->u_name;
493 if ( path_isadir( s_path) ) {
494 if (*s_path->m_name != '\0' || curdir->d_did == DIRDID_ROOT)
497 rc = deletecurdir( vol);
498 } else if (of_findname(s_path)) {
501 /* it's a file st_valid should always be true
502 * only test for ENOENT because EACCES needs
503 * to read meta data in deletefile
505 if (s_path->st_valid && s_path->st_errno == ENOENT) {
509 rc = deletefile(vol, -1, upath, 1);
511 struct dir *cachedfile;
512 if ((cachedfile = dircache_search_by_name(vol, dir, upath, strlen(upath), s_path->st.st_ctime))) {
513 dircache_remove(vol, cachedfile, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
514 dir_free(cachedfile);
518 if ( rc == AFP_OK ) {
520 setvoltime(obj, vol );
525 /* ------------------------ */
526 char *absupath(const struct vol *vol, struct dir *dir, char *u)
528 static char pathbuf[MAXPATHLEN + 1];
531 if (u == NULL || dir == NULL || vol == NULL)
534 if ((path = bstrcpy(dir->d_fullpath)) == NULL)
536 if (bcatcstr(path, "/") != BSTR_OK)
538 if (bcatcstr(path, u) != BSTR_OK)
540 if (path->slen > MAXPATHLEN)
543 LOG(log_debug, logtype_afpd, "absupath: %s", cfrombstr(path));
545 strncpy(pathbuf, cfrombstr(path), blength(path) + 1);
551 char *ctoupath(const struct vol *vol, struct dir *dir, char *name)
553 if (vol == NULL || dir == NULL || name == NULL)
555 return absupath(vol, dir, mtoupath(vol, name, dir->d_did, utf8_encoding()));
558 /* ------------------------- */
559 int afp_moveandrename(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
562 struct dir *sdir, *ddir;
564 char *oldname, *newname;
577 memcpy( &vid, ibuf, sizeof( vid ));
578 ibuf += sizeof( vid );
579 if (NULL == ( vol = getvolbyvid( vid )) ) {
580 return( AFPERR_PARAM );
583 if (vol->v_flags & AFPVOL_RO)
586 /* source did followed by dest did */
587 memcpy( &did, ibuf, sizeof( did ));
588 ibuf += sizeof( int );
589 if (NULL == ( sdir = dirlookup( vol, did )) ) {
590 return afp_errno; /* was AFPERR_PARAM */
593 memcpy( &did, ibuf, sizeof( did ));
594 ibuf += sizeof( int );
596 /* source pathname */
597 if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
598 return get_afp_errno(AFPERR_NOOBJ);
602 newname = obj->newtmp;
603 oldname = obj->oldtmp;
605 isdir = path_isadir(path);
606 if ( *path->m_name != '\0' ) {
610 strcpy(oldname, path->m_name); /* an extra copy for of_rename */
612 memcpy(oldname, cfrombstr(sdir->d_m_name), blength(sdir->d_m_name) + 1);
616 if ((sdir_fd = open(".", O_RDONLY)) == -1)
620 /* get the destination directory */
621 if (NULL == ( ddir = dirlookup( vol, did )) ) {
622 rc = afp_errno; /* was AFPERR_PARAM */
625 if (NULL == ( path = cname( vol, ddir, &ibuf ))) {
629 pdid = curdir->d_did;
630 if ( *path->m_name != '\0' ) {
631 rc = path_error(path, AFPERR_NOOBJ);
635 /* one more place where we know about path type */
636 if ((plen = copy_path_name(vol, newname, ibuf)) < 0) {
642 strcpy(newname, oldname);
645 /* This does the work */
646 LOG(log_debug, logtype_afpd, "afp_move(oldname:'%s', newname:'%s', isdir:%u)",
647 oldname, newname, isdir);
648 rc = moveandrename(vol, sdir, sdir_fd, oldname, newname, isdir);
650 if ( rc == AFP_OK ) {
651 char *upath = mtoupath(vol, newname, pdid, utf8_encoding());
659 /* if unix priv don't try to match perm with dest folder */
660 if (!isdir && !vol_unix_priv(vol)) {
661 int admode = ad_mode("", 0777) | vol->v_fperm;
663 setfilmode(upath, admode, NULL, vol->v_umask);
664 vol->vfs->vfs_setfilmode(vol, upath, admode, NULL);
666 setvoltime(obj, vol );
678 int veto_file(const char*veto_str, const char*path)
679 /* given a veto_str like "abc/zxc/" and path "abc", return 1
680 * veto_str should be '/' delimited
681 * if path matches any one of the veto_str elements exactly, then 1 is returned
682 * otherwise, 0 is returned.
685 int i; /* index to veto_str */
686 int j; /* index to path */
688 if ((veto_str == NULL) || (path == NULL))
691 for(i=0, j=0; veto_str[i] != '\0'; i++) {
692 if (veto_str[i] == '/') {
693 if ((j>0) && (path[j] == '\0')) {
694 LOG(log_debug, logtype_afpd, "vetoed file:'%s'", path);
699 if (veto_str[i] != path[j]) {
700 while ((veto_str[i] != '/')
701 && (veto_str[i] != '\0'))