]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/rmtcall.c
Be more cautious about use for privilege ports (<1024).
[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, "Can't create socket: %m");
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,
87                                 "process_hosts: can't bind to reserved port\n");
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, "xmit_mesg: can't encode RPC message!\n");
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, "xmit_mesg: sendto failed: %m");
156         } else if (err != msglen) {
157                 dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
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, "recv_rply: recvfrom failed: %m");
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, "recv_rply: can't decode RPC message!\n");
190                 goto done;
191         }
192
193         if (mesg.rm_reply.rp_stat != 0) {
194                 note(N_WARNING, "recv_rply: [%s] RPC status %d\n", 
195                                 inet_ntoa(sin->sin_addr),
196                                 mesg.rm_reply.rp_stat);
197                 goto done;
198         }
199         if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
200                 note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
201                                 inet_ntoa(sin->sin_addr),
202                                 mesg.rm_reply.rp_acpt.ar_stat);
203                 goto done;
204         }
205
206         for (lp = notify; lp != NULL; lp = lp->next) {
207                 /* LH - this was a bug... it should have been checking
208                  * the xid from the response message from the client,
209                  * not the static, internal xid */
210                 if (lp->xid != mesg.rm_xid)
211                         continue;
212                 if (lp->addr.s_addr != sin->sin_addr.s_addr) {
213                         char addr [18];
214                         strncpy (addr, inet_ntoa(lp->addr),
215                                  sizeof (addr) - 1);
216                         addr [sizeof (addr) - 1] = '\0';
217                         dprintf(N_WARNING, "address mismatch: "
218                                 "expected %s, got %s\n",
219                                 addr, inet_ntoa(sin->sin_addr));
220                 }
221                 if (lp->port == 0) {
222                         if (!xdr_u_long(xdrs, portp)) {
223                                 note(N_WARNING, "recv_rply: [%s] "
224                                         "can't decode reply body!\n",
225                                         inet_ntoa(sin->sin_addr));
226                                 lp = NULL;
227                                 goto done;
228                         }
229                 }
230                 break;
231         }
232
233 done:
234         xdr_destroy(xdrs);
235         return lp;
236 }
237
238 /*
239  * Notify operation for a single list entry
240  */
241 static int
242 process_entry(int sockfd, notify_list *lp)
243 {
244         struct sockaddr_in      sin;
245         struct status           new_status;
246         xdrproc_t               func;
247         void                    *objp;
248         u_int32_t               proc, vers, prog;
249 /*      __u32                   proc, vers, prog; */
250
251         if (NL_TIMES(lp) == 0) {
252                 note(N_DEBUG, "Cannot notify %s, giving up.\n",
253                                         inet_ntoa(NL_ADDR(lp)));
254                 return 0;
255         }
256
257         memset(&sin, 0, sizeof(sin));
258         sin.sin_family = AF_INET;
259         sin.sin_port   = lp->port;
260         /* LH - moved address into switch */
261
262         prog = NL_MY_PROG(lp);
263         vers = NL_MY_VERS(lp);
264         proc = NL_MY_PROC(lp);
265
266         /* __FORCE__ loopback for callbacks to lockd ... */
267         /* Just in case we somehow ignored it thus far */
268         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
269
270         func = (xdrproc_t) xdr_status;
271         objp = &new_status;
272         new_status.mon_name = NL_MON_NAME(lp);
273         new_status.state    = NL_STATE(lp);
274         memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
275
276         lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
277         if (!lp->xid) {
278                 note(N_WARNING, "notify_host: failed to notify port %d\n",
279                                 ntohs(lp->port));
280         }
281         NL_TIMES(lp) -= 1;
282
283         return 1;
284 }
285
286 /*
287  * Process a datagram received on the notify socket
288  */
289 int
290 process_reply(FD_SET_TYPE *rfds)
291 {
292         struct sockaddr_in      sin;
293         notify_list             *lp;
294         u_long                  port;
295
296         if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
297                 return 0;
298
299         if (!(lp = recv_rply(sockfd, &sin, &port)))
300                 return 1;
301
302         if (lp->port == 0) {
303                 if (port != 0) {
304                         lp->port = htons((unsigned short) port);
305                         process_entry(sockfd, lp);
306                         NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
307                         nlist_remove(&notify, lp);
308                         nlist_insert_timer(&notify, lp);
309                         return 1;
310                 }
311                 note(N_WARNING, "recv_rply: [%s] service %d not registered",
312                         inet_ntoa(lp->addr), NL_MY_PROG(lp));
313         } else {
314                 dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
315                         NL_MY_NAME(lp), NL_MON_NAME(lp));
316         }
317         nlist_free(&notify, lp);
318         return 1;
319 }
320
321 /*
322  * Process a notify list, either for notifying remote hosts after reboot
323  * or for calling back (local) statd clients when the remote has notified
324  * us of a crash. 
325  */
326 int
327 process_notify_list(void)
328 {
329         notify_list     *entry;
330         time_t          now;
331         int             fd;
332
333         if ((fd = statd_get_socket()) < 0)
334                 return 0;
335
336         while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
337                 if (process_entry(fd, entry)) {
338                         NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
339                         nlist_remove(&notify, entry);
340                         nlist_insert_timer(&notify, entry);
341                 } else {
342                         note(N_ERROR,
343                                 "Can't callback %s (%d,%d), giving up.",
344                                         NL_MY_NAME(entry),
345                                         NL_MY_PROG(entry),
346                                         NL_MY_VERS(entry));
347                         nlist_free(&notify, entry);
348                 }
349         }
350
351         return 1;
352 }