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;
71 while (loopcnt-- > 0) {
73 if (sockfd >= 0) close(sockfd);
75 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
76 note(N_CRIT, "Can't create socket: %m");
81 memset(&sin, 0, sizeof(sin));
82 sin.sin_family = AF_INET;
83 sin.sin_addr.s_addr = INADDR_ANY;
85 if (bindresvport(sockfd, &sin) < 0) {
87 "process_hosts: can't bind to reserved port\n");
90 se = getservbyport(sin.sin_port, "udp");
93 /* rather not use that port, try again */
95 FD_SET(sockfd, &SVC_FDSET);
100 xmit_call(int sockfd, struct sockaddr_in *sin,
101 u_int32_t prog, u_int32_t vers, u_int32_t proc,
102 xdrproc_t func, void *obj)
103 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
105 unsigned int msgbuf[MAXMSGSIZE], msglen;
108 XDR xdr, *xdrs = &xdr;
112 xid = getpid() + time(NULL);
115 mesg.rm_direction = CALL;
116 mesg.rm_call.cb_rpcvers = 2;
117 if (sin->sin_port == 0) {
118 sin->sin_port = htons(PMAPPORT);
119 mesg.rm_call.cb_prog = PMAPPROG;
120 mesg.rm_call.cb_vers = PMAPVERS;
121 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
124 pmap.pm_prot = IPPROTO_UDP;
126 func = (xdrproc_t) xdr_pmap;
129 mesg.rm_call.cb_prog = prog;
130 mesg.rm_call.cb_vers = vers;
131 mesg.rm_call.cb_proc = proc;
133 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
134 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
135 mesg.rm_call.cb_cred.oa_length = 0;
136 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
137 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
138 mesg.rm_call.cb_verf.oa_length = 0;
140 /* Create XDR memory object for encoding */
141 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
143 /* Encode the RPC header part and payload */
144 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
145 dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
150 /* Get overall length of datagram */
151 msglen = xdr_getpos(xdrs);
153 if ((err = sendto(sockfd, msgbuf, msglen, 0,
154 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
155 dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
156 } else if (err != msglen) {
157 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
162 return err == msglen? xid : 0;
166 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
168 unsigned int msgbuf[MAXMSGSIZE], msglen;
170 notify_list *lp = NULL;
171 XDR xdr, *xdrs = &xdr;
172 socklen_t alen = sizeof(*sin);
174 /* Receive message */
175 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
176 (struct sockaddr *) sin, &alen)) < 0) {
177 dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
181 /* Create XDR object for decoding buffer */
182 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
184 memset(&mesg, 0, sizeof(mesg));
185 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
186 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
188 if (!xdr_replymsg(xdrs, &mesg)) {
189 note(N_WARNING, "recv_rply: can't decode RPC message!\n");
193 if (mesg.rm_reply.rp_stat != 0) {
194 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
195 inet_ntoa(sin->sin_addr),
196 mesg.rm_reply.rp_stat);
199 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
200 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
201 inet_ntoa(sin->sin_addr),
202 mesg.rm_reply.rp_acpt.ar_stat);
206 for (lp = notify; lp != NULL; lp = lp->next) {
207 /* LH - this was a bug... it should have been checking
208 * the xid from the response message from the client,
209 * not the static, internal xid */
210 if (lp->xid != mesg.rm_xid)
212 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
214 strncpy (addr, inet_ntoa(lp->addr),
216 addr [sizeof (addr) - 1] = '\0';
217 dprintf(N_WARNING, "address mismatch: "
218 "expected %s, got %s\n",
219 addr, inet_ntoa(sin->sin_addr));
222 if (!xdr_u_long(xdrs, portp)) {
223 note(N_WARNING, "recv_rply: [%s] "
224 "can't decode reply body!\n",
225 inet_ntoa(sin->sin_addr));
239 * Notify operation for a single list entry
242 process_entry(int sockfd, notify_list *lp)
244 struct sockaddr_in sin;
245 struct status new_status;
248 u_int32_t proc, vers, prog;
249 /* __u32 proc, vers, prog; */
251 if (NL_TIMES(lp) == 0) {
252 note(N_DEBUG, "Cannot notify %s, giving up.\n",
253 inet_ntoa(NL_ADDR(lp)));
257 memset(&sin, 0, sizeof(sin));
258 sin.sin_family = AF_INET;
259 sin.sin_port = lp->port;
260 /* LH - moved address into switch */
262 prog = NL_MY_PROG(lp);
263 vers = NL_MY_VERS(lp);
264 proc = NL_MY_PROC(lp);
266 /* __FORCE__ loopback for callbacks to lockd ... */
267 /* Just in case we somehow ignored it thus far */
268 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
270 func = (xdrproc_t) xdr_status;
272 new_status.mon_name = NL_MON_NAME(lp);
273 new_status.state = NL_STATE(lp);
274 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
276 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
278 note(N_WARNING, "notify_host: failed to notify port %d\n",
287 * Process a datagram received on the notify socket
290 process_reply(FD_SET_TYPE *rfds)
292 struct sockaddr_in sin;
296 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
299 if (!(lp = recv_rply(sockfd, &sin, &port)))
304 lp->port = htons((unsigned short) port);
305 process_entry(sockfd, lp);
306 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
307 nlist_remove(¬ify, lp);
308 nlist_insert_timer(¬ify, lp);
311 note(N_WARNING, "recv_rply: [%s] service %d not registered",
312 inet_ntoa(lp->addr), NL_MY_PROG(lp));
314 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
315 NL_MY_NAME(lp), NL_MON_NAME(lp));
317 nlist_free(¬ify, lp);
322 * Process a notify list, either for notifying remote hosts after reboot
323 * or for calling back (local) statd clients when the remote has notified
327 process_notify_list(void)
333 if ((fd = statd_get_socket()) < 0)
336 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
337 if (process_entry(fd, entry)) {
338 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
339 nlist_remove(¬ify, entry);
340 nlist_insert_timer(¬ify, entry);
343 "Can't callback %s (%d,%d), giving up.",
347 nlist_free(¬ify, entry);