xref: /titanic_50/usr/src/lib/libzonecfg/common/getzoneent.c (revision eb8bc87559eb2a3f971b8ebb3eff91ca35ae6093)
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
5*eb8bc875Scarlsonj  * Common Development and Distribution License (the "License").
6*eb8bc875Scarlsonj  * 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 /*
226be356c5Sgz161490  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This module contains functions used for reading and writing the index file.
307c478bd9Sstevel@tonic-gate  * setzoneent() opens the file.  getzoneent() parses the file, doing the usual
317c478bd9Sstevel@tonic-gate  * skipping of comment lines, etc., and using gettok() to deal with the ":"
327c478bd9Sstevel@tonic-gate  * delimiters.  endzoneent() closes the file.  putzoneent() updates the file,
337c478bd9Sstevel@tonic-gate  * adding, deleting or modifying lines, locking and unlocking appropriately.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <assert.h>
44108322fbScarlsonj #include <uuid/uuid.h>
457c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	ZONE_CONFIG_ROOT "/zonecfg.XXXXXX"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * gettok() is a helper function for parsing the index file, used to split
527c478bd9Sstevel@tonic-gate  * the lines by their ":" delimiters.  Note that an entry may contain a ":"
537c478bd9Sstevel@tonic-gate  * inside double quotes; this should only affect the zonepath, as zone names
547c478bd9Sstevel@tonic-gate  * do not allow such characters, and zone states do not have them either.
557c478bd9Sstevel@tonic-gate  * Same with double-quotes themselves: they are not allowed in zone names,
567c478bd9Sstevel@tonic-gate  * and do not occur in zone states, and in theory should never occur in a
577c478bd9Sstevel@tonic-gate  * zonepath since zonecfg does not support a method for escaping them.
58108322fbScarlsonj  *
59108322fbScarlsonj  * It never returns NULL.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static char *
637c478bd9Sstevel@tonic-gate gettok(char **cpp)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	char *cp = *cpp, *retv;
667c478bd9Sstevel@tonic-gate 	boolean_t quoted = B_FALSE;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (cp == NULL)
697c478bd9Sstevel@tonic-gate 		return ("");
707c478bd9Sstevel@tonic-gate 	if (*cp == '"') {
717c478bd9Sstevel@tonic-gate 		quoted = B_TRUE;
727c478bd9Sstevel@tonic-gate 		cp++;
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	retv = cp;
757c478bd9Sstevel@tonic-gate 	if (quoted) {
767c478bd9Sstevel@tonic-gate 		while (*cp != '\0' && *cp != '"')
777c478bd9Sstevel@tonic-gate 			cp++;
787c478bd9Sstevel@tonic-gate 		if (*cp == '"')
797c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	while (*cp != '\0' && *cp != ':')
827c478bd9Sstevel@tonic-gate 		cp++;
837c478bd9Sstevel@tonic-gate 	if (*cp == '\0') {
847c478bd9Sstevel@tonic-gate 		*cpp = NULL;
857c478bd9Sstevel@tonic-gate 	} else {
867c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
877c478bd9Sstevel@tonic-gate 		*cpp = cp;
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	return (retv);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate char *
937c478bd9Sstevel@tonic-gate getzoneent(FILE *cookie)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
967c478bd9Sstevel@tonic-gate 	char *name;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if ((ze = getzoneent_private(cookie)) == NULL)
997c478bd9Sstevel@tonic-gate 		return (NULL);
1007c478bd9Sstevel@tonic-gate 	name = strdup(ze->zone_name);
1017c478bd9Sstevel@tonic-gate 	free(ze);
1027c478bd9Sstevel@tonic-gate 	return (name);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate struct zoneent *
1067c478bd9Sstevel@tonic-gate getzoneent_private(FILE *cookie)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	char *cp, buf[MAX_INDEX_LEN], *p;
1097c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (cookie == NULL)
1127c478bd9Sstevel@tonic-gate 		return (NULL);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if ((ze = malloc(sizeof (struct zoneent))) == NULL)
1157c478bd9Sstevel@tonic-gate 		return (NULL);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	for (;;) {
1187c478bd9Sstevel@tonic-gate 		if (fgets(buf, sizeof (buf), cookie) == NULL) {
1197c478bd9Sstevel@tonic-gate 			free(ze);
1207c478bd9Sstevel@tonic-gate 			return (NULL);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 		if ((cp = strpbrk(buf, "\r\n")) == NULL) {
1237c478bd9Sstevel@tonic-gate 			/* this represents a line that's too long */
1247c478bd9Sstevel@tonic-gate 			continue;
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		*cp = '\0';
1277c478bd9Sstevel@tonic-gate 		cp = buf;
1287c478bd9Sstevel@tonic-gate 		if (*cp == '#') {
1297c478bd9Sstevel@tonic-gate 			/* skip comment lines */
1307c478bd9Sstevel@tonic-gate 			continue;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		p = gettok(&cp);
133108322fbScarlsonj 		if (*p == '\0' || strlen(p) >= ZONENAME_MAX) {
1347c478bd9Sstevel@tonic-gate 			/*
1357c478bd9Sstevel@tonic-gate 			 * empty or very long zone names are not allowed
1367c478bd9Sstevel@tonic-gate 			 */
1377c478bd9Sstevel@tonic-gate 			continue;
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 		(void) strlcpy(ze->zone_name, p, ZONENAME_MAX);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		p = gettok(&cp);
142108322fbScarlsonj 		if (*p == '\0') {
1437c478bd9Sstevel@tonic-gate 			/* state field should not be empty */
1447c478bd9Sstevel@tonic-gate 			continue;
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 		errno = 0;
1477c478bd9Sstevel@tonic-gate 		if (strcmp(p, ZONE_STATE_STR_CONFIGURED) == 0) {
1487c478bd9Sstevel@tonic-gate 			ze->zone_state = ZONE_STATE_CONFIGURED;
1497c478bd9Sstevel@tonic-gate 		} else if (strcmp(p, ZONE_STATE_STR_INCOMPLETE) == 0) {
1507c478bd9Sstevel@tonic-gate 			ze->zone_state = ZONE_STATE_INCOMPLETE;
1517c478bd9Sstevel@tonic-gate 		} else if (strcmp(p, ZONE_STATE_STR_INSTALLED) == 0) {
1527c478bd9Sstevel@tonic-gate 			ze->zone_state = ZONE_STATE_INSTALLED;
153108322fbScarlsonj 		} else {
1547c478bd9Sstevel@tonic-gate 			continue;
155108322fbScarlsonj 		}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		p = gettok(&cp);
158108322fbScarlsonj 		if (strlen(p) >= MAXPATHLEN) {
1597c478bd9Sstevel@tonic-gate 			/* very long paths are not allowed */
1607c478bd9Sstevel@tonic-gate 			continue;
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 		(void) strlcpy(ze->zone_path, p, MAXPATHLEN);
1637c478bd9Sstevel@tonic-gate 
164108322fbScarlsonj 		p = gettok(&cp);
165108322fbScarlsonj 		if (uuid_parse(p, ze->zone_uuid) == -1)
166108322fbScarlsonj 			uuid_clear(ze->zone_uuid);
167108322fbScarlsonj 
1687c478bd9Sstevel@tonic-gate 		break;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	return (ze);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
174108322fbScarlsonj static boolean_t
175108322fbScarlsonj get_index_path(char *path)
176108322fbScarlsonj {
177108322fbScarlsonj 	return (snprintf(path, MAXPATHLEN, "%s%s", zonecfg_root,
178108322fbScarlsonj 	    ZONE_INDEX_FILE) < MAXPATHLEN);
179108322fbScarlsonj }
180108322fbScarlsonj 
1817c478bd9Sstevel@tonic-gate FILE *
1827c478bd9Sstevel@tonic-gate setzoneent(void)
1837c478bd9Sstevel@tonic-gate {
184108322fbScarlsonj 	char path[MAXPATHLEN];
185108322fbScarlsonj 
186108322fbScarlsonj 	if (!get_index_path(path)) {
187108322fbScarlsonj 		errno = EINVAL;
188108322fbScarlsonj 		return (NULL);
189108322fbScarlsonj 	}
190108322fbScarlsonj 	return (fopen(path, "r"));
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate void
1947c478bd9Sstevel@tonic-gate endzoneent(FILE *cookie)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	if (cookie != NULL)
1977c478bd9Sstevel@tonic-gate 		(void) fclose(cookie);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int
201108322fbScarlsonj lock_index_file(void)
2027c478bd9Sstevel@tonic-gate {
203108322fbScarlsonj 	int lock_fd;
2047c478bd9Sstevel@tonic-gate 	struct flock lock;
205108322fbScarlsonj 	char path[MAXPATHLEN];
2067c478bd9Sstevel@tonic-gate 
207108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
208108322fbScarlsonj 	    ZONE_INDEX_LOCK_DIR) >= sizeof (path))
209108322fbScarlsonj 		return (-1);
210108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && errno != EEXIST)
211108322fbScarlsonj 		return (-1);
212108322fbScarlsonj 	if (strlcat(path, ZONE_INDEX_LOCK_FILE, sizeof (path)) >=
213108322fbScarlsonj 	    sizeof (path))
214108322fbScarlsonj 		return (-1);
215108322fbScarlsonj 	lock_fd = open(path, O_CREAT|O_RDWR, 0644);
216108322fbScarlsonj 	if (lock_fd == -1)
217108322fbScarlsonj 		return (-1);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
2207c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
2217c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
2227c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
2237c478bd9Sstevel@tonic-gate 
224108322fbScarlsonj 	if (fcntl(lock_fd, F_SETLKW, &lock) == -1) {
225108322fbScarlsonj 		(void) close(lock_fd);
226108322fbScarlsonj 		return (-1);
227108322fbScarlsonj 	}
2287c478bd9Sstevel@tonic-gate 
229108322fbScarlsonj 	return (lock_fd);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static int
2337c478bd9Sstevel@tonic-gate unlock_index_file(int lock_fd)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	struct flock lock;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	lock.l_type = F_UNLCK;
2387c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
2397c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
2407c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (fcntl(lock_fd, F_SETLK, &lock) == -1)
2437c478bd9Sstevel@tonic-gate 		return (Z_UNLOCKING_FILE);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (close(lock_fd) == -1)
2467c478bd9Sstevel@tonic-gate 		return (Z_UNLOCKING_FILE);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (Z_OK);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * This function adds or removes a zone name et al. to the index file.
2537c478bd9Sstevel@tonic-gate  *
2547c478bd9Sstevel@tonic-gate  * If ze->zone_state is < 0, it means leave the
2557c478bd9Sstevel@tonic-gate  * existing value unchanged; this is only meaningful when operation ==
256087719fdSdp  * PZE_MODIFY (i.e., it's bad on PZE_ADD and a no-op on PZE_REMOVE).
2577c478bd9Sstevel@tonic-gate  *
258087719fdSdp  * A zero-length ze->zone_path means leave the existing value
2597c478bd9Sstevel@tonic-gate  * unchanged; this is only meaningful when operation == PZE_MODIFY
260087719fdSdp  * (i.e., it's bad on PZE_ADD and a no-op on PZE_REMOVE).
261087719fdSdp  *
262087719fdSdp  * A zero-length ze->zone_newname means leave the existing name
263087719fdSdp  * unchanged; otherwise the zone is renamed to zone_newname.  This is
264087719fdSdp  * only meaningful when operation == PZE_MODIFY.
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * Locking and unlocking is done via the functions above.
2677c478bd9Sstevel@tonic-gate  * The file itself is not modified in place; rather, a copy is made which
2687c478bd9Sstevel@tonic-gate  * is modified, then the copy is atomically renamed back to the main file.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate int
2717c478bd9Sstevel@tonic-gate putzoneent(struct zoneent *ze, zoneent_op_t operation)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	FILE *index_file, *tmp_file;
274*eb8bc875Scarlsonj 	char *tmp_file_name, buf[MAX_INDEX_LEN];
2757c478bd9Sstevel@tonic-gate 	int tmp_file_desc, lock_fd, err;
276*eb8bc875Scarlsonj 	boolean_t exist, need_quotes;
277*eb8bc875Scarlsonj 	char *cp;
278108322fbScarlsonj 	char path[MAXPATHLEN];
2796be356c5Sgz161490 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
280*eb8bc875Scarlsonj 	size_t tlen, namelen;
281*eb8bc875Scarlsonj 	const char *zone_name, *zone_state, *zone_path, *zone_uuid;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	assert(ze != NULL);
2847c478bd9Sstevel@tonic-gate 	if (operation == PZE_ADD &&
2857c478bd9Sstevel@tonic-gate 	    (ze->zone_state < 0 || strlen(ze->zone_path) == 0))
2867c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
287087719fdSdp 
288087719fdSdp 	if (operation != PZE_MODIFY && strlen(ze->zone_newname) != 0)
289087719fdSdp 		return (Z_INVAL);
290087719fdSdp 
291108322fbScarlsonj 	if ((lock_fd = lock_index_file()) == -1)
292108322fbScarlsonj 		return (Z_LOCKING_FILE);
293108322fbScarlsonj 
294108322fbScarlsonj 	/* using sizeof gives us room for the terminating NUL byte as well */
295108322fbScarlsonj 	tlen = sizeof (_PATH_TMPFILE) + strlen(zonecfg_root);
296108322fbScarlsonj 	tmp_file_name = malloc(tlen);
2977c478bd9Sstevel@tonic-gate 	if (tmp_file_name == NULL) {
2987c478bd9Sstevel@tonic-gate 		(void) unlock_index_file(lock_fd);
2997c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
3007c478bd9Sstevel@tonic-gate 	}
301108322fbScarlsonj 	(void) snprintf(tmp_file_name, tlen, "%s%s", zonecfg_root,
302108322fbScarlsonj 	    _PATH_TMPFILE);
303108322fbScarlsonj 
3047c478bd9Sstevel@tonic-gate 	tmp_file_desc = mkstemp(tmp_file_name);
3057c478bd9Sstevel@tonic-gate 	if (tmp_file_desc == -1) {
3067c478bd9Sstevel@tonic-gate 		(void) unlink(tmp_file_name);
3077c478bd9Sstevel@tonic-gate 		free(tmp_file_name);
3087c478bd9Sstevel@tonic-gate 		(void) unlock_index_file(lock_fd);
3097c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	if ((tmp_file = fdopen(tmp_file_desc, "w")) == NULL) {
3127c478bd9Sstevel@tonic-gate 		(void) close(tmp_file_desc);
313108322fbScarlsonj 		err = Z_MISC_FS;
314108322fbScarlsonj 		goto error;
3157c478bd9Sstevel@tonic-gate 	}
316108322fbScarlsonj 	if (!get_index_path(path)) {
317108322fbScarlsonj 		err = Z_MISC_FS;
318108322fbScarlsonj 		goto error;
319108322fbScarlsonj 	}
320108322fbScarlsonj 	if ((index_file = fopen(path, "r")) == NULL) {
321108322fbScarlsonj 		err = Z_MISC_FS;
322108322fbScarlsonj 		goto error;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
325*eb8bc875Scarlsonj 	exist = B_FALSE;
326*eb8bc875Scarlsonj 	zone_name = ze->zone_name;
327*eb8bc875Scarlsonj 	namelen = strlen(zone_name);
3287c478bd9Sstevel@tonic-gate 	for (;;) {
3297c478bd9Sstevel@tonic-gate 		if (fgets(buf, sizeof (buf), index_file) == NULL) {
330*eb8bc875Scarlsonj 			if (operation == PZE_ADD && !exist) {
331*eb8bc875Scarlsonj 				zone_state = zone_state_str(ze->zone_state);
332*eb8bc875Scarlsonj 				zone_path = ze->zone_path;
333*eb8bc875Scarlsonj 				zone_uuid = "";
334*eb8bc875Scarlsonj 				goto add_entry;
335*eb8bc875Scarlsonj 			}
336*eb8bc875Scarlsonj 			/*
337*eb8bc875Scarlsonj 			 * It's not considered an error to delete something
338*eb8bc875Scarlsonj 			 * that doesn't exist, but we can't modify a missing
339*eb8bc875Scarlsonj 			 * record.
340*eb8bc875Scarlsonj 			 */
341*eb8bc875Scarlsonj 			if (operation == PZE_MODIFY && !exist) {
342*eb8bc875Scarlsonj 				err = Z_UPDATING_INDEX;
343*eb8bc875Scarlsonj 				goto error;
344*eb8bc875Scarlsonj 			}
3457c478bd9Sstevel@tonic-gate 			break;
3467c478bd9Sstevel@tonic-gate 		}
347*eb8bc875Scarlsonj 
348*eb8bc875Scarlsonj 		if (buf[0] == '#') {
349*eb8bc875Scarlsonj 			/* skip and preserve comment lines */
350*eb8bc875Scarlsonj 			(void) fputs(buf, tmp_file);
351*eb8bc875Scarlsonj 			continue;
352*eb8bc875Scarlsonj 		}
353*eb8bc875Scarlsonj 
354*eb8bc875Scarlsonj 		if (strncmp(buf, zone_name, namelen) != 0 ||
355*eb8bc875Scarlsonj 		    buf[namelen] != ':') {
356*eb8bc875Scarlsonj 			/* skip and preserve non-target lines */
357*eb8bc875Scarlsonj 			(void) fputs(buf, tmp_file);
358*eb8bc875Scarlsonj 			continue;
359*eb8bc875Scarlsonj 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		if ((cp = strpbrk(buf, "\r\n")) == NULL) {
362*eb8bc875Scarlsonj 			/* this represents a line that's too long; delete */
3637c478bd9Sstevel@tonic-gate 			continue;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		*cp = '\0';
366*eb8bc875Scarlsonj 
3677c478bd9Sstevel@tonic-gate 		/*
368*eb8bc875Scarlsonj 		 * Skip over the zone name.  Because we've already matched the
369*eb8bc875Scarlsonj 		 * target zone (above), we know for certain here that the zone
370*eb8bc875Scarlsonj 		 * name is present and correctly formed.  No need to check.
3717c478bd9Sstevel@tonic-gate 		 */
372*eb8bc875Scarlsonj 		cp = strchr(buf, ':') + 1;
3737c478bd9Sstevel@tonic-gate 
374*eb8bc875Scarlsonj 		zone_state = gettok(&cp);
375*eb8bc875Scarlsonj 		if (*zone_state == '\0') {
3767c478bd9Sstevel@tonic-gate 			/* state field should not be empty */
377108322fbScarlsonj 			err = Z_UPDATING_INDEX;
3787c478bd9Sstevel@tonic-gate 			goto error;
3797c478bd9Sstevel@tonic-gate 		}
380*eb8bc875Scarlsonj 		zone_path = gettok(&cp);
381*eb8bc875Scarlsonj 		zone_uuid = gettok(&cp);
3827c478bd9Sstevel@tonic-gate 
383*eb8bc875Scarlsonj 		switch (operation) {
384*eb8bc875Scarlsonj 		case PZE_ADD:
385*eb8bc875Scarlsonj 			/* can't add same zone */
386*eb8bc875Scarlsonj 			err = Z_UPDATING_INDEX;
387*eb8bc875Scarlsonj 			goto error;
388*eb8bc875Scarlsonj 
389*eb8bc875Scarlsonj 		case PZE_MODIFY:
390*eb8bc875Scarlsonj 			/*
391*eb8bc875Scarlsonj 			 * If the caller specified a new state for the zone,
392*eb8bc875Scarlsonj 			 * then use that.  Otherwise, use the current state.
393*eb8bc875Scarlsonj 			 */
394*eb8bc875Scarlsonj 			if (ze->zone_state >= 0) {
395*eb8bc875Scarlsonj 				zone_state = zone_state_str(ze->zone_state);
3967c478bd9Sstevel@tonic-gate 
397087719fdSdp 				/*
398*eb8bc875Scarlsonj 				 * If the caller is uninstalling this zone,
399*eb8bc875Scarlsonj 				 * then wipe out the uuid.  The zone's contents
400*eb8bc875Scarlsonj 				 * are no longer known.
401087719fdSdp 				 */
402*eb8bc875Scarlsonj 				if (ze->zone_state < ZONE_STATE_INSTALLED)
403*eb8bc875Scarlsonj 					zone_uuid = "";
404*eb8bc875Scarlsonj 			}
405087719fdSdp 
406*eb8bc875Scarlsonj 			/* If a new name is supplied, use it. */
407*eb8bc875Scarlsonj 			if (ze->zone_newname[0] != '\0')
408*eb8bc875Scarlsonj 				zone_name = ze->zone_newname;
409*eb8bc875Scarlsonj 
410*eb8bc875Scarlsonj 			if (ze->zone_path[0] != '\0')
411*eb8bc875Scarlsonj 				zone_path = ze->zone_path;
412*eb8bc875Scarlsonj 			break;
413*eb8bc875Scarlsonj 
414*eb8bc875Scarlsonj 		case PZE_REMOVE:
415*eb8bc875Scarlsonj 		default:
416*eb8bc875Scarlsonj 			continue;
4177c478bd9Sstevel@tonic-gate 		}
418*eb8bc875Scarlsonj 
419*eb8bc875Scarlsonj 	add_entry:
420*eb8bc875Scarlsonj 		/*
421*eb8bc875Scarlsonj 		 * If the entry in the file is in greater than configured
422*eb8bc875Scarlsonj 		 * state, then we must have a UUID.  Make sure that we do.
423*eb8bc875Scarlsonj 		 * (Note that the file entry is only tokenized, not fully
424*eb8bc875Scarlsonj 		 * parsed, so we need to do a string comparison here.)
425*eb8bc875Scarlsonj 		 */
426*eb8bc875Scarlsonj 		if (strcmp(zone_state, ZONE_STATE_STR_CONFIGURED) != 0 &&
427*eb8bc875Scarlsonj 		    *zone_uuid == '\0') {
428*eb8bc875Scarlsonj 			if (uuid_is_null(ze->zone_uuid))
429*eb8bc875Scarlsonj 				uuid_generate(ze->zone_uuid);
430*eb8bc875Scarlsonj 			uuid_unparse(ze->zone_uuid, uuidstr);
431*eb8bc875Scarlsonj 			zone_uuid = uuidstr;
4327c478bd9Sstevel@tonic-gate 		}
433*eb8bc875Scarlsonj 		/*
434*eb8bc875Scarlsonj 		 * We need to quote a path that contains a ":"; this should
435*eb8bc875Scarlsonj 		 * only affect the zonepath, as zone names do not allow such
436*eb8bc875Scarlsonj 		 * characters, and zone states do not have them either.  Same
437*eb8bc875Scarlsonj 		 * with double-quotes themselves: they are not allowed in zone
438*eb8bc875Scarlsonj 		 * names, and do not occur in zone states, and in theory should
439*eb8bc875Scarlsonj 		 * never occur in a zonepath since zonecfg does not support a
440*eb8bc875Scarlsonj 		 * method for escaping them.
441*eb8bc875Scarlsonj 		 */
442*eb8bc875Scarlsonj 		need_quotes = (strchr(zone_path, ':') != NULL);
443*eb8bc875Scarlsonj 		(void) fprintf(tmp_file, "%s:%s:%s%s%s:%s\n", zone_name,
444*eb8bc875Scarlsonj 		    zone_state, need_quotes ? "\"" : "", zone_path,
445*eb8bc875Scarlsonj 		    need_quotes ? "\"" : "", zone_uuid);
446*eb8bc875Scarlsonj 		exist = B_TRUE;
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	(void) fclose(index_file);
450108322fbScarlsonj 	index_file = NULL;
4517c478bd9Sstevel@tonic-gate 	if (fclose(tmp_file) != 0) {
452108322fbScarlsonj 		tmp_file = NULL;
453108322fbScarlsonj 		err = Z_MISC_FS;
454108322fbScarlsonj 		goto error;
4557c478bd9Sstevel@tonic-gate 	}
456108322fbScarlsonj 	tmp_file = NULL;
4577c478bd9Sstevel@tonic-gate 	(void) chmod(tmp_file_name, 0644);
458108322fbScarlsonj 	if (rename(tmp_file_name, path) == -1) {
459108322fbScarlsonj 		err = errno == EACCES ? Z_ACCES : Z_MISC_FS;
460108322fbScarlsonj 		goto error;
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 	free(tmp_file_name);
4637c478bd9Sstevel@tonic-gate 	if (unlock_index_file(lock_fd) != Z_OK)
4647c478bd9Sstevel@tonic-gate 		return (Z_UNLOCKING_FILE);
4657c478bd9Sstevel@tonic-gate 	return (Z_OK);
466108322fbScarlsonj 
4677c478bd9Sstevel@tonic-gate error:
468108322fbScarlsonj 	if (index_file != NULL)
4697c478bd9Sstevel@tonic-gate 		(void) fclose(index_file);
470108322fbScarlsonj 	if (tmp_file != NULL)
4717c478bd9Sstevel@tonic-gate 		(void) fclose(tmp_file);
4727c478bd9Sstevel@tonic-gate 	(void) unlink(tmp_file_name);
4737c478bd9Sstevel@tonic-gate 	free(tmp_file_name);
474108322fbScarlsonj 	(void) unlock_index_file(lock_fd);
475108322fbScarlsonj 	return (err);
4767c478bd9Sstevel@tonic-gate }
477