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>
29 #include <arpa/inet.h>
31 #include <rpc/pmap_prot.h>
32 #include <rpc/pmap_rmt.h>
41 #include "ha-callout.h"
43 #define MAXMSGSIZE (2048 / sizeof(unsigned int))
45 static unsigned long xid = 0; /* RPC XID counter */
46 static int sockfd = -1; /* notify socket */
49 * Initialize callback socket
52 statd_get_socket(int port)
54 struct sockaddr_in sin;
59 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
60 note(N_CRIT, "Can't create socket: %m");
64 FD_SET(sockfd, &SVC_FDSET);
66 memset(&sin, 0, sizeof(sin));
67 sin.sin_family = AF_INET;
68 sin.sin_addr.s_addr = INADDR_ANY;
70 * If a local hostname is given (-n option to statd), bind to the address
71 * specified. This is required to support clients that ignore the mon_name in
72 * the statd protocol but use the source address from the request packet.
75 struct hostent *hp = gethostbyname(MY_NAME);
77 sin.sin_addr = *(struct in_addr *) hp->h_addr;
80 sin.sin_port = htons(port);
81 if (bind(sockfd, &sin, sizeof(sin)) == 0)
83 note(N_CRIT, "statd: failed to bind to outgoing port, %d\n"
84 " falling back on randomly chosen port\n", port);
86 if (bindresvport(sockfd, &sin) < 0) {
88 "process_hosts: can't bind to reserved port\n");
95 * Try to resolve host name for notify/callback request
97 * When compiled with RESTRICTED_STATD defined, we expect all
98 * host names to be dotted quads. See monitor.c for details. --okir
100 #ifdef RESTRICTED_STATD
102 try_to_resolve(notify_list *lp)
106 if (NL_TYPE(lp) == NOTIFY_REBOOT)
107 hname = NL_MON_NAME(lp);
109 hname = NL_MY_NAME(lp);
110 if (!inet_aton(hname, &(NL_ADDR(lp)))) {
111 note(N_ERROR, "%s is not an dotted-quad address", hname);
116 /* XXX: In order to handle multi-homed hosts, we could do
117 * a reverse lookup, a forward lookup, and cycle through
124 try_to_resolve(notify_list *lp)
129 if (NL_TYPE(lp) == NOTIFY_REBOOT)
130 hname = NL_MON_NAME(lp);
132 hname = NL_MY_NAME(lp);
134 dprintf(N_DEBUG, "Trying to resolve %s.", hname);
135 if (!(hp = gethostbyname(hname))) {
136 herror("gethostbyname");
141 if (hp->h_addrtype != AF_INET) {
142 note(N_ERROR, "%s is not an AF_INET address", hname);
147 /* FIXME: should try all addresses for multi-homed hosts in
148 * alternation because one interface might be down/unreachable. */
149 NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
151 dprintf(N_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
157 xmit_call(int sockfd, struct sockaddr_in *sin,
158 u_int32_t prog, u_int32_t vers, u_int32_t proc,
159 xdrproc_t func, void *obj)
160 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
162 unsigned int msgbuf[MAXMSGSIZE], msglen;
165 XDR xdr, *xdrs = &xdr;
169 xid = getpid() + time(NULL);
172 mesg.rm_direction = CALL;
173 mesg.rm_call.cb_rpcvers = 2;
174 if (sin->sin_port == 0) {
175 sin->sin_port = htons(PMAPPORT);
176 mesg.rm_call.cb_prog = PMAPPROG;
177 mesg.rm_call.cb_vers = PMAPVERS;
178 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
181 pmap.pm_prot = IPPROTO_UDP;
183 func = (xdrproc_t) xdr_pmap;
186 mesg.rm_call.cb_prog = prog;
187 mesg.rm_call.cb_vers = vers;
188 mesg.rm_call.cb_proc = proc;
190 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
191 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
192 mesg.rm_call.cb_cred.oa_length = 0;
193 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
194 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
195 mesg.rm_call.cb_verf.oa_length = 0;
197 /* Create XDR memory object for encoding */
198 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
200 /* Encode the RPC header part and payload */
201 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
202 dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
207 /* Get overall length of datagram */
208 msglen = xdr_getpos(xdrs);
210 if ((err = sendto(sockfd, msgbuf, msglen, 0,
211 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
212 dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
213 } else if (err != msglen) {
214 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
219 return err == msglen? xid : 0;
223 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
225 unsigned int msgbuf[MAXMSGSIZE], msglen;
227 notify_list *lp = NULL;
228 XDR xdr, *xdrs = &xdr;
229 int alen = sizeof(*sin);
231 /* Receive message */
232 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
233 (struct sockaddr *) sin, &alen)) < 0) {
234 dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
238 /* Create XDR object for decoding buffer */
239 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
241 memset(&mesg, 0, sizeof(mesg));
242 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
243 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
245 if (!xdr_replymsg(xdrs, &mesg)) {
246 note(N_WARNING, "recv_rply: can't decode RPC message!\n");
250 if (mesg.rm_reply.rp_stat != 0) {
251 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
252 inet_ntoa(sin->sin_addr),
253 mesg.rm_reply.rp_stat);
256 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
257 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
258 inet_ntoa(sin->sin_addr),
259 mesg.rm_reply.rp_acpt.ar_stat);
263 for (lp = notify; lp != NULL; lp = lp->next) {
264 /* LH - this was a bug... it should have been checking
265 * the xid from the response message from the client,
266 * not the static, internal xid */
267 if (lp->xid != mesg.rm_xid)
269 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
271 strncpy (addr, inet_ntoa(lp->addr),
273 addr [sizeof (addr) - 1] = '\0';
274 dprintf(N_WARNING, "address mismatch: "
275 "expected %s, got %s\n",
276 addr, inet_ntoa(sin->sin_addr));
279 if (!xdr_u_long(xdrs, portp)) {
280 note(N_WARNING, "recv_rply: [%s] "
281 "can't decode reply body!\n",
282 inet_ntoa(sin->sin_addr));
296 * Notify operation for a single list entry
299 process_entry(int sockfd, notify_list *lp)
301 struct sockaddr_in sin;
302 struct status new_status;
305 u_int32_t proc, vers, prog;
306 /* __u32 proc, vers, prog; */
308 if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
310 if (NL_TIMES(lp) == 0) {
311 note(N_DEBUG, "Cannot notify %s, giving up.\n",
312 inet_ntoa(NL_ADDR(lp)));
316 memset(&sin, 0, sizeof(sin));
317 sin.sin_family = AF_INET;
318 sin.sin_port = lp->port;
319 /* LH - moved address into switch */
321 switch (NL_TYPE(lp)) {
327 /* Use source address for notify replies */
328 sin.sin_addr = lp->addr;
330 func = (xdrproc_t) xdr_stat_chge;
331 objp = &SM_stat_chge;
333 case NOTIFY_CALLBACK:
334 prog = NL_MY_PROG(lp);
335 vers = NL_MY_VERS(lp);
336 proc = NL_MY_PROC(lp);
338 /* __FORCE__ loopback for callbacks to lockd ... */
339 /* Just in case we somehow ignored it thus far */
340 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
342 func = (xdrproc_t) xdr_status;
344 new_status.mon_name = NL_MON_NAME(lp);
345 new_status.state = NL_STATE(lp);
346 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
349 note(N_ERROR, "notify_host: unknown notify type %d",
354 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
356 note(N_WARNING, "notify_host: failed to notify %s\n",
357 inet_ntoa(lp->addr));
365 * Process a datagram received on the notify socket
368 process_reply(FD_SET_TYPE *rfds)
370 struct sockaddr_in sin;
374 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
377 if (!(lp = recv_rply(sockfd, &sin, &port)))
382 lp->port = htons((unsigned short) port);
383 process_entry(sockfd, lp);
384 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
385 nlist_remove(¬ify, lp);
386 nlist_insert_timer(¬ify, lp);
389 note(N_WARNING, "recv_rply: [%s] service %d not registered",
391 NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
392 } else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
393 dprintf(N_DEBUG, "Notification of %s succeeded.",
395 xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
397 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
398 NL_MY_NAME(lp), NL_MON_NAME(lp));
400 nlist_free(¬ify, lp);
405 * Process a notify list, either for notifying remote hosts after reboot
406 * or for calling back (local) statd clients when the remote has notified
410 process_notify_list(void)
416 if ((fd = statd_get_socket(0)) < 0)
419 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
420 if (process_entry(fd, entry)) {
421 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
422 nlist_remove(¬ify, entry);
423 nlist_insert_timer(¬ify, entry);
424 } else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
426 "Can't callback %s (%d,%d), giving up.",
430 nlist_free(¬ify, entry);
433 "Can't notify %s, giving up.",
435 /* PRC: do the HA callout */
436 ha_callout("del-client", NL_MON_NAME(entry), NL_MY_NAME(entry), -1);
437 xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
438 nlist_free(¬ify, entry);