clnt_unix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. break;
  348. case CLGET_VERS:
  349. /*
  350. * This RELIES on the information that, in the call body,
  351. * the version number field is the fifth field from the
  352. * begining of the RPC header. MUST be changed if the
  353. * call_struct is changed
  354. */
  355. *(u_long *) info = ntohl (*(u_long *) (ct->ct_mcall
  356. + 4 * BYTES_PER_XDR_UNIT));
  357. break;
  358. case CLSET_VERS:
  359. *(u_long *) (ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
  360. = htonl (*(u_long *) info);
  361. break;
  362. case CLGET_PROG:
  363. /*
  364. * This RELIES on the information that, in the call body,
  365. * the program number field is the field from the
  366. * begining of the RPC header. MUST be changed if the
  367. * call_struct is changed
  368. */
  369. *(u_long *) info = ntohl (*(u_long *) (ct->ct_mcall
  370. + 3 * BYTES_PER_XDR_UNIT));
  371. break;
  372. case CLSET_PROG:
  373. *(u_long *) (ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
  374. = htonl(*(u_long *) info);
  375. break;
  376. /* The following are only possible with TI-RPC */
  377. case CLGET_RETRY_TIMEOUT:
  378. case CLSET_RETRY_TIMEOUT:
  379. case CLGET_SVC_ADDR:
  380. case CLSET_SVC_ADDR:
  381. case CLSET_PUSH_TIMOD:
  382. case CLSET_POP_TIMOD:
  383. default:
  384. return FALSE;
  385. }
  386. return TRUE;
  387. }
  388. static void
  389. clntunix_destroy (CLIENT *h)
  390. {
  391. struct ct_data *ct =
  392. (struct ct_data *) h->cl_private;
  393. if (ct->ct_closeit)
  394. {
  395. (void) close (ct->ct_sock);
  396. }
  397. XDR_DESTROY (&(ct->ct_xdrs));
  398. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  399. mem_free ((caddr_t) h, sizeof (CLIENT));
  400. }
  401. static int
  402. __msgread (int sock, void *data, size_t cnt)
  403. {
  404. struct iovec iov;
  405. struct msghdr msg;
  406. #ifdef SCM_CREDENTIALS
  407. /*static -why??*/ char cm[CMSG_SPACE(sizeof (struct ucred))];
  408. #endif
  409. int len;
  410. iov.iov_base = data;
  411. iov.iov_len = cnt;
  412. msg.msg_iov = &iov;
  413. msg.msg_iovlen = 1;
  414. msg.msg_name = NULL;
  415. msg.msg_namelen = 0;
  416. #ifdef SCM_CREDENTIALS
  417. msg.msg_control = (caddr_t) &cm;
  418. msg.msg_controllen = CMSG_SPACE(sizeof (struct ucred));
  419. #endif
  420. msg.msg_flags = 0;
  421. #ifdef SO_PASSCRED
  422. {
  423. int on = 1;
  424. if (setsockopt (sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)))
  425. return -1;
  426. }
  427. #endif
  428. restart:
  429. len = recvmsg (sock, &msg, 0);
  430. if (len >= 0)
  431. {
  432. if (msg.msg_flags & MSG_CTRUNC || len == 0)
  433. return 0;
  434. else
  435. return len;
  436. }
  437. if (errno == EINTR)
  438. goto restart;
  439. return -1;
  440. }
  441. static int
  442. __msgwrite (int sock, void *data, size_t cnt)
  443. {
  444. #ifndef SCM_CREDENTIALS
  445. /* We cannot implement this reliably. */
  446. __set_errno (ENOSYS);
  447. return -1;
  448. #else
  449. struct iovec iov;
  450. struct msghdr msg;
  451. struct cmsghdr *cmsg = alloca (CMSG_SPACE(sizeof (struct ucred)));
  452. struct ucred cred;
  453. int len;
  454. /* XXX I'm not sure, if gete?id() is always correct, or if we should use
  455. get?id(). But since keyserv needs geteuid(), we have no other chance.
  456. It would be much better, if the kernel could pass both to the server. */
  457. cred.pid = getpid ();
  458. cred.uid = geteuid ();
  459. cred.gid = getegid ();
  460. memcpy (CMSG_DATA(cmsg), &cred, sizeof (struct ucred));
  461. cmsg->cmsg_level = SOL_SOCKET;
  462. cmsg->cmsg_type = SCM_CREDENTIALS;
  463. cmsg->cmsg_len = sizeof(*cmsg) + sizeof(struct ucred);
  464. iov.iov_base = data;
  465. iov.iov_len = cnt;
  466. msg.msg_iov = &iov;
  467. msg.msg_iovlen = 1;
  468. msg.msg_name = NULL;
  469. msg.msg_namelen = 0;
  470. msg.msg_control = cmsg;
  471. msg.msg_controllen = CMSG_ALIGN(cmsg->cmsg_len);
  472. msg.msg_flags = 0;
  473. restart:
  474. len = sendmsg (sock, &msg, 0);
  475. if (len >= 0)
  476. return len;
  477. if (errno == EINTR)
  478. goto restart;
  479. return -1;
  480. #endif
  481. }
  482. /*
  483. * Interface between xdr serializer and unix connection.
  484. * Behaves like the system calls, read & write, but keeps some error state
  485. * around for the rpc level.
  486. */
  487. static int
  488. readunix (char *ctptr, char *buf, int len)
  489. {
  490. struct ct_data *ct = (struct ct_data *) ctptr;
  491. struct pollfd fd;
  492. int milliseconds = ((ct->ct_wait.tv_sec * 1000)
  493. + (ct->ct_wait.tv_usec / 1000));
  494. if (len == 0)
  495. return 0;
  496. fd.fd = ct->ct_sock;
  497. fd.events = POLLIN;
  498. while (TRUE)
  499. {
  500. switch (poll (&fd, 1, milliseconds))
  501. {
  502. case 0:
  503. ct->ct_error.re_status = RPC_TIMEDOUT;
  504. return -1;
  505. case -1:
  506. if (errno == EINTR)
  507. continue;
  508. ct->ct_error.re_status = RPC_CANTRECV;
  509. ct->ct_error.re_errno = errno;
  510. return -1;
  511. }
  512. break;
  513. }
  514. switch (len = __msgread (ct->ct_sock, buf, len))
  515. {
  516. case 0:
  517. /* premature eof */
  518. ct->ct_error.re_errno = ECONNRESET;
  519. ct->ct_error.re_status = RPC_CANTRECV;
  520. len = -1; /* it's really an error */
  521. break;
  522. case -1:
  523. ct->ct_error.re_errno = errno;
  524. ct->ct_error.re_status = RPC_CANTRECV;
  525. break;
  526. }
  527. return len;
  528. }
  529. static int
  530. writeunix (char *ctptr, char *buf, int len)
  531. {
  532. int i, cnt;
  533. struct ct_data *ct = (struct ct_data *) ctptr;
  534. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  535. {
  536. if ((i = __msgwrite (ct->ct_sock, buf, cnt)) == -1)
  537. {
  538. ct->ct_error.re_errno = errno;
  539. ct->ct_error.re_status = RPC_CANTSEND;
  540. return -1;
  541. }
  542. }
  543. return len;
  544. }