svc_unix.c 14 KB

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