added a tdb to store the account policy informations.
[samba.git] / source / lib / time.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    time handling functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /*
25   This stuff was largely rewritten by Paul Eggert <eggert@twinsun.com>
26   in May 1996 
27   */
28
29
30 int extra_time_offset = 0;
31
32 #ifndef CHAR_BIT
33 #define CHAR_BIT 8
34 #endif
35
36 #ifndef TIME_T_MIN
37 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
38                     : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
39 #endif
40 #ifndef TIME_T_MAX
41 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
42 #endif
43
44 /*******************************************************************
45  External access to time_t_min and time_t_max.
46 ********************************************************************/
47
48 time_t get_time_t_min(void)
49 {
50         return TIME_T_MIN;
51 }
52
53 time_t get_time_t_max(void)
54 {
55         return TIME_T_MAX;
56 }
57
58 /*******************************************************************
59 a gettimeofday wrapper
60 ********************************************************************/
61 void GetTimeOfDay(struct timeval *tval)
62 {
63 #ifdef HAVE_GETTIMEOFDAY_TZ
64         gettimeofday(tval,NULL);
65 #else
66         gettimeofday(tval);
67 #endif
68 }
69
70 #define TM_YEAR_BASE 1900
71
72 /*******************************************************************
73 yield the difference between *A and *B, in seconds, ignoring leap seconds
74 ********************************************************************/
75 static int tm_diff(struct tm *a, struct tm *b)
76 {
77   int ay = a->tm_year + (TM_YEAR_BASE - 1);
78   int by = b->tm_year + (TM_YEAR_BASE - 1);
79   int intervening_leap_days =
80     (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
81   int years = ay - by;
82   int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
83   int hours = 24*days + (a->tm_hour - b->tm_hour);
84   int minutes = 60*hours + (a->tm_min - b->tm_min);
85   int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
86
87   return seconds;
88 }
89
90 /*******************************************************************
91   return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
92   ******************************************************************/
93 static int TimeZone(time_t t)
94 {
95   struct tm *tm = gmtime(&t);
96   struct tm tm_utc;
97   if (!tm)
98     return 0;
99   tm_utc = *tm;
100   tm = localtime(&t);
101   if (!tm)
102     return 0;
103   return tm_diff(&tm_utc,tm);
104
105 }
106
107 static BOOL done_serverzone_init;
108
109 /* Return the smb serverzone value */
110
111 static int get_serverzone(void)
112 {
113         static int serverzone;
114
115         if (!done_serverzone_init) {
116                 serverzone = TimeZone(time(NULL));
117
118                 if ((serverzone % 60) != 0) {
119                         DEBUG(1,("WARNING: Your timezone is not a multiple of 1 minute.\n"));
120                 }
121
122                 DEBUG(4,("Serverzone is %d\n",serverzone));
123
124                 done_serverzone_init = True;
125         }
126
127         return serverzone;
128 }
129
130 /* Re-read the smb serverzone value */
131
132 void TimeInit(void)
133 {
134         done_serverzone_init = False;
135         get_serverzone();
136 }
137
138 /*******************************************************************
139 return the same value as TimeZone, but it should be more efficient.
140
141 We keep a table of DST offsets to prevent calling localtime() on each 
142 call of this function. This saves a LOT of time on many unixes.
143
144 Updated by Paul Eggert <eggert@twinsun.com>
145 ********************************************************************/
146 static int TimeZoneFaster(time_t t)
147 {
148   static struct dst_table {time_t start,end; int zone;} *tdt, *dst_table = NULL;
149   static int table_size = 0;
150   int i;
151   int zone = 0;
152
153   if (t == 0) t = time(NULL);
154
155   /* Tunis has a 8 day DST region, we need to be careful ... */
156 #define MAX_DST_WIDTH (365*24*60*60)
157 #define MAX_DST_SKIP (7*24*60*60)
158
159   for (i=0;i<table_size;i++)
160     if (t >= dst_table[i].start && t <= dst_table[i].end) break;
161
162   if (i<table_size) {
163     zone = dst_table[i].zone;
164   } else {
165     time_t low,high;
166
167     zone = TimeZone(t);
168     tdt = (struct dst_table *)Realloc(dst_table,
169                                               sizeof(dst_table[0])*(i+1));
170     if (!tdt) {
171       DEBUG(0,("TimeZoneFaster: out of memory!\n"));
172       SAFE_FREE(dst_table);
173       table_size = 0;
174     } else {
175       dst_table = tdt;
176       table_size++;
177
178       dst_table[i].zone = zone; 
179       dst_table[i].start = dst_table[i].end = t;
180     
181       /* no entry will cover more than 6 months */
182       low = t - MAX_DST_WIDTH/2;
183       if (t < low)
184         low = TIME_T_MIN;
185       
186       high = t + MAX_DST_WIDTH/2;
187       if (high < t)
188         high = TIME_T_MAX;
189       
190       /* widen the new entry using two bisection searches */
191       while (low+60*60 < dst_table[i].start) {
192         if (dst_table[i].start - low > MAX_DST_SKIP*2)
193           t = dst_table[i].start - MAX_DST_SKIP;
194         else
195           t = low + (dst_table[i].start-low)/2;
196         if (TimeZone(t) == zone)
197           dst_table[i].start = t;
198         else
199           low = t;
200       }
201
202       while (high-60*60 > dst_table[i].end) {
203         if (high - dst_table[i].end > MAX_DST_SKIP*2)
204           t = dst_table[i].end + MAX_DST_SKIP;
205         else
206           t = high - (high-dst_table[i].end)/2;
207         if (TimeZone(t) == zone)
208           dst_table[i].end = t;
209         else
210           high = t;
211       }
212 #if 0
213       DEBUG(1,("Added DST entry from %s ",
214                asctime(localtime(&dst_table[i].start))));
215       DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table[i].end)),
216                dst_table[i].zone));
217 #endif
218     }
219   }
220   return zone;
221 }
222
223 /****************************************************************************
224   return the UTC offset in seconds west of UTC, adjusted for extra time offset
225   **************************************************************************/
226 int TimeDiff(time_t t)
227 {
228   return TimeZoneFaster(t) + 60*extra_time_offset;
229 }
230
231
232 /****************************************************************************
233   return the UTC offset in seconds west of UTC, adjusted for extra time
234   offset, for a local time value.  If ut = lt + LocTimeDiff(lt), then
235   lt = ut - TimeDiff(ut), but the converse does not necessarily hold near
236   daylight savings transitions because some local times are ambiguous.
237   LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions.
238   +**************************************************************************/
239 static int LocTimeDiff(time_t lte)
240 {
241   time_t lt = lte - 60*extra_time_offset;
242   int d = TimeZoneFaster(lt);
243   time_t t = lt + d;
244
245   /* if overflow occurred, ignore all the adjustments so far */
246   if (((lte < lt) ^ (extra_time_offset < 0))  |  ((t < lt) ^ (d < 0)))
247     t = lte;
248
249   /* now t should be close enough to the true UTC to yield the right answer */
250   return TimeDiff(t);
251 }
252
253
254 /****************************************************************************
255 try to optimise the localtime call, it can be quite expensive on some machines
256 ****************************************************************************/
257 struct tm *LocalTime(time_t *t)
258 {
259   time_t t2 = *t;
260
261   t2 -= TimeDiff(t2);
262
263   return(gmtime(&t2));
264 }
265
266 #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
267
268 /****************************************************************************
269 interpret an 8 byte "filetime" structure to a time_t
270 It's originally in "100ns units since jan 1st 1601"
271
272 It appears to be kludge-GMT (at least for file listings). This means
273 its the GMT you get by taking a localtime and adding the
274 serverzone. This is NOT the same as GMT in some cases. This routine
275 converts this to real GMT.
276 ****************************************************************************/
277 time_t nt_time_to_unix(NTTIME *nt)
278 {
279   double d;
280   time_t ret;
281   /* The next two lines are a fix needed for the 
282      broken SCO compiler. JRA. */
283   time_t l_time_min = TIME_T_MIN;
284   time_t l_time_max = TIME_T_MAX;
285
286   if (nt->high == 0) return(0);
287
288   d = ((double)nt->high)*4.0*(double)(1<<30);
289   d += (nt->low&0xFFF00000);
290   d *= 1.0e-7;
291  
292   /* now adjust by 369 years to make the secs since 1970 */
293   d -= TIME_FIXUP_CONSTANT;
294
295   if (!(l_time_min <= d && d <= l_time_max))
296     return(0);
297
298   ret = (time_t)(d+0.5);
299
300   /* this takes us from kludge-GMT to real GMT */
301   ret -= get_serverzone();
302   ret += LocTimeDiff(ret);
303
304   return(ret);
305 }
306
307 /****************************************************************************
308 convert a NTTIME structure to a time_t
309 It's originally in "100ns units"
310
311 this is an absolute version of the one above.
312 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
313 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
314 ****************************************************************************/
315 time_t nt_time_to_unix_abs(NTTIME *nt)
316 {
317         double d;
318         time_t ret;
319         /* The next two lines are a fix needed for the 
320            broken SCO compiler. JRA. */
321         time_t l_time_min = TIME_T_MIN;
322         time_t l_time_max = TIME_T_MAX;
323
324         if (nt->high == 0)
325                 return(0);
326
327         if (nt->high==0x80000000 && nt->low==0)
328                 return -1;
329
330         /* reverse the time */
331         /* it's a negative value, turn it to positive */
332         nt->high=~nt->high;
333         nt->low=~nt->low;
334
335         d = ((double)nt->high)*4.0*(double)(1<<30);
336         d += (nt->low&0xFFF00000);
337         d *= 1.0e-7;
338   
339         if (!(l_time_min <= d && d <= l_time_max))
340                 return(0);
341
342         ret = (time_t)(d+0.5);
343
344         /* this takes us from kludge-GMT to real GMT */
345         ret -= get_serverzone();
346         ret += LocTimeDiff(ret);
347
348         return(ret);
349 }
350
351
352
353 /****************************************************************************
354 interprets an nt time into a unix time_t
355 ****************************************************************************/
356 time_t interpret_long_date(char *p)
357 {
358         NTTIME nt;
359         nt.low = IVAL(p,0);
360         nt.high = IVAL(p,4);
361         return nt_time_to_unix(&nt);
362 }
363
364 /****************************************************************************
365 put a 8 byte filetime from a time_t
366 This takes real GMT as input and converts to kludge-GMT
367 ****************************************************************************/
368 void unix_to_nt_time(NTTIME *nt, time_t t)
369 {
370         double d;
371
372         if (t==0)
373         {
374                 nt->low = 0;
375                 nt->high = 0;
376                 return;
377         }
378         if (t == TIME_T_MAX)
379         {
380                 nt->low = 0xffffffff;
381                 nt->high = 0x7fffffff;
382                 return;
383         }               
384         if (t == -1)
385         {
386                 nt->low = 0xffffffff;
387                 nt->high = 0xffffffff;
388                 return;
389         }               
390
391         /* this converts GMT to kludge-GMT */
392         t -= LocTimeDiff(t) - get_serverzone(); 
393
394         d = (double)(t);
395         d += TIME_FIXUP_CONSTANT;
396         d *= 1.0e7;
397
398         nt->high = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
399         nt->low  = (uint32)(d - ((double)nt->high)*4.0*(double)(1<<30));
400 }
401
402 /****************************************************************************
403 convert a time_t to a NTTIME structure
404
405 this is an absolute version of the one above.
406 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
407 if the nttime_t was 5 seconds, the NTTIME is 5 seconds. JFM
408 ****************************************************************************/
409 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
410 {
411         double d;
412
413         if (t==0) {
414                 nt->low = 0;
415                 nt->high = 0;
416                 return;
417         }
418
419         if (t == TIME_T_MAX) {
420                 nt->low = 0xffffffff;
421                 nt->high = 0x7fffffff;
422                 return;
423         }
424                 
425         if (t == -1) {
426                 /* that's what NT uses for infinite */
427                 nt->low = 0x0;
428                 nt->high = 0x80000000;
429                 return;
430         }               
431
432         /* this converts GMT to kludge-GMT */
433         t -= LocTimeDiff(t) - get_serverzone(); 
434
435         d = (double)(t);
436         d *= 1.0e7;
437
438         nt->high = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
439         nt->low  = (uint32)(d - ((double)nt->high)*4.0*(double)(1<<30));
440
441         /* convert to a negative value */
442         nt->high=~nt->high;
443         nt->low=~nt->low;
444 }
445
446
447 /****************************************************************************
448 take an NTTIME structure, containing high / low time.  convert to unix time.
449 lkclXXXX this may need 2 SIVALs not a memcpy.  we'll see...
450 ****************************************************************************/
451 void put_long_date(char *p,time_t t)
452 {
453         NTTIME nt;
454         unix_to_nt_time(&nt, t);
455         SIVAL(p, 0, nt.low);
456         SIVAL(p, 4, nt.high);
457 }
458
459 /****************************************************************************
460 check if it's a null mtime
461 ****************************************************************************/
462 BOOL null_mtime(time_t mtime)
463 {
464   if (mtime == 0 || mtime == 0xFFFFFFFF || mtime == (time_t)-1)
465     return(True);
466   return(False);
467 }
468
469 /*******************************************************************
470   create a 16 bit dos packed date
471 ********************************************************************/
472 static uint16 make_dos_date1(struct tm *t)
473 {
474   uint16 ret=0;
475   ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
476   ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
477   return(ret);
478 }
479
480 /*******************************************************************
481   create a 16 bit dos packed time
482 ********************************************************************/
483 static uint16 make_dos_time1(struct tm *t)
484 {
485   uint16 ret=0;
486   ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3));
487   ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
488   return(ret);
489 }
490
491 /*******************************************************************
492   create a 32 bit dos packed date/time from some parameters
493   This takes a GMT time and returns a packed localtime structure
494 ********************************************************************/
495 static uint32 make_dos_date(time_t unixdate)
496 {
497   struct tm *t;
498   uint32 ret=0;
499
500   t = LocalTime(&unixdate);
501   if (!t)
502     return 0xFFFFFFFF;
503
504   ret = make_dos_date1(t);
505   ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
506
507   return(ret);
508 }
509
510 /*******************************************************************
511 put a dos date into a buffer (time/date format)
512 This takes GMT time and puts local time in the buffer
513 ********************************************************************/
514 void put_dos_date(char *buf,int offset,time_t unixdate)
515 {
516   uint32 x = make_dos_date(unixdate);
517   SIVAL(buf,offset,x);
518 }
519
520 /*******************************************************************
521 put a dos date into a buffer (date/time format)
522 This takes GMT time and puts local time in the buffer
523 ********************************************************************/
524 void put_dos_date2(char *buf,int offset,time_t unixdate)
525 {
526   uint32 x = make_dos_date(unixdate);
527   x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
528   SIVAL(buf,offset,x);
529 }
530
531 /*******************************************************************
532 put a dos 32 bit "unix like" date into a buffer. This routine takes
533 GMT and converts it to LOCAL time before putting it (most SMBs assume
534 localtime for this sort of date)
535 ********************************************************************/
536 void put_dos_date3(char *buf,int offset,time_t unixdate)
537 {
538   if (!null_mtime(unixdate))
539     unixdate -= TimeDiff(unixdate);
540   SIVAL(buf,offset,unixdate);
541 }
542
543 /*******************************************************************
544   interpret a 32 bit dos packed date/time to some parameters
545 ********************************************************************/
546 static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second)
547 {
548   uint32 p0,p1,p2,p3;
549
550   p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
551   p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
552
553   *second = 2*(p0 & 0x1F);
554   *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
555   *hour = (p1>>3)&0xFF;
556   *day = (p2&0x1F);
557   *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
558   *year = ((p3>>1)&0xFF) + 80;
559 }
560
561 /*******************************************************************
562   create a unix date (int GMT) from a dos date (which is actually in
563   localtime)
564 ********************************************************************/
565 time_t make_unix_date(void *date_ptr)
566 {
567   uint32 dos_date=0;
568   struct tm t;
569   time_t ret;
570
571   dos_date = IVAL(date_ptr,0);
572
573   if (dos_date == 0) return(0);
574   
575   interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
576                      &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
577   t.tm_isdst = -1;
578   
579   /* mktime() also does the local to GMT time conversion for us */
580   ret = mktime(&t);
581
582   return(ret);
583 }
584
585 /*******************************************************************
586 like make_unix_date() but the words are reversed
587 ********************************************************************/
588 time_t make_unix_date2(void *date_ptr)
589 {
590   uint32 x,x2;
591
592   x = IVAL(date_ptr,0);
593   x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
594   SIVAL(&x,0,x2);
595
596   return(make_unix_date((void *)&x));
597 }
598
599 /*******************************************************************
600   create a unix GMT date from a dos date in 32 bit "unix like" format
601   these generally arrive as localtimes, with corresponding DST
602   ******************************************************************/
603 time_t make_unix_date3(void *date_ptr)
604 {
605   time_t t = (time_t)IVAL(date_ptr,0);
606   if (!null_mtime(t))
607     t += LocTimeDiff(t);
608   return(t);
609 }
610
611
612 /***************************************************************************
613 return a HTTP/1.0 time string
614   ***************************************************************************/
615 char *http_timestring(time_t t)
616 {
617   static fstring buf;
618   struct tm *tm = LocalTime(&t);
619
620   if (!tm)
621     slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t);
622   else
623 #ifndef HAVE_STRFTIME
624   fstrcpy(buf, asctime(tm));
625   if(buf[strlen(buf)-1] == '\n')
626     buf[strlen(buf)-1] = 0;
627 #else /* !HAVE_STRFTIME */
628   strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
629 #endif /* !HAVE_STRFTIME */
630   return buf;
631 }
632
633
634
635 /****************************************************************************
636  Return the date and time as a string
637 ****************************************************************************/
638
639 char *timestring(BOOL hires)
640 {
641         static fstring TimeBuf;
642         struct timeval tp;
643         time_t t;
644         struct tm *tm;
645
646         if (hires) {
647                 GetTimeOfDay(&tp);
648                 t = (time_t)tp.tv_sec;
649         } else {
650                 t = time(NULL);
651         }
652         tm = LocalTime(&t);
653         if (!tm) {
654                 if (hires) {
655                         slprintf(TimeBuf,
656                                  sizeof(TimeBuf)-1,
657                                  "%ld.%06ld seconds since the Epoch",
658                                  (long)tp.tv_sec, 
659                                  (long)tp.tv_usec);
660                 } else {
661                         slprintf(TimeBuf,
662                                  sizeof(TimeBuf)-1,
663                                  "%ld seconds since the Epoch",
664                                  (long)t);
665                 }
666         } else {
667 #ifdef HAVE_STRFTIME
668                 if (hires) {
669                         strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
670                         slprintf(TimeBuf+strlen(TimeBuf),
671                                  sizeof(TimeBuf)-1 - strlen(TimeBuf), 
672                                  ".%06ld", 
673                                  (long)tp.tv_usec);
674                 } else {
675                         strftime(TimeBuf,100,"%Y/%m/%d %H:%M:%S",tm);
676                 }
677 #else
678                 if (hires) {
679                         slprintf(TimeBuf, 
680                                  sizeof(TimeBuf)-1, 
681                                  "%s.%06ld", 
682                                  asctime(tm), 
683                                  (long)tp.tv_usec);
684                 } else {
685                         fstrcpy(TimeBuf, asctime(tm));
686                 }
687 #endif
688         }
689         return(TimeBuf);
690 }
691
692 /****************************************************************************
693   return the best approximation to a 'create time' under UNIX from a stat
694   structure.
695 ****************************************************************************/
696
697 time_t get_create_time(SMB_STRUCT_STAT *st,BOOL fake_dirs)
698 {
699   time_t ret, ret1;
700
701   if(S_ISDIR(st->st_mode) && fake_dirs)
702     return (time_t)315493200L;          /* 1/1/1980 */
703     
704   ret = MIN(st->st_ctime, st->st_mtime);
705   ret1 = MIN(ret, st->st_atime);
706
707   if(ret1 != (time_t)0)
708     return ret1;
709
710   /*
711    * One of ctime, mtime or atime was zero (probably atime).
712    * Just return MIN(ctime, mtime).
713    */
714   return ret;
715 }
716
717 /****************************************************************************
718 initialise an NTTIME to -1, which means "unknown" or "don't expire"
719 ****************************************************************************/
720
721 void init_nt_time(NTTIME *nt)
722 {
723         nt->high = 0x7FFFFFFF;
724         nt->low = 0xFFFFFFFF;
725 }