History log of /openbsd-current/bin/pax/tar.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.85 17-Apr-2024 jca

Provide a pax format specific option handler

The existing tar_opt() implements support for -o write_opt=nodir for the
old tar and ustar formats. We don't really want to support it for the
pax format, and we want to be able to implement pax format specific
options (even if there are none right now). ok millert@


# 1.84 16-Apr-2024 jca

Fix reading large pax extended records

512 bytes isn't enough if you want to store rather large but still
useful long file names or symbolic links destinations. The best way to
size the buffer to read those records is based upon the largest paths
pax(1) can handle, and that is PAXPATHLEN.

Reported by caspar@, input and ok millert@


# 1.83 16-Apr-2024 jca

Fix pasto: broken storage of symbolic link long destinations in pax format


# 1.82 16-Apr-2024 jca

Revert wip patch, not intended for commit


# 1.81 16-Apr-2024 jca

Add tar(1) -F option to select write format

We want to move towards 'pax' as the default format for writing, this
option lets users downgrade to -F ustar where the 'pax' format isn't
convenient/usable (same as -x <format> in pax(1)).

-F <format> is more generic than -o/-O. -H (GNU tar) was already used
and we don't want long options so --format (NetBSD/FreeBSD) is excluded
too.

ok sthen@ caspar@ millert@


# 1.80 16-Apr-2024 jca

Correctly detect 'pax' format archives in append mode

We expect that existing pax archives start with a global or extended
header. If they don't, append operations will be done using ustar
format.

Fixes append mode on pax archives where pax(1) would bail out when
appending to pax archives, falsely detecting a mismatch. Reading was
unaffected. Reported by caspar@, ok caspar@ millert@


Revision tags: OPENBSD_7_5_BASE
# 1.79 20-Jan-2024 jca

Better formatting for pax extended header times

As specified, don't include the subsecond part if zero and drop trailing
zeros in the subsecond part. ok millert@


# 1.78 27-Dec-2023 jca

'pax' format support for mtime and atime

Access time can't be represented by ustar, so always include it when
using the pax format. Also include an extended header record for mtime
if the file modification time can't be fully represented by ustar (eg
subsecond resolution).

Input & ok millert@


# 1.77 22-Dec-2023 jca

Zap useless newline added in previous


# 1.76 22-Dec-2023 jca

'pax' format support for files over 8GB

ok millert@


# 1.75 21-Dec-2023 jca

Print the proper file name in case we fail to allocate a "path" extended header

Use name, not ln_name. Pasto introduced in previous.


# 1.74 09-Dec-2023 jca

Add basic write support for 'pax' format archives

Keep writing archives in ustar format by default. People can test the
posix 'pax' format using pax(1) -w -x pax ... or cpio -o -H pax ...;
tar(1) can't exercise this code yet. Only long names file and link
names are supported for now.

With input and tests from caspar@, ok millert@


Revision tags: OPENBSD_7_4_BASE
# 1.73 04-Sep-2023 jca

Zap #ifndef SMALL around pax format read support

-DSMALL has never been used to build the ramdisks, thus the support for
reading pax format archives has always been there. This is misleading,
so just zap the ifdef since we want to keep read support.

Went through a make release Just In Case(tm).

Spotted by caspar@, ok millert@ sthen@ caspar@


# 1.72 19-Aug-2023 guenther

Copy entire st_*tim structs at once, rather than copying
the st_*time and (obsolete) st_*timensec members separately.

ok millert@


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.79 20-Jan-2024 jca

Better formatting for pax extended header times

As specified, don't include the subsecond part if zero and drop trailing
zeros in the subsecond part. ok millert@


# 1.78 27-Dec-2023 jca

'pax' format support for mtime and atime

Access time can't be represented by ustar, so always include it when
using the pax format. Also include an extended header record for mtime
if the file modification time can't be fully represented by ustar (eg
subsecond resolution).

Input & ok millert@


# 1.77 22-Dec-2023 jca

Zap useless newline added in previous


# 1.76 22-Dec-2023 jca

'pax' format support for files over 8GB

ok millert@


# 1.75 21-Dec-2023 jca

Print the proper file name in case we fail to allocate a "path" extended header

Use name, not ln_name. Pasto introduced in previous.


# 1.74 09-Dec-2023 jca

Add basic write support for 'pax' format archives

Keep writing archives in ustar format by default. People can test the
posix 'pax' format using pax(1) -w -x pax ... or cpio -o -H pax ...;
tar(1) can't exercise this code yet. Only long names file and link
names are supported for now.

With input and tests from caspar@, ok millert@


