Fix kadm5 error cleanup
[heimdal.git] / lib / kadm5 / create_s.c
blob7d8f89837d5dc80a6055810e8953fe951ed0f4b8
1 /*
2 * Copyright (c) 1997-2001 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 get_default(kadm5_server_context *context, krb5_principal princ,
40 kadm5_principal_ent_t def)
42 kadm5_ret_t ret;
43 krb5_principal def_principal;
44 krb5_const_realm realm = krb5_principal_get_realm(context->context, princ);
46 ret = krb5_make_principal(context->context, &def_principal,
47 realm, "default", NULL);
48 if (ret)
49 return ret;
50 ret = kadm5_s_get_principal(context, def_principal, def,
51 KADM5_PRINCIPAL_NORMAL_MASK);
52 krb5_free_principal (context->context, def_principal);
53 return ret;
56 static kadm5_ret_t
57 create_principal(kadm5_server_context *context,
58 kadm5_principal_ent_t princ,
59 uint32_t mask,
60 hdb_entry_ex *ent,
61 uint32_t required_mask,
62 uint32_t forbidden_mask)
64 kadm5_ret_t ret;
65 kadm5_principal_ent_rec defrec, *defent;
66 uint32_t def_mask;
68 memset(ent, 0, sizeof(*ent));
69 if((mask & required_mask) != required_mask)
70 return KADM5_BAD_MASK;
71 if((mask & forbidden_mask))
72 return KADM5_BAD_MASK;
73 if((mask & KADM5_POLICY) && strcmp(princ->policy, "default"))
74 /* XXX no real policies for now */
75 return KADM5_UNK_POLICY;
76 ret = krb5_copy_principal(context->context, princ->principal,
77 &ent->entry.principal);
78 if(ret)
79 return ret;
81 defent = &defrec;
82 ret = get_default(context, princ->principal, defent);
83 if(ret) {
84 defent = NULL;
85 def_mask = 0;
86 } else {
87 def_mask = KADM5_ATTRIBUTES | KADM5_MAX_LIFE | KADM5_MAX_RLIFE;
90 ret = _kadm5_setup_entry(context,
91 ent, mask | def_mask,
92 princ, mask,
93 defent, def_mask);
94 if(defent)
95 kadm5_free_principal_ent(context, defent);
96 if (ret)
97 return ret;
99 ent->entry.created_by.time = time(NULL);
101 return krb5_copy_principal(context->context, context->caller,
102 &ent->entry.created_by.principal);
105 kadm5_ret_t
106 kadm5_s_create_principal_with_key(void *server_handle,
107 kadm5_principal_ent_t princ,
108 uint32_t mask)
110 kadm5_ret_t ret;
111 hdb_entry_ex ent;
112 kadm5_server_context *context = server_handle;
114 if ((mask & KADM5_KVNO) == 0) {
115 /* create_principal() through _kadm5_setup_entry(), will need this */
116 princ->kvno = 1;
117 mask |= KADM5_KVNO;
120 ret = create_principal(context, princ, mask, &ent,
121 KADM5_PRINCIPAL | KADM5_KEY_DATA,
122 KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
123 | KADM5_MOD_NAME | KADM5_MKVNO
124 | KADM5_AUX_ATTRIBUTES
125 | KADM5_POLICY_CLR | KADM5_LAST_SUCCESS
126 | KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);
127 if (ret)
128 return ret;
130 if (!context->keep_open) {
131 ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
132 if (ret) {
133 hdb_free_entry(context->context, &ent);
134 return ret;
138 ret = kadm5_log_init(context);
139 if (ret)
140 goto out;
142 ret = hdb_seal_keys(context->context, context->db, &ent.entry);
143 if (ret)
144 goto out2;
146 /* This logs the change for iprop and writes to the HDB */
147 ret = kadm5_log_create(context, &ent.entry);
149 out2:
150 (void) kadm5_log_end(context);
151 out:
152 if (!context->keep_open) {
153 kadm5_ret_t ret2;
154 ret2 = context->db->hdb_close(context->context, context->db);
155 if (ret == 0 && ret2 != 0)
156 ret = ret2;
158 hdb_free_entry(context->context, &ent);
159 return _kadm5_error_code(ret);
163 kadm5_ret_t
164 kadm5_s_create_principal(void *server_handle,
165 kadm5_principal_ent_t princ,
166 uint32_t mask,
167 int n_ks_tuple,
168 krb5_key_salt_tuple *ks_tuple,
169 const char *password)
171 kadm5_ret_t ret;
172 hdb_entry_ex ent;
173 kadm5_server_context *context = server_handle;
175 if ((mask & KADM5_KVNO) == 0) {
176 /* create_principal() through _kadm5_setup_entry(), will need this */
177 princ->kvno = 1;
178 mask |= KADM5_KVNO;
181 ret = create_principal(context, princ, mask, &ent,
182 KADM5_PRINCIPAL,
183 KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
184 | KADM5_MOD_NAME | KADM5_MKVNO
185 | KADM5_AUX_ATTRIBUTES | KADM5_KEY_DATA
186 | KADM5_POLICY_CLR | KADM5_LAST_SUCCESS
187 | KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);
188 if (ret)
189 return ret;
191 if (!context->keep_open) {
192 ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
193 if (ret) {
194 hdb_free_entry(context->context, &ent);
195 return ret;
199 ret = kadm5_log_init(context);
200 if (ret)
201 goto out;
203 ent.entry.keys.len = 0;
204 ent.entry.keys.val = NULL;
206 ret = _kadm5_set_keys(context, &ent.entry, n_ks_tuple, ks_tuple, password);
207 if (ret)
208 goto out2;
210 ret = hdb_seal_keys(context->context, context->db, &ent.entry);
211 if (ret)
212 goto out2;
214 /* This logs the change for iprop and writes to the HDB */
215 ret = kadm5_log_create(context, &ent.entry);
217 out2:
218 (void) kadm5_log_end(context);
219 out:
220 if (!context->keep_open) {
221 kadm5_ret_t ret2;
222 ret2 = context->db->hdb_close(context->context, context->db);
223 if (ret == 0 && ret2 != 0)
224 ret = ret2;
226 hdb_free_entry(context->context, &ent);
227 return _kadm5_error_code(ret);