2014-12-20 04:15:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2017-08-03 15:05:37 +00:00
|
|
|
#include <sysexits.h>
|
2014-12-20 04:15:32 +00:00
|
|
|
|
|
|
|
#if defined(READLINE)
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#elif defined(EDITLINE)
|
|
|
|
#include <histedit.h>
|
|
|
|
#endif
|
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
#include "mpw-algorithm.h"
|
|
|
|
#include "mpw-util.h"
|
2017-07-16 01:13:49 +00:00
|
|
|
#include "mpw-marshall.h"
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-08-08 04:00:14 +00:00
|
|
|
#ifndef MP_VERSION
|
|
|
|
#define MP_VERSION ?
|
|
|
|
#endif
|
|
|
|
#define MP_ENV_fullName "MP_FULLNAME"
|
|
|
|
#define MP_ENV_algorithm "MP_ALGORITHM"
|
2017-08-13 01:57:47 +00:00
|
|
|
#define MP_ENV_format "MP_FORMAT"
|
2014-12-20 04:15:32 +00:00
|
|
|
|
|
|
|
static void usage() {
|
|
|
|
|
2017-08-08 04:00:14 +00:00
|
|
|
inf( ""
|
|
|
|
" Master Password v%s\n"
|
|
|
|
" https://masterpasswordapp.com\n\n", stringify_def( MP_VERSION ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-08 00:27:08 +00:00
|
|
|
"Usage:\n"
|
|
|
|
" mpw [-u|-U full-name] [-t pw-type] [-c counter] [-a algorithm] [-s value]\n"
|
|
|
|
" [-p purpose] [-C context] [-f|-F format] [-R 0|1] [-v|-q] [-h] site-name\n\n" );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-08 00:27:08 +00:00
|
|
|
" -u full-name Specify the full name of the user.\n"
|
|
|
|
" -u checks the master password against the config,\n"
|
2017-08-07 22:57:10 +00:00
|
|
|
" -U allows updating to a new master password.\n"
|
2017-08-08 04:00:14 +00:00
|
|
|
" Defaults to %s in env or prompts.\n\n", MP_ENV_fullName );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-08 00:27:08 +00:00
|
|
|
" -t pw-type Specify the password's template.\n"
|
|
|
|
" Defaults to 'long' (-p a), 'name' (-p i) or 'phrase' (-p r).\n"
|
|
|
|
" x, maximum | 20 characters, contains symbols.\n"
|
|
|
|
" l, long | Copy-friendly, 14 characters, symbols.\n"
|
|
|
|
" m, medium | Copy-friendly, 8 characters, symbols.\n"
|
|
|
|
" b, basic | 8 characters, no symbols.\n"
|
|
|
|
" s, short | Copy-friendly, 4 characters, no symbols.\n"
|
|
|
|
" i, pin | 4 numbers.\n"
|
|
|
|
" n, name | 9 letter name.\n"
|
|
|
|
" p, phrase | 20 character sentence.\n"
|
2017-08-10 16:30:42 +00:00
|
|
|
" K, key | encryption key (set key size -s bits).\n"
|
|
|
|
" P, personal | saved personal password (save with -s pw).\n\n" );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-07 22:57:10 +00:00
|
|
|
" -c counter The value of the counter.\n"
|
|
|
|
" Defaults to 1.\n\n" );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-08 00:27:08 +00:00
|
|
|
" -a version The algorithm version to use, %d - %d.\n"
|
|
|
|
" Defaults to %s in env or %d.\n\n",
|
2017-08-08 04:00:14 +00:00
|
|
|
MPAlgorithmVersionFirst, MPAlgorithmVersionLast, MP_ENV_algorithm, MPAlgorithmVersionCurrent );
|
2017-08-08 00:27:08 +00:00
|
|
|
inf( ""
|
2017-08-10 16:30:42 +00:00
|
|
|
" -s value The value to save for -t P or -p i.\n"
|
|
|
|
" The size of they key to generate for -t K, in bits (eg. 256).\n\n" );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-07 22:57:10 +00:00
|
|
|
" -p purpose The purpose of the generated token.\n"
|
2017-08-08 00:27:08 +00:00
|
|
|
" Defaults to 'auth'.\n"
|
2017-08-07 22:57:10 +00:00
|
|
|
" a, auth | An authentication token such as a password.\n"
|
|
|
|
" i, ident | An identification token such as a username.\n"
|
|
|
|
" r, rec | A recovery token such as a security answer.\n\n" );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-07 22:57:10 +00:00
|
|
|
" -C context A purpose-specific context.\n"
|
|
|
|
" Defaults to empty.\n"
|
2017-08-08 00:27:08 +00:00
|
|
|
" -p a | -\n"
|
|
|
|
" -p i | -\n"
|
|
|
|
" -p r | Most significant word in security question.\n\n" );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-07 22:57:10 +00:00
|
|
|
" -f|F format The mpsites format to use for reading/writing site parameters.\n"
|
2017-08-08 00:27:08 +00:00
|
|
|
" -F forces the use of the given format,\n"
|
2017-08-07 22:57:10 +00:00
|
|
|
" -f allows fallback/migration.\n"
|
2017-08-13 01:57:47 +00:00
|
|
|
" Defaults to %s in env or json, falls back to plain.\n"
|
|
|
|
" n, none | No file\n"
|
2017-08-07 22:57:10 +00:00
|
|
|
" f, flat | ~/.mpw.d/Full Name.%s\n"
|
|
|
|
" j, json | ~/.mpw.d/Full Name.%s\n\n",
|
2017-08-13 01:57:47 +00:00
|
|
|
MP_ENV_format, mpw_marshall_format_extension( MPMarshallFormatFlat ), mpw_marshall_format_extension( MPMarshallFormatJSON ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-07 22:57:10 +00:00
|
|
|
" -R redacted Whether to save the mpsites in redacted format or not.\n"
|
|
|
|
" Defaults to 1, redacted.\n\n" );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-07 22:57:10 +00:00
|
|
|
" -v Increase output verbosity (can be repeated).\n"
|
|
|
|
" -q Decrease output verbosity (can be repeated).\n\n" );
|
|
|
|
inf( ""
|
|
|
|
" ENVIRONMENT\n\n"
|
|
|
|
" %-14s | The full name of the user (see -u).\n"
|
|
|
|
" %-14s | The default algorithm version (see -a).\n\n",
|
2017-08-08 04:00:14 +00:00
|
|
|
MP_ENV_fullName, MP_ENV_algorithm );
|
2014-12-20 04:15:32 +00:00
|
|
|
exit( 0 );
|
|
|
|
}
|
|
|
|
|
2017-08-08 00:27:08 +00:00
|
|
|
static const char *mpw_getenv(const char *variableName) {
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-08-08 00:27:08 +00:00
|
|
|
char *envBuf = getenv( variableName );
|
|
|
|
return envBuf? strdup( envBuf ): NULL;
|
2014-12-20 04:15:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 00:27:08 +00:00
|
|
|
static const char *mpw_getline(const char *prompt) {
|
2017-08-07 21:42:38 +00:00
|
|
|
|
|
|
|
fprintf( stderr, "%s ", prompt );
|
2014-12-20 04:15:32 +00:00
|
|
|
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t bufSize = 0;
|
2017-08-07 21:42:38 +00:00
|
|
|
ssize_t lineSize = getline( &buf, &bufSize, stdin );
|
|
|
|
if (lineSize <= 1) {
|
2014-12-20 04:15:32 +00:00
|
|
|
free( buf );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-07 21:42:38 +00:00
|
|
|
|
|
|
|
// Remove the newline.
|
|
|
|
buf[lineSize - 1] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-08-08 00:27:08 +00:00
|
|
|
static const char *mpw_getpass(const char *prompt) {
|
2017-08-07 21:42:38 +00:00
|
|
|
|
|
|
|
char *passBuf = getpass( prompt );
|
|
|
|
if (!passBuf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char *buf = strdup( passBuf );
|
|
|
|
bzero( passBuf, strlen( passBuf ) );
|
2014-12-20 04:15:32 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-08-08 00:27:08 +00:00
|
|
|
static char *mpw_path(const char *prefix, const char *extension) {
|
|
|
|
|
|
|
|
char *homedir = NULL;
|
|
|
|
struct passwd *passwd = getpwuid( getuid() );
|
|
|
|
if (passwd)
|
|
|
|
homedir = passwd->pw_dir;
|
|
|
|
if (!homedir)
|
|
|
|
homedir = getenv( "HOME" );
|
|
|
|
if (!homedir)
|
|
|
|
homedir = getcwd( NULL, 0 );
|
|
|
|
|
|
|
|
char *mpwPath = NULL;
|
|
|
|
asprintf( &mpwPath, "%s.%s", prefix, extension );
|
|
|
|
|
|
|
|
char *slash = strstr( mpwPath, "/" );
|
|
|
|
if (slash)
|
|
|
|
*slash = '\0';
|
|
|
|
|
|
|
|
asprintf( &mpwPath, "%s/.mpw.d/%s", homedir, mpwPath );
|
|
|
|
return mpwPath;
|
|
|
|
}
|
|
|
|
|
2014-12-20 04:15:32 +00:00
|
|
|
int main(int argc, char *const argv[]) {
|
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
// Master Password defaults.
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *fullName = NULL, *masterPassword = NULL, *siteName = NULL, *resultParam = NULL, *keyContext = NULL;
|
2017-08-08 00:27:08 +00:00
|
|
|
MPCounterValue siteCounter = MPCounterValueDefault;
|
2017-08-10 16:30:42 +00:00
|
|
|
MPResultType resultType = MPResultTypeDefault;
|
2017-08-01 17:45:54 +00:00
|
|
|
MPKeyPurpose keyPurpose = MPKeyPurposeAuthentication;
|
2015-01-16 05:25:18 +00:00
|
|
|
MPAlgorithmVersion algorithmVersion = MPAlgorithmVersionCurrent;
|
2017-08-06 03:19:24 +00:00
|
|
|
MPMarshallFormat sitesFormat = MPMarshallFormatDefault;
|
2017-08-07 22:57:10 +00:00
|
|
|
bool allowPasswordUpdate = false, sitesFormatFixed = false, sitesRedacted = true;
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
// Read the environment.
|
2017-08-08 00:27:08 +00:00
|
|
|
const char *fullNameArg = NULL, *masterPasswordArg = NULL, *siteNameArg = NULL;
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *resultTypeArg = NULL, *resultParamArg = NULL, *siteCounterArg = NULL, *algorithmVersionArg = NULL;
|
2017-08-07 22:57:10 +00:00
|
|
|
const char *keyPurposeArg = NULL, *keyContextArg = NULL, *sitesFormatArg = NULL, *sitesRedactedArg = NULL;
|
2017-08-08 04:00:14 +00:00
|
|
|
fullNameArg = mpw_getenv( MP_ENV_fullName );
|
|
|
|
algorithmVersionArg = mpw_getenv( MP_ENV_algorithm );
|
2017-08-13 01:57:47 +00:00
|
|
|
sitesFormatArg = mpw_getenv( MP_ENV_format );
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
// Read the command-line options.
|
2017-08-10 16:30:42 +00:00
|
|
|
for (int opt; (opt = getopt( argc, argv, "u:U:M:t:P:c:a:s:p:C:f:F:R:vqh" )) != EOF;)
|
2014-12-20 04:15:32 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'u':
|
2017-08-08 00:27:08 +00:00
|
|
|
fullNameArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-07 21:42:38 +00:00
|
|
|
allowPasswordUpdate = false;
|
|
|
|
break;
|
|
|
|
case 'U':
|
2017-08-08 00:27:08 +00:00
|
|
|
fullNameArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-07 21:42:38 +00:00
|
|
|
allowPasswordUpdate = true;
|
2014-12-20 04:15:32 +00:00
|
|
|
break;
|
2017-08-10 16:30:42 +00:00
|
|
|
case 'M':
|
2017-07-16 01:13:49 +00:00
|
|
|
// Passing your master password via the command-line is insecure. Testing purposes only.
|
2017-08-08 00:27:08 +00:00
|
|
|
masterPasswordArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2015-01-20 02:20:25 +00:00
|
|
|
break;
|
2014-12-20 04:15:32 +00:00
|
|
|
case 't':
|
2017-08-10 16:30:42 +00:00
|
|
|
resultTypeArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
resultParamArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2014-12-20 04:15:32 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2017-08-08 00:27:08 +00:00
|
|
|
siteCounterArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2014-12-20 04:15:32 +00:00
|
|
|
break;
|
2016-01-04 19:52:05 +00:00
|
|
|
case 'a':
|
2017-08-08 00:27:08 +00:00
|
|
|
algorithmVersionArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
|
|
|
break;
|
2017-08-06 03:19:24 +00:00
|
|
|
case 'p':
|
2017-08-08 00:27:08 +00:00
|
|
|
keyPurposeArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-06 03:19:24 +00:00
|
|
|
break;
|
2014-12-20 04:15:32 +00:00
|
|
|
case 'C':
|
2017-08-08 00:27:08 +00:00
|
|
|
keyContextArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2014-12-20 04:15:32 +00:00
|
|
|
break;
|
2017-08-06 03:19:24 +00:00
|
|
|
case 'f':
|
2017-08-08 00:27:08 +00:00
|
|
|
sitesFormatArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-06 03:19:24 +00:00
|
|
|
sitesFormatFixed = false;
|
|
|
|
break;
|
|
|
|
case 'F':
|
2017-08-08 00:27:08 +00:00
|
|
|
sitesFormatArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-06 03:19:24 +00:00
|
|
|
sitesFormatFixed = true;
|
|
|
|
break;
|
2017-08-07 22:57:10 +00:00
|
|
|
case 'R':
|
2017-08-08 00:27:08 +00:00
|
|
|
sitesRedactedArg = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-07 22:57:10 +00:00
|
|
|
break;
|
2016-01-04 19:52:05 +00:00
|
|
|
case 'v':
|
|
|
|
++mpw_verbosity;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
--mpw_verbosity;
|
|
|
|
break;
|
2014-12-20 04:15:32 +00:00
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
switch (optopt) {
|
|
|
|
case 'u':
|
2014-12-20 19:30:34 +00:00
|
|
|
ftl( "Missing full name to option: -%c\n", optopt );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_USAGE;
|
2014-12-20 04:15:32 +00:00
|
|
|
case 't':
|
2014-12-20 19:30:34 +00:00
|
|
|
ftl( "Missing type name to option: -%c\n", optopt );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_USAGE;
|
2014-12-20 04:15:32 +00:00
|
|
|
case 'c':
|
2014-12-20 19:30:34 +00:00
|
|
|
ftl( "Missing counter value to option: -%c\n", optopt );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_USAGE;
|
2014-12-20 04:15:32 +00:00
|
|
|
default:
|
2014-12-20 19:30:34 +00:00
|
|
|
ftl( "Unknown option: -%c\n", optopt );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_USAGE;
|
2014-12-20 04:15:32 +00:00
|
|
|
}
|
|
|
|
default:
|
2017-08-04 13:36:03 +00:00
|
|
|
ftl( "Unexpected option: %c\n", opt );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_USAGE;
|
2014-12-20 04:15:32 +00:00
|
|
|
}
|
|
|
|
if (optind < argc)
|
2017-08-08 00:27:08 +00:00
|
|
|
siteNameArg = strdup( argv[optind] );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 01:38:53 +00:00
|
|
|
// Determine fullName, siteName & masterPassword.
|
2017-07-16 01:13:49 +00:00
|
|
|
if (!(fullNameArg && (fullName = strdup( fullNameArg ))) &&
|
2017-08-07 21:42:38 +00:00
|
|
|
!(fullName = mpw_getline( "Your full name:" ))) {
|
2014-12-20 19:30:34 +00:00
|
|
|
ftl( "Missing full name.\n" );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_DATAERR;
|
2017-08-01 20:50:50 +00:00
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
if (!(siteNameArg && (siteName = strdup( siteNameArg ))) &&
|
2017-08-07 21:42:38 +00:00
|
|
|
!(siteName = mpw_getline( "Site name:" ))) {
|
2014-12-20 19:30:34 +00:00
|
|
|
ftl( "Missing site name.\n" );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_DATAERR;
|
2017-08-01 20:50:50 +00:00
|
|
|
}
|
2017-07-23 01:38:53 +00:00
|
|
|
if (!(masterPasswordArg && (masterPassword = strdup( masterPasswordArg ))))
|
|
|
|
while (!masterPassword || !strlen( masterPassword ))
|
2017-08-07 21:42:38 +00:00
|
|
|
masterPassword = mpw_getpass( "Your master password: " );
|
2017-08-06 03:19:24 +00:00
|
|
|
if (sitesFormatArg) {
|
|
|
|
sitesFormat = mpw_formatWithName( sitesFormatArg );
|
2017-08-08 00:27:08 +00:00
|
|
|
if (ERR == (int)sitesFormat) {
|
2017-08-06 03:19:24 +00:00
|
|
|
ftl( "Invalid sites format: %s\n", sitesFormatArg );
|
|
|
|
return EX_USAGE;
|
|
|
|
}
|
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 03:45:54 +00:00
|
|
|
// Find the user's sites file.
|
2017-08-06 03:19:24 +00:00
|
|
|
FILE *sitesFile = NULL;
|
|
|
|
char *sitesPath = mpw_path( fullName, mpw_marshall_format_extension( sitesFormat ) );
|
|
|
|
if (!sitesPath || !(sitesFile = fopen( sitesPath, "r" ))) {
|
|
|
|
dbg( "Couldn't open configuration file:\n %s: %s\n", sitesPath, strerror( errno ) );
|
|
|
|
|
|
|
|
// Try to fall back to the flat format.
|
|
|
|
if (!sitesFormatFixed) {
|
2017-08-13 01:57:47 +00:00
|
|
|
free( sitesPath );
|
|
|
|
sitesPath = mpw_path( fullName, mpw_marshall_format_extension( MPMarshallFormatFlat ) );
|
|
|
|
if (sitesPath && (sitesFile = fopen( sitesPath, "r" )))
|
|
|
|
sitesFormat = MPMarshallFormatFlat;
|
|
|
|
else
|
2017-08-06 03:19:24 +00:00
|
|
|
dbg( "Couldn't open configuration file:\n %s: %s\n", sitesPath, strerror( errno ) );
|
|
|
|
}
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the user's sites file.
|
2017-08-06 03:19:24 +00:00
|
|
|
MPMarshalledUser *user = NULL;
|
|
|
|
MPMarshalledSite *site = NULL;
|
|
|
|
if (!sitesFile) {
|
|
|
|
free( sitesPath );
|
|
|
|
sitesPath = NULL;
|
|
|
|
}
|
|
|
|
else {
|
2017-07-23 03:45:54 +00:00
|
|
|
// Read file.
|
2017-08-05 23:04:42 +00:00
|
|
|
size_t readAmount = 4096, bufSize = 0, bufOffset = 0, readSize = 0;
|
2017-08-13 01:57:47 +00:00
|
|
|
char *sitesInputData = NULL;
|
|
|
|
while ((mpw_realloc( &sitesInputData, &bufSize, readAmount )) &&
|
|
|
|
(bufOffset += (readSize = fread( sitesInputData + bufOffset, 1, readAmount, sitesFile ))) &&
|
2017-07-23 03:45:54 +00:00
|
|
|
(readSize == readAmount));
|
2017-08-06 03:19:24 +00:00
|
|
|
if (ferror( sitesFile ))
|
|
|
|
wrn( "Error while reading configuration file:\n %s: %d\n", sitesPath, ferror( sitesFile ) );
|
|
|
|
fclose( sitesFile );
|
2017-07-23 03:45:54 +00:00
|
|
|
|
|
|
|
// Parse file.
|
2017-08-11 01:29:59 +00:00
|
|
|
MPMarshallError marshallError = { .type = MPMarshallSuccess };
|
2017-08-13 01:57:47 +00:00
|
|
|
MPMarshallFormat sitesInputFormat = sitesFormatArg? sitesFormat: mpw_marshall_format_guess( sitesInputData );
|
|
|
|
user = mpw_marshall_read( sitesInputData, sitesInputFormat, masterPassword, &marshallError );
|
2017-08-07 21:42:38 +00:00
|
|
|
if (marshallError.type == MPMarshallErrorMasterPassword) {
|
|
|
|
// Incorrect master password.
|
|
|
|
if (!allowPasswordUpdate) {
|
2017-08-06 03:19:24 +00:00
|
|
|
ftl( "Incorrect master password according to configuration:\n %s: %s\n", sitesPath, marshallError.description );
|
2017-08-07 21:42:38 +00:00
|
|
|
mpw_marshal_free( user );
|
2017-08-13 01:57:47 +00:00
|
|
|
mpw_free( sitesInputData, bufSize );
|
2017-08-07 21:42:38 +00:00
|
|
|
free( sitesPath );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_DATAERR;
|
2017-08-04 13:36:03 +00:00
|
|
|
}
|
2017-08-07 21:42:38 +00:00
|
|
|
|
|
|
|
// Update user's master password.
|
|
|
|
while (marshallError.type == MPMarshallErrorMasterPassword) {
|
|
|
|
inf( "Given master password does not match configuration.\n" );
|
|
|
|
inf( "To update the configuration with this new master password, first confirm the old master password.\n" );
|
|
|
|
|
2017-08-08 00:27:08 +00:00
|
|
|
const char *importMasterPassword = NULL;
|
2017-08-07 21:42:38 +00:00
|
|
|
while (!importMasterPassword || !strlen( importMasterPassword ))
|
|
|
|
importMasterPassword = mpw_getpass( "Old master password: " );
|
|
|
|
|
|
|
|
mpw_marshal_free( user );
|
2017-08-13 01:57:47 +00:00
|
|
|
user = mpw_marshall_read( sitesInputData, sitesInputFormat, importMasterPassword, &marshallError );
|
2017-08-07 21:42:38 +00:00
|
|
|
}
|
|
|
|
if (user) {
|
|
|
|
mpw_free_string( user->masterPassword );
|
|
|
|
user->masterPassword = strdup( masterPassword );
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 01:57:47 +00:00
|
|
|
mpw_free( sitesInputData, bufSize );
|
2017-08-07 21:42:38 +00:00
|
|
|
if (!user || marshallError.type != MPMarshallSuccess) {
|
|
|
|
err( "Couldn't parse configuration file:\n %s: %s\n", sitesPath, marshallError.description );
|
2017-08-06 03:19:24 +00:00
|
|
|
mpw_marshal_free( user );
|
|
|
|
user = NULL;
|
|
|
|
free( sitesPath );
|
|
|
|
sitesPath = NULL;
|
2017-08-03 05:13:15 +00:00
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-07 21:42:38 +00:00
|
|
|
if (user) {
|
2017-07-23 03:45:54 +00:00
|
|
|
// Load defaults.
|
|
|
|
mpw_free_string( fullName );
|
|
|
|
mpw_free_string( masterPassword );
|
2017-08-06 03:19:24 +00:00
|
|
|
fullName = strdup( user->fullName );
|
2017-07-23 03:45:54 +00:00
|
|
|
masterPassword = strdup( user->masterPassword );
|
|
|
|
algorithmVersion = user->algorithm;
|
2017-08-10 16:30:42 +00:00
|
|
|
resultType = user->defaultType;
|
2017-08-07 22:57:10 +00:00
|
|
|
sitesRedacted = user->redacted;
|
|
|
|
|
|
|
|
if (!sitesRedacted && !sitesRedactedArg)
|
|
|
|
wrn( "Sites configuration is not redacted. Use -R 1 to change this.\n" );
|
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
for (size_t s = 0; s < user->sites_count; ++s) {
|
2017-08-06 03:19:24 +00:00
|
|
|
site = &user->sites[s];
|
|
|
|
if (strcmp( siteName, site->name ) != 0) {
|
|
|
|
site = NULL;
|
|
|
|
continue;
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
mpw_free_string( resultParam );
|
|
|
|
resultType = site->type;
|
2017-08-06 03:19:24 +00:00
|
|
|
siteCounter = site->counter;
|
|
|
|
algorithmVersion = site->algorithm;
|
|
|
|
break;
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 01:38:53 +00:00
|
|
|
// Parse default/config-overriding command-line parameters.
|
2017-08-07 22:57:10 +00:00
|
|
|
if (sitesRedactedArg)
|
|
|
|
sitesRedacted = strcmp( sitesRedactedArg, "1" ) == 0;
|
2017-08-06 03:19:24 +00:00
|
|
|
if (siteCounterArg) {
|
2017-07-16 01:13:49 +00:00
|
|
|
long long int siteCounterInt = atoll( siteCounterArg );
|
2017-08-08 00:27:08 +00:00
|
|
|
if (siteCounterInt < MPCounterValueFirst || siteCounterInt > MPCounterValueLast) {
|
2017-07-16 01:13:49 +00:00
|
|
|
ftl( "Invalid site counter: %s\n", siteCounterArg );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_USAGE;
|
2017-08-01 20:50:50 +00:00
|
|
|
}
|
2017-08-08 00:27:08 +00:00
|
|
|
siteCounter = (MPCounterValue)siteCounterInt;
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
if (algorithmVersionArg) {
|
|
|
|
int algorithmVersionInt = atoi( algorithmVersionArg );
|
|
|
|
if (algorithmVersionInt < MPAlgorithmVersionFirst || algorithmVersionInt > MPAlgorithmVersionLast) {
|
|
|
|
ftl( "Invalid algorithm version: %s\n", algorithmVersionArg );
|
|
|
|
return EX_USAGE;
|
|
|
|
}
|
|
|
|
algorithmVersion = (MPAlgorithmVersion)algorithmVersionInt;
|
|
|
|
}
|
|
|
|
if (keyPurposeArg) {
|
2017-08-04 13:36:03 +00:00
|
|
|
keyPurpose = mpw_purposeWithName( keyPurposeArg );
|
2017-08-08 00:27:08 +00:00
|
|
|
if (ERR == (int)keyPurpose) {
|
2017-08-04 13:36:03 +00:00
|
|
|
ftl( "Invalid purpose: %s\n", keyPurposeArg );
|
|
|
|
return EX_USAGE;
|
|
|
|
}
|
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
char *purposeResult = "password";
|
|
|
|
switch (keyPurpose) {
|
|
|
|
case MPKeyPurposeAuthentication:
|
|
|
|
break;
|
|
|
|
case MPKeyPurposeIdentification: {
|
2017-08-10 16:30:42 +00:00
|
|
|
resultType = MPResultTypeTemplateName;
|
2017-08-06 03:19:24 +00:00
|
|
|
purposeResult = "login";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPKeyPurposeRecovery: {
|
2017-08-10 16:30:42 +00:00
|
|
|
resultType = MPResultTypeTemplatePhrase;
|
2017-08-06 03:19:24 +00:00
|
|
|
purposeResult = "answer";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 16:30:42 +00:00
|
|
|
if (resultTypeArg) {
|
|
|
|
resultType = mpw_typeWithName( resultTypeArg );
|
|
|
|
if (ERR == (int)resultType) {
|
|
|
|
ftl( "Invalid type: %s\n", resultTypeArg );
|
2017-08-04 13:36:03 +00:00
|
|
|
return EX_USAGE;
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 16:30:42 +00:00
|
|
|
if (resultParamArg) {
|
|
|
|
mpw_free_string( resultParam );
|
|
|
|
resultParam = strdup( resultParamArg );
|
|
|
|
}
|
2017-08-08 00:27:08 +00:00
|
|
|
if (keyContextArg) {
|
|
|
|
mpw_free_string( keyContext );
|
2017-08-01 17:45:54 +00:00
|
|
|
keyContext = strdup( keyContextArg );
|
2017-08-08 00:27:08 +00:00
|
|
|
}
|
|
|
|
mpw_free_string( fullNameArg );
|
|
|
|
mpw_free_string( masterPasswordArg );
|
|
|
|
mpw_free_string( siteNameArg );
|
2017-08-10 16:30:42 +00:00
|
|
|
mpw_free_string( resultTypeArg );
|
|
|
|
mpw_free_string( resultParamArg );
|
2017-08-08 00:27:08 +00:00
|
|
|
mpw_free_string( siteCounterArg );
|
|
|
|
mpw_free_string( algorithmVersionArg );
|
|
|
|
mpw_free_string( keyPurposeArg );
|
|
|
|
mpw_free_string( keyContextArg );
|
|
|
|
mpw_free_string( sitesFormatArg );
|
|
|
|
mpw_free_string( sitesRedactedArg );
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
// Operation summary.
|
2016-11-03 14:31:07 +00:00
|
|
|
const char *identicon = mpw_identicon( fullName, masterPassword );
|
2017-07-16 01:13:49 +00:00
|
|
|
if (!identicon)
|
|
|
|
wrn( "Couldn't determine identicon.\n" );
|
|
|
|
dbg( "-----------------\n" );
|
|
|
|
dbg( "fullName : %s\n", fullName );
|
|
|
|
trc( "masterPassword : %s\n", masterPassword );
|
|
|
|
dbg( "identicon : %s\n", identicon );
|
2017-08-06 03:19:24 +00:00
|
|
|
dbg( "sitesFormat : %s%s\n", mpw_nameForFormat( sitesFormat ), sitesFormatFixed? " (fixed)": "" );
|
|
|
|
dbg( "sitesPath : %s\n", sitesPath );
|
2017-07-16 01:13:49 +00:00
|
|
|
dbg( "siteName : %s\n", siteName );
|
|
|
|
dbg( "siteCounter : %u\n", siteCounter );
|
2017-08-10 16:30:42 +00:00
|
|
|
dbg( "resultType : %s (%u)\n", mpw_nameForType( resultType ), resultType );
|
|
|
|
dbg( "resultParam : %s\n", resultParam );
|
2017-08-01 18:34:15 +00:00
|
|
|
dbg( "keyPurpose : %s (%u)\n", mpw_nameForPurpose( keyPurpose ), keyPurpose );
|
|
|
|
dbg( "keyContext : %s\n", keyContext );
|
|
|
|
dbg( "algorithmVersion : %u\n", algorithmVersion );
|
2017-07-16 01:13:49 +00:00
|
|
|
dbg( "-----------------\n\n" );
|
2017-08-06 03:19:24 +00:00
|
|
|
inf( "%s's %s for %s:\n[ %s ]: ", fullName, purposeResult, siteName, identicon );
|
2017-07-16 01:13:49 +00:00
|
|
|
mpw_free_string( identicon );
|
2017-08-06 03:19:24 +00:00
|
|
|
if (sitesPath)
|
|
|
|
free( sitesPath );
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
// Determine master key.
|
2017-08-01 12:31:39 +00:00
|
|
|
MPMasterKey masterKey = mpw_masterKey(
|
2015-01-16 05:25:18 +00:00
|
|
|
fullName, masterPassword, algorithmVersion );
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_free_string( masterPassword );
|
2016-11-03 14:31:07 +00:00
|
|
|
mpw_free_string( fullName );
|
2017-08-01 20:50:50 +00:00
|
|
|
if (!masterKey) {
|
2017-08-04 13:36:03 +00:00
|
|
|
ftl( "Couldn't derive master key.\n" );
|
2017-08-03 15:05:37 +00:00
|
|
|
return EX_SOFTWARE;
|
2017-08-01 20:50:50 +00:00
|
|
|
}
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
// Output the result.
|
2017-08-07 21:42:38 +00:00
|
|
|
if (keyPurpose == MPKeyPurposeIdentification && site && !site->loginGenerated && site->loginName)
|
|
|
|
fprintf( stdout, "%s\n", site->loginName );
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
else if (resultParam && site && resultType & MPResultTypeClassStateful) {
|
2017-08-10 16:30:42 +00:00
|
|
|
mpw_free_string( site->content );
|
|
|
|
if (!(site->content = mpw_siteState( masterKey, siteName, siteCounter,
|
|
|
|
keyPurpose, keyContext, resultType, resultParam, algorithmVersion ))) {
|
|
|
|
ftl( "Couldn't encrypt site content.\n" );
|
2017-08-06 03:19:24 +00:00
|
|
|
mpw_free( masterKey, MPMasterKeySize );
|
|
|
|
return EX_SOFTWARE;
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
inf( "saved.\n" );
|
2017-08-06 03:19:24 +00:00
|
|
|
}
|
2017-08-08 00:27:08 +00:00
|
|
|
else {
|
2017-08-13 01:57:47 +00:00
|
|
|
if (!resultParam && site && site->content && resultType & MPResultTypeClassStateful)
|
2017-08-10 16:30:42 +00:00
|
|
|
resultParam = strdup( site->content );
|
|
|
|
const char *siteResult = mpw_siteResult( masterKey, siteName, siteCounter,
|
|
|
|
keyPurpose, keyContext, resultType, resultParam, algorithmVersion );
|
|
|
|
if (!siteResult) {
|
|
|
|
ftl( "Couldn't generate site result.\n" );
|
|
|
|
mpw_free( masterKey, MPMasterKeySize );
|
|
|
|
return EX_SOFTWARE;
|
2017-08-08 00:27:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
fprintf( stdout, "%s\n", siteResult );
|
|
|
|
mpw_free_string( siteResult );
|
2017-08-06 03:19:24 +00:00
|
|
|
}
|
2017-08-07 22:57:10 +00:00
|
|
|
if (site && site->url)
|
|
|
|
inf( "See: %s\n", site->url );
|
2017-07-16 01:13:49 +00:00
|
|
|
mpw_free( masterKey, MPMasterKeySize );
|
2016-11-03 14:31:07 +00:00
|
|
|
mpw_free_string( siteName );
|
2017-08-10 16:30:42 +00:00
|
|
|
mpw_free_string( resultParam );
|
2017-08-01 17:45:54 +00:00
|
|
|
mpw_free_string( keyContext );
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
// Update the mpsites file.
|
|
|
|
if (user) {
|
2017-08-08 00:27:08 +00:00
|
|
|
// TODO: Move this up above the summary and replace the mpw lvars by user/site accessors.
|
2017-08-10 16:30:42 +00:00
|
|
|
if (keyPurpose == MPKeyPurposeAuthentication && !(resultType & MPSiteFeatureAlternative)) {
|
2017-08-07 21:42:38 +00:00
|
|
|
if (!site)
|
2017-08-10 16:30:42 +00:00
|
|
|
site = mpw_marshall_site( user, siteName, resultType, siteCounter, algorithmVersion );
|
2017-08-07 21:42:38 +00:00
|
|
|
else {
|
2017-08-10 16:30:42 +00:00
|
|
|
site->type = resultType;
|
2017-08-07 21:42:38 +00:00
|
|
|
site->counter = siteCounter;
|
|
|
|
site->algorithm = algorithmVersion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (keyPurpose == MPKeyPurposeIdentification && site) {
|
2017-08-10 16:30:42 +00:00
|
|
|
// TODO: We're not persisting the resultType of the generated login
|
|
|
|
if (resultType & MPResultTypeClassTemplate)
|
2017-08-07 21:42:38 +00:00
|
|
|
site->loginGenerated = true;
|
|
|
|
}
|
|
|
|
else if (keyPurpose == MPKeyPurposeRecovery && site && keyContext) {
|
2017-08-10 16:30:42 +00:00
|
|
|
// TODO: We're not persisting the resultType of the recovery question
|
2017-08-07 21:42:38 +00:00
|
|
|
MPMarshalledQuestion *question = NULL;
|
|
|
|
for (size_t q = 0; q < site->questions_count; ++q) {
|
|
|
|
question = &site->questions[q];
|
|
|
|
if (strcmp( keyContext, question->keyword ) != 0) {
|
|
|
|
question = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!question)
|
|
|
|
mpw_marshal_question( site, keyContext );
|
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
if (site) {
|
|
|
|
site->lastUsed = user->lastUsed = time( NULL );
|
|
|
|
site->uses++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sitesFormatFixed)
|
|
|
|
sitesFormat = MPMarshallFormatDefault;
|
2017-08-07 22:57:10 +00:00
|
|
|
user->redacted = sitesRedacted;
|
2017-08-06 03:19:24 +00:00
|
|
|
|
|
|
|
sitesPath = mpw_path( user->fullName, mpw_marshall_format_extension( sitesFormat ) );
|
|
|
|
dbg( "Updating: %s (%s)\n", sitesPath, mpw_nameForFormat( sitesFormat ) );
|
|
|
|
if (!sitesPath || !(sitesFile = fopen( sitesPath, "w" )))
|
|
|
|
wrn( "Couldn't create updated configuration file:\n %s: %s\n", sitesPath, strerror( errno ) );
|
|
|
|
|
|
|
|
else {
|
|
|
|
char *buf = NULL;
|
2017-08-11 01:29:59 +00:00
|
|
|
MPMarshallError marshallError = { .type = MPMarshallSuccess };
|
2017-08-06 03:19:24 +00:00
|
|
|
if (!mpw_marshall_write( &buf, sitesFormat, user, &marshallError ) || marshallError.type != MPMarshallSuccess)
|
|
|
|
wrn( "Couldn't encode updated configuration file:\n %s: %s\n", sitesPath, marshallError.description );
|
|
|
|
|
|
|
|
else if (fwrite( buf, sizeof( char ), strlen( buf ), sitesFile ) != strlen( buf ))
|
|
|
|
wrn( "Error while writing updated configuration file:\n %s: %d\n", sitesPath, ferror( sitesFile ) );
|
|
|
|
|
|
|
|
mpw_free_string( buf );
|
|
|
|
fclose( sitesFile );
|
|
|
|
}
|
|
|
|
free( sitesPath );
|
|
|
|
mpw_marshal_free( user );
|
|
|
|
}
|
2016-11-03 14:31:07 +00:00
|
|
|
|
2014-12-20 04:15:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|