2 * Generic RPC client socket-level APIs for nfs-utils
4 * Copyright (C) 2008 Oracle Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 0211-1301 USA
27 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
38 #include <arpa/inet.h>
41 #include <rpc/pmap_prot.h>
47 #include <netconfig.h>
48 #include <rpc/rpcb_prot.h>
49 #endif /* HAVE_LIBTIRPC */
52 * If "-1" is specified in the tv_sec field, use these defaults instead.
54 #define NFSRPC_TIMEOUT_UDP (3)
55 #define NFSRPC_TIMEOUT_TCP (10)
59 * Set up an RPC client for communicating via a AF_LOCAL socket.
61 * @timeout is initialized upon return
63 * Returns a pointer to a prepared RPC client if successful; caller
64 * must destroy a non-NULL returned RPC client. Otherwise NULL, and
65 * rpc_createerr.cf_stat is set to reflect the error.
67 static CLIENT *nfs_get_localclient(const struct sockaddr *sap,
68 const socklen_t salen,
69 const rpcprog_t program,
70 const rpcvers_t version,
71 struct timeval *timeout)
74 struct sockaddr_storage address;
75 const struct netbuf nbuf = {
76 .maxlen = sizeof(struct sockaddr_un),
80 #endif /* HAVE_LIBTIRPC */
84 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
86 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
87 rpc_createerr.cf_error.re_errno = errno;
91 if (timeout->tv_sec == -1)
92 timeout->tv_sec = NFSRPC_TIMEOUT_TCP;
95 memcpy(nbuf.buf, sap, (size_t)salen);
96 client = clnt_vc_create(sock, &nbuf, program, version, 0, 0);
97 #else /* !HAVE_LIBTIRPC */
98 client = clntunix_create((struct sockaddr_un *)sap,
99 program, version, &sock, 0, 0);
100 #endif /* !HAVE_LIBTIRPC */
102 CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
110 * Bind a socket using an unused ephemeral source port.
112 * Returns zero on success, or returns -1 on error. errno is
113 * set to reflect the nature of the error.
115 static int nfs_bind(const int sock, const sa_family_t family)
117 struct sockaddr_in sin = {
118 .sin_family = AF_INET,
119 .sin_addr.s_addr = htonl(INADDR_ANY),
121 struct sockaddr_in6 sin6 = {
122 .sin6_family = AF_INET6,
123 .sin6_addr = IN6ADDR_ANY_INIT,
128 return bind(sock, (struct sockaddr *)(char *)&sin,
129 (socklen_t)sizeof(sin));
131 return bind(sock, (struct sockaddr *)(char *)&sin6,
132 (socklen_t)sizeof(sin6));
135 errno = EAFNOSUPPORT;
142 * Bind a socket using an unused privileged source port.
144 * Returns zero on success, or returns -1 on error. errno is
145 * set to reflect the nature of the error.
147 static int nfs_bindresvport(const int sock, const sa_family_t family)
149 struct sockaddr_in sin = {
150 .sin_family = AF_INET,
151 .sin_addr.s_addr = htonl(INADDR_ANY),
153 struct sockaddr_in6 sin6 = {
154 .sin6_family = AF_INET6,
155 .sin6_addr = IN6ADDR_ANY_INIT,
160 return bindresvport_sa(sock, (struct sockaddr *)(char *)&sin);
162 return bindresvport_sa(sock, (struct sockaddr *)(char *)&sin6);
165 errno = EAFNOSUPPORT;
169 #else /* !HAVE_LIBTIRPC */
172 * Bind a socket using an unused privileged source port.
174 * Returns zero on success, or returns -1 on error. errno is
175 * set to reflect the nature of the error.
177 static int nfs_bindresvport(const int sock, const sa_family_t family)
179 if (family != AF_INET) {
180 errno = EAFNOSUPPORT;
184 return bindresvport(sock, NULL);
187 #endif /* !HAVE_LIBTIRPC */
190 * Perform a non-blocking connect on the socket fd.
192 * @timeout is modified to contain the time remaining (i.e. time provided
193 * minus time elasped).
195 * Returns zero on success, or returns -1 on error. errno is
196 * set to reflect the nature of the error.
198 static int nfs_connect_nb(const int fd, const struct sockaddr *sap,
199 const socklen_t salen, struct timeval *timeout)
204 flags = fcntl(fd, F_GETFL, 0);
208 ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
213 * From here on subsequent sys calls could change errno so
214 * we set ret = -errno to capture it in case we decide to
217 ret = connect(fd, sap, salen);
218 if (ret < 0 && errno != EINPROGRESS) {
230 ret = select(fd + 1, NULL, &rset, NULL, timeout);
238 if (FD_ISSET(fd, &rset)) {
240 socklen_t len = (socklen_t)sizeof(ret);
242 status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
248 /* Oops - something wrong with connect */
256 (void)fcntl(fd, F_SETFL, flags);
261 * Set up an RPC client for communicating via a datagram socket.
262 * A connected UDP socket is used to detect a missing remote
263 * listener as quickly as possible.
265 * @timeout is initialized upon return
267 * Returns a pointer to a prepared RPC client if successful; caller
268 * must destroy a non-NULL returned RPC client. Otherwise NULL, and
269 * rpc_createerr.cf_stat is set to reflect the error.
271 static CLIENT *nfs_get_udpclient(const struct sockaddr *sap,
272 const socklen_t salen,
273 const rpcprog_t program,
274 const rpcvers_t version,
275 struct timeval *timeout,
281 struct sockaddr_storage address;
282 const struct netbuf nbuf = {
288 #else /* !HAVE_LIBTIRPC */
290 if (sap->sa_family != AF_INET) {
291 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
294 #endif /* !HAVE_LIBTIRPC */
296 sock = socket((int)sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
298 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
299 rpc_createerr.cf_error.re_errno = errno;
304 ret = nfs_bindresvport(sock, sap->sa_family);
306 ret = nfs_bind(sock, sap->sa_family);
308 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
309 rpc_createerr.cf_error.re_errno = errno;
314 if (timeout->tv_sec == -1)
315 timeout->tv_sec = NFSRPC_TIMEOUT_UDP;
317 ret = nfs_connect_nb(sock, sap, salen, timeout);
319 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
320 rpc_createerr.cf_error.re_errno = errno;
326 memcpy(nbuf.buf, sap, (size_t)salen);
327 client = clnt_dg_create(sock, &nbuf, program, version, 0, 0);
328 #else /* !HAVE_LIBTIRPC */
329 client = clntudp_create((struct sockaddr_in *)sap, program,
330 version, *timeout, &sock);
331 #endif /* !HAVE_LIBTIRPC */
332 if (client != NULL) {
333 struct timeval retry_timeout = { 1, 0 };
334 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
335 (char *)&retry_timeout);
336 CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
344 * Set up and connect an RPC client for communicating via a stream socket.
346 * @timeout is initialized upon return
348 * Returns a pointer to a prepared and connected RPC client if
349 * successful; caller must destroy a non-NULL returned RPC client.
350 * Otherwise NULL, and rpc_createerr.cf_stat is set to reflect the
353 static CLIENT *nfs_get_tcpclient(const struct sockaddr *sap,
354 const socklen_t salen,
355 const rpcprog_t program,
356 const rpcvers_t version,
357 struct timeval *timeout,
363 struct sockaddr_storage address;
364 const struct netbuf nbuf = {
370 #else /* !HAVE_LIBTIRPC */
372 if (sap->sa_family != AF_INET) {
373 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
376 #endif /* !HAVE_LIBTIRPC */
378 sock = socket((int)sap->sa_family, SOCK_STREAM, IPPROTO_TCP);
380 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
381 rpc_createerr.cf_error.re_errno = errno;
386 ret = nfs_bindresvport(sock, sap->sa_family);
388 ret = nfs_bind(sock, sap->sa_family);
390 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
391 rpc_createerr.cf_error.re_errno = errno;
396 if (timeout->tv_sec == -1)
397 timeout->tv_sec = NFSRPC_TIMEOUT_TCP;
399 ret = nfs_connect_nb(sock, sap, salen, timeout);
401 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
402 rpc_createerr.cf_error.re_errno = errno;
408 memcpy(nbuf.buf, sap, (size_t)salen);
409 client = clnt_vc_create(sock, &nbuf, program, version, 0, 0);
410 #else /* !HAVE_LIBTIRPC */
411 client = clnttcp_create((struct sockaddr_in *)sap,
412 program, version, &sock, 0, 0);
413 #endif /* !HAVE_LIBTIRPC */
415 CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
423 * nfs_get_rpcclient - acquire an RPC client
424 * @sap: pointer to socket address of RPC server
425 * @salen: length of socket address
426 * @transport: IPPROTO_ value of transport protocol to use
427 * @program: RPC program number
428 * @version: RPC version number
429 * @timeout: pointer to request timeout (must not be NULL)
431 * Set up an RPC client for communicating with an RPC program @program
432 * and @version on the server @sap over @transport. An unprivileged
433 * source port is used.
435 * Returns a pointer to a prepared RPC client if successful, and
436 * @timeout is initialized; caller must destroy a non-NULL returned RPC
437 * client. Otherwise returns NULL, and rpc_createerr.cf_stat is set to
440 CLIENT *nfs_get_rpcclient(const struct sockaddr *sap,
441 const socklen_t salen,
442 const unsigned short transport,
443 const rpcprog_t program,
444 const rpcvers_t version,
445 struct timeval *timeout)
447 nfs_clear_rpc_createerr();
449 switch (sap->sa_family) {
451 return nfs_get_localclient(sap, salen, program,
455 if (nfs_get_port(sap) == 0) {
456 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
461 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
467 return nfs_get_tcpclient(sap, salen, program, version,
471 return nfs_get_udpclient(sap, salen, program, version,
475 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
480 * nfs_get_priv_rpcclient - acquire an RPC client
481 * @sap: pointer to socket address of RPC server
482 * @salen: length of socket address
483 * @transport: IPPROTO_ value of transport protocol to use
484 * @program: RPC program number
485 * @version: RPC version number
486 * @timeout: pointer to request timeout (must not be NULL)
488 * Set up an RPC client for communicating with an RPC program @program
489 * and @version on the server @sap over @transport. A privileged
490 * source port is used.
492 * Returns a pointer to a prepared RPC client if successful, and
493 * @timeout is initialized; caller must destroy a non-NULL returned RPC
494 * client. Otherwise returns NULL, and rpc_createerr.cf_stat is set to
497 CLIENT *nfs_get_priv_rpcclient(const struct sockaddr *sap,
498 const socklen_t salen,
499 const unsigned short transport,
500 const rpcprog_t program,
501 const rpcvers_t version,
502 struct timeval *timeout)
504 nfs_clear_rpc_createerr();
506 switch (sap->sa_family) {
508 return nfs_get_localclient(sap, salen, program,
512 if (nfs_get_port(sap) == 0) {
513 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
518 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
524 return nfs_get_tcpclient(sap, salen, program, version,
528 return nfs_get_udpclient(sap, salen, program, version,
532 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
537 * nfs_getrpcbyname - convert an RPC program name to a rpcprog_t
538 * @program: default program number to use if names not found in db
539 * @table: pointer to table of 'char *' names to try to find
541 * Returns program number of first name to be successfully looked
542 * up, or the default program number if all lookups fail.
544 rpcprog_t nfs_getrpcbyname(const rpcprog_t program, const char *table[])
546 #ifdef HAVE_GETRPCBYNAME
547 struct rpcent *entry;
551 for (i = 0; table[i] != NULL; i++) {
552 entry = getrpcbyname(table[i]);
554 return (rpcprog_t)entry->r_number;
556 #endif /* HAVE_GETRPCBYNAME */
562 * AUTH_SYS doesn't allow more than 16 gids in the supplemental group list.
563 * If there are more than that, trying to determine which ones to include
564 * in the list is problematic. This function creates an auth handle that
565 * only has the primary gid in the supplemental gids list. It's intended to
566 * be used for protocols where credentials really don't matter much (the MNT
567 * protocol, for instance).
570 nfs_authsys_create(void)
572 char machname[MAXHOSTNAMELEN + 1];
573 uid_t uid = geteuid();
574 gid_t gid = getegid();
576 if (gethostname(machname, sizeof(machname)) == -1)
579 return authunix_create(machname, uid, gid, 1, &gid);