]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/rmtcall.c
Remove notify functionality from statd in favour of sm-notify
[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 /*
88  * Try to resolve host name for notify/callback request
89  *
90  * When compiled with RESTRICTED_STATD defined, we expect all
91  * host names to be dotted quads. See monitor.c for details. --okir
92  */
93 #ifdef RESTRICTED_STATD
94 static int
95 try_to_resolve(notify_list *lp)
96 {
97         char            *hname;
98
99         hname = NL_MY_NAME(lp);
100         if (!inet_aton(hname, &(NL_ADDR(lp)))) {
101                 note(N_ERROR, "%s is not an dotted-quad address", hname);
102                 NL_TIMES(lp) = 0;
103                 return 0;
104         }
105
106         /* XXX: In order to handle multi-homed hosts, we could do
107          * a reverse lookup, a forward lookup, and cycle through
108          * all the addresses.
109          */
110         return 1;
111 }
112 #else
113 static int
114 try_to_resolve(notify_list *lp)
115 {
116         struct hostent  *hp;
117         char            *hname;
118
119         hname = NL_MY_NAME(lp);
120
121         dprintf(N_DEBUG, "Trying to resolve %s.", hname);
122         if (!(hp = gethostbyname(hname))) {
123                 herror("gethostbyname");
124                 NL_TIMES(lp) -= 1;
125                 return 0;
126         }
127
128         if (hp->h_addrtype != AF_INET) {
129                 note(N_ERROR, "%s is not an AF_INET address", hname);
130                 NL_TIMES(lp) = 0;
131                 return 0;
132         }
133
134         /* FIXME: should try all addresses for multi-homed hosts in
135          * alternation because one interface might be down/unreachable. */
136         NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
137
138         dprintf(N_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
139         return 1;
140 }
141 #endif
142
143 static unsigned long
144 xmit_call(int sockfd, struct sockaddr_in *sin,
145           u_int32_t prog, u_int32_t vers, u_int32_t proc,
146           xdrproc_t func, void *obj)
147 /*              __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
148 {
149         unsigned int            msgbuf[MAXMSGSIZE], msglen;
150         struct rpc_msg          mesg;
151         struct pmap             pmap;
152         XDR                     xdr, *xdrs = &xdr;
153         int                     err;
154
155         if (!xid)
156                 xid = getpid() + time(NULL);
157
158         mesg.rm_xid = ++xid;
159         mesg.rm_direction = CALL;
160         mesg.rm_call.cb_rpcvers = 2;
161         if (sin->sin_port == 0) {
162                 sin->sin_port = htons(PMAPPORT);
163                 mesg.rm_call.cb_prog = PMAPPROG;
164                 mesg.rm_call.cb_vers = PMAPVERS;
165                 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
166                 pmap.pm_prog = prog;
167                 pmap.pm_vers = vers;
168                 pmap.pm_prot = IPPROTO_UDP;
169                 pmap.pm_port = 0;
170                 func = (xdrproc_t) xdr_pmap;
171                 obj  = &pmap;
172         } else {
173                 mesg.rm_call.cb_prog = prog;
174                 mesg.rm_call.cb_vers = vers;
175                 mesg.rm_call.cb_proc = proc;
176         }
177         mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
178         mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
179         mesg.rm_call.cb_cred.oa_length = 0;
180         mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
181         mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
182         mesg.rm_call.cb_verf.oa_length = 0;
183
184         /* Create XDR memory object for encoding */
185         xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
186
187         /* Encode the RPC header part and payload */
188         if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
189                 dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
190                 xdr_destroy(xdrs);
191                 return 0;
192         }
193
194         /* Get overall length of datagram */
195         msglen = xdr_getpos(xdrs);
196
197         if ((err = sendto(sockfd, msgbuf, msglen, 0,
198                         (struct sockaddr *) sin, sizeof(*sin))) < 0) {
199                 dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
200         } else if (err != msglen) {
201                 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
202         }
203
204         xdr_destroy(xdrs);
205
206         return err == msglen? xid : 0;
207 }
208
209 static notify_list *
210 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
211 {
212         unsigned int            msgbuf[MAXMSGSIZE], msglen;
213         struct rpc_msg          mesg;
214         notify_list             *lp = NULL;
215         XDR                     xdr, *xdrs = &xdr;
216         socklen_t               alen = sizeof(*sin);
217
218         /* Receive message */
219         if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
220                         (struct sockaddr *) sin, &alen)) < 0) {
221                 dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
222                 return NULL;
223         }
224
225         /* Create XDR object for decoding buffer */
226         xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
227
228         memset(&mesg, 0, sizeof(mesg));
229         mesg.rm_reply.rp_acpt.ar_results.where = NULL;
230         mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
231
232         if (!xdr_replymsg(xdrs, &mesg)) {
233                 note(N_WARNING, "recv_rply: can't decode RPC message!\n");
234                 goto done;
235         }
236
237         if (mesg.rm_reply.rp_stat != 0) {
238                 note(N_WARNING, "recv_rply: [%s] RPC status %d\n", 
239                                 inet_ntoa(sin->sin_addr),
240                                 mesg.rm_reply.rp_stat);
241                 goto done;
242         }
243         if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
244                 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
245                                 inet_ntoa(sin->sin_addr),
246                                 mesg.rm_reply.rp_acpt.ar_stat);
247                 goto done;
248         }
249
250         for (lp = notify; lp != NULL; lp = lp->next) {
251                 /* LH - this was a bug... it should have been checking
252                  * the xid from the response message from the client,
253                  * not the static, internal xid */
254                 if (lp->xid != mesg.rm_xid)
255                         continue;
256                 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
257                         char addr [18];
258                         strncpy (addr, inet_ntoa(lp->addr),
259                                  sizeof (addr) - 1);
260                         addr [sizeof (addr) - 1] = '\0';
261                         dprintf(N_WARNING, "address mismatch: "
262                                 "expected %s, got %s\n",
263                                 addr, inet_ntoa(sin->sin_addr));
264                 }
265                 if (lp->port == 0) {
266                         if (!xdr_u_long(xdrs, portp)) {
267                                 note(N_WARNING, "recv_rply: [%s] "
268                                         "can't decode reply body!\n",
269                                         inet_ntoa(sin->sin_addr));
270                                 lp = NULL;
271                                 goto done;
272                         }
273                 }
274                 break;
275         }
276
277 done:
278         xdr_destroy(xdrs);
279         return lp;
280 }
281
282 /*
283  * Notify operation for a single list entry
284  */
285 static int
286 process_entry(int sockfd, notify_list *lp)
287 {
288         struct sockaddr_in      sin;
289         struct status           new_status;
290         xdrproc_t               func;
291         void                    *objp;
292         u_int32_t               proc, vers, prog;
293 /*      __u32                   proc, vers, prog; */
294
295         if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
296                 return NL_TIMES(lp);
297         if (NL_TIMES(lp) == 0) {
298                 note(N_DEBUG, "Cannot notify %s, giving up.\n",
299                                         inet_ntoa(NL_ADDR(lp)));
300                 return 0;
301         }
302
303         memset(&sin, 0, sizeof(sin));
304         sin.sin_family = AF_INET;
305         sin.sin_port   = lp->port;
306         /* LH - moved address into switch */
307
308         prog = NL_MY_PROG(lp);
309         vers = NL_MY_VERS(lp);
310         proc = NL_MY_PROC(lp);
311
312         /* __FORCE__ loopback for callbacks to lockd ... */
313         /* Just in case we somehow ignored it thus far */
314         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
315
316         func = (xdrproc_t) xdr_status;
317         objp = &new_status;
318         new_status.mon_name = NL_MON_NAME(lp);
319         new_status.state    = NL_STATE(lp);
320         memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
321
322         lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
323         if (!lp->xid) {
324                 note(N_WARNING, "notify_host: failed to notify %s\n",
325                                 inet_ntoa(lp->addr));
326         }
327         NL_TIMES(lp) -= 1;
328
329         return 1;
330 }
331
332 /*
333  * Process a datagram received on the notify socket
334  */
335 int
336 process_reply(FD_SET_TYPE *rfds)
337 {
338         struct sockaddr_in      sin;
339         notify_list             *lp;
340         u_long                  port;
341
342         if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
343                 return 0;
344
345         if (!(lp = recv_rply(sockfd, &sin, &port)))
346                 return 1;
347
348         if (lp->port == 0) {
349                 if (port != 0) {
350                         lp->port = htons((unsigned short) port);
351                         process_entry(sockfd, lp);
352                         NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
353                         nlist_remove(&notify, lp);
354                         nlist_insert_timer(&notify, lp);
355                         return 1;
356                 }
357                 note(N_WARNING, "recv_rply: [%s] service %d not registered",
358                         inet_ntoa(lp->addr), NL_MY_PROG(lp));
359         } else {
360                 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
361                         NL_MY_NAME(lp), NL_MON_NAME(lp));
362         }
363         nlist_free(&notify, lp);
364         return 1;
365 }
366
367 /*
368  * Process a notify list, either for notifying remote hosts after reboot
369  * or for calling back (local) statd clients when the remote has notified
370  * us of a crash. 
371  */
372 int
373 process_notify_list(void)
374 {
375         notify_list     *entry;
376         time_t          now;
377         int             fd;
378
379         if ((fd = statd_get_socket()) < 0)
380                 return 0;
381
382         while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
383                 if (process_entry(fd, entry)) {
384                         NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
385                         nlist_remove(&notify, entry);
386                         nlist_insert_timer(&notify, entry);
387                 } else {
388                         note(N_ERROR,
389                                 "Can't callback %s (%d,%d), giving up.",
390                                         NL_MY_NAME(entry),
391                                         NL_MY_PROG(entry),
392                                         NL_MY_VERS(entry));
393                         nlist_free(&notify, entry);
394                 }
395         }
396
397         return 1;
398 }