clnt_unix.c 16 KB

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