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 */
46 #include "ha-callout.h"
48 #if SIZEOF_SOCKLEN_T - 0 == 0
52 #define MAXMSGSIZE (2048 / sizeof(unsigned int))
54 static unsigned long xid = 0; /* RPC XID counter */
55 static int sockfd = -1; /* notify socket */
58 * Initialize socket used to notify lockd of peer reboots.
60 * Returns the file descriptor of the new socket if successful;
61 * otherwise returns -1 and logs an error.
63 * Lockd rejects such requests if the source port is not privileged.
64 * statd_get_socket() must be invoked while statd still holds root
65 * privileges in order for the socket to acquire a privileged source
69 statd_get_socket(void)
71 struct sockaddr_in sin;
78 while (loopcnt-- > 0) {
80 if (sockfd >= 0) close(sockfd);
82 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
83 xlog(L_ERROR, "%s: Can't create socket: %m", __func__);
88 memset(&sin, 0, sizeof(sin));
89 sin.sin_family = AF_INET;
90 sin.sin_addr.s_addr = INADDR_ANY;
92 if (bindresvport(sockfd, &sin) < 0) {
93 xlog(D_GENERAL, "%s: can't bind to reserved port",
97 se = getservbyport(sin.sin_port, "udp");
100 /* rather not use that port, try again */
102 FD_SET(sockfd, &SVC_FDSET);
107 xmit_call(struct sockaddr_in *sin,
108 u_int32_t prog, u_int32_t vers, u_int32_t proc,
109 xdrproc_t func, void *obj)
110 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
112 unsigned int msgbuf[MAXMSGSIZE], msglen;
115 XDR xdr, *xdrs = &xdr;
119 xid = getpid() + time(NULL);
122 mesg.rm_direction = CALL;
123 mesg.rm_call.cb_rpcvers = 2;
124 if (sin->sin_port == 0) {
125 sin->sin_port = htons(PMAPPORT);
126 mesg.rm_call.cb_prog = PMAPPROG;
127 mesg.rm_call.cb_vers = PMAPVERS;
128 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
131 pmap.pm_prot = IPPROTO_UDP;
133 func = (xdrproc_t) xdr_pmap;
136 mesg.rm_call.cb_prog = prog;
137 mesg.rm_call.cb_vers = vers;
138 mesg.rm_call.cb_proc = proc;
140 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
141 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
142 mesg.rm_call.cb_cred.oa_length = 0;
143 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
144 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
145 mesg.rm_call.cb_verf.oa_length = 0;
147 /* Create XDR memory object for encoding */
148 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
150 /* Encode the RPC header part and payload */
151 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
152 xlog(D_GENERAL, "%s: can't encode RPC message!", __func__);
157 /* Get overall length of datagram */
158 msglen = xdr_getpos(xdrs);
160 if ((err = sendto(sockfd, msgbuf, msglen, 0,
161 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
162 xlog_warn("%s: sendto failed: %m", __func__);
163 } else if (err != msglen) {
164 xlog_warn("%s: short write: %m", __func__);
169 return err == msglen? xid : 0;
173 recv_rply(struct sockaddr_in *sin, u_long *portp)
175 unsigned int msgbuf[MAXMSGSIZE], msglen;
177 notify_list *lp = NULL;
178 XDR xdr, *xdrs = &xdr;
179 socklen_t alen = sizeof(*sin);
181 /* Receive message */
182 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
183 (struct sockaddr *) sin, &alen)) < 0) {
184 xlog_warn("%s: recvfrom failed: %m", __func__);
188 /* Create XDR object for decoding buffer */
189 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
191 memset(&mesg, 0, sizeof(mesg));
192 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
193 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
195 if (!xdr_replymsg(xdrs, &mesg)) {
196 xlog_warn("%s: can't decode RPC message!", __func__);
200 if (mesg.rm_reply.rp_stat != 0) {
201 xlog_warn("%s: [%s] RPC status %d",
203 inet_ntoa(sin->sin_addr),
204 mesg.rm_reply.rp_stat);
207 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
208 xlog_warn("%s: [%s] RPC status %d",
210 inet_ntoa(sin->sin_addr),
211 mesg.rm_reply.rp_acpt.ar_stat);
215 for (lp = notify; lp != NULL; lp = lp->next) {
216 /* LH - this was a bug... it should have been checking
217 * the xid from the response message from the client,
218 * not the static, internal xid */
219 if (lp->xid != mesg.rm_xid)
221 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
223 strncpy (addr, inet_ntoa(lp->addr),
225 addr [sizeof (addr) - 1] = '\0';
226 xlog_warn("%s: address mismatch: "
227 "expected %s, got %s", __func__,
228 addr, inet_ntoa(sin->sin_addr));
231 if (!xdr_u_long(xdrs, portp)) {
232 xlog_warn("%s: [%s] can't decode reply body!",
234 inet_ntoa(sin->sin_addr));
248 * Notify operation for a single list entry
251 process_entry(notify_list *lp)
253 struct sockaddr_in sin;
254 struct status new_status;
257 u_int32_t proc, vers, prog;
258 /* __u32 proc, vers, prog; */
260 if (NL_TIMES(lp) == 0) {
261 xlog(D_GENERAL, "%s: Cannot notify %s, giving up",
262 __func__, inet_ntoa(NL_ADDR(lp)));
266 memset(&sin, 0, sizeof(sin));
267 sin.sin_family = AF_INET;
268 sin.sin_port = lp->port;
269 /* LH - moved address into switch */
271 prog = NL_MY_PROG(lp);
272 vers = NL_MY_VERS(lp);
273 proc = NL_MY_PROC(lp);
275 /* __FORCE__ loopback for callbacks to lockd ... */
276 /* Just in case we somehow ignored it thus far */
277 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
279 func = (xdrproc_t) xdr_status;
281 new_status.mon_name = NL_MON_NAME(lp);
282 new_status.state = NL_STATE(lp);
283 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
285 lp->xid = xmit_call(&sin, prog, vers, proc, func, objp);
287 xlog_warn("%s: failed to notify port %d",
288 __func__, ntohs(lp->port));
296 * Process a datagram received on the notify socket
299 process_reply(FD_SET_TYPE *rfds)
301 struct sockaddr_in sin;
305 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
308 if (!(lp = recv_rply(&sin, &port)))
313 lp->port = htons((unsigned short) port);
315 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
316 nlist_remove(¬ify, lp);
317 nlist_insert_timer(¬ify, lp);
320 xlog_warn("%s: [%s] service %d not registered",
321 __func__, inet_ntoa(lp->addr), NL_MY_PROG(lp));
323 xlog(D_GENERAL, "%s: Callback to %s (for %d) succeeded",
324 __func__, NL_MY_NAME(lp), NL_MON_NAME(lp));
326 nlist_free(¬ify, lp);
331 * Process a notify list, either for notifying remote hosts after reboot
332 * or for calling back (local) statd clients when the remote has notified
336 process_notify_list(void)
341 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
342 if (process_entry(entry)) {
343 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
344 nlist_remove(¬ify, entry);
345 nlist_insert_timer(¬ify, entry);
348 "%s: Can't callback %s (%d,%d), giving up",
353 nlist_free(¬ify, entry);