cf/largefile.m4: Fix build with autoconf-2.72
[heimdal.git] / lib / kadm5 / context_s.c
blob5c9b3e31c81a174c6b6857b76e326f510cd1a626
1 /*
2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "kadm5_locl.h"
36 RCSID("$Id$");
38 static kadm5_ret_t
39 kadm5_s_lock(void *server_handle)
41 kadm5_server_context *context = server_handle;
42 kadm5_ret_t ret;
44 if (context->keep_open) {
46 * We open/close around every operation, but we retain the DB
47 * open if the DB was locked with a prior call to kadm5_lock(),
48 * so if it's open here that must be because the DB is locked.
50 heim_assert(context->db->lock_count > 0,
51 "Internal error in tracking HDB locks");
52 return KADM5_ALREADY_LOCKED;
55 ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
56 if (ret)
57 return ret;
59 ret = context->db->hdb_lock(context->context, context->db, HDB_WLOCK);
60 if (ret) {
61 (void) context->db->hdb_close(context->context, context->db);
62 return ret;
66 * Attempt to recover the log. This will generally fail on slaves,
67 * and we can't tell if we're on a slave here.
69 * Perhaps we could set a flag in the kadm5_server_context to
70 * indicate whether a read has been done without recovering the log,
71 * in which case we could fail any subsequent writes.
73 if (kadm5_log_init(context) == 0)
74 (void) kadm5_log_end(context);
76 context->keep_open = 1;
77 return 0;
80 static kadm5_ret_t
81 kadm5_s_unlock(void *server_handle)
83 kadm5_server_context *context = server_handle;
84 kadm5_ret_t ret;
86 if (!context->keep_open)
87 return KADM5_NOT_LOCKED;
89 context->keep_open = 0;
90 ret = context->db->hdb_unlock(context->context, context->db);
91 (void) context->db->hdb_close(context->context, context->db);
92 return ret;
95 static void
96 set_funcs(kadm5_server_context *c)
98 #define SET(C, F) (C)->funcs.F = kadm5_s_ ## F
99 SET(c, chpass_principal);
100 SET(c, chpass_principal_with_key);
101 SET(c, create_principal);
102 SET(c, delete_principal);
103 SET(c, destroy);
104 SET(c, flush);
105 SET(c, get_principal);
106 SET(c, get_principals);
107 SET(c, get_privs);
108 SET(c, modify_principal);
109 SET(c, prune_principal);
110 SET(c, randkey_principal);
111 SET(c, rename_principal);
112 SET(c, lock);
113 SET(c, unlock);
114 SET(c, setkey_principal_3);
115 SET(c, iter_principals);
116 SET(c, dup_context);
119 #ifndef NO_UNIX_SOCKETS
121 static void
122 set_socket_name(krb5_context context, struct sockaddr_un *un)
124 const char *fn = kadm5_log_signal_socket(context);
126 memset(un, 0, sizeof(*un));
127 un->sun_family = AF_UNIX;
128 strlcpy (un->sun_path, fn, sizeof(un->sun_path));
131 #else
133 static void
134 set_socket_info(krb5_context context, struct addrinfo **info)
136 kadm5_log_signal_socket_info(context, 0, info);
139 #endif
141 static kadm5_ret_t
142 find_db_spec(kadm5_server_context *ctx)
144 krb5_context context = ctx->context;
145 struct hdb_dbinfo *info, *d;
146 krb5_error_code ret;
147 int aret;
149 if (ctx->config.realm) {
150 /* fetch the databases */
151 ret = hdb_get_dbinfo(context, &info);
152 if (ret)
153 return ret;
155 d = NULL;
156 while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
157 const char *p = hdb_dbinfo_get_realm(context, d);
159 /* match default (realm-less) */
160 if(p != NULL && strcmp(ctx->config.realm, p) != 0)
161 continue;
163 p = hdb_dbinfo_get_dbname(context, d);
164 if (p) {
165 ctx->config.dbname = strdup(p);
166 if (ctx->config.dbname == NULL) {
167 hdb_free_dbinfo(context, &info);
168 return krb5_enomem(context);
172 p = hdb_dbinfo_get_acl_file(context, d);
173 if (p) {
174 ctx->config.acl_file = strdup(p);
175 if (ctx->config.acl_file == NULL) {
176 hdb_free_dbinfo(context, &info);
177 return krb5_enomem(context);
181 p = hdb_dbinfo_get_mkey_file(context, d);
182 if (p) {
183 ctx->config.stash_file = strdup(p);
184 if (ctx->config.stash_file == NULL) {
185 hdb_free_dbinfo(context, &info);
186 return krb5_enomem(context);
190 p = hdb_dbinfo_get_log_file(context, d);
191 if (p) {
192 ctx->log_context.log_file = strdup(p);
193 if (ctx->log_context.log_file == NULL) {
194 hdb_free_dbinfo(context, &info);
195 return krb5_enomem(context);
198 break;
200 hdb_free_dbinfo(context, &info);
203 /* If any of the values was unset, pick up the default value */
205 if (ctx->config.dbname == NULL) {
206 ctx->config.dbname = strdup(hdb_default_db(context));
207 if (ctx->config.dbname == NULL)
208 return krb5_enomem(context);
210 if (ctx->config.acl_file == NULL) {
211 aret = asprintf(&ctx->config.acl_file, "%s/kadmind.acl",
212 hdb_db_dir(context));
213 if (aret == -1)
214 return krb5_enomem(context);
216 if (ctx->config.stash_file == NULL) {
217 aret = asprintf(&ctx->config.stash_file, "%s/m-key",
218 hdb_db_dir(context));
219 if (aret == -1)
220 return krb5_enomem(context);
222 if (ctx->log_context.log_file == NULL) {
223 aret = asprintf(&ctx->log_context.log_file, "%s/log",
224 hdb_db_dir(context));
225 if (aret == -1)
226 return krb5_enomem(context);
229 #ifndef NO_UNIX_SOCKETS
230 set_socket_name(context, &ctx->log_context.socket_name);
231 #else
232 set_socket_info(context, &ctx->log_context.socket_info);
233 #endif
235 return 0;
238 kadm5_ret_t
239 _kadm5_s_init_context(kadm5_server_context **ctx,
240 kadm5_config_params *params,
241 krb5_context context)
243 kadm5_ret_t ret = 0;
245 *ctx = calloc(1, sizeof(**ctx));
246 if (*ctx == NULL)
247 return krb5_enomem(context);
248 (*ctx)->log_context.socket_fd = rk_INVALID_SOCKET;
250 set_funcs(*ctx);
251 (*ctx)->context = context;
252 krb5_add_et_list (context, initialize_kadm5_error_table_r);
254 #define is_set(M) (params && params->mask & KADM5_CONFIG_ ## M)
255 if (params)
256 (*ctx)->config.mask = params->mask;
257 if (is_set(REALM)) {
258 (*ctx)->config.realm = strdup(params->realm);
259 if ((*ctx)->config.realm == NULL)
260 return krb5_enomem(context);
261 } else {
262 ret = krb5_get_default_realm(context, &(*ctx)->config.realm);
263 if (ret)
264 return ret;
266 if (is_set(DBNAME)) {
267 (*ctx)->config.dbname = strdup(params->dbname);
268 if ((*ctx)->config.dbname == NULL)
269 return krb5_enomem(context);
271 if (is_set(ACL_FILE)) {
272 (*ctx)->config.acl_file = strdup(params->acl_file);
273 if ((*ctx)->config.acl_file == NULL)
274 return krb5_enomem(context);
276 if (is_set(STASH_FILE)) {
277 (*ctx)->config.stash_file = strdup(params->stash_file);
278 if ((*ctx)->config.stash_file == NULL)
279 return krb5_enomem(context);
282 ret = find_db_spec(*ctx);
283 if (ret == 0)
284 ret = _kadm5_s_init_hooks(*ctx);
285 if (ret != 0) {
286 kadm5_s_destroy(*ctx);
287 *ctx = NULL;
288 return ret;
291 /* PROFILE can't be specified for now */
292 /* KADMIND_PORT is supposed to be used on the server also,
293 but this doesn't make sense */
294 /* ADMIN_SERVER is client only */
295 /* ADNAME is not used at all (as far as I can tell) */
296 /* ADB_LOCKFILE ditto */
297 /* DICT_FILE */
298 /* ADMIN_KEYTAB */
299 /* MKEY_FROM_KEYBOARD is not supported */
300 /* MKEY_NAME neither */
301 /* ENCTYPE */
302 /* MAX_LIFE */
303 /* MAX_RLIFE */
304 /* EXPIRATION */
305 /* FLAGS */
306 /* ENCTYPES */
308 return 0;
311 HDB *
312 _kadm5_s_get_db(void *server_handle)
314 kadm5_server_context *context = server_handle;
315 return context->db;