1 /* assuan-listen.c - Wait for a connection (server)
2  * Copyright (C) 2001, 2002, 2004, 2009 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, see <http://www.gnu.org/licenses/>.
18  * SPDX-License-Identifier: LGPL-2.1+
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 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <errno.h>
32 
33 #include "assuan-defs.h"
34 
35 gpg_error_t
assuan_set_hello_line(assuan_context_t ctx,const char * line)36 assuan_set_hello_line (assuan_context_t ctx, const char *line)
37 {
38   if (!ctx)
39     return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
40   if (!line)
41     {
42       _assuan_free (ctx, ctx->hello_line);
43       ctx->hello_line = NULL;
44     }
45   else
46     {
47       char *buf = _assuan_malloc (ctx, 3 + strlen (line) + 1);
48       if (!buf)
49 	return _assuan_error (ctx, gpg_err_code_from_syserror ());
50       if (strchr (line, '\n'))
51         strcpy (buf, line);
52       else
53         {
54           strcpy (buf, "OK ");
55           strcpy (buf+3, line);
56         }
57       _assuan_free (ctx, ctx->hello_line);
58       ctx->hello_line = buf;
59     }
60   return 0;
61 }
62 
63 
64 /**
65  * assuan_accept:
66  * @ctx: context
67  *
68  * Cancel any existing connection and wait for a connection from a
69  * client.  The initial handshake is performed which may include an
70  * initial authentication or encryption negotiation.
71  *
72  * Return value: 0 on success or an error if the connection could for
73  * some reason not be established.
74  **/
75 gpg_error_t
assuan_accept(assuan_context_t ctx)76 assuan_accept (assuan_context_t ctx)
77 {
78   gpg_error_t rc = 0;
79   const char *p, *pend;
80 
81   if (!ctx)
82     return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
83 
84   if (ctx->max_accepts != -1)
85     {
86       if (ctx->max_accepts-- == 0)
87 	return -1; /* second invocation for pipemode -> terminate */
88     }
89   if (ctx->accept_handler)
90     {
91       /* FIXME: This should be superfluous, if everything else is
92 	 correct.  */
93       ctx->finish_handler (ctx);
94       rc = ctx->accept_handler (ctx);
95       if (rc)
96 	return rc;
97     }
98 
99   /* Send the hello. */
100   p = ctx->hello_line;
101   if (p && (pend = strchr (p, '\n')))
102     { /* This is a multi line hello.  Send all but the last line as
103          comments. */
104       do
105         {
106           rc = _assuan_write_line (ctx, "# ", p, pend - p);
107           if (rc)
108             return rc;
109           p = pend + 1;
110           pend = strchr (p, '\n');
111         }
112       while (pend);
113       rc = _assuan_write_line (ctx, "OK ", p, strlen (p));
114     }
115   else if (p)
116     rc = assuan_write_line (ctx, p);
117   else
118     {
119       static char const okstr[] = "OK Pleased to meet you";
120       pid_t apid = assuan_get_pid (ctx);
121       if (apid != ASSUAN_INVALID_PID)
122         {
123           char tmpbuf[50];
124           snprintf (tmpbuf, sizeof tmpbuf, "%s, process %i", okstr, (int)apid);
125           rc = assuan_write_line (ctx, tmpbuf);
126         }
127       else
128         rc = assuan_write_line (ctx, okstr);
129     }
130   if (rc)
131     return rc;
132 
133   return 0;
134 }
135 
136 
137 
138 assuan_fd_t
assuan_get_input_fd(assuan_context_t ctx)139 assuan_get_input_fd (assuan_context_t ctx)
140 {
141   return ctx ? ctx->input_fd : ASSUAN_INVALID_FD;
142 }
143 
144 
145 assuan_fd_t
assuan_get_output_fd(assuan_context_t ctx)146 assuan_get_output_fd (assuan_context_t ctx)
147 {
148   return ctx ? ctx->output_fd : ASSUAN_INVALID_FD;
149 }
150 
151 
152 /* Close the fd descriptor set by the command INPUT FD=n.  We handle
153    this fd inside assuan so that we can do some initial checks */
154 gpg_error_t
assuan_close_input_fd(assuan_context_t ctx)155 assuan_close_input_fd (assuan_context_t ctx)
156 {
157   if (!ctx || ctx->input_fd == ASSUAN_INVALID_FD)
158     return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
159   _assuan_close (ctx, ctx->input_fd);
160   ctx->input_fd = ASSUAN_INVALID_FD;
161   return 0;
162 }
163 
164 /* Close the fd descriptor set by the command OUTPUT FD=n.  We handle
165    this fd inside assuan so that we can do some initial checks */
166 gpg_error_t
assuan_close_output_fd(assuan_context_t ctx)167 assuan_close_output_fd (assuan_context_t ctx)
168 {
169   if (!ctx || ctx->output_fd == ASSUAN_INVALID_FD)
170     return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
171 
172   _assuan_close (ctx, ctx->output_fd);
173   ctx->output_fd = ASSUAN_INVALID_FD;
174   return 0;
175 }
176 
177