summaryrefslogtreecommitdiff
path: root/src/main.c
blob: cb82676c031239e7bc48ee0f8985c81b09b62c5f (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 <locale.h>
   23 #include "gpg.h"
   24 
   25 
   26 void system_edit(char* file) {
   27   char cmd[256]; // Buffer for editor command
   28   sprintf(cmd, "%s %s\n", "vim", file);
   29   system(cmd);
   30 }
   31 
   32 
   33 int main(int argc, char* argv[]) {
   34   char  tmpfile[64] = "/tmp/gpgedit-XXXXXX";
   35   FILE* fd;
   36   gpgme_data_t plain;
   37   gpgme_ctx_t ctx;
   38 
   39   if(argc == 1) {
   40     printf("Please specify a gpg encrypted file to edit\n");
   41     return 1;
   42   }
   43 
   44   if(gpg_init(&ctx) != 0) {
   45     printf("ERROR\n");
   46     return 1;
   47   }
   48   // Create empty initialized gpgme data struct for output
   49   gpgme_data_new(&plain);
   50   // Decrypt file
   51   gpg_decrypt(argv[1], &ctx, plain);
   52   // Get decryption result
   53   gpgme_decrypt_result_t res = gpgme_op_decrypt_result(ctx);
   54 
   55   // Get recipient list
   56   gpgme_recipient_t recip;
   57   recip = res->recipients;
   58   while(recip != NULL) {
   59     printf("-- recipient: %s\n", recip->keyid);
   60     recip = recip->next;
   61   }
   62 
   63   // Create tmp file to write decrypted contents to
   64   mkstemp(tmpfile);
   65   fd = fopen(tmpfile, "w");
   66 
   67   // Write to stdout
   68   gpgme_data_fwrite(plain, fd);
   69 
   70   // Cleanup
   71   fclose(fd);                // close output file descriptor
   72   gpgme_data_release(plain); // Release gpgme_data_t plain object
   73   gpgme_release(ctx);
   74 
   75   // Open the system editor
   76   system_edit(tmpfile);
   77 
   78   // TODO: Encrypt and Write tmpfile to storage
   79 
   80   // Clean up tmpfile
   81   unlink(tmpfile);
   82   return 0;
   83 }

Generated by cgit