clnt_unix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * clnt_unix.c, Implements a TCP/IP based, client side RPC.
  3. *
  4. * Copyright (c) 2010, Oracle America, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. * * Neither the name of the "Oracle America, Inc." nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * TCP based RPC supports 'batched calls'.
  34. * A sequence of calls may be batched-up in a send buffer. The rpc call
  35. * return immediately to the client even though the call was not necessarily
  36. * sent. The batching occurs if the results' xdr routine is NULL (0) AND
  37. * the rpc timeout value is zero (see clnt.h, rpc).
  38. *
  39. * Clients should NOT casually batch calls that in fact return results; that is,
  40. * the server side should be aware that a call is batched and not produce any
  41. * return message. Batched calls that produce many result messages can
  42. * deadlock (netlock) the client and the server....
  43. *
  44. * Now go hang yourself.
  45. */
  46. #include <netdb.h>
  47. #include <errno.h>
  48. #include <stdio.h>
  49. #include <unistd.h>
  50. #include <libintl.h>
  51. #include <rpc/rpc.h>
  52. #include <sys/uio.h>
  53. #include <sys/poll.h>
  54. #include <sys/socket.h>
  55. #include <rpc/pmap_clnt.h>
  56. #include <wchar.h>
  57. #include <shlib-compat.h>
  58. extern u_long _create_xid (void);
  59. #define MCALL_MSG_SIZE 24
  60. struct ct_data
  61. {
  62. int ct_sock;
  63. bool_t ct_closeit;
  64. struct timeval ct_wait;
  65. bool_t ct_waitset; /* wait set by clnt_control? */
  66. struct sockaddr_un ct_addr;
  67. struct rpc_err ct_error;
  68. char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
  69. u_int ct_mpos; /* pos after marshal */
  70. XDR ct_xdrs;
  71. };
  72. static int readunix (char *, char *, int);
  73. static int writeunix (char *, char *, int);
  74. static enum clnt_stat clntunix_call (CLIENT *, u_long, xdrproc_t, caddr_t,
  75. xdrproc_t, caddr_t, struct timeval);
  76. static void clntunix_abort (void);
  77. static void clntunix_geterr (CLIENT *, struct rpc_err *);
  78. static bool_t clntunix_freeres (CLIENT *, xdrproc_t, caddr_t);
  79. static bool_t clntunix_control (CLIENT *, int, char *);
  80. static void clntunix_destroy (CLIENT *);
  81. static const struct clnt_ops unix_ops =
  82. {
  83. clntunix_call,
  84. clntunix_abort,
  85. clntunix_geterr,
  86. clntunix_freeres,
  87. clntunix_destroy,
  88. clntunix_control
  89. };
  90. /*
  91. * Create a client handle for a tcp/ip connection.
  92. * If *sockp<0, *sockp is set to a newly created TCP socket and it is
  93. * connected to raddr. If *sockp non-negative then
  94. * raddr is ignored. The rpc/tcp package does buffering
  95. * similar to stdio, so the client must pick send and receive buffer sizes,];
  96. * 0 => use the default.
  97. * If raddr->sin_port is 0, then a binder on the remote machine is
  98. * consulted for the right port number.
  99. * NB: *sockp is copied into a private area.
  100. * NB: It is the clients responsibility to close *sockp.
  101. * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
  102. * something more useful.
  103. */
  104. CLIENT *
  105. clntunix_create (struct sockaddr_un *raddr, u_long prog, u_long vers,
  106. int *sockp, u_int sendsz, u_int recvsz)
  107. {
  108. CLIENT *h;
  109. struct ct_data *ct = (struct ct_data *) mem_alloc (sizeof (*ct));
  110. struct rpc_msg call_msg;
  111. int len;
  112. h = (CLIENT *) mem_alloc (sizeof (*h));
  113. if (h == NULL || ct == NULL)
  114. {
  115. struct rpc_createerr *ce = &get_rpc_createerr ();
  116. (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
  117. ce->cf_stat = RPC_SYSTEMERROR;
  118. ce->cf_error.re_errno = ENOMEM;
  119. goto fooy;
  120. }
  121. /*
  122. * If no socket given, open one
  123. */
  124. if (*sockp < 0)
  125. {
  126. *sockp = __socket (AF_UNIX, SOCK_STREAM, 0);
  127. len = strlen (raddr->sun_path) + sizeof (raddr->sun_family) + 1;
  128. if (*sockp < 0
  129. || __connect (*sockp, (struct sockaddr *) raddr, len) < 0)
  130. {
  131. struct rpc_createerr *ce = &get_rpc_createerr ();
  132. ce->cf_stat = RPC_SYSTEMERROR;
  133. ce->cf_error.re_errno = errno;
  134. if (*sockp != -1)
  135. __close (*sockp);
  136. goto fooy;
  137. }
  138. ct->ct_closeit = TRUE;
  139. }
  140. else
  141. {
  142. ct->ct_closeit = FALSE;
  143. }
  144. /*
  145. * Set up private data struct
  146. */
  147. ct->ct_sock = *sockp;
  148. ct->ct_wait.tv_usec = 0;
  149. ct->ct_waitset = FALSE;
  150. ct->ct_addr = *raddr;
  151. /*
  152. * Initialize call message
  153. */
  154. call_msg.rm_xid = _create_xid ();
  155. call_msg.rm_direction = CALL;
  156. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  157. call_msg.rm_call.cb_prog = prog;
  158. call_msg.rm_call.cb_vers = vers;
  159. /*
  160. * pre-serialize the static part of the call msg and stash it away
  161. */
  162. xdrmem_create (&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE, XDR_ENCODE);
  163. if (!xdr_callhdr (&(ct->ct_xdrs), &call_msg))
  164. {
  165. if (ct->ct_closeit)
  166. __close (*sockp);
  167. goto fooy;
  168. }
  169. ct->ct_mpos = XDR_GETPOS (&(ct->ct_xdrs));
  170. XDR_DESTROY (&(ct->ct_xdrs));
  171. /*
  172. * Create a client handle which uses xdrrec for serialization
  173. * and authnone for authentication.
  174. */
  175. xdrrec_create (&(ct->ct_xdrs), sendsz, recvsz,
  176. (caddr_t) ct, readunix, writeunix);
  177. h->cl_ops = (struct clnt_ops *) &unix_ops;
  178. h->cl_private = (caddr_t) ct;
  179. h->cl_auth = authnone_create ();
  180. return h;
  181. fooy:
  182. /*
  183. * Something goofed, free stuff and barf
  184. */
  185. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  186. mem_free ((caddr_t) h, sizeof (CLIENT));
  187. return (CLIENT *) NULL;
  188. }
  189. libc_hidden_nolink_sunrpc (clntunix_create, GLIBC_2_1)
  190. static enum clnt_stat
  191. clntunix_call (CLIENT *h, u_long proc, xdrproc_t xdr_args, caddr_t args_ptr,
  192. xdrproc_t xdr_results, caddr_t results_ptr,
  193. struct timeval timeout)
  194. {
  195. struct ct_data *ct = (struct ct_data *) h->cl_private;
  196. XDR *xdrs = &(ct->ct_xdrs);
  197. struct rpc_msg reply_msg;
  198. u_long x_id;
  199. uint32_t *msg_x_id = (uint32_t *) (ct->ct_mcall); /* yuk */
  200. bool_t shipnow;
  201. int refreshes = 2;
  202. if (!ct->ct_waitset)
  203. {
  204. ct->ct_wait = timeout;
  205. }
  206. shipnow =
  207. (xdr_results == (xdrproc_t) 0 && ct->ct_wait.tv_sec == 0
  208. && ct->ct_wait.tv_usec == 0) ? FALSE : TRUE;
  209. call_again:
  210. xdrs->x_op = XDR_ENCODE;
  211. ct->ct_error.re_status = RPC_SUCCESS;
  212. x_id = ntohl (--(*msg_x_id));
  213. if ((!XDR_PUTBYTES (xdrs, ct->ct_mcall, ct->ct_mpos)) ||
  214. (!XDR_PUTLONG (xdrs, (long *) &proc)) ||
  215. (!AUTH_MARSHALL (h->cl_auth, xdrs)) ||
  216. (!(*xdr_args) (xdrs, args_ptr)))
  217. {
  218. if (ct->ct_error.re_status == RPC_SUCCESS)
  219. ct->ct_error.re_status = RPC_CANTENCODEARGS;
  220. (void) xdrrec_endofrecord (xdrs, TRUE);
  221. return ct->ct_error.re_status;
  222. }
  223. if (!xdrrec_endofrecord (xdrs, shipnow))
  224. return ct->ct_error.re_status = RPC_CANTSEND;
  225. if (!shipnow)
  226. return RPC_SUCCESS;
  227. /*
  228. * Hack to provide rpc-based message passing
  229. */
  230. if (ct->ct_wait.tv_sec == 0 && ct->ct_wait.tv_usec == 0)
  231. return ct->ct_error.re_status = RPC_TIMEDOUT;
  232. /*
  233. * Keep receiving until we get a valid transaction id
  234. */
  235. xdrs->x_op = XDR_DECODE;
  236. while (TRUE)
  237. {
  238. reply_msg.acpted_rply.ar_verf = _null_auth;
  239. reply_msg.acpted_rply.ar_results.where = NULL;
  240. reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
  241. if (!xdrrec_skiprecord (xdrs))
  242. return ct->ct_error.re_status;
  243. /* now decode and validate the response header */
  244. if (!xdr_replymsg (xdrs, &reply_msg))
  245. {
  246. if (ct->ct_error.re_status == RPC_SUCCESS)
  247. continue;
  248. return ct->ct_error.re_status;
  249. }
  250. if (reply_msg.rm_xid == x_id)
  251. break;
  252. }
  253. /*
  254. * process header
  255. */
  256. _seterr_reply (&reply_msg, &(ct->ct_error));
  257. if (ct->ct_error.re_status == RPC_SUCCESS)
  258. {
  259. if (!AUTH_VALIDATE (h->cl_auth, &reply_msg.acpted_rply.ar_verf))
  260. {
  261. ct->ct_error.re_status = RPC_AUTHERROR;
  262. ct->ct_error.re_why = AUTH_INVALIDRESP;
  263. }
  264. else if (!(*xdr_results) (xdrs, results_ptr))
  265. {
  266. if (ct->ct_error.re_status == RPC_SUCCESS)
  267. ct->ct_error.re_status = RPC_CANTDECODERES;
  268. }
  269. /* free verifier ... */
  270. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  271. {
  272. xdrs->x_op = XDR_FREE;
  273. (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
  274. }
  275. } /* end successful completion */
  276. else
  277. {
  278. /* maybe our credentials need to be refreshed ... */
  279. if (refreshes-- && AUTH_REFRESH (h->cl_auth))
  280. goto call_again;
  281. } /* end of unsuccessful completion */
  282. return ct->ct_error.re_status;
  283. }
  284. static void
  285. clntunix_geterr (CLIENT *h, struct rpc_err *errp)
  286. {
  287. struct ct_data *ct = (struct ct_data *) h->cl_private;
  288. *errp = ct->ct_error;
  289. }
  290. static bool_t
  291. clntunix_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
  292. {
  293. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  294. XDR *xdrs = &(ct->ct_xdrs);
  295. xdrs->x_op = XDR_FREE;
  296. return (*xdr_res) (xdrs, res_ptr);
  297. }
  298. static void
  299. clntunix_abort (void)
  300. {
  301. }
  302. static bool_t
  303. clntunix_control (CLIENT *cl, int request, char *info)
  304. {
  305. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  306. u_long ul;
  307. uint32_t ui32;
  308. switch (request)
  309. {
  310. case CLSET_FD_CLOSE:
  311. ct->ct_closeit = TRUE;
  312. break;
  313. case CLSET_FD_NCLOSE:
  314. ct->ct_closeit = FALSE;
  315. break;
  316. case CLSET_TIMEOUT:
  317. ct->ct_wait = *(struct timeval *) info;
  318. break;
  319. case CLGET_TIMEOUT:
  320. *(struct timeval *) info = ct->ct_wait;
  321. break;
  322. case CLGET_SERVER_ADDR:
  323. *(struct sockaddr_un *) info = ct->ct_addr;
  324. break;
  325. case CLGET_FD:
  326. *(int *)info = ct->ct_sock;
  327. break;
  328. case CLGET_XID:
  329. /*
  330. * use the knowledge that xid is the
  331. * first element in the call structure *.
  332. * This will get the xid of the PREVIOUS call
  333. */
  334. memcpy (&ui32, ct->ct_mcall, sizeof (ui32));
  335. ul = ntohl (ui32);
  336. memcpy (info, &ul, sizeof (ul));
  337. break;
  338. case CLSET_XID:
  339. /* This will set the xid of the NEXT call */
  340. memcpy (&ul, info, sizeof (ul));
  341. ui32 = htonl (ul - 1);
  342. memcpy (ct->ct_mcall, &ui32, sizeof (ui32));
  343. /* decrement by 1 as clntunix_call() increments once */
  344. break;
  345. case CLGET_VERS:
  346. /*
  347. * This RELIES on the information that, in the call body,
  348. * the version number field is the fifth field from the
  349. * beginning of the RPC header. MUST be changed if the
  350. * call_struct is changed
  351. */
  352. memcpy (&ui32, ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT, sizeof (ui32));
  353. ul = ntohl (ui32);
  354. memcpy (info, &ul, sizeof (ul));
  355. break;
  356. case CLSET_VERS:
  357. memcpy (&ul, info, sizeof (ul));
  358. ui32 = htonl (ul);
  359. memcpy (ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
  360. break;
  361. case CLGET_PROG:
  362. /*
  363. * This RELIES on the information that, in the call body,
  364. * the program number field is the field from the
  365. * beginning of the RPC header. MUST be changed if the
  366. * call_struct is changed
  367. */
  368. memcpy (&ui32, ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT, sizeof (ui32));
  369. ul = ntohl (ui32);
  370. memcpy (info, &ul, sizeof (ul));
  371. break;
  372. case CLSET_PROG:
  373. memcpy (&ul, info, sizeof (ul));
  374. ui32 = htonl (ul);
  375. memcpy (ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
  376. break;
  377. /* The following are only possible with TI-RPC */
  378. case CLGET_RETRY_TIMEOUT:
  379. case CLSET_RETRY_TIMEOUT:
  380. case CLGET_SVC_ADDR:
  381. case CLSET_SVC_ADDR:
  382. case CLSET_PUSH_TIMOD:
  383. case CLSET_POP_TIMOD:
  384. default:
  385. return FALSE;
  386. }
  387. return TRUE;
  388. }
  389. static void
  390. clntunix_destroy (CLIENT *h)
  391. {
  392. struct ct_data *ct =
  393. (struct ct_data *) h->cl_private;
  394. if (ct->ct_closeit)
  395. {
  396. (void) __close (ct->ct_sock);
  397. }
  398. XDR_DESTROY (&(ct->ct_xdrs));
  399. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  400. mem_free ((caddr_t) h, sizeof (CLIENT));
  401. }
  402. static int
  403. __msgread (int sock, void *data, size_t cnt)
  404. {
  405. struct iovec iov;
  406. struct msghdr msg;
  407. #ifdef SCM_CREDENTIALS
  408. static char cm[CMSG_SPACE(sizeof (struct ucred))];
  409. #endif
  410. int len;
  411. iov.iov_base = data;
  412. iov.iov_len = cnt;
  413. msg.msg_iov = &iov;
  414. msg.msg_iovlen = 1;
  415. msg.msg_name = NULL;
  416. msg.msg_namelen = 0;
  417. #ifdef SCM_CREDENTIALS
  418. msg.msg_control = (caddr_t) &cm;
  419. msg.msg_controllen = CMSG_SPACE(sizeof (struct ucred));
  420. #endif
  421. msg.msg_flags = 0;
  422. #ifdef SO_PASSCRED
  423. {
  424. int on = 1;
  425. if (__setsockopt (sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)))
  426. return -1;
  427. }
  428. #endif
  429. restart:
  430. len = __recvmsg (sock, &msg, 0);
  431. if (len >= 0)
  432. {
  433. if (msg.msg_flags & MSG_CTRUNC || len == 0)
  434. return 0;
  435. else
  436. return len;
  437. }
  438. if (errno == EINTR)
  439. goto restart;
  440. return -1;
  441. }
  442. static int
  443. __msgwrite (int sock, void *data, size_t cnt)
  444. {
  445. #ifndef SCM_CREDENTIALS
  446. /* We cannot implement this reliably. */
  447. __set_errno (ENOSYS);
  448. return -1;
  449. #else
  450. struct iovec iov;
  451. struct msghdr msg;
  452. struct cmsghdr *cmsg = alloca (CMSG_SPACE(sizeof (struct ucred)));
  453. struct ucred cred;
  454. int len;
  455. /* XXX I'm not sure, if gete?id() is always correct, or if we should use
  456. get?id(). But since keyserv needs geteuid(), we have no other chance.
  457. It would be much better, if the kernel could pass both to the server. */
  458. cred.pid = __getpid ();
  459. cred.uid = __geteuid ();
  460. cred.gid = __getegid ();
  461. memcpy (CMSG_DATA(cmsg), &cred, sizeof (struct ucred));
  462. cmsg->cmsg_level = SOL_SOCKET;
  463. cmsg->cmsg_type = SCM_CREDENTIALS;
  464. cmsg->cmsg_len = sizeof(*cmsg) + sizeof(struct ucred);
  465. iov.iov_base = data;
  466. iov.iov_len = cnt;
  467. msg.msg_iov = &iov;
  468. msg.msg_iovlen = 1;
  469. msg.msg_name = NULL;
  470. msg.msg_namelen = 0;
  471. msg.msg_control = cmsg;
  472. msg.msg_controllen = CMSG_ALIGN(cmsg->cmsg_len);
  473. msg.msg_flags = 0;
  474. restart:
  475. len = __sendmsg (sock, &msg, 0);
  476. if (len >= 0)
  477. return len;
  478. if (errno == EINTR)
  479. goto restart;
  480. return -1;
  481. #endif
  482. }
  483. /*
  484. * Interface between xdr serializer and unix connection.
  485. * Behaves like the system calls, read & write, but keeps some error state
  486. * around for the rpc level.
  487. */
  488. static int
  489. readunix (char *ctptr, char *buf, int len)
  490. {
  491. struct ct_data *ct = (struct ct_data *) ctptr;
  492. struct pollfd fd;
  493. int milliseconds = ((ct->ct_wait.tv_sec * 1000)
  494. + (ct->ct_wait.tv_usec / 1000));
  495. if (len == 0)
  496. return 0;
  497. fd.fd = ct->ct_sock;
  498. fd.events = POLLIN;
  499. while (TRUE)
  500. {
  501. switch (__poll (&fd, 1, milliseconds))
  502. {
  503. case 0:
  504. ct->ct_error.re_status = RPC_TIMEDOUT;
  505. return -1;
  506. case -1:
  507. if (errno == EINTR)
  508. continue;
  509. ct->ct_error.re_status = RPC_CANTRECV;
  510. ct->ct_error.re_errno = errno;
  511. return -1;
  512. }
  513. break;
  514. }
  515. switch (len = __msgread (ct->ct_sock, buf, len))
  516. {
  517. case 0:
  518. /* premature eof */
  519. ct->ct_error.re_errno = ECONNRESET;
  520. ct->ct_error.re_status = RPC_CANTRECV;
  521. len = -1; /* it's really an error */
  522. break;
  523. case -1:
  524. ct->ct_error.re_errno = errno;
  525. ct->ct_error.re_status = RPC_CANTRECV;
  526. break;
  527. }
  528. return len;
  529. }
  530. static int
  531. writeunix (char *ctptr, char *buf, int len)
  532. {
  533. int i, cnt;
  534. struct ct_data *ct = (struct ct_data *) ctptr;
  535. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  536. {
  537. if ((i = __msgwrite (ct->ct_sock, buf, cnt)) == -1)
  538. {
  539. ct->ct_error.re_errno = errno;
  540. ct->ct_error.re_status = RPC_CANTSEND;
  541. return -1;
  542. }
  543. }
  544. return len;
  545. }