1/*
2 * Copyright (c) 1997 - 2000, 2002 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 17445 2006-05-05 10:37:46Z lha $");
39
40static int lifetime = 255;
41
42static void
43build_request(KTEXT req,
44	      const char *name, const char *inst, const char *realm,
45	      uint32_t checksum)
46{
47    struct timeval tv;
48    krb5_storage *sp;
49    krb5_data data;
50    sp = krb5_storage_emem();
51    krb5_store_stringz(sp, name);
52    krb5_store_stringz(sp, inst);
53    krb5_store_stringz(sp, realm);
54    krb5_store_int32(sp, checksum);
55    gettimeofday(&tv, NULL);
56    krb5_store_int8(sp, tv.tv_usec  / 5000);
57    krb5_store_int32(sp, tv.tv_sec);
58    krb5_storage_to_data(sp, &data);
59    krb5_storage_free(sp);
60    memcpy(req->dat, data.data, data.length);
61    req->length = (data.length + 7) & ~7;
62    krb5_data_free(&data);
63}
64
65#ifdef KRB_MK_REQ_CONST
66int
67krb_mk_req(KTEXT authent,
68	   const char *service, const char *instance, const char *realm,
69	   int32_t checksum)
70#else
71int
72krb_mk_req(KTEXT authent,
73	   char *service, char *instance, char *realm,
74	   int32_t checksum)
75
76#endif
77{
78    CREDENTIALS cr;
79    KTEXT_ST req;
80    krb5_storage *sp;
81    int code;
82    /* XXX get user realm */
83    const char *myrealm = realm;
84    krb5_data a;
85
86    code = krb_get_cred(service, instance, realm, &cr);
87    if(code || time(NULL) > krb_life_to_time(cr.issue_date, cr.lifetime)){
88	code = get_ad_tkt((char *)service,
89			  (char *)instance, (char *)realm, lifetime);
90	if(code == KSUCCESS)
91	    code = krb_get_cred(service, instance, realm, &cr);
92    }
93
94    if(code)
95	return code;
96
97    sp = krb5_storage_emem();
98
99    krb5_store_int8(sp, KRB_PROT_VERSION);
100    krb5_store_int8(sp, AUTH_MSG_APPL_REQUEST);
101
102    krb5_store_int8(sp, cr.kvno);
103    krb5_store_stringz(sp, realm);
104    krb5_store_int8(sp, cr.ticket_st.length);
105
106    build_request(&req, cr.pname, cr.pinst, myrealm, checksum);
107    encrypt_ktext(&req, &cr.session, DES_ENCRYPT);
108
109    krb5_store_int8(sp, req.length);
110
111    krb5_storage_write(sp, cr.ticket_st.dat, cr.ticket_st.length);
112    krb5_storage_write(sp, req.dat, req.length);
113    krb5_storage_to_data(sp, &a);
114    krb5_storage_free(sp);
115    memcpy(authent->dat, a.data, a.length);
116    authent->length = a.length;
117    krb5_data_free(&a);
118
119    memset(&cr, 0, sizeof(cr));
120    memset(&req, 0, sizeof(req));
121
122    return KSUCCESS;
123}
124
125/*
126 * krb_set_lifetime sets the default lifetime for additional tickets
127 * obtained via krb_mk_req().
128 *
129 * It returns the previous value of the default lifetime.
130 */
131
132int
133krb_set_lifetime(int newval)
134{
135    int olife = lifetime;
136
137    lifetime = newval;
138    return(olife);
139}
140