Update NEWS for some recent changes
[heimdal.git] / kadmin / init.c
blobbcf61fefefa3347610d093bd119fc16d623e3e16
1 /*
2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "kadmin_locl.h"
37 #include "kadmin-commands.h"
38 #include <kadm5/private.h>
40 #define CRE_DUP_OK 1
42 static kadm5_ret_t
43 create_random_entry(krb5_principal princ,
44 unsigned max_life,
45 unsigned max_rlife,
46 uint32_t attributes,
47 unsigned flags)
49 kadm5_principal_ent_rec ent;
50 kadm5_ret_t ret;
51 int mask = 0;
52 krb5_keyblock *keys;
53 int n_keys, i;
54 char *name;
55 const char *password;
56 char pwbuf[512];
58 random_password(pwbuf, sizeof(pwbuf));
59 password = pwbuf;
61 ret = krb5_unparse_name(context, princ, &name);
62 if (ret) {
63 krb5_warn(context, ret, "failed to unparse principal name");
64 return ret;
67 memset(&ent, 0, sizeof(ent));
68 ent.principal = princ;
69 mask |= KADM5_PRINCIPAL;
70 if (max_life) {
71 ent.max_life = max_life;
72 mask |= KADM5_MAX_LIFE;
74 if (max_rlife) {
75 ent.max_renewable_life = max_rlife;
76 mask |= KADM5_MAX_RLIFE;
78 ent.attributes |= attributes | KRB5_KDB_DISALLOW_ALL_TIX;
79 mask |= KADM5_ATTRIBUTES;
81 /* Create the entry with a random password */
82 ret = kadm5_create_principal(kadm_handle, &ent, mask, password);
83 if(ret) {
84 if (ret == KADM5_DUP && (flags & CRE_DUP_OK))
85 goto out;
86 krb5_warn(context, ret, "create_random_entry(%s): randkey failed",
87 name);
88 goto out;
91 /* Replace the string2key based keys with real random bytes */
92 ret = kadm5_randkey_principal(kadm_handle, princ, &keys, &n_keys);
93 if(ret) {
94 krb5_warn(context, ret, "create_random_entry(%s): randkey failed",
95 name);
96 goto out;
98 for(i = 0; i < n_keys; i++)
99 krb5_free_keyblock_contents(context, &keys[i]);
100 free(keys);
101 ret = kadm5_get_principal(kadm_handle, princ, &ent,
102 KADM5_PRINCIPAL | KADM5_ATTRIBUTES);
103 if(ret) {
104 krb5_warn(context, ret, "create_random_entry(%s): "
105 "unable to get principal", name);
106 goto out;
108 ent.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
109 ent.kvno = 1;
110 ret = kadm5_modify_principal(kadm_handle, &ent,
111 KADM5_ATTRIBUTES|KADM5_KVNO);
112 kadm5_free_principal_ent (kadm_handle, &ent);
113 if(ret) {
114 krb5_warn(context, ret, "create_random_entry(%s): "
115 "unable to modify principal", name);
116 goto out;
118 out:
119 free(name);
120 return ret;
123 extern int local_flag;
126 init(struct init_options *opt, int argc, char **argv)
128 kadm5_ret_t ret;
129 int i;
130 HDB *db;
131 krb5_deltat max_life = 0, max_rlife = 0;
133 if (!local_flag) {
134 krb5_warnx(context, "init is only available in local (-l) mode");
135 return 0;
138 if (opt->realm_max_ticket_life_string) {
139 if (str2deltat (opt->realm_max_ticket_life_string, &max_life) != 0) {
140 krb5_warnx (context, "unable to parse \"%s\"",
141 opt->realm_max_ticket_life_string);
142 return 0;
145 if (opt->realm_max_renewable_life_string) {
146 if (str2deltat (opt->realm_max_renewable_life_string, &max_rlife) != 0) {
147 krb5_warnx (context, "unable to parse \"%s\"",
148 opt->realm_max_renewable_life_string);
149 return 0;
153 db = _kadm5_s_get_db(kadm_handle);
155 ret = db->hdb_open(context, db, O_RDWR | O_CREAT, 0600);
156 if(ret){
157 krb5_warn(context, ret, "hdb_open");
158 return 0;
160 ret = kadm5_log_reinit(kadm_handle);
161 if (ret)
162 krb5_err(context, 1, ret, "Failed iprop log initialization");
163 kadm5_log_end(kadm_handle);
164 db->hdb_close(context, db);
165 for(i = 0; i < argc; i++){
166 krb5_principal princ;
167 const char *realm = argv[i];
169 if (opt->realm_max_ticket_life_string == NULL) {
170 max_life = 0;
171 if(edit_deltat ("Realm max ticket life", &max_life, NULL, 0)) {
172 return 0;
175 if (opt->realm_max_renewable_life_string == NULL) {
176 max_rlife = 0;
177 if(edit_deltat("Realm max renewable ticket life", &max_rlife,
178 NULL, 0)) {
179 return 0;
183 /* Create `krbtgt/REALM' */
184 ret = krb5_make_principal(context, &princ, realm,
185 KRB5_TGS_NAME, realm, NULL);
186 if(ret)
187 return 0;
189 create_random_entry(princ, max_life, max_rlife, 0, 0);
190 krb5_free_principal(context, princ);
192 if (opt->bare_flag)
193 continue;
195 /* Create `kadmin/changepw' */
196 krb5_make_principal(context, &princ, realm,
197 "kadmin", "changepw", NULL);
199 * The Windows XP (at least) password changing protocol
200 * request the `kadmin/changepw' ticket with `renewable_ok,
201 * renewable, forwardable' and so fails if we disallow
202 * forwardable here.
204 create_random_entry(princ, 5*60, 5*60,
205 KRB5_KDB_DISALLOW_TGT_BASED|
206 KRB5_KDB_PWCHANGE_SERVICE|
207 KRB5_KDB_DISALLOW_POSTDATED|
208 KRB5_KDB_DISALLOW_RENEWABLE|
209 KRB5_KDB_DISALLOW_PROXIABLE|
210 KRB5_KDB_REQUIRES_PRE_AUTH,
212 krb5_free_principal(context, princ);
214 /* Create `kadmin/admin' */
215 krb5_make_principal(context, &princ, realm,
216 "kadmin", "admin", NULL);
217 create_random_entry(princ, 60*60, 60*60, KRB5_KDB_REQUIRES_PRE_AUTH, 0);
218 krb5_free_principal(context, princ);
220 /* Create `changepw/kerberos' (for v4 compat) */
221 krb5_make_principal(context, &princ, realm,
222 "changepw", "kerberos", NULL);
223 create_random_entry(princ, 60*60, 60*60,
224 KRB5_KDB_DISALLOW_TGT_BASED|
225 KRB5_KDB_PWCHANGE_SERVICE, 0);
227 krb5_free_principal(context, princ);
229 /* Create `kadmin/hprop' for database propagation */
230 krb5_make_principal(context, &princ, realm,
231 "kadmin", "hprop", NULL);
232 create_random_entry(princ, 60*60, 60*60,
233 KRB5_KDB_REQUIRES_PRE_AUTH|
234 KRB5_KDB_DISALLOW_TGT_BASED, 0);
235 krb5_free_principal(context, princ);
237 /* Create `WELLKNOWN/ANONYMOUS' for anonymous as-req */
238 krb5_make_principal(context, &princ, realm,
239 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME, NULL);
240 create_random_entry(princ, 60*60, 60*60,
241 KRB5_KDB_REQUIRES_PRE_AUTH, 0);
242 krb5_free_principal(context, princ);
245 /* Create `WELLKNONW/org.h5l.fast-cookie@WELLKNOWN:ORG.H5L' for FAST cookie */
246 krb5_make_principal(context, &princ, KRB5_WELLKNOWN_ORG_H5L_REALM,
247 KRB5_WELLKNOWN_NAME, "org.h5l.fast-cookie", NULL);
248 create_random_entry(princ, 60*60, 60*60,
249 KRB5_KDB_REQUIRES_PRE_AUTH|
250 KRB5_KDB_DISALLOW_TGT_BASED|
251 KRB5_KDB_DISALLOW_ALL_TIX, CRE_DUP_OK);
252 krb5_free_principal(context, princ);
254 /* Create `default' */
256 kadm5_principal_ent_rec ent;
257 int mask = 0;
259 memset (&ent, 0, sizeof(ent));
260 mask |= KADM5_PRINCIPAL;
261 krb5_make_principal(context, &ent.principal, realm,
262 "default", NULL);
263 mask |= KADM5_MAX_LIFE;
264 ent.max_life = 24 * 60 * 60;
265 mask |= KADM5_MAX_RLIFE;
266 ent.max_renewable_life = 7 * ent.max_life;
267 ent.attributes = KRB5_KDB_DISALLOW_ALL_TIX;
268 mask |= KADM5_ATTRIBUTES;
270 ret = kadm5_create_principal(kadm_handle, &ent, mask, "");
271 if (ret)
272 krb5_err (context, 1, ret, "kadm5_create_principal");
274 krb5_free_principal(context, ent.principal);
277 return 0;