| 1 | /* assuan-socket-server.c - Assuan socket based server |
| 2 | * Copyright (C) 2002, 2007 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 <errno.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/types.h> |
| 26 | #ifdef HAVE_W32_SYSTEM |
| 27 | # include <windows.h> |
| 28 | # if HAVE_SYS_SOCKET_H |
| 29 | # include <sys/socket.h> |
| 30 | # elif HAVE_WS2TCPIP_H |
| 31 | # include <ws2tcpip.h> |
| 32 | # endif |
| 33 | #else |
| 34 | # include <sys/socket.h> |
| 35 | # include <sys/un.h> |
| 36 | #endif |
| 37 | |
| 38 | |
| 39 | #include "assuan-defs.h" |
| 40 | |
| 41 | static struct assuan_io io = { _assuan_simple_read, _assuan_simple_write, |
| 42 | NULL, NULL }; |
| 43 | |
| 44 | static int |
| 45 | accept_connection_bottom (assuan_context_t ctx) |
| 46 | { |
| 47 | assuan_fd_t fd = ctx->connected_fd; |
| 48 | |
| 49 | ctx->peercred.valid = 0; |
| 50 | #ifdef HAVE_SO_PEERCRED |
| 51 | { |
| 52 | struct ucred cr; |
| 53 | socklen_t cl = sizeof cr; |
| 54 | |
| 55 | if ( !getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl)) |
| 56 | { |
| 57 | ctx->peercred.pid = cr.pid; |
| 58 | ctx->peercred.uid = cr.uid; |
| 59 | ctx->peercred.gid = cr.gid; |
| 60 | ctx->peercred.valid = 1; |
| 61 | |
| 62 | /* This overrides any already set PID if the function returns |
| 63 | a valid one. */ |
| 64 | if (cr.pid != (pid_t)-1 && cr.pid) |
| 65 | ctx->pid = cr.pid; |
| 66 | } |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | ctx->inbound.fd = fd; |
| 71 | ctx->inbound.eof = 0; |
| 72 | ctx->inbound.linelen = 0; |
| 73 | ctx->inbound.attic.linelen = 0; |
| 74 | ctx->inbound.attic.pending = 0; |
| 75 | |
| 76 | ctx->outbound.fd = fd; |
| 77 | ctx->outbound.data.linelen = 0; |
| 78 | ctx->outbound.data.error = 0; |
| 79 | |
| 80 | ctx->confidential = 0; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | static int |
| 87 | accept_connection (assuan_context_t ctx) |
| 88 | { |
| 89 | assuan_fd_t fd; |
| 90 | struct sockaddr_un clnt_addr; |
| 91 | socklen_t len = sizeof clnt_addr; |
| 92 | |
| 93 | fd = SOCKET2HANDLE(accept (HANDLE2SOCKET(ctx->listen_fd), |
| 94 | (struct sockaddr*)&clnt_addr, &len )); |
| 95 | if (fd == ASSUAN_INVALID_FD) |
| 96 | { |
| 97 | ctx->os_errno = errno; |
| 98 | return _assuan_error (ASSUAN_Accept_Failed); |
| 99 | } |
| 100 | if (_assuan_sock_check_nonce (fd, &ctx->listen_nonce)) |
| 101 | { |
| 102 | _assuan_close (fd); |
| 103 | ctx->os_errno = EACCES; |
| 104 | return _assuan_error (ASSUAN_Accept_Failed); |
| 105 | } |
| 106 | |
| 107 | ctx->connected_fd = fd; |
| 108 | return accept_connection_bottom (ctx); |
| 109 | } |
| 110 | |
| 111 | static int |
| 112 | finish_connection (assuan_context_t ctx) |
| 113 | { |
| 114 | if (ctx->inbound.fd != ASSUAN_INVALID_FD) |
| 115 | { |
| 116 | _assuan_close (ctx->inbound.fd); |
| 117 | } |
| 118 | ctx->inbound.fd = ASSUAN_INVALID_FD; |
| 119 | ctx->outbound.fd = ASSUAN_INVALID_FD; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static void |
| 125 | deinit_socket_server (assuan_context_t ctx) |
| 126 | { |
| 127 | finish_connection (ctx); |
| 128 | } |
| 129 | |
| 130 | /* Initialize a server for the socket LISTEN_FD which has already be |
| 131 | put into listen mode */ |
| 132 | int |
| 133 | assuan_init_socket_server (assuan_context_t *r_ctx, assuan_fd_t listen_fd) |
| 134 | { |
| 135 | return assuan_init_socket_server_ext (r_ctx, listen_fd, 0); |
| 136 | } |
| 137 | |
| 138 | /* Initialize a server using the already accepted socket FD. This |
| 139 | function is deprecated. */ |
| 140 | int |
| 141 | assuan_init_connected_socket_server (assuan_context_t *r_ctx, assuan_fd_t fd) |
| 142 | { |
| 143 | return assuan_init_socket_server_ext (r_ctx, fd, 2); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* |
| 148 | Flag bits: 0 - use sendmsg/recvmsg to allow descriptor passing |
| 149 | 1 - FD has already been accepted. |
| 150 | */ |
| 151 | int |
| 152 | assuan_init_socket_server_ext (assuan_context_t *r_ctx, assuan_fd_t fd, |
| 153 | unsigned int flags) |
| 154 | { |
| 155 | assuan_context_t ctx; |
| 156 | int rc; |
| 157 | |
| 158 | *r_ctx = NULL; |
| 159 | ctx = xtrycalloc (1, sizeof *ctx); |
| 160 | if (!ctx) |
| 161 | return _assuan_error (ASSUAN_Out_Of_Core); |
| 162 | ctx->is_server = 1; |
| 163 | if ((flags & 2)) |
| 164 | ctx->pipe_mode = 1; /* We want a second accept to indicate EOF. */ |
| 165 | ctx->input_fd = ASSUAN_INVALID_FD; |
| 166 | ctx->output_fd = ASSUAN_INVALID_FD; |
| 167 | |
| 168 | ctx->inbound.fd = ASSUAN_INVALID_FD; |
| 169 | ctx->outbound.fd = ASSUAN_INVALID_FD; |
| 170 | |
| 171 | if ((flags & 2)) |
| 172 | { |
| 173 | ctx->listen_fd = ASSUAN_INVALID_FD; |
| 174 | ctx->connected_fd = fd; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | ctx->listen_fd = fd; |
| 179 | ctx->connected_fd = ASSUAN_INVALID_FD; |
| 180 | } |
| 181 | ctx->deinit_handler = (flags & 1)? _assuan_uds_deinit:deinit_socket_server; |
| 182 | ctx->accept_handler = ((flags & 2) |
| 183 | ? accept_connection_bottom |
| 184 | : accept_connection); |
| 185 | ctx->finish_handler = finish_connection; |
| 186 | |
| 187 | ctx->io = &io; |
| 188 | if ((flags & 1)) |
| 189 | _assuan_init_uds_io (ctx); |
| 190 | |
| 191 | rc = _assuan_register_std_commands (ctx); |
| 192 | if (rc) |
| 193 | xfree (ctx); |
| 194 | else |
| 195 | *r_ctx = ctx; |
| 196 | return rc; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* Save a copy of NONCE in context CTX. This should be used to |
| 201 | register the server's nonce with an context established by |
| 202 | assuan_init_socket_server. */ |
| 203 | void |
| 204 | assuan_set_sock_nonce (assuan_context_t ctx, assuan_sock_nonce_t *nonce) |
| 205 | { |
| 206 | if (ctx && nonce) |
| 207 | ctx->listen_nonce = *nonce; |
| 208 | } |