xref: /onnv-gate/usr/src/cmd/krb5/krb5kdc/do_as_req.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * kdc/do_as_req.c
10*0Sstevel@tonic-gate  *
11*0Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
12*0Sstevel@tonic-gate  * All Rights Reserved.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * Export of this software from the United States of America may
15*0Sstevel@tonic-gate  *   require a specific license from the United States Government.
16*0Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
17*0Sstevel@tonic-gate  *   export to obtain such a license before exporting.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20*0Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
21*0Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
22*0Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
23*0Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
24*0Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
25*0Sstevel@tonic-gate  * to distribution of the software without specific, written prior
26*0Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
27*0Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
28*0Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
29*0Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
30*0Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
31*0Sstevel@tonic-gate  * or implied warranty.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * KDC Routines to deal with AS_REQ's
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "k5-int.h"
38*0Sstevel@tonic-gate #include "com_err.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <syslog.h>
41*0Sstevel@tonic-gate #ifdef HAVE_NETINET_IN_H
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <netinet/in.h>
44*0Sstevel@tonic-gate #ifndef hpux
45*0Sstevel@tonic-gate #include <arpa/inet.h>
46*0Sstevel@tonic-gate #endif	/* hpux */
47*0Sstevel@tonic-gate #endif /* HAVE_NETINET_IN_H */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include "kdc_util.h"
50*0Sstevel@tonic-gate #include "policy.h"
51*0Sstevel@tonic-gate #include "adm.h"
52*0Sstevel@tonic-gate #include "adm_proto.h"
53*0Sstevel@tonic-gate #include "extern.h"
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static krb5_error_code prepare_error_as PROTOTYPE((krb5_kdc_req *,
56*0Sstevel@tonic-gate 						   int,
57*0Sstevel@tonic-gate 						   krb5_data *,
58*0Sstevel@tonic-gate 						   krb5_data **));
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*ARGSUSED*/
61*0Sstevel@tonic-gate krb5_error_code
62*0Sstevel@tonic-gate process_as_req(request, from, portnum, response)
63*0Sstevel@tonic-gate register krb5_kdc_req *request;
64*0Sstevel@tonic-gate const krb5_fulladdr *from;		/* who sent it ? */
65*0Sstevel@tonic-gate int	portnum;
66*0Sstevel@tonic-gate krb5_data **response;			/* filled in with a response packet */
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate     krb5_db_entry client, server;
70*0Sstevel@tonic-gate     krb5_kdc_rep reply;
71*0Sstevel@tonic-gate     krb5_enc_kdc_rep_part reply_encpart;
72*0Sstevel@tonic-gate     krb5_ticket ticket_reply;
73*0Sstevel@tonic-gate     krb5_enc_tkt_part enc_tkt_reply;
74*0Sstevel@tonic-gate     krb5_error_code errcode;
75*0Sstevel@tonic-gate     int c_nprincs = 0, s_nprincs = 0;
76*0Sstevel@tonic-gate     krb5_boolean more;
77*0Sstevel@tonic-gate     krb5_timestamp kdc_time, authtime;
78*0Sstevel@tonic-gate     krb5_keyblock session_key;
79*0Sstevel@tonic-gate     krb5_keyblock encrypting_key;
80*0Sstevel@tonic-gate     const char *status;
81*0Sstevel@tonic-gate     krb5_key_data  *server_key, *client_key;
82*0Sstevel@tonic-gate     krb5_enctype useenctype;
83*0Sstevel@tonic-gate #ifdef	KRBCONF_KDC_MODIFIES_KDB
84*0Sstevel@tonic-gate     krb5_boolean update_client = 0;
85*0Sstevel@tonic-gate #endif	/* KRBCONF_KDC_MODIFIES_KDB */
86*0Sstevel@tonic-gate     krb5_data e_data;
87*0Sstevel@tonic-gate     register int i;
88*0Sstevel@tonic-gate     krb5_timestamp until, rtime;
89*0Sstevel@tonic-gate     long long tmp_client_times, tmp_server_times, tmp_realm_times;
90*0Sstevel@tonic-gate     char *cname = 0, *sname = 0, *fromstring = 0;
91*0Sstevel@tonic-gate     struct in_addr from_in4;	/* IPv4 address of sender */
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate     ticket_reply.enc_part.ciphertext.data = 0;
94*0Sstevel@tonic-gate     e_data.data = 0;
95*0Sstevel@tonic-gate     reply.padata = 0; /* avoid bogus free in error_out */
96*0Sstevel@tonic-gate     (void) memset(&encrypting_key, 0, sizeof(krb5_keyblock));
97*0Sstevel@tonic-gate     (void) memset(&session_key, 0, sizeof(krb5_keyblock));
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate #ifdef HAVE_NETINET_IN_H
100*0Sstevel@tonic-gate     if (from->address->addrtype == ADDRTYPE_INET) {
101*0Sstevel@tonic-gate 	(void) memcpy(&from_in4, from->address->contents, /* SUNW */
102*0Sstevel@tonic-gate 		    sizeof (struct in_addr));
103*0Sstevel@tonic-gate 	fromstring = inet_ntoa(from_in4);
104*0Sstevel@tonic-gate     }
105*0Sstevel@tonic-gate #endif
106*0Sstevel@tonic-gate     if (!fromstring)
107*0Sstevel@tonic-gate 	fromstring = "<unknown>";
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate     if (!request->client) {
110*0Sstevel@tonic-gate 	status = "NULL_CLIENT";
111*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
112*0Sstevel@tonic-gate 	goto errout;
113*0Sstevel@tonic-gate     }
114*0Sstevel@tonic-gate     if ((errcode = krb5_unparse_name(kdc_context, request->client, &cname))) {
115*0Sstevel@tonic-gate 	status = "UNPARSING_CLIENT";
116*0Sstevel@tonic-gate 	goto errout;
117*0Sstevel@tonic-gate     }
118*0Sstevel@tonic-gate     limit_string(cname);
119*0Sstevel@tonic-gate     if (!request->server) {
120*0Sstevel@tonic-gate 	status = "NULL_SERVER";
121*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
122*0Sstevel@tonic-gate 	goto errout;
123*0Sstevel@tonic-gate     }
124*0Sstevel@tonic-gate     if ((errcode = krb5_unparse_name(kdc_context, request->server, &sname))) {
125*0Sstevel@tonic-gate 	status = "UNPARSING_SERVER";
126*0Sstevel@tonic-gate 	goto errout;
127*0Sstevel@tonic-gate     }
128*0Sstevel@tonic-gate     limit_string(sname);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate     c_nprincs = 1;
131*0Sstevel@tonic-gate     if ((errcode = krb5_db_get_principal(kdc_context, request->client,
132*0Sstevel@tonic-gate 					 &client, &c_nprincs, &more))) {
133*0Sstevel@tonic-gate 	status = "LOOKING_UP_CLIENT";
134*0Sstevel@tonic-gate 	c_nprincs = 0;
135*0Sstevel@tonic-gate 	goto errout;
136*0Sstevel@tonic-gate     }
137*0Sstevel@tonic-gate     if (more) {
138*0Sstevel@tonic-gate 	status = "NON-UNIQUE_CLIENT";
139*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE;
140*0Sstevel@tonic-gate 	goto errout;
141*0Sstevel@tonic-gate     } else if (c_nprincs != 1) {
142*0Sstevel@tonic-gate 	status = "CLIENT_NOT_FOUND";
143*0Sstevel@tonic-gate #ifdef KRBCONF_VAGUE_ERRORS
144*0Sstevel@tonic-gate 	errcode = KRB5KRB_ERR_GENERIC;
145*0Sstevel@tonic-gate #else
146*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
147*0Sstevel@tonic-gate #endif
148*0Sstevel@tonic-gate 	goto errout;
149*0Sstevel@tonic-gate     }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate     s_nprincs = 1;
152*0Sstevel@tonic-gate     if ((errcode = krb5_db_get_principal(kdc_context, request->server, &server,
153*0Sstevel@tonic-gate 					 &s_nprincs, &more))) {
154*0Sstevel@tonic-gate 	status = "LOOKING_UP_SERVER";
155*0Sstevel@tonic-gate 	goto errout;
156*0Sstevel@tonic-gate     }
157*0Sstevel@tonic-gate     if (more) {
158*0Sstevel@tonic-gate 	status = "NON-UNIQUE_SERVER";
159*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE;
160*0Sstevel@tonic-gate 	goto errout;
161*0Sstevel@tonic-gate     } else if (s_nprincs != 1) {
162*0Sstevel@tonic-gate 	status = "SERVER_NOT_FOUND";
163*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
164*0Sstevel@tonic-gate 	goto errout;
165*0Sstevel@tonic-gate     }
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate     if ((errcode = krb5_timeofday(kdc_context, &kdc_time))) {
168*0Sstevel@tonic-gate 	status = "TIMEOFDAY";
169*0Sstevel@tonic-gate 	goto errout;
170*0Sstevel@tonic-gate     }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate     if ((errcode = validate_as_request(request, client, server,
173*0Sstevel@tonic-gate 				      kdc_time, &status))) {
174*0Sstevel@tonic-gate 	if (!status)
175*0Sstevel@tonic-gate 	    status = "UNKNOWN_REASON";
176*0Sstevel@tonic-gate 	errcode += ERROR_TABLE_BASE_krb5;
177*0Sstevel@tonic-gate 	goto errout;
178*0Sstevel@tonic-gate     }
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate     /*
181*0Sstevel@tonic-gate      * Select the keytype for the ticket session key.
182*0Sstevel@tonic-gate      */
183*0Sstevel@tonic-gate     if ((useenctype = select_session_keytype(kdc_context, &server,
184*0Sstevel@tonic-gate 					     request->nktypes,
185*0Sstevel@tonic-gate 					     request->ktype)) == 0) {
186*0Sstevel@tonic-gate 	/* unsupported ktype */
187*0Sstevel@tonic-gate 	status = "BAD_ENCRYPTION_TYPE";
188*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_ETYPE_NOSUPP;
189*0Sstevel@tonic-gate 	goto errout;
190*0Sstevel@tonic-gate     }
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate     if ((errcode = krb5_c_make_random_key(kdc_context, useenctype,
193*0Sstevel@tonic-gate 					    &session_key))) {
194*0Sstevel@tonic-gate 	/* random key failed */
195*0Sstevel@tonic-gate 	status = "RANDOM_KEY_FAILED";
196*0Sstevel@tonic-gate 	goto errout;
197*0Sstevel@tonic-gate     }
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate     ticket_reply.server = request->server;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate     enc_tkt_reply.flags = 0;
202*0Sstevel@tonic-gate     setflag(enc_tkt_reply.flags, TKT_FLG_INITIAL);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate     	/* It should be noted that local policy may affect the  */
205*0Sstevel@tonic-gate         /* processing of any of these flags.  For example, some */
206*0Sstevel@tonic-gate         /* realms may refuse to issue renewable tickets         */
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_FORWARDABLE))
209*0Sstevel@tonic-gate 	setflag(enc_tkt_reply.flags, TKT_FLG_FORWARDABLE);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_PROXIABLE))
212*0Sstevel@tonic-gate 	    setflag(enc_tkt_reply.flags, TKT_FLG_PROXIABLE);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_ALLOW_POSTDATE))
215*0Sstevel@tonic-gate 	    setflag(enc_tkt_reply.flags, TKT_FLG_MAY_POSTDATE);
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate     enc_tkt_reply.session = &session_key;
218*0Sstevel@tonic-gate     enc_tkt_reply.client = request->client;
219*0Sstevel@tonic-gate     enc_tkt_reply.transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
220*0Sstevel@tonic-gate     enc_tkt_reply.transited.tr_contents = empty_string; /* equivalent of "" */
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate     enc_tkt_reply.times.authtime = kdc_time;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_POSTDATED)) {
225*0Sstevel@tonic-gate 	setflag(enc_tkt_reply.flags, TKT_FLG_POSTDATED);
226*0Sstevel@tonic-gate 	setflag(enc_tkt_reply.flags, TKT_FLG_INVALID);
227*0Sstevel@tonic-gate 	enc_tkt_reply.times.starttime = request->from;
228*0Sstevel@tonic-gate     } else
229*0Sstevel@tonic-gate 	enc_tkt_reply.times.starttime = kdc_time;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate     until = (request->till == 0) ? kdc_infinity : request->till;
232*0Sstevel@tonic-gate 	/* These numbers could easily be large
233*0Sstevel@tonic-gate 	 * use long long variables to ensure that they don't
234*0Sstevel@tonic-gate 	 * result in negative values when added.
235*0Sstevel@tonic-gate 	*/
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate     tmp_client_times = (long long) enc_tkt_reply.times.starttime + client.max_life;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate     tmp_server_times = (long long) enc_tkt_reply.times.starttime + server.max_life;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate     tmp_realm_times = (long long) enc_tkt_reply.times.starttime + max_life_for_realm;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate    enc_tkt_reply.times.endtime =
244*0Sstevel@tonic-gate   	  min(until,
245*0Sstevel@tonic-gate 	    min(tmp_client_times,
246*0Sstevel@tonic-gate 		min(tmp_server_times,
247*0Sstevel@tonic-gate 			min(tmp_realm_times,KRB5_KDB_EXPIRATION))));
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_RENEWABLE_OK) &&
250*0Sstevel@tonic-gate 	!isflagset(client.attributes, KRB5_KDB_DISALLOW_RENEWABLE) &&
251*0Sstevel@tonic-gate 	(enc_tkt_reply.times.endtime < request->till)) {
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	/* we set the RENEWABLE option for later processing */
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	setflag(request->kdc_options, KDC_OPT_RENEWABLE);
256*0Sstevel@tonic-gate 	request->rtime = request->till;
257*0Sstevel@tonic-gate     }
258*0Sstevel@tonic-gate     rtime = (request->rtime == 0) ? kdc_infinity : request->rtime;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_RENEWABLE)) {
261*0Sstevel@tonic-gate 	/*
262*0Sstevel@tonic-gate 	 * XXX Should we squelch the output renew_till to be no
263*0Sstevel@tonic-gate 	 * earlier than the endtime of the ticket?
264*0Sstevel@tonic-gate 	 */
265*0Sstevel@tonic-gate 	setflag(enc_tkt_reply.flags, TKT_FLG_RENEWABLE);
266*0Sstevel@tonic-gate 	tmp_client_times = (double) enc_tkt_reply.times.starttime + client.max_renewable_life;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate     	tmp_server_times = (double) enc_tkt_reply.times.starttime + server.max_renewable_life;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate     	tmp_realm_times = (double) enc_tkt_reply.times.starttime + max_renewable_life_for_realm;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	enc_tkt_reply.times.renew_till =
273*0Sstevel@tonic-gate 		min(rtime, min(tmp_client_times,
274*0Sstevel@tonic-gate 				min(tmp_server_times,
275*0Sstevel@tonic-gate 					min(tmp_realm_times,KRB5_KDB_EXPIRATION))));
276*0Sstevel@tonic-gate     } else
277*0Sstevel@tonic-gate 	enc_tkt_reply.times.renew_till = 0; /* XXX */
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate     /* starttime is optional, and treated as authtime if not present.
280*0Sstevel@tonic-gate        so we can nuke it if it matches */
281*0Sstevel@tonic-gate     if (enc_tkt_reply.times.starttime == enc_tkt_reply.times.authtime)
282*0Sstevel@tonic-gate 	enc_tkt_reply.times.starttime = 0;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate     enc_tkt_reply.caddrs = request->addresses;
285*0Sstevel@tonic-gate     enc_tkt_reply.authorization_data = 0;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate     /*
288*0Sstevel@tonic-gate      * Check the preauthentication if it is there.
289*0Sstevel@tonic-gate      */
290*0Sstevel@tonic-gate     if (request->padata) {
291*0Sstevel@tonic-gate 	errcode = check_padata(kdc_context, &client, request, &enc_tkt_reply);
292*0Sstevel@tonic-gate 	if (errcode) {
293*0Sstevel@tonic-gate #ifdef KRBCONF_KDC_MODIFIES_KDB
294*0Sstevel@tonic-gate 	    /*
295*0Sstevel@tonic-gate 	     * Note: this doesn't work if you're using slave servers!!!
296*0Sstevel@tonic-gate 	     * It also causes the database to be modified (and thus
297*0Sstevel@tonic-gate 	     * need to be locked) frequently.
298*0Sstevel@tonic-gate 	     */
299*0Sstevel@tonic-gate 	    if (client.fail_auth_count < KRB5_MAX_FAIL_COUNT) {
300*0Sstevel@tonic-gate 		client.fail_auth_count = client.fail_auth_count + 1;
301*0Sstevel@tonic-gate 		if (client.fail_auth_count == KRB5_MAX_FAIL_COUNT) {
302*0Sstevel@tonic-gate 		    client.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
303*0Sstevel@tonic-gate 		}
304*0Sstevel@tonic-gate 	    }
305*0Sstevel@tonic-gate 	    client.last_failed = kdc_time;
306*0Sstevel@tonic-gate 	    update_client = 1;
307*0Sstevel@tonic-gate #endif
308*0Sstevel@tonic-gate 	    status = "PREAUTH_FAILED";
309*0Sstevel@tonic-gate #ifdef KRBCONF_VAGUE_ERRORS
310*0Sstevel@tonic-gate 	    errcode = KRB5KRB_ERR_GENERIC;
311*0Sstevel@tonic-gate #endif
312*0Sstevel@tonic-gate 	    goto errout;
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate     }
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate     /*
317*0Sstevel@tonic-gate      * Final check before handing out ticket: If the client requires
318*0Sstevel@tonic-gate      * preauthentication, verify that the proper kind of
319*0Sstevel@tonic-gate      * preauthentication was carried out.
320*0Sstevel@tonic-gate      */
321*0Sstevel@tonic-gate     status = missing_required_preauth(&client, &server, &enc_tkt_reply);
322*0Sstevel@tonic-gate     if (status) {
323*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_PREAUTH_REQUIRED;
324*0Sstevel@tonic-gate 	get_preauth_hint_list(request, &client, &server, &e_data);
325*0Sstevel@tonic-gate 	goto errout;
326*0Sstevel@tonic-gate     }
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate     ticket_reply.enc_part2 = &enc_tkt_reply;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate     /*
331*0Sstevel@tonic-gate      * Find the server key
332*0Sstevel@tonic-gate      */
333*0Sstevel@tonic-gate     if ((errcode = krb5_dbe_find_enctype(kdc_context, &server,
334*0Sstevel@tonic-gate 					 -1, /* ignore keytype */
335*0Sstevel@tonic-gate 					 -1,		/* Ignore salttype */
336*0Sstevel@tonic-gate 					 0,		/* Get highest kvno */
337*0Sstevel@tonic-gate 					 &server_key))) {
338*0Sstevel@tonic-gate 	status = "FINDING_SERVER_KEY";
339*0Sstevel@tonic-gate 	goto errout;
340*0Sstevel@tonic-gate     }
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate     /* convert server.key into a real key (it may be encrypted
343*0Sstevel@tonic-gate        in the database) */
344*0Sstevel@tonic-gate     if ((errcode = krb5_dbekd_decrypt_key_data(kdc_context, &master_keyblock,
345*0Sstevel@tonic-gate 					       server_key, &encrypting_key,
346*0Sstevel@tonic-gate 					       NULL))) {
347*0Sstevel@tonic-gate 	status = "DECRYPT_SERVER_KEY";
348*0Sstevel@tonic-gate 	goto errout;
349*0Sstevel@tonic-gate     }
350*0Sstevel@tonic-gate     if ((encrypting_key.enctype == ENCTYPE_DES_CBC_CRC) &&
351*0Sstevel@tonic-gate 	(isflagset(server.attributes, KRB5_KDB_SUPPORT_DESMD5)))
352*0Sstevel@tonic-gate 	encrypting_key.enctype = ENCTYPE_DES_CBC_MD5;
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate     errcode = krb5_encrypt_tkt_part(kdc_context, &encrypting_key, &ticket_reply);
355*0Sstevel@tonic-gate     krb5_free_keyblock_contents(kdc_context, &encrypting_key);
356*0Sstevel@tonic-gate     encrypting_key.contents = 0;
357*0Sstevel@tonic-gate     if (errcode) {
358*0Sstevel@tonic-gate 	status = "ENCRYPTING_TICKET";
359*0Sstevel@tonic-gate 	goto errout;
360*0Sstevel@tonic-gate     }
361*0Sstevel@tonic-gate     ticket_reply.enc_part.kvno = server_key->key_data_kvno;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate     /*
364*0Sstevel@tonic-gate      * Find the appropriate client key.  We search in the order specified
365*0Sstevel@tonic-gate      * by request keytype list.
366*0Sstevel@tonic-gate      */
367*0Sstevel@tonic-gate     client_key = (krb5_key_data *) NULL;
368*0Sstevel@tonic-gate     for (i = 0; i < request->nktypes; i++) {
369*0Sstevel@tonic-gate 	useenctype = request->ktype[i];
370*0Sstevel@tonic-gate 	if (!valid_enctype(useenctype))
371*0Sstevel@tonic-gate 	    continue;
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	if (!krb5_dbe_find_enctype(kdc_context, &client, useenctype, -1,
374*0Sstevel@tonic-gate 				   0, &client_key))
375*0Sstevel@tonic-gate 	    break;
376*0Sstevel@tonic-gate     }
377*0Sstevel@tonic-gate     if (!(client_key)) {
378*0Sstevel@tonic-gate 	/* Cannot find an appropriate key */
379*0Sstevel@tonic-gate 	status = "CANT_FIND_CLIENT_KEY";
380*0Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_ETYPE_NOSUPP;
381*0Sstevel@tonic-gate 	goto errout;
382*0Sstevel@tonic-gate     }
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate     /* convert client.key_data into a real key */
385*0Sstevel@tonic-gate     if ((errcode = krb5_dbekd_decrypt_key_data(kdc_context, &master_keyblock,
386*0Sstevel@tonic-gate 					       client_key, &encrypting_key,
387*0Sstevel@tonic-gate 					       NULL))) {
388*0Sstevel@tonic-gate 	status = "DECRYPT_CLIENT_KEY";
389*0Sstevel@tonic-gate 	goto errout;
390*0Sstevel@tonic-gate     }
391*0Sstevel@tonic-gate     encrypting_key.enctype = useenctype;
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate     /* Start assembling the response */
394*0Sstevel@tonic-gate     reply.msg_type = KRB5_AS_REP;
395*0Sstevel@tonic-gate     reply.client = request->client;
396*0Sstevel@tonic-gate     reply.ticket = &ticket_reply;
397*0Sstevel@tonic-gate     reply_encpart.session = &session_key;
398*0Sstevel@tonic-gate     if ((errcode = fetch_last_req_info(&client, &reply_encpart.last_req))) {
399*0Sstevel@tonic-gate 	status = "FETCH_LAST_REQ";
400*0Sstevel@tonic-gate 	goto errout;
401*0Sstevel@tonic-gate     }
402*0Sstevel@tonic-gate     reply_encpart.nonce = request->nonce;
403*0Sstevel@tonic-gate     reply_encpart.key_exp = client.expiration;
404*0Sstevel@tonic-gate     reply_encpart.flags = enc_tkt_reply.flags;
405*0Sstevel@tonic-gate     reply_encpart.server = ticket_reply.server;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate     /* copy the time fields EXCEPT for authtime; it's location
408*0Sstevel@tonic-gate        is used for ktime */
409*0Sstevel@tonic-gate     reply_encpart.times = enc_tkt_reply.times;
410*0Sstevel@tonic-gate     reply_encpart.times.authtime = authtime = kdc_time;
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate     reply_encpart.caddrs = enc_tkt_reply.caddrs;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate     /* Fetch the padata info to be returned */
415*0Sstevel@tonic-gate     errcode = return_padata(kdc_context, &client, request, &reply, client_key,
416*0Sstevel@tonic-gate 			    &encrypting_key);
417*0Sstevel@tonic-gate     if (errcode) {
418*0Sstevel@tonic-gate 	status = "KDC_RETURN_PADATA";
419*0Sstevel@tonic-gate 	goto errout;
420*0Sstevel@tonic-gate     }
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate     /* now encode/encrypt the response */
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate     reply.enc_part.enctype = encrypting_key.enctype;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate     errcode = krb5_encode_kdc_rep(kdc_context, KRB5_AS_REP, &reply_encpart,
427*0Sstevel@tonic-gate 				  0, &encrypting_key,  &reply, response);
428*0Sstevel@tonic-gate     krb5_free_keyblock_contents(kdc_context, &encrypting_key);
429*0Sstevel@tonic-gate     encrypting_key.contents = 0;
430*0Sstevel@tonic-gate     reply.enc_part.kvno = client_key->key_data_kvno;
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate     if (errcode) {
433*0Sstevel@tonic-gate 	status = "ENCODE_KDC_REP";
434*0Sstevel@tonic-gate 	goto errout;
435*0Sstevel@tonic-gate     }
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate     /* these parts are left on as a courtesy from krb5_encode_kdc_rep so we
438*0Sstevel@tonic-gate        can use them in raw form if needed.  But, we don't... */
439*0Sstevel@tonic-gate     memset(reply.enc_part.ciphertext.data, 0, reply.enc_part.ciphertext.length);
440*0Sstevel@tonic-gate     free(reply.enc_part.ciphertext.data);
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate     audit_krb5kdc_as_req(&from_in4, (in_port_t)from->port, (in_port_t)portnum,
443*0Sstevel@tonic-gate                         cname, sname, 0);
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate     krb5_klog_syslog(LOG_INFO, "AS_REQ %s(%d): ISSUE: authtime %d, %s for %s",
446*0Sstevel@tonic-gate 	             fromstring, portnum, authtime, cname, sname);
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate #ifdef	KRBCONF_KDC_MODIFIES_KDB
449*0Sstevel@tonic-gate     /*
450*0Sstevel@tonic-gate      * If we get this far, we successfully did the AS_REQ.
451*0Sstevel@tonic-gate      */
452*0Sstevel@tonic-gate     client.last_success = kdc_time;
453*0Sstevel@tonic-gate     client.fail_auth_count = 0;
454*0Sstevel@tonic-gate     update_client = 1;
455*0Sstevel@tonic-gate #endif	/* KRBCONF_KDC_MODIFIES_KDB */
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate errout:
458*0Sstevel@tonic-gate     if (status) {
459*0Sstevel@tonic-gate 	    audit_krb5kdc_as_req(&from_in4, (in_port_t)from->port,
460*0Sstevel@tonic-gate 				(in_port_t)portnum, cname, sname, errcode);
461*0Sstevel@tonic-gate 	    krb5_klog_syslog(LOG_INFO, "AS_REQ %s(%d): %s: %s for %s%s%s",
462*0Sstevel@tonic-gate 	       fromstring, portnum, status,
463*0Sstevel@tonic-gate 	       cname ? cname : "<unknown client>",
464*0Sstevel@tonic-gate 	       sname ? sname : "<unknown server>",
465*0Sstevel@tonic-gate 	       errcode ? ", " : "",
466*0Sstevel@tonic-gate 	       errcode ? error_message(errcode) : "");
467*0Sstevel@tonic-gate     }
468*0Sstevel@tonic-gate     if (errcode) {
469*0Sstevel@tonic-gate 	errcode -= ERROR_TABLE_BASE_krb5;
470*0Sstevel@tonic-gate 	if (errcode < 0 || errcode > 128)
471*0Sstevel@tonic-gate 	    errcode = KRB_ERR_GENERIC;
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	errcode = prepare_error_as(request, errcode, &e_data, response);
474*0Sstevel@tonic-gate     }
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate     krb5_free_keyblock_contents(kdc_context, &encrypting_key);
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate     if (reply.padata)
479*0Sstevel@tonic-gate 	krb5_free_pa_data(kdc_context, reply.padata);
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate     if (cname)
482*0Sstevel@tonic-gate 	    free(cname);
483*0Sstevel@tonic-gate     if (sname)
484*0Sstevel@tonic-gate 	    free(sname);
485*0Sstevel@tonic-gate     if (c_nprincs) {
486*0Sstevel@tonic-gate #ifdef	KRBCONF_KDC_MODIFIES_KDB
487*0Sstevel@tonic-gate 	if (update_client) {
488*0Sstevel@tonic-gate 	    krb5_db_put_principal(kdc_context, &client, &c_nprincs);
489*0Sstevel@tonic-gate 	    /*
490*0Sstevel@tonic-gate 	     * ptooey.  We want krb5_db_sync() or something like that.
491*0Sstevel@tonic-gate 	     */
492*0Sstevel@tonic-gate 	    krb5_db_fini(kdc_context);
493*0Sstevel@tonic-gate 	    if (kdc_active_realm->realm_dbname)
494*0Sstevel@tonic-gate 		krb5_db_set_name(kdc_active_realm->realm_context,
495*0Sstevel@tonic-gate 				 kdc_active_realm->realm_dbname);
496*0Sstevel@tonic-gate 	    krb5_db_init(kdc_context);
497*0Sstevel@tonic-gate 	    /* Reset master key */
498*0Sstevel@tonic-gate 	    krb5_db_set_mkey(kdc_context, &kdc_active_realm->realm_encblock);
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate #endif	/* KRBCONF_KDC_MODIFIES_KDB */
501*0Sstevel@tonic-gate 	krb5_db_free_principal(kdc_context, &client, c_nprincs);
502*0Sstevel@tonic-gate     }
503*0Sstevel@tonic-gate     if (s_nprincs)
504*0Sstevel@tonic-gate 	krb5_db_free_principal(kdc_context, &server, s_nprincs);
505*0Sstevel@tonic-gate     if (session_key.contents)
506*0Sstevel@tonic-gate 	krb5_free_keyblock_contents(kdc_context, &session_key);
507*0Sstevel@tonic-gate     if (ticket_reply.enc_part.ciphertext.data) {
508*0Sstevel@tonic-gate 	memset(ticket_reply.enc_part.ciphertext.data , 0,
509*0Sstevel@tonic-gate 	       ticket_reply.enc_part.ciphertext.length);
510*0Sstevel@tonic-gate 	free(ticket_reply.enc_part.ciphertext.data);
511*0Sstevel@tonic-gate     }
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate     krb5_free_data_contents(kdc_context, &e_data);
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate     return errcode;
516*0Sstevel@tonic-gate }
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate static krb5_error_code
519*0Sstevel@tonic-gate prepare_error_as (request, error, e_data, response)
520*0Sstevel@tonic-gate register krb5_kdc_req *request;
521*0Sstevel@tonic-gate int error;
522*0Sstevel@tonic-gate krb5_data *e_data;
523*0Sstevel@tonic-gate krb5_data **response;
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate     krb5_error errpkt;
526*0Sstevel@tonic-gate     krb5_error_code retval;
527*0Sstevel@tonic-gate     krb5_data *scratch;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate     errpkt.ctime = request->nonce;
530*0Sstevel@tonic-gate     errpkt.cusec = 0;
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate     if ((retval = krb5_us_timeofday(kdc_context, &errpkt.stime,
533*0Sstevel@tonic-gate 				    &errpkt.susec)))
534*0Sstevel@tonic-gate 	return(retval);
535*0Sstevel@tonic-gate     errpkt.error = error;
536*0Sstevel@tonic-gate     errpkt.server = request->server;
537*0Sstevel@tonic-gate     errpkt.client = request->client;
538*0Sstevel@tonic-gate     errpkt.text.length = strlen(error_message(error+KRB5KDC_ERR_NONE))+1;
539*0Sstevel@tonic-gate     if (!(errpkt.text.data = malloc(errpkt.text.length)))
540*0Sstevel@tonic-gate 	return ENOMEM;
541*0Sstevel@tonic-gate     (void) strcpy(errpkt.text.data, error_message(error+KRB5KDC_ERR_NONE));
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate     if (!(scratch = (krb5_data *)malloc(sizeof(*scratch)))) {
544*0Sstevel@tonic-gate 	free(errpkt.text.data);
545*0Sstevel@tonic-gate 	return ENOMEM;
546*0Sstevel@tonic-gate     }
547*0Sstevel@tonic-gate     if (e_data && e_data->data) {
548*0Sstevel@tonic-gate 	errpkt.e_data = *e_data;
549*0Sstevel@tonic-gate     } else {
550*0Sstevel@tonic-gate 	errpkt.e_data.length = 0;
551*0Sstevel@tonic-gate 	errpkt.e_data.data = 0;
552*0Sstevel@tonic-gate     }
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate     retval = krb5_mk_error(kdc_context, &errpkt, scratch);
555*0Sstevel@tonic-gate     free(errpkt.text.data);
556*0Sstevel@tonic-gate     *response = scratch;
557*0Sstevel@tonic-gate     return retval;
558*0Sstevel@tonic-gate }
559