file_subs.c revision 90113
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1992 Keith Muller.
31556Srgrimes * Copyright (c) 1992, 1993
41556Srgrimes *	The Regents of the University of California.  All rights reserved.
51556Srgrimes *
61556Srgrimes * This code is derived from software contributed to Berkeley by
71556Srgrimes * Keith Muller of the University of California, San Diego.
81556Srgrimes *
91556Srgrimes * Redistribution and use in source and binary forms, with or without
101556Srgrimes * modification, are permitted provided that the following conditions
111556Srgrimes * are met:
121556Srgrimes * 1. Redistributions of source code must retain the above copyright
131556Srgrimes *    notice, this list of conditions and the following disclaimer.
141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151556Srgrimes *    notice, this list of conditions and the following disclaimer in the
161556Srgrimes *    documentation and/or other materials provided with the distribution.
171556Srgrimes * 3. All advertising materials mentioning features or use of this software
181556Srgrimes *    must display the following acknowledgement:
191556Srgrimes *	This product includes software developed by the University of
201556Srgrimes *	California, Berkeley and its contributors.
211556Srgrimes * 4. Neither the name of the University nor the names of its contributors
221556Srgrimes *    may be used to endorse or promote products derived from this software
231556Srgrimes *    without specific prior written permission.
241556Srgrimes *
251556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351556Srgrimes * SUCH DAMAGE.
361556Srgrimes */
371556Srgrimes
381556Srgrimes#ifndef lint
3936049Scharnier#if 0
4036049Scharnierstatic char sccsid[] = "@(#)file_subs.c	8.1 (Berkeley) 5/31/93";
4136049Scharnier#endif
4236049Scharnierstatic const char rcsid[] =
4350471Speter  "$FreeBSD: head/bin/pax/file_subs.c 90113 2002-02-02 07:07:59Z imp $";
441556Srgrimes#endif /* not lint */
451556Srgrimes
461556Srgrimes#include <sys/types.h>
471556Srgrimes#include <sys/time.h>
481556Srgrimes#include <sys/stat.h>
491556Srgrimes#include <unistd.h>
501556Srgrimes#include <fcntl.h>
511556Srgrimes#include <string.h>
521556Srgrimes#include <stdio.h>
531556Srgrimes#include <errno.h>
541556Srgrimes#include <sys/uio.h>
551556Srgrimes#include <stdlib.h>
561556Srgrimes#include "pax.h"
5776351Skris#include "options.h"
581556Srgrimes#include "extern.h"
591556Srgrimes
601556Srgrimesstatic int
6190113Simpmk_link(char *,struct stat *,char *, int);
621556Srgrimes
631556Srgrimes/*
641556Srgrimes * routines that deal with file operations such as: creating, removing;
651556Srgrimes * and setting access modes, uid/gid and times of files
661556Srgrimes */
671556Srgrimes
681556Srgrimes#define FILEBITS		(S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
691556Srgrimes#define SETBITS			(S_ISUID | S_ISGID)
701556Srgrimes#define ABITS			(FILEBITS | SETBITS)
711556Srgrimes
721556Srgrimes/*
731556Srgrimes * file_creat()
741556Srgrimes *	Create and open a file.
751556Srgrimes * Return:
761556Srgrimes *	file descriptor or -1 for failure
771556Srgrimes */
781556Srgrimes
791556Srgrimesint
8090113Simpfile_creat(ARCHD *arcn)
811556Srgrimes{
821556Srgrimes	int fd = -1;
831556Srgrimes	mode_t file_mode;
841556Srgrimes	int oerrno;
851556Srgrimes
861556Srgrimes	/*
871556Srgrimes	 * assume file doesn't exist, so just try to create it, most times this
881556Srgrimes	 * works. We have to take special handling when the file does exist. To
891556Srgrimes	 * detect this, we use O_EXCL. For example when trying to create a
901556Srgrimes	 * file and a character device or fifo exists with the same name, we
911556Srgrimes	 * can accidently open the device by mistake (or block waiting to open)
9246684Skris	 * If we find that the open has failed, then figure spend the effort to
931556Srgrimes	 * figure out why. This strategy was found to have better average
941556Srgrimes	 * performance in common use than checking the file (and the path)
951556Srgrimes	 * first with lstat.
961556Srgrimes	 */
971556Srgrimes	file_mode = arcn->sb.st_mode & FILEBITS;
981556Srgrimes	if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
991556Srgrimes	    file_mode)) >= 0)
1001556Srgrimes		return(fd);
1011556Srgrimes
1021556Srgrimes	/*
1031556Srgrimes	 * the file seems to exist. First we try to get rid of it (found to be
1041556Srgrimes	 * the second most common failure when traced). If this fails, only
1051556Srgrimes	 * then we go to the expense to check and create the path to the file
1061556Srgrimes	 */
1071556Srgrimes	if (unlnk_exist(arcn->name, arcn->type) != 0)
1081556Srgrimes		return(-1);
1091556Srgrimes
1101556Srgrimes	for (;;) {
1111556Srgrimes		/*
1121556Srgrimes		 * try to open it again, if this fails, check all the nodes in
1131556Srgrimes		 * the path and give it a final try. if chk_path() finds that
1141556Srgrimes		 * it cannot fix anything, we will skip the last attempt
1151556Srgrimes		 */
1161556Srgrimes		if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC,
1171556Srgrimes		    file_mode)) >= 0)
1181556Srgrimes			break;
1191556Srgrimes		oerrno = errno;
12076351Skris		if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
12176017Skris			syswarn(1, oerrno, "Unable to create %s", arcn->name);
1221556Srgrimes			return(-1);
1231556Srgrimes		}
1241556Srgrimes	}
1251556Srgrimes	return(fd);
1261556Srgrimes}
1271556Srgrimes
1281556Srgrimes/*
1291556Srgrimes * file_close()
1301556Srgrimes *	Close file descriptor to a file just created by pax. Sets modes,
1311556Srgrimes *	ownership and times as required.
1321556Srgrimes * Return:
1331556Srgrimes *	0 for success, -1 for failure
1341556Srgrimes */
1351556Srgrimes
1361556Srgrimesvoid
13790113Simpfile_close(ARCHD *arcn, int fd)
1381556Srgrimes{
1391556Srgrimes	int res = 0;
1401556Srgrimes
1411556Srgrimes	if (fd < 0)
1421556Srgrimes		return;
1431556Srgrimes	if (close(fd) < 0)
14476017Skris		syswarn(0, errno, "Unable to close file descriptor on %s",
1451556Srgrimes		    arcn->name);
1461556Srgrimes
1471556Srgrimes	/*
1481556Srgrimes	 * set owner/groups first as this may strip off mode bits we want
1491556Srgrimes	 * then set file permission modes. Then set file access and
1508855Srgrimes	 * modification times.
1511556Srgrimes	 */
1521556Srgrimes	if (pids)
1531556Srgrimes		res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid);
1541556Srgrimes
1551556Srgrimes	/*
1561556Srgrimes	 * IMPORTANT SECURITY NOTE:
1571556Srgrimes	 * if not preserving mode or we cannot set uid/gid, then PROHIBIT
1581556Srgrimes	 * set uid/gid bits
1591556Srgrimes	 */
1601556Srgrimes	if (!pmode || res)
1611556Srgrimes		arcn->sb.st_mode &= ~(SETBITS);
1621556Srgrimes	if (pmode)
1631556Srgrimes		set_pmode(arcn->name, arcn->sb.st_mode);
1641556Srgrimes	if (patime || pmtime)
1651556Srgrimes		set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
1661556Srgrimes}
1671556Srgrimes
1681556Srgrimes/*
1691556Srgrimes * lnk_creat()
1701556Srgrimes *	Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
1718855Srgrimes *	must exist;
1721556Srgrimes * Return:
1731556Srgrimes *	0 if ok, -1 otherwise
1741556Srgrimes */
1751556Srgrimes
1761556Srgrimesint
17790113Simplnk_creat(ARCHD *arcn)
1781556Srgrimes{
1791556Srgrimes	struct stat sb;
1801556Srgrimes
1811556Srgrimes	/*
1821556Srgrimes	 * we may be running as root, so we have to be sure that link target
1831556Srgrimes	 * is not a directory, so we lstat and check
1841556Srgrimes	 */
1851556Srgrimes	if (lstat(arcn->ln_name, &sb) < 0) {
18676017Skris		syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name,
1871556Srgrimes		    arcn->name);
1881556Srgrimes		return(-1);
1891556Srgrimes	}
1901556Srgrimes
1911556Srgrimes	if (S_ISDIR(sb.st_mode)) {
19276017Skris		paxwarn(1, "A hard link to the directory %s is not allowed",
1931556Srgrimes		    arcn->ln_name);
1941556Srgrimes		return(-1);
1951556Srgrimes	}
1961556Srgrimes
1971556Srgrimes	return(mk_link(arcn->ln_name, &sb, arcn->name, 0));
1981556Srgrimes}
1991556Srgrimes
2001556Srgrimes/*
2011556Srgrimes * cross_lnk()
2021556Srgrimes *	Create a hard link to arcn->org_name from arcn->name. Only used in copy
20376017Skris *	with the -l flag. No warning or error if this does not succeed (we will
2041556Srgrimes *	then just create the file)
2051556Srgrimes * Return:
2061556Srgrimes *	1 if copy() should try to create this file node
2071556Srgrimes *	0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
2081556Srgrimes */
2091556Srgrimes
2101556Srgrimesint
21190113Simpcross_lnk(ARCHD *arcn)
2121556Srgrimes{
2131556Srgrimes	/*
21446684Skris	 * try to make a link to original file (-l flag in copy mode). make sure
2151556Srgrimes	 * we do not try to link to directories in case we are running as root
2161556Srgrimes	 * (and it might succeed).
2171556Srgrimes	 */
2181556Srgrimes	if (arcn->type == PAX_DIR)
2191556Srgrimes		return(1);
2201556Srgrimes	return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1));
2211556Srgrimes}
2221556Srgrimes
2231556Srgrimes/*
2241556Srgrimes * chk_same()
2251556Srgrimes *	In copy mode if we are not trying to make hard links between the src
2261556Srgrimes *	and destinations, make sure we are not going to overwrite ourselves by
2271556Srgrimes *	accident. This slows things down a little, but we have to protect all
2281556Srgrimes *	those people who make typing errors.
2291556Srgrimes * Return:
2301556Srgrimes *	1 the target does not exist, go ahead and copy
2311556Srgrimes *	0 skip it file exists (-k) or may be the same as source file
2321556Srgrimes */
2331556Srgrimes
2341556Srgrimesint
23590113Simpchk_same(ARCHD *arcn)
2361556Srgrimes{
2371556Srgrimes	struct stat sb;
2381556Srgrimes
2398855Srgrimes	/*
2401556Srgrimes	 * if file does not exist, return. if file exists and -k, skip it
2411556Srgrimes	 * quietly
2421556Srgrimes	 */
2431556Srgrimes	if (lstat(arcn->name, &sb) < 0)
2441556Srgrimes		return(1);
2451556Srgrimes	if (kflag)
2461556Srgrimes		return(0);
2471556Srgrimes
2481556Srgrimes	/*
2491556Srgrimes	 * better make sure the user does not have src == dest by mistake
2501556Srgrimes	 */
2511556Srgrimes	if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) {
25276017Skris		paxwarn(1, "Unable to copy %s, file would overwrite itself",
2531556Srgrimes		    arcn->name);
2541556Srgrimes		return(0);
2551556Srgrimes	}
2561556Srgrimes	return(1);
2571556Srgrimes}
2581556Srgrimes
2591556Srgrimes/*
2601556Srgrimes * mk_link()
2611556Srgrimes *	try to make a hard link between two files. if ign set, we do not
2621556Srgrimes *	complain.
2631556Srgrimes * Return:
2641556Srgrimes *	0 if successful (or we are done with this file but no error, such as
2651556Srgrimes *	finding the from file exists and the user has set -k).
2661556Srgrimes *	1 when ign was set to indicates we could not make the link but we
2671556Srgrimes *	should try to copy/extract the file as that might work (and is an
2681556Srgrimes *	allowed option). -1 an error occurred.
2691556Srgrimes */
2701556Srgrimes
2711556Srgrimesstatic int
27290113Simpmk_link(char *to, struct stat *to_sb, char *from,
2731556Srgrimes	int ign)
2741556Srgrimes{
2751556Srgrimes	struct stat sb;
2761556Srgrimes	int oerrno;
2771556Srgrimes
2781556Srgrimes	/*
2791556Srgrimes	 * if from file exists, it has to be unlinked to make the link. If the
2801556Srgrimes	 * file exists and -k is set, skip it quietly
2811556Srgrimes	 */
2821556Srgrimes	if (lstat(from, &sb) == 0) {
2831556Srgrimes		if (kflag)
2841556Srgrimes			return(0);
2851556Srgrimes
2861556Srgrimes		/*
2871556Srgrimes		 * make sure it is not the same file, protect the user
2881556Srgrimes		 */
2891556Srgrimes		if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) {
29076017Skris			paxwarn(1, "Unable to link file %s to itself", to);
2911556Srgrimes			return(-1);;
2921556Srgrimes		}
2931556Srgrimes
2941556Srgrimes		/*
2951556Srgrimes		 * try to get rid of the file, based on the type
2961556Srgrimes		 */
2971556Srgrimes		if (S_ISDIR(sb.st_mode)) {
2981556Srgrimes			if (rmdir(from) < 0) {
29976017Skris				syswarn(1, errno, "Unable to remove %s", from);
3001556Srgrimes				return(-1);
3011556Srgrimes			}
3021556Srgrimes		} else if (unlink(from) < 0) {
3031556Srgrimes			if (!ign) {
30476017Skris				syswarn(1, errno, "Unable to remove %s", from);
3051556Srgrimes				return(-1);
3061556Srgrimes			}
3071556Srgrimes			return(1);
3081556Srgrimes		}
3091556Srgrimes	}
3101556Srgrimes
3111556Srgrimes	/*
3121556Srgrimes	 * from file is gone (or did not exist), try to make the hard link.
3131556Srgrimes	 * if it fails, check the path and try it again (if chk_path() says to
3141556Srgrimes	 * try again)
3151556Srgrimes	 */
3161556Srgrimes	for (;;) {
3171556Srgrimes		if (link(to, from) == 0)
3181556Srgrimes			break;
3191556Srgrimes		oerrno = errno;
32076351Skris		if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0)
3211556Srgrimes			continue;
3221556Srgrimes		if (!ign) {
32376017Skris			syswarn(1, oerrno, "Could not link to %s from %s", to,
3241556Srgrimes			    from);
3251556Srgrimes			return(-1);
3261556Srgrimes		}
3271556Srgrimes		return(1);
3281556Srgrimes	}
3291556Srgrimes
3301556Srgrimes	/*
3311556Srgrimes	 * all right the link was made
3321556Srgrimes	 */
3331556Srgrimes	return(0);
3341556Srgrimes}
3351556Srgrimes
3361556Srgrimes/*
3371556Srgrimes * node_creat()
3381556Srgrimes *	create an entry in the file system (other than a file or hard link).
3391556Srgrimes *	If successful, sets uid/gid modes and times as required.
3401556Srgrimes * Return:
3411556Srgrimes *	0 if ok, -1 otherwise
3421556Srgrimes */
3431556Srgrimes
3441556Srgrimesint
34590113Simpnode_creat(ARCHD *arcn)
3461556Srgrimes{
34790113Simp	int res;
34890113Simp	int ign = 0;
34990113Simp	int oerrno;
35090113Simp	int pass = 0;
3511556Srgrimes	mode_t file_mode;
3521556Srgrimes	struct stat sb;
3531556Srgrimes
3541556Srgrimes	/*
3551556Srgrimes	 * create node based on type, if that fails try to unlink the node and
3561556Srgrimes	 * try again. finally check the path and try again. As noted in the
3571556Srgrimes	 * file and link creation routines, this method seems to exhibit the
3581556Srgrimes	 * best performance in general use workloads.
3591556Srgrimes	 */
3601556Srgrimes	file_mode = arcn->sb.st_mode & FILEBITS;
3611556Srgrimes
3621556Srgrimes	for (;;) {
3631556Srgrimes		switch(arcn->type) {
3641556Srgrimes		case PAX_DIR:
3651556Srgrimes			res = mkdir(arcn->name, file_mode);
3661556Srgrimes			if (ign)
3671556Srgrimes				res = 0;
3681556Srgrimes			break;
3691556Srgrimes		case PAX_CHR:
3701556Srgrimes			file_mode |= S_IFCHR;
3711556Srgrimes			res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
3721556Srgrimes			break;
3731556Srgrimes		case PAX_BLK:
3741556Srgrimes			file_mode |= S_IFBLK;
3751556Srgrimes			res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
3761556Srgrimes			break;
3771556Srgrimes		case PAX_FIF:
3781556Srgrimes			res = mkfifo(arcn->name, file_mode);
3791556Srgrimes			break;
3801556Srgrimes		case PAX_SCK:
3811556Srgrimes			/*
3821556Srgrimes			 * Skip sockets, operation has no meaning under BSD
3831556Srgrimes			 */
38476017Skris			paxwarn(0,
3851556Srgrimes			    "%s skipped. Sockets cannot be copied or extracted",
3861556Srgrimes			    arcn->name);
3871556Srgrimes			return(-1);
3881556Srgrimes		case PAX_SLK:
38976351Skris			res = symlink(arcn->ln_name, arcn->name);
3901556Srgrimes			break;
3911556Srgrimes		case PAX_CTG:
3921556Srgrimes		case PAX_HLK:
3931556Srgrimes		case PAX_HRG:
3941556Srgrimes		case PAX_REG:
3951556Srgrimes		default:
3961556Srgrimes			/*
3971556Srgrimes			 * we should never get here
3981556Srgrimes			 */
39976017Skris			paxwarn(0, "%s has an unknown file type, skipping",
4001556Srgrimes				arcn->name);
4011556Srgrimes			return(-1);
4021556Srgrimes		}
4031556Srgrimes
4041556Srgrimes		/*
4051556Srgrimes		 * if we were able to create the node break out of the loop,
4061556Srgrimes		 * otherwise try to unlink the node and try again. if that
4071556Srgrimes		 * fails check the full path and try a final time.
4081556Srgrimes		 */
4091556Srgrimes		if (res == 0)
4101556Srgrimes			break;
4111556Srgrimes
4121556Srgrimes		/*
4131556Srgrimes		 * we failed to make the node
4141556Srgrimes		 */
4151556Srgrimes		oerrno = errno;
4161556Srgrimes		if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0)
4171556Srgrimes			return(-1);
4181556Srgrimes
4191556Srgrimes		if (++pass <= 1)
4201556Srgrimes			continue;
4211556Srgrimes
42276351Skris		if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
42376017Skris			syswarn(1, oerrno, "Could not create: %s", arcn->name);
4241556Srgrimes			return(-1);
4251556Srgrimes		}
4261556Srgrimes	}
4271556Srgrimes
4281556Srgrimes	/*
4291556Srgrimes	 * we were able to create the node. set uid/gid, modes and times
4301556Srgrimes	 */
4311556Srgrimes	if (pids)
43276351Skris		res = ((arcn->type == PAX_SLK) ?
43376351Skris		    set_lids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid) :
43476351Skris		    set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid));
4351556Srgrimes	else
4361556Srgrimes		res = 0;
4371556Srgrimes
4381556Srgrimes	/*
43976351Skris	 * symlinks are done now.
44076351Skris	 */
44176351Skris	if (arcn->type == PAX_SLK)
44276351Skris		return(0);
44376351Skris
44476351Skris	/*
4451556Srgrimes	 * IMPORTANT SECURITY NOTE:
4461556Srgrimes	 * if not preserving mode or we cannot set uid/gid, then PROHIBIT any
4471556Srgrimes	 * set uid/gid bits
4481556Srgrimes	 */
4491556Srgrimes	if (!pmode || res)
4501556Srgrimes		arcn->sb.st_mode &= ~(SETBITS);
4511556Srgrimes	if (pmode)
4521556Srgrimes		set_pmode(arcn->name, arcn->sb.st_mode);
4531556Srgrimes
45476351Skris	if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) {
4551556Srgrimes		/*
4561556Srgrimes		 * Dirs must be processed again at end of extract to set times
4571556Srgrimes		 * and modes to agree with those stored in the archive. However
4581556Srgrimes		 * to allow extract to continue, we may have to also set owner
4591556Srgrimes		 * rights. This allows nodes in the archive that are children
4601556Srgrimes		 * of this directory to be extracted without failure. Both time
4611556Srgrimes		 * and modes will be fixed after the entire archive is read and
4621556Srgrimes		 * before pax exits.
4631556Srgrimes		 */
4641556Srgrimes		if (access(arcn->name, R_OK | W_OK | X_OK) < 0) {
4651556Srgrimes			if (lstat(arcn->name, &sb) < 0) {
46676017Skris				syswarn(0, errno,"Could not access %s (stat)",
4671556Srgrimes				    arcn->name);
4681556Srgrimes				set_pmode(arcn->name,file_mode | S_IRWXU);
4691556Srgrimes			} else {
4701556Srgrimes				/*
4711556Srgrimes				 * We have to add rights to the dir, so we make
4721556Srgrimes				 * sure to restore the mode. The mode must be
4731556Srgrimes				 * restored AS CREATED and not as stored if
4741556Srgrimes				 * pmode is not set.
4751556Srgrimes				 */
4761556Srgrimes				set_pmode(arcn->name,
4771556Srgrimes				    ((sb.st_mode & FILEBITS) | S_IRWXU));
4781556Srgrimes				if (!pmode)
4791556Srgrimes					arcn->sb.st_mode = sb.st_mode;
4801556Srgrimes			}
4811556Srgrimes
4821556Srgrimes			/*
4831556Srgrimes			 * we have to force the mode to what was set here,
4841556Srgrimes			 * since we changed it from the default as created.
4851556Srgrimes			 */
4861556Srgrimes			add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1);
4871556Srgrimes		} else if (pmode || patime || pmtime)
4881556Srgrimes			add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0);
4891556Srgrimes	}
4901556Srgrimes
4911556Srgrimes	if (patime || pmtime)
4921556Srgrimes		set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
4931556Srgrimes	return(0);
4941556Srgrimes}
4951556Srgrimes
4961556Srgrimes/*
4971556Srgrimes * unlnk_exist()
4981556Srgrimes *	Remove node from file system with the specified name. We pass the type
4991556Srgrimes *	of the node that is going to replace it. When we try to create a
5001556Srgrimes *	directory and find that it already exists, we allow processing to
5011556Srgrimes *	continue as proper modes etc will always be set for it later on.
5021556Srgrimes * Return:
5031556Srgrimes *	0 is ok to proceed, no file with the specified name exists
5041556Srgrimes *	-1 we were unable to remove the node, or we should not remove it (-k)
5051556Srgrimes *	1 we found a directory and we were going to create a directory.
5061556Srgrimes */
5071556Srgrimes
5081556Srgrimesint
50990113Simpunlnk_exist(char *name, int type)
5101556Srgrimes{
5111556Srgrimes	struct stat sb;
5121556Srgrimes
5131556Srgrimes	/*
5141556Srgrimes	 * the file does not exist, or -k we are done
5151556Srgrimes	 */
5161556Srgrimes	if (lstat(name, &sb) < 0)
5171556Srgrimes		return(0);
5181556Srgrimes	if (kflag)
5191556Srgrimes		return(-1);
5201556Srgrimes
5211556Srgrimes	if (S_ISDIR(sb.st_mode)) {
5221556Srgrimes		/*
5231556Srgrimes		 * try to remove a directory, if it fails and we were going to
5241556Srgrimes		 * create a directory anyway, tell the caller (return a 1)
5251556Srgrimes		 */
5261556Srgrimes		if (rmdir(name) < 0) {
5271556Srgrimes			if (type == PAX_DIR)
5288855Srgrimes				return(1);
52976017Skris			syswarn(1,errno,"Unable to remove directory %s", name);
5301556Srgrimes			return(-1);
5311556Srgrimes		}
5321556Srgrimes		return(0);
5331556Srgrimes	}
5341556Srgrimes
5351556Srgrimes	/*
5361556Srgrimes	 * try to get rid of all non-directory type nodes
5371556Srgrimes	 */
5381556Srgrimes	if (unlink(name) < 0) {
53976017Skris		syswarn(1, errno, "Could not unlink %s", name);
5401556Srgrimes		return(-1);
5411556Srgrimes	}
5421556Srgrimes	return(0);
5431556Srgrimes}
5441556Srgrimes
5451556Srgrimes/*
5461556Srgrimes * chk_path()
5471556Srgrimes *	We were trying to create some kind of node in the file system and it
5481556Srgrimes *	failed. chk_path() makes sure the path up to the node exists and is
5491556Srgrimes *	writeable. When we have to create a directory that is missing along the
5501556Srgrimes *	path somewhere, the directory we create will be set to the same
5511556Srgrimes *	uid/gid as the file has (when uid and gid are being preserved).
5521556Srgrimes *	NOTE: this routine is a real performance loss. It is only used as a
5531556Srgrimes *	last resort when trying to create entries in the file system.
5541556Srgrimes * Return:
5551556Srgrimes *	-1 when it could find nothing it is allowed to fix.
5561556Srgrimes *	0 otherwise
5571556Srgrimes */
5581556Srgrimes
5591556Srgrimesint
56090113Simpchk_path( char *name, uid_t st_uid, gid_t st_gid)
5611556Srgrimes{
56290113Simp	char *spt = name;
5631556Srgrimes	struct stat sb;
5641556Srgrimes	int retval = -1;
5651556Srgrimes
5661556Srgrimes	/*
5671556Srgrimes	 * watch out for paths with nodes stored directly in / (e.g. /bozo)
5681556Srgrimes	 */
5691556Srgrimes	if (*spt == '/')
5701556Srgrimes		++spt;
5711556Srgrimes
5721556Srgrimes	for(;;) {
5731556Srgrimes		/*
5741556Srgrimes		 * work foward from the first / and check each part of the path
5751556Srgrimes		 */
5761556Srgrimes		spt = strchr(spt, '/');
5771556Srgrimes		if (spt == NULL)
5781556Srgrimes			break;
5791556Srgrimes		*spt = '\0';
5801556Srgrimes
5811556Srgrimes		/*
5821556Srgrimes		 * if it exists we assume it is a directory, it is not within
5831556Srgrimes		 * the spec (at least it seems to read that way) to alter the
5841556Srgrimes		 * file system for nodes NOT EXPLICITLY stored on the archive.
5851556Srgrimes		 * If that assumption is changed, you would test the node here
5861556Srgrimes		 * and figure out how to get rid of it (probably like some
5871556Srgrimes		 * recursive unlink()) or fix up the directory permissions if
5881556Srgrimes		 * required (do an access()).
5891556Srgrimes		 */
5901556Srgrimes		if (lstat(name, &sb) == 0) {
5911556Srgrimes			*(spt++) = '/';
5921556Srgrimes			continue;
5931556Srgrimes		}
5941556Srgrimes
5951556Srgrimes		/*
5961556Srgrimes		 * the path fails at this point, see if we can create the
5971556Srgrimes		 * needed directory and continue on
5981556Srgrimes		 */
5991556Srgrimes		if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
6001556Srgrimes			*spt = '/';
6011556Srgrimes			retval = -1;
6021556Srgrimes			break;
6031556Srgrimes		}
6041556Srgrimes
6051556Srgrimes		/*
6061556Srgrimes		 * we were able to create the directory. We will tell the
6071556Srgrimes		 * caller that we found something to fix, and it is ok to try
6081556Srgrimes		 * and create the node again.
6091556Srgrimes		 */
6101556Srgrimes		retval = 0;
6111556Srgrimes		if (pids)
6121556Srgrimes			(void)set_ids(name, st_uid, st_gid);
6131556Srgrimes
6141556Srgrimes		/*
6151556Srgrimes		 * make sure the user doen't have some strange umask that
6161556Srgrimes		 * causes this newly created directory to be unusable. We fix
6171556Srgrimes		 * the modes and restore them back to the creation default at
6181556Srgrimes		 * the end of pax
6191556Srgrimes		 */
6201556Srgrimes		if ((access(name, R_OK | W_OK | X_OK) < 0) &&
6211556Srgrimes		    (lstat(name, &sb) == 0)) {
6221556Srgrimes			set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU));
6231556Srgrimes			add_dir(name, spt - name, &sb, 1);
6241556Srgrimes		}
6251556Srgrimes		*(spt++) = '/';
6261556Srgrimes		continue;
6271556Srgrimes	}
6281556Srgrimes	return(retval);
6291556Srgrimes}
6301556Srgrimes
6311556Srgrimes/*
6321556Srgrimes * set_ftime()
6331556Srgrimes *	Set the access time and modification time for a named file. If frc is
63476017Skris *	non-zero we force these times to be set even if the user did not
6351556Srgrimes *	request access and/or modification time preservation (this is also
6361556Srgrimes *	used by -t to reset access times).
6371556Srgrimes *	When ign is zero, only those times the user has asked for are set, the
6381556Srgrimes *	other ones are left alone. We do not assume the un-documented feature
6391556Srgrimes *	of many utimes() implementations that consider a 0 time value as a do
6401556Srgrimes *	not set request.
6411556Srgrimes */
6421556Srgrimes
6431556Srgrimesvoid
6441556Srgrimesset_ftime(char *fnm, time_t mtime, time_t atime, int frc)
6451556Srgrimes{
6461556Srgrimes	static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}};
6471556Srgrimes	struct stat sb;
6481556Srgrimes
64985616Sdillon	tv[0].tv_sec = atime;
65085616Sdillon	tv[1].tv_sec = mtime;
6511556Srgrimes	if (!frc && (!patime || !pmtime)) {
6521556Srgrimes		/*
6531556Srgrimes		 * if we are not forcing, only set those times the user wants
6541556Srgrimes		 * set. We get the current values of the times if we need them.
6551556Srgrimes		 */
6561556Srgrimes		if (lstat(fnm, &sb) == 0) {
6571556Srgrimes			if (!patime)
65885616Sdillon				tv[0].tv_sec = sb.st_atime;
6591556Srgrimes			if (!pmtime)
66085616Sdillon				tv[1].tv_sec = sb.st_mtime;
6611556Srgrimes		} else
66276017Skris			syswarn(0,errno,"Unable to obtain file stats %s", fnm);
6631556Srgrimes	}
6641556Srgrimes
6651556Srgrimes	/*
6661556Srgrimes	 * set the times
6671556Srgrimes	 */
6681556Srgrimes	if (utimes(fnm, tv) < 0)
66976017Skris		syswarn(1, errno, "Access/modification time set failed on: %s",
6701556Srgrimes		    fnm);
6711556Srgrimes	return;
6721556Srgrimes}
6731556Srgrimes
6741556Srgrimes/*
6751556Srgrimes * set_ids()
6761556Srgrimes *	set the uid and gid of a file system node
6771556Srgrimes * Return:
6781556Srgrimes *	0 when set, -1 on failure
6791556Srgrimes */
6801556Srgrimes
6811556Srgrimesint
6821556Srgrimesset_ids(char *fnm, uid_t uid, gid_t gid)
6831556Srgrimes{
6841556Srgrimes	if (chown(fnm, uid, gid) < 0) {
68576351Skris		/*
68676351Skris		 * ignore EPERM unless in verbose mode or being run by root.
68776351Skris		 * if running as pax, POSIX requires a warning.
68876351Skris		 */
68976351Skris		if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag ||
69076351Skris		    geteuid() == 0)
69176351Skris			syswarn(1, errno, "Unable to set file uid/gid of %s",
69276351Skris			    fnm);
6931556Srgrimes		return(-1);
6941556Srgrimes	}
6951556Srgrimes	return(0);
6961556Srgrimes}
6971556Srgrimes
6981556Srgrimes/*
69976351Skris * set_lids()
70076351Skris *	set the uid and gid of a file system node
70176351Skris * Return:
70276351Skris *	0 when set, -1 on failure
70376351Skris */
70476351Skris
70576351Skrisint
70676351Skrisset_lids(char *fnm, uid_t uid, gid_t gid)
70776351Skris{
70876351Skris	if (lchown(fnm, uid, gid) < 0) {
70976351Skris		/*
71076351Skris		 * ignore EPERM unless in verbose mode or being run by root.
71176351Skris		 * if running as pax, POSIX requires a warning.
71276351Skris		 */
71376351Skris		if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag ||
71476351Skris		    geteuid() == 0)
71576351Skris			syswarn(1, errno, "Unable to set file uid/gid of %s",
71676351Skris			    fnm);
71776351Skris		return(-1);
71876351Skris	}
71976351Skris	return(0);
72076351Skris}
72176351Skris
72276351Skris/*
7231556Srgrimes * set_pmode()
7241556Srgrimes *	Set file access mode
7251556Srgrimes */
7261556Srgrimes
7271556Srgrimesvoid
7281556Srgrimesset_pmode(char *fnm, mode_t mode)
7291556Srgrimes{
7301556Srgrimes	mode &= ABITS;
7311556Srgrimes	if (chmod(fnm, mode) < 0)
73276017Skris		syswarn(1, errno, "Could not set permissions on %s", fnm);
7331556Srgrimes	return;
7341556Srgrimes}
7351556Srgrimes
7361556Srgrimes/*
7371556Srgrimes * file_write()
7381556Srgrimes *	Write/copy a file (during copy or archive extract). This routine knows
7391556Srgrimes *	how to copy files with lseek holes in it. (Which are read as file
7401556Srgrimes *	blocks containing all 0's but do not have any file blocks associated
7411556Srgrimes *	with the data). Typical examples of these are files created by dbm
7421556Srgrimes *	variants (.pag files). While the file size of these files are huge, the
7431556Srgrimes *	actual storage is quite small (the files are sparse). The problem is
7441556Srgrimes *	the holes read as all zeros so are probably stored on the archive that
7451556Srgrimes *	way (there is no way to determine if the file block is really a hole,
7461556Srgrimes *	we only know that a file block of all zero's can be a hole).
7471556Srgrimes *	At this writing, no major archive format knows how to archive files
7481556Srgrimes *	with holes. However, on extraction (or during copy, -rw) we have to
7491556Srgrimes *	deal with these files. Without detecting the holes, the files can
7501556Srgrimes *	consume a lot of file space if just written to disk. This replacement
7511556Srgrimes *	for write when passed the basic allocation size of a file system block,
7521556Srgrimes *	uses lseek whenever it detects the input data is all 0 within that
7531556Srgrimes *	file block. In more detail, the strategy is as follows:
7541556Srgrimes *	While the input is all zero keep doing an lseek. Keep track of when we
7551556Srgrimes *	pass over file block boundries. Only write when we hit a non zero
7561556Srgrimes *	input. once we have written a file block, we continue to write it to
7571556Srgrimes *	the end (we stop looking at the input). When we reach the start of the
7581556Srgrimes *	next file block, start checking for zero blocks again. Working on file
7591556Srgrimes *	block boundries significantly reduces the overhead when copying files
7601556Srgrimes *	that are NOT very sparse. This overhead (when compared to a write) is
7611556Srgrimes *	almost below the measurement resolution on many systems. Without it,
7621556Srgrimes *	files with holes cannot be safely copied. It does has a side effect as
7631556Srgrimes *	it can put holes into files that did not have them before, but that is
7641556Srgrimes *	not a problem since the file contents are unchanged (in fact it saves
7651556Srgrimes *	file space). (Except on paging files for diskless clients. But since we
7661556Srgrimes *	cannot determine one of those file from here, we ignore them). If this
7671556Srgrimes *	ever ends up on a system where CTG files are supported and the holes
7681556Srgrimes *	are not desired, just do a conditional test in those routines that
7691556Srgrimes *	call file_write() and have it call write() instead. BEFORE CLOSING THE
7701556Srgrimes *	FILE, make sure to call file_flush() when the last write finishes with
7711556Srgrimes *	an empty block. A lot of file systems will not create an lseek hole at
7721556Srgrimes *	the end. In this case we drop a single 0 at the end to force the
7731556Srgrimes *	trailing 0's in the file.
7741556Srgrimes *	---Parameters---
7751556Srgrimes *	rem: how many bytes left in this file system block
7761556Srgrimes *	isempt: have we written to the file block yet (is it empty)
7771556Srgrimes *	sz: basic file block allocation size
7781556Srgrimes *	cnt: number of bytes on this write
7791556Srgrimes *	str: buffer to write
7801556Srgrimes * Return:
7811556Srgrimes *	number of bytes written, -1 on write (or lseek) error.
7821556Srgrimes */
7831556Srgrimes
7841556Srgrimesint
78590113Simpfile_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz,
7861556Srgrimes	char *name)
7871556Srgrimes{
78890113Simp	char *pt;
78990113Simp	char *end;
79090113Simp	int wcnt;
79190113Simp	char *st = str;
7928855Srgrimes
7931556Srgrimes	/*
7941556Srgrimes	 * while we have data to process
7951556Srgrimes	 */
7961556Srgrimes	while (cnt) {
7971556Srgrimes		if (!*rem) {
7981556Srgrimes			/*
7991556Srgrimes			 * We are now at the start of file system block again
8001556Srgrimes			 * (or what we think one is...). start looking for
8011556Srgrimes			 * empty blocks again
8021556Srgrimes			 */
8031556Srgrimes			*isempt = 1;
8041556Srgrimes			*rem = sz;
8051556Srgrimes		}
8061556Srgrimes
8071556Srgrimes		/*
8081556Srgrimes		 * only examine up to the end of the current file block or
8091556Srgrimes		 * remaining characters to write, whatever is smaller
8101556Srgrimes		 */
8111556Srgrimes		wcnt = MIN(cnt, *rem);
8121556Srgrimes		cnt -= wcnt;
8131556Srgrimes		*rem -= wcnt;
8141556Srgrimes		if (*isempt) {
8151556Srgrimes			/*
8161556Srgrimes			 * have not written to this block yet, so we keep
8171556Srgrimes			 * looking for zero's
8181556Srgrimes			 */
8191556Srgrimes			pt = st;
8201556Srgrimes			end = st + wcnt;
8211556Srgrimes
8221556Srgrimes			/*
8231556Srgrimes			 * look for a zero filled buffer
8241556Srgrimes			 */
8251556Srgrimes			while ((pt < end) && (*pt == '\0'))
8261556Srgrimes				++pt;
8271556Srgrimes
8281556Srgrimes			if (pt == end) {
8291556Srgrimes				/*
8301556Srgrimes				 * skip, buf is empty so far
8311556Srgrimes				 */
8321556Srgrimes				if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
83376017Skris					syswarn(1,errno,"File seek on %s",
8341556Srgrimes					    name);
8351556Srgrimes					return(-1);
8361556Srgrimes				}
8371556Srgrimes				st = pt;
8381556Srgrimes				continue;
8391556Srgrimes			}
8401556Srgrimes			/*
8411556Srgrimes			 * drat, the buf is not zero filled
8421556Srgrimes			 */
8431556Srgrimes			*isempt = 0;
8441556Srgrimes		}
8451556Srgrimes
8461556Srgrimes		/*
8471556Srgrimes		 * have non-zero data in this file system block, have to write
8481556Srgrimes		 */
8491556Srgrimes		if (write(fd, st, wcnt) != wcnt) {
85076017Skris			syswarn(1, errno, "Failed write to file %s", name);
8511556Srgrimes			return(-1);
8521556Srgrimes		}
8531556Srgrimes		st += wcnt;
8541556Srgrimes	}
8551556Srgrimes	return(st - str);
8561556Srgrimes}
8571556Srgrimes
8581556Srgrimes/*
8591556Srgrimes * file_flush()
8601556Srgrimes *	when the last file block in a file is zero, many file systems will not
8611556Srgrimes *	let us create a hole at the end. To get the last block with zeros, we
8621556Srgrimes *	write the last BYTE with a zero (back up one byte and write a zero).
8631556Srgrimes */
8641556Srgrimes
8651556Srgrimesvoid
8661556Srgrimesfile_flush(int fd, char *fname, int isempt)
8671556Srgrimes{
8681556Srgrimes	static char blnk[] = "\0";
8691556Srgrimes
8701556Srgrimes	/*
8711556Srgrimes	 * silly test, but make sure we are only called when the last block is
8721556Srgrimes	 * filled with all zeros.
8731556Srgrimes	 */
8741556Srgrimes	if (!isempt)
8751556Srgrimes		return;
8761556Srgrimes
8771556Srgrimes	/*
8781556Srgrimes	 * move back one byte and write a zero
8791556Srgrimes	 */
8801556Srgrimes	if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
88176017Skris		syswarn(1, errno, "Failed seek on file %s", fname);
8821556Srgrimes		return;
8831556Srgrimes	}
8841556Srgrimes
8851556Srgrimes	if (write(fd, blnk, 1) < 0)
88676017Skris		syswarn(1, errno, "Failed write to file %s", fname);
8871556Srgrimes	return;
8881556Srgrimes}
8891556Srgrimes
8901556Srgrimes/*
8911556Srgrimes * rdfile_close()
8921556Srgrimes *	close a file we have beed reading (to copy or archive). If we have to
8931556Srgrimes *	reset access time (tflag) do so (the times are stored in arcn).
8941556Srgrimes */
8951556Srgrimes
8961556Srgrimesvoid
89790113Simprdfile_close(ARCHD *arcn, int *fd)
8981556Srgrimes{
8991556Srgrimes	/*
9001556Srgrimes	 * make sure the file is open
9011556Srgrimes	 */
9021556Srgrimes	if (*fd < 0)
9031556Srgrimes		return;
9041556Srgrimes
9051556Srgrimes	(void)close(*fd);
9061556Srgrimes	*fd = -1;
9071556Srgrimes	if (!tflag)
9081556Srgrimes		return;
9091556Srgrimes
9101556Srgrimes	/*
9111556Srgrimes	 * user wants last access time reset
9121556Srgrimes	 */
9131556Srgrimes	set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1);
9141556Srgrimes	return;
9151556Srgrimes}
9161556Srgrimes
9171556Srgrimes/*
9181556Srgrimes * set_crc()
9191556Srgrimes *	read a file to calculate its crc. This is a real drag. Archive formats
9201556Srgrimes *	that have this, end up reading the file twice (we have to write the
9211556Srgrimes *	header WITH the crc before writing the file contents. Oh well...
9221556Srgrimes * Return:
9231556Srgrimes *	0 if was able to calculate the crc, -1 otherwise
9241556Srgrimes */
9251556Srgrimes
9261556Srgrimesint
92790113Simpset_crc(ARCHD *arcn, int fd)
9281556Srgrimes{
92990113Simp	int i;
93090113Simp	int res;
9311556Srgrimes	off_t cpcnt = 0L;
9321556Srgrimes	u_long size;
9331556Srgrimes	unsigned long crc = 0L;
9341556Srgrimes	char tbuf[FILEBLK];
9351556Srgrimes	struct stat sb;
9361556Srgrimes
9371556Srgrimes	if (fd < 0) {
9381556Srgrimes		/*
9391556Srgrimes		 * hmm, no fd, should never happen. well no crc then.
9401556Srgrimes		 */
9411556Srgrimes		arcn->crc = 0L;
9421556Srgrimes		return(0);
9431556Srgrimes	}
9441556Srgrimes
9451556Srgrimes	if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf))
9461556Srgrimes		size = (u_long)sizeof(tbuf);
9471556Srgrimes
9481556Srgrimes	/*
9491556Srgrimes	 * read all the bytes we think that there are in the file. If the user
9501556Srgrimes	 * is trying to archive an active file, forget this file.
9511556Srgrimes	 */
9521556Srgrimes	for(;;) {
9531556Srgrimes		if ((res = read(fd, tbuf, size)) <= 0)
9541556Srgrimes			break;
9551556Srgrimes		cpcnt += res;
9561556Srgrimes		for (i = 0; i < res; ++i)
9571556Srgrimes			crc += (tbuf[i] & 0xff);
9581556Srgrimes	}
9591556Srgrimes
9601556Srgrimes	/*
9611556Srgrimes	 * safety check. we want to avoid archiving files that are active as
9621556Srgrimes	 * they can create inconsistant archive copies.
9631556Srgrimes	 */
9641556Srgrimes	if (cpcnt != arcn->sb.st_size)
96576017Skris		paxwarn(1, "File changed size %s", arcn->org_name);
9661556Srgrimes	else if (fstat(fd, &sb) < 0)
96776017Skris		syswarn(1, errno, "Failed stat on %s", arcn->org_name);
9681556Srgrimes	else if (arcn->sb.st_mtime != sb.st_mtime)
96976017Skris		paxwarn(1, "File %s was modified during read", arcn->org_name);
9701556Srgrimes	else if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
97176017Skris		syswarn(1, errno, "File rewind failed on: %s", arcn->org_name);
9721556Srgrimes	else {
9731556Srgrimes		arcn->crc = crc;
9741556Srgrimes		return(0);
9751556Srgrimes	}
9761556Srgrimes	return(-1);
9771556Srgrimes}
978