1 /* $NetBSD: server.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $ */
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "kadmin_locl.h"
37 #include <krb5/krb5-private.h>
40 kadmind_dispatch(void *kadm_handlep
, krb5_boolean initial
,
41 krb5_data
*in
, krb5_data
*out
)
44 int32_t cmd
, mask
, tmp
;
45 kadm5_server_context
*contextp
= kadm_handlep
;
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(contextp
->context
, contextp
->caller
,
58 client
, sizeof(client
));
60 sp
= krb5_storage_from_data(in
);
62 krb5_errx(contextp
->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(contextp
->context
, princ
);
76 mask
|= KADM5_PRINCIPAL
;
77 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
78 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
79 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_GET
, princ
);
81 krb5_free_principal(contextp
->context
, princ
);
84 ret
= kadm5_get_principal(kadm_handlep
, 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_handlep
, &ent
);
92 krb5_free_principal(contextp
->context
, princ
);
97 ret
= krb5_ret_principal(sp
, &princ
);
100 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
101 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
102 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_DELETE
, princ
);
104 krb5_free_principal(contextp
->context
, princ
);
107 ret
= kadm5_delete_principal(kadm_handlep
, princ
);
108 krb5_free_principal(contextp
->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(contextp
->context
, &ent
);
124 ret
= krb5_ret_string(sp
, &password
);
126 kadm5_free_principal_ent(contextp
->context
, &ent
);
129 krb5_unparse_name_fixed(contextp
->context
, ent
.principal
,
131 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
132 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_ADD
,
135 kadm5_free_principal_ent(contextp
->context
, &ent
);
136 memset(password
, 0, strlen(password
));
140 ret
= kadm5_create_principal(kadm_handlep
, &ent
,
142 kadm5_free_principal_ent(kadm_handlep
, &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(contextp
, &ent
);
160 krb5_unparse_name_fixed(contextp
->context
, ent
.principal
,
162 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
163 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_MODIFY
,
166 kadm5_free_principal_ent(contextp
, &ent
);
169 ret
= kadm5_modify_principal(kadm_handlep
, &ent
, mask
);
170 kadm5_free_principal_ent(kadm_handlep
, &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(contextp
->context
, princ
);
186 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
187 krb5_unparse_name_fixed(contextp
->context
, princ2
, name2
, sizeof(name2
));
188 krb5_warnx(contextp
->context
, "%s: %s %s -> %s",
189 client
, op
, name
, name2
);
190 ret
= _kadm5_acl_check_permission(contextp
,
193 || _kadm5_acl_check_permission(contextp
,
197 krb5_free_principal(contextp
->context
, princ
);
198 krb5_free_principal(contextp
->context
, princ2
);
201 ret
= kadm5_rename_principal(kadm_handlep
, princ
, princ2
);
202 krb5_free_principal(contextp
->context
, princ
);
203 krb5_free_principal(contextp
->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(contextp
->context
, princ
);
219 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
220 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
223 * The change is allowed if at least one of:
225 * a) allowed by sysadmin
226 * b) it's for the principal him/herself and this was an
227 * initial ticket, but then, check with the password quality
229 * c) the user is on the CPW ACL.
232 if (krb5_config_get_bool_default(contextp
->context
, NULL
, TRUE
,
233 "kadmin", "allow_self_change_password", NULL
)
235 && krb5_principal_compare (contextp
->context
, contextp
->caller
,
239 const char *pwd_reason
;
241 pwd_data
.data
= password
;
242 pwd_data
.length
= strlen(password
);
244 pwd_reason
= kadm5_check_password_quality (contextp
->context
,
246 if (pwd_reason
!= NULL
)
247 ret
= KADM5_PASS_Q_DICT
;
251 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
254 krb5_free_principal(contextp
->context
, princ
);
255 memset(password
, 0, strlen(password
));
259 ret
= kadm5_chpass_principal(kadm_handlep
, princ
, password
);
260 krb5_free_principal(contextp
->context
, princ
);
261 memset(password
, 0, strlen(password
));
263 krb5_storage_free(sp
);
264 sp
= krb5_storage_emem();
265 krb5_store_int32(sp
, ret
);
268 case kadm_chpass_with_key
:{
270 krb5_key_data
*key_data
;
273 op
= "CHPASS_WITH_KEY";
274 ret
= krb5_ret_principal(sp
, &princ
);
277 ret
= krb5_ret_int32(sp
, &n_key_data
);
279 krb5_free_principal(contextp
->context
, princ
);
282 /* n_key_data will be squeezed into an int16_t below. */
283 if (n_key_data
< 0 || n_key_data
>= 1 << 16 ||
284 (size_t)n_key_data
> UINT_MAX
/sizeof(*key_data
)) {
286 krb5_free_principal(contextp
->context
, princ
);
290 key_data
= malloc (n_key_data
* sizeof(*key_data
));
291 if (key_data
== NULL
&& n_key_data
!= 0) {
293 krb5_free_principal(contextp
->context
, princ
);
297 for (i
= 0; i
< n_key_data
; ++i
) {
298 ret
= kadm5_ret_key_data (sp
, &key_data
[i
]);
302 kadm5_free_key_data (contextp
, &dummy
, key_data
);
304 krb5_free_principal(contextp
->context
, princ
);
309 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
310 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
313 * The change is only allowed if the user is on the CPW ACL,
314 * this it to force password quality check on the user.
317 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
319 int16_t dummy
= n_key_data
;
321 kadm5_free_key_data (contextp
, &dummy
, key_data
);
323 krb5_free_principal(contextp
->context
, princ
);
326 ret
= kadm5_chpass_principal_with_key(kadm_handlep
, princ
,
327 n_key_data
, key_data
);
329 int16_t dummy
= n_key_data
;
330 kadm5_free_key_data (contextp
, &dummy
, key_data
);
333 krb5_free_principal(contextp
->context
, princ
);
334 krb5_storage_free(sp
);
335 sp
= krb5_storage_emem();
336 krb5_store_int32(sp
, ret
);
341 ret
= krb5_ret_principal(sp
, &princ
);
344 krb5_unparse_name_fixed(contextp
->context
, princ
, name
, sizeof(name
));
345 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
, name
);
347 * The change is allowed if at least one of:
348 * a) it's for the principal him/herself and this was an initial ticket
349 * b) the user is on the CPW ACL.
353 && krb5_principal_compare (contextp
->context
, contextp
->caller
,
357 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_CPW
, princ
);
360 krb5_free_principal(contextp
->context
, princ
);
363 ret
= kadm5_randkey_principal(kadm_handlep
, princ
,
365 krb5_free_principal(contextp
->context
, princ
);
366 krb5_storage_free(sp
);
367 sp
= krb5_storage_emem();
368 krb5_store_int32(sp
, ret
);
371 krb5_store_int32(sp
, n_keys
);
372 for(i
= 0; i
< n_keys
; i
++){
373 krb5_store_keyblock(sp
, new_keys
[i
]);
374 krb5_free_keyblock_contents(contextp
->context
, &new_keys
[i
]);
380 case kadm_get_privs
:{
382 ret
= kadm5_get_privs(kadm_handlep
, &privs
);
383 krb5_storage_free(sp
);
384 sp
= krb5_storage_emem();
385 krb5_store_int32(sp
, ret
);
387 krb5_store_uint32(sp
, privs
);
390 case kadm_get_princs
:{
392 ret
= krb5_ret_int32(sp
, &tmp
);
396 ret
= krb5_ret_string(sp
, &expression
);
401 krb5_warnx(contextp
->context
, "%s: %s %s", client
, op
,
402 expression
? expression
: "*");
403 ret
= _kadm5_acl_check_permission(contextp
, KADM5_PRIV_LIST
, NULL
);
408 ret
= kadm5_get_principals(kadm_handlep
, expression
, &princs
, &n_princs
);
410 krb5_storage_free(sp
);
411 sp
= krb5_storage_emem();
412 krb5_store_int32(sp
, ret
);
415 krb5_store_int32(sp
, n_princs
);
416 for(i
= 0; i
< n_princs
; i
++)
417 krb5_store_string(sp
, princs
[i
]);
418 kadm5_free_name_list(kadm_handlep
, princs
, &n_princs
);
423 krb5_warnx(contextp
->context
, "%s: UNKNOWN OP %d", client
, cmd
);
424 krb5_storage_free(sp
);
425 sp
= krb5_storage_emem();
426 krb5_store_int32(sp
, KADM5_FAILURE
);
429 krb5_storage_to_data(sp
, out
);
430 krb5_storage_free(sp
);
433 krb5_warn(contextp
->context
, ret
, "%s", op
);
434 krb5_storage_seek(sp
, 0, SEEK_SET
);
435 krb5_store_int32(sp
, ret
);
436 krb5_storage_to_data(sp
, out
);
437 krb5_storage_free(sp
);
442 v5_loop (krb5_context contextp
,
443 krb5_auth_context ac
,
444 krb5_boolean initial
,
452 doing_useful_work
= 0;
455 ret
= krb5_read_priv_message(contextp
, ac
, &fd
, &in
);
456 if(ret
== HEIM_ERR_EOF
)
459 krb5_err(contextp
, 1, ret
, "krb5_read_priv_message");
460 doing_useful_work
= 1;
461 kadmind_dispatch(kadm_handlep
, initial
, &in
, &out
);
463 ret
= krb5_write_priv_message(contextp
, ac
, &fd
, &out
);
465 krb5_err(contextp
, 1, ret
, "krb5_write_priv_message");
470 match_appl_version(const void *data
, const char *appl_version
)
473 if(sscanf(appl_version
, "KADM0.%u", &minor
) != 1)
476 *(unsigned*)(intptr_t)data
= minor
;
481 handle_v5(krb5_context contextp
,
490 krb5_boolean initial
;
491 krb5_auth_context ac
= NULL
;
493 unsigned kadm_version
;
494 kadm5_config_params realm_params
;
496 ret
= krb5_recvauth_match_version(contextp
, &ac
, &fd
,
497 match_appl_version
, &kadm_version
,
498 NULL
, KRB5_RECVAUTH_IGNORE_VERSION
,
501 krb5_err(contextp
, 1, ret
, "krb5_recvauth");
503 ret
= krb5_unparse_name (contextp
, ticket
->server
, &server_name
);
505 krb5_err (contextp
, 1, ret
, "krb5_unparse_name");
507 if (strncmp (server_name
, KADM5_ADMIN_SERVICE
,
508 strlen(KADM5_ADMIN_SERVICE
)) != 0)
509 krb5_errx (contextp
, 1, "ticket for strange principal (%s)",
514 memset(&realm_params
, 0, sizeof(realm_params
));
516 if(kadm_version
== 1) {
518 ret
= krb5_read_priv_message(contextp
, ac
, &fd
, ¶ms
);
520 krb5_err(contextp
, 1, ret
, "krb5_read_priv_message");
521 _kadm5_unmarshal_params(contextp
, ¶ms
, &realm_params
);
524 initial
= ticket
->ticket
.flags
.initial
;
525 ret
= krb5_unparse_name(contextp
, ticket
->client
, &client
);
527 krb5_err (contextp
, 1, ret
, "krb5_unparse_name");
528 krb5_free_ticket (contextp
, ticket
);
529 ret
= kadm5_s_init_with_password_ctx(contextp
,
537 krb5_err (contextp
, 1, ret
, "kadm5_init_with_password_ctx");
538 v5_loop (contextp
, ac
, initial
, kadm_handlep
, fd
);
542 kadmind_loop(krb5_context contextp
,
546 u_char buf
[sizeof(KRB5_SENDAUTH_VERSION
) + 4];
550 n
= krb5_net_read(contextp
, &sock
, buf
, 4);
554 krb5_err(contextp
, 1, errno
, "read");
555 _krb5_get_int(buf
, &len
, 4);
557 if (len
== sizeof(KRB5_SENDAUTH_VERSION
)) {
559 n
= krb5_net_read(contextp
, &sock
, buf
+ 4, len
);
561 krb5_err (contextp
, 1, errno
, "reading sendauth version");
563 krb5_errx (contextp
, 1, "EOF reading sendauth version");
565 if(memcmp(buf
+ 4, KRB5_SENDAUTH_VERSION
, len
) == 0) {
566 handle_v5(contextp
, keytab
, sock
);
573 handle_mit(contextp
, buf
, len
, sock
);