]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/monitor.c
f818b2b4ae69d38f282845887853ddcb53695b74
[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 "rpcmisc.h"
25 #include "misc.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 static int
38 caller_is_localhost(struct svc_req *rqstp)
39 {
40         struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
41         struct in_addr  caller;
42
43         caller = sin->sin_addr;
44         if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
45                 xlog_warn("Call to statd from non-local host %s",
46                         inet_ntoa(caller));
47                 return 0;
48         }
49         return 1;
50 }
51
52 /*
53  * Services SM_MON requests.
54  */
55 struct sm_stat_res *
56 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
57 {
58         static sm_stat_res result;
59         char            *mon_name = argp->mon_id.mon_name,
60                         *my_name  = argp->mon_id.my_id.my_name;
61         struct my_id    *id = &argp->mon_id.my_id;
62         char            *cp;
63         notify_list     *clnt;
64         struct sockaddr_in my_addr = {
65                 .sin_family             = AF_INET,
66                 .sin_addr.s_addr        = htonl(INADDR_LOOPBACK),
67         };
68         char            *dnsname;
69         struct hostent  *hostinfo = NULL;
70
71         xlog(D_CALL, "Received SM_MON for %s from %s", mon_name, my_name);
72
73         /* Assume that we'll fail. */
74         result.res_stat = STAT_FAIL;
75         result.state = -1;      /* State is undefined for STAT_FAIL. */
76
77         /* 1.   Reject any remote callers.
78          *      Ignore the my_name specified by the caller, and
79          *      use "127.0.0.1" instead.
80          */
81         if (!caller_is_localhost(rqstp))
82                 goto failure;
83
84         /* 2.   Reject any registrations for non-lockd services.
85          *
86          *      This is specific to the linux kernel lockd, which
87          *      makes the callback procedure part of the lockd interface.
88          *      It is also prone to break when lockd changes its callback
89          *      procedure number -- which, in fact, has now happened once.
90          *      There must be a better way....   XXX FIXME
91          */
92         if (id->my_prog != 100021 ||
93             (id->my_proc != 16 && id->my_proc != 24))
94         {
95                 xlog_warn("Attempt to register callback to %d/%d",
96                         id->my_prog, id->my_proc);
97                 goto failure;
98         }
99
100         /*
101          * Check hostnames.  If I can't look them up, I won't monitor.  This
102          * might not be legal, but it adds a little bit of safety and sanity.
103          */
104
105         /* must check for /'s in hostname!  See CERT's CA-96.09 for details. */
106         if (strchr(mon_name, '/') || mon_name[0] == '.') {
107                 xlog(L_ERROR, "SM_MON request for hostname containing '/' "
108                      "or starting '.': %s", mon_name);
109                 xlog(L_ERROR, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
110                 goto failure;
111         } else if ((hostinfo = gethostbyname(mon_name)) == NULL) {
112                 xlog_warn("gethostbyname error for %s", mon_name);
113                 goto failure;
114         }
115
116         /* my_name must not have white space */
117         for (cp=my_name ; *cp ; cp++)
118                 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
119                         *cp = '_';
120
121         /*
122          * Hostnames checked OK.
123          * Now choose a hostname to use for matching.  We cannot
124          * really trust much in the incoming NOTIFY, so to make
125          * sure that multi-homed hosts work nicely, we get an
126          * FQDN now, and use that for matching
127          */
128         hostinfo = gethostbyaddr(hostinfo->h_addr,
129                                  hostinfo->h_length,
130                                  hostinfo->h_addrtype);
131         if (hostinfo)
132                 dnsname = xstrdup(hostinfo->h_name);
133         else
134                 dnsname = xstrdup(my_name);
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 (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                         goto success;
161                 }
162                 clnt = NL_NEXT(clnt);
163         }
164
165         /*
166          * We're committed...ignoring errors.  Let's hope that a malloc()
167          * doesn't fail.  (I should probably fix this assumption.)
168          */
169         if (!(clnt = nlist_new(my_name, mon_name, 0))) {
170                 xlog_warn("out of memory");
171                 goto failure;
172         }
173
174         NL_ADDR(clnt) = my_addr.sin_addr;
175         NL_MY_PROG(clnt) = id->my_prog;
176         NL_MY_VERS(clnt) = id->my_vers;
177         NL_MY_PROC(clnt) = id->my_proc;
178         memcpy(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE);
179         clnt->dns_name = dnsname;
180
181         /*
182          * Now, Create file on stable storage for host.
183          */
184         if (!nsm_insert_monitored_host(dnsname,
185                                 (struct sockaddr *)(char *)&my_addr, argp)) {
186                 nlist_free(NULL, clnt);
187                 goto failure;
188         }
189
190         /* PRC: do the HA callout: */
191         ha_callout("add-client", mon_name, my_name, -1);
192         nlist_insert(&rtnl, clnt);
193         xlog(D_GENERAL, "MONITORING %s for %s", mon_name, my_name);
194  success:
195         result.res_stat = STAT_SUCC;
196         /* SUN's sm_inter.x says this should be "state number of local site".
197          * X/Open says '"state" will be contain the state of the remote NSM.'
198          * href=http://www.opengroup.org/onlinepubs/9629799/SM_MON.htm
199          * Linux lockd currently (2.6.21 and prior) ignores whatever is
200          * returned, and given the above contraction, it probably always will..
201          * So we just return what we always returned.  If possible, we
202          * have already told lockd about our state number via a sysctl.
203          * If lockd wants the remote state, it will need to
204          * use SM_STAT (and prayer).
205          */
206         result.state = MY_STATE;
207         return (&result);
208
209 failure:
210         xlog_warn("STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
211         return (&result);
212 }
213
214 static unsigned int
215 load_one_host(const char *hostname, const struct sockaddr *sap,
216                 const struct mon *m,
217                 __attribute__ ((unused)) const time_t timestamp)
218 {
219         const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
220         notify_list *clnt;
221
222         clnt = nlist_new(m->mon_id.my_id.my_name,
223                                 m->mon_id.mon_name, 0);
224         if (clnt == NULL)
225                 return 0;
226
227         clnt->dns_name = strdup(hostname);
228         if (clnt->dns_name == NULL) {
229                 nlist_free(NULL, clnt);
230                 return 0;
231         }
232
233         xlog(D_GENERAL, "Adding record for %s to the monitor list...",
234                         hostname);
235
236         NL_ADDR(clnt) = sin->sin_addr;
237         NL_MY_PROG(clnt) = m->mon_id.my_id.my_prog;
238         NL_MY_VERS(clnt) = m->mon_id.my_id.my_vers;
239         NL_MY_PROC(clnt) = m->mon_id.my_id.my_proc;
240         memcpy(NL_PRIV(clnt), m->priv, SM_PRIV_SIZE);
241
242         nlist_insert(&rtnl, clnt);
243         return 1;
244 }
245
246 void load_state(void)
247 {
248         unsigned int count;
249
250         count = nsm_load_monitor_list(load_one_host);
251         if (count)
252                 xlog(D_GENERAL, "Loaded %u previously monitored hosts");
253 }
254
255 /*
256  * Services SM_UNMON requests.
257  *
258  * There is no statement in the X/Open spec's about returning an error
259  * for requests to unmonitor a host that we're *not* monitoring.  I just
260  * return the state of the NSM when I get such foolish requests for lack
261  * of any better ideas.  (I also log the "offense.")
262  */
263 struct sm_stat *
264 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
265 {
266         static sm_stat  result;
267         notify_list     *clnt;
268         char            *mon_name = argp->mon_name,
269                         *my_name  = argp->my_id.my_name;
270         struct my_id    *id = &argp->my_id;
271         char            *cp;
272
273         xlog(D_CALL, "Received SM_UNMON for %s from %s", mon_name, my_name);
274
275         result.state = MY_STATE;
276
277         if (!caller_is_localhost(rqstp))
278                 goto failure;
279
280         /* my_name must not have white space */
281         for (cp=my_name ; *cp ; cp++)
282                 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
283                         *cp = '_';
284
285
286         /* Check if we're monitoring anyone. */
287         if (rtnl == NULL) {
288                 xlog_warn("Received SM_UNMON request from %s for %s while not "
289                         "monitoring any hosts", my_name, argp->mon_name);
290                 return (&result);
291         }
292         clnt = rtnl;
293
294         /*
295          * OK, we are.  Now look for appropriate entry in run-time list.
296          * There should only be *one* match on this, since I block "duplicate"
297          * SM_MON calls.  (Actually, duplicate calls are allowed, but only one
298          * entry winds up in the list the way I'm currently handling them.)
299          */
300         while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
301                 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
302                         NL_MY_PROC(clnt) == id->my_proc &&
303                         NL_MY_PROG(clnt) == id->my_prog &&
304                         NL_MY_VERS(clnt) == id->my_vers) {
305                         /* Match! */
306                         xlog(D_GENERAL, "UNMONITORING %s for %s",
307                                         mon_name, my_name);
308
309                         /* PRC: do the HA callout: */
310                         ha_callout("del-client", mon_name, my_name, -1);
311
312                         nsm_delete_monitored_host(clnt->dns_name);
313                         nlist_free(&rtnl, clnt);
314
315                         return (&result);
316                 } else
317                         clnt = NL_NEXT(clnt);
318         }
319
320  failure:
321         xlog_warn("Received erroneous SM_UNMON request from %s for %s",
322                 my_name, mon_name);
323         return (&result);
324 }
325
326
327 struct sm_stat *
328 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
329 {
330         short int       count = 0;
331         static sm_stat  result;
332         notify_list     *clnt;
333         char            *my_name = argp->my_name;
334
335         xlog(D_CALL, "Received SM_UNMON_ALL for %s", my_name);
336
337         if (!caller_is_localhost(rqstp))
338                 goto failure;
339
340         result.state = MY_STATE;
341
342         if (rtnl == NULL) {
343                 xlog_warn("Received SM_UNMON_ALL request from %s "
344                         "while not monitoring any hosts", my_name);
345                 return (&result);
346         }
347         clnt = rtnl;
348
349         while ((clnt = nlist_gethost(clnt, my_name, 1))) {
350                 if (NL_MY_PROC(clnt) == argp->my_proc &&
351                         NL_MY_PROG(clnt) == argp->my_prog &&
352                         NL_MY_VERS(clnt) == argp->my_vers) {
353                         /* Watch stack! */
354                         char            mon_name[SM_MAXSTRLEN + 1];
355                         notify_list     *temp;
356
357                         xlog(D_GENERAL,
358                                 "UNMONITORING (SM_UNMON_ALL) %s for %s",
359                                 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
360                         strncpy(mon_name, NL_MON_NAME(clnt),
361                                 sizeof (mon_name) - 1);
362                         mon_name[sizeof (mon_name) - 1] = '\0';
363                         temp = NL_NEXT(clnt);
364                         /* PRC: do the HA callout: */
365                         ha_callout("del-client", mon_name, my_name, -1);
366                         nsm_delete_monitored_host(clnt->dns_name);
367                         nlist_free(&rtnl, clnt);
368                         ++count;
369                         clnt = temp;
370                 } else
371                         clnt = NL_NEXT(clnt);
372         }
373
374         if (!count) {
375                 xlog(D_GENERAL, "SM_UNMON_ALL request from %s with no "
376                         "SM_MON requests from it", my_name);
377         }
378
379  failure:
380         return (&result);
381 }