]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/monitor.c
statd: Support IPv6 in sm_mon_1_svc()
[nfs-utils.git] / utils / statd / monitor.c
1 /*
2  * Copyright (C) 1995-1999 Jeffrey A. Uphoff
3  * Major rewrite by Olaf Kirch, Dec. 1996.
4  * Modified by H.J. Lu, 1998.
5  * Tighter access control, Olaf Kirch June 1999.
6  *
7  * NSM for Linux.
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <fcntl.h>
15 #include <limits.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <errno.h>
21 #include <arpa/inet.h>
22 #include <dirent.h>
23
24 #include "sockaddr.h"
25 #include "rpcmisc.h"
26 #include "nsm.h"
27 #include "statd.h"
28 #include "notlist.h"
29 #include "ha-callout.h"
30
31 notify_list *           rtnl = NULL;    /* Run-time notify list. */
32
33 /*
34  * Reject requests from non-loopback addresses in order
35  * to prevent attack described in CERT CA-99.05.
36  *
37  * Although the kernel contacts the statd service via only IPv4
38  * transports, the statd service can receive other requests, such
39  * as SM_NOTIFY, from remote peers via IPv6.
40  */
41 static _Bool
42 caller_is_localhost(struct svc_req *rqstp)
43 {
44         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
45         char buf[INET6_ADDRSTRLEN];
46
47         if (!nfs_is_v4_loopback(sap))
48                 goto out_nonlocal;
49         return true;
50
51 out_nonlocal:
52         if (!statd_present_address(sap, buf, sizeof(buf)))
53                 buf[0] = '\0';
54         xlog_warn("SM_MON/SM_UNMON call from non-local host %s", buf);
55         return false;
56 }
57
58 /*
59  * Services SM_MON requests.
60  */
61 struct sm_stat_res *
62 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
63 {
64         static sm_stat_res result;
65         char            *mon_name = argp->mon_id.mon_name,
66                         *my_name  = argp->mon_id.my_id.my_name;
67         struct my_id    *id = &argp->mon_id.my_id;
68         char            *cp;
69         notify_list     *clnt;
70         struct sockaddr_in my_addr = {
71                 .sin_family             = AF_INET,
72                 .sin_addr.s_addr        = htonl(INADDR_LOOPBACK),
73         };
74         char *dnsname = NULL;
75
76         xlog(D_CALL, "Received SM_MON for %s from %s", mon_name, my_name);
77
78         /* Assume that we'll fail. */
79         result.res_stat = STAT_FAIL;
80         result.state = -1;      /* State is undefined for STAT_FAIL. */
81
82         /* 1.   Reject any remote callers.
83          *      Ignore the my_name specified by the caller, and
84          *      use "127.0.0.1" instead.
85          */
86         if (!caller_is_localhost(rqstp))
87                 goto failure;
88
89         /* 2.   Reject any registrations for non-lockd services.
90          *
91          *      This is specific to the linux kernel lockd, which
92          *      makes the callback procedure part of the lockd interface.
93          *      It is also prone to break when lockd changes its callback
94          *      procedure number -- which, in fact, has now happened once.
95          *      There must be a better way....   XXX FIXME
96          */
97         if (id->my_prog != 100021 ||
98             (id->my_proc != 16 && id->my_proc != 24))
99         {
100                 xlog_warn("Attempt to register callback to %d/%d",
101                         id->my_prog, id->my_proc);
102                 goto failure;
103         }
104
105         /*
106          * Check hostnames.  If I can't look them up, I won't monitor.  This
107          * might not be legal, but it adds a little bit of safety and sanity.
108          */
109
110         /* must check for /'s in hostname!  See CERT's CA-96.09 for details. */
111         if (strchr(mon_name, '/') || mon_name[0] == '.') {
112                 xlog(L_ERROR, "SM_MON request for hostname containing '/' "
113                      "or starting '.': %s", mon_name);
114                 xlog(L_ERROR, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
115                 goto failure;
116         }
117
118         /* my_name must not have white space */
119         for (cp=my_name ; *cp ; cp++)
120                 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
121                         *cp = '_';
122
123         /*
124          * Hostnames checked OK.
125          * Now choose a hostname to use for matching.  We cannot
126          * really trust much in the incoming NOTIFY, so to make
127          * sure that multi-homed hosts work nicely, we get an
128          * FQDN now, and use that for matching.
129          */
130         dnsname = statd_canonical_name(mon_name);
131         if (dnsname == NULL) {
132                 xlog(L_WARNING, "No canonical hostname found for %s", mon_name);
133                 goto failure;
134         }
135
136         /* Now check to see if this is a duplicate, and warn if so.
137          * I will also return STAT_FAIL. (I *think* this is how I should
138          * handle it.)
139          *
140          * Olaf requests that I allow duplicate SM_MON requests for
141          * hosts due to the way he is coding lockd. No problem,
142          * I'll just do a quickie success return and things should
143          * be happy.
144          */
145         clnt = rtnl;
146
147         while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
148                 if (statd_matchhostname(NL_MY_NAME(clnt), my_name) &&
149                     NL_MY_PROC(clnt) == id->my_proc &&
150                     NL_MY_PROG(clnt) == id->my_prog &&
151                     NL_MY_VERS(clnt) == id->my_vers &&
152                     memcmp(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE) == 0) {
153                         /* Hey!  We already know you guys! */
154                         xlog(D_GENERAL,
155                                 "Duplicate SM_MON request for %s "
156                                 "from procedure on %s",
157                                 mon_name, my_name);
158
159                         /* But we'll let you pass anyway. */
160                         free(dnsname);
161                         goto success;
162                 }
163                 clnt = NL_NEXT(clnt);
164         }
165
166         /*
167          * We're committed...ignoring errors.  Let's hope that a malloc()
168          * doesn't fail.  (I should probably fix this assumption.)
169          */
170         if (!(clnt = nlist_new(my_name, mon_name, 0))) {
171                 free(dnsname);
172                 xlog_warn("out of memory");
173                 goto failure;
174         }
175
176         NL_ADDR(clnt) = my_addr.sin_addr;
177         NL_MY_PROG(clnt) = id->my_prog;
178         NL_MY_VERS(clnt) = id->my_vers;
179         NL_MY_PROC(clnt) = id->my_proc;
180         memcpy(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE);
181         clnt->dns_name = dnsname;
182
183         /*
184          * Now, Create file on stable storage for host.
185          */
186         if (!nsm_insert_monitored_host(dnsname,
187                                 (struct sockaddr *)(char *)&my_addr, argp)) {
188                 nlist_free(NULL, clnt);
189                 goto failure;
190         }
191
192         /* PRC: do the HA callout: */
193         ha_callout("add-client", mon_name, my_name, -1);
194         nlist_insert(&rtnl, clnt);
195         xlog(D_GENERAL, "MONITORING %s for %s", mon_name, my_name);
196  success:
197         result.res_stat = STAT_SUCC;
198         /* SUN's sm_inter.x says this should be "state number of local site".
199          * X/Open says '"state" will be contain the state of the remote NSM.'
200          * href=http://www.opengroup.org/onlinepubs/9629799/SM_MON.htm
201          * Linux lockd currently (2.6.21 and prior) ignores whatever is
202          * returned, and given the above contraction, it probably always will..
203          * So we just return what we always returned.  If possible, we
204          * have already told lockd about our state number via a sysctl.
205          * If lockd wants the remote state, it will need to
206          * use SM_STAT (and prayer).
207          */
208         result.state = MY_STATE;
209         return (&result);
210
211 failure:
212         xlog_warn("STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
213         return (&result);
214 }
215
216 static unsigned int
217 load_one_host(const char *hostname, const struct sockaddr *sap,
218                 const struct mon *m,
219                 __attribute__ ((unused)) const time_t timestamp)
220 {
221         const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
222         notify_list *clnt;
223
224         clnt = nlist_new(m->mon_id.my_id.my_name,
225                                 m->mon_id.mon_name, 0);
226         if (clnt == NULL)
227                 return 0;
228
229         clnt->dns_name = strdup(hostname);
230         if (clnt->dns_name == NULL) {
231                 nlist_free(NULL, clnt);
232                 return 0;
233         }
234
235         xlog(D_GENERAL, "Adding record for %s to the monitor list...",
236                         hostname);
237
238         NL_ADDR(clnt) = sin->sin_addr;
239         NL_MY_PROG(clnt) = m->mon_id.my_id.my_prog;
240         NL_MY_VERS(clnt) = m->mon_id.my_id.my_vers;
241         NL_MY_PROC(clnt) = m->mon_id.my_id.my_proc;
242         memcpy(NL_PRIV(clnt), m->priv, SM_PRIV_SIZE);
243
244         nlist_insert(&rtnl, clnt);
245         return 1;
246 }
247
248 void load_state(void)
249 {
250         unsigned int count;
251
252         count = nsm_load_monitor_list(load_one_host);
253         if (count)
254                 xlog(D_GENERAL, "Loaded %u previously monitored hosts");
255 }
256
257 /*
258  * Services SM_UNMON requests.
259  *
260  * There is no statement in the X/Open spec's about returning an error
261  * for requests to unmonitor a host that we're *not* monitoring.  I just
262  * return the state of the NSM when I get such foolish requests for lack
263  * of any better ideas.  (I also log the "offense.")
264  */
265 struct sm_stat *
266 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
267 {
268         static sm_stat  result;
269         notify_list     *clnt;
270         char            *mon_name = argp->mon_name,
271                         *my_name  = argp->my_id.my_name;
272         struct my_id    *id = &argp->my_id;
273         char            *cp;
274
275         xlog(D_CALL, "Received SM_UNMON for %s from %s", mon_name, my_name);
276
277         result.state = MY_STATE;
278
279         if (!caller_is_localhost(rqstp))
280                 goto failure;
281
282         /* my_name must not have white space */
283         for (cp=my_name ; *cp ; cp++)
284                 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
285                         *cp = '_';
286
287
288         /* Check if we're monitoring anyone. */
289         if (rtnl == NULL) {
290                 xlog_warn("Received SM_UNMON request from %s for %s while not "
291                         "monitoring any hosts", my_name, argp->mon_name);
292                 return (&result);
293         }
294         clnt = rtnl;
295
296         /*
297          * OK, we are.  Now look for appropriate entry in run-time list.
298          * There should only be *one* match on this, since I block "duplicate"
299          * SM_MON calls.  (Actually, duplicate calls are allowed, but only one
300          * entry winds up in the list the way I'm currently handling them.)
301          */
302         while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
303                 if (statd_matchhostname(NL_MY_NAME(clnt), my_name) &&
304                         NL_MY_PROC(clnt) == id->my_proc &&
305                         NL_MY_PROG(clnt) == id->my_prog &&
306                         NL_MY_VERS(clnt) == id->my_vers) {
307                         /* Match! */
308                         xlog(D_GENERAL, "UNMONITORING %s for %s",
309                                         mon_name, my_name);
310
311                         /* PRC: do the HA callout: */
312                         ha_callout("del-client", mon_name, my_name, -1);
313
314                         nsm_delete_monitored_host(clnt->dns_name,
315                                                         mon_name, my_name);
316                         nlist_free(&rtnl, clnt);
317
318                         return (&result);
319                 } else
320                         clnt = NL_NEXT(clnt);
321         }
322
323  failure:
324         xlog_warn("Received erroneous SM_UNMON request from %s for %s",
325                 my_name, mon_name);
326         return (&result);
327 }
328
329
330 struct sm_stat *
331 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
332 {
333         short int       count = 0;
334         static sm_stat  result;
335         notify_list     *clnt;
336         char            *my_name = argp->my_name;
337
338         xlog(D_CALL, "Received SM_UNMON_ALL for %s", my_name);
339
340         if (!caller_is_localhost(rqstp))
341                 goto failure;
342
343         result.state = MY_STATE;
344
345         if (rtnl == NULL) {
346                 xlog_warn("Received SM_UNMON_ALL request from %s "
347                         "while not monitoring any hosts", my_name);
348                 return (&result);
349         }
350         clnt = rtnl;
351
352         while ((clnt = nlist_gethost(clnt, my_name, 1))) {
353                 if (NL_MY_PROC(clnt) == argp->my_proc &&
354                         NL_MY_PROG(clnt) == argp->my_prog &&
355                         NL_MY_VERS(clnt) == argp->my_vers) {
356                         /* Watch stack! */
357                         char            mon_name[SM_MAXSTRLEN + 1];
358                         notify_list     *temp;
359
360                         xlog(D_GENERAL,
361                                 "UNMONITORING (SM_UNMON_ALL) %s for %s",
362                                 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
363                         strncpy(mon_name, NL_MON_NAME(clnt),
364                                 sizeof (mon_name) - 1);
365                         mon_name[sizeof (mon_name) - 1] = '\0';
366                         temp = NL_NEXT(clnt);
367                         /* PRC: do the HA callout: */
368                         ha_callout("del-client", mon_name, my_name, -1);
369                         nsm_delete_monitored_host(clnt->dns_name,
370                                                         mon_name, my_name);
371                         nlist_free(&rtnl, clnt);
372                         ++count;
373                         clnt = temp;
374                 } else
375                         clnt = NL_NEXT(clnt);
376         }
377
378         if (!count) {
379                 xlog(D_GENERAL, "SM_UNMON_ALL request from %s with no "
380                         "SM_MON requests from it", my_name);
381         }
382
383  failure:
384         return (&result);
385 }