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