No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kadmin / stash.c
blob76fde2864db0e36ebc40c4aed7b32d05278c76a5
1 /*
2 * Copyright (c) 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kadmin_locl.h"
35 #include "kadmin-commands.h"
37 __RCSID("$Heimdal: stash.c 22251 2007-12-09 05:58:43Z lha $"
38 "$NetBSD: stash.c,v 1.1 2008/03/22 08:37:02 mlelstv Exp $");
40 extern int local_flag;
42 int
43 stash(struct stash_options *opt, int argc, char **argv)
45 char buf[1024];
46 krb5_error_code ret;
47 krb5_enctype enctype;
48 hdb_master_key mkey;
50 if(!local_flag) {
51 krb5_warnx(context, "stash is only available in local (-l) mode");
52 return 0;
55 ret = krb5_string_to_enctype(context, opt->enctype_string, &enctype);
56 if(ret) {
57 krb5_warn(context, ret, "%s", opt->enctype_string);
58 return 0;
61 if(opt->key_file_string == NULL) {
62 asprintf(&opt->key_file_string, "%s/m-key", hdb_db_dir(context));
63 if (opt->key_file_string == NULL)
64 errx(1, "out of memory");
67 ret = hdb_read_master_key(context, opt->key_file_string, &mkey);
68 if(ret && ret != ENOENT) {
69 krb5_warn(context, ret, "reading master key from %s",
70 opt->key_file_string);
71 return 0;
74 if (opt->convert_file_flag) {
75 if (ret)
76 krb5_warn(context, ret, "reading master key from %s",
77 opt->key_file_string);
78 return 0;
79 } else {
80 krb5_keyblock key;
81 krb5_salt salt;
82 salt.salttype = KRB5_PW_SALT;
83 /* XXX better value? */
84 salt.saltvalue.data = NULL;
85 salt.saltvalue.length = 0;
86 if(opt->master_key_fd_integer != -1) {
87 ssize_t n;
88 n = read(opt->master_key_fd_integer, buf, sizeof(buf));
89 if(n == 0)
90 krb5_warnx(context, "end of file reading passphrase");
91 else if(n < 0) {
92 krb5_warn(context, errno, "reading passphrase");
93 n = 0;
95 buf[n] = '\0';
96 buf[strcspn(buf, "\r\n")] = '\0';
97 } else {
98 if(UI_UTIL_read_pw_string(buf, sizeof(buf), "Master key: ", 1)) {
99 hdb_free_master_key(context, mkey);
100 return 0;
103 ret = krb5_string_to_key_salt(context, enctype, buf, salt, &key);
104 ret = hdb_add_master_key(context, &key, &mkey);
105 krb5_free_keyblock_contents(context, &key);
109 char *new, *old;
110 asprintf(&old, "%s.old", opt->key_file_string);
111 asprintf(&new, "%s.new", opt->key_file_string);
112 if(old == NULL || new == NULL) {
113 ret = ENOMEM;
114 goto out;
117 if(unlink(new) < 0 && errno != ENOENT) {
118 ret = errno;
119 goto out;
121 krb5_warnx(context, "writing key to \"%s\"", opt->key_file_string);
122 ret = hdb_write_master_key(context, new, mkey);
123 if(ret)
124 unlink(new);
125 else {
126 unlink(old);
127 if(link(opt->key_file_string, old) < 0 && errno != ENOENT) {
128 ret = errno;
129 unlink(new);
130 } else if(rename(new, opt->key_file_string) < 0) {
131 ret = errno;
134 out:
135 free(old);
136 free(new);
137 if(ret)
138 krb5_warn(context, errno, "writing master key file");
141 hdb_free_master_key(context, mkey);
142 return 0;