Revision tags: OPENBSD_7_4_BASE
# 1.73 04-Sep-2023 jca

Zap #ifndef SMALL around pax format read support

-DSMALL has never been used to build the ramdisks, thus the support for
reading pax format archives has always been there. This is misleading,
so just zap the ifdef since we want to keep read support.

Went through a make release Just In Case(tm).

Spotted by caspar@, ok millert@ sthen@ caspar@


# 1.72 19-Aug-2023 guenther

Copy entire st_*tim structs at once, rather than copying
the st_*time and (obsolete) st_*timensec members separately.

ok millert@


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.78 27-Dec-2023 jca

'pax' format support for mtime and atime

Access time can't be represented by ustar, so always include it when
using the pax format. Also include an extended header record for mtime
if the file modification time can't be fully represented by ustar (eg
subsecond resolution).

Input & ok millert@


# 1.77 22-Dec-2023 jca

Zap useless newline added in previous


# 1.76 22-Dec-2023 jca

'pax' format support for files over 8GB

ok millert@


# 1.75 21-Dec-2023 jca

Print the proper file name in case we fail to allocate a "path" extended header

Use name, not ln_name. Pasto introduced in previous.


# 1.74 09-Dec-2023 jca

Add basic write support for 'pax' format archives

Keep writing archives in ustar format by default. People can test the
posix 'pax' format using pax(1) -w -x pax ... or cpio -o -H pax ...;
tar(1) can't exercise this code yet. Only long names file and link
names are supported for now.

With input and tests from caspar@, ok millert@


Revision tags: OPENBSD_7_4_BASE
# 1.73 04-Sep-2023 jca

Zap #ifndef SMALL around pax format read support

-DSMALL has never been used to build the ramdisks, thus the support for
reading pax format archives has always been there. This is misleading,
so just zap the ifdef since we want to keep read support.

Went through a make release Just In Case(tm).

Spotted by caspar@, ok millert@ sthen@ caspar@


# 1.72 19-Aug-2023 guenther

Copy entire st_*tim structs at once, rather than copying
the st_*time and (obsolete) st_*timensec members separately.

ok millert@


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.77 22-Dec-2023 jca

Zap useless newline added in previous


# 1.76 22-Dec-2023 jca

'pax' format support for files over 8GB

ok millert@


# 1.75 21-Dec-2023 jca

Print the proper file name in case we fail to allocate a "path" extended header

Use name, not ln_name. Pasto introduced in previous.


# 1.74 09-Dec-2023 jca

Add basic write support for 'pax' format archives

Keep writing archives in ustar format by default. People can test the
posix 'pax' format using pax(1) -w -x pax ... or cpio -o -H pax ...;
tar(1) can't exercise this code yet. Only long names file and link
names are supported for now.

With input and tests from caspar@, ok millert@


Revision tags: OPENBSD_7_4_BASE
# 1.73 04-Sep-2023 jca

Zap #ifndef SMALL around pax format read support

-DSMALL has never been used to build the ramdisks, thus the support for
reading pax format archives has always been there. This is misleading,
so just zap the ifdef since we want to keep read support.

Went through a make release Just In Case(tm).

Spotted by caspar@, ok millert@ sthen@ caspar@


# 1.72 19-Aug-2023 guenther

Copy entire st_*tim structs at once, rather than copying
the st_*time and (obsolete) st_*timensec members separately.

ok millert@


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.75 21-Dec-2023 jca

Print the proper file name in case we fail to allocate a "path" extended header

Use name, not ln_name. Pasto introduced in previous.


# 1.74 09-Dec-2023 jca

Add basic write support for 'pax' format archives

Keep writing archives in ustar format by default. People can test the
posix 'pax' format using pax(1) -w -x pax ... or cpio -o -H pax ...;
tar(1) can't exercise this code yet. Only long names file and link
names are supported for now.

With input and tests from caspar@, ok millert@


Revision tags: OPENBSD_7_4_BASE
# 1.73 04-Sep-2023 jca

Zap #ifndef SMALL around pax format read support

-DSMALL has never been used to build the ramdisks, thus the support for
reading pax format archives has always been there. This is misleading,
so just zap the ifdef since we want to keep read support.

Went through a make release Just In Case(tm).

Spotted by caspar@, ok millert@ sthen@ caspar@


# 1.72 19-Aug-2023 guenther

Copy entire st_*tim structs at once, rather than copying
the st_*time and (obsolete) st_*timensec members separately.

ok millert@


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.74 09-Dec-2023 jca

Add basic write support for 'pax' format archives

