xref: /freebsd/crypto/krb5/src/kdc/dispatch.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* kdc/dispatch.c - Dispatch an incoming packet */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright 1990, 2009 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  *
6*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
7*7f2fe78bSCy Schubert  *   require a specific license from the United States Government.
8*7f2fe78bSCy Schubert  *   It is the responsibility of any person or organization contemplating
9*7f2fe78bSCy Schubert  *   export to obtain such a license before exporting.
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
13*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
14*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
15*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
16*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
17*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
18*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
19*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
20*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
21*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
22*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
23*7f2fe78bSCy Schubert  * or implied warranty.
24*7f2fe78bSCy Schubert  */
25*7f2fe78bSCy Schubert 
26*7f2fe78bSCy Schubert #include "k5-int.h"
27*7f2fe78bSCy Schubert #include <syslog.h>
28*7f2fe78bSCy Schubert #include "kdc_util.h"
29*7f2fe78bSCy Schubert #include "extern.h"
30*7f2fe78bSCy Schubert #include "adm_proto.h"
31*7f2fe78bSCy Schubert #include "realm_data.h"
32*7f2fe78bSCy Schubert #include <netinet/in.h>
33*7f2fe78bSCy Schubert #include <arpa/inet.h>
34*7f2fe78bSCy Schubert #include <string.h>
35*7f2fe78bSCy Schubert 
36*7f2fe78bSCy Schubert static krb5_error_code make_too_big_error(kdc_realm_t *realm, krb5_data **out);
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert struct dispatch_state {
39*7f2fe78bSCy Schubert     loop_respond_fn respond;
40*7f2fe78bSCy Schubert     void *arg;
41*7f2fe78bSCy Schubert     krb5_data *request;
42*7f2fe78bSCy Schubert     int is_tcp;
43*7f2fe78bSCy Schubert     kdc_realm_t *active_realm;
44*7f2fe78bSCy Schubert     krb5_context kdc_err_context;
45*7f2fe78bSCy Schubert };
46*7f2fe78bSCy Schubert 
47*7f2fe78bSCy Schubert static void
finish_dispatch(struct dispatch_state * state,krb5_error_code code,krb5_data * response)48*7f2fe78bSCy Schubert finish_dispatch(struct dispatch_state *state, krb5_error_code code,
49*7f2fe78bSCy Schubert                 krb5_data *response)
50*7f2fe78bSCy Schubert {
51*7f2fe78bSCy Schubert     loop_respond_fn oldrespond = state->respond;
52*7f2fe78bSCy Schubert     void *oldarg = state->arg;
53*7f2fe78bSCy Schubert 
54*7f2fe78bSCy Schubert     if (state->is_tcp == 0 && response &&
55*7f2fe78bSCy Schubert         response->length > (unsigned int)max_dgram_reply_size) {
56*7f2fe78bSCy Schubert         krb5_free_data(NULL, response);
57*7f2fe78bSCy Schubert         response = NULL;
58*7f2fe78bSCy Schubert         code = make_too_big_error(state->active_realm, &response);
59*7f2fe78bSCy Schubert         if (code)
60*7f2fe78bSCy Schubert             krb5_klog_syslog(LOG_ERR, "error constructing "
61*7f2fe78bSCy Schubert                              "KRB_ERR_RESPONSE_TOO_BIG error: %s",
62*7f2fe78bSCy Schubert                              error_message(code));
63*7f2fe78bSCy Schubert     }
64*7f2fe78bSCy Schubert 
65*7f2fe78bSCy Schubert     free(state);
66*7f2fe78bSCy Schubert     (*oldrespond)(oldarg, code, response);
67*7f2fe78bSCy Schubert }
68*7f2fe78bSCy Schubert 
69*7f2fe78bSCy Schubert static void
finish_dispatch_cache(void * arg,krb5_error_code code,krb5_data * response)70*7f2fe78bSCy Schubert finish_dispatch_cache(void *arg, krb5_error_code code, krb5_data *response)
71*7f2fe78bSCy Schubert {
72*7f2fe78bSCy Schubert     struct dispatch_state *state = arg;
73*7f2fe78bSCy Schubert     krb5_context kdc_err_context = state->kdc_err_context;
74*7f2fe78bSCy Schubert 
75*7f2fe78bSCy Schubert #ifndef NOCACHE
76*7f2fe78bSCy Schubert     /* Remove the null cache entry unless we actually want to discard this
77*7f2fe78bSCy Schubert      * request. */
78*7f2fe78bSCy Schubert     if (code != KRB5KDC_ERR_DISCARD)
79*7f2fe78bSCy Schubert         kdc_remove_lookaside(kdc_err_context, state->request);
80*7f2fe78bSCy Schubert 
81*7f2fe78bSCy Schubert     /* Put the response into the lookaside buffer (if we produced one). */
82*7f2fe78bSCy Schubert     if (code == 0 && response != NULL)
83*7f2fe78bSCy Schubert         kdc_insert_lookaside(kdc_err_context, state->request, response);
84*7f2fe78bSCy Schubert #endif
85*7f2fe78bSCy Schubert 
86*7f2fe78bSCy Schubert     finish_dispatch(state, code, response);
87*7f2fe78bSCy Schubert }
88*7f2fe78bSCy Schubert 
89*7f2fe78bSCy Schubert void
dispatch(void * cb,const krb5_fulladdr * local_addr,const krb5_fulladdr * remote_addr,krb5_data * pkt,int is_tcp,verto_ctx * vctx,loop_respond_fn respond,void * arg)90*7f2fe78bSCy Schubert dispatch(void *cb, const krb5_fulladdr *local_addr,
91*7f2fe78bSCy Schubert          const krb5_fulladdr *remote_addr, krb5_data *pkt, int is_tcp,
92*7f2fe78bSCy Schubert          verto_ctx *vctx, loop_respond_fn respond, void *arg)
93*7f2fe78bSCy Schubert {
94*7f2fe78bSCy Schubert     krb5_error_code retval;
95*7f2fe78bSCy Schubert     krb5_kdc_req *req = NULL;
96*7f2fe78bSCy Schubert     krb5_data *response = NULL;
97*7f2fe78bSCy Schubert     struct dispatch_state *state;
98*7f2fe78bSCy Schubert     struct server_handle *handle = cb;
99*7f2fe78bSCy Schubert     krb5_context kdc_err_context = handle->kdc_err_context;
100*7f2fe78bSCy Schubert 
101*7f2fe78bSCy Schubert     state = k5alloc(sizeof(*state), &retval);
102*7f2fe78bSCy Schubert     if (state == NULL) {
103*7f2fe78bSCy Schubert         (*respond)(arg, retval, NULL);
104*7f2fe78bSCy Schubert         return;
105*7f2fe78bSCy Schubert     }
106*7f2fe78bSCy Schubert     state->respond = respond;
107*7f2fe78bSCy Schubert     state->arg = arg;
108*7f2fe78bSCy Schubert     state->request = pkt;
109*7f2fe78bSCy Schubert     state->is_tcp = is_tcp;
110*7f2fe78bSCy Schubert     state->kdc_err_context = kdc_err_context;
111*7f2fe78bSCy Schubert 
112*7f2fe78bSCy Schubert     /* decode incoming packet, and dispatch */
113*7f2fe78bSCy Schubert 
114*7f2fe78bSCy Schubert #ifndef NOCACHE
115*7f2fe78bSCy Schubert     /* try the replay lookaside buffer */
116*7f2fe78bSCy Schubert     if (kdc_check_lookaside(kdc_err_context, pkt, &response)) {
117*7f2fe78bSCy Schubert         /* a hit! */
118*7f2fe78bSCy Schubert         const char *name = 0;
119*7f2fe78bSCy Schubert         char buf[46];
120*7f2fe78bSCy Schubert 
121*7f2fe78bSCy Schubert         name = inet_ntop(ADDRTYPE2FAMILY(remote_addr->address->addrtype),
122*7f2fe78bSCy Schubert                          remote_addr->address->contents, buf, sizeof(buf));
123*7f2fe78bSCy Schubert         if (name == 0)
124*7f2fe78bSCy Schubert             name = "[unknown address type]";
125*7f2fe78bSCy Schubert         if (response)
126*7f2fe78bSCy Schubert             krb5_klog_syslog(LOG_INFO,
127*7f2fe78bSCy Schubert                              "DISPATCH: repeated (retransmitted?) request "
128*7f2fe78bSCy Schubert                              "from %s, resending previous response", name);
129*7f2fe78bSCy Schubert         else
130*7f2fe78bSCy Schubert             krb5_klog_syslog(LOG_INFO,
131*7f2fe78bSCy Schubert                              "DISPATCH: repeated (retransmitted?) request "
132*7f2fe78bSCy Schubert                              "from %s during request processing, dropping "
133*7f2fe78bSCy Schubert                              "repeated request", name);
134*7f2fe78bSCy Schubert 
135*7f2fe78bSCy Schubert         finish_dispatch(state, response ? 0 : KRB5KDC_ERR_DISCARD, response);
136*7f2fe78bSCy Schubert         return;
137*7f2fe78bSCy Schubert     }
138*7f2fe78bSCy Schubert 
139*7f2fe78bSCy Schubert     /* Insert a NULL entry into the lookaside to indicate that this request
140*7f2fe78bSCy Schubert      * is currently being processed. */
141*7f2fe78bSCy Schubert     kdc_insert_lookaside(kdc_err_context, pkt, NULL);
142*7f2fe78bSCy Schubert #endif
143*7f2fe78bSCy Schubert 
144*7f2fe78bSCy Schubert     /* try TGS_REQ first; they are more common! */
145*7f2fe78bSCy Schubert 
146*7f2fe78bSCy Schubert     if (krb5_is_tgs_req(pkt))
147*7f2fe78bSCy Schubert         retval = decode_krb5_tgs_req(pkt, &req);
148*7f2fe78bSCy Schubert     else if (krb5_is_as_req(pkt))
149*7f2fe78bSCy Schubert         retval = decode_krb5_as_req(pkt, &req);
150*7f2fe78bSCy Schubert     else
151*7f2fe78bSCy Schubert         retval = KRB5KRB_AP_ERR_MSG_TYPE;
152*7f2fe78bSCy Schubert     if (retval)
153*7f2fe78bSCy Schubert         goto done;
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     state->active_realm = setup_server_realm(handle, req->server);
156*7f2fe78bSCy Schubert     if (state->active_realm == NULL) {
157*7f2fe78bSCy Schubert         retval = KRB5KDC_ERR_WRONG_REALM;
158*7f2fe78bSCy Schubert         goto done;
159*7f2fe78bSCy Schubert     }
160*7f2fe78bSCy Schubert 
161*7f2fe78bSCy Schubert     if (krb5_is_tgs_req(pkt)) {
162*7f2fe78bSCy Schubert         /* process_tgs_req frees the request */
163*7f2fe78bSCy Schubert         retval = process_tgs_req(req, pkt, remote_addr, state->active_realm,
164*7f2fe78bSCy Schubert                                  &response);
165*7f2fe78bSCy Schubert         req = NULL;
166*7f2fe78bSCy Schubert     } else if (krb5_is_as_req(pkt)) {
167*7f2fe78bSCy Schubert         /* process_as_req frees the request and calls finish_dispatch_cache. */
168*7f2fe78bSCy Schubert         process_as_req(req, pkt, local_addr, remote_addr, state->active_realm,
169*7f2fe78bSCy Schubert                        vctx, finish_dispatch_cache, state);
170*7f2fe78bSCy Schubert         return;
171*7f2fe78bSCy Schubert     }
172*7f2fe78bSCy Schubert 
173*7f2fe78bSCy Schubert done:
174*7f2fe78bSCy Schubert     krb5_free_kdc_req(kdc_err_context, req);
175*7f2fe78bSCy Schubert     finish_dispatch_cache(state, retval, response);
176*7f2fe78bSCy Schubert }
177*7f2fe78bSCy Schubert 
178*7f2fe78bSCy Schubert static krb5_error_code
make_too_big_error(kdc_realm_t * realm,krb5_data ** out)179*7f2fe78bSCy Schubert make_too_big_error(kdc_realm_t *realm, krb5_data **out)
180*7f2fe78bSCy Schubert {
181*7f2fe78bSCy Schubert     krb5_context context = realm->realm_context;
182*7f2fe78bSCy Schubert     krb5_error errpkt;
183*7f2fe78bSCy Schubert     krb5_error_code retval;
184*7f2fe78bSCy Schubert     krb5_data *scratch;
185*7f2fe78bSCy Schubert 
186*7f2fe78bSCy Schubert     *out = NULL;
187*7f2fe78bSCy Schubert     memset(&errpkt, 0, sizeof(errpkt));
188*7f2fe78bSCy Schubert 
189*7f2fe78bSCy Schubert     retval = krb5_us_timeofday(context, &errpkt.stime, &errpkt.susec);
190*7f2fe78bSCy Schubert     if (retval)
191*7f2fe78bSCy Schubert         return retval;
192*7f2fe78bSCy Schubert     errpkt.error = KRB_ERR_RESPONSE_TOO_BIG;
193*7f2fe78bSCy Schubert     errpkt.server = realm->realm_tgsprinc;
194*7f2fe78bSCy Schubert     errpkt.client = NULL;
195*7f2fe78bSCy Schubert     errpkt.text.length = 0;
196*7f2fe78bSCy Schubert     errpkt.text.data = 0;
197*7f2fe78bSCy Schubert     errpkt.e_data.length = 0;
198*7f2fe78bSCy Schubert     errpkt.e_data.data = 0;
199*7f2fe78bSCy Schubert     scratch = malloc(sizeof(*scratch));
200*7f2fe78bSCy Schubert     if (scratch == NULL)
201*7f2fe78bSCy Schubert         return ENOMEM;
202*7f2fe78bSCy Schubert     retval = krb5_mk_error(context, &errpkt, scratch);
203*7f2fe78bSCy Schubert     if (retval) {
204*7f2fe78bSCy Schubert         free(scratch);
205*7f2fe78bSCy Schubert         return retval;
206*7f2fe78bSCy Schubert     }
207*7f2fe78bSCy Schubert 
208*7f2fe78bSCy Schubert     *out = scratch;
209*7f2fe78bSCy Schubert     return 0;
210*7f2fe78bSCy Schubert }
211*7f2fe78bSCy Schubert 
get_context(void * handle)212*7f2fe78bSCy Schubert krb5_context get_context(void *handle)
213*7f2fe78bSCy Schubert {
214*7f2fe78bSCy Schubert     struct server_handle *sh = handle;
215*7f2fe78bSCy Schubert 
216*7f2fe78bSCy Schubert     return sh->kdc_err_context;
217*7f2fe78bSCy Schubert }
218