~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to tools/gpg-connect-agent.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include "../common/sysutils.h"
35
35
#include "../common/membuf.h"
36
36
#include "../common/ttyio.h"
 
37
#ifdef HAVE_W32_SYSTEM
 
38
#  include "../common/exechelp.h"
 
39
#endif
 
40
 
37
41
 
38
42
#define CONTROL_D ('D' - 'A' + 1)
39
43
#define octdigitp(p) (*(p) >= '0' && *(p) <= '7')
59
63
 
60
64
 
61
65
/* The list of commands and options. */
62
 
static ARGPARSE_OPTS opts[] =
63
 
  {
64
 
    { 301, NULL, 0, N_("@\nOptions:\n ") },
 
66
static ARGPARSE_OPTS opts[] = {
 
67
  ARGPARSE_group (301, N_("@\nOptions:\n ")),
65
68
    
66
 
    { oVerbose, "verbose",  0, N_("verbose") },
67
 
    { oQuiet, "quiet",      0, N_("quiet") },
68
 
    { oHex,   "hex",        0, N_("print data out hex encoded") },
69
 
    { oDecode,"decode",     0, N_("decode received data lines") },
70
 
    { oRawSocket, "raw-socket", 2, N_("|NAME|connect to Assuan socket NAME")},
71
 
    { oExec, "exec", 0, N_("run the Assuan server given on the command line")},
72
 
    { oNoExtConnect, "no-ext-connect",
73
 
                            0, N_("do not use extended connect mode")},
74
 
    { oRun,  "run", 2,         N_("|FILE|run commands from FILE on startup")},
75
 
    { oSubst, "subst", 0,      N_("run /subst on startup")}, 
76
 
    /* hidden options */
77
 
    { oNoVerbose, "no-verbose",  0, "@"},
78
 
    { oHomedir, "homedir", 2, "@" },   
79
 
    {0}
80
 
  };
 
69
  ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")),
 
70
  ARGPARSE_s_n (oQuiet, "quiet",     N_("quiet")),
 
71
  ARGPARSE_s_n (oHex,   "hex",       N_("print data out hex encoded")),
 
72
  ARGPARSE_s_n (oDecode,"decode",    N_("decode received data lines")),
 
73
  ARGPARSE_s_s (oRawSocket, "raw-socket", 
 
74
                N_("|NAME|connect to Assuan socket NAME")),
 
75
  ARGPARSE_s_n (oExec, "exec", 
 
76
                N_("run the Assuan server given on the command line")),
 
77
  ARGPARSE_s_n (oNoExtConnect, "no-ext-connect",
 
78
                N_("do not use extended connect mode")),
 
79
  ARGPARSE_s_s (oRun,  "run", 
 
80
                N_("|FILE|run commands from FILE on startup")),
 
81
  ARGPARSE_s_n (oSubst, "subst",     N_("run /subst on startup")), 
 
82
 
 
83
  ARGPARSE_s_n (oNoVerbose, "no-verbose", "@"),
 
84
  ARGPARSE_s_s (oHomedir, "homedir", "@" ),   
 
85
 
 
86
  ARGPARSE_end ()
 
87
};
81
88
 
82
89
 
83
90
/* We keep all global options in the structure OPT.  */
137
144
/* This is used to store the pid of the server.  */
138
145
static pid_t server_pid = (pid_t)(-1);
139
146
 
 
147
/* The current datasink file or NULL.  */
 
148
static FILE *current_datasink;
140
149
 
141
150
/* A list of open file descriptors. */
142
151
static struct
911
920
static void
912
921
do_recvfd (assuan_context_t ctx, char *line)
913
922
{
 
923
  (void)ctx;
 
924
  (void)line;
914
925
  log_info ("This command has not yet been implemented\n");
915
926
}
916
927
 
1134
1145
    char *condition;
1135
1146
  } loopstack[20];
1136
1147
  int        loopidx;
 
1148
  char **cmdline_commands = NULL;
1137
1149
 
1138
1150
  gnupg_rl_initialize ();
1139
1151
  set_strusage (my_strusage);
1140
1152
  log_set_prefix ("gpg-connect-agent", 1);
1141
1153
 
1142
1154
  /* Make sure that our subsystems are ready.  */
 
1155
  i18n_init();
1143
1156
  init_common_subsystems ();
1144
1157
 
1145
1158
  assuan_set_assuan_err_source (0);
1146
1159
 
1147
 
  i18n_init();
1148
1160
 
1149
1161
  opt.homedir = default_homedir ();
1150
1162
  opt.connect_flags = 1; /* Use extended connect mode.  */
1191
1203
        }
1192
1204
    }
