Rename context handle lifetime to endtime
[heimdal.git] / kadmin / ank.c
blobffa5b7439fc0ee5b6d8ddd7cc2d6a21b1468cbf2
1 /*
2 * Copyright (c) 1997-2006 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"
38 * fetch the default principal corresponding to `princ'
41 static krb5_error_code
42 get_default (kadm5_server_context *contextp,
43 krb5_principal princ,
44 kadm5_principal_ent_t default_ent)
46 krb5_error_code ret;
47 krb5_principal def_principal;
48 krb5_const_realm realm = krb5_principal_get_realm(contextp->context, princ);
50 ret = krb5_make_principal (contextp->context, &def_principal,
51 realm, "default", NULL);
52 if (ret)
53 return ret;
54 ret = kadm5_get_principal (contextp, def_principal, default_ent,
55 KADM5_PRINCIPAL_NORMAL_MASK);
56 krb5_free_principal (contextp->context, def_principal);
57 return ret;
61 * Add the principal `name' to the database.
62 * Prompt for all data not given by the input parameters.
65 static krb5_error_code
66 add_one_principal (const char *name,
67 int rand_key,
68 int rand_password,
69 int use_defaults,
70 char *password,
71 char *policy,
72 krb5_key_data *key_data,
73 const char *max_ticket_life,
74 const char *max_renewable_life,
75 const char *attributes,
76 const char *expiration,
77 const char *pw_expiration)
79 krb5_error_code ret;
80 kadm5_principal_ent_rec princ, defrec;
81 kadm5_principal_ent_rec *default_ent = NULL;
82 krb5_principal princ_ent = NULL;
83 int mask = 0;
84 int default_mask = 0;
85 char pwbuf[1024];
87 memset(&princ, 0, sizeof(princ));
88 ret = krb5_parse_name(context, name, &princ_ent);
89 if (ret) {
90 krb5_warn(context, ret, "krb5_parse_name");
91 return ret;
93 princ.principal = princ_ent;
94 mask |= KADM5_PRINCIPAL;
96 ret = set_entry(context, &princ, &mask,
97 max_ticket_life, max_renewable_life,
98 expiration, pw_expiration, attributes, policy);
99 if (ret)
100 goto out;
102 default_ent = &defrec;
103 ret = get_default (kadm_handle, princ_ent, default_ent);
104 if (ret) {
105 default_ent = NULL;
106 default_mask = 0;
107 } else {
108 default_mask = KADM5_ATTRIBUTES | KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
109 KADM5_PRINC_EXPIRE_TIME | KADM5_PW_EXPIRATION;
112 if(use_defaults)
113 set_defaults(&princ, &mask, default_ent, default_mask);
114 else
115 if(edit_entry(&princ, &mask, default_ent, default_mask))
116 goto out;
117 if(rand_key || key_data) {
118 princ.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
119 mask |= KADM5_ATTRIBUTES;
120 random_password (pwbuf, sizeof(pwbuf));
121 password = pwbuf;
122 } else if (rand_password) {
123 random_password (pwbuf, sizeof(pwbuf));
124 password = pwbuf;
125 } else if(password == NULL) {
126 char *princ_name;
127 char *prompt;
128 int aret;
130 ret = krb5_unparse_name(context, princ_ent, &princ_name);
131 if (ret)
132 goto out;
133 aret = asprintf (&prompt, "%s's Password: ", princ_name);
134 free (princ_name);
135 if (aret == -1) {
136 ret = ENOMEM;
137 krb5_set_error_message(context, ret, "out of memory");
138 goto out;
140 ret = UI_UTIL_read_pw_string (pwbuf, sizeof(pwbuf), prompt, 1);
141 free (prompt);
142 if (ret) {
143 ret = KRB5_LIBOS_BADPWDMATCH;
144 krb5_set_error_message(context, ret, "failed to verify password");
145 goto out;
147 password = pwbuf;
150 ret = kadm5_create_principal(kadm_handle, &princ, mask, password);
151 if(ret) {
152 krb5_warn(context, ret, "kadm5_create_principal");
153 goto out;
155 if(rand_key) {
156 krb5_keyblock *new_keys;
157 int n_keys, i;
158 ret = kadm5_randkey_principal(kadm_handle, princ_ent,
159 &new_keys, &n_keys);
160 if(ret){
161 krb5_warn(context, ret, "kadm5_randkey_principal");
162 n_keys = 0;
164 for(i = 0; i < n_keys; i++)
165 krb5_free_keyblock_contents(context, &new_keys[i]);
166 if (n_keys > 0)
167 free(new_keys);
168 kadm5_get_principal(kadm_handle, princ_ent, &princ,
169 KADM5_PRINCIPAL | KADM5_KVNO | KADM5_ATTRIBUTES);
170 krb5_free_principal(context, princ_ent);
171 princ_ent = princ.principal;
172 princ.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
174 * Updating kvno w/o key data and vice-versa gives _kadm5_setup_entry()
175 * and _kadm5_set_keys2() headaches. But we used to, so we handle
176 * this in in those two functions. Might as well leave this code as
177 * it was then.
179 princ.kvno = 1;
180 kadm5_modify_principal(kadm_handle, &princ,
181 KADM5_ATTRIBUTES | KADM5_KVNO);
182 } else if (key_data) {
183 ret = kadm5_chpass_principal_with_key (kadm_handle, princ_ent,
184 3, key_data);
185 if (ret) {
186 krb5_warn(context, ret, "kadm5_chpass_principal_with_key");
188 kadm5_get_principal(kadm_handle, princ_ent, &princ,
189 KADM5_PRINCIPAL | KADM5_ATTRIBUTES);
190 krb5_free_principal(context, princ_ent);
191 princ_ent = princ.principal;
192 princ.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
193 kadm5_modify_principal(kadm_handle, &princ, KADM5_ATTRIBUTES);
194 } else if (rand_password) {
195 char *princ_name;
197 krb5_unparse_name(context, princ_ent, &princ_name);
198 printf ("added %s with password \"%s\"\n", princ_name, password);
199 free (princ_name);
201 out:
202 kadm5_free_principal_ent(kadm_handle, &princ); /* frees princ_ent */
203 if(default_ent)
204 kadm5_free_principal_ent (kadm_handle, default_ent);
205 if (password != NULL)
206 memset (password, 0, strlen(password));
207 return ret;
211 * parse the string `key_string' into `key', returning 0 iff succesful.
215 * the ank command
219 * Parse arguments and add all the principals.
223 add_new_key(struct add_options *opt, int argc, char **argv)
225 krb5_error_code ret = 0;
226 int i;
227 int num;
228 krb5_key_data key_data[3];
229 krb5_key_data *kdp = NULL;
231 num = 0;
232 if (opt->random_key_flag)
233 ++num;
234 if (opt->random_password_flag)
235 ++num;
236 if (opt->password_string)
237 ++num;
238 if (opt->key_string)
239 ++num;
241 if (num > 1) {
242 fprintf (stderr, "give only one of "
243 "--random-key, --random-password, --password, --key\n");
244 return 1;
247 if (opt->key_string) {
248 const char *error;
250 if (parse_des_key (opt->key_string, key_data, &error)) {
251 fprintf (stderr, "failed parsing key \"%s\": %s\n",
252 opt->key_string, error);
253 return 1;
255 kdp = key_data;
258 for(i = 0; i < argc; i++) {
259 ret = add_one_principal (argv[i],
260 opt->random_key_flag,
261 opt->random_password_flag,
262 opt->use_defaults_flag,
263 opt->password_string,
264 opt->policy_string,
265 kdp,
266 opt->max_ticket_life_string,
267 opt->max_renewable_life_string,
268 opt->attributes_string,
269 opt->expiration_time_string,
270 opt->pw_expiration_time_string);
271 if (ret) {
272 krb5_warn (context, ret, "adding %s", argv[i]);
273 break;
276 if (kdp) {
277 int16_t dummy = 3;
278 kadm5_free_key_data (kadm_handle, &dummy, key_data);
280 return ret != 0;