xref: /onnv-gate/usr/src/cmd/krb5/krb5kdc/do_as_req.c (revision 7934:6aeeafc994de)
10Sstevel@tonic-gate /*
2*7934SMark.Phalan@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate 
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate  * kdc/do_as_req.c
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
110Sstevel@tonic-gate  * All Rights Reserved.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * Export of this software from the United States of America may
140Sstevel@tonic-gate  *   require a specific license from the United States Government.
150Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
160Sstevel@tonic-gate  *   export to obtain such a license before exporting.
170Sstevel@tonic-gate  *
180Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
190Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
200Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
210Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
220Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
230Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
240Sstevel@tonic-gate  * to distribution of the software without specific, written prior
250Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
260Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
270Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
280Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
290Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
300Sstevel@tonic-gate  * or implied warranty.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * KDC Routines to deal with AS_REQ's
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
362881Smp153739 #define NEED_SOCKETS
370Sstevel@tonic-gate #include "k5-int.h"
380Sstevel@tonic-gate #include "com_err.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <syslog.h>
410Sstevel@tonic-gate #ifdef HAVE_NETINET_IN_H
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <netinet/in.h>
440Sstevel@tonic-gate #ifndef hpux
450Sstevel@tonic-gate #include <arpa/inet.h>
460Sstevel@tonic-gate #endif	/* hpux */
470Sstevel@tonic-gate #endif /* HAVE_NETINET_IN_H */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include "kdc_util.h"
500Sstevel@tonic-gate #include "policy.h"
510Sstevel@tonic-gate #include "adm.h"
520Sstevel@tonic-gate #include "adm_proto.h"
530Sstevel@tonic-gate #include "extern.h"
540Sstevel@tonic-gate 
552881Smp153739 static krb5_error_code prepare_error_as (krb5_kdc_req *, int, krb5_data *,
562881Smp153739 					 krb5_data **, const char *);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*ARGSUSED*/
590Sstevel@tonic-gate krb5_error_code
process_as_req(krb5_kdc_req * request,krb5_data * req_pkt,const krb5_fulladdr * from,krb5_data ** response)60*7934SMark.Phalan@Sun.COM process_as_req(krb5_kdc_req *request, krb5_data *req_pkt,
61*7934SMark.Phalan@Sun.COM 	       const krb5_fulladdr *from, krb5_data **response)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate     krb5_db_entry client, server;
640Sstevel@tonic-gate     krb5_kdc_rep reply;
650Sstevel@tonic-gate     krb5_enc_kdc_rep_part reply_encpart;
660Sstevel@tonic-gate     krb5_ticket ticket_reply;
670Sstevel@tonic-gate     krb5_enc_tkt_part enc_tkt_reply;
680Sstevel@tonic-gate     krb5_error_code errcode;
690Sstevel@tonic-gate     int c_nprincs = 0, s_nprincs = 0;
700Sstevel@tonic-gate     krb5_boolean more;
713641Ssemery     krb5_timestamp kdc_time, authtime, etime = 0;
720Sstevel@tonic-gate     krb5_keyblock session_key;
730Sstevel@tonic-gate     krb5_keyblock encrypting_key;
740Sstevel@tonic-gate     const char *status;
750Sstevel@tonic-gate     krb5_key_data  *server_key, *client_key;
760Sstevel@tonic-gate     krb5_enctype useenctype;
770Sstevel@tonic-gate #ifdef	KRBCONF_KDC_MODIFIES_KDB
780Sstevel@tonic-gate     krb5_boolean update_client = 0;
790Sstevel@tonic-gate #endif	/* KRBCONF_KDC_MODIFIES_KDB */
800Sstevel@tonic-gate     krb5_data e_data;
810Sstevel@tonic-gate     register int i;
820Sstevel@tonic-gate     krb5_timestamp until, rtime;
830Sstevel@tonic-gate     long long tmp_client_times, tmp_server_times, tmp_realm_times;
842881Smp153739     char *cname = 0, *sname = 0;
852881Smp153739     const char *fromstring = 0;
862881Smp153739     char ktypestr[128];
872881Smp153739     char rep_etypestr[128];
882881Smp153739     char fromstringbuf[70];
89*7934SMark.Phalan@Sun.COM     void *pa_context = NULL;
900Sstevel@tonic-gate     struct in_addr from_in4;	/* IPv4 address of sender */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate     ticket_reply.enc_part.ciphertext.data = 0;
930Sstevel@tonic-gate     e_data.data = 0;
942881Smp153739     (void) memset(&encrypting_key, 0, sizeof(krb5_keyblock));
950Sstevel@tonic-gate     reply.padata = 0; /* avoid bogus free in error_out */
960Sstevel@tonic-gate     (void) memset(&session_key, 0, sizeof(krb5_keyblock));
97*7934SMark.Phalan@Sun.COM     enc_tkt_reply.authorization_data = NULL;
980Sstevel@tonic-gate 
992881Smp153739     ktypes2str(ktypestr, sizeof(ktypestr),
1002881Smp153739 	       request->nktypes, request->ktype);
1012881Smp153739 
1020Sstevel@tonic-gate 	(void) memcpy(&from_in4, from->address->contents, /* SUNW */
1030Sstevel@tonic-gate 		    sizeof (struct in_addr));
1042881Smp153739 
1052881Smp153739     fromstring = inet_ntop(ADDRTYPE2FAMILY (from->address->addrtype),
1062881Smp153739 			   &from_in4,
1072881Smp153739 			   fromstringbuf, sizeof(fromstringbuf));
1080Sstevel@tonic-gate     if (!fromstring)
1090Sstevel@tonic-gate 	fromstring = "<unknown>";
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate     if (!request->client) {
1120Sstevel@tonic-gate 	status = "NULL_CLIENT";
1130Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1140Sstevel@tonic-gate 	goto errout;
1150Sstevel@tonic-gate     }
1160Sstevel@tonic-gate     if ((errcode = krb5_unparse_name(kdc_context, request->client, &cname))) {
1170Sstevel@tonic-gate 	status = "UNPARSING_CLIENT";
1180Sstevel@tonic-gate 	goto errout;
1190Sstevel@tonic-gate     }
1200Sstevel@tonic-gate     limit_string(cname);
1210Sstevel@tonic-gate     if (!request->server) {
1220Sstevel@tonic-gate 	status = "NULL_SERVER";
1230Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1240Sstevel@tonic-gate 	goto errout;
1250Sstevel@tonic-gate     }
1260Sstevel@tonic-gate     if ((errcode = krb5_unparse_name(kdc_context, request->server, &sname))) {
1270Sstevel@tonic-gate 	status = "UNPARSING_SERVER";
1280Sstevel@tonic-gate 	goto errout;
1290Sstevel@tonic-gate     }
1300Sstevel@tonic-gate     limit_string(sname);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate     c_nprincs = 1;
1330Sstevel@tonic-gate     if ((errcode = krb5_db_get_principal(kdc_context, request->client,
1340Sstevel@tonic-gate 					 &client, &c_nprincs, &more))) {
1350Sstevel@tonic-gate 	status = "LOOKING_UP_CLIENT";
1360Sstevel@tonic-gate 	c_nprincs = 0;
1370Sstevel@tonic-gate 	goto errout;
1380Sstevel@tonic-gate     }
1390Sstevel@tonic-gate     if (more) {
1400Sstevel@tonic-gate 	status = "NON-UNIQUE_CLIENT";
1410Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE;
1420Sstevel@tonic-gate 	goto errout;
1430Sstevel@tonic-gate     } else if (c_nprincs != 1) {
1440Sstevel@tonic-gate 	status = "CLIENT_NOT_FOUND";
1450Sstevel@tonic-gate #ifdef KRBCONF_VAGUE_ERRORS
1460Sstevel@tonic-gate 	errcode = KRB5KRB_ERR_GENERIC;
1470Sstevel@tonic-gate #else
1480Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1490Sstevel@tonic-gate #endif
1500Sstevel@tonic-gate 	goto errout;
1510Sstevel@tonic-gate     }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate     s_nprincs = 1;
1540Sstevel@tonic-gate     if ((errcode = krb5_db_get_principal(kdc_context, request->server, &server,
1550Sstevel@tonic-gate 					 &s_nprincs, &more))) {
1560Sstevel@tonic-gate 	status = "LOOKING_UP_SERVER";
1570Sstevel@tonic-gate 	goto errout;
1580Sstevel@tonic-gate     }
1590Sstevel@tonic-gate     if (more) {
1600Sstevel@tonic-gate 	status = "NON-UNIQUE_SERVER";
1610Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE;
1620Sstevel@tonic-gate 	goto errout;
1630Sstevel@tonic-gate     } else if (s_nprincs != 1) {
1640Sstevel@tonic-gate 	status = "SERVER_NOT_FOUND";
1650Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1660Sstevel@tonic-gate 	goto errout;
1670Sstevel@tonic-gate     }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate     if ((errcode = krb5_timeofday(kdc_context, &kdc_time))) {
1700Sstevel@tonic-gate 	status = "TIMEOFDAY";
1710Sstevel@tonic-gate 	goto errout;
1720Sstevel@tonic-gate     }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate     if ((errcode = validate_as_request(request, client, server,
1750Sstevel@tonic-gate 				      kdc_time, &status))) {
1760Sstevel@tonic-gate 	if (!status)
1770Sstevel@tonic-gate 	    status = "UNKNOWN_REASON";
1780Sstevel@tonic-gate 	errcode += ERROR_TABLE_BASE_krb5;
1790Sstevel@tonic-gate 	goto errout;
1800Sstevel@tonic-gate     }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate     /*
1830Sstevel@tonic-gate      * Select the keytype for the ticket session key.
1840Sstevel@tonic-gate      */
1850Sstevel@tonic-gate     if ((useenctype = select_session_keytype(kdc_context, &server,
1860Sstevel@tonic-gate 					     request->nktypes,
1870Sstevel@tonic-gate 					     request->ktype)) == 0) {
1880Sstevel@tonic-gate 	/* unsupported ktype */
1890Sstevel@tonic-gate 	status = "BAD_ENCRYPTION_TYPE";
1900Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_ETYPE_NOSUPP;
1910Sstevel@tonic-gate 	goto errout;
1920Sstevel@tonic-gate     }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate     if ((errcode = krb5_c_make_random_key(kdc_context, useenctype,
1952881Smp153739 					  &session_key))) {
1960Sstevel@tonic-gate 	/* random key failed */
1970Sstevel@tonic-gate 	status = "RANDOM_KEY_FAILED";
1980Sstevel@tonic-gate 	goto errout;
1990Sstevel@tonic-gate     }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate     ticket_reply.server = request->server;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate     enc_tkt_reply.flags = 0;
2040Sstevel@tonic-gate     setflag(enc_tkt_reply.flags, TKT_FLG_INITIAL);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate     	/* It should be noted that local policy may affect the  */
2070Sstevel@tonic-gate         /* processing of any of these flags.  For example, some */
2080Sstevel@tonic-gate         /* realms may refuse to issue renewable tickets         */
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_FORWARDABLE))
2110Sstevel@tonic-gate 	setflag(enc_tkt_reply.flags, TKT_FLG_FORWARDABLE);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_PROXIABLE))
2140Sstevel@tonic-gate 	    setflag(enc_tkt_reply.flags, TKT_FLG_PROXIABLE);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_ALLOW_POSTDATE))
2170Sstevel@tonic-gate 	    setflag(enc_tkt_reply.flags, TKT_FLG_MAY_POSTDATE);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate     enc_tkt_reply.session = &session_key;
2200Sstevel@tonic-gate     enc_tkt_reply.client = request->client;
2210Sstevel@tonic-gate     enc_tkt_reply.transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
2220Sstevel@tonic-gate     enc_tkt_reply.transited.tr_contents = empty_string; /* equivalent of "" */
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate     enc_tkt_reply.times.authtime = kdc_time;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_POSTDATED)) {
2270Sstevel@tonic-gate 	setflag(enc_tkt_reply.flags, TKT_FLG_POSTDATED);
2280Sstevel@tonic-gate 	setflag(enc_tkt_reply.flags, TKT_FLG_INVALID);
2290Sstevel@tonic-gate 	enc_tkt_reply.times.starttime = request->from;
2300Sstevel@tonic-gate     } else
2310Sstevel@tonic-gate 	enc_tkt_reply.times.starttime = kdc_time;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate     until = (request->till == 0) ? kdc_infinity : request->till;
2340Sstevel@tonic-gate 	/* These numbers could easily be large
2350Sstevel@tonic-gate 	 * use long long variables to ensure that they don't
2360Sstevel@tonic-gate 	 * result in negative values when added.
2370Sstevel@tonic-gate 	*/
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate     tmp_client_times = (long long) enc_tkt_reply.times.starttime + client.max_life;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate     tmp_server_times = (long long) enc_tkt_reply.times.starttime + server.max_life;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate     tmp_realm_times = (long long) enc_tkt_reply.times.starttime + max_life_for_realm;
2440Sstevel@tonic-gate 
2452881Smp153739     enc_tkt_reply.times.endtime =
2462881Smp153739 	min(until,
2470Sstevel@tonic-gate 	    min(tmp_client_times,
2480Sstevel@tonic-gate 		min(tmp_server_times,
2490Sstevel@tonic-gate 			min(tmp_realm_times,KRB5_KDB_EXPIRATION))));
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_RENEWABLE_OK) &&
2520Sstevel@tonic-gate 	!isflagset(client.attributes, KRB5_KDB_DISALLOW_RENEWABLE) &&
2530Sstevel@tonic-gate 	(enc_tkt_reply.times.endtime < request->till)) {
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/* we set the RENEWABLE option for later processing */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	setflag(request->kdc_options, KDC_OPT_RENEWABLE);
2580Sstevel@tonic-gate 	request->rtime = request->till;
2590Sstevel@tonic-gate     }
2600Sstevel@tonic-gate     rtime = (request->rtime == 0) ? kdc_infinity : request->rtime;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate     if (isflagset(request->kdc_options, KDC_OPT_RENEWABLE)) {
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * XXX Should we squelch the output renew_till to be no
2650Sstevel@tonic-gate 	 * earlier than the endtime of the ticket?
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	setflag(enc_tkt_reply.flags, TKT_FLG_RENEWABLE);
2680Sstevel@tonic-gate 	tmp_client_times = (double) enc_tkt_reply.times.starttime + client.max_renewable_life;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate     	tmp_server_times = (double) enc_tkt_reply.times.starttime + server.max_renewable_life;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate     	tmp_realm_times = (double) enc_tkt_reply.times.starttime + max_renewable_life_for_realm;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	enc_tkt_reply.times.renew_till =
2752881Smp153739 	    min(rtime, min(tmp_client_times,
2762881Smp153739 		       min(tmp_server_times,
2772881Smp153739 			   min(tmp_realm_times,KRB5_KDB_EXPIRATION))));
2780Sstevel@tonic-gate     } else
2790Sstevel@tonic-gate 	enc_tkt_reply.times.renew_till = 0; /* XXX */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate     /* starttime is optional, and treated as authtime if not present.
2820Sstevel@tonic-gate        so we can nuke it if it matches */
2830Sstevel@tonic-gate     if (enc_tkt_reply.times.starttime == enc_tkt_reply.times.authtime)
2840Sstevel@tonic-gate 	enc_tkt_reply.times.starttime = 0;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate     enc_tkt_reply.caddrs = request->addresses;
2870Sstevel@tonic-gate     enc_tkt_reply.authorization_data = 0;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate     /*
2900Sstevel@tonic-gate      * Check the preauthentication if it is there.
2910Sstevel@tonic-gate      */
2920Sstevel@tonic-gate     if (request->padata) {
293*7934SMark.Phalan@Sun.COM 	errcode = check_padata(kdc_context, &client, req_pkt, request,
294*7934SMark.Phalan@Sun.COM 			       &enc_tkt_reply, &pa_context, &e_data);
2950Sstevel@tonic-gate 	if (errcode) {
2960Sstevel@tonic-gate #ifdef KRBCONF_KDC_MODIFIES_KDB
2970Sstevel@tonic-gate 	    /*
2980Sstevel@tonic-gate 	     * Note: this doesn't work if you're using slave servers!!!
2990Sstevel@tonic-gate 	     * It also causes the database to be modified (and thus
3000Sstevel@tonic-gate 	     * need to be locked) frequently.
3010Sstevel@tonic-gate 	     */
3020Sstevel@tonic-gate 	    if (client.fail_auth_count < KRB5_MAX_FAIL_COUNT) {
3030Sstevel@tonic-gate 		client.fail_auth_count = client.fail_auth_count + 1;
3040Sstevel@tonic-gate 		if (client.fail_auth_count == KRB5_MAX_FAIL_COUNT) {
3050Sstevel@tonic-gate 		    client.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 	    }
3080Sstevel@tonic-gate 	    client.last_failed = kdc_time;
3090Sstevel@tonic-gate 	    update_client = 1;
3100Sstevel@tonic-gate #endif
3110Sstevel@tonic-gate 	    status = "PREAUTH_FAILED";
3120Sstevel@tonic-gate #ifdef KRBCONF_VAGUE_ERRORS
3130Sstevel@tonic-gate 	    errcode = KRB5KRB_ERR_GENERIC;
3140Sstevel@tonic-gate #endif
3150Sstevel@tonic-gate 	    goto errout;
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate     }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate     /*
3200Sstevel@tonic-gate      * Final check before handing out ticket: If the client requires
3210Sstevel@tonic-gate      * preauthentication, verify that the proper kind of
3220Sstevel@tonic-gate      * preauthentication was carried out.
3230Sstevel@tonic-gate      */
3240Sstevel@tonic-gate     status = missing_required_preauth(&client, &server, &enc_tkt_reply);
3250Sstevel@tonic-gate     if (status) {
3260Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_PREAUTH_REQUIRED;
3270Sstevel@tonic-gate 	get_preauth_hint_list(request, &client, &server, &e_data);
3280Sstevel@tonic-gate 	goto errout;
3290Sstevel@tonic-gate     }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate     ticket_reply.enc_part2 = &enc_tkt_reply;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate     /*
3340Sstevel@tonic-gate      * Find the server key
3350Sstevel@tonic-gate      */
3360Sstevel@tonic-gate     if ((errcode = krb5_dbe_find_enctype(kdc_context, &server,
3370Sstevel@tonic-gate 					 -1, /* ignore keytype */
3380Sstevel@tonic-gate 					 -1,		/* Ignore salttype */
3390Sstevel@tonic-gate 					 0,		/* Get highest kvno */
3400Sstevel@tonic-gate 					 &server_key))) {
3410Sstevel@tonic-gate 	status = "FINDING_SERVER_KEY";
3420Sstevel@tonic-gate 	goto errout;
3430Sstevel@tonic-gate     }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate     /* convert server.key into a real key (it may be encrypted
3460Sstevel@tonic-gate        in the database) */
3470Sstevel@tonic-gate     if ((errcode = krb5_dbekd_decrypt_key_data(kdc_context, &master_keyblock,
3480Sstevel@tonic-gate 					       server_key, &encrypting_key,
3490Sstevel@tonic-gate 					       NULL))) {
3500Sstevel@tonic-gate 	status = "DECRYPT_SERVER_KEY";
3510Sstevel@tonic-gate 	goto errout;
3520Sstevel@tonic-gate     }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate     errcode = krb5_encrypt_tkt_part(kdc_context, &encrypting_key, &ticket_reply);
3550Sstevel@tonic-gate     krb5_free_keyblock_contents(kdc_context, &encrypting_key);
3560Sstevel@tonic-gate     encrypting_key.contents = 0;
3570Sstevel@tonic-gate     if (errcode) {
3580Sstevel@tonic-gate 	status = "ENCRYPTING_TICKET";
3590Sstevel@tonic-gate 	goto errout;
3600Sstevel@tonic-gate     }
3610Sstevel@tonic-gate     ticket_reply.enc_part.kvno = server_key->key_data_kvno;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate     /*
3640Sstevel@tonic-gate      * Find the appropriate client key.  We search in the order specified
3650Sstevel@tonic-gate      * by request keytype list.
3660Sstevel@tonic-gate      */
3670Sstevel@tonic-gate     client_key = (krb5_key_data *) NULL;
3680Sstevel@tonic-gate     for (i = 0; i < request->nktypes; i++) {
3690Sstevel@tonic-gate 	useenctype = request->ktype[i];
370781Sgtb 	if (!krb5_c_valid_enctype(useenctype))
3710Sstevel@tonic-gate 	    continue;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if (!krb5_dbe_find_enctype(kdc_context, &client, useenctype, -1,
3740Sstevel@tonic-gate 				   0, &client_key))
3750Sstevel@tonic-gate 	    break;
3760Sstevel@tonic-gate     }
3770Sstevel@tonic-gate     if (!(client_key)) {
3780Sstevel@tonic-gate 	/* Cannot find an appropriate key */
3790Sstevel@tonic-gate 	status = "CANT_FIND_CLIENT_KEY";
3800Sstevel@tonic-gate 	errcode = KRB5KDC_ERR_ETYPE_NOSUPP;
3810Sstevel@tonic-gate 	goto errout;
3820Sstevel@tonic-gate     }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate     /* convert client.key_data into a real key */
3850Sstevel@tonic-gate     if ((errcode = krb5_dbekd_decrypt_key_data(kdc_context, &master_keyblock,
3860Sstevel@tonic-gate 					       client_key, &encrypting_key,
3870Sstevel@tonic-gate 					       NULL))) {
3880Sstevel@tonic-gate 	status = "DECRYPT_CLIENT_KEY";
3890Sstevel@tonic-gate 	goto errout;
3900Sstevel@tonic-gate     }
3910Sstevel@tonic-gate     encrypting_key.enctype = useenctype;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate     /* Start assembling the response */
3940Sstevel@tonic-gate     reply.msg_type = KRB5_AS_REP;
3950Sstevel@tonic-gate     reply.client = request->client;
3960Sstevel@tonic-gate     reply.ticket = &ticket_reply;
3970Sstevel@tonic-gate     reply_encpart.session = &session_key;
3980Sstevel@tonic-gate     if ((errcode = fetch_last_req_info(&client, &reply_encpart.last_req))) {
3990Sstevel@tonic-gate 	status = "FETCH_LAST_REQ";
4000Sstevel@tonic-gate 	goto errout;
4010Sstevel@tonic-gate     }
4020Sstevel@tonic-gate     reply_encpart.nonce = request->nonce;
4033641Ssemery 
4043641Ssemery     /*
4053641Ssemery      * Take the minimum of expiration or pw_expiration if not zero.
4063641Ssemery      */
4073641Ssemery     if (client.expiration != 0 && client.pw_expiration != 0)
4083641Ssemery     	etime = min(client.expiration, client.pw_expiration);
4093641Ssemery     else
4103641Ssemery 	etime = client.expiration ? client.expiration : client.pw_expiration;
4113641Ssemery 
4123641Ssemery     reply_encpart.key_exp = etime;
4130Sstevel@tonic-gate     reply_encpart.flags = enc_tkt_reply.flags;
4140Sstevel@tonic-gate     reply_encpart.server = ticket_reply.server;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate     /* copy the time fields EXCEPT for authtime; it's location
4170Sstevel@tonic-gate        is used for ktime */
4180Sstevel@tonic-gate     reply_encpart.times = enc_tkt_reply.times;
4190Sstevel@tonic-gate     reply_encpart.times.authtime = authtime = kdc_time;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate     reply_encpart.caddrs = enc_tkt_reply.caddrs;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate     /* Fetch the padata info to be returned */
424*7934SMark.Phalan@Sun.COM     errcode = return_padata(kdc_context, &client, req_pkt, request,
425*7934SMark.Phalan@Sun.COM 			    &reply, client_key, &encrypting_key, &pa_context);
4260Sstevel@tonic-gate     if (errcode) {
4270Sstevel@tonic-gate 	status = "KDC_RETURN_PADATA";
4280Sstevel@tonic-gate 	goto errout;
4290Sstevel@tonic-gate     }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate     /* now encode/encrypt the response */
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate     reply.enc_part.enctype = encrypting_key.enctype;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate     errcode = krb5_encode_kdc_rep(kdc_context, KRB5_AS_REP, &reply_encpart,
4360Sstevel@tonic-gate 				  0, &encrypting_key,  &reply, response);
4370Sstevel@tonic-gate     krb5_free_keyblock_contents(kdc_context, &encrypting_key);
4380Sstevel@tonic-gate     encrypting_key.contents = 0;
4390Sstevel@tonic-gate     reply.enc_part.kvno = client_key->key_data_kvno;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate     if (errcode) {
4420Sstevel@tonic-gate 	status = "ENCODE_KDC_REP";
4430Sstevel@tonic-gate 	goto errout;
4440Sstevel@tonic-gate     }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate     /* these parts are left on as a courtesy from krb5_encode_kdc_rep so we
4470Sstevel@tonic-gate        can use them in raw form if needed.  But, we don't... */
4480Sstevel@tonic-gate     memset(reply.enc_part.ciphertext.data, 0, reply.enc_part.ciphertext.length);
4490Sstevel@tonic-gate     free(reply.enc_part.ciphertext.data);
4500Sstevel@tonic-gate 
4512881Smp153739     /* SUNW14resync:
4522881Smp153739      * The third argument to audit_krb5kdc_as_req() is zero as the local
4532881Smp153739      * portnumber is no longer passed to process_as_req().
4542881Smp153739      */
4552881Smp153739     audit_krb5kdc_as_req(&from_in4, (in_port_t)from->port, 0,
4560Sstevel@tonic-gate                         cname, sname, 0);
4572881Smp153739     rep_etypes2str(rep_etypestr, sizeof(rep_etypestr), &reply);
4582881Smp153739     krb5_klog_syslog(LOG_INFO,
4592881Smp153739 		     "AS_REQ (%s) %s: ISSUE: authtime %d, "
4602881Smp153739 		     "%s, %s for %s",
4612881Smp153739 		     ktypestr,
4622881Smp153739 	             fromstring, authtime,
4632881Smp153739 		     rep_etypestr,
4642881Smp153739 		     cname, sname);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate #ifdef	KRBCONF_KDC_MODIFIES_KDB
4670Sstevel@tonic-gate     /*
4680Sstevel@tonic-gate      * If we get this far, we successfully did the AS_REQ.
4690Sstevel@tonic-gate      */
4700Sstevel@tonic-gate     client.last_success = kdc_time;
4710Sstevel@tonic-gate     client.fail_auth_count = 0;
4720Sstevel@tonic-gate     update_client = 1;
4730Sstevel@tonic-gate #endif	/* KRBCONF_KDC_MODIFIES_KDB */
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate errout:
476*7934SMark.Phalan@Sun.COM     if (pa_context)
477*7934SMark.Phalan@Sun.COM 	free_padata_context(kdc_context, &pa_context);
478*7934SMark.Phalan@Sun.COM 
4790Sstevel@tonic-gate     if (status) {
480*7934SMark.Phalan@Sun.COM 	const char * emsg = 0;
481*7934SMark.Phalan@Sun.COM 	if (errcode)
482*7934SMark.Phalan@Sun.COM 	    emsg = krb5_get_error_message (kdc_context, errcode);
483*7934SMark.Phalan@Sun.COM 
4840Sstevel@tonic-gate 	    audit_krb5kdc_as_req(&from_in4, (in_port_t)from->port,
4852881Smp153739 				0, cname, sname, errcode);
4862881Smp153739         krb5_klog_syslog(LOG_INFO, "AS_REQ (%s) %s: %s: %s for %s%s%s",
4872881Smp153739 			 ktypestr,
4882881Smp153739 	       fromstring, status,
4890Sstevel@tonic-gate 	       cname ? cname : "<unknown client>",
4900Sstevel@tonic-gate 	       sname ? sname : "<unknown server>",
4910Sstevel@tonic-gate 	       errcode ? ", " : "",
492*7934SMark.Phalan@Sun.COM 	       errcode ? emsg : "");
493*7934SMark.Phalan@Sun.COM 	if (errcode)
494*7934SMark.Phalan@Sun.COM 	    krb5_free_error_message (kdc_context, emsg);
4950Sstevel@tonic-gate     }
4960Sstevel@tonic-gate     if (errcode) {
497*7934SMark.Phalan@Sun.COM         int got_err = 0;
498*7934SMark.Phalan@Sun.COM 	if (status == 0) {
499*7934SMark.Phalan@Sun.COM 	    status = krb5_get_error_message (kdc_context, errcode);
500*7934SMark.Phalan@Sun.COM 	    got_err = 1;
501*7934SMark.Phalan@Sun.COM 	}
5020Sstevel@tonic-gate 	errcode -= ERROR_TABLE_BASE_krb5;
5030Sstevel@tonic-gate 	if (errcode < 0 || errcode > 128)
5040Sstevel@tonic-gate 	    errcode = KRB_ERR_GENERIC;
5050Sstevel@tonic-gate 
5062881Smp153739 	errcode = prepare_error_as(request, errcode, &e_data, response,
5072881Smp153739 				   status);
508*7934SMark.Phalan@Sun.COM 	if (got_err) {
509*7934SMark.Phalan@Sun.COM 	    krb5_free_error_message (kdc_context, status);
510*7934SMark.Phalan@Sun.COM 	    status = 0;
511*7934SMark.Phalan@Sun.COM 	}
5120Sstevel@tonic-gate     }
5130Sstevel@tonic-gate 
514*7934SMark.Phalan@Sun.COM     if (enc_tkt_reply.authorization_data != NULL)
515*7934SMark.Phalan@Sun.COM 	krb5_free_authdata(kdc_context, enc_tkt_reply.authorization_data);
5162881Smp153739     if (encrypting_key.contents)
5172881Smp153739 	krb5_free_keyblock_contents(kdc_context, &encrypting_key);
5180Sstevel@tonic-gate     if (reply.padata)
5190Sstevel@tonic-gate 	krb5_free_pa_data(kdc_context, reply.padata);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate     if (cname)
5220Sstevel@tonic-gate 	    free(cname);
5230Sstevel@tonic-gate     if (sname)
5240Sstevel@tonic-gate 	    free(sname);
5250Sstevel@tonic-gate     if (c_nprincs) {
5260Sstevel@tonic-gate #ifdef	KRBCONF_KDC_MODIFIES_KDB
5270Sstevel@tonic-gate 	if (update_client) {
5280Sstevel@tonic-gate 	    krb5_db_put_principal(kdc_context, &client, &c_nprincs);
5290Sstevel@tonic-gate 	    /*
5300Sstevel@tonic-gate 	     * ptooey.  We want krb5_db_sync() or something like that.
5310Sstevel@tonic-gate 	     */
5320Sstevel@tonic-gate 	    krb5_db_fini(kdc_context);
5330Sstevel@tonic-gate 	    if (kdc_active_realm->realm_dbname)
5340Sstevel@tonic-gate 		krb5_db_set_name(kdc_active_realm->realm_context,
5350Sstevel@tonic-gate 				 kdc_active_realm->realm_dbname);
5360Sstevel@tonic-gate 	    krb5_db_init(kdc_context);
5370Sstevel@tonic-gate 	    /* Reset master key */
5382881Smp153739 	    krb5_db_set_mkey(kdc_context, &kdc_active_realm->realm_mkey);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate #endif	/* KRBCONF_KDC_MODIFIES_KDB */
5410Sstevel@tonic-gate 	krb5_db_free_principal(kdc_context, &client, c_nprincs);
5420Sstevel@tonic-gate     }
5430Sstevel@tonic-gate     if (s_nprincs)
5440Sstevel@tonic-gate 	krb5_db_free_principal(kdc_context, &server, s_nprincs);
5450Sstevel@tonic-gate     if (session_key.contents)
5460Sstevel@tonic-gate 	krb5_free_keyblock_contents(kdc_context, &session_key);
5470Sstevel@tonic-gate     if (ticket_reply.enc_part.ciphertext.data) {
5480Sstevel@tonic-gate 	memset(ticket_reply.enc_part.ciphertext.data , 0,
5490Sstevel@tonic-gate 	       ticket_reply.enc_part.ciphertext.length);
5500Sstevel@tonic-gate 	free(ticket_reply.enc_part.ciphertext.data);
5510Sstevel@tonic-gate     }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate     krb5_free_data_contents(kdc_context, &e_data);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate     return errcode;
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate static krb5_error_code
prepare_error_as(krb5_kdc_req * request,int error,krb5_data * e_data,krb5_data ** response,const char * status)5592881Smp153739 prepare_error_as (krb5_kdc_req *request, int error, krb5_data *e_data,
5602881Smp153739 		  krb5_data **response, const char *status)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate     krb5_error errpkt;
5630Sstevel@tonic-gate     krb5_error_code retval;
5640Sstevel@tonic-gate     krb5_data *scratch;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate     errpkt.ctime = request->nonce;
5670Sstevel@tonic-gate     errpkt.cusec = 0;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate     if ((retval = krb5_us_timeofday(kdc_context, &errpkt.stime,
5700Sstevel@tonic-gate 				    &errpkt.susec)))
5710Sstevel@tonic-gate 	return(retval);
5720Sstevel@tonic-gate     errpkt.error = error;
5730Sstevel@tonic-gate     errpkt.server = request->server;
5740Sstevel@tonic-gate     errpkt.client = request->client;
5752881Smp153739     errpkt.text.length = strlen(status)+1;
5760Sstevel@tonic-gate     if (!(errpkt.text.data = malloc(errpkt.text.length)))
5770Sstevel@tonic-gate 	return ENOMEM;
5782881Smp153739     (void) strcpy(errpkt.text.data, status);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate     if (!(scratch = (krb5_data *)malloc(sizeof(*scratch)))) {
5810Sstevel@tonic-gate 	free(errpkt.text.data);
5820Sstevel@tonic-gate 	return ENOMEM;
5830Sstevel@tonic-gate     }
5840Sstevel@tonic-gate     if (e_data && e_data->data) {
5850Sstevel@tonic-gate 	errpkt.e_data = *e_data;
5860Sstevel@tonic-gate     } else {
5870Sstevel@tonic-gate 	errpkt.e_data.length = 0;
5880Sstevel@tonic-gate 	errpkt.e_data.data = 0;
5890Sstevel@tonic-gate     }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate     retval = krb5_mk_error(kdc_context, &errpkt, scratch);
5920Sstevel@tonic-gate     free(errpkt.text.data);
593107Sbugbomb     if (retval)
594781Sgtb 	free(scratch);
595*7934SMark.Phalan@Sun.COM     else
596781Sgtb 	*response = scratch;
597781Sgtb 
5980Sstevel@tonic-gate     return retval;
5990Sstevel@tonic-gate }
600