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 xmit_call(int sockfd, struct sockaddr_in *sin,
89 u_int32_t prog, u_int32_t vers, u_int32_t proc,
90 xdrproc_t func, void *obj)
91 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
93 unsigned int msgbuf[MAXMSGSIZE], msglen;
96 XDR xdr, *xdrs = &xdr;
100 xid = getpid() + time(NULL);
103 mesg.rm_direction = CALL;
104 mesg.rm_call.cb_rpcvers = 2;
105 if (sin->sin_port == 0) {
106 sin->sin_port = htons(PMAPPORT);
107 mesg.rm_call.cb_prog = PMAPPROG;
108 mesg.rm_call.cb_vers = PMAPVERS;
109 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
112 pmap.pm_prot = IPPROTO_UDP;
114 func = (xdrproc_t) xdr_pmap;
117 mesg.rm_call.cb_prog = prog;
118 mesg.rm_call.cb_vers = vers;
119 mesg.rm_call.cb_proc = proc;
121 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
122 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
123 mesg.rm_call.cb_cred.oa_length = 0;
124 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
125 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
126 mesg.rm_call.cb_verf.oa_length = 0;
128 /* Create XDR memory object for encoding */
129 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
131 /* Encode the RPC header part and payload */
132 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
133 dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
138 /* Get overall length of datagram */
139 msglen = xdr_getpos(xdrs);
141 if ((err = sendto(sockfd, msgbuf, msglen, 0,
142 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
143 dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
144 } else if (err != msglen) {
145 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
150 return err == msglen? xid : 0;
154 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
156 unsigned int msgbuf[MAXMSGSIZE], msglen;
158 notify_list *lp = NULL;
159 XDR xdr, *xdrs = &xdr;
160 socklen_t alen = sizeof(*sin);
162 /* Receive message */
163 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
164 (struct sockaddr *) sin, &alen)) < 0) {
165 dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
169 /* Create XDR object for decoding buffer */
170 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
172 memset(&mesg, 0, sizeof(mesg));
173 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
174 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
176 if (!xdr_replymsg(xdrs, &mesg)) {
177 note(N_WARNING, "recv_rply: can't decode RPC message!\n");
181 if (mesg.rm_reply.rp_stat != 0) {
182 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
183 inet_ntoa(sin->sin_addr),
184 mesg.rm_reply.rp_stat);
187 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
188 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
189 inet_ntoa(sin->sin_addr),
190 mesg.rm_reply.rp_acpt.ar_stat);
194 for (lp = notify; lp != NULL; lp = lp->next) {
195 /* LH - this was a bug... it should have been checking
196 * the xid from the response message from the client,
197 * not the static, internal xid */
198 if (lp->xid != mesg.rm_xid)
200 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
202 strncpy (addr, inet_ntoa(lp->addr),
204 addr [sizeof (addr) - 1] = '\0';
205 dprintf(N_WARNING, "address mismatch: "
206 "expected %s, got %s\n",
207 addr, inet_ntoa(sin->sin_addr));
210 if (!xdr_u_long(xdrs, portp)) {
211 note(N_WARNING, "recv_rply: [%s] "
212 "can't decode reply body!\n",
213 inet_ntoa(sin->sin_addr));
227 * Notify operation for a single list entry
230 process_entry(int sockfd, notify_list *lp)
232 struct sockaddr_in sin;
233 struct status new_status;
236 u_int32_t proc, vers, prog;
237 /* __u32 proc, vers, prog; */
239 if (NL_TIMES(lp) == 0) {
240 note(N_DEBUG, "Cannot notify %s, giving up.\n",
241 inet_ntoa(NL_ADDR(lp)));
245 memset(&sin, 0, sizeof(sin));
246 sin.sin_family = AF_INET;
247 sin.sin_port = lp->port;
248 /* LH - moved address into switch */
250 prog = NL_MY_PROG(lp);
251 vers = NL_MY_VERS(lp);
252 proc = NL_MY_PROC(lp);
254 /* __FORCE__ loopback for callbacks to lockd ... */
255 /* Just in case we somehow ignored it thus far */
256 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
258 func = (xdrproc_t) xdr_status;
260 new_status.mon_name = NL_MON_NAME(lp);
261 new_status.state = NL_STATE(lp);
262 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
264 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
266 note(N_WARNING, "notify_host: failed to notify port %d\n",
275 * Process a datagram received on the notify socket
278 process_reply(FD_SET_TYPE *rfds)
280 struct sockaddr_in sin;
284 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
287 if (!(lp = recv_rply(sockfd, &sin, &port)))
292 lp->port = htons((unsigned short) port);
293 process_entry(sockfd, lp);
294 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
295 nlist_remove(¬ify, lp);
296 nlist_insert_timer(¬ify, lp);
299 note(N_WARNING, "recv_rply: [%s] service %d not registered",
300 inet_ntoa(lp->addr), NL_MY_PROG(lp));
302 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
303 NL_MY_NAME(lp), NL_MON_NAME(lp));
305 nlist_free(¬ify, lp);
310 * Process a notify list, either for notifying remote hosts after reboot
311 * or for calling back (local) statd clients when the remote has notified
315 process_notify_list(void)
321 if ((fd = statd_get_socket()) < 0)
324 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
325 if (process_entry(fd, entry)) {
326 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
327 nlist_remove(¬ify, entry);
328 nlist_insert_timer(¬ify, entry);
331 "Can't callback %s (%d,%d), giving up.",
335 nlist_free(¬ify, entry);