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.
12 * After reboot, notify all hosts on our notify list. In order not to
13 * hang statd with delivery to dead hosts, we perform all RPC calls in
16 * It would have been nice to use the portmapper's rmtcall feature,
17 * but that's not possible for security reasons (the portmapper would
18 * have to forward the call with root privs for most statd's, which
19 * it won't if it's worth its money).
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
30 #include <rpc/pmap_prot.h>
31 #include <rpc/pmap_rmt.h>
40 #define MAXMSGSIZE (2048 / sizeof(unsigned int))
42 static unsigned long xid = 0; /* RPC XID counter */
43 static int sockfd = -1; /* notify socket */
46 * Initialize callback socket
51 struct sockaddr_in sin;
56 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
57 log(L_CRIT, "Can't create socket: %m");
61 FD_SET(sockfd, &SVC_FDSET);
63 memset(&sin, 0, sizeof(sin));
64 sin.sin_family = AF_INET;
65 if (bindresvport(sockfd, &sin) < 0) {
67 "process_hosts: can't bind to reserved port\n");
74 * Try to resolve host name for notify/callback request
76 * When compiled with RESTRICTED_STATD defined, we expect all
77 * host names to be dotted quads. See monitor.c for details. --okir
79 #ifdef RESTRICTED_STATD
81 try_to_resolve(notify_list *lp)
85 if (NL_TYPE(lp) == NOTIFY_REBOOT)
86 hname = NL_MON_NAME(lp);
88 hname = NL_MY_NAME(lp);
89 if (!inet_aton(hname, &(NL_ADDR(lp)))) {
90 log(L_ERROR, "%s is not an dotted-quad address", hname);
95 /* XXX: In order to handle multi-homed hosts, we could do
96 * a reverse lookup, a forward lookup, and cycle through
103 try_to_resolve(notify_list *lp)
108 if (NL_TYPE(lp) == NOTIFY_REBOOT)
109 hname = NL_MON_NAME(lp);
111 hname = NL_MY_NAME(lp);
113 dprintf(L_DEBUG, "Trying to resolve %s.", hname);
114 if (!(hp = gethostbyname(hname))) {
115 herror("gethostbyname");
120 if (hp->h_addrtype != AF_INET) {
121 log(L_ERROR, "%s is not an AF_INET address", hname);
126 /* FIXME: should try all addresses for multi-homed hosts in
127 * alternation because one interface might be down/unreachable. */
128 NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
130 dprintf(L_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
136 xmit_call(int sockfd, struct sockaddr_in *sin,
137 u_int32_t prog, u_int32_t vers, u_int32_t proc,
138 xdrproc_t func, void *obj)
139 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
141 unsigned int msgbuf[MAXMSGSIZE], msglen;
144 XDR xdr, *xdrs = &xdr;
148 xid = getpid() + time(NULL);
151 mesg.rm_direction = CALL;
152 mesg.rm_call.cb_rpcvers = 2;
153 if (sin->sin_port == 0) {
154 sin->sin_port = htons(PMAPPORT);
155 mesg.rm_call.cb_prog = PMAPPROG;
156 mesg.rm_call.cb_vers = PMAPVERS;
157 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
160 pmap.pm_prot = IPPROTO_UDP;
162 func = (xdrproc_t) xdr_pmap;
165 mesg.rm_call.cb_prog = prog;
166 mesg.rm_call.cb_vers = vers;
167 mesg.rm_call.cb_proc = proc;
169 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
170 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
171 mesg.rm_call.cb_cred.oa_length = 0;
172 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
173 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
174 mesg.rm_call.cb_verf.oa_length = 0;
176 /* Create XDR memory object for encoding */
177 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
179 /* Encode the RPC header part and payload */
180 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
181 dprintf(L_WARNING, "xmit_mesg: can't encode RPC message!\n");
186 /* Get overall length of datagram */
187 msglen = xdr_getpos(xdrs);
189 if ((err = sendto(sockfd, msgbuf, msglen, 0,
190 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
191 dprintf(L_WARNING, "xmit_mesg: sendto failed: %m");
192 } else if (err != msglen) {
193 dprintf(L_WARNING, "xmit_mesg: short write: %m\n");
198 return err == msglen? xid : 0;
202 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
204 unsigned int msgbuf[MAXMSGSIZE], msglen;
206 notify_list *lp = NULL;
207 XDR xdr, *xdrs = &xdr;
208 int alen = sizeof(*sin);
210 /* Receive message */
211 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
212 (struct sockaddr *) sin, &alen)) < 0) {
213 dprintf(L_WARNING, "recv_rply: recvfrom failed: %m");
217 /* Create XDR object for decoding buffer */
218 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
220 memset(&mesg, 0, sizeof(mesg));
221 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
222 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
224 if (!xdr_replymsg(xdrs, &mesg)) {
225 log(L_WARNING, "recv_rply: can't decode RPC message!\n");
229 if (mesg.rm_reply.rp_stat != 0) {
230 log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
231 inet_ntoa(sin->sin_addr),
232 mesg.rm_reply.rp_stat);
235 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
236 log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
237 inet_ntoa(sin->sin_addr),
238 mesg.rm_reply.rp_acpt.ar_stat);
242 for (lp = notify; lp != NULL; lp = lp->next) {
243 /* LH - this was a bug... it should have been checking
244 * the xid from the response message from the client,
245 * not the static, internal xid */
246 if (lp->xid != mesg.rm_xid)
248 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
250 strncpy (addr, inet_ntoa(lp->addr),
252 addr [sizeof (addr) - 1] = '\0';
253 dprintf(L_WARNING, "address mismatch: "
254 "expected %s, got %s\n",
255 addr, inet_ntoa(sin->sin_addr));
258 if (!xdr_u_long(xdrs, portp)) {
259 log(L_WARNING, "recv_rply: [%s] "
260 "can't decode reply body!\n",
261 inet_ntoa(sin->sin_addr));
275 * Notify operation for a single list entry
278 process_entry(int sockfd, notify_list *lp)
280 struct sockaddr_in sin;
281 struct status new_status;
284 u_int32_t proc, vers, prog;
285 /* __u32 proc, vers, prog; */
287 if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
289 if (NL_TIMES(lp) == 0) {
290 log(L_DEBUG, "Cannot notify %s, giving up.\n",
291 inet_ntoa(NL_ADDR(lp)));
295 memset(&sin, 0, sizeof(sin));
296 sin.sin_family = AF_INET;
297 sin.sin_port = lp->port;
298 sin.sin_addr = lp->addr;
300 switch (NL_TYPE(lp)) {
305 func = (xdrproc_t) xdr_stat_chge;
306 objp = &SM_stat_chge;
308 case NOTIFY_CALLBACK:
309 prog = NL_MY_PROG(lp);
310 vers = NL_MY_VERS(lp);
311 proc = NL_MY_PROC(lp);
312 func = (xdrproc_t) xdr_status;
314 new_status.mon_name = NL_MON_NAME(lp);
315 new_status.state = NL_STATE(lp);
316 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
319 log(L_ERROR, "notify_host: unknown notify type %d",
324 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
326 log(L_WARNING, "notify_host: failed to notify %s\n",
327 inet_ntoa(lp->addr));
335 * Process a datagram received on the notify socket
338 process_reply(FD_SET_TYPE *rfds)
340 struct sockaddr_in sin;
344 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
347 if (!(lp = recv_rply(sockfd, &sin, &port)))
352 lp->port = htons((unsigned short) port);
353 process_entry(sockfd, lp);
354 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
355 nlist_remove(¬ify, lp);
356 nlist_insert_timer(¬ify, lp);
359 log(L_WARNING, "recv_rply: [%s] service %d not registered",
361 NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
362 } else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
363 dprintf(L_DEBUG, "Notification of %s succeeded.",
365 xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
367 dprintf(L_DEBUG, "Callback to %s (for %d) succeeded.",
368 NL_MY_NAME(lp), NL_MON_NAME(lp));
370 nlist_free(¬ify, lp);
375 * Process a notify list, either for notifying remote hosts after reboot
376 * or for calling back (local) statd clients when the remote has notified
380 process_notify_list(void)
386 if ((fd = get_socket()) < 0)
389 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
390 if (process_entry(fd, entry)) {
391 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
392 nlist_remove(¬ify, entry);
393 nlist_insert_timer(¬ify, entry);
394 } else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
396 "Can't callback %s (%d,%d), giving up.",
400 nlist_free(¬ify, entry);
403 "Can't notify %s, giving up.",
405 xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
406 nlist_free(¬ify, entry);