Update.
[platform/upstream/glibc.git] / time / tzfile.c
index 9ff24df..9289de6 100644 (file)
@@ -1,27 +1,27 @@
-/* Copyright (C) 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
-This file is part of the GNU C Library.
+/* Copyright (C) 1991, 92, 93, 95, 96, 97 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
 
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
 
-The GNU C Library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Library General Public License for more details.
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
 
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.  */
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
 
-#include <ansidecl.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
 #include <string.h>
 #include <limits.h>
+#include <unistd.h>
 
 #define        NOID
 #include <tzfile.h>
@@ -43,7 +43,10 @@ struct leap
     long int change;           /* Seconds of correction to apply.  */
   };
 
-static void compute_tzname_max __P ((size_t));
+extern char * __tzstring (const char *); /* Defined in tzset.c.  */
+
+static struct ttinfo *find_transition (time_t timer);
+static void compute_tzname_max (size_t);
 
 static size_t num_transitions;
 static time_t *transitions = NULL;
@@ -60,67 +63,100 @@ static struct leap *leaps = NULL;
 static inline int
 decode (const void *ptr)
 {
-#if BYTE_ORDER == BIG_ENDIAN
-  return *(const int *) ptr;
-#else
-  const unsigned char *p = ptr;
-  int result = 0;
-
-  result = (result << 8) | *p++;
-  result = (result << 8) | *p++;
-  result = (result << 8) | *p++;
-  result = (result << 8) | *p++;
-
-  return result;
-#endif
+  if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
+    return *(const int *) ptr;
+  else
+    {
+      const unsigned char *p = ptr;
+      int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
+
+      result = (result << 8) | *p++;
+      result = (result << 8) | *p++;
+      result = (result << 8) | *p++;
+      result = (result << 8) | *p++;
+
+      return result;
+    }
 }
 
 void
