clnt_unix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 (CLIENT *h, u_long proc, xdrproc_t xdr_args, caddr_t args_ptr,
  201. xdrproc_t xdr_results, caddr_t results_ptr,
  202. struct timeval timeout)
  203. {
  204. struct ct_data *ct = (struct ct_data *) h->cl_private;
  205. XDR *xdrs = &(ct->ct_xdrs);
  206. struct rpc_msg reply_msg;
  207. u_long x_id;
  208. u_int32_t *msg_x_id = (u_int32_t *) (ct->ct_mcall); /* yuk */
  209. bool_t shipnow;
  210. int refreshes = 2;
  211. if (!ct->ct_waitset)
  212. {
  213. ct->ct_wait = timeout;
  214. }
  215. shipnow =
  216. (xdr_results == (xdrproc_t) 0 && ct->ct_wait.tv_sec == 0
  217. && ct->ct_wait.tv_usec == 0) ? FALSE : TRUE;
  218. call_again:
  219. xdrs->x_op = XDR_ENCODE;
  220. ct->ct_error.re_status = RPC_SUCCESS;
  221. x_id = ntohl (--(*msg_x_id));
  222. if ((!XDR_PUTBYTES (xdrs, ct->ct_mcall, ct->ct_mpos)) ||
  223. (!XDR_PUTLONG (xdrs, (long *) &proc)) ||
  224. (!AUTH_MARSHALL (h->cl_auth, xdrs)) ||
  225. (!(*xdr_args) (xdrs, args_ptr)))
  226. {
  227. if (ct->ct_error.re_status == RPC_SUCCESS)
  228. ct->ct_error.re_status = RPC_CANTENCODEARGS;
  229. (void) xdrrec_endofrecord (xdrs, TRUE);
  230. return ct->ct_error.re_status;
  231. }
  232. if (!xdrrec_endofrecord (xdrs, shipnow))
  233. return ct->ct_error.re_status = RPC_CANTSEND;
  234. if (!shipnow)
  235. return RPC_SUCCESS;
  236. /*
  237. * Hack to provide rpc-based message passing
  238. */
  239. if (ct->ct_wait.tv_sec == 0 && ct->ct_wait.tv_usec == 0)
  240. return ct->ct_error.re_status = RPC_TIMEDOUT;
  241. /*
  242. * Keep receiving until we get a valid transaction id
  243. */
  244. xdrs->x_op = XDR_DECODE;
  245. while (TRUE)
  246. {
  247. reply_msg.acpted_rply.ar_verf = _null_auth;
  248. reply_msg.acpted_rply.ar_results.where = NULL;
  249. reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
  250. if (!xdrrec_skiprecord (xdrs))
  251. return ct->ct_error.re_status;
  252. /* now decode and validate the response header */
  253. if (!xdr_replymsg (xdrs, &reply_msg))
  254. {
  255. if (ct->ct_error.re_status == RPC_SUCCESS)
  256. continue;
  257. return ct->ct_error.re_status;
  258. }
  259. if (reply_msg.rm_xid == x_id)
  260. break;
  261. }
  262. /*
  263. * process header
  264. */
  265. _seterr_reply (&reply_msg, &(ct->ct_error));
  266. if (ct->ct_error.re_status == RPC_SUCCESS)
  267. {
  268. if (!AUTH_VALIDATE (h->cl_auth, &reply_msg.acpted_rply.ar_verf))
  269. {
  270. ct->ct_error.re_status = RPC_AUTHERROR;
  271. ct->ct_error.re_why = AUTH_INVALIDRESP;
  272. }
  273. else if (!(*xdr_results) (xdrs, results_ptr))
  274. {
  275. if (ct->ct_error.re_status == RPC_SUCCESS)
  276. ct->ct_error.re_status = RPC_CANTDECODERES;
  277. }
  278. /* free verifier ... */
  279. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  280. {
  281. xdrs->x_op = XDR_FREE;
  282. (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
  283. }
  284. } /* end successful completion */
  285. else
  286. {
  287. /* maybe our credentials need to be refreshed ... */
  288. if (refreshes-- && AUTH_REFRESH (h->cl_auth))
  289. goto call_again;
  290. } /* end of unsuccessful completion */
  291. return ct->ct_error.re_status;
  292. }
  293. static void
  294. clntunix_geterr (CLIENT *h, struct rpc_err *errp)
  295. {
  296. struct ct_data *ct = (struct ct_data *) h->cl_private;
  297. *errp = ct->ct_error;
  298. }
  299. static bool_t
  300. clntunix_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
  301. {
  302. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  303. XDR *xdrs = &(ct->ct_xdrs);
  304. xdrs->x_op = XDR_FREE;
  305. return (*xdr_res) (xdrs, res_ptr);
  306. }
  307. static void
  308. clntunix_abort (void)
  309. {
  310. }
  311. static bool_t
  312. clntunix_control (CLIENT *cl, int request, char *info)
  313. {
  314. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  315. switch (request)
  316. {
  317. case CLSET_FD_CLOSE:
  318. ct->ct_closeit = TRUE;
  319. break;
  320. case CLSET_FD_NCLOSE:
  321. ct->ct_closeit = FALSE;
  322. break;
  323. case CLSET_TIMEOUT:
  324. ct->ct_wait = *(struct timeval *) info;
  325. break;
  326. case CLGET_TIMEOUT:
  327. *(struct timeval *) info = ct->ct_wait;
  328. break;
  329. case CLGET_SERVER_ADDR:
  330. *(struct sockaddr_un *) info = ct->ct_addr;
  331. break;
  332. case CLGET_FD:
  333. *(int *)info = ct->ct_sock;
  334. break;
  335. case CLGET_XID:
  336. /*
  337. * use the knowledge that xid is the
  338. * first element in the call structure *.
  339. * This will get the xid of the PREVIOUS call
  340. */
  341. *(u_long *) info = ntohl (*(u_long *)ct->ct_mcall);
  342. break;
  343. case CLSET_XID:
  344. /* This will set the xid of the NEXT call */
  345. *(u_long *) ct->ct_mcall = htonl (*(u_long *)info - 1);
  346. /* decrement by 1 as clntunix_call() increments once */
  347. case CLGET_VERS:
  348. /*
  349. * This RELIES on the information that, in the call body,
  350. * the version number field is the fifth field from the
  351. * begining of the RPC header. MUST be changed if the
  352. * call_struct is changed
  353. */
  354. *(u_long *) info = ntohl (*(u_long *) (ct->ct_mcall
  355. + 4 * BYTES_PER_XDR_UNIT));
  356. break;
  357. case CLSET_VERS:
  358. *(u_long *) (ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
  359. = htonl (*(u_long *) info);
  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. * begining of the RPC header. MUST be changed if the
  366. * call_struct is changed
  367. */
  368. *(u_long *) info = ntohl (*(u_long *) (ct->ct_mcall
  369. + 3 * BYTES_PER_XDR_UNIT));
  370. break;
  371. case CLSET_PROG:
  372. *(u_long *) (ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
  373. = htonl(*(u_long *) info);
  374. break;
  375. /* The following are only possible with TI-RPC */
  376. case CLGET_RETRY_TIMEOUT:
  377. case CLSET_RETRY_TIMEOUT:
  378. case CLGET_SVC_ADDR:
  379. case CLSET_SVC_ADDR:
  380. case CLSET_PUSH_TIMOD:
  381. case CLSET_POP_TIMOD:
  382. default:
  383. return FALSE;
  384. }
  385. return TRUE;
  386. }
  387. static void
  388. clntunix_destroy (CLIENT *h)
  389. {
  390. struct ct_data *ct =
  391. (struct ct_data *) h->cl_private;
  392. if (ct->ct_closeit)
  393. {
  394. (void) close (ct->ct_sock);
  395. }
  396. XDR_DESTROY (&(ct->ct_xdrs));
  397. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  398. mem_free ((caddr_t) h, sizeof (CLIENT));
  399. }
  400. static int
  401. __msgread (int sock, void *data, size_t cnt)
  402. {
  403. struct iovec iov;
  404. struct msghdr msg;
  405. #ifdef SCM_CREDENTIALS
  406. /*static -why??*/ char cm[CMSG_SPACE(sizeof (struct ucred))];
  407. #endif
  408. int len;
  409. iov.iov_base = data;
  410. iov.iov_len = cnt;
  411. msg.msg_iov = &iov;
  412. msg.msg_iovlen = 1;
  413. msg.msg_name = NULL;
  414. msg.msg_namelen = 0;
  415. #ifdef SCM_CREDENTIALS
  416. msg.msg_control = (caddr_t) &cm;
  417. msg.msg_controllen = CMSG_SPACE(sizeof (struct ucred));
  418. #endif
  419. msg.msg_flags = 0;
  420. #ifdef SO_PASSCRED
  421. {
  422. int on = 1;
  423. if (setsockopt (sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)))
  424. return -1;
  425. }
  426. #endif
  427. restart:
  428. len = recvmsg (sock, &msg, 0);
  429. if (len >= 0)
  430. {
  431. if (msg.msg_flags & MSG_CTRUNC || len == 0)
  432. return 0;
  433. else
  434. return len;
  435. }
  436. if (errno == EINTR)
  437. goto restart;
  438. return -1;
  439. }
  440. static int
  441. __msgwrite (int sock, void *data, size_t cnt)
  442. {
  443. #ifndef SCM_CREDENTIALS
  444. /* We cannot implement this reliably. */
  445. __set_errno (ENOSYS);
  446. return -1;
  447. #else
  448. struct iovec iov;
  449. struct msghdr msg;
  450. struct cmsghdr *cmsg = alloca (CMSG_SPACE(sizeof (struct ucred)));
  451. struct ucred cred;
  452. int len;
  453. /* XXX I'm not sure, if gete?id() is always correct, or if we should use
  454. get?id(). But since keyserv needs geteuid(), we have no other chance.
  455. It would be much better, if the kernel could pass both to the server. */
  456. cred.pid = getpid ();
  457. cred.uid = geteuid ();
  458. cred.gid = getegid ();
  459. memcpy (CMSG_DATA(cmsg), &cred, sizeof (struct ucred));
  460. cmsg->cmsg_level = SOL_SOCKET;
  461. cmsg->cmsg_type = SCM_CREDENTIALS;
  462. cmsg->cmsg_len = sizeof(*cmsg) + sizeof(struct ucred);
  463. iov.iov_base = data;
  464. iov.iov_len = cnt;
  465. msg.msg_iov = &iov;
  466. msg.msg_iovlen = 1;
  467. msg.msg_name = NULL;
  468. msg.msg_namelen = 0;
  469. msg.msg_control = cmsg;
  470. msg.msg_controllen = CMSG_ALIGN(cmsg->cmsg_len);
  471. msg.msg_flags = 0;
  472. restart:
  473. len = sendmsg (sock, &msg, 0);
  474. if (len >= 0)
  475. return len;
  476. if (errno == EINTR)
  477. goto restart;
  478. return -1;
  479. #endif
  480. }
  481. /*
  482. * Interface between xdr serializer and unix connection.
  483. * Behaves like the system calls, read & write, but keeps some error state
  484. * around for the rpc level.
  485. */
  486. static int
  487. readunix (char *ctptr, char *buf, int len)
  488. {
  489. struct ct_data *ct = (struct ct_data *) ctptr;
  490. struct pollfd fd;
  491. int milliseconds = ((ct->ct_wait.tv_sec * 1000)
  492. + (ct->ct_wait.tv_usec / 1000));
  493. if (len == 0)
  494. return 0;
  495. fd.fd = ct->ct_sock;
  496. fd.events = POLLIN;
  497. while (TRUE)
  498. {
  499. switch (poll (&fd, 1, milliseconds))
  500. {
  501. case 0:
  502. ct->ct_error.re_status = RPC_TIMEDOUT;
  503. return -1;
  504. case -1:
  505. if (errno == EINTR)
  506. continue;
  507. ct->ct_error.re_status = RPC_CANTRECV;
  508. ct->ct_error.re_errno = errno;
  509. return -1;
  510. }
  511. break;
  512. }
  513. switch (len = __msgread (ct->ct_sock, buf, len))
  514. {
  515. case 0:
  516. /* premature eof */
  517. ct->ct_error.re_errno = ECONNRESET;
  518. ct->ct_error.re_status = RPC_CANTRECV;
  519. len = -1; /* it's really an error */
  520. break;
  521. case -1:
  522. ct->ct_error.re_errno = errno;
  523. ct->ct_error.re_status = RPC_CANTRECV;
  524. break;
  525. }
  526. return len;
  527. }
  528. static int
  529. writeunix (char *ctptr, char *buf, int len)
  530. {
  531. int i, cnt;
  532. struct ct_data *ct = (struct ct_data *) ctptr;
  533. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  534. {
  535. if ((i = __msgwrite (ct->ct_sock, buf, cnt)) == -1)
  536. {
  537. ct->ct_error.re_errno = errno;
  538. ct->ct_error.re_status = RPC_CANTSEND;
  539. return -1;
  540. }
  541. }
  542. return len;
  543. }