summaryrefslogtreecommitdiffstats
path: root/gpglib/libgpg.c
diff options
context:
space:
mode:
authorSam Varshavchik2013-08-19 16:39:41 -0400
committerSam Varshavchik2013-08-25 14:43:51 -0400
commit9c45d9ad13fdf439d44d7443ae75da15ea0223ed (patch)
tree7a81a04cb51efb078ee350859a64be2ebc6b8813 /gpglib/libgpg.c
parenta9520698b770168d1f33d6301463bb70a19655ec (diff)
downloadcourier-libs-9c45d9ad13fdf439d44d7443ae75da15ea0223ed.tar.bz2
Initial checkin
Imported from subversion report, converted to git. Updated all paths in scripts and makefiles, reflecting the new directory hierarchy.
Diffstat (limited to 'gpglib/libgpg.c')
-rw-r--r--gpglib/libgpg.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/gpglib/libgpg.c b/gpglib/libgpg.c
new file mode 100644
index 0000000..7bea385
--- /dev/null
+++ b/gpglib/libgpg.c
@@ -0,0 +1,104 @@
+/*
+** Copyright 2001-2003 Double Precision, Inc. See COPYING for
+** distribution information.
+*/
+
+
+#include "config.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#if HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+#ifndef WEXITSTATUS
+#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
+#endif
+#ifndef WIFEXITED
+#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
+#endif
+
+#include "gpg.h"
+#include "gpglib.h"
+
+int libmail_gpg_stdin= -1, libmail_gpg_stdout= -1,
+ libmail_gpg_stderr= -1;
+pid_t libmail_gpg_pid= -1;
+
+int libmail_gpg_cleanup()
+{
+ int rc=0;
+
+ if (libmail_gpg_stdin >= 0)
+ {
+ close(libmail_gpg_stdin);
+ libmail_gpg_stdin= -1;
+ }
+
+ if (libmail_gpg_stdout >= 0)
+ {
+ close(libmail_gpg_stdout);
+ libmail_gpg_stdout= -1;
+ }
+
+ if (libmail_gpg_stderr >= 0)
+ {
+ close(libmail_gpg_stderr);
+ libmail_gpg_stderr= -1;
+ }
+
+ if (libmail_gpg_pid >= 0 && kill(libmail_gpg_pid, 0) >= 0)
+ {
+ int waitstat;
+ pid_t p;
+
+ while ((p=wait(&waitstat)) != libmail_gpg_pid)
+ {
+ if (p < 0 && errno == ECHILD)
+ return (-1);
+ }
+
+ rc= WIFEXITED(waitstat) ? WEXITSTATUS(waitstat): -1;
+ libmail_gpg_pid= -1;
+ }
+ return (rc);
+}
+
+static char *optionfile(const char *gpgdir)
+{
+ char *p=malloc(strlen(gpgdir)+sizeof("/options"));
+
+ if (p)
+ strcat(strcpy(p, gpgdir), "/options");
+ return (p);
+}
+
+/*
+** Determine of GPG is available by checking for the options file, and the
+** gpg binary itself.
+*/
+
+int libmail_gpg_has_gpg(const char *gpgdir)
+{
+ char *p=optionfile(gpgdir);
+ struct stat stat_buf;
+ int rc;
+
+ if (!p)
+ return (-1);
+
+ rc=stat(p, &stat_buf);
+ free(p);
+ if (rc == 0)
+ rc=stat(GPG, &stat_buf);
+
+ return (rc);
+}