-DEFUN(__tzfile_read, (file), CONST char *file)
+__tzfile_read (const char *file)
 {
+  static const char default_tzdir[] = TZDIR;
   size_t num_isstd, num_isgmt;
   register FILE *f;
   struct tzhead tzhead;
   size_t chars;
   register size_t i;
+  struct ttinfo *info;
 
   __use_tzfile = 0;
 
   if (transitions != NULL)
-    free((PTR) transitions);
+    free ((void *) transitions);
   transitions = NULL;
   if (type_idxs != NULL)
-    free((PTR) type_idxs);
+    free ((void *) type_idxs);
   type_idxs = NULL;
   if (types != NULL)
-    free((PTR) types);
+    free ((void *) types);
   types = NULL;
   if (zone_names != NULL)
-    free((PTR) zone_names);
+    free ((void *) zone_names);
   zone_names = NULL;
   if (leaps != NULL)
-    free((PTR) leaps);
+    free ((void *) leaps);
   leaps = NULL;
 
-  if (file == NULL || *file == '\0')
+  if (file == NULL)
+    /* No user specification; use the site-wide default.  */
     file = TZDEFAULT;
+  else if (*file == '\0')
+    /* User specified the empty string; use UTC with no leap seconds.  */
+    return;
+  else
+    {
+      /* We must not allow to read an arbitrary file in a setuid
+        program.  So we fail for any file which is not in the
+        directory hierachy starting at TZDIR
+        and which is not the system wide default TZDEFAULT.  */
+      if (__libc_enable_secure
+         && ((*file == '/'
+              && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
+              && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
+             || strstr (file, "../") != NULL))
+       /* This test is certainly a bit too restrictive but it should
+          catch all critical cases.  */
+       return;
+    }
 
   if (*file != '/')
     {
-      static CONST char tzdir[] = TZDIR;
-      register CONST unsigned int len = strlen(file) + 1;
-      char *new = (char *) __alloca(sizeof(tzdir) + len);
-      memcpy(new, tzdir, sizeof(tzdir) - 1);
-      new[sizeof(tzdir) - 1] = '/';
-      memcpy(&new[sizeof(tzdir)], file, len);
+      const char *tzdir;
+      unsigned int len, tzdir_len;
+      char *new;
+
+      tzdir = __secure_getenv ("TZDIR");
+      if (tzdir == NULL || *tzdir == '\0')
+       {
+         tzdir = default_tzdir;
+         tzdir_len = sizeof (default_tzdir) - 1;
+       }
+      else
+       tzdir_len = strlen (tzdir);
+      len = strlen (file) + 1;
+      new = (char *) __alloca (tzdir_len + 1 + len);
+      memcpy (new, tzdir, tzdir_len);
+      new[tzdir_len] = '/';
+      memcpy (&new[tzdir_len + 1], file, len);
       file = new;
     }
 
-  f = fopen(file, "r");
+  f = fopen (file, "r");
   if (f == NULL)
     return;
 
-  if (fread((PTR) &tzhead, sizeof(tzhead), 1, f) != 1)
+  if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
     goto lose;
 
   num_transitions = (size_t) decode (tzhead.tzh_timecnt);
@@ -158,10 +194,19 @@ DEFUN(__tzfile_read, (file), CONST char *file)
        goto lose;
     }
 
-  if (fread((PTR) transitions, 4, num_transitions, f) != num_transitions ||
-      fread((PTR) type_idxs, 1, num_transitions, f) != num_transitions)
+  if (sizeof (time_t) < 4)
+      abort ();
+
+  if (fread(transitions, 4, num_transitions, f) != num_transitions ||
+      fread(type_idxs, 1, num_transitions, f) != num_transitions)
     goto lose;
 
+  /* Check for bogus indices in the data file, so we can hereafter
+     safely use type_idxs[T] as indices into `types' and never crash.  */
+  for (i = 0; i < num_transitions; ++i)
+    if (type_idxs[i] >= num_types)
+      goto lose;
+
   if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
     {
       /* Decode the transition times, stored as 4-byte integers in
@@ -169,37 +214,39 @@ DEFUN(__tzfile_read, (file), CONST char *file)
         the array so as not to clobber the next element to be
         processed when sizeof (time_t) > 4.  */
       i = num_transitions;
-      while (num_transitions-- > 0)
+      while (i-- > 0)
        transitions[i] = decode ((char *) transitions + i*4);
     }
 
   for (i = 0; i < num_types; ++i)
     {
       unsigned char x[4];
-      if (fread((PTR) x, 1, 4, f) != 4 ||
-         fread((PTR) &types[i].isdst, 1, 1, f) != 1 ||
-         fread((PTR) &types[i].idx, 1, 1, f) != 1)
+      if (fread (x, 1, 4, f) != 4 ||
+         fread (&types[i].isdst, 1, 1, f) != 1 ||
+         fread (&types[i].idx, 1, 1, f) != 1)
+       goto lose;
+      if (types[i].idx >= chars) /* Bogus index in data file.  */
        goto lose;
       types[i].offset = (long int) decode (x);
     }
 
-  if (fread((PTR) zone_names, 1, chars, f) != chars)
+  if (fread (zone_names, 1, chars, f) != chars)
     goto lose;
 
   for (i = 0; i < num_leaps; ++i)
     {
       unsigned char x[4];
-      if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
+      if (fread (x, 1, sizeof (x), f) != sizeof (x))
        goto lose;
       leaps[i].transition = (time_t) decode (x);
-      if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
+      if (fread (x, 1, sizeof (x), f) != sizeof (x))
        goto lose;
       leaps[i].change = (long int) decode (x);
     }
 
   for (i = 0; i < num_isstd; ++i)
     {
-      char c = getc(f);
+      int c = getc (f);
       if (c == EOF)
        goto lose;
       types[i].isstd = c != 0;
@@ -209,7 +256,7 @@ DEFUN(__tzfile_read, (file), CONST char *file)
 
   for (i = 0; i < num_isgmt; ++i)
     {
-      char c = getc(f);
+      int c = getc (f);
       if (c == EOF)
        goto lose;
       types[i].isgmt = c != 0;
@@ -217,15 +264,22 @@ DEFUN(__tzfile_read, (file), CONST char *file)
   while (i < num_types)
     types[i++].isgmt = 0;
 
-  (void) fclose(f);
+  fclose (f);
+
+  info = find_transition (0);
+  for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
+       ++i)
+    __tzname[types[i].isdst] = __tzstring (&zone_names[types[i].idx]);
+  if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
+    __tzname[info->isdst] = __tzstring (&zone_names[info->idx]);
 
   compute_tzname_max (chars);
-  
+
   __use_tzfile = 1;
   return;
 
  lose:;
-  (void) fclose(f);
+  fclose(f);
 }
 \f
 /* The user specified a hand-made timezone, but not its DST rules.
@@ -233,9 +287,8 @@ DEFUN(__tzfile_read, (file), CONST char *file)
    from the TZDEFRULES file.  */
 
 void
-DEFUN(__tzfile_default, (std, dst, stdoff, dstoff),
-      char *std AND char *dst AND
-      long int stdoff AND long int dstoff)
+__tzfile_default (const char *std, const char *dst,
+                 long int stdoff, long int dstoff)
 {
   size_t stdlen, dstlen, i;
   long int rule_offset, rule_stdoff, rule_dstoff;
@@ -269,7 +322,7 @@ DEFUN(__tzfile_default, (std, dst, stdoff, dstoff),
   /* Find the standard and daylight time offsets used by the rule file.
      We choose the offsets in the types of each flavor that are
      transitioned to earliest in time.  */
-  rule_dstoff = 0;
+  rule_stdoff = rule_dstoff = 0;
   for (i = 0; i < num_transitions; ++i)
     {
       if (!rule_stdoff && !types[type_idxs[i]].isdst)
@@ -321,15 +374,17 @@ DEFUN(__tzfile_default, (std, dst, stdoff, dstoff),
   types[1].offset = dstoff;
   types[1].isdst = 1;
 
+  /* Reset the zone names to point to the user's names.  */
+  __tzname[0] = (char *) std;
+  __tzname[1] = (char *) dst;
+
   compute_tzname_max (stdlen + dstlen);
 }
 \f
-int
-DEFUN(__tzfile_compute, (timer, leap_correct, leap_hit),
-      time_t timer AND long int *leap_correct AND int *leap_hit)
+static struct ttinfo *
+find_transition (time_t timer)
 {
-  struct ttinfo *info;
-  register size_t i;
+  size_t i;
 
   if (num_transitions == 0 || timer < transitions[0])
     {
@@ -352,14 +407,27 @@ DEFUN(__tzfile_compute, (timer, leap_correct, leap_hit),
       i = type_idxs[i - 1];
     }
 
-  info = &types[i];
-  __daylight = info->isdst;
-  __timezone = info->offset;
-  for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
-       ++i)
-    __tzname[types[i].isdst] = &zone_names[types[i].idx];
-  if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
-    __tzname[info->isdst] = &zone_names[info->idx];
+  return &types[i];
+}
+\f
+int
+__tzfile_compute (time_t timer, int use_localtime,
+                 long int *leap_correct, int *leap_hit)
+{
+  register size_t i;
+
+  if (use_localtime)
+    {
+      struct ttinfo *info = find_transition (timer);
+      __daylight = info->isdst;
+      __timezone = info->offset;
+      for (i = 0;
+          i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
+          ++i)
+       __tzname[types[i].isdst] = &zone_names[types[i].idx];
+      if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
+       __tzname[info->isdst] = &zone_names[info->idx];
+    }
 
   *leap_correct = 0L;
   *leap_hit = 0;
@@ -391,10 +459,10 @@ DEFUN(__tzfile_compute, (timer, leap_correct, leap_hit),
   return 1;
 }
 \f
-void
-DEFUN(compute_tzname_max, (chars), size_t chars)
+static void
+compute_tzname_max (size_t chars)
 {
-  extern long int __tzname_cur_max; /* Defined in __tzset.c. */
+  extern size_t __tzname_cur_max; /* Defined in tzset.c. */
 
   const char *p;
 
@@ -404,7 +472,7 @@ DEFUN(compute_tzname_max, (chars), size_t chars)
       const char *start = p;
       while (*p != '\0')
        ++p;
-      if (p - start > __tzname_cur_max)
+      if ((size_t) (p - start) > __tzname_cur_max)
        __tzname_cur_max = p - start;
     } while (++p < &zone_names[chars]);
 }