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 /*
23*6e1ae2a3SGary Pennington  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
245c51f124SMoriah Waterland  */
255c51f124SMoriah Waterland 
265c51f124SMoriah Waterland 
275c51f124SMoriah Waterland 
285c51f124SMoriah Waterland /*
295c51f124SMoriah Waterland  * System includes
305c51f124SMoriah Waterland  */
315c51f124SMoriah Waterland 
325c51f124SMoriah Waterland #include <stdio.h>
335c51f124SMoriah Waterland #include <limits.h>
345c51f124SMoriah Waterland #include <stdlib.h>
355c51f124SMoriah Waterland #include <unistd.h>
36af122237SJan Kryl #include <libgen.h>
37af122237SJan Kryl #include <errno.h>
385c51f124SMoriah Waterland #include <string.h>
395c51f124SMoriah Waterland #include <fcntl.h>
405c51f124SMoriah Waterland #include <sys/types.h>
415c51f124SMoriah Waterland #include <sys/stat.h>
425c51f124SMoriah Waterland #include <signal.h>
435c51f124SMoriah Waterland #include <assert.h>
445c51f124SMoriah Waterland #include <locale.h>
455c51f124SMoriah Waterland #include <libintl.h>
465c51f124SMoriah Waterland 
475c51f124SMoriah Waterland /*
485c51f124SMoriah Waterland  * local includes
495c51f124SMoriah Waterland  */
505c51f124SMoriah Waterland 
515c51f124SMoriah Waterland #include "instzones_lib.h"
525c51f124SMoriah Waterland #include "zones_strings.h"
535c51f124SMoriah Waterland 
545c51f124SMoriah Waterland #define	isdot(x)	((x[0] == '.') && (!x[1] || (x[1] == '/')))
555c51f124SMoriah Waterland #define	isdotdot(x)	((x[0] == '.') && (x[1] == '.') && \
565c51f124SMoriah Waterland 		    (!x[2] || (x[2] == '/')))
575c51f124SMoriah Waterland 
585c51f124SMoriah Waterland /*
595c51f124SMoriah Waterland  * *****************************************************************************
605c51f124SMoriah Waterland  * global external (public) functions
615c51f124SMoriah Waterland  * *****************************************************************************
625c51f124SMoriah Waterland  */
635c51f124SMoriah Waterland 
645c51f124SMoriah Waterland /*
655c51f124SMoriah Waterland  * Name:	z_make_zone_root
665c51f124SMoriah Waterland  * Description:	Given its zonepath, generate a string representing the
675c51f124SMoriah Waterland  *              mountpoint of where the root path for a nonglobal zone is
685c51f124SMoriah Waterland  *              mounted.  The zone is mounted using 'zoneadm', which mounts
695c51f124SMoriah Waterland  *              the zone's filesystems wrt <zonepath>/lu/a
705c51f124SMoriah Waterland  * Arguments:	zone_path - non-NULL pointer to string representing zonepath
715c51f124SMoriah Waterland  * Returns:	char *	- pointer to string representing zonepath of zone
725c51f124SMoriah Waterland  *		NULL	- if zone_path is NULL.
735c51f124SMoriah Waterland  * Notes:	The string returned is in static storage and should not be
745c51f124SMoriah Waterland  *              free()ed by the caller.
755c51f124SMoriah Waterland  */
765c51f124SMoriah Waterland char *
z_make_zone_root(char * zone_path)775c51f124SMoriah Waterland z_make_zone_root(char *zone_path)
785c51f124SMoriah Waterland {
795c51f124SMoriah Waterland 	static char	zone_root_buf[MAXPATHLEN];
805c51f124SMoriah Waterland 
815c51f124SMoriah Waterland 	if (zone_path == NULL)
825c51f124SMoriah Waterland 		return (NULL);
835c51f124SMoriah Waterland 
845c51f124SMoriah Waterland 	(void) snprintf(zone_root_buf, MAXPATHLEN, "%s%slu/a", zone_path,
855c51f124SMoriah Waterland 	    (zone_path[0] != '\0' &&
865c51f124SMoriah Waterland 	    zone_path[strlen(zone_path) - 1] == '/') ? "" : "/");
875c51f124SMoriah Waterland 
885c51f124SMoriah Waterland 	return (zone_root_buf);
895c51f124SMoriah Waterland }
905c51f124SMoriah Waterland 
915c51f124SMoriah Waterland void
z_path_canonize(char * a_file)925c51f124SMoriah Waterland z_path_canonize(char *a_file)
935c51f124SMoriah Waterland {
945c51f124SMoriah Waterland 	char	*pt;
955c51f124SMoriah Waterland 	char	*last;
965c51f124SMoriah Waterland 	int	level;
975c51f124SMoriah Waterland 
985c51f124SMoriah Waterland 	/* remove references such as "./" and "../" and "//" */
995c51f124SMoriah Waterland 	for (pt = a_file; *pt; /* void */) {
1005c51f124SMoriah Waterland 		if (isdot(pt)) {
1015c51f124SMoriah Waterland 			(void) strcpy(pt, pt[1] ? pt+2 : pt+1);
1025c51f124SMoriah Waterland 		} else if (isdotdot(pt)) {
1035c51f124SMoriah Waterland 			level = 0;
1045c51f124SMoriah Waterland 			last = pt;
1055c51f124SMoriah Waterland 			do {
1065c51f124SMoriah Waterland 				level++;
1075c51f124SMoriah Waterland 				last += 2;
1085c51f124SMoriah Waterland 				if (*last) {
1095c51f124SMoriah Waterland 					last++;
1105c51f124SMoriah Waterland 				}
1115c51f124SMoriah Waterland 			} while (isdotdot(last));
1125c51f124SMoriah Waterland 			--pt; /* point to previous '/' */
1135c51f124SMoriah Waterland 			while (level--) {
1145c51f124SMoriah Waterland 				if (pt <= a_file) {
1155c51f124SMoriah Waterland 					return;
1165c51f124SMoriah Waterland 				}
1175c51f124SMoriah Waterland 				while ((*--pt != '/') && (pt > a_file))
1185c51f124SMoriah Waterland 					;
1195c51f124SMoriah Waterland 			}
1205c51f124SMoriah Waterland 			if (*pt == '/') {
1215c51f124SMoriah Waterland 				pt++;
1225c51f124SMoriah Waterland 			}
1235c51f124SMoriah Waterland 			(void) strcpy(pt, last);
1245c51f124SMoriah Waterland 		} else {
1255c51f124SMoriah Waterland 			while (*pt && (*pt != '/')) {
1265c51f124SMoriah Waterland 				pt++;
1275c51f124SMoriah Waterland 			}
1285c51f124SMoriah Waterland 			if (*pt == '/') {
1295c51f124SMoriah Waterland 				while (pt[1] == '/') {
1305c51f124SMoriah Waterland 					(void) strcpy(pt, pt+1);
1315c51f124SMoriah Waterland 				}
1325c51f124SMoriah Waterland 				pt++;
1335c51f124SMoriah Waterland 			}
1345c51f124SMoriah Waterland 		}
1355c51f124SMoriah Waterland 	}
1365c51f124SMoriah Waterland 
1375c51f124SMoriah Waterland 	if ((--pt > a_file) && (*pt == '/')) {
1385c51f124SMoriah Waterland 		*pt = '\0';
1395c51f124SMoriah Waterland 	}
1405c51f124SMoriah Waterland }
1415c51f124SMoriah Waterland 
1425c51f124SMoriah Waterland void
z_canoninplace(char * src)1435c51f124SMoriah Waterland z_canoninplace(char *src)
1445c51f124SMoriah Waterland {
1455c51f124SMoriah Waterland 	char *dst;
1465c51f124SMoriah Waterland 	char *src_start;
1475c51f124SMoriah Waterland 
1485c51f124SMoriah Waterland 	/* keep a ptr to the beginning of the src string */
1495c51f124SMoriah Waterland 	src_start = src;
1505c51f124SMoriah Waterland 
1515c51f124SMoriah Waterland 	dst = src;
1525c51f124SMoriah Waterland 	while (*src) {
1535c51f124SMoriah Waterland 		if (*src == '/') {
1545c51f124SMoriah Waterland 			*dst++ = '/';
1555c51f124SMoriah Waterland 			while (*src == '/')
1565c51f124SMoriah Waterland 				src++;
1575c51f124SMoriah Waterland 		} else
1585c51f124SMoriah Waterland 			*dst++ = *src++;
1595c51f124SMoriah Waterland 	}
1605c51f124SMoriah Waterland 
1615c51f124SMoriah Waterland 	/*
1625c51f124SMoriah Waterland 	 * remove any trailing slashes, unless the whole string is just "/".
1635c51f124SMoriah Waterland 	 * If the whole string is "/" (i.e. if the last '/' cahr in dst
1645c51f124SMoriah Waterland 	 * in the beginning of the original string), just terminate it
1655c51f124SMoriah Waterland 	 * and return "/".
1665c51f124SMoriah Waterland 	 */
1675c51f124SMoriah Waterland 	if ((*(dst - 1) == '/') && ((dst - 1) != src_start))
1685c51f124SMoriah Waterland 		dst--;
1695c51f124SMoriah Waterland 	*dst = '\0';
1705c51f124SMoriah Waterland }
171