1*1c9681d1Schristos /* $NetBSD: context_s.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */
2f59d82ffSelric
3f59d82ffSelric /*
4f59d82ffSelric * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
5f59d82ffSelric * (Royal Institute of Technology, Stockholm, Sweden).
6f59d82ffSelric * All rights reserved.
7f59d82ffSelric *
8f59d82ffSelric * Redistribution and use in source and binary forms, with or without
9f59d82ffSelric * modification, are permitted provided that the following conditions
10f59d82ffSelric * are met:
11f59d82ffSelric *
12f59d82ffSelric * 1. Redistributions of source code must retain the above copyright
13f59d82ffSelric * notice, this list of conditions and the following disclaimer.
14f59d82ffSelric *
15f59d82ffSelric * 2. Redistributions in binary form must reproduce the above copyright
16f59d82ffSelric * notice, this list of conditions and the following disclaimer in the
17f59d82ffSelric * documentation and/or other materials provided with the distribution.
18f59d82ffSelric *
19f59d82ffSelric * 3. Neither the name of the Institute nor the names of its contributors
20f59d82ffSelric * may be used to endorse or promote products derived from this software
21f59d82ffSelric * without specific prior written permission.
22f59d82ffSelric *
23f59d82ffSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24f59d82ffSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f59d82ffSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f59d82ffSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27f59d82ffSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f59d82ffSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f59d82ffSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f59d82ffSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f59d82ffSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f59d82ffSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f59d82ffSelric * SUCH DAMAGE.
34f59d82ffSelric */
35f59d82ffSelric
36f59d82ffSelric #include "kadm5_locl.h"
37f59d82ffSelric
38*1c9681d1Schristos __RCSID("$NetBSD: context_s.c,v 1.2 2017/01/28 21:31:49 christos Exp $");
39e0895134Schristos
40e0895134Schristos static kadm5_ret_t
kadm5_s_lock(void * server_handle)41e0895134Schristos kadm5_s_lock(void *server_handle)
42e0895134Schristos {
43e0895134Schristos kadm5_server_context *context = server_handle;
44e0895134Schristos kadm5_ret_t ret;
45e0895134Schristos
46e0895134Schristos if (context->keep_open) {
47e0895134Schristos /*
48e0895134Schristos * We open/close around every operation, but we retain the DB
49e0895134Schristos * open if the DB was locked with a prior call to kadm5_lock(),
50e0895134Schristos * so if it's open here that must be because the DB is locked.
51e0895134Schristos */
52e0895134Schristos heim_assert(context->db->lock_count > 0,
53e0895134Schristos "Internal error in tracking HDB locks");
54e0895134Schristos return KADM5_ALREADY_LOCKED;
55e0895134Schristos }
56e0895134Schristos
57e0895134Schristos ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
58e0895134Schristos if (ret)
59e0895134Schristos return ret;
60e0895134Schristos
61e0895134Schristos ret = context->db->hdb_lock(context->context, context->db, HDB_WLOCK);
62e0895134Schristos if (ret) {
63e0895134Schristos (void) context->db->hdb_close(context->context, context->db);
64e0895134Schristos return ret;
65e0895134Schristos }
66e0895134Schristos
67e0895134Schristos /*
68e0895134Schristos * Attempt to recover the log. This will generally fail on slaves,
69e0895134Schristos * and we can't tell if we're on a slave here.
70e0895134Schristos *
71e0895134Schristos * Perhaps we could set a flag in the kadm5_server_context to
72e0895134Schristos * indicate whether a read has been done without recovering the log,
73e0895134Schristos * in which case we could fail any subsequent writes.
74e0895134Schristos */
75e0895134Schristos if (kadm5_log_init(context) == 0)
76e0895134Schristos (void) kadm5_log_end(context);
77e0895134Schristos
78e0895134Schristos context->keep_open = 1;
79e0895134Schristos return 0;
80e0895134Schristos }
81e0895134Schristos
82e0895134Schristos static kadm5_ret_t
kadm5_s_unlock(void * server_handle)83e0895134Schristos kadm5_s_unlock(void *server_handle)
84e0895134Schristos {
85e0895134Schristos kadm5_server_context *context = server_handle;
86e0895134Schristos kadm5_ret_t ret;
87e0895134Schristos
88e0895134Schristos if (!context->keep_open)
89e0895134Schristos return KADM5_NOT_LOCKED;
90e0895134Schristos
91e0895134Schristos context->keep_open = 0;
92e0895134Schristos ret = context->db->hdb_unlock(context->context, context->db);
93e0895134Schristos (void) context->db->hdb_close(context->context, context->db);
94e0895134Schristos return ret;
95e0895134Schristos }
96f59d82ffSelric
97f59d82ffSelric static void
set_funcs(kadm5_server_context * c)98f59d82ffSelric set_funcs(kadm5_server_context *c)
99f59d82ffSelric {
100f59d82ffSelric #define SET(C, F) (C)->funcs.F = kadm5_s_ ## F
101f59d82ffSelric SET(c, chpass_principal);
102f59d82ffSelric SET(c, chpass_principal_with_key);
103f59d82ffSelric SET(c, create_principal);
104f59d82ffSelric SET(c, delete_principal);
105f59d82ffSelric SET(c, destroy);
106f59d82ffSelric SET(c, flush);
107f59d82ffSelric SET(c, get_principal);
108f59d82ffSelric SET(c, get_principals);
109f59d82ffSelric SET(c, get_privs);
110f59d82ffSelric SET(c, modify_principal);
111f59d82ffSelric SET(c, randkey_principal);
112f59d82ffSelric SET(c, rename_principal);
113e0895134Schristos SET(c, lock);
114e0895134Schristos SET(c, unlock);
115e0895134Schristos SET(c, setkey_principal_3);
116f59d82ffSelric }
117f59d82ffSelric
118f59d82ffSelric #ifndef NO_UNIX_SOCKETS
119f59d82ffSelric
120f59d82ffSelric static void
set_socket_name(krb5_context context,struct sockaddr_un * un)121f59d82ffSelric set_socket_name(krb5_context context, struct sockaddr_un *un)
122f59d82ffSelric {
123f59d82ffSelric const char *fn = kadm5_log_signal_socket(context);
124f59d82ffSelric
125f59d82ffSelric memset(un, 0, sizeof(*un));
126f59d82ffSelric un->sun_family = AF_UNIX;
127f59d82ffSelric strlcpy (un->sun_path, fn, sizeof(un->sun_path));
128f59d82ffSelric
129f59d82ffSelric }
130f59d82ffSelric #else
131f59d82ffSelric
132f59d82ffSelric static void
set_socket_info(krb5_context context,struct addrinfo ** info)133f59d82ffSelric set_socket_info(krb5_context context, struct addrinfo **info)
134f59d82ffSelric {
135f59d82ffSelric kadm5_log_signal_socket_info(context, 0, info);
136f59d82ffSelric }
137f59d82ffSelric
138f59d82ffSelric #endif
139f59d82ffSelric
140f59d82ffSelric static kadm5_ret_t
find_db_spec(kadm5_server_context * ctx)141f59d82ffSelric find_db_spec(kadm5_server_context *ctx)
142f59d82ffSelric {
143f59d82ffSelric krb5_context context = ctx->context;
144f59d82ffSelric struct hdb_dbinfo *info, *d;
145f59d82ffSelric krb5_error_code ret;
146e0895134Schristos int aret;
147f59d82ffSelric
148f59d82ffSelric if (ctx->config.realm) {
149f59d82ffSelric /* fetch the databases */
150f59d82ffSelric ret = hdb_get_dbinfo(context, &info);
151f59d82ffSelric if (ret)
152f59d82ffSelric return ret;
153f59d82ffSelric
154f59d82ffSelric d = NULL;
155f59d82ffSelric while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
156f59d82ffSelric const char *p = hdb_dbinfo_get_realm(context, d);
157f59d82ffSelric
158f59d82ffSelric /* match default (realm-less) */
159f59d82ffSelric if(p != NULL && strcmp(ctx->config.realm, p) != 0)
160f59d82ffSelric continue;
161f59d82ffSelric
162f59d82ffSelric p = hdb_dbinfo_get_dbname(context, d);
163e0895134Schristos if (p) {
164f59d82ffSelric ctx->config.dbname = strdup(p);
165e0895134Schristos if (ctx->config.dbname == NULL)
166e0895134Schristos return ENOMEM;
167e0895134Schristos }
168f59d82ffSelric
169f59d82ffSelric p = hdb_dbinfo_get_acl_file(context, d);
170e0895134Schristos if (p) {
171f59d82ffSelric ctx->config.acl_file = strdup(p);
172e0895134Schristos if (ctx->config.acl_file == NULL)
173e0895134Schristos return ENOMEM;
174e0895134Schristos }
175f59d82ffSelric
176f59d82ffSelric p = hdb_dbinfo_get_mkey_file(context, d);
177e0895134Schristos if (p) {
178f59d82ffSelric ctx->config.stash_file = strdup(p);
179e0895134Schristos if (ctx->config.stash_file == NULL)
180e0895134Schristos return ENOMEM;
181e0895134Schristos }
182f59d82ffSelric
183f59d82ffSelric p = hdb_dbinfo_get_log_file(context, d);
184e0895134Schristos if (p) {
185f59d82ffSelric ctx->log_context.log_file = strdup(p);
186e0895134Schristos if (ctx->log_context.log_file == NULL)
187e0895134Schristos return ENOMEM;
188e0895134Schristos }
189f59d82ffSelric break;
190f59d82ffSelric }
191f59d82ffSelric hdb_free_dbinfo(context, &info);
192f59d82ffSelric }
193f59d82ffSelric
194f59d82ffSelric /* If any of the values was unset, pick up the default value */
195f59d82ffSelric
196e0895134Schristos if (ctx->config.dbname == NULL) {
197f59d82ffSelric ctx->config.dbname = strdup(hdb_default_db(context));
198e0895134Schristos if (ctx->config.dbname == NULL)
199e0895134Schristos return ENOMEM;
200e0895134Schristos }
201e0895134Schristos if (ctx->config.acl_file == NULL) {
202e0895134Schristos aret = asprintf(&ctx->config.acl_file, "%s/kadmind.acl",
203e0895134Schristos hdb_db_dir(context));
204e0895134Schristos if (aret == -1)
205e0895134Schristos return ENOMEM;
206e0895134Schristos }
207e0895134Schristos if (ctx->config.stash_file == NULL) {
208e0895134Schristos aret = asprintf(&ctx->config.stash_file, "%s/m-key",
209e0895134Schristos hdb_db_dir(context));
210e0895134Schristos if (aret == -1)
211e0895134Schristos return ENOMEM;
212e0895134Schristos }
213e0895134Schristos if (ctx->log_context.log_file == NULL) {
214e0895134Schristos aret = asprintf(&ctx->log_context.log_file, "%s/log",
215e0895134Schristos hdb_db_dir(context));
216e0895134Schristos if (aret == -1)
217e0895134Schristos return ENOMEM;
218e0895134Schristos }
219f59d82ffSelric
220f59d82ffSelric #ifndef NO_UNIX_SOCKETS
221f59d82ffSelric set_socket_name(context, &ctx->log_context.socket_name);
222f59d82ffSelric #else
223f59d82ffSelric set_socket_info(context, &ctx->log_context.socket_info);
224f59d82ffSelric #endif
225f59d82ffSelric
226f59d82ffSelric return 0;
227f59d82ffSelric }
228f59d82ffSelric
229f59d82ffSelric kadm5_ret_t
_kadm5_s_init_context(kadm5_server_context ** ctx,kadm5_config_params * params,krb5_context context)230f59d82ffSelric _kadm5_s_init_context(kadm5_server_context **ctx,
231f59d82ffSelric kadm5_config_params *params,
232f59d82ffSelric krb5_context context)
233f59d82ffSelric {
234e0895134Schristos kadm5_ret_t ret = 0;
235e0895134Schristos
236e0895134Schristos *ctx = calloc(1, sizeof(**ctx));
237f59d82ffSelric if (*ctx == NULL)
238f59d82ffSelric return ENOMEM;
239e0895134Schristos (*ctx)->log_context.socket_fd = rk_INVALID_SOCKET;
240e0895134Schristos
241f59d82ffSelric set_funcs(*ctx);
242f59d82ffSelric (*ctx)->context = context;
243f59d82ffSelric krb5_add_et_list (context, initialize_kadm5_error_table_r);
244e0895134Schristos
245f59d82ffSelric #define is_set(M) (params && params->mask & KADM5_CONFIG_ ## M)
246e0895134Schristos if (is_set(REALM)) {
247f59d82ffSelric (*ctx)->config.realm = strdup(params->realm);
248e0895134Schristos if ((*ctx)->config.realm == NULL)
249e0895134Schristos return ENOMEM;
250e0895134Schristos } else {
251e0895134Schristos ret = krb5_get_default_realm(context, &(*ctx)->config.realm);
252e0895134Schristos if (ret)
253e0895134Schristos return ret;
254e0895134Schristos }
255e0895134Schristos if (is_set(DBNAME)) {
256f59d82ffSelric (*ctx)->config.dbname = strdup(params->dbname);
257e0895134Schristos if ((*ctx)->config.dbname == NULL)
258e0895134Schristos return ENOMEM;
259e0895134Schristos }
260e0895134Schristos if (is_set(ACL_FILE)) {
261f59d82ffSelric (*ctx)->config.acl_file = strdup(params->acl_file);
262e0895134Schristos if ((*ctx)->config.acl_file == NULL)
263e0895134Schristos return ENOMEM;
264e0895134Schristos }
265e0895134Schristos if (is_set(STASH_FILE)) {
266f59d82ffSelric (*ctx)->config.stash_file = strdup(params->stash_file);
267e0895134Schristos if ((*ctx)->config.stash_file == NULL)
268e0895134Schristos return ENOMEM;
269e0895134Schristos }
270f59d82ffSelric
271f59d82ffSelric find_db_spec(*ctx);
272f59d82ffSelric
273f59d82ffSelric /* PROFILE can't be specified for now */
274f59d82ffSelric /* KADMIND_PORT is supposed to be used on the server also,
275f59d82ffSelric but this doesn't make sense */
276f59d82ffSelric /* ADMIN_SERVER is client only */
277f59d82ffSelric /* ADNAME is not used at all (as far as I can tell) */
278f59d82ffSelric /* ADB_LOCKFILE ditto */
279f59d82ffSelric /* DICT_FILE */
280f59d82ffSelric /* ADMIN_KEYTAB */
281f59d82ffSelric /* MKEY_FROM_KEYBOARD is not supported */
282f59d82ffSelric /* MKEY_NAME neither */
283f59d82ffSelric /* ENCTYPE */
284f59d82ffSelric /* MAX_LIFE */
285f59d82ffSelric /* MAX_RLIFE */
286f59d82ffSelric /* EXPIRATION */
287f59d82ffSelric /* FLAGS */
288f59d82ffSelric /* ENCTYPES */
289f59d82ffSelric
290f59d82ffSelric return 0;
291f59d82ffSelric }
292f59d82ffSelric
293f59d82ffSelric HDB *
_kadm5_s_get_db(void * server_handle)294f59d82ffSelric _kadm5_s_get_db(void *server_handle)
295f59d82ffSelric {
296f59d82ffSelric kadm5_server_context *context = server_handle;
297f59d82ffSelric return context->db;
298f59d82ffSelric }
299