xref: /freebsd/crypto/heimdal/kadmin/ank.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1997 - 1999 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.19 1999/12/02 17:04:57 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 		   const char *max_ticket_life,
72 		   const char *max_renewable_life,
73 		   const char *attributes,
74 		   const char *expiration,
75 		   const char *pw_expiration)
76 {
77     krb5_error_code ret;
78     kadm5_principal_ent_rec princ, defrec;
79     kadm5_principal_ent_rec *default_ent = NULL;
80     krb5_principal princ_ent = NULL;
81     int mask = 0;
82     int default_mask = 0;
83     char pwbuf[1024];
84 
85     memset(&princ, 0, sizeof(princ));
86     ret = krb5_parse_name(context, name, &princ_ent);
87     if (ret) {
88 	krb5_warn(context, ret, "krb5_parse_name");
89 	return ret;
90     }
91     princ.principal = princ_ent;
92     mask |= KADM5_PRINCIPAL;
93 
94     ret = set_entry(context, &princ, &mask,
95 		    max_ticket_life, max_renewable_life,
96 		    expiration, pw_expiration, attributes);
97     if (ret)
98 	goto out;
99 
100     default_ent = &defrec;
101     ret = get_default (kadm_handle, princ_ent, default_ent);
102     if (ret) {
103 	default_ent  = NULL;
104 	default_mask = 0;
105     } else {
106 	default_mask = KADM5_ATTRIBUTES | KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
107 	    KADM5_PRINC_EXPIRE_TIME | KADM5_PW_EXPIRATION;
108     }
109 
110     edit_entry(&princ, &mask, default_ent, default_mask);
111     if(rand_key) {
112 	princ.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
113 	mask |= KADM5_ATTRIBUTES;
114 	strlcpy (pwbuf, "hemlig", sizeof(pwbuf));
115 	password = pwbuf;
116     } else if (rand_password) {
117 	random_password (pwbuf, sizeof(pwbuf));
118 	password = pwbuf;
119     } else if(password == NULL) {
120 	char *princ_name;
121 	char *prompt;
122 
123 	krb5_unparse_name(context, princ_ent, &princ_name);
124 	asprintf (&prompt, "%s's Password: ", princ_name);
125 	free (princ_name);
126 	ret = des_read_pw_string (pwbuf, sizeof(pwbuf), prompt, 1);
127 	free (prompt);
128 	if (ret)
129 	    goto out;
130 	password = pwbuf;
131     }
132 
133     ret = kadm5_create_principal(kadm_handle, &princ, mask, password);
134     if(ret)
135 	krb5_warn(context, ret, "kadm5_create_principal");
136     if(rand_key) {
137 	krb5_keyblock *new_keys;
138 	int n_keys, i;
139 	ret = kadm5_randkey_principal(kadm_handle, princ_ent,
140 				      &new_keys, &n_keys);
141 	if(ret){
142 	    krb5_warn(context, ret, "kadm5_randkey_principal");
143 	    n_keys = 0;
144 	}
145 	for(i = 0; i < n_keys; i++)
146 	    krb5_free_keyblock_contents(context, &new_keys[i]);
147 	free(new_keys);
148 	kadm5_get_principal(kadm_handle, princ_ent, &princ,
149 			    KADM5_PRINCIPAL | KADM5_KVNO | KADM5_ATTRIBUTES);
150 	princ.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
151 	princ.kvno = 1;
152 	kadm5_modify_principal(kadm_handle, &princ,
153 			       KADM5_ATTRIBUTES | KADM5_KVNO);
154 	kadm5_free_principal_ent(kadm_handle, &princ);
155     } else if (rand_password) {
156 	char *princ_name;
157 
158 	krb5_unparse_name(context, princ_ent, &princ_name);
159 	printf ("added %s with password `%s'\n", princ_name, password);
160 	free (princ_name);
161     }
162 out:
163     if (princ_ent)
164 	krb5_free_principal (context, princ_ent);
165     if(default_ent)
166 	kadm5_free_principal_ent (context, default_ent);
167     if (password != NULL)
168 	memset (password, 0, strlen(password));
169     return ret;
170 }
171 
172 /*
173  * the ank command
174  */
175 
176 static struct getargs args[] = {
177     { "random-key",	'r',	arg_flag,	NULL, "set random key" },
178     { "random-password", 0,	arg_flag,	NULL, "set random password" },
179     { "password",	'p',	arg_string,	NULL, "princial's password" },
180     { "max-ticket-life",  0,	arg_string,	NULL, "max ticket lifetime",
181       "lifetime"},
182     { "max-renewable-life",  0,	arg_string,	NULL,
183       "max renewable lifetime", "lifetime" },
184     { "attributes",	0,	arg_string,	NULL, "principal attributes",
185       "attributes"},
186     { "expiration-time",0,	arg_string,	NULL, "Expiration time",
187       "time"},
188     { "pw-expiration-time", 0,  arg_string,	NULL,
189       "Password expiration time", "time"}
190 };
191 
192 static int num_args = sizeof(args) / sizeof(args[0]);
193 
194 static void
195 usage(void)
196 {
197     arg_printusage (args, num_args, "ank", "principal");
198 }
199 
200 /*
201  * Parse arguments and add all the principals.
202  */
203 
204 int
205 add_new_key(int argc, char **argv)
206 {
207     char *password = NULL;
208     int random_key = 0;
209     int random_password = 0;
210     int optind = 0;
211     krb5_error_code ret;
212     char *max_ticket_life	= NULL;
213     char *max_renewable_life	= NULL;
214     char *attributes		= NULL;
215     char *expiration		= NULL;
216     char *pw_expiration		= NULL;
217     int i;
218     int num;
219 
220     args[0].value = &random_key;
221     args[1].value = &random_password;
222     args[2].value = &password;
223     args[3].value = &max_ticket_life;
224     args[4].value = &max_renewable_life;
225     args[5].value = &attributes;
226     args[6].value = &expiration;
227     args[7].value = &pw_expiration;
228 
229     if(getarg(args, num_args, argc, argv, &optind)) {
230 	usage ();
231 	return 0;
232     }
233     if(optind == argc) {
234 	usage ();
235 	return 0;
236     }
237 
238     num = 0;
239     if (random_key)
240 	++num;
241     if (random_password)
242 	++num;
243     if (password)
244 	++num;
245 
246     if (num > 1) {
247 	printf ("give only one of "
248 		"--random-key, --random-password, --password\n");
249 	return 0;
250     }
251 
252     for (i = optind; i < argc; ++i) {
253 	ret = add_one_principal (argv[i], random_key, random_password,
254 				 password,
255 				 max_ticket_life,
256 				 max_renewable_life,
257 				 attributes,
258 				 expiration,
259 				 pw_expiration);
260 	if (ret) {
261 	    krb5_warn (context, ret, "adding %s", argv[i]);
262 	    break;
263 	}
264     }
265     return 0;
266 }
267