Fixed a segfault during an SSH connection failure.
[libpwmd.git] / assuan / assuan-listen.c
blob2ef9334008bdf6cf09e0f9ec6999393480d0e094
1 /* assuan-listen.c - Wait for a connection (server)
2 * Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
4 * This file is part of Assuan.
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.
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.
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/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
27 #include "assuan-defs.h"
29 assuan_error_t
30 assuan_set_hello_line (assuan_context_t ctx, const char *line)
32 if (!ctx)
33 return _assuan_error (ASSUAN_Invalid_Value);
34 if (!line)
36 xfree (ctx->hello_line);
37 ctx->hello_line = NULL;
39 else
41 char *buf = xtrymalloc (3+strlen(line)+1);
42 if (!buf)
43 return _assuan_error (ASSUAN_Out_Of_Core);
44 if (strchr (line, '\n'))
45 strcpy (buf, line);
46 else
48 strcpy (buf, "OK ");
49 strcpy (buf+3, line);
51 xfree (ctx->hello_line);
52 ctx->hello_line = buf;
54 return 0;
58 /**
59 * assuan_accept:
60 * @ctx: context
62 * Cancel any existing connection and wait for a connection from a
63 * client. The initial handshake is performed which may include an
64 * initial authentication or encryption negotiation.
66 * Return value: 0 on success or an error if the connection could for
67 * some reason not be established.
68 **/
69 assuan_error_t
70 assuan_accept (assuan_context_t ctx)
72 int rc;
73 const char *p, *pend;
75 if (!ctx)
76 return _assuan_error (ASSUAN_Invalid_Value);
78 if (ctx->pipe_mode > 1)
79 return -1; /* second invocation for pipemode -> terminate */
80 ctx->finish_handler (ctx);
82 rc = ctx->accept_handler (ctx);
83 if (rc)
84 return rc;
86 /* Send the hello. */
87 p = ctx->hello_line;
88 if (p && (pend = strchr (p, '\n')))
89 { /* This is a multi line hello. Send all but the last line as
90 comments. */
93 rc = _assuan_write_line (ctx, "# ", p, pend - p);
94 if (rc)
95 return rc;
96 p = pend + 1;
97 pend = strchr (p, '\n');
99 while (pend);
100 rc = _assuan_write_line (ctx, "OK ", p, strlen (p));
102 else if (p)
103 rc = assuan_write_line (ctx, p);
104 else
105 rc = assuan_write_line (ctx, "OK Pleased to meet you");
106 if (rc)
107 return rc;
109 if (ctx->pipe_mode)
110 ctx->pipe_mode = 2;
112 return 0;
117 assuan_fd_t
118 assuan_get_input_fd (assuan_context_t ctx)
120 return ctx? ctx->input_fd : ASSUAN_INVALID_FD;
124 assuan_fd_t
125 assuan_get_output_fd (assuan_context_t ctx)
127 return ctx? ctx->output_fd : ASSUAN_INVALID_FD;
131 /* Close the fd descriptor set by the command INPUT FD=n. We handle
132 this fd inside assuan so that we can do some initial checks */
133 assuan_error_t
134 assuan_close_input_fd (assuan_context_t ctx)
136 if (!ctx || ctx->input_fd == ASSUAN_INVALID_FD)
137 return _assuan_error (ASSUAN_Invalid_Value);
138 _assuan_close (ctx->input_fd);
139 ctx->input_fd = ASSUAN_INVALID_FD;
140 return 0;
143 /* Close the fd descriptor set by the command OUTPUT FD=n. We handle
144 this fd inside assuan so that we can do some initial checks */
145 assuan_error_t
146 assuan_close_output_fd (assuan_context_t ctx)
148 if (!ctx || ctx->output_fd == ASSUAN_INVALID_FD)
149 return _assuan_error (ASSUAN_Invalid_Value);
151 _assuan_close (ctx->output_fd);
152 ctx->output_fd = ASSUAN_INVALID_FD;
153 return 0;