*** empty log message ***
[heimdal.git] / kadmin / init.c
blob68098eb195e04585eb7728d7c68aad63832e20a5
1 /*
2 * Copyright (c) 1997 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include "kadmin_locl.h"
40 #include <kadm5/private.h>
42 RCSID("$Id$");
44 static kadm5_ret_t
45 create_random_entry(krb5_principal princ, time_t max_life, time_t max_rlife,
46 u_int32_t attributes)
48 kadm5_principal_ent_rec ent;
49 kadm5_ret_t ret;
50 int mask = 0;
51 krb5_keyblock *keys;
52 int n_keys, i;
54 memset(&ent, 0, sizeof(ent));
55 ent.principal = princ;
56 mask |= KADM5_PRINCIPAL;
57 ent.max_life = max_life;
58 mask |= KADM5_MAX_LIFE;
59 ent.max_renewable_life = max_rlife;
60 mask |= KADM5_MAX_RLIFE;
61 ent.attributes |= attributes | KRB5_KDB_DISALLOW_ALL_TIX;
62 mask |= KADM5_ATTRIBUTES;
64 ret = kadm5_create_principal(kadm_handle, &ent, mask, "hemlig");
65 if(ret)
66 return ret;
67 ret = kadm5_randkey_principal(kadm_handle, princ, &keys, &n_keys);
68 if(ret)
69 return ret;
70 for(i = 0; i < n_keys; i++)
71 krb5_free_keyblock_contents(context, &keys[i]);
72 free(keys);
73 ret = kadm5_get_principal(kadm_handle, princ, &ent,
74 KADM5_PRINCIPAL | KADM5_ATTRIBUTES);
75 if(ret)
76 return ret;
77 ent.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
78 ent.kvno = 1;
79 ret = kadm5_modify_principal(kadm_handle, &ent,
80 KADM5_ATTRIBUTES|KADM5_KVNO);
81 if(ret)
82 return ret;
83 return 0;
86 int
87 init(int argc, char **argv)
89 kadm5_ret_t ret;
90 int i;
92 HDB *db = _kadm5_s_get_db(kadm_handle);
94 ret = db->open(context, db, O_RDWR | O_CREAT, 0600);
95 if(ret){
96 krb5_warn(context, ret, "hdb_open");
97 return 0;
99 db->close(context, db);
100 for(i = 1; i < argc; i++){
101 krb5_principal princ;
102 unsigned max_life, max_rlife;
104 /* Create `krbtgt/REALM' */
105 krb5_make_principal(context, &princ, argv[i], "krbtgt", argv[i], NULL);
106 get_deltat("Realm max ticket life",
107 "unlimited",
108 &max_life);
109 if (max_life == 0)
110 max_life = 24 * 60 * 60;
111 get_deltat("Realm max renewable ticket life",
112 "unlimited",
113 &max_rlife);
114 if (max_rlife == 0)
115 max_rlife = 7 * max_life;
116 create_random_entry(princ, max_life, max_rlife, 0);
117 krb5_free_principal(context, princ);
118 /* Create `kadmin/changepw' */
119 krb5_make_principal(context, &princ, argv[i],
120 "kadmin", "changepw", NULL);
121 create_random_entry(princ, 5*60, 5*60,
122 KRB5_KDB_DISALLOW_TGT_BASED|
123 KRB5_KDB_PWCHANGE_SERVICE|
124 KRB5_KDB_DISALLOW_POSTDATED|
125 KRB5_KDB_DISALLOW_FORWARDABLE|
126 KRB5_KDB_DISALLOW_RENEWABLE|
127 KRB5_KDB_DISALLOW_PROXIABLE|
128 KRB5_KDB_REQUIRES_PRE_AUTH);
129 krb5_free_principal(context, princ);
130 /* Create `kadmin/admin' */
131 krb5_make_principal(context, &princ, argv[i],
132 "kadmin", "admin", NULL);
133 create_random_entry(princ, 60*60, 60*60, KRB5_KDB_REQUIRES_PRE_AUTH);
134 krb5_free_principal(context, princ);
135 /* Create `default' */
137 kadm5_principal_ent_rec ent;
138 int mask = 0;
140 memset (&ent, 0, sizeof(ent));
141 mask |= KADM5_PRINCIPAL;
142 krb5_make_principal(context, &ent.principal, argv[i],
143 "default", NULL);
144 mask |= KADM5_MAX_LIFE;
145 ent.max_life = 24 * 60 * 60;
146 mask |= KADM5_MAX_RLIFE;
147 ent.max_renewable_life = 7 * ent.max_life;
148 ent.attributes = KRB5_KDB_DISALLOW_ALL_TIX;
149 mask |= KADM5_ATTRIBUTES;
151 ret = kadm5_create_principal(kadm_handle, &ent, mask, "");
152 if (ret)
153 krb5_err (context, 1, ret, "kadm5_create_principal");
155 krb5_free_principal(context, ent.principal);
158 return 0;