Back to index

courier  0.68.2
libgpg.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 
00007 #include      "config.h"
00008 #include      <stdio.h>
00009 #include      <stdlib.h>
00010 #include      <string.h>
00011 #include      <unistd.h>
00012 #include      <signal.h>
00013 #include      <errno.h>
00014 #include      <sys/types.h>
00015 #include      <sys/stat.h>
00016 #if    HAVE_SYS_TIME_H
00017 #include      <sys/time.h>
00018 #endif
00019 #if HAVE_SYS_WAIT_H
00020 #include      <sys/wait.h>
00021 #endif
00022 #ifndef WEXITSTATUS
00023 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
00024 #endif
00025 #ifndef WIFEXITED
00026 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
00027 #endif
00028 
00029 #include      "gpg.h"
00030 #include      "gpglib.h"
00031 
00032 int libmail_gpg_stdin= -1, libmail_gpg_stdout= -1,
00033        libmail_gpg_stderr= -1;
00034 pid_t libmail_gpg_pid= -1;
00035 
00036 int libmail_gpg_cleanup()
00037 {
00038        int rc=0;
00039 
00040        if (libmail_gpg_stdin >= 0)
00041        {
00042               close(libmail_gpg_stdin);
00043               libmail_gpg_stdin= -1;
00044        }
00045 
00046        if (libmail_gpg_stdout >= 0)
00047        {
00048               close(libmail_gpg_stdout);
00049               libmail_gpg_stdout= -1;
00050        }
00051 
00052        if (libmail_gpg_stderr >= 0)
00053        {
00054               close(libmail_gpg_stderr);
00055               libmail_gpg_stderr= -1;
00056        }
00057 
00058        if (libmail_gpg_pid >= 0 && kill(libmail_gpg_pid, 0) >= 0)
00059        {
00060               int waitstat;
00061               pid_t p;
00062 
00063               while ((p=wait(&waitstat)) != libmail_gpg_pid)
00064               {
00065                      if (p < 0 && errno == ECHILD)
00066                             return (-1);
00067               }
00068 
00069               rc= WIFEXITED(waitstat) ? WEXITSTATUS(waitstat): -1;
00070               libmail_gpg_pid= -1;
00071        }
00072        return (rc);
00073 }
00074 
00075 static char *optionfile(const char *gpgdir)
00076 {
00077        char *p=malloc(strlen(gpgdir)+sizeof("/options"));
00078 
00079        if (p)
00080               strcat(strcpy(p, gpgdir), "/options");
00081        return (p);
00082 }
00083 
00084 /*
00085 ** Determine of GPG is available by checking for the options file, and the
00086 ** gpg binary itself.
00087 */
00088 
00089 int libmail_gpg_has_gpg(const char *gpgdir)
00090 {
00091        char *p=optionfile(gpgdir);
00092        struct stat stat_buf;
00093        int rc;
00094 
00095        if (!p)
00096               return (-1);
00097 
00098        rc=stat(p, &stat_buf);
00099        free(p);
00100        if (rc == 0)
00101               rc=stat(GPG, &stat_buf);
00102 
00103        return (rc);
00104 }