summaryrefslogtreecommitdiff
path: root/src/client.c
blob: e0759f6f7cc580d6b38eb60947828176a6698348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* client.c - Functions common to all clients.
 * Copyright (C) 2009 Free Software Foundation, Inc.
 *
 * This file is part of Assuan.
 *
 * Assuan is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Assuan is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1+
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>

#include "assuan-defs.h"
#include "debug.h"

#define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
                     *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
#define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))


void
_assuan_client_finish (assuan_context_t ctx)
{
  if (ctx->inbound.fd != ASSUAN_INVALID_FD)
    {
      _assuan_close (ctx, ctx->inbound.fd);
      if (ctx->inbound.fd == ctx->outbound.fd)
        ctx->outbound.fd = ASSUAN_INVALID_FD;
      ctx->inbound.fd = ASSUAN_INVALID_FD;
    }
  if (ctx->outbound.fd != ASSUAN_INVALID_FD)
    {
      _assuan_close (ctx, ctx->outbound.fd);
      ctx->outbound.fd = ASSUAN_INVALID_FD;
    }
  if (ctx->pid != ASSUAN_INVALID_PID && ctx->pid)
    {
      _assuan_waitpid (ctx, ctx->pid, ctx->flags.no_waitpid, NULL, 0);
      ctx->pid = ASSUAN_INVALID_PID;
    }

  _assuan_uds_deinit (ctx);
}


/* Disconnect and release the context CTX.  */
void
_assuan_client_release (assuan_context_t ctx)
{
  assuan_write_line (ctx, "BYE");

  _assuan_client_finish (ctx);
}


/* This function also does deescaping for data lines.  */
gpg_error_t
assuan_client_read_response (assuan_context_t ctx,
			     char **line_r, int *linelen_r)
{
  gpg_error_t rc;
  char *line = NULL;
  int linelen = 0;

  *line_r = NULL;
  *linelen_r = 0;

  do
    {
      do
	{
	  rc = _assuan_read_line (ctx);
	}
      while (_assuan_error_is_eagain (ctx, rc));
      if (rc)
        return rc;
      line = ctx->inbound.line;
      linelen = ctx->inbound.linelen;
    }
  while (!linelen);

  /* For data lines, we deescape immediately.  The user will never
     have to worry about it.  */
  if (linelen >= 1 && line[0] == 'D' && line[1] == ' ')
    {
      char *s, *d;
      for (s=d=line; linelen; linelen--)
	{
	  if (*s == '%' && linelen > 2)
	    { /* handle escaping */
	      s++;
	      *d++ = xtoi_2 (s);
	      s += 2;
	      linelen -= 2;
	    }
	  else
	    *d++ = *s++;
	}
      *d = 0; /* add a hidden string terminator */

      linelen = d - line;
      ctx->inbound.linelen = linelen;
    }

  *line_r = line;
  *linelen_r = linelen;

  return 0;
}


gpg_error_t
assuan_client_parse_response (assuan_context_t ctx, char *line, int linelen,
			      assuan_response_t *response, int *off)
{
  *response = ASSUAN_RESPONSE_ERROR;
  *off = 0;

  if (linelen >= 1
      && line[0] == 'D' && line[1] == ' ')
    {
      *response = ASSUAN_RESPONSE_DATA; /* data line */
      *off = 2;
    }
  else if (linelen >= 1
           && line[0] == 'S'
           && (line[1] == '\0' || line[1] == ' '))
    {
      *response = ASSUAN_RESPONSE_STATUS;
      *off = 1;
      while (line[*off] == ' ')
        ++*off;
    }
  else if (linelen >= 2
           && line[0] == 'O' && line[1] == 'K'
           && (line[2] == '\0' || line[2] == ' '))
    {
      *response = ASSUAN_RESPONSE_OK;
      *off = 2;
      while (line[*off] == ' ')
        ++*off;
    }
  else if (linelen >= 3
           && line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
           && (line[3] == '\0' || line[3] == ' '))
    {
      *response = ASSUAN_RESPONSE_ERROR;
      *off = 3;
      while (line[*off] == ' ')
        ++*off;
    }
  else if (linelen >= 7
           && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q'
           && line[3] == 'U' && line[4] == 'I' && line[5] == 'R'
           && line[6] == 'E'
           && (line[7] == '\0' || line[7] == ' '))
    {
      *response = ASSUAN_RESPONSE_INQUIRE;
      *off = 7;
      while (line[*off] == ' ')
        ++*off;
    }
  else if (linelen >= 3
           && line[0] == 'E' && line[1] == 'N' && line[2] == 'D'
           && (line[3] == '\0' || line[3] == ' '))
    {
      *response = ASSUAN_RESPONSE_END;
      *off = 3;
    }
  else if (linelen >= 1 && line[0] == '#')
    {
      *response = ASSUAN_RESPONSE_COMMENT;
      *off = 1;
    }
  else
    return _assuan_error (ctx, GPG_ERR_ASS_INV_RESPONSE);

  return 0;
}


