xref: /netbsd-src/crypto/external/bsd/heimdal/dist/kadmin/mod.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: mod.c,v 1.2 2017/01/28 21:31:44 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 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 static void
40 add_tl(kadm5_principal_ent_rec *princ, int type, krb5_data *data)
41 {
42     krb5_tl_data *tl, **ptl;
43 
44     tl = ecalloc(1, sizeof(*tl));
45     tl->tl_data_next = NULL;
46     tl->tl_data_type = type;
47     tl->tl_data_length = data->length;
48     tl->tl_data_contents = data->data;
49 
50     princ->n_tl_data++;
51     ptl = &princ->tl_data;
52     while (*ptl != NULL)
53 	ptl = &(*ptl)->tl_data_next;
54     *ptl = tl;
55 
56     return;
57 }
58 
59 static void
60 add_constrained_delegation(krb5_context contextp,
61 			   kadm5_principal_ent_rec *princ,
62 			   struct getarg_strings *strings)
63 {
64     krb5_error_code ret;
65     HDB_extension ext;
66     krb5_data buf;
67     size_t size = 0;
68 
69     memset(&ext, 0, sizeof(ext));
70     ext.mandatory = FALSE;
71     ext.data.element = choice_HDB_extension_data_allowed_to_delegate_to;
72 
73     if (strings->num_strings == 1 && strings->strings[0][0] == '\0') {
74 	ext.data.u.allowed_to_delegate_to.val = NULL;
75 	ext.data.u.allowed_to_delegate_to.len = 0;
76     } else {
77 	krb5_principal p;
78 	int i;
79 
80 	ext.data.u.allowed_to_delegate_to.val =
81 	    calloc(strings->num_strings,
82 		   sizeof(ext.data.u.allowed_to_delegate_to.val[0]));
83 	ext.data.u.allowed_to_delegate_to.len = strings->num_strings;
84 
85 	for (i = 0; i < strings->num_strings; i++) {
86 	    ret = krb5_parse_name(contextp, strings->strings[i], &p);
87 	    if (ret)
88 		abort();
89 	    ret = copy_Principal(p, &ext.data.u.allowed_to_delegate_to.val[i]);
90 	    if (ret)
91 		abort();
92 	    krb5_free_principal(contextp, p);
93 	}
94     }
95 
96     ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
97 		       &ext, &size, ret);
98     free_HDB_extension(&ext);
99     if (ret)
100 	abort();
101     if (buf.length != size)
102 	abort();
103 
104     add_tl(princ, KRB5_TL_EXTENSION, &buf);
105 }
106 
107 static void
108 add_aliases(krb5_context contextp, kadm5_principal_ent_rec *princ,
109 	    struct getarg_strings *strings)
110 {
111     krb5_error_code ret;
112     HDB_extension ext;
113     krb5_data buf;
114     krb5_principal p;
115     size_t size = 0;
116     int i;
117 
118     memset(&ext, 0, sizeof(ext));
119     ext.mandatory = FALSE;
120     ext.data.element = choice_HDB_extension_data_aliases;
121     ext.data.u.aliases.case_insensitive = 0;
122 
123     if (strings->num_strings == 1 && strings->strings[0][0] == '\0') {
124 	ext.data.u.aliases.aliases.val = NULL;
125 	ext.data.u.aliases.aliases.len = 0;
126     } else {
127 	ext.data.u.aliases.aliases.val =
128 	    calloc(strings->num_strings,
129 		   sizeof(ext.data.u.aliases.aliases.val[0]));
130 	ext.data.u.aliases.aliases.len = strings->num_strings;
131 
132 	for (i = 0; i < strings->num_strings; i++) {
133 	    ret = krb5_parse_name(contextp, strings->strings[i], &p);
134 	    ret = copy_Principal(p, &ext.data.u.aliases.aliases.val[i]);
135 	    krb5_free_principal(contextp, p);
136 	}
137     }
138 
139     ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
140 		       &ext, &size, ret);
141     free_HDB_extension(&ext);
142     if (ret)
143 	abort();
144     if (buf.length != size)
145 	abort();
146 
147     add_tl(princ, KRB5_TL_EXTENSION, &buf);
148 }
149 
150 static void
151 add_pkinit_acl(krb5_context contextp, kadm5_principal_ent_rec *princ,
152 	       struct getarg_strings *strings)
153 {
154     krb5_error_code ret;
155     HDB_extension ext;
156     krb5_data buf;
157     size_t size = 0;
158     int i;
159 
160     memset(&ext, 0, sizeof(ext));
161     ext.mandatory = FALSE;
162     ext.data.element = choice_HDB_extension_data_pkinit_acl;
163     ext.data.u.aliases.case_insensitive = 0;
164 
165     if (strings->num_strings == 1 && strings->strings[0][0] == '\0') {
166 	ext.data.u.pkinit_acl.val = NULL;
167 	ext.data.u.pkinit_acl.len = 0;
168     } else {
169 	ext.data.u.pkinit_acl.val =
170 	    calloc(strings->num_strings,
171 		   sizeof(ext.data.u.pkinit_acl.val[0]));
172 	ext.data.u.pkinit_acl.len = strings->num_strings;
173 
174 	for (i = 0; i < strings->num_strings; i++) {
175 	    ext.data.u.pkinit_acl.val[i].subject = estrdup(strings->strings[i]);
176 	}
177     }
178 
179     ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
180 		       &ext, &size, ret);
181     free_HDB_extension(&ext);
182     if (ret)
183 	abort();
184     if (buf.length != size)
185 	abort();
186 
187     add_tl(princ, KRB5_TL_EXTENSION, &buf);
188 }
189 
190 static void
191 add_kvno_diff(krb5_context contextp, kadm5_principal_ent_rec *princ,
192 	      int is_svc_diff, krb5_kvno kvno_diff)
193 {
194     krb5_error_code ret;
195     HDB_extension ext;
196     krb5_data buf;
197     size_t size = 0;
198 
199     if (kvno_diff < 0)
200 	return;
201     if (kvno_diff > 2048)
202 	kvno_diff = 2048;
203 
204     if (is_svc_diff) {
205 	ext.data.element = choice_HDB_extension_data_hist_kvno_diff_svc;
206 	ext.data.u.hist_kvno_diff_svc = (unsigned int)kvno_diff;
207     } else {
208 	ext.data.element = choice_HDB_extension_data_hist_kvno_diff_clnt;
209 	ext.data.u.hist_kvno_diff_clnt = (unsigned int)kvno_diff;
210     }
211     ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
212 		       &ext, &size, ret);
213     if (ret)
214 	abort();
215     if (buf.length != size)
216 	abort();
217 
218     add_tl(princ, KRB5_TL_EXTENSION, &buf);
219 }
220 
221 static int
222 do_mod_entry(krb5_principal principal, void *data)
223 {
224     krb5_error_code ret;
225     kadm5_principal_ent_rec princ;
226     int mask = 0;
227     struct modify_options *e = data;
228 
229     memset (&princ, 0, sizeof(princ));
230     ret = kadm5_get_principal(kadm_handle, principal, &princ,
231 			      KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
232 			      KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
233 			      KADM5_PRINC_EXPIRE_TIME |
234 			      KADM5_PW_EXPIRATION);
235     if(ret)
236 	return ret;
237 
238     if(e->max_ticket_life_string ||
239        e->max_renewable_life_string ||
240        e->expiration_time_string ||
241        e->pw_expiration_time_string ||
242        e->attributes_string ||
243        e->policy_string ||
244        e->kvno_integer != -1 ||
245        e->constrained_delegation_strings.num_strings ||
246        e->alias_strings.num_strings ||
247        e->pkinit_acl_strings.num_strings ||
248        e->hist_kvno_diff_clnt_integer != -1 ||
249        e->hist_kvno_diff_svc_integer != -1) {
250 	ret = set_entry(context, &princ, &mask,
251 			e->max_ticket_life_string,
252 			e->max_renewable_life_string,
253 			e->expiration_time_string,
254 			e->pw_expiration_time_string,
255 			e->attributes_string,
256 			e->policy_string);
257 	if(e->kvno_integer != -1) {
258 	    princ.kvno = e->kvno_integer;
259 	    mask |= KADM5_KVNO;
260 	}
261 	if (e->constrained_delegation_strings.num_strings) {
262 	    add_constrained_delegation(context, &princ,
263 				       &e->constrained_delegation_strings);
264 	    mask |= KADM5_TL_DATA;
265 	}
266 	if (e->alias_strings.num_strings) {
267 	    add_aliases(context, &princ, &e->alias_strings);
268 	    mask |= KADM5_TL_DATA;
269 	}
270 	if (e->pkinit_acl_strings.num_strings) {
271 	    add_pkinit_acl(context, &princ, &e->pkinit_acl_strings);
272 	    mask |= KADM5_TL_DATA;
273 	}
274 	if (e->hist_kvno_diff_clnt_integer != -1) {
275 	    add_kvno_diff(context, &princ, 0, e->hist_kvno_diff_clnt_integer);
276 	    mask |= KADM5_TL_DATA;
277 	}
278 	if (e->hist_kvno_diff_svc_integer != -1) {
279 	    add_kvno_diff(context, &princ, 1, e->hist_kvno_diff_svc_integer);
280 	    mask |= KADM5_TL_DATA;
281 	}
282     } else
283 	ret = edit_entry(&princ, &mask, NULL, 0);
284     if(ret == 0) {
285 	ret = kadm5_modify_principal(kadm_handle, &princ, mask);
286 	if(ret)
287 	    krb5_warn(context, ret, "kadm5_modify_principal");
288     }
289 
290     kadm5_free_principal_ent(kadm_handle, &princ);
291     return ret;
292 }
293 
294 int
295 mod_entry(struct modify_options *opt, int argc, char **argv)
296 {
297     krb5_error_code ret = 0;
298     int i;
299 
300     for(i = 0; i < argc; i++) {
301 	ret = foreach_principal(argv[i], do_mod_entry, "mod", opt);
302 	if (ret)
303 	    break;
304     }
305     return ret != 0;
306 }
307 
308