2
0

Harmonize log methods by removing trailing \n.

This commit is contained in:
Maarten Billemont 2018-03-24 15:14:41 -04:00
parent 035bb6b285
commit a1f5e0ba1c
14 changed files with 181 additions and 193 deletions

View File

@ -29,9 +29,9 @@ MPMasterKey mpw_masterKey(const char *fullName, const char *masterPassword, cons
if (masterPassword && !strlen( masterPassword )) if (masterPassword && !strlen( masterPassword ))
masterPassword = NULL; masterPassword = NULL;
trc( "-- mpw_masterKey (algorithm: %u)\n", algorithmVersion ); trc( "-- mpw_masterKey (algorithm: %u)", algorithmVersion );
trc( "fullName: %s\n", fullName ); trc( "fullName: %s", fullName );
trc( "masterPassword.id: %s\n", masterPassword? mpw_id_buf( masterPassword, strlen( masterPassword ) ): NULL ); trc( "masterPassword.id: %s", masterPassword? mpw_id_buf( masterPassword, strlen( masterPassword ) ): NULL );
if (!fullName || !masterPassword) if (!fullName || !masterPassword)
return NULL; return NULL;
@ -45,7 +45,7 @@ MPMasterKey mpw_masterKey(const char *fullName, const char *masterPassword, cons
case MPAlgorithmVersion3: case MPAlgorithmVersion3:
return mpw_masterKey_v3( fullName, masterPassword ); return mpw_masterKey_v3( fullName, masterPassword );
default: default:
err( "Unsupported version: %d\n", algorithmVersion ); err( "Unsupported version: %d", algorithmVersion );
return NULL; return NULL;
} }
} }
@ -57,11 +57,11 @@ MPSiteKey mpw_siteKey(
if (keyContext && !strlen( keyContext )) if (keyContext && !strlen( keyContext ))
keyContext = NULL; keyContext = NULL;
trc( "-- mpw_siteKey (algorithm: %u)\n", algorithmVersion ); trc( "-- mpw_siteKey (algorithm: %u)", algorithmVersion );
trc( "siteName: %s\n", siteName ); trc( "siteName: %s", siteName );
trc( "siteCounter: %d\n", siteCounter ); trc( "siteCounter: %d", siteCounter );
trc( "keyPurpose: %d (%s)\n", keyPurpose, mpw_nameForPurpose( keyPurpose ) ); trc( "keyPurpose: %d (%s)", keyPurpose, mpw_nameForPurpose( keyPurpose ) );
trc( "keyContext: %s\n", keyContext ); trc( "keyContext: %s", keyContext );
if (!masterKey || !siteName) if (!masterKey || !siteName)
return NULL; return NULL;
@ -75,7 +75,7 @@ MPSiteKey mpw_siteKey(
case MPAlgorithmVersion3: case MPAlgorithmVersion3:
return mpw_siteKey_v3( masterKey, siteName, siteCounter, keyPurpose, keyContext ); return mpw_siteKey_v3( masterKey, siteName, siteCounter, keyPurpose, keyContext );
default: default:
err( "Unsupported version: %d\n", algorithmVersion ); err( "Unsupported version: %d", algorithmVersion );
return NULL; return NULL;
} }
} }
@ -95,9 +95,9 @@ const char *mpw_siteResult(
if (!siteKey) if (!siteKey)
return NULL; return NULL;
trc( "-- mpw_siteResult (algorithm: %u)\n", algorithmVersion ); trc( "-- mpw_siteResult (algorithm: %u)", algorithmVersion );
trc( "resultType: %d (%s)\n", resultType, mpw_nameForType( resultType ) ); trc( "resultType: %d (%s)", resultType, mpw_nameForType( resultType ) );
trc( "resultParam: %s\n", resultParam ); trc( "resultParam: %s", resultParam );
char *sitePassword = NULL; char *sitePassword = NULL;
if (resultType & MPResultTypeClassTemplate) { if (resultType & MPResultTypeClassTemplate) {
@ -111,7 +111,7 @@ const char *mpw_siteResult(
case MPAlgorithmVersion3: case MPAlgorithmVersion3:
return mpw_sitePasswordFromTemplate_v3( masterKey, siteKey, resultType, resultParam ); return mpw_sitePasswordFromTemplate_v3( masterKey, siteKey, resultType, resultParam );
default: default:
err( "Unsupported version: %d\n", algorithmVersion ); err( "Unsupported version: %d", algorithmVersion );
return NULL; return NULL;
} }
} }
@ -126,7 +126,7 @@ const char *mpw_siteResult(
case MPAlgorithmVersion3: case MPAlgorithmVersion3:
return mpw_sitePasswordFromCrypt_v3( masterKey, siteKey, resultType, resultParam ); return mpw_sitePasswordFromCrypt_v3( masterKey, siteKey, resultType, resultParam );
default: default:
err( "Unsupported version: %d\n", algorithmVersion ); err( "Unsupported version: %d", algorithmVersion );
return NULL; return NULL;
} }
} }
@ -141,12 +141,12 @@ const char *mpw_siteResult(
case MPAlgorithmVersion3: case MPAlgorithmVersion3:
return mpw_sitePasswordFromDerive_v3( masterKey, siteKey, resultType, resultParam ); return mpw_sitePasswordFromDerive_v3( masterKey, siteKey, resultType, resultParam );
default: default:
err( "Unsupported version: %d\n", algorithmVersion ); err( "Unsupported version: %d", algorithmVersion );
return NULL; return NULL;
} }
} }
else { else {
err( "Unsupported password type: %d\n", resultType ); err( "Unsupported password type: %d", resultType );
} }
return sitePassword; return sitePassword;
@ -167,9 +167,9 @@ const char *mpw_siteState(
if (!siteKey) if (!siteKey)
return NULL; return NULL;
trc( "-- mpw_siteState (algorithm: %u)\n", algorithmVersion ); trc( "-- mpw_siteState (algorithm: %u)", algorithmVersion );
trc( "resultType: %d (%s)\n", resultType, mpw_nameForType( resultType ) ); trc( "resultType: %d (%s)", resultType, mpw_nameForType( resultType ) );
trc( "resultParam: %zu bytes = %s\n", sizeof( resultParam ), resultParam ); trc( "resultParam: %zu bytes = %s", sizeof( resultParam ), resultParam );
if (!masterKey || !resultParam) if (!masterKey || !resultParam)
return NULL; return NULL;
@ -183,7 +183,7 @@ const char *mpw_siteState(
case MPAlgorithmVersion3: case MPAlgorithmVersion3:
return mpw_siteState_v3( masterKey, siteKey, resultType, resultParam ); return mpw_siteState_v3( masterKey, siteKey, resultType, resultParam );
default: default:
err( "Unsupported version: %d\n", algorithmVersion ); err( "Unsupported version: %d", algorithmVersion );
return NULL; return NULL;
} }
} }

View File

