xref: /titanic_51/usr/src/cmd/svr4pkg/libinst/pkgdbmerg.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 <signal.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <assert.h>
38 #include <pkgstrct.h>
39 #include <sys/stat.h>
40 #include <locale.h>
41 #include <libintl.h>
42 #include <pkginfo.h>
43 #include <instzones_api.h>
44 #include <pkglib.h>
45 #include <libinst.h>
46 #include <messages.h>
47 
48 /* merg() return codes */
49 #define	MRG_SAME	0
50 #define	MRG_DIFFERENT	1
51 #define	MRG_REPLACE	2
52 
53 /* typechg() return codes */
54 #define	TYPE_OK		0
55 #define	TYPE_WARNING	1
56 #define	TYPE_IGNORED	2
57 #define	TYPE_REPLACE	3
58 #define	TYPE_FATAL	4
59 
60 /* message pool */
61 #define	ERR_OUTPUT	"unable to update package database"
62 #define	ERR_PINFO	"missing pinfo structure for <%s>"
63 #define	INFO_PROCESS	"   %2ld%% of information processed; continuing ..."
64 
65 #define	WRN_NOTFILE	"WARNING: %s <no longer a regular file>"
66 #define	WRN_NOTSYMLN	"WARNING: %s <no longer a symbolic link>"
67 #define	WRN_NOTLINK	"WARNING: %s <no longer a linked file>"
68 #define	WRN_NOTDIR	"WARNING: %s <no longer a directory>"
69 #define	WRN_NOTCHAR	"WARNING: %s <no longer a character special device>"
70 #define	WRN_NOTBLOCK	"WARNING: %s <no longer a block special device>"
71 #define	WRN_NOTPIPE	"WARNING: %s <no longer a named pipe>"
72 #define	WRN_TOEXCL	"WARNING: cannot convert %s to an exclusive directory."
73 #define	WRN_ODDVERIFY	"WARNING: quick verify disabled for class %s."
74 
75 #define	MSG_TYPIGN	"Object type change ignored."
76 #define	MSG_TYPE_ERR	"Package attempts fatal object type change."
77 
78 extern char	*pkginst;
79 extern int	nosetuid, nocnflct, otherstoo;
80 
81 /* pkgobjmap.c */
82 extern int	cp_cfent(struct cfent *cf_ent, struct cfextra *el_ent);
83 
84 /* setlist.c */
85 extern void	cl_def_dverify(int idx);
86 
87 char dbst = '\0';	/* usually set by installf() or removef() */
88 
89 int files_installed(void);	/* return number of files installed. */
90 
91 static int	errflg = 0;
92 static int	eptnum;
93 static int	installed;	/* # of files, already properly installed. */
94 static struct	pinfo	*pkgpinfo = (struct pinfo *)0;
95 
96 static int	is_setuid(struct cfent *ent);
97 static int	is_setgid(struct cfent *ent);
98 static int	merg(struct cfextra *el_ent, struct cfent *cf_ent);
99 static int	do_like_ent(VFP_T *vfpo, struct cfextra *el_ent,
100 		    struct cfent *cf_ent, int ctrl);
101 static int	do_new_ent(VFP_T *vfpo, struct cfextra *el_ent, int ctrl);
102 static int	typechg(struct cfent *el_ent, struct cfent *cf_ent,
103 		    struct mergstat *mstat);
104 
105 static void	set_change(struct cfextra *el_ent);
106 static void	chgclass(struct cfent *cf_ent, struct pinfo *pinfo);
107 static void	output(VFP_T *vfpo, struct cfent *ent, struct pinfo *pinfo);
108 
109 /*
110  * This scans the extlist (pkgmap) and matches them to the database, copying
111  * out the modified contents to the file at tmpfp. It updates the mergstat
112  * structures and deals with administrative defaults regarding setuid and
113  * conflict.
114  */
115 
116 int
117 pkgdbmerg(PKGserver server, VFP_T *tmpvfp, struct cfextra **extlist)
118 {
119 	static	struct	cfent	cf_ent;	/* scratch area */
120 	struct	cfextra	*el_ent;	/* extlist entry under review */
121 	int	n;
122 	int	changed;
123 	int	assume_ok = 0;
124 
125 	cf_ent.pinfo = (NULL);
126 	errflg = 0;
127 	installed = changed = 0;
128 
129 	vfpRewind(tmpvfp);
130 
131 	for (eptnum = 0; (el_ent = extlist[eptnum]) != NULL; eptnum++) {
132 		/*
133 		 * If there's an entry in the extlist at this position,
134 		 * process that entry.
135 		 */
136 		/* Metafiles don't get merged. */
137 		if ((el_ent->cf_ent.ftype == 'i') ||
138 			(el_ent->cf_ent.ftype == 'n')) {
139 			continue;
140 		}
141 
142 		/*
143 		 * Copy cfextra structure for duplicated paths.
144 		 * This is not just an optimization, it is
145 		 * necessary for correct operation of algorithm.
146 		 */
147 		if ((eptnum > 0) && (strncmp(el_ent->cf_ent.path,
148 		    extlist[eptnum-1]->cf_ent.path, PATH_MAX) == 0)) {
149 			memcpy(extlist[eptnum], extlist[eptnum-1],
150 			    sizeof (struct cfextra));
151 			continue;
152 		}
153 
154 		/*
155 		 * Normally dbst comes to us from installf() or
156 		 * removef() in order to specify their special
157 		 * database status codes. They cannot implement a
158 		 * quick verify (it just doesn't make sense). For
159 		 * that reason, we can test to see if we already have
160 		 * a special database status. If we don't (it's from
161 		 * pkgadd) then we can test to see if this is calling
162 		 * for a quick verify wherein we assume the install
163 		 * will work and fix it if it doesn't. In that case
164 		 * we set our own dbst to be ENTRY_OK.
165 		 */
166 		if (dbst == '\0') {
167 			if (cl_dvfy(el_ent->cf_ent.pkg_class_idx) ==
168 			    QKVERIFY) {
169 				assume_ok = 1;
170 			}
171 		} else {
172 			/*
173 			 * If we DO end up with an installf/quick
174 			 * verify combination, we fix that by simply
175 			 * denying the quick verify for this class.
176 			 * This forces everything to come out alright
177 			 * by forcing the standard assumptions as
178 			 * regards package database for the rest of
179 			 * the load.
180 			 */
181 			if (cl_dvfy(el_ent->cf_ent.pkg_class_idx) ==
182 			    QKVERIFY) {
183 				logerr(gettext(WRN_ODDVERIFY),
184 				    cl_nam(el_ent->cf_ent.pkg_class_idx));
185 				/*
186 				 * Set destination verification to
187 				 * default.
188 				 */
189 				cl_def_dverify(el_ent->cf_ent.pkg_class_idx);
190 			}
191 		}
192 
193 		/*
194 		 * Comply with administrative requirements regarding
195 		 * setuid/setgid processes.
196 		 */
197 		if (is_setuid(&(el_ent->cf_ent))) {
198 			el_ent->mstat.setuid = 1;
199 		}
200 		if (is_setgid(&(el_ent->cf_ent))) {
201 			el_ent->mstat.setgid = 1;
202 		}
203 
204 		/*
205 		 * If setuid/setgid processes are not allowed, reset
206 		 * those bits.
207 		 */
208 		if (nosetuid && (el_ent->mstat.setgid ||
209 		    el_ent->mstat.setuid)) {
210 			el_ent->cf_ent.ainfo.mode &= ~(S_ISUID | S_ISGID);
211 		}
212 
213 		/* Search package database for this entry. */
214 		n = srchcfile(&cf_ent, el_ent->cf_ent.path, server);
215 
216 		/*
217 		 * If there was an error, note it and return an error
218 		 * flag.
219 		 */
220 		if (n < 0) {
221 			char	*errstr = getErrstr();
222 			progerr(ERR_CFBAD);
223 			logerr(gettext("pathname: %s"),
224 			    (cf_ent.path && *cf_ent.path) ?
225 			    cf_ent.path : "Unknown");
226 			logerr(gettext("problem: %s"),
227 			    (errstr && *errstr) ? errstr : "Unknown");
228 			return (-1);
229 		/*
230 		 * If there was a match, then merge them into a
231 		 * single entry.
232 		 */
233 		} else if (n == 1) {
234 			/*
235 			 * If this package is overwriting a setuid or
236 			 * setgid process, set the status bits so we
237 			 * can inform the administrator.
238 			 */
239 			if (is_setuid(&cf_ent)) {
240 				el_ent->mstat.osetuid = 1;
241 			}
242 
243 			if (is_setgid(&cf_ent)) {
244 				el_ent->mstat.osetgid = 1;
245 			}
246 			/*
247 			 * Detect if a symlink has changed to directory
248 			 * If so mark all the files/dir supposed to be
249 			 * iniside this dir, so that they are not miss
250 			 * understood by do_new_ent later as already
251 			 * installed.
252 			 */
253 			if ((cf_ent.ftype == 's') &&
254 			    (el_ent->cf_ent.ftype == 'd')) {
255 				int i;
256 				int plen = strlen(el_ent->cf_ent.path);
257 				for (i = eptnum + 1; extlist[i]; i++) {
258 					if (strncmp(el_ent->cf_ent.path,
259 					    extlist[i]->cf_ent.path,
260 					    plen) != 0)
261 						break;
262 					extlist[i]->mstat.parentsyml2dir
263 					    = 1;
264 				}
265 			}
266 
267 			if (do_like_ent(tmpvfp, el_ent, &cf_ent, assume_ok)) {
268 				changed++;
269 			}
270 
271 		} else {
272 			/*
273 			 * The file doesn't exist in the database.
274 			 */
275 			if (do_new_ent(tmpvfp, el_ent, assume_ok)) {
276 				changed++;
277 			}
278 		}
279 	}
280 
281 	return (errflg ? -1 : changed);
282 }
283 
284 /*
285  * Merge a new entry with an installed package object of the same name and
286  * insert that object into the package database. Obey administrative defaults
287  * as regards conflicting files.
288  */
289 
290 static int
291 do_like_ent(VFP_T *vfpo, struct cfextra *el_ent, struct cfent *cf_ent, int ctrl)
292 {
293 	int	stflag, ignore, changed, mrg_result;
294 
295 	ignore = changed = 0;
296 
297 	/*
298 	 * Construct the record defining the current package. If there are
299 	 * other packages involved, this will be appended to the existing
300 	 * list. If this is an update of the same package, it will get merged
301 	 * with the existing record. If this is a preloaded record (like from
302 	 * a dryrun file), it will keep it's current pinfo pointer and will
303 	 * pass it on to the record from the contents file - because on the
304 	 * final continuation, the contents file will be wrong.
305 	 */
306 	if (el_ent->mstat.preloaded) {
307 		struct pinfo *pkginfo;
308 
309 		/* Contents file is not to be trusted for this list. */
310 		pkginfo = cf_ent->pinfo;
311 
312 		/* Free the potentially bogus list. */
313 		while (pkginfo) {
314 			struct pinfo *next;
315 			next = pkginfo->next;
316 			free(pkginfo);
317 			pkginfo = next;
318 		}
319 
320 		cf_ent->pinfo = el_ent->cf_ent.pinfo;
321 	}
322 
323 	pkgpinfo = eptstat(cf_ent, pkginst, DUP_ENTRY);
324 
325 	stflag = pkgpinfo->status;
326 
327 	if (otherstoo)
328 		el_ent->mstat.shared = 1;
329 
330 	/* If it's marked for erasure, make it official */
331 	if (el_ent->cf_ent.ftype == RM_RDY) {
332 		if (!errflg) {
333 			pkgpinfo = eptstat(cf_ent, pkginst, RM_RDY);
334 
335 			/*
336 			 * Get copy of status character in case the object is
337 			 * "shared" by a server, in which case we need to
338 			 * maintain the shared status after the entry is
339 			 * written to the package database with RM_RDY
340 			 * status. This is needed to support the `removef'
341 			 * command.
342 			 */
343 			stflag = pkgpinfo->status;
344 			pkgpinfo->status = RM_RDY;
345 
346 			if (putcvfpfile(cf_ent, vfpo)) {
347 				progerr(gettext(ERR_OUTPUT));
348 				quit(99);
349 			}
350 
351 			/*
352 			 * If object is provided by a server, allocate an
353 			 * info block and set the status to indicate this.
354 			 * This is needed to support the `removef' command.
355 			 */
356 			if (stflag == SERVED_FILE) {
357 				el_ent->cf_ent.pinfo =
358 				    (struct pinfo *)calloc(1,
359 				    sizeof (struct pinfo));
360 				el_ent->cf_ent.pinfo->next = NULL;
361 				el_ent->cf_ent.pinfo->status = SERVED_FILE;
362 			}
363 		}
364 		return (1);
365 	}
366 
367 	/*
368 	 * If there is no package associated with it, there's something
369 	 * very wrong.
370 	 */
371 	if (!pkgpinfo) {
372 		progerr(gettext(ERR_PINFO), cf_ent->path);
373 		quit(99);
374 	}
375 
376 	/*
377 	 * Do not allow installation if nocnflct is set and other packages
378 	 * reference this pathname. The cp_cfent() function below writes the
379 	 * information from the installed file over the new entry, so the
380 	 * package database will be unchanged.
381 	 *
382 	 * By the way, ftype "e" is often shared and that's OK, so ftype
383 	 * "e" doesn't count here.
384 	 */
385 	if ((nocnflct && el_ent->mstat.shared && el_ent->cf_ent.ftype != 'e')) {
386 		/*
387 		 * First set the attrchg and contchg entries for proper
388 		 * messaging in the install phase.
389 		 */
390 		set_change(el_ent);
391 
392 		/*
393 		 * Now overwrite the new entry with the entry for the
394 		 * currently installed object.
395 		 */
396 		if (cp_cfent(cf_ent, el_ent) == 0)
397 			quit(99);
398 
399 		ignore++;
400 	} else {
401 		mrg_result = merg(el_ent, cf_ent);
402 
403 		switch (mrg_result) {
404 		    case MRG_SAME:
405 			break;
406 
407 		    case MRG_DIFFERENT:
408 			changed++;
409 			break;
410 
411 		    case MRG_REPLACE:
412 			/*
413 			 * We'll pick one or the other later. For now, cf_ent
414 			 * will have the fault value and el_ent will retain
415 			 * the other value. This is the only state that allows
416 			 * the database and the pkgmap to differ.
417 			 */
418 
419 			el_ent->mstat.contchg = 1;	/* subject to change */
420 			ignore++;
421 			break;
422 
423 		    default:
424 			break;
425 		}
426 	}
427 
428 	/* el_ent structure now contains updated entry */
429 	if (!el_ent->mstat.contchg && !ignore) {
430 		/*
431 		 * We know the DB entry matches the pkgmap, so now we need to
432 		 * see if the actual object matches the pkgmap.
433 		 */
434 		set_change(el_ent);
435 	}
436 
437 	if (!errflg) {
438 		if (ctrl == 1) {	/* quick verify assumes OK */
439 			/*
440 			 * The pkgpinfo entry is already correctly
441 			 * constructed. Look into dropping this soon.
442 			 */
443 			pkgpinfo = eptstat(&(el_ent->cf_ent), pkginst,
444 			    ENTRY_OK);
445 
446 			if (stflag != DUP_ENTRY) {
447 				changed++;
448 			}
449 
450 			/*
451 			 * We could trust the prior pkginfo entry, but things
452 			 * could have changed and  we need to update the
453 			 * fs_tab[] anyway. We check for a server object
454 			 * here.
455 			 */
456 			if (is_served(el_ent->server_path,
457 			    &(el_ent->fsys_value)))
458 				pkgpinfo->status = SERVED_FILE;
459 		} else {
460 			if (!ignore && el_ent->mstat.contchg) {
461 				pkgpinfo =
462 				    eptstat(&(el_ent->cf_ent), pkginst,
463 				    (dbst ? dbst : CONFIRM_CONT));
464 			} else if (!ignore && el_ent->mstat.attrchg) {
465 				pkgpinfo =
466 				    eptstat(&(el_ent->cf_ent), pkginst,
467 				    (dbst ? dbst : CONFIRM_ATTR));
468 			} else if (!ignore && el_ent->mstat.shared) {
469 				pkgpinfo =
470 				    eptstat(&(el_ent->cf_ent), pkginst,
471 				    dbst);
472 				changed++;
473 			} else if (stflag != DUP_ENTRY) {
474 				pkgpinfo = eptstat(&(el_ent->cf_ent),
475 				    pkginst, '\0');
476 				if (stflag != ENTRY_OK) {
477 					changed++;
478 				}
479 			}
480 		}
481 
482 		if (mrg_result == MRG_REPLACE) {
483 			/*
484 			 * Put the original package database entry back into
485 			 * the package database for now.
486 			 */
487 			output(vfpo, cf_ent, pkgpinfo);
488 		} else {
489 			/* Put the merged entry into the package database. */
490 			output(vfpo, &(el_ent->cf_ent), pkgpinfo);
491 		}
492 	}
493 
494 	if (pkgpinfo->aclass[0] != '\0') {
495 		(void) strcpy(el_ent->cf_ent.pkg_class, pkgpinfo->aclass);
496 	}
497 
498 	/*
499 	 * If a sym link entry exists in the contents file and
500 	 * and the destination of the link does not exist on the the system
501 	 * then the contents file needs to be updated appropriately so a
502 	 * subsequent invocation of "installf -f" will create the destination.
503 	 */
504 	if (el_ent->mstat.contchg && pkgpinfo->status == INST_RDY) {
505 		changed++;
506 	}
507 
508 	if (!(el_ent->mstat.preloaded))
509 		el_ent->cf_ent.pinfo = NULL;
510 
511 	/*
512 	 * If no change during the merg and we don't have a case where types
513 	 * were different in odd ways, count this as installed.
514 	 */
515 	if (!el_ent->mstat.attrchg && !el_ent->mstat.contchg &&
516 	    !el_ent->mstat.replace)
517 		installed++;
518 	return (changed);
519 }
520 
521 /* Insert an entirely new entry into the package database. */
522 static int
523 do_new_ent(VFP_T *vfpo, struct cfextra *el_ent, int ctrl)
524 {
525 	struct pinfo	*pinfo;
526 	char		*tp;
527 	int		changed = 0;
528 
529 	if (el_ent->cf_ent.ftype == RM_RDY) {
530 		return (0);
531 	}
532 
533 	tp = el_ent->server_path;
534 	/*
535 	 * Check the file/dir existence only if any of the parent directory
536 	 * of the file/dir has not changed from symbolic link to directory.
537 	 * At this time we are only doing a dry run, the symlink is not yet
538 	 * replaced, so if this is done directly then access will result in
539 	 * incorrect information in case a file with the same attr and cont
540 	 * exists in the link target.
541 	 */
542 	if ((!el_ent->mstat.parentsyml2dir) && (access(tp, F_OK) == 0)) {
543 		/*
544 		 * Path exists, and although its not referenced by any
545 		 * package we make it look like it is so it appears as a
546 		 * conflicting file in case the user doesn't want it
547 		 * installed. We set the rogue flag to distinguish this from
548 		 * package object conflicts if the administrator is queried
549 		 * about this later. Note that noconflict means NO conflict
550 		 * at the file level. Even rogue files count.
551 		 */
552 		el_ent->mstat.shared = 1;
553 		el_ent->mstat.rogue = 1;
554 		set_change(el_ent);
555 	} else {
556 		/* since path doesn't exist, we're changing everything */
557 		el_ent->mstat.rogue = 0;
558 		el_ent->mstat.contchg = 1;
559 		el_ent->mstat.attrchg = 1;
560 	}
561 
562 	if (el_ent->cf_ent.ainfo.mode == WILDCARD) {
563 		if (el_ent->cf_ent.ftype == 'd') {
564 			el_ent->cf_ent.ainfo.mode = DEFAULT_MODE;
565 		} else {
566 			el_ent->cf_ent.ainfo.mode = DEFAULT_MODE_FILE;
567 		}
568 		logerr(WRN_SET_DEF_MODE, el_ent->cf_ent.path,
569 		    (int)el_ent->cf_ent.ainfo.mode);
570 	}
571 
572 	if (strcmp(el_ent->cf_ent.ainfo.owner, DB_UNDEFINED_ENTRY) == 0)
573 		(void) strcpy(el_ent->cf_ent.ainfo.owner,
574 				DEFAULT_OWNER);
575 	if (strcmp(el_ent->cf_ent.ainfo.group, DB_UNDEFINED_ENTRY) == 0)
576 		(void) strcpy(el_ent->cf_ent.ainfo.group,
577 				DEFAULT_GROUP);
578 
579 	/*
580 	 * Do not allow installation if nocnflct is set and this pathname is
581 	 * already in place. Since this entry is new (not associated with a
582 	 * package), we don't issue anything to the database we're building.
583 	 */
584 	if (nocnflct && el_ent->mstat.shared) {
585 		return (0);
586 	}
587 
588 	if (!errflg) {
589 		if (el_ent->mstat.preloaded) {
590 			/* Add this package to the already established list. */
591 			pinfo = eptstat(&(el_ent->cf_ent), pkginst, DUP_ENTRY);
592 		} else {
593 			el_ent->cf_ent.npkgs = 1;
594 			pinfo = (struct pinfo *)calloc(1,
595 			    sizeof (struct pinfo));
596 			if (!pinfo) {
597 				progerr(gettext(ERR_MEMORY), errno);
598 				quit(99);
599 			}
600 			el_ent->cf_ent.pinfo = pinfo;
601 			(void) strcpy(pinfo->pkg, pkginst);
602 		}
603 
604 		if (ctrl == 1) {	/* quick verify assumes OK */
605 			pinfo->status = dbst ? dbst : ENTRY_OK;
606 			/*
607 			 * The entry won't be verified, but the entry in the
608 			 * database isn't necessarily ENTRY_OK. If this is
609 			 * coming from a server, we need to note that
610 			 * instead.
611 			 */
612 			if (is_served(el_ent->server_path,
613 			    &(el_ent->fsys_value)))
614 				pinfo->status = SERVED_FILE;
615 		} else {
616 			pinfo->status = dbst ? dbst : CONFIRM_CONT;
617 		}
618 
619 		output(vfpo, &(el_ent->cf_ent), pinfo);
620 		changed++;
621 
622 		free(pinfo);
623 		el_ent->cf_ent.pinfo = NULL;
624 		}
625 	if (!el_ent->mstat.attrchg && !el_ent->mstat.contchg) {
626 		installed++;
627 	}
628 
629 	return (changed);
630 }
631 
632 int
633 files_installed(void)
634 {
635 	return (installed);
636 }
637 
638 /*
639  * This function determines if there is a difference between the file on
640  * the disk and the file to be laid down. It set's mstat flags attrchg
641  * and contchg accordingly.
642  */
643 static void
644 set_change(struct cfextra *el_ent)
645 {
646 	int	n;
647 	char 	*tp;
648 
649 	tp = el_ent->server_path;
650 	if ((el_ent->cf_ent.ftype == 'f') || (el_ent->cf_ent.ftype == 'e') ||
651 		(el_ent->cf_ent.ftype == 'v')) {
652 		if (cverify(0, &(el_ent->cf_ent.ftype), tp,
653 		    &(el_ent->cf_ent.cinfo), 1)) {
654 			el_ent->mstat.contchg = 1;
655 		} else if (!el_ent->mstat.contchg && !el_ent->mstat.attrchg) {
656 			if (averify(0, &(el_ent->cf_ent.ftype), tp,
657 			    &(el_ent->cf_ent.ainfo)))
658 				el_ent->mstat.attrchg = 1;
659 		}
660 	} else if (!el_ent->mstat.attrchg &&
661 		((el_ent->cf_ent.ftype == 'd') ||
662 		(el_ent->cf_ent.ftype == 'x') ||
663 		(el_ent->cf_ent.ftype == 'c') ||
664 		(el_ent->cf_ent.ftype == 'b') ||
665 		(el_ent->cf_ent.ftype == 'p'))) {
666 		n = averify(0, &(el_ent->cf_ent.ftype), tp,
667 		    &(el_ent->cf_ent.ainfo));
668 		if (n == VE_ATTR)
669 			el_ent->mstat.attrchg = 1;
670 		else if (n && (n != VE_EXIST)) {
671 			el_ent->mstat.contchg = 1;
672 		}
673 	} else if (!el_ent->mstat.attrchg &&
674 		((el_ent->cf_ent.ftype == 's') ||
675 		(el_ent->cf_ent.ftype == 'l'))) {
676 		n = averify(0, &(el_ent->cf_ent.ftype), tp,
677 		    &(el_ent->cf_ent.ainfo));
678 		if (n == VE_ATTR)
679 			el_ent->mstat.attrchg = 1;
680 		else if (n && (n == VE_EXIST)) {
681 			el_ent->mstat.contchg = 1;
682 		}
683 	}
684 }
685 
686 static int
687 is_setuid(struct cfent *ent)
688 {
689 	return (((ent->ftype == 'f') || (ent->ftype == 'v') ||
690 		(ent->ftype == 'e')) &&
691 		(ent->ainfo.mode != BADMODE) &&
692 		(ent->ainfo.mode != WILDCARD) &&
693 		(ent->ainfo.mode & S_ISUID));
694 }
695 
696 static int
697 is_setgid(struct cfent *ent)
698 {
699 	return (((ent->ftype == 'f') || (ent->ftype == 'v') ||
700 		(ent->ftype == 'e')) && (ent->ainfo.mode != BADMODE) &&
701 		(ent->ainfo.mode != WILDCARD) &&
702 		(ent->ainfo.mode & S_ISGID) &&
703 		(ent->ainfo.mode & (S_IEXEC|S_IXUSR|S_IXOTH)));
704 }
705 
706 char *types[] = {
707 	"fev",	/* type 1, regular files */
708 	"s", 	/* type 2, symbolic links */
709 	"l", 	/* type 3, linked files */
710 	"dx", 	/* type 4, directories */
711 	"c", 	/* type 5, character special devices */
712 	"b", 	/* type 6, block special devices */
713 	"p", 	/* type 7, named pipes */
714 	NULL
715 };
716 
717 /*
718  * This determines if the ftype of the file on the disk and the file to be
719  * laid down are close enough. If they aren't, this either returns an error
720  * or displays a warning. This returns :
721  *	TYPE_OK		they're identical or close enough
722  *	TYPE_WARNING	they're pretty close (probably no problem)
723  *	TYPE_IGNORED	the type change was not allowed
724  *	TYPE_REPLACE	to be reviewed later - in endofclass() maybe
725  *	TYPE_FATAL	something awful happened
726  */
727 static int
728 typechg(struct cfent *el_ent, struct cfent *cf_ent, struct mergstat *mstat)
729 {
730 	int	i, etype, itype, retcode;
731 
732 	/* If they are identical, return OK */
733 	if (cf_ent->ftype == el_ent->ftype)
734 		return (TYPE_OK);
735 
736 	/*
737 	 * If package database entry is ambiguous, set it to the new entity's
738 	 * ftype
739 	 */
740 	if (cf_ent->ftype == BADFTYPE) {
741 		cf_ent->ftype = el_ent->ftype;
742 		return (TYPE_OK); /* do nothing; not really different */
743 	}
744 
745 	/* If the new entity is ambiguous, wait for the verify */
746 	if (el_ent->ftype == BADFTYPE)
747 		return (TYPE_OK);
748 
749 	/*
750 	 * If we're trying to convert an existing regular directory to an
751 	 * exclusive directory, this is very dangerous. We will continue, but
752 	 * we will deny the conversion.
753 	 */
754 	if (el_ent->ftype == 'x' && cf_ent->ftype == 'd') {
755 		logerr(gettext(WRN_TOEXCL), el_ent->path);
756 		return (TYPE_IGNORED);
757 	}
758 
759 	etype = itype = 0;
760 
761 	/* Set etype to that of the new entity */
762 	for (i = 0; types[i]; ++i) {
763 		if (strchr(types[i], el_ent->ftype)) {
764 			etype = i+1;
765 			break;
766 		}
767 	}
768 
769 	/* Set itype to that in the package database. */
770 	for (i = 0; types[i]; ++i) {
771 		if (strchr(types[i], cf_ent->ftype)) {
772 			itype = i+1;
773 			break;
774 		}
775 	}
776 
777 	if (itype == etype) {
778 		/* same basic object type */
779 		return (TYPE_OK);
780 	}
781 
782 	retcode = TYPE_WARNING;
783 
784 	/*
785 	 * If a simple object (like a file) is overwriting a directory, mark
786 	 * it for full inspection during installation.
787 	 */
788 	if (etype != 4 && itype == 4) {
789 		mstat->dir2nondir = 1;
790 		retcode = TYPE_REPLACE;
791 	}
792 
793 	/* allow change, but warn user of possible problems */
794 	switch (itype) {
795 	    case 1:
796 		logerr(gettext(WRN_NOTFILE), el_ent->path);
797 		break;
798 
799 	    case 2:
800 		logerr(gettext(WRN_NOTSYMLN), el_ent->path);
801 		break;
802 
803 	    case 3:
804 		logerr(gettext(WRN_NOTLINK), el_ent->path);
805 		break;
806 
807 	    case 4:
808 		logerr(gettext(WRN_NOTDIR), el_ent->path);
809 		break;
810 
811 	    case 5:
812 		logerr(gettext(WRN_NOTCHAR), el_ent->path);
813 		break;
814 
815 	    case 6:
816 		logerr(gettext(WRN_NOTBLOCK), el_ent->path);
817 		break;
818 
819 	    case 7:
820 		logerr(gettext(WRN_NOTPIPE), el_ent->path);
821 		break;
822 
823 	    default:
824 		break;
825 	}
826 	return (retcode);
827 }
828 
829 /*
830  * This function takes el_ent (the entry from the pkgmap) and cf_ent (the
831  * entry from the package database) and merge them into el_ent. The rules
832  * are still being figured out, but the comments should make the approach
833  * pretty clear.
834  *
835  * RETURN CODES:
836  *	MRG_DIFFERENT	The two entries are different and el_ent now contains
837  *			the intended new entry to be installed.
838  *	MRG_SAME	The two entries were identical and the old database
839  *			entry will be replaced unchanged.
840  *	MRG_REPLACE	One or the other entry will be used but the decision
841  *			has to be made at install time.
842  */
843 static int
844 merg(struct cfextra *el_ent, struct cfent *cf_ent)
845 {
846 	int	n, changed = 0;
847 
848 	/*
849 	 * We need to change the original entry to make it look like the new
850 	 * entry (the eptstat() routine has already added appropriate package
851 	 * information, but not about 'aclass' which may represent a change
852 	 * in class from the previous installation.
853 	 *
854 	 * NOTE: elent->cf_ent.pinfo (the list of associated packages) is NULL
855 	 * upon entry to this function.
856 	 */
857 
858 	el_ent->cf_ent.pinfo = cf_ent->pinfo;
859 
860 	if (dbst == INST_RDY && el_ent->cf_ent.ftype == '?') {
861 		el_ent->cf_ent.ftype = cf_ent->ftype;
862 	}
863 
864 	/*
865 	 * Evaluate the ftype change. Usually the ftype won't change. If it
866 	 * does it may be easy (s -> f), not allowed (d -> x), so complex we
867 	 * can't figure it 'til later (d -> s) or fatal (a hook for later).
868 	 */
869 	if (cf_ent->ftype != el_ent->cf_ent.ftype) {
870 		n = typechg(&(el_ent->cf_ent), cf_ent, &(el_ent->mstat));
871 
872 		switch (n) {
873 		    case TYPE_OK:
874 			break;
875 
876 		    /* This is an allowable change. */
877 		    case TYPE_WARNING:
878 			el_ent->mstat.contchg = 1;
879 			break;
880 
881 		    /* Not allowed, but leaving it as is is OK. */
882 		    case TYPE_IGNORED:
883 			logerr(gettext(MSG_TYPIGN));
884 			if (cp_cfent(cf_ent, el_ent) == 0)
885 				quit(99);
886 			return (MRG_SAME);
887 
888 		    /* Future analysis will reveal if this is OK. */
889 		    case TYPE_REPLACE:
890 			el_ent->mstat.replace = 1;
891 			return (MRG_REPLACE);
892 
893 		    /* Kill it before it does any damage. */
894 		    case TYPE_FATAL:
895 			logerr(gettext(MSG_TYPE_ERR));
896 			quit(99);
897 
898 		    default:
899 			break;
900 		}
901 
902 		changed++;
903 	}
904 
905 	/* Evaluate and merge the class. */
906 	if (strcmp(cf_ent->pkg_class, el_ent->cf_ent.pkg_class)) {
907 		/*
908 		 * we always allow a class change as long as we have
909 		 * consistent ftypes, which at this point we must
910 		 */
911 		changed++;
912 		if (strcmp(cf_ent->pkg_class, "?")) {
913 			(void) strcpy(pkgpinfo->aclass,
914 			    el_ent->cf_ent.pkg_class);
915 			(void) strcpy(el_ent->cf_ent.pkg_class,
916 			    cf_ent->pkg_class);
917 			chgclass(&(el_ent->cf_ent), pkgpinfo);
918 		}
919 	}
920 
921 	/*
922 	 * Evaluate and merge based upon the ftype of the intended package
923 	 * database entry.
924 	 */
925 	if (((el_ent->cf_ent.ftype == 's') || (el_ent->cf_ent.ftype == 'l'))) {
926 
927 		/* If both have link sources, then they need to be merged. */
928 		if (cf_ent->ainfo.local && el_ent->cf_ent.ainfo.local) {
929 			/*
930 			 * If both sources are identical, the merge is
931 			 * already done.
932 			 */
933 			if (strcmp(cf_ent->ainfo.local,
934 			    el_ent->cf_ent.ainfo.local) != NULL) {
935 				changed++;
936 
937 				/*
938 				 * Otherwise, if the pkgmap entry is
939 				 * ambiguous, it will inherit the database
940 				 * entry.
941 				 */
942 				if (strcmp(el_ent->cf_ent.ainfo.local,
943 				    "?") == NULL) {
944 					(void) strlcpy(
945 						el_ent->cf_ent.ainfo.local,
946 						cf_ent->ainfo.local,
947 						PATH_MAX);
948 				} else {
949 					el_ent->mstat.contchg = 1;
950 				}
951 			}
952 		}
953 		return (changed ? MRG_DIFFERENT : MRG_SAME);
954 
955 	} else if (el_ent->cf_ent.ftype == 'e') {
956 
957 		/*
958 		 * The contents of edittable files are assumed to be changing
959 		 * since some class action script will be doing the work and
960 		 * we have no way of evaluating what it will actually do.
961 		 */
962 		el_ent->mstat.contchg = 1;
963 		changed++;
964 	} else if (((el_ent->cf_ent.ftype == 'f') ||
965 					(el_ent->cf_ent.ftype == 'v'))) {
966 		/*
967 		 * For regular files, Look at content information; a BADCONT
968 		 * in any el_ent field indicates the contents are unknown --
969 		 * since cf_ent is guaranteed to have a valid entry here (bad
970 		 * assumption?) this function will recognize this as a
971 		 * change. The ambiguous el_ent values will be evaluated and
972 		 * set later.
973 		 */
974 
975 		/*
976 		 * for type f/v files, if the file is in an area that is
977 		 * inherited from the global zone, that area is read only
978 		 * and the object cannot be changed - ignore any settings
979 		 * in the current package database that may be present for
980 		 * any existing object because they are irrelevant - since
981 		 * the object is in a read-only area shared from the global
982 		 * zone, accept that file's actual attributes as being correct.
983 		 */
984 
985 		if (z_path_is_inherited(el_ent->cf_ent.path,
986 			el_ent->cf_ent.ftype, get_inst_root()) == B_TRUE) {
987 			echoDebug(DBG_PKGDBMRG_INHERITED, el_ent->cf_ent.path);
988 		} else if (cf_ent->cinfo.size != el_ent->cf_ent.cinfo.size) {
989 			changed++;
990 			el_ent->mstat.contchg = 1;
991 		} else if (cf_ent->cinfo.modtime !=
992 		    el_ent->cf_ent.cinfo.modtime) {
993 			changed++;
994 			el_ent->mstat.contchg = 1;
995 		} else if (cf_ent->cinfo.cksum != el_ent->cf_ent.cinfo.cksum) {
996 			changed++;
997 			el_ent->mstat.contchg = 1;
998 		}
999 	} else if (((el_ent->cf_ent.ftype == 'c') ||
1000 					(el_ent->cf_ent.ftype == 'b'))) {
1001 		/*
1002 		 * For devices, if major or minor numbers are identical the
1003 		 * merge is trivial. If the el_ent value is ambiguous (BAD),
1004 		 * the cf_ent value is inherited. Otherwise, the el_ent value
1005 		 * is preserved.
1006 		 */
1007 		if (cf_ent->ainfo.major != el_ent->cf_ent.ainfo.major) {
1008 			changed++;
1009 			if (el_ent->cf_ent.ainfo.major == BADMAJOR) {
1010 				el_ent->cf_ent.ainfo.major =
1011 				    cf_ent->ainfo.major;
1012 			} else {
1013 				el_ent->mstat.contchg = 1;
1014 			}
1015 		}
1016 		if (cf_ent->ainfo.minor != el_ent->cf_ent.ainfo.minor) {
1017 			changed++;
1018 			if (el_ent->cf_ent.ainfo.minor == BADMINOR)
1019 				el_ent->cf_ent.ainfo.minor =
1020 				    cf_ent->ainfo.minor;
1021 			else
1022 				el_ent->mstat.contchg = 1;
1023 		}
1024 	}
1025 
1026 	/*
1027 	 * For mode, owner and group follow the same rules as above - if
1028 	 * ambiguous, inherit, otherwise keep the new one.
1029 	 */
1030 	if (cf_ent->ainfo.mode != el_ent->cf_ent.ainfo.mode) {
1031 		changed++;  /* attribute info is changing */
1032 		if (el_ent->cf_ent.ainfo.mode == BADMODE) {
1033 			el_ent->cf_ent.ainfo.mode = cf_ent->ainfo.mode;
1034 		} else if (el_ent->cf_ent.ainfo.mode == WILDCARD) {
1035 			/*
1036 			 * If pkgmap has a '?' set for mode, use the mode from
1037 			 * the pkg DB (contents file).
1038 			 */
1039 			el_ent->cf_ent.ainfo.mode = cf_ent->ainfo.mode;
1040 			el_ent->mstat.attrchg = 0;
1041 		} else {
1042 			el_ent->mstat.attrchg = 1;
1043 		}
1044 	}
1045 	if (strcmp(cf_ent->ainfo.owner, el_ent->cf_ent.ainfo.owner) != 0) {
1046 		changed++;  /* attribute info is changing */
1047 		if (strcmp(el_ent->cf_ent.ainfo.owner, BADOWNER) == 0)
1048 			(void) strcpy(el_ent->cf_ent.ainfo.owner,
1049 			    cf_ent->ainfo.owner);
1050 		else
1051 			el_ent->mstat.attrchg = 1;
1052 	}
1053 	if (strcmp(cf_ent->ainfo.group, el_ent->cf_ent.ainfo.group) != 0) {
1054 		changed++;  /* attribute info is changing */
1055 		if (strcmp(el_ent->cf_ent.ainfo.group, BADGROUP) == 0)
1056 			(void) strcpy(el_ent->cf_ent.ainfo.group,
1057 			    cf_ent->ainfo.group);
1058 		else
1059 			el_ent->mstat.attrchg = 1;
1060 	}
1061 	return (changed ? MRG_DIFFERENT : MRG_SAME);
1062 }
1063 
1064 /*
1065  * This puts the current entry into the package database in the appropriate
1066  * intermediate format for this stage of the installation. This also assures
1067  * the correct format for the various package object ftypes, stripping the
1068  * link name before storing a regular file and stuff like that.
1069  */
1070 
1071 static void
1072 output(VFP_T *vfpo, struct cfent *ent, struct pinfo *pinfo)
1073 {
1074 	short	svvolno;
1075 	char	*svpt;
1076 
1077 	/* output without volume information */
1078 	svvolno = ent->volno;
1079 	ent->volno = 0;
1080 
1081 	pinfo->editflag = 0;
1082 	if (((ent->ftype == 's') || (ent->ftype == 'l'))) {
1083 		if (putcvfpfile(ent, vfpo)) {
1084 			progerr(gettext(ERR_OUTPUT));
1085 			quit(99);
1086 		}
1087 	} else {
1088 
1089 		/* output without local pathname */
1090 		svpt = ent->ainfo.local;
1091 		ent->ainfo.local = NULL;
1092 		if (putcvfpfile(ent, vfpo)) {
1093 			progerr(gettext(ERR_OUTPUT));
1094 			quit(99);
1095 		}
1096 
1097 		ent->ainfo.local = svpt;
1098 		/*
1099 		 * If this entry represents a file which is being edited, we
1100 		 * need to store in memory the fact that it is an edittable
1101 		 * file so that when we audit it after installation we do not
1102 		 * worry about its contents; we do this by resetting the ftype
1103 		 * to 'e' in the memory array which is later used to control
1104 		 * the audit
1105 		 */
1106 		if (pinfo->editflag)
1107 			ent->ftype = 'e';
1108 	}
1109 	/* restore volume information */
1110 	ent->volno = svvolno;
1111 }
1112 
1113 static void
1114 chgclass(struct cfent *cf_ent, struct pinfo *pinfo)
1115 {
1116 	struct pinfo *pp;
1117 	char	*oldclass, newclass[CLSSIZ+1];
1118 	int	newcnt, oldcnt;
1119 
1120 	/*
1121 	 * we use this routine to minimize the use of the aclass element by
1122 	 * optimizing the use of the cf_ent->pkg_class element
1123 	 */
1124 
1125 	(void) strlcpy(newclass, pinfo->aclass, sizeof (newclass));
1126 	newcnt = 1;
1127 
1128 	oldclass = cf_ent->pkg_class;
1129 	oldcnt = 0;
1130 
1131 	/*
1132 	 * count the number of times the newclass will be used and see if it
1133 	 * exceeds the number of times the oldclass is referenced
1134 	 */
1135 	pp = cf_ent->pinfo;
1136 	while (pp) {
1137 		if (pp->aclass[0] != '\0') {
1138 			if (strcmp(pp->aclass, newclass) == 0)
1139 				newcnt++;
1140 			else if (strcmp(pp->aclass, oldclass) == 0)
1141 				oldcnt++;
1142 		}
1143 		pp = pp->next;
1144 	}
1145 	if (newcnt > oldcnt) {
1146 		pp = cf_ent->pinfo;
1147 		while (pp) {
1148 			if (pp->aclass[0] == '\0') {
1149 				(void) strcpy(pp->aclass, oldclass);
1150 			} else if (strcmp(pp->aclass, newclass) == 0) {
1151 				pp->aclass[0] = '\0';
1152 			}
1153 			pp = pp->next;
1154 		}
1155 		(void) strcpy(cf_ent->pkg_class, newclass);
1156 	}
1157 }
1158