clnt_unix.c 15 KB

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