xref: /titanic_44/usr/src/lib/libzonecfg/common/getzoneent.c (revision 555afedfc38adf0cc5fbc1de696dc811973eaaca)
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 /*
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;
274eb8bc875Scarlsonj 	char *tmp_file_name, buf[MAX_INDEX_LEN];
2757c478bd9Sstevel@tonic-gate 	int tmp_file_desc, lock_fd, err;
276eb8bc875Scarlsonj 	boolean_t exist, need_quotes;
277eb8bc875Scarlsonj 	char *cp;
278108322fbScarlsonj 	char path[MAXPATHLEN];
2796be356c5Sgz161490 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
280eb8bc875Scarlsonj 	size_t tlen, namelen;
281eb8bc875Scarlsonj 	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 	}
311*555afedfScarlsonj 	(void) fchmod(tmp_file_desc, ZONE_INDEX_MODE);
312*555afedfScarlsonj 	(void) fchown(tmp_file_desc, ZONE_INDEX_UID, ZONE_INDEX_GID);
3137c478bd9Sstevel@tonic-gate 	if ((tmp_file = fdopen(tmp_file_desc, "w")) == NULL) {
3147c478bd9Sstevel@tonic-gate 		(void) close(tmp_file_desc);
315108322fbScarlsonj 		err = Z_MISC_FS;
316108322fbScarlsonj 		goto error;
3177c478bd9Sstevel@tonic-gate 	}
318108322fbScarlsonj 	if (!get_index_path(path)) {
319108322fbScarlsonj 		err = Z_MISC_FS;
320108322fbScarlsonj 		goto error;
321108322fbScarlsonj 	}
322108322fbScarlsonj 	if ((index_file = fopen(path, "r")) == NULL) {
323108322fbScarlsonj 		err = Z_MISC_FS;
324108322fbScarlsonj 		goto error;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
327eb8bc875Scarlsonj 	exist = B_FALSE;
328eb8bc875Scarlsonj 	zone_name = ze->zone_name;
329eb8bc875Scarlsonj 	namelen = strlen(zone_name);
3307c478bd9Sstevel@tonic-gate 	for (;;) {
3317c478bd9Sstevel@tonic-gate 		if (fgets(buf, sizeof (buf), index_file) == NULL) {
332eb8bc875Scarlsonj 			if (operation == PZE_ADD && !exist) {
333eb8bc875Scarlsonj 				zone_state = zone_state_str(ze->zone_state);
334eb8bc875Scarlsonj 				zone_path = ze->zone_path;
335eb8bc875Scarlsonj 				zone_uuid = "";
336eb8bc875Scarlsonj 				goto add_entry;
337eb8bc875Scarlsonj 			}
338eb8bc875Scarlsonj 			/*
339eb8bc875Scarlsonj 			 * It's not considered an error to delete something
340eb8bc875Scarlsonj 			 * that doesn't exist, but we can't modify a missing
341eb8bc875Scarlsonj 			 * record.
342eb8bc875Scarlsonj 			 */
343eb8bc875Scarlsonj 			if (operation == PZE_MODIFY && !exist) {
344eb8bc875Scarlsonj 				err = Z_UPDATING_INDEX;
345eb8bc875Scarlsonj 				goto error;
346eb8bc875Scarlsonj 			}
3477c478bd9Sstevel@tonic-gate 			break;
3487c478bd9Sstevel@tonic-gate 		}
349eb8bc875Scarlsonj 
350eb8bc875Scarlsonj 		if (buf[0] == '#') {
351eb8bc875Scarlsonj 			/* skip and preserve comment lines */
352eb8bc875Scarlsonj 			(void) fputs(buf, tmp_file);
353eb8bc875Scarlsonj 			continue;
354eb8bc875Scarlsonj 		}
355eb8bc875Scarlsonj 
356eb8bc875Scarlsonj 		if (strncmp(buf, zone_name, namelen) != 0 ||
357eb8bc875Scarlsonj 		    buf[namelen] != ':') {
358eb8bc875Scarlsonj 			/* skip and preserve non-target lines */
359eb8bc875Scarlsonj 			(void) fputs(buf, tmp_file);
360eb8bc875Scarlsonj 			continue;
361eb8bc875Scarlsonj 		}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		if ((cp = strpbrk(buf, "\r\n")) == NULL) {
364eb8bc875Scarlsonj 			/* this represents a line that's too long; delete */
3657c478bd9Sstevel@tonic-gate 			continue;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		*cp = '\0';
368eb8bc875Scarlsonj 
3697c478bd9Sstevel@tonic-gate 		/*
370eb8bc875Scarlsonj 		 * Skip over the zone name.  Because we've already matched the
371eb8bc875Scarlsonj 		 * target zone (above), we know for certain here that the zone
372eb8bc875Scarlsonj 		 * name is present and correctly formed.  No need to check.
3737c478bd9Sstevel@tonic-gate 		 */
374eb8bc875Scarlsonj 		cp = strchr(buf, ':') + 1;
3757c478bd9Sstevel@tonic-gate 
376eb8bc875Scarlsonj 		zone_state = gettok(&cp);
377eb8bc875Scarlsonj 		if (*zone_state == '\0') {
3787c478bd9Sstevel@tonic-gate 			/* state field should not be empty */
379108322fbScarlsonj 			err = Z_UPDATING_INDEX;
3807c478bd9Sstevel@tonic-gate 			goto error;
3817c478bd9Sstevel@tonic-gate 		}
382eb8bc875Scarlsonj 		zone_path = gettok(&cp);
383eb8bc875Scarlsonj 		zone_uuid = gettok(&cp);
3847c478bd9Sstevel@tonic-gate 
385eb8bc875Scarlsonj 		switch (operation) {
386eb8bc875Scarlsonj 		case PZE_ADD:
387eb8bc875Scarlsonj 			/* can't add same zone */
388eb8bc875Scarlsonj 			err = Z_UPDATING_INDEX;
389eb8bc875Scarlsonj 			goto error;
390eb8bc875Scarlsonj 
391eb8bc875Scarlsonj 		case PZE_MODIFY:
392eb8bc875Scarlsonj 			/*
393eb8bc875Scarlsonj 			 * If the caller specified a new state for the zone,
394eb8bc875Scarlsonj 			 * then use that.  Otherwise, use the current state.
395eb8bc875Scarlsonj 			 */
396eb8bc875Scarlsonj 			if (ze->zone_state >= 0) {
397eb8bc875Scarlsonj 				zone_state = zone_state_str(ze->zone_state);
3987c478bd9Sstevel@tonic-gate 
399087719fdSdp 				/*
400eb8bc875Scarlsonj 				 * If the caller is uninstalling this zone,
401eb8bc875Scarlsonj 				 * then wipe out the uuid.  The zone's contents
402eb8bc875Scarlsonj 				 * are no longer known.
403087719fdSdp 				 */
404eb8bc875Scarlsonj 				if (ze->zone_state < ZONE_STATE_INSTALLED)
405eb8bc875Scarlsonj 					zone_uuid = "";
406eb8bc875Scarlsonj 			}
407087719fdSdp 
408eb8bc875Scarlsonj 			/* If a new name is supplied, use it. */
409eb8bc875Scarlsonj 			if (ze->zone_newname[0] != '\0')
410eb8bc875Scarlsonj 				zone_name = ze->zone_newname;
411eb8bc875Scarlsonj 
412eb8bc875Scarlsonj 			if (ze->zone_path[0] != '\0')
413eb8bc875Scarlsonj 				zone_path = ze->zone_path;
414eb8bc875Scarlsonj 			break;
415eb8bc875Scarlsonj 
416eb8bc875Scarlsonj 		case PZE_REMOVE:
417eb8bc875Scarlsonj 		default:
418eb8bc875Scarlsonj 			continue;
4197c478bd9Sstevel@tonic-gate 		}
420eb8bc875Scarlsonj 
421eb8bc875Scarlsonj 	add_entry:
422eb8bc875Scarlsonj 		/*
423eb8bc875Scarlsonj 		 * If the entry in the file is in greater than configured
424eb8bc875Scarlsonj 		 * state, then we must have a UUID.  Make sure that we do.
425eb8bc875Scarlsonj 		 * (Note that the file entry is only tokenized, not fully
426eb8bc875Scarlsonj 		 * parsed, so we need to do a string comparison here.)
427eb8bc875Scarlsonj 		 */
428eb8bc875Scarlsonj 		if (strcmp(zone_state, ZONE_STATE_STR_CONFIGURED) != 0 &&
429eb8bc875Scarlsonj 		    *zone_uuid == '\0') {
430eb8bc875Scarlsonj 			if (uuid_is_null(ze->zone_uuid))
431eb8bc875Scarlsonj 				uuid_generate(ze->zone_uuid);
432eb8bc875Scarlsonj 			uuid_unparse(ze->zone_uuid, uuidstr);
433eb8bc875Scarlsonj 			zone_uuid = uuidstr;
4347c478bd9Sstevel@tonic-gate 		}
435eb8bc875Scarlsonj 		/*
436eb8bc875Scarlsonj 		 * We need to quote a path that contains a ":"; this should
437eb8bc875Scarlsonj 		 * only affect the zonepath, as zone names do not allow such
438eb8bc875Scarlsonj 		 * characters, and zone states do not have them either.  Same
439eb8bc875Scarlsonj 		 * with double-quotes themselves: they are not allowed in zone
440eb8bc875Scarlsonj 		 * names, and do not occur in zone states, and in theory should
441eb8bc875Scarlsonj 		 * never occur in a zonepath since zonecfg does not support a
442eb8bc875Scarlsonj 		 * method for escaping them.
443eb8bc875Scarlsonj 		 */
444eb8bc875Scarlsonj 		need_quotes = (strchr(zone_path, ':') != NULL);
445eb8bc875Scarlsonj 		(void) fprintf(tmp_file, "%s:%s:%s%s%s:%s\n", zone_name,
446eb8bc875Scarlsonj 		    zone_state, need_quotes ? "\"" : "", zone_path,
447eb8bc875Scarlsonj 		    need_quotes ? "\"" : "", zone_uuid);
448eb8bc875Scarlsonj 		exist = B_TRUE;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	(void) fclose(index_file);
452108322fbScarlsonj 	index_file = NULL;
4537c478bd9Sstevel@tonic-gate 	if (fclose(tmp_file) != 0) {
454108322fbScarlsonj 		tmp_file = NULL;
455108322fbScarlsonj 		err = Z_MISC_FS;
456108322fbScarlsonj 		goto error;
4577c478bd9Sstevel@tonic-gate 	}
458108322fbScarlsonj 	tmp_file = NULL;
459108322fbScarlsonj 	if (rename(tmp_file_name, path) == -1) {
460108322fbScarlsonj 		err = errno == EACCES ? Z_ACCES : Z_MISC_FS;
461108322fbScarlsonj 		goto error;
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 	free(tmp_file_name);
4647c478bd9Sstevel@tonic-gate 	if (unlock_index_file(lock_fd) != Z_OK)
4657c478bd9Sstevel@tonic-gate 		return (Z_UNLOCKING_FILE);
4667c478bd9Sstevel@tonic-gate 	return (Z_OK);
467108322fbScarlsonj 
4687c478bd9Sstevel@tonic-gate error:
469108322fbScarlsonj 	if (index_file != NULL)
4707c478bd9Sstevel@tonic-gate 		(void) fclose(index_file);
471108322fbScarlsonj 	if (tmp_file != NULL)
4727c478bd9Sstevel@tonic-gate 		(void) fclose(tmp_file);
4737c478bd9Sstevel@tonic-gate 	(void) unlink(tmp_file_name);
4747c478bd9Sstevel@tonic-gate 	free(tmp_file_name);
475108322fbScarlsonj 	(void) unlock_index_file(lock_fd);
476108322fbScarlsonj 	return (err);
4777c478bd9Sstevel@tonic-gate }
478