~ubuntu-branches/ubuntu/quantal/gnupg2/quantal-updates

« back to all changes in this revision

Viewing changes to g10/call-agent.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2007-09-30 02:50:40 UTC
  • mto: (5.1.14 hardy) (7.1.1 squeeze) (1.1.13 upstream)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20070930025040-xmkpdrcp5fguuqpz
Tags: upstream-2.0.7
ImportĀ upstreamĀ versionĀ 2.0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* call-agent.c - divert operations to the agent
2
 
 * Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2001, 2002, 2003, 2006, 2007 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19
 
 * USA.
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20
18
 */
21
19
 
22
 
#if 0  /* let Emacs display a red warning */
23
 
#error fixme: this shares a lot of code with the file in ../sm
24
 
#endif
25
 
 
26
20
#include <config.h>
27
21
#include <stdio.h>
28
22
#include <stdlib.h>
49
43
#endif
50
44
 
51
45
static assuan_context_t agent_ctx = NULL;
52
 
static int force_pipe_server; 
53
46
 
54
47
struct cipher_parm_s 
55
48
{
79
72
static int
80
73
start_agent (void)
81
74
{
82
 
  int rc = 0;
83
 
  char *infostr, *p;
84
 
  assuan_context_t ctx;
85
 
 
86
75
  if (agent_ctx)
87
 
    return 0; /* fixme: We need a context for each thread or serialize
 
76
    return 0; /* Fixme: We need a context for each thread or serialize
88
77
                 the access to the agent. */
89
78
 
90
 
  infostr = force_pipe_server? NULL : getenv ("GPG_AGENT_INFO");
91
 
  if (!infostr || !*infostr)
92
 
    {
93
 
      const char *pgmname;
94
 
      const char *argv[3];
95
 
      int no_close_list[3];
96
 
      int i;
97
 
 
98
 
      if (opt.verbose)
99
 
        log_info (_("no running gpg-agent - starting one\n"));
100
 
 
101
 
      if (fflush (NULL))
102
 
        {
103
 
          gpg_error_t tmperr = gpg_error_from_syserror ();
104
 
          log_error ("error flushing pending output: %s\n", strerror (errno));
105
 
          return tmperr;
106
 
        }
107
 
 
108
 
      if (!opt.agent_program || !*opt.agent_program)
109
 
        opt.agent_program = GNUPG_DEFAULT_AGENT;
110
 
      if ( !(pgmname = strrchr (opt.agent_program, '/')))
111
 
        pgmname = opt.agent_program;
112
 
      else
113
 
        pgmname++;
114
 
 
115
 
      argv[0] = pgmname;
116
 
      argv[1] = "--server";
117
 
      argv[2] = NULL;
118
 
 
119
 
      i=0;
120
 
      if (log_get_fd () != -1)
121
 
        no_close_list[i++] = log_get_fd ();
122
 
      no_close_list[i++] = fileno (stderr);
123
 
      no_close_list[i] = -1;
124
 
 
125
 
      /* connect to the agent and perform initial handshaking */
126
 
      rc = assuan_pipe_connect (&ctx, opt.agent_program, argv,
127
 
                                no_close_list);
128
 
    }
129
 
  else
130
 
    {
131
 
      int prot;
132
 
      int pid;
133
 
 
134
 
      infostr = xstrdup (infostr);
135
 
      if ( !(p = strchr (infostr, ':')) || p == infostr)
136
 
        {
137
 
          log_error (_("malformed GPG_AGENT_INFO environment variable\n"));
138
 
          xfree (infostr);
139
 
          force_pipe_server = 1;
140
 
          return start_agent ();
141
 
        }
142
 
      *p++ = 0;
143
 
      pid = atoi (p);
144
 
      while (*p && *p != ':')
145
 
        p++;
146
 
      prot = *p? atoi (p+1) : 0;
147
 
      if (prot != 1)
148
 
        {
149
 
          log_error (_("gpg-agent protocol version %d is not supported\n"),
150
 
                     prot);
151
 
          xfree (infostr);
152
 
          force_pipe_server = 1;
153
 
          return start_agent ();
154
 
        }
155
 
 
156
 
      rc = assuan_socket_connect (&ctx, infostr, pid);
157
 
      xfree (infostr);
158
 
      if (gpg_err_code (rc) == GPG_ERR_ASS_CONNECT_FAILED)
159
 
        {
160
 
          log_info (_("can't connect to the agent - trying fall back\n"));
161
 
          force_pipe_server = 1;
162
 
          return start_agent ();
163
 
        }
164
 
    }
165
 
 
166
 
  if (rc)
167
 
    {
168
 
      log_error ("can't connect to the agent: %s\n", gpg_strerror (rc));
169
 
      return gpg_error (GPG_ERR_NO_AGENT);
170
 
    }
171
 
  agent_ctx = ctx;
172
 
 
173
 
  if (DBG_ASSUAN)
174
 
    log_debug ("connection to agent established\n");
175
 
 
176
 
  rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL,NULL);
177
 
  if (rc)
178
 
    return rc;
179
 
 
180
 
  return send_pinentry_environment (agent_ctx, GPG_ERR_SOURCE_DEFAULT,
181
 
                                    opt.display, opt.ttyname, opt.ttytype,
182
 
                                    opt.lc_ctype, opt.lc_messages);
 
79
  return start_new_gpg_agent (&agent_ctx,
 
80
                              GPG_ERR_SOURCE_DEFAULT,
 
81
                              opt.homedir,
 
82
                              opt.agent_program,
 
83
                              opt.display, opt.ttyname, opt.ttytype,
 
84
                              opt.lc_ctype, opt.lc_messages,
 
85
                              opt.verbose, DBG_ASSUAN,
 
86
                              NULL, NULL);
183
87
}
184
88
 
185
89
 
594
498
}
595
499
 
596
500
/* Send a GENKEY command to the SCdaemon.  SERIALNO is not used in
597
 
   this implementation. */
 
501
   this implementation.  If CREATEDATE has been given, it will be
 
502
   passed to SCDAEMON so that the key can be created with this
 
503
   timestamp; note the user needs to use the returned timestamp as old
 
504
   versions of scddaemon don't support this option.  */
598
505
int
599
506
agent_scd_genkey (struct agent_card_genkey_s *info, int keyno, int force,
600
 
                  const char *serialno)
 
507
                  const char *serialno, u32 createtime)
601
508
{
602
509
  int rc;
603
510
  char line[ASSUAN_LINELENGTH];
 
511
  gnupg_isotime_t tbuf;
604
512
 
605
513
  rc = start_agent ();
606
514
  if (rc)
607
515
    return rc;
608
516
 
 
517
  if (createtime)
 
518
    epoch2isotime (tbuf, createtime);
 
519
  else
 
520
    *tbuf = 0;
 
521
 
609
522
  memset (info, 0, sizeof *info);
610
 
  snprintf (line, DIM(line)-1, "SCD GENKEY %s%d",
611
 
            force? "--force ":"", keyno);
 
523
  snprintf (line, DIM(line)-1, "SCD GENKEY %s%s %s %d",
 
524
            *tbuf? "--timestamp=":"", tbuf,
 
525
            force? "--force":"", 
 
526
            keyno);
612
527
  line[DIM(line)-1] = 0;
613
528
 
614
529
  memset (info, 0, sizeof *info);