summaryrefslogtreecommitdiffstats
path: root/gpglib/libgpg.c
blob: 7bea3857df4cc9174feb182572ea1483c47a5a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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);
}