@ -52,10 +52,10 @@ static MPMasterKey mpw_masterKey_v0(
const char *fullName, const char *masterPassword) { const char *fullName, const char *masterPassword) {
const char *keyScope = mpw_scopeForPurpose( MPKeyPurposeAuthentication ); const char *keyScope = mpw_scopeForPurpose( MPKeyPurposeAuthentication );
trc( "keyScope: %s\n", keyScope ); trc( "keyScope: %s", keyScope );
// Calculate the master key salt. // Calculate the master key salt.
trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s\n", trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s",
keyScope, mpw_hex_l( (uint32_t)mpw_utf8_strlen( fullName ) ), fullName ); keyScope, mpw_hex_l( (uint32_t)mpw_utf8_strlen( fullName ) ), fullName );
size_t masterKeySaltSize = 0; size_t masterKeySaltSize = 0;
uint8_t *masterKeySalt = NULL; uint8_t *masterKeySalt = NULL;
@ -63,20 +63,20 @@ static MPMasterKey mpw_masterKey_v0(
mpw_push_int( &masterKeySalt, &masterKeySaltSize, (uint32_t)mpw_utf8_strlen( fullName ) ); mpw_push_int( &masterKeySalt, &masterKeySaltSize, (uint32_t)mpw_utf8_strlen( fullName ) );
mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName ); mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName );
if (!masterKeySalt) { if (!masterKeySalt) {
err( "Could not allocate master key salt: %s\n", strerror( errno ) ); err( "Could not allocate master key salt: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( " => masterKeySalt.id: %s\n", mpw_id_buf( masterKeySalt, masterKeySaltSize ) ); trc( " => masterKeySalt.id: %s", mpw_id_buf( masterKeySalt, masterKeySaltSize ) );
// Calculate the master key. // Calculate the master key.
trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )\n", MP_N, MP_r, MP_p ); trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )", MP_N, MP_r, MP_p );
MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p ); MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
mpw_free( &masterKeySalt, masterKeySaltSize ); mpw_free( &masterKeySalt, masterKeySaltSize );
if (!masterKey) { if (!masterKey) {
err( "Could not derive master key: %s\n", strerror( errno ) ); err( "Could not derive master key: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( " => masterKey.id: %s\n", mpw_id_buf( masterKey, MPMasterKeySize ) ); trc( " => masterKey.id: %s", mpw_id_buf( masterKey, MPMasterKeySize ) );
return masterKey; return masterKey;
} }
@ -86,14 +86,14 @@ static MPSiteKey mpw_siteKey_v0(
MPKeyPurpose keyPurpose, const char *keyContext) { MPKeyPurpose keyPurpose, const char *keyContext) {
const char *keyScope = mpw_scopeForPurpose( keyPurpose ); const char *keyScope = mpw_scopeForPurpose( keyPurpose );
trc( "keyScope: %s\n", keyScope ); trc( "keyScope: %s", keyScope );
// OTP counter value. // OTP counter value.
if (siteCounter == MPCounterValueTOTP) if (siteCounter == MPCounterValueTOTP)
siteCounter = ((uint32_t)time( NULL ) / MP_otp_window) * MP_otp_window; siteCounter = ((uint32_t)time( NULL ) / MP_otp_window) * MP_otp_window;
// Calculate the site seed. // Calculate the site seed.
trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s\n", trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s",
keyScope, mpw_hex_l( (uint32_t)mpw_utf8_strlen( siteName ) ), siteName, mpw_hex_l( siteCounter ), keyScope, mpw_hex_l( (uint32_t)mpw_utf8_strlen( siteName ) ), siteName, mpw_hex_l( siteCounter ),
keyContext? mpw_hex_l( (uint32_t)mpw_utf8_strlen( keyContext ) ): NULL, keyContext ); keyContext? mpw_hex_l( (uint32_t)mpw_utf8_strlen( keyContext ) ): NULL, keyContext );
size_t siteSaltSize = 0; size_t siteSaltSize = 0;
@ -107,20 +107,20 @@ static MPSiteKey mpw_siteKey_v0(
mpw_push_string( &siteSalt, &siteSaltSize, keyContext ); mpw_push_string( &siteSalt, &siteSaltSize, keyContext );
} }
if (!siteSalt) { if (!siteSalt) {
err( "Could not allocate site salt: %s\n", strerror( errno ) ); err( "Could not allocate site salt: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( " => siteSalt.id: %s\n", mpw_id_buf( siteSalt, siteSaltSize ) ); trc( " => siteSalt.id: %s", mpw_id_buf( siteSalt, siteSaltSize ) );
trc( "siteKey: hmac-sha256( masterKey.id=%s, siteSalt )\n", trc( "siteKey: hmac-sha256( masterKey.id=%s, siteSalt )",
mpw_id_buf( masterKey, MPMasterKeySize ) ); mpw_id_buf( masterKey, MPMasterKeySize ) );
MPSiteKey siteKey = mpw_hash_hmac_sha256( masterKey, MPMasterKeySize, siteSalt, siteSaltSize ); MPSiteKey siteKey = mpw_hash_hmac_sha256( masterKey, MPMasterKeySize, siteSalt, siteSaltSize );
mpw_free( &siteSalt, siteSaltSize ); mpw_free( &siteSalt, siteSaltSize );
if (!siteKey) { if (!siteKey) {
err( "Could not derive site key: %s\n", strerror( errno ) ); err( "Could not derive site key: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( " => siteKey.id: %s\n", mpw_id_buf( siteKey, MPSiteKeySize ) ); trc( " => siteKey.id: %s", mpw_id_buf( siteKey, MPSiteKeySize ) );
return siteKey; return siteKey;
} }
@ -134,11 +134,11 @@ static const char *mpw_sitePasswordFromTemplate_v0(
uint16_t seedByte; uint16_t seedByte;
mpw_uint16( (uint16_t)_siteKey[0], (uint8_t *)&seedByte ); mpw_uint16( (uint16_t)_siteKey[0], (uint8_t *)&seedByte );
const char *template = mpw_templateForType_v0( resultType, seedByte ); const char *template = mpw_templateForType_v0( resultType, seedByte );
trc( "template: %u => %s\n", seedByte, template ); trc( "template: %u => %s", seedByte, template );
if (!template) if (!template)
return NULL; return NULL;
if (strlen( template ) > MPSiteKeySize) { if (strlen( template ) > MPSiteKeySize) {
err( "Template too long for password seed: %zu\n", strlen( template ) ); err( "Template too long for password seed: %zu", strlen( template ) );
return NULL; return NULL;
} }
@ -147,10 +147,10 @@ static const char *mpw_sitePasswordFromTemplate_v0(
for (size_t c = 0; c < strlen( template ); ++c) { for (size_t c = 0; c < strlen( template ); ++c) {
mpw_uint16( (uint16_t)_siteKey[c + 1], (uint8_t *)&seedByte ); mpw_uint16( (uint16_t)_siteKey[c + 1], (uint8_t *)&seedByte );
sitePassword[c] = mpw_characterFromClass_v0( template[c], seedByte ); sitePassword[c] = mpw_characterFromClass_v0( template[c], seedByte );
trc( " - class: %c, index: %5u (0x%02hX) => character: %c\n", trc( " - class: %c, index: %5u (0x%02hX) => character: %c",
template[c], seedByte, seedByte, sitePassword[c] ); template[c], seedByte, seedByte, sitePassword[c] );
} }
trc( " => password: %s\n", sitePassword ); trc( " => password: %s", sitePassword );
return sitePassword; return sitePassword;
} }
@ -159,7 +159,7 @@ static const char *mpw_sitePasswordFromCrypt_v0(
MPMasterKey masterKey, MPSiteKey __unused siteKey, MPResultType __unused resultType, const char *cipherText) { MPMasterKey masterKey, MPSiteKey __unused siteKey, MPResultType __unused resultType, const char *cipherText) {
if (!cipherText) { if (!cipherText) {
err( "Missing encrypted state.\n" ); err( "Missing encrypted state." );
return NULL; return NULL;
} }
@ -171,7 +171,7 @@ static const char *mpw_sitePasswordFromCrypt_v0(
mpw_free( &cipherBuf, mpw_base64_decode_max( cipherText ) ); mpw_free( &cipherBuf, mpw_base64_decode_max( cipherText ) );
return NULL; return NULL;
} }
trc( "b64 decoded: %zu bytes = %s\n", bufSize, mpw_hex( cipherBuf, bufSize ) ); trc( "b64 decoded: %zu bytes = %s", bufSize, mpw_hex( cipherBuf, bufSize ) );
// Decrypt // Decrypt
const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, &bufSize ); const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, &bufSize );
@ -179,8 +179,8 @@ static const char *mpw_sitePasswordFromCrypt_v0(
const char *plainText = mpw_strndup( (char *)plainBytes, bufSize ); const char *plainText = mpw_strndup( (char *)plainBytes, bufSize );
mpw_free( &plainBytes, bufSize ); mpw_free( &plainBytes, bufSize );
if (!plainText) if (!plainText)
err( "AES decryption error: %s\n", strerror( errno ) ); err( "AES decryption error: %s", strerror( errno ) );
trc( "decrypted -> plainText: %zu bytes = %s = %s\n", strlen( plainText ), plainText, mpw_hex( plainText, strlen( plainText ) ) ); trc( "decrypted -> plainText: %zu bytes = %s = %s", strlen( plainText ), plainText, mpw_hex( plainText, strlen( plainText ) ) );
return plainText; return plainText;
} }
@ -191,23 +191,23 @@ static const char *mpw_sitePasswordFromDerive_v0(
switch (resultType) { switch (resultType) {
case MPResultTypeDeriveKey: { case MPResultTypeDeriveKey: {
if (!resultParam) { if (!resultParam) {
err( "Missing key size parameter.\n" ); err( "Missing key size parameter." );
return NULL; return NULL;
} }
int resultParamInt = atoi( resultParam ); int resultParamInt = atoi( resultParam );
if (!resultParamInt) if (!resultParamInt)
resultParamInt = 512; resultParamInt = 512;
if (resultParamInt < 128 || resultParamInt > 512 || resultParamInt % 8 != 0) { if (resultParamInt < 128 || resultParamInt > 512 || resultParamInt % 8 != 0) {
err( "Parameter is not a valid key size (should be 128 - 512): %s\n", resultParam ); err( "Parameter is not a valid key size (should be 128 - 512): %s", resultParam );
return NULL; return NULL;
} }
uint16_t keySize = (uint16_t)(resultParamInt / 8); uint16_t keySize = (uint16_t)(resultParamInt / 8);
trc( "keySize: %u\n", keySize ); trc( "keySize: %u", keySize );
// Derive key // Derive key
const uint8_t *resultKey = mpw_kdf_blake2b( keySize, siteKey, MPSiteKeySize, NULL, 0, 0, NULL ); const uint8_t *resultKey = mpw_kdf_blake2b( keySize, siteKey, MPSiteKeySize, NULL, 0, 0, NULL );
if (!resultKey) { if (!resultKey) {
err( "Could not derive result key: %s\n", strerror( errno ) ); err( "Could not derive result key: %s", strerror( errno ) );
return NULL; return NULL;
} }
@ -219,13 +219,13 @@ static const char *mpw_sitePasswordFromDerive_v0(
mpw_free_string( &b64Key ); mpw_free_string( &b64Key );
} }
else else
trc( "b64 encoded -> key: %s\n", b64Key ); trc( "b64 encoded -> key: %s", b64Key );
mpw_free( &resultKey, keySize ); mpw_free( &resultKey, keySize );
return b64Key; return b64Key;
} }
default: default:
err( "Unsupported derived password type: %d\n", resultType ); err( "Unsupported derived password type: %d", resultType );
return NULL; return NULL;
} }
} }
@ -237,10 +237,10 @@ static const char *mpw_siteState_v0(
size_t bufSize = strlen( plainText ); size_t bufSize = strlen( plainText );
const uint8_t *cipherBuf = mpw_aes_encrypt( masterKey, MPMasterKeySize, (const uint8_t *)plainText, &bufSize ); const uint8_t *cipherBuf = mpw_aes_encrypt( masterKey, MPMasterKeySize, (const uint8_t *)plainText, &bufSize );
if (!cipherBuf) { if (!cipherBuf) {
err( "AES encryption error: %s\n", strerror( errno ) ); err( "AES encryption error: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( "cipherBuf: %zu bytes = %s\n", bufSize, mpw_hex( cipherBuf, bufSize ) ); trc( "cipherBuf: %zu bytes = %s", bufSize, mpw_hex( cipherBuf, bufSize ) );
// Base64-encode // Base64-encode
size_t b64Max = mpw_base64_encode_max( bufSize ); size_t b64Max = mpw_base64_encode_max( bufSize );
@ -250,7 +250,7 @@ static const char *mpw_siteState_v0(
mpw_free_string( &cipherText ); mpw_free_string( &cipherText );
} }
else else
trc( "b64 encoded -> cipherText: %s\n", cipherText ); trc( "b64 encoded -> cipherText: %s", cipherText );
mpw_free( &cipherBuf, bufSize ); mpw_free( &cipherBuf, bufSize );
return cipherText; return cipherText;

View File

@ -58,11 +58,11 @@ static const char *mpw_sitePasswordFromTemplate_v1(
// Determine the template. // Determine the template.
uint8_t seedByte = siteKey[0]; uint8_t seedByte = siteKey[0];
const char *template = mpw_templateForType( resultType, seedByte ); const char *template = mpw_templateForType( resultType, seedByte );
trc( "template: %u => %s\n", seedByte, template ); trc( "template: %u => %s", seedByte, template );
if (!template) if (!template)
return NULL; return NULL;
if (strlen( template ) > MPSiteKeySize) { if (strlen( template ) > MPSiteKeySize) {
err( "Template too long for password seed: %zu\n", strlen( template ) ); err( "Template too long for password seed: %zu", strlen( template ) );
return NULL; return NULL;
} }
@ -71,10 +71,10 @@ static const char *mpw_sitePasswordFromTemplate_v1(
for (size_t c = 0; c < strlen( template ); ++c) { for (size_t c = 0; c < strlen( template ); ++c) {
seedByte = siteKey[c + 1]; seedByte = siteKey[c + 1];
sitePassword[c] = mpw_characterFromClass( template[c], seedByte ); sitePassword[c] = mpw_characterFromClass( template[c], seedByte );
trc( " - class: %c, index: %3u (0x%02hhX) => character: %c\n", trc( " - class: %c, index: %3u (0x%02hhX) => character: %c",
template[c], seedByte, seedByte, sitePassword[c] ); template[c], seedByte, seedByte, sitePassword[c] );
} }
trc( " => password: %s\n", sitePassword ); trc( " => password: %s", sitePassword );
return sitePassword; return sitePassword;
} }

View File

@ -51,14 +51,14 @@ static MPSiteKey mpw_siteKey_v2(
MPKeyPurpose keyPurpose, const char *keyContext) { MPKeyPurpose keyPurpose, const char *keyContext) {
const char *keyScope = mpw_scopeForPurpose( keyPurpose ); const char *keyScope = mpw_scopeForPurpose( keyPurpose );
trc( "keyScope: %s\n", keyScope ); trc( "keyScope: %s", keyScope );
// OTP counter value. // OTP counter value.
if (siteCounter == MPCounterValueTOTP) if (siteCounter == MPCounterValueTOTP)
siteCounter = ((uint32_t)time( NULL ) / MP_otp_window) * MP_otp_window; siteCounter = ((uint32_t)time( NULL ) / MP_otp_window) * MP_otp_window;
// Calculate the site seed. // Calculate the site seed.
trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s\n", trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s",
keyScope, mpw_hex_l( (uint32_t)strlen( siteName ) ), siteName, mpw_hex_l( siteCounter ), keyScope, mpw_hex_l( (uint32_t)strlen( siteName ) ), siteName, mpw_hex_l( siteCounter ),
keyContext? mpw_hex_l( (uint32_t)strlen( keyContext ) ): NULL, keyContext ); keyContext? mpw_hex_l( (uint32_t)strlen( keyContext ) ): NULL, keyContext );
size_t siteSaltSize = 0; size_t siteSaltSize = 0;
@ -72,20 +72,20 @@ static MPSiteKey mpw_siteKey_v2(
mpw_push_string( &siteSalt, &siteSaltSize, keyContext ); mpw_push_string( &siteSalt, &siteSaltSize, keyContext );
} }
if (!siteSalt) { if (!siteSalt) {
err( "Could not allocate site salt: %s\n", strerror( errno ) ); err( "Could not allocate site salt: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( " => siteSalt.id: %s\n", mpw_id_buf( siteSalt, siteSaltSize ) ); trc( " => siteSalt.id: %s", mpw_id_buf( siteSalt, siteSaltSize ) );
trc( "siteKey: hmac-sha256( masterKey.id=%s, siteSalt )\n", trc( "siteKey: hmac-sha256( masterKey.id=%s, siteSalt )",
mpw_id_buf( masterKey, MPMasterKeySize ) ); mpw_id_buf( masterKey, MPMasterKeySize ) );
MPSiteKey siteKey = mpw_hash_hmac_sha256( masterKey, MPMasterKeySize, siteSalt, siteSaltSize ); MPSiteKey siteKey = mpw_hash_hmac_sha256( masterKey, MPMasterKeySize, siteSalt, siteSaltSize );
mpw_free( &siteSalt, siteSaltSize ); mpw_free( &siteSalt, siteSaltSize );
if (!siteKey) { if (!siteKey) {
err( "Could not derive site key: %s\n", strerror( errno ) ); err( "Could not derive site key: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( " => siteKey.id: %s\n", mpw_id_buf( siteKey, MPSiteKeySize ) ); trc( " => siteKey.id: %s", mpw_id_buf( siteKey, MPSiteKeySize ) );
return siteKey; return siteKey;
} }

View File

@ -44,10 +44,10 @@ static MPMasterKey mpw_masterKey_v3(
const char *fullName, const char *masterPassword) { const char *fullName, const char *masterPassword) {
const char *keyScope = mpw_scopeForPurpose( MPKeyPurposeAuthentication ); const char *keyScope = mpw_scopeForPurpose( MPKeyPurposeAuthentication );
trc( "keyScope: %s\n", keyScope ); trc( "keyScope: %s", keyScope );
// Calculate the master key salt. // Calculate the master key salt.
trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s\n", trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s",
keyScope, mpw_hex_l( (uint32_t)strlen( fullName ) ), fullName ); keyScope, mpw_hex_l( (uint32_t)strlen( fullName ) ), fullName );
size_t masterKeySaltSize = 0; size_t masterKeySaltSize = 0;
uint8_t *masterKeySalt = NULL; uint8_t *masterKeySalt = NULL;
@ -55,20 +55,20 @@ static MPMasterKey mpw_masterKey_v3(
mpw_push_int( &masterKeySalt, &masterKeySaltSize, (uint32_t)strlen( fullName ) ); mpw_push_int( &masterKeySalt, &masterKeySaltSize, (uint32_t)strlen( fullName ) );
mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName ); mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName );
if (!masterKeySalt) { if (!masterKeySalt) {
err( "Could not allocate master key salt: %s\n", strerror( errno ) ); err( "Could not allocate master key salt: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( " => masterKeySalt.id: %s\n", mpw_id_buf( masterKeySalt, masterKeySaltSize ) ); trc( " => masterKeySalt.id: %s", mpw_id_buf( masterKeySalt, masterKeySaltSize ) );
// Calculate the master key. // Calculate the master key.
trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )\n", MP_N, MP_r, MP_p ); trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )", MP_N, MP_r, MP_p );
MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p ); MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
mpw_free( &masterKeySalt, masterKeySaltSize ); mpw_free( &masterKeySalt, masterKeySaltSize );
if (!masterKey) { if (!masterKey) {
err( "Could not derive master key: %s\n", strerror( errno ) ); err( "Could not derive master key: %s", strerror( errno ) );
return NULL; return NULL;
} }
trc( " => masterKey.id: %s\n", mpw_id_buf( masterKey, MPMasterKeySize ) ); trc( " => masterKey.id: %s", mpw_id_buf( masterKey, MPMasterKeySize ) );
return masterKey; return masterKey;
} }

View File

@ -58,7 +58,7 @@ json_object *mpw_get_json_section(
char *sectionTokenizer = mpw_strdup( section ), *sectionToken = sectionTokenizer; char *sectionTokenizer = mpw_strdup( section ), *sectionToken = sectionTokenizer;
for (sectionToken = strtok( sectionToken, "." ); sectionToken; sectionToken = strtok( NULL, "." )) for (sectionToken = strtok( sectionToken, "." ); sectionToken; sectionToken = strtok( NULL, "." ))
if (!json_object_object_get_ex( json_value, sectionToken, &json_value ) || !json_value) { if (!json_object_object_get_ex( json_value, sectionToken, &json_value ) || !json_value) {
trc( "While resolving: %s: Missing value for: %s\n", section, sectionToken ); trc( "While resolving: %s: Missing value for: %s", section, sectionToken );
json_value = NULL; json_value = NULL;
break; break;
} }
@ -106,7 +106,7 @@ bool mpw_update_masterKey(MPMasterKey *masterKey, MPAlgorithmVersion *masterKeyA
*masterKeyAlgorithm = targetKeyAlgorithm; *masterKeyAlgorithm = targetKeyAlgorithm;
*masterKey = mpw_masterKey( fullName, masterPassword, *masterKeyAlgorithm ); *masterKey = mpw_masterKey( fullName, masterPassword, *masterKeyAlgorithm );
if (!*masterKey) { if (!*masterKey) {
err( "Couldn't derive master key for user %s, algorithm %d.\n", fullName, *masterKeyAlgorithm ); err( "Couldn't derive master key for user %s, algorithm %d.", fullName, *masterKeyAlgorithm );
return false; return false;
} }
} }

View File

@ -885,7 +885,7 @@ const MPMarshalFormat mpw_formatWithName(
if (strncmp( mpw_nameForFormat( MPMarshalFormatJSON ), stdFormatName, strlen( stdFormatName ) ) == 0) if (strncmp( mpw_nameForFormat( MPMarshalFormatJSON ), stdFormatName, strlen( stdFormatName ) ) == 0)
return MPMarshalFormatJSON; return MPMarshalFormatJSON;
dbg( "Not a format name: %s\n", stdFormatName ); dbg( "Not a format name: %s", stdFormatName );
return (MPMarshalFormat)ERR; return (MPMarshalFormat)ERR;
} }
@ -900,7 +900,7 @@ const char *mpw_nameForFormat(
case MPMarshalFormatJSON: case MPMarshalFormatJSON:
return "json"; return "json";
default: { default: {
dbg( "Unknown format: %d\n", format ); dbg( "Unknown format: %d", format );
return NULL; return NULL;
} }
} }
@ -917,7 +917,7 @@ const char *mpw_marshal_format_extension(
case MPMarshalFormatJSON: case MPMarshalFormatJSON:
return "mpsites.json"; return "mpsites.json";
default: { default: {
dbg( "Unknown format: %d\n", format ); dbg( "Unknown format: %d", format );
return NULL; return NULL;
} }
} }

