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