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