xref: /freebsd-src/crypto/heimdal/lib/krb5/time.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "krb5_locl.h"
35b528cefcSMark Murray 
36*ae771770SStanislav Sedov /**
37c19800e8SDoug Rabson  * Set the absolute time that the caller knows the kdc has so the
38c19800e8SDoug Rabson  * kerberos library can calculate the relative diffrence beteen the
39c19800e8SDoug Rabson  * KDC time and local system time.
40*ae771770SStanislav Sedov  *
41*ae771770SStanislav Sedov  * @param context Keberos 5 context.
42*ae771770SStanislav Sedov  * @param sec The applications new of "now" in seconds
43*ae771770SStanislav Sedov  * @param usec The applications new of "now" in micro seconds
44*ae771770SStanislav Sedov 
45*ae771770SStanislav Sedov  * @return Kerberos 5 error code, see krb5_get_error_message().
46*ae771770SStanislav Sedov  *
47*ae771770SStanislav Sedov  * @ingroup krb5
48c19800e8SDoug Rabson  */
49c19800e8SDoug Rabson 
50*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_set_real_time(krb5_context context,krb5_timestamp sec,int32_t usec)51c19800e8SDoug Rabson krb5_set_real_time (krb5_context context,
52c19800e8SDoug Rabson 		    krb5_timestamp sec,
53c19800e8SDoug Rabson 		    int32_t usec)
54c19800e8SDoug Rabson {
55c19800e8SDoug Rabson     struct timeval tv;
56c19800e8SDoug Rabson 
57c19800e8SDoug Rabson     gettimeofday(&tv, NULL);
58c19800e8SDoug Rabson 
59c19800e8SDoug Rabson     context->kdc_sec_offset = sec - tv.tv_sec;
60*ae771770SStanislav Sedov 
61*ae771770SStanislav Sedov     /**
62*ae771770SStanislav Sedov      * If the caller passes in a negative usec, its assumed to be
63*ae771770SStanislav Sedov      * unknown and the function will use the current time usec.
64*ae771770SStanislav Sedov      */
65*ae771770SStanislav Sedov     if (usec >= 0) {
66c19800e8SDoug Rabson 	context->kdc_usec_offset = usec - tv.tv_usec;
67c19800e8SDoug Rabson 
68c19800e8SDoug Rabson 	if (context->kdc_usec_offset < 0) {
69c19800e8SDoug Rabson 	    context->kdc_sec_offset--;
70c19800e8SDoug Rabson 	    context->kdc_usec_offset += 1000000;
71c19800e8SDoug Rabson 	}
72*ae771770SStanislav Sedov     } else
73*ae771770SStanislav Sedov 	context->kdc_usec_offset = tv.tv_usec;
74*ae771770SStanislav Sedov 
75c19800e8SDoug Rabson     return 0;
76c19800e8SDoug Rabson }
7713e3f4d6SMark Murray 
7813e3f4d6SMark Murray /*
7913e3f4d6SMark Murray  * return ``corrected'' time in `timeret'.
8013e3f4d6SMark Murray  */
81b528cefcSMark Murray 
82*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_timeofday(krb5_context context,krb5_timestamp * timeret)83b528cefcSMark Murray krb5_timeofday (krb5_context context,
8413e3f4d6SMark Murray 		krb5_timestamp *timeret)
85b528cefcSMark Murray {
86b528cefcSMark Murray     *timeret = time(NULL) + context->kdc_sec_offset;
87b528cefcSMark Murray     return 0;
88b528cefcSMark Murray }
89b528cefcSMark Murray 
9013e3f4d6SMark Murray /*
9113e3f4d6SMark Murray  * like gettimeofday but with time correction to the KDC
9213e3f4d6SMark Murray  */
9313e3f4d6SMark Murray 
94*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_us_timeofday(krb5_context context,krb5_timestamp * sec,int32_t * usec)95b528cefcSMark Murray krb5_us_timeofday (krb5_context context,
96c19800e8SDoug Rabson 		   krb5_timestamp *sec,
97b528cefcSMark Murray 		   int32_t *usec)
98b528cefcSMark Murray {
99b528cefcSMark Murray     struct timeval tv;
100b528cefcSMark Murray 
101b528cefcSMark Murray     gettimeofday (&tv, NULL);
102b528cefcSMark Murray 
103b528cefcSMark Murray     *sec  = tv.tv_sec + context->kdc_sec_offset;
104b528cefcSMark Murray     *usec = tv.tv_usec;		/* XXX */
105b528cefcSMark Murray     return 0;
106b528cefcSMark Murray }
1075e9cd1aeSAssar Westerlund 
108*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_format_time(krb5_context context,time_t t,char * s,size_t len,krb5_boolean include_time)1095e9cd1aeSAssar Westerlund krb5_format_time(krb5_context context, time_t t,
1105e9cd1aeSAssar Westerlund 		 char *s, size_t len, krb5_boolean include_time)
1115e9cd1aeSAssar Westerlund {
1125e9cd1aeSAssar Westerlund     struct tm *tm;
1135e9cd1aeSAssar Westerlund     if(context->log_utc)
1145e9cd1aeSAssar Westerlund 	tm = gmtime (&t);
1155e9cd1aeSAssar Westerlund     else
1165e9cd1aeSAssar Westerlund 	tm = localtime(&t);
117c19800e8SDoug Rabson     if(tm == NULL ||
118c19800e8SDoug Rabson        strftime(s, len, include_time ? context->time_fmt : context->date_fmt, tm) == 0)
119c19800e8SDoug Rabson 	snprintf(s, len, "%ld", (long)t);
1205e9cd1aeSAssar Westerlund     return 0;
1215e9cd1aeSAssar Westerlund }
122adb0ddaeSAssar Westerlund 
123*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_string_to_deltat(const char * string,krb5_deltat * deltat)124adb0ddaeSAssar Westerlund krb5_string_to_deltat(const char *string, krb5_deltat *deltat)
125adb0ddaeSAssar Westerlund {
126adb0ddaeSAssar Westerlund     if((*deltat = parse_time(string, "s")) == -1)
127c19800e8SDoug Rabson 	return KRB5_DELTAT_BADFORMAT;
128adb0ddaeSAssar Westerlund     return 0;
129adb0ddaeSAssar Westerlund }
130