1 /* $NetBSD: cpw.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $ */
4 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "kadmin_locl.h"
37 #include "kadmin-commands.h"
39 struct cpw_entry_data
{
43 krb5_key_data
*key_data
;
47 set_random_key (krb5_principal principal
)
54 ret
= kadm5_randkey_principal(kadm_handle
, principal
, &keys
, &num_keys
);
57 for(i
= 0; i
< num_keys
; i
++)
58 krb5_free_keyblock_contents(context
, &keys
[i
]);
64 set_random_password (krb5_principal principal
)
69 random_password (pw
, sizeof(pw
));
70 ret
= kadm5_chpass_principal(kadm_handle
, principal
, pw
);
74 krb5_unparse_name(context
, principal
, &princ_name
);
76 printf ("%s's password set to \"%s\"\n", princ_name
, pw
);
79 memset (pw
, 0, sizeof(pw
));
84 set_password (krb5_principal principal
, char *password
)
86 krb5_error_code ret
= 0;
89 if(password
== NULL
) {
93 krb5_unparse_name(context
, principal
, &princ_name
);
94 asprintf(&prompt
, "%s's Password: ", princ_name
);
96 ret
= UI_UTIL_read_pw_string(pwbuf
, sizeof(pwbuf
), prompt
, 1);
99 return 0; /* XXX error code? */
104 ret
= kadm5_chpass_principal(kadm_handle
, principal
, password
);
105 memset(pwbuf
, 0, sizeof(pwbuf
));
110 set_key_data (krb5_principal principal
, krb5_key_data
*key_data
)
114 ret
= kadm5_chpass_principal_with_key (kadm_handle
, principal
,
120 do_cpw_entry(krb5_principal principal
, void *data
)
122 struct cpw_entry_data
*e
= data
;
125 return set_random_key (principal
);
126 else if (e
->random_password
)
127 return set_random_password (principal
);
128 else if (e
->key_data
)
129 return set_key_data (principal
, e
->key_data
);
131 return set_password (principal
, e
->password
);
135 cpw_entry(struct passwd_options
*opt
, int argc
, char **argv
)
137 krb5_error_code ret
= 0;
139 struct cpw_entry_data data
;
141 krb5_key_data key_data
[3];
143 data
.random_key
= opt
->random_key_flag
;
144 data
.random_password
= opt
->random_password_flag
;
145 data
.password
= opt
->password_string
;
146 data
.key_data
= NULL
;
151 if (data
.random_password
)
159 fprintf (stderr
, "give only one of "
160 "--random-key, --random-password, --password, --key\n");
164 if (opt
->key_string
) {
167 if (parse_des_key (opt
->key_string
, key_data
, &error
)) {
168 fprintf (stderr
, "failed parsing key \"%s\": %s\n",
169 opt
->key_string
, error
);
172 data
.key_data
= key_data
;
175 for(i
= 0; i
< argc
; i
++)
176 ret
= foreach_principal(argv
[i
], do_cpw_entry
, "cpw", &data
);
180 kadm5_free_key_data (kadm_handle
, &dummy
, key_data
);