No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kadmin / mod.c
blob70d24f2ea89011a2666def221b58893de5f59d48
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "kadmin_locl.h"
35 #include "kadmin-commands.h"
37 __RCSID("$Heimdal: mod.c 21968 2007-10-18 18:50:33Z lha $"
38 "$NetBSD$");
40 static void
41 add_tl(kadm5_principal_ent_rec *princ, int type, krb5_data *data)
43 krb5_tl_data *tl, **ptl;
45 tl = ecalloc(1, sizeof(*tl));
46 tl->tl_data_next = NULL;
47 tl->tl_data_type = KRB5_TL_EXTENSION;
48 tl->tl_data_length = data->length;
49 tl->tl_data_contents = data->data;
51 princ->n_tl_data++;
52 ptl = &princ->tl_data;
53 while (*ptl != NULL)
54 ptl = &(*ptl)->tl_data_next;
55 *ptl = tl;
57 return;
60 static void
61 add_constrained_delegation(krb5_context context,
62 kadm5_principal_ent_rec *princ,
63 struct getarg_strings *strings)
65 krb5_error_code ret;
66 HDB_extension ext;
67 krb5_data buf;
68 size_t size;
70 memset(&ext, 0, sizeof(ext));
71 ext.mandatory = FALSE;
72 ext.data.element = choice_HDB_extension_data_allowed_to_delegate_to;
74 if (strings->num_strings == 1 && strings->strings[0][0] == '\0') {
75 ext.data.u.allowed_to_delegate_to.val = NULL;
76 ext.data.u.allowed_to_delegate_to.len = 0;
77 } else {
78 krb5_principal p;
79 int i;
81 ext.data.u.allowed_to_delegate_to.val =
82 calloc(strings->num_strings,
83 sizeof(ext.data.u.allowed_to_delegate_to.val[0]));
84 ext.data.u.allowed_to_delegate_to.len = strings->num_strings;
86 for (i = 0; i < strings->num_strings; i++) {
87 ret = krb5_parse_name(context, strings->strings[i], &p);
88 ret = copy_Principal(p, &ext.data.u.allowed_to_delegate_to.val[i]);
89 krb5_free_principal(context, p);
93 ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
94 &ext, &size, ret);
95 free_HDB_extension(&ext);
96 if (ret)
97 abort();
98 if (buf.length != size)
99 abort();
101 add_tl(princ, KRB5_TL_EXTENSION, &buf);
104 static void
105 add_aliases(krb5_context context, kadm5_principal_ent_rec *princ,
106 struct getarg_strings *strings)
108 krb5_error_code ret;
109 HDB_extension ext;
110 krb5_data buf;
111 krb5_principal p;
112 size_t size;
113 int i;
115 memset(&ext, 0, sizeof(ext));
116 ext.mandatory = FALSE;
117 ext.data.element = choice_HDB_extension_data_aliases;
118 ext.data.u.aliases.case_insensitive = 0;
120 if (strings->num_strings == 1 && strings->strings[0][0] == '\0') {
121 ext.data.u.aliases.aliases.val = NULL;
122 ext.data.u.aliases.aliases.len = 0;
123 } else {
124 ext.data.u.aliases.aliases.val =
125 calloc(strings->num_strings,
126 sizeof(ext.data.u.aliases.aliases.val[0]));
127 ext.data.u.aliases.aliases.len = strings->num_strings;
129 for (i = 0; i < strings->num_strings; i++) {
130 ret = krb5_parse_name(context, strings->strings[i], &p);
131 ret = copy_Principal(p, &ext.data.u.aliases.aliases.val[i]);
132 krb5_free_principal(context, p);
136 ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
137 &ext, &size, ret);
138 free_HDB_extension(&ext);
139 if (ret)
140 abort();
141 if (buf.length != size)
142 abort();
144 add_tl(princ, KRB5_TL_EXTENSION, &buf);
147 static void
148 add_pkinit_acl(krb5_context context, kadm5_principal_ent_rec *princ,
149 struct getarg_strings *strings)
151 krb5_error_code ret;
152 HDB_extension ext;
153 krb5_data buf;
154 size_t size;
155 int i;
157 memset(&ext, 0, sizeof(ext));
158 ext.mandatory = FALSE;
159 ext.data.element = choice_HDB_extension_data_pkinit_acl;
160 ext.data.u.aliases.case_insensitive = 0;
162 if (strings->num_strings == 1 && strings->strings[0][0] == '\0') {
163 ext.data.u.pkinit_acl.val = NULL;
164 ext.data.u.pkinit_acl.len = 0;
165 } else {
166 ext.data.u.pkinit_acl.val =
167 calloc(strings->num_strings,
168 sizeof(ext.data.u.pkinit_acl.val[0]));
169 ext.data.u.pkinit_acl.len = strings->num_strings;
171 for (i = 0; i < strings->num_strings; i++) {
172 ext.data.u.pkinit_acl.val[i].subject = estrdup(strings->strings[i]);
176 ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
177 &ext, &size, ret);
178 free_HDB_extension(&ext);
179 if (ret)
180 abort();
181 if (buf.length != size)
182 abort();
184 add_tl(princ, KRB5_TL_EXTENSION, &buf);
187 static int
188 do_mod_entry(krb5_principal principal, void *data)
190 krb5_error_code ret;
191 kadm5_principal_ent_rec princ;
192 int mask = 0;
193 struct modify_options *e = data;
195 memset (&princ, 0, sizeof(princ));
196 ret = kadm5_get_principal(kadm_handle, principal, &princ,
197 KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
198 KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
199 KADM5_PRINC_EXPIRE_TIME |
200 KADM5_PW_EXPIRATION);
201 if(ret)
202 return ret;
204 if(e->max_ticket_life_string ||
205 e->max_renewable_life_string ||
206 e->expiration_time_string ||
207 e->pw_expiration_time_string ||
208 e->attributes_string ||
209 e->kvno_integer != -1 ||
210 e->constrained_delegation_strings.num_strings ||
211 e->alias_strings.num_strings ||
212 e->pkinit_acl_strings.num_strings) {
213 ret = set_entry(context, &princ, &mask,
214 e->max_ticket_life_string,
215 e->max_renewable_life_string,
216 e->expiration_time_string,
217 e->pw_expiration_time_string,
218 e->attributes_string);
219 if(e->kvno_integer != -1) {
220 princ.kvno = e->kvno_integer;
221 mask |= KADM5_KVNO;
223 if (e->constrained_delegation_strings.num_strings) {
224 add_constrained_delegation(context, &princ,
225 &e->constrained_delegation_strings);
226 mask |= KADM5_TL_DATA;
228 if (e->alias_strings.num_strings) {
229 add_aliases(context, &princ, &e->alias_strings);
230 mask |= KADM5_TL_DATA;
232 if (e->pkinit_acl_strings.num_strings) {
233 add_pkinit_acl(context, &princ, &e->pkinit_acl_strings);
234 mask |= KADM5_TL_DATA;
237 } else
238 ret = edit_entry(&princ, &mask, NULL, 0);
239 if(ret == 0) {
240 ret = kadm5_modify_principal(kadm_handle, &princ, mask);
241 if(ret)
242 krb5_warn(context, ret, "kadm5_modify_principal");
245 kadm5_free_principal_ent(kadm_handle, &princ);
246 return ret;
250 mod_entry(struct modify_options *opt, int argc, char **argv)
252 krb5_error_code ret = 0;
253 int i;
255 for(i = 0; i < argc; i++) {
256 ret = foreach_principal(argv[i], do_mod_entry, "mod", opt);
257 if (ret)
258 break;
260 return ret != 0;