| 1 | /* assuan-listen.c - Wait for a connection (server) |
| 2 | * Copyright (C) 2001, 2002, 2004 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 | */ |
| 19 | |
| 20 | #include <config.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #include "assuan-defs.h" |
| 28 | |
| 29 | assuan_error_t |
| 30 | assuan_set_hello_line (assuan_context_t ctx, const char *line) |
| 31 | { |
| 32 | if (!ctx) |
| 33 | return _assuan_error (ASSUAN_Invalid_Value); |
| 34 | if (!line) |
| 35 | { |
| 36 | xfree (ctx->hello_line); |
| 37 | ctx->hello_line = NULL; |
| 38 | } |
| 39 | else |
| 40 | { |
| 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 |
| 47 | { |
| 48 | strcpy (buf, "OK "); |
| 49 | strcpy (buf+3, line); |
| 50 | } |
| 51 | xfree (ctx->hello_line); |
| 52 | ctx->hello_line = buf; |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * assuan_accept: |
| 60 | * @ctx: context |
| 61 | * |
| 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. |
| 65 | * |
| 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) |
| 71 | { |
| 72 | int rc; |
| 73 | const char *p, *pend; |
| 74 | |
| 75 | if (!ctx) |
| 76 | return _assuan_error (ASSUAN_Invalid_Value); |
| 77 | |
| 78 | if (ctx->pipe_mode > 1) |
| 79 | return -1; /* second invocation for pipemode -> terminate */ |
| 80 | ctx->finish_handler (ctx); |
| 81 | |
| 82 | rc = ctx->accept_handler (ctx); |
| 83 | if (rc) |
| 84 | return rc; |
| 85 | |
| 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. */ |
| 91 | do |
| 92 | { |
| 93 | rc = _assuan_write_line (ctx, "# ", p, pend - p); |
| 94 | if (rc) |
| 95 | return rc; |
| 96 | p = pend + 1; |
| 97 | pend = strchr (p, '\n'); |
| 98 | } |
| 99 | while (pend); |
| 100 | rc = _assuan_write_line (ctx, "OK ", p, strlen (p)); |
| 101 | } |
| 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; |
| 108 | |
| 109 | if (ctx->pipe_mode) |
| 110 | ctx->pipe_mode = 2; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | |
| 117 | assuan_fd_t |
| 118 | assuan_get_input_fd (assuan_context_t ctx) |
| 119 | { |
| 120 | return ctx? ctx->input_fd : ASSUAN_INVALID_FD; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | assuan_fd_t |
| 125 | assuan_get_output_fd (assuan_context_t ctx) |
| 126 | { |
| 127 | return ctx? ctx->output_fd : ASSUAN_INVALID_FD; |
| 128 | } |
| 129 | |
| 130 | |
| 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) |
| 135 | { |
| 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; |
| 141 | } |
| 142 | |
| 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) |
| 147 | { |
| 148 | if (!ctx || ctx->output_fd == ASSUAN_INVALID_FD) |
| 149 | return _assuan_error (ASSUAN_Invalid_Value); |
| 150 | |
| 151 | _assuan_close (ctx->output_fd); |
| 152 | ctx->output_fd = ASSUAN_INVALID_FD; |
| 153 | return 0; |
| 154 | } |
| 155 |