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 __RCSID("$Heimdal: server.c 17611 2006-06-02 22:10:21Z lha $"
41 kadmind_dispatch(void *kadm_handle
, krb5_boolean initial
,
42 krb5_data
*in
, krb5_data
*out
)
45 int32_t cmd
, mask
, tmp
;
46 kadm5_server_context
*context
= kadm_handle
;
47 char client
[128], name
[128], name2
[128];
49 krb5_principal princ
, princ2
;
50 kadm5_principal_ent_rec ent
;
51 char *password
, *expression
;
52 krb5_keyblock
*new_keys
;
58 krb5_unparse_name_fixed(context
->context
, context
->caller
,
59 client
, sizeof(client
));
61 sp
= krb5_storage_from_data(in
);
63 krb5_ret_int32(sp
, &cmd
);
67 ret
= krb5_ret_principal(sp
, &princ
);
70 ret
= krb5_ret_int32(sp
, &mask
);
72 krb5_free_principal(context
->context
, princ
);
75 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
76 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
77 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_GET
, princ
);
79 krb5_free_principal(context
->context
, princ
);
82 ret
= kadm5_get_principal(kadm_handle
, princ
, &ent
, mask
);
83 krb5_storage_free(sp
);
84 sp
= krb5_storage_emem();
85 krb5_store_int32(sp
, ret
);
87 kadm5_store_principal_ent(sp
, &ent
);
88 kadm5_free_principal_ent(kadm_handle
, &ent
);
90 krb5_free_principal(context
->context
, princ
);
95 ret
= krb5_ret_principal(sp
, &princ
);
98 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
99 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
100 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_DELETE
, princ
);
102 krb5_free_principal(context
->context
, princ
);
105 ret
= kadm5_delete_principal(kadm_handle
, princ
);
106 krb5_free_principal(context
->context
, princ
);
107 krb5_storage_free(sp
);
108 sp
= krb5_storage_emem();
109 krb5_store_int32(sp
, ret
);
114 ret
= kadm5_ret_principal_ent(sp
, &ent
);
117 ret
= krb5_ret_int32(sp
, &mask
);
119 kadm5_free_principal_ent(context
->context
, &ent
);
122 ret
= krb5_ret_string(sp
, &password
);
124 kadm5_free_principal_ent(context
->context
, &ent
);
127 krb5_unparse_name_fixed(context
->context
, ent
.principal
,
129 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
130 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_ADD
,
133 kadm5_free_principal_ent(context
->context
, &ent
);
134 memset(password
, 0, strlen(password
));
138 ret
= kadm5_create_principal(kadm_handle
, &ent
,
140 kadm5_free_principal_ent(kadm_handle
, &ent
);
141 memset(password
, 0, strlen(password
));
143 krb5_storage_free(sp
);
144 sp
= krb5_storage_emem();
145 krb5_store_int32(sp
, ret
);
150 ret
= kadm5_ret_principal_ent(sp
, &ent
);
153 ret
= krb5_ret_int32(sp
, &mask
);
155 kadm5_free_principal_ent(context
, &ent
);
158 krb5_unparse_name_fixed(context
->context
, ent
.principal
,
160 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
161 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_MODIFY
,
164 kadm5_free_principal_ent(context
, &ent
);
167 ret
= kadm5_modify_principal(kadm_handle
, &ent
, mask
);
168 kadm5_free_principal_ent(kadm_handle
, &ent
);
169 krb5_storage_free(sp
);
170 sp
= krb5_storage_emem();
171 krb5_store_int32(sp
, ret
);
176 ret
= krb5_ret_principal(sp
, &princ
);
179 ret
= krb5_ret_principal(sp
, &princ2
);
181 krb5_free_principal(context
->context
, princ
);
184 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
185 krb5_unparse_name_fixed(context
->context
, princ2
, name2
, sizeof(name2
));
186 krb5_warnx(context
->context
, "%s: %s %s -> %s",
187 client
, op
, name
, name2
);
188 ret
= _kadm5_acl_check_permission(context
,
191 || _kadm5_acl_check_permission(context
,
195 krb5_free_principal(context
->context
, princ
);
196 krb5_free_principal(context
->context
, princ2
);
199 ret
= kadm5_rename_principal(kadm_handle
, princ
, princ2
);
200 krb5_free_principal(context
->context
, princ
);
201 krb5_free_principal(context
->context
, princ2
);
202 krb5_storage_free(sp
);
203 sp
= krb5_storage_emem();
204 krb5_store_int32(sp
, ret
);
209 ret
= krb5_ret_principal(sp
, &princ
);
212 ret
= krb5_ret_string(sp
, &password
);
214 krb5_free_principal(context
->context
, princ
);
217 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
218 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
221 * The change is allowed if at least one of:
223 * a) it's for the principal him/herself and this was an
224 * initial ticket, but then, check with the password quality
226 * b) the user is on the CPW ACL.
230 && krb5_principal_compare (context
->context
, context
->caller
,
234 const char *pwd_reason
;
236 pwd_data
.data
= password
;
237 pwd_data
.length
= strlen(password
);
239 pwd_reason
= kadm5_check_password_quality (context
->context
,
241 if (pwd_reason
!= NULL
)
242 ret
= KADM5_PASS_Q_DICT
;
246 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
249 krb5_free_principal(context
->context
, princ
);
250 memset(password
, 0, strlen(password
));
254 ret
= kadm5_chpass_principal(kadm_handle
, princ
, password
);
255 krb5_free_principal(context
->context
, princ
);
256 memset(password
, 0, strlen(password
));
258 krb5_storage_free(sp
);
259 sp
= krb5_storage_emem();
260 krb5_store_int32(sp
, ret
);
263 case kadm_chpass_with_key
:{
265 krb5_key_data
*key_data
;
268 op
= "CHPASS_WITH_KEY";
269 ret
= krb5_ret_principal(sp
, &princ
);
272 ret
= krb5_ret_int32(sp
, &n_key_data
);
274 krb5_free_principal(context
->context
, princ
);
277 /* n_key_data will be squeezed into an int16_t below. */
278 if (n_key_data
< 0 || n_key_data
>= 1 << 16 ||
279 n_key_data
> UINT_MAX
/sizeof(*key_data
)) {
281 krb5_free_principal(context
->context
, princ
);
285 key_data
= malloc (n_key_data
* sizeof(*key_data
));
286 if (key_data
== NULL
) {
288 krb5_free_principal(context
->context
, princ
);
292 for (i
= 0; i
< n_key_data
; ++i
) {
293 ret
= kadm5_ret_key_data (sp
, &key_data
[i
]);
297 kadm5_free_key_data (context
, &dummy
, key_data
);
299 krb5_free_principal(context
->context
, princ
);
304 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
305 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
308 * The change is only allowed if the user is on the CPW ACL,
309 * this it to force password quality check on the user.
312 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
314 int16_t dummy
= n_key_data
;
316 kadm5_free_key_data (context
, &dummy
, key_data
);
318 krb5_free_principal(context
->context
, princ
);
321 ret
= kadm5_chpass_principal_with_key(kadm_handle
, princ
,
322 n_key_data
, key_data
);
324 int16_t dummy
= n_key_data
;
325 kadm5_free_key_data (context
, &dummy
, key_data
);
328 krb5_free_principal(context
->context
, princ
);
329 krb5_storage_free(sp
);
330 sp
= krb5_storage_emem();
331 krb5_store_int32(sp
, ret
);
336 ret
= krb5_ret_principal(sp
, &princ
);
339 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
340 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
342 * The change is allowed if at least one of:
343 * a) it's for the principal him/herself and this was an initial ticket
344 * b) the user is on the CPW ACL.
348 && krb5_principal_compare (context
->context
, context
->caller
,
352 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
355 krb5_free_principal(context
->context
, princ
);
358 ret
= kadm5_randkey_principal(kadm_handle
, princ
,
360 krb5_free_principal(context
->context
, princ
);
361 krb5_storage_free(sp
);
362 sp
= krb5_storage_emem();
363 krb5_store_int32(sp
, ret
);
366 krb5_store_int32(sp
, n_keys
);
367 for(i
= 0; i
< n_keys
; i
++){
368 krb5_store_keyblock(sp
, new_keys
[i
]);
369 krb5_free_keyblock_contents(context
->context
, &new_keys
[i
]);
374 case kadm_get_privs
:{
376 ret
= kadm5_get_privs(kadm_handle
, &privs
);
377 krb5_storage_free(sp
);
378 sp
= krb5_storage_emem();
379 krb5_store_int32(sp
, ret
);
381 krb5_store_uint32(sp
, privs
);
384 case kadm_get_princs
:{
386 ret
= krb5_ret_int32(sp
, &tmp
);
390 ret
= krb5_ret_string(sp
, &expression
);
395 krb5_warnx(context
->context
, "%s: %s %s", client
, op
,
396 expression
? expression
: "*");
397 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_LIST
, NULL
);
402 ret
= kadm5_get_principals(kadm_handle
, expression
, &princs
, &n_princs
);
404 krb5_storage_free(sp
);
405 sp
= krb5_storage_emem();
406 krb5_store_int32(sp
, ret
);
409 krb5_store_int32(sp
, n_princs
);
410 for(i
= 0; i
< n_princs
; i
++)
411 krb5_store_string(sp
, princs
[i
]);
412 kadm5_free_name_list(kadm_handle
, princs
, &n_princs
);
417 krb5_warnx(context
->context
, "%s: UNKNOWN OP %d", client
, cmd
);
418 krb5_storage_free(sp
);
419 sp
= krb5_storage_emem();
420 krb5_store_int32(sp
, KADM5_FAILURE
);
423 krb5_storage_to_data(sp
, out
);
424 krb5_storage_free(sp
);
427 krb5_warn(context
->context
, ret
, "%s", op
);
428 krb5_storage_seek(sp
, 0, SEEK_SET
);
429 krb5_store_int32(sp
, ret
);
430 krb5_storage_to_data(sp
, out
);
431 krb5_storage_free(sp
);
436 v5_loop (krb5_context context
,
437 krb5_auth_context ac
,
438 krb5_boolean initial
,
446 doing_useful_work
= 0;
449 ret
= krb5_read_priv_message(context
, ac
, &fd
, &in
);
450 if(ret
== HEIM_ERR_EOF
)
453 krb5_err(context
, 1, ret
, "krb5_read_priv_message");
454 doing_useful_work
= 1;
455 kadmind_dispatch(kadm_handle
, initial
, &in
, &out
);
457 ret
= krb5_write_priv_message(context
, ac
, &fd
, &out
);
459 krb5_err(context
, 1, ret
, "krb5_write_priv_message");
464 match_appl_version(const void *data
, const char *appl_version
)
467 if(sscanf(appl_version
, "KADM0.%u", &minor
) != 1)
469 *(unsigned*)data
= minor
;
474 handle_v5(krb5_context context
,
475 krb5_auth_context ac
,
481 u_char version
[sizeof(KRB5_SENDAUTH_VERSION
)];
487 krb5_boolean initial
;
489 unsigned kadm_version
;
490 kadm5_config_params realm_params
;
492 if (len
!= sizeof(KRB5_SENDAUTH_VERSION
))
493 krb5_errx(context
, 1, "bad sendauth len %d", len
);
494 n
= krb5_net_read(context
, &fd
, version
, len
);
496 krb5_err (context
, 1, errno
, "reading sendauth version");
498 krb5_errx (context
, 1, "EOF reading sendauth version");
499 if(memcmp(version
, KRB5_SENDAUTH_VERSION
, len
) != 0)
500 krb5_errx(context
, 1, "bad sendauth version %.8s", version
);
502 ret
= krb5_recvauth_match_version(context
, &ac
, &fd
,
503 match_appl_version
, &kadm_version
,
504 NULL
, KRB5_RECVAUTH_IGNORE_VERSION
,
506 if(ret
== KRB5_KT_NOTFOUND
)
507 krb5_errx(context
, 1, "krb5_recvauth: key not found");
509 krb5_err(context
, 1, ret
, "krb5_recvauth");
511 ret
= krb5_unparse_name (context
, ticket
->server
, &server_name
);
513 krb5_err (context
, 1, ret
, "krb5_unparse_name");
515 if (strncmp (server_name
, KADM5_ADMIN_SERVICE
,
516 strlen(KADM5_ADMIN_SERVICE
)) != 0)
517 krb5_errx (context
, 1, "ticket for strange principal (%s)",
522 memset(&realm_params
, 0, sizeof(realm_params
));
524 if(kadm_version
== 1) {
526 ret
= krb5_read_priv_message(context
, ac
, &fd
, ¶ms
);
528 krb5_err(context
, 1, ret
, "krb5_read_priv_message");
529 _kadm5_unmarshal_params(context
, ¶ms
, &realm_params
);
532 initial
= ticket
->ticket
.flags
.initial
;
533 ret
= krb5_unparse_name(context
, ticket
->client
, &client
);
535 krb5_err (context
, 1, ret
, "krb5_unparse_name");
536 krb5_free_ticket (context
, ticket
);
537 ret
= kadm5_init_with_password_ctx(context
,
545 krb5_err (context
, 1, ret
, "kadm5_init_with_password_ctx");
546 v5_loop (context
, ac
, initial
, kadm_handle
, fd
);
550 kadmind_loop(krb5_context context
,
551 krb5_auth_context ac
,
555 unsigned char tmp
[4];
559 n
= krb5_net_read(context
, &fd
, tmp
, 4);
563 krb5_err(context
, 1, errno
, "read");
564 _krb5_get_int(tmp
, &len
, 4);
565 /* this v4 test could probably also go away */
566 if(len
> 0xffff && (len
& 0xffff) == ('K' << 8) + 'A') {
567 unsigned char v4reply
[] = {
569 'K', 'Y', 'O', 'U', 'L', 'O', 'S', 'E',
570 0x95, 0xb7, 0xa7, 0x08 /* KADM_BAD_VER */
572 krb5_net_write(context
, &fd
, v4reply
, sizeof(v4reply
));
573 krb5_errx(context
, 1, "packet appears to be version 4");
575 handle_v5(context
, ac
, keytab
, len
, fd
);