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 socket used to notify lockd of peer reboots.
61 * Returns the file descriptor of the new socket if successful;
62 * otherwise returns -1 and logs an error.
64 * Lockd rejects such requests if the source port is not privileged.
65 * statd_get_socket() must be invoked while statd still holds root
66 * privileges in order for the socket to acquire a privileged source
70 statd_get_socket(void)
72 struct sockaddr_in sin;
79 while (loopcnt-- > 0) {
81 if (sockfd >= 0) close(sockfd);
83 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
84 note(N_CRIT, "%s: Can't create socket: %m", __func__);
89 memset(&sin, 0, sizeof(sin));
90 sin.sin_family = AF_INET;
91 sin.sin_addr.s_addr = INADDR_ANY;
93 if (bindresvport(sockfd, &sin) < 0) {
94 dprintf(N_WARNING, "%s: can't bind to reserved port",
98 se = getservbyport(sin.sin_port, "udp");
101 /* rather not use that port, try again */
103 FD_SET(sockfd, &SVC_FDSET);
108 xmit_call(struct sockaddr_in *sin,
109 u_int32_t prog, u_int32_t vers, u_int32_t proc,
110 xdrproc_t func, void *obj)
111 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
113 unsigned int msgbuf[MAXMSGSIZE], msglen;
116 XDR xdr, *xdrs = &xdr;
120 xid = getpid() + time(NULL);
123 mesg.rm_direction = CALL;
124 mesg.rm_call.cb_rpcvers = 2;
125 if (sin->sin_port == 0) {
126 sin->sin_port = htons(PMAPPORT);
127 mesg.rm_call.cb_prog = PMAPPROG;
128 mesg.rm_call.cb_vers = PMAPVERS;
129 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
132 pmap.pm_prot = IPPROTO_UDP;
134 func = (xdrproc_t) xdr_pmap;
137 mesg.rm_call.cb_prog = prog;
138 mesg.rm_call.cb_vers = vers;
139 mesg.rm_call.cb_proc = proc;
141 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
142 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
143 mesg.rm_call.cb_cred.oa_length = 0;
144 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
145 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
146 mesg.rm_call.cb_verf.oa_length = 0;
148 /* Create XDR memory object for encoding */
149 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
151 /* Encode the RPC header part and payload */
152 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
153 dprintf(N_WARNING, "%s: can't encode RPC message!", __func__);
158 /* Get overall length of datagram */
159 msglen = xdr_getpos(xdrs);
161 if ((err = sendto(sockfd, msgbuf, msglen, 0,
162 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
163 dprintf(N_WARNING, "%s: sendto failed: %m", __func__);
164 } else if (err != msglen) {
165 dprintf(N_WARNING, "%s: short write: %m", __func__);
170 return err == msglen? xid : 0;
174 recv_rply(struct sockaddr_in *sin, u_long *portp)
176 unsigned int msgbuf[MAXMSGSIZE], msglen;
178 notify_list *lp = NULL;
179 XDR xdr, *xdrs = &xdr;
180 socklen_t alen = sizeof(*sin);
182 /* Receive message */
183 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
184 (struct sockaddr *) sin, &alen)) < 0) {
185 dprintf(N_WARNING, "%s: recvfrom failed: %m", __func__);
189 /* Create XDR object for decoding buffer */
190 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
192 memset(&mesg, 0, sizeof(mesg));
193 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
194 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
196 if (!xdr_replymsg(xdrs, &mesg)) {
197 note(N_WARNING, "%s: can't decode RPC message!", __func__);
201 if (mesg.rm_reply.rp_stat != 0) {
202 note(N_WARNING, "%s: [%s] RPC status %d",
204 inet_ntoa(sin->sin_addr),
205 mesg.rm_reply.rp_stat);
208 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
209 note(N_WARNING, "%s: [%s] RPC status %d",
211 inet_ntoa(sin->sin_addr),
212 mesg.rm_reply.rp_acpt.ar_stat);
216 for (lp = notify; lp != NULL; lp = lp->next) {
217 /* LH - this was a bug... it should have been checking
218 * the xid from the response message from the client,
219 * not the static, internal xid */
220 if (lp->xid != mesg.rm_xid)
222 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
224 strncpy (addr, inet_ntoa(lp->addr),
226 addr [sizeof (addr) - 1] = '\0';
227 dprintf(N_WARNING, "%s: address mismatch: "
228 "expected %s, got %s", __func__,
229 addr, inet_ntoa(sin->sin_addr));
232 if (!xdr_u_long(xdrs, portp)) {
234 "%s: [%s] can't decode reply body!",
236 inet_ntoa(sin->sin_addr));
250 * Notify operation for a single list entry
253 process_entry(notify_list *lp)
255 struct sockaddr_in sin;
256 struct status new_status;
259 u_int32_t proc, vers, prog;
260 /* __u32 proc, vers, prog; */
262 if (NL_TIMES(lp) == 0) {
263 note(N_DEBUG, "%s: Cannot notify %s, giving up.",
264 __func__, inet_ntoa(NL_ADDR(lp)));
268 memset(&sin, 0, sizeof(sin));
269 sin.sin_family = AF_INET;
270 sin.sin_port = lp->port;
271 /* LH - moved address into switch */
273 prog = NL_MY_PROG(lp);
274 vers = NL_MY_VERS(lp);
275 proc = NL_MY_PROC(lp);
277 /* __FORCE__ loopback for callbacks to lockd ... */
278 /* Just in case we somehow ignored it thus far */
279 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
281 func = (xdrproc_t) xdr_status;
283 new_status.mon_name = NL_MON_NAME(lp);
284 new_status.state = NL_STATE(lp);
285 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
287 lp->xid = xmit_call(&sin, prog, vers, proc, func, objp);
289 note(N_WARNING, "%s: failed to notify port %d",
290 __func__, ntohs(lp->port));
298 * Process a datagram received on the notify socket
301 process_reply(FD_SET_TYPE *rfds)
303 struct sockaddr_in sin;
307 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
310 if (!(lp = recv_rply(&sin, &port)))
315 lp->port = htons((unsigned short) port);
317 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
318 nlist_remove(¬ify, lp);
319 nlist_insert_timer(¬ify, lp);
322 note(N_WARNING, "%s: [%s] service %d not registered",
323 __func__, inet_ntoa(lp->addr), NL_MY_PROG(lp));
325 dprintf(N_DEBUG, "%s: Callback to %s (for %d) succeeded.",
326 __func__, NL_MY_NAME(lp), NL_MON_NAME(lp));
328 nlist_free(¬ify, lp);
333 * Process a notify list, either for notifying remote hosts after reboot
334 * or for calling back (local) statd clients when the remote has notified
338 process_notify_list(void)
343 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
344 if (process_entry(entry)) {
345 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
346 nlist_remove(¬ify, entry);
347 nlist_insert_timer(¬ify, entry);
350 "%s: Can't callback %s (%d,%d), giving up.",
355 nlist_free(¬ify, entry);