gpg_error_t
_assuan_read_from_server (assuan_context_t ctx, assuan_response_t *response,
			  int *off, int convey_comments)
{
  gpg_error_t rc;
  char *line;
  int linelen;

  do
    {
      *response = ASSUAN_RESPONSE_ERROR;
      *off = 0;
      rc = assuan_client_read_response (ctx, &line, &linelen);
      if (!rc)
        rc = assuan_client_parse_response (ctx, line, linelen, response, off);
    }
  while (!rc && *response == ASSUAN_RESPONSE_COMMENT && !convey_comments);

  return rc;
}


/**
 * assuan_transact:
 * @ctx: The Assuan context
 * @command: Command line to be send to the server
 * @data_cb: Callback function for data lines
 * @data_cb_arg: first argument passed to @data_cb
 * @inquire_cb: Callback function for a inquire response
 * @inquire_cb_arg: first argument passed to @inquire_cb
 * @status_cb: Callback function for a status response
 * @status_cb_arg: first argument passed to @status_cb
 *
 * FIXME: Write documentation
 *
 * Return value: 0 on success or an error code.  The error code may be
 * the one one returned by the server via error lines or from the
 * callback functions.  Take care:  If a callback returns an error
 * this function returns immediately with this error.
 **/
gpg_error_t
assuan_transact (assuan_context_t ctx,
                 const char *command,
                 gpg_error_t (*data_cb)(void *, const void *, size_t),
                 void *data_cb_arg,
                 gpg_error_t (*inquire_cb)(void*, const char *),
                 void *inquire_cb_arg,
                 gpg_error_t (*status_cb)(void*, const char *),
                 void *status_cb_arg)
{
  gpg_error_t rc;
  assuan_response_t response;
  int off;
  char *line;
  int linelen;

  rc = assuan_write_line (ctx, command);
  if (rc)
    return rc;

  if (*command == '#' || !*command)
    return 0; /* Don't expect a response for a comment line.  */

 again:
  rc = _assuan_read_from_server (ctx, &response, &off,
                                 ctx->flags.convey_comments);
  if (rc)
    return rc; /* error reading from server */

  line = ctx->inbound.line + off;
  linelen = ctx->inbound.linelen - off;

  if (response == ASSUAN_RESPONSE_ERROR)
    rc = atoi (line);
  else if (response == ASSUAN_RESPONSE_DATA)
    {
      if (!data_cb)
        rc = _assuan_error (ctx, GPG_ERR_ASS_NO_DATA_CB);
      else
        {
          rc = data_cb (data_cb_arg, line, linelen);
          if (ctx->flags.confidential)
            wipememory (ctx->inbound.line, LINELENGTH);
          if (!rc)
            goto again;
        }
    }
  else if (response == ASSUAN_RESPONSE_INQUIRE)
    {
      if (!inquire_cb)
        {
          assuan_write_line (ctx, "END"); /* get out of inquire mode */
          _assuan_read_from_server (ctx, &response, &off, 0); /* dummy read */
          rc = _assuan_error (ctx, GPG_ERR_ASS_NO_INQUIRE_CB);
        }
      else
        {
          ctx->flags.confidential_inquiry = 0;
          ctx->flags.in_inq_cb = 1;

          rc = inquire_cb (inquire_cb_arg, line);
          if (!rc)
            rc = assuan_send_data (ctx, NULL, 0); /* flush and send END */
          else
            { /* Flush and send CAN.  */
              /* Note that in this error case we don't want to return
                 an error code from sending the cancel.  The dummy
                 read is to remove the response from the server which
                 we are not interested in.  */
              assuan_send_data (ctx, NULL, 1);
              _assuan_read_from_server (ctx, &response, &off, 0);
            }

          if (ctx->flags.confidential_inquiry)
            wipememory (ctx->outbound.data.line, LINELENGTH);

          ctx->flags.confidential_inquiry = 0;
          ctx->flags.in_inq_cb = 0;

          if (!rc)
            goto again;
        }
    }
  else if (response == ASSUAN_RESPONSE_STATUS)
    {
      if (status_cb)
        rc = status_cb (status_cb_arg, line);
      if (!rc)
        goto again;
    }
  else if (response == ASSUAN_RESPONSE_COMMENT && ctx->flags.convey_comments)
    {
      line -= off; /* Send line with the comment marker.  */
      if (status_cb)
        rc = status_cb (status_cb_arg, line);
      if (!rc)
        goto again;
    }
  else if (response == ASSUAN_RESPONSE_END)
    {
      if (!data_cb)
        rc = _assuan_error (ctx, GPG_ERR_ASS_NO_DATA_CB);
      else
        {
          rc = data_cb (data_cb_arg, NULL, 0);
          if (!rc)
            goto again;
        }
    }

  return rc;
}