xref: /freebsd/crypto/krb5/src/kadmin/dbutil/kdb5_stash.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* kadmin/dbutil/kdb5_stash.c - Store the master database key in a file */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright 1990 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All Rights Reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert  *   require a specific license from the United States Government.
9*7f2fe78bSCy Schubert  *   It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert  *   export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert  *
12*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
24*7f2fe78bSCy Schubert  * or implied warranty.
25*7f2fe78bSCy Schubert  */
26*7f2fe78bSCy Schubert /*
27*7f2fe78bSCy Schubert  * Copyright (C) 1998 by the FundsXpress, INC.
28*7f2fe78bSCy Schubert  *
29*7f2fe78bSCy Schubert  * All rights reserved.
30*7f2fe78bSCy Schubert  *
31*7f2fe78bSCy Schubert  * Export of this software from the United States of America may require
32*7f2fe78bSCy Schubert  * a specific license from the United States Government.  It is the
33*7f2fe78bSCy Schubert  * responsibility of any person or organization contemplating export to
34*7f2fe78bSCy Schubert  * obtain such a license before exporting.
35*7f2fe78bSCy Schubert  *
36*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
37*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
38*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
39*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
40*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
41*7f2fe78bSCy Schubert  * the name of FundsXpress. not be used in advertising or publicity pertaining
42*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
43*7f2fe78bSCy Schubert  * permission.  FundsXpress makes no representations about the suitability of
44*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
45*7f2fe78bSCy Schubert  * or implied warranty.
46*7f2fe78bSCy Schubert  *
47*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48*7f2fe78bSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49*7f2fe78bSCy Schubert  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50*7f2fe78bSCy Schubert  */
51*7f2fe78bSCy Schubert 
52*7f2fe78bSCy Schubert #include "k5-int.h"
53*7f2fe78bSCy Schubert #include "com_err.h"
54*7f2fe78bSCy Schubert #include <kadm5/admin.h>
55*7f2fe78bSCy Schubert #include <stdio.h>
56*7f2fe78bSCy Schubert #include "kdb5_util.h"
57*7f2fe78bSCy Schubert 
58*7f2fe78bSCy Schubert extern krb5_keyblock master_keyblock;
59*7f2fe78bSCy Schubert extern krb5_principal master_princ;
60*7f2fe78bSCy Schubert extern kadm5_config_params global_params;
61*7f2fe78bSCy Schubert 
62*7f2fe78bSCy Schubert extern int exit_status;
63*7f2fe78bSCy Schubert extern int close_policy_db;
64*7f2fe78bSCy Schubert 
65*7f2fe78bSCy Schubert void
kdb5_stash(argc,argv)66*7f2fe78bSCy Schubert kdb5_stash(argc, argv)
67*7f2fe78bSCy Schubert     int argc;
68*7f2fe78bSCy Schubert     char *argv[];
69*7f2fe78bSCy Schubert {
70*7f2fe78bSCy Schubert     extern char *optarg;
71*7f2fe78bSCy Schubert     extern int optind;
72*7f2fe78bSCy Schubert     int optchar;
73*7f2fe78bSCy Schubert     krb5_error_code retval;
74*7f2fe78bSCy Schubert     char *keyfile = 0;
75*7f2fe78bSCy Schubert     krb5_kvno mkey_kvno;
76*7f2fe78bSCy Schubert 
77*7f2fe78bSCy Schubert     keyfile = global_params.stash_file;
78*7f2fe78bSCy Schubert 
79*7f2fe78bSCy Schubert     optind = 1;
80*7f2fe78bSCy Schubert     while ((optchar = getopt(argc, argv, "f:")) != -1) {
81*7f2fe78bSCy Schubert         switch(optchar) {
82*7f2fe78bSCy Schubert         case 'f':
83*7f2fe78bSCy Schubert             keyfile = optarg;
84*7f2fe78bSCy Schubert             break;
85*7f2fe78bSCy Schubert         case '?':
86*7f2fe78bSCy Schubert         default:
87*7f2fe78bSCy Schubert             usage();
88*7f2fe78bSCy Schubert             return;
89*7f2fe78bSCy Schubert         }
90*7f2fe78bSCy Schubert     }
91*7f2fe78bSCy Schubert 
92*7f2fe78bSCy Schubert     if (!krb5_c_valid_enctype(master_keyblock.enctype)) {
93*7f2fe78bSCy Schubert         char tmp[32];
94*7f2fe78bSCy Schubert         if (krb5_enctype_to_name(master_keyblock.enctype, FALSE,
95*7f2fe78bSCy Schubert                                  tmp, sizeof(tmp)))
96*7f2fe78bSCy Schubert             com_err(progname, KRB5_PROG_KEYTYPE_NOSUPP,
97*7f2fe78bSCy Schubert                     _("while setting up enctype %d"), master_keyblock.enctype);
98*7f2fe78bSCy Schubert         else
99*7f2fe78bSCy Schubert             com_err(progname, KRB5_PROG_KEYTYPE_NOSUPP, "%s", tmp);
100*7f2fe78bSCy Schubert         exit_status++; return;
101*7f2fe78bSCy Schubert     }
102*7f2fe78bSCy Schubert 
103*7f2fe78bSCy Schubert     if (global_params.mask & KADM5_CONFIG_KVNO)
104*7f2fe78bSCy Schubert         mkey_kvno = global_params.kvno; /* user specified */
105*7f2fe78bSCy Schubert     else
106*7f2fe78bSCy Schubert         mkey_kvno = IGNORE_VNO; /* use whatever krb5_db_fetch_mkey finds */
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert     if (!valid_master_key) {
109*7f2fe78bSCy Schubert         /* TRUE here means read the keyboard, but only once */
110*7f2fe78bSCy Schubert         retval = krb5_db_fetch_mkey(util_context, master_princ,
111*7f2fe78bSCy Schubert                                     master_keyblock.enctype,
112*7f2fe78bSCy Schubert                                     TRUE, FALSE, (char *) NULL,
113*7f2fe78bSCy Schubert                                     &mkey_kvno,
114*7f2fe78bSCy Schubert                                     NULL, &master_keyblock);
115*7f2fe78bSCy Schubert         if (retval) {
116*7f2fe78bSCy Schubert             com_err(progname, retval, _("while reading master key"));
117*7f2fe78bSCy Schubert             exit_status++; return;
118*7f2fe78bSCy Schubert         }
119*7f2fe78bSCy Schubert 
120*7f2fe78bSCy Schubert         retval = krb5_db_fetch_mkey_list(util_context, master_princ,
121*7f2fe78bSCy Schubert                                          &master_keyblock);
122*7f2fe78bSCy Schubert         if (retval) {
123*7f2fe78bSCy Schubert             com_err(progname, retval, _("while getting master key list"));
124*7f2fe78bSCy Schubert             exit_status++; return;
125*7f2fe78bSCy Schubert         }
126*7f2fe78bSCy Schubert     } else {
127*7f2fe78bSCy Schubert         printf(_("Using existing stashed keys to update stash file.\n"));
128*7f2fe78bSCy Schubert     }
129*7f2fe78bSCy Schubert 
130*7f2fe78bSCy Schubert     retval = krb5_db_store_master_key_list(util_context, keyfile, master_princ,
131*7f2fe78bSCy Schubert                                            NULL);
132*7f2fe78bSCy Schubert     if (retval) {
133*7f2fe78bSCy Schubert         com_err(progname, retval, _("while storing key"));
134*7f2fe78bSCy Schubert         exit_status++; return;
135*7f2fe78bSCy Schubert     }
136*7f2fe78bSCy Schubert 
137*7f2fe78bSCy Schubert     exit_status = 0;
138*7f2fe78bSCy Schubert     return;
139*7f2fe78bSCy Schubert }
140