xref: /titanic_52/usr/src/lib/libzonecfg/common/getzoneent.c (revision 930176a00b697a09f5256ff3d4b1550f8e73c3a6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * This module contains functions used for reading and writing the index file.
30  * setzoneent() opens the file.  getzoneent() parses the file, doing the usual
31  * skipping of comment lines, etc., and using gettok() to deal with the ":"
32  * delimiters.  endzoneent() closes the file.  putzoneent() updates the file,
33  * adding, deleting or modifying lines, locking and unlocking appropriately.
34  */
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <libzonecfg.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <sys/stat.h>
43 #include <assert.h>
44 #include <uuid/uuid.h>
45 #include "zonecfg_impl.h"
46 
47 
48 #define	_PATH_TMPFILE	ZONE_CONFIG_ROOT "/zonecfg.XXXXXX"
49 
50 /*
51  * gettok() is a helper function for parsing the index file, used to split
52  * the lines by their ":" delimiters.  Note that an entry may contain a ":"
53  * inside double quotes; this should only affect the zonepath, as zone names
54  * do not allow such characters, and zone states do not have them either.
55  * Same with double-quotes themselves: they are not allowed in zone names,
56  * and do not occur in zone states, and in theory should never occur in a
57  * zonepath since zonecfg does not support a method for escaping them.
58  *
59  * It never returns NULL.
60  */
61 
62 static char *
63 gettok(char **cpp)
64 {
65 	char *cp = *cpp, *retv;
66 	boolean_t quoted = B_FALSE;
67 
68 	if (cp == NULL)
69 		return ("");
70 	if (*cp == '"') {
71 		quoted = B_TRUE;
72 		cp++;
73 	}
74 	retv = cp;
75 	if (quoted) {
76 		while (*cp != '\0' && *cp != '"')
77 			cp++;
78 		if (*cp == '"')
79 			*cp++ = '\0';
80 	}
81 	while (*cp != '\0' && *cp != ':')
82 		cp++;
83 	if (*cp == '\0') {
84 		*cpp = NULL;
85 	} else {
86 		*cp++ = '\0';
87 		*cpp = cp;
88 	}
89 	return (retv);
90 }
91 
92 char *
93 getzoneent(FILE *cookie)
94 {
95 	struct zoneent *ze;
96 	char *name;
97 
98 	if ((ze = getzoneent_private(cookie)) == NULL)
99 		return (NULL);
100 	name = strdup(ze->zone_name);
101 	free(ze);
102 	return (name);
103 }
104 
105 struct zoneent *
106 getzoneent_private(FILE *cookie)
107 {
108 	char *cp, buf[MAX_INDEX_LEN], *p;
109 	struct zoneent *ze;
110 
111 	if (cookie == NULL)
112 		return (NULL);
113 
114 	if ((ze = malloc(sizeof (struct zoneent))) == NULL)
115 		return (NULL);
116 
117 	for (;;) {
118 		if (fgets(buf, sizeof (buf), cookie) == NULL) {
119 			free(ze);
120 			return (NULL);
121 		}
122 		if ((cp = strpbrk(buf, "\r\n")) == NULL) {
123 			/* this represents a line that's too long */
124 			continue;
125 		}
126 		*cp = '\0';
127 		cp = buf;
128 		if (*cp == '#') {
129 			/* skip comment lines */
130 			continue;
131 		}
132 		p = gettok(&cp);
133 		if (*p == '\0' || strlen(p) >= ZONENAME_MAX) {
134 			/*
135 			 * empty or very long zone names are not allowed
136 			 */
137 			continue;
138 		}
139 		(void) strlcpy(ze->zone_name, p, ZONENAME_MAX);
140 
141 		p = gettok(&cp);
142 		if (*p == '\0') {
143 			/* state field should not be empty */
144 			continue;
145 		}
146 		errno = 0;
147 		if (strcmp(p, ZONE_STATE_STR_CONFIGURED) == 0) {
148 			ze->zone_state = ZONE_STATE_CONFIGURED;
149 		} else if (strcmp(p, ZONE_STATE_STR_INCOMPLETE) == 0) {
150 			ze->zone_state = ZONE_STATE_INCOMPLETE;
151 		} else if (strcmp(p, ZONE_STATE_STR_INSTALLED) == 0) {
152 			ze->zone_state = ZONE_STATE_INSTALLED;
153 		} else {
154 			continue;
155 		}
156 
157 		p = gettok(&cp);
158 		if (strlen(p) >= MAXPATHLEN) {
159 			/* very long paths are not allowed */
160 			continue;
161 		}
162 		(void) strlcpy(ze->zone_path, p, MAXPATHLEN);
163 
164 		p = gettok(&cp);
165 		if (uuid_parse(p, ze->zone_uuid) == -1)
166 			uuid_clear(ze->zone_uuid);
167 
168 		break;
169 	}
170 
171 	return (ze);
172 }
173 
174 static boolean_t
175 get_index_path(char *path)
176 {
177 	return (snprintf(path, MAXPATHLEN, "%s%s", zonecfg_root,
178 	    ZONE_INDEX_FILE) < MAXPATHLEN);
179 }
180 
181 FILE *
182 setzoneent(void)
183 {
184 	char path[MAXPATHLEN];
185 
186 	if (!get_index_path(path)) {
187 		errno = EINVAL;
188 		return (NULL);
189 	}
190 	return (fopen(path, "r"));
191 }
192 
193 void
194 endzoneent(FILE *cookie)
195 {
196 	if (cookie != NULL)
197 		(void) fclose(cookie);
198 }
199 
200 static int
201 lock_index_file(void)
202 {
203 	int lock_fd;
204 	struct flock lock;
205 	char path[MAXPATHLEN];
206 
207 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
208 	    ZONE_INDEX_LOCK_DIR) >= sizeof (path))
209 		return (-1);
210 	if ((mkdir(path, S_IRWXU) == -1) && errno != EEXIST)
211 		return (-1);
212 	if (strlcat(path, ZONE_INDEX_LOCK_FILE, sizeof (path)) >=
213 	    sizeof (path))
214 		return (-1);
215 	lock_fd = open(path, O_CREAT|O_RDWR, 0644);
216 	if (lock_fd == -1)
217 		return (-1);
218 
219 	lock.l_type = F_WRLCK;
220 	lock.l_whence = SEEK_SET;
221 	lock.l_start = 0;
222 	lock.l_len = 0;
223 
224 	if (fcntl(lock_fd, F_SETLKW, &lock) == -1) {
225 		(void) close(lock_fd);
226 		return (-1);
227 	}
228 
229 	return (lock_fd);
230 }
231 
232 static int
233 unlock_index_file(int lock_fd)
234 {
235 	struct flock lock;
236 
237 	lock.l_type = F_UNLCK;
238 	lock.l_whence = SEEK_SET;
239 	lock.l_start = 0;
240 	lock.l_len = 0;
241 
242 	if (fcntl(lock_fd, F_SETLK, &lock) == -1)
243 		return (Z_UNLOCKING_FILE);
244 
245 	if (close(lock_fd) == -1)
246 		return (Z_UNLOCKING_FILE);
247 
248 	return (Z_OK);
249 }
250 
251 /*
252  * This function adds or removes a zone name et al. to the index file.
253  *
254  * If ze->zone_state is < 0, it means leave the
255  * existing value unchanged; this is only meaningful when operation ==
256  * PZE_MODIFY (i.e., it's bad on PZE_ADD and a no-op on PZE_REMOVE).
257  *
258  * A zero-length ze->zone_path means leave the existing value
259  * unchanged; this is only meaningful when operation == PZE_MODIFY
260  * (i.e., it's bad on PZE_ADD and a no-op on PZE_REMOVE).
261  *
262  * A zero-length ze->zone_newname means leave the existing name
263  * unchanged; otherwise the zone is renamed to zone_newname.  This is
264  * only meaningful when operation == PZE_MODIFY.
265  *
266  * Locking and unlocking is done via the functions above.
267  * The file itself is not modified in place; rather, a copy is made which
268  * is modified, then the copy is atomically renamed back to the main file.
269  */
270 int
271 putzoneent(struct zoneent *ze, zoneent_op_t operation)
272 {
273 	FILE *index_file, *tmp_file;
274 	char *tmp_file_name, buf[MAX_INDEX_LEN];
275 	int tmp_file_desc, lock_fd, err;
276 	boolean_t exist, need_quotes;
277 	char *cp;
278 	char path[MAXPATHLEN];
279 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
280 	size_t tlen, namelen;
281 	const char *zone_name, *zone_state, *zone_path, *zone_uuid;
282 
283 	assert(ze != NULL);
284 	if (operation == PZE_ADD &&
285 	    (ze->zone_state < 0 || strlen(ze->zone_path) == 0))
286 		return (Z_INVAL);
287 
288 	if (operation != PZE_MODIFY && strlen(ze->zone_newname) != 0)
289 		return (Z_INVAL);
290 
291 	if ((lock_fd = lock_index_file()) == -1)
292 		return (Z_LOCKING_FILE);
293 
294 	/* using sizeof gives us room for the terminating NUL byte as well */
295 	tlen = sizeof (_PATH_TMPFILE) + strlen(zonecfg_root);
296 	tmp_file_name = malloc(tlen);
297 	if (tmp_file_name == NULL) {
298 		(void) unlock_index_file(lock_fd);
299 		return (Z_NOMEM);
300 	}
301 	(void) snprintf(tmp_file_name, tlen, "%s%s", zonecfg_root,
302 	    _PATH_TMPFILE);
303 
304 	tmp_file_desc = mkstemp(tmp_file_name);
305 	if (tmp_file_desc == -1) {
306 		(void) unlink(tmp_file_name);
307 		free(tmp_file_name);
308 		(void) unlock_index_file(lock_fd);
309 		return (Z_TEMP_FILE);
310 	}
311 	if ((tmp_file = fdopen(tmp_file_desc, "w")) == NULL) {
312 		(void) close(tmp_file_desc);
313 		err = Z_MISC_FS;
314 		goto error;
315 	}
316 	if (!get_index_path(path)) {
317 		err = Z_MISC_FS;
318 		goto error;
319 	}
320 	if ((index_file = fopen(path, "r")) == NULL) {
321 		err = Z_MISC_FS;
322 		goto error;
323 	}
324 
325 	exist = B_FALSE;
326 	zone_name = ze->zone_name;
327 	namelen = strlen(zone_name);
328 	for (;;) {
329 		if (fgets(buf, sizeof (buf), index_file) == NULL) {
330 			if (operation == PZE_ADD && !exist) {
331 				zone_state = zone_state_str(ze->zone_state);
332 				zone_path = ze->zone_path;
333 				zone_uuid = "";
334 				goto add_entry;
335 			}
336 			/*
337 			 * It's not considered an error to delete something
338 			 * that doesn't exist, but we can't modify a missing
339 			 * record.
340 			 */
341 			if (operation == PZE_MODIFY && !exist) {
342 				err = Z_UPDATING_INDEX;
343 				goto error;
344 			}
345 			break;
346 		}
347 
348 		if (buf[0] == '#') {
349 			/* skip and preserve comment lines */
350 			(void) fputs(buf, tmp_file);
351 			continue;
352 		}
353 
354 		if (strncmp(buf, zone_name, namelen) != 0 ||
355 		    buf[namelen] != ':') {
356 			/* skip and preserve non-target lines */
357 			(void) fputs(buf, tmp_file);
358 			continue;
359 		}
360 
361 		if ((cp = strpbrk(buf, "\r\n")) == NULL) {
362 			/* this represents a line that's too long; delete */
363 			continue;
364 		}
365 		*cp = '\0';
366 
367 		/*
368 		 * Skip over the zone name.  Because we've already matched the
369 		 * target zone (above), we know for certain here that the zone
370 		 * name is present and correctly formed.  No need to check.
371 		 */
372 		cp = strchr(buf, ':') + 1;
373 
374 		zone_state = gettok(&cp);
375 		if (*zone_state == '\0') {
376 			/* state field should not be empty */
377 			err = Z_UPDATING_INDEX;
378 			goto error;
379 		}
380 		zone_path = gettok(&cp);
381 		zone_uuid = gettok(&cp);
382 
383 		switch (operation) {
384 		case PZE_ADD:
385 			/* can't add same zone */
386 			err = Z_UPDATING_INDEX;
387 			goto error;
388 
389 		case PZE_MODIFY:
390 			/*
391 			 * If the caller specified a new state for the zone,
392 			 * then use that.  Otherwise, use the current state.
393 			 */
394 			if (ze->zone_state >= 0) {
395 				zone_state = zone_state_str(ze->zone_state);
396 
397 				/*
398 				 * If the caller is uninstalling this zone,
399 				 * then wipe out the uuid.  The zone's contents
400 				 * are no longer known.
401 				 */
402 				if (ze->zone_state < ZONE_STATE_INSTALLED)
403 					zone_uuid = "";
404 			}
405 
406 			/* If a new name is supplied, use it. */
407 			if (ze->zone_newname[0] != '\0')
408 				zone_name = ze->zone_newname;
409 
410 			if (ze->zone_path[0] != '\0')
411 				zone_path = ze->zone_path;
412 			break;
413 
414 		case PZE_REMOVE:
415 		default:
416 			continue;
417 		}
418 
419 	add_entry:
420 		/*
421 		 * If the entry in the file is in greater than configured
422 		 * state, then we must have a UUID.  Make sure that we do.
423 		 * (Note that the file entry is only tokenized, not fully
424 		 * parsed, so we need to do a string comparison here.)
425 		 */
426 		if (strcmp(zone_state, ZONE_STATE_STR_CONFIGURED) != 0 &&
427 		    *zone_uuid == '\0') {
428 			if (uuid_is_null(ze->zone_uuid))
429 				uuid_generate(ze->zone_uuid);
430 			uuid_unparse(ze->zone_uuid, uuidstr);
431 			zone_uuid = uuidstr;
432 		}
433 		/*
434 		 * We need to quote a path that contains a ":"; this should
435 		 * only affect the zonepath, as zone names do not allow such
436 		 * characters, and zone states do not have them either.  Same
437 		 * with double-quotes themselves: they are not allowed in zone
438 		 * names, and do not occur in zone states, and in theory should
439 		 * never occur in a zonepath since zonecfg does not support a
440 		 * method for escaping them.
441 		 */
442 		need_quotes = (strchr(zone_path, ':') != NULL);
443 		(void) fprintf(tmp_file, "%s:%s:%s%s%s:%s\n", zone_name,
444 		    zone_state, need_quotes ? "\"" : "", zone_path,
445 		    need_quotes ? "\"" : "", zone_uuid);
446 		exist = B_TRUE;
447 	}
448 
449 	(void) fclose(index_file);
450 	index_file = NULL;
451 	if (fclose(tmp_file) != 0) {
452 		tmp_file = NULL;
453 		err = Z_MISC_FS;
454 		goto error;
455 	}
456 	tmp_file = NULL;
457 	(void) chmod(tmp_file_name, 0644);
458 	if (rename(tmp_file_name, path) == -1) {
459 		err = errno == EACCES ? Z_ACCES : Z_MISC_FS;
460 		goto error;
461 	}
462 	free(tmp_file_name);
463 	if (unlock_index_file(lock_fd) != Z_OK)
464 		return (Z_UNLOCKING_FILE);
465 	return (Z_OK);
466 
467 error:
468 	if (index_file != NULL)
469 		(void) fclose(index_file);
470 	if (tmp_file != NULL)
471 		(void) fclose(tmp_file);
472 	(void) unlink(tmp_file_name);
473 	free(tmp_file_name);
474 	(void) unlock_index_file(lock_fd);
475 	return (err);
476 }
477