clnt_unix.c 16 KB

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