svc_unix.c 14 KB

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