2 * Copyright (c) 1997, 1998, 1999 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.34 1999/12/20 14:05:49 assar 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);
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);
148 if(password == NULL && prompter == NULL) {
151 ret = krb5_kt_default(context, &kt);
153 ret = krb5_kt_resolve(context, keytab, &kt);
156 ret = krb5_get_init_creds_keytab (context,
163 krb5_kt_close(context, kt);
165 ret = krb5_get_init_creds_password (context,
178 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
179 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
180 case KRB5KRB_AP_ERR_MODIFIED:
181 return KADM5_BAD_PASSWORD;
185 ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &id);
188 ret = krb5_cc_initialize (context, id, cred.client);
191 ret = krb5_cc_store_cred (context, id, &cred);
194 krb5_free_creds_contents (context, &cred);
199 static krb5_error_code
200 get_cred_cache(krb5_context context,
201 const char *client_name,
202 const char *server_name,
203 const char *password,
204 krb5_prompter_fct prompter,
207 krb5_ccache *ret_cache)
210 krb5_ccache id = NULL;
211 krb5_principal default_client = NULL, client = NULL;
213 /* treat empty password as NULL */
214 if(password && *password == '\0')
216 if(server_name == NULL)
217 server_name = KADM5_ADMIN_SERVICE;
219 if(client_name != NULL) {
220 ret = krb5_parse_name(context, client_name, &client);
225 if(password != NULL || prompter != NULL) {
226 /* get principal from default cache, ok if this doesn't work */
227 ret = krb5_cc_default(context, &id);
229 ret = krb5_cc_get_principal(context, id, &default_client);
231 krb5_cc_close(context, id);
237 client = default_client;
241 user = get_default_username ();
244 return KADM5_FAILURE;
245 ret = krb5_make_principal(context, &client,
246 NULL, user, "admin", NULL);
250 if(client != default_client) {
251 krb5_free_principal(context, default_client);
252 default_client = NULL;
254 krb5_cc_close(context, id);
258 } else if(ccache != NULL)
262 if(id && (default_client == NULL ||
263 krb5_principal_compare(context, client, default_client))) {
264 ret = get_kadm_ticket(context, id, client, server_name);
267 krb5_free_principal(context, default_client);
268 if (default_client != client)
269 krb5_free_principal(context, client);
273 /* couldn't get ticket from cache */
276 /* get creds via AS request */
278 krb5_cc_close(context, id);
279 if (client != default_client)
280 krb5_free_principal(context, default_client);
282 ret = get_new_cache(context, client, password, prompter, keytab,
283 server_name, ret_cache);
284 krb5_free_principal(context, client);
289 kadm5_c_init_with_context(krb5_context context,
290 const char *client_name,
291 const char *password,
292 krb5_prompter_fct prompter,
295 const char *service_name,
296 kadm5_config_params *realm_params,
297 unsigned long struct_version,
298 unsigned long api_version,
299 void **server_handle)
302 kadm5_client_context *ctx;
303 krb5_principal server;
306 struct addrinfo *ai, *a;
307 struct addrinfo hints;
309 char portstr[NI_MAXSERV];
310 char *hostname, *slash;
312 memset (&hints, 0, sizeof(hints));
313 hints.ai_socktype = SOCK_STREAM;
314 hints.ai_protocol = IPPROTO_TCP;
316 ret = _kadm5_c_init_context(&ctx, realm_params, context);
320 snprintf (portstr, sizeof(portstr), "%u", ntohs(ctx->kadmind_port));
322 hostname = ctx->admin_server;
323 slash = strchr (hostname, '/');
325 hostname = slash + 1;
327 error = getaddrinfo (hostname, portstr, &hints, &ai);
329 return KADM5_BAD_SERVER_NAME;
331 for (a = ai; a != NULL; a = a->ai_next) {
332 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
335 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
336 krb5_warn (context, errno, "connect(%s)", hostname);
344 krb5_warnx (context, "failed to contact %s", hostname);
345 return KADM5_FAILURE;
347 ret = get_cred_cache(context, client_name, service_name,
348 password, prompter, keytab, ccache, &cc);
355 ret = krb5_parse_name(context, KADM5_ADMIN_SERVICE, &server);
359 krb5_cc_close(context, cc);
365 ret = krb5_sendauth(context, &ctx->ac, &s,
366 KADMIN_APPL_VERSION, NULL,
367 server, AP_OPTS_MUTUAL_REQUIRED,
368 NULL, NULL, cc, NULL, NULL, NULL);
370 krb5_data params, enc_data;
371 ret = _kadm5_marshal_params(context, realm_params, ¶ms);
373 ret = krb5_mk_priv(context,
379 ret = krb5_write_message(context, &s, &enc_data);
381 krb5_data_free(¶ms);
382 krb5_data_free(&enc_data);
383 } else if(ret == KRB5_SENDAUTH_BADAPPLVERS) {
386 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
391 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
398 ret = krb5_sendauth(context, &ctx->ac, &s,
399 KADMIN_OLD_APPL_VERSION, NULL,
400 server, AP_OPTS_MUTUAL_REQUIRED,
401 NULL, NULL, cc, NULL, NULL, NULL);
409 krb5_free_principal(context, server);
411 krb5_cc_close(context, cc);
417 *server_handle = ctx;
422 init_context(const char *client_name,
423 const char *password,
424 krb5_prompter_fct prompter,
427 const char *service_name,
428 kadm5_config_params *realm_params,
429 unsigned long struct_version,
430 unsigned long api_version,
431 void **server_handle)
433 krb5_context context;
435 kadm5_server_context *ctx;
437 krb5_init_context(&context);
438 ret = kadm5_c_init_with_context(context,
450 krb5_free_context(context);
453 ctx = *server_handle;
459 kadm5_c_init_with_password_ctx(krb5_context context,
460 const char *client_name,
461 const char *password,
462 const char *service_name,
463 kadm5_config_params *realm_params,
464 unsigned long struct_version,
465 unsigned long api_version,
466 void **server_handle)
468 return kadm5_c_init_with_context(context,
482 kadm5_c_init_with_password(const char *client_name,
483 const char *password,
484 const char *service_name,
485 kadm5_config_params *realm_params,
486 unsigned long struct_version,
487 unsigned long api_version,
488 void **server_handle)
490 return init_context(client_name,
503 kadm5_c_init_with_skey_ctx(krb5_context context,
504 const char *client_name,
506 const char *service_name,
507 kadm5_config_params *realm_params,
508 unsigned long struct_version,
509 unsigned long api_version,
510 void **server_handle)
512 return kadm5_c_init_with_context(context,
527 kadm5_c_init_with_skey(const char *client_name,
529 const char *service_name,
530 kadm5_config_params *realm_params,
531 unsigned long struct_version,
532 unsigned long api_version,
533 void **server_handle)
535 return init_context(client_name,
548 kadm5_c_init_with_creds_ctx(krb5_context context,
549 const char *client_name,
551 const char *service_name,
552 kadm5_config_params *realm_params,
553 unsigned long struct_version,
554 unsigned long api_version,
555 void **server_handle)
557 return kadm5_c_init_with_context(context,
571 kadm5_c_init_with_creds(const char *client_name,
573 const char *service_name,
574 kadm5_config_params *realm_params,
575 unsigned long struct_version,
576 unsigned long api_version,
577 void **server_handle)
579 return init_context(client_name,
593 kadm5_init(char *client_name, char *pass,
595 kadm5_config_params *realm_params,
596 unsigned long struct_version,
597 unsigned long api_version,
598 void **server_handle)