]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/monitor.c
5d4aa49644492c776d0d96310a58d0674fae4c12
[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 #include "misc.h"
24 #include "statd.h"
25 #include "notlist.h"
26 #include "ha-callout.h"
27
28 notify_list *           rtnl = NULL;    /* Run-time notify list. */
29
30 #define LINELEN (4*(8+1)+SM_PRIV_SIZE*2+1)
31
32 #ifdef RESTRICTED_STATD
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 in_addr  caller;
41
42         caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
43         if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
44                 note(N_WARNING,
45                         "Call to statd from non-local host %s",
46                         inet_ntoa(caller));
47                 return 0;
48         }
49         return 1;
50 }
51 #else   /* RESTRICTED_STATD */
52 /*
53  * No restrictions for remote callers.
54  */
55 static int
56 caller_is_localhost(struct svc_req *rqstp)
57 {
58         return 1;
59 }
60 #endif  /* RESTRICTED_STATD */
61
62 /*
63  * Services SM_MON requests.
64  */
65 struct sm_stat_res *
66 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
67 {
68         static sm_stat_res result;
69         char            *mon_name = argp->mon_id.mon_name,
70                         *my_name  = argp->mon_id.my_id.my_name;
71         struct my_id    *id = &argp->mon_id.my_id;
72         char            *path;
73         char            *cp;
74         int             fd;
75         notify_list     *clnt;
76         struct in_addr  my_addr;
77         char            *dnsname;
78         struct hostent  *hostinfo = NULL;
79
80         /* Assume that we'll fail. */
81         result.res_stat = STAT_FAIL;
82         result.state = -1;      /* State is undefined for STAT_FAIL. */
83
84 #ifdef RESTRICTED_STATD
85         /* 1.   Reject any remote callers.
86          *      Ignore the my_name specified by the caller, and
87          *      use "127.0.0.1" instead.
88          */
89         if (!caller_is_localhost(rqstp))
90                 goto failure;
91         my_addr.s_addr = htonl(INADDR_LOOPBACK);
92
93         /* 2.   Reject any registrations for non-lockd services.
94          *
95          *      This is specific to the linux kernel lockd, which
96          *      makes the callback procedure part of the lockd interface.
97          *      It is also prone to break when lockd changes its callback
98          *      procedure number -- which, in fact, has now happened once.
99          *      There must be a better way....   XXX FIXME
100          */
101         if (id->my_prog != 100021 ||
102             (id->my_proc != 16 && id->my_proc != 24))
103         {
104                 note(N_WARNING,
105                         "Attempt to register callback to %d/%d",
106                         id->my_prog, id->my_proc);
107                 goto failure;
108         }
109
110 #if 0
111         This is not usable anymore.  Linux-kernel can be configured to use
112         host names with NSM so that multi-homed hosts are handled properly.
113                 NeilBrown 15mar2007
114
115         /* 3.   mon_name must be an address in dotted quad.
116          *      Again, specific to the linux kernel lockd.
117          */
118         if (!inet_aton(mon_name, &mon_addr)) {
119                 note(N_WARNING,
120                         "Attempt to register host %s (not a dotted quad)",
121                         mon_name);
122                 goto failure;
123         }
124 #endif
125 #else
126         if (!(hostinfo = gethostbyname(my_name))) {
127                 note(N_WARNING, "gethostbyname error for %s", my_name);
128                 goto failure;
129         } else
130                 my_addr = *(struct in_addr *) hostinfo->h_addr;
131 #endif
132         /*
133          * Check hostnames.  If I can't look them up, I won't monitor.  This
134          * might not be legal, but it adds a little bit of safety and sanity.
135          */
136
137         /* must check for /'s in hostname!  See CERT's CA-96.09 for details. */
138         if (strchr(mon_name, '/') || mon_name[0] == '.') {
139                 note(N_CRIT, "SM_MON request for hostname containing '/' "
140                      "or starting '.': %s", mon_name);
141                 note(N_CRIT, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
142                 goto failure;
143         } else if ((hostinfo = gethostbyname(mon_name)) == NULL) {
144                 note(N_WARNING, "gethostbyname error for %s", mon_name);
145                 goto failure;
146         }
147
148         /* my_name must not have white space */
149         for (cp=my_name ; *cp ; cp++)
150                 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
151                         *cp = '_';
152
153         /*
154          * Hostnames checked OK.
155          * Now choose a hostname to use for matching.  We cannot
156          * really trust much in the incoming NOTIFY, so to make
157          * sure that multi-homed hosts work nicely, we get an
158          * FQDN now, and use that for matching
159          */
160         hostinfo = gethostbyaddr(hostinfo->h_addr,
161                                  hostinfo->h_length,
162                                  hostinfo->h_addrtype);
163         if (hostinfo)
164                 dnsname = xstrdup(hostinfo->h_name);
165         else
166                 dnsname = xstrdup(my_name);
167
168         /* Now check to see if this is a duplicate, and warn if so.
169          * I will also return STAT_FAIL. (I *think* this is how I should
170          * handle it.)
171          *
172          * Olaf requests that I allow duplicate SM_MON requests for
173          * hosts due to the way he is coding lockd. No problem,
174          * I'll just do a quickie success return and things should
175          * be happy.
176          */
177         clnt = rtnl;
178
179         while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
180                 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
181                     NL_MY_PROC(clnt) == id->my_proc &&
182                     NL_MY_PROG(clnt) == id->my_prog &&
183                     NL_MY_VERS(clnt) == id->my_vers &&
184                     memcmp(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE) == 0) {
185                         /* Hey!  We already know you guys! */
186                         dprintf(N_DEBUG,
187                                 "Duplicate SM_MON request for %s "
188                                 "from procedure on %s",
189                                 mon_name, my_name);
190
191                         /* But we'll let you pass anyway. */
192                         goto success;
193                 }
194                 clnt = NL_NEXT(clnt);
195         }
196
197         /*
198          * We're committed...ignoring errors.  Let's hope that a malloc()
199          * doesn't fail.  (I should probably fix this assumption.)
200          */
201         if (!(clnt = nlist_new(my_name, mon_name, 0))) {
202                 note(N_WARNING, "out of memory");
203                 goto failure;
204         }
205
206         NL_ADDR(clnt) = my_addr;
207         NL_MY_PROG(clnt) = id->my_prog;
208         NL_MY_VERS(clnt) = id->my_vers;
209         NL_MY_PROC(clnt) = id->my_proc;
210         memcpy(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE);
211         clnt->dns_name = dnsname;
212
213         /*
214          * Now, Create file on stable storage for host.
215          */
216
217         path=xmalloc(strlen(SM_DIR)+strlen(dnsname)+2);
218         sprintf(path, "%s/%s", SM_DIR, dnsname);
219         if ((fd = open(path, O_WRONLY|O_SYNC|O_CREAT|O_APPEND,
220                        S_IRUSR|S_IWUSR)) < 0) {
221                 /* Didn't fly.  We won't monitor. */
222                 note(N_ERROR, "creat(%s) failed: %s", path, strerror (errno));
223                 nlist_free(NULL, clnt);
224                 free(path);
225                 goto failure;
226         }
227         {
228                 char buf[LINELEN + 1 + SM_MAXSTRLEN*2 + 4];
229                 char *e;
230                 int i;
231                 e = buf + sprintf(buf, "%08x %08x %08x %08x ",
232                                   my_addr.s_addr, id->my_prog,
233                                   id->my_vers, id->my_proc);
234                 for (i=0; i<SM_PRIV_SIZE; i++)
235                         e += sprintf(e, "%02x", 0xff & (argp->priv[i]));
236                 if (e+1-buf != LINELEN) abort();
237                 e += sprintf(e, " %s %s\n", mon_name, my_name);
238                 write(fd, buf, e-buf);
239         }
240
241         free(path);
242         /* PRC: do the HA callout: */
243         ha_callout("add-client", mon_name, my_name, -1);
244         nlist_insert(&rtnl, clnt);
245         close(fd);
246         dprintf(N_DEBUG, "MONITORING %s for %s", mon_name, my_name);
247  success:
248         result.res_stat = STAT_SUCC;
249         /* SUN's sm_inter.x says this should be "state number of local site".
250          * X/Open says '"state" will be contain the state of the remote NSM.'
251          * href=http://www.opengroup.org/onlinepubs/9629799/SM_MON.htm
252          * Linux lockd currently (2.6.21 and prior) ignores whatever is
253          * returned, and given the above contraction, it probably always will..
254          * So we just return what we always returned.  If possible, we
255          * have already told lockd about our state number via a sysctl.
256          * If lockd wants the remote state, it will need to
257          * use SM_STAT (and prayer).
258          */
259         result.state = MY_STATE;
260         return (&result);
261
262 failure:
263         note(N_WARNING, "STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
264         return (&result);
265 }
266
267 void load_state(void)
268 {
269         DIR *d;
270         struct dirent *de;
271         char buf[LINELEN + 1 + SM_MAXSTRLEN + 2];
272
273         d = opendir(SM_DIR);
274         if (!d)
275                 return;
276         while ((de = readdir(d))) {
277                 char *path;
278                 FILE *f;
279                 int p;
280
281                 if (de->d_name[0] == '.')
282                         continue;
283                 path = xmalloc(strlen(SM_DIR)+strlen(de->d_name)+2);
284                 sprintf(path, "%s/%s", SM_DIR, de->d_name);
285                 f = fopen(path, "r");
286                 free(path);
287                 if (f == NULL)
288                         continue;
289                 while (fgets(buf, sizeof(buf), f) != NULL) {
290                         int addr, proc, prog, vers;
291                         char priv[SM_PRIV_SIZE];
292                         char *monname, *myname;
293                         char *b;
294                         int i;
295                         notify_list     *clnt;
296
297                         buf[sizeof(buf)-1] = 0;
298                         b = strchr(buf, '\n');
299                         if (b) *b = 0;
300                         sscanf(buf, "%x %x %x %x ",
301                                &addr, &prog, &vers, &proc);
302                         b = buf+36;
303                         for (i=0; i<SM_PRIV_SIZE; i++) {
304                                 sscanf(b, "%2x", &p);
305                                 priv[i] = p;
306                                 b += 2;
307                         }
308                         b++;
309                         monname = b;
310                         while (*b && *b != ' ') b++;
311                         if (*b) *b++ = '\0';
312                         while (*b == ' ') b++;
313                         myname = b;
314                         clnt = nlist_new(myname, monname, 0);
315                         if (!clnt)
316                                 break;
317                         NL_ADDR(clnt).s_addr = addr;
318                         NL_MY_PROG(clnt) = prog;
319                         NL_MY_VERS(clnt) = vers;
320                         NL_MY_PROC(clnt) = proc;
321                         clnt->dns_name = xstrdup(de->d_name);
322                         memcpy(NL_PRIV(clnt), priv, SM_PRIV_SIZE);
323                         nlist_insert(&rtnl, clnt);
324                 }
325                 fclose(f);
326         }
327         closedir(d);
328 }
329
330
331
332
333 /*
334  * Services SM_UNMON requests.
335  *
336  * There is no statement in the X/Open spec's about returning an error
337  * for requests to unmonitor a host that we're *not* monitoring.  I just
338  * return the state of the NSM when I get such foolish requests for lack
339  * of any better ideas.  (I also log the "offense.")
340  */
341 struct sm_stat *
342 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
343 {
344         static sm_stat  result;
345         notify_list     *clnt;
346         char            *mon_name = argp->mon_name,
347                         *my_name  = argp->my_id.my_name;
348         struct my_id    *id = &argp->my_id;
349         char            *cp;
350
351         result.state = MY_STATE;
352
353         if (!caller_is_localhost(rqstp))
354                 goto failure;
355
356         /* my_name must not have white space */
357         for (cp=my_name ; *cp ; cp++)
358                 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
359                         *cp = '_';
360
361
362         /* Check if we're monitoring anyone. */
363         if (!(clnt = rtnl)) {
364                 note(N_WARNING,
365                         "Received SM_UNMON request from %s for %s while not "
366                         "monitoring any hosts.", my_name, argp->mon_name);
367                 return (&result);
368         }
369
370         /*
371          * OK, we are.  Now look for appropriate entry in run-time list.
372          * There should only be *one* match on this, since I block "duplicate"
373          * SM_MON calls.  (Actually, duplicate calls are allowed, but only one
374          * entry winds up in the list the way I'm currently handling them.)
375          */
376         while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
377                 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
378                         NL_MY_PROC(clnt) == id->my_proc &&
379                         NL_MY_PROG(clnt) == id->my_prog &&
380                         NL_MY_VERS(clnt) == id->my_vers) {
381                         /* Match! */
382                         dprintf(N_DEBUG, "UNMONITORING %s for %s",
383                                         mon_name, my_name);
384
385                         /* PRC: do the HA callout: */
386                         ha_callout("del-client", mon_name, my_name, -1);
387
388                         xunlink(SM_DIR, clnt->dns_name, 1);
389                         nlist_free(&rtnl, clnt);
390
391                         return (&result);
392                 } else
393                         clnt = NL_NEXT(clnt);
394         }
395
396  failure:
397         note(N_WARNING, "Received erroneous SM_UNMON request from %s for %s",
398                 my_name, mon_name);
399         return (&result);
400 }
401
402
403 struct sm_stat *
404 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
405 {
406         short int       count = 0;
407         static sm_stat  result;
408         notify_list     *clnt;
409         char            *my_name = argp->my_name;
410
411         if (!caller_is_localhost(rqstp))
412                 goto failure;
413
414         result.state = MY_STATE;
415
416         if (!(clnt = rtnl)) {
417                 note(N_WARNING, "Received SM_UNMON_ALL request from %s "
418                         "while not monitoring any hosts", my_name);
419                 return (&result);
420         }
421
422         while ((clnt = nlist_gethost(clnt, my_name, 1))) {
423                 if (NL_MY_PROC(clnt) == argp->my_proc &&
424                         NL_MY_PROG(clnt) == argp->my_prog &&
425                         NL_MY_VERS(clnt) == argp->my_vers) {
426                         /* Watch stack! */
427                         char            mon_name[SM_MAXSTRLEN + 1];
428                         notify_list     *temp;
429
430                         dprintf(N_DEBUG,
431                                 "UNMONITORING (SM_UNMON_ALL) %s for %s",
432                                 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
433                         strncpy(mon_name, NL_MON_NAME(clnt),
434                                 sizeof (mon_name) - 1);
435                         mon_name[sizeof (mon_name) - 1] = '\0';
436                         temp = NL_NEXT(clnt);
437                         /* PRC: do the HA callout: */
438                         ha_callout("del-client", mon_name, my_name, -1);
439                         xunlink(SM_DIR, clnt->dns_name, 1);
440                         nlist_free(&rtnl, clnt);
441                         ++count;
442                         clnt = temp;
443                 } else
444                         clnt = NL_NEXT(clnt);
445         }
446
447         if (!count) {
448                 dprintf(N_DEBUG, "SM_UNMON_ALL request from %s with no "
449                         "SM_MON requests from it.", my_name);
450         }
451
452  failure:
453         return (&result);
454 }