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
5d260b2deSny  * Common Development and Distribution License (the "License").
6d260b2deSny  * 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	*/
22*f21450b8SToomas Soome /*	  All Rights Reserved	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
25d260b2deSny  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
264f5c01f9Sns  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
294656d474SGarrett D'Amore /*
304656d474SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
314656d474SGarrett D'Amore  */
324656d474SGarrett 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";
55*f21450b8SToomas Soome 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
pkginfofind(char * path,char * pkg_dir,char * pkginst)797c478bd9Sstevel@tonic-gate pkginfofind(char *path, char *pkg_dir, char *pkginst)
807c478bd9Sstevel@tonic-gate {
814f5c01f9Sns 	int len = 0;
824f5c01f9Sns 
837c478bd9Sstevel@tonic-gate 	/* Construct the temporary pkginfo file name. */
844f5c01f9Sns 	len =  snprintf(path, PATH_MAX, "%s/.save.%s/pkginfo", pkg_dir,
854f5c01f9Sns 	    pkginst);
864f5c01f9Sns 	if (len > PATH_MAX)
874f5c01f9Sns 		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 		 */
934f5c01f9Sns 		len =  snprintf(path, PATH_MAX, "%s/%s/pkginfo", pkg_dir,
944f5c01f9Sns 		    pkginst);
954f5c01f9Sns 		if (len > PATH_MAX)
964f5c01f9Sns 			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 *
pkginfopen(char * pkg_dir,char * pkginst)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 *
fpkgparam(FILE * fp,char * param)1217c478bd9Sstevel@tonic-gate fpkgparam(FILE *fp, char *param)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	char	ch, buffer[VALSIZ];
1247c478bd9Sstevel@tonic-gate 	char	*mempt, *copy;
125d260b2deSny 	int	c, n;
126d260b2deSny 	boolean_t check_end_quote = B_FALSE;
127d260b2deSny 	boolean_t begline, quoted, escape;
128d260b2deSny 	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 
137d260b2deSny 	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) {
1434f5c01f9Sns 			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;
153d260b2deSny 			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 
178d260b2deSny 		n = 0;
179d260b2deSny 		quoted = escape = B_FALSE;
180d260b2deSny 		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) {
1844f5c01f9Sns 			ch = (char)c;
185d260b2deSny 
1867c478bd9Sstevel@tonic-gate 			if (begline && ((ch == ' ') || (ch == '\t')))
187d260b2deSny 				continue; /* Ignore leading white space */
188d260b2deSny 
189d260b2deSny 			/*
190d260b2deSny 			 * Take last end quote 'verbatim' if anything
191d260b2deSny 			 * other than space, newline and escape.
192d260b2deSny 			 * Example:
193d260b2deSny 			 * PARAM1="zonename="test-zone""
194d260b2deSny 			 *	Here in this example the letter 't' inside
195d260b2deSny 			 *	the value is followed by '"', this makes
196d260b2deSny 			 *	the previous end quote candidate '"',
197d260b2deSny 			 *	a part of value and the end quote
198d260b2deSny 			 *	disqualfies. Reset check_end_quote.
199d260b2deSny 			 * PARAM2="value"<== newline here
200d260b2deSny 			 * PARAM3="value"\
201d260b2deSny 			 * "continued"<== newline here.
202d260b2deSny 			 *	Check for end quote continues.
203d260b2deSny 			 */
204d260b2deSny 			if (ch != NEWLINE && ch != ' ' && ch != ESCAPE &&
205d260b2deSny 			    ch != '\t' && check_end_quote)
206d260b2deSny 				check_end_quote = B_FALSE;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 			if (ch == NEWLINE) {
209d260b2deSny 				if (!escape) {
210d260b2deSny 					/*
211d260b2deSny 					 * The end quote candidate qualifies.
212d260b2deSny 					 * Eat any trailing spaces.
213d260b2deSny 					 */
214d260b2deSny 					if (check_end_quote) {
215d260b2deSny 						copy -= n - idx;
216d260b2deSny 						n = idx;
217d260b2deSny 						check_end_quote = B_FALSE;
218d260b2deSny 						quoted = B_FALSE;
2197c478bd9Sstevel@tonic-gate 					}
220d260b2deSny 					break; /* End of entry */
2217c478bd9Sstevel@tonic-gate 				}
222d260b2deSny 				/*
223d260b2deSny 				 * The end quote if exists, doesn't qualify.
224d260b2deSny 				 * Eat end quote and trailing spaces if any.
225d260b2deSny 				 * Value spans to next line.
226d260b2deSny 				 */
227d260b2deSny 				if (check_end_quote) {
228d260b2deSny 					copy -= n - idx;
229d260b2deSny 					n = idx;
230d260b2deSny 					check_end_quote = B_FALSE;
231d260b2deSny 				} else if (copy) {
232d260b2deSny 					copy--; /* Eat previous esc */
233d260b2deSny 					n--;
234d260b2deSny 				}
235d260b2deSny 				escape = B_FALSE;
236d260b2deSny 				begline = B_TRUE; /* New input line */
237d260b2deSny 				continue;
2387c478bd9Sstevel@tonic-gate 			} else {
2397c478bd9Sstevel@tonic-gate 				if (!escape && strchr(qset, ch)) {
240d260b2deSny 					/* Handle quotes */
2417c478bd9Sstevel@tonic-gate 					if (begline) {
242d260b2deSny 						/* Starting quote */
243d260b2deSny 						quoted = B_TRUE;
244d260b2deSny 						begline = B_FALSE;
2457c478bd9Sstevel@tonic-gate 						continue;
2467c478bd9Sstevel@tonic-gate 					} else if (quoted) {
247d260b2deSny 						/*
248d260b2deSny 						 * This is the candidate
249d260b2deSny 						 * for end quote. Check
250d260b2deSny 						 * to see it qualifies.
251d260b2deSny 						 */
252d260b2deSny 						check_end_quote = B_TRUE;
253d260b2deSny 						idx = n;
2547c478bd9Sstevel@tonic-gate 					}
2557c478bd9Sstevel@tonic-gate 				}
2567c478bd9Sstevel@tonic-gate 				if (ch == ESCAPE)
257d260b2deSny 					escape = B_TRUE;
2587c478bd9Sstevel@tonic-gate 				else if (escape)
259d260b2deSny 					escape = B_FALSE;
2607c478bd9Sstevel@tonic-gate 				if (copy) *copy++ = ch;
261d260b2deSny 				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,
267d260b2deSny 					    (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 *
pkgparam(char * pkg,char * param)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)) {
371*f21450b8SToomas Soome 		if (strcmp(param, "ARCH") == 0 ||
372*f21450b8SToomas Soome 		    strcmp(param, "CATEGORY") == 0) {
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
set_PKGpaths(char * path)3947c478bd9Sstevel@tonic-gate set_PKGpaths(char *path)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	if (path && *path) {
3974656d474SGarrett D'Amore 		(void) snprintf(Adm_pkgloc, sizeof (Adm_pkgloc),
3984656d474SGarrett D'Amore 		    "%s%s", path, PKGLOC);
3994656d474SGarrett D'Amore 		(void) snprintf(Adm_pkgadm, sizeof (Adm_pkgadm),
4004656d474SGarrett D'Amore 		    "%s%s", path, PKGADM);
4017c478bd9Sstevel@tonic-gate 		set_install_root(path);
4027c478bd9Sstevel@tonic-gate 	} else {
4034656d474SGarrett D'Amore 		(void) snprintf(Adm_pkgloc, sizeof (Adm_pkgloc), "%s", PKGLOC);
4044656d474SGarrett 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 *
get_PKGLOC(void)4127c478bd9Sstevel@tonic-gate get_PKGLOC(void)
4137c478bd9Sstevel@tonic-gate {
414*f21450b8SToomas Soome 	if (Adm_pkgloc[0] == '\0')
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 *
get_PKGADM(void)4217c478bd9Sstevel@tonic-gate get_PKGADM(void)
4227c478bd9Sstevel@tonic-gate {
423*f21450b8SToomas Soome 	if (Adm_pkgadm[0] == '\0')
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
set_PKGADM(char * newpath)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
set_PKGLOC(char * newpath)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
canonize_name(char * file)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
set_install_root(char * path)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 *
get_install_root()4957c478bd9Sstevel@tonic-gate get_install_root()
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	return (pkg_inst_root);
4987c478bd9Sstevel@tonic-gate }
499