Blame SOURCES/vsftpd-3.0.0-tz.patch

bd78b8
diff -up vsftpd-2.2.2/sysutil.c.tz vsftpd-2.2.2/sysutil.c
bd78b8
--- vsftpd-2.2.2/sysutil.c.tz	2012-04-26 12:45:21.095145878 +0200
bd78b8
+++ vsftpd-2.2.2/sysutil.c	2012-04-26 12:48:08.729618686 +0200
bd78b8
@@ -26,8 +26,10 @@
bd78b8
 /* For Linux, this adds nothing :-) */
bd78b8
 #include "port/porting_junk.h"
bd78b8
 
bd78b8
+#define F_LOCALTIME "/etc/localtime"
bd78b8
+#define BUFTZSIZ 64
bd78b8
+
bd78b8
 #include <signal.h>
bd78b8
-#include <string.h>
bd78b8
 #include <stdlib.h>
bd78b8
 #include <unistd.h>
bd78b8
 #include <sys/types.h>
bd78b8
@@ -55,6 +57,11 @@
bd78b8
 #include <utime.h>
bd78b8
 #include <netdb.h>
bd78b8
 #include <sys/resource.h>
bd78b8
+ 
bd78b8
+#ifndef __USE_GNU
bd78b8
+  #define __USE_GNU
bd78b8
+#endif
bd78b8
+#include <string.h>
bd78b8
 
bd78b8
 /* Private variables to this file */
bd78b8
 /* Current umask() */
bd78b8
@@ -2558,49 +2565,92 @@ error:
bd78b8
   die("reopening standard file descriptors to /dev/null failed");
bd78b8
 }
bd78b8
 
bd78b8
+char* vsf_sysutil_get_tz()
bd78b8
+{
bd78b8
+  char *ret_tz = NULL;
bd78b8
+  char buff[BUFTZSIZ];
bd78b8
+  off_t s_pos, e_pos;
bd78b8
+  size_t rcnt, rest;
bd78b8
+  int fd;
bd78b8
+
bd78b8
+  if ((fd = open(F_LOCALTIME, O_RDONLY)) > -1)
bd78b8
+  {
bd78b8
+    if ((e_pos = lseek(fd, 0, SEEK_END)) <= 0)
bd78b8
+    {
bd78b8
+      close(fd);
bd78b8
+      return NULL;
bd78b8
+    }
bd78b8
+    s_pos = e_pos > BUFTZSIZ ? e_pos - BUFTZSIZ : 0;
bd78b8
+    lseek(fd, s_pos, SEEK_SET);
bd78b8
+    rcnt = read(fd, buff, BUFTZSIZ);
bd78b8
+
bd78b8
+    if (rcnt && buff[rcnt-1] == '\n')
bd78b8
+    {
bd78b8
+      buff[rcnt-1] = 0;
bd78b8
+      e_pos--;
bd78b8
+    }
bd78b8
+
bd78b8
+    do {
bd78b8
+       char *nl = memrchr(buff, '\n', rcnt);
bd78b8
+       if (rcnt && nl)
bd78b8
+       {
bd78b8
+         int offset = (++nl) - buff;
bd78b8
+         int len = e_pos - s_pos - offset;
bd78b8
+         if (len)
bd78b8
+         {
bd78b8
+           lseek(fd, s_pos + offset, SEEK_SET);
bd78b8
+           ret_tz = calloc(1, len+4);
bd78b8
+           memcpy(ret_tz, "TZ=", 3);
bd78b8
+           rcnt = read(fd, ret_tz+3, len);
bd78b8
+         }
bd78b8
+         break;
bd78b8
+       }
bd78b8
+       if (!s_pos)
bd78b8
+       {
bd78b8
+         break;
bd78b8
+       }
bd78b8
+       rest = s_pos > BUFTZSIZ ? s_pos - BUFTZSIZ : 0;
bd78b8
+       s_pos -= rest;
bd78b8
+       lseek(fd, s_pos, SEEK_SET);
bd78b8
+       rcnt = read(fd, buff, rest);
bd78b8
+    } while (rcnt > 0);
bd78b8
+
bd78b8
+    close (fd);
bd78b8
+  }
bd78b8
+
bd78b8
+  return ret_tz;
bd78b8
+}
bd78b8
+
bd78b8
 void
bd78b8
 vsf_sysutil_tzset(void)
bd78b8
 {
bd78b8
   int retval;
bd78b8
-  char tzbuf[sizeof("+HHMM!")];
bd78b8
+  char *tz=NULL, tzbuf[sizeof("+HHMM!")];
bd78b8
   time_t the_time = time(NULL);
bd78b8
   struct tm* p_tm;
bd78b8
+
bd78b8
+  /* Set our timezone in the TZ environment variable to cater for the fact
bd78b8
+   * that modern glibc does not cache /etc/localtime (which becomes inaccessible
bd78b8
+   * when we chroot().
bd78b8
+   */
bd78b8
+  tz = vsf_sysutil_get_tz();;
bd78b8
+  if (tz)
bd78b8
+  {
bd78b8
+    putenv(tz);
bd78b8
+  }
bd78b8
   tzset();
bd78b8
   p_tm = localtime(&the_time);
bd78b8
   if (p_tm == NULL)
bd78b8
   {
bd78b8
     die("localtime");
bd78b8
   }
bd78b8
-  /* Set our timezone in the TZ environment variable to cater for the fact
bd78b8
-   * that modern glibc does not cache /etc/localtime (which becomes inaccessible
bd78b8
-   * when we chroot().
bd78b8
-   */
bd78b8
   retval = strftime(tzbuf, sizeof(tzbuf), "%z", p_tm);
bd78b8
   tzbuf[sizeof(tzbuf) - 1] = '\0';
bd78b8
   if (retval == 5)
bd78b8
   {
bd78b8
-    /* Static because putenv() does not copy the string. */
bd78b8
-    static char envtz[sizeof("TZ=UTC-hh:mm")];
bd78b8
-    /* Insert a colon so we have e.g. -05:00 instead of -0500 */
bd78b8
-    tzbuf[5] = tzbuf[4];
bd78b8
-    tzbuf[4] = tzbuf[3];
bd78b8
-    tzbuf[3] = ':';
bd78b8
-    /* Invert the sign - we just got the offset _from_ UTC but for TZ, we need
bd78b8
-     * the offset _to_ UTC.
bd78b8
-     */
bd78b8
-    if (tzbuf[0] == '+')
bd78b8
-    {
bd78b8
-      tzbuf[0] = '-';
bd78b8
-    }
bd78b8
-    else
bd78b8
-    {
bd78b8
-      tzbuf[0] = '+';
bd78b8
-    }
bd78b8
-    snprintf(envtz, sizeof(envtz), "TZ=UTC%s", tzbuf);
bd78b8
-    putenv(envtz);
bd78b8
     s_timezone = ((tzbuf[1] - '0') * 10 + (tzbuf[2] - '0')) * 60 * 60;
bd78b8
-    s_timezone += ((tzbuf[4] - '0') * 10 + (tzbuf[5] - '0')) * 60;
bd78b8
-    if (tzbuf[0] == '-')
bd78b8
+    s_timezone += ((tzbuf[3] - '0') * 10 + (tzbuf[4] - '0')) * 60;
bd78b8
+    if (tzbuf[0] == '+')
bd78b8
     {
bd78b8
       s_timezone *= -1;
bd78b8
     }