tar.c revision 1.75 1 1.75 gutterid /* $NetBSD: tar.c,v 1.75 2019/03/20 03:13:39 gutteridge Exp $ */
2 1.5 cgd
3 1.1 jtc /*-
4 1.44 agc * Copyright (c) 1992 Keith Muller.
5 1.1 jtc * Copyright (c) 1992, 1993
6 1.1 jtc * The Regents of the University of California. All rights reserved.
7 1.1 jtc *
8 1.1 jtc * This code is derived from software contributed to Berkeley by
9 1.1 jtc * Keith Muller of the University of California, San Diego.
10 1.1 jtc *
11 1.1 jtc * Redistribution and use in source and binary forms, with or without
12 1.1 jtc * modification, are permitted provided that the following conditions
13 1.1 jtc * are met:
14 1.1 jtc * 1. Redistributions of source code must retain the above copyright
15 1.1 jtc * notice, this list of conditions and the following disclaimer.
16 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jtc * notice, this list of conditions and the following disclaimer in the
18 1.1 jtc * documentation and/or other materials provided with the distribution.
19 1.43 agc * 3. Neither the name of the University nor the names of its contributors
20 1.43 agc * may be used to endorse or promote products derived from this software
21 1.43 agc * without specific prior written permission.
22 1.43 agc *
23 1.43 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.43 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.43 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.43 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.43 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.43 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.43 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.43 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.43 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.43 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.43 agc * SUCH DAMAGE.
34 1.43 agc */
35 1.43 agc
36 1.45 lukem #if HAVE_NBTOOL_CONFIG_H
37 1.45 lukem #include "nbtool_config.h"
38 1.45 lukem #endif
39 1.45 lukem
40 1.9 christos #include <sys/cdefs.h>
41 1.45 lukem #if !defined(lint)
42 1.5 cgd #if 0
43 1.5 cgd static char sccsid[] = "@(#)tar.c 8.2 (Berkeley) 4/18/94";
44 1.5 cgd #else
45 1.75 gutterid __RCSID("$NetBSD: tar.c,v 1.75 2019/03/20 03:13:39 gutteridge Exp $");
46 1.5 cgd #endif
47 1.1 jtc #endif /* not lint */
48 1.1 jtc
49 1.1 jtc #include <sys/types.h>
50 1.1 jtc #include <sys/time.h>
51 1.1 jtc #include <sys/stat.h>
52 1.1 jtc #include <sys/param.h>
53 1.10 mycroft
54 1.10 mycroft #include <ctype.h>
55 1.13 mrg #include <errno.h>
56 1.10 mycroft #include <grp.h>
57 1.10 mycroft #include <pwd.h>
58 1.10 mycroft #include <stdio.h>
59 1.10 mycroft #include <stdlib.h>
60 1.1 jtc #include <string.h>
61 1.1 jtc #include <unistd.h>
62 1.10 mycroft
63 1.1 jtc #include "pax.h"
64 1.1 jtc #include "extern.h"
65 1.1 jtc #include "tar.h"
66 1.1 jtc
67 1.73 christos extern struct stat tst;
68 1.73 christos
69 1.1 jtc /*
70 1.1 jtc * Routines for reading, writing and header identify of various versions of tar
71 1.1 jtc */
72 1.1 jtc
73 1.58 christos static int expandname(char *, size_t, char **, size_t *, const char *, size_t);
74 1.55 christos static void longlink(ARCHD *, int);
75 1.70 christos static uint32_t tar_chksm(char *, int);
76 1.19 lukem static char *name_split(char *, int);
77 1.70 christos static int u32_oct(uintmax_t, char *, int, int);
78 1.70 christos static int umax_oct(uintmax_t, char *, int, int);
79 1.33 mrg static int tar_gnutar_exclude_one(const char *, size_t);
80 1.46 matt static int check_sum(char *, size_t, char *, size_t, int);
81 1.1 jtc
82 1.1 jtc /*
83 1.1 jtc * Routines common to all versions of tar
84 1.1 jtc */
85 1.1 jtc
86 1.1 jtc static int tar_nodir; /* do not write dirs under old tar */
87 1.28 christos int is_gnutar; /* behave like gnu tar; enable gnu
88 1.65 christos * extensions and skip end-of-volume
89 1.26 christos * checks
90 1.26 christos */
91 1.31 christos static int seen_gnu_warning; /* Have we warned yet? */
92 1.29 christos static char *gnu_hack_string; /* ././@LongLink hackery */
93 1.29 christos static int gnu_hack_len; /* len of gnu_hack_string */
94 1.29 christos char *gnu_name_string; /* ././@LongLink hackery name */
95 1.30 christos char *gnu_link_string; /* ././@LongLink hackery link */
96 1.58 christos size_t gnu_name_length; /* ././@LongLink hackery name */
97 1.58 christos size_t gnu_link_length; /* ././@LongLink hackery link */
98 1.52 christos static int gnu_short_trailer; /* gnu short trailer */
99 1.1 jtc
100 1.54 christos static const char LONG_LINK[] = "././@LongLink";
101 1.54 christos
102 1.53 christos #ifdef _PAX_
103 1.53 christos char DEV_0[] = "/dev/rst0";
104 1.53 christos char DEV_1[] = "/dev/rst1";
105 1.53 christos char DEV_4[] = "/dev/rst4";
106 1.53 christos char DEV_5[] = "/dev/rst5";
107 1.53 christos char DEV_7[] = "/dev/rst7";
108 1.53 christos char DEV_8[] = "/dev/rst8";
109 1.53 christos #endif
110 1.53 christos
111 1.35 christos static int
112 1.46 matt check_sum(char *hd, size_t hdlen, char *bl, size_t bllen, int quiet)
113 1.35 christos {
114 1.70 christos uint32_t hdck, blck;
115 1.35 christos
116 1.70 christos hdck = asc_u32(hd, hdlen, OCT);
117 1.35 christos blck = tar_chksm(bl, bllen);
118 1.35 christos
119 1.35 christos if (hdck != blck) {
120 1.46 matt if (!quiet)
121 1.70 christos tty_warn(0, "Header checksum %o does not match %o",
122 1.46 matt hdck, blck);
123 1.63 dsl return -1;
124 1.35 christos }
125 1.63 dsl return 0;
126 1.35 christos }
127 1.35 christos
128 1.35 christos
129 1.1 jtc /*
130 1.1 jtc * tar_endwr()
131 1.1 jtc * add the tar trailer of two null blocks
132 1.1 jtc * Return:
133 1.1 jtc * 0 if ok, -1 otherwise (what wr_skip returns)
134 1.1 jtc */
135 1.1 jtc
136 1.1 jtc int
137 1.1 jtc tar_endwr(void)
138 1.1 jtc {
139 1.63 dsl return wr_skip((off_t)(NULLCNT * BLKMULT));
140 1.1 jtc }
141 1.1 jtc
142 1.1 jtc /*
143 1.1 jtc * tar_endrd()
144 1.1 jtc * no cleanup needed here, just return size of trailer (for append)
145 1.1 jtc * Return:
146 1.47 christos * size of trailer BLKMULT
147 1.1 jtc */
148 1.1 jtc
149 1.1 jtc off_t
150 1.1 jtc tar_endrd(void)
151 1.1 jtc {
152 1.63 dsl return (off_t)((gnu_short_trailer ? 1 : NULLCNT) * BLKMULT);
153 1.1 jtc }
154 1.1 jtc
155 1.1 jtc /*
156 1.1 jtc * tar_trail()
157 1.1 jtc * Called to determine if a header block is a valid trailer. We are passed
158 1.1 jtc * the block, the in_sync flag (which tells us we are in resync mode;
159 1.1 jtc * looking for a valid header), and cnt (which starts at zero) which is
160 1.1 jtc * used to count the number of empty blocks we have seen so far.
161 1.1 jtc * Return:
162 1.1 jtc * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
163 1.1 jtc * could never contain a header.
164 1.1 jtc */
165 1.1 jtc
166 1.1 jtc int
167 1.6 tls tar_trail(char *buf, int in_resync, int *cnt)
168 1.1 jtc {
169 1.6 tls int i;
170 1.1 jtc
171 1.52 christos gnu_short_trailer = 0;
172 1.1 jtc /*
173 1.1 jtc * look for all zero, trailer is two consecutive blocks of zero
174 1.1 jtc */
175 1.1 jtc for (i = 0; i < BLKMULT; ++i) {
176 1.36 christos if (buf[i] != '\0')
177 1.1 jtc break;
178 1.1 jtc }
179 1.1 jtc
180 1.1 jtc /*
181 1.1 jtc * if not all zero it is not a trailer, but MIGHT be a header.
182 1.1 jtc */
183 1.1 jtc if (i != BLKMULT)
184 1.63 dsl return -1;
185 1.1 jtc
186 1.1 jtc /*
187 1.1 jtc * When given a zero block, we must be careful!
188 1.1 jtc * If we are not in resync mode, check for the trailer. Have to watch
189 1.1 jtc * out that we do not mis-identify file data as the trailer, so we do
190 1.1 jtc * NOT try to id a trailer during resync mode. During resync mode we
191 1.1 jtc * might as well throw this block out since a valid header can NEVER be
192 1.1 jtc * a block of all 0 (we must have a valid file name).
193 1.1 jtc */
194 1.37 christos if (!in_resync) {
195 1.37 christos ++*cnt;
196 1.37 christos /*
197 1.37 christos * old GNU tar (up through 1.13) only writes one block of
198 1.37 christos * trailers, so we pretend we got another
199 1.37 christos */
200 1.52 christos if (is_gnutar) {
201 1.52 christos gnu_short_trailer = 1;
202 1.37 christos ++*cnt;
203 1.52 christos }
204 1.37 christos if (*cnt >= NULLCNT)
205 1.63 dsl return 0;
206 1.37 christos }
207 1.63 dsl return 1;
208 1.1 jtc }
209 1.1 jtc
210 1.1 jtc /*
211 1.70 christos * u32_oct()
212 1.70 christos * convert an uintmax_t to an octal string. many oddball field
213 1.1 jtc * termination characters are used by the various versions of tar in the
214 1.7 kleink * different fields. term selects which kind to use. str is '0' padded
215 1.1 jtc * at the front to len. we are unable to use only one format as many old
216 1.1 jtc * tar readers are very cranky about this.
217 1.1 jtc * Return:
218 1.1 jtc * 0 if the number fit into the string, -1 otherwise
219 1.1 jtc */
220 1.1 jtc
221 1.1 jtc static int
222 1.70 christos u32_oct(uintmax_t val, char *str, int len, int term)
223 1.1 jtc {
224 1.6 tls char *pt;
225 1.70 christos uint64_t p;
226 1.70 christos
227 1.70 christos p = val & TOP_HALF;
228 1.70 christos if (p && p != TOP_HALF)
229 1.70 christos return -1;
230 1.70 christos
231 1.70 christos val &= BOTTOM_HALF;
232 1.18 itohy
233 1.1 jtc /*
234 1.1 jtc * term selects the appropriate character(s) for the end of the string
235 1.1 jtc */
236 1.1 jtc pt = str + len - 1;
237 1.1 jtc switch(term) {
238 1.1 jtc case 3:
239 1.1 jtc *pt-- = '\0';
240 1.1 jtc break;
241 1.1 jtc case 2:
242 1.1 jtc *pt-- = ' ';
243 1.1 jtc *pt-- = '\0';
244 1.1 jtc break;
245 1.1 jtc case 1:
246 1.1 jtc *pt-- = ' ';
247 1.1 jtc break;
248 1.1 jtc case 0:
249 1.1 jtc default:
250 1.1 jtc *pt-- = '\0';
251 1.1 jtc *pt-- = ' ';
252 1.1 jtc break;
253 1.1 jtc }
254 1.1 jtc
255 1.1 jtc /*
256 1.1 jtc * convert and blank pad if there is space
257 1.1 jtc */
258 1.1 jtc while (pt >= str) {
259 1.1 jtc *pt-- = '0' + (char)(val & 0x7);
260 1.70 christos if ((val = val >> 3) == 0)
261 1.1 jtc break;
262 1.1 jtc }
263 1.1 jtc
264 1.1 jtc while (pt >= str)
265 1.7 kleink *pt-- = '0';
266 1.70 christos if (val != 0)
267 1.63 dsl return -1;
268 1.63 dsl return 0;
269 1.1 jtc }
270 1.1 jtc
271 1.1 jtc /*
272 1.70 christos * umax_oct()
273 1.20 lukem * convert an unsigned long long to an octal string. one of many oddball
274 1.20 lukem * field termination characters are used by the various versions of tar
275 1.20 lukem * in the different fields. term selects which kind to use. str is '0'
276 1.20 lukem * padded at the front to len. we are unable to use only one format as
277 1.20 lukem * many old tar readers are very cranky about this.
278 1.1 jtc * Return:
279 1.1 jtc * 0 if the number fit into the string, -1 otherwise
280 1.1 jtc */
281 1.1 jtc
282 1.1 jtc static int
283 1.70 christos umax_oct(uintmax_t val, char *str, int len, int term)
284 1.1 jtc {
285 1.6 tls char *pt;
286 1.18 itohy
287 1.1 jtc /*
288 1.1 jtc * term selects the appropriate character(s) for the end of the string
289 1.1 jtc */
290 1.1 jtc pt = str + len - 1;
291 1.1 jtc switch(term) {
292 1.1 jtc case 3:
293 1.1 jtc *pt-- = '\0';
294 1.1 jtc break;
295 1.1 jtc case 2:
296 1.1 jtc *pt-- = ' ';
297 1.1 jtc *pt-- = '\0';
298 1.1 jtc break;
299 1.1 jtc case 1:
300 1.1 jtc *pt-- = ' ';
301 1.1 jtc break;
302 1.1 jtc case 0:
303 1.1 jtc default:
304 1.1 jtc *pt-- = '\0';
305 1.1 jtc *pt-- = ' ';
306 1.1 jtc break;
307 1.1 jtc }
308 1.1 jtc
309 1.1 jtc /*
310 1.1 jtc * convert and blank pad if there is space
311 1.1 jtc */
312 1.1 jtc while (pt >= str) {
313 1.1 jtc *pt-- = '0' + (char)(val & 0x7);
314 1.1 jtc if ((val = val >> 3) == 0)
315 1.1 jtc break;
316 1.1 jtc }
317 1.1 jtc
318 1.1 jtc while (pt >= str)
319 1.7 kleink *pt-- = '0';
320 1.70 christos if (val != 0)
321 1.63 dsl return -1;
322 1.63 dsl return 0;
323 1.1 jtc }
324 1.1 jtc
325 1.1 jtc /*
326 1.1 jtc * tar_chksm()
327 1.1 jtc * calculate the checksum for a tar block counting the checksum field as
328 1.18 itohy * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
329 1.1 jtc * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
330 1.1 jtc * pad headers with 0.
331 1.1 jtc * Return:
332 1.1 jtc * unsigned long checksum
333 1.1 jtc */
334 1.1 jtc
335 1.70 christos static uint32_t
336 1.6 tls tar_chksm(char *blk, int len)
337 1.1 jtc {
338 1.6 tls char *stop;
339 1.6 tls char *pt;
340 1.70 christos uint32_t chksm = BLNKSUM; /* initial value is checksum field sum */
341 1.1 jtc
342 1.1 jtc /*
343 1.1 jtc * add the part of the block before the checksum field
344 1.1 jtc */
345 1.1 jtc pt = blk;
346 1.1 jtc stop = blk + CHK_OFFSET;
347 1.18 itohy while (pt < stop)
348 1.70 christos chksm += (uint32_t)(*pt++ & 0xff);
349 1.1 jtc /*
350 1.1 jtc * move past the checksum field and keep going, spec counts the
351 1.1 jtc * checksum field as the sum of 8 blanks (which is pre-computed as
352 1.1 jtc * BLNKSUM).
353 1.1 jtc * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
354 1.75 gutterid * starts, no point in summing zeros)
355 1.1 jtc */
356 1.1 jtc pt += CHK_LEN;
357 1.1 jtc stop = blk + len;
358 1.1 jtc while (pt < stop)
359 1.70 christos chksm += (uint32_t)(*pt++ & 0xff);
360 1.63 dsl return chksm;
361 1.1 jtc }
362 1.1 jtc
363 1.1 jtc /*
364 1.1 jtc * Routines for old BSD style tar (also made portable to sysV tar)
365 1.1 jtc */
366 1.1 jtc
367 1.1 jtc /*
368 1.1 jtc * tar_id()
369 1.1 jtc * determine if a block given to us is a valid tar header (and not a USTAR
370 1.1 jtc * header). We have to be on the lookout for those pesky blocks of all
371 1.75 gutterid * zeros.
372 1.1 jtc * Return:
373 1.1 jtc * 0 if a tar header, -1 otherwise
374 1.1 jtc */
375 1.1 jtc
376 1.1 jtc int
377 1.6 tls tar_id(char *blk, int size)
378 1.1 jtc {
379 1.6 tls HD_TAR *hd;
380 1.6 tls HD_USTAR *uhd;
381 1.61 christos static int is_ustar = -1;
382 1.1 jtc
383 1.1 jtc if (size < BLKMULT)
384 1.63 dsl return -1;
385 1.1 jtc hd = (HD_TAR *)blk;
386 1.1 jtc uhd = (HD_USTAR *)blk;
387 1.1 jtc
388 1.1 jtc /*
389 1.75 gutterid * check for block of zeros first, a simple and fast test, then make
390 1.1 jtc * sure this is not a ustar header by looking for the ustar magic
391 1.1 jtc * cookie. We should use TMAGLEN, but some USTAR archive programs are
392 1.1 jtc * wrong and create archives missing the \0. Last we check the
393 1.1 jtc * checksum. If this is ok we have to assume it is a valid header.
394 1.1 jtc */
395 1.1 jtc if (hd->name[0] == '\0')
396 1.63 dsl return -1;
397 1.61 christos if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0) {
398 1.61 christos if (is_ustar == -1) {
399 1.61 christos is_ustar = 1;
400 1.63 dsl return -1;
401 1.61 christos } else
402 1.61 christos tty_warn(0,
403 1.61 christos "Busted tar archive: has both ustar and old tar "
404 1.61 christos "records");
405 1.61 christos } else
406 1.61 christos is_ustar = 0;
407 1.46 matt return check_sum(hd->chksum, sizeof(hd->chksum), blk, BLKMULT, 1);
408 1.1 jtc }
409 1.1 jtc
410 1.1 jtc /*
411 1.1 jtc * tar_opt()
412 1.1 jtc * handle tar format specific -o options
413 1.1 jtc * Return:
414 1.1 jtc * 0 if ok -1 otherwise
415 1.1 jtc */
416 1.1 jtc
417 1.1 jtc int
418 1.1 jtc tar_opt(void)
419 1.1 jtc {
420 1.1 jtc OPLIST *opt;
421 1.1 jtc
422 1.1 jtc while ((opt = opt_next()) != NULL) {
423 1.1 jtc if (strcmp(opt->name, TAR_OPTION) ||
424 1.1 jtc strcmp(opt->value, TAR_NODIR)) {
425 1.9 christos tty_warn(1,
426 1.9 christos "Unknown tar format -o option/value pair %s=%s",
427 1.1 jtc opt->name, opt->value);
428 1.9 christos tty_warn(1,
429 1.9 christos "%s=%s is the only supported tar format option",
430 1.1 jtc TAR_OPTION, TAR_NODIR);
431 1.63 dsl return -1;
432 1.1 jtc }
433 1.1 jtc
434 1.1 jtc /*
435 1.1 jtc * we only support one option, and only when writing
436 1.1 jtc */
437 1.1 jtc if ((act != APPND) && (act != ARCHIVE)) {
438 1.9 christos tty_warn(1, "%s=%s is only supported when writing.",
439 1.1 jtc opt->name, opt->value);
440 1.63 dsl return -1;
441 1.1 jtc }
442 1.1 jtc tar_nodir = 1;
443 1.1 jtc }
444 1.63 dsl return 0;
445 1.1 jtc }
446 1.1 jtc
447 1.1 jtc
448 1.1 jtc /*
449 1.1 jtc * tar_rd()
450 1.1 jtc * extract the values out of block already determined to be a tar header.
451 1.1 jtc * store the values in the ARCHD parameter.
452 1.1 jtc * Return:
453 1.1 jtc * 0
454 1.1 jtc */
455 1.1 jtc
456 1.1 jtc int
457 1.6 tls tar_rd(ARCHD *arcn, char *buf)
458 1.1 jtc {
459 1.6 tls HD_TAR *hd;
460 1.6 tls char *pt;
461 1.1 jtc
462 1.1 jtc /*
463 1.1 jtc * we only get proper sized buffers passed to us
464 1.1 jtc */
465 1.1 jtc if (tar_id(buf, BLKMULT) < 0)
466 1.63 dsl return -1;
467 1.29 christos memset(arcn, 0, sizeof(*arcn));
468 1.1 jtc arcn->org_name = arcn->name;
469 1.29 christos arcn->pat = NULL;
470 1.1 jtc arcn->sb.st_nlink = 1;
471 1.1 jtc
472 1.1 jtc /*
473 1.1 jtc * copy out the name and values in the stat buffer
474 1.1 jtc */
475 1.1 jtc hd = (HD_TAR *)buf;
476 1.30 christos if (hd->linkflag != LONGLINKTYPE && hd->linkflag != LONGNAMETYPE) {
477 1.30 christos arcn->nlen = expandname(arcn->name, sizeof(arcn->name),
478 1.58 christos &gnu_name_string, &gnu_name_length, hd->name,
479 1.58 christos sizeof(hd->name));
480 1.30 christos arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name),
481 1.58 christos &gnu_link_string, &gnu_link_length, hd->linkname,
482 1.58 christos sizeof(hd->linkname));
483 1.30 christos }
484 1.70 christos arcn->sb.st_mode = (mode_t)(asc_u32(hd->mode,sizeof(hd->mode),OCT) &
485 1.1 jtc 0xfff);
486 1.70 christos arcn->sb.st_uid = (uid_t)asc_u32(hd->uid, sizeof(hd->uid), OCT);
487 1.70 christos arcn->sb.st_gid = (gid_t)asc_u32(hd->gid, sizeof(hd->gid), OCT);
488 1.19 lukem arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
489 1.74 christos if (arcn->sb.st_size == -1)
490 1.74 christos return -1;
491 1.70 christos arcn->sb.st_mtime = (time_t)(int32_t)asc_u32(hd->mtime, sizeof(hd->mtime), OCT);
492 1.1 jtc arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
493 1.1 jtc
494 1.1 jtc /*
495 1.1 jtc * have to look at the last character, it may be a '/' and that is used
496 1.1 jtc * to encode this as a directory
497 1.1 jtc */
498 1.1 jtc pt = &(arcn->name[arcn->nlen - 1]);
499 1.1 jtc arcn->pad = 0;
500 1.1 jtc arcn->skip = 0;
501 1.1 jtc switch(hd->linkflag) {
502 1.1 jtc case SYMTYPE:
503 1.1 jtc /*
504 1.1 jtc * symbolic link, need to get the link name and set the type in
505 1.1 jtc * the st_mode so -v printing will look correct.
506 1.1 jtc */
507 1.1 jtc arcn->type = PAX_SLK;
508 1.1 jtc arcn->sb.st_mode |= S_IFLNK;
509 1.1 jtc break;
510 1.1 jtc case LNKTYPE:
511 1.1 jtc /*
512 1.1 jtc * hard link, need to get the link name, set the type in the
513 1.1 jtc * st_mode and st_nlink so -v printing will look better.
514 1.1 jtc */
515 1.1 jtc arcn->type = PAX_HLK;
516 1.1 jtc arcn->sb.st_nlink = 2;
517 1.1 jtc
518 1.1 jtc /*
519 1.1 jtc * no idea of what type this thing really points at, but
520 1.1 jtc * we set something for printing only.
521 1.1 jtc */
522 1.1 jtc arcn->sb.st_mode |= S_IFREG;
523 1.16 mrg break;
524 1.16 mrg case LONGLINKTYPE:
525 1.16 mrg case LONGNAMETYPE:
526 1.16 mrg /*
527 1.16 mrg * GNU long link/file; we tag these here and let the
528 1.16 mrg * pax internals deal with it -- too ugly otherwise.
529 1.16 mrg */
530 1.16 mrg if (hd->linkflag != LONGLINKTYPE)
531 1.16 mrg arcn->type = PAX_GLF;
532 1.54 christos else
533 1.54 christos arcn->type = PAX_GLL;
534 1.16 mrg arcn->pad = TAR_PAD(arcn->sb.st_size);
535 1.16 mrg arcn->skip = arcn->sb.st_size;
536 1.1 jtc break;
537 1.1 jtc case AREGTYPE:
538 1.1 jtc case REGTYPE:
539 1.15 kleink case DIRTYPE: /* see below */
540 1.1 jtc default:
541 1.1 jtc /*
542 1.1 jtc * If we have a trailing / this is a directory and NOT a file.
543 1.15 kleink * Note: V7 tar doesn't actually have DIRTYPE, but it was
544 1.15 kleink * reported that V7 archives using USTAR directories do exist.
545 1.1 jtc */
546 1.15 kleink if (*pt == '/' || hd->linkflag == DIRTYPE) {
547 1.1 jtc /*
548 1.1 jtc * it is a directory, set the mode for -v printing
549 1.1 jtc */
550 1.1 jtc arcn->type = PAX_DIR;
551 1.1 jtc arcn->sb.st_mode |= S_IFDIR;
552 1.1 jtc arcn->sb.st_nlink = 2;
553 1.1 jtc } else {
554 1.1 jtc /*
555 1.1 jtc * have a file that will be followed by data. Set the
556 1.18 itohy * skip value to the size field and calculate the size
557 1.1 jtc * of the padding.
558 1.1 jtc */
559 1.1 jtc arcn->type = PAX_REG;
560 1.1 jtc arcn->sb.st_mode |= S_IFREG;
561 1.1 jtc arcn->pad = TAR_PAD(arcn->sb.st_size);
562 1.1 jtc arcn->skip = arcn->sb.st_size;
563 1.1 jtc }
564 1.1 jtc break;
565 1.1 jtc }
566 1.1 jtc
567 1.1 jtc /*
568 1.1 jtc * strip off any trailing slash.
569 1.1 jtc */
570 1.1 jtc if (*pt == '/') {
571 1.18 itohy *pt = '\0';
572 1.1 jtc --arcn->nlen;
573 1.1 jtc }
574 1.63 dsl return 0;
575 1.1 jtc }
576 1.1 jtc
577 1.1 jtc /*
578 1.1 jtc * tar_wr()
579 1.1 jtc * write a tar header for the file specified in the ARCHD to the archive.
580 1.1 jtc * Have to check for file types that cannot be stored and file names that
581 1.70 christos * are too long. Be careful of the term (last arg) to u32_oct, each field
582 1.1 jtc * of tar has it own spec for the termination character(s).
583 1.1 jtc * ASSUMED: space after header in header block is zero filled
584 1.1 jtc * Return:
585 1.1 jtc * 0 if file has data to be written after the header, 1 if file has NO
586 1.1 jtc * data to write after the header, -1 if archive write failed
587 1.1 jtc */
588 1.1 jtc
589 1.1 jtc int
590 1.6 tls tar_wr(ARCHD *arcn)
591 1.1 jtc {
592 1.6 tls HD_TAR *hd;
593 1.1 jtc int len;
594 1.73 christos uintmax_t mtime;
595 1.1 jtc char hdblk[sizeof(HD_TAR)];
596 1.1 jtc
597 1.1 jtc /*
598 1.1 jtc * check for those file system types which tar cannot store
599 1.1 jtc */
600 1.1 jtc switch(arcn->type) {
601 1.1 jtc case PAX_DIR:
602 1.1 jtc /*
603 1.1 jtc * user asked that dirs not be written to the archive
604 1.1 jtc */
605 1.1 jtc if (tar_nodir)
606 1.63 dsl return 1;
607 1.1 jtc break;
608 1.1 jtc case PAX_CHR:
609 1.9 christos tty_warn(1, "Tar cannot archive a character device %s",
610 1.1 jtc arcn->org_name);
611 1.63 dsl return 1;
612 1.1 jtc case PAX_BLK:
613 1.9 christos tty_warn(1,
614 1.9 christos "Tar cannot archive a block device %s", arcn->org_name);
615 1.63 dsl return 1;
616 1.1 jtc case PAX_SCK:
617 1.9 christos tty_warn(1, "Tar cannot archive a socket %s", arcn->org_name);
618 1.63 dsl return 1;
619 1.1 jtc case PAX_FIF:
620 1.9 christos tty_warn(1, "Tar cannot archive a fifo %s", arcn->org_name);
621 1.63 dsl return 1;
622 1.1 jtc case PAX_SLK:
623 1.1 jtc case PAX_HLK:
624 1.1 jtc case PAX_HRG:
625 1.67 lukem if (arcn->ln_nlen > (int)sizeof(hd->linkname)) {
626 1.9 christos tty_warn(1,"Link name too long for tar %s",
627 1.9 christos arcn->ln_name);
628 1.63 dsl return 1;
629 1.1 jtc }
630 1.1 jtc break;
631 1.1 jtc case PAX_REG:
632 1.1 jtc case PAX_CTG:
633 1.1 jtc default:
634 1.1 jtc break;
635 1.1 jtc }
636 1.1 jtc
637 1.1 jtc /*
638 1.1 jtc * check file name len, remember extra char for dirs (the / at the end)
639 1.1 jtc */
640 1.1 jtc len = arcn->nlen;
641 1.1 jtc if (arcn->type == PAX_DIR)
642 1.1 jtc ++len;
643 1.67 lukem if (len >= (int)sizeof(hd->name)) {
644 1.9 christos tty_warn(1, "File name too long for tar %s", arcn->name);
645 1.63 dsl return 1;
646 1.1 jtc }
647 1.1 jtc
648 1.1 jtc /*
649 1.1 jtc * copy the data out of the ARCHD into the tar header based on the type
650 1.1 jtc * of the file. Remember many tar readers want the unused fields to be
651 1.1 jtc * padded with zero. We set the linkflag field (type), the linkname
652 1.1 jtc * (or zero if not used),the size, and set the padding (if any) to be
653 1.1 jtc * added after the file data (0 for all other types, as they only have
654 1.1 jtc * a header)
655 1.1 jtc */
656 1.22 christos memset(hdblk, 0, sizeof(hdblk));
657 1.1 jtc hd = (HD_TAR *)hdblk;
658 1.22 christos strlcpy(hd->name, arcn->name, sizeof(hd->name));
659 1.1 jtc arcn->pad = 0;
660 1.1 jtc
661 1.1 jtc if (arcn->type == PAX_DIR) {
662 1.1 jtc /*
663 1.1 jtc * directories are the same as files, except have a filename
664 1.1 jtc * that ends with a /, we add the slash here. No data follows,
665 1.1 jtc * dirs, so no pad.
666 1.1 jtc */
667 1.1 jtc hd->linkflag = AREGTYPE;
668 1.1 jtc hd->name[len-1] = '/';
669 1.70 christos if (u32_oct((uintmax_t)0L, hd->size, sizeof(hd->size), 1))
670 1.1 jtc goto out;
671 1.1 jtc } else if (arcn->type == PAX_SLK) {
672 1.1 jtc /*
673 1.1 jtc * no data follows this file, so no pad
674 1.1 jtc */
675 1.1 jtc hd->linkflag = SYMTYPE;
676 1.22 christos strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
677 1.70 christos if (u32_oct((uintmax_t)0L, hd->size, sizeof(hd->size), 1))
678 1.1 jtc goto out;
679 1.1 jtc } else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
680 1.1 jtc /*
681 1.1 jtc * no data follows this file, so no pad
682 1.1 jtc */
683 1.1 jtc hd->linkflag = LNKTYPE;
684 1.22 christos strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
685 1.70 christos if (u32_oct((uintmax_t)0L, hd->size, sizeof(hd->size), 1))
686 1.1 jtc goto out;
687 1.1 jtc } else {
688 1.1 jtc /*
689 1.1 jtc * data follows this file, so set the pad
690 1.1 jtc */
691 1.1 jtc hd->linkflag = AREGTYPE;
692 1.19 lukem if (OFFT_OCT(arcn->sb.st_size, hd->size, sizeof(hd->size), 1)) {
693 1.9 christos tty_warn(1,"File is too large for tar %s",
694 1.9 christos arcn->org_name);
695 1.63 dsl return 1;
696 1.1 jtc }
697 1.1 jtc arcn->pad = TAR_PAD(arcn->sb.st_size);
698 1.1 jtc }
699 1.1 jtc
700 1.1 jtc /*
701 1.1 jtc * copy those fields that are independent of the type
702 1.1 jtc */
703 1.73 christos mtime = tst.st_ino ? tst.st_mtime : arcn->sb.st_mtime;
704 1.70 christos if (u32_oct((uintmax_t)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
705 1.70 christos u32_oct((uintmax_t)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
706 1.70 christos u32_oct((uintmax_t)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
707 1.73 christos u32_oct(mtime, hd->mtime, sizeof(hd->mtime), 1))
708 1.1 jtc goto out;
709 1.1 jtc
710 1.1 jtc /*
711 1.1 jtc * calculate and add the checksum, then write the header. A return of
712 1.1 jtc * 0 tells the caller to now write the file data, 1 says no data needs
713 1.1 jtc * to be written
714 1.1 jtc */
715 1.70 christos if (u32_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
716 1.22 christos sizeof(hd->chksum), 3))
717 1.23 christos goto out; /* XXX Something's wrong here
718 1.23 christos * because a zero-byte file can
719 1.23 christos * cause this to be done and
720 1.23 christos * yet the resulting warning
721 1.23 christos * seems incorrect */
722 1.23 christos
723 1.1 jtc if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
724 1.63 dsl return -1;
725 1.1 jtc if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
726 1.63 dsl return -1;
727 1.1 jtc if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
728 1.63 dsl return 0;
729 1.63 dsl return 1;
730 1.1 jtc
731 1.1 jtc out:
732 1.1 jtc /*
733 1.1 jtc * header field is out of range
734 1.1 jtc */
735 1.9 christos tty_warn(1, "Tar header field is too small for %s", arcn->org_name);
736 1.63 dsl return 1;
737 1.1 jtc }
738 1.1 jtc
739 1.1 jtc /*
740 1.1 jtc * Routines for POSIX ustar
741 1.1 jtc */
742 1.1 jtc
743 1.1 jtc /*
744 1.1 jtc * ustar_strd()
745 1.1 jtc * initialization for ustar read
746 1.1 jtc * Return:
747 1.1 jtc * 0 if ok, -1 otherwise
748 1.1 jtc */
749 1.1 jtc
750 1.1 jtc int
751 1.1 jtc ustar_strd(void)
752 1.1 jtc {
753 1.63 dsl return 0;
754 1.1 jtc }
755 1.1 jtc
756 1.1 jtc /*
757 1.1 jtc * ustar_stwr()
758 1.1 jtc * initialization for ustar write
759 1.1 jtc * Return:
760 1.1 jtc * 0 if ok, -1 otherwise
761 1.1 jtc */
762 1.1 jtc
763 1.1 jtc int
764 1.1 jtc ustar_stwr(void)
765 1.1 jtc {
766 1.63 dsl return 0;
767 1.1 jtc }
768 1.1 jtc
769 1.1 jtc /*
770 1.1 jtc * ustar_id()
771 1.1 jtc * determine if a block given to us is a valid ustar header. We have to
772 1.75 gutterid * be on the lookout for those pesky blocks of all zeros
773 1.1 jtc * Return:
774 1.1 jtc * 0 if a ustar header, -1 otherwise
775 1.1 jtc */
776 1.1 jtc
777 1.1 jtc int
778 1.1 jtc ustar_id(char *blk, int size)
779 1.1 jtc {
780 1.6 tls HD_USTAR *hd;
781 1.1 jtc
782 1.36 christos if (size < BLKMULT)
783 1.63 dsl return -1;
784 1.1 jtc hd = (HD_USTAR *)blk;
785 1.1 jtc
786 1.1 jtc /*
787 1.75 gutterid * check for block of zeros first, a simple and fast test then check
788 1.1 jtc * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
789 1.1 jtc * programs are fouled up and create archives missing the \0. Last we
790 1.1 jtc * check the checksum. If ok we have to assume it is a valid header.
791 1.1 jtc */
792 1.36 christos if (hd->name[0] == '\0')
793 1.63 dsl return -1;
794 1.36 christos if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
795 1.63 dsl return -1;
796 1.26 christos /* This is GNU tar */
797 1.31 christos if (strncmp(hd->magic, "ustar ", 8) == 0 && !is_gnutar &&
798 1.31 christos !seen_gnu_warning) {
799 1.31 christos seen_gnu_warning = 1;
800 1.32 christos tty_warn(0,
801 1.65 christos "Trying to read GNU tar archive with GNU extensions and end-of-volume checks off");
802 1.31 christos }
803 1.46 matt return check_sum(hd->chksum, sizeof(hd->chksum), blk, BLKMULT, 0);
804 1.1 jtc }
805 1.1 jtc
806 1.1 jtc /*
807 1.1 jtc * ustar_rd()
808 1.1 jtc * extract the values out of block already determined to be a ustar header.
809 1.1 jtc * store the values in the ARCHD parameter.
810 1.1 jtc * Return:
811 1.1 jtc * 0
812 1.1 jtc */
813 1.1 jtc
814 1.1 jtc int
815 1.6 tls ustar_rd(ARCHD *arcn, char *buf)
816 1.1 jtc {
817 1.6 tls HD_USTAR *hd;
818 1.6 tls char *dest;
819 1.12 mycroft int cnt;
820 1.1 jtc dev_t devmajor;
821 1.1 jtc dev_t devminor;
822 1.1 jtc
823 1.1 jtc /*
824 1.1 jtc * we only get proper sized buffers
825 1.1 jtc */
826 1.1 jtc if (ustar_id(buf, BLKMULT) < 0)
827 1.63 dsl return -1;
828 1.29 christos
829 1.29 christos memset(arcn, 0, sizeof(*arcn));
830 1.1 jtc arcn->org_name = arcn->name;
831 1.29 christos arcn->pat = NULL;
832 1.1 jtc arcn->sb.st_nlink = 1;
833 1.1 jtc hd = (HD_USTAR *)buf;
834 1.1 jtc
835 1.1 jtc /*
836 1.1 jtc * see if the filename is split into two parts. if, so joint the parts.
837 1.1 jtc * we copy the prefix first and add a / between the prefix and name.
838 1.1 jtc */
839 1.1 jtc dest = arcn->name;
840 1.1 jtc if (*(hd->prefix) != '\0') {
841 1.22 christos cnt = strlcpy(arcn->name, hd->prefix, sizeof(arcn->name));
842 1.12 mycroft dest += cnt;
843 1.1 jtc *dest++ = '/';
844 1.22 christos cnt++;
845 1.30 christos } else {
846 1.30 christos cnt = 0;
847 1.1 jtc }
848 1.30 christos
849 1.30 christos if (hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) {
850 1.30 christos arcn->nlen = expandname(dest, sizeof(arcn->name) - cnt,
851 1.58 christos &gnu_name_string, &gnu_name_length, hd->name,
852 1.58 christos sizeof(hd->name)) + cnt;
853 1.51 christos arcn->ln_nlen = expandname(arcn->ln_name,
854 1.58 christos sizeof(arcn->ln_name), &gnu_link_string, &gnu_link_length,
855 1.58 christos hd->linkname, sizeof(hd->linkname));
856 1.26 christos }
857 1.1 jtc
858 1.1 jtc /*
859 1.1 jtc * follow the spec to the letter. we should only have mode bits, strip
860 1.1 jtc * off all other crud we may be passed.
861 1.1 jtc */
862 1.70 christos arcn->sb.st_mode = (mode_t)(asc_u32(hd->mode, sizeof(hd->mode), OCT) &
863 1.1 jtc 0xfff);
864 1.19 lukem arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
865 1.74 christos if (arcn->sb.st_size == -1)
866 1.74 christos return -1;
867 1.70 christos arcn->sb.st_mtime = (time_t)(int32_t)asc_u32(hd->mtime, sizeof(hd->mtime), OCT);
868 1.1 jtc arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
869 1.1 jtc
870 1.1 jtc /*
871 1.1 jtc * If we can find the ascii names for gname and uname in the password
872 1.1 jtc * and group files we will use the uid's and gid they bind. Otherwise
873 1.1 jtc * we use the uid and gid values stored in the header. (This is what
874 1.1 jtc * the posix spec wants).
875 1.1 jtc */
876 1.1 jtc hd->gname[sizeof(hd->gname) - 1] = '\0';
877 1.10 mycroft if (gid_from_group(hd->gname, &(arcn->sb.st_gid)) < 0)
878 1.70 christos arcn->sb.st_gid = (gid_t)asc_u32(hd->gid, sizeof(hd->gid), OCT);
879 1.1 jtc hd->uname[sizeof(hd->uname) - 1] = '\0';
880 1.10 mycroft if (uid_from_user(hd->uname, &(arcn->sb.st_uid)) < 0)
881 1.70 christos arcn->sb.st_uid = (uid_t)asc_u32(hd->uid, sizeof(hd->uid), OCT);
882 1.1 jtc
883 1.1 jtc /*
884 1.1 jtc * set the defaults, these may be changed depending on the file type
885 1.1 jtc */
886 1.1 jtc arcn->pad = 0;
887 1.1 jtc arcn->skip = 0;
888 1.1 jtc arcn->sb.st_rdev = (dev_t)0;
889 1.1 jtc
890 1.1 jtc /*
891 1.1 jtc * set the mode and PAX type according to the typeflag in the header
892 1.1 jtc */
893 1.1 jtc switch(hd->typeflag) {
894 1.1 jtc case FIFOTYPE:
895 1.1 jtc arcn->type = PAX_FIF;
896 1.1 jtc arcn->sb.st_mode |= S_IFIFO;
897 1.1 jtc break;
898 1.1 jtc case DIRTYPE:
899 1.1 jtc arcn->type = PAX_DIR;
900 1.1 jtc arcn->sb.st_mode |= S_IFDIR;
901 1.1 jtc arcn->sb.st_nlink = 2;
902 1.1 jtc
903 1.1 jtc /*
904 1.1 jtc * Some programs that create ustar archives append a '/'
905 1.1 jtc * to the pathname for directories. This clearly violates
906 1.1 jtc * ustar specs, but we will silently strip it off anyway.
907 1.1 jtc */
908 1.1 jtc if (arcn->name[arcn->nlen - 1] == '/')
909 1.1 jtc arcn->name[--arcn->nlen] = '\0';
910 1.1 jtc break;
911 1.1 jtc case BLKTYPE:
912 1.1 jtc case CHRTYPE:
913 1.1 jtc /*
914 1.1 jtc * this type requires the rdev field to be set.
915 1.1 jtc */
916 1.1 jtc if (hd->typeflag == BLKTYPE) {
917 1.1 jtc arcn->type = PAX_BLK;
918 1.1 jtc arcn->sb.st_mode |= S_IFBLK;
919 1.1 jtc } else {
920 1.1 jtc arcn->type = PAX_CHR;
921 1.1 jtc arcn->sb.st_mode |= S_IFCHR;
922 1.1 jtc }
923 1.70 christos devmajor = (dev_t)asc_u32(hd->devmajor,sizeof(hd->devmajor),OCT);
924 1.70 christos devminor = (dev_t)asc_u32(hd->devminor,sizeof(hd->devminor),OCT);
925 1.1 jtc arcn->sb.st_rdev = TODEV(devmajor, devminor);
926 1.1 jtc break;
927 1.1 jtc case SYMTYPE:
928 1.1 jtc case LNKTYPE:
929 1.1 jtc if (hd->typeflag == SYMTYPE) {
930 1.1 jtc arcn->type = PAX_SLK;
931 1.1 jtc arcn->sb.st_mode |= S_IFLNK;
932 1.1 jtc } else {
933 1.1 jtc arcn->type = PAX_HLK;
934 1.1 jtc /*
935 1.1 jtc * so printing looks better
936 1.1 jtc */
937 1.1 jtc arcn->sb.st_mode |= S_IFREG;
938 1.1 jtc arcn->sb.st_nlink = 2;
939 1.1 jtc }
940 1.1 jtc break;
941 1.26 christos case LONGLINKTYPE:
942 1.26 christos case LONGNAMETYPE:
943 1.26 christos if (is_gnutar) {
944 1.26 christos /*
945 1.26 christos * GNU long link/file; we tag these here and let the
946 1.26 christos * pax internals deal with it -- too ugly otherwise.
947 1.26 christos */
948 1.26 christos if (hd->typeflag != LONGLINKTYPE)
949 1.26 christos arcn->type = PAX_GLF;
950 1.54 christos else
951 1.54 christos arcn->type = PAX_GLL;
952 1.26 christos arcn->pad = TAR_PAD(arcn->sb.st_size);
953 1.26 christos arcn->skip = arcn->sb.st_size;
954 1.26 christos } else {
955 1.26 christos tty_warn(1, "GNU Long %s found in posix ustar archive.",
956 1.26 christos hd->typeflag == LONGLINKTYPE ? "Link" : "File");
957 1.26 christos }
958 1.26 christos break;
959 1.71 christos case FILEXTYPE:
960 1.71 christos case GLOBXTYPE:
961 1.71 christos tty_warn(0, "%s extended headers posix ustar archive."
962 1.71 christos " Extracting as plain files. Following files might be"
963 1.71 christos " in the wrong directory or have wrong attributes.",
964 1.71 christos hd->typeflag == FILEXTYPE ? "File" : "Global");
965 1.71 christos /*FALLTHROUGH*/
966 1.1 jtc case CONTTYPE:
967 1.1 jtc case AREGTYPE:
968 1.1 jtc case REGTYPE:
969 1.1 jtc default:
970 1.1 jtc /*
971 1.1 jtc * these types have file data that follows. Set the skip and
972 1.1 jtc * pad fields.
973 1.1 jtc */
974 1.1 jtc arcn->type = PAX_REG;
975 1.1 jtc arcn->pad = TAR_PAD(arcn->sb.st_size);
976 1.1 jtc arcn->skip = arcn->sb.st_size;
977 1.1 jtc arcn->sb.st_mode |= S_IFREG;
978 1.1 jtc break;
979 1.1 jtc }
980 1.63 dsl return 0;
981 1.1 jtc }
982 1.1 jtc
983 1.30 christos static int
984 1.58 christos expandname(char *buf, size_t len, char **gnu_name, size_t *gnu_length,
985 1.58 christos const char *name, size_t nlen)
986 1.29 christos {
987 1.30 christos if (*gnu_name) {
988 1.30 christos len = strlcpy(buf, *gnu_name, len);
989 1.30 christos free(*gnu_name);
990 1.30 christos *gnu_name = NULL;
991 1.58 christos *gnu_length = 0;
992 1.29 christos } else {
993 1.39 christos if (len > ++nlen)
994 1.39 christos len = nlen;
995 1.30 christos len = strlcpy(buf, name, len);
996 1.29 christos }
997 1.30 christos return len;
998 1.29 christos }
999 1.26 christos
1000 1.26 christos static void
1001 1.55 christos longlink(ARCHD *arcn, int type)
1002 1.26 christos {
1003 1.26 christos ARCHD larc;
1004 1.26 christos
1005 1.56 christos (void)memset(&larc, 0, sizeof(larc));
1006 1.26 christos
1007 1.56 christos larc.type = type;
1008 1.56 christos larc.nlen = strlcpy(larc.name, LONG_LINK, sizeof(larc.name));
1009 1.56 christos
1010 1.56 christos switch (type) {
1011 1.56 christos case PAX_GLL:
1012 1.26 christos gnu_hack_string = arcn->ln_name;
1013 1.26 christos gnu_hack_len = arcn->ln_nlen + 1;
1014 1.26 christos break;
1015 1.56 christos case PAX_GLF:
1016 1.26 christos gnu_hack_string = arcn->name;
1017 1.26 christos gnu_hack_len = arcn->nlen + 1;
1018 1.56 christos break;
1019 1.56 christos default:
1020 1.72 christos errx(1, "Invalid type in GNU longlink %d", type);
1021 1.26 christos }
1022 1.56 christos
1023 1.26 christos /*
1024 1.26 christos * We need a longlink now.
1025 1.26 christos */
1026 1.26 christos ustar_wr(&larc);
1027 1.26 christos }
1028 1.26 christos
1029 1.1 jtc /*
1030 1.1 jtc * ustar_wr()
1031 1.1 jtc * write a ustar header for the file specified in the ARCHD to the archive
1032 1.1 jtc * Have to check for file types that cannot be stored and file names that
1033 1.70 christos * are too long. Be careful of the term (last arg) to u32_oct, we only use
1034 1.1 jtc * '\0' for the termination character (this is different than picky tar)
1035 1.1 jtc * ASSUMED: space after header in header block is zero filled
1036 1.1 jtc * Return:
1037 1.1 jtc * 0 if file has data to be written after the header, 1 if file has NO
1038 1.1 jtc * data to write after the header, -1 if archive write failed
1039 1.1 jtc */
1040 1.1 jtc
1041 1.60 dsl static int
1042 1.60 dsl size_err(const char *what, ARCHD *arcn)
1043 1.60 dsl {
1044 1.60 dsl /*
1045 1.60 dsl * header field is out of range
1046 1.60 dsl */
1047 1.60 dsl tty_warn(1, "Ustar %s header field is too small for %s",
1048 1.60 dsl what, arcn->org_name);
1049 1.60 dsl return 1;
1050 1.60 dsl }
1051 1.60 dsl
1052 1.1 jtc int
1053 1.6 tls ustar_wr(ARCHD *arcn)
1054 1.1 jtc {
1055 1.6 tls HD_USTAR *hd;
1056 1.6 tls char *pt;
1057 1.73 christos uintmax_t mtime;
1058 1.1 jtc char hdblk[sizeof(HD_USTAR)];
1059 1.10 mycroft const char *user, *group;
1060 1.1 jtc
1061 1.54 christos switch (arcn->type) {
1062 1.54 christos case PAX_SCK:
1063 1.54 christos /*
1064 1.54 christos * check for those file system types ustar cannot store
1065 1.54 christos */
1066 1.38 christos if (!is_gnutar)
1067 1.38 christos tty_warn(1, "Ustar cannot archive a socket %s",
1068 1.38 christos arcn->org_name);
1069 1.63 dsl return 1;
1070 1.1 jtc
1071 1.54 christos case PAX_SLK:
1072 1.54 christos case PAX_HLK:
1073 1.54 christos case PAX_HRG:
1074 1.54 christos /*
1075 1.54 christos * check the length of the linkname
1076 1.54 christos */
1077 1.67 lukem if (arcn->ln_nlen >= (int)sizeof(hd->linkname)) {
1078 1.54 christos if (is_gnutar) {
1079 1.55 christos longlink(arcn, PAX_GLL);
1080 1.54 christos } else {
1081 1.54 christos tty_warn(1, "Link name too long for ustar %s",
1082 1.54 christos arcn->ln_name);
1083 1.63 dsl return 1;
1084 1.54 christos }
1085 1.26 christos }
1086 1.54 christos break;
1087 1.54 christos default:
1088 1.54 christos break;
1089 1.1 jtc }
1090 1.1 jtc
1091 1.1 jtc /*
1092 1.1 jtc * split the path name into prefix and name fields (if needed). if
1093 1.1 jtc * pt != arcn->name, the name has to be split
1094 1.1 jtc */
1095 1.1 jtc if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
1096 1.26 christos if (is_gnutar) {
1097 1.55 christos longlink(arcn, PAX_GLF);
1098 1.26 christos pt = arcn->name;
1099 1.26 christos } else {
1100 1.26 christos tty_warn(1, "File name too long for ustar %s",
1101 1.26 christos arcn->name);
1102 1.63 dsl return 1;
1103 1.26 christos }
1104 1.1 jtc }
1105 1.22 christos
1106 1.22 christos /*
1107 1.22 christos * zero out the header so we don't have to worry about zero fill below
1108 1.22 christos */
1109 1.22 christos memset(hdblk, 0, sizeof(hdblk));
1110 1.1 jtc hd = (HD_USTAR *)hdblk;
1111 1.1 jtc arcn->pad = 0L;
1112 1.1 jtc
1113 1.1 jtc /*
1114 1.1 jtc * split the name, or zero out the prefix
1115 1.1 jtc */
1116 1.1 jtc if (pt != arcn->name) {
1117 1.1 jtc /*
1118 1.1 jtc * name was split, pt points at the / where the split is to
1119 1.1 jtc * occur, we remove the / and copy the first part to the prefix
1120 1.1 jtc */
1121 1.1 jtc *pt = '\0';
1122 1.22 christos strlcpy(hd->prefix, arcn->name, sizeof(hd->prefix));
1123 1.1 jtc *pt++ = '/';
1124 1.22 christos }
1125 1.1 jtc
1126 1.1 jtc /*
1127 1.1 jtc * copy the name part. this may be the whole path or the part after
1128 1.1 jtc * the prefix
1129 1.1 jtc */
1130 1.22 christos strlcpy(hd->name, pt, sizeof(hd->name));
1131 1.1 jtc
1132 1.18 itohy /*
1133 1.1 jtc * set the fields in the header that are type dependent
1134 1.1 jtc */
1135 1.1 jtc switch(arcn->type) {
1136 1.1 jtc case PAX_DIR:
1137 1.1 jtc hd->typeflag = DIRTYPE;
1138 1.70 christos if (u32_oct((uintmax_t)0L, hd->size, sizeof(hd->size), 3))
1139 1.60 dsl return size_err("DIRTYPE", arcn);
1140 1.1 jtc break;
1141 1.1 jtc case PAX_CHR:
1142 1.1 jtc case PAX_BLK:
1143 1.1 jtc if (arcn->type == PAX_CHR)
1144 1.1 jtc hd->typeflag = CHRTYPE;
1145 1.1 jtc else
1146 1.1 jtc hd->typeflag = BLKTYPE;
1147 1.70 christos if (u32_oct((uintmax_t)MAJOR(arcn->sb.st_rdev), hd->devmajor,
1148 1.1 jtc sizeof(hd->devmajor), 3) ||
1149 1.70 christos u32_oct((uintmax_t)MINOR(arcn->sb.st_rdev), hd->devminor,
1150 1.1 jtc sizeof(hd->devminor), 3) ||
1151 1.70 christos u32_oct((uintmax_t)0L, hd->size, sizeof(hd->size), 3))
1152 1.60 dsl return size_err("DEVTYPE", arcn);
1153 1.1 jtc break;
1154 1.1 jtc case PAX_FIF:
1155 1.1 jtc hd->typeflag = FIFOTYPE;
1156 1.70 christos if (u32_oct((uintmax_t)0L, hd->size, sizeof(hd->size), 3))
1157 1.60 dsl return size_err("FIFOTYPE", arcn);
1158 1.1 jtc break;
1159 1.26 christos case PAX_GLL:
1160 1.1 jtc case PAX_SLK:
1161 1.1 jtc case PAX_HLK:
1162 1.1 jtc case PAX_HRG:
1163 1.1 jtc if (arcn->type == PAX_SLK)
1164 1.1 jtc hd->typeflag = SYMTYPE;
1165 1.26 christos else if (arcn->type == PAX_GLL)
1166 1.26 christos hd->typeflag = LONGLINKTYPE;
1167 1.1 jtc else
1168 1.1 jtc hd->typeflag = LNKTYPE;
1169 1.22 christos strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
1170 1.70 christos if (u32_oct((uintmax_t)gnu_hack_len, hd->size,
1171 1.26 christos sizeof(hd->size), 3))
1172 1.60 dsl return size_err("LINKTYPE", arcn);
1173 1.1 jtc break;
1174 1.26 christos case PAX_GLF:
1175 1.1 jtc case PAX_REG:
1176 1.1 jtc case PAX_CTG:
1177 1.1 jtc default:
1178 1.1 jtc /*
1179 1.1 jtc * file data with this type, set the padding
1180 1.1 jtc */
1181 1.26 christos if (arcn->type == PAX_GLF) {
1182 1.26 christos hd->typeflag = LONGNAMETYPE;
1183 1.26 christos arcn->pad = TAR_PAD(gnu_hack_len);
1184 1.70 christos if (OFFT_OCT((uint32_t)gnu_hack_len, hd->size,
1185 1.26 christos sizeof(hd->size), 3)) {
1186 1.26 christos tty_warn(1,"File is too long for ustar %s",
1187 1.26 christos arcn->org_name);
1188 1.63 dsl return 1;
1189 1.26 christos }
1190 1.26 christos } else {
1191 1.26 christos if (arcn->type == PAX_CTG)
1192 1.26 christos hd->typeflag = CONTTYPE;
1193 1.26 christos else
1194 1.26 christos hd->typeflag = REGTYPE;
1195 1.26 christos arcn->pad = TAR_PAD(arcn->sb.st_size);
1196 1.26 christos if (OFFT_OCT(arcn->sb.st_size, hd->size,
1197 1.26 christos sizeof(hd->size), 3)) {
1198 1.26 christos tty_warn(1,"File is too long for ustar %s",
1199 1.26 christos arcn->org_name);
1200 1.63 dsl return 1;
1201 1.26 christos }
1202 1.1 jtc }
1203 1.1 jtc break;
1204 1.1 jtc }
1205 1.1 jtc
1206 1.22 christos strncpy(hd->magic, TMAGIC, TMAGLEN);
1207 1.26 christos if (is_gnutar)
1208 1.64 christos hd->magic[TMAGLEN - 1] = hd->version[0] = ' ';
1209 1.26 christos else
1210 1.26 christos strncpy(hd->version, TVERSION, TVERSLEN);
1211 1.1 jtc
1212 1.1 jtc /*
1213 1.1 jtc * set the remaining fields. Some versions want all 16 bits of mode
1214 1.1 jtc * we better humor them (they really do not meet spec though)....
1215 1.1 jtc */
1216 1.70 christos if (u32_oct((uintmax_t)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3))
1217 1.60 dsl return size_err("MODE", arcn);
1218 1.70 christos if (u32_oct((uintmax_t)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3))
1219 1.60 dsl return size_err("UID", arcn);
1220 1.70 christos if (u32_oct((uintmax_t)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3))
1221 1.60 dsl return size_err("GID", arcn);
1222 1.73 christos mtime = tst.st_ino ? tst.st_mtime : arcn->sb.st_mtime;
1223 1.73 christos if (u32_oct(mtime, hd->mtime, sizeof(hd->mtime), 3))
1224 1.60 dsl return size_err("MTIME", arcn);
1225 1.42 grant user = user_from_uid(arcn->sb.st_uid, 1);
1226 1.42 grant group = group_from_gid(arcn->sb.st_gid, 1);
1227 1.22 christos strncpy(hd->uname, user ? user : "", sizeof(hd->uname));
1228 1.22 christos strncpy(hd->gname, group ? group : "", sizeof(hd->gname));
1229 1.1 jtc
1230 1.1 jtc /*
1231 1.1 jtc * calculate and store the checksum write the header to the archive
1232 1.1 jtc * return 0 tells the caller to now write the file data, 1 says no data
1233 1.1 jtc * needs to be written
1234 1.1 jtc */
1235 1.70 christos if (u32_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
1236 1.1 jtc sizeof(hd->chksum), 3))
1237 1.60 dsl return size_err("CHKSUM", arcn);
1238 1.1 jtc if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
1239 1.63 dsl return -1;
1240 1.1 jtc if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
1241 1.63 dsl return -1;
1242 1.26 christos if (gnu_hack_string) {
1243 1.26 christos int res = wr_rdbuf(gnu_hack_string, gnu_hack_len);
1244 1.26 christos int pad = gnu_hack_len;
1245 1.26 christos gnu_hack_string = NULL;
1246 1.26 christos gnu_hack_len = 0;
1247 1.26 christos if (res < 0)
1248 1.63 dsl return -1;
1249 1.26 christos if (wr_skip((off_t)(BLKMULT - (pad % BLKMULT))) < 0)
1250 1.63 dsl return -1;
1251 1.26 christos }
1252 1.1 jtc if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
1253 1.63 dsl return 0;
1254 1.63 dsl return 1;
1255 1.1 jtc }
1256 1.1 jtc
1257 1.1 jtc /*
1258 1.1 jtc * name_split()
1259 1.1 jtc * see if the name has to be split for storage in a ustar header. We try
1260 1.1 jtc * to fit the entire name in the name field without splitting if we can.
1261 1.1 jtc * The split point is always at a /
1262 1.1 jtc * Return
1263 1.1 jtc * character pointer to split point (always the / that is to be removed
1264 1.1 jtc * if the split is not needed, the points is set to the start of the file
1265 1.1 jtc * name (it would violate the spec to split there). A NULL is returned if
1266 1.1 jtc * the file name is too long
1267 1.1 jtc */
1268 1.1 jtc
1269 1.1 jtc static char *
1270 1.6 tls name_split(char *name, int len)
1271 1.1 jtc {
1272 1.6 tls char *start;
1273 1.1 jtc
1274 1.1 jtc /*
1275 1.1 jtc * check to see if the file name is small enough to fit in the name
1276 1.1 jtc * field. if so just return a pointer to the name.
1277 1.1 jtc */
1278 1.22 christos if (len < TNMSZ)
1279 1.63 dsl return name;
1280 1.59 christos /*
1281 1.59 christos * GNU tar does not honor the prefix+name mode if the magic
1282 1.59 christos * is not "ustar\0". So in GNU tar compatibility mode, we don't
1283 1.59 christos * split the filename into prefix+name because we are setting
1284 1.59 christos * the magic to "ustar " as GNU tar does. This of course will
1285 1.59 christos * end up creating a LongLink record in cases where it does not
1286 1.59 christos * really need do, but we are behaving like GNU tar after all.
1287 1.59 christos */
1288 1.59 christos if (is_gnutar || len > (TPFSZ + TNMSZ))
1289 1.63 dsl return NULL;
1290 1.1 jtc
1291 1.1 jtc /*
1292 1.1 jtc * we start looking at the biggest sized piece that fits in the name
1293 1.18 itohy * field. We walk forward looking for a slash to split at. The idea is
1294 1.1 jtc * to find the biggest piece to fit in the name field (or the smallest
1295 1.1 jtc * prefix we can find) (the -1 is correct the biggest piece would
1296 1.1 jtc * include the slash between the two parts that gets thrown away)
1297 1.1 jtc */
1298 1.22 christos start = name + len - TNMSZ;
1299 1.1 jtc while ((*start != '\0') && (*start != '/'))
1300 1.1 jtc ++start;
1301 1.1 jtc
1302 1.1 jtc /*
1303 1.1 jtc * if we hit the end of the string, this name cannot be split, so we
1304 1.1 jtc * cannot store this file.
1305 1.1 jtc */
1306 1.1 jtc if (*start == '\0')
1307 1.63 dsl return NULL;
1308 1.1 jtc len = start - name;
1309 1.1 jtc
1310 1.1 jtc /*
1311 1.40 grant * NOTE: /str where the length of str == TNMSZ cannot be stored under
1312 1.1 jtc * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1313 1.1 jtc * the file would then expand on extract to //str. The len == 0 below
1314 1.1 jtc * makes this special case follow the spec to the letter.
1315 1.1 jtc */
1316 1.22 christos if ((len >= TPFSZ) || (len == 0))
1317 1.63 dsl return NULL;
1318 1.1 jtc
1319 1.1 jtc /*
1320 1.1 jtc * ok have a split point, return it to the caller
1321 1.1 jtc */
1322 1.63 dsl return start;
1323 1.13 mrg }
1324 1.13 mrg
1325 1.48 mrg /*
1326 1.48 mrg * convert a glob into a RE, and add it to the list. we convert to
1327 1.48 mrg * four different RE's (because we're using BRE's and can't use |
1328 1.48 mrg * alternation :-() with this padding:
1329 1.48 mrg * .*\/ and $
1330 1.48 mrg * .*\/ and \/.*
1331 1.48 mrg * ^ and $
1332 1.48 mrg * ^ and \/.*
1333 1.48 mrg */
1334 1.33 mrg static int
1335 1.33 mrg tar_gnutar_exclude_one(const char *line, size_t len)
1336 1.33 mrg {
1337 1.49 mrg /* 2 * buffer len + nul */
1338 1.49 mrg char sbuf[MAXPATHLEN * 2 + 1];
1339 1.49 mrg /* + / + // + .*""/\/ + \/.* */
1340 1.49 mrg char rabuf[MAXPATHLEN * 2 + 1 + 1 + 2 + 4 + 4];
1341 1.67 lukem size_t i;
1342 1.67 lukem int j = 0;
1343 1.33 mrg
1344 1.33 mrg if (line[len - 1] == '\n')
1345 1.33 mrg len--;
1346 1.49 mrg for (i = 0; i < len; i++) {
1347 1.33 mrg /*
1348 1.33 mrg * convert glob to regexp, escaping everything
1349 1.33 mrg */
1350 1.33 mrg if (line[i] == '*')
1351 1.33 mrg sbuf[j++] = '.';
1352 1.33 mrg else if (line[i] == '?') {
1353 1.33 mrg sbuf[j++] = '.';
1354 1.33 mrg continue;
1355 1.57 christos } else if (!isalnum((unsigned char)line[i]) &&
1356 1.57 christos !isblank((unsigned char)line[i]))
1357 1.33 mrg sbuf[j++] = '\\';
1358 1.33 mrg sbuf[j++] = line[i];
1359 1.33 mrg }
1360 1.57 christos sbuf[j] = '\0';
1361 1.49 mrg /* don't need the .*\/ ones if we start with /, i guess */
1362 1.49 mrg if (line[0] != '/') {
1363 1.51 christos (void)snprintf(rabuf, sizeof rabuf, "/.*\\/%s$//", sbuf);
1364 1.49 mrg if (rep_add(rabuf) < 0)
1365 1.49 mrg return (-1);
1366 1.51 christos (void)snprintf(rabuf, sizeof rabuf, "/.*\\/%s\\/.*//", sbuf);
1367 1.49 mrg if (rep_add(rabuf) < 0)
1368 1.49 mrg return (-1);
1369 1.49 mrg }
1370 1.33 mrg
1371 1.51 christos (void)snprintf(rabuf, sizeof rabuf, "/^%s$//", sbuf);
1372 1.49 mrg if (rep_add(rabuf) < 0)
1373 1.48 mrg return (-1);
1374 1.51 christos (void)snprintf(rabuf, sizeof rabuf, "/^%s\\/.*//", sbuf);
1375 1.49 mrg if (rep_add(rabuf) < 0)
1376 1.48 mrg return (-1);
1377 1.48 mrg
1378 1.33 mrg return (0);
1379 1.33 mrg }
1380 1.33 mrg
1381 1.13 mrg /*
1382 1.75 gutterid * deal with GNU tar -X/--exclude-from & --exclude switches. basically,
1383 1.33 mrg * we go through each line of the file, building a string from the "glob"
1384 1.33 mrg * lines in the file into RE lines, of the form `/^RE$//', which we pass
1385 1.33 mrg * to rep_add(), which will add a empty replacement (exclusion), for the
1386 1.33 mrg * named files.
1387 1.13 mrg */
1388 1.13 mrg int
1389 1.69 matt tar_gnutar_minus_minus_exclude(const char *path)
1390 1.33 mrg {
1391 1.33 mrg size_t len = strlen(path);
1392 1.33 mrg
1393 1.33 mrg if (len > MAXPATHLEN)
1394 1.33 mrg tty_warn(0, "pathname too long: %s", path);
1395 1.33 mrg
1396 1.33 mrg return (tar_gnutar_exclude_one(path, len));
1397 1.33 mrg }
1398 1.33 mrg
1399 1.33 mrg int
1400 1.69 matt tar_gnutar_X_compat(const char *path)
1401 1.13 mrg {
1402 1.33 mrg char *line;
1403 1.13 mrg FILE *fp;
1404 1.33 mrg int lineno = 0;
1405 1.14 mycroft size_t len;
1406 1.13 mrg
1407 1.68 christos if (path[0] == '-' && path[1] == '\0')
1408 1.68 christos fp = stdin;
1409 1.68 christos else {
1410 1.68 christos fp = fopen(path, "r");
1411 1.68 christos if (fp == NULL) {
1412 1.68 christos tty_warn(1, "cannot open %s: %s", path,
1413 1.68 christos strerror(errno));
1414 1.68 christos return -1;
1415 1.68 christos }
1416 1.13 mrg }
1417 1.13 mrg
1418 1.13 mrg while ((line = fgetln(fp, &len))) {
1419 1.13 mrg lineno++;
1420 1.13 mrg if (len > MAXPATHLEN) {
1421 1.13 mrg tty_warn(0, "pathname too long, line %d of %s",
1422 1.13 mrg lineno, path);
1423 1.13 mrg }
1424 1.33 mrg if (tar_gnutar_exclude_one(line, len))
1425 1.68 christos return -1;
1426 1.13 mrg }
1427 1.68 christos if (fp != stdin)
1428 1.68 christos fclose(fp);
1429 1.68 christos return 0;
1430 1.1 jtc }
1431