xref: /titanic_51/usr/src/cmd/krb5/kadmin/dbutil/kdb5_util.c (revision 002c70ff32f5df6f93c15f88d351ce26443e6ee7)
1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10  *
11  *	Openvision retains the copyright to derivative works of
12  *	this source code.  Do *NOT* create a derivative of this
13  *	source code before consulting with your legal department.
14  *	Do *NOT* integrate *ANY* of this source code into another
15  *	product before consulting with your legal department.
16  *
17  *	For further information, read the top-level Openvision
18  *	copyright which is contained in the top-level MIT Kerberos
19  *	copyright.
20  *
21  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
22  *
23  */
24 
25 
26 /*
27  * admin/edit/kdb5_edit.c
28  *
29  * (C) Copyright 1990,1991, 1996 by the Massachusetts Institute of Technology.
30  * All Rights Reserved.
31  *
32  * Export of this software from the United States of America may
33  *   require a specific license from the United States Government.
34  *   It is the responsibility of any person or organization contemplating
35  *   export to obtain such a license before exporting.
36  *
37  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
38  * distribute this software and its documentation for any purpose and
39  * without fee is hereby granted, provided that the above copyright
40  * notice appear in all copies and that both that copyright notice and
41  * this permission notice appear in supporting documentation, and that
42  * the name of M.I.T. not be used in advertising or publicity pertaining
43  * to distribution of the software without specific, written prior
44  * permission.  Furthermore if you modify this software you must label
45  * your software as modified software and not distribute it in such a
46  * fashion that it might be confused with the original M.I.T. software.
47  * M.I.T. makes no representations about the suitability of
48  * this software for any purpose.  It is provided "as is" without express
49  * or implied warranty.
50  *
51  *
52  * Edit a KDC database.
53  */
54 
55 /*
56  * Copyright (C) 1998 by the FundsXpress, INC.
57  *
58  * All rights reserved.
59  *
60  * Export of this software from the United States of America may require
61  * a specific license from the United States Government.  It is the
62  * responsibility of any person or organization contemplating export to
63  * obtain such a license before exporting.
64  *
65  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
66  * distribute this software and its documentation for any purpose and
67  * without fee is hereby granted, provided that the above copyright
68  * notice appear in all copies and that both that copyright notice and
69  * this permission notice appear in supporting documentation, and that
70  * the name of FundsXpress. not be used in advertising or publicity pertaining
71  * to distribution of the software without specific, written prior
72  * permission.  FundsXpress makes no representations about the suitability of
73  * this software for any purpose.  It is provided "as is" without express
74  * or implied warranty.
75  *
76  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
77  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
78  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
79  */
80 
81 /*
82  *  Yes, I know this is a hack, but we need admin.h without including the
83  *  rpc.h header. Additionally, our rpc.h header brings in
84  *  a des.h header which causes other problems.
85  */
86 #define	_RPC_RPC_H
87 
88 #include <stdio.h>
89 #include <k5-int.h>
90 #include <kadm5/admin.h>
91 #include <rpc/types.h>
92 #include <krb5/adm_proto.h>
93 #include <rpc/xdr.h>
94 #include <time.h>
95 #include <libintl.h>
96 #include <locale.h>
97 #include "kdb5_util.h"
98 
99 char	*Err_no_master_msg = "Master key not entered!\n";
100 char	*Err_no_database = "Database not currently opened!\n";
101 
102 /*
103  * XXX Ick, ick, ick.  These global variables shouldn't be global....
104  */
105 char *mkey_password = 0;
106 
107 /*
108  * I can't figure out any way for this not to be global, given how ss
109  * works.
110  */
111 
112 int exit_status = 0;
113 krb5_context util_context;
114 kadm5_config_params global_params;
115 
116 void usage()
117 {
118      fprintf(stderr, "%s: "
119 	   "kdb5_util [-x db_args]* [-r realm] [-d dbname] [-k mkeytype] [-M mkeyname]\n"
120 	     "\t        [-sf stashfilename] [-P password] [-m] cmd [cmd_options]\n"
121 	     "\tcreate	[-s]\n"
122 	     "\tdestroy	[-f]\n"
123 	     "\tstash	[-f keyfile]\n"
124 	     "\tdump	[-old] [-ov] [-b6] [-verbose] [filename	[princs...]]\n"
125 	     "\t	[-mkey_convert] [-new_mkey_file mkey_file]\n"
126 	     "\t	[-rev] [-recurse] [filename [princs...]]\n"
127 	     "\tload	[-old] [-ov] [-b6] [-verbose] [-update] filename\n"
128 	     "\tark	[-e etype_list] principal\n"
129 	     "\nwhere,\n\t[-x db_args]* - any number of database specific arguments.\n"
130 	     "\t\t\tLook at each database documentation for supported arguments\n",
131 		gettext("Usage"));
132      exit(1);
133 }
134 
135 krb5_keyblock master_key;
136 extern krb5_principal master_princ;
137 krb5_db_entry master_entry;
138 int	valid_master_key = 0;
139 
140 char *progname;
141 krb5_boolean manual_mkey = FALSE;
142 krb5_boolean dbactive = FALSE;
143 
144 static int open_db_and_mkey(void);
145 
146 static void add_random_key(int, char **);
147 
148 typedef void (*cmd_func)(int, char **);
149 
150 struct _cmd_table {
151      char *name;
152      cmd_func func;
153      int opendb;
154 } cmd_table[] = {
155      {"create", kdb5_create, 0},
156      {"destroy", kdb5_destroy, 1},
157      {"stash", kdb5_stash, 1},
158      {"dump", dump_db, 1},
159      {"load", load_db, 0},
160      {"ark", add_random_key, 1},
161      {NULL, NULL, 0},
162 };
163 
164 static struct _cmd_table *cmd_lookup(name)
165    char *name;
166 {
167      struct _cmd_table *cmd = cmd_table;
168      while (cmd->name) {
169 	  if (strcmp(cmd->name, name) == 0)
170 	       return cmd;
171 	  else
172 	       cmd++;
173      }
174 
175      return NULL;
176 }
177 
178 #define ARG_VAL (--argc > 0 ? (koptarg = *(++argv)) : (char *)(usage(), NULL))
179 
180 char **db5util_db_args = NULL;
181 int    db5util_db_args_size = 0;
182 
183 static void extended_com_err_fn (const char *myprog, errcode_t code,
184 				 const char *fmt, va_list args)
185 {
186     const char *emsg;
187     if (code) {
188 	emsg = krb5_get_error_message (util_context, code);
189 	fprintf (stderr, "%s: %s ", myprog, emsg);
190 	krb5_free_error_message (util_context, emsg);
191     } else {
192 	fprintf (stderr, "%s: ", myprog);
193     }
194     vfprintf (stderr, fmt, args);
195     fprintf (stderr, "\n");
196 }
197 
198 int add_db_arg(char *arg)
199 {
200     char **temp;
201     db5util_db_args_size++;
202     temp = realloc(db5util_db_args,
203 		   sizeof(char *) * (db5util_db_args_size + 1));
204     if (temp == NULL)
205 	return 0;
206     db5util_db_args = temp;
207     db5util_db_args[db5util_db_args_size-1] = arg;
208     db5util_db_args[db5util_db_args_size]   = NULL;
209     return 1;
210 }
211 
212 int main(argc, argv)
213     int argc;
214     char *argv[];
215 {
216     struct _cmd_table *cmd = NULL;
217     char *koptarg, **cmd_argv;
218     char *db_name_tmp = NULL;
219     int cmd_argc;
220     krb5_error_code retval;
221 
222 	(void) setlocale(LC_ALL, "");
223     set_com_err_hook(extended_com_err_fn);
224 
225 #if !defined(TEXT_DOMAIN)  /* Should be defined by cc -D */
226 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
227 #endif
228 
229 	(void) textdomain(TEXT_DOMAIN);
230 
231 	Err_no_master_msg = gettext("Master key not entered!\n");
232 	Err_no_database = gettext("Database not currently opened!\n");
233 
234     retval = kadm5_init_krb5_context(&util_context);
235     if (retval) {
236 	    com_err (progname, retval,
237 		gettext("while initializing Kerberos code"));
238 	    exit(1);
239     }
240 	progname = (strrchr(argv[0], '/') ?
241 		    strrchr(argv[0], '/') + 1 : argv[0]);
242 
243     cmd_argv = (char **) malloc(sizeof(char *)*argc);
244     if (cmd_argv == NULL) {
245 		com_err(progname, ENOMEM,
246 		    gettext("while creating sub-command arguments"));
247 	 exit(1);
248     }
249     memset(cmd_argv, 0, sizeof(char *)*argc);
250     cmd_argc = 1;
251 
252     argv++; argc--;
253     while (*argv) {
254        if (strcmp(*argv, "-P") == 0 && ARG_VAL) {
255 	    mkey_password = koptarg;
256 	    manual_mkey = TRUE;
257        } else if (strcmp(*argv, "-d") == 0 && ARG_VAL) {
258 	    global_params.dbname = koptarg;
259 	    global_params.mask |= KADM5_CONFIG_DBNAME;
260 
261 	    db_name_tmp = malloc( strlen(global_params.dbname) + sizeof("dbname="));
262 	    if( db_name_tmp == NULL )
263 	    {
264 		com_err(progname, ENOMEM, "while parsing command arguments");
265 		exit(1);
266 	    }
267 
268 	    strcpy( db_name_tmp, "dbname=");
269 	    strcat( db_name_tmp, global_params.dbname );
270 
271 	    if (!add_db_arg(db_name_tmp)) {
272 		com_err(progname, ENOMEM, "while parsing command arguments\n");
273 		exit(1);
274 	    }
275 
276        } else if (strcmp(*argv, "-x") == 0 && ARG_VAL) {
277 	   if (!add_db_arg(koptarg)) {
278 		com_err(progname, ENOMEM, "while parsing command arguments\n");
279 		exit(1);
280 	   }
281 
282        } else if (strcmp(*argv, "-r") == 0 && ARG_VAL) {
283 	    global_params.realm = koptarg;
284 	    global_params.mask |= KADM5_CONFIG_REALM;
285 	    /* not sure this is really necessary */
286 	    if ((retval = krb5_set_default_realm(util_context,
287 						 global_params.realm))) {
288 				com_err(progname, retval,
289 					gettext("while setting default "
290 						"realm name"));
291 		 exit(1);
292 	    }
293        } else if (strcmp(*argv, "-k") == 0 && ARG_VAL) {
294 	    if (krb5_string_to_enctype(koptarg, &global_params.enctype))
295 		 com_err(argv[0], 0, gettext("%s is an invalid enctype"), koptarg);
296 	    else
297 		 global_params.mask |= KADM5_CONFIG_ENCTYPE;
298        } else if (strcmp(*argv, "-M") == 0 && ARG_VAL) {
299 	    global_params.mkey_name = koptarg;
300 	    global_params.mask |= KADM5_CONFIG_MKEY_NAME;
301        } else if (((strcmp(*argv, "-sf") == 0)
302 		/* SUNWresync121 - carry the old -f forward too */
303 		|| (strcmp(*argv, "-f") == 0)) && ARG_VAL) {
304 	    global_params.stash_file = koptarg;
305 	    global_params.mask |= KADM5_CONFIG_STASH_FILE;
306        } else if (strcmp(*argv, "-m") == 0) {
307 	    manual_mkey = TRUE;
308 	    global_params.mkey_from_kbd = 1;
309 	    global_params.mask |= KADM5_CONFIG_MKEY_FROM_KBD;
310        } else if (cmd_lookup(*argv) != NULL) {
311 	    if (cmd_argv[0] == NULL)
312 		 cmd_argv[0] = *argv;
313 	    else
314 		 usage();
315        } else {
316 	    cmd_argv[cmd_argc++] = *argv;
317        }
318        argv++; argc--;
319     }
320 
321     if (cmd_argv[0] == NULL)
322 	 usage();
323 
324     retval = kadm5_get_config_params(util_context, NULL, NULL,
325 				     &global_params, &global_params);
326     if (retval) {
327 		com_err(argv[0], retval,
328 		    gettext("while retreiving configuration parameters"));
329 	 exit(1);
330     }
331 
332     /*
333      * Dump creates files which should not be world-readable.  It is
334      * easiest to do a single umask call here.
335      */
336     (void) umask(077);
337 
338     (void) memset(&master_key, 0, sizeof (krb5_keyblock));
339 
340     if ((global_params.enctype != ENCTYPE_UNKNOWN) &&
341 	(!krb5_c_valid_enctype(global_params.enctype))) {
342 	com_err(argv[0], KRB5_PROG_KEYTYPE_NOSUPP,
343 	    gettext("while setting up enctype %d"), global_params.enctype);
344     }
345 
346     cmd = cmd_lookup(cmd_argv[0]);
347     if (cmd->opendb && open_db_and_mkey())
348 	 return exit_status;
349 
350 	if (global_params.iprop_enabled == TRUE)
351 		ulog_set_role(util_context, IPROP_MASTER);
352 	else
353 		ulog_set_role(util_context, IPROP_NULL);
354 
355     (*cmd->func)(cmd_argc, cmd_argv);
356 
357     if( db_name_tmp )
358 	free( db_name_tmp );
359 
360     if( db5util_db_args )
361 	free(db5util_db_args);
362 
363     kadm5_free_config_params(util_context, &global_params);
364     krb5_free_context(util_context);
365     return exit_status;
366 }
367 
368 #if 0
369 /*
370  * This function is no longer used in kdb5_util (and it would no
371  * longer work, anyway).
372  */
373 void set_dbname(argc, argv)
374     int argc;
375     char *argv[];
376 {
377     krb5_error_code retval;
378 
379     if (argc < 3) {
380 		com_err(argv[0], 0, gettext("Too few arguments"));
381 		com_err(argv[0], 0, gettext("Usage: %s dbpathname realmname"),
382 			argv[0]);
383 	exit_status++;
384 	return;
385     }
386     if (dbactive) {
387 	if ((retval = krb5_db_fini(util_context)) && retval!= KRB5_KDB_DBNOTINITED) {
388 	    com_err(argv[0], retval, gettext("while closing previous database"));
389 	    exit_status++;
390 	    return;
391 	}
392 	if (valid_master_key) {
393 	    krb5_free_keyblock_contents(util_context, &master_key);
394 	    master_key.contents = NULL;
395 	    valid_master_key = 0;
396 	}
397 	krb5_free_principal(util_context, master_princ);
398 	dbactive = FALSE;
399     }
400 
401     (void) set_dbname_help(argv[0], argv[1]);
402     return;
403 }
404 #endif
405 
406 /*
407  * open_db_and_mkey: Opens the KDC and policy database, and sets the
408  * global master_* variables.  Sets dbactive to TRUE if the databases
409  * are opened, and valid_master_key to 1 if the global master
410  * variables are set properly.  Returns 0 on success, and 1 on
411  * failure, but it is not considered a failure if the master key
412  * cannot be fetched (the master key stash file may not exist when the
413  * program is run).
414  */
415 static int open_db_and_mkey()
416 {
417     krb5_error_code retval;
418     int nentries;
419     krb5_boolean more;
420     krb5_data scratch, pwd, seed;
421 
422     dbactive = FALSE;
423     valid_master_key = 0;
424 
425     if ((retval = krb5_db_open(util_context, db5util_db_args,
426 			       KRB5_KDB_OPEN_RW | KRB5_KDB_SRV_TYPE_ADMIN))) {
427 	com_err(progname, retval, "while initializing database");
428 	exit_status++;
429 	return(1);
430     }
431 
432    /* assemble & parse the master key name */
433 
434     if ((retval = krb5_db_setup_mkey_name(util_context,
435 					  global_params.mkey_name,
436 					  global_params.realm,
437 					  0, &master_princ))) {
438 		com_err(progname, retval,
439 		    gettext("while setting up master key name"));
440 	exit_status++;
441 	return(1);
442     }
443     nentries = 1;
444     if ((retval = krb5_db_get_principal(util_context, master_princ,
445 					&master_entry, &nentries, &more))) {
446 		com_err(progname, retval,
447 		    gettext("while retrieving master entry"));
448 	exit_status++;
449 	(void) krb5_db_fini(util_context);
450 	return(1);
451     } else if (more) {
452 	com_err(progname, KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE,
453 		    gettext("while retrieving master entry"));
454 	exit_status++;
455 	(void) krb5_db_fini(util_context);
456 	return(1);
457     } else if (!nentries) {
458 		com_err(progname, KRB5_KDB_NOENTRY,
459 		    gettext("while retrieving master entry"));
460 	exit_status++;
461 	(void) krb5_db_fini(util_context);
462 	return(1);
463     }
464 
465     krb5_db_free_principal(util_context, &master_entry, nentries);
466 
467     /* the databases are now open, and the master principal exists */
468     dbactive = TRUE;
469 
470     if (mkey_password) {
471 	pwd.data = mkey_password;
472 	pwd.length = strlen(mkey_password);
473 	retval = krb5_principal2salt(util_context, master_princ, &scratch);
474 	if (retval) {
475 		com_err(progname, retval,
476 		    gettext("while calculated master key salt"));
477 	    return(1);
478 	}
479 
480 	/* If no encryption type is set, use the default */
481 	if (global_params.enctype == ENCTYPE_UNKNOWN) {
482 	    global_params.enctype = DEFAULT_KDC_ENCTYPE;
483 	    if (!krb5_c_valid_enctype(global_params.enctype))
484 		com_err(progname, KRB5_PROG_KEYTYPE_NOSUPP,
485 			gettext("while setting up enctype %d"),
486 			global_params.enctype);
487 	}
488 
489 	retval = krb5_c_string_to_key(util_context,
490 				global_params.enctype,
491 				&pwd, &scratch, &master_key);
492 	if (retval) {
493 	    com_err(progname, retval,
494 		gettext("while transforming master key from password"));
495 	    return(1);
496 	}
497 	free(scratch.data);
498 	mkey_password = 0;
499     } else if ((retval = krb5_db_fetch_mkey(util_context, master_princ,
500 					    global_params.enctype,
501 					    manual_mkey, FALSE,
502 					    global_params.stash_file,
503 					    0, &master_key))) {
504 	com_err(progname, retval,
505 	    gettext("while reading master key"));
506 	com_err(progname, 0,
507 	    gettext("Warning: proceeding without master key"));
508 	/*
509 	 * Solaris Kerberos: We don't want to count as an error if for instance
510 	 * the stash file is not present and we are trying to automate
511 	 * propagation, which really doesn't need a master key to do so.
512 	 */
513 	if (retval != KRB5_KDB_CANTREAD_STORED)
514 		exit_status++;
515 	return(0);
516     }
517     if ((retval = krb5_db_verify_master_key(util_context, master_princ,
518 		&master_key))) {
519 	com_err(progname, retval,
520 		gettext("while verifying master key"));
521 	exit_status++;
522 	krb5_free_keyblock_contents(util_context, &master_key);
523 	return(1);
524     }
525 
526     seed.length = master_key.length;
527     seed.data = (char *)master_key.contents;
528 
529     if ((retval = krb5_c_random_seed(util_context, &seed))) {
530 	com_err(progname, retval,
531 		gettext("while initializing random key generator"));
532 	exit_status++;
533 	krb5_free_keyblock_contents(util_context, &master_key);
534 	return(1);
535     }
536 
537     valid_master_key = 1;
538     dbactive = TRUE;
539     return 0;
540 }
541 
542 #ifdef HAVE_GETCWD
543 #undef getwd
544 #endif
545 
546 int
547 quit()
548 {
549     krb5_error_code retval;
550     static krb5_boolean finished = 0;
551 
552     if (finished)
553 	return 0;
554     retval = krb5_db_fini(util_context);
555     krb5_free_keyblock_contents(util_context, &master_key);
556     finished = TRUE;
557     krb5_free_context(util_context);
558     if (retval && retval != KRB5_KDB_DBNOTINITED) {
559 		com_err(progname, retval, gettext("while closing database"));
560 	exit_status++;
561 	return 1;
562     }
563     return 0;
564 }
565 
566 static void
567 add_random_key(argc, argv)
568     int argc;
569     char **argv;
570 {
571     krb5_error_code ret;
572     krb5_principal princ;
573     krb5_db_entry dbent;
574     int n;
575     krb5_boolean more;
576     krb5_timestamp now;
577 
578     krb5_key_salt_tuple *keysalts = NULL;
579     krb5_int32 num_keysalts = 0;
580 
581     int free_keysalts;
582     char *me = argv[0];
583     char *ks_str = NULL;
584     char *pr_str;
585 
586     if (argc < 2)
587 	usage();
588     for (argv++, argc--; *argv; argv++, argc--) {
589 	if (!strcmp(*argv, "-e")) {
590 	    argv++; argc--;
591 	    ks_str = *argv;
592 	    continue;
593 	} else
594 	    break;
595     }
596     if (argc < 1)
597 	usage();
598     pr_str = *argv;
599     ret = krb5_parse_name(util_context, pr_str, &princ);
600     if (ret) {
601 	com_err(me, ret, gettext("while parsing principal name %s"), pr_str);
602 	exit_status++;
603 	return;
604     }
605     n = 1;
606     ret = krb5_db_get_principal(util_context, princ, &dbent,
607 				&n, &more);
608     if (ret) {
609 	com_err(me, ret, gettext("while fetching principal %s"), pr_str);
610 	exit_status++;
611 	return;
612     }
613     if (n != 1) {
614 	fprintf(stderr, gettext("principal %s not found\n"), pr_str);
615 	exit_status++;
616 	return;
617     }
618     if (more) {
619 	fprintf(stderr, gettext("principal %s not unique\n"), pr_str);
620 	krb5_db_free_principal(util_context, &dbent, 1);
621 	exit_status++;
622 	return;
623     }
624     ret = krb5_string_to_keysalts(ks_str,
625 				  ", \t", ":.-", 0,
626 				  &keysalts,
627 				  &num_keysalts);
628     if (ret) {
629 	com_err(me, ret, gettext("while parsing keysalts %s"), ks_str);
630 	exit_status++;
631 	return;
632     }
633     if (!num_keysalts || keysalts == NULL) {
634 	num_keysalts = global_params.num_keysalts;
635 	keysalts = global_params.keysalts;
636 	free_keysalts = 0;
637     } else
638 	free_keysalts = 1;
639     ret = krb5_dbe_ark(util_context, &master_key,
640 		       keysalts, num_keysalts,
641 		       &dbent);
642     if (free_keysalts)
643 	free(keysalts);
644     if (ret) {
645 	com_err(me, ret, gettext("while randomizing principal %s"), pr_str);
646 	krb5_db_free_principal(util_context, &dbent, 1);
647 	exit_status++;
648 	return;
649     }
650     dbent.attributes &= ~KRB5_KDB_REQUIRES_PWCHANGE;
651     ret = krb5_timeofday(util_context, &now);
652     if (ret) {
653 	com_err(me, ret, gettext("while getting time"));
654 	krb5_db_free_principal(util_context, &dbent, 1);
655 	exit_status++;
656 	return;
657     }
658     ret = krb5_dbe_update_last_pwd_change(util_context, &dbent, now);
659     if (ret) {
660 	com_err(me, ret, gettext("while setting changetime"));
661 	krb5_db_free_principal(util_context, &dbent, 1);
662 	exit_status++;
663 	return;
664     }
665     ret = krb5_db_put_principal(util_context, &dbent, &n);
666     krb5_db_free_principal(util_context, &dbent, 1);
667     if (ret) {
668 	com_err(me, ret, gettext("while saving principal %s"), pr_str);
669 	exit_status++;
670 	return;
671     }
672     printf("%s changed\n", pr_str);
673 }
674