2 * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan
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. 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.
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
36 RCSID("$Id: mk_req.c,v 1.22 1999/12/02 16:58:43 joda Exp $");
38 static int lifetime = 255; /* But no longer than TGT says. */
42 build_request(KTEXT req, char *name, char *inst, char *realm,
46 unsigned char *p = req->dat;
48 size_t rem = sizeof(req->dat);
50 tmp = krb_put_nir(name, inst, realm, p, rem);
56 tmp = krb_put_int(checksum, p, rem, 4);
62 /* Fill in the times on the request id */
63 krb_kdctimeofday(&tv);
68 *p++ = tv.tv_usec / 5000; /* 5ms */
71 tmp = krb_put_int(tv.tv_sec, p, rem, 4);
77 /* Fill to a multiple of 8 bytes for DES */
78 req->length = ((p - req->dat + 7)/8) * 8;
84 * krb_mk_req takes a text structure in which an authenticator is to
85 * be built, the name of a service, an instance, a realm,
86 * and a checksum. It then retrieves a ticket for
87 * the desired service and creates an authenticator in the text
88 * structure passed as the first argument. krb_mk_req returns
89 * KSUCCESS on success and a Kerberos error code on failure.
91 * The peer procedure on the other end is krb_rd_req. When making
92 * any changes to this routine it is important to make corresponding
93 * changes to krb_rd_req.
95 * The authenticator consists of the following:
99 * unsigned char KRB_PROT_VERSION protocol version no.
100 * unsigned char AUTH_MSG_APPL_REQUEST message type
102 * bit of above) HOST_BYTE_ORDER local byte ordering
103 * unsigned char kvno from ticket server's key version
104 * string realm server's realm
105 * unsigned char tl ticket length
106 * unsigned char idl request id length
107 * text ticket->dat ticket for server
108 * text req_id->dat request id
110 * The ticket information is retrieved from the ticket cache or
111 * fetched from Kerberos. The request id (called the "authenticator"
112 * in the papers on Kerberos) contains the following:
116 * string cr.pname {name, instance, and
117 * string cr.pinst realm of principal
118 * string myrealm making this request}
119 * 4 bytes checksum checksum argument given
120 * unsigned char tv_local.tf_usec time (milliseconds)
121 * 4 bytes tv_local.tv_sec time (seconds)
123 * req_id->length = 3 strings + 3 terminating nulls + 5 bytes for time,
124 * all rounded up to multiple of 8.
128 krb_mk_req(KTEXT authent, char *service, char *instance, char *realm,
132 KTEXT req_id = &req_st;
134 CREDENTIALS cr; /* Credentials used by retr */
135 KTEXT ticket = &(cr.ticket_st); /* Pointer to tkt_st */
136 int retval; /* Returned by krb_get_cred */
138 char myrealm[REALM_SZ];
140 unsigned char *p = authent->dat;
141 int rem = sizeof(authent->dat);
144 tmp = krb_put_int(KRB_PROT_VERSION, p, rem, 1);
150 tmp = krb_put_int(AUTH_MSG_APPL_REQUEST, p, rem, 1);
156 /* Get the ticket and move it into the authenticator */
157 if (krb_ap_req_debug)
158 krb_warning("Realm: %s\n", realm);
160 retval = krb_get_cred(service,instance,realm,&cr);
162 if (retval == RET_NOTKT) {
163 retval = get_ad_tkt(service, instance, realm, lifetime);
164 if (retval == KSUCCESS)
165 retval = krb_get_cred(service, instance, realm, &cr);
168 if (retval != KSUCCESS)
173 * With multi realm ticket files either find a matching TGT or
174 * else use the first TGT for inter-realm authentication.
176 * In myrealm hold the realm of the principal "owning" the
177 * corresponding ticket-granting-ticket.
180 retval = krb_get_cred(KRB_TICKET_GRANTING_TICKET, realm, realm, 0);
181 if (retval == KSUCCESS) {
182 strlcpy(myrealm, realm, REALM_SZ);
184 retval = krb_get_tf_realm(TKT_FILE, myrealm);
186 if (retval != KSUCCESS)
189 if (krb_ap_req_debug)
190 krb_warning("serv=%s.%s@%s princ=%s.%s@%s\n", service, instance, realm,
191 cr.pname, cr.pinst, myrealm);
193 tmp = krb_put_int(cr.kvno, p, rem, 1);
199 tmp = krb_put_string(realm, p, rem);
205 tmp = krb_put_int(ticket->length, p, rem, 1);
211 retval = build_request(req_id, cr.pname, cr.pinst, myrealm, checksum);
212 if (retval != KSUCCESS)
215 encrypt_ktext(req_id, &cr.session, DES_ENCRYPT);
217 tmp = krb_put_int(req_id->length, p, rem, 1);
223 if (rem < ticket->length + req_id->length)
226 memcpy(p, ticket->dat, ticket->length);
228 rem -= ticket->length;
229 memcpy(p, req_id->dat, req_id->length);
231 rem -= req_id->length;
233 authent->length = p - authent->dat;
235 memset(&cr, 0, sizeof(cr));
236 memset(&req_st, 0, sizeof(req_st));
238 if (krb_ap_req_debug)
239 krb_warning("Authent->length = %d\n", authent->length);
245 * krb_set_lifetime sets the default lifetime for additional tickets
246 * obtained via krb_mk_req().
248 * It returns the previous value of the default lifetime.
252 krb_set_lifetime(int newval)
254 int olife = lifetime;