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