2 * Copyright (c) 1997 - 2002 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("$Id: init_c.c,v 1.44 2002/06/16 15:13:25 nectar Exp $");
43 set_funcs(kadm5_client_context *c)
45 #define SET(C, F) (C)->funcs.F = kadm5 ## _c_ ## F
46 SET(c, chpass_principal);
47 SET(c, chpass_principal_with_key);
48 SET(c, create_principal);
49 SET(c, delete_principal);
52 SET(c, get_principal);
53 SET(c, get_principals);
55 SET(c, modify_principal);
56 SET(c, randkey_principal);
57 SET(c, rename_principal);
61 _kadm5_c_init_context(kadm5_client_context **ctx,
62 kadm5_config_params *params,
68 *ctx = malloc(sizeof(**ctx));
71 memset(*ctx, 0, sizeof(**ctx));
72 krb5_add_et_list (context, initialize_kadm5_error_table_r);
74 (*ctx)->context = context;
75 if(params->mask & KADM5_CONFIG_REALM)
76 (*ctx)->realm = strdup(params->realm);
78 krb5_get_default_realm((*ctx)->context, &(*ctx)->realm);
79 if(params->mask & KADM5_CONFIG_ADMIN_SERVER)
80 (*ctx)->admin_server = strdup(params->admin_server);
84 ret = krb5_get_krb_admin_hst (context, &(*ctx)->realm, &hostlist);
87 (*ctx)->admin_server = strdup(*hostlist);
88 krb5_free_krbhst (context, hostlist);
91 if ((*ctx)->admin_server == NULL)
93 colon = strchr ((*ctx)->admin_server, ':');
97 (*ctx)->kadmind_port = 0;
99 if(params->mask & KADM5_CONFIG_KADMIND_PORT)
100 (*ctx)->kadmind_port = params->kadmind_port;
101 else if (colon != NULL) {
104 (*ctx)->kadmind_port = htons(strtol (colon, &end, 0));
106 if ((*ctx)->kadmind_port == 0)
107 (*ctx)->kadmind_port = krb5_getportbyname (context, "kerberos-adm",
112 static krb5_error_code
113 get_kadm_ticket(krb5_context context,
115 krb5_principal client,
116 const char *server_name)
121 memset(&in, 0, sizeof(in));
123 ret = krb5_parse_name(context, server_name, &in.server);
126 ret = krb5_get_credentials(context, 0, id, &in, &out);
128 krb5_free_creds(context, out);
129 krb5_free_principal(context, in.server);
133 static krb5_error_code
134 get_new_cache(krb5_context context,
135 krb5_principal client,
136 const char *password,
137 krb5_prompter_fct prompter,
139 const char *server_name,
140 krb5_ccache *ret_cache)
144 krb5_get_init_creds_opt opt;
147 krb5_get_init_creds_opt_init (&opt);
149 krb5_get_init_creds_opt_set_default_flags(context, "kadmin",
150 krb5_principal_get_realm(context,
155 krb5_get_init_creds_opt_set_forwardable (&opt, FALSE);
156 krb5_get_init_creds_opt_set_proxiable (&opt, FALSE);
158 if(password == NULL && prompter == NULL) {
161 ret = krb5_kt_default(context, &kt);
163 ret = krb5_kt_resolve(context, keytab, &kt);
166 ret = krb5_get_init_creds_keytab (context,
173 krb5_kt_close(context, kt);
175 ret = krb5_get_init_creds_password (context,
188 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
189 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
190 case KRB5KRB_AP_ERR_MODIFIED:
191 return KADM5_BAD_PASSWORD;
195 ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &id);
198 ret = krb5_cc_initialize (context, id, cred.client);
201 ret = krb5_cc_store_cred (context, id, &cred);
204 krb5_free_creds_contents (context, &cred);
209 static krb5_error_code
210 get_cred_cache(krb5_context context,
211 const char *client_name,
212 const char *server_name,
213 const char *password,
214 krb5_prompter_fct prompter,
217 krb5_ccache *ret_cache)
220 krb5_ccache id = NULL;
221 krb5_principal default_client = NULL, client = NULL;
223 /* treat empty password as NULL */
224 if(password && *password == '\0')
226 if(server_name == NULL)
227 server_name = KADM5_ADMIN_SERVICE;
229 if(client_name != NULL) {
230 ret = krb5_parse_name(context, client_name, &client);
235 if(password != NULL || prompter != NULL) {
236 /* get principal from default cache, ok if this doesn't work */
237 ret = krb5_cc_default(context, &id);
239 ret = krb5_cc_get_principal(context, id, &default_client);
241 krb5_cc_close(context, id);
244 const char *name, *inst;
246 name = krb5_principal_get_comp_string(context,
248 inst = krb5_principal_get_comp_string(context,
250 if(inst == NULL || strcmp(inst, "admin") != 0) {
251 ret = krb5_make_principal(context, &tmp, NULL,
252 name, "admin", NULL);
254 krb5_free_principal(context, default_client);
255 krb5_cc_close(context, id);
258 krb5_free_principal(context, default_client);
259 default_client = tmp;
260 krb5_cc_close(context, id);
266 if (client != NULL) {
267 /* A client was specified by the caller. */
268 if (default_client != NULL) {
269 krb5_free_principal(context, default_client);
270 default_client = NULL;
273 else if (default_client != NULL)
274 /* No client was specified by the caller, but we have a
275 * client from the default credentials cache.
277 client = default_client;
279 /* No client was specified by the caller and we cannot determine
280 * the client from a credentials cache.
284 user = get_default_username ();
287 return KADM5_FAILURE;
288 ret = krb5_make_principal(context, &client,
289 NULL, user, "admin", NULL);
293 krb5_cc_close(context, id);
297 } else if(ccache != NULL)
300 if(id && (default_client == NULL ||
301 krb5_principal_compare(context, client, default_client))) {
302 ret = get_kadm_ticket(context, id, client, server_name);
305 krb5_free_principal(context, default_client);
306 if (default_client != client)
307 krb5_free_principal(context, client);
311 /* couldn't get ticket from cache */
314 /* get creds via AS request */
316 krb5_cc_close(context, id);
317 if (client != default_client)
318 krb5_free_principal(context, default_client);
320 ret = get_new_cache(context, client, password, prompter, keytab,
321 server_name, ret_cache);
322 krb5_free_principal(context, client);
327 kadm_connect(kadm5_client_context *ctx)
330 krb5_principal server;
333 struct addrinfo *ai, *a;
334 struct addrinfo hints;
336 char portstr[NI_MAXSERV];
337 char *hostname, *slash;
338 krb5_context context = ctx->context;
340 memset (&hints, 0, sizeof(hints));
341 hints.ai_socktype = SOCK_STREAM;
342 hints.ai_protocol = IPPROTO_TCP;
344 snprintf (portstr, sizeof(portstr), "%u", ntohs(ctx->kadmind_port));
346 hostname = ctx->admin_server;
347 slash = strchr (hostname, '/');
349 hostname = slash + 1;
351 error = getaddrinfo (hostname, portstr, &hints, &ai);
353 return KADM5_BAD_SERVER_NAME;
355 for (a = ai; a != NULL; a = a->ai_next) {
356 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
359 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
360 krb5_warn (context, errno, "connect(%s)", hostname);
368 krb5_warnx (context, "failed to contact %s", hostname);
369 return KADM5_FAILURE;
371 ret = get_cred_cache(context, ctx->client_name, ctx->service_name,
372 NULL, ctx->prompter, ctx->keytab,
380 ret = krb5_parse_name(context, KADM5_ADMIN_SERVICE, &server);
383 if(ctx->ccache == NULL)
384 krb5_cc_close(context, cc);
390 ret = krb5_sendauth(context, &ctx->ac, &s,
391 KADMIN_APPL_VERSION, NULL,
392 server, AP_OPTS_MUTUAL_REQUIRED,
393 NULL, NULL, cc, NULL, NULL, NULL);
396 kadm5_config_params p;
397 memset(&p, 0, sizeof(p));
399 p.mask |= KADM5_CONFIG_REALM;
400 p.realm = ctx->realm;
402 ret = _kadm5_marshal_params(context, &p, ¶ms);
404 ret = krb5_write_priv_message(context, ctx->ac, &s, ¶ms);
405 krb5_data_free(¶ms);
409 if(ctx->ccache == NULL)
410 krb5_cc_close(context, cc);
413 } else if(ret == KRB5_SENDAUTH_BADAPPLVERS) {
416 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
421 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
426 ret = krb5_sendauth(context, &ctx->ac, &s,
427 KADMIN_OLD_APPL_VERSION, NULL,
428 server, AP_OPTS_MUTUAL_REQUIRED,
429 NULL, NULL, cc, NULL, NULL, NULL);
437 krb5_free_principal(context, server);
438 if(ctx->ccache == NULL)
439 krb5_cc_close(context, cc);
450 _kadm5_connect(void *handle)
452 kadm5_client_context *ctx = handle;
454 return kadm_connect(ctx);
459 kadm5_c_init_with_context(krb5_context context,
460 const char *client_name,
461 const char *password,
462 krb5_prompter_fct prompter,
465 const char *service_name,
466 kadm5_config_params *realm_params,
467 unsigned long struct_version,
468 unsigned long api_version,
469 void **server_handle)
472 kadm5_client_context *ctx;
475 ret = _kadm5_c_init_context(&ctx, realm_params, context);
479 if(password != NULL && *password != '\0') {
480 ret = get_cred_cache(context, client_name, service_name,
481 password, prompter, keytab, ccache, &cc);
483 return ret; /* XXX */
488 if (client_name != NULL)
489 ctx->client_name = strdup(client_name);
491 ctx->client_name = NULL;
492 if (service_name != NULL)
493 ctx->service_name = strdup(service_name);
495 ctx->service_name = NULL;
496 ctx->prompter = prompter;
497 ctx->keytab = keytab;
498 ctx->ccache = ccache;
499 /* maybe we should copy the params here */
502 *server_handle = ctx;
507 init_context(const char *client_name,
508 const char *password,
509 krb5_prompter_fct prompter,
512 const char *service_name,
513 kadm5_config_params *realm_params,
514 unsigned long struct_version,
515 unsigned long api_version,
516 void **server_handle)
518 krb5_context context;
520 kadm5_server_context *ctx;
522 ret = krb5_init_context(&context);
525 ret = kadm5_c_init_with_context(context,
537 krb5_free_context(context);
540 ctx = *server_handle;
546 kadm5_c_init_with_password_ctx(krb5_context context,
547 const char *client_name,
548 const char *password,
549 const char *service_name,
550 kadm5_config_params *realm_params,
551 unsigned long struct_version,
552 unsigned long api_version,
553 void **server_handle)
555 return kadm5_c_init_with_context(context,
569 kadm5_c_init_with_password(const char *client_name,
570 const char *password,
571 const char *service_name,
572 kadm5_config_params *realm_params,
573 unsigned long struct_version,
574 unsigned long api_version,
575 void **server_handle)
577 return init_context(client_name,
590 kadm5_c_init_with_skey_ctx(krb5_context context,
591 const char *client_name,
593 const char *service_name,
594 kadm5_config_params *realm_params,
595 unsigned long struct_version,
596 unsigned long api_version,
597 void **server_handle)
599 return kadm5_c_init_with_context(context,
614 kadm5_c_init_with_skey(const char *client_name,
616 const char *service_name,
617 kadm5_config_params *realm_params,
618 unsigned long struct_version,
619 unsigned long api_version,
620 void **server_handle)
622 return init_context(client_name,
635 kadm5_c_init_with_creds_ctx(krb5_context context,
636 const char *client_name,
638 const char *service_name,
639 kadm5_config_params *realm_params,
640 unsigned long struct_version,
641 unsigned long api_version,
642 void **server_handle)
644 return kadm5_c_init_with_context(context,
658 kadm5_c_init_with_creds(const char *client_name,
660 const char *service_name,
661 kadm5_config_params *realm_params,
662 unsigned long struct_version,
663 unsigned long api_version,
664 void **server_handle)
666 return init_context(client_name,
680 kadm5_init(char *client_name, char *pass,
682 kadm5_config_params *realm_params,
683 unsigned long struct_version,
684 unsigned long api_version,
685 void **server_handle)