4 * Copyright 1991 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
27 * Replay lookaside cache for the KDC, to avoid extra work.
39 typedef struct _krb5_kdc_replay_ent
{
40 struct _krb5_kdc_replay_ent
*next
;
44 krb5_data
*req_packet
;
45 krb5_data
*reply_packet
;
46 } krb5_kdc_replay_ent
;
48 static krb5_kdc_replay_ent root_ptr
= {0};
52 static int max_hits_per_entry
= 0;
53 static int num_entries
= 0;
55 #define STALE_TIME 2*60 /* two minutes */
56 #define STALE(ptr) ((abs((ptr)->timein - timenow) >= STALE_TIME) || \
57 ((ptr)->db_age != db_age))
59 #define MATCH(ptr) (((ptr)->req_packet->length == inpkt->length) && \
60 !memcmp((ptr)->req_packet->data, inpkt->data, \
62 ((ptr)->db_age == db_age))
64 Todo: quench the size of the queue...
67 /* return TRUE if outpkt is filled in with a packet to reply with,
68 FALSE if the caller should do the work */
71 kdc_check_lookaside(krb5_data
*inpkt
, krb5_data
**outpkt
)
74 register krb5_kdc_replay_ent
*eptr
, *last
, *hold
;
77 if (krb5_timeofday(kdc_context
, &timenow
) ||
78 krb5_db_get_age(kdc_context
, 0, &db_age
))
83 /* search for a replay entry in the queue, possibly removing
84 stale entries while we're here */
87 for (last
= &root_ptr
, eptr
= root_ptr
.next
;
94 if (krb5_copy_data(kdc_context
, eptr
->reply_packet
, outpkt
))
98 /* return here, don't bother flushing even if it is stale.
99 if we just matched, we may get another retransmit... */
102 /* flush it and collect stats */
103 max_hits_per_entry
= max(max_hits_per_entry
, eptr
->num_hits
);
104 krb5_free_data(kdc_context
, eptr
->req_packet
);
105 krb5_free_data(kdc_context
, eptr
->reply_packet
);
107 last
->next
= eptr
->next
;
111 /* this isn't it, just move along */
119 /* insert a request & reply into the lookaside queue. assumes it's not
120 already there, and can fail softly due to other weird errors. */
123 kdc_insert_lookaside(krb5_data
*inpkt
, krb5_data
*outpkt
)
125 register krb5_kdc_replay_ent
*eptr
;
129 if (krb5_timeofday(kdc_context
, &timenow
) ||
130 krb5_db_get_age(kdc_context
, 0, &db_age
))
133 /* this is a new entry */
134 eptr
= (krb5_kdc_replay_ent
*)calloc(1, sizeof(*eptr
));
137 eptr
->timein
= timenow
;
138 eptr
->db_age
= db_age
;
140 * This is going to hurt a lot malloc()-wise due to the need to
141 * allocate memory for the krb5_data and krb5_address elements.
144 if (krb5_copy_data(kdc_context
, inpkt
, &eptr
->req_packet
)) {
148 if (krb5_copy_data(kdc_context
, outpkt
, &eptr
->reply_packet
)) {
149 krb5_free_data(kdc_context
, eptr
->req_packet
);
153 eptr
->next
= root_ptr
.next
;
154 root_ptr
.next
= eptr
;
159 /* frees memory associated with the lookaside queue for memory profiling */
161 kdc_free_lookaside(krb5_context kcontext
)
163 register krb5_kdc_replay_ent
*eptr
, *last
, *hold
;
165 for (last
= &root_ptr
, eptr
= root_ptr
.next
;
166 eptr
; eptr
= eptr
->next
) {
167 krb5_free_data(kcontext
, eptr
->req_packet
);
168 krb5_free_data(kcontext
, eptr
->reply_packet
);
170 last
->next
= eptr
->next
;