| 1 | /* assuan-listen.c - Wait for a connection (server) |
| 2 | * Copyright (C) 2001 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of GnuPG. |
| 5 | * |
| 6 | * GnuPG is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * GnuPG is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA |
| 19 | */ |
| 20 | |
| 21 | #include <config.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "assuan-defs.h" |
| 28 | |
| 29 | AssuanError |
| 30 | assuan_set_hello_line (ASSUAN_CONTEXT ctx, const char *line) |
| 31 | { |
| 32 | if (!ctx) |
| 33 | return 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_Out_Of_Core; |
| 44 | strcpy (buf, "OK "); |
| 45 | strcpy (buf+3, line); |
| 46 | xfree (ctx->hello_line); |
| 47 | ctx->hello_line = buf; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * assuan_accept: |
| 55 | * @ctx: context |
| 56 | * |
| 57 | * Cancel any existing connectiion and wait for a connection from a |
| 58 | * client. The initial handshake is performed which may include an |
| 59 | * initial authentication or encryption negotiation. |
| 60 | * |
| 61 | * Return value: 0 on success or an error if the connection could for |
| 62 | * some reason not be established. |
| 63 | **/ |
| 64 | AssuanError |
| 65 | assuan_accept (ASSUAN_CONTEXT ctx) |
| 66 | { |
| 67 | int rc; |
| 68 | |
| 69 | if (!ctx) |
| 70 | return ASSUAN_Invalid_Value; |
| 71 | |
| 72 | if (ctx->pipe_mode > 1) |
| 73 | return -1; /* second invocation for pipemode -> terminate */ |
| 74 | ctx->finish_handler (ctx); |
| 75 | |
| 76 | rc = ctx->accept_handler (ctx); |
| 77 | if (rc) |
| 78 | return rc; |
| 79 | |
| 80 | /* send the hello */ |
| 81 | rc = assuan_write_line (ctx, ctx->hello_line? ctx->hello_line |
| 82 | : "OK Your orders please"); |
| 83 | if (rc) |
| 84 | return rc; |
| 85 | |
| 86 | if (ctx->pipe_mode) |
| 87 | ctx->pipe_mode = 2; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | int |
| 95 | assuan_get_input_fd (ASSUAN_CONTEXT ctx) |
| 96 | { |
| 97 | return ctx? ctx->input_fd : -1; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | int |
| 102 | assuan_get_output_fd (ASSUAN_CONTEXT ctx) |
| 103 | { |
| 104 | return ctx? ctx->output_fd : -1; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /* Close the fd descriptor set by the command INPUT FD=n. We handle |
| 109 | this fd inside assuan so that we can do some initial checks */ |
| 110 | AssuanError |
| 111 | assuan_close_input_fd (ASSUAN_CONTEXT ctx) |
| 112 | { |
| 113 | if (!ctx || ctx->input_fd == -1) |
| 114 | return ASSUAN_Invalid_Value; |
| 115 | close (ctx->input_fd); |
| 116 | ctx->input_fd = -1; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* Close the fd descriptor set by the command OUTPUT FD=n. We handle |
| 121 | this fd inside assuan so that we can do some initial checks */ |
| 122 | AssuanError |
| 123 | assuan_close_output_fd (ASSUAN_CONTEXT ctx) |
| 124 | { |
| 125 | if (!ctx || ctx->output_fd == -1) |
| 126 | return ASSUAN_Invalid_Value; |
| 127 | |
| 128 | close (ctx->output_fd); |
| 129 | ctx->output_fd = -1; |
| 130 | return 0; |
| 131 | } |
| 132 |