svc_unix.c 14 KB

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