file_subs.c revision 1.39
1262395Sbapt/* $NetBSD: file_subs.c,v 1.39 2004/02/13 00:11:30 matt Exp $ */ 2262395Sbapt 3262395Sbapt/*- 4262395Sbapt * Copyright (c) 1992 Keith Muller. 5262395Sbapt * Copyright (c) 1992, 1993 6262395Sbapt * The Regents of the University of California. All rights reserved. 7262395Sbapt * 8262395Sbapt * This code is derived from software contributed to Berkeley by 9262395Sbapt * Keith Muller of the University of California, San Diego. 10262395Sbapt * 11262395Sbapt * Redistribution and use in source and binary forms, with or without 12262395Sbapt * modification, are permitted provided that the following conditions 13262395Sbapt * are met: 14262395Sbapt * 1. Redistributions of source code must retain the above copyright 15262395Sbapt * notice, this list of conditions and the following disclaimer. 16262395Sbapt * 2. Redistributions in binary form must reproduce the above copyright 17262395Sbapt * notice, this list of conditions and the following disclaimer in the 18262395Sbapt * documentation and/or other materials provided with the distribution. 19262395Sbapt * 3. Neither the name of the University nor the names of its contributors 20262395Sbapt * may be used to endorse or promote products derived from this software 21262395Sbapt * without specific prior written permission. 22262395Sbapt * 23262395Sbapt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24262395Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25262395Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26262395Sbapt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27262395Sbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28262395Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29262395Sbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30262395Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31262395Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32262395Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33262395Sbapt * SUCH DAMAGE. 34262395Sbapt */ 35262395Sbapt 36262395Sbapt#if HAVE_NBTOOL_CONFIG_H 37262395Sbapt#include "nbtool_config.h" 38262395Sbapt#endif 39262395Sbapt 40262395Sbapt#include <sys/cdefs.h> 41262395Sbapt#if !defined(lint) 42262395Sbapt#if 0 43262395Sbaptstatic char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93"; 44262395Sbapt#else 45262395Sbapt__RCSID("$NetBSD: file_subs.c,v 1.39 2004/02/13 00:11:30 matt Exp $"); 46262395Sbapt#endif 47262395Sbapt#endif /* not lint */ 48262395Sbapt 49262395Sbapt#include <sys/types.h> 50262395Sbapt#include <sys/time.h> 51262395Sbapt#include <sys/stat.h> 52262395Sbapt#include <unistd.h> 53262395Sbapt#include <sys/param.h> 54262395Sbapt#include <fcntl.h> 55262395Sbapt#include <string.h> 56262395Sbapt#include <stdio.h> 57262395Sbapt#include <ctype.h> 58262395Sbapt#include <errno.h> 59262395Sbapt#include <sys/uio.h> 60262395Sbapt#include <stdlib.h> 61262395Sbapt#include "pax.h" 62262395Sbapt#include "extern.h" 63262395Sbapt#include "options.h" 64262395Sbapt 65262395Sbaptstatic int 66262395Sbaptmk_link(char *,struct stat *,char *, int); 67262395Sbapt 68262395Sbapt/* 69262395Sbapt * routines that deal with file operations such as: creating, removing; 70262395Sbapt * and setting access modes, uid/gid and times of files 71262395Sbapt */ 72262395Sbapt 73262395Sbapt#define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 74262395Sbapt#define SETBITS (S_ISUID | S_ISGID) 75262395Sbapt#define ABITS (FILEBITS | SETBITS) 76262395Sbapt 77262395Sbapt/* 78262395Sbapt * file_creat() 79262395Sbapt * Create and open a file. 80262395Sbapt * Return: 81262395Sbapt * file descriptor or -1 for failure 82262395Sbapt */ 83262395Sbapt 84262395Sbaptint 85262395Sbaptfile_creat(ARCHD *arcn) 86262395Sbapt{ 87262395Sbapt int fd = -1; 88262395Sbapt int oerrno; 89262395Sbapt 90262395Sbapt /* 91262395Sbapt * Create a temporary file name so that the file doesn't have partial 92262395Sbapt * contents while restoring. 93262395Sbapt */ 94262395Sbapt arcn->tmp_name = malloc(arcn->nlen + 8); 95262395Sbapt if (arcn->tmp_name == NULL) { 96262395Sbapt syswarn(1, errno, "Cannot malloc %d bytes", arcn->nlen + 8); 97262395Sbapt return(-1); 98262395Sbapt } 99262395Sbapt (void)snprintf(arcn->tmp_name, arcn->nlen + 8, "%s.XXXXXX", arcn->name); 100262395Sbapt 101262395Sbapt fd = mkstemp(arcn->tmp_name); 102262395Sbapt if (fd >= 0) 103262395Sbapt return(fd); 104262395Sbapt 105262395Sbapt for (;;) { 106262395Sbapt /* 107262395Sbapt * try to create the temporary file we use to restore the 108262395Sbapt * contents info. if this fails, keep checking all the nodes 109262395Sbapt * in the path until chk_path() finds that it cannot fix 110262395Sbapt * anything further. if that happens we just give up. 111262395Sbapt */ 112262395Sbapt fd = mkstemp(arcn->tmp_name); 113262395Sbapt if (fd >= 0) 114262395Sbapt break; 115262395Sbapt oerrno = errno; 116262395Sbapt if (nodirs || chk_path(arcn->tmp_name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 117262395Sbapt (void)fflush(listf); 118262395Sbapt syswarn(1, oerrno, "Cannot create %s", arcn->tmp_name); 119262395Sbapt free(arcn->tmp_name); 120262395Sbapt arcn->tmp_name = NULL; 121262395Sbapt return(-1); 122262395Sbapt } 123262395Sbapt } 124262395Sbapt return(fd); 125262395Sbapt} 126262395Sbapt 127262395Sbapt/* 128262395Sbapt * file_close() 129262395Sbapt * Close file descriptor to a file just created by pax. Sets modes, 130262395Sbapt * ownership and times as required. 131262395Sbapt * Return: 132262395Sbapt * 0 for success, -1 for failure 133262395Sbapt */ 134262395Sbapt 135262395Sbaptvoid 136262395Sbaptfile_close(ARCHD *arcn, int fd) 137262395Sbapt{ 138262395Sbapt int res = 0; 139262395Sbapt 140262395Sbapt if (fd < 0) 141262395Sbapt return; 142262395Sbapt if (close(fd) < 0) 143262395Sbapt syswarn(0, errno, "Cannot close file descriptor on %s", 144262395Sbapt arcn->tmp_name); 145262395Sbapt 146262395Sbapt /* 147262395Sbapt * set owner/groups first as this may strip off mode bits we want 148262395Sbapt * then set file permission modes. Then set file access and 149262395Sbapt * modification times. 150262395Sbapt */ 151262395Sbapt if (pids) 152262395Sbapt res = set_ids(arcn->tmp_name, arcn->sb.st_uid, arcn->sb.st_gid); 153262395Sbapt 154262395Sbapt /* 155262395Sbapt * IMPORTANT SECURITY NOTE: 156262395Sbapt * if not preserving mode or we cannot set uid/gid, then PROHIBIT 157262395Sbapt * set uid/gid bits but restore the file modes (since mkstemp doesn't). 158262395Sbapt */ 159262395Sbapt if (!pmode || res) 160262395Sbapt arcn->sb.st_mode &= ~(SETBITS); 161262395Sbapt if (pmode) 162262395Sbapt set_pmode(arcn->tmp_name, arcn->sb.st_mode); 163262395Sbapt else 164262395Sbapt set_pmode(arcn->tmp_name, arcn->sb.st_mode & FILEBITS); 165262395Sbapt if (patime || pmtime) 166262395Sbapt set_ftime(arcn->tmp_name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 167262395Sbapt#if HAVE_STRUCT_STAT_ST_FLAGS 168262395Sbapt if (pfflags && arcn->type != PAX_SLK) 169262395Sbapt set_chflags(arcn->tmp_name, arcn->sb.st_flags); 170262395Sbapt#endif 171262395Sbapt 172262395Sbapt /* 173262395Sbapt * Finally, now the temp file is fully instantiated rename it to 174262395Sbapt * the desired file name. 175262395Sbapt */ 176262395Sbapt if (rename(arcn->tmp_name, arcn->name) < 0) { 177262395Sbapt syswarn(0, errno, "Cannot rename %s to %s", 178262395Sbapt arcn->tmp_name, arcn->name); 179262395Sbapt (void)unlink(arcn->tmp_name); 180262395Sbapt } 181262395Sbapt 182262395Sbapt free(arcn->tmp_name); 183262395Sbapt arcn->tmp_name = NULL; 184262395Sbapt} 185262395Sbapt 186262395Sbapt/* 187262395Sbapt * lnk_creat() 188262395Sbapt * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name 189262395Sbapt * must exist; 190262395Sbapt * Return: 191262395Sbapt * 0 if ok, -1 otherwise 192262395Sbapt */ 193262395Sbapt 194262395Sbaptint 195262395Sbaptlnk_creat(ARCHD *arcn) 196262395Sbapt{ 197262395Sbapt struct stat sb; 198262395Sbapt 199262395Sbapt /* 200262395Sbapt * we may be running as root, so we have to be sure that link target 201262395Sbapt * is not a directory, so we lstat and check 202262395Sbapt */ 203262395Sbapt if (lstat(arcn->ln_name, &sb) < 0) { 204262395Sbapt syswarn(1, errno, "Cannot link to %s from %s", arcn->ln_name, 205262395Sbapt arcn->name); 206262395Sbapt return(-1); 207262395Sbapt } 208262395Sbapt 209262395Sbapt if (S_ISDIR(sb.st_mode)) { 210262395Sbapt tty_warn(1, "A hard link to the directory %s is not allowed", 211262395Sbapt arcn->ln_name); 212262395Sbapt return(-1); 213262395Sbapt } 214262395Sbapt 215262395Sbapt return(mk_link(arcn->ln_name, &sb, arcn->name, 0)); 216262395Sbapt} 217262395Sbapt 218262395Sbapt/* 219262395Sbapt * cross_lnk() 220262395Sbapt * Create a hard link to arcn->org_name from arcn->name. Only used in copy 221262395Sbapt * with the -l flag. No warning or error if this does not succeed (we will 222262395Sbapt * then just create the file) 223262395Sbapt * Return: 224262395Sbapt * 1 if copy() should try to create this file node 225262395Sbapt * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). 226262395Sbapt */ 227262395Sbapt 228262395Sbaptint 229262395Sbaptcross_lnk(ARCHD *arcn) 230262395Sbapt{ 231262395Sbapt /* 232262395Sbapt * try to make a link to original file (-l flag in copy mode). make 233262395Sbapt * sure we do not try to link to directories in case we are running as 234262395Sbapt * root (and it might succeed). 235262395Sbapt */ 236262395Sbapt if (arcn->type == PAX_DIR) 237262395Sbapt return(1); 238262395Sbapt return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); 239262395Sbapt} 240262395Sbapt 241262395Sbapt/* 242262395Sbapt * chk_same() 243262395Sbapt * In copy mode if we are not trying to make hard links between the src 244262395Sbapt * and destinations, make sure we are not going to overwrite ourselves by 245262395Sbapt * accident. This slows things down a little, but we have to protect all 246262395Sbapt * those people who make typing errors. 247262395Sbapt * Return: 248262395Sbapt * 1 the target does not exist, go ahead and copy 249262395Sbapt * 0 skip it file exists (-k) or may be the same as source file 250262395Sbapt */ 251262395Sbapt 252262395Sbaptint 253262395Sbaptchk_same(ARCHD *arcn) 254262395Sbapt{ 255262395Sbapt struct stat sb; 256262395Sbapt 257262395Sbapt /* 258262395Sbapt * if file does not exist, return. if file exists and -k, skip it 259262395Sbapt * quietly 260262395Sbapt */ 261262395Sbapt if (lstat(arcn->name, &sb) < 0) 262262395Sbapt return(1); 263262395Sbapt if (kflag) 264262395Sbapt return(0); 265262395Sbapt 266262395Sbapt /* 267262395Sbapt * better make sure the user does not have src == dest by mistake 268262395Sbapt */ 269262395Sbapt if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { 270262395Sbapt tty_warn(1, "Unable to copy %s, file would overwrite itself", 271262395Sbapt arcn->name); 272262395Sbapt return(0); 273262395Sbapt } 274262395Sbapt return(1); 275262395Sbapt} 276262395Sbapt 277262395Sbapt/* 278262395Sbapt * mk_link() 279262395Sbapt * try to make a hard link between two files. if ign set, we do not 280262395Sbapt * complain. 281262395Sbapt * Return: 282262395Sbapt * 0 if successful (or we are done with this file but no error, such as 283262395Sbapt * finding the from file exists and the user has set -k). 284262395Sbapt * 1 when ign was set to indicates we could not make the link but we 285262395Sbapt * should try to copy/extract the file as that might work (and is an 286262395Sbapt * allowed option). -1 an error occurred. 287262395Sbapt */ 288262395Sbapt 289262395Sbaptstatic int 290262395Sbaptmk_link(char *to, struct stat *to_sb, char *from, int ign) 291262395Sbapt{ 292262395Sbapt struct stat sb; 293262395Sbapt int oerrno; 294262395Sbapt 295262395Sbapt /* 296262395Sbapt * if from file exists, it has to be unlinked to make the link. If the 297262395Sbapt * file exists and -k is set, skip it quietly 298262395Sbapt */ 299262395Sbapt if (lstat(from, &sb) == 0) { 300262395Sbapt if (kflag) 301262395Sbapt return(0); 302262395Sbapt 303262395Sbapt /* 304262395Sbapt * make sure it is not the same file, protect the user 305262395Sbapt */ 306262395Sbapt if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { 307262395Sbapt tty_warn(1, "Cannot link file %s to itself", to); 308262395Sbapt return(-1); 309262395Sbapt } 310262395Sbapt 311262395Sbapt /* 312262395Sbapt * try to get rid of the file, based on the type 313262395Sbapt */ 314262395Sbapt if (S_ISDIR(sb.st_mode)) { 315262395Sbapt if (rmdir(from) < 0) { 316262395Sbapt syswarn(1, errno, "Cannot remove %s", from); 317262395Sbapt return(-1); 318262395Sbapt } 319262395Sbapt } else if (unlink(from) < 0) { 320262395Sbapt if (!ign) { 321262395Sbapt syswarn(1, errno, "Cannot remove %s", from); 322262395Sbapt return(-1); 323262395Sbapt } 324262395Sbapt return(1); 325262395Sbapt } 326262395Sbapt } 327262395Sbapt 328262395Sbapt /* 329262395Sbapt * from file is gone (or did not exist), try to make the hard link. 330262395Sbapt * if it fails, check the path and try it again (if chk_path() says to 331262395Sbapt * try again) 332262395Sbapt */ 333262395Sbapt for (;;) { 334262395Sbapt if (link(to, from) == 0) 335262395Sbapt break; 336262395Sbapt oerrno = errno; 337262395Sbapt if (chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) 338262395Sbapt continue; 339262395Sbapt if (!ign) { 340262395Sbapt syswarn(1, oerrno, "Cannot link to %s from %s", to, 341262395Sbapt from); 342262395Sbapt return(-1); 343262395Sbapt } 344262395Sbapt return(1); 345262395Sbapt } 346262395Sbapt 347262395Sbapt /* 348262395Sbapt * all right the link was made 349262395Sbapt */ 350262395Sbapt return(0); 351262395Sbapt} 352262395Sbapt 353262395Sbapt/* 354262395Sbapt * node_creat() 355262395Sbapt * create an entry in the file system (other than a file or hard link). 356262395Sbapt * If successful, sets uid/gid modes and times as required. 357262395Sbapt * Return: 358262395Sbapt * 0 if ok, -1 otherwise 359262395Sbapt */ 360262395Sbapt 361262395Sbaptint 362262395Sbaptnode_creat(ARCHD *arcn) 363262395Sbapt{ 364262395Sbapt int res; 365262395Sbapt int ign = 0; 366262395Sbapt int oerrno; 367262395Sbapt int pass = 0; 368262395Sbapt mode_t file_mode; 369262395Sbapt struct stat sb; 370262395Sbapt char target[MAXPATHLEN]; 371262395Sbapt char *nm = arcn->name; 372262395Sbapt int len; 373262395Sbapt 374262395Sbapt /* 375262395Sbapt * create node based on type, if that fails try to unlink the node and 376262395Sbapt * try again. finally check the path and try again. As noted in the 377262395Sbapt * file and link creation routines, this method seems to exhibit the 378262395Sbapt * best performance in general use workloads. 379262395Sbapt */ 380262395Sbapt file_mode = arcn->sb.st_mode & FILEBITS; 381262395Sbapt 382262395Sbapt for (;;) { 383262395Sbapt switch(arcn->type) { 384262395Sbapt case PAX_DIR: 385262395Sbapt /* 386262395Sbapt * If -h (or -L) was given in tar-mode, follow the 387262395Sbapt * potential symlink chain before trying to create the 388262395Sbapt * directory. 389262395Sbapt */ 390262395Sbapt if (strcmp(NM_TAR, argv0) == 0 && Lflag) { 391262395Sbapt while (lstat(nm, &sb) == 0 && 392262395Sbapt S_ISLNK(sb.st_mode)) { 393262395Sbapt len = readlink(nm, target, 394262395Sbapt sizeof target - 1); 395262395Sbapt if (len == -1) { 396262395Sbapt syswarn(0, errno, 397262395Sbapt "cannot follow symlink %s in chain for %s", 398262395Sbapt nm, arcn->name); 399262395Sbapt res = -1; 400262395Sbapt goto badlink; 401262395Sbapt } 402262395Sbapt target[len] = '\0'; 403262395Sbapt nm = target; 404262395Sbapt } 405262395Sbapt } 406262395Sbapt res = mkdir(nm, file_mode); 407262395Sbapt 408262395Sbaptbadlink: 409262395Sbapt if (ign) 410262395Sbapt res = 0; 411262395Sbapt break; 412262395Sbapt case PAX_CHR: 413262395Sbapt file_mode |= S_IFCHR; 414262395Sbapt res = mknod(nm, file_mode, arcn->sb.st_rdev); 415262395Sbapt break; 416262395Sbapt case PAX_BLK: 417262395Sbapt file_mode |= S_IFBLK; 418262395Sbapt res = mknod(nm, file_mode, arcn->sb.st_rdev); 419262395Sbapt break; 420262395Sbapt case PAX_FIF: 421262395Sbapt res = mkfifo(nm, file_mode); 422262395Sbapt break; 423262395Sbapt case PAX_SCK: 424262395Sbapt /* 425262395Sbapt * Skip sockets, operation has no meaning under BSD 426262395Sbapt */ 427262395Sbapt tty_warn(0, 428262395Sbapt "%s skipped. Sockets cannot be copied or extracted", 429262395Sbapt nm); 430262395Sbapt return(-1); 431262395Sbapt case PAX_SLK: 432262395Sbapt res = symlink(arcn->ln_name, nm); 433262395Sbapt break; 434262395Sbapt case PAX_CTG: 435262395Sbapt case PAX_HLK: 436262395Sbapt case PAX_HRG: 437262395Sbapt case PAX_REG: 438262395Sbapt default: 439262395Sbapt /* 440262395Sbapt * we should never get here 441262395Sbapt */ 442262395Sbapt tty_warn(0, "%s has an unknown file type, skipping", 443262395Sbapt nm); 444262395Sbapt return(-1); 445262395Sbapt } 446262395Sbapt 447262395Sbapt /* 448262395Sbapt * if we were able to create the node break out of the loop, 449262395Sbapt * otherwise try to unlink the node and try again. if that 450262395Sbapt * fails check the full path and try a final time. 451262395Sbapt */ 452262395Sbapt if (res == 0) 453262395Sbapt break; 454262395Sbapt 455262395Sbapt /* 456262395Sbapt * we failed to make the node 457262395Sbapt */ 458262395Sbapt oerrno = errno; 459262395Sbapt if ((ign = unlnk_exist(nm, arcn->type)) < 0) 460262395Sbapt return(-1); 461262395Sbapt 462262395Sbapt if (++pass <= 1) 463262395Sbapt continue; 464262395Sbapt 465262395Sbapt if (nodirs || chk_path(nm,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { 466262395Sbapt syswarn(1, oerrno, "Cannot create %s", nm); 467262395Sbapt return(-1); 468262395Sbapt } 469262395Sbapt } 470262395Sbapt 471262395Sbapt /* 472262395Sbapt * we were able to create the node. set uid/gid, modes and times 473262395Sbapt */ 474262395Sbapt if (pids) 475262395Sbapt res = set_ids(nm, arcn->sb.st_uid, arcn->sb.st_gid); 476262395Sbapt else 477262395Sbapt res = 0; 478262395Sbapt 479262395Sbapt /* 480262395Sbapt * IMPORTANT SECURITY NOTE: 481262395Sbapt * if not preserving mode or we cannot set uid/gid, then PROHIBIT any 482262395Sbapt * set uid/gid bits 483262395Sbapt */ 484262395Sbapt if (!pmode || res) 485262395Sbapt arcn->sb.st_mode &= ~(SETBITS); 486262395Sbapt if (pmode) 487262395Sbapt set_pmode(arcn->name, arcn->sb.st_mode); 488262395Sbapt 489262395Sbapt if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { 490262395Sbapt /* 491262395Sbapt * Dirs must be processed again at end of extract to set times 492262395Sbapt * and modes to agree with those stored in the archive. However 493262395Sbapt * to allow extract to continue, we may have to also set owner 494262395Sbapt * rights. This allows nodes in the archive that are children 495262395Sbapt * of this directory to be extracted without failure. Both time 496262395Sbapt * and modes will be fixed after the entire archive is read and 497262395Sbapt * before pax exits. 498262395Sbapt */ 499262395Sbapt if (access(nm, R_OK | W_OK | X_OK) < 0) { 500262395Sbapt if (lstat(nm, &sb) < 0) { 501262395Sbapt syswarn(0, errno,"Cannot access %s (stat)", 502262395Sbapt arcn->name); 503262395Sbapt set_pmode(nm,file_mode | S_IRWXU); 504262395Sbapt } else { 505262395Sbapt /* 506262395Sbapt * We have to add rights to the dir, so we make 507262395Sbapt * sure to restore the mode. The mode must be 508262395Sbapt * restored AS CREATED and not as stored if 509262395Sbapt * pmode is not set. 510262395Sbapt */ 511262395Sbapt set_pmode(nm, 512262395Sbapt ((sb.st_mode & FILEBITS) | S_IRWXU)); 513262395Sbapt if (!pmode) 514262395Sbapt arcn->sb.st_mode = sb.st_mode; 515262395Sbapt } 516262395Sbapt 517262395Sbapt /* 518262395Sbapt * we have to force the mode to what was set here, 519262395Sbapt * since we changed it from the default as created. 520262395Sbapt */ 521262395Sbapt add_dir(nm, arcn->nlen, &(arcn->sb), 1); 522262395Sbapt } else if (pmode || patime || pmtime) 523262395Sbapt add_dir(nm, arcn->nlen, &(arcn->sb), 0); 524262395Sbapt } 525262395Sbapt 526262395Sbapt#if HAVE_LUTIMES 527262395Sbapt if (patime || pmtime) 528262395Sbapt#else 529262395Sbapt if ((patime || pmtime) && arcn->type != PAX_SLK) 530262395Sbapt#endif 531262395Sbapt set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0); 532262395Sbapt 533262395Sbapt#if HAVE_STRUCT_STAT_ST_FLAGS 534262395Sbapt if (pfflags && arcn->type != PAX_SLK) 535262395Sbapt set_chflags(arcn->name, arcn->sb.st_flags); 536262395Sbapt#endif 537262395Sbapt return(0); 538262395Sbapt} 539262395Sbapt 540262395Sbapt/* 541262395Sbapt * unlnk_exist() 542262395Sbapt * Remove node from file system with the specified name. We pass the type 543262395Sbapt * of the node that is going to replace it. When we try to create a 544262395Sbapt * directory and find that it already exists, we allow processing to 545262395Sbapt * continue as proper modes etc will always be set for it later on. 546262395Sbapt * Return: 547262395Sbapt * 0 is ok to proceed, no file with the specified name exists 548262395Sbapt * -1 we were unable to remove the node, or we should not remove it (-k) 549262395Sbapt * 1 we found a directory and we were going to create a directory. 550262395Sbapt */ 551262395Sbapt 552262395Sbaptint 553262395Sbaptunlnk_exist(char *name, int type) 554262395Sbapt{ 555262395Sbapt struct stat sb; 556262395Sbapt 557262395Sbapt /* 558262395Sbapt * the file does not exist, or -k we are done 559262395Sbapt */ 560262395Sbapt if (lstat(name, &sb) < 0) 561262395Sbapt return(0); 562262395Sbapt if (kflag) 563262395Sbapt return(-1); 564262395Sbapt 565262395Sbapt if (S_ISDIR(sb.st_mode)) { 566262395Sbapt /* 567262395Sbapt * try to remove a directory, if it fails and we were going to 568262395Sbapt * create a directory anyway, tell the caller (return a 1) 569262395Sbapt */ 570262395Sbapt if (rmdir(name) < 0) { 571262395Sbapt if (type == PAX_DIR) 572262395Sbapt return(1); 573262395Sbapt syswarn(1, errno, "Cannot remove directory %s", name); 574262395Sbapt return(-1); 575262395Sbapt } 576262395Sbapt return(0); 577262395Sbapt } 578262395Sbapt 579262395Sbapt /* 580262395Sbapt * try to get rid of all non-directory type nodes 581262395Sbapt */ 582262395Sbapt if (unlink(name) < 0) { 583262395Sbapt (void)fflush(listf); 584262395Sbapt syswarn(1, errno, "Cannot unlink %s", name); 585262395Sbapt return(-1); 586262395Sbapt } 587262395Sbapt return(0); 588262395Sbapt} 589262395Sbapt 590262395Sbapt/* 591262395Sbapt * chk_path() 592262395Sbapt * We were trying to create some kind of node in the file system and it 593262395Sbapt * failed. chk_path() makes sure the path up to the node exists and is 594262395Sbapt * writable. When we have to create a directory that is missing along the 595262395Sbapt * path somewhere, the directory we create will be set to the same 596262395Sbapt * uid/gid as the file has (when uid and gid are being preserved). 597262395Sbapt * NOTE: this routine is a real performance loss. It is only used as a 598262395Sbapt * last resort when trying to create entries in the file system. 599262395Sbapt * Return: 600262395Sbapt * -1 when it could find nothing it is allowed to fix. 601262395Sbapt * 0 otherwise 602262395Sbapt */ 603262395Sbapt 604262395Sbaptint 605262395Sbaptchk_path( char *name, uid_t st_uid, gid_t st_gid) 606262395Sbapt{ 607262395Sbapt char *spt = name; 608262395Sbapt struct stat sb; 609262395Sbapt int retval = -1; 610262395Sbapt 611262395Sbapt /* 612262395Sbapt * watch out for paths with nodes stored directly in / (e.g. /bozo) 613262395Sbapt */ 614262395Sbapt if (*spt == '/') 615262395Sbapt ++spt; 616262395Sbapt 617262395Sbapt for(;;) { 618262395Sbapt /* 619262395Sbapt * work forward from the first / and check each part of 620262395Sbapt * the path 621262395Sbapt */ 622262395Sbapt spt = strchr(spt, '/'); 623262395Sbapt if (spt == NULL) 624262395Sbapt break; 625262395Sbapt *spt = '\0'; 626262395Sbapt 627262395Sbapt /* 628262395Sbapt * if it exists we assume it is a directory, it is not within 629262395Sbapt * the spec (at least it seems to read that way) to alter the 630262395Sbapt * file system for nodes NOT EXPLICITLY stored on the archive. 631262395Sbapt * If that assumption is changed, you would test the node here 632262395Sbapt * and figure out how to get rid of it (probably like some 633262395Sbapt * recursive unlink()) or fix up the directory permissions if 634262395Sbapt * required (do an access()). 635262395Sbapt */ 636262395Sbapt if (lstat(name, &sb) == 0) { 637262395Sbapt *(spt++) = '/'; 638262395Sbapt continue; 639262395Sbapt } 640262395Sbapt 641262395Sbapt /* 642262395Sbapt * the path fails at this point, see if we can create the 643262395Sbapt * needed directory and continue on 644262395Sbapt */ 645262395Sbapt if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { 646262395Sbapt *spt = '/'; 647262395Sbapt retval = -1; 648262395Sbapt break; 649262395Sbapt } 650262395Sbapt 651262395Sbapt /* 652262395Sbapt * we were able to create the directory. We will tell the 653262395Sbapt * caller that we found something to fix, and it is ok to try 654262395Sbapt * and create the node again. 655262395Sbapt */ 656262395Sbapt retval = 0; 657262395Sbapt if (pids) 658262395Sbapt (void)set_ids(name, st_uid, st_gid); 659262395Sbapt 660262395Sbapt /* 661262395Sbapt * make sure the user doesn't have some strange umask that 662262395Sbapt * causes this newly created directory to be unusable. We fix 663262395Sbapt * the modes and restore them back to the creation default at 664262395Sbapt * the end of pax 665262395Sbapt */ 666262395Sbapt if ((access(name, R_OK | W_OK | X_OK) < 0) && 667262395Sbapt (lstat(name, &sb) == 0)) { 668262395Sbapt set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); 669262395Sbapt add_dir(name, spt - name, &sb, 1); 670262395Sbapt } 671262395Sbapt *(spt++) = '/'; 672262395Sbapt continue; 673262395Sbapt } 674262395Sbapt return(retval); 675262395Sbapt} 676262395Sbapt 677262395Sbapt/* 678262395Sbapt * set_ftime() 679262395Sbapt * Set the access time and modification time for a named file. If frc 680262395Sbapt * is non-zero we force these times to be set even if the user did not 681262395Sbapt * request access and/or modification time preservation (this is also 682262395Sbapt * used by -t to reset access times). 683262395Sbapt * When ign is zero, only those times the user has asked for are set, the 684262395Sbapt * other ones are left alone. We do not assume the un-documented feature 685262395Sbapt * of many utimes() implementations that consider a 0 time value as a do 686262395Sbapt * not set request. 687262395Sbapt */ 688262395Sbapt 689262395Sbaptvoid 690262395Sbaptset_ftime(char *fnm, time_t mtime, time_t atime, int frc) 691262395Sbapt{ 692262395Sbapt struct timeval tv[2]; 693262395Sbapt struct stat sb; 694262395Sbapt 695262395Sbapt tv[0].tv_sec = (long)atime; 696262395Sbapt tv[0].tv_usec = 0; 697262395Sbapt tv[1].tv_sec = (long)mtime; 698262395Sbapt tv[1].tv_usec = 0; 699262395Sbapt if (!frc && (!patime || !pmtime)) { 700262395Sbapt /* 701262395Sbapt * if we are not forcing, only set those times the user wants 702262395Sbapt * set. We get the current values of the times if we need them. 703262395Sbapt */ 704262395Sbapt if (lstat(fnm, &sb) == 0) { 705262395Sbapt#ifdef BSD4_4 706262395Sbapt if (!patime) 707262395Sbapt TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 708262395Sbapt if (!pmtime) 709262395Sbapt TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 710262395Sbapt#else 711262395Sbapt if (!patime) 712262395Sbapt tv[0].tv_sec = sb.st_atime; 713262395Sbapt if (!pmtime) 714262395Sbapt tv[1].tv_sec = sb.st_mtime; 715262395Sbapt#endif 716262395Sbapt } else 717262395Sbapt syswarn(0, errno, "Cannot obtain file stats %s", fnm); 718262395Sbapt } 719262395Sbapt 720262395Sbapt /* 721262395Sbapt * set the times 722262395Sbapt */ 723262395Sbapt#if HAVE_LUTIMES 724262395Sbapt if (lutimes(fnm, tv)) 725262395Sbapt#else 726262395Sbapt if (utimes(fnm, tv)) 727262395Sbapt#endif 728262395Sbapt syswarn(1, errno, "Access/modification time set failed on: %s", 729262395Sbapt fnm); 730262395Sbapt return; 731262395Sbapt} 732262395Sbapt 733262395Sbapt/* 734262395Sbapt * set_ids() 735262395Sbapt * set the uid and gid of a file system node 736262395Sbapt * Return: 737262395Sbapt * 0 when set, -1 on failure 738262395Sbapt */ 739262395Sbapt 740262395Sbaptint 741262395Sbaptset_ids(char *fnm, uid_t uid, gid_t gid) 742262395Sbapt{ 743262395Sbapt if (geteuid() == 0) 744262395Sbapt if (lchown(fnm, uid, gid)) { 745262395Sbapt (void)fflush(listf); 746262395Sbapt syswarn(1, errno, "Cannot set file uid/gid of %s", 747262395Sbapt fnm); 748262395Sbapt return(-1); 749262395Sbapt } 750262395Sbapt return(0); 751262395Sbapt} 752262395Sbapt 753262395Sbapt/* 754262395Sbapt * set_pmode() 755262395Sbapt * Set file access mode 756262395Sbapt */ 757262395Sbapt 758262395Sbaptvoid 759262395Sbaptset_pmode(char *fnm, mode_t mode) 760262395Sbapt{ 761262395Sbapt mode &= ABITS; 762262395Sbapt if (lchmod(fnm, mode)) { 763262395Sbapt (void)fflush(listf); 764262395Sbapt syswarn(1, errno, "Cannot set permissions on %s", fnm); 765262395Sbapt } 766262395Sbapt return; 767262395Sbapt} 768262395Sbapt 769262395Sbapt/* 770262395Sbapt * set_chflags() 771262395Sbapt * Set 4.4BSD file flags 772262395Sbapt */ 773262395Sbaptvoid 774262395Sbaptset_chflags(char *fnm, u_int32_t flags) 775262395Sbapt{ 776262395Sbapt 777262395Sbapt#if 0 778262395Sbapt if (chflags(fnm, flags) < 0 && errno != EOPNOTSUPP) 779262395Sbapt syswarn(1, errno, "Cannot set file flags on %s", fnm); 780262395Sbapt#endif 781262395Sbapt return; 782262395Sbapt} 783262395Sbapt 784262395Sbapt/* 785262395Sbapt * file_write() 786262395Sbapt * Write/copy a file (during copy or archive extract). This routine knows 787262395Sbapt * how to copy files with lseek holes in it. (Which are read as file 788262395Sbapt * blocks containing all 0's but do not have any file blocks associated 789262395Sbapt * with the data). Typical examples of these are files created by dbm 790262395Sbapt * variants (.pag files). While the file size of these files are huge, the 791262395Sbapt * actual storage is quite small (the files are sparse). The problem is 792262395Sbapt * the holes read as all zeros so are probably stored on the archive that 793262395Sbapt * way (there is no way to determine if the file block is really a hole, 794262395Sbapt * we only know that a file block of all zero's can be a hole). 795262395Sbapt * At this writing, no major archive format knows how to archive files 796262395Sbapt * with holes. However, on extraction (or during copy, -rw) we have to 797262395Sbapt * deal with these files. Without detecting the holes, the files can 798262395Sbapt * consume a lot of file space if just written to disk. This replacement 799262395Sbapt * for write when passed the basic allocation size of a file system block, 800262395Sbapt * uses lseek whenever it detects the input data is all 0 within that 801262395Sbapt * file block. In more detail, the strategy is as follows: 802262395Sbapt * While the input is all zero keep doing an lseek. Keep track of when we 803262395Sbapt * pass over file block boundaries. Only write when we hit a non zero 804262395Sbapt * input. once we have written a file block, we continue to write it to 805262395Sbapt * the end (we stop looking at the input). When we reach the start of the 806262395Sbapt * next file block, start checking for zero blocks again. Working on file 807262395Sbapt * block boundaries significantly reduces the overhead when copying files 808262395Sbapt * that are NOT very sparse. This overhead (when compared to a write) is 809262395Sbapt * almost below the measurement resolution on many systems. Without it, 810262395Sbapt * files with holes cannot be safely copied. It does has a side effect as 811262395Sbapt * it can put holes into files that did not have them before, but that is 812262395Sbapt * not a problem since the file contents are unchanged (in fact it saves 813262395Sbapt * file space). (Except on paging files for diskless clients. But since we 814262395Sbapt * cannot determine one of those file from here, we ignore them). If this 815262395Sbapt * ever ends up on a system where CTG files are supported and the holes 816262395Sbapt * are not desired, just do a conditional test in those routines that 817262395Sbapt * call file_write() and have it call write() instead. BEFORE CLOSING THE 818262395Sbapt * FILE, make sure to call file_flush() when the last write finishes with 819262395Sbapt * an empty block. A lot of file systems will not create an lseek hole at 820262395Sbapt * the end. In this case we drop a single 0 at the end to force the 821262395Sbapt * trailing 0's in the file. 822262395Sbapt * ---Parameters--- 823262395Sbapt * rem: how many bytes left in this file system block 824262395Sbapt * isempt: have we written to the file block yet (is it empty) 825262395Sbapt * sz: basic file block allocation size 826262395Sbapt * cnt: number of bytes on this write 827262395Sbapt * str: buffer to write 828262395Sbapt * Return: 829262395Sbapt * number of bytes written, -1 on write (or lseek) error. 830 */ 831 832int 833file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, 834 char *name) 835{ 836 char *pt; 837 char *end; 838 int wcnt; 839 char *st = str; 840 char **strp; 841 842 /* 843 * while we have data to process 844 */ 845 while (cnt) { 846 if (!*rem) { 847 /* 848 * We are now at the start of file system block again 849 * (or what we think one is...). start looking for 850 * empty blocks again 851 */ 852 *isempt = 1; 853 *rem = sz; 854 } 855 856 /* 857 * only examine up to the end of the current file block or 858 * remaining characters to write, whatever is smaller 859 */ 860 wcnt = MIN(cnt, *rem); 861 cnt -= wcnt; 862 *rem -= wcnt; 863 if (*isempt) { 864 /* 865 * have not written to this block yet, so we keep 866 * looking for zero's 867 */ 868 pt = st; 869 end = st + wcnt; 870 871 /* 872 * look for a zero filled buffer 873 */ 874 while ((pt < end) && (*pt == '\0')) 875 ++pt; 876 877 if (pt == end) { 878 /* 879 * skip, buf is empty so far 880 */ 881 if (fd > -1 && 882 lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { 883 syswarn(1, errno, "File seek on %s", 884 name); 885 return(-1); 886 } 887 st = pt; 888 continue; 889 } 890 /* 891 * drat, the buf is not zero filled 892 */ 893 *isempt = 0; 894 } 895 896 /* 897 * have non-zero data in this file system block, have to write 898 */ 899 switch (fd) { 900 case -1: 901 strp = &gnu_name_string; 902 break; 903 case -2: 904 strp = &gnu_link_string; 905 break; 906 default: 907 strp = NULL; 908 break; 909 } 910 if (strp) { 911 if (*strp) 912 err(1, "WARNING! Major Internal Error! GNU hack Failing!"); 913 *strp = malloc(wcnt + 1); 914 if (*strp == NULL) { 915 tty_warn(1, "Out of memory"); 916 return(-1); 917 } 918 strlcpy(*strp, st, wcnt); 919 break; 920 } else if (xwrite(fd, st, wcnt) != wcnt) { 921 syswarn(1, errno, "Failed write to file %s", name); 922 return(-1); 923 } 924 st += wcnt; 925 } 926 return(st - str); 927} 928 929/* 930 * file_flush() 931 * when the last file block in a file is zero, many file systems will not 932 * let us create a hole at the end. To get the last block with zeros, we 933 * write the last BYTE with a zero (back up one byte and write a zero). 934 */ 935 936void 937file_flush(int fd, char *fname, int isempt) 938{ 939 static char blnk[] = "\0"; 940 941 /* 942 * silly test, but make sure we are only called when the last block is 943 * filled with all zeros. 944 */ 945 if (!isempt) 946 return; 947 948 /* 949 * move back one byte and write a zero 950 */ 951 if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { 952 syswarn(1, errno, "Failed seek on file %s", fname); 953 return; 954 } 955 956 if (write_with_restart(fd, blnk, 1) < 0) 957 syswarn(1, errno, "Failed write to file %s", fname); 958 return; 959} 960 961/* 962 * rdfile_close() 963 * close a file we have been reading (to copy or archive). If we have to 964 * reset access time (tflag) do so (the times are stored in arcn). 965 */ 966 967void 968rdfile_close(ARCHD *arcn, int *fd) 969{ 970 /* 971 * make sure the file is open 972 */ 973 if (*fd < 0) 974 return; 975 976 (void)close(*fd); 977 *fd = -1; 978 if (!tflag) 979 return; 980 981 /* 982 * user wants last access time reset 983 */ 984 set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1); 985 return; 986} 987 988/* 989 * set_crc() 990 * read a file to calculate its crc. This is a real drag. Archive formats 991 * that have this, end up reading the file twice (we have to write the 992 * header WITH the crc before writing the file contents. Oh well... 993 * Return: 994 * 0 if was able to calculate the crc, -1 otherwise 995 */ 996 997int 998set_crc(ARCHD *arcn, int fd) 999{ 1000 int i; 1001 int res; 1002 off_t cpcnt = 0L; 1003 u_long size; 1004 unsigned long crc = 0L; 1005 char tbuf[FILEBLK]; 1006 struct stat sb; 1007 1008 if (fd < 0) { 1009 /* 1010 * hmm, no fd, should never happen. well no crc then. 1011 */ 1012 arcn->crc = 0L; 1013 return(0); 1014 } 1015 1016 if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) 1017 size = (u_long)sizeof(tbuf); 1018 1019 /* 1020 * read all the bytes we think that there are in the file. If the user 1021 * is trying to archive an active file, forget this file. 1022 */ 1023 for(;;) { 1024 if ((res = read(fd, tbuf, size)) <= 0) 1025 break; 1026 cpcnt += res; 1027 for (i = 0; i < res; ++i) 1028 crc += (tbuf[i] & 0xff); 1029 } 1030 1031 /* 1032 * safety check. we want to avoid archiving files that are active as 1033 * they can create inconsistant archive copies. 1034 */ 1035 if (cpcnt != arcn->sb.st_size) 1036 tty_warn(1, "File changed size %s", arcn->org_name); 1037 else if (fstat(fd, &sb) < 0) 1038 syswarn(1, errno, "Failed stat on %s", arcn->org_name); 1039 else if (arcn->sb.st_mtime != sb.st_mtime) 1040 tty_warn(1, "File %s was modified during read", arcn->org_name); 1041 else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) 1042 syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); 1043 else { 1044 arcn->crc = crc; 1045 return(0); 1046 } 1047 return(-1); 1048} 1049