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