xref: /minix3/crypto/external/bsd/heimdal/dist/lib/kadm5/context_s.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: context_s.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "kadm5_locl.h"
37ebfedea0SLionel Sambuc 
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc static void
set_funcs(kadm5_server_context * c)41ebfedea0SLionel Sambuc set_funcs(kadm5_server_context *c)
42ebfedea0SLionel Sambuc {
43ebfedea0SLionel Sambuc #define SET(C, F) (C)->funcs.F = kadm5_s_ ## F
44ebfedea0SLionel Sambuc     SET(c, chpass_principal);
45ebfedea0SLionel Sambuc     SET(c, chpass_principal_with_key);
46ebfedea0SLionel Sambuc     SET(c, create_principal);
47ebfedea0SLionel Sambuc     SET(c, delete_principal);
48ebfedea0SLionel Sambuc     SET(c, destroy);
49ebfedea0SLionel Sambuc     SET(c, flush);
50ebfedea0SLionel Sambuc     SET(c, get_principal);
51ebfedea0SLionel Sambuc     SET(c, get_principals);
52ebfedea0SLionel Sambuc     SET(c, get_privs);
53ebfedea0SLionel Sambuc     SET(c, modify_principal);
54ebfedea0SLionel Sambuc     SET(c, randkey_principal);
55ebfedea0SLionel Sambuc     SET(c, rename_principal);
56ebfedea0SLionel Sambuc }
57ebfedea0SLionel Sambuc 
58ebfedea0SLionel Sambuc #ifndef NO_UNIX_SOCKETS
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc static void
set_socket_name(krb5_context context,struct sockaddr_un * un)61ebfedea0SLionel Sambuc set_socket_name(krb5_context context, struct sockaddr_un *un)
62ebfedea0SLionel Sambuc {
63ebfedea0SLionel Sambuc     const char *fn = kadm5_log_signal_socket(context);
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc     memset(un, 0, sizeof(*un));
66ebfedea0SLionel Sambuc     un->sun_family = AF_UNIX;
67ebfedea0SLionel Sambuc     strlcpy (un->sun_path, fn, sizeof(un->sun_path));
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc }
70ebfedea0SLionel Sambuc #else
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc static void
set_socket_info(krb5_context context,struct addrinfo ** info)73ebfedea0SLionel Sambuc set_socket_info(krb5_context context, struct addrinfo **info)
74ebfedea0SLionel Sambuc {
75ebfedea0SLionel Sambuc     kadm5_log_signal_socket_info(context, 0, info);
76ebfedea0SLionel Sambuc }
77ebfedea0SLionel Sambuc 
78ebfedea0SLionel Sambuc #endif
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc static kadm5_ret_t
find_db_spec(kadm5_server_context * ctx)81ebfedea0SLionel Sambuc find_db_spec(kadm5_server_context *ctx)
82ebfedea0SLionel Sambuc {
83ebfedea0SLionel Sambuc     krb5_context context = ctx->context;
84ebfedea0SLionel Sambuc     struct hdb_dbinfo *info, *d;
85ebfedea0SLionel Sambuc     krb5_error_code ret;
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc     if (ctx->config.realm) {
88ebfedea0SLionel Sambuc 	/* fetch the databases */
89ebfedea0SLionel Sambuc 	ret = hdb_get_dbinfo(context, &info);
90ebfedea0SLionel Sambuc 	if (ret)
91ebfedea0SLionel Sambuc 	    return ret;
92ebfedea0SLionel Sambuc 
93ebfedea0SLionel Sambuc 	d = NULL;
94ebfedea0SLionel Sambuc 	while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
95ebfedea0SLionel Sambuc 	    const char *p = hdb_dbinfo_get_realm(context, d);
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc 	    /* match default (realm-less) */
98ebfedea0SLionel Sambuc 	    if(p != NULL && strcmp(ctx->config.realm, p) != 0)
99ebfedea0SLionel Sambuc 		continue;
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc 	    p = hdb_dbinfo_get_dbname(context, d);
102ebfedea0SLionel Sambuc 	    if (p)
103ebfedea0SLionel Sambuc 		ctx->config.dbname = strdup(p);
104ebfedea0SLionel Sambuc 
105ebfedea0SLionel Sambuc 	    p = hdb_dbinfo_get_acl_file(context, d);
106ebfedea0SLionel Sambuc 	    if (p)
107ebfedea0SLionel Sambuc 		ctx->config.acl_file = strdup(p);
108ebfedea0SLionel Sambuc 
109ebfedea0SLionel Sambuc 	    p = hdb_dbinfo_get_mkey_file(context, d);
110ebfedea0SLionel Sambuc 	    if (p)
111ebfedea0SLionel Sambuc 		ctx->config.stash_file = strdup(p);
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc 	    p = hdb_dbinfo_get_log_file(context, d);
114ebfedea0SLionel Sambuc 	    if (p)
115ebfedea0SLionel Sambuc 		ctx->log_context.log_file = strdup(p);
116ebfedea0SLionel Sambuc 	    break;
117ebfedea0SLionel Sambuc 	}
118ebfedea0SLionel Sambuc 	hdb_free_dbinfo(context, &info);
119ebfedea0SLionel Sambuc     }
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc     /* If any of the values was unset, pick up the default value */
122ebfedea0SLionel Sambuc 
123ebfedea0SLionel Sambuc     if (ctx->config.dbname == NULL)
124ebfedea0SLionel Sambuc 	ctx->config.dbname = strdup(hdb_default_db(context));
125ebfedea0SLionel Sambuc     if (ctx->config.acl_file == NULL)
126ebfedea0SLionel Sambuc 	asprintf(&ctx->config.acl_file, "%s/kadmind.acl", hdb_db_dir(context));
127ebfedea0SLionel Sambuc     if (ctx->config.stash_file == NULL)
128ebfedea0SLionel Sambuc 	asprintf(&ctx->config.stash_file, "%s/m-key", hdb_db_dir(context));
129ebfedea0SLionel Sambuc     if (ctx->log_context.log_file == NULL)
130ebfedea0SLionel Sambuc 	asprintf(&ctx->log_context.log_file, "%s/log", hdb_db_dir(context));
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc #ifndef NO_UNIX_SOCKETS
133ebfedea0SLionel Sambuc     set_socket_name(context, &ctx->log_context.socket_name);
134ebfedea0SLionel Sambuc #else
135ebfedea0SLionel Sambuc     set_socket_info(context, &ctx->log_context.socket_info);
136ebfedea0SLionel Sambuc #endif
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc     return 0;
139ebfedea0SLionel Sambuc }
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_s_init_context(kadm5_server_context ** ctx,kadm5_config_params * params,krb5_context context)142ebfedea0SLionel Sambuc _kadm5_s_init_context(kadm5_server_context **ctx,
143ebfedea0SLionel Sambuc 		      kadm5_config_params *params,
144ebfedea0SLionel Sambuc 		      krb5_context context)
145ebfedea0SLionel Sambuc {
146ebfedea0SLionel Sambuc     *ctx = malloc(sizeof(**ctx));
147ebfedea0SLionel Sambuc     if(*ctx == NULL)
148ebfedea0SLionel Sambuc 	return ENOMEM;
149ebfedea0SLionel Sambuc     memset(*ctx, 0, sizeof(**ctx));
150ebfedea0SLionel Sambuc     set_funcs(*ctx);
151ebfedea0SLionel Sambuc     (*ctx)->context = context;
152ebfedea0SLionel Sambuc     krb5_add_et_list (context, initialize_kadm5_error_table_r);
153ebfedea0SLionel Sambuc #define is_set(M) (params && params->mask & KADM5_CONFIG_ ## M)
154ebfedea0SLionel Sambuc     if(is_set(REALM))
155ebfedea0SLionel Sambuc 	(*ctx)->config.realm = strdup(params->realm);
156ebfedea0SLionel Sambuc     else
157ebfedea0SLionel Sambuc 	krb5_get_default_realm(context, &(*ctx)->config.realm);
158ebfedea0SLionel Sambuc     if(is_set(DBNAME))
159ebfedea0SLionel Sambuc 	(*ctx)->config.dbname = strdup(params->dbname);
160ebfedea0SLionel Sambuc     if(is_set(ACL_FILE))
161ebfedea0SLionel Sambuc 	(*ctx)->config.acl_file = strdup(params->acl_file);
162ebfedea0SLionel Sambuc     if(is_set(STASH_FILE))
163ebfedea0SLionel Sambuc 	(*ctx)->config.stash_file = strdup(params->stash_file);
164ebfedea0SLionel Sambuc 
165ebfedea0SLionel Sambuc     find_db_spec(*ctx);
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc     /* PROFILE can't be specified for now */
168ebfedea0SLionel Sambuc     /* KADMIND_PORT is supposed to be used on the server also,
169ebfedea0SLionel Sambuc        but this doesn't make sense */
170ebfedea0SLionel Sambuc     /* ADMIN_SERVER is client only */
171ebfedea0SLionel Sambuc     /* ADNAME is not used at all (as far as I can tell) */
172ebfedea0SLionel Sambuc     /* ADB_LOCKFILE ditto */
173ebfedea0SLionel Sambuc     /* DICT_FILE */
174ebfedea0SLionel Sambuc     /* ADMIN_KEYTAB */
175ebfedea0SLionel Sambuc     /* MKEY_FROM_KEYBOARD is not supported */
176ebfedea0SLionel Sambuc     /* MKEY_NAME neither */
177ebfedea0SLionel Sambuc     /* ENCTYPE */
178ebfedea0SLionel Sambuc     /* MAX_LIFE */
179ebfedea0SLionel Sambuc     /* MAX_RLIFE */
180ebfedea0SLionel Sambuc     /* EXPIRATION */
181ebfedea0SLionel Sambuc     /* FLAGS */
182ebfedea0SLionel Sambuc     /* ENCTYPES */
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc     return 0;
185ebfedea0SLionel Sambuc }
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc HDB *
_kadm5_s_get_db(void * server_handle)188ebfedea0SLionel Sambuc _kadm5_s_get_db(void *server_handle)
189ebfedea0SLionel Sambuc {
190ebfedea0SLionel Sambuc     kadm5_server_context *context = server_handle;
191ebfedea0SLionel Sambuc     return context->db;
192ebfedea0SLionel Sambuc }
193