Debian Codegraph @

lib/kadm5/init_c.c

raw
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
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
45
46 RCSID("$Id$");
47
48 static kadm5_ret_t
49 kadm5_c_lock(void *server_handle)
50 {
51 return ENOTSUP;
52 }
53
54 static kadm5_ret_t
55 kadm5_c_unlock(void *server_handle)
56 {
57 return ENOTSUP;
58 }
59
60 static void
61 set_funcs(kadm5_client_context *c)
62 {
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);
80 }
81
82 kadm5_ret_t
83 _kadm5_c_init_context(kadm5_client_context **ctx,
84 kadm5_config_params *params,
85 krb5_context context)
86 {
87 krb5_error_code ret;
88 char *colon;
89
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;
107 }
108 if(params->mask & KADM5_CONFIG_ADMIN_SERVER)
109 (*ctx)->admin_server = strdup(params->admin_server);
110 else {
111 char **hostlist;
112
113 ret = krb5_get_krb_admin_hst (context, &(*ctx)->realm, &hostlist);
114 if (ret) {
115 free((*ctx)->realm);
116 free(*ctx);
117 return ret;
118 }
119 (*ctx)->admin_server = strdup(*hostlist);
120 krb5_free_krbhst (context, hostlist);
121 }
122
123 if ((*ctx)->admin_server == NULL) {
124 free((*ctx)->realm);
125 free(*ctx);
126 return ENOMEM;
127 }
128 colon = strchr ((*ctx)->admin_server, ':');
129 if (colon != NULL)
130 *colon++ = '\0';
131
132 (*ctx)->kadmind_port = 0;
133
134 if(params->mask & KADM5_CONFIG_KADMIND_PORT)
135 (*ctx)->kadmind_port = params->kadmind_port;
136 else if (colon != NULL) {
137 char *end;
138
139 (*ctx)->kadmind_port = htons(strtol (colon, &end, 0));
140 }
141 if ((*ctx)->kadmind_port == 0)
142 (*ctx)->kadmind_port = krb5_getportbyname (context, "kerberos-adm",
143 "tcp", 749);
144 return 0;
145 }
146
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)
152 {
153 krb5_error_code ret;
154 krb5_creds in, *out;
155
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;
166 }
167
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)
176 {
177 krb5_error_code ret;
178 krb5_creds cred;
179 krb5_get_init_creds_opt *opt;
180 krb5_ccache id;
181
182 ret = krb5_get_init_creds_opt_alloc (context, &opt);
183 if (ret)
184 return ret;
185
186 krb5_get_init_creds_opt_set_default_flags(context, "kadmin",
187 krb5_principal_get_realm(context,
188 client),
189 opt);
190
191
192 krb5_get_init_creds_opt_set_forwardable (opt, FALSE);
193 krb5_get_init_creds_opt_set_proxiable (opt, FALSE);
194
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;
204 }
205 ret = krb5_get_init_creds_keytab (context,
206 &cred,
207 client,
208 kt,
209 0,
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,
220 0,
221 server_name,
222 opt);
223 }
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;
234 }
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;
247 }
248
249 /*
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.
255 */
256
257 static krb5_error_code
258 get_cache_principal(krb5_context context,
259 krb5_ccache *id,
260 krb5_principal *client)
261 {
262 krb5_error_code ret;
263 const char *name, *inst;
264 krb5_principal p1, p2;
265
266 ret = krb5_cc_default(context, id);
267 if(ret) {
268 *id = NULL;
269 return ret;
270 }
271
272 ret = krb5_cc_get_principal(context, *id, &p1);
273 if(ret) {
274 krb5_cc_close(context, *id);
275 *id = NULL;
276 return ret;
277 }
278
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;
286 }
287
288 {
289 krb5_creds in, *out;
290 krb5_kdc_flags flags;
291
292 flags.i = 0;
293 memset(&in, 0, sizeof(in));
294
295 in.client = p1;
296 in.server = p2;
297
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;
307 }
308 krb5_free_creds(context, out);
309 }
310 }
311 krb5_cc_close(context, *id);
312 *id = NULL;
313
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;
321
322 *client = p2;
323 return 0;
324 }
325
326 *client = p1;
327
328 return 0;
329 }
330
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)
340 {
341 krb5_error_code ret;
342 krb5_ccache id = NULL;
343 krb5_principal default_client = NULL, client = NULL;
344
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;
350
351 if(client_name != NULL) {
352 ret = krb5_parse_name(context, client_name, &client);
353 if(ret)
354 return ret;
355 }
356
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 */
364
365 ret = get_cache_principal(context, &id, &default_client);
366 if (ret) {
367 /*
368 * No client was specified by the caller and we cannot
369 * determine the client from a credentials cache.
370 */
371 const char *user;
372
373 user = get_default_username ();
374
375 if(user == NULL) {
376 krb5_set_error_message(context, KADM5_FAILURE, "Unable to find local user name");
377 return KADM5_FAILURE;
378 }
379 ret = krb5_make_principal(context, &default_client,
380 NULL, user, "admin", NULL);
381 if(ret)
382 return ret;
383 }
384 }
385
386
387 /*
388 * No client was specified by the caller, but we have a client
389 * from the default credentials cache.
390 */
391 if (client == NULL && default_client != NULL)
392 client = default_client;
393
394
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;
404 }
405 if(ccache != NULL)
406 /* couldn't get ticket from cache */
407 return -1;
408 }
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);
414
415 ret = get_new_cache(context, client, password, prompter, keytab,
416 server_name, ret_cache);
417 krb5_free_principal(context, client);
418 return ret;
419 }
420
421 static kadm5_ret_t
422 kadm_connect(kadm5_client_context *ctx)
423 {
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;
435
436 memset (&hints, 0, sizeof(hints));
437 hints.ai_socktype = SOCK_STREAM;
438 hints.ai_protocol = IPPROTO_TCP;
439
440 snprintf (portstr, sizeof(portstr), "%u", ntohs(ctx->kadmind_port));
441
442 hostname = ctx->admin_server;
443 slash = strchr (hostname, '/');
444 if (slash != NULL)
445 hostname = slash + 1;
446
447 error = getaddrinfo (hostname, portstr, &hints, &ai);
448 if (error) {
449 krb5_clear_error_message(context);
450 return KADM5_BAD_SERVER_NAME;
451 }
452
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;
462 }
463 break;
464 }
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;
470 }
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);
476
477 if(ret) {
478 freeaddrinfo (ai);
479 rk_closesocket(s);
480 return ret;
481 }
482
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);
488
489 if (error == -1 || service_name == NULL) {
490 freeaddrinfo (ai);
491 rk_closesocket(s);
492 krb5_clear_error_message(context);
493 return ENOMEM;
494 }
495
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;
504 }
505 ctx->ac = NULL;
506
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;
518 }
519 ret = _kadm5_marshal_params(context, &p, &params);
520
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;
529 }
530 } else if(ret == KRB5_SENDAUTH_BADAPPLVERS) {
531 rk_closesocket(s);
532
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;
538 }
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;
544 }
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);
549 }
550 freeaddrinfo (ai);
551 if(ret) {
552 rk_closesocket(s);
553 return ret;
554 }
555
556 krb5_free_principal(context, server);
557 if(ctx->ccache == NULL)
558 krb5_cc_close(context, cc);
559 ctx->sock = s;
560
561 return 0;
562 }
563
564 kadm5_ret_t
565 _kadm5_connect(void *handle)
566 {
567 kadm5_client_context *ctx = handle;
568 if(ctx->sock == -1)
569 return kadm_connect(ctx);
570 return 0;
571 }
572
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)
585 {
586 kadm5_ret_t ret;
587 kadm5_client_context *ctx = NULL;
588 krb5_ccache cc;
589
590 ret = _kadm5_c_init_context(&ctx, realm_params, context);
591 if (ret)
592 return ret;
593
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 kadm5_c_destroy(ctx);
601 return ret;
602 }
603 ccache = cc;
604 }
605
606
607 if (client_name != NULL)
608 ctx->client_name = strdup(client_name);
609 else
610 ctx->client_name = NULL;
611 if (service_name != NULL)
612 ctx->service_name = strdup(service_name);
613 else
614 ctx->service_name = NULL;
615 ctx->prompter = prompter;
616 ctx->keytab = keytab;
617 ctx->ccache = ccache;
618 /* maybe we should copy the params here */
619 ctx->sock = -1;
620
621 *server_handle = ctx;
622 return 0;
623 }
624
625 static kadm5_ret_t
626 init_context(const char *client_name,
627 const char *password,
628 krb5_prompter_fct prompter,
629 const char *keytab,
630 krb5_ccache ccache,
631 const char *service_name,
632 kadm5_config_params *realm_params,
633 unsigned long struct_version,
634 unsigned long api_version,
635 void **server_handle)
636 {
637 krb5_context context;
638 kadm5_ret_t ret;
639 kadm5_server_context *ctx;
640
641 ret = krb5_init_context(&context);
642 if (ret)
643 return ret;
644 ret = kadm5_c_init_with_context(context,
645 client_name,
646 password,
647 prompter,
648 keytab,
649 ccache,
650 service_name,
651 realm_params,
652 struct_version,
653 api_version,
654 server_handle);
655 if(ret){
656 krb5_free_context(context);
657 return ret;
658 }
659 ctx = *server_handle;
660 ctx->my_context = 1;
661 return 0;
662 }
663
664 kadm5_ret_t
665 kadm5_c_init_with_password_ctx(krb5_context context,
666 const char *client_name,
667 const char *password,
668 const char *service_name,
669 kadm5_config_params *realm_params,
670 unsigned long struct_version,
671 unsigned long api_version,
672 void **server_handle)
673 {
674 return kadm5_c_init_with_context(context,
675 client_name,
676 password,
677 krb5_prompter_posix,
678 NULL,
679 NULL,
680 service_name,
681 realm_params,
682 struct_version,
683 api_version,
684 server_handle);
685 }
686
687 kadm5_ret_t
688 kadm5_c_init_with_password(const char *client_name,
689 const char *password,
690 const char *service_name,
691 kadm5_config_params *realm_params,
692 unsigned long struct_version,
693 unsigned long api_version,
694 void **server_handle)
695 {
696 return init_context(client_name,
697 password,
698 krb5_prompter_posix,
699 NULL,
700 NULL,
701 service_name,
702 realm_params,
703 struct_version,
704 api_version,
705 server_handle);
706 }
707
708 kadm5_ret_t
709 kadm5_c_init_with_skey_ctx(krb5_context context,
710 const char *client_name,
711 const char *keytab,
712 const char *service_name,
713 kadm5_config_params *realm_params,
714 unsigned long struct_version,
715 unsigned long api_version,
716 void **server_handle)
717 {
718 return kadm5_c_init_with_context(context,
719 client_name,
720 NULL,
721 NULL,
722 keytab,
723 NULL,
724 service_name,
725 realm_params,
726 struct_version,
727 api_version,
728 server_handle);
729 }
730
731
732 kadm5_ret_t
733 kadm5_c_init_with_skey(const char *client_name,
734 const char *keytab,
735 const char *service_name,
736 kadm5_config_params *realm_params,
737 unsigned long struct_version,
738 unsigned long api_version,
739 void **server_handle)
740 {
741 return init_context(client_name,
742 NULL,
743 NULL,
744 keytab,
745 NULL,
746 service_name,
747 realm_params,
748 struct_version,
749 api_version,
750 server_handle);
751 }
752
753 kadm5_ret_t
754 kadm5_c_init_with_creds_ctx(krb5_context context,
755 const char *client_name,
756 krb5_ccache ccache,
757 const char *service_name,
758 kadm5_config_params *realm_params,
759 unsigned long struct_version,
760 unsigned long api_version,
761 void **server_handle)
762 {
763 return kadm5_c_init_with_context(context,
764 client_name,
765 NULL,
766 NULL,
767 NULL,
768 ccache,
769 service_name,
770 realm_params,
771 struct_version,
772 api_version,
773 server_handle);
774 }
775
776 kadm5_ret_t
777 kadm5_c_init_with_creds(const char *client_name,
778 krb5_ccache ccache,
779 const char *service_name,
780 kadm5_config_params *realm_params,
781 unsigned long struct_version,
782 unsigned long api_version,
783 void **server_handle)
784 {
785 return init_context(client_name,
786 NULL,
787 NULL,
788 NULL,
789 ccache,
790 service_name,
791 realm_params,
792 struct_version,
793 api_version,
794 server_handle);
795 }
796
797 #if 0
798 kadm5_ret_t
799 kadm5_init(char *client_name, char *pass,
800 char *service_name,
801 kadm5_config_params *realm_params,
802 unsigned long struct_version,
803 unsigned long api_version,
804 void **server_handle)
805 {
806 }
807 #endif
808