No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kadmin / init.c
blob72a5264083849fae8edea212f8d159ba3d95bf88
1 /*
2 * Copyright (c) 1997-2004 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 "kadmin_locl.h"
35 #include "kadmin-commands.h"
36 #include <kadm5/private.h>
38 __RCSID("$Heimdal: init.c 17447 2006-05-05 10:52:01Z lha $"
39 "$NetBSD$");
41 static kadm5_ret_t
42 create_random_entry(krb5_principal princ,
43 unsigned max_life,
44 unsigned max_rlife,
45 uint32_t attributes)
47 kadm5_principal_ent_rec ent;
48 kadm5_ret_t ret;
49 int mask = 0;
50 krb5_keyblock *keys;
51 int n_keys, i;
52 char *name;
53 const char *password;
54 char pwbuf[512];
56 random_password(pwbuf, sizeof(pwbuf));
57 password = pwbuf;
59 ret = krb5_unparse_name(context, princ, &name);
60 if (ret) {
61 krb5_warn(context, ret, "failed to unparse principal name");
62 return ret;
65 memset(&ent, 0, sizeof(ent));
66 ent.principal = princ;
67 mask |= KADM5_PRINCIPAL;
68 if (max_life) {
69 ent.max_life = max_life;
70 mask |= KADM5_MAX_LIFE;
72 if (max_rlife) {
73 ent.max_renewable_life = max_rlife;
74 mask |= KADM5_MAX_RLIFE;
76 ent.attributes |= attributes | KRB5_KDB_DISALLOW_ALL_TIX;
77 mask |= KADM5_ATTRIBUTES;
79 /* Create the entry with a random password */
80 ret = kadm5_create_principal(kadm_handle, &ent, mask, password);
81 if(ret) {
82 krb5_warn(context, ret, "create_random_entry(%s): randkey failed",
83 name);
84 goto out;
87 /* Replace the string2key based keys with real random bytes */
88 ret = kadm5_randkey_principal(kadm_handle, princ, &keys, &n_keys);
89 if(ret) {
90 krb5_warn(context, ret, "create_random_entry*%s): randkey failed",
91 name);
92 goto out;
94 for(i = 0; i < n_keys; i++)
95 krb5_free_keyblock_contents(context, &keys[i]);
96 free(keys);
97 ret = kadm5_get_principal(kadm_handle, princ, &ent,
98 KADM5_PRINCIPAL | KADM5_ATTRIBUTES);
99 if(ret) {
100 krb5_warn(context, ret, "create_random_entry(%s): "
101 "unable to get principal", name);
102 goto out;
104 ent.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
105 ent.kvno = 1;
106 ret = kadm5_modify_principal(kadm_handle, &ent,
107 KADM5_ATTRIBUTES|KADM5_KVNO);
108 kadm5_free_principal_ent (kadm_handle, &ent);
109 if(ret) {
110 krb5_warn(context, ret, "create_random_entry(%s): "
111 "unable to modify principal", name);
112 goto out;
114 out:
115 free(name);
116 return ret;
119 extern int local_flag;
122 init(struct init_options *opt, int argc, char **argv)
124 kadm5_ret_t ret;
125 int i;
126 HDB *db;
127 krb5_deltat max_life, max_rlife;
129 if(!local_flag) {
130 krb5_warnx(context, "init is only available in local (-l) mode");
131 return 0;
134 if (opt->realm_max_ticket_life_string) {
135 if (str2deltat (opt->realm_max_ticket_life_string, &max_life) != 0) {
136 krb5_warnx (context, "unable to parse \"%s\"",
137 opt->realm_max_ticket_life_string);
138 return 0;
141 if (opt->realm_max_renewable_life_string) {
142 if (str2deltat (opt->realm_max_renewable_life_string, &max_rlife) != 0) {
143 krb5_warnx (context, "unable to parse \"%s\"",
144 opt->realm_max_renewable_life_string);
145 return 0;
149 db = _kadm5_s_get_db(kadm_handle);
151 ret = db->hdb_open(context, db, O_RDWR | O_CREAT, 0600);
152 if(ret){
153 krb5_warn(context, ret, "hdb_open");
154 return 0;
156 db->hdb_close(context, db);
157 for(i = 0; i < argc; i++){
158 krb5_principal princ;
159 const char *realm = argv[i];
161 /* Create `krbtgt/REALM' */
162 ret = krb5_make_principal(context, &princ, realm,
163 KRB5_TGS_NAME, realm, NULL);
164 if(ret)
165 return 0;
166 if (opt->realm_max_ticket_life_string == NULL) {
167 max_life = 0;
168 if(edit_deltat ("Realm max ticket life", &max_life, NULL, 0)) {
169 krb5_free_principal(context, princ);
170 return 0;
173 if (opt->realm_max_renewable_life_string == NULL) {
174 max_rlife = 0;
175 if(edit_deltat("Realm max renewable ticket life", &max_rlife,
176 NULL, 0)) {
177 krb5_free_principal(context, princ);
178 return 0;
181 create_random_entry(princ, max_life, max_rlife, 0);
182 krb5_free_principal(context, princ);
184 /* Create `kadmin/changepw' */
185 krb5_make_principal(context, &princ, realm,
186 "kadmin", "changepw", NULL);
188 * The Windows XP (at least) password changing protocol
189 * request the `kadmin/changepw' ticket with `renewable_ok,
190 * renewable, forwardable' and so fails if we disallow
191 * forwardable here.
193 create_random_entry(princ, 5*60, 5*60,
194 KRB5_KDB_DISALLOW_TGT_BASED|
195 KRB5_KDB_PWCHANGE_SERVICE|
196 KRB5_KDB_DISALLOW_POSTDATED|
197 KRB5_KDB_DISALLOW_RENEWABLE|
198 KRB5_KDB_DISALLOW_PROXIABLE|
199 KRB5_KDB_REQUIRES_PRE_AUTH);
200 krb5_free_principal(context, princ);
202 /* Create `kadmin/admin' */
203 krb5_make_principal(context, &princ, realm,
204 "kadmin", "admin", NULL);
205 create_random_entry(princ, 60*60, 60*60, KRB5_KDB_REQUIRES_PRE_AUTH);
206 krb5_free_principal(context, princ);
208 /* Create `changepw/kerberos' (for v4 compat) */
209 krb5_make_principal(context, &princ, realm,
210 "changepw", "kerberos", NULL);
211 create_random_entry(princ, 60*60, 60*60,
212 KRB5_KDB_DISALLOW_TGT_BASED|
213 KRB5_KDB_PWCHANGE_SERVICE);
215 krb5_free_principal(context, princ);
217 /* Create `kadmin/hprop' for database propagation */
218 krb5_make_principal(context, &princ, realm,
219 "kadmin", "hprop", NULL);
220 create_random_entry(princ, 60*60, 60*60,
221 KRB5_KDB_REQUIRES_PRE_AUTH|
222 KRB5_KDB_DISALLOW_TGT_BASED);
223 krb5_free_principal(context, princ);
225 /* Create `default' */
227 kadm5_principal_ent_rec ent;
228 int mask = 0;
230 memset (&ent, 0, sizeof(ent));
231 mask |= KADM5_PRINCIPAL;
232 krb5_make_principal(context, &ent.principal, realm,
233 "default", NULL);
234 mask |= KADM5_MAX_LIFE;
235 ent.max_life = 24 * 60 * 60;
236 mask |= KADM5_MAX_RLIFE;
237 ent.max_renewable_life = 7 * ent.max_life;
238 ent.attributes = KRB5_KDB_DISALLOW_ALL_TIX;
239 mask |= KADM5_ATTRIBUTES;
241 ret = kadm5_create_principal(kadm_handle, &ent, mask, "");
242 if (ret)
243 krb5_err (context, 1, ret, "kadm5_create_principal");
245 krb5_free_principal(context, ent.principal);
248 return 0;