No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kadmin / cpw.c
blob1fa27052275502907e72e0ad9cab19d6a9bfd21b
1 /*
2 * Copyright (c) 1997 - 2004 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"
37 __RCSID("$Heimdal: cpw.c 16755 2006-02-18 23:30:32Z lha $"
38 "$NetBSD$");
40 struct cpw_entry_data {
41 int random_key;
42 int random_password;
43 char *password;
44 krb5_key_data *key_data;
47 static int
48 set_random_key (krb5_principal principal)
50 krb5_error_code ret;
51 int i;
52 krb5_keyblock *keys;
53 int num_keys;
55 ret = kadm5_randkey_principal(kadm_handle, principal, &keys, &num_keys);
56 if(ret)
57 return ret;
58 for(i = 0; i < num_keys; i++)
59 krb5_free_keyblock_contents(context, &keys[i]);
60 free(keys);
61 return 0;
64 static int
65 set_random_password (krb5_principal principal)
67 krb5_error_code ret;
68 char pw[128];
70 random_password (pw, sizeof(pw));
71 ret = kadm5_chpass_principal(kadm_handle, principal, pw);
72 if (ret == 0) {
73 char *princ_name;
75 krb5_unparse_name(context, principal, &princ_name);
77 printf ("%s's password set to \"%s\"\n", princ_name, pw);
78 free (princ_name);
80 memset (pw, 0, sizeof(pw));
81 return ret;
84 static int
85 set_password (krb5_principal principal, char *password)
87 krb5_error_code ret = 0;
88 char pwbuf[128];
90 if(password == NULL) {
91 char *princ_name;
92 char *prompt;
94 krb5_unparse_name(context, principal, &princ_name);
95 asprintf(&prompt, "%s's Password: ", princ_name);
96 free (princ_name);
97 ret = UI_UTIL_read_pw_string(pwbuf, sizeof(pwbuf), prompt, 1);
98 free (prompt);
99 if(ret){
100 return 0; /* XXX error code? */
102 password = pwbuf;
104 if(ret == 0)
105 ret = kadm5_chpass_principal(kadm_handle, principal, password);
106 memset(pwbuf, 0, sizeof(pwbuf));
107 return ret;
110 static int
111 set_key_data (krb5_principal principal, krb5_key_data *key_data)
113 krb5_error_code ret;
115 ret = kadm5_chpass_principal_with_key (kadm_handle, principal,
116 3, key_data);
117 return ret;
120 static int
121 do_cpw_entry(krb5_principal principal, void *data)
123 struct cpw_entry_data *e = data;
125 if (e->random_key)
126 return set_random_key (principal);
127 else if (e->random_password)
128 return set_random_password (principal);
129 else if (e->key_data)
130 return set_key_data (principal, e->key_data);
131 else
132 return set_password (principal, e->password);
136 cpw_entry(struct passwd_options *opt, int argc, char **argv)
138 krb5_error_code ret = 0;
139 int i;
140 struct cpw_entry_data data;
141 int num;
142 krb5_key_data key_data[3];
144 data.random_key = opt->random_key_flag;
145 data.random_password = opt->random_password_flag;
146 data.password = opt->password_string;
147 data.key_data = NULL;
149 num = 0;
150 if (data.random_key)
151 ++num;
152 if (data.random_password)
153 ++num;
154 if (data.password)
155 ++num;
156 if (opt->key_string)
157 ++num;
159 if (num > 1) {
160 fprintf (stderr, "give only one of "
161 "--random-key, --random-password, --password, --key\n");
162 return 1;
165 if (opt->key_string) {
166 const char *error;
168 if (parse_des_key (opt->key_string, key_data, &error)) {
169 fprintf (stderr, "failed parsing key \"%s\": %s\n",
170 opt->key_string, error);
171 return 1;
173 data.key_data = key_data;
176 for(i = 0; i < argc; i++)
177 ret = foreach_principal(argv[i], do_cpw_entry, "cpw", &data);
179 if (data.key_data) {
180 int16_t dummy;
181 kadm5_free_key_data (kadm_handle, &dummy, key_data);
184 return ret != 0;