1 /*
2  * lib/krb5/os/toffset.c
3  *
4  * Copyright 1995 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  *
26  * These routines manipulates the time offset fields in the os context.
27  */
28 
29 #include "k5-int.h"
30 
31 /*
32  * This routine takes the "real time" as input, and sets the time
33  * offset field in the context structure so that the krb5 time
34  * routines will return the correct time as corrected by difference
35  * between the system time and the "real time" as passed to this
36  * routine
37  */
38 krb5_error_code KRB5_CALLCONV
krb5_set_real_time(krb5_context context,krb5_timestamp seconds,krb5_int32 microseconds)39 krb5_set_real_time(krb5_context context, krb5_timestamp seconds, krb5_int32 microseconds)
40 {
41     krb5_os_context os_ctx = context->os_context;
42     krb5_int32 sec, usec;
43     krb5_error_code retval;
44 
45     retval = krb5_crypto_us_timeofday(&sec, &usec);
46     if (retval)
47 	    return retval;
48     os_ctx->time_offset = seconds - sec;
49     os_ctx->usec_offset = microseconds - usec;
50     os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_TIME) |
51 			KRB5_OS_TOFFSET_VALID);
52     return 0;
53 }
54 
55 /*
56  * This routine sets the krb5 time routines so that they will return
57  * the seconds and microseconds value as input to this function.  This
58  * is useful for running the krb5 routines through test suites
59  */
60 krb5_error_code
krb5_set_debugging_time(krb5_context context,krb5_timestamp seconds,krb5_int32 microseconds)61 krb5_set_debugging_time(krb5_context context, krb5_timestamp seconds, krb5_int32 microseconds)
62 {
63     krb5_os_context os_ctx = context->os_context;
64 
65     os_ctx->time_offset = seconds;
66     os_ctx->usec_offset = microseconds;
67     os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_VALID) |
68 			KRB5_OS_TOFFSET_TIME);
69     return 0;
70 }
71 
72 /*
73  * This routine turns off the time correction fields, so that the krb5
74  * routines return the "natural" time.
75  */
76 krb5_error_code
krb5_use_natural_time(krb5_context context)77 krb5_use_natural_time(krb5_context context)
78 {
79     krb5_os_context os_ctx = context->os_context;
80 
81     os_ctx->os_flags &= ~(KRB5_OS_TOFFSET_VALID|KRB5_OS_TOFFSET_TIME);
82 
83     return 0;
84 }
85 
86 /*
87  * This routine returns the current time offsets in use.
88  */
89 krb5_error_code KRB5_CALLCONV
krb5_get_time_offsets(krb5_context context,krb5_timestamp * seconds,krb5_int32 * microseconds)90 krb5_get_time_offsets(krb5_context context, krb5_timestamp *seconds, krb5_int32 *microseconds)
91 {
92     krb5_os_context os_ctx = context->os_context;
93 
94     if (seconds)
95 	*seconds = os_ctx->time_offset;
96     if (microseconds)
97 	*microseconds = os_ctx->usec_offset;
98     return 0;
99 }
100 
101 
102 /*
103  * This routine sets the time offsets directly.
104  */
105 krb5_error_code
krb5_set_time_offsets(krb5_context context,krb5_timestamp seconds,krb5_int32 microseconds)106 krb5_set_time_offsets(krb5_context context, krb5_timestamp seconds, krb5_int32 microseconds)
107 {
108     krb5_os_context os_ctx = context->os_context;
109 
110     os_ctx->time_offset = seconds;
111     os_ctx->usec_offset = microseconds;
112     os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_TIME) |
113 			KRB5_OS_TOFFSET_VALID);
114     return 0;
115 }
116