1*1c9681d1Schristos /*	$NetBSD: remove.c,v 1.2 2017/01/28 21:31:44 christos Exp $	*/
2f59d82ffSelric 
3f59d82ffSelric /*
4f59d82ffSelric  * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
5f59d82ffSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6f59d82ffSelric  * All rights reserved.
7f59d82ffSelric  *
8f59d82ffSelric  * Redistribution and use in source and binary forms, with or without
9f59d82ffSelric  * modification, are permitted provided that the following conditions
10f59d82ffSelric  * are met:
11f59d82ffSelric  *
12f59d82ffSelric  * 1. Redistributions of source code must retain the above copyright
13f59d82ffSelric  *    notice, this list of conditions and the following disclaimer.
14f59d82ffSelric  *
15f59d82ffSelric  * 2. Redistributions in binary form must reproduce the above copyright
16f59d82ffSelric  *    notice, this list of conditions and the following disclaimer in the
17f59d82ffSelric  *    documentation and/or other materials provided with the distribution.
18f59d82ffSelric  *
19f59d82ffSelric  * 3. Neither the name of the Institute nor the names of its contributors
20f59d82ffSelric  *    may be used to endorse or promote products derived from this software
21f59d82ffSelric  *    without specific prior written permission.
22f59d82ffSelric  *
23f59d82ffSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24f59d82ffSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f59d82ffSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f59d82ffSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27f59d82ffSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f59d82ffSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f59d82ffSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f59d82ffSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f59d82ffSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f59d82ffSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f59d82ffSelric  * SUCH DAMAGE.
34f59d82ffSelric  */
35f59d82ffSelric 
36f59d82ffSelric #include "ktutil_locl.h"
37f59d82ffSelric 
38*1c9681d1Schristos __RCSID("$NetBSD: remove.c,v 1.2 2017/01/28 21:31:44 christos Exp $");
39f59d82ffSelric 
40f59d82ffSelric int
kt_remove(struct remove_options * opt,int argc,char ** argv)41f59d82ffSelric kt_remove(struct remove_options *opt, int argc, char **argv)
42f59d82ffSelric {
43f59d82ffSelric     krb5_error_code ret = 0;
44f59d82ffSelric     krb5_keytab_entry entry;
45f59d82ffSelric     krb5_keytab keytab;
46f59d82ffSelric     krb5_principal principal = NULL;
47f59d82ffSelric     krb5_enctype enctype = 0;
48f59d82ffSelric 
49f59d82ffSelric     if(opt->principal_string) {
50f59d82ffSelric 	ret = krb5_parse_name(context, opt->principal_string, &principal);
51f59d82ffSelric 	if(ret) {
52f59d82ffSelric 	    krb5_warn(context, ret, "%s", opt->principal_string);
53f59d82ffSelric 	    return 1;
54f59d82ffSelric 	}
55f59d82ffSelric     }
56f59d82ffSelric     if(opt->enctype_string) {
57f59d82ffSelric 	ret = krb5_string_to_enctype(context, opt->enctype_string, &enctype);
58f59d82ffSelric 	if(ret) {
59f59d82ffSelric 	    int t;
60f59d82ffSelric 	    if(sscanf(opt->enctype_string, "%d", &t) == 1)
61f59d82ffSelric 		enctype = t;
62f59d82ffSelric 	    else {
63f59d82ffSelric 		krb5_warn(context, ret, "%s", opt->enctype_string);
64f59d82ffSelric 		if(principal)
65f59d82ffSelric 		    krb5_free_principal(context, principal);
66f59d82ffSelric 		return 1;
67f59d82ffSelric 	    }
68f59d82ffSelric 	}
69f59d82ffSelric     }
70f59d82ffSelric     if (!principal && !enctype && !opt->kvno_integer) {
71f59d82ffSelric 	krb5_warnx(context,
72f59d82ffSelric 		   "You must give at least one of "
73f59d82ffSelric 		   "principal, enctype or kvno.");
74f59d82ffSelric 	ret = EINVAL;
75f59d82ffSelric 	goto out;
76f59d82ffSelric     }
77f59d82ffSelric 
78f59d82ffSelric     if((keytab = ktutil_open_keytab()) == NULL) {
79f59d82ffSelric 	ret = 1;
80f59d82ffSelric 	goto out;
81f59d82ffSelric     }
82f59d82ffSelric 
83f59d82ffSelric     entry.principal = principal;
84f59d82ffSelric     entry.keyblock.keytype = enctype;
85f59d82ffSelric     entry.vno = opt->kvno_integer;
86f59d82ffSelric     ret = krb5_kt_remove_entry(context, keytab, &entry);
87f59d82ffSelric     krb5_kt_close(context, keytab);
88f59d82ffSelric     if(ret)
89f59d82ffSelric 	krb5_warn(context, ret, "remove");
90f59d82ffSelric  out:
91f59d82ffSelric     if(principal)
92f59d82ffSelric 	krb5_free_principal(context, principal);
93f59d82ffSelric     return ret != 0;
94f59d82ffSelric }
95f59d82ffSelric 
96