/* gpg-connect-agent.c - Tool to connect to the agent.
- * Copyright (C) 2005, 2007 Free Software Foundation, Inc.
+ * Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
#include "../common/sysutils.h"
#include "../common/membuf.h"
#include "../common/ttyio.h"
+#ifdef HAVE_W32_SYSTEM
+# include "../common/exechelp.h"
+#endif
+
#define CONTROL_D ('D' - 'A' + 1)
#define octdigitp(p) (*(p) >= '0' && *(p) <= '7')
oQuiet = 'q',
oVerbose = 'v',
oRawSocket = 'S',
+ oTcpSocket = 'T',
oExec = 'E',
oRun = 'r',
oSubst = 's',
/* The list of commands and options. */
-static ARGPARSE_OPTS opts[] =
- {
- { 301, NULL, 0, N_("@\nOptions:\n ") },
+static ARGPARSE_OPTS opts[] = {
+ ARGPARSE_group (301, N_("@\nOptions:\n ")),
- { oVerbose, "verbose", 0, N_("verbose") },
- { oQuiet, "quiet", 0, N_("quiet") },
- { oHex, "hex", 0, N_("print data out hex encoded") },
- { oDecode,"decode", 0, N_("decode received data lines") },
- { oRawSocket, "raw-socket", 2, N_("|NAME|connect to Assuan socket NAME")},
- { oExec, "exec", 0, N_("run the Assuan server given on the command line")},
- { oNoExtConnect, "no-ext-connect",
- 0, N_("do not use extended connect mode")},
- { oRun, "run", 2, N_("|FILE|run commands from FILE on startup")},
- { oSubst, "subst", 0, N_("run /subst on startup")},
- /* hidden options */
- { oNoVerbose, "no-verbose", 0, "@"},
- { oHomedir, "homedir", 2, "@" },
- {0}
- };
+ ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")),
+ ARGPARSE_s_n (oQuiet, "quiet", N_("quiet")),
+ ARGPARSE_s_n (oHex, "hex", N_("print data out hex encoded")),
+ ARGPARSE_s_n (oDecode,"decode", N_("decode received data lines")),
+ ARGPARSE_s_s (oRawSocket, "raw-socket",
+ N_("|NAME|connect to Assuan socket NAME")),
+ ARGPARSE_s_s (oTcpSocket, "tcp-socket",
+ N_("|ADDR|connect to Assuan server at ADDR")),
+ ARGPARSE_s_n (oExec, "exec",
+ N_("run the Assuan server given on the command line")),
+ ARGPARSE_s_n (oNoExtConnect, "no-ext-connect",
+ N_("do not use extended connect mode")),
+ ARGPARSE_s_s (oRun, "run",
+ N_("|FILE|run commands from FILE on startup")),
+ ARGPARSE_s_n (oSubst, "subst", N_("run /subst on startup")),
+
+ ARGPARSE_s_n (oNoVerbose, "no-verbose", "@"),
+ ARGPARSE_s_s (oHomedir, "homedir", "@" ),
+
+ ARGPARSE_end ()
+};
/* We keep all global options in the structure OPT. */
int hex; /* Print data lines in hex format. */
int decode; /* Decode received data lines. */
const char *raw_socket; /* Name of socket to connect in raw mode. */
+ const char *tcp_socket; /* Name of server to connect in tcp mode. */
int exec; /* Run the pgm given on the command line. */
unsigned int connect_flags; /* Flags used for connecting. */
int enable_varsubst; /* Set if variable substitution is enabled. */
/* This is used to store the pid of the server. */
static pid_t server_pid = (pid_t)(-1);
+/* The current datasink file or NULL. */
+static FILE *current_datasink;
/* A list of open file descriptors. */
static struct
/*-- local prototypes --*/
static char *substitute_line_copy (const char *buffer);
-static int read_and_print_response (assuan_context_t ctx, int *r_goterr);
+static int read_and_print_response (assuan_context_t ctx, int withhash,
+ int *r_goterr);
static assuan_context_t start_agent (void);
break;
case 13: p = VERSION; break;
case 17: p = PRINTABLE_OS_NAME; break;
- case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n");
- break;
+ case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
+
case 1:
case 40: p = _("Usage: gpg-connect-agent [options] (-h for help)");
break;
}
-/* Unescale STRING and returned the malloced result. The surrounding
+/* Unescape STRING and returned the malloced result. The surrounding
quotes must already be removed from STRING. */
static char *
unescape_string (const char *string)
result = strtol (operands, NULL, 0);
while (*operands && !spacep (operands) )
operands++;
+ if (operator == '!')
+ result = !result;
+
while (*operands)
{
while ( spacep (operands) )
return NULL;
result %= value;
break;
+ case '!': result = !value; break;
+ case '|': result = result || value; break;
+ case '&': result = result && value; break;
default:
log_error ("unknown arithmetic operator `%c'\n", operator);
return NULL;
/* Extended version of get_var. This returns a malloced string and
- understand the fucntion syntax: "func args".
+ understand the function syntax: "func args".
Defined functions are
linefeeds and carriage returns are also escaped.
"percent+" also maps spaces to plus characters.
+ errcode ARG
+ Assuming ARG is an integer, return the gpg-error code.
+
+ errsource ARG
+ Assuming ARG is an integer, return the gpg-error source.
+
+ errstring ARG
+ Assuming ARG is an integer return a formatted fpf error string.
+
+
Example: get_var_ext ("get sysconfdir") -> "/etc/gnupg"
*/
char *result;
char *p;
char *free_me = NULL;
+ int intvalue;
if (recursion_count > 50)
{
else if (!strcmp (s, "datadir"))
result = xstrdup (gnupg_datadir ());
else if (!strcmp (s, "serverpid"))
- {
- char numbuf[30];
- snprintf (numbuf, sizeof numbuf, "%d", (int)server_pid);
- result = xstrdup (numbuf);
- }
+ result = xasprintf ("%d", (int)server_pid);
else
{
log_error ("invalid argument `%s' for variable function `get'\n", s);
if (*p == ' ')
*p = '+';
}
- else if ( (s - name) == 1 && strchr ("+-*/%", *name))
+ else if ( (s - name) == 7 && !strncmp (name, "errcode", 7))
+ {
+ s++;
+ intvalue = (int)strtol (s, NULL, 0);
+ result = xasprintf ("%d", gpg_err_code (intvalue));
+ }
+ else if ( (s - name) == 9 && !strncmp (name, "errsource", 9))
+ {
+ s++;
+ intvalue = (int)strtol (s, NULL, 0);
+ result = xasprintf ("%d", gpg_err_source (intvalue));
+ }
+ else if ( (s - name) == 9 && !strncmp (name, "errstring", 9))
+ {
+ s++;
+ intvalue = (int)strtol (s, NULL, 0);
+ result = xasprintf ("%s <%s>",
+ gpg_strerror (intvalue), gpg_strsource (intvalue));
+ }
+ else if ( (s - name) == 1 && strchr ("+-*/%!|&", *name))
{
result = arithmetic_op (*name, s+1);
}
static void
do_recvfd (assuan_context_t ctx, char *line)
{
+ (void)ctx;
+ (void)line;
log_info ("This command has not yet been implemented\n");
}
-static int
+static gpg_error_t
getinfo_pid_cb (void *opaque, const void *buffer, size_t length)
{
membuf_t *mb = opaque;
loopline_t *tail;
loopline_t current;
unsigned int nestlevel;
+ int oneshot;
char *condition;
} loopstack[20];
int loopidx;
+ char **cmdline_commands = NULL;
gnupg_rl_initialize ();
set_strusage (my_strusage);
log_set_prefix ("gpg-connect-agent", 1);
/* Make sure that our subsystems are ready. */
- init_common_subsystems ();
+ i18n_init();
+ init_common_subsystems (&argc, &argv);
- assuan_set_assuan_err_source (0);
+ assuan_set_gpg_err_source (0);
- i18n_init();
opt.homedir = default_homedir ();
- opt.connect_flags = 1; /* Use extended connect mode. */
+ opt.connect_flags = 1;
/* Parse the command line. */
pargs.argc = &argc;
case oHex: opt.hex = 1; break;
case oDecode: opt.decode = 1; break;
case oRawSocket: opt.raw_socket = pargs.r.ret_str; break;
+ case oTcpSocket: opt.tcp_socket = pargs.r.ret_str; break;
case oExec: opt.exec = 1; break;
case oNoExtConnect: opt.connect_flags &= ~(1); break;
case oRun: opt_run = pargs.r.ret_str; break;
}
}
else if (argc)
- usage (1);
+ cmdline_commands = argv;
if (opt.exec && opt.raw_socket)
- log_info (_("option \"%s\" ignored due to \"%s\"\n"),
- "--raw-socket", "--exec");
+ {
+ opt.raw_socket = NULL;
+ log_info (_("option \"%s\" ignored due to \"%s\"\n"),
+ "--raw-socket", "--exec");
+ }
+ if (opt.exec && opt.tcp_socket)
+ {
+ opt.tcp_socket = NULL;
+ log_info (_("option \"%s\" ignored due to \"%s\"\n"),
+ "--tcp-socket", "--exec");
+ }
+ if (opt.tcp_socket && opt.raw_socket)
+ {
+ opt.tcp_socket = NULL;
+ log_info (_("option \"%s\" ignored due to \"%s\"\n"),
+ "--tcp-socket", "--raw-socket");
+ }
if (opt_run && !(script_fp = fopen (opt_run, "r")))
{
{
int no_close[3];
- no_close[0] = fileno (stderr);
- no_close[1] = log_get_fd ();
+ no_close[0] = assuan_fd_from_posix_fd (fileno (stderr));
+ no_close[1] = assuan_fd_from_posix_fd (log_get_fd ());
no_close[2] = -1;
- rc = assuan_pipe_connect_ext (&ctx, *argv, (const char **)argv,
- no_close, NULL, NULL,
- opt.connect_flags);
+
+ rc = assuan_new (&ctx);
+ if (rc)
+ {
+ log_error ("assuan_new failed: %s\n", gpg_strerror (rc));
+ exit (1);
+ }
+
+ rc = assuan_pipe_connect
+ (ctx, *argv, (const char **)argv, no_close, NULL, NULL,
+ (opt.connect_flags & 1) ? ASSUAN_PIPE_CONNECT_FDPASSING : 0);
if (rc)
{
log_error ("assuan_pipe_connect_ext failed: %s\n",
}
else if (opt.raw_socket)
{
- rc = assuan_socket_connect_ext (&ctx, opt.raw_socket, 0,
- opt.connect_flags);
+ rc = assuan_new (&ctx);
+ if (rc)
+ {
+ log_error ("assuan_new failed: %s\n", gpg_strerror (rc));
+ exit (1);
+ }
+
+ rc = assuan_socket_connect
+ (ctx, opt.raw_socket, 0,
+ (opt.connect_flags & 1) ? ASSUAN_SOCKET_CONNECT_FDPASSING : 0);
if (rc)
{
log_error ("can't connect to socket `%s': %s\n",
if (opt.verbose)
log_info ("connection to socket `%s' established\n", opt.raw_socket);
}
+ else if (opt.tcp_socket)
+ {
+ char *url;
+
+ url = xstrconcat ("assuan://", opt.tcp_socket, NULL);
+
+ rc = assuan_new (&ctx);
+ if (rc)
+ {
+ log_error ("assuan_new failed: %s\n", gpg_strerror (rc));
+ exit (1);
+ }
+
+ rc = assuan_socket_connect (ctx, opt.tcp_socket, 0, 0);
+ if (rc)
+ {
+ log_error ("can't connect to server `%s': %s\n",
+ opt.tcp_socket, gpg_strerror (rc));
+ exit (1);
+ }
+
+ if (opt.verbose)
+ log_info ("connection to socket `%s' established\n", url);
+
+ xfree (url);
+ }
else
ctx = start_agent ();
assuan did not run the initial handshaking). */
if (assuan_pending_line (ctx))
{
- rc = read_and_print_response (ctx, &cmderr);
+ rc = read_and_print_response (ctx, 0, &cmderr);
if (rc)
log_info (_("receiving line failed: %s\n"), gpg_strerror (rc) );
}
else
log_fatal ("/end command vanished\n");
}
+ else if (cmdline_commands && *cmdline_commands && !script_fp)
+ {
+ keep_line = 0;
+ xfree (line);
+ line = xstrdup (*cmdline_commands);
+ cmdline_commands++;
+ n = strlen (line);
+ if (n >= maxlength)
+ maxlength = 0;
+ }
else if (use_tty && !script_fp)
{
keep_line = 0;
else
add_definq (p, 0, 1);
}
+ else if (!strcmp (cmd, "datafile"))
+ {
+ const char *fname;
+
+ if (current_datasink)
+ {
+ if (current_datasink != stdout)
+ fclose (current_datasink);
+ current_datasink = NULL;
+ }
+ tmpline = opt.enable_varsubst? substitute_line (p) : NULL;
+ fname = tmpline? tmpline : p;
+ if (fname && !strcmp (fname, "-"))
+ current_datasink = stdout;
+ else if (fname && *fname)
+ {
+ current_datasink = fopen (fname, "wb");
+ if (!current_datasink)
+ log_error ("can't open `%s': %s\n",
+ fname, strerror (errno));
+ }
+ xfree (tmpline);
+ }
else if (!strcmp (cmd, "showdef"))
{
show_definq ();
{
if (loopidx+2 >= (int)DIM(loopstack))
{
- log_error ("loops are nested too deep\n");
+ log_error ("blocks are nested too deep\n");
/* We should better die or break all loop in this
case as recovering from this error won't be
easy. */
loopstack[loopidx+1].tail = &loopstack[loopidx+1].head;
loopstack[loopidx+1].current = NULL;
loopstack[loopidx+1].nestlevel = 1;
+ loopstack[loopidx+1].oneshot = 0;
loopstack[loopidx+1].condition = xstrdup (p);
loopstack[loopidx+1].collecting = 1;
}
}
+ else if (!strcmp (cmd, "if"))
+ {
+ if (loopidx+2 >= (int)DIM(loopstack))
+ {
+ log_error ("blocks are nested too deep\n");
+ }
+ else
+ {
+ /* Note that we need to evaluate the condition right
+ away and not just at the end of the block as we
+ do with a WHILE. */
+ loopstack[loopidx+1].head = NULL;
+ loopstack[loopidx+1].tail = &loopstack[loopidx+1].head;
+ loopstack[loopidx+1].current = NULL;
+ loopstack[loopidx+1].nestlevel = 1;
+ loopstack[loopidx+1].oneshot = 1;
+ loopstack[loopidx+1].condition = substitute_line_copy (p);
+ loopstack[loopidx+1].collecting = 1;
+ }
+ }
else if (!strcmp (cmd, "end"))
{
if (loopidx < 0)
/* Evaluate the condition. */
tmpcond = xstrdup (loopstack[loopidx].condition);
+ if (loopstack[loopidx].oneshot)
+ {
+ xfree (loopstack[loopidx].condition);
+ loopstack[loopidx].condition = xstrdup ("0");
+ }
tmpline = substitute_line (tmpcond);
value = tmpline? tmpline : tmpcond;
condition = strtol (value, NULL, 0);
loopstack[loopidx].current = NULL;
loopstack[loopidx].nestlevel = 0;
loopstack[loopidx].collecting = 0;
+ loopstack[loopidx].oneshot = 0;
xfree (loopstack[loopidx].condition);
loopstack[loopidx].condition = NULL;
loopidx--;
"/definq NAME VAR Use content of VAR for inquiries with NAME.\n"
"/definqfile NAME FILE Use content of FILE for inquiries with NAME.\n"
"/definqprog NAME PGM Run PGM for inquiries with NAME.\n"
+"/datafile [NAME] Write all D line content to file NAME.\n"
"/showdef Print all definitions.\n"
"/cleardef Delete all definitions.\n"
"/sendfd FILE MODE Open FILE and pass descriptor to server.\n"
"/serverpid Retrieve the pid of the server.\n"
"/[no]hex Enable hex dumping of received data lines.\n"
"/[no]decode Enable decoding of received data lines.\n"
-"/[no]subst Enable varibale substitution.\n"
+"/[no]subst Enable variable substitution.\n"
"/run FILE Run commands from FILE.\n"
+"/if VAR Begin conditional block controlled by VAR.\n"
"/while VAR Begin loop controlled by VAR.\n"
-"/end End loop.\n"
+"/end End loop or condition\n"
"/bye Terminate gpg-connect-agent.\n"
"/help Print this help.");
}
if (*line == '#' || !*line)
continue; /* Don't expect a response for a comment line. */
- rc = read_and_print_response (ctx, &cmderr);
+ rc = read_and_print_response (ctx, (!ascii_strncasecmp (line, "HELP", 4)
+ && (spacep (line+4) || !line[4])),
+ &cmderr);
if (rc)
log_info (_("receiving line failed: %s\n"), gpg_strerror (rc) );
if ((rc || cmderr) && script_fp)
/* Read all response lines from server and print them. Returns 0 on
- success or an assuan error code. Set R_GOTERR to true if the
- command did not returned OK. */
+ success or an assuan error code. If WITHHASH istrue, comment lines
+ are printed. Sets R_GOTERR to true if the command did not returned
+ OK. */
static int
-read_and_print_response (assuan_context_t ctx, int *r_goterr)
+read_and_print_response (assuan_context_t ctx, int withhash, int *r_goterr)
{
char *line;
size_t linelen;
- assuan_error_t rc;
+ gpg_error_t rc;
int i, j;
int need_lf = 0;
if (rc)
return rc;
- if (opt.verbose > 1 && *line == '#')
+ if ((withhash || opt.verbose > 1) && *line == '#')
{
fwrite (line, linelen, 1, stdout);
putchar ('\n');
if (linelen >= 1
&& line[0] == 'D' && line[1] == ' ')
{
- if (opt.hex)
+ if (current_datasink)
+ {
+ const unsigned char *s;
+ int c = 0;
+
+ for (j=2, s=(unsigned char*)line+2; j < linelen; j++, s++ )
+ {
+ if (*s == '%' && j+2 < linelen)
+ {
+ s++; j++;
+ c = xtoi_2 ( s );
+ s++; j++;
+ }
+ else
+ c = *s;
+ putc (c, current_datasink);
+ }
+ }
+ else if (opt.hex)
{
for (i=2; i < linelen; )
{
{
if (need_lf)
{
- putchar ('\n');
+ if (!current_datasink || current_datasink != stdout)
+ putchar ('\n');
need_lf = 0;
}
&& line[0] == 'S'
&& (line[1] == '\0' || line[1] == ' '))
{
- fwrite (line, linelen, 1, stdout);
- putchar ('\n');
+ if (!current_datasink || current_datasink != stdout)
+ {
+ fwrite (line, linelen, 1, stdout);
+ putchar ('\n');
+ }
}
else if (linelen >= 2
&& line[0] == 'O' && line[1] == 'K'
&& (line[2] == '\0' || line[2] == ' '))
{
- fwrite (line, linelen, 1, stdout);
- putchar ('\n');
+ if (!current_datasink || current_datasink != stdout)
+ {
+ fwrite (line, linelen, 1, stdout);
+ putchar ('\n');
+ }
+ set_int_var ("?", 0);
return 0;
}
else if (linelen >= 3
&& line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
&& (line[3] == '\0' || line[3] == ' '))
{
- fwrite (line, linelen, 1, stdout);
- putchar ('\n');
+ int errval;
+
+ errval = strtol (line+3, NULL, 10);
+ if (!errval)
+ errval = -1;
+ set_int_var ("?", errval);
+ if (!current_datasink || current_datasink != stdout)
+ {
+ fwrite (line, linelen, 1, stdout);
+ putchar ('\n');
+ }
*r_goterr = 1;
return 0;
}
&& line[6] == 'E'
&& (line[7] == '\0' || line[7] == ' '))
{
- fwrite (line, linelen, 1, stdout);
- putchar ('\n');
+ if (!current_datasink || current_datasink != stdout)
+ {
+ fwrite (line, linelen, 1, stdout);
+ putchar ('\n');
+ }
if (!handle_inquire (ctx, line))
assuan_write_line (ctx, "CANCEL");
}
&& line[0] == 'E' && line[1] == 'N' && line[2] == 'D'
&& (line[3] == '\0' || line[3] == ' '))
{
- fwrite (line, linelen, 1, stdout);
- putchar ('\n');
+ if (!current_datasink || current_datasink != stdout)
+ {
+ fwrite (line, linelen, 1, stdout);
+ putchar ('\n');
+ }
/* Received from server, thus more responses are expected. */
}
else
int rc = 0;
char *infostr, *p;
assuan_context_t ctx;
+ session_env_t session_env;
infostr = getenv ("GPG_AGENT_INFO");
if (!infostr || !*infostr)
{
char *sockname;
+ rc = assuan_new (&ctx);
+ if (rc)
+ {
+ log_error ("assuan_new failed: %s\n", gpg_strerror (rc));
+ exit (1);
+ }
+
/* Check whether we can connect at the standard socket. */
sockname = make_filename (opt.homedir, "S.gpg-agent", NULL);
- rc = assuan_socket_connect (&ctx, sockname, 0);
+ rc = assuan_socket_connect (ctx, sockname, 0, 0);
+
+#ifdef HAVE_W32_SYSTEM
+ /* If we failed to connect under Windows, we fire up the agent. */
+ if (gpg_err_code (rc) == GPG_ERR_ASS_CONNECT_FAILED)
+ {
+ const char *agent_program;
+ const char *argv[3];
+ int save_rc = rc;
+
+ if (opt.verbose)
+ log_info (_("no running gpg-agent - starting one\n"));
+ agent_program = gnupg_module_name (GNUPG_MODULE_NAME_AGENT);
+
+ argv[0] = "--daemon";
+ argv[1] = "--use-standard-socket";
+ argv[2] = NULL;
+
+ rc = gnupg_spawn_process_detached (agent_program, argv, NULL);
+ if (rc)
+ log_debug ("failed to start agent `%s': %s\n",
+ agent_program, gpg_strerror (rc));
+ else
+ {
+ /* Give the agent some time to prepare itself. */
+ gnupg_sleep (3);
+ /* Now try again to connect the agent. */
+ rc = assuan_new (&ctx);
+ if (rc)
+ {
+ log_error ("assuan_new failed: %s\n", gpg_strerror (rc));
+ exit (1);
+ }
+
+ rc = assuan_socket_connect (ctx, sockname, 0, 0);
+ }
+ if (rc)
+ rc = save_rc;
+ }
+#endif /*HAVE_W32_SYSTEM*/
xfree (sockname);
}
else
exit (1);
}
- rc = assuan_socket_connect (&ctx, infostr, pid);
+ rc = assuan_new (&ctx);
+ if (rc)
+ {
+ log_error ("assuan_new failed: %s\n", gpg_strerror (rc));
+ exit (1);
+ }
+
+ rc = assuan_socket_connect (ctx, infostr, pid, 0);
xfree (infostr);
}
exit (1);
}
+ session_env = session_env_new ();
+ if (!session_env)
+ log_fatal ("error allocating session environment block: %s\n",
+ strerror (errno));
+
rc = send_pinentry_environment (ctx, GPG_ERR_SOURCE_DEFAULT,
- NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+ NULL, NULL, session_env);
+ session_env_release (session_env);
if (rc)
{
log_error (_("error sending standard options: %s\n"), gpg_strerror (rc));