xref: /freebsd/crypto/heimdal/admin/purge.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "ktutil_locl.h"
35b528cefcSMark Murray 
36*ae771770SStanislav Sedov RCSID("$Id$");
37b528cefcSMark Murray 
38b528cefcSMark Murray /*
39b528cefcSMark Murray  * keep track of the highest version for every principal.
40b528cefcSMark Murray  */
41b528cefcSMark Murray 
42b528cefcSMark Murray struct e {
43b528cefcSMark Murray     krb5_principal principal;
44b528cefcSMark Murray     int max_vno;
45c19800e8SDoug Rabson     time_t timestamp;
46b528cefcSMark Murray     struct e *next;
47b528cefcSMark Murray };
48b528cefcSMark Murray 
49b528cefcSMark Murray static struct e *
get_entry(krb5_principal princ,struct e * head)50b528cefcSMark Murray get_entry (krb5_principal princ, struct e *head)
51b528cefcSMark Murray {
52b528cefcSMark Murray     struct e *e;
53b528cefcSMark Murray 
54b528cefcSMark Murray     for (e = head; e != NULL; e = e->next)
55b528cefcSMark Murray 	if (krb5_principal_compare (context, princ, e->principal))
56b528cefcSMark Murray 	    return e;
57b528cefcSMark Murray     return NULL;
58b528cefcSMark Murray }
59b528cefcSMark Murray 
60b528cefcSMark Murray static void
add_entry(krb5_principal princ,int vno,time_t timestamp,struct e ** head)61c19800e8SDoug Rabson add_entry (krb5_principal princ, int vno, time_t timestamp, struct e **head)
62b528cefcSMark Murray {
63b528cefcSMark Murray     krb5_error_code ret;
64b528cefcSMark Murray     struct e *e;
65b528cefcSMark Murray 
66b528cefcSMark Murray     e = get_entry (princ, *head);
67b528cefcSMark Murray     if (e != NULL) {
68c19800e8SDoug Rabson 	if(e->max_vno < vno) {
69c19800e8SDoug Rabson 	    e->max_vno = vno;
70c19800e8SDoug Rabson 	    e->timestamp = timestamp;
71c19800e8SDoug Rabson 	}
72b528cefcSMark Murray 	return;
73b528cefcSMark Murray     }
74b528cefcSMark Murray     e = malloc (sizeof (*e));
75b528cefcSMark Murray     if (e == NULL)
76b528cefcSMark Murray 	krb5_errx (context, 1, "malloc: out of memory");
77b528cefcSMark Murray     ret = krb5_copy_principal (context, princ, &e->principal);
78b528cefcSMark Murray     if (ret)
79b528cefcSMark Murray 	krb5_err (context, 1, ret, "krb5_copy_principal");
80b528cefcSMark Murray     e->max_vno = vno;
81c19800e8SDoug Rabson     e->timestamp = timestamp;
82b528cefcSMark Murray     e->next    = *head;
83b528cefcSMark Murray     *head      = e;
84b528cefcSMark Murray }
85b528cefcSMark Murray 
86b528cefcSMark Murray static void
delete_list(struct e * head)87b528cefcSMark Murray delete_list (struct e *head)
88b528cefcSMark Murray {
89b528cefcSMark Murray     while (head != NULL) {
90b528cefcSMark Murray 	struct e *next = head->next;
91b528cefcSMark Murray 	krb5_free_principal (context, head->principal);
92b528cefcSMark Murray 	free (head);
93b528cefcSMark Murray 	head = next;
94b528cefcSMark Murray     }
95b528cefcSMark Murray }
96b528cefcSMark Murray 
97b528cefcSMark Murray /*
98b528cefcSMark Murray  * Remove all entries that have newer versions and that are older
99b528cefcSMark Murray  * than `age'
100b528cefcSMark Murray  */
101b528cefcSMark Murray 
102b528cefcSMark Murray int
kt_purge(struct purge_options * opt,int argc,char ** argv)103c19800e8SDoug Rabson kt_purge(struct purge_options *opt, int argc, char **argv)
104b528cefcSMark Murray {
105adb0ddaeSAssar Westerlund     krb5_error_code ret = 0;
106b528cefcSMark Murray     krb5_kt_cursor cursor;
107adb0ddaeSAssar Westerlund     krb5_keytab keytab;
108b528cefcSMark Murray     krb5_keytab_entry entry;
1095e9cd1aeSAssar Westerlund     int age;
110b528cefcSMark Murray     struct e *head = NULL;
111b528cefcSMark Murray     time_t judgement_day;
112b528cefcSMark Murray 
113c19800e8SDoug Rabson     age = parse_time(opt->age_string, "s");
1145e9cd1aeSAssar Westerlund     if(age < 0) {
115c19800e8SDoug Rabson 	krb5_warnx(context, "unparasable time `%s'", opt->age_string);
116adb0ddaeSAssar Westerlund 	return 1;
117adb0ddaeSAssar Westerlund     }
118adb0ddaeSAssar Westerlund 
1194137ff4cSJacques Vidrine     if((keytab = ktutil_open_keytab()) == NULL)
120adb0ddaeSAssar Westerlund 	return 1;
1215e9cd1aeSAssar Westerlund 
122b528cefcSMark Murray     ret = krb5_kt_start_seq_get(context, keytab, &cursor);
123b528cefcSMark Murray     if(ret){
124c19800e8SDoug Rabson 	krb5_warn(context, ret, "%s", keytab_string);
125adb0ddaeSAssar Westerlund 	goto out;
126b528cefcSMark Murray     }
127b528cefcSMark Murray 
128*ae771770SStanislav Sedov     while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0) {
129c19800e8SDoug Rabson 	add_entry (entry.principal, entry.vno, entry.timestamp, &head);
130b528cefcSMark Murray 	krb5_kt_free_entry(context, &entry);
131b528cefcSMark Murray     }
132*ae771770SStanislav Sedov     krb5_kt_end_seq_get(context, keytab, &cursor);
133b528cefcSMark Murray 
134b528cefcSMark Murray     judgement_day = time (NULL);
135b528cefcSMark Murray 
136b528cefcSMark Murray     ret = krb5_kt_start_seq_get(context, keytab, &cursor);
137b528cefcSMark Murray     if(ret){
138c19800e8SDoug Rabson 	krb5_warn(context, ret, "%s", keytab_string);
139adb0ddaeSAssar Westerlund 	goto out;
140b528cefcSMark Murray     }
141b528cefcSMark Murray 
142*ae771770SStanislav Sedov     while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0) {
143b528cefcSMark Murray 	struct e *e = get_entry (entry.principal, head);
144b528cefcSMark Murray 
145b528cefcSMark Murray 	if (e == NULL) {
146b528cefcSMark Murray 	    krb5_warnx (context, "ignoring extra entry");
147b528cefcSMark Murray 	    continue;
148b528cefcSMark Murray 	}
149b528cefcSMark Murray 
150b528cefcSMark Murray 	if (entry.vno < e->max_vno
151c19800e8SDoug Rabson 	    && judgement_day - e->timestamp > age) {
152b528cefcSMark Murray 	    if (verbose_flag) {
153b528cefcSMark Murray 		char *name_str;
154b528cefcSMark Murray 
155b528cefcSMark Murray 		krb5_unparse_name (context, entry.principal, &name_str);
156b528cefcSMark Murray 		printf ("removing %s vno %d\n", name_str, entry.vno);
157b528cefcSMark Murray 		free (name_str);
158b528cefcSMark Murray 	    }
159b528cefcSMark Murray 	    ret = krb5_kt_remove_entry (context, keytab, &entry);
160b528cefcSMark Murray 	    if (ret)
161b528cefcSMark Murray 		krb5_warn (context, ret, "remove");
162b528cefcSMark Murray 	}
163b528cefcSMark Murray 	krb5_kt_free_entry(context, &entry);
164b528cefcSMark Murray     }
165b528cefcSMark Murray     ret = krb5_kt_end_seq_get(context, keytab, &cursor);
166b528cefcSMark Murray 
167b528cefcSMark Murray     delete_list (head);
168b528cefcSMark Murray 
169adb0ddaeSAssar Westerlund  out:
170adb0ddaeSAssar Westerlund     krb5_kt_close (context, keytab);
171adb0ddaeSAssar Westerlund     return ret != 0;
172b528cefcSMark Murray }
173