svc_unix.c 14 KB

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