~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-security

« back to all changes in this revision

Viewing changes to assuan/assuan-pipe-connect.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* assuan-pipe-connect.c - Establish a pipe connection (client) 
2
 
 *      Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3
 
 *
4
 
 * This file is part of Assuan.
5
 
 *
6
 
 * Assuan is free software; you can redistribute it and/or modify it
7
 
 * under the terms of the GNU Lesser General Public License as
8
 
 * published by the Free Software Foundation; either version 2.1 of
9
 
 * the License, or (at your option) any later version.
10
 
 *
11
 
 * Assuan is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * Lesser General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU Lesser General Public
17
 
 * License along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 
19
 
 */
20
 
 
21
 
#ifdef HAVE_CONFIG_H
22
 
#include <config.h>
23
 
#endif
24
 
 
25
 
#include <stdlib.h>
26
 
#include <stdio.h>
27
 
#include <string.h>
28
 
#include <signal.h>
29
 
#include <unistd.h>
30
 
#include <errno.h>
31
 
#include <fcntl.h>
32
 
#include <sys/types.h>
33
 
#include <sys/wait.h>
34
 
 
35
 
#include "assuan-defs.h"
36
 
 
37
 
#ifdef _POSIX_OPEN_MAX
38
 
#define MAX_OPEN_FDS _POSIX_OPEN_MAX
39
 
#else
40
 
#define MAX_OPEN_FDS 20
41
 
#endif
42
 
 
43
 
#ifdef HAVE_JNLIB_LOGGING
44
 
#include "../jnlib/logging.h"
45
 
#define LOGERROR1(a,b)   log_error ((a), (b))
46
 
#else
47
 
#define LOGERROR1(a,b)   fprintf (stderr, (a), (b))
48
 
#endif
49
 
 
50
 
 
51
 
 
52
 
static int
53
 
writen ( int fd, const char *buffer, size_t length )
54
 
{
55
 
  while (length)
56
 
    {
57
 
      int nwritten = write (fd, buffer, length);
58
 
      
59
 
      if (nwritten < 0)
60
 
        {
61
 
          if (errno == EINTR)
62
 
            continue;
63
 
          return -1; /* write error */
64
 
        }
65
 
      length -= nwritten;
66
 
      buffer += nwritten;
67
 
    }
68
 
  return 0;  /* okay */
69
 
}
70
 
 
71
 
 
72
 
static int
73
 
do_finish (ASSUAN_CONTEXT ctx)
74
 
{
75
 
  if (ctx->inbound.fd != -1)
76
 
    {
77
 
      close (ctx->inbound.fd);
78
 
      ctx->inbound.fd = -1;
79
 
    }
80
 
  if (ctx->outbound.fd != -1)
81
 
    {
82
 
      close (ctx->outbound.fd);
83
 
      ctx->outbound.fd = -1;
84
 
    }
85
 
  if (ctx->pid != -1)
86
 
    {
87
 
      waitpid (ctx->pid, NULL, 0);  /* FIXME Check return value.  */
88
 
      ctx->pid = -1;
89
 
    }
90
 
  return 0;
91
 
}
92
 
 
93
 
static void
94
 
do_deinit (ASSUAN_CONTEXT ctx)
95
 
{
96
 
  do_finish (ctx);
97
 
}
98
 
 
99
 
 
100
 
 
101
 
/* Connect to a server over a pipe, creating the assuan context and
102
 
   returning it in CTX.  The server filename is NAME, the argument
103
 
   vector in ARGV.  FD_CHILD_LIST is a -1 terminated list of file
104
 
   descriptors not to close in the child.  */
105
 
AssuanError
106
 
assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[],
107
 
                     int *fd_child_list)
108
 
