summaryrefslogtreecommitdiffstats
path: root/src/lib/krb4/mac_time.c
blob: 28a56223dc8496d27b7293e5cda4aa2b0e5e9de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * mac_time.c
 * (Originally time_stuff.c)
 *
 * Copyright 1989 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Macintosh ooperating system interface for Kerberos.
 */

#include "mit-copyright.h"
#define	DEFINE_SOCKADDR		/* For struct hostent, <netdb.h>, etc */
#define NEED_TIME_H
#include "krb.h"
#include "des.h"
#include "AddressXlation.h"	/* for ip_addr */

#include <script.h>			/* Defines MachineLocation, used by getTimeZoneOffset */
#include <ToolUtils.h>		/* Defines BitTst(), called by getTimeZoneOffset() */
#include <OSUtils.h>		/* Defines GetDateTime */

/* Mac Cincludes */
#include <string.h>
#include <stddef.h>


  /*******************************
  The Unix epoch is 1/1/70, the Mac epoch is 1/1/04.

  70 - 4 = 66 year differential

  Thus the offset is:

  (66 yrs) * (365 days/yr) * (24 hours/day) * (60 mins/hour) * (60 secs/min)
  plus
  (17 leap days) * (24 hours/day) * (60 mins/hour) * (60 secs/min)

  Don't forget the offset from GMT.
  *******************************/


/* returns the offset in hours between the mac local time and the GMT  */

unsigned long
getTimeZoneOffset()
{
	MachineLocation		macLocation;
	long			gmtDelta;

	macLocation.gmtFlags.gmtDelta=0L;
	ReadLocation(&macLocation); 
	gmtDelta=macLocation.gmtFlags.gmtDelta & 0x00FFFFFF;
	if (BitTst((void *)&gmtDelta,23L))	gmtDelta |= 0xFF000000;
	gmtDelta /= 3600L;
	return(gmtDelta);
}


/* Returns the GMT in seconds using the Unix epoch, ie. Net time */

static unsigned long
gettimeofdaynet_no_offset()
{
	time_t the_time;
	
	GetDateTime (&the_time);
	the_time = the_time - 
		((66 * 365 * 24 * 60 * 60) + 
		      (17 *  24 * 60 * 60) +
           (getTimeZoneOffset() * 60 * 60));
	return the_time;
}



int	
gettimeofdaynet (struct timeval *tp, struct timezone *tz)
{ 
	tp->tv_sec = gettimeofdaynet_no_offset();
	return 0;
}


#if 0

int	
gettimeofdaynet (struct timeval *tp, struct timezone *tz)
{
	int result;
	
	if (!net_got_offset)
		result = get_net_offset();
	else result = 0;
	
	time ((time_t *) &(tp->tv_sec));

	tp->tv_sec = tp->tv_sec - (66 * 365 * 24 * 60 * 60
            + 17 * 60 * 60 * 24) + net_offset;

	return (result);
}


#define TIME_PORT 37
#define TM_OFFSET 2208988800

/*
 *
 *   get_net_offset () -- Use UDP time protocol to figure out the
 *	offset between what the Mac thinks the time is an what
 *	the network thinks.
 *
 */
int
get_net_offset()
{
     time_t tv;
     char buf[512],ts[256];
     long *nettime;
     int attempts, cc, time_port;
     long unixtime;
	 char	realm[REALM_SZ];
	 ip_addr	fromaddr;
	 unsigned short	fromport;
	 int result;
	 
     nettime = (long *)buf;
	 time_port = TIME_PORT;

	 cc = sizeof(buf);
	 result = hosts_send_recv(ts, 1, buf, &cc, "", time_port);
     time (&tv);
	 
	 if (result!=KSUCCESS || cc<4) {
	 	net_offset = 0;
	 	if (!result) result = 100;
	 	return result;
	 }
						
     unixtime = (long) ntohl(*nettime) - TM_OFFSET;

     tv  -= 66 * 365 * 24 * 60 * 60
	  + 17 * 60 * 60 * 24;			/* Convert to unix time w/o offset */
     net_offset = unixtime - tv;
     net_got_offset = 1;
     
     return 0;
}

#endif