1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/toffset.c - Manipulate time offset fields in os context */
3 /*
4 * Copyright 1995, 2007 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
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 * If the real time microseconds are given as -1 the caller doesn't
37 * know the microseconds value so the usec offset is always zero.
38 */
39 krb5_error_code KRB5_CALLCONV
krb5_set_real_time(krb5_context context,krb5_timestamp seconds,krb5_int32 microseconds)40 krb5_set_real_time(krb5_context context, krb5_timestamp seconds, krb5_int32 microseconds)
41 {
42 krb5_os_context os_ctx = &context->os_context;
43 krb5_timestamp sec;
44 krb5_int32 usec;
45 krb5_error_code retval;
46
47 retval = krb5_crypto_us_timeofday(&sec, &usec);
48 if (retval)
49 return retval;
50
51 os_ctx->time_offset = ts_delta(seconds, sec);
52 os_ctx->usec_offset = (microseconds > -1) ? microseconds - usec : 0;
53
54 os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_TIME) |
55 KRB5_OS_TOFFSET_VALID);
56 return 0;
57 }
58
59 /*
60 * This routine sets the krb5 time routines so that they will return
61 * the seconds and microseconds value as input to this function. This
62 * is useful for running the krb5 routines through test suites
63 */
64 krb5_error_code
krb5_set_debugging_time(krb5_context context,krb5_timestamp seconds,krb5_int32 microseconds)65 krb5_set_debugging_time(krb5_context context, krb5_timestamp seconds, krb5_int32 microseconds)
66 {
67 krb5_os_context os_ctx = &context->os_context;
68
69 os_ctx->time_offset = seconds;
70 os_ctx->usec_offset = microseconds;
71 os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_VALID) |
72 KRB5_OS_TOFFSET_TIME);
73 return 0;
74 }
75
76 /*
77 * This routine turns off the time correction fields, so that the krb5
78 * routines return the "natural" time.
79 */
80 krb5_error_code
krb5_use_natural_time(krb5_context context)81 krb5_use_natural_time(krb5_context context)
82 {
83 krb5_os_context os_ctx = &context->os_context;
84
85 os_ctx->os_flags &= ~(KRB5_OS_TOFFSET_VALID|KRB5_OS_TOFFSET_TIME);
86
87 return 0;
88 }
89
90 /*
91 * This routine returns the current time offsets in use.
92 */
93 krb5_error_code KRB5_CALLCONV
krb5_get_time_offsets(krb5_context context,krb5_timestamp * seconds,krb5_int32 * microseconds)94 krb5_get_time_offsets(krb5_context context, krb5_timestamp *seconds, krb5_int32 *microseconds)
95 {
96 krb5_os_context os_ctx = &context->os_context;
97
98 if (seconds)
99 *seconds = os_ctx->time_offset;
100 if (microseconds)
101 *microseconds = os_ctx->usec_offset;
102 return 0;
103 }
104
105
106 /*
107 * This routine sets the time offsets directly.
108 */
109 krb5_error_code
krb5_set_time_offsets(krb5_context context,krb5_timestamp seconds,krb5_int32 microseconds)110 krb5_set_time_offsets(krb5_context context, krb5_timestamp seconds, krb5_int32 microseconds)
111 {
112 krb5_os_context os_ctx = &context->os_context;
113
114 os_ctx->time_offset = seconds;
115 os_ctx->usec_offset = microseconds;
116 os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_TIME) |
117 KRB5_OS_TOFFSET_VALID);
118 return 0;
119 }
120