clnt_unix.c 16 KB

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