]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/rmtcall.c
rpc.statd: Use __func__ in dprintf
[nfs-utils.git] / utils / statd / rmtcall.c
1 /*
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
8  *
9  * NSM for Linux.
10  */
11
12 /*
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
15  * parallel.
16  *
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).
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/time.h>
30 #include <netinet/in.h>
31 #include <net/if.h>
32 #include <arpa/inet.h>
33 #include <rpc/rpc.h>
34 #include <rpc/pmap_prot.h>
35 #include <rpc/pmap_rmt.h>
36 #include <time.h>
37 #include <netdb.h>
38 #include <string.h>
39 #include <unistd.h>
40 #ifdef HAVE_IFADDRS_H
41 #include <ifaddrs.h>
42 #endif /* HAVE_IFADDRS_H */
43 #include "sm_inter.h"
44 #include "statd.h"
45 #include "notlist.h"
46 #include "log.h"
47 #include "ha-callout.h"
48
49 #if SIZEOF_SOCKLEN_T - 0 == 0
50 #define socklen_t int
51 #endif
52
53 #define MAXMSGSIZE      (2048 / sizeof(unsigned int))
54
55 static unsigned long    xid = 0;        /* RPC XID counter */
56 static int              sockfd = -1;    /* notify socket */
57
58 /*
59  * Initialize callback socket
60  */
61 int
62 statd_get_socket(void)
63 {
64         struct sockaddr_in      sin;
65         struct servent *se;
66         int loopcnt = 100;
67
68         if (sockfd >= 0)
69                 return sockfd;
70
71         while (loopcnt-- > 0) {
72
73                 if (sockfd >= 0) close(sockfd);
74
75                 if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
76                         note(N_CRIT, "%s: Can't create socket: %m", __func__);
77                         return -1;
78                 }
79
80
81                 memset(&sin, 0, sizeof(sin));
82                 sin.sin_family = AF_INET;
83                 sin.sin_addr.s_addr = INADDR_ANY;
84
85                 if (bindresvport(sockfd, &sin) < 0) {
86                         dprintf(N_WARNING, "%s: can't bind to reserved port",
87                                         __func__);
88                         break;
89                 }
90                 se = getservbyport(sin.sin_port, "udp");
91                 if (se == NULL)
92                         break;
93                 /* rather not use that port, try again */
94         }
95         FD_SET(sockfd, &SVC_FDSET);
96         return sockfd;
97 }
98
99 static unsigned long
100 xmit_call(int sockfd, struct sockaddr_in *sin,
101           u_int32_t prog, u_int32_t vers, u_int32_t proc,
102           xdrproc_t func, void *obj)
103 /*              __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
104 {
105         unsigned int            msgbuf[MAXMSGSIZE], msglen;
106         struct rpc_msg          mesg;
107         struct pmap             pmap;
108         XDR                     xdr, *xdrs = &xdr;
109         int                     err;
110
111         if (!xid)
112                 xid = getpid() + time(NULL);
113
114         mesg.rm_xid = ++xid;
115         mesg.rm_direction = CALL;
116         mesg.rm_call.cb_rpcvers = 2;
117         if (sin->sin_port == 0) {
118                 sin->sin_port = htons(PMAPPORT);
119                 mesg.rm_call.cb_prog = PMAPPROG;
120                 mesg.rm_call.cb_vers = PMAPVERS;
121                 mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
122                 pmap.pm_prog = prog;
123                 pmap.pm_vers = vers;
124                 pmap.pm_prot = IPPROTO_UDP;
125                 pmap.pm_port = 0;
126                 func = (xdrproc_t) xdr_pmap;
127                 obj  = &pmap;
128         } else {
129                 mesg.rm_call.cb_prog = prog;
130                 mesg.rm_call.cb_vers = vers;
131                 mesg.rm_call.cb_proc = proc;
132         }
133         mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
134         mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
135         mesg.rm_call.cb_cred.oa_length = 0;
136         mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
137         mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
138         mesg.rm_call.cb_verf.oa_length = 0;
139
140         /* Create XDR memory object for encoding */
141         xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
142
143         /* Encode the RPC header part and payload */
144         if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
145                 dprintf(N_WARNING, "%s: can't encode RPC message!", __func__);
146                 xdr_destroy(xdrs);
147                 return 0;
148         }
149
150         /* Get overall length of datagram */
151         msglen = xdr_getpos(xdrs);
152
153         if ((err = sendto(sockfd, msgbuf, msglen, 0,
154                         (struct sockaddr *) sin, sizeof(*sin))) < 0) {
155                 dprintf(N_WARNING, "%s: sendto failed: %m", __func__);
156         } else if (err != msglen) {
157                 dprintf(N_WARNING, "%s: short write: %m", __func__);
158         }
159
160         xdr_destroy(xdrs);
161
162         return err == msglen? xid : 0;
163 }
164
165 static notify_list *
166 recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
167 {
168         unsigned int            msgbuf[MAXMSGSIZE], msglen;
169         struct rpc_msg          mesg;
170         notify_list             *lp = NULL;
171         XDR                     xdr, *xdrs = &xdr;
172         socklen_t               alen = sizeof(*sin);
173
174         /* Receive message */
175         if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
176                         (struct sockaddr *) sin, &alen)) < 0) {
177                 dprintf(N_WARNING, "%s: recvfrom failed: %m", __func__);
178                 return NULL;
179         }
180
181         /* Create XDR object for decoding buffer */
182         xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
183
184         memset(&mesg, 0, sizeof(mesg));
185         mesg.rm_reply.rp_acpt.ar_results.where = NULL;
186         mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
187
188         if (!xdr_replymsg(xdrs, &mesg)) {
189                 note(N_WARNING, "%s: can't decode RPC message!", __func__);
190                 goto done;
191         }
192
193         if (mesg.rm_reply.rp_stat != 0) {
194                 note(N_WARNING, "%s: [%s] RPC status %d", 
195                                 __func__,
196                                 inet_ntoa(sin->sin_addr),
197                                 mesg.rm_reply.rp_stat);
198                 goto done;
199         }
200         if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
201                 note(N_WARNING, "%s: [%s] RPC status %d",
202                                 __func__,
203                                 inet_ntoa(sin->sin_addr),
204                                 mesg.rm_reply.rp_acpt.ar_stat);
205                 goto done;
206         }
207
208         for (lp = notify; lp != NULL; lp = lp->next) {
209                 /* LH - this was a bug... it should have been checking
210                  * the xid from the response message from the client,
211                  * not the static, internal xid */
212                 if (lp->xid != mesg.rm_xid)
213                         continue;
214                 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
215                         char addr [18];
216                         strncpy (addr, inet_ntoa(lp->addr),
217                                  sizeof (addr) - 1);
218                         addr [sizeof (addr) - 1] = '\0';
219                         dprintf(N_WARNING, "%s: address mismatch: "
220                                 "expected %s, got %s", __func__,
221                                 addr, inet_ntoa(sin->sin_addr));
222                 }
223                 if (lp->port == 0) {
224                         if (!xdr_u_long(xdrs, portp)) {
225                                 note(N_WARNING,
226                                         "%s: [%s] can't decode reply body!",
227                                         __func__,
228                                         inet_ntoa(sin->sin_addr));
229                                 lp = NULL;
230                                 goto done;
231                         }
232                 }
233                 break;
234         }
235
236 done:
237         xdr_destroy(xdrs);
238         return lp;
239 }
240
241 /*
242  * Notify operation for a single list entry
243  */
244 static int
245 process_entry(int sockfd, notify_list *lp)
246 {
247         struct sockaddr_in      sin;
248         struct status           new_status;
249         xdrproc_t               func;
250         void                    *objp;
251         u_int32_t               proc, vers, prog;
252 /*      __u32                   proc, vers, prog; */
253
254         if (NL_TIMES(lp) == 0) {
255                 note(N_DEBUG, "%s: Cannot notify %s, giving up.",
256                                 __func__, inet_ntoa(NL_ADDR(lp)));
257                 return 0;
258         }
259
260         memset(&sin, 0, sizeof(sin));
261         sin.sin_family = AF_INET;
262         sin.sin_port   = lp->port;
263         /* LH - moved address into switch */
264
265         prog = NL_MY_PROG(lp);
266         vers = NL_MY_VERS(lp);
267         proc = NL_MY_PROC(lp);
268
269         /* __FORCE__ loopback for callbacks to lockd ... */
270         /* Just in case we somehow ignored it thus far */
271         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
272
273         func = (xdrproc_t) xdr_status;
274         objp = &new_status;
275         new_status.mon_name = NL_MON_NAME(lp);
276         new_status.state    = NL_STATE(lp);
277         memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
278
279         lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
280         if (!lp->xid) {
281                 note(N_WARNING, "%s: failed to notify port %d",
282                                 __func__, ntohs(lp->port));
283         }
284         NL_TIMES(lp) -= 1;
285
286         return 1;
287 }
288
289 /*
290  * Process a datagram received on the notify socket
291  */
292 int
293 process_reply(FD_SET_TYPE *rfds)
294 {
295         struct sockaddr_in      sin;
296         notify_list             *lp;
297         u_long                  port;
298
299         if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
300                 return 0;
301
302         if (!(lp = recv_rply(sockfd, &sin, &port)))
303                 return 1;
304
305         if (lp->port == 0) {
306                 if (port != 0) {
307                         lp->port = htons((unsigned short) port);
308                         process_entry(sockfd, lp);
309                         NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
310                         nlist_remove(&notify, lp);
311                         nlist_insert_timer(&notify, lp);
312                         return 1;
313                 }
314                 note(N_WARNING, "%s: [%s] service %d not registered",
315                         __func__, inet_ntoa(lp->addr), NL_MY_PROG(lp));
316         } else {
317                 dprintf(N_DEBUG, "%s: Callback to %s (for %d) succeeded.",
318                         __func__, NL_MY_NAME(lp), NL_MON_NAME(lp));
319         }
320         nlist_free(&notify, lp);
321         return 1;
322 }
323
324 /*
325  * Process a notify list, either for notifying remote hosts after reboot
326  * or for calling back (local) statd clients when the remote has notified
327  * us of a crash. 
328  */
329 int
330 process_notify_list(void)
331 {
332         notify_list     *entry;
333         time_t          now;
334         int             fd;
335
336         if ((fd = statd_get_socket()) < 0)
337                 return 0;
338
339         while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
340                 if (process_entry(fd, entry)) {
341                         NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
342                         nlist_remove(&notify, entry);
343                         nlist_insert_timer(&notify, entry);
344                 } else {
345                         note(N_ERROR,
346                                 "%s: Can't callback %s (%d,%d), giving up.",
347                                         __func__,
348                                         NL_MY_NAME(entry),
349                                         NL_MY_PROG(entry),
350                                         NL_MY_VERS(entry));
351                         nlist_free(&notify, entry);
352                 }
353         }
354
355         return 1;
356 }