xref: /dragonfly/usr.sbin/tzsetup/tzsetup.c (revision f9993810)
1 /*
2  * Copyright 1996 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: head/usr.sbin/tzsetup/tzsetup.c 337522 2018-08-09 02:47:22Z delphij $
30  */
31 
32 /*
33  * Second attempt at a `tzmenu' program, using the separate description
34  * files provided in newer tzdata releases.
35  */
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <unistd.h>
44 
45 #include <sys/fcntl.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/stat.h>
49 #include <sys/sysctl.h>
50 
51 #ifdef HAVE_DIALOG
52 #include <dialog.h>
53 #endif
54 
55 #define	_PATH_ZONETAB		"/usr/share/zoneinfo/zone.tab"
56 #define	_PATH_ISO3166		"/usr/share/misc/iso3166"
57 #define	_PATH_ZONEINFO		"/usr/share/zoneinfo"
58 #define	_PATH_LOCALTIME		"/etc/localtime"
59 #define	_PATH_DB		"/var/db/zoneinfo"
60 #define	_PATH_WALL_CMOS_CLOCK	"/etc/wall_cmos_clock"
61 
62 #ifdef PATH_MAX
63 #define	SILLY_BUFFER_SIZE	2*PATH_MAX
64 #else
65 #warning "Somebody needs to fix this to dynamically size this buffer."
66 #define	SILLY_BUFFER_SIZE	2048
67 #endif
68 
69 /* special return codes for `fire' actions */
70 #define DITEM_FAILURE           1
71 
72 /* flags - returned in upper 16 bits of return status */
73 #define DITEM_LEAVE_MENU        (1 << 16)
74 #define DITEM_RECREATE          (1 << 18)
75 
76 static char	path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
77 		path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
78 		path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
79 
80 static int reallydoit = 1;
81 static int reinstall = 0;
82 static char *chrootenv = NULL;
83 
84 static void	usage(void);
85 static int	install_zoneinfo(const char *zoneinfo);
86 static int	install_zoneinfo_file(const char *zoneinfo_file);
87 
88 #ifdef HAVE_DIALOG
89 /* for use in describing more exotic behaviors */
90 typedef struct dialogMenuItem {
91 	char *prompt;
92 	char *title;
93 	int (*fire)(struct dialogMenuItem *self);
94 	void *data;
95 } dialogMenuItem;
96 
97 static int
98 xdialog_count_rows(const char *p)
99 {
100 	int rows = 0;
101 
102 	while ((p = strchr(p, '\n')) != NULL) {
103 		p++;
104 		if (*p == '\0')
105 			break;
106 		rows++;
107 	}
108 
109 	return (rows ? rows : 1);
110 }
111 
112 static int
113 xdialog_count_columns(const char *p)
114 {
115 	int len;
116 	int max_len = 0;
117 	const char *q;
118 
119 	for (; (q = strchr(p, '\n')) != NULL; p = q + 1) {
120 		len = q - p;
121 		max_len = MAX(max_len, len);
122 	}
123 
124 	len = strlen(p);
125 	max_len = MAX(max_len, len);
126 	return (max_len);
127 }
128 
129 static int
130 xdialog_menu(const char *title, const char *cprompt, int height, int width,
131 	     int menu_height, int item_no, dialogMenuItem *ditems)
132 {
133 	int i, result, choice = 0;
134 	DIALOG_LISTITEM *listitems;
135 	DIALOG_VARS save_vars;
136 
137 	dlg_save_vars(&save_vars);
138 
139 	/* initialize list items */
140 	listitems = calloc(item_no + 1, sizeof(DIALOG_LISTITEM));
141 	if (listitems == NULL)
142 		errx(1, "Failed to allocate memory in xdialog_menu");
143 	for (i = 0; i < item_no; i++) {
144 		listitems[i].name = ditems[i].prompt;
145 		listitems[i].text = ditems[i].title;
146 	}
147 
148 	/* calculate height */
149 	if (height < 0)
150 		height = xdialog_count_rows(cprompt) + menu_height + 4 + 2;
151 	if (height > LINES)
152 		height = LINES;
153 
154 	/* calculate width */
155 	if (width < 0) {
156 		int tag_x = 0;
157 
158 		for (i = 0; i < item_no; i++) {
159 			int j, l;
160 
161 			l = strlen(listitems[i].name);
162 			for (j = 0; j < item_no; j++) {
163 				int k = strlen(listitems[j].text);
164 				tag_x = MAX(tag_x, l + k + 2);
165 			}
166 		}
167 		width = MAX(xdialog_count_columns(cprompt), title != NULL ?
168 		    xdialog_count_columns(title) : 0);
169 		width = MAX(width, tag_x + 4) + 4;
170 	}
171 	width = MAX(width, 24);
172 	if (width > COLS)
173 		width = COLS;
174 
175 again:
176 	dialog_vars.default_item = listitems[choice].name;
177 	result = dlg_menu(title, cprompt, height, width,
178 	    menu_height, item_no, listitems, &choice, NULL);
179 	switch (result) {
180 	case DLG_EXIT_ESC:
181 		result = -1;
182 		break;
183 	case DLG_EXIT_OK:
184 		if (ditems[choice].fire != NULL) {
185 			int status;
186 
187 			status = ditems[choice].fire(ditems + choice);
188 			if (status & DITEM_RECREATE) {
189 				dlg_clear();
190 				goto again;
191 			}
192 		}
193 		result = 0;
194 		break;
195 	case DLG_EXIT_CANCEL:
196 	default:
197 		result = 1;
198 		break;
199 	}
200 
201 	free(listitems);
202 	dlg_restore_vars(&save_vars);
203 	return (result);
204 }
205 
206 static int usedialog = 1;
207 
208 static int	confirm_zone(const char *filename);
209 static int	continent_country_menu(dialogMenuItem *);
210 static int	set_zone_multi(dialogMenuItem *);
211 static int	set_zone_whole_country(dialogMenuItem *);
212 static int	set_zone_menu(dialogMenuItem *);
213 static int	set_zone_utc(void);
214 
215 struct continent {
216 	dialogMenuItem *menu;
217 	int		nitems;
218 };
219 
220 static struct continent	africa, america, antarctica, arctic, asia, atlantic;
221 static struct continent	australia, europe, indian, pacific, utc;
222 
223 static struct continent_names {
224 	const char	*name;
225 	struct continent *continent;
226 } continent_names[] = {
227 	{ "Africa",	&africa },
228 	{ "America",	&america },
229 	{ "Antarctica",	&antarctica },
230 	{ "Arctic",	&arctic },
231 	{ "Asia",	&asia },
232 	{ "Atlantic",	&atlantic },
233 	{ "Australia",	&australia },
234 	{ "Europe",	&europe },
235 	{ "Indian",	&indian },
236 	{ "Pacific",	&pacific },
237 	{ "UTC",	&utc }
238 };
239 
240 static struct continent_items {
241 	char		prompt[2];
242 	char		title[30];
243 } continent_items[] = {
244 	{ "1",	"Africa" },
245 	{ "2",	"America -- North and South" },
246 	{ "3",	"Antarctica" },
247 	{ "4",	"Arctic Ocean" },
248 	{ "5",	"Asia" },
249 	{ "6",	"Atlantic Ocean" },
250 	{ "7",	"Australia" },
251 	{ "8",	"Europe" },
252 	{ "9",	"Indian Ocean" },
253 	{ "0",	"Pacific Ocean" },
254 	{ "a",	"UTC" }
255 };
256 
257 #define	NCONTINENTS	\
258     (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
259 static dialogMenuItem continents[NCONTINENTS];
260 
261 #define	OCEANP(x)	((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
262 
263 static int
264 continent_country_menu(dialogMenuItem *continent)
265 {
266 	char		title[64], prompt[64];
267 	struct continent *contp = continent->data;
268 	int		isocean = OCEANP(continent - continents);
269 	int		menulen;
270 	int		rv;
271 
272 	if (strcmp(continent->title, "UTC") == 0)
273 		return (set_zone_utc());
274 
275 	/* Short cut -- if there's only one country, don't post a menu. */
276 	if (contp->nitems == 1)
277 		return (contp->menu[0].fire(&contp->menu[0]));
278 
279 	/* It's amazing how much good grammar really matters... */
280 	if (!isocean) {
281 		snprintf(title, sizeof(title), "Countries in %s",
282 		    continent->title);
283 		snprintf(prompt, sizeof(prompt), "Select a country or region");
284 	} else {
285 		snprintf(title, sizeof(title), "Islands and groups in the %s",
286 		    continent->title);
287 		snprintf(prompt, sizeof(prompt), "Select an island or group");
288 	}
289 
290 	menulen = contp->nitems < 16 ? contp->nitems : 16;
291 	rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems,
292 	    contp->menu);
293 	if (rv == 0)
294 		return (DITEM_LEAVE_MENU);
295 	return (DITEM_RECREATE);
296 }
297 
298 static struct continent *
299 find_continent(const char *name)
300 {
301 	int		i;
302 
303 	for (i = 0; i < NCONTINENTS; i++)
304 		if (strcmp(name, continent_names[i].name) == 0)
305 			return (continent_names[i].continent);
306 	return (0);
307 }
308 
309 struct country {
310 	char		*name;
311 	char		*tlc;
312 	int		nzones;
313 	char		*filename;	/* use iff nzones < 0 */
314 	struct continent *continent;	/* use iff nzones < 0 */
315 	TAILQ_HEAD(, zone) zones;	/* use iff nzones > 0 */
316 	dialogMenuItem	*submenu;	/* use iff nzones > 0 */
317 };
318 
319 struct zone {
320 	TAILQ_ENTRY(zone) link;
321 	char		*descr;
322 	char		*filename;
323 	struct continent *continent;
324 };
325 
326 /*
327  * This is the easiest organization... we use ISO 3166 country codes,
328  * of the two-letter variety, so we just size this array to suit.
329  * Beats worrying about dynamic allocation.
330  */
331 #define	NCOUNTRIES	(26 * 26)
332 static struct country countries[NCOUNTRIES];
333 
334 #define	CODE2INT(s)	((s[0] - 'A') * 26 + (s[1] - 'A'))
335 
336 /*
337  * Read the ISO 3166 country code database in _PATH_ISO3166
338  * (/usr/share/misc/iso3166).  On error, exit via err(3).
339  */
340 static void
341 read_iso3166_table(void)
342 {
343 	FILE		*fp;
344 	struct country	*cp;
345 	size_t		len;
346 	char		*s, *t, *name;
347 	int		lineno;
348 
349 	fp = fopen(path_iso3166, "r");
350 	if (!fp)
351 		err(1, "%s", path_iso3166);
352 	lineno = 0;
353 
354 	while ((s = fgetln(fp, &len)) != NULL) {
355 		lineno++;
356 		if (s[len - 1] != '\n')
357 			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
358 		s[len - 1] = '\0';
359 		if (s[0] == '#' || strspn(s, " \t") == len - 1)
360 			continue;
361 
362 		/* Isolate the two-letter code. */
363 		t = strsep(&s, "\t");
364 		if (t == NULL || strlen(t) != 2)
365 			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
366 		if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
367 			errx(1, "%s:%d: invalid code `%s'", path_iso3166,
368 			    lineno, t);
369 
370 		name = s;
371 
372 		cp = &countries[CODE2INT(t)];
373 		if (cp->name)
374 			errx(1, "%s:%d: country code `%s' multiply defined: %s",
375 			    path_iso3166, lineno, t, cp->name);
376 		cp->name = strdup(name);
377 		if (cp->name == NULL)
378 			errx(1, "malloc failed");
379 		cp->tlc = strdup(t);
380 		if (cp->tlc == NULL)
381 			errx(1, "malloc failed");
382 	}
383 
384 	fclose(fp);
385 }
386 
387 static void
388 add_zone_to_country(int lineno, const char *tlc, const char *descr,
389     const char *file, struct continent *cont)
390 {
391 	struct zone	*zp;
392 	struct country	*cp;
393 
394 	if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
395 		errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
396 		    lineno, tlc);
397 
398 	cp = &countries[CODE2INT(tlc)];
399 	if (cp->name == 0)
400 		errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
401 		    lineno, tlc);
402 
403 	if (descr) {
404 		if (cp->nzones < 0)
405 			errx(1, "%s:%d: conflicting zone definition",
406 			    path_zonetab, lineno);
407 
408 		zp = malloc(sizeof(*zp));
409 		if (zp == NULL)
410 			errx(1, "malloc(%zu)", sizeof(*zp));
411 
412 		if (cp->nzones == 0)
413 			TAILQ_INIT(&cp->zones);
414 
415 		zp->descr = strdup(descr);
416 		if (zp->descr == NULL)
417 			errx(1, "malloc failed");
418 		zp->filename = strdup(file);
419 		if (zp->filename == NULL)
420 			errx(1, "malloc failed");
421 		zp->continent = cont;
422 		TAILQ_INSERT_TAIL(&cp->zones, zp, link);
423 		cp->nzones++;
424 	} else {
425 		if (cp->nzones > 0)
426 			errx(1, "%s:%d: zone must have description",
427 			    path_zonetab, lineno);
428 		if (cp->nzones < 0)
429 			errx(1, "%s:%d: zone multiply defined",
430 			    path_zonetab, lineno);
431 		cp->nzones = -1;
432 		cp->filename = strdup(file);
433 		if (cp->filename == NULL)
434 			errx(1, "malloc failed");
435 		cp->continent = cont;
436 	}
437 }
438 
439 /*
440  * This comparison function intentionally sorts all of the null-named
441  * ``countries''---i.e., the codes that don't correspond to a real
442  * country---to the end.  Everything else is lexical by country name.
443  */
444 static int
445 compare_countries(const void *xa, const void *xb)
446 {
447 	const struct country *a = xa, *b = xb;
448 
449 	if (a->name == 0 && b->name == 0)
450 		return (0);
451 	if (a->name == 0 && b->name != 0)
452 		return (1);
453 	if (b->name == 0)
454 		return (-1);
455 
456 	return (strcmp(a->name, b->name));
457 }
458 
459 /*
460  * This must be done AFTER all zone descriptions are read, since it breaks
461  * CODE2INT().
462  */
463 static void
464 sort_countries(void)
465 {
466 
467 	qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
468 }
469 
470 static void
471 read_zones(void)
472 {
473 	char		contbuf[16];
474 	FILE		*fp;
475 	struct continent *cont;
476 	size_t		len, contlen;
477 	char		*line, *tlc, *file, *descr, *p;
478 	int		lineno;
479 
480 	fp = fopen(path_zonetab, "r");
481 	if (!fp)
482 		err(1, "%s", path_zonetab);
483 	lineno = 0;
484 
485 	while ((line = fgetln(fp, &len)) != NULL) {
486 		lineno++;
487 		if (line[len - 1] != '\n')
488 			errx(1, "%s:%d: invalid format", path_zonetab, lineno);
489 		line[len - 1] = '\0';
490 		if (line[0] == '#')
491 			continue;
492 
493 		tlc = strsep(&line, "\t");
494 		if (strlen(tlc) != 2)
495 			errx(1, "%s:%d: invalid country code `%s'",
496 			    path_zonetab, lineno, tlc);
497 		/* coord = */ strsep(&line, "\t");	 /* Unused */
498 		file = strsep(&line, "\t");
499 		/* get continent portion from continent/country */
500 		p = strchr(file, '/');
501 		if (p == NULL)
502 			errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
503 			    lineno, file);
504 		contlen = p - file + 1;		/* trailing nul */
505 		if (contlen > sizeof(contbuf))
506 			errx(1, "%s:%d: continent name in zone name `%s' too long",
507 			    path_zonetab, lineno, file);
508 		strlcpy(contbuf, file, contlen);
509 		cont = find_continent(contbuf);
510 		if (!cont)
511 			errx(1, "%s:%d: invalid region `%s'", path_zonetab,
512 			    lineno, contbuf);
513 
514 		descr = (line != NULL && *line != '\0') ? line : NULL;
515 
516 		add_zone_to_country(lineno, tlc, descr, file, cont);
517 	}
518 	fclose(fp);
519 }
520 
521 static void
522 make_menus(void)
523 {
524 	struct country	*cp;
525 	struct zone	*zp, *zp2;
526 	struct continent *cont;
527 	dialogMenuItem	*dmi;
528 	int		i;
529 
530 	/*
531 	 * First, count up all the countries in each continent/ocean.
532 	 * Be careful to count those countries which have multiple zones
533 	 * only once for each.  NB: some countries are in multiple
534 	 * continents/oceans.
535 	 */
536 	for (cp = countries; cp->name; cp++) {
537 		if (cp->nzones == 0)
538 			continue;
539 		if (cp->nzones < 0) {
540 			cp->continent->nitems++;
541 		} else {
542 			TAILQ_FOREACH(zp, &cp->zones, link) {
543 				cont = zp->continent;
544 				for (zp2 = TAILQ_FIRST(&cp->zones);
545 				    zp2->continent != cont;
546 				    zp2 = TAILQ_NEXT(zp2, link))
547 					;
548 				if (zp2 == zp)
549 					zp->continent->nitems++;
550 			}
551 		}
552 	}
553 
554 	/*
555 	 * Now allocate memory for the country menus and initialize
556 	 * continent menus.  We set nitems back to zero so that we can
557 	 * use it for counting again when we actually build the menus.
558 	 */
559 	memset(continents, 0, sizeof(continents));
560 	for (i = 0; i < NCONTINENTS; i++) {
561 		continent_names[i].continent->menu =
562 		    malloc(sizeof(dialogMenuItem) *
563 		    continent_names[i].continent->nitems);
564 		if (continent_names[i].continent->menu == NULL)
565 			errx(1, "malloc for continent menu");
566 		continent_names[i].continent->nitems = 0;
567 		continents[i].prompt = continent_items[i].prompt;
568 		continents[i].title = continent_items[i].title;
569 		continents[i].fire = continent_country_menu;
570 		continents[i].data = continent_names[i].continent;
571 	}
572 
573 	/*
574 	 * Now that memory is allocated, create the menu items for
575 	 * each continent.  For multiple-zone countries, also create
576 	 * the country's zone submenu.
577 	 */
578 	for (cp = countries; cp->name; cp++) {
579 		if (cp->nzones == 0)
580 			continue;
581 		if (cp->nzones < 0) {
582 			dmi = &cp->continent->menu[cp->continent->nitems];
583 			memset(dmi, 0, sizeof(*dmi));
584 			asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
585 			dmi->title = cp->name;
586 			dmi->fire = set_zone_whole_country;
587 			dmi->data = cp;
588 		} else {
589 			cp->submenu = malloc(cp->nzones * sizeof(*dmi));
590 			if (cp->submenu == 0)
591 				errx(1, "malloc for submenu");
592 			cp->nzones = 0;
593 			TAILQ_FOREACH(zp, &cp->zones, link) {
594 				cont = zp->continent;
595 				dmi = &cp->submenu[cp->nzones];
596 				memset(dmi, 0, sizeof(*dmi));
597 				asprintf(&dmi->prompt, "%d", ++cp->nzones);
598 				dmi->title = zp->descr;
599 				dmi->fire = set_zone_multi;
600 				dmi->data = zp;
601 
602 				for (zp2 = TAILQ_FIRST(&cp->zones);
603 				    zp2->continent != cont;
604 				    zp2 = TAILQ_NEXT(zp2, link))
605 					;
606 				if (zp2 != zp)
607 					continue;
608 
609 				dmi = &cont->menu[cont->nitems];
610 				memset(dmi, 0, sizeof(*dmi));
611 				asprintf(&dmi->prompt, "%d", ++cont->nitems);
612 				dmi->title = cp->name;
613 				dmi->fire = set_zone_menu;
614 				dmi->data = cp;
615 			}
616 		}
617 	}
618 }
619 
620 static int
621 set_zone_menu(dialogMenuItem *dmi)
622 {
623 	char		title[64], prompt[64];
624 	struct country	*cp = dmi->data;
625 	int		menulen;
626 	int		rv;
627 
628 	snprintf(title, sizeof(title), "%s Time Zones", cp->name);
629 	snprintf(prompt, sizeof(prompt),
630 	    "Select a zone which observes the same time as your locality.");
631 	menulen = cp->nzones < 16 ? cp->nzones : 16;
632 	rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
633 	    cp->submenu);
634 	if (rv != 0)
635 		return (DITEM_RECREATE);
636 	return (DITEM_LEAVE_MENU);
637 }
638 
639 static int
640 set_zone_utc(void)
641 {
642 	if (!confirm_zone("UTC"))
643 		return (DITEM_FAILURE | DITEM_RECREATE);
644 
645 	return (install_zoneinfo("UTC"));
646 }
647 
648 static int
649 confirm_zone(const char *filename)
650 {
651 	char		title[64], prompt[64];
652 	time_t		t = time(0);
653 	struct tm	*tm;
654 	int		rv;
655 
656 	setenv("TZ", filename, 1);
657 	tzset();
658 	tm = localtime(&t);
659 
660 	snprintf(title, sizeof(title), "Confirmation");
661 	snprintf(prompt, sizeof(prompt),
662 	    "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
663 	rv = !dialog_yesno(title, prompt, 5, 72);
664 	return (rv);
665 }
666 
667 static int
668 set_zone_multi(dialogMenuItem *dmi)
669 {
670 	struct zone	*zp = dmi->data;
671 	int		rv;
672 
673 	if (!confirm_zone(zp->filename))
674 		return (DITEM_FAILURE | DITEM_RECREATE);
675 
676 	rv = install_zoneinfo(zp->filename);
677 	return (rv);
678 }
679 
680 static int
681 set_zone_whole_country(dialogMenuItem *dmi)
682 {
683 	struct country	*cp = dmi->data;
684 	int		rv;
685 
686 	if (!confirm_zone(cp->filename))
687 		return (DITEM_FAILURE | DITEM_RECREATE);
688 
689 	rv = install_zoneinfo(cp->filename);
690 	return (rv);
691 }
692 
693 #endif
694 
695 static int
696 install_zoneinfo_file(const char *zoneinfo_file)
697 {
698 	char		buf[1024];
699 	char		title[64], prompt[SILLY_BUFFER_SIZE];
700 	struct stat	sb;
701 	ssize_t		len;
702 	int		fd1, fd2, copymode;
703 
704 	if (lstat(path_localtime, &sb) < 0) {
705 		/* Nothing there yet... */
706 		copymode = 1;
707 	} else if (S_ISLNK(sb.st_mode))
708 		copymode = 0;
709 	else
710 		copymode = 1;
711 
712 #ifdef VERBOSE
713 	snprintf(title, sizeof(title), "Info");
714 	if (copymode)
715 		snprintf(prompt, sizeof(prompt),
716 		    "Copying %s to %s", zoneinfo_file, path_localtime);
717 	else
718 		snprintf(prompt, sizeof(prompt),
719 		    "Creating symbolic link %s to %s",
720 		    path_localtime, zoneinfo_file);
721 #ifdef HAVE_DIALOG
722 	if (usedialog)
723 		dialog_msgbox(title, prompt, 8, 72, 1);
724 	else
725 #endif
726 		fprintf(stderr, "%s\n", prompt);
727 #endif
728 
729 	if (reallydoit) {
730 		if (copymode) {
731 			fd1 = open(zoneinfo_file, O_RDONLY, 0);
732 			if (fd1 < 0) {
733 				snprintf(title, sizeof(title), "Error");
734 				snprintf(prompt, sizeof(prompt),
735 				    "Could not open %s: %s", zoneinfo_file,
736 				    strerror(errno));
737 #ifdef HAVE_DIALOG
738 				if (usedialog)
739 					dialog_msgbox(title, prompt, 8, 72, 1);
740 				else
741 #endif
742 					fprintf(stderr, "%s\n", prompt);
743 				return (DITEM_FAILURE | DITEM_RECREATE);
744 			}
745 
746 			if (unlink(path_localtime) < 0 && errno != ENOENT) {
747 				snprintf(prompt, sizeof(prompt),
748 				    "Could not delete %s: %s",
749 				    path_localtime, strerror(errno));
750 #ifdef HAVE_DIALOG
751 				if (usedialog) {
752 					snprintf(title, sizeof(title), "Error");
753 					dialog_msgbox(title, prompt, 8, 72, 1);
754 				} else
755 #endif
756 					fprintf(stderr, "%s\n", prompt);
757 				return (DITEM_FAILURE | DITEM_RECREATE);
758 			}
759 
760 			fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
761 			    S_IRUSR | S_IRGRP | S_IROTH);
762 			if (fd2 < 0) {
763 				snprintf(title, sizeof(title), "Error");
764 				snprintf(prompt, sizeof(prompt),
765 				    "Could not open %s: %s",
766 				    path_localtime, strerror(errno));
767 #ifdef HAVE_DIALOG
768 				if (usedialog)
769 					dialog_msgbox(title, prompt, 8, 72, 1);
770 				else
771 #endif
772 					fprintf(stderr, "%s\n", prompt);
773 				return (DITEM_FAILURE | DITEM_RECREATE);
774 			}
775 
776 			while ((len = read(fd1, buf, sizeof(buf))) > 0)
777 				if ((len = write(fd2, buf, len)) < 0)
778 					break;
779 
780 			if (len == -1) {
781 				snprintf(title, sizeof(title), "Error");
782 				snprintf(prompt, sizeof(prompt),
783 				    "Error copying %s to %s %s", zoneinfo_file,
784 				    path_localtime, strerror(errno));
785 #ifdef HAVE_DIALOG
786 				if (usedialog)
787 					dialog_msgbox(title, prompt, 8, 72, 1);
788 				else
789 #endif
790 					fprintf(stderr, "%s\n", prompt);
791 				/* Better to leave none than a corrupt one. */
792 				unlink(path_localtime);
793 				return (DITEM_FAILURE | DITEM_RECREATE);
794 			}
795 			close(fd1);
796 			close(fd2);
797 		} else {
798 			if (access(zoneinfo_file, R_OK) != 0) {
799 				snprintf(title, sizeof(title), "Error");
800 				snprintf(prompt, sizeof(prompt),
801 				    "Cannot access %s: %s", zoneinfo_file,
802 				    strerror(errno));
803 #ifdef HAVE_DIALOG
804 				if (usedialog)
805 					dialog_msgbox(title, prompt, 8, 72, 1);
806 				else
807 #endif
808 					fprintf(stderr, "%s\n", prompt);
809 				return (DITEM_FAILURE | DITEM_RECREATE);
810 			}
811 			if (unlink(path_localtime) < 0 && errno != ENOENT) {
812 				snprintf(prompt, sizeof(prompt),
813 				    "Could not delete %s: %s",
814 				    path_localtime, strerror(errno));
815 #ifdef HAVE_DIALOG
816 				if (usedialog) {
817 					snprintf(title, sizeof(title), "Error");
818 					dialog_msgbox(title, prompt, 8, 72, 1);
819 				} else
820 #endif
821 					fprintf(stderr, "%s\n", prompt);
822 				return (DITEM_FAILURE | DITEM_RECREATE);
823 			}
824 			if (symlink(zoneinfo_file, path_localtime) < 0) {
825 				snprintf(title, sizeof(title), "Error");
826 				snprintf(prompt, sizeof(prompt),
827 				    "Cannot create symbolic link %s to %s: %s",
828 				    path_localtime, zoneinfo_file,
829 				    strerror(errno));
830 #ifdef HAVE_DIALOG
831 				if (usedialog)
832 					dialog_msgbox(title, prompt, 8, 72, 1);
833 				else
834 #endif
835 					fprintf(stderr, "%s\n", prompt);
836 				return (DITEM_FAILURE | DITEM_RECREATE);
837 			}
838 		}
839 
840 #ifdef VERBOSE
841 		snprintf(title, sizeof(title), "Done");
842 		if (copymode)
843 			snprintf(prompt, sizeof(prompt),
844 			    "Copied timezone file from %s to %s",
845 			    zoneinfo_file, path_localtime);
846 		else
847 			snprintf(prompt, sizeof(prompt),
848 			    "Created symbolic link from %s to %s",
849 			    zoneinfo_file, path_localtime);
850 #ifdef HAVE_DIALOG
851 		if (usedialog)
852 			dialog_msgbox(title, prompt, 8, 72, 1);
853 		else
854 #endif
855 			fprintf(stderr, "%s\n", prompt);
856 #endif
857 	} /* reallydoit */
858 
859 	return (DITEM_LEAVE_MENU);
860 }
861 
862 static int
863 install_zoneinfo(const char *zoneinfo)
864 {
865 	int		rv;
866 	FILE		*f;
867 	char		path_zoneinfo_file[MAXPATHLEN + 2];
868 
869 	if ((size_t)snprintf(path_zoneinfo_file, sizeof(path_zoneinfo_file),
870 	    "%s/%s", path_zoneinfo, zoneinfo) >= sizeof(path_zoneinfo_file))
871 		errx(1, "%s/%s name too long", path_zoneinfo, zoneinfo);
872 	rv = install_zoneinfo_file(path_zoneinfo_file);
873 
874 	/* Save knowledge for later */
875 	if (reallydoit && (rv & DITEM_FAILURE) == 0) {
876 		if ((f = fopen(path_db, "w")) != NULL) {
877 			fprintf(f, "%s\n", zoneinfo);
878 			fclose(f);
879 		}
880 	}
881 
882 	return (rv);
883 }
884 
885 static void
886 usage(void)
887 {
888 
889 	fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
890 	    " [zoneinfo_file | zoneinfo_name]\n");
891 	exit(1);
892 }
893 
894 int
895 main(int argc, char **argv)
896 {
897 #ifdef HAVE_DIALOG
898 	char		title[64], prompt[128];
899 	int		fd;
900 #endif
901 	int		c, rv;
902 #ifdef HAVE_DIALOG
903 	char		vmm_guest[16] = "";
904 	size_t		len = sizeof(vmm_guest);
905 	int		skiputc;
906 
907 	skiputc = 0;
908 
909 	/* Default skiputc to 1 for VM guests */
910 	if (sysctlbyname("kern.vmm_guest", vmm_guest, &len, NULL, 0) == 0 &&
911 	    strcmp(vmm_guest, "none") != 0)
912 		skiputc = 1;
913 #endif
914 
915 	while ((c = getopt(argc, argv, "C:nrs")) != -1) {
916 		switch(c) {
917 		case 'C':
918 			chrootenv = optarg;
919 			break;
920 		case 'n':
921 			reallydoit = 0;
922 			break;
923 		case 'r':
924 			reinstall = 1;
925 #ifdef HAVE_DIALOG
926 			usedialog = 0;
927 #endif
928 			break;
929 		case 's':
930 #ifdef HAVE_DIALOG
931 			skiputc = 1;
932 #endif
933 			break;
934 		default:
935 			usage();
936 		}
937 	}
938 
939 	if (argc - optind > 1)
940 		usage();
941 
942 	if (chrootenv == NULL) {
943 		strcpy(path_zonetab, _PATH_ZONETAB);
944 		strcpy(path_iso3166, _PATH_ISO3166);
945 		strcpy(path_zoneinfo, _PATH_ZONEINFO);
946 		strcpy(path_localtime, _PATH_LOCALTIME);
947 		strcpy(path_db, _PATH_DB);
948 		strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
949 	} else {
950 		sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
951 		sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
952 		sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
953 		sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
954 		sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
955 		sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
956 		    _PATH_WALL_CMOS_CLOCK);
957 	}
958 
959 	/* Override the user-supplied umask. */
960 	umask(S_IWGRP | S_IWOTH);
961 
962 	if (reinstall == 1) {
963 		FILE *f;
964 		char zoneinfo[MAXPATHLEN];
965 
966 		if ((f = fopen(path_db, "r")) != NULL) {
967 			if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) {
968 				zoneinfo[sizeof(zoneinfo) - 1] = 0;
969 				if (strlen(zoneinfo) > 0) {
970 					zoneinfo[strlen(zoneinfo) - 1] = 0;
971 					rv = install_zoneinfo(zoneinfo);
972 					exit(rv & ~DITEM_LEAVE_MENU);
973 				}
974 				errx(1, "Error reading %s.\n", path_db);
975 			}
976 			fclose(f);
977 			errx(1,
978 			    "Unable to determine earlier installed zoneinfo "
979 			    "name. Check %s", path_db);
980 		}
981 		errx(1, "Cannot open %s for reading. Does it exist?", path_db);
982 	}
983 
984 	/*
985 	 * If the arguments on the command-line do not specify a file,
986 	 * then interpret it as a zoneinfo name
987 	 */
988 	if (optind == argc - 1) {
989 		struct stat sb;
990 
991 		if (stat(argv[optind], &sb) != 0) {
992 #ifdef HAVE_DIALOG
993 			usedialog = 0;
994 #endif
995 			rv = install_zoneinfo(argv[optind]);
996 			exit(rv & ~DITEM_LEAVE_MENU);
997 		}
998 		/* FALLTHROUGH */
999 	}
1000 #ifdef HAVE_DIALOG
1001 
1002 	read_iso3166_table();
1003 	read_zones();
1004 	sort_countries();
1005 	make_menus();
1006 
1007 	init_dialog(stdin, stdout);
1008 	if (skiputc == 0) {
1009 		DIALOG_VARS save_vars;
1010 		int yesno;
1011 
1012 		snprintf(title, sizeof(title),
1013 		    "Select local or UTC (Greenwich Mean Time) clock");
1014 		snprintf(prompt, sizeof(prompt),
1015 		    "Is this machine's CMOS clock set to UTC?  "
1016 		    "If it is set to local time,\n"
1017 		    "or you don't know, please choose NO here!");
1018 		dlg_save_vars(&save_vars);
1019 		yesno = dialog_yesno(title, prompt, 7, 73);
1020 		dlg_restore_vars(&save_vars);
1021 		if (!yesno) {
1022 			if (reallydoit)
1023 				unlink(path_wall_cmos_clock);
1024 		} else {
1025 			if (reallydoit) {
1026 				fd = open(path_wall_cmos_clock,
1027 				    O_WRONLY | O_CREAT | O_TRUNC,
1028 				    S_IRUSR | S_IRGRP | S_IROTH);
1029 				if (fd < 0) {
1030 					end_dialog();
1031 					err(1, "create %s",
1032 					    path_wall_cmos_clock);
1033 				}
1034 				close(fd);
1035 			}
1036 		}
1037 		dlg_clear();
1038 	}
1039 	if (optind == argc - 1) {
1040 		snprintf(title, sizeof(title), "Default timezone provided");
1041 		snprintf(prompt, sizeof(prompt),
1042 		    "\nUse the default `%s' zone?", argv[optind]);
1043 		if (!dialog_yesno(title, prompt, 7, 72)) {
1044 			rv = install_zoneinfo_file(argv[optind]);
1045 			dlg_clear();
1046 			end_dialog();
1047 			exit(rv & ~DITEM_LEAVE_MENU);
1048 		}
1049 		dlg_clear();
1050 	}
1051 	snprintf(title, sizeof(title), "Time Zone Selector");
1052 	snprintf(prompt, sizeof(prompt), "Select a region");
1053 	xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,
1054 	    continents);
1055 
1056 	dlg_clear();
1057 	end_dialog();
1058 #else
1059 	usage();
1060 #endif
1061 	return (0);
1062 }
1063