1 /* 2 * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 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. 16 * 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. 20 * 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. 32 */ 33 34 #include "ktutil_locl.h" 35 36 RCSID("$Id: purge.c,v 1.1 2000/01/02 05:06:50 assar Exp $"); 37 38 /* 39 * keep track of the highest version for every principal. 40 */ 41 42 struct e { 43 krb5_principal principal; 44 int max_vno; 45 struct e *next; 46 }; 47 48 static struct e * 49 get_entry (krb5_principal princ, struct e *head) 50 { 51 struct e *e; 52 53 for (e = head; e != NULL; e = e->next) 54 if (krb5_principal_compare (context, princ, e->principal)) 55 return e; 56 return NULL; 57 } 58 59 static void 60 add_entry (krb5_principal princ, int vno, struct e **head) 61 { 62 krb5_error_code ret; 63 struct e *e; 64 65 e = get_entry (princ, *head); 66 if (e != NULL) { 67 e->max_vno = max (e->max_vno, vno); 68 return; 69 } 70 e = malloc (sizeof (*e)); 71 if (e == NULL) 72 krb5_errx (context, 1, "malloc: out of memory"); 73 ret = krb5_copy_principal (context, princ, &e->principal); 74 if (ret) 75 krb5_err (context, 1, ret, "krb5_copy_principal"); 76 e->max_vno = vno; 77 e->next = *head; 78 *head = e; 79 } 80 81 static void 82 delete_list (struct e *head) 83 { 84 while (head != NULL) { 85 struct e *next = head->next; 86 krb5_free_principal (context, head->principal); 87 free (head); 88 head = next; 89 } 90 } 91 92 /* 93 * Remove all entries that have newer versions and that are older 94 * than `age' 95 */ 96 97 int 98 kt_purge(int argc, char **argv) 99 { 100 krb5_error_code ret; 101 krb5_kt_cursor cursor; 102 krb5_keytab_entry entry; 103 int help_flag = 0; 104 int age = 7 * 24 * 60 * 60; 105 struct getargs args[] = { 106 { "age", 0, arg_integer, NULL, "age to retire" }, 107 { "help", 'h', arg_flag, NULL } 108 }; 109 int num_args = sizeof(args) / sizeof(args[0]); 110 int optind = 0; 111 int i = 0; 112 struct e *head = NULL; 113 time_t judgement_day; 114 115 args[i++].value = &age; 116 args[i++].value = &help_flag; 117 118 if(getarg(args, num_args, argc, argv, &optind)) { 119 arg_printusage(args, num_args, "ktutil remove", ""); 120 return 0; 121 } 122 if(help_flag) { 123 arg_printusage(args, num_args, "ktutil remove", ""); 124 return 0; 125 } 126 127 ret = krb5_kt_start_seq_get(context, keytab, &cursor); 128 if(ret){ 129 krb5_warn(context, ret, "krb5_kt_start_seq_get"); 130 return 1; 131 } 132 133 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) { 134 add_entry (entry.principal, entry.vno, &head); 135 krb5_kt_free_entry(context, &entry); 136 } 137 ret = krb5_kt_end_seq_get(context, keytab, &cursor); 138 139 judgement_day = time (NULL); 140 141 ret = krb5_kt_start_seq_get(context, keytab, &cursor); 142 if(ret){ 143 krb5_warn(context, ret, "krb5_kt_start_seq_get"); 144 return 1; 145 } 146 147 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) { 148 struct e *e = get_entry (entry.principal, head); 149 150 if (e == NULL) { 151 krb5_warnx (context, "ignoring extra entry"); 152 continue; 153 } 154 155 if (entry.vno < e->max_vno 156 && judgement_day - entry.timestamp > age) { 157 if (verbose_flag) { 158 char *name_str; 159 160 krb5_unparse_name (context, entry.principal, &name_str); 161 printf ("removing %s vno %d\n", name_str, entry.vno); 162 free (name_str); 163 } 164 ret = krb5_kt_remove_entry (context, keytab, &entry); 165 if (ret) 166 krb5_warn (context, ret, "remove"); 167 } 168 krb5_kt_free_entry(context, &entry); 169 } 170 ret = krb5_kt_end_seq_get(context, keytab, &cursor); 171 172 delete_list (head); 173 174 return 0; 175 } 176