2 * Copyright (c) 1997 - 2006 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 "kadm5_locl.h"
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
49 set_funcs(kadm5_client_context
*c
)
51 #define SET(C, F) (C)->funcs.F = kadm5 ## _c_ ## F
52 SET(c
, chpass_principal
);
53 SET(c
, chpass_principal_with_key
);
54 SET(c
, create_principal
);
55 SET(c
, delete_principal
);
58 SET(c
, get_principal
);
59 SET(c
, get_principals
);
61 SET(c
, modify_principal
);
62 SET(c
, randkey_principal
);
63 SET(c
, rename_principal
);
67 _kadm5_c_init_context(kadm5_client_context
**ctx
,
68 kadm5_config_params
*params
,
74 *ctx
= malloc(sizeof(**ctx
));
77 memset(*ctx
, 0, sizeof(**ctx
));
78 krb5_add_et_list (context
, initialize_kadm5_error_table_r
);
80 (*ctx
)->context
= context
;
81 if(params
->mask
& KADM5_CONFIG_REALM
) {
83 (*ctx
)->realm
= strdup(params
->realm
);
84 if ((*ctx
)->realm
== NULL
)
87 ret
= krb5_get_default_realm((*ctx
)->context
, &(*ctx
)->realm
);
92 if(params
->mask
& KADM5_CONFIG_ADMIN_SERVER
)
93 (*ctx
)->admin_server
= strdup(params
->admin_server
);
97 ret
= krb5_get_krb_admin_hst (context
, &(*ctx
)->realm
, &hostlist
);
103 (*ctx
)->admin_server
= strdup(*hostlist
);
104 krb5_free_krbhst (context
, hostlist
);
107 if ((*ctx
)->admin_server
== NULL
) {
112 colon
= strchr ((*ctx
)->admin_server
, ':');
116 (*ctx
)->kadmind_port
= 0;
118 if(params
->mask
& KADM5_CONFIG_KADMIND_PORT
)
119 (*ctx
)->kadmind_port
= params
->kadmind_port
;
120 else if (colon
!= NULL
) {
123 (*ctx
)->kadmind_port
= htons(strtol (colon
, &end
, 0));
125 if ((*ctx
)->kadmind_port
== 0)
126 (*ctx
)->kadmind_port
= krb5_getportbyname (context
, "kerberos-adm",
131 static krb5_error_code
132 get_kadm_ticket(krb5_context context
,
134 krb5_principal client
,
135 const char *server_name
)
140 memset(&in
, 0, sizeof(in
));
142 ret
= krb5_parse_name(context
, server_name
, &in
.server
);
145 ret
= krb5_get_credentials(context
, 0, id
, &in
, &out
);
147 krb5_free_creds(context
, out
);
148 krb5_free_principal(context
, in
.server
);
152 static krb5_error_code
153 get_new_cache(krb5_context context
,
154 krb5_principal client
,
155 const char *password
,
156 krb5_prompter_fct prompter
,
158 const char *server_name
,
159 krb5_ccache
*ret_cache
)
163 krb5_get_init_creds_opt
*opt
;
166 ret
= krb5_get_init_creds_opt_alloc (context
, &opt
);
170 krb5_get_init_creds_opt_set_default_flags(context
, "kadmin",
171 krb5_principal_get_realm(context
,
176 krb5_get_init_creds_opt_set_forwardable (opt
, FALSE
);
177 krb5_get_init_creds_opt_set_proxiable (opt
, FALSE
);
179 if(password
== NULL
&& prompter
== NULL
) {
182 ret
= krb5_kt_default(context
, &kt
);
184 ret
= krb5_kt_resolve(context
, keytab
, &kt
);
186 krb5_get_init_creds_opt_free(context
, opt
);
189 ret
= krb5_get_init_creds_keytab (context
,
196 krb5_kt_close(context
, kt
);
198 ret
= krb5_get_init_creds_password (context
,
208 krb5_get_init_creds_opt_free(context
, opt
);
212 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
213 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
214 case KRB5KRB_AP_ERR_MODIFIED
:
215 return KADM5_BAD_PASSWORD
;
219 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, &id
);
222 ret
= krb5_cc_initialize (context
, id
, cred
.client
);
225 ret
= krb5_cc_store_cred (context
, id
, &cred
);
228 krb5_free_cred_contents (context
, &cred
);
234 * Check the credential cache `id´ to figure out what principal to use
235 * when talking to the kadmind. If there is a initial kadmin/admin@
236 * credential in the cache, use that client principal. Otherwise, use
237 * the client principals first component and add /admin to the
241 static krb5_error_code
242 get_cache_principal(krb5_context context
,
244 krb5_principal
*client
)
247 const char *name
, *inst
;
248 krb5_principal p1
, p2
;
250 ret
= krb5_cc_default(context
, id
);
256 ret
= krb5_cc_get_principal(context
, *id
, &p1
);
258 krb5_cc_close(context
, *id
);
263 ret
= krb5_make_principal(context
, &p2
, NULL
,
264 "kadmin", "admin", NULL
);
266 krb5_cc_close(context
, *id
);
268 krb5_free_principal(context
, p1
);
274 krb5_kdc_flags flags
;
277 memset(&in
, 0, sizeof(in
));
282 /* check for initial ticket kadmin/admin */
283 ret
= krb5_get_credentials_with_flags(context
, KRB5_GC_CACHED
, flags
,
285 krb5_free_principal(context
, p2
);
287 if (out
->flags
.b
.initial
) {
289 krb5_free_creds(context
, out
);
292 krb5_free_creds(context
, out
);
295 krb5_cc_close(context
, *id
);
298 name
= krb5_principal_get_comp_string(context
, p1
, 0);
299 inst
= krb5_principal_get_comp_string(context
, p1
, 1);
300 if(inst
== NULL
|| strcmp(inst
, "admin") != 0) {
301 ret
= krb5_make_principal(context
, &p2
, NULL
, name
, "admin", NULL
);
302 krb5_free_principal(context
, p1
);
316 _kadm5_c_get_cred_cache(krb5_context context
,
317 const char *client_name
,
318 const char *server_name
,
319 const char *password
,
320 krb5_prompter_fct prompter
,
323 krb5_ccache
*ret_cache
)
326 krb5_ccache id
= NULL
;
327 krb5_principal default_client
= NULL
, client
= NULL
;
329 /* treat empty password as NULL */
330 if(password
&& *password
== '\0')
332 if(server_name
== NULL
)
333 server_name
= KADM5_ADMIN_SERVICE
;
335 if(client_name
!= NULL
) {
336 ret
= krb5_parse_name(context
, client_name
, &client
);
343 ret
= krb5_cc_get_principal(context
, id
, &client
);
347 /* get principal from default cache, ok if this doesn't work */
349 ret
= get_cache_principal(context
, &id
, &default_client
);
352 * No client was specified by the caller and we cannot
353 * determine the client from a credentials cache.
357 user
= get_default_username ();
360 krb5_set_error_message(context
, KADM5_FAILURE
, "Unable to find local user name");
361 return KADM5_FAILURE
;
363 ret
= krb5_make_principal(context
, &default_client
,
364 NULL
, user
, "admin", NULL
);
372 * No client was specified by the caller, but we have a client
373 * from the default credentials cache.
375 if (client
== NULL
&& default_client
!= NULL
)
376 client
= default_client
;
379 if(id
&& client
&& (default_client
== NULL
||
380 krb5_principal_compare(context
, client
, default_client
) != 0)) {
381 ret
= get_kadm_ticket(context
, id
, client
, server_name
);
384 krb5_free_principal(context
, default_client
);
385 if (default_client
!= client
)
386 krb5_free_principal(context
, client
);
390 /* couldn't get ticket from cache */
393 /* get creds via AS request */
394 if(id
&& (id
!= ccache
))
395 krb5_cc_close(context
, id
);
396 if (client
!= default_client
)
397 krb5_free_principal(context
, default_client
);
399 ret
= get_new_cache(context
, client
, password
, prompter
, keytab
,
400 server_name
, ret_cache
);
401 krb5_free_principal(context
, client
);
406 kadm_connect(kadm5_client_context
*ctx
)
409 krb5_principal server
;
412 struct addrinfo
*ai
, *a
;
413 struct addrinfo hints
;
415 char portstr
[NI_MAXSERV
];
416 char *hostname
, *slash
;
418 krb5_context context
= ctx
->context
;
420 memset (&hints
, 0, sizeof(hints
));
421 hints
.ai_socktype
= SOCK_STREAM
;
422 hints
.ai_protocol
= IPPROTO_TCP
;
424 snprintf (portstr
, sizeof(portstr
), "%u", ntohs(ctx
->kadmind_port
));
426 hostname
= ctx
->admin_server
;
427 slash
= strchr (hostname
, '/');
429 hostname
= slash
+ 1;
431 error
= getaddrinfo (hostname
, portstr
, &hints
, &ai
);
433 krb5_clear_error_message(context
);
434 return KADM5_BAD_SERVER_NAME
;
437 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
438 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
441 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
442 krb5_clear_error_message(context
);
443 krb5_warn (context
, errno
, "connect(%s)", hostname
);
451 krb5_clear_error_message(context
);
452 krb5_warnx (context
, "failed to contact %s", hostname
);
453 return KADM5_FAILURE
;
455 ret
= _kadm5_c_get_cred_cache(context
,
458 NULL
, ctx
->prompter
, ctx
->keytab
,
468 asprintf(&service_name
, "%s@%s", KADM5_ADMIN_SERVICE
, ctx
->realm
);
470 asprintf(&service_name
, "%s", KADM5_ADMIN_SERVICE
);
472 if (service_name
== NULL
) {
475 krb5_clear_error_message(context
);
479 ret
= krb5_parse_name(context
, service_name
, &server
);
483 if(ctx
->ccache
== NULL
)
484 krb5_cc_close(context
, cc
);
490 ret
= krb5_sendauth(context
, &ctx
->ac
, &s
,
491 KADMIN_APPL_VERSION
, NULL
,
492 server
, AP_OPTS_MUTUAL_REQUIRED
,
493 NULL
, NULL
, cc
, NULL
, NULL
, NULL
);
496 kadm5_config_params p
;
497 memset(&p
, 0, sizeof(p
));
499 p
.mask
|= KADM5_CONFIG_REALM
;
500 p
.realm
= ctx
->realm
;
502 ret
= _kadm5_marshal_params(context
, &p
, ¶ms
);
504 ret
= krb5_write_priv_message(context
, ctx
->ac
, &s
, ¶ms
);
505 krb5_data_free(¶ms
);
509 if(ctx
->ccache
== NULL
)
510 krb5_cc_close(context
, cc
);
513 } else if(ret
== KRB5_SENDAUTH_BADAPPLVERS
) {
516 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
519 krb5_clear_error_message(context
);
522 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
525 krb5_clear_error_message(context
);
528 ret
= krb5_sendauth(context
, &ctx
->ac
, &s
,
529 KADMIN_OLD_APPL_VERSION
, NULL
,
530 server
, AP_OPTS_MUTUAL_REQUIRED
,
531 NULL
, NULL
, cc
, NULL
, NULL
, NULL
);
539 krb5_free_principal(context
, server
);
540 if(ctx
->ccache
== NULL
)
541 krb5_cc_close(context
, cc
);
548 _kadm5_connect(void *handle
)
550 kadm5_client_context
*ctx
= handle
;
552 return kadm_connect(ctx
);
557 kadm5_c_init_with_context(krb5_context context
,
558 const char *client_name
,
559 const char *password
,
560 krb5_prompter_fct prompter
,
563 const char *service_name
,
564 kadm5_config_params
*realm_params
,
565 unsigned long struct_version
,
566 unsigned long api_version
,
567 void **server_handle
)
570 kadm5_client_context
*ctx
;
573 ret
= _kadm5_c_init_context(&ctx
, realm_params
, context
);
577 if(password
!= NULL
&& *password
!= '\0') {
578 ret
= _kadm5_c_get_cred_cache(context
,
581 password
, prompter
, keytab
, ccache
, &cc
);
583 return ret
; /* XXX */
588 if (client_name
!= NULL
)
589 ctx
->client_name
= strdup(client_name
);
591 ctx
->client_name
= NULL
;
592 if (service_name
!= NULL
)
593 ctx
->service_name
= strdup(service_name
);
595 ctx
->service_name
= NULL
;
596 ctx
->prompter
= prompter
;
597 ctx
->keytab
= keytab
;
598 ctx
->ccache
= ccache
;
599 /* maybe we should copy the params here */
602 *server_handle
= ctx
;
607 init_context(const char *client_name
,
608 const char *password
,
609 krb5_prompter_fct prompter
,
612 const char *service_name
,
613 kadm5_config_params
*realm_params
,
614 unsigned long struct_version
,
615 unsigned long api_version
,
616 void **server_handle
)
618 krb5_context context
;
620 kadm5_server_context
*ctx
;
622 ret
= krb5_init_context(&context
);
625 ret
= kadm5_c_init_with_context(context
,
637 krb5_free_context(context
);
640 ctx
= *server_handle
;
646 kadm5_c_init_with_password_ctx(krb5_context context
,
647 const char *client_name
,
648 const char *password
,
649 const char *service_name
,
650 kadm5_config_params
*realm_params
,
651 unsigned long struct_version
,
652 unsigned long api_version
,
653 void **server_handle
)
655 return kadm5_c_init_with_context(context
,
669 kadm5_c_init_with_password(const char *client_name
,
670 const char *password
,
671 const char *service_name
,
672 kadm5_config_params
*realm_params
,
673 unsigned long struct_version
,
674 unsigned long api_version
,
675 void **server_handle
)
677 return init_context(client_name
,
690 kadm5_c_init_with_skey_ctx(krb5_context context
,
691 const char *client_name
,
693 const char *service_name
,
694 kadm5_config_params
*realm_params
,
695 unsigned long struct_version
,
696 unsigned long api_version
,
697 void **server_handle
)
699 return kadm5_c_init_with_context(context
,
714 kadm5_c_init_with_skey(const char *client_name
,
716 const char *service_name
,
717 kadm5_config_params
*realm_params
,
718 unsigned long struct_version
,
719 unsigned long api_version
,
720 void **server_handle
)
722 return init_context(client_name
,
735 kadm5_c_init_with_creds_ctx(krb5_context context
,
736 const char *client_name
,
738 const char *service_name
,
739 kadm5_config_params
*realm_params
,
740 unsigned long struct_version
,
741 unsigned long api_version
,
742 void **server_handle
)
744 return kadm5_c_init_with_context(context
,
758 kadm5_c_init_with_creds(const char *client_name
,
760 const char *service_name
,
761 kadm5_config_params
*realm_params
,
762 unsigned long struct_version
,
763 unsigned long api_version
,
764 void **server_handle
)
766 return init_context(client_name
,
780 kadm5_init(char *client_name
, char *pass
,
782 kadm5_config_params
*realm_params
,
783 unsigned long struct_version
,
784 unsigned long api_version
,
785 void **server_handle
)