Debian Codegraph @

lib/kadm5/context_s.c

raw
1 /*
2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34 #include "kadm5_locl.h"
35
36 RCSID("$Id$");
37
38 static kadm5_ret_t
39 kadm5_s_lock(void *server_handle)
40 {
41 kadm5_server_context *context = server_handle;
42 kadm5_ret_t ret;
43
44 if (context->keep_open) {
45 /*
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.
49 */
50 heim_assert(context->db->lock_count > 0,
51 "Internal error in tracking HDB locks");
52 return KADM5_ALREADY_LOCKED;
53 }
54
55 ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
56 if (ret)
57 return ret;
58
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;
63 }
64
65 /*
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.
68 *
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.
72 */
73 if (kadm5_log_init(context) == 0)
74 (void) kadm5_log_end(context);
75
76 context->keep_open = 1;
77 return 0;
78 }
79
80 static kadm5_ret_t
81 kadm5_s_unlock(void *server_handle)
82 {
83 kadm5_server_context *context = server_handle;
84 kadm5_ret_t ret;
85
86 if (!context->keep_open)
87 return KADM5_NOT_LOCKED;
88
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;
93 }
94
95 static void
96 set_funcs(kadm5_server_context *c)
97 {
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, randkey_principal);
110 SET(c, rename_principal);
111 SET(c, lock);
112 SET(c, unlock);
113 SET(c, setkey_principal_3);
114 }
115
116 #ifndef NO_UNIX_SOCKETS
117
118 static void
119 set_socket_name(krb5_context context, struct sockaddr_un *un)
120 {
121 const char *fn = kadm5_log_signal_socket(context);
122
123 memset(un, 0, sizeof(*un));
124 un->sun_family = AF_UNIX;
125 strlcpy (un->sun_path, fn, sizeof(un->sun_path));
126
127 }
128 #else
129
130 static void
131 set_socket_info(krb5_context context, struct addrinfo **info)
132 {
133 kadm5_log_signal_socket_info(context, 0, info);
134 }
135
136 #endif
137
138 static kadm5_ret_t
139 find_db_spec(kadm5_server_context *ctx)
140 {
141 krb5_context context = ctx->context;
142 struct hdb_dbinfo *info, *d;
143 krb5_error_code ret;
144 int aret;
145
146 if (ctx->config.realm) {
147 /* fetch the databases */
148 ret = hdb_get_dbinfo(context, &info);
149 if (ret)
150 return ret;
151
152 d = NULL;
153 while ((d = hdb_dbinfo_get_next(info, d)) != NULL) {
154 const char *p = hdb_dbinfo_get_realm(context, d);
155
156 /* match default (realm-less) */
157 if(p != NULL && strcmp(ctx->config.realm, p) != 0)
158 continue;
159
160 p = hdb_dbinfo_get_dbname(context, d);
161 if (p) {
162 ctx->config.dbname = strdup(p);
163 if (ctx->config.dbname == NULL)
164 return ENOMEM;
165 }
166
167 p = hdb_dbinfo_get_acl_file(context, d);
168 if (p) {
169 ctx->config.acl_file = strdup(p);
170 if (ctx->config.acl_file == NULL)
171 return ENOMEM;
172 }
173
174 p = hdb_dbinfo_get_mkey_file(context, d);
175 if (p) {
176 ctx->config.stash_file = strdup(p);
177 if (ctx->config.stash_file == NULL)
178 return ENOMEM;
179 }
180
181 p = hdb_dbinfo_get_log_file(context, d);
182 if (p) {
183 ctx->log_context.log_file = strdup(p);
184 if (ctx->log_context.log_file == NULL)
185 return ENOMEM;
186 }
187 break;
188 }
189 hdb_free_dbinfo(context, &info);
190 }
191
192 /* If any of the values was unset, pick up the default value */
193
194 if (ctx->config.dbname == NULL) {
195 ctx->config.dbname = strdup(hdb_default_db(context));
196 if (ctx->config.dbname == NULL)
197 return ENOMEM;
198 }
199 if (ctx->config.acl_file == NULL) {
200 aret = asprintf(&ctx->config.acl_file, "%s/kadmind.acl",
201 hdb_db_dir(context));
202 if (aret == -1)
203 return ENOMEM;
204 }
205 if (ctx->config.stash_file == NULL) {
206 aret = asprintf(&ctx->config.stash_file, "%s/m-key",
207 hdb_db_dir(context));
208 if (aret == -1)
209 return ENOMEM;
210 }
211 if (ctx->log_context.log_file == NULL) {
212 aret = asprintf(&ctx->log_context.log_file, "%s/log",
213 hdb_db_dir(context));
214 if (aret == -1)
215 return ENOMEM;
216 }
217
218 #ifndef NO_UNIX_SOCKETS
219 set_socket_name(context, &ctx->log_context.socket_name);
220 #else
221 set_socket_info(context, &ctx->log_context.socket_info);
222 #endif
223
224 return 0;
225 }
226
227 kadm5_ret_t
228 _kadm5_s_init_context(kadm5_server_context **ctx,
229 kadm5_config_params *params,
230 krb5_context context)
231 {
232 kadm5_ret_t ret = 0;
233
234 *ctx = calloc(1, sizeof(**ctx));
235 if (*ctx == NULL)
236 return ENOMEM;
237 (*ctx)->log_context.socket_fd = rk_INVALID_SOCKET;
238
239 set_funcs(*ctx);
240 (*ctx)->context = context;
241 krb5_add_et_list (context, initialize_kadm5_error_table_r);
242
243 #define is_set(M) (params && params->mask & KADM5_CONFIG_ ## M)
244 if (is_set(REALM)) {
245 (*ctx)->config.realm = strdup(params->realm);
246 if ((*ctx)->config.realm == NULL)
247 return ENOMEM;
248 } else {
249 ret = krb5_get_default_realm(context, &(*ctx)->config.realm);
250 if (ret)
251 return ret;
252 }
253 if (is_set(DBNAME)) {
254 (*ctx)->config.dbname = strdup(params->dbname);
255 if ((*ctx)->config.dbname == NULL)
256 return ENOMEM;
257 }
258 if (is_set(ACL_FILE)) {
259 (*ctx)->config.acl_file = strdup(params->acl_file);
260 if ((*ctx)->config.acl_file == NULL)
261 return ENOMEM;
262 }
263 if (is_set(STASH_FILE)) {
264 (*ctx)->config.stash_file = strdup(params->stash_file);
265 if ((*ctx)->config.stash_file == NULL)
266 return ENOMEM;
267 }
268
269 find_db_spec(*ctx);
270
271 /* PROFILE can't be specified for now */
272 /* KADMIND_PORT is supposed to be used on the server also,
273 but this doesn't make sense */
274 /* ADMIN_SERVER is client only */
275 /* ADNAME is not used at all (as far as I can tell) */
276 /* ADB_LOCKFILE ditto */
277 /* DICT_FILE */
278 /* ADMIN_KEYTAB */
279 /* MKEY_FROM_KEYBOARD is not supported */
280 /* MKEY_NAME neither */
281 /* ENCTYPE */
282 /* MAX_LIFE */
283 /* MAX_RLIFE */
284 /* EXPIRATION */
285 /* FLAGS */
286 /* ENCTYPES */
287
288 return 0;
289 }
290
291 HDB *
292 _kadm5_s_get_db(void *server_handle)
293 {
294 kadm5_server_context *context = server_handle;
295 return context->db;
296 }