Merge pull request #174 from abhinav-upadhyay/fix-krb5.conf.5
[heimdal.git] / lib / kadm5 / init_c.c
blobfa4bc4b9a0c50829b8fea8f522cf937e3b192c38
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "kadm5_locl.h"
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
41 #endif
42 #ifdef HAVE_NETDB_H
43 #include <netdb.h>
44 #endif
46 RCSID("$Id$");
48 static kadm5_ret_t
49 kadm5_c_lock(void *server_handle)
51 return ENOTSUP;
54 static kadm5_ret_t
55 kadm5_c_unlock(void *server_handle)
57 return ENOTSUP;
60 static void
61 set_funcs(kadm5_client_context *c)
63 #define SET(C, F) (C)->funcs.F = kadm5 ## _c_ ## F
64 #define SETNOTIMP(C, F) (C)->funcs.F = 0
65 SET(c, chpass_principal);
66 SET(c, chpass_principal_with_key);
67 SET(c, create_principal);
68 SET(c, delete_principal);
69 SET(c, destroy);
70 SET(c, flush);
71 SET(c, get_principal);
72 SET(c, get_principals);
73 SET(c, get_privs);
74 SET(c, modify_principal);
75 SET(c, randkey_principal);
76 SET(c, rename_principal);
77 SET(c, lock);
78 SET(c, unlock);
79 SETNOTIMP(c, setkey_principal_3);
82 kadm5_ret_t
83 _kadm5_c_init_context(kadm5_client_context **ctx,
84 kadm5_config_params *params,
85 krb5_context context)
87 krb5_error_code ret;
88 char *colon;
90 *ctx = malloc(sizeof(**ctx));
91 if(*ctx == NULL)
92 return ENOMEM;
93 memset(*ctx, 0, sizeof(**ctx));
94 krb5_add_et_list (context, initialize_kadm5_error_table_r);
95 set_funcs(*ctx);
96 (*ctx)->context = context;
97 if(params->mask & KADM5_CONFIG_REALM) {
98 ret = 0;
99 (*ctx)->realm = strdup(params->realm);
100 if ((*ctx)->realm == NULL)
101 ret = ENOMEM;
102 } else
103 ret = krb5_get_default_realm((*ctx)->context, &(*ctx)->realm);
104 if (ret) {
105 free(*ctx);
106 return ret;
108 if(params->mask & KADM5_CONFIG_ADMIN_SERVER)
109 (*ctx)->admin_server = strdup(params->admin_server);
110 else {
111 char **hostlist;
113 ret = krb5_get_krb_admin_hst (context, &(*ctx)->realm, &hostlist);
114 if (ret) {
115 free((*ctx)->realm);
116 free(*ctx);
117 return ret;
119 (*ctx)->admin_server = strdup(*hostlist);
120 krb5_free_krbhst (context, hostlist);
123 if ((*ctx)->admin_server == NULL) {
124 free((*ctx)->realm);
125 free(*ctx);
126 return ENOMEM;
128 colon = strchr ((*ctx)->admin_server, ':');
129 if (colon != NULL)
130 *colon++ = '\0';
132 (*ctx)->kadmind_port = 0;
134 if(params->mask & KADM5_CONFIG_KADMIND_PORT)
135 (*ctx)->kadmind_port = params->kadmind_port;
136 else if (colon != NULL) {
137 char *end;
139 (*ctx)->kadmind_port = htons(strtol (colon, &end, 0));
141 if ((*ctx)->kadmind_port == 0)
142 (*ctx)->kadmind_port = krb5_getportbyname (context, "kerberos-adm",
143 "tcp", 749);
144 return 0;
147 static krb5_error_code
148 get_kadm_ticket(krb5_context context,
149 krb5_ccache id,
150 krb5_principal client,
151 const char *server_name)
153 krb5_error_code ret;
154 krb5_creds in, *out;
156 memset(&in, 0, sizeof(in));
157 in.client = client;
158 ret = krb5_parse_name(context, server_name, &in.server);
159 if(ret)
160 return ret;
161 ret = krb5_get_credentials(context, 0, id, &in, &out);
162 if(ret == 0)
163 krb5_free_creds(context, out);
164 krb5_free_principal(context, in.server);
165 return ret;
168 static krb5_error_code
169 get_new_cache(krb5_context context,
170 krb5_principal client,
171 const char *password,
172 krb5_prompter_fct prompter,
173 const char *keytab,
174 const char *server_name,
175 krb5_ccache *ret_cache)
177 krb5_error_code ret;
178 krb5_creds cred;
179 krb5_get_init_creds_opt *opt;
180 krb5_ccache id;
182 ret = krb5_get_init_creds_opt_alloc (context, &opt);
183 if (ret)
184 return ret;
186 krb5_get_init_creds_opt_set_default_flags(context, "kadmin",
187 krb5_principal_get_realm(context,
188 client),
189 opt);
192 krb5_get_init_creds_opt_set_forwardable (opt, FALSE);
193 krb5_get_init_creds_opt_set_proxiable (opt, FALSE);
195 if(password == NULL && prompter == NULL) {
196 krb5_keytab kt;
197 if(keytab == NULL)
198 ret = krb5_kt_default(context, &kt);
199 else
200 ret = krb5_kt_resolve(context, keytab, &kt);
201 if(ret) {
202 krb5_get_init_creds_opt_free(context, opt);
203 return ret;
205 ret = krb5_get_init_creds_keytab (context,
206 &cred,
207 client,
210 server_name,
211 opt);
212 krb5_kt_close(context, kt);
213 } else {
214 ret = krb5_get_init_creds_password (context,
215 &cred,
216 client,
217 password,
218 prompter,
219 NULL,
221 server_name,
222 opt);
224 krb5_get_init_creds_opt_free(context, opt);
225 switch(ret){
226 case 0:
227 break;
228 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
229 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
230 case KRB5KRB_AP_ERR_MODIFIED:
231 return KADM5_BAD_PASSWORD;
232 default:
233 return ret;
235 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &id);
236 if(ret)
237 return ret;
238 ret = krb5_cc_initialize (context, id, cred.client);
239 if (ret)
240 return ret;
241 ret = krb5_cc_store_cred (context, id, &cred);
242 if (ret)
243 return ret;
244 krb5_free_cred_contents (context, &cred);
245 *ret_cache = id;
246 return 0;
250 * Check the credential cache `id´ to figure out what principal to use
251 * when talking to the kadmind. If there is a initial kadmin/admin@
252 * credential in the cache, use that client principal. Otherwise, use
253 * the client principals first component and add /admin to the
254 * principal.
257 static krb5_error_code
258 get_cache_principal(krb5_context context,
259 krb5_ccache *id,
260 krb5_principal *client)
262 krb5_error_code ret;
263 const char *name, *inst;
264 krb5_principal p1, p2;
266 ret = krb5_cc_default(context, id);
267 if(ret) {
268 *id = NULL;
269 return ret;
272 ret = krb5_cc_get_principal(context, *id, &p1);
273 if(ret) {
274 krb5_cc_close(context, *id);
275 *id = NULL;
276 return ret;
279 ret = krb5_make_principal(context, &p2, NULL,
280 "kadmin", "admin", NULL);
281 if (ret) {
282 krb5_cc_close(context, *id);
283 *id = NULL;
284 krb5_free_principal(context, p1);
285 return ret;
289 krb5_creds in, *out;
290 krb5_kdc_flags flags;
292 flags.i = 0;
293 memset(&in, 0, sizeof(in));
295 in.client = p1;
296 in.server = p2;
298 /* check for initial ticket kadmin/admin */
299 ret = krb5_get_credentials_with_flags(context, KRB5_GC_CACHED, flags,
300 *id, &in, &out);
301 krb5_free_principal(context, p2);
302 if (ret == 0) {
303 if (out->flags.b.initial) {
304 *client = p1;
305 krb5_free_creds(context, out);
306 return 0;
308 krb5_free_creds(context, out);
311 krb5_cc_close(context, *id);
312 *id = NULL;
314 name = krb5_principal_get_comp_string(context, p1, 0);
315 inst = krb5_principal_get_comp_string(context, p1, 1);
316 if(inst == NULL || strcmp(inst, "admin") != 0) {
317 ret = krb5_make_principal(context, &p2, NULL, name, "admin", NULL);
318 krb5_free_principal(context, p1);
319 if(ret != 0)
320 return ret;
322 *client = p2;
323 return 0;
326 *client = p1;
328 return 0;
331 krb5_error_code
332 _kadm5_c_get_cred_cache(krb5_context context,
333 const char *client_name,
334 const char *server_name,
335 const char *password,
336 krb5_prompter_fct prompter,
337 const char *keytab,
338 krb5_ccache ccache,
339 krb5_ccache *ret_cache)
341 krb5_error_code ret;
342 krb5_ccache id = NULL;
343 krb5_principal default_client = NULL, client = NULL;
345 /* treat empty password as NULL */
346 if(password && *password == '\0')
347 password = NULL;
348 if(server_name == NULL)
349 server_name = KADM5_ADMIN_SERVICE;
351 if(client_name != NULL) {
352 ret = krb5_parse_name(context, client_name, &client);
353 if(ret)
354 return ret;
357 if(ccache != NULL) {
358 id = ccache;
359 ret = krb5_cc_get_principal(context, id, &client);
360 if(ret)
361 return ret;
362 } else {
363 /* get principal from default cache, ok if this doesn't work */
365 ret = get_cache_principal(context, &id, &default_client);
366 if (ret) {
368 * No client was specified by the caller and we cannot
369 * determine the client from a credentials cache.
371 const char *user;
373 user = get_default_username ();
375 if(user == NULL) {
376 krb5_set_error_message(context, KADM5_FAILURE, "Unable to find local user name");
377 return KADM5_FAILURE;
379 ret = krb5_make_principal(context, &default_client,
380 NULL, user, "admin", NULL);
381 if(ret)
382 return ret;
388 * No client was specified by the caller, but we have a client
389 * from the default credentials cache.
391 if (client == NULL && default_client != NULL)
392 client = default_client;
395 if(id && client && (default_client == NULL ||
396 krb5_principal_compare(context, client, default_client) != 0)) {
397 ret = get_kadm_ticket(context, id, client, server_name);
398 if(ret == 0) {
399 *ret_cache = id;
400 krb5_free_principal(context, default_client);
401 if (default_client != client)
402 krb5_free_principal(context, client);
403 return 0;
405 if(ccache != NULL)
406 /* couldn't get ticket from cache */
407 return -1;
409 /* get creds via AS request */
410 if(id && (id != ccache))
411 krb5_cc_close(context, id);
412 if (client != default_client)
413 krb5_free_principal(context, default_client);
415 ret = get_new_cache(context, client, password, prompter, keytab,
416 server_name, ret_cache);
417 krb5_free_principal(context, client);
418 return ret;
421 static kadm5_ret_t
422 kadm_connect(kadm5_client_context *ctx)
424 kadm5_ret_t ret;
425 krb5_principal server;
426 krb5_ccache cc;
427 rk_socket_t s = rk_INVALID_SOCKET;
428 struct addrinfo *ai, *a;
429 struct addrinfo hints;
430 int error;
431 char portstr[NI_MAXSERV];
432 char *hostname, *slash;
433 char *service_name;
434 krb5_context context = ctx->context;
436 memset (&hints, 0, sizeof(hints));
437 hints.ai_socktype = SOCK_STREAM;
438 hints.ai_protocol = IPPROTO_TCP;
440 snprintf (portstr, sizeof(portstr), "%u", ntohs(ctx->kadmind_port));
442 hostname = ctx->admin_server;
443 slash = strchr (hostname, '/');
444 if (slash != NULL)
445 hostname = slash + 1;
447 error = getaddrinfo (hostname, portstr, &hints, &ai);
448 if (error) {
449 krb5_clear_error_message(context);
450 return KADM5_BAD_SERVER_NAME;
453 for (a = ai; a != NULL; a = a->ai_next) {
454 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
455 if (s < 0)
456 continue;
457 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
458 krb5_clear_error_message(context);
459 krb5_warn (context, errno, "connect(%s)", hostname);
460 rk_closesocket (s);
461 continue;
463 break;
465 if (a == NULL) {
466 freeaddrinfo (ai);
467 krb5_clear_error_message(context);
468 krb5_warnx (context, "failed to contact %s", hostname);
469 return KADM5_FAILURE;
471 ret = _kadm5_c_get_cred_cache(context,
472 ctx->client_name,
473 ctx->service_name,
474 NULL, ctx->prompter, ctx->keytab,
475 ctx->ccache, &cc);
477 if(ret) {
478 freeaddrinfo (ai);
479 rk_closesocket(s);
480 return ret;
483 if (ctx->realm)
484 error = asprintf(&service_name, "%s@%s", KADM5_ADMIN_SERVICE,
485 ctx->realm);
486 else
487 error = asprintf(&service_name, "%s", KADM5_ADMIN_SERVICE);
489 if (error == -1 || service_name == NULL) {
490 freeaddrinfo (ai);
491 rk_closesocket(s);
492 krb5_clear_error_message(context);
493 return ENOMEM;
496 ret = krb5_parse_name(context, service_name, &server);
497 free(service_name);
498 if(ret) {
499 freeaddrinfo (ai);
500 if(ctx->ccache == NULL)
501 krb5_cc_close(context, cc);
502 rk_closesocket(s);
503 return ret;
505 ctx->ac = NULL;
507 ret = krb5_sendauth(context, &ctx->ac, &s,
508 KADMIN_APPL_VERSION, NULL,
509 server, AP_OPTS_MUTUAL_REQUIRED,
510 NULL, NULL, cc, NULL, NULL, NULL);
511 if(ret == 0) {
512 krb5_data params;
513 kadm5_config_params p;
514 memset(&p, 0, sizeof(p));
515 if(ctx->realm) {
516 p.mask |= KADM5_CONFIG_REALM;
517 p.realm = ctx->realm;
519 ret = _kadm5_marshal_params(context, &p, &params);
521 ret = krb5_write_priv_message(context, ctx->ac, &s, &params);
522 krb5_data_free(&params);
523 if(ret) {
524 freeaddrinfo (ai);
525 rk_closesocket(s);
526 if(ctx->ccache == NULL)
527 krb5_cc_close(context, cc);
528 return ret;
530 } else if(ret == KRB5_SENDAUTH_BADAPPLVERS) {
531 rk_closesocket(s);
533 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
534 if (s < 0) {
535 freeaddrinfo (ai);
536 krb5_clear_error_message(context);
537 return errno;
539 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
540 rk_closesocket (s);
541 freeaddrinfo (ai);
542 krb5_clear_error_message(context);
543 return errno;
545 ret = krb5_sendauth(context, &ctx->ac, &s,
546 KADMIN_OLD_APPL_VERSION, NULL,
547 server, AP_OPTS_MUTUAL_REQUIRED,
548 NULL, NULL, cc, NULL, NULL, NULL);
550 freeaddrinfo (ai);
551 if(ret) {
552 rk_closesocket(s);
553 return ret;
556 krb5_free_principal(context, server);
557 if(ctx->ccache == NULL)
558 krb5_cc_close(context, cc);
559 ctx->sock = s;
561 return 0;
564 kadm5_ret_t
565 _kadm5_connect(void *handle)
567 kadm5_client_context *ctx = handle;
568 if(ctx->sock == -1)
569 return kadm_connect(ctx);
570 return 0;
573 static kadm5_ret_t
574 kadm5_c_init_with_context(krb5_context context,
575 const char *client_name,
576 const char *password,
577 krb5_prompter_fct prompter,
578 const char *keytab,
579 krb5_ccache ccache,
580 const char *service_name,
581 kadm5_config_params *realm_params,
582 unsigned long struct_version,
583 unsigned long api_version,
584 void **server_handle)
586 kadm5_ret_t ret;
587 kadm5_client_context *ctx;
588 krb5_ccache cc;
590 ret = _kadm5_c_init_context(&ctx, realm_params, context);
591 if(ret)
592 return ret;
594 if(password != NULL && *password != '\0') {
595 ret = _kadm5_c_get_cred_cache(context,
596 client_name,
597 service_name,
598 password, prompter, keytab, ccache, &cc);
599 if(ret)
600 return ret; /* XXX */
601 ccache = cc;
605 if (client_name != NULL)
606 ctx->client_name = strdup(client_name);
607 else
608 ctx->client_name = NULL;
609 if (service_name != NULL)
610 ctx->service_name = strdup(service_name);
611 else
612 ctx->service_name = NULL;
613 ctx->prompter = prompter;
614 ctx->keytab = keytab;
615 ctx->ccache = ccache;
616 /* maybe we should copy the params here */
617 ctx->sock = -1;
619 *server_handle = ctx;
620 return 0;
623 static kadm5_ret_t
624 init_context(const char *client_name,
625 const char *password,
626 krb5_prompter_fct prompter,
627 const char *keytab,
628 krb5_ccache ccache,
629 const char *service_name,
630 kadm5_config_params *realm_params,
631 unsigned long struct_version,
632 unsigned long api_version,
633 void **server_handle)
635 krb5_context context;
636 kadm5_ret_t ret;
637 kadm5_server_context *ctx;
639 ret = krb5_init_context(&context);
640 if (ret)
641 return ret;
642 ret = kadm5_c_init_with_context(context,
643 client_name,
644 password,
645 prompter,
646 keytab,
647 ccache,
648 service_name,
649 realm_params,
650 struct_version,
651 api_version,
652 server_handle);
653 if(ret){
654 krb5_free_context(context);
655 return ret;
657 ctx = *server_handle;
658 ctx->my_context = 1;
659 return 0;
662 kadm5_ret_t
663 kadm5_c_init_with_password_ctx(krb5_context context,
664 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 kadm5_c_init_with_context(context,
673 client_name,
674 password,
675 krb5_prompter_posix,
676 NULL,
677 NULL,
678 service_name,
679 realm_params,
680 struct_version,
681 api_version,
682 server_handle);
685 kadm5_ret_t
686 kadm5_c_init_with_password(const char *client_name,
687 const char *password,
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 init_context(client_name,
695 password,
696 krb5_prompter_posix,
697 NULL,
698 NULL,
699 service_name,
700 realm_params,
701 struct_version,
702 api_version,
703 server_handle);
706 kadm5_ret_t
707 kadm5_c_init_with_skey_ctx(krb5_context context,
708 const char *client_name,
709 const char *keytab,
710 const char *service_name,
711 kadm5_config_params *realm_params,
712 unsigned long struct_version,
713 unsigned long api_version,
714 void **server_handle)
716 return kadm5_c_init_with_context(context,
717 client_name,
718 NULL,
719 NULL,
720 keytab,
721 NULL,
722 service_name,
723 realm_params,
724 struct_version,
725 api_version,
726 server_handle);
730 kadm5_ret_t
731 kadm5_c_init_with_skey(const char *client_name,
732 const char *keytab,
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 init_context(client_name,
740 NULL,
741 NULL,
742 keytab,
743 NULL,
744 service_name,
745 realm_params,
746 struct_version,
747 api_version,
748 server_handle);
751 kadm5_ret_t
752 kadm5_c_init_with_creds_ctx(krb5_context context,
753 const char *client_name,
754 krb5_ccache ccache,
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 kadm5_c_init_with_context(context,
762 client_name,
763 NULL,
764 NULL,
765 NULL,
766 ccache,
767 service_name,
768 realm_params,
769 struct_version,
770 api_version,
771 server_handle);
774 kadm5_ret_t
775 kadm5_c_init_with_creds(const char *client_name,
776 krb5_ccache ccache,
777 const char *service_name,
778 kadm5_config_params *realm_params,
779 unsigned long struct_version,
780 unsigned long api_version,
781 void **server_handle)
783 return init_context(client_name,
784 NULL,
785 NULL,
786 NULL,
787 ccache,
788 service_name,
789 realm_params,
790 struct_version,
791 api_version,
792 server_handle);
795 #if 0
796 kadm5_ret_t
797 kadm5_init(char *client_name, char *pass,
798 char *service_name,
799 kadm5_config_params *realm_params,
800 unsigned long struct_version,
801 unsigned long api_version,
802 void **server_handle)
805 #endif