summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 162982a3a65e3e21e77fd361eb6976490b165c16 (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 #include "gpg.h"
   23 
   24 void system_edit(char* file) {
   25   char cmd[256]; // Buffer for editor command
   26   sprintf(cmd, "%s %s\n", "vim", file);
   27   system(cmd);
   28 }
   29 
   30 
   31 int main(int argc, char* argv[]) {
   32   char  tmpfile[32] = "/tmp/gpgedit-XXXXXX";
   33   gpgme_ctx_t decctx;
   34   gpgme_error_t err;
   35 
   36   if(argc == 1) {
   37     printf("Please specify a gpg encrypted file to edit\n");
   38     return 1;
   39   }
   40 
   41   // Initialize decryption context, create tmp file to write decrypted contents
   42   // to, and run decryption operation.
   43   err = init_gpg(&decctx, GPGME_PROTOCOL_OPENPGP);
   44   if(err != GPG_ERR_NO_ERROR) {
   45     printf("Error: %s\n", gpgme_strerror(err));
   46     return 1;
   47   }
   48 
   49   mkstemp(tmpfile);
   50   gpgme_decrypt_result_t res = gpg_decrypt_file(argv[1], tmpfile, decctx);
   51   // Get keyids array from recipients list
   52   char** keyids = gpg_recip_get_keyids(res->recipients);
   53   // Freedom!
   54   gpgme_release(decctx);
   55 
   56   // Open the system editor
   57   system_edit(tmpfile);
   58 
   59   // Re-encrypt the plaintext tmp file
   60   gpg_encrypt_file(tmpfile, argv[1], keyids);
   61 
   62   // Clean up tmpfile
   63   unlink(tmpfile);
   64   return 0;
   65 }

Generated by cgit