1*0a6a1f1dSLionel Sambuc /* $NetBSD: cpw.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "kadmin_locl.h"
37ebfedea0SLionel Sambuc #include "kadmin-commands.h"
38ebfedea0SLionel Sambuc
39ebfedea0SLionel Sambuc struct cpw_entry_data {
40ebfedea0SLionel Sambuc int random_key;
41ebfedea0SLionel Sambuc int random_password;
42ebfedea0SLionel Sambuc char *password;
43ebfedea0SLionel Sambuc krb5_key_data *key_data;
44ebfedea0SLionel Sambuc };
45ebfedea0SLionel Sambuc
46ebfedea0SLionel Sambuc static int
set_random_key(krb5_principal principal)47ebfedea0SLionel Sambuc set_random_key (krb5_principal principal)
48ebfedea0SLionel Sambuc {
49ebfedea0SLionel Sambuc krb5_error_code ret;
50ebfedea0SLionel Sambuc int i;
51ebfedea0SLionel Sambuc krb5_keyblock *keys;
52ebfedea0SLionel Sambuc int num_keys;
53ebfedea0SLionel Sambuc
54ebfedea0SLionel Sambuc ret = kadm5_randkey_principal(kadm_handle, principal, &keys, &num_keys);
55ebfedea0SLionel Sambuc if(ret)
56ebfedea0SLionel Sambuc return ret;
57ebfedea0SLionel Sambuc for(i = 0; i < num_keys; i++)
58ebfedea0SLionel Sambuc krb5_free_keyblock_contents(context, &keys[i]);
59ebfedea0SLionel Sambuc free(keys);
60ebfedea0SLionel Sambuc return 0;
61ebfedea0SLionel Sambuc }
62ebfedea0SLionel Sambuc
63ebfedea0SLionel Sambuc static int
set_random_password(krb5_principal principal)64ebfedea0SLionel Sambuc set_random_password (krb5_principal principal)
65ebfedea0SLionel Sambuc {
66ebfedea0SLionel Sambuc krb5_error_code ret;
67ebfedea0SLionel Sambuc char pw[128];
68ebfedea0SLionel Sambuc
69ebfedea0SLionel Sambuc random_password (pw, sizeof(pw));
70ebfedea0SLionel Sambuc ret = kadm5_chpass_principal(kadm_handle, principal, pw);
71ebfedea0SLionel Sambuc if (ret == 0) {
72ebfedea0SLionel Sambuc char *princ_name;
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc krb5_unparse_name(context, principal, &princ_name);
75ebfedea0SLionel Sambuc
76ebfedea0SLionel Sambuc printf ("%s's password set to \"%s\"\n", princ_name, pw);
77ebfedea0SLionel Sambuc free (princ_name);
78ebfedea0SLionel Sambuc }
79ebfedea0SLionel Sambuc memset (pw, 0, sizeof(pw));
80ebfedea0SLionel Sambuc return ret;
81ebfedea0SLionel Sambuc }
82ebfedea0SLionel Sambuc
83ebfedea0SLionel Sambuc static int
set_password(krb5_principal principal,char * password)84ebfedea0SLionel Sambuc set_password (krb5_principal principal, char *password)
85ebfedea0SLionel Sambuc {
86ebfedea0SLionel Sambuc krb5_error_code ret = 0;
87ebfedea0SLionel Sambuc char pwbuf[128];
88ebfedea0SLionel Sambuc
89ebfedea0SLionel Sambuc if(password == NULL) {
90ebfedea0SLionel Sambuc char *princ_name;
91ebfedea0SLionel Sambuc char *prompt;
92ebfedea0SLionel Sambuc
93ebfedea0SLionel Sambuc krb5_unparse_name(context, principal, &princ_name);
94ebfedea0SLionel Sambuc asprintf(&prompt, "%s's Password: ", princ_name);
95ebfedea0SLionel Sambuc free (princ_name);
96ebfedea0SLionel Sambuc ret = UI_UTIL_read_pw_string(pwbuf, sizeof(pwbuf), prompt, 1);
97ebfedea0SLionel Sambuc free (prompt);
98ebfedea0SLionel Sambuc if(ret){
99ebfedea0SLionel Sambuc return 0; /* XXX error code? */
100ebfedea0SLionel Sambuc }
101ebfedea0SLionel Sambuc password = pwbuf;
102ebfedea0SLionel Sambuc }
103ebfedea0SLionel Sambuc if(ret == 0)
104ebfedea0SLionel Sambuc ret = kadm5_chpass_principal(kadm_handle, principal, password);
105ebfedea0SLionel Sambuc memset(pwbuf, 0, sizeof(pwbuf));
106ebfedea0SLionel Sambuc return ret;
107ebfedea0SLionel Sambuc }
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc static int
set_key_data(krb5_principal principal,krb5_key_data * key_data)110ebfedea0SLionel Sambuc set_key_data (krb5_principal principal, krb5_key_data *key_data)
111ebfedea0SLionel Sambuc {
112ebfedea0SLionel Sambuc krb5_error_code ret;
113ebfedea0SLionel Sambuc
114ebfedea0SLionel Sambuc ret = kadm5_chpass_principal_with_key (kadm_handle, principal,
115ebfedea0SLionel Sambuc 3, key_data);
116ebfedea0SLionel Sambuc return ret;
117ebfedea0SLionel Sambuc }
118ebfedea0SLionel Sambuc
119ebfedea0SLionel Sambuc static int
do_cpw_entry(krb5_principal principal,void * data)120ebfedea0SLionel Sambuc do_cpw_entry(krb5_principal principal, void *data)
121ebfedea0SLionel Sambuc {
122ebfedea0SLionel Sambuc struct cpw_entry_data *e = data;
123ebfedea0SLionel Sambuc
124ebfedea0SLionel Sambuc if (e->random_key)
125ebfedea0SLionel Sambuc return set_random_key (principal);
126ebfedea0SLionel Sambuc else if (e->random_password)
127ebfedea0SLionel Sambuc return set_random_password (principal);
128ebfedea0SLionel Sambuc else if (e->key_data)
129ebfedea0SLionel Sambuc return set_key_data (principal, e->key_data);
130ebfedea0SLionel Sambuc else
131ebfedea0SLionel Sambuc return set_password (principal, e->password);
132ebfedea0SLionel Sambuc }
133ebfedea0SLionel Sambuc
134ebfedea0SLionel Sambuc int
cpw_entry(struct passwd_options * opt,int argc,char ** argv)135ebfedea0SLionel Sambuc cpw_entry(struct passwd_options *opt, int argc, char **argv)
136ebfedea0SLionel Sambuc {
137ebfedea0SLionel Sambuc krb5_error_code ret = 0;
138ebfedea0SLionel Sambuc int i;
139ebfedea0SLionel Sambuc struct cpw_entry_data data;
140ebfedea0SLionel Sambuc int num;
141ebfedea0SLionel Sambuc krb5_key_data key_data[3];
142ebfedea0SLionel Sambuc
143ebfedea0SLionel Sambuc data.random_key = opt->random_key_flag;
144ebfedea0SLionel Sambuc data.random_password = opt->random_password_flag;
145ebfedea0SLionel Sambuc data.password = opt->password_string;
146ebfedea0SLionel Sambuc data.key_data = NULL;
147ebfedea0SLionel Sambuc
148ebfedea0SLionel Sambuc num = 0;
149ebfedea0SLionel Sambuc if (data.random_key)
150ebfedea0SLionel Sambuc ++num;
151ebfedea0SLionel Sambuc if (data.random_password)
152ebfedea0SLionel Sambuc ++num;
153ebfedea0SLionel Sambuc if (data.password)
154ebfedea0SLionel Sambuc ++num;
155ebfedea0SLionel Sambuc if (opt->key_string)
156ebfedea0SLionel Sambuc ++num;
157ebfedea0SLionel Sambuc
158ebfedea0SLionel Sambuc if (num > 1) {
159ebfedea0SLionel Sambuc fprintf (stderr, "give only one of "
160ebfedea0SLionel Sambuc "--random-key, --random-password, --password, --key\n");
161ebfedea0SLionel Sambuc return 1;
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc if (opt->key_string) {
165ebfedea0SLionel Sambuc const char *error;
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc if (parse_des_key (opt->key_string, key_data, &error)) {
168ebfedea0SLionel Sambuc fprintf (stderr, "failed parsing key \"%s\": %s\n",
169ebfedea0SLionel Sambuc opt->key_string, error);
170ebfedea0SLionel Sambuc return 1;
171ebfedea0SLionel Sambuc }
172ebfedea0SLionel Sambuc data.key_data = key_data;
173ebfedea0SLionel Sambuc }
174ebfedea0SLionel Sambuc
175ebfedea0SLionel Sambuc for(i = 0; i < argc; i++)
176ebfedea0SLionel Sambuc ret = foreach_principal(argv[i], do_cpw_entry, "cpw", &data);
177ebfedea0SLionel Sambuc
178ebfedea0SLionel Sambuc if (data.key_data) {
179ebfedea0SLionel Sambuc int16_t dummy;
180ebfedea0SLionel Sambuc kadm5_free_key_data (kadm_handle, &dummy, key_data);
181ebfedea0SLionel Sambuc }
182ebfedea0SLionel Sambuc
183ebfedea0SLionel Sambuc return ret != 0;
184ebfedea0SLionel Sambuc }
185