1193
1205
  else if (argc)
1194
 
    usage (1);
 
1206
    cmdline_commands = argv;
1195
1207
 
1196
1208
  if (opt.exec && opt.raw_socket)
1197
1209
    log_info (_("option \"%s\" ignored due to \"%s\"\n"),
1279
1291
          else
1280
1292
            log_fatal ("/end command vanished\n");
1281
1293
        }
 
1294
      else if (cmdline_commands && *cmdline_commands && !script_fp)
 
1295
        {
 
1296
          keep_line = 0;
 
1297
          xfree (line);
 
1298
          line = xstrdup (*cmdline_commands);
 
1299
          cmdline_commands++;
 
1300
          n = strlen (line);
 
1301
          if (n >= maxlength)
 
1302
            maxlength = 0;
 
1303
        }
1282
1304
      else if (use_tty && !script_fp)
1283
1305
        {
1284
1306
          keep_line = 0;
1431
1453
              else
1432
1454
                add_definq (p, 0, 1);
1433
1455
            }
 
1456
          else if (!strcmp (cmd, "datafile"))
 
1457
            {
 
1458
              const char *fname;
 
1459
 
 
1460
              if (current_datasink)
 
1461
                {
 
1462
                  if (current_datasink != stdout)
 
1463
                    fclose (current_datasink);
 
1464
                  current_datasink = NULL;
 
1465
                }
 
1466
              tmpline = opt.enable_varsubst? substitute_line (p) : NULL;
 
1467
              fname = tmpline? tmpline : p;
 
1468
              if (fname && !strcmp (fname, "-"))
 
1469
                current_datasink = stdout;
 
1470
              else if (fname && *fname)
 
1471
                {
 
1472
                  current_datasink = fopen (fname, "wb");
 
1473
                  if (!current_datasink)
 
1474
                    log_error ("can't open `%s': %s\n", 
 
1475
                               fname, strerror (errno));
 
1476
                }
 
1477
              xfree (tmpline);
 
1478
            }
1434
1479
          else if (!strcmp (cmd, "showdef"))
