svc_unix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3. * unrestricted use provided that this legend is included on all tape
  4. * media and as a part of the software program in whole or part. Users
  5. * may copy or modify Sun RPC without charge, but are not authorized
  6. * to license or distribute it to anyone else except as part of a product or
  7. * program developed by the user.
  8. *
  9. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12. *
  13. * Sun RPC is provided with no support and without any obligation on the
  14. * part of Sun Microsystems, Inc. to assist in its use, correction,
  15. * modification or enhancement.
  16. *
  17. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19. * OR ANY PART THEREOF.
  20. *
  21. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22. * or profits or other special, indirect and consequential damages, even if
  23. * Sun has been advised of the possibility of such damages.
  24. *
  25. * Sun Microsystems, Inc.
  26. * 2550 Garcia Avenue
  27. * Mountain View, California 94043
  28. */
  29. /*
  30. * svc_unix.c, Server side for TCP/IP based RPC.
  31. *
  32. * Copyright (C) 1984, Sun Microsystems, Inc.
  33. *
  34. * Actually implements two flavors of transporter -
  35. * a unix rendezvouser (a listener and connection establisher)
  36. * and a record/unix stream.
  37. */
  38. #define __FORCE_GLIBC
  39. #include <features.h>
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include <rpc/rpc.h>
  44. #include <rpc/svc.h>
  45. #include <sys/socket.h>
  46. #include <sys/uio.h>
  47. #include <sys/poll.h>
  48. #include <errno.h>
  49. #include <stdlib.h>
  50. #ifdef USE_IN_LIBIO
  51. # include <wchar.h>
  52. #endif
  53. libc_hidden_proto(memcpy)
  54. libc_hidden_proto(memset)
  55. libc_hidden_proto(strlen)
  56. libc_hidden_proto(socket)
  57. libc_hidden_proto(close)
  58. libc_hidden_proto(perror)
  59. libc_hidden_proto(getpid)
  60. libc_hidden_proto(xdrrec_create)
  61. libc_hidden_proto(xdrrec_endofrecord)
  62. libc_hidden_proto(xdrrec_skiprecord)
  63. libc_hidden_proto(xdrrec_eof)
  64. libc_hidden_proto(xdr_callmsg)
  65. libc_hidden_proto(xdr_replymsg)
  66. libc_hidden_proto(xprt_register)
  67. libc_hidden_proto(xprt_unregister)
  68. libc_hidden_proto(getegid)
  69. libc_hidden_proto(geteuid)
  70. libc_hidden_proto(getsockname)
  71. libc_hidden_proto(setsockopt)
  72. libc_hidden_proto(bind)
  73. libc_hidden_proto(recvmsg)
  74. libc_hidden_proto(sendmsg)
  75. libc_hidden_proto(poll)
  76. libc_hidden_proto(accept)
  77. libc_hidden_proto(listen)
  78. libc_hidden_proto(fputs)
  79. libc_hidden_proto(abort)
  80. libc_hidden_proto(stderr)
  81. /*
  82. * Ops vector for AF_UNIX based rpc service handle
  83. */
  84. static bool_t svcunix_recv (SVCXPRT *, struct rpc_msg *);
  85. static enum xprt_stat svcunix_stat (SVCXPRT *);
  86. static bool_t svcunix_getargs (SVCXPRT *, xdrproc_t, caddr_t);
  87. static bool_t svcunix_reply (SVCXPRT *, struct rpc_msg *);
  88. static bool_t svcunix_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
  89. static void svcunix_destroy (SVCXPRT *);
  90. static const struct xp_ops svcunix_op =
  91. {
  92. svcunix_recv,
  93. svcunix_stat,
  94. svcunix_getargs,
  95. svcunix_reply,
  96. svcunix_freeargs,
  97. svcunix_destroy
  98. };
  99. /*
  100. * Ops vector for AF_UNIX rendezvous handler
  101. */
  102. static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
  103. static enum xprt_stat rendezvous_stat (SVCXPRT *);
  104. static void svcunix_rendezvous_abort (void) attribute_noreturn;
  105. /* This function makes sure abort() relocation goes through PLT
  106. and thus can be lazy bound. */
  107. static void
  108. svcunix_rendezvous_abort (void)
  109. {
  110. abort ();
  111. };
  112. static const struct xp_ops svcunix_rendezvous_op =
  113. {
  114. rendezvous_request,
  115. rendezvous_stat,
  116. (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svcunix_rendezvous_abort,
  117. (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svcunix_rendezvous_abort,
  118. (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svcunix_rendezvous_abort,
  119. svcunix_destroy
  120. };
  121. static int readunix (char*, char *, int);
  122. static int writeunix (char *, char *, int);
  123. static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
  124. struct unix_rendezvous { /* kept in xprt->xp_p1 */
  125. u_int sendsize;
  126. u_int recvsize;
  127. };
  128. struct unix_conn { /* kept in xprt->xp_p1 */
  129. enum xprt_stat strm_stat;
  130. u_long x_id;
  131. XDR xdrs;
  132. char verf_body[MAX_AUTH_BYTES];
  133. };
  134. /*
  135. * Usage:
  136. * xprt = svcunix_create(sock, send_buf_size, recv_buf_size);
  137. *
  138. * Creates, registers, and returns a (rpc) unix based transporter.
  139. * Once *xprt is initialized, it is registered as a transporter
  140. * see (svc.h, xprt_register). This routine returns
  141. * a NULL if a problem occurred.
  142. *
  143. * If sock<0 then a socket is created, else sock is used.
  144. * If the socket, sock is not bound to a port then svcunix_create
  145. * binds it to an arbitrary port. The routine then starts a unix
  146. * listener on the socket's associated port. In any (successful) case,
  147. * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  148. * associated port number.
  149. *
  150. * Since unix streams do buffered io similar to stdio, the caller can specify
  151. * how big the send and receive buffers are via the second and third parms;
  152. * 0 => use the system default.
  153. */
  154. SVCXPRT *
  155. svcunix_create (int sock, u_int sendsize, u_int recvsize, char *path)
  156. {
  157. bool_t madesock = FALSE;
  158. SVCXPRT *xprt;
  159. struct unix_rendezvous *r;
  160. struct sockaddr_un addr;
  161. socklen_t len = sizeof (struct sockaddr_in);
  162. if (sock == RPC_ANYSOCK)
  163. {
  164. if ((sock = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
  165. {
  166. perror (_("svc_unix.c - AF_UNIX socket creation problem"));
  167. return (SVCXPRT *) NULL;
  168. }
  169. madesock = TRUE;
  170. }
  171. memset (&addr, '\0', sizeof (addr));
  172. addr.sun_family = AF_UNIX;
  173. len = strlen (path) + 1;
  174. memcpy (addr.sun_path, path, len);
  175. len += sizeof (addr.sun_family);
  176. bind (sock, (struct sockaddr *) &addr, len);
  177. if (getsockname (sock, (struct sockaddr *) &addr, &len) != 0
  178. || listen (sock, 2) != 0)
  179. {
  180. perror (_("svc_unix.c - cannot getsockname or listen"));
  181. if (madesock)
  182. close (sock);
  183. return (SVCXPRT *) NULL;
  184. }
  185. r = (struct unix_rendezvous *) mem_alloc (sizeof (*r));
  186. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  187. if (r == NULL || xprt == NULL)
  188. {
  189. #ifdef USE_IN_LIBIO
  190. if (_IO_fwide (stderr, 0) > 0)
  191. __fwprintf (stderr, L"%s", _("svcunix_create: out of memory\n"));
  192. else
  193. #endif
  194. fputs (_("svcunix_create: out of memory\n"), stderr);
  195. mem_free (r, sizeof (*r));
  196. mem_free (xprt, sizeof (SVCXPRT));
  197. return NULL;
  198. }
  199. r->sendsize = sendsize;
  200. r->recvsize = recvsize;
  201. xprt->xp_p2 = NULL;
  202. xprt->xp_p1 = (caddr_t) r;
  203. xprt->xp_verf = _null_auth;
  204. xprt->xp_ops = &svcunix_rendezvous_op;
  205. xprt->xp_port = -1;
  206. xprt->xp_sock = sock;
  207. xprt_register (xprt);
  208. return xprt;
  209. }
  210. /*
  211. * Like svunix_create(), except the routine takes any *open* UNIX file
  212. * descriptor as its first input.
  213. */
  214. SVCXPRT *
  215. svcunixfd_create (int fd, u_int sendsize, u_int recvsize);
  216. SVCXPRT *
  217. svcunixfd_create (int fd, u_int sendsize, u_int recvsize)
  218. {
  219. return makefd_xprt (fd, sendsize, recvsize);
  220. }
  221. static SVCXPRT *
  222. internal_function
  223. makefd_xprt (int fd, u_int sendsize, u_int recvsize)
  224. {
  225. SVCXPRT *xprt;
  226. struct unix_conn *cd;
  227. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  228. cd = (struct unix_conn *) mem_alloc (sizeof (struct unix_conn));
  229. if (xprt == (SVCXPRT *) NULL || cd == (struct unix_conn *) NULL)
  230. {
  231. #ifdef USE_IN_LIBIO
  232. if (_IO_fwide (stderr, 0) > 0)
  233. (void) __fwprintf (stderr, L"%s",
  234. _("svc_unix: makefd_xprt: out of memory\n"));
  235. else
  236. #endif
  237. (void) fputs (_("svc_unix: makefd_xprt: out of memory\n"), stderr);
  238. mem_free (xprt, sizeof (SVCXPRT));
  239. mem_free (cd, sizeof (struct unix_conn));
  240. return NULL;
  241. }
  242. cd->strm_stat = XPRT_IDLE;
  243. xdrrec_create (&(cd->xdrs), sendsize, recvsize,
  244. (caddr_t) xprt, readunix, writeunix);
  245. xprt->xp_p2 = NULL;
  246. xprt->xp_p1 = (caddr_t) cd;
  247. xprt->xp_verf.oa_base = cd->verf_body;
  248. xprt->xp_addrlen = 0;
  249. xprt->xp_ops = &svcunix_op; /* truly deals with calls */
  250. xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
  251. xprt->xp_sock = fd;
  252. xprt_register (xprt);
  253. return xprt;
  254. }
  255. static bool_t
  256. rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg attribute_unused)
  257. {
  258. int sock;
  259. struct unix_rendezvous *r;
  260. struct sockaddr_un addr;
  261. struct sockaddr_in in_addr;
  262. socklen_t len;
  263. r = (struct unix_rendezvous *) xprt->xp_p1;
  264. again:
  265. len = sizeof (struct sockaddr_un);
  266. if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
  267. {
  268. if (errno == EINTR)
  269. goto again;
  270. return FALSE;
  271. }
  272. /*
  273. * make a new transporter (re-uses xprt)
  274. */
  275. memset (&in_addr, '\0', sizeof (in_addr));
  276. in_addr.sin_family = AF_UNIX;
  277. xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
  278. memcpy (&xprt->xp_raddr, &in_addr, sizeof (in_addr));
  279. xprt->xp_addrlen = len;
  280. return FALSE; /* there is never an rpc msg to be processed */
  281. }
  282. static enum xprt_stat
  283. rendezvous_stat (SVCXPRT *xprt attribute_unused)
  284. {
  285. return XPRT_IDLE;
  286. }
  287. static void
  288. svcunix_destroy (SVCXPRT *xprt)
  289. {
  290. struct unix_conn *cd = (struct unix_conn *) xprt->xp_p1;
  291. xprt_unregister (xprt);
  292. close (xprt->xp_sock);
  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 unix_conn));
  304. mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
  305. }
  306. #ifdef SCM_CREDENTIALS
  307. struct cmessage {
  308. struct cmsghdr cmsg;
  309. struct ucred cmcred;
  310. /* hack to make sure we have enough memory */
  311. char dummy[(CMSG_ALIGN (sizeof (struct ucred)) - sizeof (struct ucred) + sizeof (long))];
  312. };
  313. /* XXX This is not thread safe, but since the main functions in svc.c
  314. and the rpcgen generated *_svc functions for the daemon are also not
  315. thread safe and uses static global variables, it doesn't matter. */
  316. static struct cmessage cm;
  317. #endif
  318. static int
  319. __msgread (int sock, void *data, size_t cnt)
  320. {
  321. struct iovec iov;
  322. struct msghdr msg;
  323. int len;
  324. iov.iov_base = data;
  325. iov.iov_len = cnt;
  326. msg.msg_iov = &iov;
  327. msg.msg_iovlen = 1;
  328. msg.msg_name = NULL;
  329. msg.msg_namelen = 0;
  330. #ifdef SCM_CREDENTIALS
  331. msg.msg_control = (caddr_t) &cm;
  332. msg.msg_controllen = sizeof (struct cmessage);
  333. #endif
  334. msg.msg_flags = 0;
  335. #ifdef SO_PASSCRED
  336. {
  337. int on = 1;
  338. if (setsockopt (sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)))
  339. return -1;
  340. }
  341. #endif
  342. restart:
  343. len = recvmsg (sock, &msg, 0);
  344. if (len >= 0)
  345. {
  346. if (msg.msg_flags & MSG_CTRUNC || len == 0)
  347. return 0;
  348. else
  349. return len;
  350. }
  351. if (errno == EINTR)
  352. goto restart;
  353. return -1;
  354. }
  355. static int
  356. __msgwrite (int sock, void *data, size_t cnt)
  357. {
  358. #ifndef SCM_CREDENTIALS
  359. /* We cannot implement this reliably. */
  360. __set_errno (ENOSYS);
  361. return -1;
  362. #else
  363. struct iovec iov;
  364. struct msghdr msg;
  365. struct cmsghdr *cmsg = &cm.cmsg;
  366. struct ucred cred;
  367. int len;
  368. /* XXX I'm not sure, if gete?id() is always correct, or if we should use
  369. get?id(). But since keyserv needs geteuid(), we have no other chance.
  370. It would be much better, if the kernel could pass both to the server. */
  371. cred.pid = getpid ();
  372. cred.uid = geteuid ();
  373. cred.gid = getegid ();
  374. memcpy (CMSG_DATA(cmsg), &cred, sizeof (struct ucred));
  375. cmsg->cmsg_level = SOL_SOCKET;
  376. cmsg->cmsg_type = SCM_CREDENTIALS;
  377. cmsg->cmsg_len = sizeof(*cmsg) + sizeof(struct ucred);
  378. iov.iov_base = data;
  379. iov.iov_len = cnt;
  380. msg.msg_iov = &iov;
  381. msg.msg_iovlen = 1;
  382. msg.msg_name = NULL;
  383. msg.msg_namelen = 0;
  384. msg.msg_control = cmsg;
  385. msg.msg_controllen = CMSG_ALIGN(cmsg->cmsg_len);
  386. msg.msg_flags = 0;
  387. restart:
  388. len = sendmsg (sock, &msg, 0);
  389. if (len >= 0)
  390. return len;
  391. if (errno == EINTR)
  392. goto restart;
  393. return -1;
  394. #endif
  395. }
  396. /*
  397. * reads data from the unix connection.
  398. * any error is fatal and the connection is closed.
  399. * (And a read of zero bytes is a half closed stream => error.)
  400. */
  401. static int
  402. readunix (char *xprtptr, char *buf, int len)
  403. {
  404. SVCXPRT *xprt = (SVCXPRT *) xprtptr;
  405. int sock = xprt->xp_sock;
  406. int milliseconds = 35 * 1000;
  407. struct pollfd pollfd;
  408. do
  409. {
  410. pollfd.fd = sock;
  411. pollfd.events = POLLIN;
  412. switch (poll (&pollfd, 1, milliseconds))
  413. {
  414. case -1:
  415. if (errno == EINTR)
  416. continue;
  417. /*FALLTHROUGH*/
  418. case 0:
  419. goto fatal_err;
  420. default:
  421. if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
  422. || (pollfd.revents & POLLNVAL))
  423. goto fatal_err;
  424. break;
  425. }
  426. }
  427. while ((pollfd.revents & POLLIN) == 0);
  428. if ((len = __msgread (sock, buf, len)) > 0)
  429. return len;
  430. fatal_err:
  431. ((struct unix_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  432. return -1;
  433. }
  434. /*
  435. * writes data to the unix connection.
  436. * Any error is fatal and the connection is closed.
  437. */
  438. static int
  439. writeunix (char *xprtptr, char * buf, int len)
  440. {
  441. SVCXPRT *xprt = (SVCXPRT *) xprtptr;
  442. int i, cnt;
  443. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  444. {
  445. if ((i = __msgwrite (xprt->xp_sock, buf, cnt)) < 0)
  446. {
  447. ((struct unix_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  448. return -1;
  449. }
  450. }
  451. return len;
  452. }
  453. static enum xprt_stat
  454. svcunix_stat (SVCXPRT *xprt)
  455. {
  456. struct unix_conn *cd =
  457. (struct unix_conn *) (xprt->xp_p1);
  458. if (cd->strm_stat == XPRT_DIED)
  459. return XPRT_DIED;
  460. if (!xdrrec_eof (&(cd->xdrs)))
  461. return XPRT_MOREREQS;
  462. return XPRT_IDLE;
  463. }
  464. static bool_t
  465. svcunix_recv (SVCXPRT *xprt, struct rpc_msg *msg)
  466. {
  467. struct unix_conn *cd = (struct unix_conn *) (xprt->xp_p1);
  468. XDR *xdrs = &(cd->xdrs);
  469. xdrs->x_op = XDR_DECODE;
  470. xdrrec_skiprecord (xdrs);
  471. if (xdr_callmsg (xdrs, msg))
  472. {
  473. cd->x_id = msg->rm_xid;
  474. /* set up verifiers */
  475. #ifdef SCM_CREDENTIALS
  476. msg->rm_call.cb_verf.oa_flavor = AUTH_UNIX;
  477. msg->rm_call.cb_verf.oa_base = (caddr_t) &cm;
  478. msg->rm_call.cb_verf.oa_length = sizeof (cm);
  479. #endif
  480. return TRUE;
  481. }
  482. cd->strm_stat = XPRT_DIED; /* XXXX */
  483. return FALSE;
  484. }
  485. static bool_t
  486. svcunix_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  487. {
  488. return (*xdr_args) (&(((struct unix_conn *) (xprt->xp_p1))->xdrs),
  489. args_ptr);
  490. }
  491. static bool_t
  492. svcunix_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  493. {
  494. XDR *xdrs = &(((struct unix_conn *) (xprt->xp_p1))->xdrs);
  495. xdrs->x_op = XDR_FREE;
  496. return (*xdr_args) (xdrs, args_ptr);
  497. }
  498. static bool_t
  499. svcunix_reply (SVCXPRT *xprt, struct rpc_msg *msg)
  500. {
  501. struct unix_conn *cd = (struct unix_conn *) (xprt->xp_p1);
  502. XDR *xdrs = &(cd->xdrs);
  503. bool_t stat;
  504. xdrs->x_op = XDR_ENCODE;
  505. msg->rm_xid = cd->x_id;
  506. stat = xdr_replymsg (xdrs, msg);
  507. (void) xdrrec_endofrecord (xdrs, TRUE);
  508. return stat;
  509. }