1*1c9681d1Schristos /*	$NetBSD: del_enctype.c,v 1.2 2017/01/28 21:31:44 christos Exp $	*/
2f59d82ffSelric 
3f59d82ffSelric /*
4f59d82ffSelric  * Copyright (c) 1999-2006 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 "kadmin_locl.h"
37f59d82ffSelric #include "kadmin-commands.h"
38f59d82ffSelric 
39f59d82ffSelric /*
40f59d82ffSelric  * del_enctype principal enctypes...
41f59d82ffSelric  */
42f59d82ffSelric 
43f59d82ffSelric int
del_enctype(void * opt,int argc,char ** argv)44f59d82ffSelric del_enctype(void *opt, int argc, char **argv)
45f59d82ffSelric {
46f59d82ffSelric     kadm5_principal_ent_rec princ;
47f59d82ffSelric     krb5_principal princ_ent = NULL;
48f59d82ffSelric     krb5_error_code ret;
49f59d82ffSelric     const char *princ_name;
50f59d82ffSelric     int i, j, k;
51f59d82ffSelric     krb5_key_data *new_key_data;
52f59d82ffSelric     int n_etypes;
53f59d82ffSelric     krb5_enctype *etypes;
54e0895134Schristos     krb5_key_data *key;
55f59d82ffSelric 
56f59d82ffSelric     memset (&princ, 0, sizeof(princ));
57f59d82ffSelric     princ_name = argv[0];
58f59d82ffSelric     n_etypes   = argc - 1;
59f59d82ffSelric     etypes     = malloc (n_etypes * sizeof(*etypes));
60f59d82ffSelric     if (etypes == NULL) {
61f59d82ffSelric 	krb5_warnx (context, "out of memory");
62f59d82ffSelric 	return 0;
63f59d82ffSelric     }
64f59d82ffSelric     argv++;
65f59d82ffSelric     for (i = 0; i < n_etypes; ++i) {
66f59d82ffSelric 	ret = krb5_string_to_enctype (context, argv[i], &etypes[i]);
67f59d82ffSelric 	if (ret) {
68f59d82ffSelric 	    krb5_warnx (context, "bad enctype \"%s\"", argv[i]);
69f59d82ffSelric 	    goto out2;
70f59d82ffSelric 	}
71f59d82ffSelric     }
72f59d82ffSelric 
73f59d82ffSelric     ret = krb5_parse_name(context, princ_name, &princ_ent);
74f59d82ffSelric     if (ret) {
75f59d82ffSelric 	krb5_warn (context, ret, "krb5_parse_name %s", princ_name);
76f59d82ffSelric 	goto out2;
77f59d82ffSelric     }
78f59d82ffSelric 
79f59d82ffSelric     ret = kadm5_get_principal(kadm_handle, princ_ent, &princ,
80f59d82ffSelric 			      KADM5_PRINCIPAL | KADM5_KEY_DATA);
81f59d82ffSelric     if (ret) {
82f59d82ffSelric 	krb5_free_principal (context, princ_ent);
83f59d82ffSelric 	krb5_warnx (context, "no such principal: %s", princ_name);
84f59d82ffSelric 	goto out2;
85f59d82ffSelric     }
86f59d82ffSelric 
87e0895134Schristos     if (kadm5_all_keys_are_bogus(princ.n_key_data, princ.key_data)) {
88e0895134Schristos 	krb5_warnx(context, "user lacks get-keys privilege");
89e0895134Schristos 	goto out;
90e0895134Schristos     }
91e0895134Schristos 
92f59d82ffSelric     new_key_data   = malloc(princ.n_key_data * sizeof(*new_key_data));
93f59d82ffSelric     if (new_key_data == NULL && princ.n_key_data != 0) {
94f59d82ffSelric 	krb5_warnx (context, "out of memory");
95f59d82ffSelric 	goto out;
96f59d82ffSelric     }
97f59d82ffSelric 
98f59d82ffSelric     for (i = 0, j = 0; i < princ.n_key_data; ++i) {
99f59d82ffSelric 	int docopy = 1;
100e0895134Schristos 	key = &princ.key_data[i];
101f59d82ffSelric 
102e0895134Schristos 	for (k = 0; k < n_etypes; ++k) {
103f59d82ffSelric 	    if (etypes[k] == key->key_data_type[0]) {
104f59d82ffSelric 		docopy = 0;
105f59d82ffSelric 		break;
106f59d82ffSelric 	    }
107e0895134Schristos 	}
108f59d82ffSelric 	if (docopy) {
109f59d82ffSelric 	    new_key_data[j++] = *key;
110f59d82ffSelric 	} else {
111f59d82ffSelric 	    int16_t ignore = 1;
112f59d82ffSelric 
113f59d82ffSelric 	    kadm5_free_key_data (kadm_handle, &ignore, key);
114f59d82ffSelric 	}
115f59d82ffSelric     }
116f59d82ffSelric 
117f59d82ffSelric     free (princ.key_data);
118e0895134Schristos     if (j == 0) {
119e0895134Schristos 	free(new_key_data);
120e0895134Schristos 	new_key_data = NULL;
121e0895134Schristos     }
122f59d82ffSelric     princ.n_key_data = j;
123f59d82ffSelric     princ.key_data   = new_key_data;
124f59d82ffSelric 
125f59d82ffSelric     ret = kadm5_modify_principal (kadm_handle, &princ, KADM5_KEY_DATA);
126f59d82ffSelric     if (ret)
127f59d82ffSelric 	krb5_warn(context, ret, "kadm5_modify_principal");
128f59d82ffSelric out:
129f59d82ffSelric     krb5_free_principal (context, princ_ent);
130f59d82ffSelric     kadm5_free_principal_ent(kadm_handle, &princ);
131f59d82ffSelric out2:
132f59d82ffSelric     free (etypes);
133f59d82ffSelric     return ret != 0;
134f59d82ffSelric }
135