2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
11 #include <sys/syslog.h>
12 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <netatalk/endian.h>
16 #include <atalk/adouble.h>
17 #include <atalk/afp.h>
18 #include <atalk/util.h>
19 #include <atalk/cnid.h>
27 #include "directory.h"
35 int matchfile2dirperms(upath, vol, did)
36 /* Since it's kinda' big; I decided against an
41 /* The below code changes the way file ownership is determined in the name of
42 fixing dropboxes. It has known security problem. See the netatalk FAQ for
51 syslog (LOG_INFO, "begin matchfile2dirperms:");
54 if (stat(upath, &st ) < 0)
55 syslog(LOG_ERR, "Could not stat %s: %m", upath);
56 strcpy (adpath, "./.AppleDouble/");
57 strcat (adpath, upath);
58 if (( dir = dirsearch( vol, did )) == NULL ) {
59 syslog (LOG_ERR, "matchfile2dirperms: Unable to get directory info.");
60 return( AFPERR_NOOBJ );
62 else if (stat(".", &sb) < 0) {
64 "matchfile2dirperms: Error checking directory \"%s\": %m",
66 return(AFPERR_NOOBJ );
70 if ( uid != sb.st_uid )
73 if (lchown(upath, sb.st_uid, sb.st_gid) < 0)
76 "matchfile2dirperms: Error changing owner/gid of %s: %m", upath);
77 return (AFPERR_ACCESS);
79 if (chmod(upath,(st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
82 "matchfile2dirperms: Error adding file read permissions: %m");
83 return (AFPERR_ACCESS);
88 "matchfile2dirperms: Added S_IRGRP and S_IROTH: %m");
90 if (lchown(adpath, sb.st_uid, sb.st_gid) < 0)
93 "matchfile2dirperms: Error changing AppleDouble owner/gid %s: %m",
95 return (AFPERR_ACCESS);
97 if (chmod(adpath, (st.st_mode&0x0FFFF)| S_IRGRP| S_IROTH) < 0)
100 "matchfile2dirperms: Error adding AD file read permissions: %m");
101 return (AFPERR_ACCESS);
106 "matchfile2dirperms: Added S_IRGRP and S_IROTH to AD: %m");
112 "matchfile2dirperms: No ownership change necessary.");
114 } /* end else if stat success */
115 seteuid(uid); /* Restore process ownership to normal */
117 syslog (LOG_INFO, "end matchfile2dirperms:");
125 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
128 int ibuflen, *rbuflen;
136 u_int16_t fbitmap, dbitmap, vid;
139 syslog(LOG_INFO, "begin afp_getfildirparams:");
145 memcpy( &vid, ibuf, sizeof( vid ));
146 ibuf += sizeof( vid );
147 if (( vol = getvolbyvid( vid )) == NULL ) {
148 return( AFPERR_PARAM );
151 memcpy( &did, ibuf, sizeof( did ));
152 ibuf += sizeof( did );
154 if (( dir = dirsearch( vol, did )) == NULL ) {
155 return( AFPERR_NOOBJ );
158 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
159 fbitmap = ntohs( fbitmap );
160 ibuf += sizeof( fbitmap );
161 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
162 dbitmap = ntohs( dbitmap );
163 ibuf += sizeof( dbitmap );
165 if (( path = cname( vol, dir, &ibuf )) == NULL) {
166 return( AFPERR_NOOBJ );
169 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
170 return( AFPERR_NOOBJ );
174 if (S_ISDIR(st.st_mode)) {
175 if (dbitmap && ( ret = getdirparams(vol, dbitmap, ".", curdir,
176 &st, rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
179 /* this is a directory */
180 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISDIR;
182 if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
183 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
187 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
189 *rbuflen = buflen + 3 * sizeof( u_int16_t );
190 fbitmap = htons( fbitmap );
191 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
192 rbuf += sizeof( fbitmap );
193 dbitmap = htons( dbitmap );
194 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
195 rbuf += sizeof( dbitmap ) + sizeof( u_char );
199 syslog(LOG_INFO, "end afp_getfildirparams:");
205 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
208 int ibuflen, *rbuflen;
214 u_int16_t vid, bitmap;
218 syslog(LOG_INFO, "begin afp_setfildirparams:");
223 memcpy( &vid, ibuf, sizeof(vid));
224 ibuf += sizeof( vid );
226 if (( vol = getvolbyvid( vid )) == NULL ) {
227 return( AFPERR_PARAM );
230 if (vol->v_flags & AFPVOL_RO)
233 memcpy( &did, ibuf, sizeof( did));
234 ibuf += sizeof( did);
236 if (( dir = dirsearch( vol, did )) == NULL ) {
237 return( AFPERR_NOOBJ );
240 memcpy( &bitmap, ibuf, sizeof( bitmap ));
241 bitmap = ntohs( bitmap );
242 ibuf += sizeof( bitmap );
244 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
245 return( AFPERR_NOOBJ );
248 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
249 return( AFPERR_NOOBJ );
253 * If ibuf is odd, make it even.
255 if ((u_long)ibuf & 1 ) {
259 if (S_ISDIR(st.st_mode)) {
260 rc = setdirparams(vol, path, bitmap, ibuf );
262 rc = setfilparams(vol, path, bitmap, ibuf );
264 if ( rc == AFP_OK ) {
265 setvoltime(obj, vol );
269 syslog(LOG_INFO, "end afp_setfildirparams:");
275 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
278 int ibuflen, *rbuflen;
283 struct dir *dir, *odir = NULL;
284 char *path, *buf, *upath, *newpath;
289 #if AD_VERSION > AD_VERSION1
294 syslog(LOG_INFO, "begin afp_rename:");
300 memcpy( &vid, ibuf, sizeof( vid ));
301 ibuf += sizeof( vid );
302 if (( vol = getvolbyvid( vid )) == NULL ) {
303 return( AFPERR_PARAM );
306 if (vol->v_flags & AFPVOL_RO)
309 memcpy( &did, ibuf, sizeof( did ));
310 ibuf += sizeof( did );
311 if (( dir = dirsearch( vol, did )) == NULL ) {
312 return( AFPERR_NOOBJ );
315 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
316 return( AFPERR_NOOBJ );
319 /* another place where we know about the path type */
320 if ( *ibuf++ != 2 ) {
321 return( AFPERR_PARAM );
323 plen = (unsigned char) *ibuf++;
324 *( ibuf + plen ) = '\0';
326 if ( *path == '\0' ) {
327 if ( curdir->d_parent == NULL ) { /* root directory */
328 return( AFPERR_NORENAME );
331 path = curdir->d_name;
332 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
333 return( AFPERR_NOOBJ );
338 if ( strcasecmp( path, ibuf ) == 0 ) {
343 /* if a curdir/newname ofork exists, return busy */
344 if (of_findname(vol, curdir, ibuf))
347 /* source == destination. just say okay. */
348 if (strcmp(path, ibuf) == 0)
351 /* check for illegal characters */
352 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
353 strpbrk(ibuf, MSWINDOWS_BADCHARS))
356 newpath = obj->oldtmp;
357 strcpy( newpath, mtoupath(vol, ibuf ));
359 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(newpath, '/'))
362 if (!validupath(vol, newpath))
365 /* the strdiacasecmp deals with case-insensitive, case preserving
367 if (stat( newpath, &st ) == 0 && strdiacasecmp(path, ibuf))
368 return( AFPERR_EXIST );
370 upath = mtoupath(vol, path);
372 #if AD_VERSION > AD_VERSION1
373 id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
376 if ( rename( upath, newpath ) < 0 ) {
379 return( AFPERR_NOOBJ );
381 return( AFPERR_ACCESS );
383 return( AFPERR_PARAM );
387 #if AD_VERSION > AD_VERSION1
388 if (stat(newpath, &st) < 0) /* this shouldn't fail */
390 cnid_update(vol->v_db, id, &st, curdir->d_did, newpath, strlen(newpath));
394 newadpath = obj->newtmp;
395 strcpy( newadpath, ad_path( newpath, 0 ));
396 if ( rename( ad_path( upath, 0 ), newadpath ) < 0 ) {
397 if ( errno == ENOENT ) { /* no adouble header file */
398 if (( unlink( newadpath ) < 0 ) && ( errno != ENOENT )) {
399 return( AFPERR_PARAM );
403 return( AFPERR_PARAM );
406 memset(&ad, 0, sizeof(ad));
407 if ( ad_open( newpath, ADFLAGS_HF, O_RDWR|O_CREAT, 0666,
409 return( AFPERR_PARAM );
414 memset(&ad, 0, sizeof(ad));
415 if ( ad_open( newpath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
416 O_RDWR|O_CREAT, 0666, &ad) < 0 ) {
417 if (!((errno == ENOENT) && vol_noadouble(vol)))
418 return( AFPERR_PARAM );
421 if ((buf = realloc( odir->d_name, plen + 1 )) == NULL ) {
422 syslog( LOG_ERR, "afp_rename: realloc: %m" );
424 ad_flush(&ad, ADFLAGS_HF); /* in case of create */
425 ad_close(&ad, ADFLAGS_HF);
430 strcpy( odir->d_name, ibuf );
435 ad_setentrylen( &ad, ADEID_NAME, plen );
436 memcpy( ad_entry( &ad, ADEID_NAME ), ibuf, plen );
437 ad_flush( &ad, ADFLAGS_HF );
438 ad_close( &ad, ADFLAGS_HF );
441 setvoltime(obj, vol );
443 /* if it's still open, rename the ofork as well. */
444 if (of_rename(vol, curdir, path, curdir, ibuf) < 0)
448 syslog(LOG_INFO, "end afp_rename:");
455 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
458 int ibuflen, *rbuflen;
467 syslog(LOG_INFO, "begin afp_delete:");
473 memcpy( &vid, ibuf, sizeof( vid ));
474 ibuf += sizeof( vid );
475 if (( vol = getvolbyvid( vid )) == NULL ) {
476 return( AFPERR_PARAM );
479 if (vol->v_flags & AFPVOL_RO)
482 memcpy( &did, ibuf, sizeof( did ));
483 ibuf += sizeof( int );
484 if (( dir = dirsearch( vol, did )) == NULL ) {
485 return( AFPERR_NOOBJ );
488 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
489 return( AFPERR_NOOBJ );
492 if ( *path == '\0' ) {
493 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
494 } else if (of_findname(vol, curdir, path)) {
496 } else if ((rc = deletefile( upath = mtoupath(vol, path ))) == AFP_OK) {
497 #if AD_VERSION > AD_VERSION1 /* get rid of entry */
498 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
499 cnid_delete(vol->v_db, id);
502 if ( rc == AFP_OK ) {
503 setvoltime(obj, vol );
507 syslog(LOG_INFO, "end afp_delete:");
513 char *ctoupath( vol, dir, name )
514 const struct vol *vol;
519 static char path[ MAXPATHLEN + 1];
523 p = path + sizeof( path ) - 1;
525 u = mtoupath(vol, name );
528 strncpy( p, u, len );
529 for ( d = dir; d->d_parent; d = d->d_parent ) {
531 u = mtoupath(vol, d->d_name );
534 strncpy( p, u, len );
537 len = strlen( vol->v_path );
539 strncpy( p, vol->v_path, len );
545 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
548 int ibuflen, *rbuflen;
551 struct dir *sdir, *ddir, *odir = NULL;
553 char *oldname, *newname;
554 char *path, *p, *upath;
558 #if AD_VERSION > AD_VERSION1
563 syslog(LOG_INFO, "begin afp_moveandrename:");
569 memcpy( &vid, ibuf, sizeof( vid ));
570 ibuf += sizeof( vid );
571 if (( vol = getvolbyvid( vid )) == NULL ) {
572 return( AFPERR_PARAM );
575 if (vol->v_flags & AFPVOL_RO)
578 /* source did followed by dest did */
579 memcpy( &did, ibuf, sizeof( did ));
580 ibuf += sizeof( int );
581 if (( sdir = dirsearch( vol, did )) == NULL ) {
582 return( AFPERR_PARAM );
585 memcpy( &did, ibuf, sizeof( did ));
586 ibuf += sizeof( int );
588 /* source pathname */
589 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
590 return( AFPERR_NOOBJ );
594 newname = obj->newtmp;
595 oldname = obj->oldtmp;
596 if ( *path != '\0' ) {
597 /* not a directory */
598 strcpy(newname, path);
599 strcpy(oldname, path); /* an extra copy for of_rename */
600 #if AD_VERSION > AD_VERSION1
601 p = mtoupath(vol, path);
602 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
604 p = ctoupath( vol, sdir, newname );
607 strcpy( newname, odir->d_name );
608 strcpy(oldname, odir->d_name);
609 p = ctoupath( vol, odir->d_parent, newname );
610 #if AD_VERSION > AD_VERSION1
611 id = curdir->d_did; /* we already have the CNID */
615 * p now points to the full pathname of the source fs object.
618 /* get the destination directory */
619 if (( ddir = dirsearch( vol, did )) == NULL ) {
620 return( AFPERR_PARAM );
622 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
623 return( AFPERR_NOOBJ );
625 if ( *path != '\0' ) {
626 return( AFPERR_BADTYPE );
629 /* one more place where we know about path type */
630 if ( *ibuf++ != 2 ) {
631 return( AFPERR_PARAM );
634 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
635 strncpy( newname, ibuf, plen );
636 newname[ plen ] = '\0';
639 /* check for illegal characters */
640 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
641 strpbrk(newname, MSWINDOWS_BADCHARS))
644 upath = mtoupath(vol, newname);
646 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
649 if (!validupath(vol, upath))
652 /* source == destination. we just silently accept this. */
653 if (curdir == sdir) {
654 if (strcmp(oldname, newname) == 0)
657 /* deal with case insensitive, case-preserving filesystems. */
658 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
661 } else if (stat(upath, &st ) == 0)
662 return( AFPERR_EXIST );
665 if (of_findname(vol, curdir, newname)) {
667 } else if ((rc = renamefile( p, upath, newname,
668 vol_noadouble(vol) )) == AFP_OK) {
669 /* if it's still open, rename the ofork as well. */
670 rc = of_rename(vol, sdir, oldname, curdir, newname);
673 rc = renamedir(p, upath, odir, curdir, newname, vol_noadouble(vol));
677 if (retvalue=matchfile2dirperms (newname, vol, did) != AFP_OK)
681 if ( rc == AFP_OK ) {
682 #if AD_VERSION > AD_VERSION1
683 /* renaming may have moved the file/dir across a filesystem */
684 if (stat(upath, &st) < 0)
687 /* fix up the catalog entry */
688 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
690 setvoltime(obj, vol );
694 syslog(LOG_INFO, "end afp_moveandrename:");