summaryrefslogtreecommitdiff
path: root/src/gpg.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpg.c')
-rw-r--r--src/gpg.c63
1 files changed, 21 insertions, 42 deletions
diff --git a/src/gpg.c b/src/gpg.c
index 51e614f..3656084 100644
--- a/src/gpg.c
+++ b/src/gpg.c
@@ -18,23 +18,26 @@
int gpg_failiferr(gpgme_error_t err) {
if(err != GPG_ERR_NO_ERROR) {
- printf("ERROR: ");
if(err == GPG_ERR_INV_VALUE) {
- printf("Invalid data structure\n");
- } else if(err == GPG_ERR_ENOMEM) {
- printf("Insufficient memory to allocate GPG context\n");
+ logger(LOG_FATAL, "Invalid data structure\n");
+ } else if(err == GPG_ERR_NO_DATA) {
+ logger(LOG_FATAL, "Data struct contains no encrypted data\n");
+ } else if(err == GPG_ERR_DECRYPT_FAILED) {
+ logger(LOG_FATAL, "Could not decrypt file\n");
} else if(err == GPG_ERR_ENOMEM) {
- printf("Insufficient memory to allocate GPG context\n");
+ logger(LOG_FATAL, "Insufficient memory to allocate GPG context\n");
} else if(err == GPG_ERR_NOT_OPERATIONAL) {
- printf("GPGME not initialized\n");
+ logger(LOG_FATAL, "GPGME not initialized\n");
} else if(err == GPG_ERR_UNUSABLE_PUBKEY) {
- printf("One or more public keys unusable\n");
+ logger(LOG_FATAL, "One or more public keys unusable\n");
} else if(err == GPG_ERR_BAD_PASSPHRASE) {
- printf("Incorrect passphrase\n");
+ logger(LOG_FATAL, "Incorrect passphrase\n");
+ } else if(err == GPG_ERR_NOT_IMPLEMENTED) {
+ logger(LOG_FATAL, "Not implemented\n");
} else {
- printf("Unknown error: %d\n", err);
+ logger(LOG_ERROR, "Unknown error: %d\n", err);
}
- printf("Message:\n %s\n", gpgme_strerror(err));
+ logger(LOG_ERROR, " %s\n", gpgme_strerror(err));
return err;
}
return 0;
@@ -68,7 +71,7 @@ int gpg_data_fwrite(gpgme_data_t dh, FILE* out) {
gpgme_data_seek(dh, 0, SEEK_SET);
if(gpgme_data_read(dh, buf, 1) < 1) {
- printf("Error: GPG data struct is empty. Nothing to write.\n");
+ logger(LOG_ERROR, "GPG data struct is empty. Nothing to write.\n");
return -1;
}
@@ -92,7 +95,7 @@ int gpg_recip_count(gpgme_recipient_t firstrecip) {
// Get recipient list
recip = firstrecip;
while(recip != NULL) {
- //printf("-- recipient: %s\n", recip->keyid);
+ // printf("-- recipient: %s\n", recip->keyid);
recip = recip->next;
count++;
}
@@ -162,7 +165,8 @@ size_t gpg_encrypt_file(char* infile, char* outfile, struct strll* recipll) {
gpg_failiferr(err);
// For debugging purposes
- // printf("Reading updated data from memory\n"); // Initialize data structures
+ logger(LOG_DEBUG, "Reading updated data from memory\n");
+ // Initialize data structures
err = gpgme_data_new_from_file(&indata, infile, 1);
gpg_failiferr(err);
@@ -183,11 +187,10 @@ size_t gpg_encrypt_file(char* infile, char* outfile, struct strll* recipll) {
err = gpgme_op_encrypt(ctx, keys, GPGME_ENCRYPT_ALWAYS_TRUST, indata, outdata);
gpg_failiferr(err);
- // For debugging purposes
- // printf("No errors detecting during encryption.\n");
// For debugging purposes
- // printf("Writing to disk\n");
+ logger(LOG_DEBUG, "No errors detecting during encryption. Writing to disk\n");
+
// Write to disk!
FILE* refd = fopen(outfile, "w");
gpg_data_fwrite(outdata, refd);
@@ -211,35 +214,11 @@ gpg_decrypt_file(char* infile, char* outfile, gpgme_ctx_t ctx) {
// returned.
gpgme_data_new(&outdata);
err = gpgme_data_new_from_file(&indata, infile, 1);
- if(err != GPG_ERR_NO_ERROR) {
- printf("ERROR: ");
- if(err == GPG_ERR_INV_VALUE) {
- printf("Invalid data struct or file path\n");
- } else if(err == GPG_ERR_NOT_IMPLEMENTED) {
- printf("Not implemented\n");
- } else if(err == GPG_ERR_ENOMEM) {
- printf("Insufficient memory to allocate data struct\n");
- }
- return NULL;
- }
+ gpg_failiferr(err);
// Attempt to decrypt
err = gpgme_op_decrypt(ctx, indata, outdata);
- if(err != GPG_ERR_NO_ERROR) {
- printf("ERROR: ");
- if(err == GPG_ERR_INV_VALUE) {
- printf("Invalid pointer to cipher or plain data structs\n");
- } else if(err == GPG_ERR_NO_DATA) {
- printf("Cipher struct contains no encrypted data\n");
- } else if(err == GPG_ERR_DECRYPT_FAILED) {
- printf("Ciphertext is invalid\n");
- } else if(err == GPG_ERR_BAD_PASSPHRASE) {
- printf("Passphrase is incorrect\n");
- } else {
- printf("Unhandled error\n %s\n", gpgme_strerror(err));
- }
- return NULL;
- }
+ gpg_failiferr(err);
// Write decrypted data to outfile
FILE* outfd = fopen(outfile, "w");

Generated by cgit