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>
41 #define MAXMSGSIZE (2048 / sizeof(unsigned int))
43 static unsigned long xid = 0; /* RPC XID counter */
44 static int sockfd = -1; /* notify socket */
47 * Initialize callback socket
52 struct sockaddr_in sin;
57 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
58 log(L_CRIT, "Can't create socket: %m");
62 FD_SET(sockfd, &SVC_FDSET);
64 memset(&sin, 0, sizeof(sin));
65 sin.sin_family = AF_INET;
66 if (bindresvport(sockfd, &sin) < 0) {
68 "process_hosts: can't bind to reserved port\n");
75 * Try to resolve host name for notify/callback request
77 * When compiled with RESTRICTED_STATD defined, we expect all
78 * host names to be dotted quads. See monitor.c for details. --okir
80 #ifdef RESTRICTED_STATD
82 try_to_resolve(notify_list *lp)
86 if (NL_TYPE(lp) == NOTIFY_REBOOT)
87 hname = NL_MON_NAME(lp);
89 hname = NL_MY_NAME(lp);
90 if (!inet_aton(hname, &(NL_ADDR(lp)))) {
91 log(L_ERROR, "%s is not an dotted-quad address", hname);
96 /* XXX: In order to handle multi-homed hosts, we could do
97 * a reverse lookup, a forward lookup, and cycle through
104 try_to_resolve(notify_list *lp)
109 if (NL_TYPE(lp) == NOTIFY_REBOOT)
110 hname = NL_MON_NAME(lp);
112 hname = NL_MY_NAME(lp);
114 dprintf(L_DEBUG, "Trying to resolve %s.", hname);
115 if (!(hp = gethostbyname(hname))) {
116 herror("gethostbyname");
121 if (hp->h_addrtype != AF_INET) {
122 log(L_ERROR, "%s is not an AF_INET address", hname);
127 /* FIXME: should try all addresses for multi-homed hosts in
128 * alternation because one interface might be down/unreachable. */
129 NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
131 dprintf(L_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
137 xmit_call(int sockfd, struct sockaddr_in *sin,
138 u_int32_t prog, u_int32_t vers, u_int32_t proc,
139 xdrproc_t func, void *obj)
140 /* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
142 unsigned int msgbuf[MAXMSGSIZE], msglen;
145 XDR xdr, *xdrs = &xdr;
149 xid = getpid() + time(NULL);
152 mesg.rm_direction = CALL;
153 mesg.rm_call.cb_rpcvers = 2;
154 if (sin->sin_port == 0) {
155 sin->sin_port = htons(PMAPPORT);
156 mesg.rm_call.cb_prog = PMAPPROG;
157 mesg.rm_call.cb_vers = PMAPVERS;
158 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
161 pmap.pm_prot = IPPROTO_UDP;
163 func = (xdrproc_t) xdr_pmap;
166 mesg.rm_call.cb_prog = prog;
167 mesg.rm_call.cb_vers = vers;
168 mesg.rm_call.cb_proc = proc;
170 mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
171 mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
172 mesg.rm_call.cb_cred.oa_length = 0;
173 mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
174 mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
175 mesg.rm_call.cb_verf.oa_length = 0;
177 /* Create XDR memory object for encoding */
178 xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
180 /* Encode the RPC header part and payload */
181 if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
182 dprintf(L_WARNING, "xmit_mesg: can't encode RPC message!\n");
187 /* Get overall length of datagram */
188 msglen = xdr_getpos(xdrs);
190 if ((err = sendto(sockfd, msgbuf, msglen, 0,
191 (struct sockaddr *) sin, sizeof(*sin))) < 0) {
192 dprintf(L_WARNING, "xmit_mesg: sendto failed: %m");
193 } else if (err != msglen) {
194 dprintf(L_WARNING, "xmit_mesg: short write: %m\n");
199 return err == msglen? xid : 0;
203 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
205 unsigned int msgbuf[MAXMSGSIZE], msglen;
207 notify_list *lp = NULL;
208 XDR xdr, *xdrs = &xdr;
209 int alen = sizeof(*sin);
211 /* Receive message */
212 if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
213 (struct sockaddr *) sin, &alen)) < 0) {
214 dprintf(L_WARNING, "recv_rply: recvfrom failed: %m");
218 /* Create XDR object for decoding buffer */
219 xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
221 memset(&mesg, 0, sizeof(mesg));
222 mesg.rm_reply.rp_acpt.ar_results.where = NULL;
223 mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
225 if (!xdr_replymsg(xdrs, &mesg)) {
226 log(L_WARNING, "recv_rply: can't decode RPC message!\n");
230 if (mesg.rm_reply.rp_stat != 0) {
231 log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
232 inet_ntoa(sin->sin_addr),
233 mesg.rm_reply.rp_stat);
236 if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
237 log(L_WARNING, "recv_rply: [%s] RPC status %d\n",
238 inet_ntoa(sin->sin_addr),
239 mesg.rm_reply.rp_acpt.ar_stat);
243 for (lp = notify; lp != NULL; lp = lp->next) {
244 /* LH - this was a bug... it should have been checking
245 * the xid from the response message from the client,
246 * not the static, internal xid */
247 if (lp->xid != mesg.rm_xid)
249 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
251 strncpy (addr, inet_ntoa(lp->addr),
253 addr [sizeof (addr) - 1] = '\0';
254 dprintf(L_WARNING, "address mismatch: "
255 "expected %s, got %s\n",
256 addr, inet_ntoa(sin->sin_addr));
259 if (!xdr_u_long(xdrs, portp)) {
260 log(L_WARNING, "recv_rply: [%s] "
261 "can't decode reply body!\n",
262 inet_ntoa(sin->sin_addr));
276 * Notify operation for a single list entry
279 process_entry(int sockfd, notify_list *lp)
281 struct sockaddr_in sin;
282 struct status new_status;
285 u_int32_t proc, vers, prog;
286 /* __u32 proc, vers, prog; */
288 if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
290 if (NL_TIMES(lp) == 0) {
291 log(L_DEBUG, "Cannot notify %s, giving up.\n",
292 inet_ntoa(NL_ADDR(lp)));
296 memset(&sin, 0, sizeof(sin));
297 sin.sin_family = AF_INET;
298 sin.sin_port = lp->port;
299 /* LH - moved address into switch */
301 switch (NL_TYPE(lp)) {
307 /* Use source address for notify replies */
308 sin.sin_addr = lp->addr;
310 func = (xdrproc_t) xdr_stat_chge;
311 objp = &SM_stat_chge;
313 case NOTIFY_CALLBACK:
314 prog = NL_MY_PROG(lp);
315 vers = NL_MY_VERS(lp);
316 proc = NL_MY_PROC(lp);
318 /* __FORCE__ loopback for callbacks to lockd ... */
319 /* Just in case we somehow ignored it thus far */
320 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
322 func = (xdrproc_t) xdr_status;
324 new_status.mon_name = NL_MON_NAME(lp);
325 new_status.state = NL_STATE(lp);
326 memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
329 log(L_ERROR, "notify_host: unknown notify type %d",
334 lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
336 log(L_WARNING, "notify_host: failed to notify %s\n",
337 inet_ntoa(lp->addr));
345 * Process a datagram received on the notify socket
348 process_reply(FD_SET_TYPE *rfds)
350 struct sockaddr_in sin;
354 if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
357 if (!(lp = recv_rply(sockfd, &sin, &port)))
362 lp->port = htons((unsigned short) port);
363 process_entry(sockfd, lp);
364 NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
365 nlist_remove(¬ify, lp);
366 nlist_insert_timer(¬ify, lp);
369 log(L_WARNING, "recv_rply: [%s] service %d not registered",
371 NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
372 } else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
373 dprintf(L_DEBUG, "Notification of %s succeeded.",
375 xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
377 dprintf(L_DEBUG, "Callback to %s (for %d) succeeded.",
378 NL_MY_NAME(lp), NL_MON_NAME(lp));
380 nlist_free(¬ify, lp);
385 * Process a notify list, either for notifying remote hosts after reboot
386 * or for calling back (local) statd clients when the remote has notified
390 process_notify_list(void)
396 if ((fd = get_socket()) < 0)
399 while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
400 if (process_entry(fd, entry)) {
401 NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
402 nlist_remove(¬ify, entry);
403 nlist_insert_timer(¬ify, entry);
404 } else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
406 "Can't callback %s (%d,%d), giving up.",
410 nlist_free(¬ify, entry);
413 "Can't notify %s, giving up.",
415 xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
416 nlist_free(¬ify, entry);