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
5eb8bc875Scarlsonj  * Common Development and Distribution License (the "License").
6eb8bc875Scarlsonj  * 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 /*
22*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This module contains functions used for reading and writing the index file.
287c478bd9Sstevel@tonic-gate  * setzoneent() opens the file.  getzoneent() parses the file, doing the usual
297c478bd9Sstevel@tonic-gate  * skipping of comment lines, etc., and using gettok() to deal with the ":"
307c478bd9Sstevel@tonic-gate  * delimiters.  endzoneent() closes the file.  putzoneent() updates the file,
317c478bd9Sstevel@tonic-gate  * adding, deleting or modifying lines, locking and unlocking appropriately.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <fcntl.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <assert.h>
42108322fbScarlsonj #include <uuid/uuid.h>
437c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	ZONE_CONFIG_ROOT "/zonecfg.XXXXXX"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * gettok() is a helper function for parsing the index file, used to split
507c478bd9Sstevel@tonic-gate  * the lines by their ":" delimiters.  Note that an entry may contain a ":"
517c478bd9Sstevel@tonic-gate  * inside double quotes; this should only affect the zonepath, as zone names
527c478bd9Sstevel@tonic-gate  * do not allow such characters, and zone states do not have them either.
537c478bd9Sstevel@tonic-gate  * Same with double-quotes themselves: they are not allowed in zone names,
547c478bd9Sstevel@tonic-gate  * and do not occur in zone states, and in theory should never occur in a
557c478bd9Sstevel@tonic-gate  * zonepath since zonecfg does not support a method for escaping them.
56108322fbScarlsonj  *
57108322fbScarlsonj  * It never returns NULL.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static char *
gettok(char ** cpp)617c478bd9Sstevel@tonic-gate gettok(char **cpp)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	char *cp = *cpp, *retv;
647c478bd9Sstevel@tonic-gate 	boolean_t quoted = B_FALSE;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (cp == NULL)
677c478bd9Sstevel@tonic-gate 		return ("");
687c478bd9Sstevel@tonic-gate 	if (*cp == '"') {
697c478bd9Sstevel@tonic-gate 		quoted = B_TRUE;
707c478bd9Sstevel@tonic-gate 		cp++;
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 	retv = cp;
737c478bd9Sstevel@tonic-gate 	if (quoted) {
747c478bd9Sstevel@tonic-gate 		while (*cp != '\0' && *cp != '"')
757c478bd9Sstevel@tonic-gate 			cp++;
767c478bd9Sstevel@tonic-gate 		if (*cp == '"')
777c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	while (*cp != '\0' && *cp != ':')
807c478bd9Sstevel@tonic-gate 		cp++;
817c478bd9Sstevel@tonic-gate 	if (*cp == '\0') {
827c478bd9Sstevel@tonic-gate 		*cpp = NULL;
837c478bd9Sstevel@tonic-gate 	} else {
847c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
857c478bd9Sstevel@tonic-gate 		*cpp = cp;
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	return (retv);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate char *
getzoneent(FILE * cookie)917c478bd9Sstevel@tonic-gate getzoneent(FILE *cookie)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
947c478bd9Sstevel@tonic-gate 	char *name;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if ((ze = getzoneent_private(cookie)) == NULL)
977c478bd9Sstevel@tonic-gate 		return (NULL);
987c478bd9Sstevel@tonic-gate 	name = strdup(ze->zone_name);
997c478bd9Sstevel@tonic-gate 	free(ze);
1007c478bd9Sstevel@tonic-gate 	return (name);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate struct zoneent *
getzoneent_private(FILE * cookie)1047c478bd9Sstevel@tonic-gate getzoneent_private(FILE *cookie)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	char *cp, buf[MAX_INDEX_LEN], *p;
1077c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (cookie == NULL)
1107c478bd9Sstevel@tonic-gate 		return (NULL);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if ((ze = malloc(sizeof (struct zoneent))) == NULL)
1137c478bd9Sstevel@tonic-gate 		return (NULL);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	for (;;) {
1167c478bd9Sstevel@tonic-gate 		if (fgets(buf, sizeof (buf), cookie) == NULL) {
1177c478bd9Sstevel@tonic-gate 			free(ze);
1187c478bd9Sstevel@tonic-gate 			return (NULL);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 		if ((cp = strpbrk(buf, "\r\n")) == NULL) {
1217c478bd9Sstevel@tonic-gate 			/* this represents a line that's too long */
1227c478bd9Sstevel@tonic-gate 			continue;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 		*cp = '\0';
1257c478bd9Sstevel@tonic-gate 		cp = buf;
1267c478bd9Sstevel@tonic-gate 		if (*cp == '#') {
1277c478bd9Sstevel@tonic-gate 			/* skip comment lines */
1287c478bd9Sstevel@tonic-gate 			continue;
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		p = gettok(&cp);
131108322fbScarlsonj 		if (*p == '\0' || strlen(p) >= ZONENAME_MAX) {
1327c478bd9Sstevel@tonic-gate 			/*
1337c478bd9Sstevel@tonic-gate 			 * empty or very long zone names are not allowed
1347c478bd9Sstevel@tonic-gate 			 */
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		(void) strlcpy(ze->zone_name, p, ZONENAME_MAX);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		p = gettok(&cp);
140108322fbScarlsonj 		if (*p == '\0') {
1417c478bd9Sstevel@tonic-gate 			/* state field should not be empty */
1427c478bd9Sstevel@tonic-gate 			continue;
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 		errno = 0;
1457c478bd9Sstevel@tonic-gate 		if (strcmp(p, ZONE_STATE_STR_CONFIGURED) == 0) {
1467c478bd9Sstevel@tonic-gate 			ze->zone_state = ZONE_STATE_CONFIGURED;
1477c478bd9Sstevel@tonic-gate 		} else if (strcmp(p, ZONE_STATE_STR_INCOMPLETE) == 0) {
1487c478bd9Sstevel@tonic-gate 			ze->zone_state = ZONE_STATE_INCOMPLETE;
1497c478bd9Sstevel@tonic-gate 		} else if (strcmp(p, ZONE_STATE_STR_INSTALLED) == 0) {
1507c478bd9Sstevel@tonic-gate 			ze->zone_state = ZONE_STATE_INSTALLED;
151108322fbScarlsonj 		} else {
1527c478bd9Sstevel@tonic-gate 			continue;
153108322fbScarlsonj 		}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		p = gettok(&cp);
156108322fbScarlsonj 		if (strlen(p) >= MAXPATHLEN) {
1577c478bd9Sstevel@tonic-gate 			/* very long paths are not allowed */
1587c478bd9Sstevel@tonic-gate 			continue;
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		(void) strlcpy(ze->zone_path, p, MAXPATHLEN);
1617c478bd9Sstevel@tonic-gate 
162108322fbScarlsonj 		p = gettok(&cp);
163108322fbScarlsonj 		if (uuid_parse(p, ze->zone_uuid) == -1)
164108322fbScarlsonj 			uuid_clear(ze->zone_uuid);
165108322fbScarlsonj 
1667c478bd9Sstevel@tonic-gate 		break;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return (ze);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
172108322fbScarlsonj static boolean_t
get_index_path(char * path)173108322fbScarlsonj get_index_path(char *path)
174108322fbScarlsonj {
175108322fbScarlsonj 	return (snprintf(path, MAXPATHLEN, "%s%s", zonecfg_root,
176108322fbScarlsonj 	    ZONE_INDEX_FILE) < MAXPATHLEN);
177108322fbScarlsonj }
178108322fbScarlsonj 
1797c478bd9Sstevel@tonic-gate FILE *
setzoneent(void)1807c478bd9Sstevel@tonic-gate setzoneent(void)
1817c478bd9Sstevel@tonic-gate {
182108322fbScarlsonj 	char path[MAXPATHLEN];
183108322fbScarlsonj 
184108322fbScarlsonj 	if (!get_index_path(path)) {
185108322fbScarlsonj 		errno = EINVAL;
186108322fbScarlsonj 		return (NULL);
187108322fbScarlsonj 	}
188108322fbScarlsonj 	return (fopen(path, "r"));
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate void
endzoneent(FILE * cookie)1927c478bd9Sstevel@tonic-gate endzoneent(FILE *cookie)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	if (cookie != NULL)
1957c478bd9Sstevel@tonic-gate 		(void) fclose(cookie);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate static int
lock_index_file(void)199108322fbScarlsonj lock_index_file(void)
2007c478bd9Sstevel@tonic-gate {
201108322fbScarlsonj 	int lock_fd;
2027c478bd9Sstevel@tonic-gate 	struct flock lock;
203108322fbScarlsonj 	char path[MAXPATHLEN];
204108322fbScarlsonj 
205108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
206108322fbScarlsonj 	    ZONE_INDEX_LOCK_DIR) >= sizeof (path))
207108322fbScarlsonj 		return (-1);
208108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && errno != EEXIST)
209108322fbScarlsonj 		return (-1);
210108322fbScarlsonj 	if (strlcat(path, ZONE_INDEX_LOCK_FILE, sizeof (path)) >=
211108322fbScarlsonj 	    sizeof (path))
212108322fbScarlsonj 		return (-1);
213108322fbScarlsonj 	lock_fd = open(path, O_CREAT|O_RDWR, 0644);
214108322fbScarlsonj 	if (lock_fd == -1)
215108322fbScarlsonj 		return (-1);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
2187c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
2197c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
2207c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
2217c478bd9Sstevel@tonic-gate 
222108322fbScarlsonj 	if (fcntl(lock_fd, F_SETLKW, &lock) == -1) {
223108322fbScarlsonj 		(void) close(lock_fd);
224108322fbScarlsonj 		return (-1);
225108322fbScarlsonj 	}
2267c478bd9Sstevel@tonic-gate 
227108322fbScarlsonj 	return (lock_fd);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static int
unlock_index_file(int lock_fd)2317c478bd9Sstevel@tonic-gate unlock_index_file(int lock_fd)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	struct flock lock;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	lock.l_type = F_UNLCK;
2367c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
2377c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
2387c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (fcntl(lock_fd, F_SETLK, &lock) == -1)
2417c478bd9Sstevel@tonic-gate 		return (Z_UNLOCKING_FILE);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (close(lock_fd) == -1)
2447c478bd9Sstevel@tonic-gate 		return (Z_UNLOCKING_FILE);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return (Z_OK);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * This function adds or removes a zone name et al. to the index file.
2517c478bd9Sstevel@tonic-gate  *
2527c478bd9Sstevel@tonic-gate  * If ze->zone_state is < 0, it means leave the
2537c478bd9Sstevel@tonic-gate  * existing value unchanged; this is only meaningful when operation ==
254087719fdSdp  * PZE_MODIFY (i.e., it's bad on PZE_ADD and a no-op on PZE_REMOVE).
2557c478bd9Sstevel@tonic-gate  *
256087719fdSdp  * A zero-length ze->zone_path means leave the existing value
2577c478bd9Sstevel@tonic-gate  * unchanged; this is only meaningful when operation == PZE_MODIFY
258087719fdSdp  * (i.e., it's bad on PZE_ADD and a no-op on PZE_REMOVE).
259087719fdSdp  *
260087719fdSdp  * A zero-length ze->zone_newname means leave the existing name
261087719fdSdp  * unchanged; otherwise the zone is renamed to zone_newname.  This is
262087719fdSdp  * only meaningful when operation == PZE_MODIFY.
2637c478bd9Sstevel@tonic-gate  *
2647c478bd9Sstevel@tonic-gate  * Locking and unlocking is done via the functions above.
2657c478bd9Sstevel@tonic-gate  * The file itself is not modified in place; rather, a copy is made which
2667c478bd9Sstevel@tonic-gate  * is modified, then the copy is atomically renamed back to the main file.
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate int
putzoneent(struct zoneent * ze,zoneent_op_t operation)2697c478bd9Sstevel@tonic-gate putzoneent(struct zoneent *ze, zoneent_op_t operation)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	FILE *index_file, *tmp_file;
272eb8bc875Scarlsonj 	char *tmp_file_name, buf[MAX_INDEX_LEN];
2737c478bd9Sstevel@tonic-gate 	int tmp_file_desc, lock_fd, err;
274eb8bc875Scarlsonj 	boolean_t exist, need_quotes;
275eb8bc875Scarlsonj 	char *cp;
276108322fbScarlsonj 	char path[MAXPATHLEN];
2776be356c5Sgz 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
278eb8bc875Scarlsonj 	size_t tlen, namelen;
279eb8bc875Scarlsonj 	const char *zone_name, *zone_state, *zone_path, *zone_uuid;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	assert(ze != NULL);
282*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 
283*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 	/*
284*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 	 * Don't allow modification of Global Zone entry
285*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 	 * in index file
286*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 	 */
287*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 	if ((operation == PZE_MODIFY) &&
288*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 	    (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0)) {
289*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 		return (Z_OK);
290*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 	}
291*810f07c5Sphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 
2927c478bd9Sstevel@tonic-gate 	if (operation == PZE_ADD &&
2937c478bd9Sstevel@tonic-gate 	    (ze->zone_state < 0 || strlen(ze->zone_path) == 0))
2947c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
295087719fdSdp 
296087719fdSdp 	if (operation != PZE_MODIFY && strlen(ze->zone_newname) != 0)
297087719fdSdp 		return (Z_INVAL);
298087719fdSdp 
299108322fbScarlsonj 	if ((lock_fd = lock_index_file()) == -1)
300108322fbScarlsonj 		return (Z_LOCKING_FILE);
301108322fbScarlsonj 
302108322fbScarlsonj 	/* using sizeof gives us room for the terminating NUL byte as well */
303108322fbScarlsonj 	tlen = sizeof (_PATH_TMPFILE) + strlen(zonecfg_root);
304108322fbScarlsonj 	tmp_file_name = malloc(tlen);
3057c478bd9Sstevel@tonic-gate 	if (tmp_file_name == NULL) {
3067c478bd9Sstevel@tonic-gate 		(void) unlock_index_file(lock_fd);
3077c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
3087c478bd9Sstevel@tonic-gate 	}
309108322fbScarlsonj 	(void) snprintf(tmp_file_name, tlen, "%s%s", zonecfg_root,
310108322fbScarlsonj 	    _PATH_TMPFILE);
311108322fbScarlsonj 
3127c478bd9Sstevel@tonic-gate 	tmp_file_desc = mkstemp(tmp_file_name);
3137c478bd9Sstevel@tonic-gate 	if (tmp_file_desc == -1) {
3147c478bd9Sstevel@tonic-gate 		(void) unlink(tmp_file_name);
3157c478bd9Sstevel@tonic-gate 		free(tmp_file_name);
3167c478bd9Sstevel@tonic-gate 		(void) unlock_index_file(lock_fd);
3177c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
3187c478bd9Sstevel@tonic-gate 	}
319555afedfScarlsonj 	(void) fchmod(tmp_file_desc, ZONE_INDEX_MODE);
320555afedfScarlsonj 	(void) fchown(tmp_file_desc, ZONE_INDEX_UID, ZONE_INDEX_GID);
3217c478bd9Sstevel@tonic-gate 	if ((tmp_file = fdopen(tmp_file_desc, "w")) == NULL) {
3227c478bd9Sstevel@tonic-gate 		(void) close(tmp_file_desc);
323108322fbScarlsonj 		err = Z_MISC_FS;
324108322fbScarlsonj 		goto error;
3257c478bd9Sstevel@tonic-gate 	}
326108322fbScarlsonj 	if (!get_index_path(path)) {
327108322fbScarlsonj 		err = Z_MISC_FS;
328108322fbScarlsonj 		goto error;
329108322fbScarlsonj 	}
330108322fbScarlsonj 	if ((index_file = fopen(path, "r")) == NULL) {
331108322fbScarlsonj 		err = Z_MISC_FS;
332108322fbScarlsonj 		goto error;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
335eb8bc875Scarlsonj 	exist = B_FALSE;
336eb8bc875Scarlsonj 	zone_name = ze->zone_name;
337eb8bc875Scarlsonj 	namelen = strlen(zone_name);
3387c478bd9Sstevel@tonic-gate 	for (;;) {
3397c478bd9Sstevel@tonic-gate 		if (fgets(buf, sizeof (buf), index_file) == NULL) {
340eb8bc875Scarlsonj 			if (operation == PZE_ADD && !exist) {
341eb8bc875Scarlsonj 				zone_state = zone_state_str(ze->zone_state);
342eb8bc875Scarlsonj 				zone_path = ze->zone_path;
343eb8bc875Scarlsonj 				zone_uuid = "";
344eb8bc875Scarlsonj 				goto add_entry;
345eb8bc875Scarlsonj 			}
346eb8bc875Scarlsonj 			/*
347eb8bc875Scarlsonj 			 * It's not considered an error to delete something
348eb8bc875Scarlsonj 			 * that doesn't exist, but we can't modify a missing
349eb8bc875Scarlsonj 			 * record.
350eb8bc875Scarlsonj 			 */
351eb8bc875Scarlsonj 			if (operation == PZE_MODIFY && !exist) {
352eb8bc875Scarlsonj 				err = Z_UPDATING_INDEX;
353eb8bc875Scarlsonj 				goto error;
354eb8bc875Scarlsonj 			}
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 
358eb8bc875Scarlsonj 		if (buf[0] == '#') {
359eb8bc875Scarlsonj 			/* skip and preserve comment lines */
360eb8bc875Scarlsonj 			(void) fputs(buf, tmp_file);
3617c478bd9Sstevel@tonic-gate 			continue;
3627c478bd9Sstevel@tonic-gate 		}
363eb8bc875Scarlsonj 
364eb8bc875Scarlsonj 		if (strncmp(buf, zone_name, namelen) != 0 ||
365eb8bc875Scarlsonj 		    buf[namelen] != ':') {
366eb8bc875Scarlsonj 			/* skip and preserve non-target lines */
367eb8bc875Scarlsonj 			(void) fputs(buf, tmp_file);
3687c478bd9Sstevel@tonic-gate 			continue;
3697c478bd9Sstevel@tonic-gate 		}
370eb8bc875Scarlsonj 
371eb8bc875Scarlsonj 		if ((cp = strpbrk(buf, "\r\n")) == NULL) {
372eb8bc875Scarlsonj 			/* this represents a line that's too long; delete */
3737c478bd9Sstevel@tonic-gate 			continue;
3747c478bd9Sstevel@tonic-gate 		}
375eb8bc875Scarlsonj 		*cp = '\0';
3767c478bd9Sstevel@tonic-gate 
377eb8bc875Scarlsonj 		/*
378eb8bc875Scarlsonj 		 * Skip over the zone name.  Because we've already matched the
379eb8bc875Scarlsonj 		 * target zone (above), we know for certain here that the zone
380eb8bc875Scarlsonj 		 * name is present and correctly formed.  No need to check.
381eb8bc875Scarlsonj 		 */
382eb8bc875Scarlsonj 		cp = strchr(buf, ':') + 1;
383eb8bc875Scarlsonj 
384eb8bc875Scarlsonj 		zone_state = gettok(&cp);
385eb8bc875Scarlsonj 		if (*zone_state == '\0') {
386eb8bc875Scarlsonj 			/* state field should not be empty */
387eb8bc875Scarlsonj 			err = Z_UPDATING_INDEX;
388eb8bc875Scarlsonj 			goto error;
389eb8bc875Scarlsonj 		}
390eb8bc875Scarlsonj 		zone_path = gettok(&cp);
391eb8bc875Scarlsonj 		zone_uuid = gettok(&cp);
392eb8bc875Scarlsonj 
393eb8bc875Scarlsonj 		switch (operation) {
394eb8bc875Scarlsonj 		case PZE_ADD:
395eb8bc875Scarlsonj 			/* can't add same zone */
396eb8bc875Scarlsonj 			err = Z_UPDATING_INDEX;
397eb8bc875Scarlsonj 			goto error;
398eb8bc875Scarlsonj 
399eb8bc875Scarlsonj 		case PZE_MODIFY:
400eb8bc875Scarlsonj 			/*
401eb8bc875Scarlsonj 			 * If the caller specified a new state for the zone,
402eb8bc875Scarlsonj 			 * then use that.  Otherwise, use the current state.
403eb8bc875Scarlsonj 			 */
404eb8bc875Scarlsonj 			if (ze->zone_state >= 0) {
405eb8bc875Scarlsonj 				zone_state = zone_state_str(ze->zone_state);
4067c478bd9Sstevel@tonic-gate 
407087719fdSdp 				/*
408eb8bc875Scarlsonj 				 * If the caller is uninstalling this zone,
409eb8bc875Scarlsonj 				 * then wipe out the uuid.  The zone's contents
410eb8bc875Scarlsonj 				 * are no longer known.
411087719fdSdp 				 */
412eb8bc875Scarlsonj 				if (ze->zone_state < ZONE_STATE_INSTALLED)
413eb8bc875Scarlsonj 					zone_uuid = "";
4147c478bd9Sstevel@tonic-gate 			}
415eb8bc875Scarlsonj 
416eb8bc875Scarlsonj 			/* If a new name is supplied, use it. */
417eb8bc875Scarlsonj 			if (ze->zone_newname[0] != '\0')
418eb8bc875Scarlsonj 				zone_name = ze->zone_newname;
419eb8bc875Scarlsonj 
420eb8bc875Scarlsonj 			if (ze->zone_path[0] != '\0')
421eb8bc875Scarlsonj 				zone_path = ze->zone_path;
422eb8bc875Scarlsonj 			break;
423eb8bc875Scarlsonj 
424eb8bc875Scarlsonj 		case PZE_REMOVE:
425eb8bc875Scarlsonj 		default:
426eb8bc875Scarlsonj 			continue;
427eb8bc875Scarlsonj 		}
428eb8bc875Scarlsonj 
429eb8bc875Scarlsonj 	add_entry:
430eb8bc875Scarlsonj 		/*
431eb8bc875Scarlsonj 		 * If the entry in the file is in greater than configured
432eb8bc875Scarlsonj 		 * state, then we must have a UUID.  Make sure that we do.
433eb8bc875Scarlsonj 		 * (Note that the file entry is only tokenized, not fully
434eb8bc875Scarlsonj 		 * parsed, so we need to do a string comparison here.)
435eb8bc875Scarlsonj 		 */
436eb8bc875Scarlsonj 		if (strcmp(zone_state, ZONE_STATE_STR_CONFIGURED) != 0 &&
437eb8bc875Scarlsonj 		    *zone_uuid == '\0') {
438eb8bc875Scarlsonj 			if (uuid_is_null(ze->zone_uuid))
439eb8bc875Scarlsonj 				uuid_generate(ze->zone_uuid);
440eb8bc875Scarlsonj 			uuid_unparse(ze->zone_uuid, uuidstr);
441eb8bc875Scarlsonj 			zone_uuid = uuidstr;
4427c478bd9Sstevel@tonic-gate 		}
443eb8bc875Scarlsonj 		/*
444eb8bc875Scarlsonj 		 * We need to quote a path that contains a ":"; this should
445eb8bc875Scarlsonj 		 * only affect the zonepath, as zone names do not allow such
446eb8bc875Scarlsonj 		 * characters, and zone states do not have them either.  Same
447eb8bc875Scarlsonj 		 * with double-quotes themselves: they are not allowed in zone
448eb8bc875Scarlsonj 		 * names, and do not occur in zone states, and in theory should
449eb8bc875Scarlsonj 		 * never occur in a zonepath since zonecfg does not support a
450eb8bc875Scarlsonj 		 * method for escaping them.
451eb8bc875Scarlsonj 		 */
452eb8bc875Scarlsonj 		need_quotes = (strchr(zone_path, ':') != NULL);
453eb8bc875Scarlsonj 		(void) fprintf(tmp_file, "%s:%s:%s%s%s:%s\n", zone_name,
454eb8bc875Scarlsonj 		    zone_state, need_quotes ? "\"" : "", zone_path,
455eb8bc875Scarlsonj 		    need_quotes ? "\"" : "", zone_uuid);
456eb8bc875Scarlsonj 		exist = B_TRUE;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	(void) fclose(index_file);
460108322fbScarlsonj 	index_file = NULL;
4617c478bd9Sstevel@tonic-gate 	if (fclose(tmp_file) != 0) {
462108322fbScarlsonj 		tmp_file = NULL;
463108322fbScarlsonj 		err = Z_MISC_FS;
464108322fbScarlsonj 		goto error;
4657c478bd9Sstevel@tonic-gate 	}
466108322fbScarlsonj 	tmp_file = NULL;
467108322fbScarlsonj 	if (rename(tmp_file_name, path) == -1) {
468108322fbScarlsonj 		err = errno == EACCES ? Z_ACCES : Z_MISC_FS;
469108322fbScarlsonj 		goto error;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	free(tmp_file_name);
4727c478bd9Sstevel@tonic-gate 	if (unlock_index_file(lock_fd) != Z_OK)
4737c478bd9Sstevel@tonic-gate 		return (Z_UNLOCKING_FILE);
4747c478bd9Sstevel@tonic-gate 	return (Z_OK);
475108322fbScarlsonj 
4767c478bd9Sstevel@tonic-gate error:
477108322fbScarlsonj 	if (index_file != NULL)
478108322fbScarlsonj 		(void) fclose(index_file);
479108322fbScarlsonj 	if (tmp_file != NULL)
480108322fbScarlsonj 		(void) fclose(tmp_file);
4817c478bd9Sstevel@tonic-gate 	(void) unlink(tmp_file_name);
4827c478bd9Sstevel@tonic-gate 	free(tmp_file_name);
483108322fbScarlsonj 	(void) unlock_index_file(lock_fd);
484108322fbScarlsonj 	return (err);
4857c478bd9Sstevel@tonic-gate }
486