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