155682Smarkm/* 2233294Sstas * Copyright (c) 1997-2004 Kungliga Tekniska H��gskolan 3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4233294Sstas * All rights reserved. 555682Smarkm * 6233294Sstas * Redistribution and use in source and binary forms, with or without 7233294Sstas * modification, are permitted provided that the following conditions 8233294Sstas * are met: 955682Smarkm * 10233294Sstas * 1. Redistributions of source code must retain the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer. 1255682Smarkm * 13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 14233294Sstas * notice, this list of conditions and the following disclaimer in the 15233294Sstas * documentation and/or other materials provided with the distribution. 1655682Smarkm * 17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors 18233294Sstas * may be used to endorse or promote products derived from this software 19233294Sstas * without specific prior written permission. 2055682Smarkm * 21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31233294Sstas * SUCH DAMAGE. 3255682Smarkm */ 3355682Smarkm 3455682Smarkm#include "krb5_locl.h" 3555682Smarkm 36233294Sstas/** 37178825Sdfr * Set the absolute time that the caller knows the kdc has so the 38178825Sdfr * kerberos library can calculate the relative diffrence beteen the 39178825Sdfr * KDC time and local system time. 40233294Sstas * 41233294Sstas * @param context Keberos 5 context. 42233294Sstas * @param sec The applications new of "now" in seconds 43233294Sstas * @param usec The applications new of "now" in micro seconds 44233294Sstas 45233294Sstas * @return Kerberos 5 error code, see krb5_get_error_message(). 46233294Sstas * 47233294Sstas * @ingroup krb5 48178825Sdfr */ 49178825Sdfr 50233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 51178825Sdfrkrb5_set_real_time (krb5_context context, 52178825Sdfr krb5_timestamp sec, 53178825Sdfr int32_t usec) 54178825Sdfr{ 55178825Sdfr struct timeval tv; 56233294Sstas 57178825Sdfr gettimeofday(&tv, NULL); 58178825Sdfr 59178825Sdfr context->kdc_sec_offset = sec - tv.tv_sec; 60178825Sdfr 61233294Sstas /** 62233294Sstas * If the caller passes in a negative usec, its assumed to be 63233294Sstas * unknown and the function will use the current time usec. 64233294Sstas */ 65233294Sstas if (usec >= 0) { 66233294Sstas context->kdc_usec_offset = usec - tv.tv_usec; 67233294Sstas 68233294Sstas if (context->kdc_usec_offset < 0) { 69233294Sstas context->kdc_sec_offset--; 70233294Sstas context->kdc_usec_offset += 1000000; 71233294Sstas } 72233294Sstas } else 73233294Sstas context->kdc_usec_offset = tv.tv_usec; 74233294Sstas 75178825Sdfr return 0; 76178825Sdfr} 77178825Sdfr 78178825Sdfr/* 7957416Smarkm * return ``corrected'' time in `timeret'. 8057416Smarkm */ 8157416Smarkm 82233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 8355682Smarkmkrb5_timeofday (krb5_context context, 8457416Smarkm krb5_timestamp *timeret) 8555682Smarkm{ 8655682Smarkm *timeret = time(NULL) + context->kdc_sec_offset; 8755682Smarkm return 0; 8855682Smarkm} 8955682Smarkm 9057416Smarkm/* 9157416Smarkm * like gettimeofday but with time correction to the KDC 9257416Smarkm */ 9357416Smarkm 94233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 9555682Smarkmkrb5_us_timeofday (krb5_context context, 96178825Sdfr krb5_timestamp *sec, 9755682Smarkm int32_t *usec) 9855682Smarkm{ 9955682Smarkm struct timeval tv; 10055682Smarkm 10155682Smarkm gettimeofday (&tv, NULL); 10255682Smarkm 10355682Smarkm *sec = tv.tv_sec + context->kdc_sec_offset; 10455682Smarkm *usec = tv.tv_usec; /* XXX */ 10555682Smarkm return 0; 10655682Smarkm} 10772445Sassar 108233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 109233294Sstaskrb5_format_time(krb5_context context, time_t t, 11072445Sassar char *s, size_t len, krb5_boolean include_time) 11172445Sassar{ 11272445Sassar struct tm *tm; 11372445Sassar if(context->log_utc) 11472445Sassar tm = gmtime (&t); 11572445Sassar else 11672445Sassar tm = localtime(&t); 117178825Sdfr if(tm == NULL || 118178825Sdfr strftime(s, len, include_time ? context->time_fmt : context->date_fmt, tm) == 0) 119178825Sdfr snprintf(s, len, "%ld", (long)t); 12072445Sassar return 0; 12172445Sassar} 12278527Sassar 123233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 12478527Sassarkrb5_string_to_deltat(const char *string, krb5_deltat *deltat) 12578527Sassar{ 12678527Sassar if((*deltat = parse_time(string, "s")) == -1) 127178825Sdfr return KRB5_DELTAT_BADFORMAT; 12878527Sassar return 0; 12978527Sassar} 130