~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to assuan/assuan-client.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-client.c - client functions
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
 
#include <config.h>
22
 
#include <stdlib.h>
23
 
#include <stdio.h>
24
 
#include <errno.h>
25
 
#include <unistd.h>
26
 
#include <assert.h>
27
 
 
28
 
#include "assuan-defs.h"
29
 
 
30
 
#define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
31
 
                     *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
32
 
#define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))
33
 
 
34
 
 
35
 
AssuanError
36
 
_assuan_read_from_server (ASSUAN_CONTEXT ctx, int *okay, int *off)
37
 
{
38
 
  char *line;
39
 
  int linelen;
40
 
  AssuanError rc;
41
 
 
42
 
  *okay = 0;
43
 
  *off = 0;
44
 
  do 
45
 
    {
46
 
      rc = _assuan_read_line (ctx);
47
 
      if (rc)
48
 
        return rc;
49
 
      line = ctx->inbound.line;
50
 
      linelen = ctx->inbound.linelen;
51
 
    }    
52
 
  while (*line == '#' || !linelen);
53
 
 
54
 
  if (linelen >= 1
55
 
      && line[0] == 'D' && line[1] == ' ')
56
 
    {
57
 
      *okay = 2; /* data line */
58
 
      *off = 2;
59
 
    }
60
 
  else if (linelen >= 1
61
 
           && line[0] == 'S' 
62
 
           && (line[1] == '\0' || line[1] == ' '))
63
 
    {
64
 
      *okay = 4;
65
 
      *off = 1;
66
 
      while (line[*off] == ' ')
67
 
        ++*off;
68
 
    }  
69
 
  else if (linelen >= 2
70
 
           && line[0] == 'O' && line[1] == 'K'
71
 
           && (line[2] == '\0' || line[2] == ' '))
72
 
    {
73
 
      *okay = 1;
74
 
      *off = 2;
75
 
      while (line[*off] == ' ')
76
 
        ++*off;
77
 
    }
78
 
  else if (linelen >= 3
79
 
           && line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
80
 
           && (line[3] == '\0' || line[3] == ' '))
81
 
    {
82
 
      *okay = 0;
83
 
      *off = 3;
84
 
      while (line[*off] == ' ')
85
 
        ++*off;
86
 
    }  
87
 
  else if (linelen >= 7
88
 
           && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q'
89
 
           && line[3] == 'U' && line[4] == 'I' && line[5] == 'R'
90
 
           && line[6] == 'E' 
91
 
           && (line[7] == '\0' || line[7] == ' '))
92
 
    {
93
 
      *okay = 3;
94
 
      *off = 7;
95
 
      while (line[*off] == ' ')
96
 
        ++*off;
97
 
    }
98
 
  else if (linelen >= 3
99
 
           && line[0] == 'E' && line[1] == 'N' && line[2] == 'D'
100
 
           && (line[3] == '\0' || line[3] == ' '))
101
 
    {
102
 
      *okay = 5; /* end line */
103
 
      *off = 3;
104
 
    }
105
 
  else
106
 
    rc = ASSUAN_Invalid_Response;
107
 
  return rc;
108
 
}
109
 
 
110
 
 
111
 
 
112
 
/**
113
 
 * assuan_transact:
114
 
 * @ctx: The Assuan context
115
 
 * @command: Coimmand line to be send to server
116
 
 * @data_cb: Callback function for data lines
117
 
 * @data_cb_arg: first argument passed to @data_cb
118
 
 * @inquire_cb: Callback function for a inquire response
119
 
 * @inquire_cb_arg: first argument passed to @inquire_cb
120
 
 * @status_cb: Callback function for a status response
121
 
 * @status_cb_arg: first argument passed to @status_cb
122
 
 * 
123
 
 * FIXME: Write documentation
124
 
 * 
125
 
 * Return value: 0 on success or error code.  The error code may be
126
 
 * the one one returned by the server in error lines or from the
127
 
 * callback functions.
128
 
 **/
129
 
AssuanError
130
 
assuan_transact (ASSUAN_CONTEXT ctx,
131
 
                 const char *command,
132
 
                 AssuanError (*data_cb)(void *, const void *, size_t),
133
 
                 void *data_cb_arg,
134
 
                 AssuanError (*inquire_cb)(void*, const char *),
135
 
                 void *inquire_cb_arg,
136
 
                 AssuanError (*status_cb)(void*, const char *),
137
 
                 void *status_cb_arg)
138
 
{
139
 
  int rc, okay, off;
140
 
  unsigned char *line;
141
 
  int linelen;
142
 
 
143
 
  rc = assuan_write_line (ctx, command);
144
 
  if (rc)
145
 
    return rc;
146
 
 
147
 
 again:
148
 
  rc = _assuan_read_from_server (ctx, &okay, &off);
149
 
  if (rc)
150
 
    return rc; /* error reading from server */
151
 
 
152
 
  line = ctx->inbound.line + off;
153
 
  linelen = ctx->inbound.linelen - off;
154
 
 
155
 
  if (!okay)
156
 
    {
157
 
      rc = atoi (line);
158
 
      if (rc < 100)
159
 
        rc = ASSUAN_Server_Fault;
160
 
    }
161
 
  else if (okay == 2)
162
 
    {
163
 
      if (!data_cb)
164
 
        rc = ASSUAN_No_Data_Callback;
165
 
      else 
166
 
        {
167
 
          unsigned char *s, *d;
168
 
 
169
 
          for (s=d=line; linelen; linelen--)
170
 
            {
171
 
              if (*s == '%' && linelen > 2)
172
 
                { /* handle escaping */
173
 
                  s++;
174
 
                  *d++ = xtoi_2 (s);
175
 
                  s += 2;
176
 
                  linelen -= 2;
177
 
                }
178
 
              else
179
 
                *d++ = *s++;
180
 
            }
181
 
          *d = 0; /* add a hidden string terminator */
182
 
          rc = data_cb (data_cb_arg, line, d - line);
183
 
          if (!rc)
184
 
            goto again;
185
 
        }
186
 
    }
187
 
  else if (okay == 3)
188
 
    {
189
 
      if (!inquire_cb)
190
 
        {
191
 
          assuan_write_line (ctx, "END"); /* get out of inquire mode */
192
 
          _assuan_read_from_server (ctx, &okay, &off); /* dummy read */
193
 
          rc = ASSUAN_No_Inquire_Callback;
194
 
        }
195
 
      else
196
 
        {
197
 
          rc = inquire_cb (inquire_cb_arg, line);
198
 
          if (!rc)
199
 
            rc = assuan_send_data (ctx, NULL, 0); /* flush and send END */
200
 
          if (!rc)
201
 
            goto again;
202
 
        }
203
 
    }
204
 
  else if (okay == 4)
205
 
    {
206
 
      if (status_cb)
207
 
        rc = status_cb (status_cb_arg, line);
208
 
      if (!rc)
209
 
        goto again;
210
 
    }
211
 
  else if (okay == 5)
212
 
    {
213
 
      if (!data_cb)
214
 
        rc = ASSUAN_No_Data_Callback;
215
 
      else 
216
 
        {
217
 
          rc = data_cb (data_cb_arg, NULL, 0);
218
 
          if (!rc)
219
 
            goto again;
220
 
        }
221
 
    }
222
 
 
223
 
  return rc;
224
 
}
225