2 * Copyright (c) 1997 - 2005 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>
38 kadmind_dispatch(void *kadm_handlep
, krb5_boolean initial
,
39 krb5_data
*in
, krb5_data
*out
)
42 int32_t cmd
, mask
, tmp
;
43 kadm5_server_context
*contextp
= kadm_handlep
;
44 char client
[128], name
[128], name2
[128];
46 krb5_principal princ
, princ2
;
47 kadm5_principal_ent_rec ent
;
48 char *password
, *expression
;
49 krb5_keyblock
*new_keys
;
50 krb5_key_salt_tuple
*ks_tuple
= NULL
;
51 krb5_boolean keepold
= FALSE
;
59 krb5_unparse_name_fixed(contextp
->context
, contextp
->caller
,
60 client
, sizeof(client
));
62 sp
= krb5_storage_from_data(in
);
64 krb5_errx(contextp
->context
, 1, "out of memory");
66 krb5_ret_int32(sp
, &cmd
);
70 ret
= krb5_ret_principal(sp
, &princ
);
73 ret
= krb5_ret_int32(sp
, &mask
);
75 krb5_free_principal(contextp
->context
, princ
);
78 mask
|= KADM5_PRINCIPAL
;
79 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
80 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
81 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_GET_KEYS
, princ
);
85 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_GET
, princ
);
87 krb5_free_principal(contextp
->context
, princ
);
90 ret
= kadm5_get_principal(kadm_handlep
, princ
, &ent
, mask
);
91 krb5_storage_free(sp
);
92 sp
= krb5_storage_emem();
93 krb5_store_int32(sp
, ret
);
96 kadm5_store_principal_ent(sp
, &ent
);
98 kadm5_store_principal_ent_nokeys(sp
, &ent
);
99 kadm5_free_principal_ent(kadm_handlep
, &ent
);
101 krb5_free_principal(contextp
->context
, princ
);
106 ret
= krb5_ret_principal(sp
, &princ
);
109 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
110 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
111 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_DELETE
, princ
);
113 krb5_free_principal(contextp
->context
, princ
);
116 ret
= kadm5_delete_principal(kadm_handlep
, princ
);
117 krb5_free_principal(contextp
->context
, princ
);
118 krb5_storage_free(sp
);
119 sp
= krb5_storage_emem();
120 krb5_store_int32(sp
, ret
);
125 ret
= kadm5_ret_principal_ent(sp
, &ent
);
128 ret
= krb5_ret_int32(sp
, &mask
);
130 kadm5_free_principal_ent(contextp
->context
, &ent
);
133 ret
= krb5_ret_string(sp
, &password
);
135 kadm5_free_principal_ent(contextp
->context
, &ent
);
138 krb5_unparse_name_fixed(contextp
->context
, ent
.principal
,
140 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
141 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_ADD
,
144 kadm5_free_principal_ent(contextp
->context
, &ent
);
145 memset(password
, 0, strlen(password
));
149 ret
= kadm5_create_principal(kadm_handlep
, &ent
,
151 kadm5_free_principal_ent(kadm_handlep
, &ent
);
152 memset(password
, 0, strlen(password
));
154 krb5_storage_free(sp
);
155 sp
= krb5_storage_emem();
156 krb5_store_int32(sp
, ret
);
161 ret
= kadm5_ret_principal_ent(sp
, &ent
);
164 ret
= krb5_ret_int32(sp
, &mask
);
166 kadm5_free_principal_ent(contextp
, &ent
);
169 krb5_unparse_name_fixed(contextp
->context
, ent
.principal
,
171 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
172 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_MODIFY
,
175 kadm5_free_principal_ent(contextp
, &ent
);
178 ret
= kadm5_modify_principal(kadm_handlep
, &ent
, mask
);
179 kadm5_free_principal_ent(kadm_handlep
, &ent
);
180 krb5_storage_free(sp
);
181 sp
= krb5_storage_emem();
182 krb5_store_int32(sp
, ret
);
187 ret
= krb5_ret_principal(sp
, &princ
);
190 ret
= krb5_ret_principal(sp
, &princ2
);
192 krb5_free_principal(contextp
->context
, princ
);
195 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
196 krb5_unparse_name_fixed(contextp
->context
, princ2
, name2
, sizeof(name2
));
197 krb5_warnx(contextp
->context
, "%s: %s %s -> %s",
198 client
, op
, name
, name2
);
199 ret
= _kadm5_acl_check_permission(contextp
,
202 || _kadm5_acl_check_permission(contextp
,
206 krb5_free_principal(contextp
->context
, princ
);
207 krb5_free_principal(contextp
->context
, princ2
);
210 ret
= kadm5_rename_principal(kadm_handlep
, princ
, princ2
);
211 krb5_free_principal(contextp
->context
, princ
);
212 krb5_free_principal(contextp
->context
, princ2
);
213 krb5_storage_free(sp
);
214 sp
= krb5_storage_emem();
215 krb5_store_int32(sp
, ret
);
220 ret
= krb5_ret_principal(sp
, &princ
);
223 ret
= krb5_ret_string(sp
, &password
);
225 krb5_free_principal(contextp
->context
, princ
);
228 ret
= krb5_ret_int32(sp
, &keepold
);
229 if (ret
&& ret
!= HEIM_ERR_EOF
) {
230 krb5_free_principal(contextp
->context
, princ
);
233 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
234 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
237 * The change is allowed if at least one of:
239 * a) allowed by sysadmin
240 * b) it's for the principal him/herself and this was an
241 * initial ticket, but then, check with the password quality
243 * c) the user is on the CPW ACL.
246 if (krb5_config_get_bool_default(contextp
->context
, NULL
, TRUE
,
247 "kadmin", "allow_self_change_password", NULL
)
249 && krb5_principal_compare (contextp
->context
, contextp
->caller
,
253 const char *pwd_reason
;
255 pwd_data
.data
= password
;
256 pwd_data
.length
= strlen(password
);
258 pwd_reason
= kadm5_check_password_quality (contextp
->context
,
260 if (pwd_reason
!= NULL
)
261 ret
= KADM5_PASS_Q_DICT
;
265 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
268 krb5_free_principal(contextp
->context
, princ
);
269 memset(password
, 0, strlen(password
));
273 ret
= kadm5_chpass_principal_3(kadm_handlep
, princ
, keepold
, 0, NULL
,
275 krb5_free_principal(contextp
->context
, princ
);
276 memset(password
, 0, strlen(password
));
278 krb5_storage_free(sp
);
279 sp
= krb5_storage_emem();
280 krb5_store_int32(sp
, ret
);
283 case kadm_chpass_with_key
:{
285 krb5_key_data
*key_data
;
288 op
= "CHPASS_WITH_KEY";
289 ret
= krb5_ret_principal(sp
, &princ
);
292 ret
= krb5_ret_int32(sp
, &n_key_data
);
294 krb5_free_principal(contextp
->context
, princ
);
297 ret
= krb5_ret_int32(sp
, &keepold
);
298 if (ret
&& ret
!= HEIM_ERR_EOF
) {
299 krb5_free_principal(contextp
->context
, princ
);
302 /* n_key_data will be squeezed into an int16_t below. */
303 if (n_key_data
< 0 || n_key_data
>= 1 << 16 ||
304 (size_t)n_key_data
> UINT_MAX
/sizeof(*key_data
)) {
306 krb5_free_principal(contextp
->context
, princ
);
310 key_data
= malloc (n_key_data
* sizeof(*key_data
));
311 if (key_data
== NULL
&& n_key_data
!= 0) {
313 krb5_free_principal(contextp
->context
, princ
);
317 for (i
= 0; i
< n_key_data
; ++i
) {
318 ret
= kadm5_ret_key_data (sp
, &key_data
[i
]);
322 kadm5_free_key_data (contextp
, &dummy
, key_data
);
324 krb5_free_principal(contextp
->context
, princ
);
329 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
330 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
333 * The change is only allowed if the user is on the CPW ACL,
334 * this it to force password quality check on the user.
337 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
339 int16_t dummy
= n_key_data
;
341 kadm5_free_key_data (contextp
, &dummy
, key_data
);
343 krb5_free_principal(contextp
->context
, princ
);
346 ret
= kadm5_chpass_principal_with_key_3(kadm_handlep
, princ
, keepold
,
347 n_key_data
, key_data
);
349 int16_t dummy
= n_key_data
;
350 kadm5_free_key_data (contextp
, &dummy
, key_data
);
353 krb5_free_principal(contextp
->context
, princ
);
354 krb5_storage_free(sp
);
355 sp
= krb5_storage_emem();
356 krb5_store_int32(sp
, ret
);
361 ret
= krb5_ret_principal(sp
, &princ
);
364 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
365 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
367 * The change is allowed if at least one of:
368 * a) it's for the principal him/herself and this was an initial ticket
369 * b) the user is on the CPW ACL.
373 && krb5_principal_compare (contextp
->context
, contextp
->caller
,
377 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
380 krb5_free_principal(contextp
->context
, princ
);
385 * See comments in kadm5_c_randkey_principal() regarding the
388 ret
= krb5_ret_int32(sp
, &keepold
);
389 if (ret
!= 0 && ret
!= HEIM_ERR_EOF
) {
390 krb5_free_principal(contextp
->context
, princ
);
394 ret
= krb5_ret_int32(sp
, &n_ks_tuple
);
395 if (ret
!= 0 && ret
!= HEIM_ERR_EOF
) {
396 krb5_free_principal(contextp
->context
, princ
);
398 } else if (ret
== 0) {
401 if (n_ks_tuple
< 0) {
403 krb5_free_principal(contextp
->context
, princ
);
407 if ((ks_tuple
= calloc(n_ks_tuple
, sizeof (*ks_tuple
))) == NULL
) {
409 krb5_free_principal(contextp
->context
, princ
);
413 for (i
= 0; i
< n_ks_tuple
; i
++) {
414 ret
= krb5_ret_int32(sp
, &ks_tuple
[i
].ks_enctype
);
416 krb5_free_principal(contextp
->context
, princ
);
419 ret
= krb5_ret_int32(sp
, &ks_tuple
[i
].ks_salttype
);
421 krb5_free_principal(contextp
->context
, princ
);
426 ret
= kadm5_randkey_principal_3(kadm_handlep
, princ
, keepold
,
427 n_ks_tuple
, ks_tuple
, &new_keys
,
429 krb5_free_principal(contextp
->context
, princ
);
431 krb5_storage_free(sp
);
432 sp
= krb5_storage_emem();
433 krb5_store_int32(sp
, ret
);
436 krb5_store_int32(sp
, n_keys
);
437 for(i
= 0; i
< n_keys
; i
++){
438 krb5_store_keyblock(sp
, new_keys
[i
]);
439 krb5_free_keyblock_contents(contextp
->context
, &new_keys
[i
]);
445 case kadm_get_privs
:{
447 ret
= kadm5_get_privs(kadm_handlep
, &privs
);
448 krb5_storage_free(sp
);
449 sp
= krb5_storage_emem();
450 krb5_store_int32(sp
, ret
);
452 krb5_store_uint32(sp
, privs
);
455 case kadm_get_princs
:{
457 ret
= krb5_ret_int32(sp
, &tmp
);
461 ret
= krb5_ret_string(sp
, &expression
);
466 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
,
467 expression
? expression
: "*");
468 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_LIST
, NULL
);
473 ret
= kadm5_get_principals(kadm_handlep
, expression
, &princs
, &n_princs
);
475 krb5_storage_free(sp
);
476 sp
= krb5_storage_emem();
477 krb5_store_int32(sp
, ret
);
480 krb5_store_int32(sp
, n_princs
);
481 for(i
= 0; i
< n_princs
; i
++)
482 krb5_store_string(sp
, princs
[i
]);
483 kadm5_free_name_list(kadm_handlep
, princs
, &n_princs
);
488 krb5_warnx(contextp
->context
, "%s: UNKNOWN OP %d", client
, cmd
);
489 krb5_storage_free(sp
);
490 sp
= krb5_storage_emem();
491 krb5_store_int32(sp
, KADM5_FAILURE
);
494 krb5_storage_to_data(sp
, out
);
495 krb5_storage_free(sp
);
498 krb5_warn(contextp
->context
, ret
, "%s", op
);
499 krb5_storage_seek(sp
, 0, SEEK_SET
);
500 krb5_store_int32(sp
, ret
);
501 krb5_storage_to_data(sp
, out
);
502 krb5_storage_free(sp
);
507 v5_loop (krb5_context contextp
,
508 krb5_auth_context ac
,
509 krb5_boolean initial
,
517 doing_useful_work
= 0;
520 ret
= krb5_read_priv_message(contextp
, ac
, &fd
, &in
);
521 if(ret
== HEIM_ERR_EOF
)
524 krb5_err(contextp
, 1, ret
, "krb5_read_priv_message");
525 doing_useful_work
= 1;
526 kadmind_dispatch(kadm_handlep
, initial
, &in
, &out
);
528 ret
= krb5_write_priv_message(contextp
, ac
, &fd
, &out
);
530 krb5_err(contextp
, 1, ret
, "krb5_write_priv_message");
535 match_appl_version(const void *data
, const char *appl_version
)
538 if(sscanf(appl_version
, "KADM0.%u", &minor
) != 1)
541 *(unsigned*)(intptr_t)data
= minor
;
546 handle_v5(krb5_context contextp
,
555 krb5_boolean initial
;
556 krb5_auth_context ac
= NULL
;
558 unsigned kadm_version
;
559 kadm5_config_params realm_params
;
561 ret
= krb5_recvauth_match_version(contextp
, &ac
, &fd
,
562 match_appl_version
, &kadm_version
,
563 NULL
, KRB5_RECVAUTH_IGNORE_VERSION
,
566 krb5_err(contextp
, 1, ret
, "krb5_recvauth");
568 ret
= krb5_unparse_name (contextp
, ticket
->server
, &server_name
);
570 krb5_err (contextp
, 1, ret
, "krb5_unparse_name");
572 if (strncmp (server_name
, KADM5_ADMIN_SERVICE
,
573 strlen(KADM5_ADMIN_SERVICE
)) != 0)
574 krb5_errx (contextp
, 1, "ticket for strange principal (%s)",
579 memset(&realm_params
, 0, sizeof(realm_params
));
581 if(kadm_version
== 1) {
583 ret
= krb5_read_priv_message(contextp
, ac
, &fd
, ¶ms
);
585 krb5_err(contextp
, 1, ret
, "krb5_read_priv_message");
586 _kadm5_unmarshal_params(contextp
, ¶ms
, &realm_params
);
589 initial
= ticket
->ticket
.flags
.initial
;
590 ret
= krb5_unparse_name(contextp
, ticket
->client
, &client
);
592 krb5_err (contextp
, 1, ret
, "krb5_unparse_name");
593 krb5_free_ticket (contextp
, ticket
);
594 ret
= kadm5_s_init_with_password_ctx(contextp
,
602 krb5_err (contextp
, 1, ret
, "kadm5_init_with_password_ctx");
603 v5_loop (contextp
, ac
, initial
, kadm_handlep
, fd
);
607 kadmind_loop(krb5_context contextp
,
611 u_char buf
[sizeof(KRB5_SENDAUTH_VERSION
) + 4];
615 n
= krb5_net_read(contextp
, &sock
, buf
, 4);
619 krb5_err(contextp
, 1, errno
, "read");
620 _krb5_get_int(buf
, &len
, 4);
622 if (len
== sizeof(KRB5_SENDAUTH_VERSION
)) {
624 n
= krb5_net_read(contextp
, &sock
, buf
+ 4, len
);
626 krb5_err (contextp
, 1, errno
, "reading sendauth version");
628 krb5_errx (contextp
, 1, "EOF reading sendauth version");
630 if(memcmp(buf
+ 4, KRB5_SENDAUTH_VERSION
, len
) == 0) {
631 handle_v5(contextp
, keytab
, sock
);
638 handle_mit(contextp
, buf
, len
, sock
);