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>
37 static kadm5_ret_t
check_aliases(kadm5_server_context
*,
38 kadm5_principal_ent_rec
*,
39 kadm5_principal_ent_rec
*);
42 * All the iter_cb stuff is about online listing of principals via
43 * kadm5_iter_principals(). Search for "LIST" to see more commentary.
53 unsigned int initial
:1;
58 * This function sends the current chunk of principal listing and checks if the
59 * client requested that the listing stop.
62 iter_cb_send_now(struct iter_cb_data
*d
)
74 * The client can send us one message to interrupt the iteration.
76 * TODO: Maybe we should have the client send a message every N chunks
77 * so we can clock the listing and have a chance to receive any
78 * interrupt message from the client?
84 nfds
= select(d
->fd
+ 1, &fds
, NULL
, NULL
, &tv
);
87 } else if (nfds
> 0) {
89 * And it did. We'll throw this message away. It should be a NOP
90 * call, which we'd throw away anyways. If the client's stop
91 * message arrives after we're done anyways, well, it will be
92 * processed as a NOP and thrown away.
95 d
->ret
= krb5_read_priv_message(d
->context
, d
->ac
, &d
->fd
, &out
);
97 if (d
->ret
== HEIM_ERR_EOF
)
102 d
->ret
= krb5_storage_to_data(d
->rsp
, &out
);
104 d
->ret
= krb5_write_priv_message(d
->context
, d
->ac
, &d
->fd
, &out
);
105 krb5_data_free(&out
);
106 krb5_storage_free(d
->rsp
);
107 if ((d
->rsp
= krb5_storage_emem()) == NULL
)
108 return krb5_enomem(d
->context
);
113 iter_cb(void *cbdata
, const char *p
)
115 struct iter_cb_data
*d
= cbdata
;
116 krb5_error_code ret
= 0;
119 /* Convince the compiler that `-(int)d->n' is defined */
120 if (n
== 0 || n
> INT_MAX
)
122 if (d
->rsp
== NULL
&& (d
->rsp
= krb5_storage_emem()) == NULL
)
123 return krb5_enomem(d
->context
);
125 /* Every chunk starts with a result code */
126 ret
= krb5_store_int32(d
->rsp
, d
->ret
);
134 * We'll send up to `d->n' entries per-write. We send a negative
135 * number to indicate we accepted the client's proposal that we speak
136 * the online LIST protocol.
138 * Note that if we're here then we've already placed a result code in
139 * this reply (see above).
142 ret
= krb5_store_int32(d
->rsp
, -(int)n
); /* Princs per-chunk */
144 ret
= iter_cb_send_now(d
);
148 * Now that we've sent the acceptance reply, put a result code as the
149 * first thing in the next reply, which will have the first chunk of
152 ret
= krb5_store_int32(d
->rsp
, d
->ret
);
160 ret
= krb5_store_string(d
->rsp
, p
);
164 * We get called with `p == NULL' when the listing is done. This
165 * forces us to iter_cb_send_now(d) below, but also forces us to have a
166 * properly formed reply (i.e., that we have a result code as the first
167 * item), even if the chunk is otherwise empty (`d->i == 0').
172 if (ret
== 0 && d
->i
== n
)
173 ret
= iter_cb_send_now(d
); /* Chunk finished; send it */
180 kadmind_dispatch(void *kadm_handlep
, krb5_boolean initial
,
181 krb5_data
*in
, krb5_auth_context ac
, int fd
,
182 krb5_data
*out
, int readonly
)
185 kadm5_ret_t ret_sp
= 0;
186 int32_t cmd
, mask
, kvno
, tmp
;
187 kadm5_server_context
*contextp
= kadm_handlep
;
188 char client
[128], name
[128], name2
[128];
190 krb5_principal princ
= NULL
, princ2
= NULL
;
191 kadm5_principal_ent_rec ent
, ent_prev
;
192 char *password
= NULL
, *expression
;
193 krb5_keyblock
*new_keys
;
194 krb5_key_salt_tuple
*ks_tuple
= NULL
;
201 krb5_storage
*rsp
; /* response goes here */
205 memset(&ent
, 0, sizeof(ent
));
206 memset(&ent_prev
, 0, sizeof(ent_prev
));
209 rsp
= krb5_storage_emem();
211 return krb5_enomem(contextp
->context
);
213 sp
= krb5_storage_from_data(in
);
215 krb5_storage_free(rsp
);
216 return krb5_enomem(contextp
->context
);
219 ret
= krb5_unparse_name_fixed(contextp
->context
, contextp
->caller
,
220 client
, sizeof(client
));
222 ret
= krb5_ret_int32(sp
, &cmd
);
224 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
231 * In the future we could use this for versioning.
233 * We used to respond to NOPs with KADM5_FAILURE. Now we respond with
234 * zero. In the future we could send back a protocol version number
235 * and use NOPs for protocol version negotiation.
237 * In the meantime, this gets called only if a client wants to
238 * interrupt a long-running LIST operation.
241 ret
= krb5_ret_int32(sp
, &tmp
);
242 if (ret
== 0 && tmp
== 0) {
244 * Reply not wanted. This would be a LIST interrupt request.
246 krb5_storage_free(rsp
);
247 krb5_storage_free(sp
);
250 ret_sp
= krb5_store_int32(rsp
, ret
= 0);
255 ret
= krb5_ret_principal(sp
, &princ
);
257 ret_sp
= krb5_store_int32(rsp
, KADM5_UNK_PRINC
);
260 ret
= krb5_ret_int32(sp
, &mask
);
262 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
266 mask
|= KADM5_PRINCIPAL
;
267 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
268 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
270 /* If the caller doesn't have KADM5_PRIV_GET, we're done. */
271 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_GET
, princ
);
273 ret_sp
= krb5_store_int32(rsp
, ret
);
277 /* Then check to see if it is ok to return keys */
278 if ((mask
& KADM5_KEY_DATA
) != 0) {
279 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_GET_KEYS
,
283 } else if ((mask
== (KADM5_PRINCIPAL
|KADM5_KEY_DATA
)) ||
284 (mask
== (KADM5_PRINCIPAL
|KADM5_KVNO
|KADM5_KEY_DATA
))) {
286 * Requests for keys will get bogus keys, which is useful if
287 * the client just wants to see what (kvno, enctype)s the
288 * principal has keys for, but terrible if the client wants to
289 * write the keys into a keytab or modify the principal and
290 * write the bogus keys back to the server.
292 * We use a heuristic to detect which case we're handling here.
293 * If the client only asks for the flags in the above
294 * condition, then it's very likely a kadmin ext_keytab,
295 * add_enctype, or other request that should not see bogus
296 * keys. We deny them.
298 * The kadmin get command can be coaxed into making a request
299 * with the same mask. But the default long and terse output
300 * modes request other things too, so in all likelihood this
301 * heuristic will not hurt any kadmin get uses.
303 ret_sp
= krb5_store_int32(rsp
, ret
);
308 ret
= kadm5_get_principal(kadm_handlep
, princ
, &ent
, mask
);
309 ret_sp
= krb5_store_int32(rsp
, ret
);
311 if (ret_sp
== 0 && keys_ok
)
312 ret_sp
= kadm5_store_principal_ent(rsp
, &ent
);
313 else if (ret_sp
== 0)
314 ret_sp
= kadm5_store_principal_ent_nokeys(rsp
, &ent
);
316 kadm5_free_principal_ent(kadm_handlep
, &ent
);
322 ret_sp
= krb5_store_int32(rsp
, ret
= KADM5_READ_ONLY
);
325 ret
= krb5_ret_principal(sp
, &princ
);
327 ret
= krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
329 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_DELETE
, princ
);
330 krb5_warnx(contextp
->context
, "%s: %s %s (%s)", client
, op
, name
,
331 ret
== 0 ? "granted" : "denied");
335 * There's no need to check that the caller has permission to
336 * delete the victim principal's aliases.
339 ret
= kadm5_delete_principal(kadm_handlep
, princ
);
340 ret_sp
= krb5_store_int32(rsp
, ret
);
346 ret_sp
= krb5_store_int32(rsp
, ret
= KADM5_READ_ONLY
);
349 ret
= kadm5_ret_principal_ent(sp
, &ent
);
351 ret_sp
= krb5_store_int32(rsp
, ret
);
354 ret
= krb5_ret_int32(sp
, &mask
);
356 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
357 kadm5_free_principal_ent(kadm_handlep
, &ent
);
360 ret
= krb5_ret_string(sp
, &password
);
362 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
363 kadm5_free_principal_ent(kadm_handlep
, &ent
);
366 krb5_unparse_name_fixed(contextp
->context
, ent
.principal
,
368 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
369 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_ADD
,
372 ret_sp
= krb5_store_int32(rsp
, ret
);
373 kadm5_free_principal_ent(kadm_handlep
, &ent
);
376 if ((mask
& KADM5_TL_DATA
)) {
378 * Also check that the caller can create the aliases, if the
379 * new principal has any.
381 ret
= check_aliases(contextp
, &ent
, NULL
);
383 ret_sp
= krb5_store_int32(rsp
, KADM5_BAD_PRINCIPAL
);
384 kadm5_free_principal_ent(kadm_handlep
, &ent
);
388 ret
= kadm5_create_principal(kadm_handlep
, &ent
,
390 kadm5_free_principal_ent(kadm_handlep
, &ent
);
391 ret_sp
= krb5_store_int32(rsp
, ret
);
397 ret_sp
= krb5_store_int32(rsp
, ret
= KADM5_READ_ONLY
);
400 ret
= kadm5_ret_principal_ent(sp
, &ent
);
402 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
405 ret
= krb5_ret_int32(sp
, &mask
);
407 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
408 kadm5_free_principal_ent(contextp
, &ent
);
411 krb5_unparse_name_fixed(contextp
->context
, ent
.principal
,
413 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
414 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_MODIFY
,
417 ret_sp
= krb5_store_int32(rsp
, ret
);
418 kadm5_free_principal_ent(contextp
, &ent
);
421 if ((mask
& KADM5_TL_DATA
)) {
423 * Also check that the caller can create aliases that are in
424 * the new entry but not the old one. There's no need to
425 * check that the caller can delete aliases it wants to
426 * drop. See also handling of rename.
428 ret
= kadm5_get_principal(kadm_handlep
, ent
.principal
, &ent_prev
, mask
);
430 ret_sp
= krb5_store_int32(rsp
, ret
);
431 kadm5_free_principal_ent(contextp
, &ent
);
434 ret
= check_aliases(contextp
, &ent
, &ent_prev
);
435 kadm5_free_principal_ent(contextp
, &ent_prev
);
437 ret_sp
= krb5_store_int32(rsp
, KADM5_BAD_PRINCIPAL
);
438 kadm5_free_principal_ent(contextp
, &ent
);
442 ret
= kadm5_modify_principal(kadm_handlep
, &ent
, mask
);
443 kadm5_free_principal_ent(kadm_handlep
, &ent
);
444 ret_sp
= krb5_store_int32(rsp
, ret
);
450 ret_sp
= krb5_store_int32(rsp
, ret
= KADM5_READ_ONLY
);
453 ret
= krb5_ret_principal(sp
, &princ
);
455 ret
= krb5_ret_int32(sp
, &kvno
);
456 if (ret
== HEIM_ERR_EOF
) {
459 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
462 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
463 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
464 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
466 ret_sp
= krb5_store_int32(rsp
, ret
);
470 ret
= kadm5_prune_principal(kadm_handlep
, princ
, kvno
);
471 ret_sp
= krb5_store_int32(rsp
, ret
);
477 ret_sp
= krb5_store_int32(rsp
, ret
= KADM5_READ_ONLY
);
480 ret
= krb5_ret_principal(sp
, &princ
);
482 ret
= krb5_ret_principal(sp
, &princ2
);
484 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
488 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
489 krb5_unparse_name_fixed(contextp
->context
, princ2
,
490 name2
, sizeof(name2
));
491 krb5_warnx(contextp
->context
, "%s: %s %s -> %s",
492 client
, op
, name
, name2
);
493 ret
= _kadm5_acl_check_permission(contextp
,
498 * Also require modify for the principal. For backwards
499 * compatibility, allow delete permission on the old name to
500 * cure lack of modify permission on the old name.
502 ret
= _kadm5_acl_check_permission(contextp
,
506 ret
= _kadm5_acl_check_permission(contextp
,
512 ret_sp
= krb5_store_int32(rsp
, ret
);
516 ret
= kadm5_rename_principal(kadm_handlep
, princ
, princ2
);
517 ret_sp
= krb5_store_int32(rsp
, ret
);
521 krb5_boolean is_self_cpw
, allow_self_cpw
;
525 ret_sp
= krb5_store_int32(rsp
, ret
= KADM5_READ_ONLY
);
528 ret
= krb5_ret_principal(sp
, &princ
);
530 ret
= krb5_ret_string(sp
, &password
);
532 ret
= krb5_ret_int32(sp
, &keepold
);
533 if (ret
== HEIM_ERR_EOF
)
536 ret
= krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
538 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
541 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
546 * Change password requests are subject to ACLs unless the principal is
547 * changing their own password and the initial ticket flag is set, and
548 * the allow_self_change_password configuration option is TRUE.
551 krb5_principal_compare(contextp
->context
, contextp
->caller
, princ
);
553 krb5_config_get_bool_default(contextp
->context
, NULL
, TRUE
,
554 "kadmin", "allow_self_change_password", NULL
);
555 if (!(is_self_cpw
&& initial
&& allow_self_cpw
)) {
556 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
558 ret_sp
= krb5_store_int32(rsp
, ret
);
563 ret
= kadm5_chpass_principal_3(kadm_handlep
, princ
, keepold
, 0, NULL
,
565 ret_sp
= krb5_store_int32(rsp
, ret
);
568 case kadm_chpass_with_key
:{
570 krb5_key_data
*key_data
;
573 op
= "CHPASS_WITH_KEY";
575 ret_sp
= krb5_store_int32(rsp
, ret
= KADM5_READ_ONLY
);
578 ret
= krb5_ret_principal(sp
, &princ
);
580 ret
= krb5_ret_int32(sp
, &n_key_data
);
582 ret
= krb5_ret_int32(sp
, &keepold
);
583 if (ret
== HEIM_ERR_EOF
)
587 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
591 /* n_key_data will be squeezed into an int16_t below. */
592 if (n_key_data
< 0 || n_key_data
>= 1 << 16 ||
593 (size_t)n_key_data
> UINT_MAX
/sizeof(*key_data
)) {
594 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
599 key_data
= malloc (n_key_data
* sizeof(*key_data
));
600 if (key_data
== NULL
&& n_key_data
!= 0) {
601 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
602 ret
= krb5_enomem(contextp
->context
);
606 for (i
= 0; i
< n_key_data
; ++i
) {
607 ret
= kadm5_ret_key_data (sp
, &key_data
[i
]);
611 kadm5_free_key_data (contextp
, &dummy
, key_data
);
613 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
619 * The change is only allowed if the user is on the CPW ACL,
620 * this it to force password quality check on the user.
623 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
624 ret_sp
= krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
626 krb5_warnx(contextp
->context
, "%s: %s %s (%s)", client
, op
, name
,
627 ret
? "denied" : "granted");
629 int16_t dummy
= n_key_data
;
631 kadm5_free_key_data (contextp
, &dummy
, key_data
);
633 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
636 ret
= kadm5_chpass_principal_with_key_3(kadm_handlep
, princ
, keepold
,
637 n_key_data
, key_data
);
639 int16_t dummy
= n_key_data
;
640 kadm5_free_key_data (contextp
, &dummy
, key_data
);
643 ret_sp
= krb5_store_int32(rsp
, ret
);
651 ret_sp
= krb5_store_int32(rsp
, ret
= KADM5_READ_ONLY
);
654 ret
= krb5_ret_principal(sp
, &princ
);
656 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
659 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
660 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
662 * The change is allowed if at least one of:
663 * a) it's for the principal him/herself and this was an initial ticket
664 * b) the user is on the CPW ACL.
668 && krb5_principal_compare (contextp
->context
, contextp
->caller
,
672 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
675 ret_sp
= krb5_store_int32(rsp
, ret
);
680 * See comments in kadm5_c_randkey_principal() regarding the
683 ret
= krb5_ret_int32(sp
, &keepold
);
684 if (ret
!= 0 && ret
!= HEIM_ERR_EOF
) {
685 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
689 ret
= krb5_ret_int32(sp
, &n_ks_tuple
);
690 if (ret
== HEIM_ERR_EOF
) {
691 const char *enctypes
;
694 enctypes
= krb5_config_get_string(contextp
->context
, NULL
,
696 krb5_principal_get_realm(contextp
->context
,
698 "supported_enctypes", NULL
);
699 if (enctypes
== NULL
|| enctypes
[0] == '\0')
700 enctypes
= "aes128-cts-hmac-sha1-96";
701 ret
= krb5_string_to_keysalts2(contextp
->context
, enctypes
,
706 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
); /* XXX */
710 if (n_ks_tuple
< 0) {
711 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
); /* XXX */
716 if ((ks_tuple
= calloc(n_ks_tuple
, sizeof (*ks_tuple
))) == NULL
) {
717 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
722 for (i
= 0; i
< n_ks_tuple
; i
++) {
723 ret
= krb5_ret_int32(sp
, &ks_tuple
[i
].ks_enctype
);
725 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
729 ret
= krb5_ret_int32(sp
, &ks_tuple
[i
].ks_salttype
);
731 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
736 ret
= kadm5_randkey_principal_3(kadm_handlep
, princ
, keepold
,
737 n_ks_tuple
, ks_tuple
, &new_keys
,
741 ret_sp
= krb5_store_int32(rsp
, ret
);
742 if (ret
== 0 && ret_sp
== 0){
743 ret_sp
= krb5_store_int32(rsp
, n_keys
);
744 for (i
= 0; i
< n_keys
; i
++){
746 ret_sp
= krb5_store_keyblock(rsp
, new_keys
[i
]);
747 krb5_free_keyblock_contents(contextp
->context
, &new_keys
[i
]);
753 case kadm_get_privs
:{
755 ret
= kadm5_get_privs(kadm_handlep
, &privs
);
757 ret_sp
= krb5_store_uint32(rsp
, privs
);
760 case kadm_get_princs
:{
762 ret
= krb5_ret_int32(sp
, &tmp
);
764 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
767 /* See kadm5_c_iter_principals() */
768 if (tmp
== 0x55555555) {
769 /* Want online iteration */
770 ret
= krb5_ret_string(sp
, &expression
);
772 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
775 if (expression
[0] == '\0') {
780 ret
= krb5_ret_string(sp
, &expression
);
782 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
787 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
,
788 expression
? expression
: "*");
789 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_LIST
, NULL
);
791 ret_sp
= krb5_store_int32(rsp
, ret
);
795 if (fd
> -1 && tmp
== 0x55555555) {
796 struct iter_cb_data iter_cbdata
;
800 * The client proposes that we speak the online variation of LIST
801 * by sending a magic value in the int32 that is meant to be a
802 * boolean for "an expression follows". The client must send an
803 * expression in this case because the server might be an old one,
804 * so even if the caller to kadm5_get/iter_principals() passed NULL
805 * for the expression, the client must send something ("*").
807 * The list of principals will be streamed in multiple replies.
809 * The first reply will have just a return code and a negative
810 * count of maximum number of names per-subsequent reply. See
813 * The second reply, third, .., nth replies will have a return code
814 * followed by 50 names, except the last reply must have fewer than
815 * 50 names -zero if need be- so the client can deterministically
816 * notice the end of the stream.
821 n
= krb5_config_get_int_default(contextp
->context
, NULL
, -1,
822 "kadmin", "list_chunk_size", NULL
);
827 if ((iter_cbdata
.rsp
= krb5_storage_emem()) == NULL
) {
828 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
829 ret
= krb5_enomem(contextp
->context
);
832 iter_cbdata
.context
= contextp
->context
;
833 iter_cbdata
.initial
= 1;
834 iter_cbdata
.stop
= 0;
842 * All sending of replies will happen in iter_cb, except for the
843 * final chunk with the final result code.
845 iter_cbdata
.ret
= kadm5_iter_principals(kadm_handlep
, expression
,
846 iter_cb
, &iter_cbdata
);
847 /* Send terminating chunk */
848 iter_cb(&iter_cbdata
, NULL
);
850 ret
= krb5_store_int32(rsp
, iter_cbdata
.ret
);
851 krb5_storage_free(iter_cbdata
.rsp
);
853 ret
= kadm5_get_principals(kadm_handlep
, expression
, &princs
, &n_princs
);
854 ret_sp
= krb5_store_int32(rsp
, ret
);
855 if (ret
== 0 && ret_sp
== 0) {
858 ret_sp
= krb5_store_int32(rsp
, n_princs
);
859 for (i
= 0; ret_sp
== 0 && i
< n_princs
; i
++)
860 ret_sp
= krb5_store_string(rsp
, princs
[i
]);
861 kadm5_free_name_list(kadm_handlep
, princs
, &n_princs
);
868 krb5_warnx(contextp
->context
, "%s: UNKNOWN OP %d", client
, cmd
);
869 ret_sp
= krb5_store_int32(rsp
, KADM5_FAILURE
);
874 if (password
!= NULL
) {
875 len
= strlen(password
);
876 memset_s(password
, len
, 0, len
);
879 krb5_storage_to_data(rsp
, out
);
880 krb5_storage_free(rsp
);
881 krb5_storage_free(sp
);
882 krb5_free_principal(contextp
->context
, princ
);
883 krb5_free_principal(contextp
->context
, princ2
);
885 krb5_warn(contextp
->context
, ret
, "%s", op
);
886 if (out
->length
== 0)
887 krb5_warn(contextp
->context
, ret
, "%s: reply failed", op
);
889 krb5_warn(contextp
->context
, ret
, "%s: reply incomplete", op
);
895 struct iter_aliases_ctx
{
896 HDB_Ext_Aliases aliases
;
903 iter_aliases(kadm5_principal_ent_rec
*from
,
904 struct iter_aliases_ctx
*ctx
,
920 if (ctx
->done
== 0) {
921 if (ctx
->alias_idx
< ctx
->aliases
.aliases
.len
) {
922 *out
= &ctx
->aliases
.aliases
.val
[ctx
->alias_idx
++];
925 /* Out of aliases in this TL, step to next TL */
926 ctx
->tl
= ctx
->tl
->tl_data_next
;
927 } else if (ctx
->done
< 0) {
928 /* Setup iteration context */
929 memset(ctx
, 0, sizeof(*ctx
));
931 ctx
->aliases
.aliases
.val
= NULL
;
932 ctx
->aliases
.aliases
.len
= 0;
933 ctx
->tl
= from
->tl_data
;
936 free_HDB_Ext_Aliases(&ctx
->aliases
);
939 /* Find TL with aliases */
940 for (; ctx
->tl
!= NULL
; ctx
->tl
= ctx
->tl
->tl_data_next
) {
941 if (ctx
->tl
->tl_data_type
!= KRB5_TL_EXTENSION
)
944 ret
= decode_HDB_extension(ctx
->tl
->tl_data_contents
,
945 ctx
->tl
->tl_data_length
,
949 if (ext
.data
.element
== choice_HDB_extension_data_aliases
&&
950 ext
.data
.u
.aliases
.aliases
.len
> 0) {
951 ctx
->aliases
= ext
.data
.u
.aliases
;
954 free_HDB_extension(&ext
);
957 if (ctx
->tl
!= NULL
&& ctx
->aliases
.aliases
.len
> 0) {
958 *out
= &ctx
->aliases
.aliases
.val
[ctx
->alias_idx
++];
967 check_aliases(kadm5_server_context
*contextp
,
968 kadm5_principal_ent_rec
*add_princ
,
969 kadm5_principal_ent_rec
*del_princ
)
972 struct iter_aliases_ctx iter
;
973 struct iter_aliases_ctx iter_del
;
974 krb5_principal new_name
, old_name
;
978 * Yeah, this is O(N^2). Gathering and sorting all the aliases
979 * would be a bit of a pain; if we ever have principals with enough
980 * aliases for this to be a problem, we can fix it then.
982 for (iter
.done
= -1; iter
.done
!= 1;) {
984 ret
= iter_aliases(add_princ
, &iter
, &new_name
);
989 for (iter_del
.done
= -1; iter_del
.done
!= 1;) {
990 ret
= iter_aliases(del_princ
, &iter_del
, &old_name
);
993 if (iter_del
.done
== 1)
995 if (!krb5_principal_compare(contextp
->context
, new_name
, old_name
))
997 free_HDB_Ext_Aliases(&iter_del
.aliases
);
1003 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_ADD
, new_name
);
1005 free_HDB_Ext_Aliases(&iter
.aliases
);
1014 v5_loop (krb5_context contextp
,
1015 krb5_auth_context ac
,
1016 krb5_boolean initial
,
1021 krb5_error_code ret
;
1025 doing_useful_work
= 0;
1028 ret
= krb5_read_priv_message(contextp
, ac
, &fd
, &in
);
1029 if(ret
== HEIM_ERR_EOF
)
1032 krb5_err(contextp
, 1, ret
, "krb5_read_priv_message");
1033 doing_useful_work
= 1;
1034 ret
= kadmind_dispatch(kadm_handlep
, initial
, &in
, ac
, fd
, &out
,
1037 krb5_err(contextp
, 1, ret
, "kadmind_dispatch");
1038 krb5_data_free(&in
);
1040 ret
= krb5_write_priv_message(contextp
, ac
, &fd
, &out
);
1041 krb5_data_free(&out
);
1043 krb5_err(contextp
, 1, ret
, "krb5_write_priv_message");
1048 match_appl_version(const void *data
, const char *appl_version
)
1051 if(sscanf(appl_version
, "KADM0.%u", &minor
) != 1)
1054 *(unsigned*)(intptr_t)data
= minor
;
1059 handle_v5(krb5_context contextp
,
1064 krb5_error_code ret
;
1065 krb5_ticket
*ticket
;
1069 krb5_boolean initial
;
1070 krb5_auth_context ac
= NULL
;
1071 unsigned kadm_version
= 1;
1072 kadm5_config_params realm_params
;
1074 ret
= krb5_recvauth_match_version(contextp
, &ac
, &fd
,
1075 match_appl_version
, &kadm_version
,
1076 NULL
, KRB5_RECVAUTH_IGNORE_VERSION
,
1079 krb5_err(contextp
, 1, ret
, "krb5_recvauth");
1082 ret
= krb5_unparse_name(contextp
, ticket
->server
, &server_name
);
1084 krb5_err(contextp
, 1, ret
, "krb5_unparse_name");
1085 krb5_free_ticket(contextp
, ticket
);
1088 if (strncmp(server_name
, KADM5_ADMIN_SERVICE
,
1089 strlen(KADM5_ADMIN_SERVICE
)) != 0) {
1090 krb5_errx(contextp
, 1, "ticket for strange principal (%s)", server_name
);
1091 krb5_free_ticket(contextp
, ticket
);
1097 memset(&realm_params
, 0, sizeof(realm_params
));
1099 if(kadm_version
== 1) {
1101 ret
= krb5_read_priv_message(contextp
, ac
, &fd
, ¶ms
);
1103 krb5_err(contextp
, 1, ret
, "krb5_read_priv_message");
1104 krb5_free_ticket(contextp
, ticket
);
1107 ret
= _kadm5_unmarshal_params(contextp
, ¶ms
, &realm_params
);
1109 krb5_err(contextp
, 1, ret
,
1110 "Could not read or parse kadm5 parameters");
1111 krb5_free_ticket(contextp
, ticket
);
1116 initial
= ticket
->ticket
.flags
.initial
;
1117 ret
= krb5_unparse_name(contextp
, ticket
->client
, &client
);
1118 krb5_free_ticket(contextp
, ticket
);
1120 krb5_err(contextp
, 1, ret
, "krb5_unparse_name");
1123 ret
= kadm5_s_init_with_password_ctx(contextp
,
1126 KADM5_ADMIN_SERVICE
,
1131 krb5_err(contextp
, 1, ret
, "kadm5_init_with_password_ctx");
1134 v5_loop(contextp
, ac
, initial
, kadm_handlep
, fd
, readonly
);
1138 kadmind_loop(krb5_context contextp
,
1143 u_char buf
[sizeof(KRB5_SENDAUTH_VERSION
) + 4];
1147 n
= krb5_net_read(contextp
, &sock
, buf
, 4);
1151 krb5_err(contextp
, 1, errno
, "read");
1152 _krb5_get_int(buf
, &len
, 4);
1154 if (len
== sizeof(KRB5_SENDAUTH_VERSION
)) {
1156 n
= krb5_net_read(contextp
, &sock
, buf
+ 4, len
);
1158 krb5_err (contextp
, 1, errno
, "reading sendauth version");
1160 krb5_errx (contextp
, 1, "EOF reading sendauth version");
1162 if(memcmp(buf
+ 4, KRB5_SENDAUTH_VERSION
, len
) == 0) {
1163 handle_v5(contextp
, keytab
, sock
, readonly
);
1170 handle_mit(contextp
, buf
, len
, sock
, readonly
);