1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/*
* Unix crypt(1)-style interface.
* Written by T.E.Dickey for vile (March 1999).
*
* $Id: ucrypt.c,v 1.25 2018/10/25 22:50:17 tom Exp $
*/
#include "estruct.h"
#include "edef.h"
#if OPT_ENCRYPT
#include "vl_crypt.h"
/*
* Get the string to use as an encryption string.
*/
static int
get_encryption_key(char *key, /* where to write key */
size_t len)
{
int status; /* return status */
int save_vl_echo = vl_echo;
char temp[NKEYLEN];
/* turn command input echo off */
vl_echo = FALSE;
temp[0] = EOS;
status = mlreply("-Encryption String: ", temp, (UINT) (len - 1));
vl_echo = save_vl_echo;
if (status == TRUE)
vl_make_encrypt_key(key, temp);
mlerase();
return (status);
}
/*
* Set the buffer's encryption key if needed.
*/
int
vl_resetkey(BUFFER *bp,
const char *fname)
{
register int s; /* temporary return value */
/* flag the buffer as not having encryption */
ffdocrypt(FALSE);
/* if we want to encrypt */
if (b_val(bp, MDCRYPT)) {
char temp[NFILEN];
/* don't automatically inherit key from other buffers */
if (bp->b_cryptkey[0] != EOS
&& !b_is_argument(bp)
&& strcmp(lengthen_path(vl_strncpy(temp, fname, sizeof(temp))), bp->b_fname)) {
char prompt[80];
(void) lsprintf(prompt, "Use crypt-key from %s", bp->b_bname);
s = mlyesno(prompt);
if (s != TRUE)
return (s == FALSE);
}
/* make a key if we don't have one */
if (bp->b_cryptkey[0] == EOS) {
s = get_encryption_key(bp->b_cryptkey, sizeof(bp->b_cryptkey));
if (s != TRUE)
return (s == FALSE);
}
ffdocrypt(TRUE);
vl_setup_encrypt(bp->b_cryptkey, vl_seed);
}
return TRUE;
}
/* change current buffer's encryption key */
/* ARGSUSED */
int
vl_setkey(int f GCC_UNUSED,
int n GCC_UNUSED)
{
char result[NKEYLEN];
int rc;
memset(result, 0, sizeof(result));
rc = get_encryption_key(result, sizeof(result));
if (rc == TRUE) {
TRACE(("set key for %s\n", curbp->b_bname));
(void) vl_strncpy(curbp->b_cryptkey, result, sizeof(curbp->b_cryptkey));
set_local_b_val(curbp, MDCRYPT, TRUE);
curwp->w_flag |= WFMODE;
} else if (rc == FALSE) {
if (curbp->b_cryptkey[0] != EOS) {
rc = mlyesno("Discard encryption key");
if (rc == TRUE) {
TRACE(("reset key for %s\n", curbp->b_bname));
curbp->b_cryptkey[0] = EOS;
if (global_b_val(MDCRYPT)) {
set_local_b_val(curbp, MDCRYPT, FALSE);
curwp->w_flag |= WFMODE;
} else if (b_val(curbp, MDCRYPT)) {
make_global_val(curbp->b_values.bv, global_b_values.bv, MDCRYPT);
curwp->w_flag |= WFMODE;
}
}
}
}
return (rc);
}
#endif
|