2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
36 #include "kadmin_locl.h"
37 #include "kadmin-commands.h"
38 #include <kadm5/private.h>
41 create_random_entry(krb5_principal princ
,
46 kadm5_principal_ent_rec ent
;
55 random_password(pwbuf
, sizeof(pwbuf
));
58 ret
= krb5_unparse_name(context
, princ
, &name
);
60 krb5_warn(context
, ret
, "failed to unparse principal name");
64 memset(&ent
, 0, sizeof(ent
));
65 ent
.principal
= princ
;
66 mask
|= KADM5_PRINCIPAL
;
68 ent
.max_life
= max_life
;
69 mask
|= KADM5_MAX_LIFE
;
72 ent
.max_renewable_life
= max_rlife
;
73 mask
|= KADM5_MAX_RLIFE
;
75 ent
.attributes
|= attributes
| KRB5_KDB_DISALLOW_ALL_TIX
;
76 mask
|= KADM5_ATTRIBUTES
;
78 /* Create the entry with a random password */
79 ret
= kadm5_create_principal(kadm_handle
, &ent
, mask
, password
);
81 krb5_warn(context
, ret
, "create_random_entry(%s): randkey failed",
86 /* Replace the string2key based keys with real random bytes */
87 ret
= kadm5_randkey_principal(kadm_handle
, princ
, &keys
, &n_keys
);
89 krb5_warn(context
, ret
, "create_random_entry(%s): randkey failed",
93 for(i
= 0; i
< n_keys
; i
++)
94 krb5_free_keyblock_contents(context
, &keys
[i
]);
96 ret
= kadm5_get_principal(kadm_handle
, princ
, &ent
,
97 KADM5_PRINCIPAL
| KADM5_ATTRIBUTES
);
99 krb5_warn(context
, ret
, "create_random_entry(%s): "
100 "unable to get principal", name
);
103 ent
.attributes
&= (~KRB5_KDB_DISALLOW_ALL_TIX
);
105 ret
= kadm5_modify_principal(kadm_handle
, &ent
,
106 KADM5_ATTRIBUTES
|KADM5_KVNO
);
107 kadm5_free_principal_ent (kadm_handle
, &ent
);
109 krb5_warn(context
, ret
, "create_random_entry(%s): "
110 "unable to modify principal", name
);
118 extern int local_flag
;
121 init(struct init_options
*opt
, int argc
, char **argv
)
126 krb5_deltat max_life
= 0, max_rlife
= 0;
129 krb5_warnx(context
, "init is only available in local (-l) mode");
133 if (opt
->realm_max_ticket_life_string
) {
134 if (str2deltat (opt
->realm_max_ticket_life_string
, &max_life
) != 0) {
135 krb5_warnx (context
, "unable to parse \"%s\"",
136 opt
->realm_max_ticket_life_string
);
140 if (opt
->realm_max_renewable_life_string
) {
141 if (str2deltat (opt
->realm_max_renewable_life_string
, &max_rlife
) != 0) {
142 krb5_warnx (context
, "unable to parse \"%s\"",
143 opt
->realm_max_renewable_life_string
);
148 db
= _kadm5_s_get_db(kadm_handle
);
150 ret
= db
->hdb_open(context
, db
, O_RDWR
| O_CREAT
, 0600);
152 krb5_warn(context
, ret
, "hdb_open");
155 db
->hdb_close(context
, db
);
156 for(i
= 0; i
< argc
; i
++){
157 krb5_principal princ
;
158 const char *realm
= argv
[i
];
160 if (opt
->realm_max_ticket_life_string
== NULL
) {
162 if(edit_deltat ("Realm max ticket life", &max_life
, NULL
, 0)) {
166 if (opt
->realm_max_renewable_life_string
== NULL
) {
168 if(edit_deltat("Realm max renewable ticket life", &max_rlife
,
174 /* Create `krbtgt/REALM' */
175 ret
= krb5_make_principal(context
, &princ
, realm
,
176 KRB5_TGS_NAME
, realm
, NULL
);
180 create_random_entry(princ
, max_life
, max_rlife
, 0);
181 krb5_free_principal(context
, princ
);
186 /* Create `kadmin/changepw' */
187 krb5_make_principal(context
, &princ
, realm
,
188 "kadmin", "changepw", NULL
);
190 * The Windows XP (at least) password changing protocol
191 * request the `kadmin/changepw' ticket with `renewable_ok,
192 * renewable, forwardable' and so fails if we disallow
195 create_random_entry(princ
, 5*60, 5*60,
196 KRB5_KDB_DISALLOW_TGT_BASED
|
197 KRB5_KDB_PWCHANGE_SERVICE
|
198 KRB5_KDB_DISALLOW_POSTDATED
|
199 KRB5_KDB_DISALLOW_RENEWABLE
|
200 KRB5_KDB_DISALLOW_PROXIABLE
|
201 KRB5_KDB_REQUIRES_PRE_AUTH
);
202 krb5_free_principal(context
, princ
);
204 /* Create `kadmin/admin' */
205 krb5_make_principal(context
, &princ
, realm
,
206 "kadmin", "admin", NULL
);
207 create_random_entry(princ
, 60*60, 60*60, KRB5_KDB_REQUIRES_PRE_AUTH
);
208 krb5_free_principal(context
, princ
);
210 /* Create `changepw/kerberos' (for v4 compat) */
211 krb5_make_principal(context
, &princ
, realm
,
212 "changepw", "kerberos", NULL
);
213 create_random_entry(princ
, 60*60, 60*60,
214 KRB5_KDB_DISALLOW_TGT_BASED
|
215 KRB5_KDB_PWCHANGE_SERVICE
);
217 krb5_free_principal(context
, princ
);
219 /* Create `kadmin/hprop' for database propagation */
220 krb5_make_principal(context
, &princ
, realm
,
221 "kadmin", "hprop", NULL
);
222 create_random_entry(princ
, 60*60, 60*60,
223 KRB5_KDB_REQUIRES_PRE_AUTH
|
224 KRB5_KDB_DISALLOW_TGT_BASED
);
225 krb5_free_principal(context
, princ
);
227 /* Create `WELLKNOWN/ANONYMOUS' for anonymous as-req */
228 krb5_make_principal(context
, &princ
, realm
,
229 KRB5_WELLKNOWN_NAME
, KRB5_ANON_NAME
, NULL
);
230 create_random_entry(princ
, 60*60, 60*60,
231 KRB5_KDB_REQUIRES_PRE_AUTH
);
232 krb5_free_principal(context
, princ
);
235 /* Create `default' */
237 kadm5_principal_ent_rec ent
;
240 memset (&ent
, 0, sizeof(ent
));
241 mask
|= KADM5_PRINCIPAL
;
242 krb5_make_principal(context
, &ent
.principal
, realm
,
244 mask
|= KADM5_MAX_LIFE
;
245 ent
.max_life
= 24 * 60 * 60;
246 mask
|= KADM5_MAX_RLIFE
;
247 ent
.max_renewable_life
= 7 * ent
.max_life
;
248 ent
.attributes
= KRB5_KDB_DISALLOW_ALL_TIX
;
249 mask
|= KADM5_ATTRIBUTES
;
251 ret
= kadm5_create_principal(kadm_handle
, &ent
, mask
, "");
253 krb5_err (context
, 1, ret
, "kadm5_create_principal");
255 krb5_free_principal(context
, ent
.principal
);