diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..cb82676 --- /dev/null +++ b/src/main.c @@ -0,0 +1,83 @@ +// GPGEdit edits GPG encrypted files +// Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <gpgme.h> +#include <locale.h> +#include "gpg.h" + + +void system_edit(char* file) { + char cmd[256]; // Buffer for editor command + sprintf(cmd, "%s %s\n", "vim", file); + system(cmd); +} + + +int main(int argc, char* argv[]) { + char tmpfile[64] = "/tmp/gpgedit-XXXXXX"; + FILE* fd; + gpgme_data_t plain; + gpgme_ctx_t ctx; + + if(argc == 1) { + printf("Please specify a gpg encrypted file to edit\n"); + return 1; + } + + if(gpg_init(&ctx) != 0) { + printf("ERROR\n"); + return 1; + } + // Create empty initialized gpgme data struct for output + gpgme_data_new(&plain); + // Decrypt file + gpg_decrypt(argv[1], &ctx, plain); + // Get decryption result + gpgme_decrypt_result_t res = gpgme_op_decrypt_result(ctx); + + // Get recipient list + gpgme_recipient_t recip; + recip = res->recipients; + while(recip != NULL) { + printf("-- recipient: %s\n", recip->keyid); + recip = recip->next; + } + + // Create tmp file to write decrypted contents to + mkstemp(tmpfile); + fd = fopen(tmpfile, "w"); + + // Write to stdout + gpgme_data_fwrite(plain, fd); + + // Cleanup + fclose(fd); // close output file descriptor + gpgme_data_release(plain); // Release gpgme_data_t plain object + gpgme_release(ctx); + + // Open the system editor + system_edit(tmpfile); + + // TODO: Encrypt and Write tmpfile to storage + + // Clean up tmpfile + unlink(tmpfile); + return 0; +} |