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