xref: /titanic_44/usr/src/cmd/svr4pkg/pkgchk/main.c (revision 1babaf948dd28d81d79cf3ec089d6edc111ed4a8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 #include <stdio.h>
32 #include <limits.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <dirent.h>
38 #include <sys/stat.h>
39 #include <pkginfo.h>
40 #include <pkglocs.h>
41 #include <sys/types.h>
42 #include <pkgstrct.h>
43 #include <pkgtrans.h>
44 #include <locale.h>
45 #include <libintl.h>
46 #include <pkglib.h>
47 #include <libadm.h>
48 #include <libinst.h>
49 
50 #define	MAXPATHS	1024
51 
52 #define	MSG_CHK_STRM	"Checking uninstalled stream format package " \
53 				"<%s> from <%s>\n"
54 #define	MSG_CHK_DIR	"Checking uninstalled directory format package " \
55 				"<%s> from <%s>\n"
56 #define	MSG_NOTROOT	"NOTE: \"root\" permission may be required to " \
57 				"validate all objects in the client filesystem."
58 #define	MSG_CONT	"Continuing."
59 
60 #define	WRN_F_SPOOL	"WARNING: %s is spooled. Ignoring \"f\" argument"
61 
62 #define	ERR_ROOT_SET	"Could not set install root from the environment."
63 #define	ERR_ROOT_CMD	"Command line install root contends with environment."
64 #define	ERR_IOPEN	"unable to open input file <%s>"
65 #define	ERR_IEMPTY	"no pathnames in file specified by -i option"
66 #define	ERR_POPTION	"no pathname included with -p option"
67 #define	ERR_PARTIAL_POPTION	"no pathname included with -P option"
68 #define	ERR_MAXPATHS	"too many pathnames in option list (limit is %d)"
69 #define	ERR_NOTROOT	"You must be \"root\" for \"%s -f\" to" \
70 					"execute properly."
71 #define	ERR_SEL_PKG "No packages selected for verification."
72 #define	ERR_CAT_LNGTH "The category argument exceeds the SVr4 ABI\n" \
73 		"        defined maximum supported length of 16 characters."
74 #define	ERR_CAT_FND "Category argument <%s> cannot be found."
75 #define	ERR_CAT_INV "Category argument <%s> is invalid."
76 #define	ERR_TOO_MANY "too many pathnames in list, limit is %d"
77 #define	ERR_PATHS_INVALID "Pathnames in %s are not valid."
78 #define	ERR_MKDIR "unable to make directory <%s>"
79 #define	ERR_USAGE	"usage:\n" \
80 		"\t%s [-l|vqacnxf] [-R rootdir] [-p path[, ...] | " \
81 		"-P path[, ...]]\n" \
82 		"\t\t[-i file] [options]\n" \
83 		"\t%s -d device [-f][-l|v] [-p path[, ...] | " \
84 		"-P path[, ...]]\n" \
85 		"\t\t[-V ...] [-M] [-i file] [-Y category[, ...] | " \
86 		"pkginst [...]]\n" \
87 		"\twhere options may include ONE of the " \
88 		"following:\n " \
89 		"\t\t-m pkgmap [-e envfile]\n" \
90 		"\t\tpkginst [...]\n" \
91 		"\t\t-Y category[, ...]\n"
92 
93 #define	LINK	1
94 
95 char	**pkg = NULL;
96 int	pkgcnt = 0;
97 char	*basedir;
98 char	*pathlist[MAXPATHS], *ppathlist[MAXPATHS], pkgspool[PATH_MAX];
99 short	used[MAXPATHS];
100 short	npaths;
101 struct cfent **eptlist;
102 
103 int	aflag = (-1);
104 int	cflag = (-1);
105 int	vflag = 0;
106 int	nflag = 0;
107 int	lflag = 0;
108 int	Lflag = 0;
109 int	fflag = 0;
110 int	xflag = 0;
111 int	qflag = 0;
112 int	Rflag = 0;
113 int	dflag = 0;
114 char 	*device;
115 
116 char	*uniTmp;
117 
118 static char	*mapfile,
119 		*spooldir,
120 		*tmpdir,
121 		*envfile;
122 static int	errflg = 0;
123 static int	map_client = 1;
124 
125 void	quit(int);
126 static void	setpathlist(char *);
127 static void	usage(void);
128 
129 extern	char	**environ;
130 extern	char	*pkgdir;
131 
132 /* checkmap.c */
133 extern int	checkmap(int, int, char *, char *, char *, char *, int);
134 /* scriptvfy.c */
135 extern int	checkscripts(char *inst_dir, int silent);
136 
137 int
138 main(int argc, char *argv[])
139 {
140 	int	pkgfmt = 0;	/* Makes more sense as a pointer, but */
141 				/*	18N is compromised. */
142 	char	file[PATH_MAX+1],
143 		*abi_sym_ptr,
144 		*vfstab_file = NULL;
145 	char *all_pkgs[4] = {"all", NULL};
146 	char **category = NULL;
147 	char *catg_arg = NULL;
148 	int	c;
149 	int	n = 0;
150 	char	*prog,
151 		*Rvalue,
152 		*dvalue;
153 	int dbcreate = 0;
154 	int pathtype;
155 
156 	/* initialize locale mechanism */
157 
158 	(void) setlocale(LC_ALL, "");
159 
160 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
161 #define	TEXT_DOMAIN "SYS_TEST"
162 #endif
163 	(void) textdomain(TEXT_DOMAIN);
164 
165 	/* determine program name */
166 
167 	prog = set_prog_name(argv[0]);
168 
169 	/* establish installation root directory */
170 
171 	if (!set_inst_root(getenv("PKG_INSTALL_ROOT"))) {
172 		progerr(gettext(ERR_ROOT_SET));
173 		quit(1);
174 	}
175 
176 	/* check if not ABI compliant mode */
177 	abi_sym_ptr = getenv("PKG_NONABI_SYMLINKS");
178 	if (abi_sym_ptr && strncasecmp(abi_sym_ptr, "TRUE", 4) == 0) {
179 		set_nonABI_symlinks();
180 	}
181 
182 	/* bugId 4012147 */
183 	if ((uniTmp = getenv("PKG_NO_UNIFIED")) != NULL)
184 		map_client = 0;
185 
186 	while ((c = getopt(argc, argv, "Y:R:e:p:d:nLli:vaV:Mm:cqxfQP:?"))
187 			!= EOF) {
188 		switch (c) {
189 		case 'p':
190 			pathlist[npaths] = strtok(optarg, " , ");
191 			if (pathlist[npaths++] == NULL) {
192 				progerr(gettext(ERR_POPTION));
193 				quit(1);
194 			}
195 			while (pathlist[npaths] = strtok(NULL, " , ")) {
196 				if (npaths++ >= MAXPATHS) {
197 					progerr(gettext(ERR_MAXPATHS),
198 						MAXPATHS);
199 					quit(1);
200 				}
201 			}
202 			break;
203 
204 		case 'd':
205 			dvalue = optarg;
206 			dflag = 1;
207 			break;
208 
209 		case 'n':
210 			nflag++;
211 			break;
212 
213 		case 'M':
214 			map_client = 0;
215 			break;
216 
217 		/*
218 		 * Allow admin to establish the client filesystem using a
219 		 * vfstab-like file of stable format.
220 		 */
221 		case 'V':
222 			vfstab_file = flex_device(optarg, 2);
223 			map_client = 1;
224 			break;
225 
226 		case 'f':
227 			if (getuid()) {
228 				progerr(gettext(ERR_NOTROOT), prog);
229 				quit(1);
230 			}
231 			fflag++;
232 			break;
233 
234 		case 'i':
235 			setpathlist(optarg);
236 			break;
237 
238 		case 'v':
239 			vflag++;
240 			break;
241 
242 		case 'l':
243 			lflag++;
244 			break;
245 
246 		case 'L':
247 			Lflag++;
248 			break;
249 
250 		case 'x':
251 			if (aflag < 0)
252 				aflag = 0;
253 			if (cflag < 0)
254 				cflag = 0;
255 			xflag++;
256 			break;
257 
258 		case 'q':
259 			qflag++;
260 			break;
261 
262 		case 'a':
263 			if (cflag < 0)
264 				cflag = 0;
265 			aflag = 1;
266 			break;
267 
268 		case 'c':
269 			if (aflag < 0)
270 				aflag = 0;
271 			cflag = 1;
272 			break;
273 
274 		case 'e':
275 			envfile = optarg;
276 			break;
277 
278 		case 'm':
279 			mapfile = optarg;
280 			break;
281 
282 		case 'R':
283 			Rvalue = optarg;
284 			Rflag = 1;
285 			break;
286 
287 		case 'Y':
288 			catg_arg = strdup(optarg);
289 
290 			if ((category = get_categories(catg_arg)) == NULL) {
291 				progerr(gettext(ERR_CAT_INV), catg_arg);
292 				quit(1);
293 			} else if (is_not_valid_length(category)) {
294 				progerr(gettext(ERR_CAT_LNGTH));
295 				quit(1);
296 			}
297 			break;
298 
299 		case 'Q':
300 			dbcreate++;
301 			break;
302 
303 		case 'P':
304 			ppathlist[npaths] = strtok(optarg, " , ");
305 			if ((ppathlist[npaths] == NULL) ||
306 			    (ppathlist[npaths][0] == '-')) {
307 				progerr(gettext(ERR_PARTIAL_POPTION));
308 				quit(1);
309 			}
310 			npaths++;
311 			while (ppathlist[npaths] = strtok(NULL, " , ")) {
312 				if (npaths++ >= MAXPATHS) {
313 					progerr(gettext(ERR_MAXPATHS),
314 						MAXPATHS);
315 					quit(1);
316 				}
317 			}
318 			break;
319 
320 		default:
321 			usage();
322 			/*NOTREACHED*/
323 			/*
324 			 * Although usage() calls a noreturn function,
325 			 * needed to add return (1);  so that main() would
326 			 * pass compilation checks. The statement below
327 			 * should never be executed.
328 			 */
329 			return (1);
330 		}
331 	}
332 
333 	/* Check for incompatible options */
334 	if (dflag && Rflag)
335 		usage();
336 
337 	/* Check for root dir and device dir if set */
338 	if (Rflag) {
339 		if (!set_inst_root(Rvalue)) {
340 			progerr(gettext(ERR_ROOT_CMD));
341 			quit(1);
342 		}
343 	}
344 
345 	if (dflag)
346 		device = flex_device(dvalue, 1);
347 
348 	if (lflag || Lflag) {
349 		/* we're only supposed to list information */
350 		if ((cflag >= 0) || (aflag >= 0) ||
351 		qflag || xflag || fflag || nflag || vflag)
352 			usage();
353 	}
354 
355 	set_PKGpaths(get_inst_root());
356 
357 	if (catg_arg != NULL && device == NULL) {
358 		if (argc - optind) {
359 			usage();
360 		}
361 		pkg = gpkglist(pkgdir, all_pkgs, category);
362 		if (pkg == NULL) {
363 			progerr(gettext(ERR_CAT_FND), catg_arg);
364 			quit(1);
365 		} else {
366 			for (pkgcnt = 0; pkg[pkgcnt] != NULL; pkgcnt++);
367 		}
368 	} else if (catg_arg != NULL && optind < argc) {
369 		usage();
370 	} else {
371 		pkg = &argv[optind];
372 		pkgcnt = (argc - optind);
373 	}
374 
375 	/* read the environment for the pkgserver */
376 	pkgserversetmode(DEFAULTMODE);
377 
378 	environ = NULL;		/* Sever the parent environment. */
379 
380 	if (vcfile() == 0) {
381 		quit(99);
382 	}
383 
384 	errflg = 0;
385 	if (mapfile) {
386 		/* check for incompatible options */
387 		if (device || pkgcnt)
388 			usage();
389 		put_path_params();	/* Restore what's needed. */
390 
391 		/* send pathtype if partial path */
392 		pathtype = (ppathlist[0] != NULL) ? 1 : 0;
393 		if (checkmap(0, (device != NULL), mapfile, envfile, NULL,
394 		    NULL, pathtype))
395 			errflg++;
396 	} else if (device) {
397 		/* check for incompatible options */
398 		if ((cflag >= 0) || (aflag >= 0))
399 			usage();
400 		if (qflag || xflag || nflag || envfile)
401 			usage();
402 		tmpdir = NULL;
403 		if ((spooldir = devattr(device, "pathname")) == NULL)
404 			spooldir = device;
405 		if (isdir(spooldir)) {
406 			tmpdir = spooldir = qstrdup(tmpnam(NULL));
407 			if (fflag) {
408 				logerr(gettext(WRN_F_SPOOL), *pkg);
409 				fflag = 0;
410 			}
411 			if (mkdir(spooldir, 0755)) {
412 				progerr(gettext(ERR_MKDIR), spooldir);
413 				quit(99);
414 			}
415 			if (n = pkgtrans(device, spooldir, pkg, PT_SILENT,
416 				NULL, NULL))
417 				quit(n);
418 			if (catg_arg != NULL)
419 				pkg = gpkglist(spooldir, all_pkgs, category);
420 			else
421 				pkg = gpkglist(spooldir, all_pkgs, NULL);
422 			pkgfmt = 0;
423 		} else {
424 			if (catg_arg != NULL)
425 				pkg = gpkglist(spooldir,
426 					pkgcnt ? pkg : all_pkgs, category);
427 			else
428 				pkg = gpkglist(spooldir,
429 					pkgcnt ? pkg : all_pkgs, NULL);
430 			pkgfmt = 1;
431 		}
432 
433 		/*
434 		 * At this point pkg[] is the list of packages to check. They
435 		 * are in directory format in spooldir.
436 		 */
437 		if (pkg == NULL) {
438 			if (catg_arg != NULL) {
439 				progerr(gettext(ERR_CAT_FND), catg_arg);
440 				quit(1);
441 			} else {
442 				progerr(gettext(ERR_SEL_PKG));
443 				quit(1);
444 			}
445 		}
446 
447 		aflag = 0;
448 
449 		for (n = 0; pkg[n]; n++) {
450 			char locenv[PATH_MAX];
451 
452 			if (pkgfmt)
453 				(void) printf(
454 					gettext(MSG_CHK_DIR), pkg[n], device);
455 			else
456 				(void) printf(
457 					gettext(MSG_CHK_STRM), pkg[n], device);
458 
459 			(void) snprintf(pkgspool, sizeof (pkgspool),
460 				"%s/%s", spooldir, pkg[n]);
461 			(void) snprintf(file, sizeof (file),
462 				"%s/install", pkgspool);
463 			/* Here we check the install scripts. */
464 			(void) printf(
465 				gettext("## Checking control scripts.\n"));
466 			(void) checkscripts(file, 0);
467 			/* Verify consistency with the pkgmap. */
468 			(void) printf(
469 				gettext("## Checking package objects.\n"));
470 			(void) snprintf(file, sizeof (file),
471 				"%s/pkgmap", pkgspool);
472 			(void) snprintf(locenv, sizeof (locenv),
473 				"%s/pkginfo", pkgspool);
474 			envfile = locenv;
475 
476 			/*
477 			 * NOTE : checkmap() frees the environ data and
478 			 * pointer when it's through with them.
479 			 */
480 			if (checkmap(0, (device != NULL), file, envfile,
481 					pkg[n], NULL, 0))
482 				errflg++;
483 			(void) printf(
484 				gettext("## Checking is complete.\n"));
485 		}
486 	} else {
487 		if (envfile)
488 			usage();
489 
490 		put_path_params();	/* Restore what's needed. */
491 
492 		/*
493 		 * If this is a check of a client of some sort, we'll need to
494 		 * mount up the client's filesystems. If the caller isn't
495 		 * root, this may not be possible.
496 		 */
497 		if (is_an_inst_root()) {
498 			if (getuid()) {
499 				logerr(gettext(MSG_NOTROOT));
500 				logerr(gettext(MSG_CONT));
501 			} else {
502 				if (get_mntinfo(map_client, vfstab_file))
503 					map_client = 0;
504 				if (map_client)
505 					mount_client();
506 			}
507 		}
508 
509 		(void) snprintf(file, sizeof (file),
510 			"%s/contents", get_PKGADM());
511 		if (ppathlist[0] != NULL) {
512 			for (n = 0; ppathlist[n]; n++) {
513 				if (checkmap(1, (device != NULL), file, NULL,
514 						NULL, ppathlist[n], 1))
515 					errflg++;
516 			}
517 		} else if (pkg[0] != NULL) {
518 				if (checkmap(1, (device != NULL), file, NULL,
519 					pkg[0], NULL, 0)) {
520 					errflg++;
521 				}
522 		} else {
523 			if (checkmap(1, (device != NULL), file, NULL,
524 					NULL, NULL, 0)) {
525 				errflg++;
526 			}
527 		}
528 
529 		if (map_client) {
530 			unmount_client();
531 		}
532 	}
533 	quit(errflg ? 1 : 0);
534 	/* LINTED: no return */
535 }
536 
537 static void
538 setpathlist(char *file)
539 {
540 	int fd;
541 	struct stat st;
542 	FILE *fplist;
543 	char pathname[PATH_MAX];
544 	/*
545 	 * This trap laid to catch a mismatch between the declaration above and
546 	 * the hard-coded constant in the fscanf below
547 	 */
548 #if PATH_MAX != 1024
549 #error "PATH_MAX changed, so we have a bug to fix"
550 #endif
551 
552 	if (strcmp(file, "-") == 0) {
553 		fplist = stdin;
554 	} else {
555 		if ((fd = open(file, O_RDONLY)) == -1) {
556 			progerr(gettext(ERR_IOPEN), file);
557 			quit(1);
558 		}
559 		if (fstat(fd, &st) == -1) {
560 			progerr(gettext(ERR_IOPEN), file);
561 			quit(1);
562 		}
563 		if (S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
564 			progerr(gettext(ERR_PATHS_INVALID), file);
565 			quit(1);
566 		}
567 		if ((fplist = fdopen(fd, "r")) == NULL) {
568 			progerr(gettext(ERR_IOPEN), file);
569 			quit(1);
570 		}
571 	}
572 	while (fscanf(fplist, "%1024s", pathname) == 1) {
573 		if (*pathname == '\0') {
574 			progerr(gettext(ERR_PATHS_INVALID), file);
575 			quit(1);
576 		}
577 		pathlist[npaths] = qstrdup(pathname);
578 		if (npaths++ > MAXPATHS) {
579 			progerr(gettext(ERR_TOO_MANY), MAXPATHS);
580 			quit(1);
581 		}
582 	}
583 	if (npaths == 0) {
584 		progerr(gettext(ERR_IEMPTY));
585 		quit(1);
586 	}
587 	(void) fclose(fplist);
588 }
589 
590 void
591 quit(int n)
592 {
593 	/* cleanup any temporary directories */
594 	(void) chdir("/");
595 	if (tmpdir != NULL) {
596 		(void) rrmdir(tmpdir);
597 		free(tmpdir);
598 		tmpdir = NULL;
599 	}
600 	(void) pkghead(NULL);
601 	exit(n);
602 	/*NOTREACHED*/
603 }
604 
605 static void
606 usage(void)
607 {
608 	char *prog = get_prog_name();
609 
610 	(void) fprintf(stderr, gettext(ERR_USAGE), prog, prog);
611 	quit(1);
612 	/*NOTREACHED*/
613 }
614