summaryrefslogtreecommitdiff
path: root/src/main.c
blob: a5afe97310a58f67c3246170c96e1d580615fb33 (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 "gpgedit_config.h"
   25 #include "strll.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 void parseargs(int argc, char* argv[], struct gpgedit_config* config) {
   35   int i = 1;
   36 
   37   while(i < argc) {
   38     if(strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--recipient") == 0) {
   39       i++;
   40       //printf("Adding recipient %s\n", argv[i]);
   41       strll_add(config->recipients, argv[i]);
   42     } else {
   43       gpgedit_config_file_set(config, argv[i]);
   44     }
   45     i++;
   46   }
   47 }
   48 
   49 
   50 int main(int argc, char* argv[]) {
   51   char  tmpfile[32] = "/tmp/gpgedit-XXXXXX";
   52   gpgme_ctx_t decctx;
   53   gpgme_error_t err;
   54   struct gpgedit_config* config;
   55 
   56   if(argc == 1) {
   57     printf("Please specify a gpg encrypted file to edit\n");
   58     return 1;
   59   }
   60 
   61   config = gpgedit_config_new();
   62   parseargs(argc, argv, config);
   63 
   64   // Initialize decryption context, create tmp file to write decrypted contents
   65   // to, and run decryption operation.
   66   err = init_gpg(&decctx, GPGME_PROTOCOL_OPENPGP);
   67   if(err != GPG_ERR_NO_ERROR) {
   68     printf("Error: %s\n", gpgme_strerror(err));
   69     return 1;
   70   }
   71 
   72   mkstemp(tmpfile);
   73   gpgme_decrypt_result_t res = gpg_decrypt_file(config->file, tmpfile, decctx);
   74   // Get keyids array from recipients list
   75   gpgedit_config_recip_from_gpgme_recip(config, res->recipients);
   76   // Freedom!
   77   gpgme_release(decctx);
   78 
   79   // Open the system editor
   80   system_edit(tmpfile);
   81 
   82   printf("Recipients:\n");
   83   strll_dump(config->recipients);
   84 
   85   // Re-encrypt the plaintext tmp file
   86   //printf("Re-encrypting file '%s' -> '%s'\n", tmpfile, config->file);
   87   gpg_encrypt_file(tmpfile, config->file, config->recipients);
   88 
   89   // Clean up tmpfile
   90   gpgedit_config_release(config);
   91   unlink(tmpfile);
   92   return 0;
   93 }

Generated by cgit