Keep writing archives in ustar format by default. People can test the
posix 'pax' format using pax(1) -w -x pax ... or cpio -o -H pax ...;
tar(1) can't exercise this code yet. Only long names file and link
names are supported for now.

With input and tests from caspar@, ok millert@


Revision tags: OPENBSD_7_4_BASE
# 1.73 04-Sep-2023 jca

Zap #ifndef SMALL around pax format read support

-DSMALL has never been used to build the ramdisks, thus the support for
reading pax format archives has always been there. This is misleading,
so just zap the ifdef since we want to keep read support.

Went through a make release Just In Case(tm).

Spotted by caspar@, ok millert@ sthen@ caspar@


# 1.72 19-Aug-2023 guenther

Copy entire st_*tim structs at once, rather than copying
the st_*time and (obsolete) st_*timensec members separately.

ok millert@


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.73 04-Sep-2023 jca

Zap #ifndef SMALL around pax format read support

-DSMALL has never been used to build the ramdisks, thus the support for
reading pax format archives has always been there. This is misleading,
so just zap the ifdef since we want to keep read support.

Went through a make release Just In Case(tm).

Spotted by caspar@, ok millert@ sthen@ caspar@


# 1.72 19-Aug-2023 guenther

Copy entire st_*tim structs at once, rather than copying
the st_*time and (obsolete) st_*timensec members separately.

ok millert@


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.72 19-Aug-2023 guenther

Copy entire st_*tim structs at once, rather than copying
the st_*time and (obsolete) st_*timensec members separately.

ok millert@


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.71 26-Jun-2023 millert

pax: truncate times to MAX_TIME_T, not INT_MAX
If the mtime in the file header is larger than MAX_TIME_T, trucate
it to MAX_TIME_T, not INT_MAX. OK otto@


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.70 01-Mar-2022 sthen

Support mtime/atime/ctime extended headers in !SMALL builds.
These are becoming quite common in distributed software (including
tars produced by Python and Go) and often standard timestamps are
not set, resulting in extracted files dated as the epoch.

Lots of help from tb@, ok tb@ millert@


Revision tags: OPENBSD_7_0_BASE
# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.69 14-Jun-2021 deraadt

32-bit systems incorrectly parse the (64-bit) length of ustar extended
headers (hd->size) using a 32-bit operation.
from Samanta Navarro
ok guenther


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.68 24-Jun-2019 deraadt

uid_from_user() and gid_from_user() return -1 when indicating error,
not arbitrary values < 0.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.67 13-Sep-2018 millert

Use the new libc uid_from_user() and gid_from_group() instead of
the pax-specific functions in cache.c. OK guenther@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


Revision tags: OPENBSD_6_2_BASE
# 1.66 16-Sep-2017 otto

Carefully add casts to silence clang sign-compare warnings. ok millert@


# 1.65 12-Sep-2017 otto

there is no offical way to get the max value of time_t, but this one works
on any sensible posix system (in which time_t must be an integer type)
ok deraadt@ millert@


# 1.64 08-Sep-2017 otto

Avoid clang warning and make code better by using a signed long;
with hint from millert@; ok millert@ guenther@


Revision tags: OPENBSD_6_1_BASE
# 1.63 26-Aug-2016 guenther

Don't need <sys/time.h> or "options.h" here


# 1.62 25-Aug-2016 guenther

Replace name_{uid,gid}() with the libc routines user_from_uid() and
group_from_gid(). Eliminate some superfluous strncpy() calls.

ok millert@


# 1.61 14-Aug-2016 guenther

Remove many unnecessary casts. Verified by comparing generated code on
both ILP32 and LP64.

ok millert@


# 1.60 14-Aug-2016 guenther

Replace u_quad_t with unsigned long long and replace "uqd" with "ull" in
function names to match. Pull some tangled assignments out of conditions
and use >>= where possible.

ok millert@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.59 15-Feb-2016 guenther

To archive a 101 character absolute path in ustar format we must
split it on a slash other than the leading one.

Fix based on patches from Peter Fokker (openbsd (at) berestijn.nl) and
Peter Bisroev (peter (at) int19h.net)


Revision tags: OPENBSD_5_8_BASE
# 1.58 17-Mar-2015 guenther

Add PAX_IS_{REG,HARDLINK,LINK} macros to simply many file type tests

ok millert@


# 1.57 15-Mar-2015 guenther

Handle tar -o by setting the tar_nodir directly instead of faking up a
pax-like -o write_opt=nodir.

ok millert@ otto@


# 1.56 09-Mar-2015 guenther

