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