svc_unix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * svc_unix.c, Server side for TCP/IP based RPC.
  3. *
  4. * Copyright (C) 2012-2019 Free Software Foundation, Inc.
  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. *
  21. * Copyright (c) 2010, Oracle America, Inc.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions are
  25. * met:
  26. *
  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.
  36. *
  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.
  49. *
  50. * Actually implements two flavors of transporter -
  51. * a unix rendezvouser (a listener and connection establisher)
  52. * and a record/unix stream.
  53. */
  54. #include <stdio.h>
  55. #include <unistd.h>
  56. #include <string.h>
  57. #include <rpc/rpc.h>
  58. #include <rpc/svc.h>
  59. #include <sys/socket.h>
  60. #include <sys/uio.h>
  61. #include <sys/poll.h>
  62. #include <errno.h>
  63. #include <stdlib.h>
  64. #include <libintl.h>
  65. #include <wchar.h>
  66. #include <shlib-compat.h>
  67. /*
  68. * Ops vector for AF_UNIX based rpc service handle
  69. */
  70. static bool_t svcunix_recv (SVCXPRT *, struct rpc_msg *);
  71. static enum xprt_stat svcunix_stat (SVCXPRT *);
  72. static bool_t svcunix_getargs (SVCXPRT *, xdrproc_t, caddr_t);
  73. static bool_t svcunix_reply (SVCXPRT *, struct rpc_msg *);
  74. static bool_t svcunix_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
  75. static void svcunix_destroy (SVCXPRT *);
  76. static const struct xp_ops svcunix_op =
  77. {
  78. svcunix_recv,
  79. svcunix_stat,
  80. svcunix_getargs,
  81. svcunix_reply,
  82. svcunix_freeargs,
  83. svcunix_destroy
  84. };
  85. /*
  86. * Ops vector for AF_UNIX rendezvous handler
  87. */
  88. static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
  89. static enum xprt_stat rendezvous_stat (SVCXPRT *);
  90. static void svcunix_rendezvous_abort (void) __attribute__ ((__noreturn__));
  91. /* This function makes sure abort() relocation goes through PLT
  92. and thus can be lazy bound. */
  93. static void
  94. svcunix_rendezvous_abort (void)
  95. {
  96. abort ();
  97. };
  98. static const struct xp_ops svcunix_rendezvous_op =
  99. {
  100. rendezvous_request,
  101. rendezvous_stat,
  102. (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svcunix_rendezvous_abort,
  103. (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svcunix_rendezvous_abort,
  104. (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svcunix_rendezvous_abort,
  105. svcunix_destroy
  106. };
  107. static int readunix (char*, char *, int);
  108. static int writeunix (char *, char *, int);
  109. static SVCXPRT *makefd_xprt (int, u_int, u_int);
  110. struct unix_rendezvous { /* kept in xprt->xp_p1 */
  111. u_int sendsize;
  112. u_int recvsize;
  113. };
  114. struct unix_conn { /* kept in xprt->xp_p1 */
  115. enum xprt_stat strm_stat;
  116. u_long x_id;
  117. XDR xdrs;
  118. char verf_body[MAX_AUTH_BYTES];
  119. };
  120. /*
  121. * Usage:
  122. * xprt = svcunix_create(sock, send_buf_size, recv_buf_size);
  123. *
  124. * Creates, registers, and returns a (rpc) unix based transporter.
  125. * Once *xprt is initialized, it is registered as a transporter
  126. * see (svc.h, xprt_register). This routine returns
  127. * a NULL if a problem occurred.
  128. *
  129. * If sock<0 then a socket is created, else sock is used.
  130. * If the socket, sock is not bound to a port then svcunix_create
  131. * binds it to an arbitrary port. The routine then starts a unix
  132. * listener on the socket's associated port. In any (successful) case,
  133. * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  134. * associated port number.
  135. *
  136. * Since unix streams do buffered io similar to stdio, the caller can specify
  137. * how big the send and receive buffers are via the second and third parms;
  138. * 0 => use the system default.
  139. */
  140. SVCXPRT *
  141. svcunix_create (int sock, u_int sendsize, u_int recvsize, char *path)
  142. {
  143. bool_t madesock = FALSE;
  144. SVCXPRT *xprt;
  145. struct unix_rendezvous *r;
  146. struct sockaddr_un addr;
  147. socklen_t len = sizeof (struct sockaddr_in);
  148. if (sock == RPC_ANYSOCK)
  149. {
  150. if ((sock = __socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
  151. {
  152. perror (_("svc_unix.c - AF_UNIX socket creation problem"));
  153. return (SVCXPRT *) NULL;
  154. }
  155. madesock = TRUE;
  156. }
  157. memset (&addr, '\0', sizeof (addr));
  158. addr.sun_family = AF_UNIX;
  159. len = strlen (path) + 1;
  160. memcpy (addr.sun_path, path, len);
  161. len += sizeof (addr.sun_family);
  162. __bind (sock, (struct sockaddr *) &addr, len);
  163. if (__getsockname (sock, (struct sockaddr *) &addr, &len) != 0
  164. || __listen (sock, SOMAXCONN) != 0)
  165. {
  166. perror (_("svc_unix.c - cannot getsockname or listen"));
  167. if (madesock)
  168. __close (sock);
  169. return (SVCXPRT *) NULL;
  170. }
  171. r = (struct unix_rendezvous *) mem_alloc (sizeof (*r));
  172. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  173. if (r == NULL || xprt == NULL)
  174. {
  175. __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
  176. mem_free (r, sizeof (*r));
  177. mem_free (xprt, sizeof (SVCXPRT));
  178. return NULL;
  179. }
  180. r->sendsize = sendsize;
  181. r->recvsize = recvsize;
  182. xprt->xp_p2 = NULL;
  183. xprt->xp_p1 = (caddr_t) r;
  184. xprt->xp_verf = _null_auth;
  185. xprt->xp_ops = &svcunix_rendezvous_op;
  186. xprt->xp_port = -1;
  187. xprt->xp_sock = sock;
  188. xprt_register (xprt);
  189. return xprt;
  190. }
  191. libc_hidden_nolink_sunrpc (svcunix_create, GLIBC_2_1)
  192. /*
  193. * Like svunix_create(), except the routine takes any *open* UNIX file
  194. * descriptor as its first input.
  195. */
  196. SVCXPRT *
  197. svcunixfd_create (int fd, u_int sendsize, u_int recvsize)
  198. {
  199. return makefd_xprt (fd, sendsize, recvsize);
  200. }
  201. libc_hidden_nolink_sunrpc (svcunixfd_create, GLIBC_2_1)
  202. static SVCXPRT *
  203. makefd_xprt (int fd, u_int sendsize, u_int recvsize)
  204. {
  205. SVCXPRT *xprt;
  206. struct unix_conn *cd;
  207. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  208. cd = (struct unix_conn *) mem_alloc (sizeof (struct unix_conn));
  209. if (xprt == (SVCXPRT *) NULL || cd == (struct unix_conn *) NULL)
  210. {
  211. (void) __fxprintf (NULL, "%s: %s", "svc_unix: makefd_xprt",
  212. _("out of memory\n"));
  213. mem_free (xprt, sizeof (SVCXPRT));
  214. mem_free (cd, sizeof (struct unix_conn));
  215. return NULL;
  216. }
  217. cd->strm_stat = XPRT_IDLE;
  218. xdrrec_create (&(cd->xdrs), sendsize, recvsize,
  219. (caddr_t) xprt, readunix, writeunix);
  220. xprt->xp_p2 = NULL;
  221. xprt->xp_p1 = (caddr_t) cd;
  222. xprt->xp_verf.oa_base = cd->verf_body;
  223. xprt->xp_addrlen = 0;
  224. xprt->xp_ops = &svcunix_op; /* truly deals with calls */
  225. xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
  226. xprt->xp_sock = fd;
  227. xprt_register (xprt);
  228. return xprt;
  229. }
  230. static bool_t
  231. rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
  232. {
  233. int sock;
  234. struct unix_rendezvous *r;
  235. struct sockaddr_un addr;
  236. struct sockaddr_in in_addr;
  237. socklen_t len;
  238. r = (struct unix_rendezvous *) xprt->xp_p1;
  239. again:
  240. len = sizeof (struct sockaddr_un);
  241. if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
  242. {
  243. if (errno == EINTR)
  244. goto again;
  245. __svc_accept_failed ();
  246. return FALSE;
  247. }
  248. /*
  249. * make a new transporter (re-uses xprt)
  250. */
  251. memset (&in_addr, '\0', sizeof (in_addr));
  252. in_addr.sin_family = AF_UNIX;
  253. xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
  254. memcpy (&xprt->xp_raddr, &in_addr, sizeof (in_addr));
  255. xprt->xp_addrlen = len;
  256. return FALSE; /* there is never an rpc msg to be processed */
  257. }
  258. static enum xprt_stat
  259. rendezvous_stat (SVCXPRT *xprt)
  260. {
  261. return XPRT_IDLE;
  262. }
  263. static void
  264. svcunix_destroy (SVCXPRT *xprt)
  265. {
  266. struct unix_conn *cd = (struct unix_conn *) xprt->xp_p1;
  267. xprt_unregister (xprt);
  268. __close (xprt->xp_sock);
  269. if (xprt->xp_port != 0)
  270. {
  271. /* a rendezvouser socket */
  272. xprt->xp_port = 0;
  273. }
  274. else
  275. {
  276. /* an actual connection socket */
  277. XDR_DESTROY (&(cd->xdrs));
  278. }
  279. mem_free ((caddr_t) cd, sizeof (struct unix_conn));
  280. mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
  281. }
  282. #ifdef SCM_CREDENTIALS
  283. struct cmessage {
  284. struct cmsghdr cmsg;
  285. struct ucred cmcred;
  286. /* hack to make sure we have enough memory */
  287. char dummy[(CMSG_ALIGN (sizeof (struct ucred)) - sizeof (struct ucred) + sizeof (long))];
  288. };
  289. /* XXX This is not thread safe, but since the main functions in svc.c
  290. and the rpcgen generated *_svc functions for the daemon are also not
  291. thread safe and uses static global variables, it doesn't matter. */
  292. static struct cmessage cm;
  293. #endif
  294. static int
  295. __msgread (int sock, void *data, size_t cnt)
  296. {
  297. struct iovec iov;
  298. struct msghdr msg;
  299. int len;
  300. iov.iov_base = data;
  301. iov.iov_len = cnt;
  302. msg.msg_iov = &iov;
  303. msg.msg_iovlen = 1;
  304. msg.msg_name = NULL;
  305. msg.msg_namelen = 0;
  306. #ifdef SCM_CREDENTIALS
  307. msg.msg_control = (caddr_t) &cm;
  308. msg.msg_controllen = sizeof (struct cmessage);
  309. #endif
  310. msg.msg_flags = 0;
  311. #ifdef SO_PASSCRED
  312. {
  313. int on = 1;
  314. if (__setsockopt (sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)))
  315. return -1;
  316. }
  317. #endif
  318. restart:
  319. len = __recvmsg (sock, &msg, 0);
  320. if (len >= 0)
  321. {
  322. if (msg.msg_flags & MSG_CTRUNC || len == 0)
  323. return 0;
  324. else
  325. return len;
  326. }
  327. if (errno == EINTR)
  328. goto restart;
  329. return -1;
  330. }
  331. static int
  332. __msgwrite (int sock, void *data, size_t cnt)
  333. {
  334. #ifndef SCM_CREDENTIALS
  335. /* We cannot implement this reliably. */
  336. __set_errno (ENOSYS);
  337. return -1;
  338. #else
  339. struct iovec iov;
  340. struct msghdr msg;
  341. struct cmsghdr *cmsg = &cm.cmsg;
  342. struct ucred cred;
  343. int len;
  344. /* XXX I'm not sure, if gete?id() is always correct, or if we should use
  345. get?id(). But since keyserv needs geteuid(), we have no other chance.
  346. It would be much better, if the kernel could pass both to the server. */
  347. cred.pid = __getpid ();
  348. cred.uid = __geteuid ();
  349. cred.gid = __getegid ();
  350. memcpy (CMSG_DATA(cmsg), &cred, sizeof (struct ucred));
  351. cmsg->cmsg_level = SOL_SOCKET;
  352. cmsg->cmsg_type = SCM_CREDENTIALS;
  353. cmsg->cmsg_len = sizeof(*cmsg) + sizeof(struct ucred);
  354. iov.iov_base = data;
  355. iov.iov_len = cnt;
  356. msg.msg_iov = &iov;
  357. msg.msg_iovlen = 1;
  358. msg.msg_name = NULL;
  359. msg.msg_namelen = 0;
  360. msg.msg_control = cmsg;
  361. msg.msg_controllen = CMSG_ALIGN(cmsg->cmsg_len);
  362. msg.msg_flags = 0;
  363. restart:
  364. len = __sendmsg (sock, &msg, 0);
  365. if (len >= 0)
  366. return len;
  367. if (errno == EINTR)
  368. goto restart;
  369. return -1;
  370. #endif
  371. }
  372. /*
  373. * reads data from the unix connection.
  374. * any error is fatal and the connection is closed.
  375. * (And a read of zero bytes is a half closed stream => error.)
  376. */
  377. static int
  378. readunix (char *xprtptr, char *buf, int len)
  379. {
  380. SVCXPRT *xprt = (SVCXPRT *) xprtptr;
  381. int sock = xprt->xp_sock;
  382. int milliseconds = 35 * 1000;
  383. struct pollfd pollfd;
  384. do
  385. {
  386. pollfd.fd = sock;
  387. pollfd.events = POLLIN;
  388. switch (__poll (&pollfd, 1, milliseconds))
  389. {
  390. case -1:
  391. if (errno == EINTR)
  392. continue;
  393. /*FALLTHROUGH*/
  394. case 0:
  395. goto fatal_err;
  396. default:
  397. if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
  398. || (pollfd.revents & POLLNVAL))
  399. goto fatal_err;
  400. break;
  401. }
  402. }
  403. while ((pollfd.revents & POLLIN) == 0);
  404. if ((len = __msgread (sock, buf, len)) > 0)
  405. return len;
  406. fatal_err:
  407. ((struct unix_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  408. return -1;
  409. }
  410. /*
  411. * writes data to the unix connection.
  412. * Any error is fatal and the connection is closed.
  413. */
  414. static int
  415. writeunix (char *xprtptr, char * buf, int len)
  416. {
  417. SVCXPRT *xprt = (SVCXPRT *) xprtptr;
  418. int i, cnt;
  419. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  420. {
  421. if ((i = __msgwrite (xprt->xp_sock, buf, cnt)) < 0)
  422. {
  423. ((struct unix_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  424. return -1;
  425. }
  426. }
  427. return len;
  428. }
  429. static enum xprt_stat
  430. svcunix_stat (SVCXPRT *xprt)
  431. {
  432. struct unix_conn *cd =
  433. (struct unix_conn *) (xprt->xp_p1);
  434. if (cd->strm_stat == XPRT_DIED)
  435. return XPRT_DIED;
  436. if (!xdrrec_eof (&(cd->xdrs)))
  437. return XPRT_MOREREQS;
  438. return XPRT_IDLE;
  439. }
  440. static bool_t
  441. svcunix_recv (SVCXPRT *xprt, struct rpc_msg *msg)
  442. {
  443. struct unix_conn *cd = (struct unix_conn *) (xprt->xp_p1);
  444. XDR *xdrs = &(cd->xdrs);
  445. xdrs->x_op = XDR_DECODE;
  446. xdrrec_skiprecord (xdrs);
  447. if (xdr_callmsg (xdrs, msg))
  448. {
  449. cd->x_id = msg->rm_xid;
  450. /* set up verifiers */
  451. #ifdef SCM_CREDENTIALS
  452. msg->rm_call.cb_verf.oa_flavor = AUTH_UNIX;
  453. msg->rm_call.cb_verf.oa_base = (caddr_t) &cm;
  454. msg->rm_call.cb_verf.oa_length = sizeof (cm);
  455. #endif
  456. return TRUE;
  457. }
  458. cd->strm_stat = XPRT_DIED; /* XXXX */
  459. return FALSE;
  460. }
  461. static bool_t
  462. svcunix_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  463. {
  464. return (*xdr_args) (&(((struct unix_conn *) (xprt->xp_p1))->xdrs),
  465. args_ptr);
  466. }
  467. static bool_t
  468. svcunix_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  469. {
  470. XDR *xdrs = &(((struct unix_conn *) (xprt->xp_p1))->xdrs);
  471. xdrs->x_op = XDR_FREE;
  472. return (*xdr_args) (xdrs, args_ptr);
  473. }
  474. static bool_t
  475. svcunix_reply (SVCXPRT *xprt, struct rpc_msg *msg)
  476. {
  477. struct unix_conn *cd = (struct unix_conn *) (xprt->xp_p1);
  478. XDR *xdrs = &(cd->xdrs);
  479. bool_t stat;
  480. xdrs->x_op = XDR_ENCODE;
  481. msg->rm_xid = cd->x_id;
  482. stat = xdr_replymsg (xdrs, msg);
  483. (void) xdrrec_endofrecord (xdrs, TRUE);
  484. return stat;
  485. }