summaryrefslogtreecommitdiff
path: root/src/assuan-listen.c
blob: 6755d59b87e85cffe3ed677b1f6da8d0a96bf612 (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
/* assuan-listen.c - Wait for a connection (server)
 * Copyright (C) 2001, 2002, 2004, 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 <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>

#include "assuan-defs.h"

gpg_error_t
assuan_set_hello_line (assuan_context_t ctx, const char *line)
{
  if (!ctx)
    return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
  if (!line)
    {
      _assuan_free (ctx, ctx->hello_line);
      ctx->hello_line = NULL;
    }
  else
    {
      char *buf = _assuan_malloc (ctx, 3 + strlen (line) + 1);
      if (!buf)
	return _assuan_error (ctx, gpg_err_code_from_syserror ());
      if (strchr (line, '\n'))
        strcpy (buf, line);
      else
        {
          strcpy (buf, "OK ");
          strcpy (buf+3, line);
        }
      _assuan_free (ctx, ctx->hello_line);
      ctx->hello_line = buf;
    }
  return 0;
}


/**
 * assuan_accept:
 * @ctx: context
 *
 * Cancel any existing connection and wait for a connection from a
 * client.  The initial handshake is performed which may include an
 * initial authentication or encryption negotiation.
 *
 * Return value: 0 on success or an error if the connection could for
 * some reason not be established.
 **/
gpg_error_t
assuan_accept (assuan_context_t ctx)
{
  gpg_error_t rc = 0;
  const char *p, *pend;

  if (!ctx)
    return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);

  if (ctx->max_accepts != -1)
    {
      if (ctx->max_accepts-- == 0)
	return -1; /* second invocation for pipemode -> terminate */
    }
  if (ctx->accept_handler)
    {
      /* FIXME: This should be superfluous, if everything else is
	 correct.  */
      ctx->finish_handler (ctx);
      rc = ctx->accept_handler (ctx);
      if (rc)
	return rc;
    }

  /* Send the hello. */
  p = ctx->hello_line;
  if (p && (pend = strchr (p, '\n')))
    { /* This is a multi line hello.  Send all but the last line as
         comments. */
      do
        {
          rc = _assuan_write_line (ctx, "# ", p, pend - p);
          if (rc)
            return rc;
          p = pend + 1;
          pend = strchr (p, '\n');
        }
      while (pend);
      rc = _assuan_write_line (ctx, "OK ", p, strlen (p));
    }
  else if (p)
    rc = assuan_write_line (ctx, p);
  else
    {
      static char const okstr[] = "OK Pleased to meet you";
      pid_t apid = assuan_get_pid (ctx);
      if (apid != ASSUAN_INVALID_PID)
        {
          char tmpbuf[50];
          snprintf (tmpbuf, sizeof tmpbuf, "%s, process %i", okstr, (int)apid);
          rc = assuan_write_line (ctx, tmpbuf);
        }
      else
        rc = assuan_write_line (ctx, okstr);
    }
  if (rc)
    return rc;

  return 0;
}



assuan_fd_t
assuan_get_input_fd (assuan_context_t ctx)
{
  return ctx ? ctx->input_fd : ASSUAN_INVALID_FD;
}


assuan_fd_t
assuan_get_output_fd (assuan_context_t ctx)
{
  return ctx ? ctx->output_fd : ASSUAN_INVALID_FD;
}


/* Close the fd descriptor set by the command INPUT FD=n.  We handle
   this fd inside assuan so that we can do some initial checks */
gpg_error_t
assuan_close_input_fd (assuan_context_t ctx)
{
  if (!ctx || ctx->input_fd == ASSUAN_INVALID_FD)
    return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
  _assuan_close (ctx, ctx->input_fd);
  ctx->input_fd = ASSUAN_INVALID_FD;
  return 0;
}

/* Close the fd descriptor set by the command OUTPUT FD=n.  We handle
   this fd inside assuan so that we can do some initial checks */
gpg_error_t
assuan_close_output_fd (assuan_context_t ctx)
{
  if (!ctx || ctx->output_fd == ASSUAN_INVALID_FD)
    return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);

  _assuan_close (ctx, ctx->output_fd);
  ctx->output_fd = ASSUAN_INVALID_FD;
  return 0;
}