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(void)
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 (bindresvport(sockfd, &sin) < 0) {
82 "process_hosts: can't bind to reserved port\n");
88 * Try to resolve host name for notify/callback request
90 * When compiled with RESTRICTED_STATD defined, we expect all
91 * host names to be dotted quads. See monitor.c for details. --okir
93 #ifdef RESTRICTED_STATD
95 try_to_resolve(notify_list *lp)
99 hname = NL_MY_NAME(lp);
100 if (!inet_aton(hname, &(NL_ADDR(lp)))) {
101 note(N_ERROR, "%s is not an dotted-quad address", hname);
106 /* XXX: In order to handle multi-homed hosts, we could do
107 * a reverse lookup, a forward lookup, and cycle through
114 try_to_resolve(notify_list *lp)
119 hname = NL_MY_NAME(lp);
121 dprintf(N_DEBUG, "Trying to resolve %s.", hname);
122 if (!(hp = gethostbyname(hname))) {
123 herror("gethostbyname");
128 if (hp->h_addrtype != AF_INET) {
129 note(N_ERROR, "%s is not an AF_INET address", hname);
134 /* FIXME: should try all addresses for multi-homed hosts in
135 * alternation because one interface might be down/unreachable. */
136 NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
138 dprintf(N_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
144 xmit_call(int sockfd, struct sockaddr_in *sin,
145 u_int32_t prog, u_int32_t vers, u_int32_t proc,
146 xdrproc_t func, void *obj)
147 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
149 unsigned int msgbuf[MAXMSGSIZE], msglen;
152 XDR xdr, *xdrs = &xdr;
156 xid = getpid() + time(NULL);
159 mesg.rm_direction = CALL;
160 mesg.rm_call.cb_rpcvers = 2;
161 if (sin->sin_port == 0) {
162 sin->sin_port = htons(PMAPPORT);
163 mesg.rm_call.cb_prog = PMAPPROG;
164 mesg.rm_call.cb_vers = PMAPVERS;
165 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
168 pmap.pm_prot = IPPROTO_UDP;
170 func = (xdrproc_t) xdr_pmap;
173 mesg.rm_call.cb_prog = prog;
174 mesg.rm_call.cb_vers = vers;
175 mesg.rm_call.cb_proc = proc;
177 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
178 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
179 mesg.rm_call.cb_cred.oa_length = 0;
180 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
181 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
182 mesg.rm_call.cb_verf.oa_length = 0;
184 /* Create XDR memory object for encoding */
185 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
187 /* Encode the RPC header part and payload */
188 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
189 dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
194 /* Get overall length of datagram */
195 msglen = xdr_getpos(xdrs);
197 if ((err = sendto(sockfd, msgbuf, msglen, 0,
198 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
199 dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
200 } else if (err != msglen) {
201 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
206 return err == msglen? xid : 0;
210 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
212 unsigned int msgbuf[MAXMSGSIZE], msglen;
214 notify_list *lp = NULL;
215 XDR xdr, *xdrs = &xdr;
216 socklen_t alen = sizeof(*sin);
218 /* Receive message */
219 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
220 (struct sockaddr *) sin, &alen)) < 0) {
221 dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
225 /* Create XDR object for decoding buffer */
226 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
228 memset(&mesg, 0, sizeof(mesg));
229 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
230 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
232 if (!xdr_replymsg(xdrs, &mesg)) {
233 note(N_WARNING, "recv_rply: can't decode RPC message!\n");
237 if (mesg.rm_reply.rp_stat != 0) {
238 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
239 inet_ntoa(sin->sin_addr),
240 mesg.rm_reply.rp_stat);
243 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
244 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
245 inet_ntoa(sin->sin_addr),
246 mesg.rm_reply.rp_acpt.ar_stat);
250 for (lp = notify; lp != NULL; lp = lp->next) {
251 /* LH - this was a bug... it should have been checking
252 * the xid from the response message from the client,
253 * not the static, internal xid */
254 if (lp->xid != mesg.rm_xid)
256 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
258 strncpy (addr, inet_ntoa(lp->addr),
260 addr [sizeof (addr) - 1] = '\0';
261 dprintf(N_WARNING, "address mismatch: "
262 "expected %s, got %s\n",
263 addr, inet_ntoa(sin->sin_addr));
266 if (!xdr_u_long(xdrs, portp)) {
267 note(N_WARNING, "recv_rply: [%s] "
268 "can't decode reply body!\n",
269 inet_ntoa(sin->sin_addr));
283 * Notify operation for a single list entry
286 process_entry(int sockfd, notify_list *lp)
288 struct sockaddr_in sin;
289 struct status new_status;
292 u_int32_t proc, vers, prog;
293 /* __u32 proc, vers, prog; */
295 if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
297 if (NL_TIMES(lp) == 0) {
298 note(N_DEBUG, "Cannot notify %s, giving up.\n",
299 inet_ntoa(NL_ADDR(lp)));
303 memset(&sin, 0, sizeof(sin));
304 sin.sin_family = AF_INET;
305 sin.sin_port = lp->port;
306 /* LH - moved address into switch */
308 prog = NL_MY_PROG(lp);
309 vers = NL_MY_VERS(lp);
310 proc = NL_MY_PROC(lp);
312 /* __FORCE__ loopback for callbacks to lockd ... */
313 /* Just in case we somehow ignored it thus far */
314 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
316 func = (xdrproc_t) xdr_status;
318 new_status.mon_name = NL_MON_NAME(lp);
319 new_status.state = NL_STATE(lp);
320 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
322 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
324 note(N_WARNING, "notify_host: failed to notify %s\n",
325 inet_ntoa(lp->addr));
333 * Process a datagram received on the notify socket
336 process_reply(FD_SET_TYPE *rfds)
338 struct sockaddr_in sin;
342 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
345 if (!(lp = recv_rply(sockfd, &sin, &port)))
350 lp->port = htons((unsigned short) port);
351 process_entry(sockfd, lp);
352 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
353 nlist_remove(¬ify, lp);
354 nlist_insert_timer(¬ify, lp);
357 note(N_WARNING, "recv_rply: [%s] service %d not registered",
358 inet_ntoa(lp->addr), NL_MY_PROG(lp));
360 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
361 NL_MY_NAME(lp), NL_MON_NAME(lp));
363 nlist_free(¬ify, lp);
368 * Process a notify list, either for notifying remote hosts after reboot
369 * or for calling back (local) statd clients when the remote has notified
373 process_notify_list(void)
379 if ((fd = statd_get_socket()) < 0)
382 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
383 if (process_entry(fd, entry)) {
384 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
385 nlist_remove(¬ify, entry);
386 nlist_insert_timer(¬ify, entry);
389 "Can't callback %s (%d,%d), giving up.",
393 nlist_free(¬ify, entry);