2 * Copyright (c) 1997 - 2004 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
, princ
);
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
, princ
);
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
,
132 kadm5_free_principal_ent(context
->context
, &ent
);
133 memset(password
, 0, strlen(password
));
137 ret
= kadm5_create_principal(kadm_handle
, &ent
,
139 kadm5_free_principal_ent(kadm_handle
, &ent
);
140 memset(password
, 0, strlen(password
));
142 krb5_storage_free(sp
);
143 sp
= krb5_storage_emem();
144 krb5_store_int32(sp
, ret
);
149 ret
= kadm5_ret_principal_ent(sp
, &ent
);
152 ret
= krb5_ret_int32(sp
, &mask
);
154 kadm5_free_principal_ent(context
, &ent
);
157 krb5_unparse_name_fixed(context
->context
, ent
.principal
,
159 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
160 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_MODIFY
,
163 kadm5_free_principal_ent(context
, &ent
);
166 ret
= kadm5_modify_principal(kadm_handle
, &ent
, mask
);
167 kadm5_free_principal_ent(kadm_handle
, &ent
);
168 krb5_storage_free(sp
);
169 sp
= krb5_storage_emem();
170 krb5_store_int32(sp
, ret
);
175 ret
= krb5_ret_principal(sp
, &princ
);
178 ret
= krb5_ret_principal(sp
, &princ2
);
180 krb5_free_principal(context
->context
, princ
);
183 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
184 krb5_unparse_name_fixed(context
->context
, princ2
, name2
, sizeof(name2
));
185 krb5_warnx(context
->context
, "%s: %s %s -> %s",
186 client
, op
, name
, name2
);
187 ret
= _kadm5_acl_check_permission(context
,
190 || _kadm5_acl_check_permission(context
,
194 krb5_free_principal(context
->context
, princ
);
197 ret
= kadm5_rename_principal(kadm_handle
, princ
, princ2
);
198 krb5_free_principal(context
->context
, princ
);
199 krb5_free_principal(context
->context
, princ2
);
200 krb5_storage_free(sp
);
201 sp
= krb5_storage_emem();
202 krb5_store_int32(sp
, ret
);
207 ret
= krb5_ret_principal(sp
, &princ
);
210 ret
= krb5_ret_string(sp
, &password
);
212 krb5_free_principal(context
->context
, princ
);
215 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
216 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
219 * The change is allowed if at least one of:
221 * a) it's for the principal him/herself and this was an
222 * initial ticket, but then, check with the password quality
224 * b) the user is on the CPW ACL.
228 && krb5_principal_compare (context
->context
, context
->caller
,
232 const char *pwd_reason
;
234 pwd_data
.data
= password
;
235 pwd_data
.length
= strlen(password
);
237 pwd_reason
= kadm5_check_password_quality (context
->context
,
239 if (pwd_reason
!= NULL
)
240 ret
= KADM5_PASS_Q_DICT
;
244 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
247 krb5_free_principal(context
->context
, princ
);
248 memset(password
, 0, strlen(password
));
252 ret
= kadm5_chpass_principal(kadm_handle
, princ
, password
);
253 krb5_free_principal(context
->context
, princ
);
254 memset(password
, 0, strlen(password
));
256 krb5_storage_free(sp
);
257 sp
= krb5_storage_emem();
258 krb5_store_int32(sp
, ret
);
261 case kadm_chpass_with_key
:{
263 krb5_key_data
*key_data
;
266 op
= "CHPASS_WITH_KEY";
267 ret
= krb5_ret_principal(sp
, &princ
);
270 ret
= krb5_ret_int32(sp
, &n_key_data
);
272 krb5_free_principal(context
->context
, princ
);
275 /* n_key_data will be squeezed into an int16_t below. */
276 if (n_key_data
< 0 || n_key_data
>= 1 << 16 ||
277 n_key_data
> UINT_MAX
/sizeof(*key_data
)) {
279 krb5_free_principal(context
->context
, princ
);
283 key_data
= malloc (n_key_data
* sizeof(*key_data
));
284 if (key_data
== NULL
) {
286 krb5_free_principal(context
->context
, princ
);
290 for (i
= 0; i
< n_key_data
; ++i
) {
291 ret
= kadm5_ret_key_data (sp
, &key_data
[i
]);
295 kadm5_free_key_data (context
, &dummy
, key_data
);
297 krb5_free_principal(context
->context
, princ
);
302 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
303 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
306 * The change is only allowed if the user is on the CPW ACL,
307 * this it to force password quality check on the user.
310 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
312 int16_t dummy
= n_key_data
;
314 kadm5_free_key_data (context
, &dummy
, key_data
);
316 krb5_free_principal(context
->context
, princ
);
319 ret
= kadm5_chpass_principal_with_key(kadm_handle
, princ
,
320 n_key_data
, key_data
);
322 int16_t dummy
= n_key_data
;
323 kadm5_free_key_data (context
, &dummy
, key_data
);
326 krb5_free_principal(context
->context
, princ
);
327 krb5_storage_free(sp
);
328 sp
= krb5_storage_emem();
329 krb5_store_int32(sp
, ret
);
334 ret
= krb5_ret_principal(sp
, &princ
);
337 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
338 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
340 * The change is allowed if at least one of:
341 * a) it's for the principal him/herself and this was an initial ticket
342 * b) the user is on the CPW ACL.
346 && krb5_principal_compare (context
->context
, context
->caller
,
350 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
353 krb5_free_principal(context
->context
, princ
);
356 ret
= kadm5_randkey_principal(kadm_handle
, princ
,
358 krb5_free_principal(context
->context
, princ
);
359 krb5_storage_free(sp
);
360 sp
= krb5_storage_emem();
361 krb5_store_int32(sp
, ret
);
364 krb5_store_int32(sp
, n_keys
);
365 for(i
= 0; i
< n_keys
; i
++){
366 krb5_store_keyblock(sp
, new_keys
[i
]);
367 krb5_free_keyblock_contents(context
->context
, &new_keys
[i
]);
372 case kadm_get_privs
:{
373 ret
= kadm5_get_privs(kadm_handle
, &mask
);
374 krb5_storage_free(sp
);
375 sp
= krb5_storage_emem();
376 krb5_store_int32(sp
, ret
);
378 krb5_store_int32(sp
, mask
);
381 case kadm_get_princs
:{
383 ret
= krb5_ret_int32(sp
, &tmp
);
387 ret
= krb5_ret_string(sp
, &exp
);
392 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, exp
? exp
: "*");
393 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_LIST
, NULL
);
398 ret
= kadm5_get_principals(kadm_handle
, exp
, &princs
, &n_princs
);
400 krb5_storage_free(sp
);
401 sp
= krb5_storage_emem();
402 krb5_store_int32(sp
, ret
);
405 krb5_store_int32(sp
, n_princs
);
406 for(i
= 0; i
< n_princs
; i
++)
407 krb5_store_string(sp
, princs
[i
]);
408 kadm5_free_name_list(kadm_handle
, princs
, &n_princs
);
413 krb5_warnx(context
->context
, "%s: UNKNOWN OP %d", client
, cmd
);
414 krb5_storage_free(sp
);
415 sp
= krb5_storage_emem();
416 krb5_store_int32(sp
, KADM5_FAILURE
);
419 krb5_storage_to_data(sp
, out
);
420 krb5_storage_free(sp
);
423 krb5_warn(context
->context
, ret
, "%s", op
);
424 krb5_storage_seek(sp
, 0, SEEK_SET
);
425 krb5_store_int32(sp
, ret
);
426 krb5_storage_to_data(sp
, out
);
427 krb5_storage_free(sp
);
432 v5_loop (krb5_context context
,
433 krb5_auth_context ac
,
434 krb5_boolean initial
,
442 doing_useful_work
= 0;
445 ret
= krb5_read_priv_message(context
, ac
, &fd
, &in
);
446 if(ret
== HEIM_ERR_EOF
)
449 krb5_err(context
, 1, ret
, "krb5_read_priv_message");
450 doing_useful_work
= 1;
451 kadmind_dispatch(kadm_handle
, initial
, &in
, &out
);
453 ret
= krb5_write_priv_message(context
, ac
, &fd
, &out
);
455 krb5_err(context
, 1, ret
, "krb5_write_priv_message");
460 match_appl_version(const void *data
, const char *appl_version
)
463 if(sscanf(appl_version
, "KADM0.%u", &minor
) != 1)
465 *(unsigned*)data
= minor
;
470 handle_v5(krb5_context context
,
471 krb5_auth_context ac
,
477 u_char version
[sizeof(KRB5_SENDAUTH_VERSION
)];
483 krb5_boolean initial
;
485 unsigned kadm_version
;
486 kadm5_config_params realm_params
;
488 if (len
!= sizeof(KRB5_SENDAUTH_VERSION
))
489 krb5_errx(context
, 1, "bad sendauth len %d", len
);
490 n
= krb5_net_read(context
, &fd
, version
, len
);
492 krb5_err (context
, 1, errno
, "reading sendauth version");
494 krb5_errx (context
, 1, "EOF reading sendauth version");
495 if(memcmp(version
, KRB5_SENDAUTH_VERSION
, len
) != 0)
496 krb5_errx(context
, 1, "bad sendauth version %.8s", version
);
498 ret
= krb5_recvauth_match_version(context
, &ac
, &fd
,
499 match_appl_version
, &kadm_version
,
500 NULL
, KRB5_RECVAUTH_IGNORE_VERSION
,
502 if(ret
== KRB5_KT_NOTFOUND
)
503 krb5_errx(context
, 1, "krb5_recvauth: key not found");
505 krb5_err(context
, 1, ret
, "krb5_recvauth");
507 ret
= krb5_unparse_name (context
, ticket
->server
, &server_name
);
509 krb5_err (context
, 1, ret
, "krb5_unparse_name");
511 if (strncmp (server_name
, KADM5_ADMIN_SERVICE
,
512 strlen(KADM5_ADMIN_SERVICE
)) != 0)
513 krb5_errx (context
, 1, "ticket for strange principal (%s)",
518 memset(&realm_params
, 0, sizeof(realm_params
));
520 if(kadm_version
== 1) {
522 ret
= krb5_read_priv_message(context
, ac
, &fd
, ¶ms
);
524 krb5_err(context
, 1, ret
, "krb5_read_priv_message");
525 _kadm5_unmarshal_params(context
, ¶ms
, &realm_params
);
528 initial
= ticket
->ticket
.flags
.initial
;
529 ret
= krb5_unparse_name(context
, ticket
->client
, &client
);
531 krb5_err (context
, 1, ret
, "krb5_unparse_name");
532 krb5_free_ticket (context
, ticket
);
533 ret
= kadm5_init_with_password_ctx(context
,
541 krb5_err (context
, 1, ret
, "kadm5_init_with_password_ctx");
542 v5_loop (context
, ac
, initial
, kadm_handle
, fd
);
546 kadmind_loop(krb5_context context
,
547 krb5_auth_context ac
,
551 unsigned char tmp
[4];
555 n
= krb5_net_read(context
, &fd
, tmp
, 4);
559 krb5_err(context
, 1, errno
, "read");
560 _krb5_get_int(tmp
, &len
, 4);
561 /* this v4 test could probably also go away */
562 if(len
> 0xffff && (len
& 0xffff) == ('K' << 8) + 'A') {
563 unsigned char v4reply
[] = {
565 'K', 'Y', 'O', 'U', 'L', 'O', 'S', 'E',
566 0x95, 0xb7, 0xa7, 0x08 /* KADM_BAD_VER */
568 krb5_net_write(context
, &fd
, v4reply
, sizeof(v4reply
));
569 krb5_errx(context
, 1, "packet appears to be version 4");
571 handle_v5(context
, ac
, keytab
, len
, fd
);