Back to index

courier  0.68.2
mimegpg.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001-2003 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 #include "config.h"
00007 #include "gpglib.h"
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <locale.h>
00011 #include <string.h>
00012 #include <signal.h>
00013 
00014 extern void libmail_gpg_noexec(int fd);
00015 
00016 void rfc2045_error(const char *p)
00017 {
00018        fprintf(stderr, "%s\n", p);
00019        exit(1);
00020 }
00021 
00022 static void usage()
00023 {
00024        fprintf(stderr, "Usage: mimegpg [-s] [-e] [-c] [-d] -- [gpg options]\n");
00025 }
00026 
00027 static void my_output(const char *p, size_t n,
00028                     void *dummy)
00029 {
00030        FILE *fp=(FILE *)dummy;
00031 
00032        if (fwrite(p, n, 1, fp) != 1)
00033        {
00034               perror("write");
00035               exit(1);
00036        }
00037 }
00038 
00039 static int my_inputfunc(char *buf, size_t buf_size, void *vp)
00040 {
00041        FILE *fp=(FILE *)vp;
00042        size_t n;
00043 
00044        if (buf_size <= 0)
00045               return -1;
00046 
00047        --buf_size;
00048 
00049        for (n=0; n<buf_size; n++)
00050        {
00051               int c=fgetc(fp);
00052 
00053               if (c == EOF)
00054               {
00055                      buf[n]=0;
00056 
00057                      return n ? 0:-1;
00058               }
00059 
00060               buf[n]=c;
00061 
00062               if (c == '\n')
00063               {
00064                      n++;
00065                      break;
00066               }
00067        }
00068        buf[n]=0;
00069        return 0;
00070 }
00071 
00072 
00073 static void my_errhandler(const char *msg, void *dummy)
00074 {
00075        fprintf(stderr, "ERROR: %s\n", msg);
00076 }
00077 
00078 int main(int argc, char **argv)
00079 {
00080        int argn;
00081        int dosign=0;
00082        int doencode=0;
00083        int dodecode=0;
00084        int rc;
00085        const char *passphrase_fd=0;
00086        struct libmail_gpg_info gpg_info;
00087 
00088        setlocale(LC_ALL, "C");
00089 
00090        for (argn=1; argn < argc; argn++)
00091        {
00092               if (strcmp(argv[argn], "--") == 0)
00093               {
00094                      ++argn;
00095                      break;
00096               }
00097 
00098               if (strcmp(argv[argn], "-s") == 0)
00099               {
00100                      dosign=1;
00101                      continue;
00102               }
00103 
00104               if (strcmp(argv[argn], "-e") == 0)
00105               {
00106                      doencode=LIBMAIL_GPG_INDIVIDUAL;
00107                      continue;
00108               }
00109 
00110               if (strcmp(argv[argn], "-E") == 0)
00111               {
00112                      doencode=LIBMAIL_GPG_ENCAPSULATE;
00113                      continue;
00114               }
00115 
00116               if (strcmp(argv[argn], "-d") == 0)
00117               {
00118                      dodecode |= LIBMAIL_GPG_UNENCRYPT;
00119                      continue;
00120               }
00121 
00122               if (strcmp(argv[argn], "-c") == 0)
00123               {
00124                      dodecode |= LIBMAIL_GPG_CHECKSIGN;
00125                      continue;
00126               }
00127 
00128               if (strcmp(argv[argn], "-p") == 0)
00129               {
00130                      ++argn;
00131                      if (argn < argc)
00132                      {
00133                             passphrase_fd=argv[argn];
00134                             continue;
00135                      }
00136                      --argn;
00137               }
00138 
00139               fprintf(stderr, "Unknown option: %s\n", argv[argn]);
00140               exit(1);
00141        }
00142 
00143        if (!dosign && !doencode && !dodecode)
00144        {
00145               usage();
00146               return (1);
00147        }
00148 
00149        signal(SIGCHLD, SIG_DFL);
00150        signal(SIGPIPE, SIG_IGN);
00151 
00152        /* Make things sane */
00153 
00154        if (dosign || doencode)
00155               dodecode=0;
00156 
00157 #if 0
00158        if (dosign && !doencode)
00159               reformime(argv[0]);
00160 #endif
00161 
00162        memset(&gpg_info, 0, sizeof(gpg_info));
00163 
00164        gpg_info.input_func=my_inputfunc;
00165        gpg_info.input_func_arg=stdin;
00166        gpg_info.output_func=my_output;
00167        gpg_info.output_func_arg=stdout;
00168        gpg_info.errhandler_func=my_errhandler;
00169        gpg_info.errhandler_arg=NULL;
00170        gpg_info.passphrase_fd=passphrase_fd;
00171        gpg_info.argc=argc - argn;
00172        gpg_info.argv=argv + argn;
00173 
00174        libmail_gpg_noexec(fileno(stdin));
00175        libmail_gpg_noexec(fileno(stdout));
00176 
00177        rc=dodecode ? libmail_gpg_decode(dodecode, &gpg_info)
00178               : libmail_gpg_signencode(dosign, doencode, &gpg_info);
00179 
00180 
00181        if (rc == 0 && (fflush(stdout) || ferror(stdout)))
00182        {
00183               perror("write");
00184               rc=1;
00185        }
00186 
00187        exit(rc);
00188        return (0);
00189 }