No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / admin / purge.c
blobbba6c53c5fdc57928bfc7cd8eb911c5eaab813f8
1 /*
2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "ktutil_locl.h"
36 __RCSID("$Heimdal: purge.c 14261 2004-09-23 14:46:43Z joda $"
37 "$NetBSD$");
40 * keep track of the highest version for every principal.
43 struct e {
44 krb5_principal principal;
45 int max_vno;
46 time_t timestamp;
47 struct e *next;
50 static struct e *
51 get_entry (krb5_principal princ, struct e *head)
53 struct e *e;
55 for (e = head; e != NULL; e = e->next)
56 if (krb5_principal_compare (context, princ, e->principal))
57 return e;
58 return NULL;
61 static void
62 add_entry (krb5_principal princ, int vno, time_t timestamp, struct e **head)
64 krb5_error_code ret;
65 struct e *e;
67 e = get_entry (princ, *head);
68 if (e != NULL) {
69 if(e->max_vno < vno) {
70 e->max_vno = vno;
71 e->timestamp = timestamp;
73 return;
75 e = malloc (sizeof (*e));
76 if (e == NULL)
77 krb5_errx (context, 1, "malloc: out of memory");
78 ret = krb5_copy_principal (context, princ, &e->principal);
79 if (ret)
80 krb5_err (context, 1, ret, "krb5_copy_principal");
81 e->max_vno = vno;
82 e->timestamp = timestamp;
83 e->next = *head;
84 *head = e;
87 static void
88 delete_list (struct e *head)
90 while (head != NULL) {
91 struct e *next = head->next;
92 krb5_free_principal (context, head->principal);
93 free (head);
94 head = next;
99 * Remove all entries that have newer versions and that are older
100 * than `age'
104 kt_purge(struct purge_options *opt, int argc, char **argv)
106 krb5_error_code ret = 0;
107 krb5_kt_cursor cursor;
108 krb5_keytab keytab;
109 krb5_keytab_entry entry;
110 int age;
111 struct e *head = NULL;
112 time_t judgement_day;
114 age = parse_time(opt->age_string, "s");
115 if(age < 0) {
116 krb5_warnx(context, "unparasable time `%s'", opt->age_string);
117 return 1;
120 if((keytab = ktutil_open_keytab()) == NULL)
121 return 1;
123 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
124 if(ret){
125 krb5_warn(context, ret, "%s", keytab_string);
126 goto out;
129 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
130 add_entry (entry.principal, entry.vno, entry.timestamp, &head);
131 krb5_kt_free_entry(context, &entry);
133 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
135 judgement_day = time (NULL);
137 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
138 if(ret){
139 krb5_warn(context, ret, "%s", keytab_string);
140 goto out;
143 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
144 struct e *e = get_entry (entry.principal, head);
146 if (e == NULL) {
147 krb5_warnx (context, "ignoring extra entry");
148 continue;
151 if (entry.vno < e->max_vno
152 && judgement_day - e->timestamp > age) {
153 if (verbose_flag) {
154 char *name_str;
156 krb5_unparse_name (context, entry.principal, &name_str);
157 printf ("removing %s vno %d\n", name_str, entry.vno);
158 free (name_str);
160 ret = krb5_kt_remove_entry (context, keytab, &entry);
161 if (ret)
162 krb5_warn (context, ret, "remove");
164 krb5_kt_free_entry(context, &entry);
166 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
168 delete_list (head);
170 out:
171 krb5_kt_close (context, keytab);
172 return ret != 0;