| 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>cxx . . $ `<file>/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"cxx . . $ `<file>/src/assuan/assuan-defs.h`/ |
| 40 | |
| 41 | static struct assuan_iocxx . . $ assuan_io# iocxx . . $ io. = { _assuan_simple_readcxx . . $ `src/assuan/assuan.h:168:9`!, _assuan_simple_writecxx . . $ `src/assuan/assuan.h:169:9`!, |
| 42 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! }; |
| 43 | |
| 44 | static int |
| 45 | accept_connection_bottomcxx . . $ accept_connection_bottom(29493cd997091d1). (assuan_context_tcxx . . $ assuan_context_t# ctx) |
| 46 | { |
| 47 | assuan_fd_tcxx . . $ assuan_fd_t# fd = ctx->connected_fdcxx . . $ assuan_context_s#connected_fd.; |
| 48 | |
| 49 | ctx->peercredcxx . . $ assuan_context_s#peercred..validcxx . . $ assuan_context_s#$anonymous_type_5#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->inboundcxx . . $ assuan_context_s#inbound..fdcxx . . $ assuan_context_s#$anonymous_type_1#fd. = fd; |
| 71 | ctx->inboundcxx . . $ assuan_context_s#inbound..eofcxx . . $ assuan_context_s#$anonymous_type_1#eof. = 0; |
| 72 | ctx->inboundcxx . . $ assuan_context_s#inbound..linelencxx . . $ assuan_context_s#$anonymous_type_1#linelen. = 0; |
| 73 | ctx->inboundcxx . . $ assuan_context_s#inbound..atticcxx . . $ assuan_context_s#$anonymous_type_1#attic..linelencxx . . $ assuan_context_s#$anonymous_type_1#$anonymous_type_2#linelen. = 0; |
| 74 | ctx->inboundcxx . . $ assuan_context_s#inbound..atticcxx . . $ assuan_context_s#$anonymous_type_1#attic..pendingcxx . . $ assuan_context_s#$anonymous_type_1#$anonymous_type_2#pending. = 0; |
| 75 | |
| 76 | ctx->outboundcxx . . $ assuan_context_s#outbound..fdcxx . . $ assuan_context_s#$anonymous_type_3#fd. = fd; |
| 77 | ctx->outboundcxx . . $ assuan_context_s#outbound..datacxx . . $ assuan_context_s#$anonymous_type_3#data..linelencxx . . $ assuan_context_s#$anonymous_type_3#$anonymous_type_4#linelen. = 0; |
| 78 | ctx->outboundcxx . . $ assuan_context_s#outbound..datacxx . . $ assuan_context_s#$anonymous_type_3#data..errorcxx . . $ assuan_context_s#$anonymous_type_3#$anonymous_type_4#error. = 0; |
| 79 | |
| 80 | ctx->confidentialcxx . . $ assuan_context_s#confidential. = 0; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | static int |
| 87 | accept_connectioncxx . . $ accept_connection(29493cd997091d1). (assuan_context_tcxx . . $ assuan_context_t# ctx) |
| 88 | { |
| 89 | assuan_fd_tcxx . . $ assuan_fd_t# fd; |
| 90 | struct sockaddr_uncxx . . $ sockaddr_un# clnt_addr; |
| 91 | socklen_tcxx . . $ socklen_t# len = sizeof clnt_addr; |
| 92 | |
| 93 | fd = SOCKET2HANDLEcxx . . $ `src/assuan/assuan-defs.h:356:9`!(accept (HANDLE2SOCKETcxx . . $ `src/assuan/assuan-defs.h:357:9`!(ctx->listen_fd), |
| 94 | (struct sockaddr*)&clnt_addr, &len )); |
| 95 | if (fd == ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!) |
| 96 | { |
| 97 | ctx->os_errnocxx . . $ assuan_context_s#os_errno. = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 98 | return _assuan_errorcxx . . $ `src/assuan/assuan.h:189:9`! (ASSUAN_Accept_Failedcxx . . $ `src/assuan/assuan.h:256:10`!); |
| 99 | } |
| 100 | if (_assuan_sock_check_noncecxx . . $ `src/assuan/assuan.h:203:9`! (fd, &ctx->listen_noncecxx . . $ assuan_context_s#listen_nonce.)) |
| 101 | { |
| 102 | _assuan_closecxx . . $ _assuan_close(7864480464b09eea). (fd); |
| 103 | ctx->os_errnocxx . . $ assuan_context_s#os_errno. = EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!; |
| 104 | return _assuan_errorcxx . . $ `src/assuan/assuan.h:189:9`! (ASSUAN_Accept_Failedcxx . . $ `src/assuan/assuan.h:256:10`!); |
| 105 | } |
| 106 | |
| 107 | ctx->connected_fdcxx . . $ assuan_context_s#connected_fd. = fd; |
| 108 | return accept_connection_bottomcxx . . $ accept_connection_bottom(29493cd997091d1). (ctx); |
| 109 | } |
| 110 | |
| 111 | static int |
| 112 | finish_connectioncxx . . $ finish_connection(29493cd997091d1). (assuan_context_tcxx . . $ assuan_context_t# ctx) |
| 113 | { |
| 114 | if (ctx->inboundcxx . . $ assuan_context_s#inbound..fdcxx . . $ assuan_context_s#$anonymous_type_1#fd. != ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!) |
| 115 | { |
| 116 | _assuan_closecxx . . $ _assuan_close(7864480464b09eea). (ctx->inboundcxx . . $ assuan_context_s#inbound..fdcxx . . $ assuan_context_s#$anonymous_type_1#fd.); |
| 117 | } |
| 118 | ctx->inboundcxx . . $ assuan_context_s#inbound..fdcxx . . $ assuan_context_s#$anonymous_type_1#fd. = ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!; |
| 119 | ctx->outboundcxx . . $ assuan_context_s#outbound..fdcxx . . $ assuan_context_s#$anonymous_type_3#fd. = ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static void |
| 125 | deinit_socket_servercxx . . $ deinit_socket_server(746ddb6f27ff0333). (assuan_context_tcxx . . $ assuan_context_t# ctx) |
| 126 | { |
| 127 | finish_connectioncxx . . $ finish_connection(29493cd997091d1). (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_servercxx . . $ `src/assuan/assuan.h:108:9`! (assuan_context_tcxx . . $ assuan_context_t# *r_ctx, assuan_fd_tcxx . . $ assuan_fd_t# listen_fd) |
| 134 | { |
| 135 | return assuan_init_socket_server_extcxx . . $ `src/assuan/assuan.h:111:9`! (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_servercxx . . $ `src/assuan/assuan.h:109:9`! (assuan_context_tcxx . . $ assuan_context_t# *r_ctx, assuan_fd_tcxx . . $ assuan_fd_t# fd) |
| 142 | { |
| 143 | return assuan_init_socket_server_extcxx . . $ `src/assuan/assuan.h:111:9`! (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_extcxx . . $ `src/assuan/assuan.h:111:9`! (assuan_context_tcxx . . $ assuan_context_t# *r_ctx, assuan_fd_tcxx . . $ assuan_fd_t# fd, |
| 153 | unsigned int flags) |
| 154 | { |
| 155 | assuan_context_tcxx . . $ assuan_context_t# ctx; |
| 156 | int rc; |
| 157 | |
| 158 | *r_ctx = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 159 | ctx = xtrycalloccxx . . $ `src/assuan/assuan-defs.h:262:9`! (1, sizeof *ctx); |
| 160 | if (!ctx) |
| 161 | return _assuan_errorcxx . . $ `src/assuan/assuan.h:189:9`! (ASSUAN_Out_Of_Corecxx . . $ `src/assuan/assuan.h:239:10`!); |
| 162 | ctx->is_servercxx . . $ assuan_context_s#is_server. = 1; |
| 163 | if ((flags & 2)) |
| 164 | ctx->pipe_modecxx . . $ assuan_context_s#pipe_mode. = 1; /* We want a second accept to indicate EOF. */ |
| 165 | ctx->input_fdcxx . . $ assuan_context_s#input_fd. = ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!; |
| 166 | ctx->output_fdcxx . . $ assuan_context_s#output_fd. = ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!; |
| 167 | |
| 168 | ctx->inboundcxx . . $ assuan_context_s#inbound..fdcxx . . $ assuan_context_s#$anonymous_type_1#fd. = ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!; |
| 169 | ctx->outboundcxx . . $ assuan_context_s#outbound..fdcxx . . $ assuan_context_s#$anonymous_type_3#fd. = ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!; |
| 170 | |
| 171 | if ((flags & 2)) |
| 172 | { |
| 173 | ctx->listen_fdcxx . . $ assuan_context_s#listen_fd. = ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!; |
| 174 | ctx->connected_fdcxx . . $ assuan_context_s#connected_fd. = fd; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | ctx->listen_fdcxx . . $ assuan_context_s#listen_fd. = fd; |
| 179 | ctx->connected_fdcxx . . $ assuan_context_s#connected_fd. = ASSUAN_INVALID_FDcxx . . $ `src/assuan/assuan.h:395:9`!; |
| 180 | } |
| 181 | ctx->deinit_handlercxx . . $ assuan_context_s#deinit_handler. = (flags & 1)? _assuan_uds_deinitcxx . . $ `src/assuan/assuan.h:193:9`!:deinit_socket_servercxx . . $ deinit_socket_server(746ddb6f27ff0333).; |
| 182 | ctx->accept_handlercxx . . $ assuan_context_s#accept_handler. = ((flags & 2) |
| 183 | ? accept_connection_bottomcxx . . $ accept_connection_bottom(29493cd997091d1). |
| 184 | : accept_connectioncxx . . $ accept_connection(29493cd997091d1).); |
| 185 | ctx->finish_handlercxx . . $ assuan_context_s#finish_handler. = finish_connectioncxx . . $ finish_connection(29493cd997091d1).; |
| 186 | |
| 187 | ctx->iocxx . . $ assuan_context_s#io. = &iocxx . . $ io.; |
| 188 | if ((flags & 1)) |
| 189 | _assuan_init_uds_iocxx . . $ `src/assuan/assuan.h:191:9`! (ctx); |
| 190 | |
| 191 | rc = _assuan_register_std_commandscxx . . $ `src/assuan/assuan.h:166:9`! (ctx); |
| 192 | if (rc) |
| 193 | xfreecxx . . $ `src/assuan/assuan-defs.h:264:9`! (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_noncecxx . . $ assuan_set_sock_nonce(c42f2c2b4c722d5e). (assuan_context_tcxx . . $ assuan_context_t# ctx, assuan_sock_nonce_tcxx . . $ assuan_sock_nonce_t# *nonce) |
| 205 | { |
| 206 | if (ctx && nonce) |
| 207 | ctx->listen_noncecxx . . $ assuan_context_s#listen_nonce. = *nonce; |
| 208 | } |