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