1 /* $NetBSD: add_enctype.c,v 1.2 2017/01/28 21:31:44 christos Exp $ */
2
3 /*
4 * Copyright (c) 1999-2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include "kadmin_locl.h"
37 #include "kadmin-commands.h"
38
39 /*
40 * del_enctype principal enctypes...
41 */
42
43 int
add_enctype(struct add_enctype_options * opt,int argc,char ** argv)44 add_enctype(struct add_enctype_options*opt, int argc, char **argv)
45 {
46 kadm5_principal_ent_rec princ;
47 krb5_principal princ_ent = NULL;
48 krb5_error_code ret;
49 const char *princ_name;
50 int i, j;
51 krb5_key_data *new_key_data;
52 int n_etypes;
53 krb5_enctype *etypes;
54
55 if (!opt->random_key_flag) {
56 krb5_warnx (context, "only random key is supported now");
57 return 0;
58 }
59
60 memset(&princ, 0, sizeof(princ));
61 princ_name = argv[0];
62 n_etypes = argc - 1;
63 etypes = malloc (n_etypes * sizeof(*etypes));
64 if (etypes == NULL) {
65 krb5_warnx (context, "out of memory");
66 return 0;
67 }
68 argv++;
69 for (i = 0; i < n_etypes; ++i) {
70 ret = krb5_string_to_enctype(context, argv[i], &etypes[i]);
71 if (ret) {
72 krb5_warnx (context, "bad enctype \"%s\"", argv[i]);
73 goto out2;
74 }
75 }
76
77 ret = krb5_parse_name(context, princ_name, &princ_ent);
78 if (ret) {
79 krb5_warn(context, ret, "krb5_parse_name %s", princ_name);
80 goto out2;
81 }
82
83 /* The principal might have zero keys, but it will still have a kvno! */
84 ret = kadm5_get_principal(kadm_handle, princ_ent, &princ,
85 KADM5_KVNO | KADM5_PRINCIPAL | KADM5_KEY_DATA);
86 if (ret) {
87 krb5_free_principal(context, princ_ent);
88 krb5_warnx(context, "no such principal: %s", princ_name);
89 goto out2;
90 }
91
92 /* Check that we got key data */
93 if (kadm5_all_keys_are_bogus(princ.n_key_data, princ.key_data)) {
94 krb5_warnx(context, "user lacks get-keys privilege");
95 goto out;
96 }
97
98 new_key_data = calloc(princ.n_key_data + n_etypes,
99 sizeof(*new_key_data));
100 if (new_key_data == NULL) {
101 krb5_warnx (context, "out of memory");
102 goto out;
103 }
104
105 for (i = 0; i < princ.n_key_data; ++i) {
106 krb5_key_data *key = &princ.key_data[i];
107
108 for (j = 0; j < n_etypes; ++j) {
109 if (etypes[j] == key->key_data_type[0]) {
110 /* XXX Should this be an error? The admin can del_enctype... */
111 krb5_warnx(context, "enctype %d already exists",
112 (int)etypes[j]);
113 free(new_key_data);
114 goto out;
115 }
116 }
117 new_key_data[i] = *key;
118 }
119
120 for (i = 0; i < n_etypes; ++i) {
121 int n = princ.n_key_data + i;
122 krb5_keyblock keyblock;
123
124 memset(&new_key_data[n], 0, sizeof(new_key_data[n]));
125 new_key_data[n].key_data_ver = 2;
126 new_key_data[n].key_data_kvno = princ.kvno;
127
128 ret = krb5_generate_random_keyblock (context, etypes[i], &keyblock);
129 if (ret) {
130 krb5_warnx(context, "genernate enctype %d failed", (int)etypes[i]);
131 while (--i >= 0)
132 free(new_key_data[--n].key_data_contents[0]);
133 goto out;
134 }
135
136 /* key */
137 new_key_data[n].key_data_type[0] = etypes[i];
138 new_key_data[n].key_data_contents[0] = malloc(keyblock.keyvalue.length);
139 if (new_key_data[n].key_data_contents[0] == NULL) {
140 ret = ENOMEM;
141 krb5_warn(context, ret, "out of memory");
142 while (--i >= 0)
143 free(new_key_data[--n].key_data_contents[0]);
144 goto out;
145 }
146 new_key_data[n].key_data_length[0] = keyblock.keyvalue.length;
147 memcpy(new_key_data[n].key_data_contents[0],
148 keyblock.keyvalue.data,
149 keyblock.keyvalue.length);
150 krb5_free_keyblock_contents(context, &keyblock);
151
152 /* salt */
153 new_key_data[n].key_data_type[1] = KRB5_PW_SALT;
154 new_key_data[n].key_data_length[1] = 0;
155 new_key_data[n].key_data_contents[1] = NULL;
156
157 }
158
159 free (princ.key_data);
160 princ.n_key_data += n_etypes;
161 princ.key_data = new_key_data;
162 new_key_data = NULL;
163
164 ret = kadm5_modify_principal (kadm_handle, &princ, KADM5_KEY_DATA);
165 if (ret)
166 krb5_warn(context, ret, "kadm5_modify_principal");
167 out:
168 krb5_free_principal (context, princ_ent);
169 kadm5_free_principal_ent(kadm_handle, &princ);
170 out2:
171 free (etypes);
172 return ret != 0;
173 }
174