1435
1480
            {
1436
1481
              show_definq ();
1658
1703
"/definq NAME VAR       Use content of VAR for inquiries with NAME.\n"
1659
1704
"/definqfile NAME FILE  Use content of FILE for inquiries with NAME.\n"
1660
1705
"/definqprog NAME PGM   Run PGM for inquiries with NAME.\n"
 
1706
"/datafile [NAME]       Write all D line content to file NAME.\n"
1661
1707
"/showdef               Print all definitions.\n"
1662
1708
"/cleardef              Delete all definitions.\n"
1663
1709
"/sendfd FILE MODE      Open FILE and pass descriptor to server.\n"
1668
1714
"/serverpid             Retrieve the pid of the server.\n"
1669
1715
"/[no]hex               Enable hex dumping of received data lines.\n"
1670
1716
"/[no]decode            Enable decoding of received data lines.\n"
1671
 
"/[no]subst             Enable varibale substitution.\n"
 
1717
"/[no]subst             Enable variable substitution.\n"
1672
1718
"/run FILE              Run commands from FILE.\n"
1673
1719
"/if VAR                Begin conditional block controlled by VAR.\n"
1674
1720
"/while VAR             Begin loop controlled by VAR.\n"
1861
1907
      if (linelen >= 1
1862
1908
          && line[0] == 'D' && line[1] == ' ')
1863
1909
        {
1864
 
          if (opt.hex)
 
1910
          if (current_datasink)
 
1911
            {
 
1912
              const unsigned char *s;
 
1913
              int c = 0;
 
1914
 
 
1915
              for (j=2, s=(unsigned char*)line+2; j < linelen; j++, s++ )
 
1916
                {
 
1917
                  if (*s == '%' && j+2 < linelen)
 
1918
                    { 
 
1919
                      s++; j++;
 
1920
                      c = xtoi_2 ( s );
 
1921
                      s++; j++;
 
1922
                    }
 
1923
                  else
 
1924
                    c = *s;
 
1925
                  putc (c, current_datasink);
 
1926
                }
 
1927
            }
 
1928
          else if (opt.hex)
1865
1929
            {
1866
1930
              for (i=2; i < linelen; )
1867
1931
                {
1929
1993
        {
1930
1994
          if (need_lf)
1931
1995
            {
1932
 
              putchar ('\n');
 
1996
              if (!current_datasink || current_datasink != stdout)
 
1997
                putchar ('\n');
1933
1998
              need_lf = 0;
1934
1999
            }
1935
2000
 
1937
2002
              && line[0] == 'S' 
1938
2003
              && (line[1] == '\0' || line[1] == ' '))
1939
2004
            {
1940
 
              fwrite (line, linelen, 1, stdout);
1941
 
              putchar ('\n');
 
2005
              if (!current_datasink || current_datasink != stdout)
 
2006
                {
 
2007
                  fwrite (line, linelen, 1, stdout);
 
2008
                  putchar ('\n');
 
2009
                }
1942
2010
            }  
1943
2011
          else if (linelen >= 2
1944
2012
                   && line[0] == 'O' && line[1] == 'K'
1945
2013
                   && (line[2] == '\0' || line[2] == ' '))
1946
2014
            {
1947
 
              fwrite (line, linelen, 1, stdout);
1948
 
              putchar ('\n');
 
2015
              if (!current_datasink || current_datasink != stdout)
 
2016
                {
 
2017
                  fwrite (line, linelen, 1, stdout);
 
2018
                  putchar ('\n');
 
2019
                }
1949
2020
              set_int_var ("?", 0);
1950
2021
              return 0;
1951
2022
            }
1959
2030
              if (!errval)
1960
2031
                errval = -1;
1961
2032
              set_int_var ("?", errval);
1962
 
              fwrite (line, linelen, 1, stdout);
1963
 
              putchar ('\n');
 
2033
              if (!current_datasink || current_datasink != stdout)
 
2034
                {
 
2035
                  fwrite (line, linelen, 1, stdout);
 
2036
                  putchar ('\n');
 
2037
                }
1964
2038
              *r_goterr = 1;
1965
2039
              return 0;
1966
2040
            }  
1970
2044
                   && line[6] == 'E' 
1971
2045
                   && (line[7] == '\0' || line[7] == ' '))
1972
2046
            {
1973
 
              fwrite (line, linelen, 1, stdout);
1974
 
              putchar ('\n');
 
2047
              if (!current_datasink || current_datasink != stdout)
 
2048
                {
 
2049
                  fwrite (line, linelen, 1, stdout);
 
2050
                  putchar ('\n');
 
2051
                }
1975
2052
              if (!handle_inquire (ctx, line))
1976
2053
                assuan_write_line (ctx, "CANCEL");
1977
2054
            }
1979
2056
                   && line[0] == 'E' && line[1] == 'N' && line[2] == 'D'
1980
2057
                   && (line[3] == '\0' || line[3] == ' '))
1981
2058
            {
1982
 
              fwrite (line, linelen, 1, stdout);
1983
 
              putchar ('\n');
 
2059
              if (!current_datasink || current_datasink != stdout)
 
2060
                {
 
2061
                  fwrite (line, linelen, 1, stdout);
 
2062
                  putchar ('\n');
 
2063
                }
1984
2064
              /* Received from server, thus more responses are expected.  */
1985
2065
            }
1986
2066
          else
2008
2088
      /* Check whether we can connect at the standard socket.  */
2009
2089
      sockname = make_filename (opt.homedir, "S.gpg-agent", NULL);
2010
2090
      rc = assuan_socket_connect (&ctx, sockname, 0);
 
2091
 
 
2092
#ifdef HAVE_W32_SYSTEM
 
2093
      /* If we failed to connect under Windows, we fire up the agent.  */
 
2094
      if (gpg_err_code (rc) == GPG_ERR_ASS_CONNECT_FAILED)
 
2095
        {
 
2096
          const char *agent_program;
 
2097
          const char *argv[3];
 
2098
          int save_rc = rc;
 
2099
          
 
2100
          if (opt.verbose)
 
2101
            log_info (_("no running gpg-agent - starting one\n"));
 
2102
          agent_program = gnupg_module_name (GNUPG_MODULE_NAME_AGENT);
 
2103
          
 
2104
          argv[0] = "--daemon";
 
2105
          argv[1] = "--use-standard-socket"; 
 
2106
          argv[2] = NULL;  
 
2107
 
 
2108
          rc = gnupg_spawn_process_detached (agent_program, argv, NULL);
 
2109
          if (rc)
 
2110
            log_debug ("failed to start agent `%s': %s\n",
 
2111
                       agent_program, gpg_strerror (rc));
 
2112
          else
 
2113
            {
 
2114
              /* Give the agent some time to prepare itself. */
 
2115
              gnupg_sleep (3);
 
2116
              /* Now try again to connect the agent.  */
 
2117
              rc = assuan_socket_connect (&ctx, sockname, 0);
 
2118
            }
 
2119
          if (rc)
 
2120
            rc = save_rc;
 
2121
        }
 
2122
#endif /*HAVE_W32_SYSTEM*/
2011
2123
      xfree (sockname);
2012
2124
    }
2013
2125
  else