svc_unix.c 14 KB

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