xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/kadm5/create_s.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: create_s.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 1997-2001 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include "kadm5_locl.h"
37ca1c9b0cSelric 
38*d3273b5bSchristos __RCSID("$NetBSD: create_s.c,v 1.2 2017/01/28 21:31:49 christos Exp $");
39ca1c9b0cSelric 
40ca1c9b0cSelric static kadm5_ret_t
get_default(kadm5_server_context * context,krb5_principal princ,kadm5_principal_ent_t def)41ca1c9b0cSelric get_default(kadm5_server_context *context, krb5_principal princ,
42ca1c9b0cSelric 	    kadm5_principal_ent_t def)
43ca1c9b0cSelric {
44ca1c9b0cSelric     kadm5_ret_t ret;
45ca1c9b0cSelric     krb5_principal def_principal;
46ca1c9b0cSelric     krb5_const_realm realm = krb5_principal_get_realm(context->context, princ);
47ca1c9b0cSelric 
48ca1c9b0cSelric     ret = krb5_make_principal(context->context, &def_principal,
49ca1c9b0cSelric 			      realm, "default", NULL);
50ca1c9b0cSelric     if (ret)
51ca1c9b0cSelric 	return ret;
52ca1c9b0cSelric     ret = kadm5_s_get_principal(context, def_principal, def,
53ca1c9b0cSelric 				KADM5_PRINCIPAL_NORMAL_MASK);
54ca1c9b0cSelric     krb5_free_principal (context->context, def_principal);
55ca1c9b0cSelric     return ret;
56ca1c9b0cSelric }
57ca1c9b0cSelric 
58ca1c9b0cSelric static kadm5_ret_t
create_principal(kadm5_server_context * context,kadm5_principal_ent_t princ,uint32_t mask,hdb_entry_ex * ent,uint32_t required_mask,uint32_t forbidden_mask)59ca1c9b0cSelric create_principal(kadm5_server_context *context,
60ca1c9b0cSelric 		 kadm5_principal_ent_t princ,
61ca1c9b0cSelric 		 uint32_t mask,
62ca1c9b0cSelric 		 hdb_entry_ex *ent,
63ca1c9b0cSelric 		 uint32_t required_mask,
64ca1c9b0cSelric 		 uint32_t forbidden_mask)
65ca1c9b0cSelric {
66ca1c9b0cSelric     kadm5_ret_t ret;
67ca1c9b0cSelric     kadm5_principal_ent_rec defrec, *defent;
68ca1c9b0cSelric     uint32_t def_mask;
69ca1c9b0cSelric 
70b9d004c6Schristos     memset(ent, 0, sizeof(*ent));
71ca1c9b0cSelric     if((mask & required_mask) != required_mask)
72ca1c9b0cSelric 	return KADM5_BAD_MASK;
73ca1c9b0cSelric     if((mask & forbidden_mask))
74ca1c9b0cSelric 	return KADM5_BAD_MASK;
75ca1c9b0cSelric     if((mask & KADM5_POLICY) && strcmp(princ->policy, "default"))
76ca1c9b0cSelric 	/* XXX no real policies for now */
77ca1c9b0cSelric 	return KADM5_UNK_POLICY;
78ca1c9b0cSelric     ret  = krb5_copy_principal(context->context, princ->principal,
79ca1c9b0cSelric 			       &ent->entry.principal);
80ca1c9b0cSelric     if(ret)
81ca1c9b0cSelric 	return ret;
82ca1c9b0cSelric 
83ca1c9b0cSelric     defent = &defrec;
84ca1c9b0cSelric     ret = get_default(context, princ->principal, defent);
85ca1c9b0cSelric     if(ret) {
86ca1c9b0cSelric 	defent   = NULL;
87ca1c9b0cSelric 	def_mask = 0;
88ca1c9b0cSelric     } else {
89ca1c9b0cSelric 	def_mask = KADM5_ATTRIBUTES | KADM5_MAX_LIFE | KADM5_MAX_RLIFE;
90ca1c9b0cSelric     }
91ca1c9b0cSelric 
92ca1c9b0cSelric     ret = _kadm5_setup_entry(context,
93ca1c9b0cSelric 			     ent, mask | def_mask,
94ca1c9b0cSelric 			     princ, mask,
95ca1c9b0cSelric 			     defent, def_mask);
96ca1c9b0cSelric     if(defent)
97ca1c9b0cSelric 	kadm5_free_principal_ent(context, defent);
98ca1c9b0cSelric     if (ret)
99ca1c9b0cSelric 	return ret;
100ca1c9b0cSelric 
101ca1c9b0cSelric     ent->entry.created_by.time = time(NULL);
102ca1c9b0cSelric 
103ca1c9b0cSelric     return krb5_copy_principal(context->context, context->caller,
104ca1c9b0cSelric 			       &ent->entry.created_by.principal);
105ca1c9b0cSelric }
106ca1c9b0cSelric 
107ca1c9b0cSelric kadm5_ret_t
kadm5_s_create_principal_with_key(void * server_handle,kadm5_principal_ent_t princ,uint32_t mask)108ca1c9b0cSelric kadm5_s_create_principal_with_key(void *server_handle,
109ca1c9b0cSelric 				  kadm5_principal_ent_t princ,
110ca1c9b0cSelric 				  uint32_t mask)
111ca1c9b0cSelric {
112ca1c9b0cSelric     kadm5_ret_t ret;
113ca1c9b0cSelric     hdb_entry_ex ent;
114ca1c9b0cSelric     kadm5_server_context *context = server_handle;
115ca1c9b0cSelric 
116b9d004c6Schristos     if ((mask & KADM5_KVNO) == 0) {
117b9d004c6Schristos 	/* create_principal() through _kadm5_setup_entry(), will need this */
118b9d004c6Schristos 	princ->kvno = 1;
119b9d004c6Schristos 	mask |= KADM5_KVNO;
120b9d004c6Schristos     }
121b9d004c6Schristos 
122ca1c9b0cSelric     ret = create_principal(context, princ, mask, &ent,
123ca1c9b0cSelric 			   KADM5_PRINCIPAL | KADM5_KEY_DATA,
124ca1c9b0cSelric 			   KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
125ca1c9b0cSelric 			   | KADM5_MOD_NAME | KADM5_MKVNO
126ca1c9b0cSelric 			   | KADM5_AUX_ATTRIBUTES
127ca1c9b0cSelric 			   | KADM5_POLICY_CLR | KADM5_LAST_SUCCESS
128ca1c9b0cSelric 			   | KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);
129ca1c9b0cSelric     if (ret)
130b9d004c6Schristos         return ret;
131ca1c9b0cSelric 
132b9d004c6Schristos     if (!context->keep_open) {
133b9d004c6Schristos         ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
134b9d004c6Schristos         if (ret) {
135b9d004c6Schristos             hdb_free_entry(context->context, &ent);
136b9d004c6Schristos             return ret;
137b9d004c6Schristos         }
138b9d004c6Schristos     }
139b9d004c6Schristos 
140b9d004c6Schristos     ret = kadm5_log_init(context);
141b9d004c6Schristos     if (ret)
142b9d004c6Schristos         goto out;
143ca1c9b0cSelric 
144ca1c9b0cSelric     ret = hdb_seal_keys(context->context, context->db, &ent.entry);
145ca1c9b0cSelric     if (ret)
146b9d004c6Schristos 	goto out2;
147ca1c9b0cSelric 
148b9d004c6Schristos     /* This logs the change for iprop and writes to the HDB */
149b9d004c6Schristos     ret = kadm5_log_create(context, &ent.entry);
150ca1c9b0cSelric 
151b9d004c6Schristos  out2:
152b9d004c6Schristos     (void) kadm5_log_end(context);
153ca1c9b0cSelric  out:
154b9d004c6Schristos     if (!context->keep_open) {
155b9d004c6Schristos         kadm5_ret_t ret2;
156b9d004c6Schristos         ret2 = context->db->hdb_close(context->context, context->db);
157b9d004c6Schristos         if (ret == 0 && ret2 != 0)
158b9d004c6Schristos             ret = ret2;
159b9d004c6Schristos     }
160ca1c9b0cSelric     hdb_free_entry(context->context, &ent);
161ca1c9b0cSelric     return _kadm5_error_code(ret);
162ca1c9b0cSelric }
163ca1c9b0cSelric 
164ca1c9b0cSelric 
165ca1c9b0cSelric kadm5_ret_t
kadm5_s_create_principal(void * server_handle,kadm5_principal_ent_t princ,uint32_t mask,int n_ks_tuple,krb5_key_salt_tuple * ks_tuple,const char * password)166ca1c9b0cSelric kadm5_s_create_principal(void *server_handle,
167ca1c9b0cSelric 			 kadm5_principal_ent_t princ,
168ca1c9b0cSelric 			 uint32_t mask,
169b9d004c6Schristos 			 int n_ks_tuple,
170b9d004c6Schristos 			 krb5_key_salt_tuple *ks_tuple,
171ca1c9b0cSelric 			 const char *password)
172ca1c9b0cSelric {
173ca1c9b0cSelric     kadm5_ret_t ret;
174ca1c9b0cSelric     hdb_entry_ex ent;
175ca1c9b0cSelric     kadm5_server_context *context = server_handle;
176ca1c9b0cSelric 
177b9d004c6Schristos     if ((mask & KADM5_KVNO) == 0) {
178b9d004c6Schristos 	/* create_principal() through _kadm5_setup_entry(), will need this */
179b9d004c6Schristos 	princ->kvno = 1;
180b9d004c6Schristos 	mask |= KADM5_KVNO;
181b9d004c6Schristos     }
182b9d004c6Schristos 
183ca1c9b0cSelric     ret = create_principal(context, princ, mask, &ent,
184ca1c9b0cSelric 			   KADM5_PRINCIPAL,
185ca1c9b0cSelric 			   KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
186ca1c9b0cSelric 			   | KADM5_MOD_NAME | KADM5_MKVNO
187ca1c9b0cSelric 			   | KADM5_AUX_ATTRIBUTES | KADM5_KEY_DATA
188ca1c9b0cSelric 			   | KADM5_POLICY_CLR | KADM5_LAST_SUCCESS
189ca1c9b0cSelric 			   | KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);
190ca1c9b0cSelric     if (ret)
191b9d004c6Schristos         return ret;
192ca1c9b0cSelric 
193b9d004c6Schristos     if (!context->keep_open) {
194b9d004c6Schristos         ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
195b9d004c6Schristos         if (ret) {
196b9d004c6Schristos             hdb_free_entry(context->context, &ent);
197b9d004c6Schristos             return ret;
198b9d004c6Schristos         }
199b9d004c6Schristos     }
200b9d004c6Schristos 
201b9d004c6Schristos     ret = kadm5_log_init(context);
202b9d004c6Schristos     if (ret)
203b9d004c6Schristos         goto out;
204ca1c9b0cSelric 
205ca1c9b0cSelric     ent.entry.keys.len = 0;
206ca1c9b0cSelric     ent.entry.keys.val = NULL;
207ca1c9b0cSelric 
208b9d004c6Schristos     ret = _kadm5_set_keys(context, &ent.entry, n_ks_tuple, ks_tuple, password);
209ca1c9b0cSelric     if (ret)
210b9d004c6Schristos 	goto out2;
211ca1c9b0cSelric 
212ca1c9b0cSelric     ret = hdb_seal_keys(context->context, context->db, &ent.entry);
213ca1c9b0cSelric     if (ret)
214b9d004c6Schristos 	goto out2;
215ca1c9b0cSelric 
216b9d004c6Schristos     /* This logs the change for iprop and writes to the HDB */
217b9d004c6Schristos     ret = kadm5_log_create(context, &ent.entry);
218ca1c9b0cSelric 
219b9d004c6Schristos  out2:
220b9d004c6Schristos     (void) kadm5_log_end(context);
221ca1c9b0cSelric  out:
222b9d004c6Schristos     if (!context->keep_open) {
223b9d004c6Schristos         kadm5_ret_t ret2;
224b9d004c6Schristos         ret2 = context->db->hdb_close(context->context, context->db);
225b9d004c6Schristos         if (ret == 0 && ret2 != 0)
226b9d004c6Schristos             ret = ret2;
227b9d004c6Schristos     }
228ca1c9b0cSelric     hdb_free_entry(context->context, &ent);
229ca1c9b0cSelric     return _kadm5_error_code(ret);
230ca1c9b0cSelric }
231ca1c9b0cSelric 
232