Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / mk_req.c
1 /*
2  * Copyright (c) 1995, 1996, 1997, 1998 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 #include "krb_locl.h"
35
36 RCSID("$Id: mk_req.c,v 1.22 1999/12/02 16:58:43 joda Exp $");
37
38 static int lifetime = 255;      /* But no longer than TGT says. */
39
40
41 static int
42 build_request(KTEXT req, char *name, char *inst, char *realm, 
43               u_int32_t checksum)
44 {
45     struct timeval tv;
46     unsigned char *p = req->dat;
47     int tmp;
48     size_t rem = sizeof(req->dat);
49
50     tmp = krb_put_nir(name, inst, realm, p, rem);
51     if (tmp < 0)
52         return KFAILURE;
53     p += tmp;
54     rem -= tmp;
55
56     tmp = krb_put_int(checksum, p, rem, 4);
57     if (tmp < 0)
58         return KFAILURE;
59     p += tmp;
60     rem -= tmp;
61
62     /* Fill in the times on the request id */
63     krb_kdctimeofday(&tv);
64
65     if (rem < 1)
66         return KFAILURE;
67
68     *p++ = tv.tv_usec / 5000; /* 5ms */
69     --rem;
70     
71     tmp = krb_put_int(tv.tv_sec, p, rem, 4);
72     if (tmp < 0)
73         return KFAILURE;
74     p += tmp;
75     rem -= tmp;
76
77     /* Fill to a multiple of 8 bytes for DES */
78     req->length = ((p - req->dat + 7)/8) * 8;
79     return 0;
80 }
81
82
83 /*
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.
90  *
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.
94  *
95  * The authenticator consists of the following:
96  *
97  * authent->dat
98  *
99  * unsigned char        KRB_PROT_VERSION        protocol version no.
100  * unsigned char        AUTH_MSG_APPL_REQUEST   message type
101  * (least significant
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
109  *
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:
113  *
114  * req_id->dat
115  *
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)
122  *
123  * req_id->length = 3 strings + 3 terminating nulls + 5 bytes for time,
124  *                  all rounded up to multiple of 8.
125  */
126
127 int
128 krb_mk_req(KTEXT authent, char *service, char *instance, char *realm, 
129            int32_t checksum)
130 {
131     KTEXT_ST req_st;
132     KTEXT req_id = &req_st;
133
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 */
137
138     char myrealm[REALM_SZ];
139
140     unsigned char *p = authent->dat;
141     int rem = sizeof(authent->dat);
142     int tmp;
143
144     tmp = krb_put_int(KRB_PROT_VERSION, p, rem, 1);
145     if (tmp < 0)
146         return KFAILURE;
147     p += tmp;
148     rem -= tmp;
149
150     tmp = krb_put_int(AUTH_MSG_APPL_REQUEST, p, rem, 1);
151     if (tmp < 0)
152         return KFAILURE;
153     p += tmp;
154     rem -= tmp;
155
156     /* Get the ticket and move it into the authenticator */
157     if (krb_ap_req_debug)
158         krb_warning("Realm: %s\n", realm);
159
160     retval = krb_get_cred(service,instance,realm,&cr);
161
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);
166     }
167
168     if (retval != KSUCCESS)
169         return retval;
170
171
172     /*
173      * With multi realm ticket files either find a matching TGT or
174      * else use the first TGT for inter-realm authentication.
175      *
176      * In myrealm hold the realm of the principal "owning" the
177      * corresponding ticket-granting-ticket.
178      */
179
180     retval = krb_get_cred(KRB_TICKET_GRANTING_TICKET, realm, realm, 0);
181     if (retval == KSUCCESS) {
182       strlcpy(myrealm, realm, REALM_SZ);
183     } else
184       retval = krb_get_tf_realm(TKT_FILE, myrealm);
185     
186     if (retval != KSUCCESS)
187         return retval;
188     
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);
192
193     tmp = krb_put_int(cr.kvno, p, rem, 1);
194     if (tmp < 0)
195         return KFAILURE;
196     p += tmp;
197     rem -= tmp;
198
199     tmp = krb_put_string(realm, p, rem);
200     if (tmp < 0)
201         return KFAILURE;
202     p += tmp;
203     rem -= tmp;
204
205     tmp = krb_put_int(ticket->length, p, rem, 1);
206     if (tmp < 0)
207         return KFAILURE;
208     p += tmp;
209     rem -= tmp;
210
211     retval = build_request(req_id, cr.pname, cr.pinst, myrealm, checksum);
212     if (retval != KSUCCESS)
213         return retval;
214     
215     encrypt_ktext(req_id, &cr.session, DES_ENCRYPT);
216
217     tmp = krb_put_int(req_id->length, p, rem, 1);
218     if (tmp < 0)
219         return KFAILURE;
220     p += tmp;
221     rem -= tmp;
222
223     if (rem < ticket->length + req_id->length)
224         return KFAILURE;
225
226     memcpy(p, ticket->dat, ticket->length);
227     p += ticket->length;
228     rem -= ticket->length;
229     memcpy(p, req_id->dat, req_id->length);
230     p += req_id->length;
231     rem -= req_id->length;
232
233     authent->length = p - authent->dat;
234     
235     memset(&cr, 0, sizeof(cr));
236     memset(&req_st, 0, sizeof(req_st));
237
238     if (krb_ap_req_debug)
239         krb_warning("Authent->length = %d\n", authent->length);
240
241     return KSUCCESS;
242 }
243
244 /* 
245  * krb_set_lifetime sets the default lifetime for additional tickets
246  * obtained via krb_mk_req().
247  * 
248  * It returns the previous value of the default lifetime.
249  */
250
251 int
252 krb_set_lifetime(int newval)
253 {
254     int olife = lifetime;
255
256     lifetime = newval;
257     return(olife);
258 }