1c19800e8SDoug Rabson /*
2ae771770SStanislav Sedov * Copyright (c) 2004 Kungliga Tekniska Högskolan
3c19800e8SDoug Rabson * (Royal Institute of Technology, Stockholm, Sweden).
4c19800e8SDoug Rabson * All rights reserved.
5c19800e8SDoug Rabson *
6ae771770SStanislav Sedov * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7ae771770SStanislav Sedov *
8c19800e8SDoug Rabson * Redistribution and use in source and binary forms, with or without
9c19800e8SDoug Rabson * modification, are permitted provided that the following conditions
10c19800e8SDoug Rabson * are met:
11c19800e8SDoug Rabson *
12c19800e8SDoug Rabson * 1. Redistributions of source code must retain the above copyright
13c19800e8SDoug Rabson * notice, this list of conditions and the following disclaimer.
14c19800e8SDoug Rabson *
15c19800e8SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
16c19800e8SDoug Rabson * notice, this list of conditions and the following disclaimer in the
17c19800e8SDoug Rabson * documentation and/or other materials provided with the distribution.
18c19800e8SDoug Rabson *
19c19800e8SDoug Rabson * 3. Neither the name of the Institute nor the names of its contributors
20c19800e8SDoug Rabson * may be used to endorse or promote products derived from this software
21c19800e8SDoug Rabson * without specific prior written permission.
22c19800e8SDoug Rabson *
23c19800e8SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24c19800e8SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25c19800e8SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26c19800e8SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27c19800e8SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28c19800e8SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29c19800e8SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30c19800e8SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31c19800e8SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32c19800e8SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33c19800e8SDoug Rabson * SUCH DAMAGE.
34c19800e8SDoug Rabson */
35c19800e8SDoug Rabson
36c19800e8SDoug Rabson #include "kadmin_locl.h"
37c19800e8SDoug Rabson #include "kadmin-commands.h"
38c19800e8SDoug Rabson
39c19800e8SDoug Rabson extern int local_flag;
40c19800e8SDoug Rabson
41c19800e8SDoug Rabson int
stash(struct stash_options * opt,int argc,char ** argv)42c19800e8SDoug Rabson stash(struct stash_options *opt, int argc, char **argv)
43c19800e8SDoug Rabson {
44c19800e8SDoug Rabson char buf[1024];
45c19800e8SDoug Rabson krb5_error_code ret;
46c19800e8SDoug Rabson krb5_enctype enctype;
47c19800e8SDoug Rabson hdb_master_key mkey;
48c19800e8SDoug Rabson
49c19800e8SDoug Rabson if(!local_flag) {
50c19800e8SDoug Rabson krb5_warnx(context, "stash is only available in local (-l) mode");
51c19800e8SDoug Rabson return 0;
52c19800e8SDoug Rabson }
53c19800e8SDoug Rabson
54c19800e8SDoug Rabson ret = krb5_string_to_enctype(context, opt->enctype_string, &enctype);
55c19800e8SDoug Rabson if(ret) {
56c19800e8SDoug Rabson krb5_warn(context, ret, "%s", opt->enctype_string);
57c19800e8SDoug Rabson return 0;
58c19800e8SDoug Rabson }
59c19800e8SDoug Rabson
60c19800e8SDoug Rabson if(opt->key_file_string == NULL) {
61c19800e8SDoug Rabson asprintf(&opt->key_file_string, "%s/m-key", hdb_db_dir(context));
62c19800e8SDoug Rabson if (opt->key_file_string == NULL)
63c19800e8SDoug Rabson errx(1, "out of memory");
64c19800e8SDoug Rabson }
65c19800e8SDoug Rabson
66c19800e8SDoug Rabson ret = hdb_read_master_key(context, opt->key_file_string, &mkey);
67c19800e8SDoug Rabson if(ret && ret != ENOENT) {
68c19800e8SDoug Rabson krb5_warn(context, ret, "reading master key from %s",
69c19800e8SDoug Rabson opt->key_file_string);
70c19800e8SDoug Rabson return 0;
71c19800e8SDoug Rabson }
72c19800e8SDoug Rabson
73c19800e8SDoug Rabson if (opt->convert_file_flag) {
74c19800e8SDoug Rabson if (ret)
75c19800e8SDoug Rabson krb5_warn(context, ret, "reading master key from %s",
76c19800e8SDoug Rabson opt->key_file_string);
77c19800e8SDoug Rabson return 0;
78c19800e8SDoug Rabson } else {
79c19800e8SDoug Rabson krb5_keyblock key;
80c19800e8SDoug Rabson krb5_salt salt;
81c19800e8SDoug Rabson salt.salttype = KRB5_PW_SALT;
82c19800e8SDoug Rabson /* XXX better value? */
83c19800e8SDoug Rabson salt.saltvalue.data = NULL;
84c19800e8SDoug Rabson salt.saltvalue.length = 0;
85c19800e8SDoug Rabson if(opt->master_key_fd_integer != -1) {
86c19800e8SDoug Rabson ssize_t n;
87c19800e8SDoug Rabson n = read(opt->master_key_fd_integer, buf, sizeof(buf));
88c19800e8SDoug Rabson if(n == 0)
89c19800e8SDoug Rabson krb5_warnx(context, "end of file reading passphrase");
90ae771770SStanislav Sedov else if(n < 0) {
91c19800e8SDoug Rabson krb5_warn(context, errno, "reading passphrase");
92ae771770SStanislav Sedov n = 0;
93ae771770SStanislav Sedov }
94c19800e8SDoug Rabson buf[n] = '\0';
95c19800e8SDoug Rabson buf[strcspn(buf, "\r\n")] = '\0';
96ae771770SStanislav Sedov } else if (opt->random_password_flag) {
97ae771770SStanislav Sedov random_password (buf, sizeof(buf));
98ae771770SStanislav Sedov printf("Using random master stash password: %s\n", buf);
99c19800e8SDoug Rabson } else {
100c19800e8SDoug Rabson if(UI_UTIL_read_pw_string(buf, sizeof(buf), "Master key: ", 1)) {
101c19800e8SDoug Rabson hdb_free_master_key(context, mkey);
102c19800e8SDoug Rabson return 0;
103c19800e8SDoug Rabson }
104c19800e8SDoug Rabson }
105c19800e8SDoug Rabson ret = krb5_string_to_key_salt(context, enctype, buf, salt, &key);
106*ed549cb0SCy Schubert if (ret == 0)
107c19800e8SDoug Rabson ret = hdb_add_master_key(context, &key, &mkey);
108*ed549cb0SCy Schubert if (ret)
109*ed549cb0SCy Schubert krb5_warn(context, errno, "setting master key");
110c19800e8SDoug Rabson krb5_free_keyblock_contents(context, &key);
111c19800e8SDoug Rabson }
112c19800e8SDoug Rabson
113c19800e8SDoug Rabson {
114c19800e8SDoug Rabson char *new, *old;
115c19800e8SDoug Rabson asprintf(&old, "%s.old", opt->key_file_string);
116c19800e8SDoug Rabson asprintf(&new, "%s.new", opt->key_file_string);
117c19800e8SDoug Rabson if(old == NULL || new == NULL) {
118c19800e8SDoug Rabson ret = ENOMEM;
119c19800e8SDoug Rabson goto out;
120c19800e8SDoug Rabson }
121c19800e8SDoug Rabson
122c19800e8SDoug Rabson if(unlink(new) < 0 && errno != ENOENT) {
123c19800e8SDoug Rabson ret = errno;
124c19800e8SDoug Rabson goto out;
125c19800e8SDoug Rabson }
126c19800e8SDoug Rabson krb5_warnx(context, "writing key to \"%s\"", opt->key_file_string);
127c19800e8SDoug Rabson ret = hdb_write_master_key(context, new, mkey);
128c19800e8SDoug Rabson if(ret)
129c19800e8SDoug Rabson unlink(new);
130c19800e8SDoug Rabson else {
131c19800e8SDoug Rabson unlink(old);
132ae771770SStanislav Sedov #ifndef NO_POSIX_LINKS
133c19800e8SDoug Rabson if(link(opt->key_file_string, old) < 0 && errno != ENOENT) {
134c19800e8SDoug Rabson ret = errno;
135c19800e8SDoug Rabson unlink(new);
136ae771770SStanislav Sedov } else {
137ae771770SStanislav Sedov #endif
138ae771770SStanislav Sedov if(rename(new, opt->key_file_string) < 0) {
139c19800e8SDoug Rabson ret = errno;
140c19800e8SDoug Rabson }
141ae771770SStanislav Sedov #ifndef NO_POSIX_LINKS
142ae771770SStanislav Sedov }
143ae771770SStanislav Sedov #endif
144c19800e8SDoug Rabson }
145c19800e8SDoug Rabson out:
146c19800e8SDoug Rabson free(old);
147c19800e8SDoug Rabson free(new);
148c19800e8SDoug Rabson if(ret)
149c19800e8SDoug Rabson krb5_warn(context, errno, "writing master key file");
150c19800e8SDoug Rabson }
151c19800e8SDoug Rabson
152c19800e8SDoug Rabson hdb_free_master_key(context, mkey);
153c19800e8SDoug Rabson return 0;
154c19800e8SDoug Rabson }
155