/*1* Copyright (c) 2001-2004 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "ktutil_locl.h"3435RCSID("$Id$");3637int38kt_rename(struct rename_options *opt, int argc, char **argv)39{40krb5_error_code ret = 0;41krb5_keytab_entry entry;42krb5_keytab keytab;43krb5_kt_cursor cursor;44krb5_principal from_princ, to_princ;4546ret = krb5_parse_name(context, argv[0], &from_princ);47if(ret != 0) {48krb5_warn(context, ret, "%s", argv[0]);49return 1;50}5152ret = krb5_parse_name(context, argv[1], &to_princ);53if(ret != 0) {54krb5_free_principal(context, from_princ);55krb5_warn(context, ret, "%s", argv[1]);56return 1;57}5859if((keytab = ktutil_open_keytab()) == NULL) {60krb5_free_principal(context, from_princ);61krb5_free_principal(context, to_princ);62return 1;63}6465ret = krb5_kt_start_seq_get(context, keytab, &cursor);66if(ret) {67krb5_kt_close(context, keytab);68krb5_free_principal(context, from_princ);69krb5_free_principal(context, to_princ);70return 1;71}72while(1) {73ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);74if(ret != 0) {75if(ret != KRB5_CC_END && ret != KRB5_KT_END)76krb5_warn(context, ret, "getting entry from keytab");77else78ret = 0;79break;80}81if(krb5_principal_compare(context, entry.principal, from_princ)) {82krb5_free_principal(context, entry.principal);83entry.principal = to_princ;84ret = krb5_kt_add_entry(context, keytab, &entry);85if(ret) {86entry.principal = NULL;87krb5_kt_free_entry(context, &entry);88krb5_warn(context, ret, "adding entry");89break;90}91if (opt->delete_flag) {92entry.principal = from_princ;93ret = krb5_kt_remove_entry(context, keytab, &entry);94if(ret) {95entry.principal = NULL;96krb5_kt_free_entry(context, &entry);97krb5_warn(context, ret, "removing entry");98break;99}100}101entry.principal = NULL;102}103krb5_kt_free_entry(context, &entry);104}105krb5_kt_end_seq_get(context, keytab, &cursor);106107krb5_free_principal(context, from_princ);108krb5_free_principal(context, to_princ);109110return ret != 0;111}112113114115