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).
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <netinet/in.h>
30 #include <arpa/inet.h>
32 #include <rpc/pmap_prot.h>
33 #include <rpc/pmap_rmt.h>
43 #include "ha-callout.h"
45 #define MAXMSGSIZE (2048 / sizeof(unsigned int))
47 static unsigned long xid = 0; /* RPC XID counter */
48 static int sockfd = -1; /* notify socket */
51 * Initialize callback socket
54 statd_get_socket(int port)
56 struct sockaddr_in sin;
61 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
62 note(N_CRIT, "Can't create socket: %m");
66 FD_SET(sockfd, &SVC_FDSET);
68 memset(&sin, 0, sizeof(sin));
69 sin.sin_family = AF_INET;
70 sin.sin_addr.s_addr = INADDR_ANY;
72 * If a local hostname is given (-n option to statd), bind to the address
73 * specified. This is required to support clients that ignore the mon_name in
74 * the statd protocol but use the source address from the request packet.
77 struct hostent *hp = gethostbyname(MY_NAME);
79 sin.sin_addr = *(struct in_addr *) hp->h_addr;
82 sin.sin_port = htons(port);
83 if (bind(sockfd, &sin, sizeof(sin)) == 0)
85 note(N_CRIT, "statd: failed to bind to outgoing port, %d\n"
86 " falling back on randomly chosen port\n", port);
88 if (bindresvport(sockfd, &sin) < 0) {
90 "process_hosts: can't bind to reserved port\n");
96 * Using the NL_ADDR(lp), reset (if needed) the hostname
97 * that will be put in the SM_NOTIFY to the hostname
98 * that is associated with the network interface
102 reset_my_name(notify_list *lp)
104 struct ifaddrs *ifa = NULL, *ifap;
105 struct in_addr netaddr, tmp;
106 struct sockaddr_in *sin, *nsin;
109 netaddr.s_addr = inet_netof(NL_ADDR(lp));
110 if (getifaddrs(&ifa) >= 0) {
111 for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
112 if (!(ifap->ifa_flags & IFF_UP))
115 note(N_DEBUG, "ifa_name %s\n", ifap->ifa_name);
116 if (ifap->ifa_addr == NULL)
118 if (ifap->ifa_addr->sa_family != AF_INET)
121 sin = (struct sockaddr_in *)ifap->ifa_addr;
122 nsin = (struct sockaddr_in *)ifap->ifa_netmask;
123 tmp.s_addr = sin->sin_addr.s_addr & nsin->sin_addr.s_addr;
124 if (memcmp(&tmp.s_addr, &netaddr.s_addr, sizeof(netaddr.s_addr)))
126 hp = gethostbyaddr((char *)&sin->sin_addr,
127 sizeof(sin->sin_addr), AF_INET);
130 if (strcmp(NL_MY_NAME(lp), hp->h_name)) {
131 free(NL_MY_NAME(lp));
132 NL_MY_NAME(lp)= strdup(hp->h_name);
133 note(N_DEBUG, "NL_MY_NAME %s\n", NL_MY_NAME(lp));
140 * Try to resolve host name for notify/callback request
142 * When compiled with RESTRICTED_STATD defined, we expect all
143 * host names to be dotted quads. See monitor.c for details. --okir
145 #ifdef RESTRICTED_STATD
147 try_to_resolve(notify_list *lp)
151 if (NL_TYPE(lp) == NOTIFY_REBOOT)
152 hname = NL_MON_NAME(lp);
154 hname = NL_MY_NAME(lp);
155 if (!inet_aton(hname, &(NL_ADDR(lp)))) {
156 note(N_ERROR, "%s is not an dotted-quad address", hname);
161 /* XXX: In order to handle multi-homed hosts, we could do
162 * a reverse lookup, a forward lookup, and cycle through
169 try_to_resolve(notify_list *lp)
174 if (NL_TYPE(lp) == NOTIFY_REBOOT)
175 hname = NL_MON_NAME(lp);
177 hname = NL_MY_NAME(lp);
179 dprintf(N_DEBUG, "Trying to resolve %s.", hname);
180 if (!(hp = gethostbyname(hname))) {
181 herror("gethostbyname");
186 if (hp->h_addrtype != AF_INET) {
187 note(N_ERROR, "%s is not an AF_INET address", hname);
192 /* FIXME: should try all addresses for multi-homed hosts in
193 * alternation because one interface might be down/unreachable. */
194 NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
196 dprintf(N_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
202 xmit_call(int sockfd, struct sockaddr_in *sin,
203 u_int32_t prog, u_int32_t vers, u_int32_t proc,
204 xdrproc_t func, void *obj)
205 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
207 unsigned int msgbuf[MAXMSGSIZE], msglen;
210 XDR xdr, *xdrs = &xdr;
214 xid = getpid() + time(NULL);
217 mesg.rm_direction = CALL;
218 mesg.rm_call.cb_rpcvers = 2;
219 if (sin->sin_port == 0) {
220 sin->sin_port = htons(PMAPPORT);
221 mesg.rm_call.cb_prog = PMAPPROG;
222 mesg.rm_call.cb_vers = PMAPVERS;
223 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
226 pmap.pm_prot = IPPROTO_UDP;
228 func = (xdrproc_t) xdr_pmap;
231 mesg.rm_call.cb_prog = prog;
232 mesg.rm_call.cb_vers = vers;
233 mesg.rm_call.cb_proc = proc;
235 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
236 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
237 mesg.rm_call.cb_cred.oa_length = 0;
238 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
239 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
240 mesg.rm_call.cb_verf.oa_length = 0;
242 /* Create XDR memory object for encoding */
243 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
245 /* Encode the RPC header part and payload */
246 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
247 dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
252 /* Get overall length of datagram */
253 msglen = xdr_getpos(xdrs);
255 if ((err = sendto(sockfd, msgbuf, msglen, 0,
256 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
257 dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
258 } else if (err != msglen) {
259 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
264 return err == msglen? xid : 0;
268 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
270 unsigned int msgbuf[MAXMSGSIZE], msglen;
272 notify_list *lp = NULL;
273 XDR xdr, *xdrs = &xdr;
274 int alen = sizeof(*sin);
276 /* Receive message */
277 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
278 (struct sockaddr *) sin, &alen)) < 0) {
279 dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
283 /* Create XDR object for decoding buffer */
284 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
286 memset(&mesg, 0, sizeof(mesg));
287 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
288 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
290 if (!xdr_replymsg(xdrs, &mesg)) {
291 note(N_WARNING, "recv_rply: can't decode RPC message!\n");
295 if (mesg.rm_reply.rp_stat != 0) {
296 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
297 inet_ntoa(sin->sin_addr),
298 mesg.rm_reply.rp_stat);
301 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
302 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
303 inet_ntoa(sin->sin_addr),
304 mesg.rm_reply.rp_acpt.ar_stat);
308 for (lp = notify; lp != NULL; lp = lp->next) {
309 /* LH - this was a bug... it should have been checking
310 * the xid from the response message from the client,
311 * not the static, internal xid */
312 if (lp->xid != mesg.rm_xid)
314 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
316 strncpy (addr, inet_ntoa(lp->addr),
318 addr [sizeof (addr) - 1] = '\0';
319 dprintf(N_WARNING, "address mismatch: "
320 "expected %s, got %s\n",
321 addr, inet_ntoa(sin->sin_addr));
324 if (!xdr_u_long(xdrs, portp)) {
325 note(N_WARNING, "recv_rply: [%s] "
326 "can't decode reply body!\n",
327 inet_ntoa(sin->sin_addr));
341 * Notify operation for a single list entry
344 process_entry(int sockfd, notify_list *lp)
346 struct sockaddr_in sin;
347 struct status new_status;
351 u_int32_t proc, vers, prog;
352 /* __u32 proc, vers, prog; */
354 if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
356 if (NL_TIMES(lp) == 0) {
357 note(N_DEBUG, "Cannot notify %s, giving up.\n",
358 inet_ntoa(NL_ADDR(lp)));
362 memset(&sin, 0, sizeof(sin));
363 sin.sin_family = AF_INET;
364 sin.sin_port = lp->port;
365 /* LH - moved address into switch */
367 switch (NL_TYPE(lp)) {
373 /* Use source address for notify replies */
374 sin.sin_addr = lp->addr;
376 * Unless a static hostname has been defined
377 * set the NL_MY_NAME(lp) hostname to the
378 * one associated with the network interface
380 if (!(run_mode & STATIC_HOSTNAME))
383 func = (xdrproc_t) xdr_stat_chge;
384 new_stat.state = MY_STATE;
385 new_stat.mon_name = NL_MY_NAME(lp);
389 case NOTIFY_CALLBACK:
390 prog = NL_MY_PROG(lp);
391 vers = NL_MY_VERS(lp);
392 proc = NL_MY_PROC(lp);
394 /* __FORCE__ loopback for callbacks to lockd ... */
395 /* Just in case we somehow ignored it thus far */
396 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
398 func = (xdrproc_t) xdr_status;
400 new_status.mon_name = NL_MON_NAME(lp);
401 new_status.state = NL_STATE(lp);
402 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
405 note(N_ERROR, "notify_host: unknown notify type %d",
410 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
412 note(N_WARNING, "notify_host: failed to notify %s\n",
413 inet_ntoa(lp->addr));
421 * Process a datagram received on the notify socket
424 process_reply(FD_SET_TYPE *rfds)
426 struct sockaddr_in sin;
430 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
433 if (!(lp = recv_rply(sockfd, &sin, &port)))
438 lp->port = htons((unsigned short) port);
439 process_entry(sockfd, lp);
440 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
441 nlist_remove(¬ify, lp);
442 nlist_insert_timer(¬ify, lp);
445 note(N_WARNING, "recv_rply: [%s] service %d not registered",
447 NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
448 } else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
449 dprintf(N_DEBUG, "Notification of %s succeeded.",
451 xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
453 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
454 NL_MY_NAME(lp), NL_MON_NAME(lp));
456 nlist_free(¬ify, lp);
461 * Process a notify list, either for notifying remote hosts after reboot
462 * or for calling back (local) statd clients when the remote has notified
466 process_notify_list(void)
472 if ((fd = statd_get_socket(0)) < 0)
475 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
476 if (process_entry(fd, entry)) {
477 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
478 nlist_remove(¬ify, entry);
479 nlist_insert_timer(¬ify, entry);
480 } else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
482 "Can't callback %s (%d,%d), giving up.",
486 nlist_free(¬ify, entry);
489 "Can't notify %s, giving up.",
491 /* PRC: do the HA callout */
492 ha_callout("del-client", NL_MON_NAME(entry), NL_MY_NAME(entry), -1);
493 xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
494 nlist_free(¬ify, entry);