2 * Copyright (C) 1996, 1999 Olaf Kirch
3 * Modified by Jeffrey A. Uphoff, 1997-1999.
4 * Modified by H.J. Lu, 1998.
10 * After reboot, notify all hosts on our notify list. In order not to
11 * hang statd with delivery to dead hosts, we perform all RPC calls in
14 * It would have been nice to use the portmapper's rmtcall feature,
15 * but that's not possible for security reasons (the portmapper would
16 * have to forward the call with root privs for most statd's, which
17 * it won't if it's worth its money).
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include <rpc/pmap_prot.h>
29 #include <rpc/pmap_rmt.h>
38 #define MAXMSGSIZE (2048 / sizeof(unsigned int))
40 static unsigned long xid = 0; /* RPC XID counter */
41 static int sockfd = -1; /* notify socket */
44 * Initialize callback socket
49 struct sockaddr_in sin;
54 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
55 log(L_CRIT, "Can't create socket: %m");
59 FD_SET(sockfd, &SVC_FDSET);
61 memset(&sin, 0, sizeof(sin));
62 sin.sin_family = AF_INET;
63 if (bindresvport(sockfd, &sin) < 0) {
65 "process_hosts: can't bind to reserved port\n");
72 * Try to resolve host name for notify/callback request
74 * When compiled with RESTRICTED_STATD defined, we expect all
75 * host names to be dotted quads. See monitor.c for details. --okir
77 #ifdef RESTRICTED_STATD
79 try_to_resolve(notify_list *lp)
83 if (NL_TYPE(lp) == NOTIFY_REBOOT)
84 hname = NL_MON_NAME(lp);
86 hname = NL_MY_NAME(lp);
87 if (!inet_aton(hname, &(NL_ADDR(lp)))) {
88 log(L_ERROR, "%s is not an dotted-quad address", hname);
93 /* XXX: In order to handle multi-homed hosts, we could do
94 * a reverse lookup, a forward lookup, and cycle through
101 try_to_resolve(notify_list *lp)
106 if (NL_TYPE(lp) == NOTIFY_REBOOT)
107 hname = NL_MON_NAME(lp);
109 hname = NL_MY_NAME(lp);
111 dprintf(L_DEBUG, "Trying to resolve %s.", hname);
112 if (!(hp = gethostbyname(hname))) {
113 herror("gethostbyname");
118 if (hp->h_addrtype != AF_INET) {
119 log(L_ERROR, "%s is not an AF_INET address", hname);
124 /* FIXME: should try all addresses for multi-homed hosts in
125 * alternation because one interface might be down/unreachable. */
126 NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
128 dprintf(L_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
134 xmit_call(int sockfd, struct sockaddr_in *sin,
135 u_int32_t prog, u_int32_t vers, u_int32_t proc,
136 xdrproc_t func, void *obj)
137 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
139 unsigned int msgbuf[MAXMSGSIZE], msglen;
142 XDR xdr, *xdrs = &xdr;
146 xid = getpid() + time(NULL);
149 mesg.rm_direction = CALL;
150 mesg.rm_call.cb_rpcvers = 2;
151 if (sin->sin_port == 0) {
152 sin->sin_port = htons(PMAPPORT);
153 mesg.rm_call.cb_prog = PMAPPROG;
154 mesg.rm_call.cb_vers = PMAPVERS;
155 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
158 pmap.pm_prot = IPPROTO_UDP;
160 func = (xdrproc_t) xdr_pmap;
163 mesg.rm_call.cb_prog = prog;
164 mesg.rm_call.cb_vers = vers;
165 mesg.rm_call.cb_proc = proc;
167 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
168 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
169 mesg.rm_call.cb_cred.oa_length = 0;
170 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
171 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
172 mesg.rm_call.cb_verf.oa_length = 0;
174 /* Create XDR memory object for encoding */
175 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
177 /* Encode the RPC header part and payload */
178 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
179 dprintf(L_WARNING, "xmit_mesg: can't encode RPC message!\n");
184 /* Get overall length of datagram */
185 msglen = xdr_getpos(xdrs);
187 if ((err = sendto(sockfd, msgbuf, msglen, 0,
188 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
189 dprintf(L_WARNING, "xmit_mesg: sendto failed: %m");
190 } else if (err != msglen) {
191 dprintf(L_WARNING, "xmit_mesg: short write: %m\n");
196 return err == msglen? xid : 0;
200 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
202 unsigned int msgbuf[MAXMSGSIZE], msglen;
204 notify_list *lp = NULL;
205 XDR xdr, *xdrs = &xdr;
206 int alen = sizeof(*sin);
208 /* Receive message */
209 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
210 (struct sockaddr *) sin, &alen)) < 0) {
211 dprintf(L_WARNING, "recv_rply: recvfrom failed: %m");
215 /* Create XDR object for decoding buffer */
216 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
218 memset(&mesg, 0, sizeof(mesg));
219 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
220 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
222 if (!xdr_replymsg(xdrs, &mesg)) {
223 log(L_WARNING, "recv_rply: can't decode RPC message!\n");
227 if (mesg.rm_reply.rp_stat != 0) {
228 log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
229 inet_ntoa(sin->sin_addr),
230 mesg.rm_reply.rp_stat);
233 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
234 log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
235 inet_ntoa(sin->sin_addr),
236 mesg.rm_reply.rp_acpt.ar_stat);
240 for (lp = notify; lp != NULL; lp = lp->next) {
243 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
245 strncpy (addr, inet_ntoa(lp->addr),
247 addr [sizeof (addr) - 1] = '\0';
248 dprintf(L_WARNING, "address mismatch: "
249 "expected %s, got %s\n",
250 addr, inet_ntoa(sin->sin_addr));
253 if (!xdr_u_long(xdrs, portp)) {
254 log(L_WARNING, "recv_rply: [%s] "
255 "can't decode reply body!\n",
256 inet_ntoa(sin->sin_addr));
270 * Notify operation for a single list entry
273 process_entry(int sockfd, notify_list *lp)
275 struct sockaddr_in sin;
276 struct status new_status;
279 u_int32_t proc, vers, prog;
280 /* __u32 proc, vers, prog; */
282 if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
284 if (NL_TIMES(lp) == 0) {
285 log(L_DEBUG, "Cannot notify %s, giving up.\n",
286 inet_ntoa(NL_ADDR(lp)));
290 memset(&sin, 0, sizeof(sin));
291 sin.sin_family = AF_INET;
292 sin.sin_port = lp->port;
293 sin.sin_addr = lp->addr;
295 switch (NL_TYPE(lp)) {
300 func = (xdrproc_t) xdr_stat_chge;
301 objp = &SM_stat_chge;
303 case NOTIFY_CALLBACK:
304 prog = NL_MY_PROG(lp);
305 vers = NL_MY_VERS(lp);
306 proc = NL_MY_PROC(lp);
307 func = (xdrproc_t) xdr_status;
309 new_status.mon_name = NL_MON_NAME(lp);
310 new_status.state = NL_STATE(lp);
311 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
314 log(L_ERROR, "notify_host: unknown notify type %d",
319 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
321 log(L_WARNING, "notify_host: failed to notify %s\n",
322 inet_ntoa(lp->addr));
330 * Process a datagram received on the notify socket
333 process_reply(FD_SET_TYPE *rfds)
335 struct sockaddr_in sin;
339 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
342 if (!(lp = recv_rply(sockfd, &sin, &port)))
347 lp->port = htons((unsigned short) port);
348 process_entry(sockfd, lp);
349 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
350 nlist_remove(¬ify, lp);
351 nlist_insert_timer(¬ify, lp);
354 log(L_WARNING, "recv_rply: [%s] service %d not registered",
356 NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
357 } else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
358 dprintf(L_DEBUG, "Notification of %s succeeded.",
360 xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
362 dprintf(L_DEBUG, "Callback to %s (for %d) succeeded.",
363 NL_MY_NAME(lp), NL_MON_NAME(lp));
365 nlist_free(¬ify, lp);
370 * Process a notify list, either for notifying remote hosts after reboot
371 * or for calling back (local) statd clients when the remote has notified
375 process_notify_list(void)
381 if ((fd = get_socket()) < 0)
384 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
385 if (process_entry(fd, entry)) {
386 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
387 nlist_remove(¬ify, entry);
388 nlist_insert_timer(¬ify, entry);
389 } else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
391 "Can't callback %s (%d,%d), giving up.",
395 nlist_free(¬ify, entry);
398 "Can't notify %s, giving up.",
400 xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
401 nlist_free(¬ify, entry);