xref: /freebsd/crypto/heimdal/kadmin/ank.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * Copyright (c) 1997-2000 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "kadmin_locl.h"
35 
36 RCSID("$Id: ank.c,v 1.21 2000/09/10 19:16:39 joda Exp $");
37 
38 /*
39  * fetch the default principal corresponding to `princ'
40  */
41 
42 static krb5_error_code
43 get_default (kadm5_server_context *context,
44 	     krb5_principal princ,
45 	     kadm5_principal_ent_t default_ent)
46 {
47     krb5_error_code ret;
48     krb5_principal def_principal;
49     krb5_realm *realm = krb5_princ_realm(context->context, princ);
50 
51     ret = krb5_make_principal (context->context, &def_principal,
52 			       *realm, "default", NULL);
53     if (ret)
54 	return ret;
55     ret = kadm5_get_principal (context, def_principal, default_ent,
56 			       KADM5_PRINCIPAL_NORMAL_MASK);
57     krb5_free_principal (context->context, def_principal);
58     return ret;
59 }
60 
61 /*
62  * Add the principal `name' to the database.
63  * Prompt for all data not given by the input parameters.
64  */
65 
66 static krb5_error_code
67 add_one_principal (const char *name,
68 		   int rand_key,
69 		   int rand_password,
70 		   char *password,
71 		   krb5_key_data *key_data,
72 		   const char *max_ticket_life,
73 		   const char *max_renewable_life,
74 		   const char *attributes,
75 		   const char *expiration,
76 		   const char *pw_expiration)
77 {
78     krb5_error_code ret;
79     kadm5_principal_ent_rec princ, defrec;
80     kadm5_principal_ent_rec *default_ent = NULL;
81     krb5_principal princ_ent = NULL;
82     int mask = 0;
83     int default_mask = 0;
84     char pwbuf[1024];
85 
86     memset(&princ, 0, sizeof(princ));
87     ret = krb5_parse_name(context, name, &princ_ent);
88     if (ret) {
89 	krb5_warn(context, ret, "krb5_parse_name");
90 	return ret;
91     }
92     princ.principal = princ_ent;
93     mask |= KADM5_PRINCIPAL;
94 
95     ret = set_entry(context, &princ, &mask,
96 		    max_ticket_life, max_renewable_life,
97 		    expiration, pw_expiration, attributes);
98     if (ret)
99 	goto out;
100 
101     default_ent = &defrec;
102     ret = get_default (kadm_handle, princ_ent, default_ent);
103     if (ret) {
104 	default_ent  = NULL;
105 	default_mask = 0;
106     } else {
107 	default_mask = KADM5_ATTRIBUTES | KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
108 	    KADM5_PRINC_EXPIRE_TIME | KADM5_PW_EXPIRATION;
109     }
110 
111     edit_entry(&princ, &mask, default_ent, default_mask);
112     if(rand_key || key_data) {
113 	princ.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
114 	mask |= KADM5_ATTRIBUTES;
115 	strlcpy (pwbuf, "hemlig", sizeof(pwbuf));
116 	password = pwbuf;
117     } else if (rand_password) {
118 	random_password (pwbuf, sizeof(pwbuf));
119 	password = pwbuf;
120     } else if(password == NULL) {
121 	char *princ_name;
122 	char *prompt;
123 
124 	krb5_unparse_name(context, princ_ent, &princ_name);
125 	asprintf (&prompt, "%s's Password: ", princ_name);
126 	free (princ_name);
127 	ret = des_read_pw_string (pwbuf, sizeof(pwbuf), prompt, 1);
128 	free (prompt);
129 	if (ret)
130 	    goto out;
131 	password = pwbuf;
132     }
133 
134     ret = kadm5_create_principal(kadm_handle, &princ, mask, password);
135     if(ret)
136 	krb5_warn(context, ret, "kadm5_create_principal");
137     if(rand_key) {
138 	krb5_keyblock *new_keys;
139 	int n_keys, i;
140 	ret = kadm5_randkey_principal(kadm_handle, princ_ent,
141 				      &new_keys, &n_keys);
142 	if(ret){
143 	    krb5_warn(context, ret, "kadm5_randkey_principal");
144 	    n_keys = 0;
145 	}
146 	for(i = 0; i < n_keys; i++)
147 	    krb5_free_keyblock_contents(context, &new_keys[i]);
148 	free(new_keys);
149 	kadm5_get_principal(kadm_handle, princ_ent, &princ,
150 			    KADM5_PRINCIPAL | KADM5_KVNO | KADM5_ATTRIBUTES);
151 	princ.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
152 	princ.kvno = 1;
153 	kadm5_modify_principal(kadm_handle, &princ,
154 			       KADM5_ATTRIBUTES | KADM5_KVNO);
155 	kadm5_free_principal_ent(kadm_handle, &princ);
156     } else if (key_data) {
157 	ret = kadm5_chpass_principal_with_key (kadm_handle, princ_ent,
158 					       3, key_data);
159 	if (ret) {
160 	    krb5_warn(context, ret, "kadm5_chpass_principal_with_key");
161 	}
162 	kadm5_get_principal(kadm_handle, princ_ent, &princ,
163 			    KADM5_PRINCIPAL | KADM5_ATTRIBUTES);
164 	princ.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
165 	kadm5_modify_principal(kadm_handle, &princ, KADM5_ATTRIBUTES);
166 	kadm5_free_principal_ent(kadm_handle, &princ);
167     } else if (rand_password) {
168 	char *princ_name;
169 
170 	krb5_unparse_name(context, princ_ent, &princ_name);
171 	printf ("added %s with password `%s'\n", princ_name, password);
172 	free (princ_name);
173     }
174 out:
175     if (princ_ent)
176 	krb5_free_principal (context, princ_ent);
177     if(default_ent)
178 	kadm5_free_principal_ent (context, default_ent);
179     if (password != NULL)
180 	memset (password, 0, strlen(password));
181     return ret;
182 }
183 
184 /*
185  * parse the string `key_string' into `key', returning 0 iff succesful.
186  */
187 
188 /*
189  * the ank command
190  */
191 
192 static struct getargs args[] = {
193     { "random-key",	'r',	arg_flag,	NULL, "set random key" },
194     { "random-password", 0,	arg_flag,	NULL, "set random password" },
195     { "password",	'p',	arg_string,	NULL, "princial's password" },
196     { "key",		0,	arg_string,	NULL, "DES-key in hex" },
197     { "max-ticket-life",  0,	arg_string,	NULL, "max ticket lifetime",
198       "lifetime"},
199     { "max-renewable-life",  0,	arg_string,	NULL,
200       "max renewable lifetime", "lifetime" },
201     { "attributes",	0,	arg_string,	NULL, "principal attributes",
202       "attributes"},
203     { "expiration-time",0,	arg_string,	NULL, "Expiration time",
204       "time"},
205     { "pw-expiration-time", 0,  arg_string,	NULL,
206       "Password expiration time", "time"}
207 };
208 
209 static int num_args = sizeof(args) / sizeof(args[0]);
210 
211 static void
212 usage(void)
213 {
214     arg_printusage (args, num_args, "add", "principal...");
215 }
216 
217 /*
218  * Parse arguments and add all the principals.
219  */
220 
221 int
222 add_new_key(int argc, char **argv)
223 {
224     char *password = NULL;
225     char *key = NULL;
226     int random_key = 0;
227     int random_password = 0;
228     int optind = 0;
229     krb5_error_code ret;
230     char *max_ticket_life	= NULL;
231     char *max_renewable_life	= NULL;
232     char *attributes		= NULL;
233     char *expiration		= NULL;
234     char *pw_expiration		= NULL;
235     int i;
236     int num;
237     krb5_key_data key_data[3];
238     krb5_key_data *kdp = NULL;
239 
240     args[0].value = &random_key;
241     args[1].value = &random_password;
242     args[2].value = &password;
243     args[3].value = &key;
244     args[4].value = &max_ticket_life;
245     args[5].value = &max_renewable_life;
246     args[6].value = &attributes;
247     args[7].value = &expiration;
248     args[8].value = &pw_expiration;
249 
250     if(getarg(args, num_args, argc, argv, &optind)) {
251 	usage ();
252 	return 0;
253     }
254     if(optind == argc) {
255 	usage ();
256 	return 0;
257     }
258 
259     num = 0;
260     if (random_key)
261 	++num;
262     if (random_password)
263 	++num;
264     if (password)
265 	++num;
266     if (key)
267 	++num;
268 
269     if (num > 1) {
270 	printf ("give only one of "
271 		"--random-key, --random-password, --password, --key\n");
272 	return 0;
273     }
274 
275     if (key) {
276 	const char *error;
277 
278 	if (parse_des_key (key, key_data, &error)) {
279 	    printf ("failed parsing key `%s': %s\n", key, error);
280 	    return 0;
281 	}
282 	kdp = key_data;
283     }
284 
285     for (i = optind; i < argc; ++i) {
286 	ret = add_one_principal (argv[i], random_key, random_password,
287 				 password,
288 				 kdp,
289 				 max_ticket_life,
290 				 max_renewable_life,
291 				 attributes,
292 				 expiration,
293 				 pw_expiration);
294 	if (ret) {
295 	    krb5_warn (context, ret, "adding %s", argv[i]);
296 	    break;
297 	}
298     }
299     if (kdp) {
300 	int16_t dummy = 3;
301 	kadm5_free_key_data (kadm_handle, &dummy, key_data);
302     }
303     return 0;
304 }
305