2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "ktutil_locl.h"
36 RCSID("$Id: purge.c,v 1.6 2001/07/23 09:46:41 joda Exp $");
39 * keep track of the highest version for every principal.
43 krb5_principal principal;
49 get_entry (krb5_principal princ, struct e *head)
53 for (e = head; e != NULL; e = e->next)
54 if (krb5_principal_compare (context, princ, e->principal))
60 add_entry (krb5_principal princ, int vno, struct e **head)
65 e = get_entry (princ, *head);
67 e->max_vno = max (e->max_vno, vno);
70 e = malloc (sizeof (*e));
72 krb5_errx (context, 1, "malloc: out of memory");
73 ret = krb5_copy_principal (context, princ, &e->principal);
75 krb5_err (context, 1, ret, "krb5_copy_principal");
82 delete_list (struct e *head)
84 while (head != NULL) {
85 struct e *next = head->next;
86 krb5_free_principal (context, head->principal);
93 * Remove all entries that have newer versions and that are older
98 kt_purge(int argc, char **argv)
100 krb5_error_code ret = 0;
101 krb5_kt_cursor cursor;
103 krb5_keytab_entry entry;
105 char *age_str = "1 week";
107 struct getargs args[] = {
108 { "age", 0, arg_string, NULL, "age to retire" },
109 { "help", 'h', arg_flag, NULL }
111 int num_args = sizeof(args) / sizeof(args[0]);
114 struct e *head = NULL;
115 time_t judgement_day;
117 args[i++].value = &age_str;
118 args[i++].value = &help_flag;
120 if(getarg(args, num_args, argc, argv, &optind)) {
121 arg_printusage(args, num_args, "ktutil purge", "");
125 arg_printusage(args, num_args, "ktutil purge", "");
129 age = parse_time(age_str, "s");
131 krb5_warnx(context, "unparasable time `%s'", age_str);
135 if((keytab = ktutil_open_keytab()) == NULL)
138 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
140 krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_string);
144 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
145 add_entry (entry.principal, entry.vno, &head);
146 krb5_kt_free_entry(context, &entry);
148 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
150 judgement_day = time (NULL);
152 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
154 krb5_warn(context, ret, "krb5_kt_start_seq_get, %s", keytab_string);
158 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
159 struct e *e = get_entry (entry.principal, head);
162 krb5_warnx (context, "ignoring extra entry");
166 if (entry.vno < e->max_vno
167 && judgement_day - entry.timestamp > age) {
171 krb5_unparse_name (context, entry.principal, &name_str);
172 printf ("removing %s vno %d\n", name_str, entry.vno);
175 ret = krb5_kt_remove_entry (context, keytab, &entry);
177 krb5_warn (context, ret, "remove");
179 krb5_kt_free_entry(context, &entry);
181 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
186 krb5_kt_close (context, keytab);