File: agpg.c

package info (click to toggle)
quintuple-agent 1.0.4-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,956 kB
  • ctags: 1,287
  • sloc: ansic: 11,724; sh: 3,999; makefile: 387; yacc: 316; sed: 16
file content (195 lines) | stat: -rw-r--r-- 4,879 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Quintuple Agent gpg wrapper
 * Copyright (C) 1999 Robert Bihlmeyer <robbe@orcus.priv.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define _GNU_SOURCE 1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else 
#include "getopt.h"
#endif /* HAVE_GETOPT_H */

#include "i18n.h"
#include "agentlib.h"
#include "memory.h"
#include "util.h"

#ifndef HAVE_STRDUP
#include "strdup.h"
#endif

#define GPG		"gpg"
#define GPG_KEYLIST	GPG " --list-secret-keys"
#define GPG_KEYLISTID	GPG " --list-secret-keys '%s'"

int debug = 0;

/* find_id - scans ARGV for those arguments that affect which secret key is
   used, then forks off gpg to list possible secret keys, and returns the
   first one.
   The returned string should be free()'d after use. */ 
char *find_id(int argc, char **argv)
{
  int opt_version = 0;
  struct option opts[] = {
    { "local-user",	required_argument,	NULL,		'u'},
    { "lock-once",	no_argument,		NULL,		'?'},
    { "version",	no_argument,		&opt_version,	1  },
    { NULL, 0, NULL, 0 }
  };
  int opt;
  FILE *gpg;
  char *id = NULL, *buf, *line = NULL;
  size_t size;
  ssize_t len;

  opterr = 0;
  while ((opt = getopt_long(argc, argv, "+abcdekno:qr:stu:vz", opts, NULL))
	 != -1) {
    if (opt == 'u') {
      id = optarg;
      break;
    }
  }
  if (opt_version) {
    printf("agpg " VERSION " (" PACKAGE ")\n");
    execlp(GPG, GPG, "--version", NULL);
    fprintf(stderr, _("could not exec %s: %s\n"), GPG, strerror(errno));
    exit(EXIT_FAILURE);
  }

  if (id) {
    if (asprintf(&buf, GPG_KEYLISTID, id) < 0) {
      fprintf(stderr, _("out of memory\n"));
      return NULL;
    }
  } else
    buf = GPG_KEYLIST;
  if (!(gpg = popen(buf, "r"))) {
    fprintf(stderr, _("could not exec %s: %s\n"), buf, strerror(errno));
    if (id)
      free(buf);
    return NULL;
  }
  if (id)
    free(buf);
  while ((len = getline(&line, &size, gpg)) > 0) {
    if (len > 10 && !strncmp(line, "sec ", 4) && line[10] == '/') {
      char *x;
      if ((x = strchr(line + 11, ' ')) != NULL) {
	*x = 0;
	id = strdup(line + 11);
	free(line);
	pclose(gpg);
	return id;
      }
    }
  }
  pclose(gpg);
  fprintf(stderr, _("could not determine key id\n"));
  return NULL;
}

int main(int argc, char **argv)
{
  pid_t child;
  int p[2];
  char *id;

  secmem_init(1);
  secmem_set_flags(SECMEM_WARN);
  drop_privs();

  if (getenv("AGPG_RUNNING")) {
    fprintf(stderr, _("agpg running inside itself, do you perhaps have gpg point to agpg?\n"
	      "Bailing out ...\n"));
    return EXIT_FAILURE;
  }
  if (setenv("AGPG_RUNNING", "1", 0) < 0) {
    perror(_("could not set $AGPG_RUNNING"));
    return EXIT_FAILURE;
  }

  if (!(id = find_id(argc, argv))) {
    return EXIT_FAILURE;
  }
  if (pipe(p) < 0) {
    perror(_("could not create pipe"));
    return EXIT_FAILURE;
  }
  if ((child = fork()) < 0) {
    perror(_("could not fork"));
    return EXIT_FAILURE;
  } else if (child > 0) {
    reply_get *r;
    int status;

    close(p[0]);
    if (agent_init() >= 0) {
      if (agent_get(id, &r) == STATUS_OK) {
	if (write(p[1], r->data, strlen(r->data)) < 0)
	  perror(_("error while writing passphrase"));
      } else {
	fprintf(stderr, _("agent could not provide passphrase\n"));
      }
    }
    if (close(p[1]) < 0)
      perror(_("could not finish writing passphrase"));
    agent_done();
    if (waitpid(child, &status, 0) < 0) {
      perror(_("could not wait for child"));
      return EXIT_FAILURE;
    }
    if (WIFEXITED(status))
      return WEXITSTATUS(status);
    else
      return EXIT_FAILURE;
  } else {
    char **args;
    int i;

    close(p[1]);
    args = malloc(sizeof(char *) * (argc + 2));
    if (!args) {
      fprintf(stderr, _("out of memory\n"));
      return EXIT_FAILURE;
    }
    args[0] = GPG;
    args[1] = "--passphrase-fd";
    if (asprintf(&args[2], "%d", p[0]) < 0) {
      fprintf(stderr, _("out of memory\n"));
      return EXIT_FAILURE;
    }
    for (i = 1; i < argc; i++)
      args[i+2] = argv[i];
    args[i+2] = NULL;
    execvp(GPG, args);
    return EXIT_FAILURE;
  }
}