summaryrefslogtreecommitdiffstats
path: root/src/admin/edit/cpw.c
blob: c75c73a70d9690c1995fb7dcaa8de5a14842cf6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * admin/edit/cpw.c
 *
 * Copyright 1995 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 * Change passwords for a KDC db entry.
 */

#include "k5-int.h"
#include "com_err.h"
#include "adm.h"
#include "adm_proto.h"
#include <stdio.h>
#include <time.h>

#include "kdb5_edit.h"

extern char	*Err_no_master_msg;
extern char	*Err_no_database;
extern char	*current_dbname;

/*
 * I can't figure out any way for this not to be global, given how ss
 * works.
 */
extern int exit_status;
extern krb5_context edit_context;
extern krb5_keyblock master_keyblock;
extern krb5_principal master_princ;
extern krb5_db_entry master_entry;
extern krb5_encrypt_block master_encblock;
extern int	valid_master_key;
extern char *krb5_default_pwd_prompt1, *krb5_default_pwd_prompt2;
extern krb5_boolean dbactive;
extern FILE *scriptfile;

/*
 * XXX  This realy should be configured from kdc.conf
 */
static krb5_key_salt_tuple ks_tuple_rnd_def[] =
	{{ ENCTYPE_DES_CBC_CRC, 0 },
	 { ENCTYPE_DES_CBC_MD5, 0 },
	 { ENCTYPE_DES_CBC_CRC, KRB5_KDB_SALTTYPE_V4}};
	 { ENCTYPE_DES_CBC_MD5, KRB5_KDB_SALTTYPE_V4}};
static int ks_tuple_rnd_def_count = 4;

static void
enter_rnd_key(argc, argv, entry)
    int			  argc;
    char	       ** argv;
    krb5_db_entry 	* entry;
{
    krb5_error_code 	  retval;
    int 		  nprincs = 1;
    
    if ((retval = krb5_dbe_crk(edit_context, &master_encblock,
			       ks_tuple_rnd_def,
			       ks_tuple_rnd_def_count, entry))) {
	com_err(argv[0], retval, "while generating random key");
        krb5_db_free_principal(edit_context, entry, nprincs);
	exit_status++;
	return;
    }

    if ((retval = krb5_db_put_principal(edit_context, entry, &nprincs))) {
	com_err(argv[0], retval, "while storing entry for '%s'\n", argv[1]);
        krb5_db_free_principal(edit_context, entry, nprincs);
	exit_status++;
	return;
    }
    
    krb5_db_free_principal(edit_context, entry, nprincs);

    if (nprincs != 1) {
	com_err(argv[0], 0, "entry not stored in database (unknown failure)");
	exit_status++;
    }
	
}

static int
pre_key(argc, argv, newprinc, entry)
    int 	 	  argc;
    char 	       ** argv;
    krb5_principal 	* newprinc;
    krb5_db_entry 	* entry;
{
    krb5_boolean 	  more;
    krb5_error_code 	  retval;
    int 		  nprincs = 1;

    if (!dbactive) {
	com_err(argv[0], 0, Err_no_database);
    } else if (!valid_master_key) {
	com_err(argv[0], 0, Err_no_master_msg);
    } else if ((retval = krb5_parse_name(edit_context,
					 argv[argc-1],
					 newprinc))) {
	com_err(argv[0], retval, "while parsing '%s'", argv[argc-1]);
    } else if ((retval = krb5_db_get_principal(edit_context, *newprinc, entry, 
					       &nprincs, &more))) {
        com_err(argv[0],retval,"while trying to get principal's db entry");
    } else if ((nprincs > 1) || (more)) {
	krb5_db_free_principal(edit_context, entry, nprincs);
    	krb5_free_principal(edit_context, *newprinc);
    } else if (nprincs) 
	return(1);
    else 
	return(0);
    return(-1);
}

void add_rnd_key(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code 	  retval;
    krb5_principal 	  newprinc;
    krb5_db_entry 	  entry;

    if (argc < 2) {
	com_err(argv[0], 0, "Too few arguments");
	com_err(argv[0], 0, "Usage: %s principal", argv[0]);
    	exit_status++;
	return;
    }
    switch (pre_key(argc, argv, &newprinc, &entry)) {
    case 0:
	if ((retval = create_db_entry(newprinc, &entry))) {
	    com_err(argv[0], retval, "While creating new db entry.");
	    exit_status++;
	    return;
	}
    	krb5_free_principal(edit_context, newprinc);
        enter_rnd_key(argc, argv, &entry);
	return;
    case 1:
	com_err(argv[0], 0, "Principal '%s' already exists.", argv[1]);
        krb5_db_free_principal(edit_context, &entry, 1);
        krb5_free_principal(edit_context, newprinc);
    default:
    	exit_status++;
	break;
    }
}

void change_rnd_key(argc, argv)
    int argc;
    char *argv[];
{
    krb5_principal 	  newprinc;
    krb5_db_entry 	  entry;

    if (argc < 2) {
	com_err(argv[0], 0, "Too few arguments");
	com_err(argv[0], 0, "Usage: %s principal", argv[0]);
    	exit_status++;
	return;
    }
    switch (pre_key(argc, argv, &newprinc, &entry)) {
    case 1:
    	krb5_free_principal(edit_context, newprinc);
        enter_rnd_key(argc, argv, &entry);
	break;
    case 0:
    	com_err(argv[0], 0, "No principal '%s' exists", argv[1]);
    default:
        exit_status++;
	break;
    }
}

