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
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
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).
27 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
32 #include <arpa/inet.h>
34 #include <rpc/pmap_prot.h>
35 #include <rpc/pmap_rmt.h>
42 #endif /* HAVE_IFADDRS_H */
47 #include "ha-callout.h"
49 #if SIZEOF_SOCKLEN_T - 0 == 0
53 #define MAXMSGSIZE (2048 / sizeof(unsigned int))
55 static unsigned long xid = 0; /* RPC XID counter */
56 static int sockfd = -1; /* notify socket */
59 * Initialize callback socket
62 statd_get_socket(int port)
64 struct sockaddr_in sin;
69 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
70 note(N_CRIT, "Can't create socket: %m");
74 FD_SET(sockfd, &SVC_FDSET);
76 memset(&sin, 0, sizeof(sin));
77 sin.sin_family = AF_INET;
78 sin.sin_addr.s_addr = INADDR_ANY;
80 * If a local hostname is given (-n option to statd), bind to the address
81 * specified. This is required to support clients that ignore the mon_name in
82 * the statd protocol but use the source address from the request packet.
85 struct hostent *hp = gethostbyname(MY_NAME);
87 sin.sin_addr = *(struct in_addr *) hp->h_addr;
90 sin.sin_port = htons(port);
91 if (bind(sockfd, &sin, sizeof(sin)) == 0)
93 note(N_CRIT, "statd: failed to bind to outgoing port, %d\n"
94 " falling back on randomly chosen port\n", port);
96 if (bindresvport(sockfd, &sin) < 0) {
98 "process_hosts: can't bind to reserved port\n");
104 #ifdef HAVE_IFADDRS_H
106 * Using the NL_ADDR(lp), reset (if needed) the hostname
107 * that will be put in the SM_NOTIFY to the hostname
108 * that is associated with the network interface
112 reset_my_name(notify_list *lp)
114 struct ifaddrs *ifa = NULL, *ifap;
115 struct in_addr netaddr, tmp;
116 struct sockaddr_in *sin, *nsin;
119 netaddr.s_addr = inet_netof(NL_ADDR(lp));
120 if (getifaddrs(&ifa) >= 0) {
121 for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
122 if (!(ifap->ifa_flags & IFF_UP))
125 note(N_DEBUG, "ifa_name %s\n", ifap->ifa_name);
126 if (ifap->ifa_addr == NULL)
128 if (ifap->ifa_addr->sa_family != AF_INET)
131 sin = (struct sockaddr_in *)ifap->ifa_addr;
132 nsin = (struct sockaddr_in *)ifap->ifa_netmask;
133 tmp.s_addr = sin->sin_addr.s_addr & nsin->sin_addr.s_addr;
134 if (memcmp(&tmp.s_addr, &netaddr.s_addr, sizeof(netaddr.s_addr)))
136 hp = gethostbyaddr((char *)&sin->sin_addr,
137 sizeof(sin->sin_addr), AF_INET);
140 if (strcmp(NL_MY_NAME(lp), hp->h_name)) {
141 free(NL_MY_NAME(lp));
142 NL_MY_NAME(lp)= strdup(hp->h_name);
143 note(N_DEBUG, "NL_MY_NAME %s\n", NL_MY_NAME(lp));
149 #endif /* HAVE_IFADDRS_H */
151 * Try to resolve host name for notify/callback request
153 * When compiled with RESTRICTED_STATD defined, we expect all
154 * host names to be dotted quads. See monitor.c for details. --okir
156 #ifdef RESTRICTED_STATD
158 try_to_resolve(notify_list *lp)
162 if (NL_TYPE(lp) == NOTIFY_REBOOT)
163 hname = NL_MON_NAME(lp);
165 hname = NL_MY_NAME(lp);
166 if (!inet_aton(hname, &(NL_ADDR(lp)))) {
167 note(N_ERROR, "%s is not an dotted-quad address", hname);
172 /* XXX: In order to handle multi-homed hosts, we could do
173 * a reverse lookup, a forward lookup, and cycle through
180 try_to_resolve(notify_list *lp)
185 if (NL_TYPE(lp) == NOTIFY_REBOOT)
186 hname = NL_MON_NAME(lp);
188 hname = NL_MY_NAME(lp);
190 dprintf(N_DEBUG, "Trying to resolve %s.", hname);
191 if (!(hp = gethostbyname(hname))) {
192 herror("gethostbyname");
197 if (hp->h_addrtype != AF_INET) {
198 note(N_ERROR, "%s is not an AF_INET address", hname);
203 /* FIXME: should try all addresses for multi-homed hosts in
204 * alternation because one interface might be down/unreachable. */
205 NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
207 dprintf(N_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
213 xmit_call(int sockfd, struct sockaddr_in *sin,
214 u_int32_t prog, u_int32_t vers, u_int32_t proc,
215 xdrproc_t func, void *obj)
216 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
218 unsigned int msgbuf[MAXMSGSIZE], msglen;
221 XDR xdr, *xdrs = &xdr;
225 xid = getpid() + time(NULL);
228 mesg.rm_direction = CALL;
229 mesg.rm_call.cb_rpcvers = 2;
230 if (sin->sin_port == 0) {
231 sin->sin_port = htons(PMAPPORT);
232 mesg.rm_call.cb_prog = PMAPPROG;
233 mesg.rm_call.cb_vers = PMAPVERS;
234 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
237 pmap.pm_prot = IPPROTO_UDP;
239 func = (xdrproc_t) xdr_pmap;
242 mesg.rm_call.cb_prog = prog;
243 mesg.rm_call.cb_vers = vers;
244 mesg.rm_call.cb_proc = proc;
246 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
247 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
248 mesg.rm_call.cb_cred.oa_length = 0;
249 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
250 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
251 mesg.rm_call.cb_verf.oa_length = 0;
253 /* Create XDR memory object for encoding */
254 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
256 /* Encode the RPC header part and payload */
257 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
258 dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
263 /* Get overall length of datagram */
264 msglen = xdr_getpos(xdrs);
266 if ((err = sendto(sockfd, msgbuf, msglen, 0,
267 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
268 dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
269 } else if (err != msglen) {
270 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
275 return err == msglen? xid : 0;
279 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
281 unsigned int msgbuf[MAXMSGSIZE], msglen;
283 notify_list *lp = NULL;
284 XDR xdr, *xdrs = &xdr;
285 socklen_t alen = sizeof(*sin);
287 /* Receive message */
288 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
289 (struct sockaddr *) sin, &alen)) < 0) {
290 dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
294 /* Create XDR object for decoding buffer */
295 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
297 memset(&mesg, 0, sizeof(mesg));
298 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
299 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
301 if (!xdr_replymsg(xdrs, &mesg)) {
302 note(N_WARNING, "recv_rply: can't decode RPC message!\n");
306 if (mesg.rm_reply.rp_stat != 0) {
307 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
308 inet_ntoa(sin->sin_addr),
309 mesg.rm_reply.rp_stat);
312 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
313 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
314 inet_ntoa(sin->sin_addr),
315 mesg.rm_reply.rp_acpt.ar_stat);
319 for (lp = notify; lp != NULL; lp = lp->next) {
320 /* LH - this was a bug... it should have been checking
321 * the xid from the response message from the client,
322 * not the static, internal xid */
323 if (lp->xid != mesg.rm_xid)
325 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
327 strncpy (addr, inet_ntoa(lp->addr),
329 addr [sizeof (addr) - 1] = '\0';
330 dprintf(N_WARNING, "address mismatch: "
331 "expected %s, got %s\n",
332 addr, inet_ntoa(sin->sin_addr));
335 if (!xdr_u_long(xdrs, portp)) {
336 note(N_WARNING, "recv_rply: [%s] "
337 "can't decode reply body!\n",
338 inet_ntoa(sin->sin_addr));
352 * Notify operation for a single list entry
355 process_entry(int sockfd, notify_list *lp)
357 struct sockaddr_in sin;
358 struct status new_status;
362 u_int32_t proc, vers, prog;
363 /* __u32 proc, vers, prog; */
365 if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
367 if (NL_TIMES(lp) == 0) {
368 note(N_DEBUG, "Cannot notify %s, giving up.\n",
369 inet_ntoa(NL_ADDR(lp)));
373 memset(&sin, 0, sizeof(sin));
374 sin.sin_family = AF_INET;
375 sin.sin_port = lp->port;
376 /* LH - moved address into switch */
378 switch (NL_TYPE(lp)) {
384 /* Use source address for notify replies */
385 sin.sin_addr = lp->addr;
387 * Unless a static hostname has been defined
388 * set the NL_MY_NAME(lp) hostname to the
389 * one associated with the network interface
391 #ifdef HAVE_IFADDRS_H
392 if (!(run_mode & STATIC_HOSTNAME))
394 #endif /* HAVE_IFADDRS_H */
395 func = (xdrproc_t) xdr_stat_chge;
396 new_stat.state = MY_STATE;
397 new_stat.mon_name = NL_MY_NAME(lp);
401 case NOTIFY_CALLBACK:
402 prog = NL_MY_PROG(lp);
403 vers = NL_MY_VERS(lp);
404 proc = NL_MY_PROC(lp);
406 /* __FORCE__ loopback for callbacks to lockd ... */
407 /* Just in case we somehow ignored it thus far */
408 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
410 func = (xdrproc_t) xdr_status;
412 new_status.mon_name = NL_MON_NAME(lp);
413 new_status.state = NL_STATE(lp);
414 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
417 note(N_ERROR, "notify_host: unknown notify type %d",
422 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
424 note(N_WARNING, "notify_host: failed to notify %s\n",
425 inet_ntoa(lp->addr));
433 * Process a datagram received on the notify socket
436 process_reply(FD_SET_TYPE *rfds)
438 struct sockaddr_in sin;
442 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
445 if (!(lp = recv_rply(sockfd, &sin, &port)))
450 lp->port = htons((unsigned short) port);
451 process_entry(sockfd, lp);
452 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
453 nlist_remove(¬ify, lp);
454 nlist_insert_timer(¬ify, lp);
457 note(N_WARNING, "recv_rply: [%s] service %d not registered",
459 NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
460 } else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
461 dprintf(N_DEBUG, "Notification of %s succeeded.",
463 xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
465 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
466 NL_MY_NAME(lp), NL_MON_NAME(lp));
468 nlist_free(¬ify, lp);
473 * Process a notify list, either for notifying remote hosts after reboot
474 * or for calling back (local) statd clients when the remote has notified
478 process_notify_list(void)
484 if ((fd = statd_get_socket(0)) < 0)
487 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
488 if (process_entry(fd, entry)) {
489 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
490 nlist_remove(¬ify, entry);
491 nlist_insert_timer(¬ify, entry);
492 } else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
494 "Can't callback %s (%d,%d), giving up.",
498 nlist_free(¬ify, entry);
501 "Can't notify %s, giving up.",
503 /* PRC: do the HA callout */
504 ha_callout("del-client", NL_MON_NAME(entry), NL_MY_NAME(entry), -1);
505 xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
506 nlist_free(¬ify, entry);