xref: /freebsd/crypto/heimdal/kadmin/init.c (revision c19800e8cd5640693f36f2040db4ab5e8d738146)
1b528cefcSMark Murray /*
2c19800e8SDoug Rabson  * Copyright (c) 1997-2004 Kungliga Tekniska H�gskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "kadmin_locl.h"
35c19800e8SDoug Rabson #include "kadmin-commands.h"
36b528cefcSMark Murray #include <kadm5/private.h>
37b528cefcSMark Murray 
38c19800e8SDoug Rabson RCSID("$Id: init.c 17447 2006-05-05 10:52:01Z lha $");
39b528cefcSMark Murray 
40b528cefcSMark Murray static kadm5_ret_t
41b528cefcSMark Murray create_random_entry(krb5_principal princ,
42b528cefcSMark Murray 		    unsigned max_life,
43b528cefcSMark Murray 		    unsigned max_rlife,
44c19800e8SDoug Rabson 		    uint32_t attributes)
45b528cefcSMark Murray {
46b528cefcSMark Murray     kadm5_principal_ent_rec ent;
47b528cefcSMark Murray     kadm5_ret_t ret;
48b528cefcSMark Murray     int mask = 0;
49b528cefcSMark Murray     krb5_keyblock *keys;
50b528cefcSMark Murray     int n_keys, i;
51c19800e8SDoug Rabson     char *name;
52c19800e8SDoug Rabson     const char *password;
53c19800e8SDoug Rabson     char pwbuf[512];
54c19800e8SDoug Rabson 
55c19800e8SDoug Rabson     random_password(pwbuf, sizeof(pwbuf));
56c19800e8SDoug Rabson     password = pwbuf;
57c19800e8SDoug Rabson 
58c19800e8SDoug Rabson     ret = krb5_unparse_name(context, princ, &name);
59c19800e8SDoug Rabson     if (ret) {
60c19800e8SDoug Rabson 	krb5_warn(context, ret, "failed to unparse principal name");
61c19800e8SDoug Rabson 	return ret;
62c19800e8SDoug Rabson     }
63b528cefcSMark Murray 
64b528cefcSMark Murray     memset(&ent, 0, sizeof(ent));
65b528cefcSMark Murray     ent.principal = princ;
66b528cefcSMark Murray     mask |= KADM5_PRINCIPAL;
67b528cefcSMark Murray     if (max_life) {
68b528cefcSMark Murray 	ent.max_life = max_life;
69b528cefcSMark Murray 	mask |= KADM5_MAX_LIFE;
70b528cefcSMark Murray     }
71b528cefcSMark Murray     if (max_rlife) {
72b528cefcSMark Murray 	ent.max_renewable_life = max_rlife;
73b528cefcSMark Murray 	mask |= KADM5_MAX_RLIFE;
74b528cefcSMark Murray     }
75b528cefcSMark Murray     ent.attributes |= attributes | KRB5_KDB_DISALLOW_ALL_TIX;
76b528cefcSMark Murray     mask |= KADM5_ATTRIBUTES;
77b528cefcSMark Murray 
78c19800e8SDoug Rabson     /* Create the entry with a random password */
79c19800e8SDoug Rabson     ret = kadm5_create_principal(kadm_handle, &ent, mask, password);
80c19800e8SDoug Rabson     if(ret) {
81c19800e8SDoug Rabson 	krb5_warn(context, ret, "create_random_entry(%s): randkey failed",
82c19800e8SDoug Rabson 		  name);
83c19800e8SDoug Rabson 	goto out;
84c19800e8SDoug Rabson     }
85c19800e8SDoug Rabson 
86c19800e8SDoug Rabson     /* Replace the string2key based keys with real random bytes */
87b528cefcSMark Murray     ret = kadm5_randkey_principal(kadm_handle, princ, &keys, &n_keys);
88c19800e8SDoug Rabson     if(ret) {
89c19800e8SDoug Rabson 	krb5_warn(context, ret, "create_random_entry*%s): randkey failed",
90c19800e8SDoug Rabson 		  name);
91c19800e8SDoug Rabson 	goto out;
92c19800e8SDoug Rabson     }
93b528cefcSMark Murray     for(i = 0; i < n_keys; i++)
94b528cefcSMark Murray 	krb5_free_keyblock_contents(context, &keys[i]);
95b528cefcSMark Murray     free(keys);
96b528cefcSMark Murray     ret = kadm5_get_principal(kadm_handle, princ, &ent,
97b528cefcSMark Murray 			      KADM5_PRINCIPAL | KADM5_ATTRIBUTES);
98c19800e8SDoug Rabson     if(ret) {
99c19800e8SDoug Rabson 	krb5_warn(context, ret, "create_random_entry(%s): "
100c19800e8SDoug Rabson 		  "unable to get principal", name);
101c19800e8SDoug Rabson 	goto out;
102c19800e8SDoug Rabson     }
103b528cefcSMark Murray     ent.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
104b528cefcSMark Murray     ent.kvno = 1;
105b528cefcSMark Murray     ret = kadm5_modify_principal(kadm_handle, &ent,
106b528cefcSMark Murray 				 KADM5_ATTRIBUTES|KADM5_KVNO);
107b528cefcSMark Murray     kadm5_free_principal_ent (kadm_handle, &ent);
108c19800e8SDoug Rabson     if(ret) {
109c19800e8SDoug Rabson 	krb5_warn(context, ret, "create_random_entry(%s): "
110c19800e8SDoug Rabson 		  "unable to modify principal", name);
111c19800e8SDoug Rabson 	goto out;
112c19800e8SDoug Rabson     }
113c19800e8SDoug Rabson  out:
114c19800e8SDoug Rabson     free(name);
115b528cefcSMark Murray     return ret;
116b528cefcSMark Murray }
117b528cefcSMark Murray 
118c19800e8SDoug Rabson extern int local_flag;
119b528cefcSMark Murray 
120b528cefcSMark Murray int
121c19800e8SDoug Rabson init(struct init_options *opt, int argc, char **argv)
122b528cefcSMark Murray {
123b528cefcSMark Murray     kadm5_ret_t ret;
124b528cefcSMark Murray     int i;
125b528cefcSMark Murray     HDB *db;
126b528cefcSMark Murray     krb5_deltat max_life, max_rlife;
127b528cefcSMark Murray 
128c19800e8SDoug Rabson     if(!local_flag) {
129c19800e8SDoug Rabson 	krb5_warnx(context, "init is only available in local (-l) mode");
130b528cefcSMark Murray 	return 0;
131b528cefcSMark Murray     }
132b528cefcSMark Murray 
133c19800e8SDoug Rabson     if (opt->realm_max_ticket_life_string) {
134c19800e8SDoug Rabson 	if (str2deltat (opt->realm_max_ticket_life_string, &max_life) != 0) {
135c19800e8SDoug Rabson 	    krb5_warnx (context, "unable to parse \"%s\"",
136c19800e8SDoug Rabson 			opt->realm_max_ticket_life_string);
137b528cefcSMark Murray 	    return 0;
138b528cefcSMark Murray 	}
139b528cefcSMark Murray     }
140c19800e8SDoug Rabson     if (opt->realm_max_renewable_life_string) {
141c19800e8SDoug Rabson 	if (str2deltat (opt->realm_max_renewable_life_string, &max_rlife) != 0) {
142c19800e8SDoug Rabson 	    krb5_warnx (context, "unable to parse \"%s\"",
143c19800e8SDoug Rabson 			opt->realm_max_renewable_life_string);
144b528cefcSMark Murray 	    return 0;
145b528cefcSMark Murray 	}
146b528cefcSMark Murray     }
147b528cefcSMark Murray 
148b528cefcSMark Murray     db = _kadm5_s_get_db(kadm_handle);
149b528cefcSMark Murray 
150c19800e8SDoug Rabson     ret = db->hdb_open(context, db, O_RDWR | O_CREAT, 0600);
151b528cefcSMark Murray     if(ret){
152b528cefcSMark Murray 	krb5_warn(context, ret, "hdb_open");
153b528cefcSMark Murray 	return 0;
154b528cefcSMark Murray     }
155c19800e8SDoug Rabson     db->hdb_close(context, db);
156c19800e8SDoug Rabson     for(i = 0; i < argc; i++){
157b528cefcSMark Murray 	krb5_principal princ;
158b528cefcSMark Murray 	const char *realm = argv[i];
159b528cefcSMark Murray 
160b528cefcSMark Murray 	/* Create `krbtgt/REALM' */
161bbd80c28SJacques Vidrine 	ret = krb5_make_principal(context, &princ, realm,
1625e9cd1aeSAssar Westerlund 				  KRB5_TGS_NAME, realm, NULL);
163bbd80c28SJacques Vidrine 	if(ret)
164bbd80c28SJacques Vidrine 	    return 0;
165c19800e8SDoug Rabson 	if (opt->realm_max_ticket_life_string == NULL) {
166b528cefcSMark Murray 	    max_life = 0;
167bbd80c28SJacques Vidrine 	    if(edit_deltat ("Realm max ticket life", &max_life, NULL, 0)) {
168bbd80c28SJacques Vidrine 		krb5_free_principal(context, princ);
169bbd80c28SJacques Vidrine 		return 0;
170bbd80c28SJacques Vidrine 	    }
171b528cefcSMark Murray 	}
172c19800e8SDoug Rabson 	if (opt->realm_max_renewable_life_string == NULL) {
173b528cefcSMark Murray 	    max_rlife = 0;
174bbd80c28SJacques Vidrine 	    if(edit_deltat("Realm max renewable ticket life", &max_rlife,
175bbd80c28SJacques Vidrine 			   NULL, 0)) {
176bbd80c28SJacques Vidrine 		krb5_free_principal(context, princ);
177bbd80c28SJacques Vidrine 		return 0;
178bbd80c28SJacques Vidrine 	    }
179b528cefcSMark Murray 	}
180b528cefcSMark Murray 	create_random_entry(princ, max_life, max_rlife, 0);
181b528cefcSMark Murray 	krb5_free_principal(context, princ);
182b528cefcSMark Murray 
183b528cefcSMark Murray 	/* Create `kadmin/changepw' */
184b528cefcSMark Murray 	krb5_make_principal(context, &princ, realm,
185b528cefcSMark Murray 			    "kadmin", "changepw", NULL);
186c19800e8SDoug Rabson 	/*
187c19800e8SDoug Rabson 	 * The Windows XP (at least) password changing protocol
188c19800e8SDoug Rabson 	 * request the `kadmin/changepw' ticket with `renewable_ok,
189c19800e8SDoug Rabson 	 * renewable, forwardable' and so fails if we disallow
190c19800e8SDoug Rabson 	 * forwardable here.
191c19800e8SDoug Rabson 	 */
192b528cefcSMark Murray 	create_random_entry(princ, 5*60, 5*60,
193b528cefcSMark Murray 			    KRB5_KDB_DISALLOW_TGT_BASED|
194b528cefcSMark Murray 			    KRB5_KDB_PWCHANGE_SERVICE|
195b528cefcSMark Murray 			    KRB5_KDB_DISALLOW_POSTDATED|
196b528cefcSMark Murray 			    KRB5_KDB_DISALLOW_RENEWABLE|
197b528cefcSMark Murray 			    KRB5_KDB_DISALLOW_PROXIABLE|
198b528cefcSMark Murray 			    KRB5_KDB_REQUIRES_PRE_AUTH);
199b528cefcSMark Murray 	krb5_free_principal(context, princ);
200b528cefcSMark Murray 
201b528cefcSMark Murray 	/* Create `kadmin/admin' */
202b528cefcSMark Murray 	krb5_make_principal(context, &princ, realm,
203b528cefcSMark Murray 			    "kadmin", "admin", NULL);
204b528cefcSMark Murray 	create_random_entry(princ, 60*60, 60*60, KRB5_KDB_REQUIRES_PRE_AUTH);
205b528cefcSMark Murray 	krb5_free_principal(context, princ);
206b528cefcSMark Murray 
207b528cefcSMark Murray 	/* Create `changepw/kerberos' (for v4 compat) */
208b528cefcSMark Murray 	krb5_make_principal(context, &princ, realm,
209b528cefcSMark Murray 			    "changepw", "kerberos", NULL);
2105e9cd1aeSAssar Westerlund 	create_random_entry(princ, 60*60, 60*60,
2115e9cd1aeSAssar Westerlund 			    KRB5_KDB_DISALLOW_TGT_BASED|
2125e9cd1aeSAssar Westerlund 			    KRB5_KDB_PWCHANGE_SERVICE);
2135e9cd1aeSAssar Westerlund 
2145e9cd1aeSAssar Westerlund 	krb5_free_principal(context, princ);
2155e9cd1aeSAssar Westerlund 
2165e9cd1aeSAssar Westerlund 	/* Create `kadmin/hprop' for database propagation */
2175e9cd1aeSAssar Westerlund 	krb5_make_principal(context, &princ, realm,
2185e9cd1aeSAssar Westerlund 			    "kadmin", "hprop", NULL);
2195e9cd1aeSAssar Westerlund 	create_random_entry(princ, 60*60, 60*60,
2205e9cd1aeSAssar Westerlund 			    KRB5_KDB_REQUIRES_PRE_AUTH|
2215e9cd1aeSAssar Westerlund 			    KRB5_KDB_DISALLOW_TGT_BASED);
222b528cefcSMark Murray 	krb5_free_principal(context, princ);
223b528cefcSMark Murray 
224b528cefcSMark Murray 	/* Create `default' */
225b528cefcSMark Murray 	{
226b528cefcSMark Murray 	    kadm5_principal_ent_rec ent;
227b528cefcSMark Murray 	    int mask = 0;
228b528cefcSMark Murray 
229b528cefcSMark Murray 	    memset (&ent, 0, sizeof(ent));
230b528cefcSMark Murray 	    mask |= KADM5_PRINCIPAL;
231b528cefcSMark Murray 	    krb5_make_principal(context, &ent.principal, realm,
232b528cefcSMark Murray 				"default", NULL);
233b528cefcSMark Murray 	    mask |= KADM5_MAX_LIFE;
234b528cefcSMark Murray 	    ent.max_life = 24 * 60 * 60;
235b528cefcSMark Murray 	    mask |= KADM5_MAX_RLIFE;
236b528cefcSMark Murray 	    ent.max_renewable_life = 7 * ent.max_life;
237b528cefcSMark Murray 	    ent.attributes = KRB5_KDB_DISALLOW_ALL_TIX;
238b528cefcSMark Murray 	    mask |= KADM5_ATTRIBUTES;
239b528cefcSMark Murray 
240b528cefcSMark Murray 	    ret = kadm5_create_principal(kadm_handle, &ent, mask, "");
241b528cefcSMark Murray 	    if (ret)
242b528cefcSMark Murray 		krb5_err (context, 1, ret, "kadm5_create_principal");
243b528cefcSMark Murray 
244b528cefcSMark Murray 	    krb5_free_principal(context, ent.principal);
245b528cefcSMark Murray 	}
246b528cefcSMark Murray     }
247b528cefcSMark Murray     return 0;
248b528cefcSMark Murray }
249