clnt_unix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. * clnt_unix.c, Implements a TCP/IP based, client side RPC.
  31. *
  32. * Copyright (C) 1984, Sun Microsystems, Inc.
  33. *
  34. * TCP based RPC supports 'batched calls'.
  35. * A sequence of calls may be batched-up in a send buffer. The rpc call
  36. * return immediately to the client even though the call was not necessarily
  37. * sent. The batching occurs if the results' xdr routine is NULL (0) AND
  38. * the rpc timeout value is zero (see clnt.h, rpc).
  39. *
  40. * Clients should NOT casually batch calls that in fact return results; that is,
  41. * the server side should be aware that a call is batched and not produce any
  42. * return message. Batched calls that produce many result messages can
  43. * deadlock (netlock) the client and the server....
  44. *
  45. * Now go hang yourself.
  46. */
  47. #include <netdb.h>
  48. #include <errno.h>
  49. #include <stdio.h>
  50. #include <unistd.h>
  51. #include "rpc_private.h"
  52. #include <sys/uio.h>
  53. #include <sys/poll.h>
  54. #include <sys/socket.h>
  55. #include <rpc/pmap_clnt.h>
  56. #ifdef USE_IN_LIBIO
  57. # include <wchar.h>
  58. #endif
  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. #ifdef USE_IN_LIBIO
  117. if (_IO_fwide (stderr, 0) > 0)
  118. (void) fwprintf (stderr, L"%s",
  119. _("clntunix_create: out of memory\n"));
  120. else
  121. #endif
  122. (void) fputs (_("clntunix_create: out of memory\n"), stderr);
  123. ce->cf_stat = RPC_SYSTEMERROR;
  124. ce->cf_error.re_errno = ENOMEM;
  125. goto fooy;
  126. }
  127. /*
  128. * If no socket given, open one
  129. */
  130. if (*sockp < 0)
  131. {
  132. *sockp = socket (AF_UNIX, SOCK_STREAM, 0);
  133. len = strlen (raddr->sun_path) + sizeof (raddr->sun_family) + 1;
  134. if (*sockp < 0
  135. || connect (*sockp, (struct sockaddr *) raddr, len) < 0)
  136. {
  137. struct rpc_createerr *ce = &get_rpc_createerr ();
  138. ce->cf_stat = RPC_SYSTEMERROR;
  139. ce->cf_error.re_errno = errno;
  140. if (*sockp != -1)
  141. close (*sockp);
  142. goto fooy;
  143. }
  144. ct->ct_closeit = TRUE;
  145. }
  146. else
  147. {
  148. ct->ct_closeit = FALSE;
  149. }
  150. /*
  151. * Set up private data struct
  152. */
  153. ct->ct_sock = *sockp;
  154. ct->ct_wait.tv_usec = 0;
  155. ct->ct_waitset = FALSE;
  156. ct->ct_addr = *raddr;
  157. /*
  158. * Initialize call message
  159. */
  160. call_msg.rm_xid = _create_xid ();
  161. call_msg.rm_direction = CALL;
  162. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  163. call_msg.rm_call.cb_prog = prog;
  164. call_msg.rm_call.cb_vers = vers;
  165. /*
  166. * pre-serialize the static part of the call msg and stash it away
  167. */
  168. xdrmem_create (&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE, XDR_ENCODE);
  169. if (!xdr_callhdr (&(ct->ct_xdrs), &call_msg))
  170. {
  171. if (ct->ct_closeit)
  172. close (*sockp);
  173. goto fooy;
  174. }
  175. ct->ct_mpos = XDR_GETPOS (&(ct->ct_xdrs));
  176. XDR_DESTROY (&(ct->ct_xdrs));
  177. /*
  178. * Create a client handle which uses xdrrec for serialization
  179. * and authnone for authentication.
  180. */
  181. xdrrec_create (&(ct->ct_xdrs), sendsz, recvsz,
  182. (caddr_t) ct, readunix, writeunix);
  183. h->cl_ops = &unix_ops;
  184. h->cl_private = (caddr_t) ct;
  185. h->cl_auth = authnone_create ();
  186. return h;
  187. fooy:
  188. /*
  189. * Something goofed, free stuff and barf
  190. */
  191. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  192. mem_free ((caddr_t) h, sizeof (CLIENT));
  193. return (CLIENT *) NULL;
  194. }
  195. libc_hidden_def(clntunix_create)
  196. static enum clnt_stat
  197. clntunix_call (CLIENT *h, u_long proc, xdrproc_t xdr_args, caddr_t args_ptr,
  198. xdrproc_t xdr_results, caddr_t results_ptr,
  199. struct timeval timeout)
  200. {
  201. struct ct_data *ct = (struct ct_data *) h->cl_private;
  202. XDR *xdrs = &(ct->ct_xdrs);
  203. struct rpc_msg reply_msg;
  204. u_long x_id;
  205. u_int32_t *msg_x_id = (u_int32_t *) (ct->ct_mcall); /* yuk */
  206. bool_t shipnow;
  207. int refreshes = 2;
  208. if (!ct->ct_waitset)
  209. {
  210. ct->ct_wait = timeout;
  211. }
  212. shipnow =
  213. (xdr_results == (xdrproc_t) 0 && ct->ct_wait.tv_sec == 0
  214. && ct->ct_wait.tv_usec == 0) ? FALSE : TRUE;
  215. call_again:
  216. xdrs->x_op = XDR_ENCODE;
  217. ct->ct_error.re_status = RPC_SUCCESS;
  218. x_id = ntohl (--(*msg_x_id));
  219. if ((!XDR_PUTBYTES (xdrs, ct->ct_mcall, ct->ct_mpos)) ||
  220. (!XDR_PUTLONG (xdrs, (long *) &proc)) ||
  221. (!AUTH_MARSHALL (h->cl_auth, xdrs)) ||
  222. (!(*xdr_args) (xdrs, args_ptr)))
  223. {
  224. if (ct->ct_error.re_status == RPC_SUCCESS)
  225. ct->ct_error.re_status = RPC_CANTENCODEARGS;
  226. (void) xdrrec_endofrecord (xdrs, TRUE);
  227. return ct->ct_error.re_status;
  228. }
  229. if (!xdrrec_endofrecord (xdrs, shipnow))
  230. return ct->ct_error.re_status = RPC_CANTSEND;
  231. if (!shipnow)
  232. return RPC_SUCCESS;
  233. /*
  234. * Hack to provide rpc-based message passing
  235. */
  236. if (ct->ct_wait.tv_sec == 0 && ct->ct_wait.tv_usec == 0)
  237. return ct->ct_error.re_status = RPC_TIMEDOUT;
  238. /*
  239. * Keep receiving until we get a valid transaction id
  240. */
  241. xdrs->x_op = XDR_DECODE;
  242. while (TRUE)
  243. {
  244. reply_msg.acpted_rply.ar_verf = _null_auth;
  245. reply_msg.acpted_rply.ar_results.where = NULL;
  246. reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
  247. if (!xdrrec_skiprecord (xdrs))
  248. return ct->ct_error.re_status;
  249. /* now decode and validate the response header */
  250. if (!xdr_replymsg (xdrs, &reply_msg))
  251. {
  252. if (ct->ct_error.re_status == RPC_SUCCESS)
  253. continue;
  254. return ct->ct_error.re_status;
  255. }
  256. if (reply_msg.rm_xid == x_id)
  257. break;
  258. }
  259. /*
  260. * process header
  261. */
  262. _seterr_reply (&reply_msg, &(ct->ct_error));
  263. if (ct->ct_error.re_status == RPC_SUCCESS)
  264. {
  265. if (!AUTH_VALIDATE (h->cl_auth, &reply_msg.acpted_rply.ar_verf))
  266. {
  267. ct->ct_error.re_status = RPC_AUTHERROR;
  268. ct->ct_error.re_why = AUTH_INVALIDRESP;
  269. }
  270. else if (!(*xdr_results) (xdrs, results_ptr))
  271. {
  272. if (ct->ct_error.re_status == RPC_SUCCESS)
  273. ct->ct_error.re_status = RPC_CANTDECODERES;
  274. }
  275. /* free verifier ... */
  276. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  277. {
  278. xdrs->x_op = XDR_FREE;
  279. (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
  280. }
  281. } /* end successful completion */
  282. else
  283. {
  284. /* maybe our credentials need to be refreshed ... */
  285. if (refreshes-- && AUTH_REFRESH (h->cl_auth))
  286. goto call_again;
  287. } /* end of unsuccessful completion */
  288. return ct->ct_error.re_status;
  289. }
  290. static void
  291. clntunix_geterr (CLIENT *h, struct rpc_err *errp)
  292. {
  293. struct ct_data *ct = (struct ct_data *) h->cl_private;
  294. *errp = ct->ct_error;
  295. }
  296. static bool_t
  297. clntunix_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
  298. {
  299. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  300. XDR *xdrs = &(ct->ct_xdrs);
  301. xdrs->x_op = XDR_FREE;
  302. return (*xdr_res) (xdrs, res_ptr);
  303. }
  304. static void
  305. clntunix_abort (void)
  306. {
  307. }
  308. static bool_t
  309. clntunix_control (CLIENT *cl, int request, char *info)
  310. {
  311. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  312. switch (request)
  313. {
  314. case CLSET_FD_CLOSE:
  315. ct->ct_closeit = TRUE;
  316. break;
  317. case CLSET_FD_NCLOSE:
  318. ct->ct_closeit = FALSE;
  319. break;
  320. case CLSET_TIMEOUT:
  321. ct->ct_wait = *(struct timeval *) info;
  322. break;
  323. case CLGET_TIMEOUT:
  324. *(struct timeval *) info = ct->ct_wait;
  325. break;
  326. case CLGET_SERVER_ADDR:
  327. *(struct sockaddr_un *) info = ct->ct_addr;
  328. break;
  329. case CLGET_FD:
  330. *(int *)info = ct->ct_sock;
  331. break;
  332. case CLGET_XID:
  333. /*
  334. * use the knowledge that xid is the
  335. * first element in the call structure *.
  336. * This will get the xid of the PREVIOUS call
  337. */
  338. *(u_long *) info = ntohl (*(u_long *)ct->ct_mcall);
  339. break;
  340. case CLSET_XID:
  341. /* This will set the xid of the NEXT call */
  342. *(u_long *) ct->ct_mcall = htonl (*(u_long *)info - 1);
  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. * begining of the RPC header. MUST be changed if the
  350. * call_struct is changed
  351. */
  352. *(u_long *) info = ntohl (*(u_long *) (ct->ct_mcall
  353. + 4 * BYTES_PER_XDR_UNIT));
  354. break;
  355. case CLSET_VERS:
  356. *(u_long *) (ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
  357. = htonl (*(u_long *) info);
  358. break;
  359. case CLGET_PROG:
  360. /*
  361. * This RELIES on the information that, in the call body,
  362. * the program number field is the field from the
  363. * begining of the RPC header. MUST be changed if the
  364. * call_struct is changed
  365. */
  366. *(u_long *) info = ntohl (*(u_long *) (ct->ct_mcall
  367. + 3 * BYTES_PER_XDR_UNIT));
  368. break;
  369. case CLSET_PROG:
  370. *(u_long *) (ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
  371. = htonl(*(u_long *) info);
  372. break;
  373. /* The following are only possible with TI-RPC */
  374. case CLGET_RETRY_TIMEOUT:
  375. case CLSET_RETRY_TIMEOUT:
  376. case CLGET_SVC_ADDR:
  377. case CLSET_SVC_ADDR:
  378. case CLSET_PUSH_TIMOD:
  379. case CLSET_POP_TIMOD:
  380. default:
  381. return FALSE;
  382. }
  383. return TRUE;
  384. }
  385. static void
  386. clntunix_destroy (CLIENT *h)
  387. {
  388. struct ct_data *ct =
  389. (struct ct_data *) h->cl_private;
  390. if (ct->ct_closeit)
  391. {
  392. (void) close (ct->ct_sock);
  393. }
  394. XDR_DESTROY (&(ct->ct_xdrs));
  395. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  396. mem_free ((caddr_t) h, sizeof (CLIENT));
  397. }
  398. static int
  399. __msgread (int sock, void *data, size_t cnt)
  400. {
  401. struct iovec iov;
  402. struct msghdr msg;
  403. #ifdef SCM_CREDENTIALS
  404. /*static -why??*/ char cm[CMSG_SPACE(sizeof (struct ucred))];
  405. #endif
  406. int len;
  407. iov.iov_base = data;
  408. iov.iov_len = cnt;
  409. msg.msg_iov = &iov;
  410. msg.msg_iovlen = 1;
  411. msg.msg_name = NULL;
  412. msg.msg_namelen = 0;
  413. #ifdef SCM_CREDENTIALS
  414. msg.msg_control = (caddr_t) &cm;
  415. msg.msg_controllen = CMSG_SPACE(sizeof (struct ucred));
  416. #endif
  417. msg.msg_flags = 0;
  418. #ifdef SO_PASSCRED
  419. {
  420. int on = 1;
  421. if (setsockopt (sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)))
  422. return -1;
  423. }
  424. #endif
  425. restart:
  426. len = recvmsg (sock, &msg, 0);
  427. if (len >= 0)
  428. {
  429. if (msg.msg_flags & MSG_CTRUNC || len == 0)
  430. return 0;
  431. else
  432. return len;
  433. }
  434. if (errno == EINTR)
  435. goto restart;
  436. return -1;
  437. }
  438. static int
  439. __msgwrite (int sock, void *data, size_t cnt)
  440. {
  441. #ifndef SCM_CREDENTIALS
  442. /* We cannot implement this reliably. */
  443. __set_errno (ENOSYS);
  444. return -1;
  445. #else
  446. struct iovec iov;
  447. struct msghdr msg;
  448. struct cmsghdr *cmsg = alloca (CMSG_SPACE(sizeof (struct ucred)));
  449. struct ucred cred;
  450. int len;
  451. /* XXX I'm not sure, if gete?id() is always correct, or if we should use
  452. get?id(). But since keyserv needs geteuid(), we have no other chance.
  453. It would be much better, if the kernel could pass both to the server. */
  454. cred.pid = getpid ();
  455. cred.uid = geteuid ();
  456. cred.gid = getegid ();
  457. memcpy (CMSG_DATA(cmsg), &cred, sizeof (struct ucred));
  458. cmsg->cmsg_level = SOL_SOCKET;
  459. cmsg->cmsg_type = SCM_CREDENTIALS;
  460. cmsg->cmsg_len = sizeof(*cmsg) + sizeof(struct ucred);
  461. iov.iov_base = data;
  462. iov.iov_len = cnt;
  463. msg.msg_iov = &iov;
  464. msg.msg_iovlen = 1;
  465. msg.msg_name = NULL;
  466. msg.msg_namelen = 0;
  467. msg.msg_control = cmsg;
  468. msg.msg_controllen = CMSG_ALIGN(cmsg->cmsg_len);
  469. msg.msg_flags = 0;
  470. restart:
  471. len = sendmsg (sock, &msg, 0);
  472. if (len >= 0)
  473. return len;
  474. if (errno == EINTR)
  475. goto restart;
  476. return -1;
  477. #endif
  478. }
  479. /*
  480. * Interface between xdr serializer and unix connection.
  481. * Behaves like the system calls, read & write, but keeps some error state
  482. * around for the rpc level.
  483. */
  484. static int
  485. readunix (char *ctptr, char *buf, int len)
  486. {
  487. struct ct_data *ct = (struct ct_data *) ctptr;
  488. struct pollfd fd;
  489. int milliseconds = ((ct->ct_wait.tv_sec * 1000)
  490. + (ct->ct_wait.tv_usec / 1000));
  491. if (len == 0)
  492. return 0;
  493. fd.fd = ct->ct_sock;
  494. fd.events = POLLIN;
  495. while (TRUE)
  496. {
  497. switch (poll (&fd, 1, milliseconds))
  498. {
  499. case 0:
  500. ct->ct_error.re_status = RPC_TIMEDOUT;
  501. return -1;
  502. case -1:
  503. if (errno == EINTR)
  504. continue;
  505. ct->ct_error.re_status = RPC_CANTRECV;
  506. ct->ct_error.re_errno = errno;
  507. return -1;
  508. }
  509. break;
  510. }
  511. switch (len = __msgread (ct->ct_sock, buf, len))
  512. {
  513. case 0:
  514. /* premature eof */
  515. ct->ct_error.re_errno = ECONNRESET;
  516. ct->ct_error.re_status = RPC_CANTRECV;
  517. len = -1; /* it's really an error */
  518. break;
  519. case -1:
  520. ct->ct_error.re_errno = errno;
  521. ct->ct_error.re_status = RPC_CANTRECV;
  522. break;
  523. }
  524. return len;
  525. }
  526. static int
  527. writeunix (char *ctptr, char *buf, int len)
  528. {
  529. int i, cnt;
  530. struct ct_data *ct = (struct ct_data *) ctptr;
  531. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  532. {
  533. if ((i = __msgwrite (ct->ct_sock, buf, cnt)) == -1)
  534. {
  535. ct->ct_error.re_errno = errno;
  536. ct->ct_error.re_status = RPC_CANTSEND;
  537. return -1;
  538. }
  539. }
  540. return len;
  541. }