]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/rmtcall.c
statd - remove try_to_resolve
[nfs-utils.git] / utils / statd / rmtcall.c
1 /*
2  * Copyright (C) 1996, 1999 Olaf Kirch
3  * Modified by Jeffrey A. Uphoff, 1997-1999.
4  * Modified by H.J. Lu, 1998.
5  * Modified by Lon Hohberger, Oct. 2000
6  *   - Bugfix handling client responses.
7  *   - Paranoia on NOTIFY_CALLBACK case
8  *
9  * NSM for Linux.
10  */
11
12 /*
13  * After reboot, notify all hosts on our notify list. In order not to
14  * hang statd with delivery to dead hosts, we perform all RPC calls in
15  * parallel.
16  *
17  * It would have been nice to use the portmapper's rmtcall feature,
18  * but that's not possible for security reasons (the portmapper would
19  * have to forward the call with root privs for most statd's, which
20  * it won't if it's worth its money).
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/time.h>
30 #include <netinet/in.h>
31 #include <net/if.h>
32 #include <arpa/inet.h>
33 #include <rpc/rpc.h>
34 #include <rpc/pmap_prot.h>
35 #include <rpc/pmap_rmt.h>
36 #include <time.h>
37 #include <netdb.h>
38 #include <string.h>
39 #include <unistd.h>
40 #ifdef HAVE_IFADDRS_H
41 #include <ifaddrs.h>
42 #endif /* HAVE_IFADDRS_H */
43 #include "sm_inter.h"
44 #include "statd.h"
45 #include "notlist.h"
46 #include "log.h"
47 #include "ha-callout.h"
48
49 #if SIZEOF_SOCKLEN_T - 0 == 0
50 #define socklen_t int
51 #endif
52
53 #define MAXMSGSIZE      (2048 / sizeof(unsigned int))
54
55 static unsigned long    xid = 0;        /* RPC XID counter */
56 static int              sockfd = -1;    /* notify socket */
57
58 /*
59  * Initialize callback socket
60  */
61 int
62 statd_get_socket(void)
63 {
64         struct sockaddr_in      sin;
65
66         if (sockfd >= 0)
67                 return sockfd;
68
69         if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
70                 note(N_CRIT, "Can't create socket: %m");
71                 return -1;
72         }
73
74         FD_SET(sockfd, &SVC_FDSET);
75
76         memset(&sin, 0, sizeof(sin));
77         sin.sin_family = AF_INET;
78         sin.sin_addr.s_addr = INADDR_ANY;
79
80         if (bindresvport(sockfd, &sin) < 0) {
81                 dprintf(N_WARNING,
82                         "process_hosts: can't bind to reserved port\n");
83         }
84         return sockfd;
85 }
86
87 static unsigned long
88 xmit_call(int sockfd, struct sockaddr_in *sin,
89           u_int32_t prog, u_int32_t vers, u_int32_t proc,
90           xdrproc_t func, void *obj)
91 /*              __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
92 {
93         unsigned int            msgbuf[MAXMSGSIZE], msglen;
94         struct rpc_msg          mesg;
95         struct pmap             pmap;
96         XDR                     xdr, *xdrs = &xdr;
97         int                     err;
98
99         if (!xid)
100                 xid = getpid() + time(NULL);
101
102         mesg.rm_xid = ++xid;
103         mesg.rm_direction = CALL;
104         mesg.rm_call.cb_rpcvers = 2;
105         if (sin->sin_port == 0) {
106                 sin->sin_port = htons(PMAPPORT);
107                 mesg.rm_call.cb_prog = PMAPPROG;
108                 mesg.rm_call.cb_vers = PMAPVERS;
109                 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
110                 pmap.pm_prog = prog;
111                 pmap.pm_vers = vers;
112                 pmap.pm_prot = IPPROTO_UDP;
113                 pmap.pm_port = 0;
114                 func = (xdrproc_t) xdr_pmap;
115                 obj  = &pmap;
116         } else {
117                 mesg.rm_call.cb_prog = prog;
118                 mesg.rm_call.cb_vers = vers;
119                 mesg.rm_call.cb_proc = proc;
120         }
121         mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
122         mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
123         mesg.rm_call.cb_cred.oa_length = 0;
124         mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
125         mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
126         mesg.rm_call.cb_verf.oa_length = 0;
127
128         /* Create XDR memory object for encoding */
129         xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
130
131         /* Encode the RPC header part and payload */
132         if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
133                 dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
134                 xdr_destroy(xdrs);
135                 return 0;
136         }
137
138         /* Get overall length of datagram */
139         msglen = xdr_getpos(xdrs);
140
141         if ((err = sendto(sockfd, msgbuf, msglen, 0,
142                         (struct sockaddr *) sin, sizeof(*sin))) < 0) {
143                 dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
144         } else if (err != msglen) {
145                 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
146         }
147
148         xdr_destroy(xdrs);
149
150         return err == msglen? xid : 0;
151 }
152
153 static notify_list *
154 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
155 {
156         unsigned int            msgbuf[MAXMSGSIZE], msglen;
157         struct rpc_msg          mesg;
158         notify_list             *lp = NULL;
159         XDR                     xdr, *xdrs = &xdr;
160         socklen_t               alen = sizeof(*sin);
161
162         /* Receive message */
163         if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
164                         (struct sockaddr *) sin, &alen)) < 0) {
165                 dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
166                 return NULL;
167         }
168
169         /* Create XDR object for decoding buffer */
170         xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
171
172         memset(&mesg, 0, sizeof(mesg));
173         mesg.rm_reply.rp_acpt.ar_results.where = NULL;
174         mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
175
176         if (!xdr_replymsg(xdrs, &mesg)) {
177                 note(N_WARNING, "recv_rply: can't decode RPC message!\n");
178                 goto done;
179         }
180
181         if (mesg.rm_reply.rp_stat != 0) {
182                 note(N_WARNING, "recv_rply: [%s] RPC status %d\n", 
183                                 inet_ntoa(sin->sin_addr),
184                                 mesg.rm_reply.rp_stat);
185                 goto done;
186         }
187         if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
188                 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
189                                 inet_ntoa(sin->sin_addr),
190                                 mesg.rm_reply.rp_acpt.ar_stat);
191                 goto done;
192         }
193
194         for (lp = notify; lp != NULL; lp = lp->next) {
195                 /* LH - this was a bug... it should have been checking
196                  * the xid from the response message from the client,
197                  * not the static, internal xid */
198                 if (lp->xid != mesg.rm_xid)
199                         continue;
200                 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
201                         char addr [18];
202                         strncpy (addr, inet_ntoa(lp->addr),
203                                  sizeof (addr) - 1);
204                         addr [sizeof (addr) - 1] = '\0';
205                         dprintf(N_WARNING, "address mismatch: "
206                                 "expected %s, got %s\n",
207                                 addr, inet_ntoa(sin->sin_addr));
208                 }
209                 if (lp->port == 0) {
210                         if (!xdr_u_long(xdrs, portp)) {
211                                 note(N_WARNING, "recv_rply: [%s] "
212                                         "can't decode reply body!\n",
213                                         inet_ntoa(sin->sin_addr));
214                                 lp = NULL;
215                                 goto done;
216                         }
217                 }
218                 break;
219         }
220
221 done:
222         xdr_destroy(xdrs);
223         return lp;
224 }
225
226 /*
227  * Notify operation for a single list entry
228  */
229 static int
230 process_entry(int sockfd, notify_list *lp)
231 {
232         struct sockaddr_in      sin;
233         struct status           new_status;
234         xdrproc_t               func;
235         void                    *objp;
236         u_int32_t               proc, vers, prog;
237 /*      __u32                   proc, vers, prog; */
238
239         if (NL_TIMES(lp) == 0) {
240                 note(N_DEBUG, "Cannot notify %s, giving up.\n",
241                                         inet_ntoa(NL_ADDR(lp)));
242                 return 0;
243         }
244
245         memset(&sin, 0, sizeof(sin));
246         sin.sin_family = AF_INET;
247         sin.sin_port   = lp->port;
248         /* LH - moved address into switch */
249
250         prog = NL_MY_PROG(lp);
251         vers = NL_MY_VERS(lp);
252         proc = NL_MY_PROC(lp);
253
254         /* __FORCE__ loopback for callbacks to lockd ... */
255         /* Just in case we somehow ignored it thus far */
256         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
257
258         func = (xdrproc_t) xdr_status;
259         objp = &new_status;
260         new_status.mon_name = NL_MON_NAME(lp);
261         new_status.state    = NL_STATE(lp);
262         memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
263
264         lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
265         if (!lp->xid) {
266                 note(N_WARNING, "notify_host: failed to notify port %d\n",
267                                 ntohs(lp->port));
268         }
269         NL_TIMES(lp) -= 1;
270
271         return 1;
272 }
273
274 /*
275  * Process a datagram received on the notify socket
276  */
277 int
278 process_reply(FD_SET_TYPE *rfds)
279 {
280         struct sockaddr_in      sin;
281         notify_list             *lp;
282         u_long                  port;
283
284         if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
285                 return 0;
286
287         if (!(lp = recv_rply(sockfd, &sin, &port)))
288                 return 1;
289
290         if (lp->port == 0) {
291                 if (port != 0) {
292                         lp->port = htons((unsigned short) port);
293                         process_entry(sockfd, lp);
294                         NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
295                         nlist_remove(&notify, lp);
296                         nlist_insert_timer(&notify, lp);
297                         return 1;
298                 }
299                 note(N_WARNING, "recv_rply: [%s] service %d not registered",
300                         inet_ntoa(lp->addr), NL_MY_PROG(lp));
301         } else {
302                 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
303                         NL_MY_NAME(lp), NL_MON_NAME(lp));
304         }
305         nlist_free(&notify, lp);
306         return 1;
307 }
308
309 /*
310  * Process a notify list, either for notifying remote hosts after reboot
311  * or for calling back (local) statd clients when the remote has notified
312  * us of a crash. 
313  */
314 int
315 process_notify_list(void)
316 {
317         notify_list     *entry;
318         time_t          now;
319         int             fd;
320
321         if ((fd = statd_get_socket()) < 0)
322                 return 0;
323
324         while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
325                 if (process_entry(fd, entry)) {
326                         NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
327                         nlist_remove(&notify, entry);
328                         nlist_insert_timer(&notify, entry);
329                 } else {
330                         note(N_ERROR,
331                                 "Can't callback %s (%d,%d), giving up.",
332                                         NL_MY_NAME(entry),
333                                         NL_MY_PROG(entry),
334                                         NL_MY_VERS(entry));
335                         nlist_free(&notify, entry);
336                 }
337         }
338
339         return 1;
340 }