View File

@ -84,7 +84,7 @@ const MPResultType mpw_typeWithName(const char *typeName) {
if (strncmp( mpw_nameForType( MPResultTypeDeriveKey ), stdTypeName, strlen( stdTypeName ) ) == 0) if (strncmp( mpw_nameForType( MPResultTypeDeriveKey ), stdTypeName, strlen( stdTypeName ) ) == 0)
return MPResultTypeDeriveKey; return MPResultTypeDeriveKey;
dbg( "Not a generated type name: %s\n", stdTypeName ); dbg( "Not a generated type name: %s", stdTypeName );
return (MPResultType)ERR; return (MPResultType)ERR;
} }
@ -114,7 +114,7 @@ const char *mpw_nameForType(MPResultType resultType) {
case MPResultTypeDeriveKey: case MPResultTypeDeriveKey:
return "key"; return "key";
default: { default: {
dbg( "Unknown password type: %d\n", resultType ); dbg( "Unknown password type: %d", resultType );
return NULL; return NULL;
} }
} }
@ -123,7 +123,7 @@ const char *mpw_nameForType(MPResultType resultType) {
const char **mpw_templatesForType(MPResultType type, size_t *count) { const char **mpw_templatesForType(MPResultType type, size_t *count) {
if (!(type & MPResultTypeClassTemplate)) { if (!(type & MPResultTypeClassTemplate)) {
dbg( "Not a generated type: %d\n", type ); dbg( "Not a generated type: %d", type );
return NULL; return NULL;
} }
@ -159,7 +159,7 @@ const char **mpw_templatesForType(MPResultType type, size_t *count) {
return mpw_alloc_array( count, const char *, return mpw_alloc_array( count, const char *,
"cvcc cvc cvccvcv cvc", "cvc cvccvcvcv cvcv", "cv cvccv cvc cvcvccv" ); "cvcc cvc cvccvcv cvc", "cvc cvccvcvcv cvcv", "cv cvccv cvc cvcvccv" );
default: { default: {
dbg( "Unknown generated type: %d\n", type ); dbg( "Unknown generated type: %d", type );
return NULL; return NULL;
} }
} }
@ -191,7 +191,7 @@ const MPKeyPurpose mpw_purposeWithName(const char *purposeName) {
if (strncmp( mpw_nameForPurpose( MPKeyPurposeRecovery ), stdPurposeName, strlen( stdPurposeName ) ) == 0) if (strncmp( mpw_nameForPurpose( MPKeyPurposeRecovery ), stdPurposeName, strlen( stdPurposeName ) ) == 0)
return MPKeyPurposeRecovery; return MPKeyPurposeRecovery;
dbg( "Not a purpose name: %s\n", stdPurposeName ); dbg( "Not a purpose name: %s", stdPurposeName );
return (MPKeyPurpose)ERR; return (MPKeyPurpose)ERR;
} }
@ -205,7 +205,7 @@ const char *mpw_nameForPurpose(MPKeyPurpose purpose) {
case MPKeyPurposeRecovery: case MPKeyPurposeRecovery:
return "recovery"; return "recovery";
default: { default: {
dbg( "Unknown purpose: %d\n", purpose ); dbg( "Unknown purpose: %d", purpose );
return NULL; return NULL;
} }
} }
@ -221,7 +221,7 @@ const char *mpw_scopeForPurpose(MPKeyPurpose purpose) {
case MPKeyPurposeRecovery: case MPKeyPurposeRecovery:
return "com.lyndir.masterpassword.answer"; return "com.lyndir.masterpassword.answer";
default: { default: {
dbg( "Unknown purpose: %d\n", purpose ); dbg( "Unknown purpose: %d", purpose );
return NULL; return NULL;
} }
} }
@ -251,7 +251,7 @@ const char *mpw_charactersInClass(char characterClass) {
case ' ': case ' ':
return " "; return " ";
default: { default: {
dbg( "Unknown character class: %c\n", characterClass ); dbg( "Unknown character class: %c", characterClass );
return NULL; return NULL;
} }
} }

