svc_unix.c 14 KB

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