{
109
 
  static int fixed_signals = 0;
110
 
  AssuanError err;
111
 
  int rp[2];
112
 
  int wp[2];
113
 
 
114
 
  if (!ctx || !name || !argv || !argv[0])
115
 
    return ASSUAN_Invalid_Value;
116
 
 
117
 
  if (!fixed_signals)
118
 
    { 
119
 
      struct sigaction act;
120
 
        
121
 
      sigaction (SIGPIPE, NULL, &act);
122
 
      if (act.sa_handler == SIG_DFL)
123
 
        {
124
 
          act.sa_handler = SIG_IGN;
125
 
          sigemptyset (&act.sa_mask);
126
 
          act.sa_flags = 0;
127
 
          sigaction (SIGPIPE, &act, NULL);
128
 
        }
129
 
      fixed_signals = 1;
130
 
      /* FIXME: This is not MT safe */
131
 
    }
132
 
 
133
 
  if (pipe (rp) < 0)
134
 
    return ASSUAN_General_Error;
135
 
 
136
 
  if (pipe (wp) < 0)
137
 
    {
138
 
      close (rp[0]);
139
 
      close (rp[1]);
140
 
      return ASSUAN_General_Error;
141
 
    }
142
 
  
143
 
  err = _assuan_new_context (ctx);
144
 
  if (err)
145
 
    {
146
 
      close (rp[0]);
147
 
      close (rp[1]);
148
 
      close (wp[0]);
149
 
      close (wp[1]);
150
 
      return err;
151
 
    }
152
 
  (*ctx)->pipe_mode = 1;
153
 
  (*ctx)->inbound.fd  = rp[0];  /* Our inbound is read end of read pipe. */
154
 
  (*ctx)->outbound.fd = wp[1];  /* Our outbound is write end of write pipe. */
155
 
  (*ctx)->deinit_handler = do_deinit;
156
 
  (*ctx)->finish_handler = do_finish;
157
 
 
158
 
  (*ctx)->pid = fork ();
159
 
  if ((*ctx)->pid < 0)
160
 
    {
161
 
      close (rp[0]);
162
 
      close (rp[1]);
163
 
      close (wp[0]);
164
 
      close (wp[1]);
165
 
      _assuan_release_context (*ctx); 
166
 
      return ASSUAN_General_Error;
167
 
    }
168
 
 
169
 
  if ((*ctx)->pid == 0)
170
 
    {
171
 
      int i, n;
172
 
      char errbuf[512];
173
 
      int *fdp;
174
 
 
175
 
      /* Close all files which will not be duped and are not in the
176
 
         fd_child_list. */
177
 
      n = sysconf (_SC_OPEN_MAX);
178
 
      if (n < 0)
179
 
        n = MAX_OPEN_FDS;
180
 
      for (i=0; i < n; i++)
181
 
        {
182
 
          fdp = fd_child_list;
183
 
          if (fdp)
184
 
            {
185
 
              while (*fdp != -1 && *fdp != i)
186
 
                fdp++;
187
 
            }
188
 
 
189
 
          if (!(fdp && *fdp != -1)
190
 
              && i != rp[1] && i != wp[0])
191
 
            close(i);
192
 
        }
193
 
      errno = 0;
194
 
 
195
 
      /* Dup stderr to /dev/null unless it is in the list of FDs to be
196
 
         passed to the child. */
197
 
      fdp = fd_child_list;
198
 
      if (fdp)
199
 
        {
200
 
          for (; *fdp != -1 && *fdp != STDERR_FILENO; fdp++)
201
 
            ;
202
 
        }
203
 
      if (!fdp || *fdp == -1)
204
 
        {
205
 
          int fd = open ("/dev/null", O_WRONLY);
206
 
          if (fd == -1)
207
 
            {
208
 
              LOGERROR1 ("can't open `/dev/null': %s\n", strerror (errno));
209
 
              _exit (4);
210
 
            }
211
 
          if (dup2 (fd, STDERR_FILENO) == -1)
212
 
            {
213
 
              LOGERROR1 ("dup2(dev/null, 2) failed: %s\n", strerror (errno));
214
 
              _exit (4);
215
 
            }
216
 
          close (fd);
217
 
        }
218
 
 
219
 
      /* Dup handles and to stdin/stdout and exec. */
220
 
      if (rp[1] != STDOUT_FILENO)
221
 
        {
222
 
          if (dup2 (rp[1], STDOUT_FILENO) == -1)
223
 
            {
224
 
              LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno));
225
 
              _exit (4);
226
 
            }
227
 
          close (rp[1]);
228
 
        }
229
 
      if (wp[0] != STDIN_FILENO)
230
 
        {
231
 
          if (dup2 (wp[0], STDIN_FILENO) == -1)
232
 
            {
233
 
              LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno));
234
 
              _exit (4);
235
 
            }
236
 
          close (wp[0]);
237
 
        }
238
 
 
239
 
      execv (name, argv); 
240
 
      /* oops - use the pipe to tell the parent about it */
241
 
      snprintf (errbuf, sizeof(errbuf)-1, "ERR %d can't exec `%s': %.50s\n",
242
 
                ASSUAN_Problem_Starting_Server, name, strerror (errno));
243
 
      errbuf[sizeof(errbuf)-1] = 0;
244
 
      writen (1, errbuf, strlen (errbuf));
245
 
      _exit (4);
246
 
    }
247
 
 
248
 
  close (rp[1]);
249
 
  close (wp[0]);
250
 
 
251
 
  /* initial handshake */
252
 
  {
253
 
    int okay, off;
254
 
 
255
 
    err = _assuan_read_from_server (*ctx, &okay, &off);
256
 
    if (err)
257
 
      {
258
 
        LOGERROR1 ("can't connect server: %s\n", assuan_strerror (err));
259
 
      }
260
 
    else if (okay != 1)
261
 
      {
262
 
        LOGERROR1 ("can't connect server: `%s'\n", (*ctx)->inbound.line);
263
 
        err = ASSUAN_Connect_Failed;
264
 
      }
265
 
  }
266
 
 
267
 
  if (err)
268
 
    {
269
 
      assuan_disconnect (*ctx);
270
 
      *ctx = NULL;
271
 
    }
272
 
 
273
 
  return err;
274
 
}
275
 
 
276
 
 
277
 
 
278
 
 
279
 
 
280
 
 
281
 
 
282
 
 
283
 
 
284
 
 
285
 
 
286
 
 
287
 
 
288