View File

@ -26,48 +26,36 @@
//// Logging. //// Logging.
#ifndef trc #ifndef log
extern int mpw_verbosity; extern int mpw_verbosity;
#define trc_level 3 #define log(level, format, ...) ({ \
if (mpw_verbosity >= level) { \
fprintf( stderr, format "\n", ##__VA_ARGS__ ); \
}; })
/** Logging internal state. */ /** Logging internal state. */
#define trc(...) ({ \ #define trc_level 3
if (mpw_verbosity >= trc_level) \ #define trc(...) log( trc_level, ##__VA_ARGS__ )
fprintf( stderr, __VA_ARGS__ ); })
#endif
#ifndef dbg
#define dbg_level 2
/** Logging state and events interesting when investigating issues. */ /** Logging state and events interesting when investigating issues. */
#define dbg(...) ({ \ #define dbg_level 2
if (mpw_verbosity >= dbg_level) \ #define dbg(...) log( dbg_level, ##__VA_ARGS__ )
fprintf( stderr, __VA_ARGS__ ); })
#endif
#ifndef inf
#define inf_level 1
/** User messages. */ /** User messages. */
#define inf(...) ({ \ #define inf_level 1
if (mpw_verbosity >= inf_level) \ #define inf(...) log( inf_level, ##__VA_ARGS__ )
fprintf( stderr, __VA_ARGS__ ); })
#endif
#ifndef wrn
#define wrn_level 0
/** Recoverable issues and user suggestions. */ /** Recoverable issues and user suggestions. */
#define wrn(...) ({ \ #define wrn_level 0
if (mpw_verbosity >= wrn_level) \ #define wrn(...) log( wrn_level, ##__VA_ARGS__ )
fprintf( stderr, __VA_ARGS__ ); })
#endif
#ifndef err
#define err_level -1
/** Unrecoverable issues. */ /** Unrecoverable issues. */
#define err(...) ({ \ #define err_level -1
if (mpw_verbosity >= err_level) \ #define err(...) log( err_level, ##__VA_ARGS__ )
fprintf( stderr, __VA_ARGS__ ); })
#endif
#ifndef ftl
#define ftl_level -2
/** Issues that lead to abortion. */ /** Issues that lead to abortion. */
#define ftl(...) ({ \ #define ftl_level -2
if (mpw_verbosity >= ftl_level) \ #define ftl(...) log( ftl_level, ##__VA_ARGS__ )
fprintf( stderr, __VA_ARGS__ ); })
#endif #endif
#ifndef min #ifndef min

