1/*
2 * Copyright (c) 1997 - 2001 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 "krb5_locl.h"
35#include "config_plugin.h"
36
37static void
38add_default_realm(krb5_context context, void *userctx, krb5_const_realm realm)
39{
40    heim_array_t array = userctx;
41    heim_string_t s = heim_string_create(realm);
42
43    if (s) {
44	heim_array_append_value(array, s);
45	heim_release(s);
46    }
47}
48
49static krb5_error_code
50config_plugin(krb5_context context,
51	      const void *plug, void *plugctx, void *userctx)
52{
53    const krb5plugin_config_ftable *config = plug;
54    if (config->get_default_realm == NULL)
55	return KRB5_PLUGIN_NO_HANDLE;
56
57    return config->get_default_realm(context, plugctx, userctx, add_default_realm);
58}
59
60static krb5_error_code
61get_plugin(krb5_context context, heim_array_t array)
62{
63    return krb5_plugin_run_f(context, "krb5",
64			     KRB5_PLUGIN_CONFIGURATION,
65			     KRB5_PLUGIN_CONFIGURATION_VERSION_0,
66			     0, array, config_plugin);
67}
68
69/**
70 * Set the knowledge of the default realm(s) in context.
71 * If realm is not NULL, that's the new default realm.
72 * Otherwise, the realm(s) are figured out from plugin, configuration file or DNS.
73 *
74 * @param context Kerberos 5 context.
75 * @param realm the new default realm or NULL for using configuration.
76
77 * @return Returns 0 to indicate success. Otherwise an kerberos et
78 * error code is returned, see krb5_get_error_message().
79 *
80 * @ingroup krb5
81 */
82
83KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
84krb5_set_default_realm(krb5_context context,
85		       const char *realm)
86{
87    krb5_error_code ret = 0;
88    krb5_realm *realms = NULL;
89    heim_array_t array;
90    size_t n;
91
92    array = heim_array_create();
93
94    if (realm == NULL) {
95	get_plugin(context, array);
96
97	realms = krb5_config_get_strings (context, NULL,
98					  "libdefaults",
99					  "default_realm",
100					  NULL);
101	if (realms == NULL && heim_array_get_length(array) == 0) {
102	    ret = krb5_get_host_realm(context, NULL, &realms);
103	    if (ret) {
104		heim_release(array);
105		return ret;
106	    }
107	}
108
109	if (realms) {
110	    for (n = 0; realms[n]; n++)
111		add_default_realm(context, array, realms[n]);
112	    krb5_free_host_realm(context, realms);
113	}
114    } else {
115	add_default_realm(context, array, realm);
116    }
117
118    heim_release(context->default_realms);
119    context->default_realms = array;
120
121    return 0;
122}
123