summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svr4pkg/installf/main.c
blob: 53d7c6b48e643df5e23548e14016b849a523e6f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */


#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <pkginfo.h>
#include <pkgstrct.h>
#include <pkglocs.h>
#include <locale.h>
#include <libintl.h>
#include <instzones_api.h>
#include <pkglib.h>
#include <install.h>
#include <libadm.h>
#include <libinst.h>
#include "installf.h"

#define	BASEDIR	"/BASEDIR/"

#define	INSTALF	(*prog == 'i')
#define	REMOVEF	(*prog == 'r')

#define	MSG_MANMOUNT	"Assuming mounts were provided."

#define	ERR_PKGNAME_TOO_LONG	\
"The package name specified on the command line\n" \
"exceeds the maximum package name length: a package name may contain a\n" \
"maximum of <%d> characters; however, the package name specified on\n" \
"the command line contains <%d> characters, which exceeds the maximum\n" \
"package name length by <%d> characters. Please specify a package name\n" \
"that contains no more than <%d> characters."

#define	ERR_DB_GET "unable to retrieve entries from the database."
#define	ERR_DB_PUT "unable to update the package database."
#define	ERR_ROOT_SET	"Could not set install root from the environment."
#define	ERR_ROOT_CMD	"Command line install root contends with environment."
#define	ERR_CLASSLONG	"classname argument too long"
#define	ERR_CLASSCHAR	"bad character in classname"
#define	ERR_INVAL	"package instance <%s> is invalid"
#define	ERR_NOTINST	"package instance <%s> is not installed"
#define	ERR_MERG	"unable to merge contents file"
#define	ERR_SORT	"unable to sort contents file"
#define	ERR_I_FAIL	"installf did not complete successfully"
#define	ERR_R_FAIL	"removef did not complete successfully"
#define	ERR_NOTROOT	"You must be \"root\" for %s to execute properly."
#define	ERR_USAGE0	"usage:\n" \
	"\t%s [[-M|-A] -R host_path] [-V ...] pkginst path " \
	"[path ...]\n" \
	"\t%s [[-M|-A] -R host_path] [-V ...] pkginst path\n"

#define	ERR_USAGE1	"usage:\n" \
	"\t%s [[-M] -R host_path] [-V ...] [-c class] <pkginst> " \
	"<path>\n" \
	"\t%s [[-M] -R host_path] [-V ...] [-c class] <pkginst> " \
	"<path> <specs>\n" \
	"\t   where <specs> may be defined as:\n" \
	"\t\tf <mode> <owner> <group>\n" \
	"\t\tv <mode> <owner> <group>\n" \
	"\t\te <mode> <owner> <group>\n" \
	"\t\td <mode> <owner> <group>\n" \
	"\t\tx <mode> <owner> <group>\n" \
	"\t\tp <mode> <owner> <group>\n" \
	"\t\tc <major> <minor> <mode> <owner> <group>\n" \
	"\t\tb <major> <minor> <mode> <owner> <group>\n" \
	"\t\ts <path>=<srcpath>\n" \
	"\t\tl <path>=<srcpath>\n" \
	"\t%s [[-M] -R host_path] [-V ...] [-c class] -f pkginst\n"

#define	CMD_SORT	"sort +0 -1"

#define	LINK	1

extern char	dbst;			/* libinst/pkgdbmerg.c */

struct cfextra **extlist;
struct pinfo **eptlist;

char	*classname = NULL;
char	*pkginst;
char	*uniTmp;
char	*abi_sym_ptr;
char	*ulim;
char	*script;

int	nosetuid;
int	nocnflct;
int	warnflag = 0;

/* libadm/pkgparam.c */
extern void	set_PKGADM(char *newpath);
extern void	set_PKGLOC(char *newpath);

extern void set_limit(void);

