summaryrefslogtreecommitdiffstats
path: root/src/kdc/do_as_req.c
blob: 0291ecce3ec406b5dc309f226703f908f563cdb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * $Source$
 * $Author$
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <krb5/mit-copyright.h>.
 *
 * KDC Routines to deal with AS_REQ's
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_do_as_req_c[] =
"$Id$";
#endif	/* !lint & !SABER */

#include <krb5/copyright.h>

#include <krb5/krb5.h>
#include <krb5/krb5_err.h>
#include <krb5/kdb.h>
#include <stdio.h>
#include <krb5/libos-proto.h>
#include <krb5/asn1.h>
#include <errno.h>
#include <com_err.h>

#include <sys/types.h>
#include <krb5/ext-proto.h>

#include "kdc_util.h"
#include "policy.h"
#include "extern.h"

static krb5_error_code prepare_error_as PROTOTYPE((krb5_as_req *,
						int,
						krb5_data **));

/*
 * Do all the processing required for a AS_REQ
 */

/* XXX needs lots of cleanup and modularizing */

/*ARGSUSED*/
krb5_error_code
process_as_req(request, from, response)
register krb5_as_req *request;
krb5_fulladdr *from;			/* who sent it ? */
krb5_data **response;			/* filled in with a response packet */
{

    krb5_db_entry client, server;
    krb5_kdc_rep reply;
    krb5_enc_kdc_rep_part reply_encpart;
    krb5_ticket ticket_reply;
    krb5_enc_tkt_part enc_tkt_reply;
    krb5_error_code retval;
    int nprincs;
    krb5_boolean more;
    krb5_timestamp kdc_time;
    krb5_keyblock *session_key;
    krb5_keyblock encrypting_key;

    krb5_timestamp until, rtime;

    nprincs = 1;
    if (retval = krb5_db_get_principal(request->client, &client, &nprincs,
				       &more))
	return(retval);
    if (more) {
	krb5_db_free_principal(&client, nprincs);
	return(prepare_error_as(request, KDC_ERR_PRINCIPAL_NOT_UNIQUE, response));
    } else if (nprincs != 1) {
	krb5_db_free_principal(&client, nprincs);
	return(prepare_error_as(request, KDC_ERR_C_PRINCIPAL_UNKNOWN, response));
    }	
	
    nprincs = 1;
    if (retval = krb5_db_get_principal(request->server, &server, &nprincs,
				       &more))
	return(retval);
    if (more) {
	krb5_db_free_principal(&client, 1);
	krb5_db_free_principal(&server, nprincs);
	return(prepare_error_as(request, KDC_ERR_PRINCIPAL_NOT_UNIQUE, response));
    } else if (nprincs != 1) {
	krb5_db_free_principal(&client, 1);
	krb5_db_free_principal(&server, nprincs);
	return(prepare_error_as(request, KDC_ERR_S_PRINCIPAL_UNKNOWN, response));
    }

#define cleanup() {krb5_db_free_principal(&client, 1); krb5_db_free_principal(&server, 1); }

    if (retval = krb5_timeofday(&kdc_time)) {
	cleanup();
	return(retval);
    }

    if (!valid_etype(request->etype)) {
	/* unsupported etype */

	cleanup();
	return(prepare_error_as(request, KDC_ERR_ETYPE_NOSUPP, response));
    }

    if (retval = (*(krb5_csarray[request->etype]->system->random_key))(krb5_csarray[request->etype]->random_sequence, &session_key)) {
	/* random key failed */
	cleanup();
	return(retval);
    }

#undef cleanup
#define cleanup() {krb5_db_free_principal(&client, 1); krb5_db_free_principal(&server, 1); bzero((char *)session_key->contents, session_key->length); free((char *)session_key->contents); session_key->contents = 0; }


    ticket_reply.server = request->server;
    ticket_reply.etype = request->etype;
    ticket_reply.skvno = server.kvno;

    enc_tkt_reply.flags = 0;

        /* It should be noted that local policy may affect the  */
        /* processing of any of these flags.  For example, some */
        /* realms may refuse to issue renewable tickets         */

    if (against_flag_policy_as(request)) {
	cleanup();
	return(prepare_error_as(request, KDC_ERR_BADOPTION, response));
    }

    if (isset(request->kdc_options, KDC_OPT_FORWARDABLE))
	set(enc_tkt_reply.flags, TKT_FLG_FORWARDABLE);

    if (isset(request->kdc_options, KDC_OPT_PROXIABLE))
	set(enc_tkt_reply.flags, TKT_FLG_PROXIABLE);

    if (isset(request->kdc_options, KDC_OPT_ALLOW_POSTDATE))
	set(enc_tkt_reply.flags, TKT_FLG_MAY_POSTDATE);

    if (isset(request->kdc_options, KDC_OPT_DUPLICATE_SKEY))
	set(enc_tkt_reply.flags, TKT_FLG_DUPLICATE_SKEY);


    enc_tkt_reply.session = session_key;
    enc_tkt_reply.client = request->client;
    enc_tkt_reply.transited = empty_string; /* equivalent of "" */
    enc_tkt_reply.times.authtime = kdc_time;

    if (isset(request->kdc_options, KDC_OPT_POSTDATED)) {
	if (against_postdate_policy(request->from)) {
	    cleanup();
	    return(prepare_error_as(request, KDC_ERR_POLICY, response));
	}
	set(enc_tkt_reply.flags, TKT_FLG_INVALID);
	enc_tkt_reply.times.starttime = request->from;
    } else
	enc_tkt_reply.times.starttime = kdc_time;
    

    until = (request->till == 0) ? infinity : request->till;

    enc_tkt_reply.times.endtime =
	min(until,
	    min(enc_tkt_reply.times.starttime + client.max_life,
		min(enc_tkt_reply.times.starttime + server.max_life,
		    enc_tkt_reply.times.starttime + max_life_for_realm)));

    /* XXX why && request->till ? */
    if (isset(request->kdc_options, KDC_OPT_RENEWABLE_OK) && 
	request->till && (enc_tkt_reply.times.endtime < request->till)) {

	/* we set the RENEWABLE option for later processing */

	set(request->kdc_options, KDC_OPT_RENEWABLE);
	request->rtime = request->till;
    }
    rtime = (request->rtime == 0) ? infinity : request->rtime;

    if (isset(request->kdc_options, KDC_OPT_RENEWABLE)) {
	set(enc_tkt_reply.flags, TKT_FLG_RENEWABLE);
	enc_tkt_reply.times.renew_till =
	    min(rtime, min(enc_tkt_reply.times.starttime +
			   client.max_renewable_life,
			   min(enc_tkt_reply.times.starttime +
			       server.max_renewable_life,
			       enc_tkt_reply.times.starttime +
			       max_renewable_life_for_realm)));
    } else
	enc_tkt_reply.times.renew_till = 0; /* XXX */

    enc_tkt_reply.caddrs = request->addresses;
    enc_tkt_reply.authorization_data = 0; /* XXX? */

    /* XXX need separate etypes for ticket encryption and kdc_rep encryption */

    ticket_reply.enc_part2 = &enc_tkt_reply;

    /* convert server.key into a real key (it may be encrypted
       in the database) */
    if (retval = kdc_convert_key(&server.key, &encrypting_key,
				 CONVERT_OUTOF_DB)) {
	cleanup();
	return retval;
    }
    retval = krb5_encrypt_tkt_part(&encrypting_key, &ticket_reply);
    bzero((char *)encrypting_key.contents, encrypting_key.length);
    free((char *)encrypting_key.contents);
    if (retval) {
	cleanup();
	return retval;
    }

    krb5_db_free_principal(&server, 1);

#undef cleanup
#define cleanup() {krb5_db_free_principal(&client, 1);bzero((char *)session_key->contents, session_key->length); free((char *)session_key->contents); session_key->contents = 0; bzero(ticket_reply.enc_part.data, ticket_reply.enc_part.length); free(ticket_reply.enc_part.data);}


    /* Start assembling the response */
    reply.client = request->client;
    reply.etype = request->etype;
    reply.ckvno = client.kvno;
    reply.ticket = &ticket_reply;

    reply_encpart.session = session_key;
    if (retval = fetch_last_req_info(&client, &reply_encpart.last_req)) {
	cleanup();
	return retval;
    }

    reply_encpart.ctime = request->ctime;
    reply_encpart.key_exp = client.expiration;
    reply_encpart.flags = enc_tkt_reply.flags;
    reply_encpart.server = ticket_reply.server;

    /* copy the time fields EXCEPT for authtime; it's location
       is used for ktime */
    reply_encpart.times = enc_tkt_reply.times;
    reply_encpart.times.authtime = kdc_time;

    reply_encpart.caddrs = enc_tkt_reply.caddrs;

    /* now encode/encrypt the response */

    /* convert client.key into a real key (it may be encrypted
       in the database) */
    if (retval = kdc_convert_key(&client.key, &encrypting_key,
				 CONVERT_OUTOF_DB)) {
	cleanup();
	return retval;
    }
    retval = krb5_encode_kdc_rep(KRB5_AS_REP, &reply, &reply_encpart,
				 &encrypting_key, response);
    bzero((char *)encrypting_key.contents, encrypting_key.length);
    free((char *)encrypting_key.contents);
    cleanup();
    return retval;
}

static krb5_error_code
prepare_error_as (request, error, response)
register krb5_as_req *request;
int error;
krb5_data **response;
{
    krb5_error errpkt;
    krb5_error_code retval;
    krb5_data *scratch;

    errpkt.ctime = request->ctime;
    errpkt.cmsec = 0;

    if (retval = krb5_ms_timeofday(&errpkt.stime, &errpkt.smsec))
	return(retval);
    errpkt.error = error;
    errpkt.server = request->server;
    errpkt.client = request->client;
    errpkt.text.length = strlen(error_message(error+KRB5KDC_ERR_NONE))+1;
    if (!(errpkt.text.data = malloc(errpkt.text.length)))
	return ENOMEM;
    (void) strcpy(errpkt.text.data, error_message(error+KRB5KDC_ERR_NONE));

    if (!(scratch = (krb5_data *)malloc(sizeof(*scratch)))) {
	free(errpkt.text.data);
	return ENOMEM;
    }
    retval = krb5_mk_error(&errpkt, scratch);
    free(errpkt.text.data);
    *response = scratch;
    return retval;
}