xref: /freebsd/crypto/heimdal/kadmin/mod.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*
2  * Copyright (c) 1997 - 2006 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "kadmin_locl.h"
35 #include "kadmin-commands.h"
36 
37 RCSID("$Id: mod.c 21968 2007-10-18 18:50:33Z lha $");
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 = KRB5_TL_EXTENSION;
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 context,
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;
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(context, strings->strings[i], &p);
87 	    ret = copy_Principal(p, &ext.data.u.allowed_to_delegate_to.val[i]);
88 	    krb5_free_principal(context, p);
89 	}
90     }
91 
92     ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
93 		       &ext, &size, ret);
94     free_HDB_extension(&ext);
95     if (ret)
96 	abort();
97     if (buf.length != size)
98 	abort();
99 
100     add_tl(princ, KRB5_TL_EXTENSION, &buf);
101 }
102 
103 static void
104 add_aliases(krb5_context context, kadm5_principal_ent_rec *princ,
105 	    struct getarg_strings *strings)
106 {
107     krb5_error_code ret;
108     HDB_extension ext;
109     krb5_data buf;
110     krb5_principal p;
111     size_t size;
112     int i;
113 
114     memset(&ext, 0, sizeof(ext));
115     ext.mandatory = FALSE;
116     ext.data.element = choice_HDB_extension_data_aliases;
117     ext.data.u.aliases.case_insensitive = 0;
118 
119     if (strings->num_strings == 1 && strings->strings[0][0] == '\0') {
120 	ext.data.u.aliases.aliases.val = NULL;
121 	ext.data.u.aliases.aliases.len = 0;
122     } else {
123 	ext.data.u.aliases.aliases.val =
124 	    calloc(strings->num_strings,
125 		   sizeof(ext.data.u.aliases.aliases.val[0]));
126 	ext.data.u.aliases.aliases.len = strings->num_strings;
127 
128 	for (i = 0; i < strings->num_strings; i++) {
129 	    ret = krb5_parse_name(context, strings->strings[i], &p);
130 	    ret = copy_Principal(p, &ext.data.u.aliases.aliases.val[i]);
131 	    krb5_free_principal(context, p);
132 	}
133     }
134 
135     ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
136 		       &ext, &size, ret);
137     free_HDB_extension(&ext);
138     if (ret)
139 	abort();
140     if (buf.length != size)
141 	abort();
142 
143     add_tl(princ, KRB5_TL_EXTENSION, &buf);
144 }
145 
146 static void
147 add_pkinit_acl(krb5_context context, kadm5_principal_ent_rec *princ,
148 	       struct getarg_strings *strings)
149 {
150     krb5_error_code ret;
151     HDB_extension ext;
152     krb5_data buf;
153     size_t size;
154     int i;
155 
156     memset(&ext, 0, sizeof(ext));
157     ext.mandatory = FALSE;
158     ext.data.element = choice_HDB_extension_data_pkinit_acl;
159     ext.data.u.aliases.case_insensitive = 0;
160 
161     if (strings->num_strings == 1 && strings->strings[0][0] == '\0') {
162 	ext.data.u.pkinit_acl.val = NULL;
163 	ext.data.u.pkinit_acl.len = 0;
164     } else {
165 	ext.data.u.pkinit_acl.val =
166 	    calloc(strings->num_strings,
167 		   sizeof(ext.data.u.pkinit_acl.val[0]));
168 	ext.data.u.pkinit_acl.len = strings->num_strings;
169 
170 	for (i = 0; i < strings->num_strings; i++) {
171 	    ext.data.u.pkinit_acl.val[i].subject = estrdup(strings->strings[i]);
172 	}
173     }
174 
175     ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
176 		       &ext, &size, ret);
177     free_HDB_extension(&ext);
178     if (ret)
179 	abort();
180     if (buf.length != size)
181 	abort();
182 
183     add_tl(princ, KRB5_TL_EXTENSION, &buf);
184 }
185 
186 static int
187 do_mod_entry(krb5_principal principal, void *data)
188 {
189     krb5_error_code ret;
190     kadm5_principal_ent_rec princ;
191     int mask = 0;
192     struct modify_options *e = data;
193 
194     memset (&princ, 0, sizeof(princ));
195     ret = kadm5_get_principal(kadm_handle, principal, &princ,
196 			      KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
197 			      KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
198 			      KADM5_PRINC_EXPIRE_TIME |
199 			      KADM5_PW_EXPIRATION);
200     if(ret)
201 	return ret;
202 
203     if(e->max_ticket_life_string ||
204        e->max_renewable_life_string ||
205        e->expiration_time_string ||
206        e->pw_expiration_time_string ||
207        e->attributes_string ||
208        e->kvno_integer != -1 ||
209        e->constrained_delegation_strings.num_strings ||
210        e->alias_strings.num_strings ||
211        e->pkinit_acl_strings.num_strings) {
212 	ret = set_entry(context, &princ, &mask,
213 			e->max_ticket_life_string,
214 			e->max_renewable_life_string,
215 			e->expiration_time_string,
216 			e->pw_expiration_time_string,
217 			e->attributes_string);
218 	if(e->kvno_integer != -1) {
219 	    princ.kvno = e->kvno_integer;
220 	    mask |= KADM5_KVNO;
221 	}
222 	if (e->constrained_delegation_strings.num_strings) {
223 	    add_constrained_delegation(context, &princ,
224 				       &e->constrained_delegation_strings);
225 	    mask |= KADM5_TL_DATA;
226 	}
227 	if (e->alias_strings.num_strings) {
228 	    add_aliases(context, &princ, &e->alias_strings);
229 	    mask |= KADM5_TL_DATA;
230 	}
231 	if (e->pkinit_acl_strings.num_strings) {
232 	    add_pkinit_acl(context, &princ, &e->pkinit_acl_strings);
233 	    mask |= KADM5_TL_DATA;
234 	}
235 
236     } else
237 	ret = edit_entry(&princ, &mask, NULL, 0);
238     if(ret == 0) {
239 	ret = kadm5_modify_principal(kadm_handle, &princ, mask);
240 	if(ret)
241 	    krb5_warn(context, ret, "kadm5_modify_principal");
242     }
243 
244     kadm5_free_principal_ent(kadm_handle, &princ);
245     return ret;
246 }
247 
248 int
249 mod_entry(struct modify_options *opt, int argc, char **argv)
250 {
251     krb5_error_code ret = 0;
252     int i;
253 
254     for(i = 0; i < argc; i++) {
255 	ret = foreach_principal(argv[i], do_mod_entry, "mod", opt);
256 	if (ret)
257 	    break;
258     }
259     return ret != 0;
260 }
261 
262