static krb5_key_salt_tuple ks_tuple_default[] = {{ ENCTYPE_DES_CBC_CRC, 0 }};
static int ks_tuple_count_default = 1;

void 
enter_pwd_key(cmdname, princ, ks_tuple, ks_tuple_count, entry)
    char 		* cmdname;
    char 		* princ;
    krb5_key_salt_tuple	* ks_tuple;
    int			  ks_tuple_count;
    krb5_db_entry 	* entry;
{
    char 		  password[KRB5_ADM_MAX_PASSWORD_LEN];
    int 		  pwsize = KRB5_ADM_MAX_PASSWORD_LEN;
    krb5_error_code 	  retval;
    int			  one = 1;
  
    /* Prompt for password only if interactive */
    if (!scriptfile) {
	if ((retval = krb5_read_password(edit_context,
					 krb5_default_pwd_prompt1,
					 krb5_default_pwd_prompt2,
					 password, &pwsize))) {
	    com_err(cmdname, retval, "while reading password for '%s'", princ);
	    goto errout;
	}
    }
    else {
	if (!fgets(password, pwsize, scriptfile)) {
	    com_err(cmdname, errno, "while reading password for '%s'", princ);
	    retval = errno;
	    goto errout;
	}
	else {
	    pwsize = strlen(password);
	    if (password[pwsize-1] == '\n') {
		password[pwsize-1] = '\0';
		pwsize--;
	    }
	}
    }
    
    if (ks_tuple_count == 0) {
	ks_tuple_count = ks_tuple_count_default;
	ks_tuple = ks_tuple_default;
    }
    if ((retval = krb5_dbe_cpw(edit_context, &master_encblock, ks_tuple,
			       ks_tuple_count, password, entry))) {
	com_err(cmdname, retval, "while storing entry for '%s'\n", princ);
        memset(password, 0, sizeof(password)); /* erase it */
	krb5_dbe_free_contents(edit_context, entry);
	goto errout;
    }
    memset(password, 0, sizeof(password)); /* erase it */

    /* Write the entry back out and we're done */
    if ((retval = krb5_db_put_principal(edit_context, entry, &one))) {
	com_err(cmdname, retval, "while storing entry for '%s'\n", princ);
    }

    if (one != 1) {
        com_err(cmdname, 0, "entry not stored in database (unknown failure)");
        exit_status++;
    }

errout:;
    krb5_db_free_principal(edit_context, entry, one);
    if (retval)
        exit_status++;
    return;
}

void change_pwd_key(argc, argv)
    int argc;
    char *argv[];
{
    krb5_key_salt_tuple	* ks_tuple = NULL;
    krb5_int32		  n_ks_tuple = 0;
    krb5_principal 	  newprinc;
    krb5_db_entry	  entry;

    int			  i;

    if (argc < 2) {
	com_err(argv[0], 0, "Too few arguments");
	com_err(argv[0], 0, "Usage: %s [<key_type[:<salt_type>]>] principal",
		argv[0]);
    	exit_status++;
	return;
    }

    for (i = 1; i < (argc - 1); i++) {
	if (krb5_string_to_keysalts(argv[i],
				    "",
				    ":",
				    0,
				    &ks_tuple,
				    &n_ks_tuple)) {
	    com_err(argv[0], 0, "Unrecognized key/salt type %s", argv[i]);
	    exit_status++;
	    return;
	}
    }

    switch (pre_key(argc, argv, &newprinc, &entry)) {
    case 1:
        /* Done with principal */ 
        krb5_free_principal(edit_context, newprinc);
        enter_pwd_key(argv[0], argv[i], ks_tuple, n_ks_tuple, &entry);
	break;
    case 0:
    	com_err(argv[0], 0, "No principal '%s' exists", argv[i]);
    default:
        exit_status++;
	break;
    }

    if (ks_tuple) {
	free(ks_tuple);
    }
}

void add_new_key(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code 	  retval;
    krb5_principal 	  newprinc;
    krb5_db_entry	  entry;

    if (argc < 2) {
	com_err(argv[0], 0, "Too few arguments");
	com_err(argv[0], 0, "Usage: %s [<key_type[:<salt_type>]>] principal",
		argv[0]);
    	exit_status++;
	return;
    }
    switch (pre_key(argc, argv, &newprinc, &entry)) {
    case 0:
	if ((retval = create_db_entry(newprinc, &entry))) {
	    com_err(argv[0], retval, "While creating new db entry.");
	    exit_status++;
	    return;
	}
        enter_pwd_key(argv[0], argv[argc - 1], NULL, 0, &entry);
    	krb5_free_principal(edit_context, newprinc);
	return;
    case 1:
	com_err(argv[0], 0, "Principal '%s' already exists.", argv[argc - 1]);
        krb5_db_free_principal(edit_context, &entry, 1);
        krb5_free_principal(edit_context, newprinc);
    default:
    	exit_status++;
	break;
    }
}