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