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$");
37
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
45
46 #include <sys/fcntl.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/stat.h>
50 #include <sys/sysctl.h>
51
52 #ifdef HAVE_DIALOG
53 #include <dialog.h>
54 #endif
55
56 #define _PATH_ZONETAB "/usr/share/zoneinfo/zone.tab"
57 #define _PATH_ISO3166 "/usr/share/misc/iso3166"
58 #define _PATH_ZONEINFO "/usr/share/zoneinfo"
59 #define _PATH_LOCALTIME "/etc/localtime"
60 #define _PATH_DB "/var/db/zoneinfo"
61 #define _PATH_WALL_CMOS_CLOCK "/etc/wall_cmos_clock"
62
63 #ifdef PATH_MAX
64 #define SILLY_BUFFER_SIZE 2*PATH_MAX
65 #else
66 #warning "Somebody needs to fix this to dynamically size this buffer."
67 #define SILLY_BUFFER_SIZE 2048
68 #endif
69
70 /* special return codes for `fire' actions */
71 #define DITEM_FAILURE 1
72
73 /* flags - returned in upper 16 bits of return status */
74 #define DITEM_LEAVE_MENU (1 << 16)
75 #define DITEM_RECREATE (1 << 18)
76
77 static char path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
78 path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
79 path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
80
81 static int reallydoit = 1;
82 static int reinstall = 0;
83 static char *chrootenv = NULL;
84
85 static void usage(void);
86 static int install_zoneinfo(const char *zoneinfo);
87 static int install_zoneinfo_file(const char *zoneinfo_file);
88
89 #ifdef HAVE_DIALOG
90 /* for use in describing more exotic behaviors */
91 typedef struct dialogMenuItem {
92 char *prompt;
93 char *title;
94 int (*fire)(struct dialogMenuItem *self);
95 void *data;
96 } dialogMenuItem;
97
98 static int
xdialog_count_rows(const char * p)99 xdialog_count_rows(const char *p)
100 {
101 int rows = 0;
102
103 while ((p = strchr(p, '\n')) != NULL) {
104 p++;
105 if (*p == '\0')
106 break;
107 rows++;
108 }
109
110 return (rows ? rows : 1);
111 }
112
113 static int
xdialog_count_columns(const char * p)114 xdialog_count_columns(const char *p)
115 {
116 int len;
117 int max_len = 0;
118 const char *q;
119
120 for (; (q = strchr(p, '\n')) != NULL; p = q + 1) {
121 len = q - p;
122 max_len = MAX(max_len, len);
123 }
124
125 len = strlen(p);
126 max_len = MAX(max_len, len);
127 return (max_len);
128 }
129
130 static int
xdialog_menu(const char * title,const char * cprompt,int height,int width,int menu_height,int item_no,dialogMenuItem * ditems)131 xdialog_menu(const char *title, const char *cprompt, int height, int width,
132 int menu_height, int item_no, dialogMenuItem *ditems)
133 {
134 int i, result, choice = 0;
135 DIALOG_LISTITEM *listitems;
136 DIALOG_VARS save_vars;
137
138 dlg_save_vars(&save_vars);
139
140 /* initialize list items */
141 listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1);
142 assert_ptr(listitems, "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
continent_country_menu(dialogMenuItem * continent)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 *
find_continent(const char * name)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
read_iso3166_table(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 /* Now skip past the three-letter and numeric codes. */
371 name = strsep(&s, "\t"); /* 3-let */
372 if (name == NULL || strlen(name) != 3)
373 errx(1, "%s:%d: invalid format", path_iso3166, lineno);
374 name = strsep(&s, "\t"); /* numeric */
375 if (name == NULL || strlen(name) != 3)
376 errx(1, "%s:%d: invalid format", path_iso3166, lineno);
377
378 name = s;
379
380 cp = &countries[CODE2INT(t)];
381 if (cp->name)
382 errx(1, "%s:%d: country code `%s' multiply defined: %s",
383 path_iso3166, lineno, t, cp->name);
384 cp->name = strdup(name);
385 if (cp->name == NULL)
386 errx(1, "malloc failed");
387 cp->tlc = strdup(t);
388 if (cp->tlc == NULL)
389 errx(1, "malloc failed");
390 }
391
392 fclose(fp);
393 }
394
395 static void
add_zone_to_country(int lineno,const char * tlc,const char * descr,const char * file,struct continent * cont)396 add_zone_to_country(int lineno, const char *tlc, const char *descr,
397 const char *file, struct continent *cont)
398 {
399 struct zone *zp;
400 struct country *cp;
401
402 if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
403 errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
404 lineno, tlc);
405
406 cp = &countries[CODE2INT(tlc)];
407 if (cp->name == 0)
408 errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
409 lineno, tlc);
410
411 if (descr) {
412 if (cp->nzones < 0)
413 errx(1, "%s:%d: conflicting zone definition",
414 path_zonetab, lineno);
415
416 zp = malloc(sizeof(*zp));
417 if (zp == NULL)
418 errx(1, "malloc(%zu)", sizeof(*zp));
419
420 if (cp->nzones == 0)
421 TAILQ_INIT(&cp->zones);
422
423 zp->descr = strdup(descr);
424 if (zp->descr == NULL)
425 errx(1, "malloc failed");
426 zp->filename = strdup(file);
427 if (zp->filename == NULL)
428 errx(1, "malloc failed");
429 zp->continent = cont;
430 TAILQ_INSERT_TAIL(&cp->zones, zp, link);
431 cp->nzones++;
432 } else {
433 if (cp->nzones > 0)
434 errx(1, "%s:%d: zone must have description",
435 path_zonetab, lineno);
436 if (cp->nzones < 0)
437 errx(1, "%s:%d: zone multiply defined",
438 path_zonetab, lineno);
439 cp->nzones = -1;
440 cp->filename = strdup(file);
441 if (cp->filename == NULL)
442 errx(1, "malloc failed");
443 cp->continent = cont;
444 }
445 }
446
447 /*
448 * This comparison function intentionally sorts all of the null-named
449 * ``countries''---i.e., the codes that don't correspond to a real
450 * country---to the end. Everything else is lexical by country name.
451 */
452 static int
compare_countries(const void * xa,const void * xb)453 compare_countries(const void *xa, const void *xb)
454 {
455 const struct country *a = xa, *b = xb;
456
457 if (a->name == 0 && b->name == 0)
458 return (0);
459 if (a->name == 0 && b->name != 0)
460 return (1);
461 if (b->name == 0)
462 return (-1);
463
464 return (strcmp(a->name, b->name));
465 }
466
467 /*
468 * This must be done AFTER all zone descriptions are read, since it breaks
469 * CODE2INT().
470 */
471 static void
sort_countries(void)472 sort_countries(void)
473 {
474
475 qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
476 }
477
478 static void
read_zones(void)479 read_zones(void)
480 {
481 char contbuf[16];
482 FILE *fp;
483 struct continent *cont;
484 size_t len, contlen;
485 char *line, *tlc, *file, *descr, *p;
486 int lineno;
487
488 fp = fopen(path_zonetab, "r");
489 if (!fp)
490 err(1, "%s", path_zonetab);
491 lineno = 0;
492
493 while ((line = fgetln(fp, &len)) != NULL) {
494 lineno++;
495 if (line[len - 1] != '\n')
496 errx(1, "%s:%d: invalid format", path_zonetab, lineno);
497 line[len - 1] = '\0';
498 if (line[0] == '#')
499 continue;
500
501 tlc = strsep(&line, "\t");
502 if (strlen(tlc) != 2)
503 errx(1, "%s:%d: invalid country code `%s'",
504 path_zonetab, lineno, tlc);
505 /* coord = */ strsep(&line, "\t"); /* Unused */
506 file = strsep(&line, "\t");
507 /* get continent portion from continent/country */
508 p = strchr(file, '/');
509 if (p == NULL)
510 errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
511 lineno, file);
512 contlen = p - file + 1; /* trailing nul */
513 if (contlen > sizeof(contbuf))
514 errx(1, "%s:%d: continent name in zone name `%s' too long",
515 path_zonetab, lineno, file);
516 strlcpy(contbuf, file, contlen);
517 cont = find_continent(contbuf);
518 if (!cont)
519 errx(1, "%s:%d: invalid region `%s'", path_zonetab,
520 lineno, contbuf);
521
522 descr = (line != NULL && *line != '\0') ? line : NULL;
523
524 add_zone_to_country(lineno, tlc, descr, file, cont);
525 }
526 fclose(fp);
527 }
528
529 static void
make_menus(void)530 make_menus(void)
531 {
532 struct country *cp;
533 struct zone *zp, *zp2;
534 struct continent *cont;
535 dialogMenuItem *dmi;
536 int i;
537
538 /*
539 * First, count up all the countries in each continent/ocean.
540 * Be careful to count those countries which have multiple zones
541 * only once for each. NB: some countries are in multiple
542 * continents/oceans.
543 */
544 for (cp = countries; cp->name; cp++) {
545 if (cp->nzones == 0)
546 continue;
547 if (cp->nzones < 0) {
548 cp->continent->nitems++;
549 } else {
550 TAILQ_FOREACH(zp, &cp->zones, link) {
551 cont = zp->continent;
552 for (zp2 = TAILQ_FIRST(&cp->zones);
553 zp2->continent != cont;
554 zp2 = TAILQ_NEXT(zp2, link))
555 ;
556 if (zp2 == zp)
557 zp->continent->nitems++;
558 }
559 }
560 }
561
562 /*
563 * Now allocate memory for the country menus and initialize
564 * continent menus. We set nitems back to zero so that we can
565 * use it for counting again when we actually build the menus.
566 */
567 memset(continents, 0, sizeof(continents));
568 for (i = 0; i < NCONTINENTS; i++) {
569 continent_names[i].continent->menu =
570 malloc(sizeof(dialogMenuItem) *
571 continent_names[i].continent->nitems);
572 if (continent_names[i].continent->menu == NULL)
573 errx(1, "malloc for continent menu");
574 continent_names[i].continent->nitems = 0;
575 continents[i].prompt = continent_items[i].prompt;
576 continents[i].title = continent_items[i].title;
577 continents[i].fire = continent_country_menu;
578 continents[i].data = continent_names[i].continent;
579 }
580
581 /*
582 * Now that memory is allocated, create the menu items for
583 * each continent. For multiple-zone countries, also create
584 * the country's zone submenu.
585 */
586 for (cp = countries; cp->name; cp++) {
587 if (cp->nzones == 0)
588 continue;
589 if (cp->nzones < 0) {
590 dmi = &cp->continent->menu[cp->continent->nitems];
591 memset(dmi, 0, sizeof(*dmi));
592 asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
593 dmi->title = cp->name;
594 dmi->fire = set_zone_whole_country;
595 dmi->data = cp;
596 } else {
597 cp->submenu = malloc(cp->nzones * sizeof(*dmi));
598 if (cp->submenu == 0)
599 errx(1, "malloc for submenu");
600 cp->nzones = 0;
601 TAILQ_FOREACH(zp, &cp->zones, link) {
602 cont = zp->continent;
603 dmi = &cp->submenu[cp->nzones];
604 memset(dmi, 0, sizeof(*dmi));
605 asprintf(&dmi->prompt, "%d", ++cp->nzones);
606 dmi->title = zp->descr;
607 dmi->fire = set_zone_multi;
608 dmi->data = zp;
609
610 for (zp2 = TAILQ_FIRST(&cp->zones);
611 zp2->continent != cont;
612 zp2 = TAILQ_NEXT(zp2, link))
613 ;
614 if (zp2 != zp)
615 continue;
616
617 dmi = &cont->menu[cont->nitems];
618 memset(dmi, 0, sizeof(*dmi));
619 asprintf(&dmi->prompt, "%d", ++cont->nitems);
620 dmi->title = cp->name;
621 dmi->fire = set_zone_menu;
622 dmi->data = cp;
623 }
624 }
625 }
626 }
627
628 static int
set_zone_menu(dialogMenuItem * dmi)629 set_zone_menu(dialogMenuItem *dmi)
630 {
631 char title[64], prompt[64];
632 struct country *cp = dmi->data;
633 int menulen;
634 int rv;
635
636 snprintf(title, sizeof(title), "%s Time Zones", cp->name);
637 snprintf(prompt, sizeof(prompt),
638 "Select a zone which observes the same time as your locality.");
639 menulen = cp->nzones < 16 ? cp->nzones : 16;
640 rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
641 cp->submenu);
642 if (rv != 0)
643 return (DITEM_RECREATE);
644 return (DITEM_LEAVE_MENU);
645 }
646
647 static int
set_zone_utc(void)648 set_zone_utc(void)
649 {
650 if (!confirm_zone("UTC"))
651 return (DITEM_FAILURE | DITEM_RECREATE);
652
653 return (install_zoneinfo("UTC"));
654 }
655
656 static int
confirm_zone(const char * filename)657 confirm_zone(const char *filename)
658 {
659 char title[64], prompt[64];
660 time_t t = time(0);
661 struct tm *tm;
662 int rv;
663
664 setenv("TZ", filename, 1);
665 tzset();
666 tm = localtime(&t);
667
668 snprintf(title, sizeof(title), "Confirmation");
669 snprintf(prompt, sizeof(prompt),
670 "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
671 rv = !dialog_yesno(title, prompt, 5, 72);
672 return (rv);
673 }
674
675 static int
set_zone_multi(dialogMenuItem * dmi)676 set_zone_multi(dialogMenuItem *dmi)
677 {
678 struct zone *zp = dmi->data;
679 int rv;
680
681 if (!confirm_zone(zp->filename))
682 return (DITEM_FAILURE | DITEM_RECREATE);
683
684 rv = install_zoneinfo(zp->filename);
685 return (rv);
686 }
687
688 static int
set_zone_whole_country(dialogMenuItem * dmi)689 set_zone_whole_country(dialogMenuItem *dmi)
690 {
691 struct country *cp = dmi->data;
692 int rv;
693
694 if (!confirm_zone(cp->filename))
695 return (DITEM_FAILURE | DITEM_RECREATE);
696
697 rv = install_zoneinfo(cp->filename);
698 return (rv);
699 }
700
701 #endif
702
703 static int
install_zoneinfo_file(const char * zoneinfo_file)704 install_zoneinfo_file(const char *zoneinfo_file)
705 {
706 char buf[1024];
707 char title[64], prompt[SILLY_BUFFER_SIZE];
708 struct stat sb;
709 ssize_t len;
710 int fd1, fd2, copymode;
711
712 if (lstat(path_localtime, &sb) < 0) {
713 /* Nothing there yet... */
714 copymode = 1;
715 } else if (S_ISLNK(sb.st_mode))
716 copymode = 0;
717 else
718 copymode = 1;
719
720 #ifdef VERBOSE
721 snprintf(title, sizeof(title), "Info");
722 if (copymode)
723 snprintf(prompt, sizeof(prompt),
724 "Copying %s to %s", zoneinfo_file, path_localtime);
725 else
726 snprintf(prompt, sizeof(prompt),
727 "Creating symbolic link %s to %s",
728 path_localtime, zoneinfo_file);
729 #ifdef HAVE_DIALOG
730 if (usedialog)
731 dialog_msgbox(title, prompt, 8, 72, 1);
732 else
733 #endif
734 fprintf(stderr, "%s\n", prompt);
735 #endif
736
737 if (reallydoit) {
738 if (copymode) {
739 fd1 = open(zoneinfo_file, O_RDONLY, 0);
740 if (fd1 < 0) {
741 snprintf(title, sizeof(title), "Error");
742 snprintf(prompt, sizeof(prompt),
743 "Could not open %s: %s", zoneinfo_file,
744 strerror(errno));
745 #ifdef HAVE_DIALOG
746 if (usedialog)
747 dialog_msgbox(title, prompt, 8, 72, 1);
748 else
749 #endif
750 fprintf(stderr, "%s\n", prompt);
751 return (DITEM_FAILURE | DITEM_RECREATE);
752 }
753
754 if (unlink(path_localtime) < 0 && errno != ENOENT) {
755 snprintf(prompt, sizeof(prompt),
756 "Could not delete %s: %s",
757 path_localtime, strerror(errno));
758 #ifdef HAVE_DIALOG
759 if (usedialog) {
760 snprintf(title, sizeof(title), "Error");
761 dialog_msgbox(title, prompt, 8, 72, 1);
762 } else
763 #endif
764 fprintf(stderr, "%s\n", prompt);
765 return (DITEM_FAILURE | DITEM_RECREATE);
766 }
767
768 fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
769 S_IRUSR | S_IRGRP | S_IROTH);
770 if (fd2 < 0) {
771 snprintf(title, sizeof(title), "Error");
772 snprintf(prompt, sizeof(prompt),
773 "Could not open %s: %s",
774 path_localtime, strerror(errno));
775 #ifdef HAVE_DIALOG
776 if (usedialog)
777 dialog_msgbox(title, prompt, 8, 72, 1);
778 else
779 #endif
780 fprintf(stderr, "%s\n", prompt);
781 return (DITEM_FAILURE | DITEM_RECREATE);
782 }
783
784 while ((len = read(fd1, buf, sizeof(buf))) > 0)
785 if ((len = write(fd2, buf, len)) < 0)
786 break;
787
788 if (len == -1) {
789 snprintf(title, sizeof(title), "Error");
790 snprintf(prompt, sizeof(prompt),
791 "Error copying %s to %s %s", zoneinfo_file,
792 path_localtime, strerror(errno));
793 #ifdef HAVE_DIALOG
794 if (usedialog)
795 dialog_msgbox(title, prompt, 8, 72, 1);
796 else
797 #endif
798 fprintf(stderr, "%s\n", prompt);
799 /* Better to leave none than a corrupt one. */
800 unlink(path_localtime);
801 return (DITEM_FAILURE | DITEM_RECREATE);
802 }
803 close(fd1);
804 close(fd2);
805 } else {
806 if (access(zoneinfo_file, R_OK) != 0) {
807 snprintf(title, sizeof(title), "Error");
808 snprintf(prompt, sizeof(prompt),
809 "Cannot access %s: %s", zoneinfo_file,
810 strerror(errno));
811 #ifdef HAVE_DIALOG
812 if (usedialog)
813 dialog_msgbox(title, prompt, 8, 72, 1);
814 else
815 #endif
816 fprintf(stderr, "%s\n", prompt);
817 return (DITEM_FAILURE | DITEM_RECREATE);
818 }
819 if (unlink(path_localtime) < 0 && errno != ENOENT) {
820 snprintf(prompt, sizeof(prompt),
821 "Could not delete %s: %s",
822 path_localtime, strerror(errno));
823 #ifdef HAVE_DIALOG
824 if (usedialog) {
825 snprintf(title, sizeof(title), "Error");
826 dialog_msgbox(title, prompt, 8, 72, 1);
827 } else
828 #endif
829 fprintf(stderr, "%s\n", prompt);
830 return (DITEM_FAILURE | DITEM_RECREATE);
831 }
832 if (symlink(zoneinfo_file, path_localtime) < 0) {
833 snprintf(title, sizeof(title), "Error");
834 snprintf(prompt, sizeof(prompt),
835 "Cannot create symbolic link %s to %s: %s",
836 path_localtime, zoneinfo_file,
837 strerror(errno));
838 #ifdef HAVE_DIALOG
839 if (usedialog)
840 dialog_msgbox(title, prompt, 8, 72, 1);
841 else
842 #endif
843 fprintf(stderr, "%s\n", prompt);
844 return (DITEM_FAILURE | DITEM_RECREATE);
845 }
846 }
847
848 #ifdef VERBOSE
849 snprintf(title, sizeof(title), "Done");
850 if (copymode)
851 snprintf(prompt, sizeof(prompt),
852 "Copied timezone file from %s to %s",
853 zoneinfo_file, path_localtime);
854 else
855 snprintf(prompt, sizeof(prompt),
856 "Created symbolic link from %s to %s",
857 zoneinfo_file, path_localtime);
858 #ifdef HAVE_DIALOG
859 if (usedialog)
860 dialog_msgbox(title, prompt, 8, 72, 1);
861 else
862 #endif
863 fprintf(stderr, "%s\n", prompt);
864 #endif
865 } /* reallydoit */
866
867 return (DITEM_LEAVE_MENU);
868 }
869
870 static int
install_zoneinfo(const char * zoneinfo)871 install_zoneinfo(const char *zoneinfo)
872 {
873 int rv;
874 FILE *f;
875 char path_zoneinfo_file[MAXPATHLEN];
876
877 if ((size_t)snprintf(path_zoneinfo_file, sizeof(path_zoneinfo_file),
878 "%s/%s", path_zoneinfo, zoneinfo) >= sizeof(path_zoneinfo_file))
879 errx(1, "%s/%s name too long", path_zoneinfo, zoneinfo);
880 rv = install_zoneinfo_file(path_zoneinfo_file);
881
882 /* Save knowledge for later */
883 if (reallydoit && (rv & DITEM_FAILURE) == 0) {
884 if ((f = fopen(path_db, "w")) != NULL) {
885 fprintf(f, "%s\n", zoneinfo);
886 fclose(f);
887 }
888 }
889
890 return (rv);
891 }
892
893 static void
usage(void)894 usage(void)
895 {
896
897 fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
898 " [zoneinfo_file | zoneinfo_name]\n");
899 exit(1);
900 }
901
902 int
main(int argc,char ** argv)903 main(int argc, char **argv)
904 {
905 #ifdef HAVE_DIALOG
906 char title[64], prompt[128];
907 int fd;
908 #endif
909 int c, rv, skiputc;
910 char vm_guest[16] = "";
911 size_t len = sizeof(vm_guest);
912
913 skiputc = 0;
914
915 /* Default skiputc to 1 for VM guests */
916 if (sysctlbyname("kern.vm_guest", vm_guest, &len, NULL, 0) == 0 &&
917 strcmp(vm_guest, "none") != 0)
918 skiputc = 1;
919
920 while ((c = getopt(argc, argv, "C:nrs")) != -1) {
921 switch(c) {
922 case 'C':
923 chrootenv = optarg;
924 break;
925 case 'n':
926 reallydoit = 0;
927 break;
928 case 'r':
929 reinstall = 1;
930 #ifdef HAVE_DIALOG
931 usedialog = 0;
932 #endif
933 break;
934 case 's':
935 skiputc = 1;
936 break;
937 default:
938 usage();
939 }
940 }
941
942 if (argc - optind > 1)
943 usage();
944
945 if (chrootenv == NULL) {
946 strcpy(path_zonetab, _PATH_ZONETAB);
947 strcpy(path_iso3166, _PATH_ISO3166);
948 strcpy(path_zoneinfo, _PATH_ZONEINFO);
949 strcpy(path_localtime, _PATH_LOCALTIME);
950 strcpy(path_db, _PATH_DB);
951 strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
952 } else {
953 sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
954 sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
955 sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
956 sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
957 sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
958 sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
959 _PATH_WALL_CMOS_CLOCK);
960 }
961
962 /* Override the user-supplied umask. */
963 (void)umask(S_IWGRP | S_IWOTH);
964
965 if (reinstall == 1) {
966 FILE *f;
967 char zoneinfo[MAXPATHLEN];
968
969 if ((f = fopen(path_db, "r")) != NULL) {
970 if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) {
971 zoneinfo[sizeof(zoneinfo) - 1] = 0;
972 if (strlen(zoneinfo) > 0) {
973 zoneinfo[strlen(zoneinfo) - 1] = 0;
974 rv = install_zoneinfo(zoneinfo);
975 exit(rv & ~DITEM_LEAVE_MENU);
976 }
977 errx(1, "Error reading %s.\n", path_db);
978 }
979 fclose(f);
980 errx(1,
981 "Unable to determine earlier installed zoneinfo "
982 "name. Check %s", path_db);
983 }
984 errx(1, "Cannot open %s for reading. Does it exist?", path_db);
985 }
986
987 /*
988 * If the arguments on the command-line do not specify a file,
989 * then interpret it as a zoneinfo name
990 */
991 if (optind == argc - 1) {
992 struct stat sb;
993
994 if (stat(argv[optind], &sb) != 0) {
995 #ifdef HAVE_DIALOG
996 usedialog = 0;
997 #endif
998 rv = install_zoneinfo(argv[optind]);
999 exit(rv & ~DITEM_LEAVE_MENU);
1000 }
1001 /* FALLTHROUGH */
1002 }
1003 #ifdef HAVE_DIALOG
1004
1005 read_iso3166_table();
1006 read_zones();
1007 sort_countries();
1008 make_menus();
1009
1010 init_dialog(stdin, stdout);
1011 if (skiputc == 0) {
1012 DIALOG_VARS save_vars;
1013 int yesno;
1014
1015 snprintf(title, sizeof(title),
1016 "Select local or UTC (Greenwich Mean Time) clock");
1017 snprintf(prompt, sizeof(prompt),
1018 "Is this machine's CMOS clock set to UTC? "
1019 "If it is set to local time,\n"
1020 "or you don't know, please choose NO here!");
1021 dlg_save_vars(&save_vars);
1022 #if !defined(__sparc64__)
1023 dialog_vars.defaultno = TRUE;
1024 #endif
1025 yesno = dialog_yesno(title, prompt, 7, 73);
1026 dlg_restore_vars(&save_vars);
1027 if (!yesno) {
1028 if (reallydoit)
1029 unlink(path_wall_cmos_clock);
1030 } else {
1031 if (reallydoit) {
1032 fd = open(path_wall_cmos_clock,
1033 O_WRONLY | O_CREAT | O_TRUNC,
1034 S_IRUSR | S_IRGRP | S_IROTH);
1035 if (fd < 0) {
1036 end_dialog();
1037 err(1, "create %s",
1038 path_wall_cmos_clock);
1039 }
1040 close(fd);
1041 }
1042 }
1043 dlg_clear();
1044 }
1045 if (optind == argc - 1) {
1046 snprintf(title, sizeof(title), "Default timezone provided");
1047 snprintf(prompt, sizeof(prompt),
1048 "\nUse the default `%s' zone?", argv[optind]);
1049 if (!dialog_yesno(title, prompt, 7, 72)) {
1050 rv = install_zoneinfo_file(argv[optind]);
1051 dlg_clear();
1052 end_dialog();
1053 exit(rv & ~DITEM_LEAVE_MENU);
1054 }
1055 dlg_clear();
1056 }
1057 snprintf(title, sizeof(title), "Time Zone Selector");
1058 snprintf(prompt, sizeof(prompt), "Select a region");
1059 xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,
1060 continents);
1061
1062 dlg_clear();
1063 end_dialog();
1064 #else
1065 usage();
1066 #endif
1067 return (0);
1068 }
1069