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