17c478bd9Sstevel@tonic-gate /*
2*159d09a2SMark Phalan * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate
77c478bd9Sstevel@tonic-gate /*
87c478bd9Sstevel@tonic-gate * kdc/do_as_req.c
97c478bd9Sstevel@tonic-gate *
107c478bd9Sstevel@tonic-gate * Copyright 1990,1991 by the Massachusetts Institute of Technology.
117c478bd9Sstevel@tonic-gate * All Rights Reserved.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may
147c478bd9Sstevel@tonic-gate * require a specific license from the United States Government.
157c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
167c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting.
177c478bd9Sstevel@tonic-gate *
187c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
197c478bd9Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
207c478bd9Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
217c478bd9Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
227c478bd9Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
237c478bd9Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
247c478bd9Sstevel@tonic-gate * to distribution of the software without specific, written prior
257c478bd9Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
267c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a
277c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
287c478bd9Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
297c478bd9Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
307c478bd9Sstevel@tonic-gate * or implied warranty.
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * KDC Routines to deal with AS_REQ's
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
3656a424ccSmp153739 #define NEED_SOCKETS
377c478bd9Sstevel@tonic-gate #include "k5-int.h"
387c478bd9Sstevel@tonic-gate #include "com_err.h"
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <syslog.h>
417c478bd9Sstevel@tonic-gate #ifdef HAVE_NETINET_IN_H
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <netinet/in.h>
447c478bd9Sstevel@tonic-gate #ifndef hpux
457c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
467c478bd9Sstevel@tonic-gate #endif /* hpux */
477c478bd9Sstevel@tonic-gate #endif /* HAVE_NETINET_IN_H */
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #include "kdc_util.h"
507c478bd9Sstevel@tonic-gate #include "policy.h"
517c478bd9Sstevel@tonic-gate #include "adm.h"
527c478bd9Sstevel@tonic-gate #include "adm_proto.h"
537c478bd9Sstevel@tonic-gate #include "extern.h"
547c478bd9Sstevel@tonic-gate
5556a424ccSmp153739 static krb5_error_code prepare_error_as (krb5_kdc_req *, int, krb5_data *,
5656a424ccSmp153739 krb5_data **, const char *);
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
597c478bd9Sstevel@tonic-gate krb5_error_code
process_as_req(krb5_kdc_req * request,krb5_data * req_pkt,const krb5_fulladdr * from,krb5_data ** response)60*159d09a2SMark Phalan process_as_req(krb5_kdc_req *request, krb5_data *req_pkt,
61*159d09a2SMark Phalan const krb5_fulladdr *from, krb5_data **response)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate krb5_db_entry client, server;
647c478bd9Sstevel@tonic-gate krb5_kdc_rep reply;
657c478bd9Sstevel@tonic-gate krb5_enc_kdc_rep_part reply_encpart;
667c478bd9Sstevel@tonic-gate krb5_ticket ticket_reply;
677c478bd9Sstevel@tonic-gate krb5_enc_tkt_part enc_tkt_reply;
687c478bd9Sstevel@tonic-gate krb5_error_code errcode;
697c478bd9Sstevel@tonic-gate int c_nprincs = 0, s_nprincs = 0;
707c478bd9Sstevel@tonic-gate krb5_boolean more;
713125ebfcSsemery krb5_timestamp kdc_time, authtime, etime = 0;
727c478bd9Sstevel@tonic-gate krb5_keyblock session_key;
737c478bd9Sstevel@tonic-gate krb5_keyblock encrypting_key;
747c478bd9Sstevel@tonic-gate const char *status;
757c478bd9Sstevel@tonic-gate krb5_key_data *server_key, *client_key;
767c478bd9Sstevel@tonic-gate krb5_enctype useenctype;
777c478bd9Sstevel@tonic-gate #ifdef KRBCONF_KDC_MODIFIES_KDB
787c478bd9Sstevel@tonic-gate krb5_boolean update_client = 0;
797c478bd9Sstevel@tonic-gate #endif /* KRBCONF_KDC_MODIFIES_KDB */
807c478bd9Sstevel@tonic-gate krb5_data e_data;
817c478bd9Sstevel@tonic-gate register int i;
827c478bd9Sstevel@tonic-gate krb5_timestamp until, rtime;
837c478bd9Sstevel@tonic-gate long long tmp_client_times, tmp_server_times, tmp_realm_times;
8456a424ccSmp153739 char *cname = 0, *sname = 0;
8556a424ccSmp153739 const char *fromstring = 0;
8656a424ccSmp153739 char ktypestr[128];
8756a424ccSmp153739 char rep_etypestr[128];
8856a424ccSmp153739 char fromstringbuf[70];
89*159d09a2SMark Phalan void *pa_context = NULL;
907c478bd9Sstevel@tonic-gate struct in_addr from_in4; /* IPv4 address of sender */
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate ticket_reply.enc_part.ciphertext.data = 0;
937c478bd9Sstevel@tonic-gate e_data.data = 0;
947c478bd9Sstevel@tonic-gate (void) memset(&encrypting_key, 0, sizeof(krb5_keyblock));
9556a424ccSmp153739 reply.padata = 0; /* avoid bogus free in error_out */
967c478bd9Sstevel@tonic-gate (void) memset(&session_key, 0, sizeof(krb5_keyblock));
97*159d09a2SMark Phalan enc_tkt_reply.authorization_data = NULL;
987c478bd9Sstevel@tonic-gate
9956a424ccSmp153739 ktypes2str(ktypestr, sizeof(ktypestr),
10056a424ccSmp153739 request->nktypes, request->ktype);
10156a424ccSmp153739
1027c478bd9Sstevel@tonic-gate (void) memcpy(&from_in4, from->address->contents, /* SUNW */
1037c478bd9Sstevel@tonic-gate sizeof (struct in_addr));
10456a424ccSmp153739
10556a424ccSmp153739 fromstring = inet_ntop(ADDRTYPE2FAMILY (from->address->addrtype),
10656a424ccSmp153739 &from_in4,
10756a424ccSmp153739 fromstringbuf, sizeof(fromstringbuf));
1087c478bd9Sstevel@tonic-gate if (!fromstring)
1097c478bd9Sstevel@tonic-gate fromstring = "<unknown>";
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if (!request->client) {
1127c478bd9Sstevel@tonic-gate status = "NULL_CLIENT";
1137c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1147c478bd9Sstevel@tonic-gate goto errout;
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate if ((errcode = krb5_unparse_name(kdc_context, request->client, &cname))) {
1177c478bd9Sstevel@tonic-gate status = "UNPARSING_CLIENT";
1187c478bd9Sstevel@tonic-gate goto errout;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate limit_string(cname);
1217c478bd9Sstevel@tonic-gate if (!request->server) {
1227c478bd9Sstevel@tonic-gate status = "NULL_SERVER";
1237c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1247c478bd9Sstevel@tonic-gate goto errout;
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate if ((errcode = krb5_unparse_name(kdc_context, request->server, &sname))) {
1277c478bd9Sstevel@tonic-gate status = "UNPARSING_SERVER";
1287c478bd9Sstevel@tonic-gate goto errout;
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate limit_string(sname);
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate c_nprincs = 1;
1337c478bd9Sstevel@tonic-gate if ((errcode = krb5_db_get_principal(kdc_context, request->client,
1347c478bd9Sstevel@tonic-gate &client, &c_nprincs, &more))) {
1357c478bd9Sstevel@tonic-gate status = "LOOKING_UP_CLIENT";
1367c478bd9Sstevel@tonic-gate c_nprincs = 0;
1377c478bd9Sstevel@tonic-gate goto errout;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate if (more) {
1407c478bd9Sstevel@tonic-gate status = "NON-UNIQUE_CLIENT";
1417c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE;
1427c478bd9Sstevel@tonic-gate goto errout;
1437c478bd9Sstevel@tonic-gate } else if (c_nprincs != 1) {
1447c478bd9Sstevel@tonic-gate status = "CLIENT_NOT_FOUND";
1457c478bd9Sstevel@tonic-gate #ifdef KRBCONF_VAGUE_ERRORS
1467c478bd9Sstevel@tonic-gate errcode = KRB5KRB_ERR_GENERIC;
1477c478bd9Sstevel@tonic-gate #else
1487c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1497c478bd9Sstevel@tonic-gate #endif
1507c478bd9Sstevel@tonic-gate goto errout;
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate s_nprincs = 1;
1547c478bd9Sstevel@tonic-gate if ((errcode = krb5_db_get_principal(kdc_context, request->server, &server,
1557c478bd9Sstevel@tonic-gate &s_nprincs, &more))) {
1567c478bd9Sstevel@tonic-gate status = "LOOKING_UP_SERVER";
1577c478bd9Sstevel@tonic-gate goto errout;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate if (more) {
1607c478bd9Sstevel@tonic-gate status = "NON-UNIQUE_SERVER";
1617c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE;
1627c478bd9Sstevel@tonic-gate goto errout;
1637c478bd9Sstevel@tonic-gate } else if (s_nprincs != 1) {
1647c478bd9Sstevel@tonic-gate status = "SERVER_NOT_FOUND";
1657c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1667c478bd9Sstevel@tonic-gate goto errout;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate if ((errcode = krb5_timeofday(kdc_context, &kdc_time))) {
1707c478bd9Sstevel@tonic-gate status = "TIMEOFDAY";
1717c478bd9Sstevel@tonic-gate goto errout;
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate if ((errcode = validate_as_request(request, client, server,
1757c478bd9Sstevel@tonic-gate kdc_time, &status))) {
1767c478bd9Sstevel@tonic-gate if (!status)
1777c478bd9Sstevel@tonic-gate status = "UNKNOWN_REASON";
1787c478bd9Sstevel@tonic-gate errcode += ERROR_TABLE_BASE_krb5;
1797c478bd9Sstevel@tonic-gate goto errout;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate * Select the keytype for the ticket session key.
1847c478bd9Sstevel@tonic-gate */
1857c478bd9Sstevel@tonic-gate if ((useenctype = select_session_keytype(kdc_context, &server,
1867c478bd9Sstevel@tonic-gate request->nktypes,
1877c478bd9Sstevel@tonic-gate request->ktype)) == 0) {
1887c478bd9Sstevel@tonic-gate /* unsupported ktype */
1897c478bd9Sstevel@tonic-gate status = "BAD_ENCRYPTION_TYPE";
1907c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_ETYPE_NOSUPP;
1917c478bd9Sstevel@tonic-gate goto errout;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if ((errcode = krb5_c_make_random_key(kdc_context, useenctype,
1957c478bd9Sstevel@tonic-gate &session_key))) {
1967c478bd9Sstevel@tonic-gate /* random key failed */
1977c478bd9Sstevel@tonic-gate status = "RANDOM_KEY_FAILED";
1987c478bd9Sstevel@tonic-gate goto errout;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate ticket_reply.server = request->server;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate enc_tkt_reply.flags = 0;
2047c478bd9Sstevel@tonic-gate setflag(enc_tkt_reply.flags, TKT_FLG_INITIAL);
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /* It should be noted that local policy may affect the */
2077c478bd9Sstevel@tonic-gate /* processing of any of these flags. For example, some */
2087c478bd9Sstevel@tonic-gate /* realms may refuse to issue renewable tickets */
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate if (isflagset(request->kdc_options, KDC_OPT_FORWARDABLE))
2117c478bd9Sstevel@tonic-gate setflag(enc_tkt_reply.flags, TKT_FLG_FORWARDABLE);
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate if (isflagset(request->kdc_options, KDC_OPT_PROXIABLE))
2147c478bd9Sstevel@tonic-gate setflag(enc_tkt_reply.flags, TKT_FLG_PROXIABLE);
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if (isflagset(request->kdc_options, KDC_OPT_ALLOW_POSTDATE))
2177c478bd9Sstevel@tonic-gate setflag(enc_tkt_reply.flags, TKT_FLG_MAY_POSTDATE);
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate enc_tkt_reply.session = &session_key;
2207c478bd9Sstevel@tonic-gate enc_tkt_reply.client = request->client;
2217c478bd9Sstevel@tonic-gate enc_tkt_reply.transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
2227c478bd9Sstevel@tonic-gate enc_tkt_reply.transited.tr_contents = empty_string; /* equivalent of "" */
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate enc_tkt_reply.times.authtime = kdc_time;
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate if (isflagset(request->kdc_options, KDC_OPT_POSTDATED)) {
2277c478bd9Sstevel@tonic-gate setflag(enc_tkt_reply.flags, TKT_FLG_POSTDATED);
2287c478bd9Sstevel@tonic-gate setflag(enc_tkt_reply.flags, TKT_FLG_INVALID);
2297c478bd9Sstevel@tonic-gate enc_tkt_reply.times.starttime = request->from;
2307c478bd9Sstevel@tonic-gate } else
2317c478bd9Sstevel@tonic-gate enc_tkt_reply.times.starttime = kdc_time;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate until = (request->till == 0) ? kdc_infinity : request->till;
2347c478bd9Sstevel@tonic-gate /* These numbers could easily be large
2357c478bd9Sstevel@tonic-gate * use long long variables to ensure that they don't
2367c478bd9Sstevel@tonic-gate * result in negative values when added.
2377c478bd9Sstevel@tonic-gate */
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate tmp_client_times = (long long) enc_tkt_reply.times.starttime + client.max_life;
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate tmp_server_times = (long long) enc_tkt_reply.times.starttime + server.max_life;
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate tmp_realm_times = (long long) enc_tkt_reply.times.starttime + max_life_for_realm;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate enc_tkt_reply.times.endtime =
2467c478bd9Sstevel@tonic-gate min(until,
2477c478bd9Sstevel@tonic-gate min(tmp_client_times,
2487c478bd9Sstevel@tonic-gate min(tmp_server_times,
2497c478bd9Sstevel@tonic-gate min(tmp_realm_times,KRB5_KDB_EXPIRATION))));
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate if (isflagset(request->kdc_options, KDC_OPT_RENEWABLE_OK) &&
2527c478bd9Sstevel@tonic-gate !isflagset(client.attributes, KRB5_KDB_DISALLOW_RENEWABLE) &&
2537c478bd9Sstevel@tonic-gate (enc_tkt_reply.times.endtime < request->till)) {
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /* we set the RENEWABLE option for later processing */
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate setflag(request->kdc_options, KDC_OPT_RENEWABLE);
2587c478bd9Sstevel@tonic-gate request->rtime = request->till;
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate rtime = (request->rtime == 0) ? kdc_infinity : request->rtime;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate if (isflagset(request->kdc_options, KDC_OPT_RENEWABLE)) {
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate * XXX Should we squelch the output renew_till to be no
2657c478bd9Sstevel@tonic-gate * earlier than the endtime of the ticket?
2667c478bd9Sstevel@tonic-gate */
2677c478bd9Sstevel@tonic-gate setflag(enc_tkt_reply.flags, TKT_FLG_RENEWABLE);
2687c478bd9Sstevel@tonic-gate tmp_client_times = (double) enc_tkt_reply.times.starttime + client.max_renewable_life;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate tmp_server_times = (double) enc_tkt_reply.times.starttime + server.max_renewable_life;
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate tmp_realm_times = (double) enc_tkt_reply.times.starttime + max_renewable_life_for_realm;
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate enc_tkt_reply.times.renew_till =
2757c478bd9Sstevel@tonic-gate min(rtime, min(tmp_client_times,
2767c478bd9Sstevel@tonic-gate min(tmp_server_times,
2777c478bd9Sstevel@tonic-gate min(tmp_realm_times,KRB5_KDB_EXPIRATION))));
2787c478bd9Sstevel@tonic-gate } else
2797c478bd9Sstevel@tonic-gate enc_tkt_reply.times.renew_till = 0; /* XXX */
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /* starttime is optional, and treated as authtime if not present.
2827c478bd9Sstevel@tonic-gate so we can nuke it if it matches */
2837c478bd9Sstevel@tonic-gate if (enc_tkt_reply.times.starttime == enc_tkt_reply.times.authtime)
2847c478bd9Sstevel@tonic-gate enc_tkt_reply.times.starttime = 0;
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate enc_tkt_reply.caddrs = request->addresses;
2877c478bd9Sstevel@tonic-gate enc_tkt_reply.authorization_data = 0;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate * Check the preauthentication if it is there.
2917c478bd9Sstevel@tonic-gate */
2927c478bd9Sstevel@tonic-gate if (request->padata) {
293*159d09a2SMark Phalan errcode = check_padata(kdc_context, &client, req_pkt, request,
294*159d09a2SMark Phalan &enc_tkt_reply, &pa_context, &e_data);
2957c478bd9Sstevel@tonic-gate if (errcode) {
2967c478bd9Sstevel@tonic-gate #ifdef KRBCONF_KDC_MODIFIES_KDB
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate * Note: this doesn't work if you're using slave servers!!!
2997c478bd9Sstevel@tonic-gate * It also causes the database to be modified (and thus
3007c478bd9Sstevel@tonic-gate * need to be locked) frequently.
3017c478bd9Sstevel@tonic-gate */
3027c478bd9Sstevel@tonic-gate if (client.fail_auth_count < KRB5_MAX_FAIL_COUNT) {
3037c478bd9Sstevel@tonic-gate client.fail_auth_count = client.fail_auth_count + 1;
3047c478bd9Sstevel@tonic-gate if (client.fail_auth_count == KRB5_MAX_FAIL_COUNT) {
3057c478bd9Sstevel@tonic-gate client.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate client.last_failed = kdc_time;
3097c478bd9Sstevel@tonic-gate update_client = 1;
3107c478bd9Sstevel@tonic-gate #endif
3117c478bd9Sstevel@tonic-gate status = "PREAUTH_FAILED";
3127c478bd9Sstevel@tonic-gate #ifdef KRBCONF_VAGUE_ERRORS
3137c478bd9Sstevel@tonic-gate errcode = KRB5KRB_ERR_GENERIC;
3147c478bd9Sstevel@tonic-gate #endif
3157c478bd9Sstevel@tonic-gate goto errout;
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate * Final check before handing out ticket: If the client requires
3217c478bd9Sstevel@tonic-gate * preauthentication, verify that the proper kind of
3227c478bd9Sstevel@tonic-gate * preauthentication was carried out.
3237c478bd9Sstevel@tonic-gate */
3247c478bd9Sstevel@tonic-gate status = missing_required_preauth(&client, &server, &enc_tkt_reply);
3257c478bd9Sstevel@tonic-gate if (status) {
3267c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_PREAUTH_REQUIRED;
3277c478bd9Sstevel@tonic-gate get_preauth_hint_list(request, &client, &server, &e_data);
3287c478bd9Sstevel@tonic-gate goto errout;
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate ticket_reply.enc_part2 = &enc_tkt_reply;
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate * Find the server key
3357c478bd9Sstevel@tonic-gate */
3367c478bd9Sstevel@tonic-gate if ((errcode = krb5_dbe_find_enctype(kdc_context, &server,
3377c478bd9Sstevel@tonic-gate -1, /* ignore keytype */
3387c478bd9Sstevel@tonic-gate -1, /* Ignore salttype */
3397c478bd9Sstevel@tonic-gate 0, /* Get highest kvno */
3407c478bd9Sstevel@tonic-gate &server_key))) {
3417c478bd9Sstevel@tonic-gate status = "FINDING_SERVER_KEY";
3427c478bd9Sstevel@tonic-gate goto errout;
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate /* convert server.key into a real key (it may be encrypted
3467c478bd9Sstevel@tonic-gate in the database) */
3477c478bd9Sstevel@tonic-gate if ((errcode = krb5_dbekd_decrypt_key_data(kdc_context, &master_keyblock,
3487c478bd9Sstevel@tonic-gate server_key, &encrypting_key,
3497c478bd9Sstevel@tonic-gate NULL))) {
3507c478bd9Sstevel@tonic-gate status = "DECRYPT_SERVER_KEY";
3517c478bd9Sstevel@tonic-gate goto errout;
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate
3547c478bd9Sstevel@tonic-gate errcode = krb5_encrypt_tkt_part(kdc_context, &encrypting_key, &ticket_reply);
3557c478bd9Sstevel@tonic-gate krb5_free_keyblock_contents(kdc_context, &encrypting_key);
3567c478bd9Sstevel@tonic-gate encrypting_key.contents = 0;
3577c478bd9Sstevel@tonic-gate if (errcode) {
3587c478bd9Sstevel@tonic-gate status = "ENCRYPTING_TICKET";
3597c478bd9Sstevel@tonic-gate goto errout;
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate ticket_reply.enc_part.kvno = server_key->key_data_kvno;
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate * Find the appropriate client key. We search in the order specified
3657c478bd9Sstevel@tonic-gate * by request keytype list.
3667c478bd9Sstevel@tonic-gate */
3677c478bd9Sstevel@tonic-gate client_key = (krb5_key_data *) NULL;
3687c478bd9Sstevel@tonic-gate for (i = 0; i < request->nktypes; i++) {
3697c478bd9Sstevel@tonic-gate useenctype = request->ktype[i];
370505d05c7Sgtb if (!krb5_c_valid_enctype(useenctype))
3717c478bd9Sstevel@tonic-gate continue;
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate if (!krb5_dbe_find_enctype(kdc_context, &client, useenctype, -1,
3747c478bd9Sstevel@tonic-gate 0, &client_key))
3757c478bd9Sstevel@tonic-gate break;
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate if (!(client_key)) {
3787c478bd9Sstevel@tonic-gate /* Cannot find an appropriate key */
3797c478bd9Sstevel@tonic-gate status = "CANT_FIND_CLIENT_KEY";
3807c478bd9Sstevel@tonic-gate errcode = KRB5KDC_ERR_ETYPE_NOSUPP;
3817c478bd9Sstevel@tonic-gate goto errout;
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate /* convert client.key_data into a real key */
3857c478bd9Sstevel@tonic-gate if ((errcode = krb5_dbekd_decrypt_key_data(kdc_context, &master_keyblock,
3867c478bd9Sstevel@tonic-gate client_key, &encrypting_key,
3877c478bd9Sstevel@tonic-gate NULL))) {
3887c478bd9Sstevel@tonic-gate status = "DECRYPT_CLIENT_KEY";
3897c478bd9Sstevel@tonic-gate goto errout;
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate encrypting_key.enctype = useenctype;
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate /* Start assembling the response */
3947c478bd9Sstevel@tonic-gate reply.msg_type = KRB5_AS_REP;
3957c478bd9Sstevel@tonic-gate reply.client = request->client;
3967c478bd9Sstevel@tonic-gate reply.ticket = &ticket_reply;
3977c478bd9Sstevel@tonic-gate reply_encpart.session = &session_key;
3987c478bd9Sstevel@tonic-gate if ((errcode = fetch_last_req_info(&client, &reply_encpart.last_req))) {
3997c478bd9Sstevel@tonic-gate status = "FETCH_LAST_REQ";
4007c478bd9Sstevel@tonic-gate goto errout;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate reply_encpart.nonce = request->nonce;
4033125ebfcSsemery
4043125ebfcSsemery /*
4053125ebfcSsemery * Take the minimum of expiration or pw_expiration if not zero.
4063125ebfcSsemery */
4073125ebfcSsemery if (client.expiration != 0 && client.pw_expiration != 0)
4083125ebfcSsemery etime = min(client.expiration, client.pw_expiration);
4093125ebfcSsemery else
4103125ebfcSsemery etime = client.expiration ? client.expiration : client.pw_expiration;
4113125ebfcSsemery
4123125ebfcSsemery reply_encpart.key_exp = etime;
4137c478bd9Sstevel@tonic-gate reply_encpart.flags = enc_tkt_reply.flags;
4147c478bd9Sstevel@tonic-gate reply_encpart.server = ticket_reply.server;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate /* copy the time fields EXCEPT for authtime; it's location
4177c478bd9Sstevel@tonic-gate is used for ktime */
4187c478bd9Sstevel@tonic-gate reply_encpart.times = enc_tkt_reply.times;
4197c478bd9Sstevel@tonic-gate reply_encpart.times.authtime = authtime = kdc_time;
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate reply_encpart.caddrs = enc_tkt_reply.caddrs;
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /* Fetch the padata info to be returned */
424*159d09a2SMark Phalan errcode = return_padata(kdc_context, &client, req_pkt, request,
425*159d09a2SMark Phalan &reply, client_key, &encrypting_key, &pa_context);
4267c478bd9Sstevel@tonic-gate if (errcode) {
4277c478bd9Sstevel@tonic-gate status = "KDC_RETURN_PADATA";
4287c478bd9Sstevel@tonic-gate goto errout;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate /* now encode/encrypt the response */
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate reply.enc_part.enctype = encrypting_key.enctype;
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate errcode = krb5_encode_kdc_rep(kdc_context, KRB5_AS_REP, &reply_encpart,
4367c478bd9Sstevel@tonic-gate 0, &encrypting_key, &reply, response);
4377c478bd9Sstevel@tonic-gate krb5_free_keyblock_contents(kdc_context, &encrypting_key);
4387c478bd9Sstevel@tonic-gate encrypting_key.contents = 0;
4397c478bd9Sstevel@tonic-gate reply.enc_part.kvno = client_key->key_data_kvno;
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate if (errcode) {
4427c478bd9Sstevel@tonic-gate status = "ENCODE_KDC_REP";
4437c478bd9Sstevel@tonic-gate goto errout;
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate /* these parts are left on as a courtesy from krb5_encode_kdc_rep so we
4477c478bd9Sstevel@tonic-gate can use them in raw form if needed. But, we don't... */
4487c478bd9Sstevel@tonic-gate memset(reply.enc_part.ciphertext.data, 0, reply.enc_part.ciphertext.length);
4497c478bd9Sstevel@tonic-gate free(reply.enc_part.ciphertext.data);
4507c478bd9Sstevel@tonic-gate
45156a424ccSmp153739 /* SUNW14resync:
45256a424ccSmp153739 * The third argument to audit_krb5kdc_as_req() is zero as the local
45356a424ccSmp153739 * portnumber is no longer passed to process_as_req().
45456a424ccSmp153739 */
45556a424ccSmp153739 audit_krb5kdc_as_req(&from_in4, (in_port_t)from->port, 0,
4567c478bd9Sstevel@tonic-gate cname, sname, 0);
45756a424ccSmp153739 rep_etypes2str(rep_etypestr, sizeof(rep_etypestr), &reply);
45856a424ccSmp153739 krb5_klog_syslog(LOG_INFO,
45956a424ccSmp153739 "AS_REQ (%s) %s: ISSUE: authtime %d, "
46056a424ccSmp153739 "%s, %s for %s",
46156a424ccSmp153739 ktypestr,
46256a424ccSmp153739 fromstring, authtime,
46356a424ccSmp153739 rep_etypestr,
46456a424ccSmp153739 cname, sname);
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate #ifdef KRBCONF_KDC_MODIFIES_KDB
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate * If we get this far, we successfully did the AS_REQ.
4697c478bd9Sstevel@tonic-gate */
4707c478bd9Sstevel@tonic-gate client.last_success = kdc_time;
4717c478bd9Sstevel@tonic-gate client.fail_auth_count = 0;
4727c478bd9Sstevel@tonic-gate update_client = 1;
4737c478bd9Sstevel@tonic-gate #endif /* KRBCONF_KDC_MODIFIES_KDB */
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate errout:
476*159d09a2SMark Phalan if (pa_context)
477*159d09a2SMark Phalan free_padata_context(kdc_context, &pa_context);
478*159d09a2SMark Phalan
4797c478bd9Sstevel@tonic-gate if (status) {
480*159d09a2SMark Phalan const char * emsg = 0;
481*159d09a2SMark Phalan if (errcode)
482*159d09a2SMark Phalan emsg = krb5_get_error_message (kdc_context, errcode);
483*159d09a2SMark Phalan
4847c478bd9Sstevel@tonic-gate audit_krb5kdc_as_req(&from_in4, (in_port_t)from->port,
48556a424ccSmp153739 0, cname, sname, errcode);
48656a424ccSmp153739 krb5_klog_syslog(LOG_INFO, "AS_REQ (%s) %s: %s: %s for %s%s%s",
48756a424ccSmp153739 ktypestr,
48856a424ccSmp153739 fromstring, status,
4897c478bd9Sstevel@tonic-gate cname ? cname : "<unknown client>",
4907c478bd9Sstevel@tonic-gate sname ? sname : "<unknown server>",
4917c478bd9Sstevel@tonic-gate errcode ? ", " : "",
492*159d09a2SMark Phalan errcode ? emsg : "");
493*159d09a2SMark Phalan if (errcode)
494*159d09a2SMark Phalan krb5_free_error_message (kdc_context, emsg);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate if (errcode) {
497*159d09a2SMark Phalan int got_err = 0;
498*159d09a2SMark Phalan if (status == 0) {
499*159d09a2SMark Phalan status = krb5_get_error_message (kdc_context, errcode);
500*159d09a2SMark Phalan got_err = 1;
501*159d09a2SMark Phalan }
5027c478bd9Sstevel@tonic-gate errcode -= ERROR_TABLE_BASE_krb5;
5037c478bd9Sstevel@tonic-gate if (errcode < 0 || errcode > 128)
5047c478bd9Sstevel@tonic-gate errcode = KRB_ERR_GENERIC;
5057c478bd9Sstevel@tonic-gate
50656a424ccSmp153739 errcode = prepare_error_as(request, errcode, &e_data, response,
50756a424ccSmp153739 status);
508*159d09a2SMark Phalan if (got_err) {
509*159d09a2SMark Phalan krb5_free_error_message (kdc_context, status);
510*159d09a2SMark Phalan status = 0;
511*159d09a2SMark Phalan }
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate
514*159d09a2SMark Phalan if (enc_tkt_reply.authorization_data != NULL)
515*159d09a2SMark Phalan krb5_free_authdata(kdc_context, enc_tkt_reply.authorization_data);
51656a424ccSmp153739 if (encrypting_key.contents)
5177c478bd9Sstevel@tonic-gate krb5_free_keyblock_contents(kdc_context, &encrypting_key);
5187c478bd9Sstevel@tonic-gate if (reply.padata)
5197c478bd9Sstevel@tonic-gate krb5_free_pa_data(kdc_context, reply.padata);
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate if (cname)
5227c478bd9Sstevel@tonic-gate free(cname);
5237c478bd9Sstevel@tonic-gate if (sname)
5247c478bd9Sstevel@tonic-gate free(sname);
5257c478bd9Sstevel@tonic-gate if (c_nprincs) {
5267c478bd9Sstevel@tonic-gate #ifdef KRBCONF_KDC_MODIFIES_KDB
5277c478bd9Sstevel@tonic-gate if (update_client) {
5287c478bd9Sstevel@tonic-gate krb5_db_put_principal(kdc_context, &client, &c_nprincs);
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate * ptooey. We want krb5_db_sync() or something like that.
5317c478bd9Sstevel@tonic-gate */
5327c478bd9Sstevel@tonic-gate krb5_db_fini(kdc_context);
5337c478bd9Sstevel@tonic-gate if (kdc_active_realm->realm_dbname)
5347c478bd9Sstevel@tonic-gate krb5_db_set_name(kdc_active_realm->realm_context,
5357c478bd9Sstevel@tonic-gate kdc_active_realm->realm_dbname);
5367c478bd9Sstevel@tonic-gate krb5_db_init(kdc_context);
5377c478bd9Sstevel@tonic-gate /* Reset master key */
53856a424ccSmp153739 krb5_db_set_mkey(kdc_context, &kdc_active_realm->realm_mkey);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate #endif /* KRBCONF_KDC_MODIFIES_KDB */
5417c478bd9Sstevel@tonic-gate krb5_db_free_principal(kdc_context, &client, c_nprincs);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate if (s_nprincs)
5447c478bd9Sstevel@tonic-gate krb5_db_free_principal(kdc_context, &server, s_nprincs);
5457c478bd9Sstevel@tonic-gate if (session_key.contents)
5467c478bd9Sstevel@tonic-gate krb5_free_keyblock_contents(kdc_context, &session_key);
5477c478bd9Sstevel@tonic-gate if (ticket_reply.enc_part.ciphertext.data) {
5487c478bd9Sstevel@tonic-gate memset(ticket_reply.enc_part.ciphertext.data , 0,
5497c478bd9Sstevel@tonic-gate ticket_reply.enc_part.ciphertext.length);
5507c478bd9Sstevel@tonic-gate free(ticket_reply.enc_part.ciphertext.data);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate krb5_free_data_contents(kdc_context, &e_data);
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate return errcode;
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@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)55956a424ccSmp153739 prepare_error_as (krb5_kdc_req *request, int error, krb5_data *e_data,
56056a424ccSmp153739 krb5_data **response, const char *status)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate krb5_error errpkt;
5637c478bd9Sstevel@tonic-gate krb5_error_code retval;
5647c478bd9Sstevel@tonic-gate krb5_data *scratch;
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate errpkt.ctime = request->nonce;
5677c478bd9Sstevel@tonic-gate errpkt.cusec = 0;
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate if ((retval = krb5_us_timeofday(kdc_context, &errpkt.stime,
5707c478bd9Sstevel@tonic-gate &errpkt.susec)))
5717c478bd9Sstevel@tonic-gate return(retval);
5727c478bd9Sstevel@tonic-gate errpkt.error = error;
5737c478bd9Sstevel@tonic-gate errpkt.server = request->server;
5747c478bd9Sstevel@tonic-gate errpkt.client = request->client;
57556a424ccSmp153739 errpkt.text.length = strlen(status)+1;
5767c478bd9Sstevel@tonic-gate if (!(errpkt.text.data = malloc(errpkt.text.length)))
5777c478bd9Sstevel@tonic-gate return ENOMEM;
57856a424ccSmp153739 (void) strcpy(errpkt.text.data, status);
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate if (!(scratch = (krb5_data *)malloc(sizeof(*scratch)))) {
5817c478bd9Sstevel@tonic-gate free(errpkt.text.data);
5827c478bd9Sstevel@tonic-gate return ENOMEM;
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate if (e_data && e_data->data) {
5857c478bd9Sstevel@tonic-gate errpkt.e_data = *e_data;
5867c478bd9Sstevel@tonic-gate } else {
5877c478bd9Sstevel@tonic-gate errpkt.e_data.length = 0;
5887c478bd9Sstevel@tonic-gate errpkt.e_data.data = 0;
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate retval = krb5_mk_error(kdc_context, &errpkt, scratch);
5927c478bd9Sstevel@tonic-gate free(errpkt.text.data);
5936cf54e34Sbugbomb if (retval)
5946cf54e34Sbugbomb free(scratch);
5956cf54e34Sbugbomb else
5967c478bd9Sstevel@tonic-gate *response = scratch;
5976cf54e34Sbugbomb
5987c478bd9Sstevel@tonic-gate return retval;
5997c478bd9Sstevel@tonic-gate }
600