int
main(int argc, char **argv)
{
	VFP_T		*cfTmpVfp;
	PKGserver	pkgserver = NULL;
	char		*tp;
	char		*prog;
	char		*pt;
	char		*vfstab_file = NULL;
	char		*temp_cl_basedir;
	char		outbuf[PATH_MAX];
	int		c;
	int		dbchg;
	int		err;
	int		fflag = 0;
	int		map_client = 1;
	int		n;
	int		pkgrmremote = 0;	/* don't remove remote files */
	struct cfent	*ept;

	/* hookup signals */

	(void) signal(SIGHUP, exit);
	(void) signal(SIGINT, exit);
	(void) signal(SIGQUIT, exit);

	/* initialize locale mechanism */

	(void) setlocale(LC_ALL, "");

#if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"
#endif	/* !defined(TEXT_DOMAIN) */

	(void) textdomain(TEXT_DOMAIN);

	/* determine program name */

	prog = set_prog_name(argv[0]);

	/* tell instzones interface how to access package output functions */

	z_set_output_functions(echo, echoDebug, progerr);

	/* only allow root to run this program */

	if (getuid() != 0) {
		progerr(gettext(ERR_NOTROOT), prog);
		exit(1);
	}

	ulim = getenv("PKG_ULIMIT");
	script = getenv("PKG_PROC_SCRIPT");

	if (ulim && script) {
		set_limit();
		clr_ulimit();
	}

	/* bug id 4244631, not ABI compliant */
	abi_sym_ptr = getenv("PKG_NONABI_SYMLINKS");
	if (abi_sym_ptr && strncasecmp(abi_sym_ptr, "TRUE", 4) == 0)
		set_nonABI_symlinks();

	/* bugId 4012147 */
	if ((uniTmp = getenv("PKG_NO_UNIFIED")) != NULL)
		map_client = 0;
	if (!set_inst_root(getenv("PKG_INSTALL_ROOT"))) {
		progerr(gettext(ERR_ROOT_SET));
		exit(1);
	}

	while ((c = getopt(argc, argv, "c:V:fAMR:?")) != EOF) {
		switch (c) {
			case 'f':
			fflag++;
			break;

			case 'c':
			classname = optarg;
			/* validate that classname is acceptable */
			if (strlen(classname) > (size_t)CLSSIZ) {
				progerr(gettext(ERR_CLASSLONG));
				exit(1);
			}
			for (pt = classname; *pt; pt++) {
				if (!isalpha(*pt) && !isdigit(*pt)) {
					progerr(gettext(ERR_CLASSCHAR));
					exit(1);
				}
			}
			break;

		/*
		 * Don't map the client filesystem onto the server's. Assume
		 * the mounts have been made for us.
		 */
			case 'M':
			map_client = 0;
			break;

		/*
		 * Allow admin to establish the client filesystem using a
		 * vfstab-like file of stable format.
		 */
			case 'V':
			vfstab_file = flex_device(optarg, 2);
			map_client = 1;
			break;

			case 'A':
			pkgrmremote++;
			break;

			case 'R':	/* added for newroot option */
			if (!set_inst_root(optarg)) {
				progerr(gettext(ERR_ROOT_CMD));
				exit(1);
			}
			break;

			default:
			usage();
			/*NOTREACHED*/
			/*
			 * Although usage() calls a noreturn function,
			 * needed to add return (1);  so that main() would
			 * pass compilation checks. The statement below
			 * should never be executed.
			 */
			return (1);
		}
	}

	if (pkgrmremote && (!is_an_inst_root() || fflag || INSTALF)) {
		usage();
		/*NOTREACHED*/
	}

	/*
	 * Get the mount table info and store internally.
	 */
	if (get_mntinfo(map_client, vfstab_file))
		exit(1);

	/*
	 * This function defines the standard /var/... directories used later
	 * to construct the paths to the various databases.
	 */
	(void) set_PKGpaths(get_inst_root());

	/*
	 * If this is being installed on a client whose /var filesystem is
	 * mounted in some odd way, remap the administrative paths to the
	 * real filesystem. This could be avoided by simply mounting up the
	 * client now; but we aren't yet to the point in the process where
	 * modification of the filesystem is permitted.
	 */
	if (is_an_inst_root()) {
		int fsys_value;

		fsys_value = fsys(get_PKGLOC());
		if (use_srvr_map_n(fsys_value))
			set_PKGLOC(server_map(get_PKGLOC(), fsys_value));

		fsys_value = fsys(get_PKGADM());
		if (use_srvr_map_n(fsys_value))
			set_PKGADM(server_map(get_PKGADM(), fsys_value));
	}

	/*
	 * get the package name and verify length is not too long
	 */

	pkginst = argv[optind++];
	if (pkginst == NULL) {
		usage();
		/*NOTREACHED*/

	}

	n = strlen(pkginst);
	if (n > PKGSIZ) {
		progerr(gettext(ERR_PKGNAME_TOO_LONG), PKGSIZ, n, n-PKGSIZ,
		    PKGSIZ);
		usage();
		/*NOTREACHED*/
	}

	/*
	 * The following is used to setup the environment. Note that the
	 * variable 'BASEDIR' is only meaningful for this utility if there
	 * is an install root, recorded in PKG_INSTALL_ROOT. Otherwise, this
	 * utility can create a file or directory anywhere unfettered by
	 * the basedir associated with the package instance.
	 */
	if ((err = set_basedirs(0, NULL, pkginst, 1)) != 0)
		exit(err);

	if (INSTALF)
		mkbasedir(0, get_basedir());

	if (fflag) {
		/* installf and removef must only have pkginst */
		if (optind != argc) {
			usage();
			/*NOTREACHED*/
		}
	} else {
		/*
		 * installf and removef must have at minimum
		 * pkginst & pathname specified on command line
		 */
		if (optind >= argc) {
			usage();
			/*NOTREACHED*/
		}
	}
	if (REMOVEF) {
		if (classname) {
			usage();
		}
	}
	if (pkgnmchk(pkginst, "all", 0)) {
		progerr(gettext(ERR_INVAL), pkginst);
		exit(1);
	}
	if (fpkginst(pkginst, NULL, NULL) == NULL) {
		progerr(gettext(ERR_NOTINST), pkginst);
		exit(1);
	}

	/*
	 * This maps the client filesystems into the server's space.
	 */
	if (map_client && !mount_client())
		logerr(gettext(MSG_MANMOUNT));

	/* open the package database (contents) file */

	if (!ocfile(&pkgserver, &cfTmpVfp, 0L)) {
		quit(1);
	}

	if (fflag) {
		dbchg = dofinal(pkgserver, cfTmpVfp, REMOVEF, classname, prog);
	} else {
		if (INSTALF) {
			dbst = INST_RDY;
			if (installf(argc-optind, &argv[optind]))
				quit(1);
		} else {
			dbst = RM_RDY;
			removef(argc-optind, &argv[optind]);
		}

		dbchg = pkgdbmerg(pkgserver, cfTmpVfp, extlist);
		if (dbchg < 0) {
			progerr(gettext(ERR_MERG));
			quit(99);
		}
	}

	if (dbchg) {
		if ((n = swapcfile(pkgserver, &cfTmpVfp, pkginst, 1))
		    == RESULT_WRN) {
			warnflag++;
		} else if (n == RESULT_ERR) {
			quit(99);
		}
	}

	relslock();

	if (REMOVEF && !fflag) {
		for (n = 0; extlist[n]; n++) {
			ept = &(extlist[n]->cf_ent);

			/* Skip duplicated paths */
			if ((n > 0) && (strncmp(ept->path,
			    extlist[n-1]->cf_ent.path, PATH_MAX) == 0)) {
				continue;
			}

			if (!extlist[n]->mstat.shared) {
				/*
				 * Only output paths that can be deleted.
				 * so need to skip if the object is owned
				 * by a remote server and removal is not
				 * being forced.
				 */
				if (ept->pinfo &&
				    (ept->pinfo->status == SERVED_FILE) &&
				    !pkgrmremote)
					continue;

				c = 0;
				if (is_a_cl_basedir() && !is_an_inst_root()) {
					/*
					 * A path in contents db might have
					 * other prefix than BASEDIR of the
					 * package
					 */
					temp_cl_basedir = get_client_basedir();
					if (strncmp(ept->path, temp_cl_basedir,
					    strlen(temp_cl_basedir)) == 0) {
						c = strlen(temp_cl_basedir);
						(void) snprintf(outbuf,
						    sizeof (outbuf), "%s/%s\n",
						    get_basedir(),
						    &(ept->path[c]));
					} else {
						(void) snprintf(outbuf,
						    sizeof (outbuf),
						    "%s\n", &(ept->path[c]));
					}
				} else if (is_an_inst_root()) {
					(void) snprintf(outbuf, sizeof (outbuf),
					    "%s/%s\n", get_inst_root(),
					    &(ept->path[c]));
				} else {
					(void) snprintf(outbuf, sizeof (outbuf),
					    "%s\n", &(ept->path[c]));
				}
				canonize(outbuf);
				(void) printf("%s", outbuf);
			}
		}
	} else if (INSTALF && !fflag) {
		for (n = 0; extlist[n]; n++) {
			ept = &(extlist[n]->cf_ent);

			if (strchr("dxcbp", ept->ftype)) {
				tp = fixpath(ept->path);
				(void) averify(1, &ept->ftype, tp, &ept->ainfo);
			}
		}
	}

	pkgcloseserver(pkgserver);

	z_destroyMountTable();

	quit(warnflag ? 1 : 0);
	/* LINTED: no return */
}

void
quit(int n)
{
	char *prog = get_prog_name();

	unmount_client();

	if (ulim && script) {
		if (REMOVEF) {
			set_ulimit(script, gettext(ERR_R_FAIL));
		} else {
			set_ulimit(script, gettext(ERR_I_FAIL));
		}
	}

	exit(n);
}

void
usage(void)
{
	char *prog = get_prog_name();

	if (REMOVEF) {
		(void) fprintf(stderr, gettext(ERR_USAGE0), prog, prog);
	} else {
		(void) fprintf(stderr, gettext(ERR_USAGE1), prog, prog, prog);
	}
	exit(1);
}