View File

@ -43,7 +43,7 @@
static void mpw_getTime(struct timeval *time) { static void mpw_getTime(struct timeval *time) {
if (gettimeofday( time, NULL ) != 0) if (gettimeofday( time, NULL ) != 0)
ftl( "Could not get time: %s\n", strerror( errno ) ); ftl( "Could not get time: %s", strerror( errno ) );
} }
static const double mpw_showSpeed(struct timeval startTime, const unsigned int iterations, const char *operation) { static const double mpw_showSpeed(struct timeval startTime, const unsigned int iterations, const char *operation) {
@ -82,7 +82,7 @@ int main(int argc, char *const argv[]) {
iterations = 4200000; /* tuned to ~10s on dev machine */ iterations = 4200000; /* tuned to ~10s on dev machine */
masterKey = mpw_masterKey( fullName, masterPassword, MPAlgorithmVersionCurrent ); masterKey = mpw_masterKey( fullName, masterPassword, MPAlgorithmVersionCurrent );
if (!masterKey) { if (!masterKey) {
ftl( "Could not allocate master key: %s\n", strerror( errno ) ); ftl( "Could not allocate master key: %s", strerror( errno ) );
abort(); abort();
} }
mpw_getTime( &startTime ); mpw_getTime( &startTime );
@ -127,7 +127,7 @@ int main(int argc, char *const argv[]) {
for (int i = 1; i <= iterations; ++i) { for (int i = 1; i <= iterations; ++i) {
masterKey = mpw_masterKey( fullName, masterPassword, MPAlgorithmVersionCurrent ); masterKey = mpw_masterKey( fullName, masterPassword, MPAlgorithmVersionCurrent );
if (!masterKey) { if (!masterKey) {
ftl( "Could not allocate master key: %s\n", strerror( errno ) ); ftl( "Could not allocate master key: %s", strerror( errno ) );
break; break;
} }

View File

@ -51,13 +51,13 @@ char *mpw_askpass(const char *prompt) {
int pipes[2]; int pipes[2];
if (pipe( pipes ) == ERR) { if (pipe( pipes ) == ERR) {
wrn( "Couldn't pipe: %s\n", strerror( errno ) ); wrn( "Couldn't pipe: %s", strerror( errno ) );
return NULL; return NULL;
} }
pid_t pid = fork(); pid_t pid = fork();
if (pid == ERR) { if (pid == ERR) {
wrn( "Couldn't fork for askpass:\n %s: %s\n", askpass, strerror( errno ) ); wrn( "Couldn't fork for askpass:\n %s: %s", askpass, strerror( errno ) );
return NULL; return NULL;
} }
@ -65,10 +65,10 @@ char *mpw_askpass(const char *prompt) {
// askpass fork // askpass fork
close( pipes[0] ); close( pipes[0] );
if (dup2( pipes[1], STDOUT_FILENO ) == ERR) if (dup2( pipes[1], STDOUT_FILENO ) == ERR)
ftl( "Couldn't connect pipe to process: %s\n", strerror( errno ) ); ftl( "Couldn't connect pipe to process: %s", strerror( errno ) );
else if (execlp( askpass, askpass, prompt, NULL ) == ERR) else if (execlp( askpass, askpass, prompt, NULL ) == ERR)
ftl( "Couldn't execute askpass:\n %s: %s\n", askpass, strerror( errno ) ); ftl( "Couldn't execute askpass:\n %s: %s", askpass, strerror( errno ) );
exit( EX_SOFTWARE ); exit( EX_SOFTWARE );
} }
@ -78,7 +78,7 @@ char *mpw_askpass(const char *prompt) {
close( pipes[0] ); close( pipes[0] );
int status; int status;
if (waitpid( pid, &status, 0 ) == ERR) { if (waitpid( pid, &status, 0 ) == ERR) {
wrn( "Couldn't wait for askpass: %s\n", strerror( errno ) ); wrn( "Couldn't wait for askpass: %s", strerror( errno ) );
mpw_free_string( &answer ); mpw_free_string( &answer );
return NULL; return NULL;
} }
@ -244,7 +244,7 @@ bool mpw_mkdirs(const char *filePath) {
free( path ); free( path );
if (chdir( cwd ) == ERR) if (chdir( cwd ) == ERR)
wrn( "Could not restore cwd:\n %s: %s\n", cwd, strerror( errno ) ); wrn( "Could not restore cwd:\n %s: %s", cwd, strerror( errno ) );
free( cwd ); free( cwd );
return success; return success;
@ -291,7 +291,7 @@ static bool mpw_setupterm() {
if (!termsetup) { if (!termsetup) {
int errret; int errret;
if (!(termsetup = (setupterm( NULL, STDERR_FILENO, &errret ) == OK))) { if (!(termsetup = (setupterm( NULL, STDERR_FILENO, &errret ) == OK))) {
wrn( "Terminal doesn't support color (setupterm errret %d).\n", errret ); wrn( "Terminal doesn't support color (setupterm errret %d).", errret );
return false; return false;
} }
} }

View File

@ -36,24 +36,24 @@ static void usage() {
inf( "" inf( ""
" Master Password v%s\n" " Master Password v%s\n"
"--------------------------------------------------------------------------------\n" "--------------------------------------------------------------------------------\n"
" https://masterpasswordapp.com\n\n", stringify_def( MP_VERSION ) ); " https://masterpasswordapp.com\n", stringify_def( MP_VERSION ) );
inf( "" inf( ""
"\nUSAGE\n\n" "\nUSAGE\n\n"
" mpw [-u|-U full-name] [-m fd] [-t pw-type] [-P value] [-c counter]\n" " 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" " [-a version] [-p purpose] [-C context] [-f|F format] [-R 0|1]\n"
" [-v|-q] [-h] [site-name]\n\n" ); " [-v|-q] [-h] [site-name]\n" );
inf( "" inf( ""
" -u full-name Specify the full name of the user.\n" " -u full-name Specify the full name of the user.\n"
" -u checks the master password against the config,\n" " -u checks the master password against the config,\n"
" -U allows updating to a new master password.\n" " -U allows updating to a new master password.\n"
" Defaults to %s in env or prompts.\n\n", MP_ENV_fullName ); " Defaults to %s in env or prompts.\n", MP_ENV_fullName );
dbg( "" dbg( ""
" -M master-pw Specify the master password of the user.\n" " -M master-pw Specify the master password of the user.\n"
" Passing secrets as arguments is unsafe, for use in testing only.\n" ); " Passing secrets as arguments is unsafe, for use in testing only." );
inf( "" inf( ""
" -m fd Read the master password of the user from a file descriptor.\n" " -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" " Tip: don't send extra characters like newlines such as by using\n"
" echo in a pipe. Consider printf instead.\n\n" ); " echo in a pipe. Consider printf instead.\n" );
inf( "" inf( ""
" -t pw-type Specify the password's template.\n" " -t pw-type Specify the password's template.\n"
" Defaults to 'long' (-p a), 'name' (-p i) or 'phrase' (-p r).\n" " Defaults to 'long' (-p a), 'name' (-p i) or 'phrase' (-p r).\n"
@ -66,31 +66,31 @@ static void usage() {
" n, name | 9 letter name.\n" " n, name | 9 letter name.\n"
" p, phrase | 20 character sentence.\n" " p, phrase | 20 character sentence.\n"
" K, key | encryption key (512 bit or -P bits).\n" " K, key | encryption key (512 bit or -P bits).\n"
" P, personal | saved personal password (save with -P pw).\n\n" ); " P, personal | saved personal password (save with -P pw).\n" );
inf( "" inf( ""
" -P value The parameter value.\n" " -P value The parameter value.\n"
" -p i | The login name for the site.\n" " -p i | The login name for the site.\n"
" -t K | The bit size of the key to generate (eg. 256).\n" " -t K | The bit size of the key to generate (eg. 256).\n"
" -t P | The personal password to encrypt.\n\n" ); " -t P | The personal password to encrypt.\n" );
inf( "" inf( ""
" -c counter The value of the counter.\n" " -c counter The value of the counter.\n"
" Defaults to 1.\n\n" ); " Defaults to 1.\n" );
inf( "" inf( ""
" -a version The algorithm version to use, %d - %d.\n" " -a version The algorithm version to use, %d - %d.\n"
" Defaults to %s in env or %d.\n\n", " Defaults to %s in env or %d.\n",
MPAlgorithmVersionFirst, MPAlgorithmVersionLast, MP_ENV_algorithm, MPAlgorithmVersionCurrent ); MPAlgorithmVersionFirst, MPAlgorithmVersionLast, MP_ENV_algorithm, MPAlgorithmVersionCurrent );
inf( "" inf( ""
" -p purpose The purpose of the generated token.\n" " -p purpose The purpose of the generated token.\n"
" Defaults to 'auth'.\n" " Defaults to 'auth'.\n"
" a, auth | An authentication token such as a password.\n" " a, auth | An authentication token such as a password.\n"
" i, ident | An identification token such as a username.\n" " i, ident | An identification token such as a username.\n"
" r, rec | A recovery token such as a security answer.\n\n" ); " r, rec | A recovery token such as a security answer.\n" );
inf( "" inf( ""
" -C context A purpose-specific context.\n" " -C context A purpose-specific context.\n"
" Defaults to empty.\n" " Defaults to empty.\n"
" -p a | -\n" " -p a | -\n"
" -p i | -\n" " -p i | -\n"
" -p r | Most significant word in security question.\n\n" ); " -p r | Most significant word in security question.\n" );
inf( "" inf( ""
" -f|F format The mpsites format to use for reading/writing site parameters.\n" " -f|F format The mpsites format to use for reading/writing site parameters.\n"
" -F forces the use of the given format,\n" " -F forces the use of the given format,\n"
@ -98,24 +98,24 @@ static void usage() {
" Defaults to %s in env or json, falls back to plain.\n" " Defaults to %s in env or json, falls back to plain.\n"
" n, none | No file\n" " n, none | No file\n"
" f, flat | ~/.mpw.d/Full Name.%s\n" " f, flat | ~/.mpw.d/Full Name.%s\n"
" j, json | ~/.mpw.d/Full Name.%s\n\n", " j, json | ~/.mpw.d/Full Name.%s\n",
MP_ENV_format, mpw_marshal_format_extension( MPMarshalFormatFlat ), mpw_marshal_format_extension( MPMarshalFormatJSON ) ); MP_ENV_format, mpw_marshal_format_extension( MPMarshalFormatFlat ), mpw_marshal_format_extension( MPMarshalFormatJSON ) );
inf( "" inf( ""
" -R redacted Whether to save the mpsites in redacted format or not.\n" " -R redacted Whether to save the mpsites in redacted format or not.\n"
" Redaction omits or encrypts any secrets, making the file safe\n" " Redaction omits or encrypts any secrets, making the file safe\n"
" for saving on or transmitting via untrusted media.\n" " for saving on or transmitting via untrusted media.\n"
" Defaults to 1, redacted.\n\n" ); " Defaults to 1, redacted.\n" );
inf( "" inf( ""
" -v Increase output verbosity (can be repeated).\n" " -v Increase output verbosity (can be repeated).\n"
" -q Decrease output verbosity (can be repeated).\n\n" ); " -q Decrease output verbosity (can be repeated).\n" );
inf( "" inf( ""
" -h Show this help output instead of performing any operation.\n\n" ); " -h Show this help output instead of performing any operation.\n" );
inf( "" inf( ""
"\nENVIRONMENT\n\n" "\nENVIRONMENT\n\n"
" %-12s The full name of the user (see -u).\n" " %-12s The full name of the user (see -u).\n"
" %-12s The default algorithm version (see -a).\n" " %-12s The default algorithm version (see -a).\n"
" %-12s The default mpsites format (see -f).\n" " %-12s The default mpsites format (see -f).\n"
" %-12s The askpass program to use for prompting the user.\n\n", " %-12s The askpass program to use for prompting the user.\n",
MP_ENV_fullName, MP_ENV_algorithm, MP_ENV_format, MP_ENV_askpass ); MP_ENV_fullName, MP_ENV_algorithm, MP_ENV_format, MP_ENV_askpass );
exit( EX_OK ); exit( EX_OK );
} }
@ -225,24 +225,24 @@ int main(const int argc, char *const argv[]) {
cli_free( &args, NULL ); cli_free( &args, NULL );
// Operation summary. // Operation summary.
dbg( "-----------------\n" ); dbg( "-----------------" );
if (operation.user) { if (operation.user) {
dbg( "fullName : %s\n", operation.user->fullName ); dbg( "fullName : %s", operation.user->fullName );
trc( "masterPassword : %s\n", operation.user->masterPassword ); trc( "masterPassword : %s", operation.user->masterPassword );
dbg( "identicon : %s\n", operation.identicon ); dbg( "identicon : %s", operation.identicon );
dbg( "sitesFormat : %s%s\n", mpw_nameForFormat( operation.sitesFormat ), operation.sitesFormatFixed? " (fixed)": "" ); dbg( "sitesFormat : %s%s", mpw_nameForFormat( operation.sitesFormat ), operation.sitesFormatFixed? " (fixed)": "" );
dbg( "sitesPath : %s\n", operation.sitesPath ); dbg( "sitesPath : %s", operation.sitesPath );
} }
if (operation.site) { if (operation.site) {
dbg( "siteName : %s\n", operation.site->name ); dbg( "siteName : %s", operation.site->name );
dbg( "siteCounter : %u\n", operation.siteCounter ); dbg( "siteCounter : %u", operation.siteCounter );
dbg( "resultType : %s (%u)\n", mpw_nameForType( operation.resultType ), operation.resultType ); dbg( "resultType : %s (%u)", mpw_nameForType( operation.resultType ), operation.resultType );
dbg( "resultParam : %s\n", operation.resultParam ); dbg( "resultParam : %s", operation.resultParam );
dbg( "keyPurpose : %s (%u)\n", mpw_nameForPurpose( operation.keyPurpose ), operation.keyPurpose ); dbg( "keyPurpose : %s (%u)", mpw_nameForPurpose( operation.keyPurpose ), operation.keyPurpose );
dbg( "keyContext : %s\n", operation.keyContext ); dbg( "keyContext : %s", operation.keyContext );
dbg( "algorithmVersion : %u\n", operation.site->algorithm ); dbg( "algorithmVersion : %u", operation.site->algorithm );
} }
dbg( "-----------------\n\n" ); dbg( "-----------------" );
// Finally ready to perform the actual operation. // Finally ready to perform the actual operation.
cli_mpw( &args, &operation ); cli_mpw( &args, &operation );
@ -333,20 +333,20 @@ void cli_args(Arguments *args, Operation *operation, const int argc, char *const
case '?': case '?':
switch (optopt) { switch (optopt) {
case 'u': case 'u':
ftl( "Missing full name to option: -%c\n", optopt ); ftl( "Missing full name to option: -%c", optopt );
exit( EX_USAGE ); exit( EX_USAGE );
case 't': case 't':
ftl( "Missing type name to option: -%c\n", optopt ); ftl( "Missing type name to option: -%c", optopt );
exit( EX_USAGE ); exit( EX_USAGE );
case 'c': case 'c':
ftl( "Missing counter value to option: -%c\n", optopt ); ftl( "Missing counter value to option: -%c", optopt );
exit( EX_USAGE ); exit( EX_USAGE );
default: default:
ftl( "Unknown option: -%c\n", optopt ); ftl( "Unknown option: -%c", optopt );
exit( EX_USAGE ); exit( EX_USAGE );
} }
default: default:
ftl( "Unexpected option: %c\n", opt ); ftl( "Unexpected option: %c", opt );
exit( EX_USAGE ); exit( EX_USAGE );
} }
@ -367,7 +367,7 @@ void cli_fullName(Arguments *args, Operation *operation) {
} while (operation->fullName && !strlen( operation->fullName )); } while (operation->fullName && !strlen( operation->fullName ));
if (!operation->fullName || !strlen( operation->fullName )) { if (!operation->fullName || !strlen( operation->fullName )) {
ftl( "Missing full name.\n" ); ftl( "Missing full name." );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
} }
@ -380,7 +380,7 @@ void cli_masterPassword(Arguments *args, Operation *operation) {
if (args->masterPasswordFD) { if (args->masterPasswordFD) {
operation->masterPassword = mpw_read_fd( atoi( args->masterPasswordFD ) ); operation->masterPassword = mpw_read_fd( atoi( args->masterPasswordFD ) );
if (!operation->masterPassword && errno) if (!operation->masterPassword && errno)
wrn( "Error reading master password from FD %s: %s\n", args->masterPasswordFD, strerror( errno ) ); wrn( "Error reading master password from FD %s: %s", args->masterPasswordFD, strerror( errno ) );
} }
if (args->masterPassword && !operation->masterPassword) if (args->masterPassword && !operation->masterPassword)
@ -392,7 +392,7 @@ void cli_masterPassword(Arguments *args, Operation *operation) {
} while (operation->masterPassword && !strlen( operation->masterPassword )); } while (operation->masterPassword && !strlen( operation->masterPassword ));
if (!operation->masterPassword || !strlen( operation->masterPassword )) { if (!operation->masterPassword || !strlen( operation->masterPassword )) {
ftl( "Missing master password.\n" ); ftl( "Missing master password." );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
} }
@ -408,7 +408,7 @@ void cli_siteName(Arguments *args, Operation *operation) {
operation->siteName = mpw_getline( "Site name:" ); operation->siteName = mpw_getline( "Site name:" );
if (!operation->siteName) { if (!operation->siteName) {
ftl( "Missing site name.\n" ); ftl( "Missing site name." );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
} }
@ -421,7 +421,7 @@ void cli_sitesFormat(Arguments *args, Operation *operation) {
operation->sitesFormat = mpw_formatWithName( args->sitesFormat ); operation->sitesFormat = mpw_formatWithName( args->sitesFormat );
if (ERR == (int)operation->sitesFormat) { if (ERR == (int)operation->sitesFormat) {
ftl( "Invalid sites format: %s\n", args->sitesFormat ); ftl( "Invalid sites format: %s", args->sitesFormat );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
} }
@ -434,7 +434,7 @@ void cli_keyPurpose(Arguments *args, Operation *operation) {
operation->keyPurpose = mpw_purposeWithName( args->keyPurpose ); operation->keyPurpose = mpw_purposeWithName( args->keyPurpose );
if (ERR == (int)operation->keyPurpose) { if (ERR == (int)operation->keyPurpose) {
ftl( "Invalid purpose: %s\n", args->keyPurpose ); ftl( "Invalid purpose: %s", args->keyPurpose );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
} }
@ -455,7 +455,7 @@ void cli_user(Arguments *args, Operation *operation) {
mpw_free_string( &operation->sitesPath ); mpw_free_string( &operation->sitesPath );
operation->sitesPath = mpw_path( operation->fullName, mpw_marshal_format_extension( operation->sitesFormat ) ); operation->sitesPath = mpw_path( operation->fullName, mpw_marshal_format_extension( operation->sitesFormat ) );
if (!operation->sitesPath || !(sitesFile = fopen( operation->sitesPath, "r" ))) { if (!operation->sitesPath || !(sitesFile = fopen( operation->sitesPath, "r" ))) {
dbg( "Couldn't open configuration file:\n %s: %s\n", operation->sitesPath, strerror( errno ) ); dbg( "Couldn't open configuration file:\n %s: %s", operation->sitesPath, strerror( errno ) );
// Try to fall back to the flat format. // Try to fall back to the flat format.
if (!operation->sitesFormatFixed) { if (!operation->sitesFormatFixed) {
@ -464,7 +464,7 @@ void cli_user(Arguments *args, Operation *operation) {
if (operation->sitesPath && (sitesFile = fopen( operation->sitesPath, "r" ))) if (operation->sitesPath && (sitesFile = fopen( operation->sitesPath, "r" )))
operation->sitesFormat = MPMarshalFormatFlat; operation->sitesFormat = MPMarshalFormatFlat;
else else
dbg( "Couldn't open configuration file:\n %s: %s\n", operation->sitesPath, strerror( errno ) ); dbg( "Couldn't open configuration file:\n %s: %s", operation->sitesPath, strerror( errno ) );
} }
} }
@ -476,7 +476,7 @@ void cli_user(Arguments *args, Operation *operation) {
// Read file. // Read file.
char *sitesInputData = mpw_read_file( sitesFile ); char *sitesInputData = mpw_read_file( sitesFile );
if (ferror( sitesFile )) if (ferror( sitesFile ))
wrn( "Error while reading configuration file:\n %s: %d\n", operation->sitesPath, ferror( sitesFile ) ); wrn( "Error while reading configuration file:\n %s: %d", operation->sitesPath, ferror( sitesFile ) );
fclose( sitesFile ); fclose( sitesFile );
// Parse file. // Parse file.
@ -489,8 +489,8 @@ void cli_user(Arguments *args, Operation *operation) {
if (marshalError.type == MPMarshalErrorMasterPassword && operation->allowPasswordUpdate) { if (marshalError.type == MPMarshalErrorMasterPassword && operation->allowPasswordUpdate) {
// Update master password in mpsites. // Update master password in mpsites.
while (marshalError.type == MPMarshalErrorMasterPassword) { while (marshalError.type == MPMarshalErrorMasterPassword) {
inf( "Given master password does not match configuration.\n" ); inf( "Given master password does not match configuration." );
inf( "To update the configuration with this new master password, first confirm the old master password.\n" ); inf( "To update the configuration with this new master password, first confirm the old master password." );
const char *importMasterPassword = NULL; const char *importMasterPassword = NULL;
while (!importMasterPassword || !strlen( importMasterPassword )) while (!importMasterPassword || !strlen( importMasterPassword ))
@ -509,14 +509,14 @@ void cli_user(Arguments *args, Operation *operation) {
// Incorrect master password. // Incorrect master password.
if (marshalError.type == MPMarshalErrorMasterPassword) { if (marshalError.type == MPMarshalErrorMasterPassword) {
ftl( "Incorrect master password according to configuration:\n %s: %s\n", operation->sitesPath, marshalError.description ); ftl( "Incorrect master password according to configuration:\n %s: %s", operation->sitesPath, marshalError.description );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
} }
// Any other parse error. // Any other parse error.
if (!operation->user || marshalError.type != MPMarshalSuccess) { if (!operation->user || marshalError.type != MPMarshalSuccess) {
err( "Couldn't parse configuration file:\n %s: %s\n", operation->sitesPath, marshalError.description ); err( "Couldn't parse configuration file:\n %s: %s", operation->sitesPath, marshalError.description );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
} }
@ -611,7 +611,7 @@ void cli_resultType(Arguments *args, Operation *operation) {
operation->resultType = mpw_typeWithName( args->resultType ); operation->resultType = mpw_typeWithName( args->resultType );
if (ERR == (int)operation->resultType) { if (ERR == (int)operation->resultType) {
ftl( "Invalid type: %s\n", args->resultType ); ftl( "Invalid type: %s", args->resultType );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_USAGE ); exit( EX_USAGE );
} }
@ -640,7 +640,7 @@ void cli_siteCounter(Arguments *args, Operation *operation) {
long long int siteCounterInt = atoll( args->siteCounter ); long long int siteCounterInt = atoll( args->siteCounter );
if (siteCounterInt < MPCounterValueFirst || siteCounterInt > MPCounterValueLast) { if (siteCounterInt < MPCounterValueFirst || siteCounterInt > MPCounterValueLast) {
ftl( "Invalid site counter: %s\n", args->siteCounter ); ftl( "Invalid site counter: %s", args->siteCounter );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_USAGE ); exit( EX_USAGE );
} }
@ -674,7 +674,7 @@ void cli_algorithmVersion(Arguments *args, Operation *operation) {
int algorithmVersionInt = atoi( args->algorithmVersion ); int algorithmVersionInt = atoi( args->algorithmVersion );
if (algorithmVersionInt < MPAlgorithmVersionFirst || algorithmVersionInt > MPAlgorithmVersionLast) { if (algorithmVersionInt < MPAlgorithmVersionFirst || algorithmVersionInt > MPAlgorithmVersionLast) {
ftl( "Invalid algorithm version: %s\n", args->algorithmVersion ); ftl( "Invalid algorithm version: %s", args->algorithmVersion );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_USAGE ); exit( EX_USAGE );
} }
@ -687,7 +687,7 @@ void cli_sitesRedacted(Arguments *args, Operation *operation) {
operation->user->redacted = strcmp( args->sitesRedacted, "1" ) == 0; operation->user->redacted = strcmp( args->sitesRedacted, "1" ) == 0;
else if (!operation->user->redacted) else if (!operation->user->redacted)
wrn( "Sites configuration is not redacted. Use -R 1 to change this.\n" ); wrn( "Sites configuration is not redacted. Use -R 1 to change this." );
} }
void cli_mpw(Arguments *args, Operation *operation) { void cli_mpw(Arguments *args, Operation *operation) {
@ -701,7 +701,7 @@ void cli_mpw(Arguments *args, Operation *operation) {
MPMasterKey masterKey = mpw_masterKey( MPMasterKey masterKey = mpw_masterKey(
operation->user->fullName, operation->user->masterPassword, operation->site->algorithm ); operation->user->fullName, operation->user->masterPassword, operation->site->algorithm );
if (!masterKey) { if (!masterKey) {
ftl( "Couldn't derive master key.\n" ); ftl( "Couldn't derive master key." );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_SOFTWARE ); exit( EX_SOFTWARE );
} }
@ -712,7 +712,7 @@ void cli_mpw(Arguments *args, Operation *operation) {
if (!(operation->resultState = mpw_siteState( masterKey, operation->site->name, operation->siteCounter, if (!(operation->resultState = mpw_siteState( masterKey, operation->site->name, operation->siteCounter,
operation->keyPurpose, operation->keyContext, operation->resultType, operation->resultParam, operation->keyPurpose, operation->keyContext, operation->resultType, operation->resultParam,
operation->site->algorithm ))) { operation->site->algorithm ))) {
ftl( "Couldn't encrypt site result.\n" ); ftl( "Couldn't encrypt site result." );
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_SOFTWARE ); exit( EX_SOFTWARE );
@ -751,13 +751,13 @@ void cli_mpw(Arguments *args, Operation *operation) {
operation->keyPurpose, operation->keyContext, operation->resultType, operation->resultParam, operation->site->algorithm ); operation->keyPurpose, operation->keyContext, operation->resultType, operation->resultParam, operation->site->algorithm );
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
if (!result) { if (!result) {
ftl( "Couldn't generate site result.\n" ); ftl( "Couldn't generate site result." );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_SOFTWARE ); exit( EX_SOFTWARE );
} }
fprintf( stdout, "%s\n", result ); fprintf( stdout, "%s\n", result );
if (operation->site->url) if (operation->site->url)
inf( "See: %s\n", operation->site->url ); inf( "See: %s", operation->site->url );
mpw_free_string( &result ); mpw_free_string( &result );
// Update usage metadata. // Update usage metadata.
@ -775,21 +775,21 @@ void cli_save(Arguments __unused *args, Operation *operation) {
mpw_free_string( &operation->sitesPath ); mpw_free_string( &operation->sitesPath );
operation->sitesPath = mpw_path( operation->user->fullName, mpw_marshal_format_extension( operation->sitesFormat ) ); operation->sitesPath = mpw_path( operation->user->fullName, mpw_marshal_format_extension( operation->sitesFormat ) );
dbg( "Updating: %s (%s)\n", operation->sitesPath, mpw_nameForFormat( operation->sitesFormat ) ); dbg( "Updating: %s (%s)", operation->sitesPath, mpw_nameForFormat( operation->sitesFormat ) );
FILE *sitesFile = NULL; FILE *sitesFile = NULL;
if (!operation->sitesPath || !mpw_mkdirs( operation->sitesPath ) || !(sitesFile = fopen( operation->sitesPath, "w" ))) { 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 ) ); wrn( "Couldn't create updated configuration file:\n %s: %s", operation->sitesPath, strerror( errno ) );
return; return;
} }
char *buf = NULL; char *buf = NULL;
MPMarshalError marshalError = { .type = MPMarshalSuccess }; MPMarshalError marshalError = { .type = MPMarshalSuccess };
if (!mpw_marshal_write( &buf, operation->sitesFormat, operation->user, &marshalError ) || marshalError.type != MPMarshalSuccess) if (!mpw_marshal_write( &buf, operation->sitesFormat, operation->user, &marshalError ) || marshalError.type != MPMarshalSuccess)
wrn( "Couldn't encode updated configuration file:\n %s: %s\n", operation->sitesPath, marshalError.description ); wrn( "Couldn't encode updated configuration file:\n %s: %s", operation->sitesPath, marshalError.description );
else if (fwrite( buf, sizeof( char ), strlen( buf ), sitesFile ) != strlen( buf )) 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 ) ); wrn( "Error while writing updated configuration file:\n %s: %d", operation->sitesPath, ferror( sitesFile ) );
mpw_free_string( &buf ); mpw_free_string( &buf );
fclose( sitesFile ); fclose( sitesFile );

View File

@ -75,7 +75,7 @@ xmlNodePtr mpw_xmlTestCaseNode(xmlNodePtr testCaseNode, const char *nodeName) {
} }
} }
err( "Missing parent: %s, for case: %s\n", parentId, mpw_xmlTestCaseString( testCaseNode, "id" ) ); err( "Missing parent: %s, for case: %s", parentId, mpw_xmlTestCaseString( testCaseNode, "id" ) );
return NULL; return NULL;
} }

View File

@ -32,7 +32,7 @@ int main(int argc, char *const argv[]) {
xmlNodePtr tests = xmlDocGetRootElement( xmlParseFile( "mpw_tests.xml" ) ); xmlNodePtr tests = xmlDocGetRootElement( xmlParseFile( "mpw_tests.xml" ) );
if (!tests) { if (!tests) {
ftl( "Couldn't find test case: mpw_tests.xml\n" ); ftl( "Couldn't find test case: mpw_tests.xml" );
abort(); abort();
} }
@ -60,7 +60,7 @@ int main(int argc, char *const argv[]) {
do { do {
fprintf( stdout, "test case %s... ", id ); fprintf( stdout, "test case %s... ", id );
if (!xmlStrlen( result )) { if (!xmlStrlen( result )) {
fprintf( stdout, "abstract.\n" ); fprintf( stdout, "abstract." );
continue; continue;
} }
@ -68,7 +68,7 @@ int main(int argc, char *const argv[]) {
MPMasterKey masterKey = mpw_masterKey( MPMasterKey masterKey = mpw_masterKey(
(char *)fullName, (char *)masterPassword, algorithm ); (char *)fullName, (char *)masterPassword, algorithm );
if (!masterKey) { if (!masterKey) {
ftl( "Couldn't derive master key.\n" ); ftl( "Couldn't derive master key." );
abort(); abort();
} }
@ -85,7 +85,7 @@ int main(int argc, char *const argv[]) {
masterKey, (char *)siteName, siteCounter, keyPurpose, (char *)keyContext, resultType, NULL, algorithm ); masterKey, (char *)siteName, siteCounter, keyPurpose, (char *)keyContext, resultType, NULL, algorithm );
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
if (!testResult) { if (!testResult) {
ftl( "Couldn't derive site password.\n" ); ftl( "Couldn't derive site password." );
continue; continue;
} }