summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 7eed5b23493dff8539d3ea542ac52369023b03ff (plain)
    1 // GPGEdit edits GPG encrypted files
    2 // Copyright (C) 2018  Aaron Ball <nullspoon@oper.io>
    3 //
    4 // This program is free software: you can redistribute it and/or modify
    5 // it under the terms of the GNU General Public License as published by
    6 // the Free Software Foundation, either version 3 of the License, or
    7 // (at your option) any later version.
    8 //
    9 // This program is distributed in the hope that it will be useful,
   10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 // GNU General Public License for more details.
   13 //
   14 // You should have received a copy of the GNU General Public License
   15 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
   16 
   17 #include <stdio.h>
   18 #include <stdlib.h>
   19 #include <string.h>
   20 #include <unistd.h>
   21 #include <gpgme.h>
   22 
   23 #include "gpg.h"
   24 #include "gpg_keyids.h"
   25 #include "strarray.h"
   26 
   27 void system_edit(char* file) {
   28   char cmd[256]; // Buffer for editor command
   29   sprintf(cmd, "%s %s\n", "vim", file);
   30   system(cmd);
   31 }
   32 
   33 
   34 int main(int argc, char* argv[]) {
   35   char  tmpfile[32] = "/tmp/gpgedit-XXXXXX";
   36   gpgme_ctx_t decctx;
   37   gpgme_error_t err;
   38   char** keyids;
   39 
   40   if(argc == 1) {
   41     printf("Please specify a gpg encrypted file to edit\n");
   42     return 1;
   43   }
   44 
   45   // Initialize decryption context, create tmp file to write decrypted contents
   46   // to, and run decryption operation.
   47   err = init_gpg(&decctx, GPGME_PROTOCOL_OPENPGP);
   48   if(err != GPG_ERR_NO_ERROR) {
   49     printf("Error: %s\n", gpgme_strerror(err));
   50     return 1;
   51   }
   52 
   53   mkstemp(tmpfile);
   54   gpgme_decrypt_result_t res = gpg_decrypt_file(argv[1], tmpfile, decctx);
   55   // Get keyids array from recipients list
   56   keyids = strarray_new();
   57   gpg_keyids_load_from_recip(&keyids, res->recipients);
   58   // Freedom!
   59   gpgme_release(decctx);
   60 
   61   // Open the system editor
   62   system_edit(tmpfile);
   63 
   64   // Re-encrypt the plaintext tmp file
   65   //printf("Re-encrypting file '%s' -> '%s'\n", tmpfile, argv[1]);
   66   gpg_encrypt_file(tmpfile, argv[1], keyids);
   67 
   68   // Clean up tmpfile
   69   strarray_release(&keyids);
   70   unlink(tmpfile);
   71   return 0;
   72 }

Generated by cgit