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