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