xinstall.c revision 1.127 1 1.127 lukem /* $NetBSD: xinstall.c,v 1.127 2023/07/20 16:21:23 lukem Exp $ */
2 1.5 jtc
3 1.1 cgd /*
4 1.5 jtc * Copyright (c) 1987, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.79 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.126 rillig /*-
33 1.126 rillig * Copyright (c) 2015 The NetBSD Foundation, Inc.
34 1.126 rillig * All rights reserved.
35 1.126 rillig *
36 1.126 rillig * This code is derived from software contributed to The NetBSD Foundation
37 1.126 rillig * by Christos Zoulas.
38 1.126 rillig *
39 1.126 rillig * Redistribution and use in source and binary forms, with or without
40 1.126 rillig * modification, are permitted provided that the following conditions
41 1.126 rillig * are met:
42 1.126 rillig * 1. Redistributions of source code must retain the above copyright
43 1.126 rillig * notice, this list of conditions and the following disclaimer.
44 1.126 rillig * 2. Redistributions in binary form must reproduce the above copyright
45 1.126 rillig * notice, this list of conditions and the following disclaimer in the
46 1.126 rillig * documentation and/or other materials provided with the distribution.
47 1.126 rillig *
48 1.126 rillig * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 1.126 rillig * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 1.126 rillig * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 1.126 rillig * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 1.126 rillig * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 1.126 rillig * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 1.126 rillig * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 1.126 rillig * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 1.126 rillig * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 1.126 rillig * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 1.126 rillig * POSSIBILITY OF SUCH DAMAGE.
59 1.126 rillig */
60 1.126 rillig
61 1.125 pgoyette #define __MKTEMP_OK__ /* All uses of mktemp have been checked */
62 1.125 pgoyette
63 1.80 lukem #if HAVE_NBTOOL_CONFIG_H
64 1.80 lukem #include "nbtool_config.h"
65 1.65 tv #else
66 1.65 tv #define HAVE_FUTIMES 1
67 1.127 lukem #define HAVE_POSIX_SPAWN 1
68 1.65 tv #define HAVE_STRUCT_STAT_ST_FLAGS 1
69 1.65 tv #endif
70 1.65 tv
71 1.20 mrg #include <sys/cdefs.h>
72 1.67 tv #if defined(__COPYRIGHT) && !defined(lint)
73 1.103 lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993\
74 1.103 lukem The Regents of the University of California. All rights reserved.");
75 1.1 cgd #endif /* not lint */
76 1.1 cgd
77 1.67 tv #if defined(__RCSID) && !defined(lint)
78 1.5 jtc #if 0
79 1.5 jtc static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
80 1.20 mrg #else
81 1.127 lukem __RCSID("$NetBSD: xinstall.c,v 1.127 2023/07/20 16:21:23 lukem Exp $");
82 1.5 jtc #endif
83 1.1 cgd #endif /* not lint */
84 1.1 cgd
85 1.1 cgd #include <sys/param.h>
86 1.5 jtc #include <sys/mman.h>
87 1.1 cgd #include <sys/stat.h>
88 1.61 simonb #include <sys/wait.h>
89 1.104 christos #include <sys/time.h>
90 1.5 jtc
91 1.5 jtc #include <ctype.h>
92 1.67 tv #include <err.h>
93 1.5 jtc #include <errno.h>
94 1.5 jtc #include <fcntl.h>
95 1.1 cgd #include <grp.h>
96 1.67 tv #include <libgen.h>
97 1.5 jtc #include <paths.h>
98 1.1 cgd #include <pwd.h>
99 1.1 cgd #include <stdio.h>
100 1.5 jtc #include <stdlib.h>
101 1.5 jtc #include <string.h>
102 1.5 jtc #include <unistd.h>
103 1.101 he #include <util.h>
104 1.50 lukem #include <vis.h>
105 1.5 jtc
106 1.118 martin #ifdef HAVE_POSIX_SPAWN
107 1.118 martin #include <spawn.h>
108 1.118 martin #endif
109 1.118 martin
110 1.84 lukem #include <md5.h>
111 1.100 christos #include <rmd160.h>
112 1.84 lukem #include <sha1.h>
113 1.109 apb #include <sha2.h>
114 1.84 lukem
115 1.1 cgd #include "pathnames.h"
116 1.74 lukem #include "mtree.h"
117 1.1 cgd
118 1.31 hubertf #define BACKUP_SUFFIX ".old"
119 1.28 wsanchez
120 1.115 joerg static int dobackup, dodir, dostrip, dolink, dopreserve, dorename, dounpriv;
121 1.115 joerg static int haveopt_f, haveopt_g, haveopt_m, haveopt_o;
122 1.115 joerg static int numberedbackup;
123 1.115 joerg static int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
124 1.115 joerg static char pathbuf[MAXPATHLEN];
125 1.115 joerg static uid_t uid = -1;
126 1.115 joerg static gid_t gid = -1;
127 1.115 joerg static char *group, *owner, *fflags, *tags;
128 1.115 joerg static FILE *metafp;
129 1.115 joerg static char *metafile;
130 1.115 joerg static u_long fileflags;
131 1.115 joerg static char *stripArgs;
132 1.115 joerg static char *afterinstallcmd;
133 1.115 joerg static const char *suffix = BACKUP_SUFFIX;
134 1.115 joerg static char *destdir;
135 1.5 jtc
136 1.84 lukem enum {
137 1.84 lukem DIGEST_NONE = 0,
138 1.84 lukem DIGEST_MD5,
139 1.84 lukem DIGEST_RMD160,
140 1.84 lukem DIGEST_SHA1,
141 1.109 apb DIGEST_SHA256,
142 1.109 apb DIGEST_SHA384,
143 1.109 apb DIGEST_SHA512,
144 1.84 lukem } digesttype = DIGEST_NONE;
145 1.115 joerg
146 1.115 joerg static char *digest;
147 1.84 lukem
148 1.16 christos #define LN_ABSOLUTE 0x01
149 1.16 christos #define LN_RELATIVE 0x02
150 1.16 christos #define LN_HARD 0x04
151 1.16 christos #define LN_SYMBOLIC 0x08
152 1.16 christos #define LN_MIXED 0x10
153 1.16 christos
154 1.5 jtc #define DIRECTORY 0x01 /* Tell install it's a directory. */
155 1.5 jtc #define SETFLAGS 0x02 /* Tell install to set flags. */
156 1.50 lukem #define HASUID 0x04 /* Tell install the uid was given */
157 1.50 lukem #define HASGID 0x08 /* Tell install the gid was given */
158 1.5 jtc
159 1.115 joerg static void afterinstall(const char *, const char *, int);
160 1.115 joerg static void backup(const char *);
161 1.115 joerg static char *copy(int, char *, int, char *, off_t);
162 1.115 joerg static int do_link(char *, char *);
163 1.115 joerg static void do_symlink(char *, char *);
164 1.115 joerg static void install(char *, char *, u_int);
165 1.115 joerg static void install_dir(char *, u_int);
166 1.115 joerg static void makelink(char *, char *);
167 1.115 joerg static void metadata_log(const char *, const char *, struct timeval *,
168 1.106 apb const char *, const char *, off_t);
169 1.115 joerg static int parseid(char *, id_t *);
170 1.119 christos static void run(const char *, const char *, const char *, int);
171 1.119 christos static void strip(const char *);
172 1.115 joerg __dead static void usage(void);
173 1.115 joerg static char *xbasename(char *);
174 1.115 joerg static char *xdirname(char *);
175 1.126 rillig static int needshell(const char *, int);
176 1.1 cgd
177 1.5 jtc int
178 1.48 simonb main(int argc, char *argv[])
179 1.1 cgd {
180 1.53 lukem struct stat from_sb, to_sb;
181 1.53 lukem void *set;
182 1.53 lukem u_int iflags;
183 1.53 lukem int ch, no_target;
184 1.53 lukem char *p, *to_name;
185 1.43 cgd
186 1.43 cgd setprogname(argv[0]);
187 1.1 cgd
188 1.5 jtc iflags = 0;
189 1.84 lukem while ((ch = getopt(argc, argv, "a:cbB:dD:f:g:h:l:m:M:N:o:prsS:T:U"))
190 1.75 lukem != -1)
191 1.1 cgd switch((char)ch) {
192 1.69 lukem case 'a':
193 1.69 lukem afterinstallcmd = strdup(optarg);
194 1.69 lukem if (afterinstallcmd == NULL)
195 1.119 christos err(EXIT_FAILURE,
196 1.119 christos "Can't allocate after command");
197 1.69 lukem break;
198 1.31 hubertf case 'B':
199 1.31 hubertf suffix = optarg;
200 1.35 hubertf numberedbackup = 0;
201 1.35 hubertf {
202 1.35 hubertf /* Check if given suffix really generates
203 1.35 hubertf different suffixes - catch e.g. ".%" */
204 1.35 hubertf char suffix_expanded0[FILENAME_MAX],
205 1.35 hubertf suffix_expanded1[FILENAME_MAX];
206 1.35 hubertf (void)snprintf(suffix_expanded0, FILENAME_MAX,
207 1.35 hubertf suffix, 0);
208 1.35 hubertf (void)snprintf(suffix_expanded1, FILENAME_MAX,
209 1.35 hubertf suffix, 1);
210 1.35 hubertf if (strcmp(suffix_expanded0, suffix_expanded1)
211 1.35 hubertf != 0)
212 1.35 hubertf numberedbackup = 1;
213 1.35 hubertf }
214 1.31 hubertf /* fall through; -B implies -b */
215 1.61 simonb /*FALLTHROUGH*/
216 1.31 hubertf case 'b':
217 1.31 hubertf dobackup = 1;
218 1.31 hubertf break;
219 1.1 cgd case 'c':
220 1.83 simonb /* ignored; was "docopy" which is now the default. */
221 1.1 cgd break;
222 1.26 christos case 'd':
223 1.26 christos dodir = 1;
224 1.26 christos break;
225 1.75 lukem case 'D':
226 1.75 lukem destdir = optarg;
227 1.75 lukem break;
228 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
229 1.5 jtc case 'f':
230 1.106 apb haveopt_f = 1;
231 1.52 tv fflags = optarg;
232 1.5 jtc break;
233 1.65 tv #endif
234 1.1 cgd case 'g':
235 1.106 apb haveopt_g = 1;
236 1.1 cgd group = optarg;
237 1.1 cgd break;
238 1.84 lukem case 'h':
239 1.84 lukem digest = optarg;
240 1.84 lukem break;
241 1.16 christos case 'l':
242 1.16 christos for (p = optarg; *p; p++)
243 1.16 christos switch (*p) {
244 1.16 christos case 's':
245 1.16 christos dolink &= ~(LN_HARD|LN_MIXED);
246 1.16 christos dolink |= LN_SYMBOLIC;
247 1.16 christos break;
248 1.16 christos case 'h':
249 1.16 christos dolink &= ~(LN_SYMBOLIC|LN_MIXED);
250 1.16 christos dolink |= LN_HARD;
251 1.16 christos break;
252 1.16 christos case 'm':
253 1.16 christos dolink &= ~(LN_SYMBOLIC|LN_HARD);
254 1.16 christos dolink |= LN_MIXED;
255 1.16 christos break;
256 1.16 christos case 'a':
257 1.16 christos dolink &= ~LN_RELATIVE;
258 1.16 christos dolink |= LN_ABSOLUTE;
259 1.16 christos break;
260 1.16 christos case 'r':
261 1.16 christos dolink &= ~LN_ABSOLUTE;
262 1.16 christos dolink |= LN_RELATIVE;
263 1.16 christos break;
264 1.16 christos default:
265 1.119 christos errx(EXIT_FAILURE, "%c: invalid link type", *p);
266 1.61 simonb /* NOTREACHED */
267 1.16 christos }
268 1.16 christos break;
269 1.26 christos case 'm':
270 1.106 apb haveopt_m = 1;
271 1.26 christos if (!(set = setmode(optarg)))
272 1.119 christos err(EXIT_FAILURE, "Cannot set file mode `%s'", optarg);
273 1.26 christos mode = getmode(set, 0);
274 1.42 enami free(set);
275 1.26 christos break;
276 1.50 lukem case 'M':
277 1.50 lukem metafile = optarg;
278 1.50 lukem break;
279 1.74 lukem case 'N':
280 1.74 lukem if (! setup_getid(optarg))
281 1.119 christos errx(EXIT_FAILURE,
282 1.74 lukem "Unable to use user and group databases in `%s'",
283 1.74 lukem optarg);
284 1.74 lukem break;
285 1.26 christos case 'o':
286 1.106 apb haveopt_o = 1;
287 1.26 christos owner = optarg;
288 1.26 christos break;
289 1.26 christos case 'p':
290 1.26 christos dopreserve = 1;
291 1.26 christos break;
292 1.33 christos case 'r':
293 1.33 christos dorename = 1;
294 1.33 christos break;
295 1.28 wsanchez case 'S':
296 1.49 simonb stripArgs = strdup(optarg);
297 1.49 simonb if (stripArgs == NULL)
298 1.119 christos err(EXIT_FAILURE, "Can't allocate options");
299 1.28 wsanchez /* fall through; -S implies -s */
300 1.61 simonb /*FALLTHROUGH*/
301 1.26 christos case 's':
302 1.26 christos dostrip = 1;
303 1.26 christos break;
304 1.54 lukem case 'T':
305 1.54 lukem tags = optarg;
306 1.54 lukem break;
307 1.38 sommerfe case 'U':
308 1.50 lukem dounpriv = 1;
309 1.38 sommerfe break;
310 1.1 cgd case '?':
311 1.1 cgd default:
312 1.1 cgd usage();
313 1.1 cgd }
314 1.1 cgd argc -= optind;
315 1.1 cgd argv += optind;
316 1.2 jtc
317 1.23 tv /* strip and link options make no sense when creating directories */
318 1.23 tv if ((dostrip || dolink) && dodir)
319 1.16 christos usage();
320 1.16 christos
321 1.16 christos /* strip and flags make no sense with links */
322 1.52 tv if ((dostrip || fflags) && dolink)
323 1.2 jtc usage();
324 1.2 jtc
325 1.2 jtc /* must have at least two arguments, except when creating directories */
326 1.2 jtc if (argc < 2 && !dodir)
327 1.1 cgd usage();
328 1.1 cgd
329 1.84 lukem if (digest) {
330 1.84 lukem if (0) {
331 1.84 lukem } else if (strcmp(digest, "none") == 0) {
332 1.84 lukem digesttype = DIGEST_NONE;
333 1.84 lukem } else if (strcmp(digest, "md5") == 0) {
334 1.84 lukem digesttype = DIGEST_MD5;
335 1.84 lukem } else if (strcmp(digest, "rmd160") == 0) {
336 1.84 lukem digesttype = DIGEST_RMD160;
337 1.84 lukem } else if (strcmp(digest, "sha1") == 0) {
338 1.84 lukem digesttype = DIGEST_SHA1;
339 1.109 apb } else if (strcmp(digest, "sha256") == 0) {
340 1.109 apb digesttype = DIGEST_SHA256;
341 1.109 apb } else if (strcmp(digest, "sha384") == 0) {
342 1.109 apb digesttype = DIGEST_SHA384;
343 1.109 apb } else if (strcmp(digest, "sha512") == 0) {
344 1.109 apb digesttype = DIGEST_SHA512;
345 1.84 lukem } else {
346 1.84 lukem warnx("unknown digest `%s'", digest);
347 1.84 lukem usage();
348 1.84 lukem }
349 1.84 lukem }
350 1.84 lukem
351 1.1 cgd /* get group and owner id's */
352 1.52 tv if (group && !dounpriv) {
353 1.114 tron if (gid_from_group(group, &gid) == -1) {
354 1.114 tron id_t id;
355 1.114 tron if (!parseid(group, &id))
356 1.119 christos errx(EXIT_FAILURE, "unknown group %s", group);
357 1.114 tron gid = id;
358 1.114 tron }
359 1.50 lukem iflags |= HASGID;
360 1.50 lukem }
361 1.52 tv if (owner && !dounpriv) {
362 1.114 tron if (uid_from_user(owner, &uid) == -1) {
363 1.114 tron id_t id;
364 1.114 tron if (!parseid(owner, &id))
365 1.119 christos errx(EXIT_FAILURE, "unknown user %s", owner);
366 1.114 tron uid = id;
367 1.114 tron }
368 1.50 lukem iflags |= HASUID;
369 1.50 lukem }
370 1.50 lukem
371 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
372 1.52 tv if (fflags && !dounpriv) {
373 1.52 tv if (string_to_flags(&fflags, &fileflags, NULL))
374 1.119 christos errx(EXIT_FAILURE, "%s: invalid flag", fflags);
375 1.98 daniel /* restore fflags since string_to_flags() changed it */
376 1.98 daniel fflags = flags_to_string(fileflags, "-");
377 1.52 tv iflags |= SETFLAGS;
378 1.52 tv }
379 1.65 tv #endif
380 1.52 tv
381 1.50 lukem if (metafile) {
382 1.50 lukem if ((metafp = fopen(metafile, "a")) == NULL)
383 1.50 lukem warn("open %s", metafile);
384 1.84 lukem } else
385 1.84 lukem digesttype = DIGEST_NONE;
386 1.1 cgd
387 1.2 jtc if (dodir) {
388 1.2 jtc for (; *argv != NULL; ++argv)
389 1.50 lukem install_dir(*argv, iflags);
390 1.2 jtc exit (0);
391 1.5 jtc }
392 1.2 jtc
393 1.1 cgd no_target = stat(to_name = argv[argc - 1], &to_sb);
394 1.2 jtc if (!no_target && S_ISDIR(to_sb.st_mode)) {
395 1.1 cgd for (; *argv != to_name; ++argv)
396 1.50 lukem install(*argv, to_name, iflags | DIRECTORY);
397 1.1 cgd exit(0);
398 1.1 cgd }
399 1.1 cgd
400 1.1 cgd /* can't do file1 file2 directory/file */
401 1.99 rillig if (argc != 2) {
402 1.99 rillig errx(EXIT_FAILURE, "the last argument (%s) "
403 1.99 rillig "must name an existing directory", argv[argc - 1]);
404 1.99 rillig /* NOTREACHED */
405 1.99 rillig }
406 1.1 cgd
407 1.1 cgd if (!no_target) {
408 1.61 simonb /* makelink() handles checks for links */
409 1.61 simonb if (!dolink) {
410 1.61 simonb if (stat(*argv, &from_sb))
411 1.119 christos err(EXIT_FAILURE, "%s: stat", *argv);
412 1.61 simonb if (!S_ISREG(to_sb.st_mode))
413 1.119 christos errx(EXIT_FAILURE, "%s: not a regular file", to_name);
414 1.61 simonb if (to_sb.st_dev == from_sb.st_dev &&
415 1.61 simonb to_sb.st_ino == from_sb.st_ino)
416 1.119 christos errx(EXIT_FAILURE, "%s and %s are the same file", *argv,
417 1.61 simonb to_name);
418 1.61 simonb }
419 1.5 jtc /*
420 1.5 jtc * Unlink now... avoid ETXTBSY errors later. Try and turn
421 1.5 jtc * off the append/immutable bits -- if we fail, go ahead,
422 1.5 jtc * it might work.
423 1.5 jtc */
424 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
425 1.5 jtc #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
426 1.5 jtc if (to_sb.st_flags & NOCHANGEBITS)
427 1.5 jtc (void)chflags(to_name,
428 1.5 jtc to_sb.st_flags & ~(NOCHANGEBITS));
429 1.65 tv #endif
430 1.32 hubertf if (dobackup)
431 1.32 hubertf backup(to_name);
432 1.34 christos else if (!dorename)
433 1.31 hubertf (void)unlink(to_name);
434 1.1 cgd }
435 1.50 lukem install(*argv, to_name, iflags);
436 1.1 cgd exit(0);
437 1.1 cgd }
438 1.1 cgd
439 1.1 cgd /*
440 1.50 lukem * parseid --
441 1.50 lukem * parse uid or gid from arg into id, returning non-zero if successful
442 1.50 lukem */
443 1.115 joerg static int
444 1.50 lukem parseid(char *name, id_t *id)
445 1.50 lukem {
446 1.50 lukem char *ep;
447 1.50 lukem
448 1.50 lukem errno = 0;
449 1.50 lukem *id = (id_t)strtoul(name, &ep, 10);
450 1.50 lukem if (errno || *ep != '\0')
451 1.50 lukem return (0);
452 1.50 lukem return (1);
453 1.50 lukem }
454 1.50 lukem
455 1.50 lukem /*
456 1.61 simonb * do_link --
457 1.61 simonb * make a hard link, obeying dorename if set
458 1.61 simonb * return -1 on failure
459 1.61 simonb */
460 1.115 joerg static int
461 1.61 simonb do_link(char *from_name, char *to_name)
462 1.61 simonb {
463 1.61 simonb char tmpl[MAXPATHLEN];
464 1.61 simonb int ret;
465 1.61 simonb
466 1.61 simonb if (dorename) {
467 1.112 gson (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
468 1.91 christos /* This usage is safe. */
469 1.61 simonb if (mktemp(tmpl) == NULL)
470 1.119 christos err(EXIT_FAILURE, "%s: mktemp", tmpl);
471 1.61 simonb ret = link(from_name, tmpl);
472 1.61 simonb if (ret == 0) {
473 1.61 simonb ret = rename(tmpl, to_name);
474 1.96 dbj /* If rename has posix semantics, then the temporary
475 1.96 dbj * file may still exist when from_name and to_name point
476 1.102 joerg * to the same file, so unlink it unconditionally.
477 1.96 dbj */
478 1.96 dbj (void)unlink(tmpl);
479 1.61 simonb }
480 1.61 simonb return (ret);
481 1.61 simonb } else
482 1.61 simonb return (link(from_name, to_name));
483 1.61 simonb }
484 1.61 simonb
485 1.61 simonb /*
486 1.61 simonb * do_symlink --
487 1.61 simonb * make a symbolic link, obeying dorename if set
488 1.61 simonb * exit on failure
489 1.61 simonb */
490 1.115 joerg static void
491 1.61 simonb do_symlink(char *from_name, char *to_name)
492 1.61 simonb {
493 1.61 simonb char tmpl[MAXPATHLEN];
494 1.61 simonb
495 1.61 simonb if (dorename) {
496 1.112 gson (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
497 1.91 christos /* This usage is safe. */
498 1.61 simonb if (mktemp(tmpl) == NULL)
499 1.119 christos err(EXIT_FAILURE, "%s: mktemp", tmpl);
500 1.61 simonb
501 1.61 simonb if (symlink(from_name, tmpl) == -1)
502 1.119 christos err(EXIT_FAILURE, "symlink %s -> %s", from_name, tmpl);
503 1.61 simonb if (rename(tmpl, to_name) == -1) {
504 1.61 simonb /* remove temporary link before exiting */
505 1.61 simonb (void)unlink(tmpl);
506 1.119 christos err(EXIT_FAILURE, "%s: rename", to_name);
507 1.61 simonb }
508 1.61 simonb } else {
509 1.61 simonb if (symlink(from_name, to_name) == -1)
510 1.119 christos err(EXIT_FAILURE, "symlink %s -> %s", from_name, to_name);
511 1.61 simonb }
512 1.61 simonb }
513 1.61 simonb
514 1.61 simonb /*
515 1.16 christos * makelink --
516 1.16 christos * make a link from source to destination
517 1.16 christos */
518 1.115 joerg static void
519 1.48 simonb makelink(char *from_name, char *to_name)
520 1.16 christos {
521 1.53 lukem char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
522 1.68 lukem struct stat to_sb;
523 1.16 christos
524 1.16 christos /* Try hard links first */
525 1.16 christos if (dolink & (LN_HARD|LN_MIXED)) {
526 1.61 simonb if (do_link(from_name, to_name) == -1) {
527 1.16 christos if ((dolink & LN_HARD) || errno != EXDEV)
528 1.119 christos err(EXIT_FAILURE, "link %s -> %s", from_name, to_name);
529 1.68 lukem } else {
530 1.68 lukem if (stat(to_name, &to_sb))
531 1.119 christos err(EXIT_FAILURE, "%s: stat", to_name);
532 1.69 lukem if (S_ISREG(to_sb.st_mode)) {
533 1.106 apb /* XXX: hard links to anything
534 1.106 apb * other than plain files are not
535 1.106 apb * metalogged
536 1.106 apb */
537 1.69 lukem int omode;
538 1.69 lukem char *oowner, *ogroup, *offlags;
539 1.85 lukem char *dres;
540 1.69 lukem
541 1.106 apb /* XXX: use underlying perms,
542 1.106 apb * unless overridden on command line.
543 1.106 apb */
544 1.69 lukem omode = mode;
545 1.106 apb if (!haveopt_m)
546 1.106 apb mode = (to_sb.st_mode & 0777);
547 1.69 lukem oowner = owner;
548 1.106 apb if (!haveopt_o)
549 1.106 apb owner = NULL;
550 1.69 lukem ogroup = group;
551 1.106 apb if (!haveopt_g)
552 1.106 apb group = NULL;
553 1.69 lukem offlags = fflags;
554 1.106 apb if (!haveopt_f)
555 1.106 apb fflags = NULL;
556 1.85 lukem switch (digesttype) {
557 1.85 lukem case DIGEST_MD5:
558 1.85 lukem dres = MD5File(from_name, NULL);
559 1.85 lukem break;
560 1.85 lukem case DIGEST_RMD160:
561 1.85 lukem dres = RMD160File(from_name, NULL);
562 1.85 lukem break;
563 1.85 lukem case DIGEST_SHA1:
564 1.85 lukem dres = SHA1File(from_name, NULL);
565 1.85 lukem break;
566 1.109 apb case DIGEST_SHA256:
567 1.109 apb dres = SHA256_File(from_name, NULL);
568 1.109 apb break;
569 1.109 apb case DIGEST_SHA384:
570 1.109 apb dres = SHA384_File(from_name, NULL);
571 1.109 apb break;
572 1.109 apb case DIGEST_SHA512:
573 1.109 apb dres = SHA512_File(from_name, NULL);
574 1.109 apb break;
575 1.85 lukem default:
576 1.85 lukem dres = NULL;
577 1.85 lukem }
578 1.106 apb metadata_log(to_name, "file", NULL, NULL,
579 1.106 apb dres, to_sb.st_size);
580 1.85 lukem free(dres);
581 1.69 lukem mode = omode;
582 1.69 lukem owner = oowner;
583 1.69 lukem group = ogroup;
584 1.69 lukem fflags = offlags;
585 1.69 lukem }
586 1.16 christos return;
587 1.57 lukem }
588 1.16 christos }
589 1.16 christos
590 1.16 christos /* Symbolic links */
591 1.16 christos if (dolink & LN_ABSOLUTE) {
592 1.16 christos /* Convert source path to absolute */
593 1.16 christos if (realpath(from_name, src) == NULL)
594 1.119 christos err(EXIT_FAILURE, "%s: realpath", from_name);
595 1.61 simonb do_symlink(src, to_name);
596 1.90 lukem /* XXX: src may point outside of destdir */
597 1.106 apb metadata_log(to_name, "link", NULL, src, NULL, 0);
598 1.16 christos return;
599 1.16 christos }
600 1.16 christos
601 1.16 christos if (dolink & LN_RELATIVE) {
602 1.61 simonb char *cp, *d, *s;
603 1.16 christos
604 1.16 christos /* Resolve pathnames */
605 1.16 christos if (realpath(from_name, src) == NULL)
606 1.119 christos err(EXIT_FAILURE, "%s: realpath", from_name);
607 1.61 simonb
608 1.61 simonb /*
609 1.61 simonb * The last component of to_name may be a symlink,
610 1.61 simonb * so use realpath to resolve only the directory.
611 1.61 simonb */
612 1.63 perry cp = xdirname(to_name);
613 1.61 simonb if (realpath(cp, dst) == NULL)
614 1.119 christos err(EXIT_FAILURE, "%s: realpath", cp);
615 1.61 simonb /* .. and add the last component */
616 1.61 simonb if (strcmp(dst, "/") != 0) {
617 1.61 simonb if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
618 1.119 christos errx(EXIT_FAILURE, "resolved pathname too long");
619 1.61 simonb }
620 1.61 simonb cp = xbasename(to_name);
621 1.61 simonb if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
622 1.119 christos errx(EXIT_FAILURE, "resolved pathname too long");
623 1.16 christos
624 1.16 christos /* trim common path components */
625 1.16 christos for (s = src, d = dst; *s == *d; s++, d++)
626 1.16 christos continue;
627 1.16 christos while (*s != '/')
628 1.16 christos s--, d--;
629 1.16 christos
630 1.16 christos /* count the number of directories we need to backtrack */
631 1.16 christos for (++d, lnk[0] = '\0'; *d; d++)
632 1.16 christos if (*d == '/')
633 1.78 itojun (void)strlcat(lnk, "../", sizeof(lnk));
634 1.16 christos
635 1.78 itojun (void)strlcat(lnk, ++s, sizeof(lnk));
636 1.16 christos
637 1.90 lukem do_symlink(lnk, to_name);
638 1.90 lukem /* XXX: lnk may point outside of destdir */
639 1.106 apb metadata_log(to_name, "link", NULL, lnk, NULL, 0);
640 1.16 christos return;
641 1.16 christos }
642 1.16 christos
643 1.16 christos /*
644 1.16 christos * If absolute or relative was not specified,
645 1.16 christos * try the names the user provided
646 1.16 christos */
647 1.61 simonb do_symlink(from_name, to_name);
648 1.90 lukem /* XXX: from_name may point outside of destdir */
649 1.106 apb metadata_log(to_name, "link", NULL, from_name, NULL, 0);
650 1.16 christos }
651 1.16 christos
652 1.16 christos /*
653 1.1 cgd * install --
654 1.1 cgd * build a path name and install the file
655 1.1 cgd */
656 1.115 joerg static void
657 1.50 lukem install(char *from_name, char *to_name, u_int flags)
658 1.1 cgd {
659 1.65 tv struct stat from_sb;
660 1.65 tv struct stat to_sb;
661 1.53 lukem struct timeval tv[2];
662 1.108 apb off_t size;
663 1.60 dillo int devnull, from_fd, to_fd, serrno, tmpmode;
664 1.84 lukem char *p, tmpl[MAXPATHLEN], *oto_name, *digestresult;
665 1.5 jtc
666 1.108 apb size = -1;
667 1.71 lukem if (!dolink) {
668 1.71 lukem /* ensure that from_sb & tv are sane if !dolink */
669 1.71 lukem if (stat(from_name, &from_sb))
670 1.119 christos err(EXIT_FAILURE, "%s: stat", from_name);
671 1.108 apb size = from_sb.st_size;
672 1.87 jmc #if BSD4_4 && !HAVE_NBTOOL_CONFIG_H
673 1.71 lukem TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
674 1.71 lukem TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
675 1.70 lukem #else
676 1.71 lukem tv[0].tv_sec = from_sb.st_atime;
677 1.71 lukem tv[0].tv_usec = 0;
678 1.71 lukem tv[1].tv_sec = from_sb.st_mtime;
679 1.71 lukem tv[1].tv_usec = 0;
680 1.70 lukem #endif
681 1.71 lukem }
682 1.70 lukem
683 1.108 apb if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL) != 0) {
684 1.108 apb devnull = 0;
685 1.56 perry if (!dolink) {
686 1.56 perry if (!S_ISREG(from_sb.st_mode))
687 1.119 christos errx(EXIT_FAILURE, "%s: not a regular file", from_name);
688 1.56 perry }
689 1.5 jtc /* Build the target path. */
690 1.5 jtc if (flags & DIRECTORY) {
691 1.5 jtc (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
692 1.5 jtc to_name,
693 1.21 lukem (p = strrchr(from_name, '/')) ? ++p : from_name);
694 1.1 cgd to_name = pathbuf;
695 1.1 cgd }
696 1.5 jtc } else {
697 1.108 apb devnull = 1;
698 1.108 apb size = 0;
699 1.65 tv #if HAVE_STRUCT_STAT_ST_FLAGS
700 1.5 jtc from_sb.st_flags = 0; /* XXX */
701 1.65 tv #endif
702 1.5 jtc }
703 1.1 cgd
704 1.5 jtc /*
705 1.5 jtc * Unlink now... avoid ETXTBSY errors later. Try and turn
706 1.5 jtc * off the append/immutable bits -- if we fail, go ahead,
707 1.5 jtc * it might work.
708 1.5 jtc */
709 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
710 1.5 jtc if (stat(to_name, &to_sb) == 0 &&
711 1.5 jtc to_sb.st_flags & (NOCHANGEBITS))
712 1.5 jtc (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
713 1.65 tv #endif
714 1.33 christos if (dorename) {
715 1.112 gson (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
716 1.33 christos oto_name = to_name;
717 1.33 christos to_name = tmpl;
718 1.33 christos } else {
719 1.37 christos oto_name = NULL; /* pacify gcc */
720 1.33 christos if (dobackup)
721 1.33 christos backup(to_name);
722 1.33 christos else
723 1.33 christos (void)unlink(to_name);
724 1.33 christos }
725 1.16 christos
726 1.16 christos if (dolink) {
727 1.61 simonb makelink(from_name, dorename ? oto_name : to_name);
728 1.16 christos return;
729 1.16 christos }
730 1.1 cgd
731 1.5 jtc /* Create target. */
732 1.33 christos if (dorename) {
733 1.33 christos if ((to_fd = mkstemp(to_name)) == -1)
734 1.119 christos err(EXIT_FAILURE, "%s: mkstemp", to_name);
735 1.33 christos } else {
736 1.33 christos if ((to_fd = open(to_name,
737 1.33 christos O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
738 1.119 christos err(EXIT_FAILURE, "%s: open", to_name);
739 1.33 christos }
740 1.86 lukem digestresult = NULL;
741 1.1 cgd if (!devnull) {
742 1.1 cgd if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
743 1.1 cgd (void)unlink(to_name);
744 1.119 christos err(EXIT_FAILURE, "%s: open", from_name);
745 1.1 cgd }
746 1.84 lukem digestresult =
747 1.84 lukem copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
748 1.1 cgd (void)close(from_fd);
749 1.1 cgd }
750 1.9 jonathan
751 1.9 jonathan if (dostrip) {
752 1.1 cgd strip(to_name);
753 1.9 jonathan
754 1.9 jonathan /*
755 1.9 jonathan * Re-open our fd on the target, in case we used a strip
756 1.9 jonathan * that does not work in-place -- like gnu binutils strip.
757 1.9 jonathan */
758 1.9 jonathan close(to_fd);
759 1.9 jonathan if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
760 1.119 christos err(EXIT_FAILURE, "stripping %s", to_name);
761 1.108 apb
762 1.108 apb /*
763 1.108 apb * Recalculate size and digestresult after stripping.
764 1.108 apb */
765 1.108 apb if (fstat(to_fd, &to_sb) != 0)
766 1.119 christos err(EXIT_FAILURE, "%s: fstat", to_name);
767 1.108 apb size = to_sb.st_size;
768 1.108 apb digestresult =
769 1.108 apb copy(to_fd, to_name, -1, NULL, size);
770 1.108 apb
771 1.69 lukem }
772 1.69 lukem
773 1.69 lukem if (afterinstallcmd != NULL) {
774 1.69 lukem afterinstall(afterinstallcmd, to_name, 1);
775 1.69 lukem
776 1.69 lukem /*
777 1.69 lukem * Re-open our fd on the target, in case we used an
778 1.69 lukem * after-install command that does not work in-place
779 1.69 lukem */
780 1.69 lukem close(to_fd);
781 1.69 lukem if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
782 1.119 christos err(EXIT_FAILURE, "running after install command on %s", to_name);
783 1.9 jonathan }
784 1.9 jonathan
785 1.41 cgd /*
786 1.41 cgd * Set owner, group, mode for target; do the chown first,
787 1.41 cgd * chown may lose the setuid bits.
788 1.41 cgd */
789 1.50 lukem if (!dounpriv &&
790 1.50 lukem (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
791 1.41 cgd serrno = errno;
792 1.41 cgd (void)unlink(to_name);
793 1.119 christos errc(EXIT_FAILURE, serrno, "%s: chown/chgrp", to_name);
794 1.1 cgd }
795 1.60 dillo tmpmode = mode;
796 1.58 tv if (dounpriv)
797 1.60 dillo tmpmode &= S_IRWXU|S_IRWXG|S_IRWXO;
798 1.60 dillo if (fchmod(to_fd, tmpmode) == -1) {
799 1.5 jtc serrno = errno;
800 1.5 jtc (void)unlink(to_name);
801 1.119 christos errc(EXIT_FAILURE, serrno, "%s: chmod", to_name);
802 1.5 jtc }
803 1.5 jtc
804 1.5 jtc /*
805 1.26 christos * Preserve the date of the source file.
806 1.26 christos */
807 1.26 christos if (dopreserve) {
808 1.65 tv #if HAVE_FUTIMES
809 1.65 tv if (futimes(to_fd, tv) == -1)
810 1.65 tv warn("%s: futimes", to_name);
811 1.65 tv #else
812 1.65 tv if (utimes(to_name, tv) == -1)
813 1.66 tv warn("%s: utimes", to_name);
814 1.65 tv #endif
815 1.5 jtc }
816 1.5 jtc
817 1.1 cgd (void)close(to_fd);
818 1.33 christos
819 1.50 lukem if (dorename) {
820 1.33 christos if (rename(to_name, oto_name) == -1)
821 1.119 christos err(EXIT_FAILURE, "%s: rename", to_name);
822 1.50 lukem to_name = oto_name;
823 1.50 lukem }
824 1.33 christos
825 1.50 lukem /*
826 1.50 lukem * If provided a set of flags, set them, otherwise, preserve the
827 1.50 lukem * flags, except for the dump flag.
828 1.50 lukem */
829 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
830 1.50 lukem if (!dounpriv && chflags(to_name,
831 1.51 lukem flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
832 1.51 lukem {
833 1.50 lukem if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
834 1.50 lukem warn("%s: chflags", to_name);
835 1.50 lukem }
836 1.65 tv #endif
837 1.50 lukem
838 1.108 apb metadata_log(to_name, "file", tv, NULL, digestresult, size);
839 1.84 lukem free(digestresult);
840 1.1 cgd }
841 1.1 cgd
842 1.1 cgd /*
843 1.1 cgd * copy --
844 1.108 apb * copy from one file to another, returning a digest.
845 1.108 apb *
846 1.108 apb * If to_fd < 0, just calculate a digest, don't copy.
847 1.1 cgd */
848 1.115 joerg static char *
849 1.48 simonb copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
850 1.1 cgd {
851 1.53 lukem ssize_t nr, nw;
852 1.53 lukem int serrno;
853 1.95 mrg u_char *p;
854 1.95 mrg u_char buf[MAXBSIZE];
855 1.84 lukem MD5_CTX ctxMD5;
856 1.84 lukem RMD160_CTX ctxRMD160;
857 1.84 lukem SHA1_CTX ctxSHA1;
858 1.109 apb SHA256_CTX ctxSHA256;
859 1.109 apb SHA384_CTX ctxSHA384;
860 1.109 apb SHA512_CTX ctxSHA512;
861 1.84 lukem
862 1.84 lukem switch (digesttype) {
863 1.84 lukem case DIGEST_MD5:
864 1.84 lukem MD5Init(&ctxMD5);
865 1.84 lukem break;
866 1.84 lukem case DIGEST_RMD160:
867 1.84 lukem RMD160Init(&ctxRMD160);
868 1.84 lukem break;
869 1.84 lukem case DIGEST_SHA1:
870 1.84 lukem SHA1Init(&ctxSHA1);
871 1.84 lukem break;
872 1.109 apb case DIGEST_SHA256:
873 1.109 apb SHA256_Init(&ctxSHA256);
874 1.109 apb break;
875 1.109 apb case DIGEST_SHA384:
876 1.109 apb SHA384_Init(&ctxSHA384);
877 1.109 apb break;
878 1.109 apb case DIGEST_SHA512:
879 1.109 apb SHA512_Init(&ctxSHA512);
880 1.109 apb break;
881 1.84 lukem case DIGEST_NONE:
882 1.108 apb if (to_fd < 0)
883 1.108 apb return NULL; /* no need to do anything */
884 1.84 lukem default:
885 1.84 lukem break;
886 1.84 lukem }
887 1.5 jtc /*
888 1.28 wsanchez * There's no reason to do anything other than close the file
889 1.28 wsanchez * now if it's empty, so let's not bother.
890 1.5 jtc */
891 1.28 wsanchez if (size > 0) {
892 1.45 chs
893 1.28 wsanchez /*
894 1.45 chs * Mmap and write if less than 8M (the limit is so we
895 1.45 chs * don't totally trash memory on big files). This is
896 1.45 chs * really a minor hack, but it wins some CPU back.
897 1.28 wsanchez */
898 1.45 chs
899 1.28 wsanchez if (size <= 8 * 1048576) {
900 1.28 wsanchez if ((p = mmap(NULL, (size_t)size, PROT_READ,
901 1.45 chs MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
902 1.45 chs == MAP_FAILED) {
903 1.45 chs goto mmap_failed;
904 1.36 thorpej }
905 1.77 thorpej #if defined(MADV_SEQUENTIAL) && !defined(__APPLE__)
906 1.39 christos if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
907 1.39 christos && errno != EOPNOTSUPP)
908 1.119 christos warn("madvise");
909 1.44 cgd #endif
910 1.38 sommerfe
911 1.108 apb if (to_fd >= 0 && write(to_fd, p, size) != size) {
912 1.36 thorpej serrno = errno;
913 1.36 thorpej (void)unlink(to_name);
914 1.119 christos errc(EXIT_FAILURE, serrno, "%s: write",
915 1.119 christos to_name);
916 1.36 thorpej }
917 1.84 lukem switch (digesttype) {
918 1.84 lukem case DIGEST_MD5:
919 1.84 lukem MD5Update(&ctxMD5, p, size);
920 1.84 lukem break;
921 1.84 lukem case DIGEST_RMD160:
922 1.84 lukem RMD160Update(&ctxRMD160, p, size);
923 1.84 lukem break;
924 1.84 lukem case DIGEST_SHA1:
925 1.84 lukem SHA1Update(&ctxSHA1, p, size);
926 1.84 lukem break;
927 1.109 apb case DIGEST_SHA256:
928 1.109 apb SHA256_Update(&ctxSHA256, p, size);
929 1.109 apb break;
930 1.109 apb case DIGEST_SHA384:
931 1.109 apb SHA384_Update(&ctxSHA384, p, size);
932 1.109 apb break;
933 1.109 apb case DIGEST_SHA512:
934 1.109 apb SHA512_Update(&ctxSHA512, p, size);
935 1.109 apb break;
936 1.84 lukem default:
937 1.84 lukem break;
938 1.84 lukem }
939 1.81 christos (void)munmap(p, size);
940 1.28 wsanchez } else {
941 1.84 lukem mmap_failed:
942 1.36 thorpej while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
943 1.108 apb if (to_fd >= 0 &&
944 1.108 apb (nw = write(to_fd, buf, nr)) != nr) {
945 1.28 wsanchez serrno = errno;
946 1.28 wsanchez (void)unlink(to_name);
947 1.119 christos errc(EXIT_FAILURE,
948 1.119 christos nw > 0 ? EIO : serrno,
949 1.119 christos "%s: write", to_name);
950 1.28 wsanchez }
951 1.84 lukem switch (digesttype) {
952 1.84 lukem case DIGEST_MD5:
953 1.84 lukem MD5Update(&ctxMD5, buf, nr);
954 1.84 lukem break;
955 1.84 lukem case DIGEST_RMD160:
956 1.84 lukem RMD160Update(&ctxRMD160, buf, nr);
957 1.84 lukem break;
958 1.84 lukem case DIGEST_SHA1:
959 1.84 lukem SHA1Update(&ctxSHA1, buf, nr);
960 1.84 lukem break;
961 1.109 apb case DIGEST_SHA256:
962 1.109 apb SHA256_Update(&ctxSHA256, buf, nr);
963 1.109 apb break;
964 1.109 apb case DIGEST_SHA384:
965 1.109 apb SHA384_Update(&ctxSHA384, buf, nr);
966 1.109 apb break;
967 1.109 apb case DIGEST_SHA512:
968 1.109 apb SHA512_Update(&ctxSHA512, buf, nr);
969 1.109 apb break;
970 1.84 lukem default:
971 1.84 lukem break;
972 1.84 lukem }
973 1.36 thorpej }
974 1.28 wsanchez if (nr != 0) {
975 1.5 jtc serrno = errno;
976 1.5 jtc (void)unlink(to_name);
977 1.119 christos errc(EXIT_FAILURE, serrno, "%s: read",
978 1.119 christos from_name);
979 1.5 jtc }
980 1.1 cgd }
981 1.1 cgd }
982 1.84 lukem switch (digesttype) {
983 1.84 lukem case DIGEST_MD5:
984 1.84 lukem return MD5End(&ctxMD5, NULL);
985 1.84 lukem case DIGEST_RMD160:
986 1.84 lukem return RMD160End(&ctxRMD160, NULL);
987 1.84 lukem case DIGEST_SHA1:
988 1.84 lukem return SHA1End(&ctxSHA1, NULL);
989 1.109 apb case DIGEST_SHA256:
990 1.109 apb return SHA256_End(&ctxSHA256, NULL);
991 1.109 apb case DIGEST_SHA384:
992 1.109 apb return SHA384_End(&ctxSHA384, NULL);
993 1.109 apb case DIGEST_SHA512:
994 1.109 apb return SHA512_End(&ctxSHA512, NULL);
995 1.84 lukem default:
996 1.84 lukem return NULL;
997 1.84 lukem }
998 1.1 cgd }
999 1.1 cgd
1000 1.115 joerg static void
1001 1.119 christos run(const char *command, const char *flags, const char *to_name, int errunlink)
1002 1.1 cgd {
1003 1.119 christos char *args[4];
1004 1.120 christos char *cmd;
1005 1.118 martin int status;
1006 1.118 martin int rv;
1007 1.119 christos size_t i;
1008 1.102 joerg
1009 1.119 christos i = 1;
1010 1.119 christos status = 0;
1011 1.102 joerg
1012 1.122 christos if (needshell(command, 1)) {
1013 1.120 christos rv = asprintf(&cmd, "%s %s%s%s", command, flags ? flags : "",
1014 1.120 christos flags ? " " : "", to_name);
1015 1.120 christos if (rv < 0) {
1016 1.120 christos warn("Cannot execute %s", command);
1017 1.120 christos goto out;
1018 1.120 christos }
1019 1.120 christos command = _PATH_BSHELL;
1020 1.120 christos flags = "-c";
1021 1.120 christos } else
1022 1.120 christos cmd = __UNCONST(to_name);
1023 1.120 christos
1024 1.119 christos args[0] = __UNCONST(command);
1025 1.119 christos if (flags)
1026 1.119 christos args[i++] = __UNCONST(flags);
1027 1.120 christos args[i++] = cmd;
1028 1.119 christos args[i] = NULL;
1029 1.1 cgd
1030 1.118 martin #ifdef HAVE_POSIX_SPAWN
1031 1.119 christos if (*command == '/')
1032 1.119 christos rv = posix_spawn(NULL, command, NULL, NULL, args, NULL);
1033 1.119 christos else
1034 1.119 christos rv = posix_spawnp(NULL, command, NULL, NULL, args, NULL);
1035 1.123 christos if (rv != 0)
1036 1.119 christos warnc(rv, "Cannot execute %s", command);
1037 1.123 christos /*
1038 1.123 christos * the wait below will fail if we did not create a child it will
1039 1.123 christos * make rv negative.
1040 1.123 christos */
1041 1.118 martin #else
1042 1.1 cgd switch (vfork()) {
1043 1.1 cgd case -1:
1044 1.119 christos rv = errno;
1045 1.119 christos if (errunlink)
1046 1.119 christos (void)unlink(to_name);
1047 1.119 christos errc(EXIT_FAILURE, rv, "vfork");
1048 1.61 simonb /*NOTREACHED*/
1049 1.1 cgd case 0:
1050 1.119 christos if (*command == '/')
1051 1.119 christos execv(command, args);
1052 1.102 joerg else
1053 1.119 christos execvp(command, args);
1054 1.119 christos rv = errno;
1055 1.119 christos const char *arr[] = {
1056 1.119 christos getprogname(),
1057 1.119 christos ": exec failed for ",
1058 1.119 christos command,
1059 1.119 christos " (",
1060 1.119 christos strerror(rv),
1061 1.119 christos ")\n",
1062 1.119 christos };
1063 1.119 christos for (i = 0; i < __arraycount(arr); i++)
1064 1.119 christos write(STDERR_FILENO, arr[i], strlen(arr[i]));
1065 1.22 thorpej _exit(1);
1066 1.61 simonb /*NOTREACHED*/
1067 1.1 cgd default:
1068 1.119 christos break;
1069 1.1 cgd }
1070 1.118 martin #endif
1071 1.120 christos rv = wait(&status);
1072 1.120 christos if (cmd != to_name)
1073 1.120 christos free(cmd);
1074 1.120 christos out:
1075 1.120 christos if ((rv < 0 || status) && errunlink)
1076 1.119 christos (void)unlink(to_name);
1077 1.119 christos }
1078 1.102 joerg
1079 1.119 christos /*
1080 1.119 christos * strip --
1081 1.119 christos * use strip(1) to strip the target file
1082 1.119 christos */
1083 1.119 christos static void
1084 1.119 christos strip(const char *to_name)
1085 1.119 christos {
1086 1.119 christos const char *stripprog;
1087 1.119 christos
1088 1.119 christos if ((stripprog = getenv("STRIP")) == NULL || *stripprog == '\0') {
1089 1.119 christos #ifdef TARGET_STRIP
1090 1.119 christos stripprog = TARGET_STRIP;
1091 1.119 christos #else
1092 1.119 christos stripprog = _PATH_STRIP;
1093 1.119 christos #endif
1094 1.119 christos }
1095 1.121 christos run(stripprog, stripArgs, to_name, 1);
1096 1.32 hubertf }
1097 1.32 hubertf
1098 1.32 hubertf /*
1099 1.69 lukem * afterinstall --
1100 1.69 lukem * run provided command on the target file or directory after it's been
1101 1.69 lukem * installed and stripped, but before permissions are set or it's renamed
1102 1.69 lukem */
1103 1.115 joerg static void
1104 1.69 lukem afterinstall(const char *command, const char *to_name, int errunlink)
1105 1.69 lukem {
1106 1.119 christos run(command, NULL, to_name, errunlink);
1107 1.69 lukem }
1108 1.69 lukem
1109 1.69 lukem /*
1110 1.61 simonb * backup --
1111 1.61 simonb * backup file "to_name" to to_name.suffix
1112 1.61 simonb * if suffix contains a "%", it's taken as a printf(3) pattern
1113 1.61 simonb * used for a numbered backup.
1114 1.32 hubertf */
1115 1.115 joerg static void
1116 1.48 simonb backup(const char *to_name)
1117 1.32 hubertf {
1118 1.61 simonb char bname[FILENAME_MAX];
1119 1.32 hubertf
1120 1.35 hubertf if (numberedbackup) {
1121 1.32 hubertf /* Do numbered backup */
1122 1.32 hubertf int cnt;
1123 1.32 hubertf char suffix_expanded[FILENAME_MAX];
1124 1.32 hubertf
1125 1.32 hubertf cnt=0;
1126 1.32 hubertf do {
1127 1.50 lukem (void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
1128 1.50 lukem cnt);
1129 1.61 simonb (void)snprintf(bname, FILENAME_MAX, "%s%s", to_name,
1130 1.61 simonb suffix_expanded);
1131 1.32 hubertf cnt++;
1132 1.61 simonb } while (access(bname, F_OK) == 0);
1133 1.32 hubertf } else {
1134 1.32 hubertf /* Do simple backup */
1135 1.61 simonb (void)snprintf(bname, FILENAME_MAX, "%s%s", to_name, suffix);
1136 1.32 hubertf }
1137 1.32 hubertf
1138 1.61 simonb (void)rename(to_name, bname);
1139 1.1 cgd }
1140 1.1 cgd
1141 1.1 cgd /*
1142 1.5 jtc * install_dir --
1143 1.47 wiz * build directory hierarchy
1144 1.2 jtc */
1145 1.115 joerg static void
1146 1.50 lukem install_dir(char *path, u_int flags)
1147 1.2 jtc {
1148 1.111 yamt char *p;
1149 1.111 yamt struct stat sb;
1150 1.111 yamt int ch;
1151 1.111 yamt
1152 1.111 yamt for (p = path;; ++p)
1153 1.111 yamt if (!*p || (p != path && *p == '/')) {
1154 1.111 yamt ch = *p;
1155 1.111 yamt *p = '\0';
1156 1.111 yamt if (mkdir(path, 0777) < 0) {
1157 1.110 gson /*
1158 1.110 gson * Can't create; path exists or no perms.
1159 1.110 gson * stat() path to determine what's there now.
1160 1.110 gson */
1161 1.110 gson int sverrno;
1162 1.110 gson sverrno = errno;
1163 1.110 gson if (stat(path, &sb) < 0) {
1164 1.110 gson /* Not there; use mkdir()s error */
1165 1.110 gson errno = sverrno;
1166 1.119 christos err(EXIT_FAILURE, "%s: mkdir", path);
1167 1.111 yamt }
1168 1.110 gson if (!S_ISDIR(sb.st_mode)) {
1169 1.119 christos errx(EXIT_FAILURE,
1170 1.111 yamt "%s exists but is not a directory",
1171 1.111 yamt path);
1172 1.110 gson }
1173 1.97 daniel }
1174 1.111 yamt if (!(*p = ch))
1175 1.4 cgd break;
1176 1.111 yamt }
1177 1.2 jtc
1178 1.69 lukem if (afterinstallcmd != NULL)
1179 1.69 lukem afterinstall(afterinstallcmd, path, 0);
1180 1.69 lukem
1181 1.50 lukem if (!dounpriv && (
1182 1.50 lukem ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
1183 1.50 lukem || chmod(path, mode) == -1 )) {
1184 1.111 yamt warn("%s: chown/chmod", path);
1185 1.41 cgd }
1186 1.106 apb metadata_log(path, "dir", NULL, NULL, NULL, 0);
1187 1.2 jtc }
1188 1.2 jtc
1189 1.2 jtc /*
1190 1.50 lukem * metadata_log --
1191 1.50 lukem * if metafp is not NULL, output mtree(8) full path name and settings to
1192 1.106 apb * metafp, to allow permissions to be set correctly by other tools,
1193 1.106 apb * or to allow integrity checks to be performed.
1194 1.50 lukem */
1195 1.115 joerg static void
1196 1.59 perry metadata_log(const char *path, const char *type, struct timeval *tv,
1197 1.107 lukem const char *slink, const char *digestresult, off_t size)
1198 1.50 lukem {
1199 1.72 yamt static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
1200 1.88 lukem const char *p;
1201 1.88 lukem char *buf;
1202 1.75 lukem size_t destlen;
1203 1.73 lukem struct flock metalog_lock;
1204 1.50 lukem
1205 1.50 lukem if (!metafp)
1206 1.50 lukem return;
1207 1.119 christos buf = malloc(4 * strlen(path) + 1); /* buf for strsvis(3) */
1208 1.50 lukem if (buf == NULL) {
1209 1.119 christos warn("Can't allocate metadata");
1210 1.50 lukem return;
1211 1.50 lukem }
1212 1.73 lukem /* lock log file */
1213 1.73 lukem metalog_lock.l_start = 0;
1214 1.73 lukem metalog_lock.l_len = 0;
1215 1.73 lukem metalog_lock.l_whence = SEEK_SET;
1216 1.73 lukem metalog_lock.l_type = F_WRLCK;
1217 1.73 lukem if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
1218 1.50 lukem warn("can't lock %s", metafile);
1219 1.94 christos free(buf);
1220 1.50 lukem return;
1221 1.50 lukem }
1222 1.50 lukem
1223 1.88 lukem p = path; /* remove destdir */
1224 1.75 lukem if (destdir) {
1225 1.75 lukem destlen = strlen(destdir);
1226 1.76 lukem if (strncmp(p, destdir, destlen) == 0 &&
1227 1.76 lukem (p[destlen] == '/' || p[destlen] == '\0'))
1228 1.75 lukem p += destlen;
1229 1.75 lukem }
1230 1.75 lukem while (*p && *p == '/') /* remove leading /s */
1231 1.75 lukem p++;
1232 1.88 lukem strsvis(buf, p, VIS_CSTYLE, extra); /* encode name */
1233 1.88 lukem p = buf;
1234 1.75 lukem /* print details */
1235 1.106 apb fprintf(metafp, ".%s%s type=%s", *p ? "/" : "", p, type);
1236 1.106 apb if (owner)
1237 1.106 apb fprintf(metafp, " uname=%s", owner);
1238 1.106 apb if (group)
1239 1.106 apb fprintf(metafp, " gname=%s", group);
1240 1.106 apb fprintf(metafp, " mode=%#o", mode);
1241 1.107 lukem if (slink) {
1242 1.107 lukem strsvis(buf, slink, VIS_CSTYLE, extra); /* encode link */
1243 1.89 lukem fprintf(metafp, " link=%s", buf);
1244 1.89 lukem }
1245 1.106 apb if (*type == 'f') /* type=file */
1246 1.106 apb fprintf(metafp, " size=%lld", (long long)size);
1247 1.106 apb if (tv != NULL && dopreserve)
1248 1.117 apb fprintf(metafp, " time=%lld.%0*lld",
1249 1.116 apb (long long)tv[1].tv_sec,
1250 1.116 apb (tv[1].tv_usec == 0 ? 1 : 9),
1251 1.117 apb (long long)tv[1].tv_usec * 1000);
1252 1.106 apb if (digestresult && digest)
1253 1.106 apb fprintf(metafp, " %s=%s", digest, digestresult);
1254 1.52 tv if (fflags)
1255 1.52 tv fprintf(metafp, " flags=%s", fflags);
1256 1.54 lukem if (tags)
1257 1.54 lukem fprintf(metafp, " tags=%s", tags);
1258 1.50 lukem fputc('\n', metafp);
1259 1.50 lukem fflush(metafp); /* flush output */
1260 1.73 lukem /* unlock log file */
1261 1.73 lukem metalog_lock.l_type = F_UNLCK;
1262 1.73 lukem if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
1263 1.50 lukem warn("can't unlock %s", metafile);
1264 1.50 lukem }
1265 1.50 lukem free(buf);
1266 1.50 lukem }
1267 1.50 lukem
1268 1.61 simonb /*
1269 1.61 simonb * xbasename --
1270 1.61 simonb * libc basename(3) that returns a pointer to a static buffer
1271 1.61 simonb * instead of overwriting that passed-in string.
1272 1.61 simonb */
1273 1.115 joerg static char *
1274 1.61 simonb xbasename(char *path)
1275 1.61 simonb {
1276 1.61 simonb static char tmp[MAXPATHLEN];
1277 1.61 simonb
1278 1.61 simonb (void)strlcpy(tmp, path, sizeof(tmp));
1279 1.61 simonb return (basename(tmp));
1280 1.61 simonb }
1281 1.61 simonb
1282 1.61 simonb /*
1283 1.61 simonb * xdirname --
1284 1.61 simonb * libc dirname(3) that returns a pointer to a static buffer
1285 1.61 simonb * instead of overwriting that passed-in string.
1286 1.61 simonb */
1287 1.115 joerg static char *
1288 1.61 simonb xdirname(char *path)
1289 1.61 simonb {
1290 1.61 simonb static char tmp[MAXPATHLEN];
1291 1.50 lukem
1292 1.61 simonb (void)strlcpy(tmp, path, sizeof(tmp));
1293 1.61 simonb return (dirname(tmp));
1294 1.61 simonb }
1295 1.50 lukem
1296 1.50 lukem /*
1297 1.1 cgd * usage --
1298 1.1 cgd * print a usage message and die
1299 1.1 cgd */
1300 1.115 joerg static void
1301 1.48 simonb usage(void)
1302 1.1 cgd {
1303 1.69 lukem const char *prog;
1304 1.69 lukem
1305 1.69 lukem prog = getprogname();
1306 1.53 lukem
1307 1.69 lukem (void)fprintf(stderr,
1308 1.75 lukem "usage: %s [-Ubcprs] [-M log] [-D dest] [-T tags] [-B suffix]\n"
1309 1.75 lukem " [-a aftercmd] [-f flags] [-m mode] [-N dbdir] [-o owner] [-g group] \n"
1310 1.84 lukem " [-l linkflags] [-h hash] [-S stripflags] file1 file2\n"
1311 1.75 lukem " %s [-Ubcprs] [-M log] [-D dest] [-T tags] [-B suffix]\n"
1312 1.75 lukem " [-a aftercmd] [-f flags] [-m mode] [-N dbdir] [-o owner] [-g group]\n"
1313 1.84 lukem " [-l linkflags] [-h hash] [-S stripflags] file1 ... fileN directory\n"
1314 1.75 lukem " %s -d [-Up] [-M log] [-D dest] [-T tags] [-a aftercmd] [-m mode]\n"
1315 1.74 lukem " [-N dbdir] [-o owner] [-g group] directory ...\n",
1316 1.69 lukem prog, prog, prog);
1317 1.6 jtc exit(1);
1318 1.1 cgd }
1319 1.126 rillig
1320 1.126 rillig /*
1321 1.126 rillig * The following array is used to make a fast determination of which
1322 1.126 rillig * characters are interpreted specially by the shell. If a command
1323 1.126 rillig * contains any of these characters, it is executed by the shell, not
1324 1.126 rillig * directly by us.
1325 1.126 rillig */
1326 1.126 rillig static unsigned char _metachar[128] = {
1327 1.126 rillig /* nul soh stx etx eot enq ack bel */
1328 1.126 rillig 1, 0, 0, 0, 0, 0, 0, 0,
1329 1.126 rillig /* bs ht nl vt np cr so si */
1330 1.126 rillig 0, 0, 1, 0, 0, 0, 0, 0,
1331 1.126 rillig /* dle dc1 dc2 dc3 dc4 nak syn etb */
1332 1.126 rillig 0, 0, 0, 0, 0, 0, 0, 0,
1333 1.126 rillig /* can em sub esc fs gs rs us */
1334 1.126 rillig 0, 0, 0, 0, 0, 0, 0, 0,
1335 1.126 rillig /* sp ! " # $ % & ' */
1336 1.126 rillig 0, 1, 1, 1, 1, 0, 1, 1,
1337 1.126 rillig /* ( ) * + , - . / */
1338 1.126 rillig 1, 1, 1, 0, 0, 0, 0, 0,
1339 1.126 rillig /* 0 1 2 3 4 5 6 7 */
1340 1.126 rillig 0, 0, 0, 0, 0, 0, 0, 0,
1341 1.126 rillig /* 8 9 : ; < = > ? */
1342 1.126 rillig 0, 0, 0, 1, 1, 0, 1, 1,
1343 1.126 rillig /* @ A B C D E F G */
1344 1.126 rillig 0, 0, 0, 0, 0, 0, 0, 0,
1345 1.126 rillig /* H I J K L M N O */
1346 1.126 rillig 0, 0, 0, 0, 0, 0, 0, 0,
1347 1.126 rillig /* P Q R S T U V W */
1348 1.126 rillig 0, 0, 0, 0, 0, 0, 0, 0,
1349 1.126 rillig /* X Y Z [ \ ] ^ _ */
1350 1.126 rillig 0, 0, 0, 1, 1, 1, 1, 0,
1351 1.126 rillig /* ` a b c d e f g */
1352 1.126 rillig 1, 0, 0, 0, 0, 0, 0, 0,
1353 1.126 rillig /* h i j k l m n o */
1354 1.126 rillig 0, 0, 0, 0, 0, 0, 0, 0,
1355 1.126 rillig /* p q r s t u v w */
1356 1.126 rillig 0, 0, 0, 0, 0, 0, 0, 0,
1357 1.126 rillig /* x y z { | } ~ del */
1358 1.126 rillig 0, 0, 0, 1, 1, 1, 1, 0,
1359 1.126 rillig };
1360 1.126 rillig
1361 1.126 rillig #define ismeta(c) _metachar[(c) & 0x7f]
1362 1.126 rillig
1363 1.126 rillig static int
1364 1.126 rillig needshell(const char *cmd, int white)
1365 1.126 rillig {
1366 1.126 rillig while (!ismeta(*cmd) && *cmd != ':' && *cmd != '=') {
1367 1.126 rillig if (white && isspace((unsigned char)*cmd))
1368 1.126 rillig break;
1369 1.126 rillig cmd++;
1370 1.126 rillig }
1371 1.126 rillig
1372 1.126 rillig return *cmd != '\0';
1373 1.126 rillig }
1374