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