]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/svc_tcp.c
Include shlib-compat.h in many sunrpc/nis source files.
[thirdparty/glibc.git] / sunrpc / svc_tcp.c
CommitLineData
28f540f4 1/*
a7ab6ec8 2 * svc_tcp.c, Server side for TCP/IP based RPC.
cbd3dceb 3 *
bfff8b1b 4 * Copyright (C) 2012-2017 Free Software Foundation, Inc.
14bc93a9
JL
5 * This file is part of the GNU C Library.
6 *
7 * The GNU C Library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * The GNU C Library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with the GNU C Library; if not, see
19 * <http://www.gnu.org/licenses/>.
20 *
a7ab6ec8 21 * Copyright (c) 2010, Oracle America, Inc.
cbd3dceb 22 *
a7ab6ec8
UD
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are
25 * met:
cbd3dceb 26 *
a7ab6ec8
UD
27 * * Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * * Redistributions in binary form must reproduce the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer in the documentation and/or other materials
32 * provided with the distribution.
33 * * Neither the name of the "Oracle America, Inc." nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
cb636bb2 36 *
a7ab6ec8
UD
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
40 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
41 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
44 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
46 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
47 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28f540f4
RM
49 *
50 * Actually implements two flavors of transporter -
6d52618b 51 * a tcp rendezvouser (a listener and connection establisher)
28f540f4
RM
52 * and a record/tcp stream.
53 */
54
55#include <stdio.h>
e7fd8a39
UD
56#include <unistd.h>
57#include <string.h>
4360eafd 58#include <libintl.h>
28f540f4
RM
59#include <rpc/rpc.h>
60#include <sys/socket.h>
090ca000 61#include <sys/poll.h>
28f540f4 62#include <errno.h>
e7fd8a39 63#include <stdlib.h>
28f540f4 64
3ce1f295
UD
65#include <wchar.h>
66#include <libio/iolibio.h>
82f43dd2 67#include <shlib-compat.h>
50304ef0 68
28f540f4
RM
69/*
70 * Ops vector for TCP/IP based rpc service handle
71 */
e7fd8a39
UD
72static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
73static enum xprt_stat svctcp_stat (SVCXPRT *);
74static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
75static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
76static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
77static void svctcp_destroy (SVCXPRT *);
78
79static const struct xp_ops svctcp_op =
80{
81 svctcp_recv,
82 svctcp_stat,
83 svctcp_getargs,
84 svctcp_reply,
85 svctcp_freeargs,
86 svctcp_destroy
28f540f4
RM
87};
88
89/*
90 * Ops vector for TCP/IP rendezvous handler
91 */
e7fd8a39
UD
92static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
93static enum xprt_stat rendezvous_stat (SVCXPRT *);
a49d0179 94static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__));
4b9afc43
UD
95
96/* This function makes sure abort() relocation goes through PLT
97 and thus can be lazy bound. */
98static void
99svctcp_rendezvous_abort (void)
100{
101 abort ();
102};
e7fd8a39
UD
103
104static const struct xp_ops svctcp_rendezvous_op =
105{
106 rendezvous_request,
107 rendezvous_stat,
4b9afc43
UD
108 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
109 (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
110 (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
e7fd8a39 111 svctcp_destroy
28f540f4
RM
112};
113
e7fd8a39
UD
114static int readtcp (char*, char *, int);
115static int writetcp (char *, char *, int);
dfd2257a 116static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
28f540f4 117
e7fd8a39
UD
118struct tcp_rendezvous
119 { /* kept in xprt->xp_p1 */
120 u_int sendsize;
121 u_int recvsize;
122 };
28f540f4 123
e7fd8a39
UD
124struct tcp_conn
125 { /* kept in xprt->xp_p1 */
126 enum xprt_stat strm_stat;
127 u_long x_id;
128 XDR xdrs;
129 char verf_body[MAX_AUTH_BYTES];
130 };
28f540f4
RM
131
132/*
133 * Usage:
e7fd8a39 134 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
28f540f4
RM
135 *
136 * Creates, registers, and returns a (rpc) tcp based transporter.
137 * Once *xprt is initialized, it is registered as a transporter
138 * see (svc.h, xprt_register). This routine returns
139 * a NULL if a problem occurred.
140 *
141 * If sock<0 then a socket is created, else sock is used.
142 * If the socket, sock is not bound to a port then svctcp_create
143 * binds it to an arbitrary port. The routine then starts a tcp
144 * listener on the socket's associated port. In any (successful) case,
145 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
146 * associated port number.
147 *
148 * Since tcp streams do buffered io similar to stdio, the caller can specify
149 * how big the send and receive buffers are via the second and third parms;
150 * 0 => use the system default.
151 */
152SVCXPRT *
e7fd8a39 153svctcp_create (int sock, u_int sendsize, u_int recvsize)
28f540f4 154{
e7fd8a39
UD
155 bool_t madesock = FALSE;
156 SVCXPRT *xprt;
157 struct tcp_rendezvous *r;
158 struct sockaddr_in addr;
41df5ed4 159 socklen_t len = sizeof (struct sockaddr_in);
e7fd8a39
UD
160
161 if (sock == RPC_ANYSOCK)
162 {
50304ef0 163 if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
e7fd8a39 164 {
e852e889 165 perror (_("svc_tcp.c - tcp socket creation problem"));
e7fd8a39 166 return (SVCXPRT *) NULL;
28f540f4 167 }
e7fd8a39
UD
168 madesock = TRUE;
169 }
50304ef0 170 __bzero ((char *) &addr, sizeof (addr));
e7fd8a39 171 addr.sin_family = AF_INET;
a585ba22 172 if (bindresvport (sock, &addr))
e7fd8a39
UD
173 {
174 addr.sin_port = 0;
b2bffca2 175 (void) __bind (sock, (struct sockaddr *) &addr, len);
e7fd8a39 176 }
b2bffca2 177 if ((__getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
a3a449c1 178 (__listen (sock, SOMAXCONN) != 0))
e7fd8a39 179 {
e852e889 180 perror (_("svc_tcp.c - cannot getsockname or listen"));
e7fd8a39 181 if (madesock)
50304ef0 182 (void) __close (sock);
e7fd8a39
UD
183 return (SVCXPRT *) NULL;
184 }
185 r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
51028f34
UD
186 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
187 if (r == NULL || xprt == NULL)
e7fd8a39 188 {
1d20f7f8 189 (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
2e3e5db6
UD
190 mem_free (r, sizeof (*r));
191 mem_free (xprt, sizeof (SVCXPRT));
e7fd8a39
UD
192 return NULL;
193 }
194 r->sendsize = sendsize;
195 r->recvsize = recvsize;
e7fd8a39
UD
196 xprt->xp_p2 = NULL;
197 xprt->xp_p1 = (caddr_t) r;
198 xprt->xp_verf = _null_auth;
199 xprt->xp_ops = &svctcp_rendezvous_op;
200 xprt->xp_port = ntohs (addr.sin_port);
201 xprt->xp_sock = sock;
202 xprt_register (xprt);
203 return xprt;
28f540f4 204}
7b57bfe5
UD
205#ifdef EXPORT_RPC_SYMBOLS
206libc_hidden_def (svctcp_create)
207#else
021db4be 208libc_hidden_nolink_sunrpc (svctcp_create, GLIBC_2_0)
7b57bfe5 209#endif
28f540f4
RM
210
211/*
212 * Like svtcp_create(), except the routine takes any *open* UNIX file
213 * descriptor as its first input.
214 */
215SVCXPRT *
e7fd8a39 216svcfd_create (int fd, u_int sendsize, u_int recvsize)
28f540f4 217{
e7fd8a39 218 return makefd_xprt (fd, sendsize, recvsize);
28f540f4 219}
021db4be 220libc_hidden_nolink_sunrpc (svcfd_create, GLIBC_2_0)
28f540f4
RM
221
222static SVCXPRT *
dfd2257a 223internal_function
e7fd8a39 224makefd_xprt (int fd, u_int sendsize, u_int recvsize)
28f540f4 225{
e7fd8a39
UD
226 SVCXPRT *xprt;
227 struct tcp_conn *cd;
228
229 xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
e7fd8a39 230 cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
51028f34 231 if (xprt == (SVCXPRT *) NULL || cd == NULL)
e7fd8a39 232 {
1d20f7f8
UD
233 (void) __fxprintf (NULL, "%s: %s", "svc_tcp: makefd_xprt",
234 _("out of memory\n"));
2e3e5db6
UD
235 mem_free (xprt, sizeof (SVCXPRT));
236 mem_free (cd, sizeof (struct tcp_conn));
51028f34 237 return NULL;
e7fd8a39
UD
238 }
239 cd->strm_stat = XPRT_IDLE;
7b57bfe5
UD
240 xdrrec_create (&(cd->xdrs), sendsize, recvsize,
241 (caddr_t) xprt, readtcp, writetcp);
e7fd8a39
UD
242 xprt->xp_p2 = NULL;
243 xprt->xp_p1 = (caddr_t) cd;
244 xprt->xp_verf.oa_base = cd->verf_body;
245 xprt->xp_addrlen = 0;
246 xprt->xp_ops = &svctcp_op; /* truly deals with calls */
247 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
248 xprt->xp_sock = fd;
249 xprt_register (xprt);
e7fd8a39 250 return xprt;
28f540f4
RM
251}
252
253static bool_t
e7fd8a39 254rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
28f540f4 255{
e7fd8a39
UD
256 int sock;
257 struct tcp_rendezvous *r;
258 struct sockaddr_in addr;
41df5ed4 259 socklen_t len;
e7fd8a39
UD
260
261 r = (struct tcp_rendezvous *) xprt->xp_p1;
262again:
263 len = sizeof (struct sockaddr_in);
50304ef0 264 if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
e7fd8a39
UD
265 {
266 if (errno == EINTR)
267 goto again;
14bc93a9 268 __svc_accept_failed ();
e7fd8a39
UD
269 return FALSE;
270 }
271 /*
272 * make a new transporter (re-uses xprt)
273 */
274 xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
c1301d9a 275 memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
e7fd8a39
UD
276 xprt->xp_addrlen = len;
277 return FALSE; /* there is never an rpc msg to be processed */
28f540f4
RM
278}
279
280static enum xprt_stat
e7fd8a39 281rendezvous_stat (SVCXPRT *xprt)
28f540f4 282{
e7fd8a39 283 return XPRT_IDLE;
28f540f4
RM
284}
285
286static void
e7fd8a39 287svctcp_destroy (SVCXPRT *xprt)
28f540f4 288{
e7fd8a39
UD
289 struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
290
291 xprt_unregister (xprt);
50304ef0 292 (void) __close (xprt->xp_sock);
e7fd8a39
UD
293 if (xprt->xp_port != 0)
294 {
295 /* a rendezvouser socket */
296 xprt->xp_port = 0;
297 }
298 else
299 {
300 /* an actual connection socket */
301 XDR_DESTROY (&(cd->xdrs));
302 }
303 mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
304 mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
28f540f4
RM
305}
306
28f540f4
RM
307
308/*
6d52618b 309 * reads data from the tcp connection.
28f540f4
RM
310 * any error is fatal and the connection is closed.
311 * (And a read of zero bytes is a half closed stream => error.)
312 */
313static int
e7fd8a39 314readtcp (char *xprtptr, char *buf, int len)
28f540f4 315{
e7fd8a39
UD
316 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
317 int sock = xprt->xp_sock;
090ca000
UD
318 int milliseconds = 35 * 1000;
319 struct pollfd pollfd;
320
321 do
e7fd8a39 322 {
090ca000
UD
323 pollfd.fd = sock;
324 pollfd.events = POLLIN;
325 switch (__poll (&pollfd, 1, milliseconds))
e7fd8a39 326 {
090ca000 327 case -1:
e7fd8a39 328 if (errno == EINTR)
fed8f7f7 329 continue;
090ca000
UD
330 /*FALLTHROUGH*/
331 case 0:
e7fd8a39 332 goto fatal_err;
090ca000 333 default:
7b57bfe5
UD
334 if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
335 || (pollfd.revents & POLLNVAL))
336 goto fatal_err;
090ca000 337 break;
7d1de115 338 }
e7fd8a39 339 }
090ca000 340 while ((pollfd.revents & POLLIN) == 0);
fed8f7f7 341
50304ef0 342 if ((len = __read (sock, buf, len)) > 0)
fed8f7f7
UD
343 return len;
344
090ca000 345 fatal_err:
e7fd8a39
UD
346 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
347 return -1;
28f540f4
RM
348}
349
350/*
351 * writes data to the tcp connection.
352 * Any error is fatal and the connection is closed.
353 */
354static int
e7fd8a39 355writetcp (char *xprtptr, char * buf, int len)
28f540f4 356{
e7fd8a39
UD
357 SVCXPRT *xprt = (SVCXPRT *)xprtptr;
358 int i, cnt;
359
360 for (cnt = len; cnt > 0; cnt -= i, buf += i)
361 {
50304ef0 362 if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
e7fd8a39 363 {
090ca000
UD
364 ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
365 return -1;
28f540f4 366 }
e7fd8a39 367 }
090ca000 368 return len;
28f540f4
RM
369}
370
371static enum xprt_stat
e7fd8a39 372svctcp_stat (SVCXPRT *xprt)
28f540f4 373{
e7fd8a39
UD
374 struct tcp_conn *cd =
375 (struct tcp_conn *) (xprt->xp_p1);
376
377 if (cd->strm_stat == XPRT_DIED)
090ca000 378 return XPRT_DIED;
7b57bfe5 379 if (!xdrrec_eof (&(cd->xdrs)))
090ca000
UD
380 return XPRT_MOREREQS;
381 return XPRT_IDLE;
28f540f4
RM
382}
383
384static bool_t
090ca000 385svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
28f540f4 386{
090ca000 387 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
e7fd8a39
UD
388 XDR *xdrs = &(cd->xdrs);
389
390 xdrs->x_op = XDR_DECODE;
7b57bfe5
UD
391 (void) xdrrec_skiprecord (xdrs);
392 if (xdr_callmsg (xdrs, msg))
e7fd8a39
UD
393 {
394 cd->x_id = msg->rm_xid;
090ca000 395 return TRUE;
e7fd8a39 396 }
41aa20c2 397 cd->strm_stat = XPRT_DIED; /* XXXX */
090ca000 398 return FALSE;
28f540f4
RM
399}
400
401static bool_t
090ca000 402svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
28f540f4 403{
090ca000
UD
404 return ((*xdr_args) (&(((struct tcp_conn *)
405 (xprt->xp_p1))->xdrs), args_ptr));
28f540f4
RM
406}
407
408static bool_t
090ca000 409svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
28f540f4 410{
090ca000 411 XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
28f540f4 412
e7fd8a39
UD
413 xdrs->x_op = XDR_FREE;
414 return ((*xdr_args) (xdrs, args_ptr));
28f540f4
RM
415}
416
417static bool_t
090ca000 418svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
28f540f4 419{
090ca000 420 struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
e7fd8a39
UD
421 XDR *xdrs = &(cd->xdrs);
422 bool_t stat;
423
424 xdrs->x_op = XDR_ENCODE;
425 msg->rm_xid = cd->x_id;
7b57bfe5
UD
426 stat = xdr_replymsg (xdrs, msg);
427 (void) xdrrec_endofrecord (xdrs, TRUE);
090ca000 428 return stat;
28f540f4 429}