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