1 /* $NetBSD: init_c.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $ */
4 * Copyright (c) 1997 - 2006 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 "kadm5_locl.h"
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
41 #ifdef HAVE_NETINET_IN_H
42 #include <netinet/in.h>
51 set_funcs(kadm5_client_context
*c
)
53 #define SET(C, F) (C)->funcs.F = kadm5 ## _c_ ## F
54 SET(c
, chpass_principal
);
55 SET(c
, chpass_principal_with_key
);
56 SET(c
, create_principal
);
57 SET(c
, delete_principal
);
60 SET(c
, get_principal
);
61 SET(c
, get_principals
);
63 SET(c
, modify_principal
);
64 SET(c
, randkey_principal
);
65 SET(c
, rename_principal
);
69 _kadm5_c_init_context(kadm5_client_context
**ctx
,
70 kadm5_config_params
*params
,
76 *ctx
= malloc(sizeof(**ctx
));
79 memset(*ctx
, 0, sizeof(**ctx
));
80 krb5_add_et_list (context
, initialize_kadm5_error_table_r
);
82 (*ctx
)->context
= context
;
83 if(params
->mask
& KADM5_CONFIG_REALM
) {
85 (*ctx
)->realm
= strdup(params
->realm
);
86 if ((*ctx
)->realm
== NULL
)
89 ret
= krb5_get_default_realm((*ctx
)->context
, &(*ctx
)->realm
);
94 if(params
->mask
& KADM5_CONFIG_ADMIN_SERVER
)
95 (*ctx
)->admin_server
= strdup(params
->admin_server
);
99 ret
= krb5_get_krb_admin_hst (context
, &(*ctx
)->realm
, &hostlist
);
105 (*ctx
)->admin_server
= strdup(*hostlist
);
106 krb5_free_krbhst (context
, hostlist
);
109 if ((*ctx
)->admin_server
== NULL
) {
114 colon
= strchr ((*ctx
)->admin_server
, ':');
118 (*ctx
)->kadmind_port
= 0;
120 if(params
->mask
& KADM5_CONFIG_KADMIND_PORT
)
121 (*ctx
)->kadmind_port
= params
->kadmind_port
;
122 else if (colon
!= NULL
) {
125 (*ctx
)->kadmind_port
= htons(strtol (colon
, &end
, 0));
127 if ((*ctx
)->kadmind_port
== 0)
128 (*ctx
)->kadmind_port
= krb5_getportbyname (context
, "kerberos-adm",
133 static krb5_error_code
134 get_kadm_ticket(krb5_context context
,
136 krb5_principal client
,
137 const char *server_name
)
142 memset(&in
, 0, sizeof(in
));
144 ret
= krb5_parse_name(context
, server_name
, &in
.server
);
147 ret
= krb5_get_credentials(context
, 0, id
, &in
, &out
);
149 krb5_free_creds(context
, out
);
150 krb5_free_principal(context
, in
.server
);
154 static krb5_error_code
155 get_new_cache(krb5_context context
,
156 krb5_principal client
,
157 const char *password
,
158 krb5_prompter_fct prompter
,
160 const char *server_name
,
161 krb5_ccache
*ret_cache
)
165 krb5_get_init_creds_opt
*opt
;
168 ret
= krb5_get_init_creds_opt_alloc (context
, &opt
);
172 krb5_get_init_creds_opt_set_default_flags(context
, "kadmin",
173 krb5_principal_get_realm(context
,
178 krb5_get_init_creds_opt_set_forwardable (opt
, FALSE
);
179 krb5_get_init_creds_opt_set_proxiable (opt
, FALSE
);
181 if(password
== NULL
&& prompter
== NULL
) {
184 ret
= krb5_kt_default(context
, &kt
);
186 ret
= krb5_kt_resolve(context
, keytab
, &kt
);
188 krb5_get_init_creds_opt_free(context
, opt
);
191 ret
= krb5_get_init_creds_keytab (context
,
198 krb5_kt_close(context
, kt
);
200 ret
= krb5_get_init_creds_password (context
,
210 krb5_get_init_creds_opt_free(context
, opt
);
214 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
215 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
216 case KRB5KRB_AP_ERR_MODIFIED
:
217 return KADM5_BAD_PASSWORD
;
221 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, &id
);
224 ret
= krb5_cc_initialize (context
, id
, cred
.client
);
227 ret
= krb5_cc_store_cred (context
, id
, &cred
);
230 krb5_free_cred_contents (context
, &cred
);
236 * Check the credential cache `id´ to figure out what principal to use
237 * when talking to the kadmind. If there is a initial kadmin/admin@
238 * credential in the cache, use that client principal. Otherwise, use
239 * the client principals first component and add /admin to the
243 static krb5_error_code
244 get_cache_principal(krb5_context context
,
246 krb5_principal
*client
)
249 const char *name
, *inst
;
250 krb5_principal p1
, p2
;
252 ret
= krb5_cc_default(context
, id
);
258 ret
= krb5_cc_get_principal(context
, *id
, &p1
);
260 krb5_cc_close(context
, *id
);
265 ret
= krb5_make_principal(context
, &p2
, NULL
,
266 "kadmin", "admin", NULL
);
268 krb5_cc_close(context
, *id
);
270 krb5_free_principal(context
, p1
);
276 krb5_kdc_flags flags
;
279 memset(&in
, 0, sizeof(in
));
284 /* check for initial ticket kadmin/admin */
285 ret
= krb5_get_credentials_with_flags(context
, KRB5_GC_CACHED
, flags
,
287 krb5_free_principal(context
, p2
);
289 if (out
->flags
.b
.initial
) {
291 krb5_free_creds(context
, out
);
294 krb5_free_creds(context
, out
);
297 krb5_cc_close(context
, *id
);
300 name
= krb5_principal_get_comp_string(context
, p1
, 0);
301 inst
= krb5_principal_get_comp_string(context
, p1
, 1);
302 if(inst
== NULL
|| strcmp(inst
, "admin") != 0) {
303 ret
= krb5_make_principal(context
, &p2
, NULL
, name
, "admin", NULL
);
304 krb5_free_principal(context
, p1
);
318 _kadm5_c_get_cred_cache(krb5_context context
,
319 const char *client_name
,
320 const char *server_name
,
321 const char *password
,
322 krb5_prompter_fct prompter
,
325 krb5_ccache
*ret_cache
)
328 krb5_ccache id
= NULL
;
329 krb5_principal default_client
= NULL
, client
= NULL
;
331 /* treat empty password as NULL */
332 if(password
&& *password
== '\0')
334 if(server_name
== NULL
)
335 server_name
= KADM5_ADMIN_SERVICE
;
337 if(client_name
!= NULL
) {
338 ret
= krb5_parse_name(context
, client_name
, &client
);
345 ret
= krb5_cc_get_principal(context
, id
, &client
);
349 /* get principal from default cache, ok if this doesn't work */
351 ret
= get_cache_principal(context
, &id
, &default_client
);
354 * No client was specified by the caller and we cannot
355 * determine the client from a credentials cache.
359 user
= get_default_username ();
362 krb5_set_error_message(context
, KADM5_FAILURE
, "Unable to find local user name");
363 return KADM5_FAILURE
;
365 ret
= krb5_make_principal(context
, &default_client
,
366 NULL
, user
, "admin", NULL
);
374 * No client was specified by the caller, but we have a client
375 * from the default credentials cache.
377 if (client
== NULL
&& default_client
!= NULL
)
378 client
= default_client
;
381 if(id
&& client
&& (default_client
== NULL
||
382 krb5_principal_compare(context
, client
, default_client
) != 0)) {
383 ret
= get_kadm_ticket(context
, id
, client
, server_name
);
386 krb5_free_principal(context
, default_client
);
387 if (default_client
!= client
)
388 krb5_free_principal(context
, client
);
392 /* couldn't get ticket from cache */
395 /* get creds via AS request */
396 if(id
&& (id
!= ccache
))
397 krb5_cc_close(context
, id
);
398 if (client
!= default_client
)
399 krb5_free_principal(context
, default_client
);
401 ret
= get_new_cache(context
, client
, password
, prompter
, keytab
,
402 server_name
, ret_cache
);
403 krb5_free_principal(context
, client
);
408 kadm_connect(kadm5_client_context
*ctx
)
411 krb5_principal server
;
413 rk_socket_t s
= rk_INVALID_SOCKET
;
414 struct addrinfo
*ai
, *a
;
415 struct addrinfo hints
;
417 char portstr
[NI_MAXSERV
];
418 char *hostname
, *slash
;
420 krb5_context context
= ctx
->context
;
422 memset (&hints
, 0, sizeof(hints
));
423 hints
.ai_socktype
= SOCK_STREAM
;
424 hints
.ai_protocol
= IPPROTO_TCP
;
426 snprintf (portstr
, sizeof(portstr
), "%u", ntohs(ctx
->kadmind_port
));
428 hostname
= ctx
->admin_server
;
429 slash
= strchr (hostname
, '/');
431 hostname
= slash
+ 1;
433 error
= getaddrinfo (hostname
, portstr
, &hints
, &ai
);
435 krb5_clear_error_message(context
);
436 return KADM5_BAD_SERVER_NAME
;
439 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
440 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
443 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
444 krb5_clear_error_message(context
);
445 krb5_warn (context
, errno
, "connect(%s)", hostname
);
453 krb5_clear_error_message(context
);
454 krb5_warnx (context
, "failed to contact %s", hostname
);
455 return KADM5_FAILURE
;
457 ret
= _kadm5_c_get_cred_cache(context
,
460 NULL
, ctx
->prompter
, ctx
->keytab
,
470 asprintf(&service_name
, "%s@%s", KADM5_ADMIN_SERVICE
, ctx
->realm
);
472 asprintf(&service_name
, "%s", KADM5_ADMIN_SERVICE
);
474 if (service_name
== NULL
) {
477 krb5_clear_error_message(context
);
481 ret
= krb5_parse_name(context
, service_name
, &server
);
485 if(ctx
->ccache
== NULL
)
486 krb5_cc_close(context
, cc
);
492 ret
= krb5_sendauth(context
, &ctx
->ac
, &s
,
493 KADMIN_APPL_VERSION
, NULL
,
494 server
, AP_OPTS_MUTUAL_REQUIRED
,
495 NULL
, NULL
, cc
, NULL
, NULL
, NULL
);
498 kadm5_config_params p
;
499 memset(&p
, 0, sizeof(p
));
501 p
.mask
|= KADM5_CONFIG_REALM
;
502 p
.realm
= ctx
->realm
;
504 ret
= _kadm5_marshal_params(context
, &p
, ¶ms
);
506 ret
= krb5_write_priv_message(context
, ctx
->ac
, &s
, ¶ms
);
507 krb5_data_free(¶ms
);
511 if(ctx
->ccache
== NULL
)
512 krb5_cc_close(context
, cc
);
515 } else if(ret
== KRB5_SENDAUTH_BADAPPLVERS
) {
518 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
521 krb5_clear_error_message(context
);
524 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
527 krb5_clear_error_message(context
);
530 ret
= krb5_sendauth(context
, &ctx
->ac
, &s
,
531 KADMIN_OLD_APPL_VERSION
, NULL
,
532 server
, AP_OPTS_MUTUAL_REQUIRED
,
533 NULL
, NULL
, cc
, NULL
, NULL
, NULL
);
541 krb5_free_principal(context
, server
);
542 if(ctx
->ccache
== NULL
)
543 krb5_cc_close(context
, cc
);
550 _kadm5_connect(void *handle
)
552 kadm5_client_context
*ctx
= handle
;
554 return kadm_connect(ctx
);
559 kadm5_c_init_with_context(krb5_context context
,
560 const char *client_name
,
561 const char *password
,
562 krb5_prompter_fct prompter
,
565 const char *service_name
,
566 kadm5_config_params
*realm_params
,
567 unsigned long struct_version
,
568 unsigned long api_version
,
569 void **server_handle
)
572 kadm5_client_context
*ctx
;
575 ret
= _kadm5_c_init_context(&ctx
, realm_params
, context
);
579 if(password
!= NULL
&& *password
!= '\0') {
580 ret
= _kadm5_c_get_cred_cache(context
,
583 password
, prompter
, keytab
, ccache
, &cc
);
585 return ret
; /* XXX */
590 if (client_name
!= NULL
)
591 ctx
->client_name
= strdup(client_name
);
593 ctx
->client_name
= NULL
;
594 if (service_name
!= NULL
)
595 ctx
->service_name
= strdup(service_name
);
597 ctx
->service_name
= NULL
;
598 ctx
->prompter
= prompter
;
599 ctx
->keytab
= keytab
;
600 ctx
->ccache
= ccache
;
601 /* maybe we should copy the params here */
604 *server_handle
= ctx
;
609 init_context(const char *client_name
,
610 const char *password
,
611 krb5_prompter_fct prompter
,
614 const char *service_name
,
615 kadm5_config_params
*realm_params
,
616 unsigned long struct_version
,
617 unsigned long api_version
,
618 void **server_handle
)
620 krb5_context context
;
622 kadm5_server_context
*ctx
;
624 ret
= krb5_init_context(&context
);
627 ret
= kadm5_c_init_with_context(context
,
639 krb5_free_context(context
);
642 ctx
= *server_handle
;
648 kadm5_c_init_with_password_ctx(krb5_context context
,
649 const char *client_name
,
650 const char *password
,
651 const char *service_name
,
652 kadm5_config_params
*realm_params
,
653 unsigned long struct_version
,
654 unsigned long api_version
,
655 void **server_handle
)
657 return kadm5_c_init_with_context(context
,
671 kadm5_c_init_with_password(const char *client_name
,
672 const char *password
,
673 const char *service_name
,
674 kadm5_config_params
*realm_params
,
675 unsigned long struct_version
,
676 unsigned long api_version
,
677 void **server_handle
)
679 return init_context(client_name
,
692 kadm5_c_init_with_skey_ctx(krb5_context context
,
693 const char *client_name
,
695 const char *service_name
,
696 kadm5_config_params
*realm_params
,
697 unsigned long struct_version
,
698 unsigned long api_version
,
699 void **server_handle
)
701 return kadm5_c_init_with_context(context
,
716 kadm5_c_init_with_skey(const char *client_name
,
718 const char *service_name
,
719 kadm5_config_params
*realm_params
,
720 unsigned long struct_version
,
721 unsigned long api_version
,
722 void **server_handle
)
724 return init_context(client_name
,
737 kadm5_c_init_with_creds_ctx(krb5_context context
,
738 const char *client_name
,
740 const char *service_name
,
741 kadm5_config_params
*realm_params
,
742 unsigned long struct_version
,
743 unsigned long api_version
,
744 void **server_handle
)
746 return kadm5_c_init_with_context(context
,
760 kadm5_c_init_with_creds(const char *client_name
,
762 const char *service_name
,
763 kadm5_config_params
*realm_params
,
764 unsigned long struct_version
,
765 unsigned long api_version
,
766 void **server_handle
)
768 return init_context(client_name
,
782 kadm5_init(char *client_name
, char *pass
,
784 kadm5_config_params
*realm_params
,
785 unsigned long struct_version
,
786 unsigned long api_version
,
787 void **server_handle
)