]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/network.c
00237699e185c0e152f203c65595381a7e39cd41
[nfs-utils.git] / utils / mount / network.c
1 /*
2  * network.c -- Provide common network functions for NFS mount/umount
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 021110-1307, USA.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <time.h>
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <rpc/rpc.h>
41 #include <rpc/pmap_prot.h>
42 #include <rpc/pmap_clnt.h>
43
44 #include "xcommon.h"
45 #include "mount.h"
46 #include "nls.h"
47 #include "nfs_mount.h"
48 #include "mount_constants.h"
49 #include "network.h"
50
51 /*
52  * Earlier versions of glibc's /usr/include/netdb.h exclude these
53  * definitions because it was thought they were not part of a stable
54  * POSIX standard.  However, they are defined by RFC 2553 and 3493
55  * and in POSIX 1003.1-2001, so these definitions were added in later
56  * versions of netdb.h.
57  */
58 #ifndef AI_V4MAPPED
59 #define AI_V4MAPPED     0x0008  /* IPv4-mapped addresses are acceptable.  */
60 #endif  /* AI_V4MAPPED */
61 #ifndef AI_ALL
62 #define AI_ALL          0x0010  /* Return both IPv4 and IPv6 addresses.  */
63 #endif  /* AI_ALL */
64 #ifndef AI_ADDRCONFIG
65 #define AI_ADDRCONFIG   0x0020  /* Use configuration of this host to choose \
66                                    returned address type.  */
67 #endif  /* AI_ADDRCONFIG */
68
69 #define PMAP_TIMEOUT    (10)
70 #define CONNECT_TIMEOUT (20)
71 #define MOUNT_TIMEOUT   (30)
72
73 #if SIZEOF_SOCKLEN_T - 0 == 0
74 #define socklen_t unsigned int
75 #endif
76
77 extern int nfs_mount_data_version;
78 extern char *progname;
79 extern int verbose;
80
81 static const unsigned long nfs_to_mnt[] = {
82         0,
83         0,
84         1,
85         3,
86 };
87
88 static const unsigned long mnt_to_nfs[] = {
89         0,
90         2,
91         2,
92         3,
93 };
94
95 /*
96  * Map an NFS version into the corresponding Mountd version
97  */
98 unsigned long nfsvers_to_mnt(const unsigned long vers)
99 {
100         if (vers <= 3)
101                 return nfs_to_mnt[vers];
102         return 0;
103 }
104
105 /*
106  * Map a Mountd version into the corresponding NFS version
107  */
108 static unsigned long mntvers_to_nfs(const unsigned long vers)
109 {
110         if (vers <= 3)
111                 return mnt_to_nfs[vers];
112         return 0;
113 }
114
115 static const unsigned int probe_udp_only[] = {
116         IPPROTO_UDP,
117         0,
118 };
119
120 static const unsigned int probe_udp_first[] = {
121         IPPROTO_UDP,
122         IPPROTO_TCP,
123         0,
124 };
125
126 static const unsigned int probe_tcp_first[] = {
127         IPPROTO_TCP,
128         IPPROTO_UDP,
129         0,
130 };
131
132 static const unsigned long probe_nfs2_only[] = {
133         2,
134         0,
135 };
136
137 static const unsigned long probe_nfs3_first[] = {
138         3,
139         2,
140         0,
141 };
142
143 static const unsigned long probe_mnt1_first[] = {
144         1,
145         2,
146         0,
147 };
148
149 static const unsigned long probe_mnt3_first[] = {
150         3,
151         1,
152         2,
153         0,
154 };
155
156 /**
157  * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
158  * @hostname: pointer to C string containing DNS hostname to resolve
159  * @sap: pointer to buffer to fill with socket address
160  * @len: IN: size of buffer to fill; OUT: size of socket address
161  *
162  * Returns 1 and places a socket address at @sap if successful;
163  * otherwise zero.
164  */
165 int nfs_name_to_address(const char *hostname,
166                         const sa_family_t af_hint,
167                         struct sockaddr *sap, socklen_t *salen)
168 {
169         struct addrinfo *gai_results;
170         struct addrinfo gai_hint = {
171                 .ai_family      = af_hint,
172                 .ai_flags       = AI_ADDRCONFIG,
173         };
174         socklen_t len = *salen;
175         int error, ret = 0;
176
177         if (af_hint == AF_INET6)
178                 gai_hint.ai_flags |= AI_V4MAPPED|AI_ALL;
179
180         *salen = 0;
181
182         error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
183         if (error) {
184                 nfs_error(_("%s: DNS resolution failed for %s: %s"),
185                         progname, hostname, (error == EAI_SYSTEM ?
186                                 strerror(errno) : gai_strerror(error)));
187                 return ret;
188         }
189
190         switch (gai_results->ai_addr->sa_family) {
191         case AF_INET:
192         case AF_INET6:
193                 if (len >= gai_results->ai_addrlen) {
194                         *salen = gai_results->ai_addrlen;
195                         memcpy(sap, gai_results->ai_addr, *salen);
196                         ret = 1;
197                 }
198                 break;
199         default:
200                 /* things are really broken if we get here, so warn */
201                 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
202                                 progname, hostname);
203                 break;
204         }
205
206         freeaddrinfo(gai_results);
207         return ret;
208 }
209
210 /**
211  * nfs_gethostbyname - resolve a hostname to an IPv4 address
212  * @hostname: pointer to a C string containing a DNS hostname
213  * @saddr: returns an IPv4 address 
214  *
215  * Returns 1 if successful, otherwise zero.
216  */
217 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
218 {
219         socklen_t len = sizeof(*sin);
220
221         return nfs_name_to_address(hostname, AF_INET,
222                                         (struct sockaddr *)sin, &len);
223 }
224
225 /**
226  * nfs_string_to_sockaddr - convert string address to sockaddr
227  * @address:    pointer to presentation format address to convert
228  * @addrlen:    length of presentation address
229  * @sap:        pointer to socket address buffer to fill in
230  * @salen:      IN: length of address buffer
231  *              OUT: length of converted socket address
232  *
233  * Convert a presentation format address string to a socket address.
234  * Similar to nfs_name_to_address(), but the DNS query is squelched,
235  * and won't make any noise if the getaddrinfo() call fails.
236  *
237  * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
238  *
239  * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
240  * on presenting IPv6 addresses as text strings.
241  */
242 int nfs_string_to_sockaddr(const char *address, const size_t addrlen,
243                            struct sockaddr *sap, socklen_t *salen)
244 {
245         struct addrinfo *gai_results;
246         struct addrinfo gai_hint = {
247                 .ai_flags       = AI_NUMERICHOST,
248         };
249         socklen_t len = *salen;
250         int ret = 0;
251
252         *salen = 0;
253
254         if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
255                 switch (gai_results->ai_addr->sa_family) {
256                 case AF_INET:
257                 case AF_INET6:
258                         if (len >= gai_results->ai_addrlen) {
259                                 *salen = gai_results->ai_addrlen;
260                                 memcpy(sap, gai_results->ai_addr, *salen);
261                                 ret = 1;
262                         }
263                         break;
264                 }
265                 freeaddrinfo(gai_results);
266         }
267
268         return ret;
269 }
270
271 /**
272  * nfs_present_sockaddr - convert sockaddr to string
273  * @sap: pointer to socket address to convert
274  * @salen: length of socket address
275  * @buf: pointer to buffer to fill in
276  * @buflen: length of buffer
277  *
278  * Convert the passed-in sockaddr-style address to presentation format.
279  * The presentation format address is placed in @buf and is
280  * '\0'-terminated.
281  *
282  * Returns 1 if successful; otherwise zero.
283  *
284  * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
285  * on presenting IPv6 addresses as text strings.
286  */
287 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
288                          char *buf, const size_t buflen)
289 {
290 #ifdef HAVE_GETNAMEINFO
291         int result;
292
293         result = getnameinfo(sap, salen, buf, buflen,
294                                         NULL, 0, NI_NUMERICHOST);
295         if (!result)
296                 return 1;
297
298         nfs_error(_("%s: invalid server address: %s"), progname,
299                         gai_strerror(result));
300         return 0;
301 #else   /* HAVE_GETNAMEINFO */
302         char *addr;
303
304         if (sap->sa_family == AF_INET) {
305                 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
306                 if (addr && strlen(addr) < buflen) {
307                         strcpy(buf, addr);
308                         return 1;
309                 }
310         }
311
312         nfs_error(_("%s: invalid server address"), progname);
313         return 0;
314 #endif  /* HAVE_GETNAMEINFO */
315 }
316
317 /*
318  * Attempt to connect a socket, but time out after "timeout" seconds.
319  *
320  * On error return, caller closes the socket.
321  */
322 static int connect_to(int fd, struct sockaddr *addr,
323                         socklen_t addrlen, int timeout)
324 {
325         int ret, saved;
326         fd_set rset, wset;
327         struct timeval tv = {
328                 .tv_sec = timeout,
329         };
330
331         saved = fcntl(fd, F_GETFL, 0);
332         fcntl(fd, F_SETFL, saved | O_NONBLOCK);
333
334         ret = connect(fd, addr, addrlen);
335         if (ret < 0 && errno != EINPROGRESS)
336                 return -1;
337         if (ret == 0)
338                 goto out;
339
340         FD_ZERO(&rset);
341         FD_SET(fd, &rset);
342         wset = rset;
343         ret = select(fd + 1, &rset, &wset, NULL, &tv);
344         if (ret == 0) {
345                 errno = ETIMEDOUT;
346                 return -1;
347         }
348         if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
349                 int error;
350                 socklen_t len = sizeof(error);
351                 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
352                         return -1;
353                 if (error) {
354                         errno = error;
355                         return -1;
356                 }
357         } else
358                 return -1;
359
360 out:
361         fcntl(fd, F_SETFL, saved);
362         return 0;
363 }
364
365 /*
366  * Create a socket that is locally bound to a reserved or non-reserved port.
367  *
368  * The caller should check rpc_createerr to determine the cause of any error.
369  */
370 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
371                         unsigned int timeout, int resvp, int conn)
372 {
373         int so, cc, type;
374         struct sockaddr_in laddr;
375         socklen_t namelen = sizeof(laddr);
376
377         type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
378         if ((so = socket (AF_INET, type, p_prot)) < 0)
379                 goto err_socket;
380
381         laddr.sin_family = AF_INET;
382         laddr.sin_port = 0;
383         laddr.sin_addr.s_addr = htonl(INADDR_ANY);
384         if (resvp) {
385                 if (bindresvport(so, &laddr) < 0)
386                         goto err_bindresvport;
387         } else {
388                 cc = bind(so, (struct sockaddr *)&laddr, namelen);
389                 if (cc < 0)
390                         goto err_bind;
391         }
392         if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
393                 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
394                                 timeout);
395                 if (cc < 0)
396                         goto err_connect;
397         }
398         return so;
399
400 err_socket:
401         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
402         rpc_createerr.cf_error.re_errno = errno;
403         if (verbose) {
404                 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
405                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
406                         errno, strerror(errno));
407         }
408         return RPC_ANYSOCK;
409
410 err_bindresvport:
411         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
412         rpc_createerr.cf_error.re_errno = errno;
413         if (verbose) {
414                 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
415                                 " (%s)\n"),
416                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
417                         errno, strerror(errno));
418         }
419         close(so);
420         return RPC_ANYSOCK;
421
422 err_bind:
423         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
424         rpc_createerr.cf_error.re_errno = errno;
425         if (verbose) {
426                 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
427                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
428                         errno, strerror(errno));
429         }
430         close(so);
431         return RPC_ANYSOCK;
432
433 err_connect:
434         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
435         rpc_createerr.cf_error.re_errno = errno;
436         if (verbose) {
437                 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
438                         progname, inet_ntoa(saddr->sin_addr),
439                         ntohs(saddr->sin_port), errno, strerror(errno));
440         }
441         close(so);
442         return RPC_ANYSOCK;
443 }
444
445 /*
446  * getport() is very similar to pmap_getport() with the exception that
447  * this version tries to use an ephemeral port, since reserved ports are
448  * not needed for GETPORT queries.  This conserves the very limited
449  * reserved port space, which helps reduce failed socket binds
450  * during mount storms.
451  *
452  * A side effect of calling this function is that rpccreateerr is set.
453  */
454 static unsigned short getport(struct sockaddr_in *saddr,
455                                 unsigned long program,
456                                 unsigned long version,
457                                 unsigned int proto)
458 {
459         struct sockaddr_in bind_saddr;
460         unsigned short port = 0;
461         int socket;
462         CLIENT *clnt = NULL;
463         enum clnt_stat stat;
464  
465         bind_saddr = *saddr;
466         bind_saddr.sin_port = htons(PMAPPORT);
467
468         socket = get_socket(&bind_saddr, proto, PMAP_TIMEOUT, FALSE, FALSE);
469         if (socket == RPC_ANYSOCK) {
470                 if (proto == IPPROTO_TCP &&
471                     rpc_createerr.cf_error.re_errno == ETIMEDOUT)
472                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
473                 return 0;
474         }
475
476         switch (proto) {
477         case IPPROTO_UDP:
478                 clnt = clntudp_bufcreate(&bind_saddr,
479                                          PMAPPROG, PMAPVERS,
480                                          RETRY_TIMEOUT, &socket,
481                                          RPCSMALLMSGSIZE,
482                                          RPCSMALLMSGSIZE);
483                 break;
484         case IPPROTO_TCP:
485                 clnt = clnttcp_create(&bind_saddr,
486                                       PMAPPROG, PMAPVERS,
487                                       &socket,
488                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
489                 break;
490         }
491         if (clnt != NULL) {
492                 struct pmap parms = {
493                         .pm_prog        = program,
494                         .pm_vers        = version,
495                         .pm_prot        = proto,
496                 };
497
498                 stat = clnt_call(clnt, PMAPPROC_GETPORT,
499                                  (xdrproc_t)xdr_pmap, (caddr_t)&parms,
500                                  (xdrproc_t)xdr_u_short, (caddr_t)&port,
501                                  TIMEOUT);
502                 if (stat) {
503                         clnt_geterr(clnt, &rpc_createerr.cf_error);
504                         rpc_createerr.cf_stat = stat;
505                 }
506                 clnt_destroy(clnt);
507                 if (stat != RPC_SUCCESS)
508                         port = 0;
509                 else if (port == 0)
510                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
511         }
512         close(socket);
513
514         return port;
515 }
516
517 /*
518  * Use the portmapper to discover whether or not the service we want is
519  * available. The lists 'versions' and 'protos' define ordered sequences
520  * of service versions and udp/tcp protocols to probe for.
521  */
522 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
523                         const unsigned int *protos)
524 {
525         struct sockaddr_in *saddr = &server->saddr;
526         struct pmap *pmap = &server->pmap;
527         const unsigned long prog = pmap->pm_prog, *p_vers;
528         const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
529         const u_short port = (u_short) pmap->pm_port;
530         unsigned long vers = pmap->pm_vers;
531         unsigned short p_port;
532
533         p_prot = prot ? &prot : protos;
534         p_vers = vers ? &vers : versions;
535         rpc_createerr.cf_stat = 0;
536         for (;;) {
537                 p_port = getport(saddr, prog, *p_vers, *p_prot);
538                 if (p_port) {
539                         if (!port || port == p_port) {
540                                 saddr->sin_port = htons(p_port);
541                                 if (verbose) {
542                                         printf(_("%s: trying %s prog %ld vers "
543                                                 "%ld prot %s port %d\n"),
544                                                 progname,
545                                                 inet_ntoa(saddr->sin_addr),
546                                                 prog, *p_vers,
547                                                 *p_prot == IPPROTO_UDP ?
548                                                         _("UDP") : _("TCP"),
549                                                 p_port);
550                                 }
551                                 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
552                                         goto out_ok;
553                         }
554                 }
555                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
556                     rpc_createerr.cf_stat != RPC_TIMEDOUT &&
557                     rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
558                         goto out_bad;
559
560                 if (!prot) {
561                         if (*++p_prot)
562                                 continue;
563                         p_prot = protos;
564                 }
565                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
566                         goto out_bad;
567
568                 if (vers || !*++p_vers)
569                         break;
570         }
571
572 out_bad:
573         return 0;
574
575 out_ok:
576         if (!vers)
577                 pmap->pm_vers = *p_vers;
578         if (!prot)
579                 pmap->pm_prot = *p_prot;
580         if (!port)
581                 pmap->pm_port = p_port;
582         rpc_createerr.cf_stat = 0;
583         return 1;
584 }
585
586 static int probe_nfsport(clnt_addr_t *nfs_server)
587 {
588         struct pmap *pmap = &nfs_server->pmap;
589
590         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
591                 return 1;
592
593         if (nfs_mount_data_version >= 4)
594                 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
595         else
596                 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
597 }
598
599 static int probe_mntport(clnt_addr_t *mnt_server)
600 {
601         struct pmap *pmap = &mnt_server->pmap;
602
603         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
604                 return 1;
605
606         if (nfs_mount_data_version >= 4)
607                 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
608         else
609                 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
610 }
611
612 /**
613  * probe_bothports - discover the RPC endpoints of mountd and NFS server
614  * @mnt_server: pointer to address and pmap argument for mountd results
615  * @nfs_server: pointer to address and pmap argument for NFS server
616  *
617  * Returns 1 if successful, otherwise zero if some error occurred.
618  * Note that the arguments are both input and output arguments.
619  *
620  * A side effect of calling this function is that rpccreateerr is set.
621  */
622 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
623 {
624         struct pmap *nfs_pmap = &nfs_server->pmap;
625         struct pmap *mnt_pmap = &mnt_server->pmap;
626         struct pmap save_nfs, save_mnt;
627         int res;
628         const unsigned long *probe_vers;
629
630         if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
631                 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
632         else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
633                 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
634         if (nfs_pmap->pm_vers)
635                 goto version_fixed;
636
637         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
638         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
639         probe_vers = (nfs_mount_data_version >= 4) ?
640                         probe_mnt3_first : probe_mnt1_first;
641
642         for (; *probe_vers; probe_vers++) {
643                 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
644                 if ((res = probe_nfsport(nfs_server) != 0)) {
645                         mnt_pmap->pm_vers = *probe_vers;
646                         if ((res = probe_mntport(mnt_server)) != 0)
647                                 return 1;
648                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
649                 }
650                 switch (rpc_createerr.cf_stat) {
651                 case RPC_PROGVERSMISMATCH:
652                 case RPC_PROGNOTREGISTERED:
653                         break;
654                 default:
655                         goto out_bad;
656                 }
657                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
658         }
659
660 out_bad:
661         return 0;
662
663 version_fixed:
664         if (!probe_nfsport(nfs_server))
665                 goto out_bad;
666         return probe_mntport(mnt_server);
667 }
668
669 static int probe_statd(void)
670 {
671         struct sockaddr_in addr;
672         unsigned short port;
673
674         memset(&addr, 0, sizeof(addr));
675         addr.sin_family = AF_INET;
676         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
677         port = getport(&addr, 100024, 1, IPPROTO_UDP);
678
679         if (port == 0)
680                 return 0;
681         addr.sin_port = htons(port);
682
683         if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
684                 return 0;
685
686         return 1;
687 }
688
689 /**
690  * start_statd - attempt to start rpc.statd
691  *
692  * Returns 1 if statd is running; otherwise zero.
693  */
694 int start_statd(void)
695 {
696 #ifdef START_STATD
697         struct stat stb;
698 #endif
699
700         if (probe_statd())
701                 return 1;
702
703 #ifdef START_STATD
704         if (stat(START_STATD, &stb) == 0) {
705                 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
706                         system(START_STATD);
707                         if (probe_statd())
708                                 return 1;
709                 }
710         }
711 #endif
712
713         return 0;
714 }
715
716 /**
717  * nfs_call_umount - ask the server to remove a share from it's rmtab
718  * @mnt_server: address of RPC MNT program server
719  * @argp: directory path of share to "unmount"
720  *
721  * Returns one if the unmount call succeeded; zero if the unmount
722  * failed for any reason.
723  *
724  * Note that a side effect of calling this function is that rpccreateerr
725  * is set.
726  */
727 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
728 {
729         CLIENT *clnt;
730         enum clnt_stat res = 0;
731         int msock;
732
733         if (!probe_mntport(mnt_server))
734                 return 0;
735         clnt = mnt_openclnt(mnt_server, &msock);
736         if (!clnt)
737                 return 0;
738         res = clnt_call(clnt, MOUNTPROC_UMNT,
739                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
740                         (xdrproc_t)xdr_void, NULL,
741                         TIMEOUT);
742         mnt_closeclnt(clnt, msock);
743
744         if (res == RPC_SUCCESS)
745                 return 1;
746         return 0;
747 }
748
749 /**
750  * mnt_openclnt - get a handle for a remote mountd service
751  * @mnt_server: address and pmap arguments of mountd service
752  * @msock: returns a file descriptor of the underlying transport socket
753  *
754  * Returns an active handle for the remote's mountd service
755  */
756 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
757 {
758         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
759         struct pmap *mnt_pmap = &mnt_server->pmap;
760         CLIENT *clnt = NULL;
761
762         mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
763         *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
764                                 TRUE, FALSE);
765         if (*msock == RPC_ANYSOCK) {
766                 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
767                         /*
768                          * Probably in-use by a TIME_WAIT connection,
769                          * It is worth waiting a while and trying again.
770                          */
771                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
772                 return NULL;
773         }
774
775         switch (mnt_pmap->pm_prot) {
776         case IPPROTO_UDP:
777                 clnt = clntudp_bufcreate(mnt_saddr,
778                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
779                                          RETRY_TIMEOUT, msock,
780                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
781                 break;
782         case IPPROTO_TCP:
783                 clnt = clnttcp_create(mnt_saddr,
784                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
785                                       msock,
786                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
787                 break;
788         }
789         if (clnt) {
790                 /* try to mount hostname:dirname */
791                 clnt->cl_auth = authunix_create_default();
792                 return clnt;
793         }
794         return NULL;
795 }
796
797 /**
798  * mnt_closeclnt - terminate a handle for a remote mountd service
799  * @clnt: pointer to an active handle for a remote mountd service
800  * @msock: file descriptor of the underlying transport socket
801  *
802  */
803 void mnt_closeclnt(CLIENT *clnt, int msock)
804 {
805         auth_destroy(clnt->cl_auth);
806         clnt_destroy(clnt);
807         close(msock);
808 }
809
810 /**
811  * clnt_ping - send an RPC ping to the remote RPC service endpoint
812  * @saddr: server's address
813  * @prog: target RPC program number
814  * @vers: target RPC version number
815  * @prot: target RPC protocol
816  * @caddr: filled in with our network address
817  *
818  * Sigh... getport() doesn't actually check the version number.
819  * In order to make sure that the server actually supports the service
820  * we're requesting, we open and RPC client, and fire off a NULL
821  * RPC call.
822  *
823  * caddr is the network address that the server will use to call us back.
824  * On multi-homed clients, this address depends on which NIC we use to
825  * route requests to the server.
826  *
827  * Returns one if successful, otherwise zero.
828  */
829 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
830                 const unsigned long vers, const unsigned int prot,
831                 struct sockaddr_in *caddr)
832 {
833         CLIENT *clnt = NULL;
834         int sock, stat;
835         static char clnt_res;
836         struct sockaddr dissolve;
837
838         rpc_createerr.cf_stat = stat = 0;
839         sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
840         if (sock == RPC_ANYSOCK) {
841                 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
842                         /*
843                          * TCP timeout. Bubble up the error to see 
844                          * how it should be handled.
845                          */
846                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
847                 }
848                 return 0;
849         }
850
851         if (caddr) {
852                 /* Get the address of our end of this connection */
853                 socklen_t len = sizeof(*caddr);
854                 if (getsockname(sock, caddr, &len) != 0)
855                         caddr->sin_family = 0;
856         }
857
858         switch(prot) {
859         case IPPROTO_UDP:
860                 /* The socket is connected (so we could getsockname successfully),
861                  * but some servers on multi-homed hosts reply from
862                  * the wrong address, so if we stay connected, we lose the reply.
863                  */
864                 dissolve.sa_family = AF_UNSPEC;
865                 connect(sock, &dissolve, sizeof(dissolve));
866
867                 clnt = clntudp_bufcreate(saddr, prog, vers,
868                                          RETRY_TIMEOUT, &sock,
869                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
870                 break;
871         case IPPROTO_TCP:
872                 clnt = clnttcp_create(saddr, prog, vers, &sock,
873                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
874                 break;
875         }
876         if (!clnt) {
877                 close(sock);
878                 return 0;
879         }
880         memset(&clnt_res, 0, sizeof(clnt_res));
881         stat = clnt_call(clnt, NULLPROC,
882                          (xdrproc_t)xdr_void, (caddr_t)NULL,
883                          (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
884                          TIMEOUT);
885         if (stat) {
886                 clnt_geterr(clnt, &rpc_createerr.cf_error);
887                 rpc_createerr.cf_stat = stat;
888         }
889         clnt_destroy(clnt);
890         close(sock);
891
892         if (stat == RPC_SUCCESS)
893                 return 1;
894         else
895                 return 0;
896 }
897
898 /*
899  * Try a getsockname() on a connected datagram socket.
900  *
901  * Returns 1 and fills in @buf if successful; otherwise, zero.
902  *
903  * A connected datagram socket prevents leaving a socket in TIME_WAIT.
904  * This conserves the ephemeral port number space, helping reduce failed
905  * socket binds during mount storms.
906  */
907 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
908                            struct sockaddr *buf, socklen_t *buflen)
909 {
910         struct sockaddr_in sin = {
911                 .sin_family             = AF_INET,
912                 .sin_addr.s_addr        = htonl(INADDR_ANY),
913         };
914         struct sockaddr_in6 sin6 = {
915                 .sin6_family            = AF_INET6,
916                 .sin6_addr              = IN6ADDR_ANY_INIT,
917         };
918         int sock;
919
920         sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
921         if (sock < 0)
922                 return 0;
923
924         switch (sap->sa_family) {
925         case AF_INET:
926                 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
927                         close(sock);
928                         return 0;
929                 }
930                 break;
931         case AF_INET6:
932                 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
933                         close(sock);
934                         return 0;
935                 }
936                 break;
937         default:
938                 errno = EAFNOSUPPORT;
939                 return 0;
940         }
941
942         if (connect(sock, sap, salen) < 0) {
943                 close(sock);
944                 return 0;
945         }
946
947         return !getsockname(sock, buf, buflen);
948 }
949
950 /*
951  * Try to generate an address that prevents the server from calling us.
952  *
953  * Returns 1 and fills in @buf if successful; otherwise, zero.
954  */
955 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
956                       struct sockaddr *buf, socklen_t *buflen)
957 {
958         struct addrinfo *gai_results;
959         struct addrinfo gai_hint = {
960                 .ai_family      = sap->sa_family,
961                 .ai_flags       = AI_PASSIVE,   /* ANYADDR */
962         };
963
964         if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
965                 return 0;
966
967         *buflen = gai_results->ai_addrlen;
968         memcpy(buf, gai_results->ai_addr, *buflen);
969
970         freeaddrinfo(gai_results);
971
972         return 1;
973 }
974
975 /**
976  * nfs_callback_address - acquire our local network address
977  * @sap: pointer to address of remote
978  * @sap_len: length of address
979  * @buf: pointer to buffer to be filled in with local network address
980  * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
981  *
982  * Discover a network address that an NFSv4 server can use to call us back.
983  * On multi-homed clients, this address depends on which NIC we use to
984  * route requests to the server.
985  *
986  * Returns 1 and fills in @buf if an unambiguous local address is
987  * available; returns 1 and fills in an appropriate ANYADDR address
988  * if a local address isn't available; otherwise, returns zero.
989  */
990 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
991                          struct sockaddr *buf, socklen_t *buflen)
992 {
993         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
994
995         if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
996                 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
997                         goto out_failed;
998
999         /*
1000          * The server can't use an interface ID that was generated
1001          * here on the client, so always clear sin6_scope_id.
1002          */
1003         if (sin6->sin6_family == AF_INET6)
1004                 sin6->sin6_scope_id = 0;
1005
1006         return 1;
1007
1008 out_failed:
1009         *buflen = 0;
1010         if (verbose)
1011                 nfs_error(_("%s: failed to construct callback address"));
1012         return 0;
1013
1014 }