clnt_unix.c 16 KB

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