456193a41aed9bb145d3a6be6644eb05fc4533d1
[platform/upstream/krb5.git] / src / lib / krb5 / os / toffset.c
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
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_int32 sec, usec;
44     krb5_error_code retval;
45
46     retval = krb5_crypto_us_timeofday(&sec, &usec);
47     if (retval)
48         return retval;
49
50     os_ctx->time_offset = seconds - sec;
51     os_ctx->usec_offset = (microseconds > -1) ? microseconds - usec : 0;
52
53     os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_TIME) |
54                         KRB5_OS_TOFFSET_VALID);
55     return 0;
56 }
57
58 /*
59  * This routine sets the krb5 time routines so that they will return
60  * the seconds and microseconds value as input to this function.  This
61  * is useful for running the krb5 routines through test suites
62  */
63 krb5_error_code
64 krb5_set_debugging_time(krb5_context context, krb5_timestamp seconds, krb5_int32 microseconds)
65 {
66     krb5_os_context os_ctx = &context->os_context;
67
68     os_ctx->time_offset = seconds;
69     os_ctx->usec_offset = microseconds;
70     os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_VALID) |
71                         KRB5_OS_TOFFSET_TIME);
72     return 0;
73 }
74
75 /*
76  * This routine turns off the time correction fields, so that the krb5
77  * routines return the "natural" time.
78  */
79 krb5_error_code
80 krb5_use_natural_time(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 KRB5_CALLCONV
93 krb5_get_time_offsets(krb5_context context, krb5_timestamp *seconds, krb5_int32 *microseconds)
94 {
95     krb5_os_context os_ctx = &context->os_context;
96
97     if (seconds)
98         *seconds = os_ctx->time_offset;
99     if (microseconds)
100         *microseconds = os_ctx->usec_offset;
101     return 0;
102 }
103
104
105 /*
106  * This routine sets the time offsets directly.
107  */
108 krb5_error_code
109 krb5_set_time_offsets(krb5_context context, krb5_timestamp seconds, krb5_int32 microseconds)
110 {
111     krb5_os_context os_ctx = &context->os_context;
112
113     os_ctx->time_offset = seconds;
114     os_ctx->usec_offset = microseconds;
115     os_ctx->os_flags = ((os_ctx->os_flags & ~KRB5_OS_TOFFSET_TIME) |
116                         KRB5_OS_TOFFSET_VALID);
117     return 0;
118 }