2 * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska H
\9agskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the Kungliga Tekniska
20 * H
\9agskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "sasl_mac_krb_locl.h"
41 RCSID("$Id: mk_req.c,v 1.2 2001/12/04 02:06:08 rjs3 Exp $");
43 static int lifetime = 255; /* But no longer than TGT says. */
47 build_request(KTEXT req, char *name, char *inst, char *realm,
51 unsigned char *p = req->dat;
53 size_t rem = sizeof(req->dat);
55 tmp = krb_put_nir(name, inst, realm, p, rem);
61 tmp = krb_put_int(checksum, p, rem, 4);
67 /* Fill in the times on the request id */
68 krb_kdctimeofday(&tv);
73 *p++ = tv.tv_usec / 5000; /* 5ms */
76 tmp = krb_put_int(tv.tv_sec, p, rem, 4);
82 /* Fill to a multiple of 8 bytes for DES */
83 req->length = ((p - req->dat + 7)/8) * 8;
89 * krb_mk_req takes a text structure in which an authenticator is to
90 * be built, the name of a service, an instance, a realm,
91 * and a checksum. It then retrieves a ticket for
92 * the desired service and creates an authenticator in the text
93 * structure passed as the first argument. krb_mk_req returns
94 * KSUCCESS on success and a Kerberos error code on failure.
96 * The peer procedure on the other end is krb_rd_req. When making
97 * any changes to this routine it is important to make corresponding
98 * changes to krb_rd_req.
100 * The authenticator consists of the following:
104 * unsigned char KRB_PROT_VERSION protocol version no.
105 * unsigned char AUTH_MSG_APPL_REQUEST message type
107 * bit of above) HOST_BYTE_ORDER local byte ordering
108 * unsigned char kvno from ticket server's key version
109 * string realm server's realm
110 * unsigned char tl ticket length
111 * unsigned char idl request id length
112 * text ticket->dat ticket for server
113 * text req_id->dat request id
115 * The ticket information is retrieved from the ticket cache or
116 * fetched from Kerberos. The request id (called the "authenticator"
117 * in the papers on Kerberos) contains the following:
121 * string cr.pname {name, instance, and
122 * string cr.pinst realm of principal
123 * string myrealm making this request}
124 * 4 bytes checksum checksum argument given
125 * unsigned char tv_local.tf_usec time (milliseconds)
126 * 4 bytes tv_local.tv_sec time (seconds)
128 * req_id->length = 3 strings + 3 terminating nulls + 5 bytes for time,
129 * all rounded up to multiple of 8.
133 krb_mk_req(KTEXT authent, char *service, char *instance, char *realm,
137 KTEXT req_id = &req_st;
139 CREDENTIALS cr; /* Credentials used by retr */
140 KTEXT ticket = &(cr.ticket_st); /* Pointer to tkt_st */
141 int retval; /* Returned by krb_get_cred */
143 char myrealm[REALM_SZ];
145 unsigned char *p = authent->dat;
146 int rem = sizeof(authent->dat);
149 tmp = krb_put_int(KRB_PROT_VERSION, p, rem, 1);
155 tmp = krb_put_int(AUTH_MSG_APPL_REQUEST, p, rem, 1);
161 /* Get the ticket and move it into the authenticator */
162 if (krb_ap_req_debug)
163 krb_warning("Realm: %s\n", realm);
165 retval = krb_get_cred(service,instance,realm,&cr);
167 if (retval == RET_NOTKT) {
168 retval = get_ad_tkt(service, instance, realm, lifetime);
169 if (retval == KSUCCESS)
170 retval = krb_get_cred(service, instance, realm, &cr);
173 if (retval != KSUCCESS)
178 * With multi realm ticket files either find a matching TGT or
179 * else use the first TGT for inter-realm authentication.
181 * In myrealm hold the realm of the principal "owning" the
182 * corresponding ticket-granting-ticket.
185 retval = krb_get_cred(KRB_TICKET_GRANTING_TICKET, realm, realm, 0);
186 if (retval == KSUCCESS) {
187 strcpy_truncate(myrealm, realm, REALM_SZ);
189 retval = krb_get_tf_realm(TKT_FILE, myrealm);
191 if (retval != KSUCCESS)
194 if (krb_ap_req_debug)
195 krb_warning("serv=%s.%s@%s princ=%s.%s@%s\n", service, instance, realm,
196 cr.pname, cr.pinst, myrealm);
198 tmp = krb_put_int(cr.kvno, p, rem, 1);
204 tmp = krb_put_string(realm, p, rem);
210 tmp = krb_put_int(ticket->length, p, rem, 1);
216 retval = build_request(req_id, cr.pname, cr.pinst, myrealm, checksum);
217 if (retval != KSUCCESS)
220 encrypt_ktext(req_id, &cr.session, DES_ENCRYPT);
222 tmp = krb_put_int(req_id->length, p, rem, 1);
228 if (rem < ticket->length + req_id->length)
231 memcpy(p, ticket->dat, ticket->length);
233 rem -= ticket->length;
234 memcpy(p, req_id->dat, req_id->length);
236 rem -= req_id->length;
238 authent->length = p - authent->dat;
240 memset(&cr, 0, sizeof(cr));
241 memset(&req_st, 0, sizeof(req_st));
243 if (krb_ap_req_debug)
244 krb_warning("Authent->length = %d\n", authent->length);
250 * krb_set_lifetime sets the default lifetime for additional tickets
251 * obtained via krb_mk_req().
253 * It returns the previous value of the default lifetime.
257 krb_set_lifetime(int newval)
259 int olife = lifetime;