Only ask for password if we can't get creds
[cyrus-sasl.git] / mac / krb4_sources / mk_req.c
1 /*
2  * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska H\9agskolan
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. 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.
21  * 
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.
25  * 
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
36  * SUCH DAMAGE.
37  */
38
39 #include "sasl_mac_krb_locl.h"
40
41 RCSID("$Id: mk_req.c,v 1.2 2001/12/04 02:06:08 rjs3 Exp $");
42
43 static int lifetime = 255;      /* But no longer than TGT says. */
44
45
46 static int
47 build_request(KTEXT req, char *name, char *inst, char *realm, 
48               u_int32_t checksum)
49 {
50     struct timeval tv;
51     unsigned char *p = req->dat;
52     int tmp;
53     size_t rem = sizeof(req->dat);
54
55     tmp = krb_put_nir(name, inst, realm, p, rem);
56     if (tmp < 0)
57         return KFAILURE;
58     p += tmp;
59     rem -= tmp;
60
61     tmp = krb_put_int(checksum, p, rem, 4);
62     if (tmp < 0)
63         return KFAILURE;
64     p += tmp;
65     rem -= tmp;
66
67     /* Fill in the times on the request id */
68     krb_kdctimeofday(&tv);
69
70     if (rem < 1)
71         return KFAILURE;
72
73     *p++ = tv.tv_usec / 5000; /* 5ms */
74     --rem;
75     
76     tmp = krb_put_int(tv.tv_sec, p, rem, 4);
77     if (tmp < 0)
78         return KFAILURE;
79     p += tmp;
80     rem -= tmp;
81
82     /* Fill to a multiple of 8 bytes for DES */
83     req->length = ((p - req->dat + 7)/8) * 8;
84     return 0;
85 }
86
87
88 /*
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.
95  *
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.
99  *
100  * The authenticator consists of the following:
101  *
102  * authent->dat
103  *
104  * unsigned char        KRB_PROT_VERSION        protocol version no.
105  * unsigned char        AUTH_MSG_APPL_REQUEST   message type
106  * (least significant
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
114  *
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:
118  *
119  * req_id->dat
120  *
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)
127  *
128  * req_id->length = 3 strings + 3 terminating nulls + 5 bytes for time,
129  *                  all rounded up to multiple of 8.
130  */
131
132 int
133 krb_mk_req(KTEXT authent, char *service, char *instance, char *realm, 
134            int32_t checksum)
135 {
136     KTEXT_ST req_st;
137     KTEXT req_id = &req_st;
138
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 */
142
143     char myrealm[REALM_SZ];
144
145     unsigned char *p = authent->dat;
146     int rem = sizeof(authent->dat);
147     int tmp;
148
149     tmp = krb_put_int(KRB_PROT_VERSION, p, rem, 1);
150     if (tmp < 0)
151         return KFAILURE;
152     p += tmp;
153     rem -= tmp;
154
155     tmp = krb_put_int(AUTH_MSG_APPL_REQUEST, p, rem, 1);
156     if (tmp < 0)
157         return KFAILURE;
158     p += tmp;
159     rem -= tmp;
160
161     /* Get the ticket and move it into the authenticator */
162     if (krb_ap_req_debug)
163         krb_warning("Realm: %s\n", realm);
164
165     retval = krb_get_cred(service,instance,realm,&cr);
166
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);
171     }
172
173     if (retval != KSUCCESS)
174         return retval;
175
176
177     /*
178      * With multi realm ticket files either find a matching TGT or
179      * else use the first TGT for inter-realm authentication.
180      *
181      * In myrealm hold the realm of the principal "owning" the
182      * corresponding ticket-granting-ticket.
183      */
184
185     retval = krb_get_cred(KRB_TICKET_GRANTING_TICKET, realm, realm, 0);
186     if (retval == KSUCCESS) {
187       strcpy_truncate(myrealm, realm, REALM_SZ);
188     } else
189       retval = krb_get_tf_realm(TKT_FILE, myrealm);
190     
191     if (retval != KSUCCESS)
192         return retval;
193     
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);
197
198     tmp = krb_put_int(cr.kvno, p, rem, 1);
199     if (tmp < 0)
200         return KFAILURE;
201     p += tmp;
202     rem -= tmp;
203
204     tmp = krb_put_string(realm, p, rem);
205     if (tmp < 0)
206         return KFAILURE;
207     p += tmp;
208     rem -= tmp;
209
210     tmp = krb_put_int(ticket->length, p, rem, 1);
211     if (tmp < 0)
212         return KFAILURE;
213     p += tmp;
214     rem -= tmp;
215
216     retval = build_request(req_id, cr.pname, cr.pinst, myrealm, checksum);
217     if (retval != KSUCCESS)
218         return retval;
219     
220     encrypt_ktext(req_id, &cr.session, DES_ENCRYPT);
221
222     tmp = krb_put_int(req_id->length, p, rem, 1);
223     if (tmp < 0)
224         return KFAILURE;
225     p += tmp;
226     rem -= tmp;
227
228     if (rem < ticket->length + req_id->length)
229         return KFAILURE;
230
231     memcpy(p, ticket->dat, ticket->length);
232     p += ticket->length;
233     rem -= ticket->length;
234     memcpy(p, req_id->dat, req_id->length);
235     p += req_id->length;
236     rem -= req_id->length;
237
238     authent->length = p - authent->dat;
239     
240     memset(&cr, 0, sizeof(cr));
241     memset(&req_st, 0, sizeof(req_st));
242
243     if (krb_ap_req_debug)
244         krb_warning("Authent->length = %d\n", authent->length);
245
246     return KSUCCESS;
247 }
248
249 /* 
250  * krb_set_lifetime sets the default lifetime for additional tickets
251  * obtained via krb_mk_req().
252  * 
253  * It returns the previous value of the default lifetime.
254  */
255
256 int
257 krb_set_lifetime(int newval)
258 {
259     int olife = lifetime;
260
261     lifetime = newval;
262     return(olife);
263 }