15c51f124SMoriah Waterland /*
25c51f124SMoriah Waterland  * CDDL HEADER START
35c51f124SMoriah Waterland  *
45c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
55c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
65c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
75c51f124SMoriah Waterland  *
85c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
105c51f124SMoriah Waterland  * See the License for the specific language governing permissions
115c51f124SMoriah Waterland  * and limitations under the License.
125c51f124SMoriah Waterland  *
135c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
145c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
165c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
175c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f124SMoriah Waterland  *
195c51f124SMoriah Waterland  * CDDL HEADER END
205c51f124SMoriah Waterland  */
215c51f124SMoriah Waterland 
225c51f124SMoriah Waterland /*
235c51f124SMoriah Waterland  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
245c51f124SMoriah Waterland  * Use is subject to license terms.
255c51f124SMoriah Waterland  */
265c51f124SMoriah Waterland 
275c51f124SMoriah Waterland 
285c51f124SMoriah Waterland 
295c51f124SMoriah Waterland #include <stdio.h>
305c51f124SMoriah Waterland #include <sys/types.h>
315c51f124SMoriah Waterland #include <sys/param.h>
325c51f124SMoriah Waterland #include <dirent.h>
335c51f124SMoriah Waterland #include <limits.h>
345c51f124SMoriah Waterland #include <errno.h>
355c51f124SMoriah Waterland #include <ctype.h>
365c51f124SMoriah Waterland #include <fcntl.h>
375c51f124SMoriah Waterland #include <stdlib.h>
385c51f124SMoriah Waterland #include <string.h>
395c51f124SMoriah Waterland #include <unistd.h>
405c51f124SMoriah Waterland #include <pkglib.h>
415c51f124SMoriah Waterland #include <libintl.h>
425c51f124SMoriah Waterland #include <libinst.h>
435c51f124SMoriah Waterland #include <install.h>
445c51f124SMoriah Waterland 
455c51f124SMoriah Waterland #define	ERR_NOPKGMAP	"Cannot open pkgmap file."
465c51f124SMoriah Waterland 
475c51f124SMoriah Waterland #define	ENTRY_MAX (PATH_MAX + 38)
485c51f124SMoriah Waterland #define	IGNORE_START	":#!"
495c51f124SMoriah Waterland #define	IGNORE_TYPE	"i"
505c51f124SMoriah Waterland 
515c51f124SMoriah Waterland static int	has_rel_path(char *entry);
525c51f124SMoriah Waterland static int	is_relative(char *entry);
535c51f124SMoriah Waterland 
545c51f124SMoriah Waterland /*
555c51f124SMoriah Waterland  * This routine attempts to determine with certainty whether or not
565c51f124SMoriah Waterland  * the package is relocatable or not. It first attempts to determine if
575c51f124SMoriah Waterland  * there is a reloc directory by scanning pkginstdir. If that fails to
585c51f124SMoriah Waterland  * provide a definite result (pkg is coming from a stream device and
595c51f124SMoriah Waterland  * the directories aren't in place) it inspects the pkgmap in pkginstdir
605c51f124SMoriah Waterland  * in order to determine if the package has relocatable elements. If
615c51f124SMoriah Waterland  * there is a single relative pathname or $BASEDIR/... construct,
625c51f124SMoriah Waterland  * this returns 1. If no relative pathnames are found it returns 0
635c51f124SMoriah Waterland  * meaning absolute package and all the things that implies.
645c51f124SMoriah Waterland  *
655c51f124SMoriah Waterland  * This does not determine the validity of the pkgmap file. If the pkgmap
665c51f124SMoriah Waterland  * is corrupted, this returns 0.
675c51f124SMoriah Waterland  */
685c51f124SMoriah Waterland int
isreloc(char * pkginstdir)695c51f124SMoriah Waterland isreloc(char *pkginstdir)
705c51f124SMoriah Waterland {
715c51f124SMoriah Waterland 	FILE	*pkg_fp;
725c51f124SMoriah Waterland 	struct	dirent *drp;
735c51f124SMoriah Waterland 	DIR	*dirfp;
745c51f124SMoriah Waterland 	int	retcode = 0;
755c51f124SMoriah Waterland 
765c51f124SMoriah Waterland 	/* First look in the directory */
775c51f124SMoriah Waterland 	if ((dirfp = opendir(pkginstdir)) != NULL) {
785c51f124SMoriah Waterland 		while ((drp = readdir(dirfp)) != NULL) {
795c51f124SMoriah Waterland 			if (drp->d_name[0] == '.')
805c51f124SMoriah Waterland 				continue;
815c51f124SMoriah Waterland 			if (strlen(drp->d_name) < (size_t)5)
825c51f124SMoriah Waterland 				continue;
835c51f124SMoriah Waterland 			if (strncmp(drp->d_name, "reloc", 5) == 0) {
845c51f124SMoriah Waterland 				retcode = 1;
855c51f124SMoriah Waterland 				break;
865c51f124SMoriah Waterland 			}
875c51f124SMoriah Waterland 		}
885c51f124SMoriah Waterland 		(void) closedir(dirfp);
895c51f124SMoriah Waterland 	}
905c51f124SMoriah Waterland 
915c51f124SMoriah Waterland 	/*
925c51f124SMoriah Waterland 	 * If retcode == 0, meaning we didn't find a reloc directory then we
935c51f124SMoriah Waterland 	 * probably don't have a complete directory structure available to
945c51f124SMoriah Waterland 	 * us. We'll have to determine what type of package it is by scanning
955c51f124SMoriah Waterland 	 * the pkgmap file.
965c51f124SMoriah Waterland 	 */
975c51f124SMoriah Waterland 	if (retcode == 0) {
985c51f124SMoriah Waterland 		char	path_buffer[ENTRY_MAX];
995c51f124SMoriah Waterland 
1005c51f124SMoriah Waterland 		(void) snprintf(path_buffer, sizeof (path_buffer),
1015c51f124SMoriah Waterland 						"%s/pkgmap", pkginstdir);
1025c51f124SMoriah Waterland 
1035c51f124SMoriah Waterland 		canonize(path_buffer);
1045c51f124SMoriah Waterland 
1055c51f124SMoriah Waterland 		if ((pkg_fp = fopen(path_buffer, "r")) != NULL) {
1065c51f124SMoriah Waterland 			while (fgets(path_buffer, sizeof (path_buffer), pkg_fp))
1075c51f124SMoriah Waterland 				if (has_rel_path(path_buffer)) {
1085c51f124SMoriah Waterland 					retcode = 1;
1095c51f124SMoriah Waterland 					break;
1105c51f124SMoriah Waterland 				}
1115c51f124SMoriah Waterland 			(void) fclose(pkg_fp);
1125c51f124SMoriah Waterland 		} else {
1135c51f124SMoriah Waterland 			progerr(gettext(ERR_NOPKGMAP));
1145c51f124SMoriah Waterland 			quit(99);
1155c51f124SMoriah Waterland 		}
1165c51f124SMoriah Waterland 	}
1175c51f124SMoriah Waterland 
1185c51f124SMoriah Waterland 	return (retcode);
1195c51f124SMoriah Waterland }
1205c51f124SMoriah Waterland 
1215c51f124SMoriah Waterland /*
1225c51f124SMoriah Waterland  * Test the string for the presence of a relative path. If found, return
1235c51f124SMoriah Waterland  * 1 otherwise return 0. If we get past the IGNORE_TYPE test, we're working
1245c51f124SMoriah Waterland  * with a line of the form :
1255c51f124SMoriah Waterland  *
1265c51f124SMoriah Waterland  *	dpart type classname pathname ...
1275c51f124SMoriah Waterland  *
1285c51f124SMoriah Waterland  * It's pathname we're going to test here.
1295c51f124SMoriah Waterland  *
1305c51f124SMoriah Waterland  * Yes, yes, I know about sscanf(); but, I don't need to reserve 4K of
1315c51f124SMoriah Waterland  * space and parse the whole string, I just need to get to two tokens.
1325c51f124SMoriah Waterland  * We're in a hurry.
1335c51f124SMoriah Waterland  */
1345c51f124SMoriah Waterland static int
has_rel_path(char * entry)1355c51f124SMoriah Waterland has_rel_path(char *entry)
1365c51f124SMoriah Waterland {
1375c51f124SMoriah Waterland 	register int entry_pos = 1;
1385c51f124SMoriah Waterland 
1395c51f124SMoriah Waterland 	/* If the line is a comment or special directive, return 0 */
140*c57b7750SToomas Soome 	if (*entry == '\0' || strchr(IGNORE_START, *entry) != NULL)
1415c51f124SMoriah Waterland 		return (0);
1425c51f124SMoriah Waterland 
1435c51f124SMoriah Waterland 	/* Skip past this data entry if it is volume number. */
1445c51f124SMoriah Waterland 	if (isdigit(*entry)) {
1455c51f124SMoriah Waterland 		while (*entry && !isspace(*entry)) {
1465c51f124SMoriah Waterland 			entry++;
1475c51f124SMoriah Waterland 		}
1485c51f124SMoriah Waterland 	}
1495c51f124SMoriah Waterland 
1505c51f124SMoriah Waterland 	/* Skip past this white space */
1515c51f124SMoriah Waterland 	while (*entry && isspace(*entry)) {
1525c51f124SMoriah Waterland 		entry++;
1535c51f124SMoriah Waterland 	}
1545c51f124SMoriah Waterland 
1555c51f124SMoriah Waterland 	/*
1565c51f124SMoriah Waterland 	 * Now we're either pointing at the type or we're pointing at
1575c51f124SMoriah Waterland 	 * the termination of a degenerate entry. If the line is degenerate
1585c51f124SMoriah Waterland 	 * or the type indicates this line should be ignored, we return
1595c51f124SMoriah Waterland 	 * as though not relative.
1605c51f124SMoriah Waterland 	 */
161*c57b7750SToomas Soome 	if (*entry == '\0' || strchr(IGNORE_TYPE, *entry))
1625c51f124SMoriah Waterland 		return (0);
1635c51f124SMoriah Waterland 
1645c51f124SMoriah Waterland 	/* The pathname is in the third position */
1655c51f124SMoriah Waterland 	do {
1665c51f124SMoriah Waterland 		/* Skip past this data entry */
1675c51f124SMoriah Waterland 		while (*entry && !isspace(*entry)) {
1685c51f124SMoriah Waterland 			entry++;
1695c51f124SMoriah Waterland 		}
1705c51f124SMoriah Waterland 
1715c51f124SMoriah Waterland 		/* Skip past this white space and call this the next entry */
1725c51f124SMoriah Waterland 		while (*entry && isspace(*entry)) {
1735c51f124SMoriah Waterland 			entry++;
1745c51f124SMoriah Waterland 		}
175*c57b7750SToomas Soome 	} while (++entry_pos < 3 && *entry != '\0');
1765c51f124SMoriah Waterland 
1775c51f124SMoriah Waterland 	/*
1785c51f124SMoriah Waterland 	 * Now we're pointing at the first character of the pathname.
1795c51f124SMoriah Waterland 	 * If the file is corrupted, we're pointing at NULL. is_relative()
1805c51f124SMoriah Waterland 	 * will return FALSE for NULL which will yield the correct return
1815c51f124SMoriah Waterland 	 * value.
1825c51f124SMoriah Waterland 	 */
1835c51f124SMoriah Waterland 	return (is_relative(entry));
1845c51f124SMoriah Waterland }
1855c51f124SMoriah Waterland 
1865c51f124SMoriah Waterland /*
1875c51f124SMoriah Waterland  * If the path doesn't begin with a variable, the first character in the
1885c51f124SMoriah Waterland  * path is tested for '/' to determine if it is absolute or not. If the
1895c51f124SMoriah Waterland  * path begins with a '$', that variable is resolved if possible. If it
1905c51f124SMoriah Waterland  * isn't defined yet, we exit with error code 1.
1915c51f124SMoriah Waterland  */
1925c51f124SMoriah Waterland static int
is_relative(char * entry)1935c51f124SMoriah Waterland is_relative(char *entry)
1945c51f124SMoriah Waterland {
1955c51f124SMoriah Waterland 	register char *eopath = entry;	/* end of full pathname pointer */
1965c51f124SMoriah Waterland 	register char **lasts = &entry;
1975c51f124SMoriah Waterland 
1985c51f124SMoriah Waterland 	/* If there is a path, test it */
1995c51f124SMoriah Waterland 	if (entry && *entry) {
2005c51f124SMoriah Waterland 		if (*entry == '$') {	/* it's an environment parameter */
2015c51f124SMoriah Waterland 			entry++;	/* skip the '$' */
2025c51f124SMoriah Waterland 
2035c51f124SMoriah Waterland 			while (*eopath && !isspace(*eopath))
2045c51f124SMoriah Waterland 				eopath++;
2055c51f124SMoriah Waterland 
2065c51f124SMoriah Waterland 			*eopath = '\0';	/* terminate the pathname */
2075c51f124SMoriah Waterland 
2085c51f124SMoriah Waterland 			/* isolate the variable */
2095c51f124SMoriah Waterland 			entry = strtok_r(entry, "/", lasts);
2105c51f124SMoriah Waterland 
2115c51f124SMoriah Waterland 			/*
2125c51f124SMoriah Waterland 			 * Some packages call out $BASEDIR for relative
2135c51f124SMoriah Waterland 			 * paths in the pkgmap even though that is
2145c51f124SMoriah Waterland 			 * redundant. This special case is actually
2155c51f124SMoriah Waterland 			 * an indication that this is a relative
2165c51f124SMoriah Waterland 			 * path.
2175c51f124SMoriah Waterland 			 */
2185c51f124SMoriah Waterland 			if (strcmp(entry, "BASEDIR") == 0)
2195c51f124SMoriah Waterland 				return (1);
2205c51f124SMoriah Waterland 			/*
2215c51f124SMoriah Waterland 			 * Since entry is pointing to a now-expendable PATH_MAX
2225c51f124SMoriah Waterland 			 * size buffer, we can expand the path variable into it
2235c51f124SMoriah Waterland 			 * here.
2245c51f124SMoriah Waterland 			 */
2255c51f124SMoriah Waterland 			entry = getenv(entry);
2265c51f124SMoriah Waterland 		}
2275c51f124SMoriah Waterland 
2285c51f124SMoriah Waterland 		/*
2295c51f124SMoriah Waterland 		 * Return type of path. If pathname was unresolvable
2305c51f124SMoriah Waterland 		 * variable, assume relative. This looks like a strange
2315c51f124SMoriah Waterland 		 * assumption since the resolved path may end up
2325c51f124SMoriah Waterland 		 * absolute and pkgadd may prompt the user for a basedir
2335c51f124SMoriah Waterland 		 * incorrectly because of this assumption. Unfortunately,
2345c51f124SMoriah Waterland 		 * the request script MUST have a final BASEDIR in the
2355c51f124SMoriah Waterland 		 * environment before it executes.
2365c51f124SMoriah Waterland 		 */
2375c51f124SMoriah Waterland 		if (entry && *entry)
2385c51f124SMoriah Waterland 			return (RELATIVE(entry));
2395c51f124SMoriah Waterland 		else
2405c51f124SMoriah Waterland 			return (1);
2415c51f124SMoriah Waterland 	} else		/* no path, so we skip it */
2425c51f124SMoriah Waterland 		return (0);
2435c51f124SMoriah Waterland }
244