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