mk_req.c revision 55682
1/* 2 * Copyright (c) 1997 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34/* implementation of krb_mk_req that uses 524 protocol */ 35 36#include "45_locl.h" 37 38RCSID("$Id: mk_req.c,v 1.2 1999/12/02 17:05:01 joda Exp $"); 39 40static int lifetime = 255; 41 42static void 43build_request(KTEXT req, char *name, char *inst, char *realm, 44 u_int32_t checksum) 45{ 46 struct timeval tv; 47 krb5_storage *sp; 48 krb5_data data; 49 sp = krb5_storage_emem(); 50 krb5_store_stringz(sp, name); 51 krb5_store_stringz(sp, inst); 52 krb5_store_stringz(sp, realm); 53 krb5_store_int32(sp, checksum); 54 gettimeofday(&tv, NULL); 55 krb5_store_int8(sp, tv.tv_usec / 5000); 56 krb5_store_int32(sp, tv.tv_sec); 57 krb5_storage_to_data(sp, &data); 58 krb5_storage_free(sp); 59 memcpy(req->dat, data.data, data.length); 60 req->length = (data.length + 7) & ~7; 61 krb5_data_free(&data); 62} 63 64int 65krb_mk_req(KTEXT authent, char *service, char *instance, char *realm, 66 int32_t checksum) 67{ 68 CREDENTIALS cr; 69 KTEXT_ST req; 70 krb5_storage *sp; 71 int code; 72 char *myrealm; 73 krb5_data a; 74 75 code = krb_get_cred(service, instance, realm, &cr); 76 if(code || time(NULL) > krb_life_to_time(cr.issue_date, cr.lifetime)){ 77 code = get_ad_tkt(service, instance, realm, lifetime); 78 if(code == KSUCCESS) 79 code = krb_get_cred(service, instance, realm, &cr); 80 } 81 82 if(code) 83 return code; 84 85 /* XXX get user realm */ 86 myrealm = realm; 87 88 sp = krb5_storage_emem(); 89 90 krb5_store_int8(sp, KRB_PROT_VERSION); 91 krb5_store_int8(sp, AUTH_MSG_APPL_REQUEST); 92 93 krb5_store_int8(sp, cr.kvno); 94 krb5_store_stringz(sp, realm); 95 krb5_store_int8(sp, cr.ticket_st.length); 96 97 build_request(&req, cr.pname, cr.pinst, myrealm, checksum); 98 encrypt_ktext(&req, &cr.session, DES_ENCRYPT); 99 100 krb5_store_int8(sp, req.length); 101 102 sp->store(sp, cr.ticket_st.dat, cr.ticket_st.length); 103 sp->store(sp, req.dat, req.length); 104 krb5_storage_to_data(sp, &a); 105 krb5_storage_free(sp); 106 memcpy(authent->dat, a.data, a.length); 107 authent->length = a.length; 108 krb5_data_free(&a); 109 110 memset(&cr, 0, sizeof(cr)); 111 memset(&req, 0, sizeof(req)); 112 113 return KSUCCESS; 114} 115 116/* 117 * krb_set_lifetime sets the default lifetime for additional tickets 118 * obtained via krb_mk_req(). 119 * 120 * It returns the previous value of the default lifetime. 121 */ 122 123int 124krb_set_lifetime(int newval) 125{ 126 int olife = lifetime; 127 128 lifetime = newval; 129 return(olife); 130} 131