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>
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
;
50 char *password
, *expression
;
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_errx(context
->context
, 1, "out of memory");
64 krb5_ret_int32(sp
, &cmd
);
68 ret
= krb5_ret_principal(sp
, &princ
);
71 ret
= krb5_ret_int32(sp
, &mask
);
73 krb5_free_principal(context
->context
, princ
);
76 mask
|= KADM5_PRINCIPAL
;
77 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
78 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
79 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_GET
, princ
);
81 krb5_free_principal(context
->context
, princ
);
84 ret
= kadm5_get_principal(kadm_handle
, princ
, &ent
, mask
);
85 krb5_storage_free(sp
);
86 sp
= krb5_storage_emem();
87 krb5_store_int32(sp
, ret
);
89 kadm5_store_principal_ent(sp
, &ent
);
90 kadm5_free_principal_ent(kadm_handle
, &ent
);
92 krb5_free_principal(context
->context
, princ
);
97 ret
= krb5_ret_principal(sp
, &princ
);
100 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
101 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
102 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_DELETE
, princ
);
104 krb5_free_principal(context
->context
, princ
);
107 ret
= kadm5_delete_principal(kadm_handle
, princ
);
108 krb5_free_principal(context
->context
, princ
);
109 krb5_storage_free(sp
);
110 sp
= krb5_storage_emem();
111 krb5_store_int32(sp
, ret
);
116 ret
= kadm5_ret_principal_ent(sp
, &ent
);
119 ret
= krb5_ret_int32(sp
, &mask
);
121 kadm5_free_principal_ent(context
->context
, &ent
);
124 ret
= krb5_ret_string(sp
, &password
);
126 kadm5_free_principal_ent(context
->context
, &ent
);
129 krb5_unparse_name_fixed(context
->context
, ent
.principal
,
131 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
132 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_ADD
,
135 kadm5_free_principal_ent(context
->context
, &ent
);
136 memset(password
, 0, strlen(password
));
140 ret
= kadm5_create_principal(kadm_handle
, &ent
,
142 kadm5_free_principal_ent(kadm_handle
, &ent
);
143 memset(password
, 0, strlen(password
));
145 krb5_storage_free(sp
);
146 sp
= krb5_storage_emem();
147 krb5_store_int32(sp
, ret
);
152 ret
= kadm5_ret_principal_ent(sp
, &ent
);
155 ret
= krb5_ret_int32(sp
, &mask
);
157 kadm5_free_principal_ent(context
, &ent
);
160 krb5_unparse_name_fixed(context
->context
, ent
.principal
,
162 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
163 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_MODIFY
,
166 kadm5_free_principal_ent(context
, &ent
);
169 ret
= kadm5_modify_principal(kadm_handle
, &ent
, mask
);
170 kadm5_free_principal_ent(kadm_handle
, &ent
);
171 krb5_storage_free(sp
);
172 sp
= krb5_storage_emem();
173 krb5_store_int32(sp
, ret
);
178 ret
= krb5_ret_principal(sp
, &princ
);
181 ret
= krb5_ret_principal(sp
, &princ2
);
183 krb5_free_principal(context
->context
, princ
);
186 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
187 krb5_unparse_name_fixed(context
->context
, princ2
, name2
, sizeof(name2
));
188 krb5_warnx(context
->context
, "%s: %s %s -> %s",
189 client
, op
, name
, name2
);
190 ret
= _kadm5_acl_check_permission(context
,
193 || _kadm5_acl_check_permission(context
,
197 krb5_free_principal(context
->context
, princ
);
198 krb5_free_principal(context
->context
, princ2
);
201 ret
= kadm5_rename_principal(kadm_handle
, princ
, princ2
);
202 krb5_free_principal(context
->context
, princ
);
203 krb5_free_principal(context
->context
, princ2
);
204 krb5_storage_free(sp
);
205 sp
= krb5_storage_emem();
206 krb5_store_int32(sp
, ret
);
211 ret
= krb5_ret_principal(sp
, &princ
);
214 ret
= krb5_ret_string(sp
, &password
);
216 krb5_free_principal(context
->context
, princ
);
219 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
220 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
223 * The change is allowed if at least one of:
225 * a) it's for the principal him/herself and this was an
226 * initial ticket, but then, check with the password quality
228 * b) the user is on the CPW ACL.
232 && krb5_principal_compare (context
->context
, context
->caller
,
236 const char *pwd_reason
;
238 pwd_data
.data
= password
;
239 pwd_data
.length
= strlen(password
);
241 pwd_reason
= kadm5_check_password_quality (context
->context
,
243 if (pwd_reason
!= NULL
)
244 ret
= KADM5_PASS_Q_DICT
;
248 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
251 krb5_free_principal(context
->context
, princ
);
252 memset(password
, 0, strlen(password
));
256 ret
= kadm5_chpass_principal(kadm_handle
, princ
, password
);
257 krb5_free_principal(context
->context
, princ
);
258 memset(password
, 0, strlen(password
));
260 krb5_storage_free(sp
);
261 sp
= krb5_storage_emem();
262 krb5_store_int32(sp
, ret
);
265 case kadm_chpass_with_key
:{
267 krb5_key_data
*key_data
;
270 op
= "CHPASS_WITH_KEY";
271 ret
= krb5_ret_principal(sp
, &princ
);
274 ret
= krb5_ret_int32(sp
, &n_key_data
);
276 krb5_free_principal(context
->context
, princ
);
279 /* n_key_data will be squeezed into an int16_t below. */
280 if (n_key_data
< 0 || n_key_data
>= 1 << 16 ||
281 n_key_data
> UINT_MAX
/sizeof(*key_data
)) {
283 krb5_free_principal(context
->context
, princ
);
287 key_data
= malloc (n_key_data
* sizeof(*key_data
));
288 if (key_data
== NULL
) {
290 krb5_free_principal(context
->context
, princ
);
294 for (i
= 0; i
< n_key_data
; ++i
) {
295 ret
= kadm5_ret_key_data (sp
, &key_data
[i
]);
299 kadm5_free_key_data (context
, &dummy
, key_data
);
301 krb5_free_principal(context
->context
, princ
);
306 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
307 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
310 * The change is only allowed if the user is on the CPW ACL,
311 * this it to force password quality check on the user.
314 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
316 int16_t dummy
= n_key_data
;
318 kadm5_free_key_data (context
, &dummy
, key_data
);
320 krb5_free_principal(context
->context
, princ
);
323 ret
= kadm5_chpass_principal_with_key(kadm_handle
, princ
,
324 n_key_data
, key_data
);
326 int16_t dummy
= n_key_data
;
327 kadm5_free_key_data (context
, &dummy
, key_data
);
330 krb5_free_principal(context
->context
, princ
);
331 krb5_storage_free(sp
);
332 sp
= krb5_storage_emem();
333 krb5_store_int32(sp
, ret
);
338 ret
= krb5_ret_principal(sp
, &princ
);
341 krb5_unparse_name_fixed(context
->context
, princ
, name
, sizeof(name
));
342 krb5_warnx(context
->context
, "%s: %s %s", client
, op
, name
);
344 * The change is allowed if at least one of:
345 * a) it's for the principal him/herself and this was an initial ticket
346 * b) the user is on the CPW ACL.
350 && krb5_principal_compare (context
->context
, context
->caller
,
354 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_CPW
, princ
);
357 krb5_free_principal(context
->context
, princ
);
360 ret
= kadm5_randkey_principal(kadm_handle
, princ
,
362 krb5_free_principal(context
->context
, princ
);
363 krb5_storage_free(sp
);
364 sp
= krb5_storage_emem();
365 krb5_store_int32(sp
, ret
);
368 krb5_store_int32(sp
, n_keys
);
369 for(i
= 0; i
< n_keys
; i
++){
370 krb5_store_keyblock(sp
, new_keys
[i
]);
371 krb5_free_keyblock_contents(context
->context
, &new_keys
[i
]);
377 case kadm_get_privs
:{
379 ret
= kadm5_get_privs(kadm_handle
, &privs
);
380 krb5_storage_free(sp
);
381 sp
= krb5_storage_emem();
382 krb5_store_int32(sp
, ret
);
384 krb5_store_uint32(sp
, privs
);
387 case kadm_get_princs
:{
389 ret
= krb5_ret_int32(sp
, &tmp
);
393 ret
= krb5_ret_string(sp
, &expression
);
398 krb5_warnx(context
->context
, "%s: %s %s", client
, op
,
399 expression
? expression
: "*");
400 ret
= _kadm5_acl_check_permission(context
, KADM5_PRIV_LIST
, NULL
);
405 ret
= kadm5_get_principals(kadm_handle
, expression
, &princs
, &n_princs
);
407 krb5_storage_free(sp
);
408 sp
= krb5_storage_emem();
409 krb5_store_int32(sp
, ret
);
412 krb5_store_int32(sp
, n_princs
);
413 for(i
= 0; i
< n_princs
; i
++)
414 krb5_store_string(sp
, princs
[i
]);
415 kadm5_free_name_list(kadm_handle
, princs
, &n_princs
);
420 krb5_warnx(context
->context
, "%s: UNKNOWN OP %d", client
, cmd
);
421 krb5_storage_free(sp
);
422 sp
= krb5_storage_emem();
423 krb5_store_int32(sp
, KADM5_FAILURE
);
426 krb5_storage_to_data(sp
, out
);
427 krb5_storage_free(sp
);
430 krb5_warn(context
->context
, ret
, "%s", op
);
431 krb5_storage_seek(sp
, 0, SEEK_SET
);
432 krb5_store_int32(sp
, ret
);
433 krb5_storage_to_data(sp
, out
);
434 krb5_storage_free(sp
);
439 v5_loop (krb5_context context
,
440 krb5_auth_context ac
,
441 krb5_boolean initial
,
449 doing_useful_work
= 0;
452 ret
= krb5_read_priv_message(context
, ac
, &fd
, &in
);
453 if(ret
== HEIM_ERR_EOF
)
456 krb5_err(context
, 1, ret
, "krb5_read_priv_message");
457 doing_useful_work
= 1;
458 kadmind_dispatch(kadm_handle
, initial
, &in
, &out
);
460 ret
= krb5_write_priv_message(context
, ac
, &fd
, &out
);
462 krb5_err(context
, 1, ret
, "krb5_write_priv_message");
467 match_appl_version(const void *data
, const char *appl_version
)
470 if(sscanf(appl_version
, "KADM0.%u", &minor
) != 1)
472 *(unsigned*)data
= minor
;
477 handle_v5(krb5_context context
,
486 krb5_boolean initial
;
487 krb5_auth_context ac
= NULL
;
489 unsigned kadm_version
;
490 kadm5_config_params realm_params
;
492 ret
= krb5_recvauth_match_version(context
, &ac
, &fd
,
493 match_appl_version
, &kadm_version
,
494 NULL
, KRB5_RECVAUTH_IGNORE_VERSION
,
496 if(ret
== KRB5_KT_NOTFOUND
)
497 krb5_errx(context
, 1, "krb5_recvauth: key not found");
499 krb5_err(context
, 1, ret
, "krb5_recvauth");
501 ret
= krb5_unparse_name (context
, ticket
->server
, &server_name
);
503 krb5_err (context
, 1, ret
, "krb5_unparse_name");
505 if (strncmp (server_name
, KADM5_ADMIN_SERVICE
,
506 strlen(KADM5_ADMIN_SERVICE
)) != 0)
507 krb5_errx (context
, 1, "ticket for strange principal (%s)",
512 memset(&realm_params
, 0, sizeof(realm_params
));
514 if(kadm_version
== 1) {
516 ret
= krb5_read_priv_message(context
, ac
, &fd
, ¶ms
);
518 krb5_err(context
, 1, ret
, "krb5_read_priv_message");
519 _kadm5_unmarshal_params(context
, ¶ms
, &realm_params
);
522 initial
= ticket
->ticket
.flags
.initial
;
523 ret
= krb5_unparse_name(context
, ticket
->client
, &client
);
525 krb5_err (context
, 1, ret
, "krb5_unparse_name");
526 krb5_free_ticket (context
, ticket
);
527 ret
= kadm5_s_init_with_password_ctx(context
,
535 krb5_err (context
, 1, ret
, "kadm5_init_with_password_ctx");
536 v5_loop (context
, ac
, initial
, kadm_handle
, fd
);
540 kadmind_loop(krb5_context context
,
544 u_char buf
[sizeof(KRB5_SENDAUTH_VERSION
) + 4];
548 n
= krb5_net_read(context
, &fd
, buf
, 4);
552 krb5_err(context
, 1, errno
, "read");
553 _krb5_get_int(buf
, &len
, 4);
555 if (len
== sizeof(KRB5_SENDAUTH_VERSION
)) {
557 n
= krb5_net_read(context
, &fd
, buf
+ 4, len
);
559 krb5_err (context
, 1, errno
, "reading sendauth version");
561 krb5_errx (context
, 1, "EOF reading sendauth version");
563 if(memcmp(buf
+ 4, KRB5_SENDAUTH_VERSION
, len
) == 0) {
564 handle_v5(context
, keytab
, fd
);
571 handle_mit(context
, buf
, len
, fd
);