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>
38 kadmind_dispatch(void *kadm_handlep, krb5_boolean initial,
39 krb5_data *in, krb5_data *out)
42 int32_t cmd, mask, tmp;
43 kadm5_server_context *contextp = kadm_handlep;
44 char client[128], name[128], name2[128];
46 krb5_principal princ, princ2;
47 kadm5_principal_ent_rec ent;
48 char *password, *expression;
49 krb5_keyblock *new_keys;
50 krb5_key_salt_tuple *ks_tuple = NULL;
51 krb5_boolean keepold = FALSE;
59 krb5_unparse_name_fixed(contextp->context, contextp->caller,
60 client, sizeof(client));
62 sp = krb5_storage_from_data(in);
64 krb5_errx(contextp->context, 1, "out of memory");
66 krb5_ret_int32(sp, &cmd);
70 ret = krb5_ret_principal(sp, &princ);
73 ret = krb5_ret_int32(sp, &mask);
75 krb5_free_principal(contextp->context, princ);
78 mask |= KADM5_PRINCIPAL;
79 krb5_unparse_name_fixed(contextp->context, princ, name, sizeof(name));
80 krb5_warnx(contextp->context, "%s: %s %s", client, op, name);
81 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_GET_KEYS, princ);
85 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_GET, princ);
87 krb5_free_principal(contextp->context, princ);
90 ret = kadm5_get_principal(kadm_handlep, princ, &ent, mask);
91 krb5_storage_free(sp);
92 sp = krb5_storage_emem();
93 krb5_store_int32(sp, ret);
96 kadm5_store_principal_ent(sp, &ent);
98 kadm5_store_principal_ent_nokeys(sp, &ent);
99 kadm5_free_principal_ent(kadm_handlep, &ent);
101 krb5_free_principal(contextp->context, princ);
106 ret = krb5_ret_principal(sp, &princ);
109 krb5_unparse_name_fixed(contextp->context, princ, name, sizeof(name));
110 krb5_warnx(contextp->context, "%s: %s %s", client, op, name);
111 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_DELETE, princ);
113 krb5_free_principal(contextp->context, princ);
116 ret = kadm5_delete_principal(kadm_handlep, princ);
117 krb5_free_principal(contextp->context, princ);
118 krb5_storage_free(sp);
119 sp = krb5_storage_emem();
120 krb5_store_int32(sp, ret);
125 ret = kadm5_ret_principal_ent(sp, &ent);
128 ret = krb5_ret_int32(sp, &mask);
130 kadm5_free_principal_ent(contextp->context, &ent);
133 ret = krb5_ret_string(sp, &password);
135 kadm5_free_principal_ent(contextp->context, &ent);
138 krb5_unparse_name_fixed(contextp->context, ent.principal,
140 krb5_warnx(contextp->context, "%s: %s %s", client, op, name);
141 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_ADD,
144 kadm5_free_principal_ent(contextp->context, &ent);
145 memset(password, 0, strlen(password));
149 ret = kadm5_create_principal(kadm_handlep, &ent,
151 kadm5_free_principal_ent(kadm_handlep, &ent);
152 memset(password, 0, strlen(password));
154 krb5_storage_free(sp);
155 sp = krb5_storage_emem();
156 krb5_store_int32(sp, ret);
161 ret = kadm5_ret_principal_ent(sp, &ent);
164 ret = krb5_ret_int32(sp, &mask);
166 kadm5_free_principal_ent(contextp, &ent);
169 krb5_unparse_name_fixed(contextp->context, ent.principal,
171 krb5_warnx(contextp->context, "%s: %s %s", client, op, name);
172 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_MODIFY,
175 kadm5_free_principal_ent(contextp, &ent);
178 ret = kadm5_modify_principal(kadm_handlep, &ent, mask);
179 kadm5_free_principal_ent(kadm_handlep, &ent);
180 krb5_storage_free(sp);
181 sp = krb5_storage_emem();
182 krb5_store_int32(sp, ret);
187 ret = krb5_ret_principal(sp, &princ);
190 ret = krb5_ret_principal(sp, &princ2);
192 krb5_free_principal(contextp->context, princ);
195 krb5_unparse_name_fixed(contextp->context, princ, name, sizeof(name));
196 krb5_unparse_name_fixed(contextp->context, princ2, name2, sizeof(name2));
197 krb5_warnx(contextp->context, "%s: %s %s -> %s",
198 client, op, name, name2);
199 ret = _kadm5_acl_check_permission(contextp,
202 || _kadm5_acl_check_permission(contextp,
206 krb5_free_principal(contextp->context, princ);
207 krb5_free_principal(contextp->context, princ2);
210 ret = kadm5_rename_principal(kadm_handlep, princ, princ2);
211 krb5_free_principal(contextp->context, princ);
212 krb5_free_principal(contextp->context, princ2);
213 krb5_storage_free(sp);
214 sp = krb5_storage_emem();
215 krb5_store_int32(sp, ret);
220 ret = krb5_ret_principal(sp, &princ);
223 ret = krb5_ret_string(sp, &password);
225 krb5_free_principal(contextp->context, princ);
228 ret = krb5_ret_int32(sp, &keepold);
229 if (ret && ret != HEIM_ERR_EOF) {
230 krb5_free_principal(contextp->context, princ);
233 krb5_unparse_name_fixed(contextp->context, princ, name, sizeof(name));
234 krb5_warnx(contextp->context, "%s: %s %s", client, op, name);
237 * The change is allowed if at least one of:
239 * a) it's for the principal him/herself and this was an
240 * initial ticket, but then, check with the password quality
242 * b) the user is on the CPW ACL.
246 && krb5_principal_compare (contextp->context, contextp->caller,
250 const char *pwd_reason;
252 pwd_data.data = password;
253 pwd_data.length = strlen(password);
255 pwd_reason = kadm5_check_password_quality (contextp->context,
257 if (pwd_reason != NULL)
258 ret = KADM5_PASS_Q_DICT;
262 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_CPW, princ);
265 krb5_free_principal(contextp->context, princ);
266 memset(password, 0, strlen(password));
270 ret = kadm5_chpass_principal_3(kadm_handlep, princ, keepold, 0, NULL,
272 krb5_free_principal(contextp->context, princ);
273 memset(password, 0, strlen(password));
275 krb5_storage_free(sp);
276 sp = krb5_storage_emem();
277 krb5_store_int32(sp, ret);
280 case kadm_chpass_with_key:{
282 krb5_key_data *key_data;
285 op = "CHPASS_WITH_KEY";
286 ret = krb5_ret_principal(sp, &princ);
289 ret = krb5_ret_int32(sp, &n_key_data);
291 krb5_free_principal(contextp->context, princ);
294 ret = krb5_ret_int32(sp, &keepold);
295 if (ret && ret != HEIM_ERR_EOF) {
296 krb5_free_principal(contextp->context, princ);
299 /* n_key_data will be squeezed into an int16_t below. */
300 if (n_key_data < 0 || n_key_data >= 1 << 16 ||
301 (size_t)n_key_data > UINT_MAX/sizeof(*key_data)) {
303 krb5_free_principal(contextp->context, princ);
307 key_data = malloc (n_key_data * sizeof(*key_data));
308 if (key_data == NULL && n_key_data != 0) {
310 krb5_free_principal(contextp->context, princ);
314 for (i = 0; i < n_key_data; ++i) {
315 ret = kadm5_ret_key_data (sp, &key_data[i]);
319 kadm5_free_key_data (contextp, &dummy, key_data);
321 krb5_free_principal(contextp->context, princ);
326 krb5_unparse_name_fixed(contextp->context, princ, name, sizeof(name));
327 krb5_warnx(contextp->context, "%s: %s %s", client, op, name);
330 * The change is only allowed if the user is on the CPW ACL,
331 * this it to force password quality check on the user.
334 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_CPW, princ);
336 int16_t dummy = n_key_data;
338 kadm5_free_key_data (contextp, &dummy, key_data);
340 krb5_free_principal(contextp->context, princ);
343 ret = kadm5_chpass_principal_with_key_3(kadm_handlep, princ, keepold,
344 n_key_data, key_data);
346 int16_t dummy = n_key_data;
347 kadm5_free_key_data (contextp, &dummy, key_data);
350 krb5_free_principal(contextp->context, princ);
351 krb5_storage_free(sp);
352 sp = krb5_storage_emem();
353 krb5_store_int32(sp, ret);
358 ret = krb5_ret_principal(sp, &princ);
361 krb5_unparse_name_fixed(contextp->context, princ, name, sizeof(name));
362 krb5_warnx(contextp->context, "%s: %s %s", client, op, name);
364 * The change is allowed if at least one of:
365 * a) it's for the principal him/herself and this was an initial ticket
366 * b) the user is on the CPW ACL.
370 && krb5_principal_compare (contextp->context, contextp->caller,
374 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_CPW, princ);
377 krb5_free_principal(contextp->context, princ);
382 * See comments in kadm5_c_randkey_principal() regarding the
385 ret = krb5_ret_int32(sp, &keepold);
386 if (ret != 0 && ret != HEIM_ERR_EOF) {
387 krb5_free_principal(contextp->context, princ);
391 ret = krb5_ret_int32(sp, &n_ks_tuple);
392 if (ret != 0 && ret != HEIM_ERR_EOF) {
393 krb5_free_principal(contextp->context, princ);
395 } else if (ret == 0) {
398 if (n_ks_tuple < 0) {
400 krb5_free_principal(contextp->context, princ);
404 if ((ks_tuple = calloc(n_ks_tuple, sizeof (*ks_tuple))) == NULL) {
406 krb5_free_principal(contextp->context, princ);
410 for (i = 0; i < n_ks_tuple; i++) {
411 ret = krb5_ret_int32(sp, &ks_tuple[i].ks_enctype);
413 krb5_free_principal(contextp->context, princ);
416 ret = krb5_ret_int32(sp, &ks_tuple[i].ks_salttype);
418 krb5_free_principal(contextp->context, princ);
423 ret = kadm5_randkey_principal_3(kadm_handlep, princ, keepold,
424 n_ks_tuple, ks_tuple, &new_keys,
426 krb5_free_principal(contextp->context, princ);
428 krb5_storage_free(sp);
429 sp = krb5_storage_emem();
430 krb5_store_int32(sp, ret);
433 krb5_store_int32(sp, n_keys);
434 for(i = 0; i < n_keys; i++){
435 krb5_store_keyblock(sp, new_keys[i]);
436 krb5_free_keyblock_contents(contextp->context, &new_keys[i]);
442 case kadm_get_privs:{
444 ret = kadm5_get_privs(kadm_handlep, &privs);
445 krb5_storage_free(sp);
446 sp = krb5_storage_emem();
447 krb5_store_int32(sp, ret);
449 krb5_store_uint32(sp, privs);
452 case kadm_get_princs:{
454 ret = krb5_ret_int32(sp, &tmp);
458 ret = krb5_ret_string(sp, &expression);
463 krb5_warnx(contextp->context, "%s: %s %s", client, op,
464 expression ? expression : "*");
465 ret = _kadm5_acl_check_permission(contextp, KADM5_PRIV_LIST, NULL);
470 ret = kadm5_get_principals(kadm_handlep, expression, &princs, &n_princs);
472 krb5_storage_free(sp);
473 sp = krb5_storage_emem();
474 krb5_store_int32(sp, ret);
477 krb5_store_int32(sp, n_princs);
478 for(i = 0; i < n_princs; i++)
479 krb5_store_string(sp, princs[i]);
480 kadm5_free_name_list(kadm_handlep, princs, &n_princs);
485 krb5_warnx(contextp->context, "%s: UNKNOWN OP %d", client, cmd);
486 krb5_storage_free(sp);
487 sp = krb5_storage_emem();
488 krb5_store_int32(sp, KADM5_FAILURE);
491 krb5_storage_to_data(sp, out);
492 krb5_storage_free(sp);
495 krb5_warn(contextp->context, ret, "%s", op);
496 krb5_storage_seek(sp, 0, SEEK_SET);
497 krb5_store_int32(sp, ret);
498 krb5_storage_to_data(sp, out);
499 krb5_storage_free(sp);
504 v5_loop (krb5_context contextp,
505 krb5_auth_context ac,
506 krb5_boolean initial,
514 doing_useful_work = 0;
517 ret = krb5_read_priv_message(contextp, ac, &fd, &in);
518 if(ret == HEIM_ERR_EOF)
521 krb5_err(contextp, 1, ret, "krb5_read_priv_message");
522 doing_useful_work = 1;
523 kadmind_dispatch(kadm_handlep, initial, &in, &out);
525 ret = krb5_write_priv_message(contextp, ac, &fd, &out);
527 krb5_err(contextp, 1, ret, "krb5_write_priv_message");
532 match_appl_version(const void *data, const char *appl_version)
535 if(sscanf(appl_version, "KADM0.%u", &minor) != 1)
538 *(unsigned*)(intptr_t)data = minor;
543 handle_v5(krb5_context contextp,
552 krb5_boolean initial;
553 krb5_auth_context ac = NULL;
555 unsigned kadm_version;
556 kadm5_config_params realm_params;
558 ret = krb5_recvauth_match_version(contextp, &ac, &fd,
559 match_appl_version, &kadm_version,
560 NULL, KRB5_RECVAUTH_IGNORE_VERSION,
563 krb5_err(contextp, 1, ret, "krb5_recvauth");
565 ret = krb5_unparse_name (contextp, ticket->server, &server_name);
567 krb5_err (contextp, 1, ret, "krb5_unparse_name");
569 if (strncmp (server_name, KADM5_ADMIN_SERVICE,
570 strlen(KADM5_ADMIN_SERVICE)) != 0)
571 krb5_errx (contextp, 1, "ticket for strange principal (%s)",
576 memset(&realm_params, 0, sizeof(realm_params));
578 if(kadm_version == 1) {
580 ret = krb5_read_priv_message(contextp, ac, &fd, ¶ms);
582 krb5_err(contextp, 1, ret, "krb5_read_priv_message");
583 _kadm5_unmarshal_params(contextp, ¶ms, &realm_params);
586 initial = ticket->ticket.flags.initial;
587 ret = krb5_unparse_name(contextp, ticket->client, &client);
589 krb5_err (contextp, 1, ret, "krb5_unparse_name");
590 krb5_free_ticket (contextp, ticket);
591 ret = kadm5_s_init_with_password_ctx(contextp,
599 krb5_err (contextp, 1, ret, "kadm5_init_with_password_ctx");
600 v5_loop (contextp, ac, initial, kadm_handlep, fd);
604 kadmind_loop(krb5_context contextp,
608 u_char buf[sizeof(KRB5_SENDAUTH_VERSION) + 4];
612 n = krb5_net_read(contextp, &sock, buf, 4);
616 krb5_err(contextp, 1, errno, "read");
617 _krb5_get_int(buf, &len, 4);
619 if (len == sizeof(KRB5_SENDAUTH_VERSION)) {
621 n = krb5_net_read(contextp, &sock, buf + 4, len);
623 krb5_err (contextp, 1, errno, "reading sendauth version");
625 krb5_errx (contextp, 1, "EOF reading sendauth version");
627 if(memcmp(buf + 4, KRB5_SENDAUTH_VERSION, len) == 0) {
628 handle_v5(contextp, keytab, sock);
635 handle_mit(contextp, buf, len, sock);