Unrevert post-unlock:
* Prevent an archive from esacaping the current directory by itself:
when extracting a symlink whose value is absolute or contains ".."
components, just create a zero-length normal file (with additional
tracking of the mode and hardlinks to the symlink) until everything
else is extracted, then go back and replace it with the requested
link (if it's still that zero-length placeholder).

* For tar without -P, if a path in the archive has any ".." components
then strip everything up to and including the last of them (if
it ends in ".." then it becomes ".")
This mostly follows GNU tar's behavior, except for 'tar tf' and
'tar xvf' we report the modified path that would be/was actually
created instead of the raw path from the archive

Above two fixes prompted by a report from Daniel Cegielka
(daniel.cegielka (at) gmail.com)

* For directories whose times or mode will be fixed up in the
clean-up pass, record their dev+ino and then use
open(O_DIRECTORY)+fstat() to verify that we're updating the correct
directory before using futimens() and fchmod().

* Correct buffer overflow in handling of pax extension headers,
caught by the memcpy() overlap check.


previously ok millert@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.55 21-Feb-2015 guenther

branches: 1.55.2;
Recent changes haven't been completely stable, so revert for the 5.7 release

requested by deraadt@


# 1.54 29-Jan-2015 guenther

Correct buffer overflow in handling of pax extension headers, caught
by the memcpy() overlap check.

ok millert@ deraadt@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.53 19-Feb-2014 guenther

branches: 1.53.6;
Map negative mtimes to zero instead of skipping the affected files.

problem noted by miod@
ok krw@ millert@


# 1.52 08-Jan-2014 guenther

Zap trailing whitespace


# 1.51 08-Jan-2014 guenther

Eliminate poisonous LONG_OFF_T conditional

adjective suggested by deraadt@


# 1.50 08-Jan-2014 guenther

Mark some functions as printf-like and fix a bogus format string

ok fgsch@


# 1.49 21-Nov-2013 deraadt

add unsigned char casts for specific calls to ctype.h macros.
ok guenther step


Revision tags: OPENBSD_5_4_BASE
# 1.48 03-Jul-2013 guenther

cpio and ustar formats store times in octal fields that are 11 characters
wide, so they support up to 33bits. Take advantage of the extra bits by
no longer forcing them into 32bit ints before the time_t conversion. This
gets us another 204 years of range once time_t changes type

ok deraadt@ tedu@


# 1.47 11-Apr-2013 guenther

The tweaks I suggested to the previous diff resulted in the typeflag
being checked after it was overwritten by the next block read in.
Eliminate the argument aliasing that led to this being overlooked
by passing rd_xheader() the size and typeflag directly.

problem discovery and ok fgsch@


# 1.46 09-Apr-2013 fgsch

Add extended header support for ustar. Currently only path and linkpath are
handled.
input from zhuk and guenther. tested by zhuk and sthen on a bulk.
ok guenther.


# 1.45 27-Mar-2013 zhuk

Enable support for write_opt=nodir for ustar archives which helps to fix
issues with some crappy tar archive readers out there that rely on the
fact that directory entries ends up with "/" when created by GNU Tar, and
now I'm finishing this commit message by mentioning people who gave input
and okays: deraadt@ millert@ jmc@


Revision tags: OPENBSD_5_3_BASE
# 1.44 04-Dec-2012 deraadt

remove some unnecessary sys/param.h inclusions


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.43 02-Dec-2010 tedu

a -N option for tar that uses numeric only IDs, useful for cross system
tar file manipulation. with advice from guenther and jmc.


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.42 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.41 04-Mar-2006 otto

Properly take into account that the name and prefix field in the tar
header are not always NUL-terminated. This means there's room for 1
more byte in those field. This effectively reverts revs 1.13 and 1.14;
ok jaredy@ millert@


Revision tags: OPENBSD_3_9_BASE
# 1.40 17-Dec-2005 otto

Avoid sign extend when writing time stamps; fixes "tar: Ustar header
field is too small for foo" error messages when writing files with
negative time stamps on 64 bit archs.
ok millert@ deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.39 13-Jun-2005 otto

Fix writing of old-style tar headers. Filenames and linknames actually
are not NUL terminated if the fill the whole field.
ok millert@ jaredy@


# 1.38 28-Apr-2005 otto

Use a special crafted string copy function to copy data from ustar
headers to the generic pax structs. ustar is "funny" since some fields
are not always NUL terminated. Old-style tar headers and ustar
creation remains to be done. ok millert@ beck@


# 1.37 21-Apr-2005 beck

fix strlcpy abuse in pax - this commit turns potential overflows into
potential non-spec compliance - the use of these fields as strings needs
to be revisited more thouroughly.
ok millert@ otto@


# 1.36 14-Apr-2005 markus

add the prefix length to nlen for ustar; ok otto millert


# 1.35 10-Apr-2005 otto

Handle path names of exactly 100 chars correctly. Based on a diff
from espie@. ok espie@ deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.34 23-Oct-2004 otto

If a uid or gid does not fit into into the tar header, issue a warning
and use the uid/gid of nobody. Spotted by and ok drahn@, ok millert@


Revision tags: OPENBSD_3_6_BASE
# 1.33 16-Apr-2004 deraadt

spacing


# 1.32 30-Mar-2004 millert

Add support for expanding GNU long links from NetBSD. I've had this
in my tree for ages but didn't have a proper test case. Thanks to
otto@ for providing one.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.31 26-Jun-2003 deraadt

protos. this requires changing the api for the *trail() functions a bit


# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE
# 1.29 18-Oct-2002 millert

Pull in some changes from NetBSD
o When extracting GNU tar archives, honor @LongLink long links/files
o Add an option to prevent pax from prompting for the next volume
upon premature end of archive.


# 1.28 16-Oct-2002 millert

sprinkle const; mostly from NetBSD


# 1.27 16-Oct-2002 millert

Fix comment typos; most from NetBSD and FreeBSD


# 1.26 16-Oct-2002 millert

kill register


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.25 19-Feb-2002 millert

We live in an ANSI C world. Remove lots of gratuitous #ifdef __STDC__ cruft.


# 1.24 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.23 19-Dec-2001 millert

When writing tar and ustar archives, zero out the header before we
do anything else to it. This lets us restore the strlcpy() calls
and remove a buch of memset()s that were zeroing out individual
fields of the header.


# 1.22 19-Dec-2001 millert

More strlcpy() backout. Various tar programs require ustar header
elements to be zero padded too.


# 1.21 19-Dec-2001 millert

Back out a strncpy -> strlcpy conversion. When writing old style
tar files we need to zero-pad the file name or many tars will get
a directory checksum error trying to unpack the archive.

This does not affect ustar archives (pax's default) though whether
or not padding matters there still needs to be determined.


Revision tags: OPENBSD_3_0_BASE
# 1.20 26-Jun-2001 lebel

really use strlcpy.


# 1.19 26-Jun-2001 lebel

use strlcpy vs strncpy+a[len-1]='\0'


# 1.18 26-May-2001 millert

Get rid of NET2_FTS and NET2_REGEX #ifdefs since they are useless.
Change NET2_STAT into LONG_OFF_T for portability to systems with
a 32bit off_t.


# 1.17 16-May-2001 mickey

use proper str*cpy functions instead of home grown one, spaces; millert@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.16 04-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.15 21-Jan-2000 tholo

Work with files larger than 2 Gb; from NetBSD change by mycroft@netbsd.org


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.14 19-Oct-1998 millert

Fix off-by-one error when adding files of exactly 100 characters; wsanchez@apple.com


# 1.13 26-Sep-1998 millert

fix storage of paths that are exactly 100 chars; mgw@pacbell.net


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.12 01-Sep-1997 deraadt

i am bored enough to fix terminal space/tab uglies


# 1.11 25-Jul-1997 mickey

#if __STDC__ --> #ifdef __STDC__


# 1.10 04-Jun-1997 millert

Fix usage of l_strncpy() (noticed by Theo) and make l_strncpy()
pad with NULL's like strncpy(3). This eliminates the need for
zf_strncpy(); ocurrences of zf_strncpy() have been changed to l_strncpy().


Revision tags: OPENBSD_2_1_BASE
# 1.9 05-Apr-1997 millert

Strip leading '/' of pathnames (only in tar mode). -S option turns
this off like GNU tar.


# 1.8 02-Apr-1997 millert

Deal with old-style tar archives with a directory specifier (extension).


# 1.7 25-Mar-1997 millert

Back out my previous kludge to make tar files with short trailers work in
favor of a simpler change--never try to do a media change when invoked
as tar. This fixes problems that crop up if gzip exits unhappily and
others.


# 1.6 16-Feb-1997 tholo

Correct handling of long filenames that has been stored with a prefix in
the archive; generate POSIXly correct padding of octal fields.


# 1.5 10-Feb-1997 millert

Permit single block trailers differently.


Revision tags: OPENBSD_2_0_BASE
# 1.4 27-Aug-1996 tholo

Fix uses of strncpy


# 1.3 23-Jun-1996 deraadt

update rcsid


# 1.2 11-Jun-1996 tholo

Correct compile warnings
Rename warn() to paxwarn() so <err.h> can be included

Remove #include <ctype.h> when not needed; from FreeBSD


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision