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).
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
31 #include <rpc/pmap_prot.h>
32 #include <rpc/pmap_rmt.h>
42 #define MAXMSGSIZE (2048 / sizeof(unsigned int))
44 static unsigned long xid = 0; /* RPC XID counter */
45 static int sockfd = -1; /* notify socket */
48 * Initialize callback socket
53 struct sockaddr_in sin;
58 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
59 log(L_CRIT, "Can't create socket: %m");
63 FD_SET(sockfd, &SVC_FDSET);
65 memset(&sin, 0, sizeof(sin));
66 sin.sin_family = AF_INET;
67 if (bindresvport(sockfd, &sin) < 0) {
69 "process_hosts: can't bind to reserved port\n");
76 * Try to resolve host name for notify/callback request
78 * When compiled with RESTRICTED_STATD defined, we expect all
79 * host names to be dotted quads. See monitor.c for details. --okir
81 #ifdef RESTRICTED_STATD
83 try_to_resolve(notify_list *lp)
87 if (NL_TYPE(lp) == NOTIFY_REBOOT)
88 hname = NL_MON_NAME(lp);
90 hname = NL_MY_NAME(lp);
91 if (!inet_aton(hname, &(NL_ADDR(lp)))) {
92 log(L_ERROR, "%s is not an dotted-quad address", hname);
97 /* XXX: In order to handle multi-homed hosts, we could do
98 * a reverse lookup, a forward lookup, and cycle through
105 try_to_resolve(notify_list *lp)
110 if (NL_TYPE(lp) == NOTIFY_REBOOT)
111 hname = NL_MON_NAME(lp);
113 hname = NL_MY_NAME(lp);
115 dprintf(L_DEBUG, "Trying to resolve %s.", hname);
116 if (!(hp = gethostbyname(hname))) {
117 herror("gethostbyname");
122 if (hp->h_addrtype != AF_INET) {
123 log(L_ERROR, "%s is not an AF_INET address", hname);
128 /* FIXME: should try all addresses for multi-homed hosts in
129 * alternation because one interface might be down/unreachable. */
130 NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
132 dprintf(L_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
138 xmit_call(int sockfd, struct sockaddr_in *sin,
139 u_int32_t prog, u_int32_t vers, u_int32_t proc,
140 xdrproc_t func, void *obj)
141 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
143 unsigned int msgbuf[MAXMSGSIZE], msglen;
146 XDR xdr, *xdrs = &xdr;
150 xid = getpid() + time(NULL);
153 mesg.rm_direction = CALL;
154 mesg.rm_call.cb_rpcvers = 2;
155 if (sin->sin_port == 0) {
156 sin->sin_port = htons(PMAPPORT);
157 mesg.rm_call.cb_prog = PMAPPROG;
158 mesg.rm_call.cb_vers = PMAPVERS;
159 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
162 pmap.pm_prot = IPPROTO_UDP;
164 func = (xdrproc_t) xdr_pmap;
167 mesg.rm_call.cb_prog = prog;
168 mesg.rm_call.cb_vers = vers;
169 mesg.rm_call.cb_proc = proc;
171 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
172 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
173 mesg.rm_call.cb_cred.oa_length = 0;
174 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
175 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
176 mesg.rm_call.cb_verf.oa_length = 0;
178 /* Create XDR memory object for encoding */
179 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
181 /* Encode the RPC header part and payload */
182 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
183 dprintf(L_WARNING, "xmit_mesg: can't encode RPC message!\n");
188 /* Get overall length of datagram */
189 msglen = xdr_getpos(xdrs);
191 if ((err = sendto(sockfd, msgbuf, msglen, 0,
192 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
193 dprintf(L_WARNING, "xmit_mesg: sendto failed: %m");
194 } else if (err != msglen) {
195 dprintf(L_WARNING, "xmit_mesg: short write: %m\n");
200 return err == msglen? xid : 0;
204 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
206 unsigned int msgbuf[MAXMSGSIZE], msglen;
208 notify_list *lp = NULL;
209 XDR xdr, *xdrs = &xdr;
210 int alen = sizeof(*sin);
212 /* Receive message */
213 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
214 (struct sockaddr *) sin, &alen)) < 0) {
215 dprintf(L_WARNING, "recv_rply: recvfrom failed: %m");
219 /* Create XDR object for decoding buffer */
220 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
222 memset(&mesg, 0, sizeof(mesg));
223 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
224 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
226 if (!xdr_replymsg(xdrs, &mesg)) {
227 log(L_WARNING, "recv_rply: can't decode RPC message!\n");
231 if (mesg.rm_reply.rp_stat != 0) {
232 log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
233 inet_ntoa(sin->sin_addr),
234 mesg.rm_reply.rp_stat);
237 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
238 log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
239 inet_ntoa(sin->sin_addr),
240 mesg.rm_reply.rp_acpt.ar_stat);
244 for (lp = notify; lp != NULL; lp = lp->next) {
245 /* LH - this was a bug... it should have been checking
246 * the xid from the response message from the client,
247 * not the static, internal xid */
248 if (lp->xid != mesg.rm_xid)
250 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
252 strncpy (addr, inet_ntoa(lp->addr),
254 addr [sizeof (addr) - 1] = '\0';
255 dprintf(L_WARNING, "address mismatch: "
256 "expected %s, got %s\n",
257 addr, inet_ntoa(sin->sin_addr));
260 if (!xdr_u_long(xdrs, portp)) {
261 log(L_WARNING, "recv_rply: [%s] "
262 "can't decode reply body!\n",
263 inet_ntoa(sin->sin_addr));
277 * Notify operation for a single list entry
280 process_entry(int sockfd, notify_list *lp)
282 struct sockaddr_in sin;
283 struct status new_status;
286 u_int32_t proc, vers, prog;
287 /* __u32 proc, vers, prog; */
289 if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
291 if (NL_TIMES(lp) == 0) {
292 log(L_DEBUG, "Cannot notify %s, giving up.\n",
293 inet_ntoa(NL_ADDR(lp)));
297 memset(&sin, 0, sizeof(sin));
298 sin.sin_family = AF_INET;
299 sin.sin_port = lp->port;
300 /* LH - moved address into switch */
302 switch (NL_TYPE(lp)) {
308 /* Use source address for notify replies */
309 sin.sin_addr = lp->addr;
311 func = (xdrproc_t) xdr_stat_chge;
312 objp = &SM_stat_chge;
314 case NOTIFY_CALLBACK:
315 prog = NL_MY_PROG(lp);
316 vers = NL_MY_VERS(lp);
317 proc = NL_MY_PROC(lp);
319 /* __FORCE__ loopback for callbacks to lockd ... */
320 /* Just in case we somehow ignored it thus far */
321 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
323 func = (xdrproc_t) xdr_status;
325 new_status.mon_name = NL_MON_NAME(lp);
326 new_status.state = NL_STATE(lp);
327 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
330 log(L_ERROR, "notify_host: unknown notify type %d",
335 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
337 log(L_WARNING, "notify_host: failed to notify %s\n",
338 inet_ntoa(lp->addr));
346 * Process a datagram received on the notify socket
349 process_reply(FD_SET_TYPE *rfds)
351 struct sockaddr_in sin;
355 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
358 if (!(lp = recv_rply(sockfd, &sin, &port)))
363 lp->port = htons((unsigned short) port);
364 process_entry(sockfd, lp);
365 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
366 nlist_remove(¬ify, lp);
367 nlist_insert_timer(¬ify, lp);
370 log(L_WARNING, "recv_rply: [%s] service %d not registered",
372 NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
373 } else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
374 dprintf(L_DEBUG, "Notification of %s succeeded.",
376 xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
378 dprintf(L_DEBUG, "Callback to %s (for %d) succeeded.",
379 NL_MY_NAME(lp), NL_MON_NAME(lp));
381 nlist_free(¬ify, lp);
386 * Process a notify list, either for notifying remote hosts after reboot
387 * or for calling back (local) statd clients when the remote has notified
391 process_notify_list(void)
397 if ((fd = get_socket()) < 0)
400 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
401 if (process_entry(fd, entry)) {
402 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
403 nlist_remove(¬ify, entry);
404 nlist_insert_timer(¬ify, entry);
405 } else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
407 "Can't callback %s (%d,%d), giving up.",
411 nlist_free(¬ify, entry);
414 "Can't notify %s, giving up.",
416 xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
417 nlist_free(¬ify, entry);