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