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 * kdc/dispatch.c
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * Copyright 1990 by the Massachusetts Institute of Technology.
107c478bd9Sstevel@tonic-gate *
117c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may
127c478bd9Sstevel@tonic-gate * require a specific license from the United States Government.
137c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
147c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting.
157c478bd9Sstevel@tonic-gate *
167c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
177c478bd9Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
187c478bd9Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
197c478bd9Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
207c478bd9Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
217c478bd9Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
227c478bd9Sstevel@tonic-gate * to distribution of the software without specific, written prior
237c478bd9Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
247c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a
257c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
267c478bd9Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
277c478bd9Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
287c478bd9Sstevel@tonic-gate * or implied warranty.
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * Dispatch an incoming packet.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include "k5-int.h"
367c478bd9Sstevel@tonic-gate #include <syslog.h>
377c478bd9Sstevel@tonic-gate #include "kdc_util.h"
387c478bd9Sstevel@tonic-gate #include "extern.h"
397c478bd9Sstevel@tonic-gate #include "adm_proto.h"
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate
4456a424ccSmp153739 static krb5_int32 last_usec = 0, last_os_random = 0;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate krb5_error_code
dispatch(krb5_data * pkt,const krb5_fulladdr * from,krb5_data ** response)4756a424ccSmp153739 dispatch(krb5_data *pkt, const krb5_fulladdr *from, krb5_data **response)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate krb5_error_code retval;
517c478bd9Sstevel@tonic-gate krb5_kdc_req *as_req;
5256a424ccSmp153739 krb5_int32 now, now_usec;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /* decode incoming packet, and dispatch */
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate #ifndef NOCACHE
577c478bd9Sstevel@tonic-gate /* try the replay lookaside buffer */
58*159d09a2SMark Phalan if (kdc_check_lookaside(pkt, response)) {
597c478bd9Sstevel@tonic-gate /* a hit! */
607c478bd9Sstevel@tonic-gate const char *name = 0;
617c478bd9Sstevel@tonic-gate char buf[46];
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate name = (char *) inet_ntop (ADDRTYPE2FAMILY (from->address->addrtype),
647c478bd9Sstevel@tonic-gate from->address->contents, buf, sizeof (buf));
657c478bd9Sstevel@tonic-gate if (name == 0)
667c478bd9Sstevel@tonic-gate name = "[unknown address type]";
677c478bd9Sstevel@tonic-gate krb5_klog_syslog(LOG_INFO,
6856a424ccSmp153739 "DISPATCH: repeated (retransmitted?) request from %s, resending previous response",
6956a424ccSmp153739 name);
707c478bd9Sstevel@tonic-gate return 0;
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate #endif
7356a424ccSmp153739 /* SUNW14resync XXX */
7456a424ccSmp153739 #if 0
7556a424ccSmp153739 retval = krb5_crypto_us_timeofday(&now, &now_usec);
7656a424ccSmp153739 if (retval == 0) {
7756a424ccSmp153739 krb5_int32 usec_difference = now_usec-last_usec;
7856a424ccSmp153739 krb5_data data;
7956a424ccSmp153739 if(last_os_random == 0)
8056a424ccSmp153739 last_os_random = now;
8156a424ccSmp153739 /* Grab random data from OS every hour*/
8256a424ccSmp153739 if(now-last_os_random >= 60*60) {
8356a424ccSmp153739 krb5_c_random_os_entropy(kdc_context, 0, NULL);
8456a424ccSmp153739 last_os_random = now;
8556a424ccSmp153739 }
8656a424ccSmp153739
8756a424ccSmp153739 data.length = sizeof(krb5_int32);
8856a424ccSmp153739 data.data = (void *) &usec_difference;
8956a424ccSmp153739
9056a424ccSmp153739 krb5_c_random_add_entropy(kdc_context,
9156a424ccSmp153739 KRB5_C_RANDSOURCE_TIMING, &data);
9256a424ccSmp153739 last_usec = now_usec;
9356a424ccSmp153739 }
9456a424ccSmp153739 #endif
957c478bd9Sstevel@tonic-gate /* try TGS_REQ first; they are more common! */
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate if (krb5_is_tgs_req(pkt)) {
9856a424ccSmp153739 retval = process_tgs_req(pkt, from, response);
997c478bd9Sstevel@tonic-gate } else if (krb5_is_as_req(pkt)) {
1007c478bd9Sstevel@tonic-gate if (!(retval = decode_krb5_as_req(pkt, &as_req))) {
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate * setup_server_realm() sets up the global realm-specific data
1037c478bd9Sstevel@tonic-gate * pointer.
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate if (!(retval = setup_server_realm(as_req->server))) {
106*159d09a2SMark Phalan retval = process_as_req(as_req, pkt, from, response);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate krb5_free_kdc_req(kdc_context, as_req);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate }
11156a424ccSmp153739 #ifdef KRB5_KRB4_COMPAT
11256a424ccSmp153739 else if (pkt->data[0] == 4) /* old version */
11356a424ccSmp153739 retval = process_v4(pkt, from, response);
11456a424ccSmp153739 #endif
1157c478bd9Sstevel@tonic-gate else
1167c478bd9Sstevel@tonic-gate retval = KRB5KRB_AP_ERR_MSG_TYPE;
1177c478bd9Sstevel@tonic-gate #ifndef NOCACHE
1187c478bd9Sstevel@tonic-gate /* put the response into the lookaside buffer */
1197c478bd9Sstevel@tonic-gate if (!retval)
120*159d09a2SMark Phalan kdc_insert_lookaside(pkt, *response);
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate return retval;
1247c478bd9Sstevel@tonic-gate }
125