xref: /titanic_53/usr/src/cmd/krb5/krb5kdc/dispatch.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * kdc/dispatch.c
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
12*7c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
13*7c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
14*7c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
15*7c478bd9Sstevel@tonic-gate  *
16*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
17*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
18*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
19*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
20*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
21*7c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
22*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
23*7c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
24*7c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
25*7c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
26*7c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
27*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
28*7c478bd9Sstevel@tonic-gate  * or implied warranty.
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  * Dispatch an incoming packet.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define NEED_SOCKETS
37*7c478bd9Sstevel@tonic-gate #include "k5-int.h"
38*7c478bd9Sstevel@tonic-gate #include <syslog.h>
39*7c478bd9Sstevel@tonic-gate #include "kdc_util.h"
40*7c478bd9Sstevel@tonic-gate #include "extern.h"
41*7c478bd9Sstevel@tonic-gate #include "adm_proto.h"
42*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
43*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
44*7c478bd9Sstevel@tonic-gate #include <string.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate extern krb5_error_code setup_server_realm(krb5_principal);
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate krb5_error_code
49*7c478bd9Sstevel@tonic-gate dispatch(krb5_data *pkt, const krb5_fulladdr *from, int portnum,
50*7c478bd9Sstevel@tonic-gate 	krb5_data **response)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate     krb5_error_code retval;
54*7c478bd9Sstevel@tonic-gate     krb5_kdc_req *as_req;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate     /* decode incoming packet, and dispatch */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #ifndef NOCACHE
59*7c478bd9Sstevel@tonic-gate     /* try the replay lookaside buffer */
60*7c478bd9Sstevel@tonic-gate     if (kdc_check_lookaside(pkt, from, response)) {
61*7c478bd9Sstevel@tonic-gate 	/* a hit! */
62*7c478bd9Sstevel@tonic-gate 	const char *name = 0;
63*7c478bd9Sstevel@tonic-gate 	char buf[46];
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	name = (char *) inet_ntop (ADDRTYPE2FAMILY (from->address->addrtype),
66*7c478bd9Sstevel@tonic-gate 			  from->address->contents, buf, sizeof (buf));
67*7c478bd9Sstevel@tonic-gate 	if (name == 0)
68*7c478bd9Sstevel@tonic-gate 	    name = "[unknown address type]";
69*7c478bd9Sstevel@tonic-gate 	krb5_klog_syslog(LOG_INFO,
70*7c478bd9Sstevel@tonic-gate 			 "DISPATCH: repeated (retransmitted?) request from %s port %d, resending previous response",
71*7c478bd9Sstevel@tonic-gate 			 name, portnum);
72*7c478bd9Sstevel@tonic-gate 	return 0;
73*7c478bd9Sstevel@tonic-gate     }
74*7c478bd9Sstevel@tonic-gate #endif
75*7c478bd9Sstevel@tonic-gate     /* try TGS_REQ first; they are more common! */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate     if (krb5_is_tgs_req(pkt)) {
78*7c478bd9Sstevel@tonic-gate 	retval = process_tgs_req(pkt, from, portnum, response);
79*7c478bd9Sstevel@tonic-gate     } else if (krb5_is_as_req(pkt)) {
80*7c478bd9Sstevel@tonic-gate 	if (!(retval = decode_krb5_as_req(pkt, &as_req))) {
81*7c478bd9Sstevel@tonic-gate 	    /*
82*7c478bd9Sstevel@tonic-gate 	     * setup_server_realm() sets up the global realm-specific data
83*7c478bd9Sstevel@tonic-gate 	     * pointer.
84*7c478bd9Sstevel@tonic-gate 	     */
85*7c478bd9Sstevel@tonic-gate 	    if (!(retval = setup_server_realm(as_req->server))) {
86*7c478bd9Sstevel@tonic-gate 		retval = process_as_req(as_req, from, portnum, response);
87*7c478bd9Sstevel@tonic-gate 	    }
88*7c478bd9Sstevel@tonic-gate 	    krb5_free_kdc_req(kdc_context, as_req);
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate     }
91*7c478bd9Sstevel@tonic-gate     else
92*7c478bd9Sstevel@tonic-gate 	retval = KRB5KRB_AP_ERR_MSG_TYPE;
93*7c478bd9Sstevel@tonic-gate #ifndef NOCACHE
94*7c478bd9Sstevel@tonic-gate     /* put the response into the lookaside buffer */
95*7c478bd9Sstevel@tonic-gate     if (!retval)
96*7c478bd9Sstevel@tonic-gate 	kdc_insert_lookaside(pkt, from, *response);
97*7c478bd9Sstevel@tonic-gate #endif
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate     return retval;
100*7c478bd9Sstevel@tonic-gate }
101