xref: /freebsd/crypto/heimdal/lib/kadm5/create_s.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copyright (c) 1997-2001 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: create_s.c,v 1.19 2001/01/30 01:24:28 assar Exp $");
37 
38 static kadm5_ret_t
39 get_default(kadm5_server_context *context, krb5_principal princ,
40 	    kadm5_principal_ent_t def)
41 {
42     kadm5_ret_t ret;
43     krb5_principal def_principal;
44     krb5_realm *realm = krb5_princ_realm(context->context, princ);
45 
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;
54 }
55 
56 static kadm5_ret_t
57 create_principal(kadm5_server_context *context,
58 		 kadm5_principal_ent_t princ,
59 		 u_int32_t mask,
60 		 hdb_entry *ent,
61 		 u_int32_t required_mask,
62 		 u_int32_t forbidden_mask)
63 {
64     kadm5_ret_t ret;
65     kadm5_principal_ent_rec defrec, *defent;
66     u_int32_t def_mask;
67 
68     if((mask & required_mask) != required_mask)
69 	return KADM5_BAD_MASK;
70     if((mask & forbidden_mask))
71 	return KADM5_BAD_MASK;
72     if((mask & KADM5_POLICY) && strcmp(princ->policy, "default"))
73 	/* XXX no real policies for now */
74 	return KADM5_UNK_POLICY;
75     memset(ent, 0, sizeof(*ent));
76     ret  = krb5_copy_principal(context->context, princ->principal,
77 			       &ent->principal);
78     if(ret)
79 	return ret;
80 
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;
88     }
89 
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 
97     ent->created_by.time = time(NULL);
98     ret = krb5_copy_principal(context->context, context->caller,
99 			      &ent->created_by.principal);
100 
101     return ret;
102 }
103 
104 kadm5_ret_t
105 kadm5_s_create_principal_with_key(void *server_handle,
106 				  kadm5_principal_ent_t princ,
107 				  u_int32_t mask)
108 {
109     kadm5_ret_t ret;
110     hdb_entry ent;
111     kadm5_server_context *context = server_handle;
112 
113     ret = create_principal(context, princ, mask, &ent,
114 			   KADM5_PRINCIPAL | KADM5_KEY_DATA,
115 			   KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
116 			   | KADM5_MOD_NAME | KADM5_MKVNO
117 			   | KADM5_AUX_ATTRIBUTES
118 			   | KADM5_POLICY_CLR | KADM5_LAST_SUCCESS
119 			   | KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);
120     if(ret)
121 	goto out;
122 
123     ret = _kadm5_set_keys2(context, &ent, princ->n_key_data, princ->key_data);
124     if(ret)
125 	goto out;
126 
127     ret = hdb_seal_keys(context->context, context->db, &ent);
128     if (ret)
129 	goto out;
130 
131     kadm5_log_create (context, &ent);
132 
133     ret = context->db->open(context->context, context->db, O_RDWR, 0);
134     if(ret)
135 	goto out;
136     ret = context->db->store(context->context, context->db, 0, &ent);
137     context->db->close(context->context, context->db);
138 out:
139     hdb_free_entry(context->context, &ent);
140     return _kadm5_error_code(ret);
141 }
142 
143 
144 kadm5_ret_t
145 kadm5_s_create_principal(void *server_handle,
146 			 kadm5_principal_ent_t princ,
147 			 u_int32_t mask,
148 			 char *password)
149 {
150     kadm5_ret_t ret;
151     hdb_entry ent;
152     kadm5_server_context *context = server_handle;
153 
154     ret = create_principal(context, princ, mask, &ent,
155 			   KADM5_PRINCIPAL,
156 			   KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
157 			   | KADM5_MOD_NAME | KADM5_MKVNO
158 			   | KADM5_AUX_ATTRIBUTES | KADM5_KEY_DATA
159 			   | KADM5_POLICY_CLR | KADM5_LAST_SUCCESS
160 			   | KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);
161     if(ret)
162 	goto out;
163 
164     /* XXX this should be fixed */
165     ent.keys.len = 4;
166     ent.keys.val = calloc(ent.keys.len, sizeof(*ent.keys.val));
167     ent.keys.val[0].key.keytype = ETYPE_DES_CBC_CRC;
168     /* flag as version 4 compatible salt; ignored by _kadm5_set_keys
169        if we don't want to be compatible */
170     ent.keys.val[0].salt = calloc(1, sizeof(*ent.keys.val[0].salt));
171     ent.keys.val[0].salt->type = hdb_pw_salt;
172     ent.keys.val[1].key.keytype = ETYPE_DES_CBC_MD4;
173     ent.keys.val[1].salt = calloc(1, sizeof(*ent.keys.val[1].salt));
174     ent.keys.val[1].salt->type = hdb_pw_salt;
175     ent.keys.val[2].key.keytype = ETYPE_DES_CBC_MD5;
176     ent.keys.val[2].salt = calloc(1, sizeof(*ent.keys.val[2].salt));
177     ent.keys.val[2].salt->type = hdb_pw_salt;
178     ent.keys.val[3].key.keytype = ETYPE_DES3_CBC_SHA1;
179     ret = _kadm5_set_keys(context, &ent, password);
180     if (ret)
181 	goto out;
182 
183     ret = hdb_seal_keys(context->context, context->db, &ent);
184     if (ret)
185 	goto out;
186 
187     kadm5_log_create (context, &ent);
188 
189     ret = context->db->open(context->context, context->db, O_RDWR, 0);
190     if(ret)
191 	goto out;
192     ret = context->db->store(context->context, context->db, 0, &ent);
193     context->db->close(context->context, context->db);
194 out:
195     hdb_free_entry(context->context, &ent);
196     return _kadm5_error_code(ret);
197 }
198 
199