xref: /freebsd/crypto/heimdal/admin/rename.c (revision 4137ff4cc173ea2e05227027e1c9e0ea42bcc0dc)
14137ff4cSJacques Vidrine /*
24137ff4cSJacques Vidrine  * Copyright (c) 2001 Kungliga Tekniska H�gskolan
34137ff4cSJacques Vidrine  * (Royal Institute of Technology, Stockholm, Sweden).
44137ff4cSJacques Vidrine  * All rights reserved.
54137ff4cSJacques Vidrine  *
64137ff4cSJacques Vidrine  * Redistribution and use in source and binary forms, with or without
74137ff4cSJacques Vidrine  * modification, are permitted provided that the following conditions
84137ff4cSJacques Vidrine  * are met:
94137ff4cSJacques Vidrine  *
104137ff4cSJacques Vidrine  * 1. Redistributions of source code must retain the above copyright
114137ff4cSJacques Vidrine  *    notice, this list of conditions and the following disclaimer.
124137ff4cSJacques Vidrine  *
134137ff4cSJacques Vidrine  * 2. Redistributions in binary form must reproduce the above copyright
144137ff4cSJacques Vidrine  *    notice, this list of conditions and the following disclaimer in the
154137ff4cSJacques Vidrine  *    documentation and/or other materials provided with the distribution.
164137ff4cSJacques Vidrine  *
174137ff4cSJacques Vidrine  * 3. Neither the name of the Institute nor the names of its contributors
184137ff4cSJacques Vidrine  *    may be used to endorse or promote products derived from this software
194137ff4cSJacques Vidrine  *    without specific prior written permission.
204137ff4cSJacques Vidrine  *
214137ff4cSJacques Vidrine  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
224137ff4cSJacques Vidrine  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234137ff4cSJacques Vidrine  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244137ff4cSJacques Vidrine  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
254137ff4cSJacques Vidrine  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264137ff4cSJacques Vidrine  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274137ff4cSJacques Vidrine  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284137ff4cSJacques Vidrine  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294137ff4cSJacques Vidrine  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304137ff4cSJacques Vidrine  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314137ff4cSJacques Vidrine  * SUCH DAMAGE.
324137ff4cSJacques Vidrine  */
334137ff4cSJacques Vidrine 
344137ff4cSJacques Vidrine #include "ktutil_locl.h"
354137ff4cSJacques Vidrine 
364137ff4cSJacques Vidrine RCSID("$Id: rename.c,v 1.1 2001/07/23 10:17:32 joda Exp $");
374137ff4cSJacques Vidrine 
384137ff4cSJacques Vidrine int
394137ff4cSJacques Vidrine kt_rename(int argc, char **argv)
404137ff4cSJacques Vidrine {
414137ff4cSJacques Vidrine     krb5_error_code ret = 0;
424137ff4cSJacques Vidrine     krb5_keytab_entry entry;
434137ff4cSJacques Vidrine     krb5_keytab keytab;
444137ff4cSJacques Vidrine     krb5_kt_cursor cursor;
454137ff4cSJacques Vidrine     krb5_principal from_princ, to_princ;
464137ff4cSJacques Vidrine     int help_flag = 0;
474137ff4cSJacques Vidrine 
484137ff4cSJacques Vidrine     struct getargs args[] = {
494137ff4cSJacques Vidrine 	{ "help", 'h', arg_flag, NULL }
504137ff4cSJacques Vidrine     };
514137ff4cSJacques Vidrine     int num_args = sizeof(args) / sizeof(args[0]);
524137ff4cSJacques Vidrine     int optind = 0;
534137ff4cSJacques Vidrine     int i = 0;
544137ff4cSJacques Vidrine 
554137ff4cSJacques Vidrine     args[i++].value = &help_flag;
564137ff4cSJacques Vidrine     if(getarg(args, num_args, argc, argv, &optind)) {
574137ff4cSJacques Vidrine 	arg_printusage(args, num_args, "ktutil rename", "from to");
584137ff4cSJacques Vidrine 	return 1;
594137ff4cSJacques Vidrine     }
604137ff4cSJacques Vidrine     if(help_flag) {
614137ff4cSJacques Vidrine 	arg_printusage(args, num_args, "ktutil rename", "from to");
624137ff4cSJacques Vidrine 	return 0;
634137ff4cSJacques Vidrine     }
644137ff4cSJacques Vidrine     argv += optind;
654137ff4cSJacques Vidrine     argc -= optind;
664137ff4cSJacques Vidrine     if(argc != 2) {
674137ff4cSJacques Vidrine 	arg_printusage(args, num_args, "ktutil rename", "from to");
684137ff4cSJacques Vidrine 	return 0;
694137ff4cSJacques Vidrine     }
704137ff4cSJacques Vidrine 
714137ff4cSJacques Vidrine     ret = krb5_parse_name(context, argv[0], &from_princ);
724137ff4cSJacques Vidrine     if(ret != 0) {
734137ff4cSJacques Vidrine 	krb5_warn(context, ret, "%s", argv[0]);
744137ff4cSJacques Vidrine 	return 0;
754137ff4cSJacques Vidrine     }
764137ff4cSJacques Vidrine 
774137ff4cSJacques Vidrine     ret = krb5_parse_name(context, argv[1], &to_princ);
784137ff4cSJacques Vidrine     if(ret != 0) {
794137ff4cSJacques Vidrine 	krb5_free_principal(context, from_princ);
804137ff4cSJacques Vidrine 	krb5_warn(context, ret, "%s", argv[1]);
814137ff4cSJacques Vidrine 	return 0;
824137ff4cSJacques Vidrine     }
834137ff4cSJacques Vidrine 
844137ff4cSJacques Vidrine     if((keytab = ktutil_open_keytab()) == NULL) {
854137ff4cSJacques Vidrine 	krb5_free_principal(context, from_princ);
864137ff4cSJacques Vidrine 	krb5_free_principal(context, to_princ);
874137ff4cSJacques Vidrine 	return 1;
884137ff4cSJacques Vidrine     }
894137ff4cSJacques Vidrine 
904137ff4cSJacques Vidrine     ret = krb5_kt_start_seq_get(context, keytab, &cursor);
914137ff4cSJacques Vidrine     if(ret) {
924137ff4cSJacques Vidrine 	krb5_kt_close(context, keytab);
934137ff4cSJacques Vidrine 	krb5_free_principal(context, from_princ);
944137ff4cSJacques Vidrine 	krb5_free_principal(context, to_princ);
954137ff4cSJacques Vidrine 	return 1;
964137ff4cSJacques Vidrine     }
974137ff4cSJacques Vidrine     while(1) {
984137ff4cSJacques Vidrine 	ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);
994137ff4cSJacques Vidrine 	if(ret != 0) {
1004137ff4cSJacques Vidrine 	    if(ret != KRB5_CC_END && ret != KRB5_KT_END)
1014137ff4cSJacques Vidrine 		krb5_warn(context, ret, "getting entry from keytab");
1024137ff4cSJacques Vidrine 	    break;
1034137ff4cSJacques Vidrine 	}
1044137ff4cSJacques Vidrine 	if(krb5_principal_compare(context, entry.principal, from_princ)) {
1054137ff4cSJacques Vidrine 	    krb5_free_principal(context, entry.principal);
1064137ff4cSJacques Vidrine 	    entry.principal = to_princ;
1074137ff4cSJacques Vidrine 	    ret = krb5_kt_add_entry(context, keytab, &entry);
1084137ff4cSJacques Vidrine 	    if(ret) {
1094137ff4cSJacques Vidrine 		entry.principal = NULL;
1104137ff4cSJacques Vidrine 		krb5_kt_free_entry(context, &entry);
1114137ff4cSJacques Vidrine 		krb5_warn(context, ret, "adding entry");
1124137ff4cSJacques Vidrine 		break;
1134137ff4cSJacques Vidrine 	    }
1144137ff4cSJacques Vidrine 	    entry.principal = from_princ;
1154137ff4cSJacques Vidrine 	    ret = krb5_kt_remove_entry(context, keytab, &entry);
1164137ff4cSJacques Vidrine 	    if(ret) {
1174137ff4cSJacques Vidrine 		entry.principal = NULL;
1184137ff4cSJacques Vidrine 		krb5_kt_free_entry(context, &entry);
1194137ff4cSJacques Vidrine 		krb5_warn(context, ret, "removing entry");
1204137ff4cSJacques Vidrine 		break;
1214137ff4cSJacques Vidrine 	    }
1224137ff4cSJacques Vidrine 	    entry.principal = NULL;
1234137ff4cSJacques Vidrine 	}
1244137ff4cSJacques Vidrine 	krb5_kt_free_entry(context, &entry);
1254137ff4cSJacques Vidrine     }
1264137ff4cSJacques Vidrine     krb5_kt_end_seq_get(context, keytab, &cursor);
1274137ff4cSJacques Vidrine 
1284137ff4cSJacques Vidrine     krb5_free_principal(context, from_princ);
1294137ff4cSJacques Vidrine     krb5_free_principal(context, to_princ);
1304137ff4cSJacques Vidrine 
1314137ff4cSJacques Vidrine     return 0;
1324137ff4cSJacques Vidrine }
1334137ff4cSJacques Vidrine 
134