2014-12-20 04:15:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.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
|
|
|
|
2017-08-31 19:30:42 +00:00
|
|
|
#include "mpw-cli-util.h"
|
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-31 15:49:36 +00:00
|
|
|
/** Output the program's usage documentation. */
|
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"
|
2017-08-30 23:04:22 +00:00
|
|
|
"--------------------------------------------------------------------------------\n"
|
|
|
|
" https://masterpasswordapp.com\n\n", stringify_def( MP_VERSION ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
inf( ""
|
2017-08-31 19:32:58 +00:00
|
|
|
"\nUSAGE\n\n"
|
2017-08-30 23:04:22 +00:00
|
|
|
" mpw [-u|-U full-name] [-m fd] [-t pw-type] [-P value] [-c counter]\n"
|
|
|
|
" [-a version] [-p purpose] [-C context] [-f|-F format] [-R 0|1]\n"
|
|
|
|
" [-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-08-30 23:04:22 +00:00
|
|
|
dbg( ""
|
2017-08-22 15:37:18 +00:00
|
|
|
" -M master-pw Specify the master password of the user.\n"
|
2017-08-30 23:04:22 +00:00
|
|
|
" Passing secrets as arguments is unsafe, for use in testing only.\n" );
|
|
|
|
inf( ""
|
|
|
|
" -m fd Read the master password of the user from a file descriptor.\n"
|
|
|
|
" Tip: don't send extra characters like newlines such as by using\n"
|
|
|
|
" echo in a pipe. Consider printf instead.\n\n" );
|
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-08-22 15:37:18 +00:00
|
|
|
inf( ""
|
|
|
|
" -P value The parameter value.\n"
|
|
|
|
" -p i | The login name for the site.\n"
|
2017-08-30 23:04:22 +00:00
|
|
|
" -t K | The bit size of the key to generate (eg. 256).\n"
|
2017-08-22 15:37:18 +00:00
|
|
|
" -t P | The personal password to encrypt.\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-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( ""
|
2017-08-31 19:32:58 +00:00
|
|
|
"\nENVIRONMENT\n\n"
|
2017-08-30 23:04:22 +00:00
|
|
|
" %-12s The full name of the user (see -u).\n"
|
|
|
|
" %-12s The default algorithm version (see -a).\n"
|
2017-08-31 17:42:03 +00:00
|
|
|
" %-12s The default mpsites format (see -f).\n"
|
|
|
|
" %-12s The askpass program to use for prompting the user.\n\n",
|
|
|
|
MP_ENV_fullName, MP_ENV_algorithm, MP_ENV_format, MP_ENV_askpass );
|
2017-09-03 19:41:12 +00:00
|
|
|
exit( EX_OK );
|
2014-12-20 04:15:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
// Internal state.
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *fullName;
|
|
|
|
const char *masterPasswordFD;
|
|
|
|
const char *masterPassword;
|
|
|
|
const char *siteName;
|
|
|
|
const char *resultType;
|
|
|
|
const char *resultParam;
|
|
|
|
const char *siteCounter;
|
|
|
|
const char *algorithmVersion;
|
|
|
|
const char *keyPurpose;
|
|
|
|
const char *keyContext;
|
|
|
|
const char *sitesFormat;
|
|
|
|
const char *sitesRedacted;
|
|
|
|
} Arguments;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
bool allowPasswordUpdate;
|
|
|
|
bool sitesFormatFixed;
|
|
|
|
const char *fullName;
|
|
|
|
const char *masterPassword;
|
|
|
|
const char *siteName;
|
|
|
|
MPMarshallFormat sitesFormat;
|
|
|
|
MPKeyPurpose keyPurpose;
|
|
|
|
const char *keyContext;
|
|
|
|
const char *sitesPath;
|
|
|
|
MPResultType resultType;
|
|
|
|
MPCounterValue siteCounter;
|
|
|
|
const char *purposeResult;
|
|
|
|
const char *resultState;
|
|
|
|
const char *resultParam;
|
|
|
|
MPMarshalledUser *user;
|
|
|
|
MPMarshalledSite *site;
|
|
|
|
MPMarshalledQuestion *question;
|
|
|
|
} Operation;
|
|
|
|
|
|
|
|
// Processing steps.
|
|
|
|
|
|
|
|
void cli_free(Arguments *args, Operation *operation);
|
|
|
|
void cli_args(Arguments *args, Operation *operation, const int argc, char *const argv[]);
|
|
|
|
void cli_fullName(Arguments *args, Operation *operation);
|
|
|
|
void cli_masterPassword(Arguments *args, Operation *operation);
|
|
|
|
void cli_siteName(Arguments *args, Operation *operation);
|
|
|
|
void cli_sitesFormat(Arguments *args, Operation *operation);
|
|
|
|
void cli_keyPurpose(Arguments *args, Operation *operation);
|
|
|
|
void cli_keyContext(Arguments *args, Operation *operation);
|
|
|
|
void cli_user(Arguments *args, Operation *operation);
|
|
|
|
void cli_site(Arguments *args, Operation *operation);
|
|
|
|
void cli_question(Arguments *args, Operation *operation);
|
|
|
|
void cli_operation(Arguments *args, Operation *operation);
|
|
|
|
void cli_resultType(Arguments *args, Operation *operation);
|
|
|
|
void cli_siteCounter(Arguments *args, Operation *operation);
|
|
|
|
void cli_resultParam(Arguments *args, Operation *operation);
|
|
|
|
void cli_algorithmVersion(Arguments *args, Operation *operation);
|
|
|
|
void cli_sitesRedacted(Arguments *args, Operation *operation);
|
|
|
|
void cli_mpw(Arguments *args, Operation *operation);
|
|
|
|
void cli_save(Arguments *args, Operation *operation);
|
|
|
|
|
2017-08-31 15:49:36 +00:00
|
|
|
/** ========================================================================
|
|
|
|
* MAIN */
|
2017-08-28 21:58:57 +00:00
|
|
|
int main(const int argc, char *const argv[]) {
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
// Application defaults.
|
|
|
|
Arguments args = {
|
|
|
|
.fullName = mpw_getenv( MP_ENV_fullName ),
|
|
|
|
.algorithmVersion = mpw_getenv( MP_ENV_algorithm ),
|
|
|
|
.sitesFormat = mpw_getenv( MP_ENV_format ),
|
|
|
|
};
|
|
|
|
Operation operation = {
|
|
|
|
.allowPasswordUpdate = false,
|
|
|
|
.sitesFormatFixed = false,
|
|
|
|
.sitesFormat = MPMarshallFormatDefault,
|
|
|
|
.keyPurpose = MPKeyPurposeAuthentication,
|
|
|
|
.resultType = MPResultTypeDefault,
|
|
|
|
.siteCounter = MPCounterValueDefault,
|
|
|
|
};
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
// Read the command-line options.
|
2017-09-03 19:41:12 +00:00
|
|
|
cli_args( &args, &operation, argc, argv );
|
|
|
|
|
|
|
|
// Determine the operation parameters not sourced from mpsites.
|
|
|
|
cli_fullName( &args, &operation );
|
|
|
|
cli_masterPassword( &args, &operation );
|
|
|
|
cli_siteName( &args, &operation );
|
|
|
|
cli_sitesFormat( &args, &operation );
|
|
|
|
cli_keyPurpose( &args, &operation );
|
|
|
|
cli_keyContext( &args, &operation );
|
|
|
|
|
|
|
|
// Load the operation parameters from mpsites.
|
|
|
|
cli_user( &args, &operation );
|
|
|
|
cli_site( &args, &operation );
|
|
|
|
cli_question( &args, &operation );
|
|
|
|
cli_operation( &args, &operation );
|
|
|
|
|
|
|
|
// Override the operation parameters from command-line arguments.
|
|
|
|
cli_resultType( &args, &operation );
|
|
|
|
cli_siteCounter( &args, &operation );
|
|
|
|
cli_resultParam( &args, &operation );
|
|
|
|
cli_algorithmVersion( &args, &operation );
|
|
|
|
cli_sitesRedacted( &args, &operation );
|
|
|
|
cli_free( &args, NULL );
|
|
|
|
|
|
|
|
// Operation summary.
|
|
|
|
const char *identicon = mpw_identicon( operation.user->fullName, operation.user->masterPassword );
|
|
|
|
if (!identicon)
|
|
|
|
wrn( "Couldn't determine identicon.\n" );
|
|
|
|
dbg( "-----------------\n" );
|
|
|
|
dbg( "fullName : %s\n", operation.user->fullName );
|
|
|
|
trc( "masterPassword : %s\n", operation.user->masterPassword );
|
|
|
|
dbg( "identicon : %s\n", identicon );
|
|
|
|
dbg( "sitesFormat : %s%s\n", mpw_nameForFormat( operation.sitesFormat ), operation.sitesFormatFixed? " (fixed)": "" );
|
|
|
|
dbg( "sitesPath : %s\n", operation.sitesPath );
|
|
|
|
dbg( "siteName : %s\n", operation.site->name );
|
|
|
|
dbg( "siteCounter : %u\n", operation.siteCounter );
|
|
|
|
dbg( "resultType : %s (%u)\n", mpw_nameForType( operation.resultType ), operation.resultType );
|
|
|
|
dbg( "resultParam : %s\n", operation.resultParam );
|
|
|
|
dbg( "keyPurpose : %s (%u)\n", mpw_nameForPurpose( operation.keyPurpose ), operation.keyPurpose );
|
|
|
|
dbg( "keyContext : %s\n", operation.keyContext );
|
|
|
|
dbg( "algorithmVersion : %u\n", operation.site->algorithm );
|
|
|
|
dbg( "-----------------\n\n" );
|
|
|
|
inf( "%s's %s for %s:\n[ %s ]: ", operation.user->fullName, operation.purposeResult, operation.site->name, identicon );
|
|
|
|
mpw_free_strings( &identicon, &operation.sitesPath, NULL );
|
|
|
|
|
|
|
|
// Finally ready to perform the actual operation.
|
|
|
|
cli_mpw( &args, &operation );
|
|
|
|
|
|
|
|
// Update metadata and save state.
|
|
|
|
operation.site->lastUsed = operation.user->lastUsed = time( NULL );
|
|
|
|
operation.site->uses++;
|
|
|
|
cli_save( &args, &operation );
|
|
|
|
|
|
|
|
cli_free( &args, &operation );
|
|
|
|
return EX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cli_free(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if (args) {
|
|
|
|
mpw_free_strings( &args->fullName, &args->masterPasswordFD, &args->masterPassword, &args->siteName, NULL );
|
|
|
|
mpw_free_strings( &args->resultType, &args->resultParam, &args->siteCounter, &args->algorithmVersion, NULL );
|
|
|
|
mpw_free_strings( &args->keyPurpose, &args->keyContext, &args->sitesFormat, &args->sitesRedacted, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation) {
|
|
|
|
mpw_free_strings( &operation->fullName, &operation->masterPassword, &operation->siteName, NULL );
|
|
|
|
mpw_free_strings( &operation->keyContext, &operation->sitesPath, &operation->purposeResult, NULL );
|
|
|
|
mpw_free_strings( &operation->resultState, &operation->resultParam, NULL );
|
|
|
|
mpw_marshal_free( &operation->user );
|
|
|
|
operation->site = NULL;
|
|
|
|
operation->question = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cli_args(Arguments *args, Operation *operation, const int argc, char *const argv[]) {
|
|
|
|
|
|
|
|
for (int opt; (opt = getopt( argc, argv, "u:U:m:M:t:P:c:a:p:C:f:F:R:vqh" )) != EOF; optarg? bzero( optarg, strlen( optarg ) ): NULL)
|
2014-12-20 04:15:32 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'u':
|
2017-09-03 19:41:12 +00:00
|
|
|
args->fullName = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
|
|
|
operation->allowPasswordUpdate = false;
|
2017-08-07 21:42:38 +00:00
|
|
|
break;
|
|
|
|
case 'U':
|
2017-09-03 19:41:12 +00:00
|
|
|
args->fullName = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
|
|
|
operation->allowPasswordUpdate = true;
|
2014-12-20 04:15:32 +00:00
|
|
|
break;
|
2017-08-30 23:04:22 +00:00
|
|
|
case 'm':
|
2017-09-03 19:41:12 +00:00
|
|
|
args->masterPasswordFD = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-30 23:04:22 +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-09-03 19:41:12 +00:00
|
|
|
args->masterPassword = 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-09-03 19:41:12 +00:00
|
|
|
args->resultType = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-10 16:30:42 +00:00
|
|
|
break;
|
|
|
|
case 'P':
|
2017-09-03 19:41:12 +00:00
|
|
|
args->resultParam = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2014-12-20 04:15:32 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2017-09-03 19:41:12 +00:00
|
|
|
args->siteCounter = 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-09-03 19:41:12 +00:00
|
|
|
args->algorithmVersion = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
2017-08-08 00:27:08 +00:00
|
|
|
break;
|
2017-08-06 03:19:24 +00:00
|
|
|
case 'p':
|
2017-09-03 19:41:12 +00:00
|
|
|
args->keyPurpose = 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-09-03 19:41:12 +00:00
|
|
|
args->keyContext = 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-09-03 19:41:12 +00:00
|
|
|
args->sitesFormat = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
|
|
|
operation->sitesFormatFixed = false;
|
2017-08-06 03:19:24 +00:00
|
|
|
break;
|
|
|
|
case 'F':
|
2017-09-03 19:41:12 +00:00
|
|
|
args->sitesFormat = optarg && strlen( optarg )? strdup( optarg ): NULL;
|
|
|
|
operation->sitesFormatFixed = true;
|
2017-08-06 03:19:24 +00:00
|
|
|
break;
|
2017-08-07 22:57:10 +00:00
|
|
|
case 'R':
|
2017-09-03 19:41:12 +00:00
|
|
|
args->sitesRedacted = 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-09-03 19:41:12 +00:00
|
|
|
exit( 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-09-03 19:41:12 +00:00
|
|
|
exit( 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-09-03 19:41:12 +00:00
|
|
|
exit( 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-09-03 19:41:12 +00:00
|
|
|
exit( 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-09-03 19:41:12 +00:00
|
|
|
exit( EX_USAGE );
|
2014-12-20 04:15:32 +00:00
|
|
|
}
|
2017-08-23 04:01:23 +00:00
|
|
|
if (optind < argc && argv[optind])
|
2017-09-03 19:41:12 +00:00
|
|
|
args->siteName = strdup( argv[optind] );
|
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
void cli_fullName(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if ((!operation->fullName || !strlen( operation->fullName )) && args->fullName)
|
|
|
|
operation->fullName = strdup( args->fullName );
|
|
|
|
if (!operation->fullName || !strlen( operation->fullName ))
|
2017-08-31 15:49:36 +00:00
|
|
|
do {
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->fullName = mpw_getline( "Your full name:" );
|
|
|
|
} while (operation->fullName && !strlen( operation->fullName ));
|
|
|
|
if (!operation->fullName || !strlen( operation->fullName )) {
|
2017-08-31 15:49:36 +00:00
|
|
|
ftl( "Missing full name.\n" );
|
2017-09-03 19:41:12 +00:00
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_DATAERR );
|
2017-08-31 15:49:36 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_masterPassword(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if ((!operation->masterPassword || !strlen( operation->masterPassword )) && args->masterPasswordFD) {
|
|
|
|
operation->masterPassword = mpw_read_fd( atoi( args->masterPasswordFD ) );
|
|
|
|
if (!operation->masterPassword && errno)
|
|
|
|
wrn( "Error reading master password from FD %s: %s\n", args->masterPasswordFD, strerror( errno ) );
|
2017-08-01 20:50:50 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
if ((!operation->masterPassword || !strlen( operation->masterPassword )) && args->masterPassword)
|
|
|
|
operation->masterPassword = strdup( args->masterPassword );
|
|
|
|
if (!operation->masterPassword || !strlen( operation->masterPassword ))
|
2017-08-31 15:49:36 +00:00
|
|
|
do {
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->masterPassword = mpw_getpass( "Your master password: " );
|
|
|
|
} while (operation->masterPassword && !strlen( operation->masterPassword ));
|
|
|
|
if (!operation->masterPassword || !strlen( operation->masterPassword )) {
|
2017-08-31 15:49:36 +00:00
|
|
|
ftl( "Missing master password.\n" );
|
2017-09-03 19:41:12 +00:00
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_DATAERR );
|
2017-08-31 15:49:36 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_siteName(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if ((!operation->siteName || !strlen( operation->siteName )) && args->siteName)
|
|
|
|
operation->siteName = strdup( args->siteName );
|
|
|
|
if (!operation->siteName || !strlen( operation->siteName ))
|
2017-08-31 15:49:36 +00:00
|
|
|
do {
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->siteName = mpw_getline( "Site name:" );
|
|
|
|
} while (operation->siteName && !strlen( operation->siteName ));
|
|
|
|
|
|
|
|
if (!operation->siteName || !strlen( operation->siteName )) {
|
2017-08-31 15:49:36 +00:00
|
|
|
ftl( "Missing site name.\n" );
|
2017-09-03 19:41:12 +00:00
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_DATAERR );
|
2017-08-31 15:49:36 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_sitesFormat(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if (args->sitesFormat) {
|
|
|
|
operation->sitesFormat = mpw_formatWithName( args->sitesFormat );
|
|
|
|
if (ERR == (int)operation->sitesFormat) {
|
|
|
|
ftl( "Invalid sites format: %s\n", args->sitesFormat );
|
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_DATAERR );
|
2017-08-06 03:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_keyPurpose(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if (args->keyPurpose) {
|
|
|
|
operation->keyPurpose = mpw_purposeWithName( args->keyPurpose );
|
|
|
|
if (ERR == (int)operation->keyPurpose) {
|
|
|
|
ftl( "Invalid purpose: %s\n", args->keyPurpose );
|
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_DATAERR );
|
2017-08-23 04:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_keyContext(Arguments *args, Operation *operation) {
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
if (args->keyContext)
|
|
|
|
operation->keyContext = strdup( args->keyContext );
|
|
|
|
}
|
|
|
|
|
|
|
|
void cli_user(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
// Find mpsites file from parameters.
|
2017-08-06 03:19:24 +00:00
|
|
|
FILE *sitesFile = NULL;
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_free_string( &operation->sitesPath );
|
|
|
|
operation->sitesPath = mpw_path( operation->fullName, mpw_marshall_format_extension( operation->sitesFormat ) );
|
|
|
|
if (!operation->sitesPath || !(sitesFile = fopen( operation->sitesPath, "r" ))) {
|
|
|
|
dbg( "Couldn't open configuration file:\n %s: %s\n", operation->sitesPath, strerror( errno ) );
|
2017-08-06 03:19:24 +00:00
|
|
|
|
|
|
|
// Try to fall back to the flat format.
|
2017-09-03 19:41:12 +00:00
|
|
|
if (!operation->sitesFormatFixed) {
|
|
|
|
mpw_free_string( &operation->sitesPath );
|
|
|
|
operation->sitesPath = mpw_path( operation->fullName, mpw_marshall_format_extension( MPMarshallFormatFlat ) );
|
|
|
|
if (operation->sitesPath && (sitesFile = fopen( operation->sitesPath, "r" )))
|
|
|
|
operation->sitesFormat = MPMarshallFormatFlat;
|
2017-08-13 01:57:47 +00:00
|
|
|
else
|
2017-09-03 19:41:12 +00:00
|
|
|
dbg( "Couldn't open configuration file:\n %s: %s\n", operation->sitesPath, strerror( errno ) );
|
2017-08-06 03:19:24 +00:00
|
|
|
}
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
// Load the user object from mpsites.
|
2017-08-23 04:01:23 +00:00
|
|
|
if (!sitesFile)
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_free_string( &operation->sitesPath );
|
2017-08-23 04:01:23 +00:00
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
else {
|
2017-07-23 03:45:54 +00:00
|
|
|
// Read file.
|
2017-08-30 23:04:22 +00:00
|
|
|
char *sitesInputData = mpw_read_file( sitesFile );
|
2017-08-06 03:19:24 +00:00
|
|
|
if (ferror( sitesFile ))
|
2017-09-03 19:41:12 +00:00
|
|
|
wrn( "Error while reading configuration file:\n %s: %d\n", operation->sitesPath, ferror( sitesFile ) );
|
2017-08-06 03:19:24 +00:00
|
|
|
fclose( sitesFile );
|
2017-07-23 03:45:54 +00:00
|
|
|
|
|
|
|
// Parse file.
|
2017-08-13 12:50:16 +00:00
|
|
|
MPMarshallInfo *sitesInputInfo = mpw_marshall_read_info( sitesInputData );
|
2017-09-03 19:41:12 +00:00
|
|
|
MPMarshallFormat sitesInputFormat = args->sitesFormat? operation->sitesFormat: sitesInputInfo->format;
|
2017-08-11 01:29:59 +00:00
|
|
|
MPMarshallError marshallError = { .type = MPMarshallSuccess };
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_marshal_info_free( &sitesInputInfo );
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->user = mpw_marshall_read( sitesInputData, sitesInputFormat, operation->masterPassword, &marshallError );
|
|
|
|
if (marshallError.type == MPMarshallErrorMasterPassword && operation->allowPasswordUpdate) {
|
|
|
|
// Update master password in mpsites.
|
2017-08-07 21:42:38 +00:00
|
|
|
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: " );
|
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_marshal_free( &operation->user );
|
|
|
|
operation->user = mpw_marshall_read( sitesInputData, sitesInputFormat, importMasterPassword, &marshallError );
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_free_string( &importMasterPassword );
|
2017-08-07 21:42:38 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
if (operation->user) {
|
|
|
|
mpw_free_string( &operation->user->masterPassword );
|
|
|
|
operation->user->masterPassword = strdup( operation->masterPassword );
|
2017-08-07 21:42:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-30 23:04:22 +00:00
|
|
|
mpw_free_string( &sitesInputData );
|
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
// Incorrect master password.
|
|
|
|
if (marshallError.type == MPMarshallErrorMasterPassword) {
|
|
|
|
ftl( "Incorrect master password according to configuration:\n %s: %s\n", operation->sitesPath, marshallError.description );
|
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_DATAERR );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any other parse error.
|
|
|
|
if (!operation->user || marshallError.type != MPMarshallSuccess) {
|
|
|
|
err( "Couldn't parse configuration file:\n %s: %s\n", operation->sitesPath, marshallError.description );
|
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_DATAERR );
|
2017-08-03 05:13:15 +00:00
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
2017-08-22 22:18:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
// If no user from mpsites, create a new one.
|
|
|
|
if (!operation->user)
|
|
|
|
operation->user = mpw_marshall_user(
|
|
|
|
operation->fullName, operation->masterPassword, MPAlgorithmVersionCurrent );
|
|
|
|
}
|
|
|
|
|
|
|
|
void cli_site(Arguments __unused *args, Operation *operation) {
|
|
|
|
|
|
|
|
// Load the site object from mpsites.
|
|
|
|
for (size_t s = 0; !operation->site && s < operation->user->sites_count; ++s)
|
|
|
|
if (strcmp( operation->siteName, (&operation->user->sites[s])->name ) == 0)
|
|
|
|
operation->site = &operation->user->sites[s];
|
|
|
|
|
|
|
|
// If no site from mpsites, create a new one.
|
|
|
|
if (!operation->site)
|
|
|
|
operation->site = mpw_marshall_site(
|
|
|
|
operation->user, operation->siteName, MPResultTypeDefault, MPCounterValueDefault, operation->user->algorithm );
|
|
|
|
}
|
|
|
|
|
|
|
|
void cli_question(Arguments __unused *args, Operation *operation) {
|
|
|
|
|
|
|
|
// Load the question object from mpsites.
|
|
|
|
switch (operation->keyPurpose) {
|
2017-08-23 04:53:14 +00:00
|
|
|
case MPKeyPurposeAuthentication:
|
|
|
|
case MPKeyPurposeIdentification:
|
|
|
|
break;
|
|
|
|
case MPKeyPurposeRecovery:
|
2017-09-03 19:41:12 +00:00
|
|
|
for (size_t q = 0; !operation->question && q < operation->site->questions_count; ++q)
|
|
|
|
if ((!operation->keyContext && !strlen( (&operation->site->questions[q])->keyword )) ||
|
|
|
|
(operation->keyContext && strcmp( (&operation->site->questions[q])->keyword, operation->keyContext ) != 0))
|
|
|
|
operation->question = &operation->site->questions[q];
|
|
|
|
|
|
|
|
// If no question from mpsites, create a new one.
|
|
|
|
if (!operation->question)
|
|
|
|
operation->question = mpw_marshal_question( operation->site, operation->keyContext );
|
2017-08-23 04:53:14 +00:00
|
|
|
break;
|
2017-08-22 22:18:24 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_operation(Arguments __unused *args, Operation *operation) {
|
2017-08-22 22:18:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
switch (operation->keyPurpose) {
|
2017-08-22 22:18:24 +00:00
|
|
|
case MPKeyPurposeAuthentication: {
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->purposeResult = "password";
|
|
|
|
operation->resultType = operation->site->type;
|
|
|
|
operation->resultState = operation->site->content? strdup( operation->site->content ): NULL;
|
|
|
|
operation->siteCounter = operation->site->counter;
|
2017-08-06 03:19:24 +00:00
|
|
|
break;
|
2017-08-22 22:18:24 +00:00
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
case MPKeyPurposeIdentification: {
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->purposeResult = "login";
|
|
|
|
operation->resultType = operation->site->loginType;
|
|
|
|
operation->resultState = operation->site->loginContent? strdup( operation->site->loginContent ): NULL;
|
|
|
|
operation->siteCounter = MPCounterValueInitial;
|
2017-08-06 03:19:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPKeyPurposeRecovery: {
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_free_string( &operation->keyContext );
|
|
|
|
operation->purposeResult = "answer";
|
|
|
|
operation->keyContext = operation->question->keyword? strdup( operation->question->keyword ): NULL;
|
|
|
|
operation->resultType = operation->question->type;
|
|
|
|
operation->resultState = operation->question->content? strdup( operation->question->content ): NULL;
|
|
|
|
operation->siteCounter = MPCounterValueInitial;
|
2017-08-06 03:19:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
2017-08-22 22:18:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
void cli_resultType(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if (args->resultType) {
|
|
|
|
operation->resultType = mpw_typeWithName( args->resultType );
|
|
|
|
if (ERR == (int)operation->resultType) {
|
|
|
|
ftl( "Invalid type: %s\n", args->resultType );
|
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_USAGE );
|
2017-08-04 13:36:03 +00:00
|
|
|
}
|
2017-08-22 22:18:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
if (!(operation->resultType & MPSiteFeatureAlternative)) {
|
|
|
|
switch (operation->keyPurpose) {
|
2017-08-22 22:18:24 +00:00
|
|
|
case MPKeyPurposeAuthentication:
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->site->type = operation->resultType;
|
2017-08-22 22:18:24 +00:00
|
|
|
break;
|
|
|
|
case MPKeyPurposeIdentification:
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->site->loginType = operation->resultType;
|
2017-08-22 22:18:24 +00:00
|
|
|
break;
|
|
|
|
case MPKeyPurposeRecovery:
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->question->type = operation->resultType;
|
2017-08-22 22:18:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-04 13:36:03 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_siteCounter(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if (args->siteCounter) {
|
|
|
|
long long int siteCounterInt = atoll( args->siteCounter );
|
2017-08-22 22:18:24 +00:00
|
|
|
if (siteCounterInt < MPCounterValueFirst || siteCounterInt > MPCounterValueLast) {
|
2017-09-03 19:41:12 +00:00
|
|
|
ftl( "Invalid site counter: %s\n", args->siteCounter );
|
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_USAGE );
|
2017-08-22 22:18:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
switch (operation->keyPurpose) {
|
2017-08-22 22:18:24 +00:00
|
|
|
case MPKeyPurposeAuthentication:
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->siteCounter = operation->site->counter = (MPCounterValue)siteCounterInt;
|
2017-08-22 22:18:24 +00:00
|
|
|
break;
|
|
|
|
case MPKeyPurposeIdentification:
|
|
|
|
case MPKeyPurposeRecovery:
|
|
|
|
// NOTE: counter for login & question is not persisted.
|
|
|
|
break;
|
|
|
|
}
|
2017-08-10 16:30:42 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_resultParam(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if (args->resultParam)
|
|
|
|
operation->resultParam = strdup( args->resultParam );
|
|
|
|
}
|
|
|
|
|
|
|
|
void cli_algorithmVersion(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if (args->algorithmVersion) {
|
|
|
|
int algorithmVersionInt = atoi( args->algorithmVersion );
|
2017-08-22 22:18:24 +00:00
|
|
|
if (algorithmVersionInt < MPAlgorithmVersionFirst || algorithmVersionInt > MPAlgorithmVersionLast) {
|
2017-09-03 19:41:12 +00:00
|
|
|
ftl( "Invalid algorithm version: %s\n", args->algorithmVersion );
|
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_USAGE );
|
2017-08-22 22:18:24 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->site->algorithm = (MPAlgorithmVersion)algorithmVersionInt;
|
2017-08-08 00:27:08 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
void cli_sitesRedacted(Arguments *args, Operation *operation) {
|
|
|
|
|
|
|
|
if (args->sitesRedacted)
|
|
|
|
operation->user->redacted = strcmp( args->sitesRedacted, "1" ) == 0;
|
|
|
|
else if (!operation->user->redacted)
|
|
|
|
wrn( "Sites configuration is not redacted. Use -R 1 to change this.\n" );
|
|
|
|
}
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
void cli_mpw(Arguments *args, Operation *operation) {
|
2017-08-06 03:19:24 +00:00
|
|
|
// Determine master key.
|
2017-08-01 12:31:39 +00:00
|
|
|
MPMasterKey masterKey = mpw_masterKey(
|
2017-09-03 19:41:12 +00:00
|
|
|
operation->user->fullName, operation->user->masterPassword, operation->site->algorithm );
|
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-09-03 19:41:12 +00:00
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_SOFTWARE );
|
2017-08-01 20:50:50 +00:00
|
|
|
}
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
// Update state from resultParam if stateful.
|
|
|
|
if (operation->resultParam && operation->resultType & MPResultTypeClassStateful) {
|
|
|
|
mpw_free_string( &operation->resultState );
|
|
|
|
if (!(operation->resultState = mpw_siteState( masterKey, operation->site->name, operation->siteCounter,
|
|
|
|
operation->keyPurpose, operation->keyContext, operation->resultType, operation->resultParam, operation->site->algorithm ))) {
|
2017-08-22 22:18:24 +00:00
|
|
|
ftl( "Couldn't encrypt site result.\n" );
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_free( &masterKey, MPMasterKeySize );
|
2017-09-03 19:41:12 +00:00
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_SOFTWARE );
|
2017-08-22 15:37:18 +00:00
|
|
|
}
|
2017-09-03 19:41:12 +00:00
|
|
|
inf( "(state) %s => ", operation->resultState );
|
2017-08-22 15:37:18 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
switch (operation->keyPurpose) {
|
2017-08-22 22:18:24 +00:00
|
|
|
case MPKeyPurposeAuthentication: {
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_free_string( &operation->site->content );
|
|
|
|
operation->site->content = strdup( operation->resultState );
|
2017-08-22 22:18:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MPKeyPurposeIdentification: {
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_free_string( &operation->site->loginContent );
|
|
|
|
operation->site->loginContent = strdup( operation->resultState );
|
2017-08-22 22:18:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-07 21:42:38 +00:00
|
|
|
|
2017-08-22 22:18:24 +00:00
|
|
|
case MPKeyPurposeRecovery: {
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_free_string( &operation->question->content );
|
|
|
|
operation->question->content = strdup( operation->resultState );
|
2017-08-22 22:18:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 22:18:24 +00:00
|
|
|
// resultParam is consumed.
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_free_string( &operation->resultParam );
|
2017-08-06 03:19:24 +00:00
|
|
|
}
|
2017-08-08 00:27:08 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
// resultParam defaults to state.
|
|
|
|
if (!operation->resultParam && operation->resultState)
|
|
|
|
operation->resultParam = strdup( operation->resultState );
|
2017-08-22 22:18:24 +00:00
|
|
|
|
|
|
|
// Generate result.
|
2017-09-03 19:41:12 +00:00
|
|
|
const char *result = mpw_siteResult( masterKey, operation->site->name, operation->siteCounter,
|
|
|
|
operation->keyPurpose, operation->keyContext, operation->resultType, operation->resultParam, operation->site->algorithm );
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_free( &masterKey, MPMasterKeySize );
|
2017-08-22 22:18:24 +00:00
|
|
|
if (!result) {
|
|
|
|
ftl( "Couldn't generate site result.\n" );
|
2017-09-03 19:41:12 +00:00
|
|
|
cli_free( args, operation );
|
|
|
|
exit( EX_SOFTWARE );
|
2017-08-06 03:19:24 +00:00
|
|
|
}
|
2017-08-22 22:18:24 +00:00
|
|
|
fprintf( stdout, "%s\n", result );
|
2017-09-03 19:41:12 +00:00
|
|
|
if (operation->site->url)
|
|
|
|
inf( "See: %s\n", operation->site->url );
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_free_string( &result );
|
2017-09-03 19:41:12 +00:00
|
|
|
}
|
2014-12-20 04:15:32 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
void cli_save(Arguments __unused *args, Operation *operation) {
|
2017-08-06 03:19:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
if (operation->sitesFormat == MPMarshallFormatNone)
|
|
|
|
return;
|
2017-08-22 22:18:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
if (!operation->sitesFormatFixed)
|
|
|
|
operation->sitesFormat = MPMarshallFormatDefault;
|
|
|
|
operation->sitesPath = mpw_path( operation->user->fullName, mpw_marshall_format_extension( operation->sitesFormat ) );
|
|
|
|
dbg( "Updating: %s (%s)\n", operation->sitesPath, mpw_nameForFormat( operation->sitesFormat ) );
|
2017-08-06 03:19:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
FILE *sitesFile = NULL;
|
|
|
|
if (!operation->sitesPath || !mpw_mkdirs( operation->sitesPath ) || !(sitesFile = fopen( operation->sitesPath, "w" ))) {
|
|
|
|
wrn( "Couldn't create updated configuration file:\n %s: %s\n", operation->sitesPath, strerror( errno ) );
|
|
|
|
return;
|
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
char *buf = NULL;
|
|
|
|
MPMarshallError marshallError = { .type = MPMarshallSuccess };
|
|
|
|
if (!mpw_marshall_write( &buf, operation->sitesFormat, operation->user, &marshallError ) || marshallError.type != MPMarshallSuccess)
|
|
|
|
wrn( "Couldn't encode updated configuration file:\n %s: %s\n", operation->sitesPath, marshallError.description );
|
2017-08-06 03:19:24 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
else if (fwrite( buf, sizeof( char ), strlen( buf ), sitesFile ) != strlen( buf ))
|
|
|
|
wrn( "Error while writing updated configuration file:\n %s: %d\n", operation->sitesPath, ferror( sitesFile ) );
|
2016-11-03 14:31:07 +00:00
|
|
|
|
2017-09-03 19:41:12 +00:00
|
|
|
mpw_free_string( &buf );
|
|
|
|
fclose( sitesFile );
|
2014-12-20 04:15:32 +00:00
|
|
|
}
|