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