2 * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "kadmin_locl.h"
35 #include <krb5-private.h>
40 kadmind_dispatch(void *kadm_handle
, krb5_boolean initial
,
41 krb5_data
*in
, krb5_data
*out
)
44 int32_t cmd
, mask
, tmp
;
45 kadm5_server_context
*context
= kadm_handle
;
46 char client
[128], name
[128], name2
[128];
48 krb5_principal princ
, princ2
;
49 kadm5_principal_ent_rec ent
;
51 krb5_keyblock
*new_keys
;
57 krb5_unparse_name_fixed(context
->context
, context
->caller
,
58 client
, sizeof(client
));
60 sp
= krb5_storage_from_data(in
);
62 krb5_ret_int32(sp
, &cmd
);
66 ret
= krb5_ret_principal(sp
, &princ
);
69 ret
= krb5_ret_int32(sp
, &mask
);
71 krb5_free_principal(context
->context
, princ
);
74 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
75 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
76 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_GET
);
78 krb5_free_principal(context
->context
, princ
);
81 ret
= kadm5_get_principal(kadm_handle
, princ
, &ent
, mask
);
82 krb5_storage_free(sp
);
83 sp
= krb5_storage_emem();
84 krb5_store_int32(sp
, ret
);
86 kadm5_store_principal_ent(sp
, &ent
);
87 kadm5_free_principal_ent(kadm_handle
, &ent
);
89 krb5_free_principal(context
->context
, princ
);
94 ret
= krb5_ret_principal(sp
, &princ
);
97 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
98 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
99 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_DELETE
);
101 krb5_free_principal(context
->context
, princ
);
104 ret
= kadm5_delete_principal(kadm_handle
, princ
);
105 krb5_free_principal(context
->context
, princ
);
106 krb5_storage_free(sp
);
107 sp
= krb5_storage_emem();
108 krb5_store_int32(sp
, ret
);
113 ret
= kadm5_ret_principal_ent(sp
, &ent
);
116 ret
= krb5_ret_int32(sp
, &mask
);
118 kadm5_free_principal_ent(context
->context
, &ent
);
121 ret
= krb5_ret_string(sp
, &password
);
123 kadm5_free_principal_ent(context
->context
, &ent
);
126 krb5_unparse_name_fixed(context
->context
, ent
.principal
,
128 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
129 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_ADD
);
131 kadm5_free_principal_ent(context
->context
, &ent
);
132 memset(password
, 0, strlen(password
));
136 ret
= kadm5_create_principal(kadm_handle
, &ent
,
138 kadm5_free_principal_ent(kadm_handle
, &ent
);
139 memset(password
, 0, strlen(password
));
141 krb5_storage_free(sp
);
142 sp
= krb5_storage_emem();
143 krb5_store_int32(sp
, ret
);
148 ret
= kadm5_ret_principal_ent(sp
, &ent
);
151 ret
= krb5_ret_int32(sp
, &mask
);
153 kadm5_free_principal_ent(context
, &ent
);
156 krb5_unparse_name_fixed(context
->context
, ent
.principal
,
158 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
159 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_MODIFY
);
161 kadm5_free_principal_ent(context
, &ent
);
164 ret
= kadm5_modify_principal(kadm_handle
, &ent
, mask
);
165 kadm5_free_principal_ent(kadm_handle
, &ent
);
166 krb5_storage_free(sp
);
167 sp
= krb5_storage_emem();
168 krb5_store_int32(sp
, ret
);
173 ret
= krb5_ret_principal(sp
, &princ
);
176 ret
= krb5_ret_principal(sp
, &princ2
);
178 krb5_free_principal(context
->context
, princ
);
181 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
182 krb5_unparse_name_fixed(context
->context
, princ2
, name2
, sizeof(name2
));
183 krb5_warnx(context
->context
, "%s: %s %s -> %s",
184 client
, op
, name
, name2
);
185 ret
= _kadm5_acl_check_permission(context
,
186 KADM5_PRIV_ADD
|KADM5_PRIV_DELETE
);
188 krb5_free_principal(context
->context
, princ
);
191 ret
= kadm5_rename_principal(kadm_handle
, princ
, princ2
);
192 krb5_free_principal(context
->context
, princ
);
193 krb5_free_principal(context
->context
, princ2
);
194 krb5_storage_free(sp
);
195 sp
= krb5_storage_emem();
196 krb5_store_int32(sp
, ret
);
201 ret
= krb5_ret_principal(sp
, &princ
);
204 ret
= krb5_ret_string(sp
, &password
);
206 krb5_free_principal(context
->context
, princ
);
209 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
210 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
213 * The change is allowed if at least one of:
214 * a) it's for the principal him/herself and this was an initial ticket
215 * b) the user is on the CPW ACL.
219 && krb5_principal_compare (context
->context
, context
->caller
,
223 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
);
226 krb5_free_principal(context
->context
, princ
);
229 ret
= kadm5_chpass_principal(kadm_handle
, princ
, password
);
230 krb5_free_principal(context
->context
, princ
);
231 memset(password
, 0, strlen(password
));
233 krb5_storage_free(sp
);
234 sp
= krb5_storage_emem();
235 krb5_store_int32(sp
, ret
);
240 ret
= krb5_ret_principal(sp
, &princ
);
243 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
244 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
246 * The change is allowed if at least one of:
247 * a) it's for the principal him/herself and this was an initial ticket
248 * b) the user is on the CPW ACL.
252 && krb5_principal_compare (context
->context
, context
->caller
,
256 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
);
259 krb5_free_principal(context
->context
, princ
);
262 ret
= kadm5_randkey_principal(kadm_handle
, princ
,
264 krb5_free_principal(context
->context
, princ
);
265 krb5_storage_free(sp
);
266 sp
= krb5_storage_emem();
267 krb5_store_int32(sp
, ret
);
270 krb5_store_int32(sp
, n_keys
);
271 for(i
= 0; i
< n_keys
; i
++){
272 krb5_store_keyblock(sp
, new_keys
[i
]);
273 krb5_free_keyblock_contents(context
->context
, &new_keys
[i
]);
278 case kadm_get_privs
:{
279 ret
= kadm5_get_privs(kadm_handle
, &mask
);
280 krb5_storage_free(sp
);
281 sp
= krb5_storage_emem();
282 krb5_store_int32(sp
, ret
);
284 krb5_store_int32(sp
, mask
);
287 case kadm_get_princs
:{
289 ret
= krb5_ret_int32(sp
, &tmp
);
293 ret
= krb5_ret_string(sp
, &exp
);
298 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, exp
? exp
: "*");
299 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_LIST
);
304 ret
= kadm5_get_principals(kadm_handle
, exp
, &princs
, &n_princs
);
306 krb5_storage_free(sp
);
307 sp
= krb5_storage_emem();
308 krb5_store_int32(sp
, ret
);
311 krb5_store_int32(sp
, n_princs
);
312 for(i
= 0; i
< n_princs
; i
++)
313 krb5_store_string(sp
, princs
[i
]);
314 kadm5_free_name_list(kadm_handle
, princs
, &n_princs
);
319 krb5_warnx(context
->context
, "%s: UNKNOWN OP %d", client
, cmd
);
320 krb5_storage_free(sp
);
321 sp
= krb5_storage_emem();
322 krb5_store_int32(sp
, KADM5_FAILURE
);
325 krb5_storage_to_data(sp
, out
);
326 krb5_storage_free(sp
);
329 krb5_warn(context
->context
, ret
, "%s", op
);
330 sp
->seek(sp
, 0, SEEK_SET
);
331 krb5_store_int32(sp
, ret
);
332 krb5_storage_to_data(sp
, out
);
333 krb5_storage_free(sp
);
338 v5_loop (krb5_context context
,
339 krb5_auth_context ac
,
340 krb5_boolean initial
,
349 krb5_data in
, out
, msg
, reply
;
352 n
= krb5_net_read(context
, &fd
, tmp
, 4);
354 krb5_err (context
, 1, errno
, "krb5_net_read");
357 _krb5_get_int (tmp
, &len
, 4);
359 ret
= krb5_data_alloc(&in
, len
);
361 krb5_err (context
, 1, ret
, "krb5_data_alloc");
363 n
= krb5_net_read(context
, &fd
, in
.data
, in
.length
);
367 krb5_errx(context
, 1, "read error: %d", errno
);
368 ret
= krb5_rd_priv(context
, ac
, &in
, &out
, NULL
);
370 krb5_err(context
, 1, ret
, "krb5_rd_priv");
372 kadmind_dispatch(kadm_handle
, initial
, &out
, &msg
);
373 krb5_data_free(&out
);
374 ret
= krb5_mk_priv(context
, ac
, &msg
, &reply
, NULL
);
375 krb5_data_free(&msg
);
377 krb5_err(context
, 1, ret
, "krb5_mk_priv");
379 _krb5_put_int(tmp
, reply
.length
, 4);
381 iov
[0].iov_base
= tmp
;
383 iov
[1].iov_base
= reply
.data
;
384 iov
[1].iov_len
= reply
.length
;
385 n
= writev(fd
, iov
, 2);
386 krb5_data_free(&reply
);
388 krb5_err(context
, 1, errno
, "writev");
389 if(n
< iov
[0].iov_len
+ iov
[1].iov_len
)
390 krb5_errx(context
, 1, "short write");
395 match_appl_version(void *data
, const char *appl_version
)
398 if(sscanf(appl_version
, "KADM0.%u", &minor
) != 1)
400 *(unsigned*)data
= minor
;
405 handle_v5(krb5_context context
,
406 krb5_auth_context ac
,
412 u_char version
[sizeof(KRB5_SENDAUTH_VERSION
)];
414 krb5_principal server
;
418 krb5_boolean initial
;
420 unsigned kadm_version
;
421 kadm5_config_params realm_params
;
423 if (len
!= sizeof(KRB5_SENDAUTH_VERSION
))
424 krb5_errx(context
, 1, "bad sendauth len %d", len
);
425 n
= krb5_net_read(context
, &fd
, version
, len
);
427 krb5_err (context
, 1, errno
, "reading sendauth version");
429 krb5_errx (context
, 1, "EOF reading sendauth version");
430 if(memcmp(version
, KRB5_SENDAUTH_VERSION
, len
) != 0)
431 krb5_errx(context
, 1, "bad sendauth version %.8s", version
);
433 ret
= krb5_parse_name(context
, KADM5_ADMIN_SERVICE
, &server
);
435 krb5_err (context
, 1, ret
, "krb5_parse_name %s", KADM5_ADMIN_SERVICE
);
436 ret
= krb5_recvauth_match_version(context
, &ac
, &fd
,
437 match_appl_version
, &kadm_version
,
438 server
, KRB5_RECVAUTH_IGNORE_VERSION
,
440 if(ret
== KRB5_KT_NOTFOUND
) {
442 krb5_unparse_name(context
, server
, &name
);
443 krb5_errx(context
, 1, "krb5_recvauth: %s (%s)",
444 krb5_get_err_text(context
, ret
),
447 krb5_free_principal(context
, server
);
450 krb5_err(context
, 1, ret
, "krb5_recvauth");
452 memset(&realm_params
, 0, sizeof(realm_params
));
454 if(kadm_version
== 1) {
455 krb5_data enc_data
, params
;
456 ret
= krb5_read_message(context
, &fd
, &enc_data
);
457 ret
= krb5_rd_priv(context
, ac
, &enc_data
, ¶ms
, NULL
);
458 krb5_data_free(&enc_data
);
459 _kadm5_unmarshal_params(context
, ¶ms
, &realm_params
);
462 initial
= ticket
->ticket
.flags
.initial
;
463 ret
= krb5_unparse_name(context
, ticket
->client
, &client
);
465 krb5_err (context
, 1, ret
, "krb5_unparse_name");
466 krb5_free_ticket (context
, ticket
);
467 ret
= kadm5_init_with_password_ctx(context
,
475 krb5_err (context
, 1, ret
, "kadm5_init_with_password_ctx");
476 v5_loop (context
, ac
, initial
, kadm_handle
, fd
);
480 kadmind_loop(krb5_context context
,
481 krb5_auth_context ac
,
485 unsigned char tmp
[4];
489 n
= krb5_net_read(context
, &fd
, tmp
, 4);
493 krb5_errx(context
, 1, "read error: %d", errno
);
494 _krb5_get_int(tmp
, &len
, 4);
495 if(len
> 0xffff && (len
& 0xffff) == ('K' << 8) + 'A') {
498 handle_v4(context
, len
, fd
);
500 krb5_errx(context
, 1, "packet appears to be version 4");
503 handle_v5(context
, ac
, keytab
, len
, fd
);