xref: /titanic_51/usr/src/lib/libadm/common/pkgparam.c (revision d260b2decd1eb31144c8ec2f1dce879ed62c2c12)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d260b2deSny155746  * Common Development and Distribution License (the "License").
6*d260b2deSny155746  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
25*d260b2deSny155746  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
264f5c01f9Sns158690  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1 */
307c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*   5-20-92   newroot support added  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <limits.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <pkgstrct.h>
417c478bd9Sstevel@tonic-gate #include <pkginfo.h>
427c478bd9Sstevel@tonic-gate #include <pkglocs.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include "libadm.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	VALSIZ	128
487c478bd9Sstevel@tonic-gate #define	NEWLINE	'\n'
497c478bd9Sstevel@tonic-gate #define	ESCAPE	'\\'
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char sepset[] =	":=\n";
527c478bd9Sstevel@tonic-gate static char qset[] = 	"'\"";
537c478bd9Sstevel@tonic-gate static char *pkg_inst_root = NULL;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate char *pkgdir = NULL;
567c478bd9Sstevel@tonic-gate char *pkgfile = NULL;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static char Adm_pkgold[PATH_MAX] = { 0 }; /* added for newroot */
597c478bd9Sstevel@tonic-gate static char Adm_pkgloc[PATH_MAX] = { 0 }; /* added for newroot */
607c478bd9Sstevel@tonic-gate static char Adm_pkgadm[PATH_MAX] = { 0 }; /* added for newroot */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * This looks in a directory that might be the top level directory of a
647c478bd9Sstevel@tonic-gate  * package. It tests a temporary install directory first and then for a
657c478bd9Sstevel@tonic-gate  * standard directory. This looks a little confusing, so here's what's
667c478bd9Sstevel@tonic-gate  * happening. If this pkginfo is being openned in a script during a pkgadd
677c478bd9Sstevel@tonic-gate  * which is updating an existing package, the original pkginfo file is in a
687c478bd9Sstevel@tonic-gate  * directory that has been renamed from <pkginst> to .save.<pkginst>. If the
697c478bd9Sstevel@tonic-gate  * pkgadd fails it will be renamed back to <pkginst>. We are always interested
707c478bd9Sstevel@tonic-gate  * in the OLD pkginfo data because the new pkginfo data is already in our
717c478bd9Sstevel@tonic-gate  * environment. For that reason, we try to open the backup first - that has
727c478bd9Sstevel@tonic-gate  * the old data. This returns the first accessible path in "path" and a "1"
737c478bd9Sstevel@tonic-gate  * if an appropriate pkginfo file was found. It returns a 0 if no type of
747c478bd9Sstevel@tonic-gate  * pkginfo was located.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate int
777c478bd9Sstevel@tonic-gate pkginfofind(char *path, char *pkg_dir, char *pkginst)
787c478bd9Sstevel@tonic-gate {
794f5c01f9Sns158690 	int len = 0;
804f5c01f9Sns158690 
817c478bd9Sstevel@tonic-gate 	/* Construct the temporary pkginfo file name. */
824f5c01f9Sns158690 	len =  snprintf(path, PATH_MAX, "%s/.save.%s/pkginfo", pkg_dir,
834f5c01f9Sns158690 	    pkginst);
844f5c01f9Sns158690 	if (len > PATH_MAX)
854f5c01f9Sns158690 		return (0);
867c478bd9Sstevel@tonic-gate 	if (access(path, 0)) {
877c478bd9Sstevel@tonic-gate 		/*
887c478bd9Sstevel@tonic-gate 		 * This isn't a temporary directory, so we look for a
897c478bd9Sstevel@tonic-gate 		 * regular one.
907c478bd9Sstevel@tonic-gate 		 */
914f5c01f9Sns158690 		len =  snprintf(path, PATH_MAX, "%s/%s/pkginfo", pkg_dir,
924f5c01f9Sns158690 		    pkginst);
934f5c01f9Sns158690 		if (len > PATH_MAX)
944f5c01f9Sns158690 			return (0);
957c478bd9Sstevel@tonic-gate 		if (access(path, 0))
967c478bd9Sstevel@tonic-gate 			return (0); /* doesn't appear to be a package */
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	return (1);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * This opens the appropriate pkginfo file for a particular package.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate FILE *
1067c478bd9Sstevel@tonic-gate pkginfopen(char *pkg_dir, char *pkginst)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	FILE *fp = NULL;
1097c478bd9Sstevel@tonic-gate 	char temp[PATH_MAX];
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (pkginfofind(temp, pkg_dir, pkginst))
1127c478bd9Sstevel@tonic-gate 		fp = fopen(temp, "r");
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	return (fp);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate char *
1197c478bd9Sstevel@tonic-gate fpkgparam(FILE *fp, char *param)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	char	ch, buffer[VALSIZ];
1227c478bd9Sstevel@tonic-gate 	char	*mempt, *copy;
123*d260b2deSny155746 	int	c, n;
124*d260b2deSny155746 	boolean_t check_end_quote = B_FALSE;
125*d260b2deSny155746 	boolean_t begline, quoted, escape;
126*d260b2deSny155746 	int idx = 0;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (param == NULL) {
1297c478bd9Sstevel@tonic-gate 		errno = ENOENT;
1307c478bd9Sstevel@tonic-gate 		return (NULL);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	mempt = NULL;
1347c478bd9Sstevel@tonic-gate 
135*d260b2deSny155746 	for (;;) {		/* For each entry in the file fp */
1367c478bd9Sstevel@tonic-gate 		copy = buffer;
1377c478bd9Sstevel@tonic-gate 		n = 0;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		/* Get the next token. */
1407c478bd9Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
1417c478bd9Sstevel@tonic-gate 			ch = (char)c;
1427c478bd9Sstevel@tonic-gate 			if (strchr(sepset, ch))
1437c478bd9Sstevel@tonic-gate 				break;
1447c478bd9Sstevel@tonic-gate 			if (++n < VALSIZ)
1457c478bd9Sstevel@tonic-gate 				*copy++ = ch;
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		/* If it's the end of the file, exit the for() loop */
1497c478bd9Sstevel@tonic-gate 		if (c == EOF) {
1507c478bd9Sstevel@tonic-gate 			errno = EINVAL;
151*d260b2deSny155746 			return (NULL); /* No more entries left */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		/* If it's end of line, look for the next parameter. */
1547c478bd9Sstevel@tonic-gate 		} else if (c == NEWLINE)
1557c478bd9Sstevel@tonic-gate 			continue;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		/* At this point copy points to the end of a valid parameter. */
1587c478bd9Sstevel@tonic-gate 		*copy = '\0';		/* Terminate the string. */
1597c478bd9Sstevel@tonic-gate 		if (buffer[0] == '#')	/* If it's a comment, drop thru. */
1607c478bd9Sstevel@tonic-gate 			copy = NULL;	/* Comments don't get buffered. */
1617c478bd9Sstevel@tonic-gate 		else {
1627c478bd9Sstevel@tonic-gate 			/* If parameter is NULL, we return whatever we got. */
1637c478bd9Sstevel@tonic-gate 			if (param[0] == '\0') {
1647c478bd9Sstevel@tonic-gate 				(void) strcpy(param, buffer);
1657c478bd9Sstevel@tonic-gate 				copy = buffer;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 			/* If this doesn't match the parameter, drop thru. */
1687c478bd9Sstevel@tonic-gate 			} else if (strcmp(param, buffer))
1697c478bd9Sstevel@tonic-gate 				copy = NULL;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 			/* Otherwise, this is our boy. */
1727c478bd9Sstevel@tonic-gate 			else
1737c478bd9Sstevel@tonic-gate 				copy = buffer;
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 
176*d260b2deSny155746 		n = 0;
177*d260b2deSny155746 		quoted = escape = B_FALSE;
178*d260b2deSny155746 		begline = B_TRUE; /* Value's line begins */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		/* Now read the parameter value. */
1817c478bd9Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
1827c478bd9Sstevel@tonic-gate 			ch = (char)c;
183*d260b2deSny155746 
1847c478bd9Sstevel@tonic-gate 			if (begline && ((ch == ' ') || (ch == '\t')))
185*d260b2deSny155746 				continue; /* Ignore leading white space */
186*d260b2deSny155746 
187*d260b2deSny155746 			/*
188*d260b2deSny155746 			 * Take last end quote 'verbatim' if anything
189*d260b2deSny155746 			 * other than space, newline and escape.
190*d260b2deSny155746 			 * Example:
191*d260b2deSny155746 			 * PARAM1="zonename="test-zone""
192*d260b2deSny155746 			 *	Here in this example the letter 't' inside
193*d260b2deSny155746 			 *	the value is followed by '"', this makes
194*d260b2deSny155746 			 *	the previous end quote candidate '"',
195*d260b2deSny155746 			 *	a part of value and the end quote
196*d260b2deSny155746 			 *	disqualfies. Reset check_end_quote.
197*d260b2deSny155746 			 * PARAM2="value"<== newline here
198*d260b2deSny155746 			 * PARAM3="value"\
199*d260b2deSny155746 			 * "continued"<== newline here.
200*d260b2deSny155746 			 *	Check for end quote continues.
201*d260b2deSny155746 			 */
202*d260b2deSny155746 			if (ch != NEWLINE && ch != ' ' && ch != ESCAPE &&
203*d260b2deSny155746 			    ch != '\t' && check_end_quote)
204*d260b2deSny155746 				check_end_quote = B_FALSE;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 			if (ch == NEWLINE) {
207*d260b2deSny155746 				if (!escape) {
208*d260b2deSny155746 					/*
209*d260b2deSny155746 					 * The end quote candidate qualifies.
210*d260b2deSny155746 					 * Eat any trailing spaces.
211*d260b2deSny155746 					 */
212*d260b2deSny155746 					if (check_end_quote) {
213*d260b2deSny155746 						copy -= n - idx;
214*d260b2deSny155746 						n = idx;
215*d260b2deSny155746 						check_end_quote = B_FALSE;
216*d260b2deSny155746 						quoted = B_FALSE;
217*d260b2deSny155746 					}
218*d260b2deSny155746 					break; /* End of entry */
219*d260b2deSny155746 				}
220*d260b2deSny155746 				/*
221*d260b2deSny155746 				 * The end quote if exists, doesn't qualify.
222*d260b2deSny155746 				 * Eat end quote and trailing spaces if any.
223*d260b2deSny155746 				 * Value spans to next line.
224*d260b2deSny155746 				 */
225*d260b2deSny155746 				if (check_end_quote) {
226*d260b2deSny155746 					copy -= n - idx;
227*d260b2deSny155746 					n = idx;
228*d260b2deSny155746 					check_end_quote = B_FALSE;
229*d260b2deSny155746 				} else if (copy) {
230*d260b2deSny155746 					copy--; /* Eat previous esc */
2317c478bd9Sstevel@tonic-gate 					n--;
2327c478bd9Sstevel@tonic-gate 				}
233*d260b2deSny155746 				escape = B_FALSE;
234*d260b2deSny155746 				begline = B_TRUE; /* New input line */
235*d260b2deSny155746 				continue;
2367c478bd9Sstevel@tonic-gate 			} else {
2377c478bd9Sstevel@tonic-gate 				if (!escape && strchr(qset, ch)) {
238*d260b2deSny155746 					/* Handle quotes */
2397c478bd9Sstevel@tonic-gate 					if (begline) {
240*d260b2deSny155746 						/* Starting quote */
241*d260b2deSny155746 						quoted = B_TRUE;
242*d260b2deSny155746 						begline = B_FALSE;
2437c478bd9Sstevel@tonic-gate 						continue;
2447c478bd9Sstevel@tonic-gate 					} else if (quoted) {
245*d260b2deSny155746 						/*
246*d260b2deSny155746 						 * This is the candidate
247*d260b2deSny155746 						 * for end quote. Check
248*d260b2deSny155746 						 * to see it qualifies.
249*d260b2deSny155746 						 */
250*d260b2deSny155746 						check_end_quote = B_TRUE;
251*d260b2deSny155746 						idx = n;
2527c478bd9Sstevel@tonic-gate 					}
2537c478bd9Sstevel@tonic-gate 				}
2547c478bd9Sstevel@tonic-gate 				if (ch == ESCAPE)
255*d260b2deSny155746 					escape = B_TRUE;
2567c478bd9Sstevel@tonic-gate 				else if (escape)
257*d260b2deSny155746 					escape = B_FALSE;
2587c478bd9Sstevel@tonic-gate 				if (copy) *copy++ = ch;
259*d260b2deSny155746 				begline = B_FALSE;
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 			if (copy && ((++n % VALSIZ) == 0)) {
2637c478bd9Sstevel@tonic-gate 				if (mempt) {
2647c478bd9Sstevel@tonic-gate 					mempt = realloc(mempt,
2657c478bd9Sstevel@tonic-gate 					    (n+VALSIZ)*sizeof (char));
2667c478bd9Sstevel@tonic-gate 					if (!mempt)
2677c478bd9Sstevel@tonic-gate 						return (NULL);
2687c478bd9Sstevel@tonic-gate 				} else {
2697c478bd9Sstevel@tonic-gate 					mempt = calloc((size_t)(2*VALSIZ),
2707c478bd9Sstevel@tonic-gate 					    sizeof (char));
2717c478bd9Sstevel@tonic-gate 					if (!mempt)
2727c478bd9Sstevel@tonic-gate 						return (NULL);
2737c478bd9Sstevel@tonic-gate 					(void) strncpy(mempt, buffer, n);
2747c478bd9Sstevel@tonic-gate 				}
2757c478bd9Sstevel@tonic-gate 				copy = &mempt[n];
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		/*
2807c478bd9Sstevel@tonic-gate 		 * Don't allow trailing white space.
2817c478bd9Sstevel@tonic-gate 		 * NOTE : White space in the middle is OK, since this may
2827c478bd9Sstevel@tonic-gate 		 * be a list. At some point it would be a good idea to let
2837c478bd9Sstevel@tonic-gate 		 * this function know how to validate such a list. -- JST
2847c478bd9Sstevel@tonic-gate 		 *
2857c478bd9Sstevel@tonic-gate 		 * Now while there's a parametric value and it ends in a
2867c478bd9Sstevel@tonic-gate 		 * space and the actual remaining string length is still
2877c478bd9Sstevel@tonic-gate 		 * greater than 0, back over the space.
2887c478bd9Sstevel@tonic-gate 		 */
2897c478bd9Sstevel@tonic-gate 		while (copy && isspace((unsigned char)*(copy - 1)) && n-- > 0)
2907c478bd9Sstevel@tonic-gate 			copy--;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		if (quoted) {
2937c478bd9Sstevel@tonic-gate 			if (mempt)
2947c478bd9Sstevel@tonic-gate 				(void) free(mempt);
2957c478bd9Sstevel@tonic-gate 			errno = EFAULT; /* missing closing quote */
2967c478bd9Sstevel@tonic-gate 			return (NULL);
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		if (copy) {
2997c478bd9Sstevel@tonic-gate 			*copy = '\0';
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 		if (c == EOF) {
3037c478bd9Sstevel@tonic-gate 			errno = EINVAL; /* parameter not found */
3047c478bd9Sstevel@tonic-gate 			return (NULL);
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (!mempt)
3097c478bd9Sstevel@tonic-gate 		mempt = strdup(buffer);
3107c478bd9Sstevel@tonic-gate 	else
3117c478bd9Sstevel@tonic-gate 		mempt = realloc(mempt, (strlen(mempt)+1)*sizeof (char));
3127c478bd9Sstevel@tonic-gate 	return (mempt);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate char *
3167c478bd9Sstevel@tonic-gate pkgparam(char *pkg, char *param)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	static char lastfname[PATH_MAX];
3197c478bd9Sstevel@tonic-gate 	static FILE *fp = NULL;
3207c478bd9Sstevel@tonic-gate 	char *pt, *copy, *value, line[PATH_MAX];
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (!pkgdir)
3237c478bd9Sstevel@tonic-gate 		pkgdir = get_PKGLOC();
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (!pkg) {
3267c478bd9Sstevel@tonic-gate 		/* request to close file */
3277c478bd9Sstevel@tonic-gate 		if (fp) {
3287c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
3297c478bd9Sstevel@tonic-gate 			fp = NULL;
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		return (NULL);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (!param) {
3357c478bd9Sstevel@tonic-gate 		errno = ENOENT;
3367c478bd9Sstevel@tonic-gate 		return (NULL);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (pkgfile)
3407c478bd9Sstevel@tonic-gate 		(void) strcpy(line, pkgfile); /* filename was passed */
3417c478bd9Sstevel@tonic-gate 	else
3427c478bd9Sstevel@tonic-gate 		(void) pkginfofind(line, pkgdir, pkg);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (fp && strcmp(line, lastfname)) {
3457c478bd9Sstevel@tonic-gate 		/* different filename implies need for different fp */
3467c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
3477c478bd9Sstevel@tonic-gate 		fp = NULL;
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 	if (!fp) {
3507c478bd9Sstevel@tonic-gate 		(void) strcpy(lastfname, line);
3517c478bd9Sstevel@tonic-gate 		if ((fp = fopen(lastfname, "r")) == NULL)
3527c478bd9Sstevel@tonic-gate 			return (NULL);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * if parameter is a null string, then the user is requesting us
3577c478bd9Sstevel@tonic-gate 	 * to find the value of the next available parameter for this
3587c478bd9Sstevel@tonic-gate 	 * package and to copy the parameter name into the provided string;
3597c478bd9Sstevel@tonic-gate 	 * if it is not, then it is a request for a specified parameter, in
3607c478bd9Sstevel@tonic-gate 	 * which case we rewind the file to start search from beginning
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	if (param[0]) {
3637c478bd9Sstevel@tonic-gate 		/* new parameter request, so reset file position */
3647c478bd9Sstevel@tonic-gate 		if (fseek(fp, 0L, 0))
3657c478bd9Sstevel@tonic-gate 			return (NULL);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (pt = fpkgparam(fp, param)) {
3697c478bd9Sstevel@tonic-gate 		if (strcmp(param, "ARCH") == NULL ||
3707c478bd9Sstevel@tonic-gate 		    strcmp(param, "CATEGORY") == NULL) {
3717c478bd9Sstevel@tonic-gate 			/* remove all whitespace from value */
3727c478bd9Sstevel@tonic-gate 			value = copy = pt;
3737c478bd9Sstevel@tonic-gate 			while (*value) {
3747c478bd9Sstevel@tonic-gate 				if (!isspace((unsigned char)*value))
3757c478bd9Sstevel@tonic-gate 					*copy++ = *value;
3767c478bd9Sstevel@tonic-gate 				value++;
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 			*copy = '\0';
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 		return (pt);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 	return (NULL);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * This routine sets adm_pkgloc and adm_pkgadm which are the
3867c478bd9Sstevel@tonic-gate  * replacement location for PKGLOC and PKGADM.
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate static void canonize_name(char *);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate void
3927c478bd9Sstevel@tonic-gate set_PKGpaths(char *path)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	if (path && *path) {
3957c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s%s", path, PKGLOC);
3967c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s%s", path, PKGOLD);
3977c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s%s", path, PKGADM);
3987c478bd9Sstevel@tonic-gate 		set_install_root(path);
3997c478bd9Sstevel@tonic-gate 	} else {
4007c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s", PKGLOC);
4017c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s", PKGOLD);
4027c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s", PKGADM);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	canonize_name(Adm_pkgloc);
4057c478bd9Sstevel@tonic-gate 	canonize_name(Adm_pkgold);
4067c478bd9Sstevel@tonic-gate 	canonize_name(Adm_pkgadm);
4077c478bd9Sstevel@tonic-gate 	pkgdir = Adm_pkgloc;
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate char *
4117c478bd9Sstevel@tonic-gate get_PKGLOC(void)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	if (Adm_pkgloc[0] == NULL)
4147c478bd9Sstevel@tonic-gate 		return (PKGLOC);
4157c478bd9Sstevel@tonic-gate 	else
4167c478bd9Sstevel@tonic-gate 		return (Adm_pkgloc);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate char *
4207c478bd9Sstevel@tonic-gate get_PKGOLD(void)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	if (Adm_pkgold[0] == NULL)
4237c478bd9Sstevel@tonic-gate 		return (PKGOLD);
4247c478bd9Sstevel@tonic-gate 	else
4257c478bd9Sstevel@tonic-gate 		return (Adm_pkgold);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate char *
4297c478bd9Sstevel@tonic-gate get_PKGADM(void)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	if (Adm_pkgadm[0] == NULL)
4327c478bd9Sstevel@tonic-gate 		return (PKGADM);
4337c478bd9Sstevel@tonic-gate 	else
4347c478bd9Sstevel@tonic-gate 		return (Adm_pkgadm);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate void
4387c478bd9Sstevel@tonic-gate set_PKGADM(char *newpath)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	(void) strcpy(Adm_pkgadm, newpath);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate void
4447c478bd9Sstevel@tonic-gate set_PKGLOC(char *newpath)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	(void) strcpy(Adm_pkgloc, newpath);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate #define	isdot(x)	((x[0] == '.')&&(!x[1]||(x[1] == '/')))
4507c478bd9Sstevel@tonic-gate #define	isdotdot(x)	((x[0] == '.')&&(x[1] == '.')&&(!x[2]||(x[2] == '/')))
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate static void
4537c478bd9Sstevel@tonic-gate canonize_name(char *file)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	char *pt, *last;
4567c478bd9Sstevel@tonic-gate 	int level;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/* Remove references such as "./" and "../" and "//" */
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	for (pt = file; *pt; ) {
4617c478bd9Sstevel@tonic-gate 		if (isdot(pt))
4627c478bd9Sstevel@tonic-gate 			(void) strcpy(pt, pt[1] ? pt+2 : pt+1);
4637c478bd9Sstevel@tonic-gate 		else if (isdotdot(pt)) {
4647c478bd9Sstevel@tonic-gate 			level = 0;
4657c478bd9Sstevel@tonic-gate 			last = pt;
4667c478bd9Sstevel@tonic-gate 			do {
4677c478bd9Sstevel@tonic-gate 				level++;
4687c478bd9Sstevel@tonic-gate 				last += 2;
4697c478bd9Sstevel@tonic-gate 				if (*last)
4707c478bd9Sstevel@tonic-gate 					last++;
4717c478bd9Sstevel@tonic-gate 			} while (isdotdot(last));
4727c478bd9Sstevel@tonic-gate 			--pt; /* point to previous '/' */
4737c478bd9Sstevel@tonic-gate 			while (level--) {
4747c478bd9Sstevel@tonic-gate 				if (pt <= file)
4757c478bd9Sstevel@tonic-gate 					return;
4767c478bd9Sstevel@tonic-gate 				while ((*--pt != '/') && (pt > file))
4777c478bd9Sstevel@tonic-gate 					;
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 			if (*pt == '/')
4807c478bd9Sstevel@tonic-gate 				pt++;
4817c478bd9Sstevel@tonic-gate 			(void) strcpy(pt, last);
4827c478bd9Sstevel@tonic-gate 		} else {
4837c478bd9Sstevel@tonic-gate 			while (*pt && (*pt != '/'))
4847c478bd9Sstevel@tonic-gate 				pt++;
4857c478bd9Sstevel@tonic-gate 			if (*pt == '/') {
4867c478bd9Sstevel@tonic-gate 				while (pt[1] == '/')
4877c478bd9Sstevel@tonic-gate 					(void) strcpy(pt, pt+1);
4887c478bd9Sstevel@tonic-gate 				pt++;
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	if ((--pt > file) && (*pt == '/'))
4937c478bd9Sstevel@tonic-gate 		*pt = '\0';
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate void
4977c478bd9Sstevel@tonic-gate set_install_root(char *path)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	pkg_inst_root = strdup(path);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate char *
5037c478bd9Sstevel@tonic-gate get_install_root()
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	return (pkg_inst_root);
5067c478bd9Sstevel@tonic-gate }
507