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