xref: /titanic_50/usr/src/cmd/svr4pkg/pkgchk/checkmap.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
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 2006 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 <string.h>
33 #include <limits.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <dirent.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/param.h>
41 #include <sys/mman.h>
42 #include <pkgstrct.h>
43 #include <pkglocs.h>
44 #include <locale.h>
45 #include <libintl.h>
46 #include <pkglib.h>
47 #include "libadm.h"
48 #include "libinst.h"
49 
50 extern int	qflag, lflag, Lflag, pkgcnt;
51 extern short	npaths;
52 
53 extern char	*basedir, *pathlist[], *ppathlist[], **pkg, **environ;
54 
55 extern short	used[];
56 extern struct cfent **eptlist;
57 
58 /* ocfile.c */
59 extern int	socfile(VFP_T **vfp);	/* simple open & lock of DB. */
60 extern int	relslock(void);		/* unlock the database. */
61 
62 /* ckentry.c */
63 extern int	ckentry(int envflag, int maptyp, struct cfent *ept, VFP_T *vfp);
64 
65 #define	NXTENTRY(P, VFP) \
66 		(maptyp ? srchcfile((P), "*", (VFP), (VFP_T *)NULL) : \
67 		gpkgmapvfp((P), (VFP)))
68 
69 #define	MSG_ARCHIVE	"NOTE: some pathnames are in private formats " \
70 			    "and cannot be verified"
71 #define	WRN_NOPKG	"WARNING: no pathnames were associated with <%s>"
72 #define	WRN_NOPATH	"WARNING: no information associated with pathname <%s>"
73 #define	EMPTY_PKG "WARNING: Package <%s> is installed but empty"
74 #define	ERR_NOMEM	"unable to allocate dynamic memory, errno=%d"
75 #define	ERR_PKGMAP	"unable to open pkgmap file <%s>"
76 #define	ERR_ENVFILE	"unable to open environment file <%s>"
77 
78 static struct cfent entry;
79 
80 static int	shellmatch(char *, char *);
81 static int is_partial_path_in_DB(char *, char *);
82 
83 int	selpath(char *, int);
84 int	selpkg(char *);
85 
86 /*
87  * This routine checks all files which are referenced in the pkgmap which is
88  * identified by the mapfile arg. When the package is installed, the mapfile
89  * may be the contents file or a separate pkgmap (maptyp tells the function
90  * which it is). The variable uninst tells the function whether the package
91  * is in the installed state or not. The envfile entry is usually a pkginfo
92  * file, but it could be any environment parameter list.
93  */
94 
95 int
96 checkmap(int maptyp, int uninst, char *mapfile, char *envfile,
97 		char *pkginst, char *path, int pathtype)
98 {
99 	FILE		*fp;
100 	char		*cl = NULL;
101 	char		*value;
102 	char		param[MAX_PKG_PARAM_LENGTH];
103 	int		count;
104 	int		errflg;
105 	int		n;
106 	int		selected;
107 	struct pinfo	*pinfo;
108 	VFP_T		*vfp = (VFP_T *)NULL;
109 
110 	if (envfile != NULL) {
111 		if ((fp = fopen(envfile, "r")) == NULL) {
112 			progerr(gettext(ERR_ENVFILE), envfile);
113 			return (-1);
114 		}
115 		param[0] = '\0';
116 		while (value = fpkgparam(fp, param)) {
117 			if (strcmp("PATH", param) != 0) {
118 				/*
119 				 * If checking an uninstalled package, we
120 				 * only want two parameters. If we took all
121 				 * of them, including path definitions, we
122 				 * wouldn't be looking in the right places in
123 				 * the reloc and root directories.
124 				 */
125 				if (uninst) {
126 					if ((strncmp("PKG_SRC_NOVERIFY", param,
127 					    16) == 0) && value) {
128 						logerr(gettext(MSG_ARCHIVE));
129 						putparam(param, value);
130 					}
131 					if ((strncmp("CLASSES", param,
132 					    7) == 0) && value)
133 						putparam(param, value);
134 				} else
135 					putparam(param, value);
136 			}
137 
138 			free(value);
139 
140 			param[0] = '\0';
141 		}
142 		(void) fclose(fp);
143 		basedir = getenv("BASEDIR");
144 	}
145 
146 	/*
147 	 * If we are using a contents file for the map, this locks the
148 	 * contents file in order to freeze the database and assure it
149 	 * remains synchronized with the file system against which it is
150 	 * being compared. There is no practical way to lock another pkgmap
151 	 * on some unknown medium so we don't bother.
152 	 */
153 	if (maptyp) {	/* If this is the contents file */
154 		if (!socfile(&vfp)) {
155 			progerr(gettext(ERR_PKGMAP), "contents");
156 			return (-1);
157 		}
158 	} else {
159 		if (vfpOpen(&vfp, mapfile, "r", VFP_NONE) != 0) {
160 			progerr(gettext(ERR_PKGMAP), mapfile);
161 			return (-1);
162 		}
163 	}
164 
165 	if ((cl = getenv("CLASSES")) != NULL)
166 		cl_sets(qstrdup(cl));
167 
168 	errflg = count = 0;
169 
170 	do {
171 		if ((n = NXTENTRY(&entry, vfp)) == 0) {
172 			break;
173 		}
174 		/*
175 		 * Search for partial paths in the ext DB.
176 		 */
177 		if (pathtype) {
178 			/* LINTED warning: statement has no consequent: if */
179 			if (is_partial_path_in_DB(entry.path, path)) {
180 				/* Check this entry */
181 				;
182 			} else if (entry.ftype == 's' ||
183 						entry.ftype == 'l') {
184 				if (is_partial_path_in_DB(
185 				/* LINTED warning: statement has no consequen */
186 					entry.ainfo.local, path)) {
187 					/* Check this entry */
188 					;
189 				} else {
190 					continue;
191 				}
192 			} else {
193 				/* Skip to next DB entry */
194 				continue;
195 			}
196 		}
197 
198 		if (n < 0) {
199 			char	*errstr = getErrstr();
200 			logerr(gettext("ERROR: garbled entry"));
201 			logerr(gettext("pathname: %s"),
202 			    (entry.path && *entry.path) ? entry.path :
203 			    "Unknown");
204 			logerr(gettext("problem: %s"),
205 			    (errstr && *errstr) ? errstr : "Unknown");
206 			exit(99);
207 		}
208 		if (n == 0)
209 			break; /* done with file */
210 
211 		/*
212 		 * The class list may not be complete for good reason, so
213 		 * there's no complaining if this returns an index of -1.
214 		 */
215 		if (cl != NULL)
216 			entry.pkg_class_idx = cl_idx(entry.pkg_class);
217 
218 		if (maptyp && pkginst != NULL) {
219 			/*
220 			 * check to see if the entry we just read
221 			 * is associated with one of the packages
222 			 * we have listed on the command line
223 			 */
224 			selected = 0;
225 			pinfo = entry.pinfo;
226 			while (pinfo) {
227 				if (selpkg(pinfo->pkg)) {
228 					selected++;
229 					break;
230 				}
231 				pinfo = pinfo->next;
232 			}
233 			if (!selected)
234 				continue; /* not selected */
235 		}
236 
237 		/*
238 		 * Check to see if the pathname associated with the entry
239 		 * we just read is associated with the list of paths we
240 		 * supplied on the command line
241 		 */
242 		if (!selpath(entry.path, pathtype))
243 			continue; /* not selected */
244 
245 		/*
246 		 * Determine if this is a package object wanting
247 		 * verification. Metafiles are always checked, otherwise, we
248 		 * rely on the class to discriminate.
249 		 */
250 		if (entry.ftype != 'i')
251 			/* If there's no class list... */
252 			if (cl != NULL)
253 				/*
254 				 * ... or this entry isn't in that class list
255 				 * or it's in a private format, then don't
256 				 * check it.
257 				 */
258 				if (entry.pkg_class_idx == -1 ||
259 				    cl_svfy(entry.pkg_class_idx) == NOVERIFY)
260 					continue;
261 
262 		count++;
263 		if (ckentry((envfile ? 1 : 0), maptyp, &entry, vfp))
264 			errflg++;
265 	} while (n != 0);
266 
267 	(void) vfpClose(&vfp);
268 
269 	if (maptyp)
270 		relslock();
271 
272 	if (environ) {
273 		/* free up environment resources */
274 		for (n = 0; environ[n]; n++)
275 			free(environ[n]);
276 		free(environ);
277 		environ = NULL;
278 	}
279 
280 	if (maptyp) {
281 		/*
282 		 * make sure each listed package was associated with
283 		 * an entry from the prototype or pkgmap
284 		 */
285 		(void) selpkg(NULL);
286 	}
287 	if (!qflag && !lflag && !Lflag) {
288 		/*
289 		 * make sure each listed pathname was associated with an entry
290 		 * from the prototype or pkgmap
291 		 */
292 		(void) selpath(NULL, pathtype);
293 	}
294 	return (errflg);
295 }
296 
297 int
298 selpkg(char *p)
299 {
300 	static char *selected;
301 	char buf[80];
302 	char *root;
303 	register int i;
304 
305 	if (p == NULL) {
306 		if (selected == NULL) {
307 			if (pkgcnt) {
308 				for (i = 0; i < pkgcnt; ++i) {
309 					/* bugid 1227628 */
310 					root = get_inst_root();
311 					if (root)
312 						(void) snprintf(buf,
313 						sizeof (buf),
314 						"%s/var/sadm/pkg/%s/pkginfo",
315 						root, pkg[i]);
316 					else
317 						(void) snprintf(buf,
318 						sizeof (buf),
319 						"/var/sadm/pkg/%s/pkginfo",
320 						pkg[i]);
321 
322 					if (access(buf, F_OK))
323 						logerr(gettext(WRN_NOPKG),
324 							pkg[i]);
325 					else
326 						logerr(gettext(EMPTY_PKG),
327 							pkg[i]);
328 				}
329 			}
330 		} else {
331 			for (i = 0; i < pkgcnt; ++i) {
332 				if (selected[i] == NULL) {
333 					root = get_inst_root();
334 					if (root)
335 						(void) snprintf(buf,
336 						sizeof (buf),
337 						"%s/var/sadm/pkg/%s/pkginfo",
338 						root, pkg[i]);
339 					else
340 						(void) snprintf(buf,
341 						sizeof (buf),
342 						"/var/sadm/pkg/%s/pkginfo",
343 						pkg[i]);
344 
345 					if (access(buf, F_OK))
346 						logerr(gettext(WRN_NOPKG),
347 							pkg[i]);
348 					else
349 						logerr(gettext(EMPTY_PKG),
350 							pkg[i]);
351 				}
352 			}
353 		}
354 		return (0); /* return value not important */
355 	} else if (pkgcnt == 0)
356 		return (1);
357 	else if (selected == NULL) {
358 		selected =
359 		    (char *)calloc((unsigned)(pkgcnt+1), sizeof (char));
360 		if (selected == NULL) {
361 			progerr(gettext(ERR_NOMEM), errno);
362 			exit(99);
363 			/*NOTREACHED*/
364 		}
365 	}
366 
367 	for (i = 0; i < pkgcnt; ++i) {
368 		if (pkgnmchk(p, pkg[i], 0) == 0) {
369 			if (selected != NULL)
370 				selected[i] = 'b';
371 			return (1);
372 		}
373 	}
374 	return (0);
375 }
376 
377 int
378 selpath(char *path, int partial_path)
379 {
380 	int n;
381 
382 	if (!npaths)
383 		return (1); /* everything is selectable */
384 
385 	for (n = 0; n < npaths; n++) {
386 		if (path == NULL) {
387 			if (!used[n])
388 				logerr(gettext(WRN_NOPATH),
389 					partial_path ? ppathlist[n] :
390 					pathlist[n]);
391 		} else if (partial_path) {
392 			used[n] = 1;
393 			return (1);
394 		} else if (!shellmatch(pathlist[n], path)) {
395 			used[n] = 1;
396 			return (1);
397 		}
398 	}
399 	return (0); /* not selected */
400 }
401 
402 static int
403 shellmatch(char *spec, char *path)
404 {
405 	/* Check if the value is NULL */
406 	if (spec == NULL || path == NULL)
407 		return (1);
408 
409 	while (*spec && (*spec == *path)) {
410 		spec++, path++;
411 	}
412 	if ((*spec == *path) || (*spec == '*'))
413 		return (0);
414 	return (1);
415 }
416 
417 static int
418 is_partial_path_in_DB(char *srcpath, char *trgtpath)
419 {
420 	if (strstr(srcpath, trgtpath) == NULL) {
421 		return (0);
422 	} else {
423 		return (1);
424 	}
425 }
426