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 #include <sys/socket.h>
37 #include <netinet/in.h>
40 __RCSID("$Heimdal: init_c.c 21972 2007-10-18 19:11:15Z lha $"
44 set_funcs(kadm5_client_context
*c
)
46 #define SET(C, F) (C)->funcs.F = kadm5 ## _c_ ## F
47 SET(c
, chpass_principal
);
48 SET(c
, chpass_principal_with_key
);
49 SET(c
, create_principal
);
50 SET(c
, delete_principal
);
53 SET(c
, get_principal
);
54 SET(c
, get_principals
);
56 SET(c
, modify_principal
);
57 SET(c
, randkey_principal
);
58 SET(c
, rename_principal
);
62 _kadm5_c_init_context(kadm5_client_context
**ctx
,
63 kadm5_config_params
*params
,
69 *ctx
= malloc(sizeof(**ctx
));
72 memset(*ctx
, 0, sizeof(**ctx
));
73 krb5_add_et_list (context
, initialize_kadm5_error_table_r
);
75 (*ctx
)->context
= context
;
76 if(params
->mask
& KADM5_CONFIG_REALM
) {
78 (*ctx
)->realm
= strdup(params
->realm
);
79 if ((*ctx
)->realm
== NULL
)
82 ret
= krb5_get_default_realm((*ctx
)->context
, &(*ctx
)->realm
);
87 if(params
->mask
& KADM5_CONFIG_ADMIN_SERVER
)
88 (*ctx
)->admin_server
= strdup(params
->admin_server
);
92 ret
= krb5_get_krb_admin_hst (context
, &(*ctx
)->realm
, &hostlist
);
98 (*ctx
)->admin_server
= strdup(*hostlist
);
99 krb5_free_krbhst (context
, hostlist
);
102 if ((*ctx
)->admin_server
== NULL
) {
107 colon
= strchr ((*ctx
)->admin_server
, ':');
111 (*ctx
)->kadmind_port
= 0;
113 if(params
->mask
& KADM5_CONFIG_KADMIND_PORT
)
114 (*ctx
)->kadmind_port
= params
->kadmind_port
;
115 else if (colon
!= NULL
) {
118 (*ctx
)->kadmind_port
= htons(strtol (colon
, &end
, 0));
120 if ((*ctx
)->kadmind_port
== 0)
121 (*ctx
)->kadmind_port
= krb5_getportbyname (context
, "kerberos-adm",
126 static krb5_error_code
127 get_kadm_ticket(krb5_context context
,
129 krb5_principal client
,
130 const char *server_name
)
135 memset(&in
, 0, sizeof(in
));
137 ret
= krb5_parse_name(context
, server_name
, &in
.server
);
140 ret
= krb5_get_credentials(context
, 0, id
, &in
, &out
);
142 krb5_free_creds(context
, out
);
143 krb5_free_principal(context
, in
.server
);
147 static krb5_error_code
148 get_new_cache(krb5_context context
,
149 krb5_principal client
,
150 const char *password
,
151 krb5_prompter_fct prompter
,
153 const char *server_name
,
154 krb5_ccache
*ret_cache
)
158 krb5_get_init_creds_opt
*opt
;
161 ret
= krb5_get_init_creds_opt_alloc (context
, &opt
);
165 krb5_get_init_creds_opt_set_default_flags(context
, "kadmin",
166 krb5_principal_get_realm(context
,
171 krb5_get_init_creds_opt_set_forwardable (opt
, FALSE
);
172 krb5_get_init_creds_opt_set_proxiable (opt
, FALSE
);
174 if(password
== NULL
&& prompter
== NULL
) {
177 ret
= krb5_kt_default(context
, &kt
);
179 ret
= krb5_kt_resolve(context
, keytab
, &kt
);
181 krb5_get_init_creds_opt_free(context
, opt
);
184 ret
= krb5_get_init_creds_keytab (context
,
191 krb5_kt_close(context
, kt
);
193 ret
= krb5_get_init_creds_password (context
,
203 krb5_get_init_creds_opt_free(context
, opt
);
207 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
208 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
209 case KRB5KRB_AP_ERR_MODIFIED
:
210 return KADM5_BAD_PASSWORD
;
214 ret
= krb5_cc_gen_new(context
, &krb5_mcc_ops
, &id
);
217 ret
= krb5_cc_initialize (context
, id
, cred
.client
);
220 ret
= krb5_cc_store_cred (context
, id
, &cred
);
223 krb5_free_cred_contents (context
, &cred
);
229 * Check the credential cache `id´ to figure out what principal to use
230 * when talking to the kadmind. If there is a initial kadmin/admin@
231 * credential in the cache, use that client principal. Otherwise, use
232 * the client principals first component and add /admin to the
236 static krb5_error_code
237 get_cache_principal(krb5_context context
,
239 krb5_principal
*client
)
242 const char *name
, *inst
;
243 krb5_principal p1
, p2
;
245 ret
= krb5_cc_default(context
, id
);
251 ret
= krb5_cc_get_principal(context
, *id
, &p1
);
253 krb5_cc_close(context
, *id
);
258 ret
= krb5_make_principal(context
, &p2
, NULL
,
259 "kadmin", "admin", NULL
);
261 krb5_cc_close(context
, *id
);
263 krb5_free_principal(context
, p1
);
269 krb5_kdc_flags flags
;
272 memset(&in
, 0, sizeof(in
));
277 /* check for initial ticket kadmin/admin */
278 ret
= krb5_get_credentials_with_flags(context
, KRB5_GC_CACHED
, flags
,
280 krb5_free_principal(context
, p2
);
282 if (out
->flags
.b
.initial
) {
284 krb5_free_creds(context
, out
);
287 krb5_free_creds(context
, out
);
290 krb5_cc_close(context
, *id
);
293 name
= krb5_principal_get_comp_string(context
, p1
, 0);
294 inst
= krb5_principal_get_comp_string(context
, p1
, 1);
295 if(inst
== NULL
|| strcmp(inst
, "admin") != 0) {
296 ret
= krb5_make_principal(context
, &p2
, NULL
, name
, "admin", NULL
);
297 krb5_free_principal(context
, p1
);
311 _kadm5_c_get_cred_cache(krb5_context context
,
312 const char *client_name
,
313 const char *server_name
,
314 const char *password
,
315 krb5_prompter_fct prompter
,
318 krb5_ccache
*ret_cache
)
321 krb5_ccache id
= NULL
;
322 krb5_principal default_client
= NULL
, client
= NULL
;
324 /* treat empty password as NULL */
325 if(password
&& *password
== '\0')
327 if(server_name
== NULL
)
328 server_name
= KADM5_ADMIN_SERVICE
;
330 if(client_name
!= NULL
) {
331 ret
= krb5_parse_name(context
, client_name
, &client
);
338 ret
= krb5_cc_get_principal(context
, id
, &client
);
342 /* get principal from default cache, ok if this doesn't work */
344 ret
= get_cache_principal(context
, &id
, &default_client
);
347 * No client was specified by the caller and we cannot
348 * determine the client from a credentials cache.
352 user
= get_default_username ();
355 krb5_set_error_string(context
, "Unable to find local user name");
356 return KADM5_FAILURE
;
358 ret
= krb5_make_principal(context
, &default_client
,
359 NULL
, user
, "admin", NULL
);
367 * No client was specified by the caller, but we have a client
368 * from the default credentials cache.
370 if (client
== NULL
&& default_client
!= NULL
)
371 client
= default_client
;
374 if(id
&& (default_client
== NULL
||
375 krb5_principal_compare(context
, client
, default_client
))) {
376 ret
= get_kadm_ticket(context
, id
, client
, server_name
);
379 krb5_free_principal(context
, default_client
);
380 if (default_client
!= client
)
381 krb5_free_principal(context
, client
);
385 /* couldn't get ticket from cache */
388 /* get creds via AS request */
389 if(id
&& (id
!= ccache
))
390 krb5_cc_close(context
, id
);
391 if (client
!= default_client
)
392 krb5_free_principal(context
, default_client
);
394 ret
= get_new_cache(context
, client
, password
, prompter
, keytab
,
395 server_name
, ret_cache
);
396 krb5_free_principal(context
, client
);
401 kadm_connect(kadm5_client_context
*ctx
)
404 krb5_principal server
;
407 struct addrinfo
*ai
, *a
;
408 struct addrinfo hints
;
410 char portstr
[NI_MAXSERV
];
411 char *hostname
, *slash
;
413 krb5_context context
= ctx
->context
;
415 memset (&hints
, 0, sizeof(hints
));
416 hints
.ai_socktype
= SOCK_STREAM
;
417 hints
.ai_protocol
= IPPROTO_TCP
;
419 snprintf (portstr
, sizeof(portstr
), "%u", ntohs(ctx
->kadmind_port
));
421 hostname
= ctx
->admin_server
;
422 slash
= strchr (hostname
, '/');
424 hostname
= slash
+ 1;
426 error
= getaddrinfo (hostname
, portstr
, &hints
, &ai
);
428 krb5_clear_error_string(context
);
429 return KADM5_BAD_SERVER_NAME
;
432 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
433 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
436 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
437 krb5_clear_error_string(context
);
438 krb5_warn (context
, errno
, "connect(%s)", hostname
);
446 krb5_clear_error_string(context
);
447 krb5_warnx (context
, "failed to contact %s", hostname
);
448 return KADM5_FAILURE
;
450 ret
= _kadm5_c_get_cred_cache(context
,
453 NULL
, ctx
->prompter
, ctx
->keytab
,
463 asprintf(&service_name
, "%s@%s", KADM5_ADMIN_SERVICE
, ctx
->realm
);
465 asprintf(&service_name
, "%s", KADM5_ADMIN_SERVICE
);
467 if (service_name
== NULL
) {
470 krb5_clear_error_string(context
);
474 ret
= krb5_parse_name(context
, service_name
, &server
);
478 if(ctx
->ccache
== NULL
)
479 krb5_cc_close(context
, cc
);
485 ret
= krb5_sendauth(context
, &ctx
->ac
, &s
,
486 KADMIN_APPL_VERSION
, NULL
,
487 server
, AP_OPTS_MUTUAL_REQUIRED
,
488 NULL
, NULL
, cc
, NULL
, NULL
, NULL
);
491 kadm5_config_params p
;
492 memset(&p
, 0, sizeof(p
));
494 p
.mask
|= KADM5_CONFIG_REALM
;
495 p
.realm
= ctx
->realm
;
497 ret
= _kadm5_marshal_params(context
, &p
, ¶ms
);
499 ret
= krb5_write_priv_message(context
, ctx
->ac
, &s
, ¶ms
);
500 krb5_data_free(¶ms
);
504 if(ctx
->ccache
== NULL
)
505 krb5_cc_close(context
, cc
);
508 } else if(ret
== KRB5_SENDAUTH_BADAPPLVERS
) {
511 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
514 krb5_clear_error_string(context
);
517 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
520 krb5_clear_error_string(context
);
523 ret
= krb5_sendauth(context
, &ctx
->ac
, &s
,
524 KADMIN_OLD_APPL_VERSION
, NULL
,
525 server
, AP_OPTS_MUTUAL_REQUIRED
,
526 NULL
, NULL
, cc
, NULL
, NULL
, NULL
);
534 krb5_free_principal(context
, server
);
535 if(ctx
->ccache
== NULL
)
536 krb5_cc_close(context
, cc
);
543 _kadm5_connect(void *handle
)
545 kadm5_client_context
*ctx
= handle
;
547 return kadm_connect(ctx
);
552 kadm5_c_init_with_context(krb5_context context
,
553 const char *client_name
,
554 const char *password
,
555 krb5_prompter_fct prompter
,
558 const char *service_name
,
559 kadm5_config_params
*realm_params
,
560 unsigned long struct_version
,
561 unsigned long api_version
,
562 void **server_handle
)
565 kadm5_client_context
*ctx
;
568 ret
= _kadm5_c_init_context(&ctx
, realm_params
, context
);
572 if(password
!= NULL
&& *password
!= '\0') {
573 ret
= _kadm5_c_get_cred_cache(context
,
576 password
, prompter
, keytab
, ccache
, &cc
);
578 return ret
; /* XXX */
583 if (client_name
!= NULL
)
584 ctx
->client_name
= strdup(client_name
);
586 ctx
->client_name
= NULL
;
587 if (service_name
!= NULL
)
588 ctx
->service_name
= strdup(service_name
);
590 ctx
->service_name
= NULL
;
591 ctx
->prompter
= prompter
;
592 ctx
->keytab
= keytab
;
593 ctx
->ccache
= ccache
;
594 /* maybe we should copy the params here */
597 *server_handle
= ctx
;
602 init_context(const char *client_name
,
603 const char *password
,
604 krb5_prompter_fct prompter
,
607 const char *service_name
,
608 kadm5_config_params
*realm_params
,
609 unsigned long struct_version
,
610 unsigned long api_version
,
611 void **server_handle
)
613 krb5_context context
;
615 kadm5_server_context
*ctx
;
617 ret
= krb5_init_context(&context
);
620 ret
= kadm5_c_init_with_context(context
,
632 krb5_free_context(context
);
635 ctx
= *server_handle
;
641 kadm5_c_init_with_password_ctx(krb5_context context
,
642 const char *client_name
,
643 const char *password
,
644 const char *service_name
,
645 kadm5_config_params
*realm_params
,
646 unsigned long struct_version
,
647 unsigned long api_version
,
648 void **server_handle
)
650 return kadm5_c_init_with_context(context
,
664 kadm5_c_init_with_password(const char *client_name
,
665 const char *password
,
666 const char *service_name
,
667 kadm5_config_params
*realm_params
,
668 unsigned long struct_version
,
669 unsigned long api_version
,
670 void **server_handle
)
672 return init_context(client_name
,
685 kadm5_c_init_with_skey_ctx(krb5_context context
,
686 const char *client_name
,
688 const char *service_name
,
689 kadm5_config_params
*realm_params
,
690 unsigned long struct_version
,
691 unsigned long api_version
,
692 void **server_handle
)
694 return kadm5_c_init_with_context(context
,
709 kadm5_c_init_with_skey(const char *client_name
,
711 const char *service_name
,
712 kadm5_config_params
*realm_params
,
713 unsigned long struct_version
,
714 unsigned long api_version
,
715 void **server_handle
)
717 return init_context(client_name
,
730 kadm5_c_init_with_creds_ctx(krb5_context context
,
731 const char *client_name
,
733 const char *service_name
,
734 kadm5_config_params
*realm_params
,
735 unsigned long struct_version
,
736 unsigned long api_version
,
737 void **server_handle
)
739 return kadm5_c_init_with_context(context
,
753 kadm5_c_init_with_creds(const char *client_name
,
755 const char *service_name
,
756 kadm5_config_params
*realm_params
,
757 unsigned long struct_version
,
758 unsigned long api_version
,
759 void **server_handle
)
761 return init_context(client_name
,
775 kadm5_init(char *client_name
, char *pass
,
777 kadm5_config_params
*realm_params
,
778 unsigned long struct_version
,
779 unsigned long api_version
,
780 void **server_handle
)