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