1*0a6a1f1dSLionel Sambuc /* $NetBSD: kstash.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "headers.h"
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc krb5_context context;
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc static char *keyfile;
41ebfedea0SLionel Sambuc static int convert_flag;
42ebfedea0SLionel Sambuc static int help_flag;
43ebfedea0SLionel Sambuc static int version_flag;
44ebfedea0SLionel Sambuc
45ebfedea0SLionel Sambuc static int master_key_fd = -1;
46ebfedea0SLionel Sambuc static int random_key_flag;
47ebfedea0SLionel Sambuc
48ebfedea0SLionel Sambuc static const char *enctype_str = "des3-cbc-sha1";
49ebfedea0SLionel Sambuc
50ebfedea0SLionel Sambuc static struct getargs args[] = {
51*0a6a1f1dSLionel Sambuc { "enctype", 'e', arg_string, rk_UNCONST(&enctype_str), "encryption type",
52*0a6a1f1dSLionel Sambuc NULL },
53ebfedea0SLionel Sambuc { "key-file", 'k', arg_string, &keyfile, "master key file", "file" },
54ebfedea0SLionel Sambuc { "convert-file", 0, arg_flag, &convert_flag,
55*0a6a1f1dSLionel Sambuc "just convert keyfile to new format", NULL },
56ebfedea0SLionel Sambuc { "master-key-fd", 0, arg_integer, &master_key_fd,
57ebfedea0SLionel Sambuc "filedescriptor to read passphrase from", "fd" },
58*0a6a1f1dSLionel Sambuc { "random-key", 0, arg_flag, &random_key_flag,
59*0a6a1f1dSLionel Sambuc "generate a random master key", NULL },
60*0a6a1f1dSLionel Sambuc { "help", 'h', arg_flag, &help_flag, NULL, NULL },
61*0a6a1f1dSLionel Sambuc { "version", 0, arg_flag, &version_flag, NULL, NULL }
62ebfedea0SLionel Sambuc };
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc int num_args = sizeof(args) / sizeof(args[0]);
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc int
main(int argc,char ** argv)67ebfedea0SLionel Sambuc main(int argc, char **argv)
68ebfedea0SLionel Sambuc {
69ebfedea0SLionel Sambuc char buf[1024];
70ebfedea0SLionel Sambuc krb5_error_code ret;
71ebfedea0SLionel Sambuc
72ebfedea0SLionel Sambuc krb5_enctype enctype;
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc hdb_master_key mkey;
75ebfedea0SLionel Sambuc
76ebfedea0SLionel Sambuc krb5_program_setup(&context, argc, argv, args, num_args, NULL);
77ebfedea0SLionel Sambuc
78ebfedea0SLionel Sambuc if(help_flag)
79ebfedea0SLionel Sambuc krb5_std_usage(0, args, num_args);
80ebfedea0SLionel Sambuc if(version_flag){
81ebfedea0SLionel Sambuc print_version(NULL);
82ebfedea0SLionel Sambuc exit(0);
83ebfedea0SLionel Sambuc }
84ebfedea0SLionel Sambuc
85ebfedea0SLionel Sambuc if (master_key_fd != -1 && random_key_flag)
86ebfedea0SLionel Sambuc krb5_errx(context, 1, "random-key and master-key-fd "
87ebfedea0SLionel Sambuc "is mutual exclusive");
88ebfedea0SLionel Sambuc
89ebfedea0SLionel Sambuc if (keyfile == NULL)
90ebfedea0SLionel Sambuc asprintf(&keyfile, "%s/m-key", hdb_db_dir(context));
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc ret = krb5_string_to_enctype(context, enctype_str, &enctype);
93ebfedea0SLionel Sambuc if(ret)
94ebfedea0SLionel Sambuc krb5_err(context, 1, ret, "krb5_string_to_enctype");
95ebfedea0SLionel Sambuc
96ebfedea0SLionel Sambuc ret = hdb_read_master_key(context, keyfile, &mkey);
97ebfedea0SLionel Sambuc if(ret && ret != ENOENT)
98ebfedea0SLionel Sambuc krb5_err(context, 1, ret, "reading master key from %s", keyfile);
99ebfedea0SLionel Sambuc
100ebfedea0SLionel Sambuc if (convert_flag) {
101ebfedea0SLionel Sambuc if (ret)
102ebfedea0SLionel Sambuc krb5_err(context, 1, ret, "reading master key from %s", keyfile);
103ebfedea0SLionel Sambuc } else {
104ebfedea0SLionel Sambuc krb5_keyblock key;
105ebfedea0SLionel Sambuc krb5_salt salt;
106ebfedea0SLionel Sambuc salt.salttype = KRB5_PW_SALT;
107ebfedea0SLionel Sambuc /* XXX better value? */
108ebfedea0SLionel Sambuc salt.saltvalue.data = NULL;
109ebfedea0SLionel Sambuc salt.saltvalue.length = 0;
110ebfedea0SLionel Sambuc if (random_key_flag) {
111ebfedea0SLionel Sambuc ret = krb5_generate_random_keyblock(context, enctype, &key);
112ebfedea0SLionel Sambuc if (ret)
113ebfedea0SLionel Sambuc krb5_err(context, 1, ret, "krb5_generate_random_keyblock");
114ebfedea0SLionel Sambuc
115ebfedea0SLionel Sambuc } else {
116ebfedea0SLionel Sambuc if(master_key_fd != -1) {
117ebfedea0SLionel Sambuc ssize_t n;
118ebfedea0SLionel Sambuc n = read(master_key_fd, buf, sizeof(buf));
119ebfedea0SLionel Sambuc if(n <= 0)
120ebfedea0SLionel Sambuc krb5_err(context, 1, errno, "failed to read passphrase");
121ebfedea0SLionel Sambuc buf[n] = '\0';
122ebfedea0SLionel Sambuc buf[strcspn(buf, "\r\n")] = '\0';
123ebfedea0SLionel Sambuc
124ebfedea0SLionel Sambuc } else {
125ebfedea0SLionel Sambuc if(UI_UTIL_read_pw_string(buf, sizeof(buf), "Master key: ", 1))
126ebfedea0SLionel Sambuc exit(1);
127ebfedea0SLionel Sambuc }
128ebfedea0SLionel Sambuc krb5_string_to_key_salt(context, enctype, buf, salt, &key);
129ebfedea0SLionel Sambuc }
130ebfedea0SLionel Sambuc ret = hdb_add_master_key(context, &key, &mkey);
131ebfedea0SLionel Sambuc
132ebfedea0SLionel Sambuc krb5_free_keyblock_contents(context, &key);
133ebfedea0SLionel Sambuc
134ebfedea0SLionel Sambuc }
135ebfedea0SLionel Sambuc
136ebfedea0SLionel Sambuc {
137ebfedea0SLionel Sambuc char *new, *old;
138ebfedea0SLionel Sambuc asprintf(&old, "%s.old", keyfile);
139ebfedea0SLionel Sambuc asprintf(&new, "%s.new", keyfile);
140ebfedea0SLionel Sambuc if(unlink(new) < 0 && errno != ENOENT) {
141ebfedea0SLionel Sambuc ret = errno;
142ebfedea0SLionel Sambuc goto out;
143ebfedea0SLionel Sambuc }
144ebfedea0SLionel Sambuc krb5_warnx(context, "writing key to `%s'", keyfile);
145ebfedea0SLionel Sambuc ret = hdb_write_master_key(context, new, mkey);
146ebfedea0SLionel Sambuc if(ret)
147ebfedea0SLionel Sambuc unlink(new);
148ebfedea0SLionel Sambuc else {
149ebfedea0SLionel Sambuc #ifndef NO_POSIX_LINKS
150ebfedea0SLionel Sambuc unlink(old);
151ebfedea0SLionel Sambuc if(link(keyfile, old) < 0 && errno != ENOENT) {
152ebfedea0SLionel Sambuc ret = errno;
153ebfedea0SLionel Sambuc unlink(new);
154ebfedea0SLionel Sambuc } else {
155ebfedea0SLionel Sambuc #endif
156ebfedea0SLionel Sambuc if(rename(new, keyfile) < 0) {
157ebfedea0SLionel Sambuc ret = errno;
158ebfedea0SLionel Sambuc }
159ebfedea0SLionel Sambuc #ifndef NO_POSIX_LINKS
160ebfedea0SLionel Sambuc }
161ebfedea0SLionel Sambuc #endif
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc out:
164ebfedea0SLionel Sambuc free(old);
165ebfedea0SLionel Sambuc free(new);
166ebfedea0SLionel Sambuc if(ret)
167ebfedea0SLionel Sambuc krb5_warn(context, errno, "writing master key file");
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc hdb_free_master_key(context, mkey);
171ebfedea0SLionel Sambuc
172ebfedea0SLionel Sambuc exit(ret != 0);
173ebfedea0SLionel Sambuc }
174