afslog.1: Remove documentation for removed no-v4 argument.
[heimdal.git] / lib / kadm5 / context_s.c
blobb28902a7980e5ea8a08ef44c96106711b0ef8ef4
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 return ret;
63 context->keep_open = 1;
64 return 0;
67 static kadm5_ret_t
68 kadm5_s_unlock(void *server_handle)
70 kadm5_server_context *context = server_handle;
71 kadm5_ret_t ret;
73 if (!context->keep_open)
74 return KADM5_NOT_LOCKED;
76 context->keep_open = 0;
77 ret = context->db->hdb_unlock(context->context, context->db);
78 (void) context->db->hdb_close(context->context, context->db);
79 return ret;
82 static void
83 set_funcs(kadm5_server_context *c)
85 #define SET(C, F) (C)->funcs.F = kadm5_s_ ## F
86 SET(c, chpass_principal);
87 SET(c, chpass_principal_with_key);
88 SET(c, create_principal);
89 SET(c, delete_principal);
90 SET(c, destroy);
91 SET(c, flush);
92 SET(c, get_principal);
93 SET(c, get_principals);
94 SET(c, get_privs);
95 SET(c, modify_principal);
96 SET(c, randkey_principal);
97 SET(c, rename_principal);
98 SET(c, lock);
99 SET(c, unlock);
102 #ifndef NO_UNIX_SOCKETS
104 static void
105 set_socket_name(krb5_context context, struct sockaddr_un *un)
107 const char *fn = kadm5_log_signal_socket(context);
109 memset(un, 0, sizeof(*un));
110 un->sun_family = AF_UNIX;
111 strlcpy (un->sun_path, fn, sizeof(un->sun_path));
114 #else
116 static void
117 set_socket_info(krb5_context context, struct addrinfo **info)
119 kadm5_log_signal_socket_info(context, 0, info);
122 #endif
124 static kadm5_ret_t
125 find_db_spec(kadm5_server_context *ctx)
127 krb5_context context = ctx->context;
128 struct hdb_dbinfo *info, *d;
129 krb5_error_code ret;
130 int aret;
132 if (ctx->config.realm) {
133 /* fetch the databases */
134 ret = hdb_get_dbinfo(context, &info);
135 if (ret)
136 return ret;
138 d = NULL;
139 while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
140 const char *p = hdb_dbinfo_get_realm(context, d);
142 /* match default (realm-less) */
143 if(p != NULL && strcmp(ctx->config.realm, p) != 0)
144 continue;
146 p = hdb_dbinfo_get_dbname(context, d);
147 if (p)
148 ctx->config.dbname = strdup(p);
150 p = hdb_dbinfo_get_acl_file(context, d);
151 if (p)
152 ctx->config.acl_file = strdup(p);
154 p = hdb_dbinfo_get_mkey_file(context, d);
155 if (p)
156 ctx->config.stash_file = strdup(p);
158 p = hdb_dbinfo_get_log_file(context, d);
159 if (p)
160 ctx->log_context.log_file = strdup(p);
161 break;
163 hdb_free_dbinfo(context, &info);
166 /* If any of the values was unset, pick up the default value */
168 if (ctx->config.dbname == NULL)
169 ctx->config.dbname = strdup(hdb_default_db(context));
170 if (ctx->config.acl_file == NULL) {
171 aret = asprintf(&ctx->config.acl_file, "%s/kadmind.acl",
172 hdb_db_dir(context));
173 if (aret == -1)
174 return ENOMEM;
176 if (ctx->config.stash_file == NULL) {
177 aret = asprintf(&ctx->config.stash_file, "%s/m-key",
178 hdb_db_dir(context));
179 if (aret == -1)
180 return ENOMEM;
182 if (ctx->log_context.log_file == NULL) {
183 aret = asprintf(&ctx->log_context.log_file, "%s/log",
184 hdb_db_dir(context));
185 if (aret == -1)
186 return ENOMEM;
189 #ifndef NO_UNIX_SOCKETS
190 set_socket_name(context, &ctx->log_context.socket_name);
191 #else
192 set_socket_info(context, &ctx->log_context.socket_info);
193 #endif
195 return 0;
198 kadm5_ret_t
199 _kadm5_s_init_context(kadm5_server_context **ctx,
200 kadm5_config_params *params,
201 krb5_context context)
203 *ctx = malloc(sizeof(**ctx));
204 if(*ctx == NULL)
205 return ENOMEM;
206 memset(*ctx, 0, sizeof(**ctx));
207 set_funcs(*ctx);
208 (*ctx)->context = context;
209 krb5_add_et_list (context, initialize_kadm5_error_table_r);
210 #define is_set(M) (params && params->mask & KADM5_CONFIG_ ## M)
211 if(is_set(REALM))
212 (*ctx)->config.realm = strdup(params->realm);
213 else
214 krb5_get_default_realm(context, &(*ctx)->config.realm);
215 if(is_set(DBNAME))
216 (*ctx)->config.dbname = strdup(params->dbname);
217 if(is_set(ACL_FILE))
218 (*ctx)->config.acl_file = strdup(params->acl_file);
219 if(is_set(STASH_FILE))
220 (*ctx)->config.stash_file = strdup(params->stash_file);
222 find_db_spec(*ctx);
224 /* PROFILE can't be specified for now */
225 /* KADMIND_PORT is supposed to be used on the server also,
226 but this doesn't make sense */
227 /* ADMIN_SERVER is client only */
228 /* ADNAME is not used at all (as far as I can tell) */
229 /* ADB_LOCKFILE ditto */
230 /* DICT_FILE */
231 /* ADMIN_KEYTAB */
232 /* MKEY_FROM_KEYBOARD is not supported */
233 /* MKEY_NAME neither */
234 /* ENCTYPE */
235 /* MAX_LIFE */
236 /* MAX_RLIFE */
237 /* EXPIRATION */
238 /* FLAGS */
239 /* ENCTYPES */
241 return 0;
244 HDB *
245 _kadm5_s_get_db(void *server_handle)
247 kadm5_server_context *context = server_handle;
248 return context->db;