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, "%s: Can't create socket: %m", __func__);
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) {
86 dprintf(N_WARNING, "%s: can't bind to reserved port",
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, "%s: can't encode RPC message!", __func__);
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, "%s: sendto failed: %m", __func__);
156 } else if (err != msglen) {
157 dprintf(N_WARNING, "%s: short write: %m", __func__);
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, "%s: recvfrom failed: %m", __func__);
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, "%s: can't decode RPC message!", __func__);
193 if (mesg.rm_reply.rp_stat != 0) {
194 note(N_WARNING, "%s: [%s] RPC status %d",
196 inet_ntoa(sin->sin_addr),
197 mesg.rm_reply.rp_stat);
200 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
201 note(N_WARNING, "%s: [%s] RPC status %d",
203 inet_ntoa(sin->sin_addr),
204 mesg.rm_reply.rp_acpt.ar_stat);
208 for (lp = notify; lp != NULL; lp = lp->next) {
209 /* LH - this was a bug... it should have been checking
210 * the xid from the response message from the client,
211 * not the static, internal xid */
212 if (lp->xid != mesg.rm_xid)
214 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
216 strncpy (addr, inet_ntoa(lp->addr),
218 addr [sizeof (addr) - 1] = '\0';
219 dprintf(N_WARNING, "%s: address mismatch: "
220 "expected %s, got %s", __func__,
221 addr, inet_ntoa(sin->sin_addr));
224 if (!xdr_u_long(xdrs, portp)) {
226 "%s: [%s] can't decode reply body!",
228 inet_ntoa(sin->sin_addr));
242 * Notify operation for a single list entry
245 process_entry(int sockfd, notify_list *lp)
247 struct sockaddr_in sin;
248 struct status new_status;
251 u_int32_t proc, vers, prog;
252 /* __u32 proc, vers, prog; */
254 if (NL_TIMES(lp) == 0) {
255 note(N_DEBUG, "%s: Cannot notify %s, giving up.",
256 __func__, inet_ntoa(NL_ADDR(lp)));
260 memset(&sin, 0, sizeof(sin));
261 sin.sin_family = AF_INET;
262 sin.sin_port = lp->port;
263 /* LH - moved address into switch */
265 prog = NL_MY_PROG(lp);
266 vers = NL_MY_VERS(lp);
267 proc = NL_MY_PROC(lp);
269 /* __FORCE__ loopback for callbacks to lockd ... */
270 /* Just in case we somehow ignored it thus far */
271 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
273 func = (xdrproc_t) xdr_status;
275 new_status.mon_name = NL_MON_NAME(lp);
276 new_status.state = NL_STATE(lp);
277 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
279 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
281 note(N_WARNING, "%s: failed to notify port %d",
282 __func__, ntohs(lp->port));
290 * Process a datagram received on the notify socket
293 process_reply(FD_SET_TYPE *rfds)
295 struct sockaddr_in sin;
299 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
302 if (!(lp = recv_rply(sockfd, &sin, &port)))
307 lp->port = htons((unsigned short) port);
308 process_entry(sockfd, lp);
309 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
310 nlist_remove(¬ify, lp);
311 nlist_insert_timer(¬ify, lp);
314 note(N_WARNING, "%s: [%s] service %d not registered",
315 __func__, inet_ntoa(lp->addr), NL_MY_PROG(lp));
317 dprintf(N_DEBUG, "%s: Callback to %s (for %d) succeeded.",
318 __func__, NL_MY_NAME(lp), NL_MON_NAME(lp));
320 nlist_free(¬ify, lp);
325 * Process a notify list, either for notifying remote hosts after reboot
326 * or for calling back (local) statd clients when the remote has notified
330 process_notify_list(void)
336 if ((fd = statd_get_socket()) < 0)
339 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
340 if (process_entry(fd, entry)) {
341 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
342 nlist_remove(¬ify, entry);
343 nlist_insert_timer(¬ify, entry);
346 "%s: Can't callback %s (%d,%d), giving up.",
351 nlist_free(¬ify, entry);