1b528cefcSMark Murray /* 2adb0ddaeSAssar Westerlund * Copyright (c) 1997 - 2001 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 36adb0ddaeSAssar Westerlund RCSID("$Id: remove.c,v 1.2 2001/05/10 15:44:58 assar Exp $"); 37b528cefcSMark Murray 38b528cefcSMark Murray int 39b528cefcSMark Murray kt_remove(int argc, char **argv) 40b528cefcSMark Murray { 41adb0ddaeSAssar Westerlund krb5_error_code ret = 0; 42b528cefcSMark Murray krb5_keytab_entry entry; 43adb0ddaeSAssar Westerlund krb5_keytab keytab; 44b528cefcSMark Murray char *principal_string = NULL; 45b528cefcSMark Murray krb5_principal principal = NULL; 46b528cefcSMark Murray int kvno = 0; 47b528cefcSMark Murray char *keytype_string = NULL; 48b528cefcSMark Murray krb5_enctype enctype = 0; 49b528cefcSMark Murray int help_flag = 0; 50b528cefcSMark Murray struct getargs args[] = { 51b528cefcSMark Murray { "principal", 'p', arg_string, NULL, "principal to remove" }, 52b528cefcSMark Murray { "kvno", 'V', arg_integer, NULL, "key version to remove" }, 53b528cefcSMark Murray { "enctype", 'e', arg_string, NULL, "enctype to remove" }, 54b528cefcSMark Murray { "help", 'h', arg_flag, NULL } 55b528cefcSMark Murray }; 56b528cefcSMark Murray int num_args = sizeof(args) / sizeof(args[0]); 57b528cefcSMark Murray int optind = 0; 58b528cefcSMark Murray int i = 0; 59b528cefcSMark Murray args[i++].value = &principal_string; 60b528cefcSMark Murray args[i++].value = &kvno; 61b528cefcSMark Murray args[i++].value = &keytype_string; 62b528cefcSMark Murray args[i++].value = &help_flag; 63b528cefcSMark Murray if(getarg(args, num_args, argc, argv, &optind)) { 64b528cefcSMark Murray arg_printusage(args, num_args, "ktutil remove", ""); 65adb0ddaeSAssar Westerlund return 1; 66b528cefcSMark Murray } 67b528cefcSMark Murray if(help_flag) { 68b528cefcSMark Murray arg_printusage(args, num_args, "ktutil remove", ""); 69b528cefcSMark Murray return 0; 70b528cefcSMark Murray } 71b528cefcSMark Murray if(principal_string) { 72b528cefcSMark Murray ret = krb5_parse_name(context, principal_string, &principal); 73b528cefcSMark Murray if(ret) { 74b528cefcSMark Murray krb5_warn(context, ret, "%s", principal_string); 75adb0ddaeSAssar Westerlund return 1; 76b528cefcSMark Murray } 77b528cefcSMark Murray } 78b528cefcSMark Murray if(keytype_string) { 79b528cefcSMark Murray ret = krb5_string_to_enctype(context, keytype_string, &enctype); 80b528cefcSMark Murray if(ret) { 81b528cefcSMark Murray int t; 82b528cefcSMark Murray if(sscanf(keytype_string, "%d", &t) == 1) 83b528cefcSMark Murray enctype = t; 84b528cefcSMark Murray else { 85b528cefcSMark Murray krb5_warn(context, ret, "%s", keytype_string); 86b528cefcSMark Murray if(principal) 87b528cefcSMark Murray krb5_free_principal(context, principal); 88adb0ddaeSAssar Westerlund return 1; 89b528cefcSMark Murray } 90b528cefcSMark Murray } 91b528cefcSMark Murray } 92b528cefcSMark Murray if (!principal && !enctype && !kvno) { 93b528cefcSMark Murray krb5_warnx(context, 94b528cefcSMark Murray "You must give at least one of " 95b528cefcSMark Murray "principal, enctype or kvno."); 96adb0ddaeSAssar Westerlund return 1; 97b528cefcSMark Murray } 98adb0ddaeSAssar Westerlund 99adb0ddaeSAssar Westerlund if (keytab_string == NULL) { 100adb0ddaeSAssar Westerlund ret = krb5_kt_default_modify_name (context, keytab_buf, 101adb0ddaeSAssar Westerlund sizeof(keytab_buf)); 102adb0ddaeSAssar Westerlund if (ret) { 103adb0ddaeSAssar Westerlund krb5_warn(context, ret, "krb5_kt_default_modify_name"); 104adb0ddaeSAssar Westerlund return 1; 105adb0ddaeSAssar Westerlund } 106adb0ddaeSAssar Westerlund keytab_string = keytab_buf; 107adb0ddaeSAssar Westerlund } 108adb0ddaeSAssar Westerlund ret = krb5_kt_resolve(context, keytab_string, &keytab); 109adb0ddaeSAssar Westerlund if (ret) { 110adb0ddaeSAssar Westerlund krb5_warn(context, ret, "resolving keytab %s", keytab_string); 111adb0ddaeSAssar Westerlund return 1; 112adb0ddaeSAssar Westerlund } 113adb0ddaeSAssar Westerlund 114adb0ddaeSAssar Westerlund if (verbose_flag) 115adb0ddaeSAssar Westerlund fprintf (stderr, "Using keytab %s\n", keytab_string); 116adb0ddaeSAssar Westerlund 117b528cefcSMark Murray entry.principal = principal; 118b528cefcSMark Murray entry.keyblock.keytype = enctype; 119b528cefcSMark Murray entry.vno = kvno; 120b528cefcSMark Murray ret = krb5_kt_remove_entry(context, keytab, &entry); 121adb0ddaeSAssar Westerlund krb5_kt_close(context, keytab); 122b528cefcSMark Murray if(ret) 123b528cefcSMark Murray krb5_warn(context, ret, "remove"); 124b528cefcSMark Murray if(principal) 125b528cefcSMark Murray krb5_free_principal(context, principal); 126b528cefcSMark Murray return 0; 127b528cefcSMark Murray } 128b528cefcSMark Murray 129