Back to index

courier  0.68.2
Functions
mimegpg.c File Reference
#include "config.h"
#include "gpglib.h"
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <signal.h>

Go to the source code of this file.

Functions

void libmail_gpg_noexec (int fd)
void rfc2045_error (const char *p)
static void usage ()
static void my_output (const char *p, size_t n, void *dummy)
static int my_inputfunc (char *buf, size_t buf_size, void *vp)
static void my_errhandler (const char *msg, void *dummy)
int main (int argc, char **argv)

Function Documentation

void libmail_gpg_noexec ( int  fd)

Definition at line 79 of file gpg.c.

{
#ifdef FD_CLOEXEC
       fcntl(fd, F_SETFD, FD_CLOEXEC);
#endif
}

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 78 of file mimegpg.c.

{
       int argn;
       int dosign=0;
       int doencode=0;
       int dodecode=0;
       int rc;
       const char *passphrase_fd=0;
       struct libmail_gpg_info gpg_info;

       setlocale(LC_ALL, "C");

       for (argn=1; argn < argc; argn++)
       {
              if (strcmp(argv[argn], "--") == 0)
              {
                     ++argn;
                     break;
              }

              if (strcmp(argv[argn], "-s") == 0)
              {
                     dosign=1;
                     continue;
              }

              if (strcmp(argv[argn], "-e") == 0)
              {
                     doencode=LIBMAIL_GPG_INDIVIDUAL;
                     continue;
              }

              if (strcmp(argv[argn], "-E") == 0)
              {
                     doencode=LIBMAIL_GPG_ENCAPSULATE;
                     continue;
              }

              if (strcmp(argv[argn], "-d") == 0)
              {
                     dodecode |= LIBMAIL_GPG_UNENCRYPT;
                     continue;
              }

              if (strcmp(argv[argn], "-c") == 0)
              {
                     dodecode |= LIBMAIL_GPG_CHECKSIGN;
                     continue;
              }

              if (strcmp(argv[argn], "-p") == 0)
              {
                     ++argn;
                     if (argn < argc)
                     {
                            passphrase_fd=argv[argn];
                            continue;
                     }
                     --argn;
              }

              fprintf(stderr, "Unknown option: %s\n", argv[argn]);
              exit(1);
       }

       if (!dosign && !doencode && !dodecode)
       {
              usage();
              return (1);
       }

       signal(SIGCHLD, SIG_DFL);
       signal(SIGPIPE, SIG_IGN);

       /* Make things sane */

       if (dosign || doencode)
              dodecode=0;

#if 0
       if (dosign && !doencode)
              reformime(argv[0]);
#endif

       memset(&gpg_info, 0, sizeof(gpg_info));

       gpg_info.input_func=my_inputfunc;
       gpg_info.input_func_arg=stdin;
       gpg_info.output_func=my_output;
       gpg_info.output_func_arg=stdout;
       gpg_info.errhandler_func=my_errhandler;
       gpg_info.errhandler_arg=NULL;
       gpg_info.passphrase_fd=passphrase_fd;
       gpg_info.argc=argc - argn;
       gpg_info.argv=argv + argn;

       libmail_gpg_noexec(fileno(stdin));
       libmail_gpg_noexec(fileno(stdout));

       rc=dodecode ? libmail_gpg_decode(dodecode, &gpg_info)
              : libmail_gpg_signencode(dosign, doencode, &gpg_info);


       if (rc == 0 && (fflush(stdout) || ferror(stdout)))
       {
              perror("write");
              rc=1;
       }

       exit(rc);
       return (0);
}

Here is the call graph for this function:

static void my_errhandler ( const char *  msg,
void *  dummy 
) [static]

Definition at line 73 of file mimegpg.c.

{
       fprintf(stderr, "ERROR: %s\n", msg);
}

Here is the caller graph for this function:

static int my_inputfunc ( char *  buf,
size_t  buf_size,
void *  vp 
) [static]

Definition at line 39 of file mimegpg.c.

{
       FILE *fp=(FILE *)vp;
       size_t n;

       if (buf_size <= 0)
              return -1;

       --buf_size;

       for (n=0; n<buf_size; n++)
       {
              int c=fgetc(fp);

              if (c == EOF)
              {
                     buf[n]=0;

                     return n ? 0:-1;
              }

              buf[n]=c;

              if (c == '\n')
              {
                     n++;
                     break;
              }
       }
       buf[n]=0;
       return 0;
}

Here is the caller graph for this function:

static void my_output ( const char *  p,
size_t  n,
void *  dummy 
) [static]

Definition at line 27 of file mimegpg.c.

{
       FILE *fp=(FILE *)dummy;

       if (fwrite(p, n, 1, fp) != 1)
       {
              perror("write");
              exit(1);
       }
}

Here is the caller graph for this function:

void rfc2045_error ( const char *  p)

Definition at line 16 of file mimegpg.c.

{
       fprintf(stderr, "%s\n", p);
       exit(1);
}
static void usage ( ) [static]

Definition at line 22 of file mimegpg.c.

{
       fprintf(stderr, "Usage: mimegpg [-s] [-e] [-c] [-d] -- [gpg options]\n");
}