10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * kdc/replay.c
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * Copyright 1991 by the Massachusetts Institute of Technology.
50Sstevel@tonic-gate * All Rights Reserved.
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * Export of this software from the United States of America may
80Sstevel@tonic-gate * require a specific license from the United States Government.
90Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
100Sstevel@tonic-gate * export to obtain such a license before exporting.
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
130Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
140Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
150Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
160Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
170Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
180Sstevel@tonic-gate * to distribution of the software without specific, written prior
190Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
200Sstevel@tonic-gate * your software as modified software and not distribute it in such a
210Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
220Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
230Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
240Sstevel@tonic-gate * or implied warranty.
250Sstevel@tonic-gate *
260Sstevel@tonic-gate *
270Sstevel@tonic-gate * Replay lookaside cache for the KDC, to avoid extra work.
280Sstevel@tonic-gate *
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "k5-int.h"
340Sstevel@tonic-gate #include "kdc_util.h"
350Sstevel@tonic-gate #include "extern.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate #ifndef NOCACHE
380Sstevel@tonic-gate
390Sstevel@tonic-gate typedef struct _krb5_kdc_replay_ent {
400Sstevel@tonic-gate struct _krb5_kdc_replay_ent *next;
410Sstevel@tonic-gate int num_hits;
420Sstevel@tonic-gate krb5_int32 timein;
430Sstevel@tonic-gate time_t db_age;
440Sstevel@tonic-gate krb5_data *req_packet;
450Sstevel@tonic-gate krb5_data *reply_packet;
460Sstevel@tonic-gate } krb5_kdc_replay_ent;
470Sstevel@tonic-gate
480Sstevel@tonic-gate static krb5_kdc_replay_ent root_ptr = {0};
490Sstevel@tonic-gate
500Sstevel@tonic-gate static int hits = 0;
510Sstevel@tonic-gate static int calls = 0;
520Sstevel@tonic-gate static int max_hits_per_entry = 0;
530Sstevel@tonic-gate static int num_entries = 0;
540Sstevel@tonic-gate
550Sstevel@tonic-gate #define STALE_TIME 2*60 /* two minutes */
560Sstevel@tonic-gate #define STALE(ptr) ((abs((ptr)->timein - timenow) >= STALE_TIME) || \
570Sstevel@tonic-gate ((ptr)->db_age != db_age))
580Sstevel@tonic-gate
590Sstevel@tonic-gate #define MATCH(ptr) (((ptr)->req_packet->length == inpkt->length) && \
600Sstevel@tonic-gate !memcmp((ptr)->req_packet->data, inpkt->data, \
610Sstevel@tonic-gate inpkt->length) && \
620Sstevel@tonic-gate ((ptr)->db_age == db_age))
630Sstevel@tonic-gate /* XXX
640Sstevel@tonic-gate Todo: quench the size of the queue...
650Sstevel@tonic-gate */
660Sstevel@tonic-gate
670Sstevel@tonic-gate /* return TRUE if outpkt is filled in with a packet to reply with,
680Sstevel@tonic-gate FALSE if the caller should do the work */
690Sstevel@tonic-gate
700Sstevel@tonic-gate krb5_boolean
kdc_check_lookaside(krb5_data * inpkt,krb5_data ** outpkt)71*7934SMark.Phalan@Sun.COM kdc_check_lookaside(krb5_data *inpkt, krb5_data **outpkt)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate krb5_int32 timenow;
740Sstevel@tonic-gate register krb5_kdc_replay_ent *eptr, *last, *hold;
750Sstevel@tonic-gate time_t db_age;
760Sstevel@tonic-gate
770Sstevel@tonic-gate if (krb5_timeofday(kdc_context, &timenow) ||
780Sstevel@tonic-gate krb5_db_get_age(kdc_context, 0, &db_age))
790Sstevel@tonic-gate return FALSE;
800Sstevel@tonic-gate
810Sstevel@tonic-gate calls++;
820Sstevel@tonic-gate
830Sstevel@tonic-gate /* search for a replay entry in the queue, possibly removing
840Sstevel@tonic-gate stale entries while we're here */
850Sstevel@tonic-gate
860Sstevel@tonic-gate if (root_ptr.next) {
870Sstevel@tonic-gate for (last = &root_ptr, eptr = root_ptr.next;
880Sstevel@tonic-gate eptr;
890Sstevel@tonic-gate eptr = eptr->next) {
900Sstevel@tonic-gate if (MATCH(eptr)) {
910Sstevel@tonic-gate eptr->num_hits++;
920Sstevel@tonic-gate hits++;
930Sstevel@tonic-gate
940Sstevel@tonic-gate if (krb5_copy_data(kdc_context, eptr->reply_packet, outpkt))
950Sstevel@tonic-gate return FALSE;
960Sstevel@tonic-gate else
970Sstevel@tonic-gate return TRUE;
980Sstevel@tonic-gate /* return here, don't bother flushing even if it is stale.
990Sstevel@tonic-gate if we just matched, we may get another retransmit... */
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate if (STALE(eptr)) {
1020Sstevel@tonic-gate /* flush it and collect stats */
1030Sstevel@tonic-gate max_hits_per_entry = max(max_hits_per_entry, eptr->num_hits);
1040Sstevel@tonic-gate krb5_free_data(kdc_context, eptr->req_packet);
1050Sstevel@tonic-gate krb5_free_data(kdc_context, eptr->reply_packet);
1060Sstevel@tonic-gate hold = eptr;
1070Sstevel@tonic-gate last->next = eptr->next;
1080Sstevel@tonic-gate eptr = last;
1090Sstevel@tonic-gate free(hold);
1100Sstevel@tonic-gate } else {
1110Sstevel@tonic-gate /* this isn't it, just move along */
1120Sstevel@tonic-gate last = eptr;
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate return FALSE;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /* insert a request & reply into the lookaside queue. assumes it's not
1200Sstevel@tonic-gate already there, and can fail softly due to other weird errors. */
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate void
kdc_insert_lookaside(krb5_data * inpkt,krb5_data * outpkt)123*7934SMark.Phalan@Sun.COM kdc_insert_lookaside(krb5_data *inpkt, krb5_data *outpkt)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate register krb5_kdc_replay_ent *eptr;
1260Sstevel@tonic-gate krb5_int32 timenow;
1270Sstevel@tonic-gate time_t db_age;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate if (krb5_timeofday(kdc_context, &timenow) ||
1300Sstevel@tonic-gate krb5_db_get_age(kdc_context, 0, &db_age))
1310Sstevel@tonic-gate return;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /* this is a new entry */
1340Sstevel@tonic-gate eptr = (krb5_kdc_replay_ent *)calloc(1, sizeof(*eptr));
1350Sstevel@tonic-gate if (!eptr)
1360Sstevel@tonic-gate return;
1370Sstevel@tonic-gate eptr->timein = timenow;
1380Sstevel@tonic-gate eptr->db_age = db_age;
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * This is going to hurt a lot malloc()-wise due to the need to
1410Sstevel@tonic-gate * allocate memory for the krb5_data and krb5_address elements.
1420Sstevel@tonic-gate * ARGH!
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate if (krb5_copy_data(kdc_context, inpkt, &eptr->req_packet)) {
1450Sstevel@tonic-gate free(eptr);
1460Sstevel@tonic-gate return;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate if (krb5_copy_data(kdc_context, outpkt, &eptr->reply_packet)) {
1490Sstevel@tonic-gate krb5_free_data(kdc_context, eptr->req_packet);
1500Sstevel@tonic-gate free(eptr);
1510Sstevel@tonic-gate return;
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate eptr->next = root_ptr.next;
1540Sstevel@tonic-gate root_ptr.next = eptr;
1550Sstevel@tonic-gate num_entries++;
1560Sstevel@tonic-gate return;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1592881Smp153739 /* frees memory associated with the lookaside queue for memory profiling */
1602881Smp153739 void
kdc_free_lookaside(krb5_context kcontext)1612881Smp153739 kdc_free_lookaside(krb5_context kcontext)
1622881Smp153739 {
1632881Smp153739 register krb5_kdc_replay_ent *eptr, *last, *hold;
1642881Smp153739 if (root_ptr.next) {
1652881Smp153739 for (last = &root_ptr, eptr = root_ptr.next;
1662881Smp153739 eptr; eptr = eptr->next) {
1672881Smp153739 krb5_free_data(kcontext, eptr->req_packet);
1682881Smp153739 krb5_free_data(kcontext, eptr->reply_packet);
1692881Smp153739 hold = eptr;
1702881Smp153739 last->next = eptr->next;
1712881Smp153739 eptr = last;
1722881Smp153739 free(hold);
1732881Smp153739 }
1742881Smp153739 }
1752881Smp153739 }
1762881Smp153739
1770Sstevel@tonic-gate #endif /* NOCACHE */
178