xref: /onnv-gate/usr/src/cmd/krb5/kadmin/dbutil/kdb5_stash.c (revision 8092:19771b16f0a8)
10Sstevel@tonic-gate /*
27934SMark.Phalan@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  *	Openvision retains the copyright to derivative works of
100Sstevel@tonic-gate  *	this source code.  Do *NOT* create a derivative of this
110Sstevel@tonic-gate  *	source code before consulting with your legal department.
120Sstevel@tonic-gate  *	Do *NOT* integrate *ANY* of this source code into another
130Sstevel@tonic-gate  *	product before consulting with your legal department.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  *	For further information, read the top-level Openvision
160Sstevel@tonic-gate  *	copyright which is contained in the top-level MIT Kerberos
170Sstevel@tonic-gate  *	copyright.
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
230Sstevel@tonic-gate 
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate  * admin/stash/kdb5_stash.c
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
280Sstevel@tonic-gate  * All Rights Reserved.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * Export of this software from the United States of America may
310Sstevel@tonic-gate  *   require a specific license from the United States Government.
320Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
330Sstevel@tonic-gate  *   export to obtain such a license before exporting.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
360Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
370Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
380Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
390Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
400Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
410Sstevel@tonic-gate  * to distribution of the software without specific, written prior
420Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
430Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
440Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
450Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
460Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
470Sstevel@tonic-gate  * or implied warranty.
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * Store the master database key in a file.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
532881Smp153739 /*
542881Smp153739  * Copyright (C) 1998 by the FundsXpress, INC.
552881Smp153739  *
562881Smp153739  * All rights reserved.
572881Smp153739  *
582881Smp153739  * Export of this software from the United States of America may require
592881Smp153739  * a specific license from the United States Government.  It is the
602881Smp153739  * responsibility of any person or organization contemplating export to
612881Smp153739  * obtain such a license before exporting.
622881Smp153739  *
632881Smp153739  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
642881Smp153739  * distribute this software and its documentation for any purpose and
652881Smp153739  * without fee is hereby granted, provided that the above copyright
662881Smp153739  * notice appear in all copies and that both that copyright notice and
672881Smp153739  * this permission notice appear in supporting documentation, and that
682881Smp153739  * the name of FundsXpress. not be used in advertising or publicity pertaining
692881Smp153739  * to distribution of the software without specific, written prior
702881Smp153739  * permission.  FundsXpress makes no representations about the suitability of
712881Smp153739  * this software for any purpose.  It is provided "as is" without express
722881Smp153739  * or implied warranty.
732881Smp153739  *
742881Smp153739  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
752881Smp153739  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
762881Smp153739  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
772881Smp153739  */
782881Smp153739 
797934SMark.Phalan@Sun.COM #include "k5-int.h"
800Sstevel@tonic-gate #include <kadm5/admin.h>
810Sstevel@tonic-gate #include "com_err.h"
820Sstevel@tonic-gate #include <kadm5/admin.h>
830Sstevel@tonic-gate #include <stdio.h>
840Sstevel@tonic-gate #include <libintl.h>
852881Smp153739 #include "kdb5_util.h"
860Sstevel@tonic-gate 
870Sstevel@tonic-gate extern krb5_principal master_princ;
880Sstevel@tonic-gate extern kadm5_config_params global_params;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate extern int exit_status;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate void
kdb5_stash(argc,argv)930Sstevel@tonic-gate kdb5_stash(argc, argv)
942881Smp153739     int argc;
952881Smp153739     char *argv[];
960Sstevel@tonic-gate {
970Sstevel@tonic-gate     extern char *optarg;
980Sstevel@tonic-gate     extern int optind;
990Sstevel@tonic-gate     int optchar;
1000Sstevel@tonic-gate     krb5_error_code retval;
1010Sstevel@tonic-gate     char *dbname = (char *) NULL;
1020Sstevel@tonic-gate     char *realm = 0;
1030Sstevel@tonic-gate     char *mkey_name = 0;
1040Sstevel@tonic-gate     char *mkey_fullname;
1050Sstevel@tonic-gate     char *keyfile = 0;
1060Sstevel@tonic-gate     krb5_context context;
1070Sstevel@tonic-gate     krb5_keyblock mkey;
1080Sstevel@tonic-gate 
109*8092SMark.Phalan@Sun.COM /* Solaris Kerberos */
110*8092SMark.Phalan@Sun.COM #if 0
1110Sstevel@tonic-gate     if (strrchr(argv[0], '/'))
1120Sstevel@tonic-gate 	argv[0] = strrchr(argv[0], '/')+1;
113*8092SMark.Phalan@Sun.COM #endif
1144960Swillf     retval = kadm5_init_krb5_context(&context);
1154960Swillf     if( retval )
1164960Swillf     {
117*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
118*8092SMark.Phalan@Sun.COM 	com_err(progname, retval, "while initializing krb5_context");
1194960Swillf 	exit(1);
1204960Swillf     }
1210Sstevel@tonic-gate 
1224960Swillf     if ((retval = krb5_set_default_realm(context,
1234960Swillf 					  util_context->default_realm))) {
124*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
125*8092SMark.Phalan@Sun.COM 	com_err(progname, retval, "while setting default realm name");
1264960Swillf 	exit(1);
1274960Swillf     }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate     dbname = global_params.dbname;
1300Sstevel@tonic-gate     realm = global_params.realm;
1310Sstevel@tonic-gate     mkey_name = global_params.mkey_name;
1320Sstevel@tonic-gate     keyfile = global_params.stash_file;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate     optind = 1;
1350Sstevel@tonic-gate     while ((optchar = getopt(argc, argv, "f:")) != -1) {
1360Sstevel@tonic-gate 	switch(optchar) {
1370Sstevel@tonic-gate 	case 'f':
1380Sstevel@tonic-gate 	    keyfile = optarg;
1390Sstevel@tonic-gate 	    break;
1400Sstevel@tonic-gate 	case '?':
1410Sstevel@tonic-gate 	default:
1420Sstevel@tonic-gate 	    usage();
1430Sstevel@tonic-gate 	    return;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate     }
1460Sstevel@tonic-gate 
147781Sgtb     if (!krb5_c_valid_enctype(global_params.enctype)) {
1480Sstevel@tonic-gate 	char tmp[32];
1490Sstevel@tonic-gate 	if (krb5_enctype_to_string(global_params.enctype,
1500Sstevel@tonic-gate 					    tmp, sizeof (tmp)))
151*8092SMark.Phalan@Sun.COM 	    /* Solaris Kerberos */
152*8092SMark.Phalan@Sun.COM 	    com_err(progname, KRB5_PROG_KEYTYPE_NOSUPP,
1530Sstevel@tonic-gate 		gettext("while setting up enctype %d"),
1540Sstevel@tonic-gate 		global_params.enctype);
155*8092SMark.Phalan@Sun.COM 	else {
156*8092SMark.Phalan@Sun.COM 	    /* Solaris Kerberos */
157*8092SMark.Phalan@Sun.COM 	    com_err(progname, KRB5_PROG_KEYTYPE_NOSUPP, tmp);
158*8092SMark.Phalan@Sun.COM 	}
1592881Smp153739 	exit_status++; return;
1600Sstevel@tonic-gate     }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate     /* assemble & parse the master key name */
1632881Smp153739     retval = krb5_db_setup_mkey_name(context, mkey_name, realm,
1642881Smp153739 				     &mkey_fullname, &master_princ);
1652881Smp153739     if (retval) {
166*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
167*8092SMark.Phalan@Sun.COM 	com_err(progname, retval,
1680Sstevel@tonic-gate 		gettext("while setting up master key name"));
1692881Smp153739 	exit_status++; return;
1700Sstevel@tonic-gate     }
1712881Smp153739 
1724960Swillf     retval = krb5_db_open(context, db5util_db_args,
1734960Swillf 			  KRB5_KDB_OPEN_RW | KRB5_KDB_SRV_TYPE_OTHER);
1742881Smp153739     if (retval) {
175*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
176*8092SMark.Phalan@Sun.COM 	com_err(progname, retval,
1770Sstevel@tonic-gate 		gettext("while initializing the database '%s'"),
1782881Smp153739 		dbname);
1792881Smp153739 	exit_status++; return;
1800Sstevel@tonic-gate     }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate     /* TRUE here means read the keyboard, but only once */
1832881Smp153739     retval = krb5_db_fetch_mkey(context, master_princ,
1842881Smp153739 				global_params.enctype,
1852881Smp153739 				TRUE, FALSE, (char *) NULL,
1862881Smp153739 				0, &mkey);
1872881Smp153739     if (retval) {
188*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
189*8092SMark.Phalan@Sun.COM 	com_err(progname, retval, gettext("while reading master key"));
1900Sstevel@tonic-gate 	(void) krb5_db_fini(context);
1912881Smp153739 	exit_status++; return;
1920Sstevel@tonic-gate     }
1932881Smp153739 
1942881Smp153739     retval = krb5_db_verify_master_key(context, master_princ, &mkey);
1952881Smp153739     if (retval) {
196*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
197*8092SMark.Phalan@Sun.COM 	com_err(progname, retval, gettext("while verifying master key"));
1980Sstevel@tonic-gate 	krb5_free_keyblock_contents(context, &mkey);
1990Sstevel@tonic-gate 	(void) krb5_db_fini(context);
2002881Smp153739 	exit_status++; return;
2010Sstevel@tonic-gate     }
2022881Smp153739 
2034960Swillf     retval = krb5_db_store_master_key(context, keyfile, master_princ,
2044960Swillf 				    &mkey, NULL);
2052881Smp153739     if (retval) {
206*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
207*8092SMark.Phalan@Sun.COM 	com_err(progname, errno, gettext("while storing key"));
2080Sstevel@tonic-gate 	krb5_free_keyblock_contents(context, &mkey);
2090Sstevel@tonic-gate 	(void) krb5_db_fini(context);
2102881Smp153739 	exit_status++; return;
2110Sstevel@tonic-gate     }
2120Sstevel@tonic-gate     krb5_free_keyblock_contents(context, &mkey);
2132881Smp153739 
2142881Smp153739     retval = krb5_db_fini(context);
2152881Smp153739     if (retval) {
216*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
217*8092SMark.Phalan@Sun.COM 	com_err(progname, retval,
2180Sstevel@tonic-gate 		gettext("closing database '%s'"), dbname);
2192881Smp153739 	exit_status++; return;
2200Sstevel@tonic-gate     }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate     krb5_free_context(context);
2230Sstevel@tonic-gate     exit_status = 0;
2242881Smp153739     return;
2250Sstevel@tonic-gate }
226