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