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