]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/monitor.c
statd: Replace note() with xlog() in rpc.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
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                 xlog_warn("Call to statd from non-local host %s",
47                         inet_ntoa(caller));
48                 return 0;
49         }
50         return 1;
51 }
52
53 /*
54  * Services SM_MON requests.
55  */
56 struct sm_stat_res *
57 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
58 {
59         static sm_stat_res result;
60         char            *mon_name = argp->mon_id.mon_name,
61                         *my_name  = argp->mon_id.my_id.my_name;
62         struct my_id    *id = &argp->mon_id.my_id;
63         char            *path;
64         char            *cp;
65         int             fd;
66         notify_list     *clnt;
67         struct in_addr  my_addr;
68         char            *dnsname;
69         struct hostent  *hostinfo = NULL;
70
71         xlog(D_CALL, "Received SM_MON for %s from %s", mon_name, my_name);
72
73         /* Assume that we'll fail. */
74         result.res_stat = STAT_FAIL;
75         result.state = -1;      /* State is undefined for STAT_FAIL. */
76
77         /* 1.   Reject any remote callers.
78          *      Ignore the my_name specified by the caller, and
79          *      use "127.0.0.1" instead.
80          */
81         if (!caller_is_localhost(rqstp))
82                 goto failure;
83         my_addr.s_addr = htonl(INADDR_LOOPBACK);
84
85         /* 2.   Reject any registrations for non-lockd services.
86          *
87          *      This is specific to the linux kernel lockd, which
88          *      makes the callback procedure part of the lockd interface.
89          *      It is also prone to break when lockd changes its callback
90          *      procedure number -- which, in fact, has now happened once.
91          *      There must be a better way....   XXX FIXME
92          */
93         if (id->my_prog != 100021 ||
94             (id->my_proc != 16 && id->my_proc != 24))
95         {
96                 xlog_warn("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                 xlog(L_ERROR, "SM_MON request for hostname containing '/' "
109                      "or starting '.': %s", mon_name);
110                 xlog(L_ERROR, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
111                 goto failure;
112         } else if ((hostinfo = gethostbyname(mon_name)) == NULL) {
113                 xlog_warn("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                         xlog(D_GENERAL,
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                 xlog_warn("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                 xlog(L_ERROR, "creat(%s) failed: %m", path);
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                 if (write(fd, buf, e-buf) != (e-buf)) {
208                         xlog_warn("writing to %s failed: errno %d (%s)",
209                                 path, errno, strerror(errno));
210                 }
211         }
212
213         free(path);
214         /* PRC: do the HA callout: */
215         ha_callout("add-client", mon_name, my_name, -1);
216         nlist_insert(&rtnl, clnt);
217         close(fd);
218         xlog(D_GENERAL, "MONITORING %s for %s", mon_name, my_name);
219  success:
220         result.res_stat = STAT_SUCC;
221         /* SUN's sm_inter.x says this should be "state number of local site".
222          * X/Open says '"state" will be contain the state of the remote NSM.'
223          * href=http://www.opengroup.org/onlinepubs/9629799/SM_MON.htm
224          * Linux lockd currently (2.6.21 and prior) ignores whatever is
225          * returned, and given the above contraction, it probably always will..
226          * So we just return what we always returned.  If possible, we
227          * have already told lockd about our state number via a sysctl.
228          * If lockd wants the remote state, it will need to
229          * use SM_STAT (and prayer).
230          */
231         result.state = MY_STATE;
232         return (&result);
233
234 failure:
235         xlog_warn("STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
236         return (&result);
237 }
238
239 void load_state(void)
240 {
241         DIR *d;
242         struct dirent *de;
243         char buf[LINELEN + 1 + SM_MAXSTRLEN + 2];
244
245         d = opendir(SM_DIR);
246         if (!d)
247                 return;
248         while ((de = readdir(d))) {
249                 char *path;
250                 FILE *f;
251                 int p;
252
253                 if (de->d_name[0] == '.')
254                         continue;
255                 path = xmalloc(strlen(SM_DIR)+strlen(de->d_name)+2);
256                 sprintf(path, "%s/%s", SM_DIR, de->d_name);
257                 f = fopen(path, "r");
258                 free(path);
259                 if (f == NULL)
260                         continue;
261                 while (fgets(buf, sizeof(buf), f) != NULL) {
262                         int addr, proc, prog, vers;
263                         char priv[SM_PRIV_SIZE];
264                         char *monname, *myname;
265                         char *b;
266                         int i;
267                         notify_list     *clnt;
268
269                         buf[sizeof(buf)-1] = 0;
270                         b = strchr(buf, '\n');
271                         if (b) *b = 0;
272                         sscanf(buf, "%x %x %x %x ",
273                                &addr, &prog, &vers, &proc);
274                         b = buf+36;
275                         for (i=0; i<SM_PRIV_SIZE; i++) {
276                                 sscanf(b, "%2x", &p);
277                                 priv[i] = p;
278                                 b += 2;
279                         }
280                         b++;
281                         monname = b;
282                         while (*b && *b != ' ') b++;
283                         if (*b) *b++ = '\0';
284                         while (*b == ' ') b++;
285                         myname = b;
286                         clnt = nlist_new(myname, monname, 0);
287                         if (!clnt)
288                                 break;
289                         NL_ADDR(clnt).s_addr = addr;
290                         NL_MY_PROG(clnt) = prog;
291                         NL_MY_VERS(clnt) = vers;
292                         NL_MY_PROC(clnt) = proc;
293                         clnt->dns_name = xstrdup(de->d_name);
294                         memcpy(NL_PRIV(clnt), priv, SM_PRIV_SIZE);
295                         nlist_insert(&rtnl, clnt);
296                 }
297                 fclose(f);
298         }
299         closedir(d);
300 }
301
302
303
304
305 /*
306  * Services SM_UNMON requests.
307  *
308  * There is no statement in the X/Open spec's about returning an error
309  * for requests to unmonitor a host that we're *not* monitoring.  I just
310  * return the state of the NSM when I get such foolish requests for lack
311  * of any better ideas.  (I also log the "offense.")
312  */
313 struct sm_stat *
314 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
315 {
316         static sm_stat  result;
317         notify_list     *clnt;
318         char            *mon_name = argp->mon_name,
319                         *my_name  = argp->my_id.my_name;
320         struct my_id    *id = &argp->my_id;
321         char            *cp;
322
323         xlog(D_CALL, "Received SM_UNMON for %s from %s", mon_name, my_name);
324
325         result.state = MY_STATE;
326
327         if (!caller_is_localhost(rqstp))
328                 goto failure;
329
330         /* my_name must not have white space */
331         for (cp=my_name ; *cp ; cp++)
332                 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
333                         *cp = '_';
334
335
336         /* Check if we're monitoring anyone. */
337         if (rtnl == NULL) {
338                 xlog_warn("Received SM_UNMON request from %s for %s while not "
339                         "monitoring any hosts", my_name, argp->mon_name);
340                 return (&result);
341         }
342         clnt = rtnl;
343
344         /*
345          * OK, we are.  Now look for appropriate entry in run-time list.
346          * There should only be *one* match on this, since I block "duplicate"
347          * SM_MON calls.  (Actually, duplicate calls are allowed, but only one
348          * entry winds up in the list the way I'm currently handling them.)
349          */
350         while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
351                 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
352                         NL_MY_PROC(clnt) == id->my_proc &&
353                         NL_MY_PROG(clnt) == id->my_prog &&
354                         NL_MY_VERS(clnt) == id->my_vers) {
355                         /* Match! */
356                         xlog(D_GENERAL, "UNMONITORING %s for %s",
357                                         mon_name, my_name);
358
359                         /* PRC: do the HA callout: */
360                         ha_callout("del-client", mon_name, my_name, -1);
361
362                         xunlink(SM_DIR, clnt->dns_name);
363                         nlist_free(&rtnl, clnt);
364
365                         return (&result);
366                 } else
367                         clnt = NL_NEXT(clnt);
368         }
369
370  failure:
371         xlog_warn("Received erroneous SM_UNMON request from %s for %s",
372                 my_name, mon_name);
373         return (&result);
374 }
375
376
377 struct sm_stat *
378 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
379 {
380         short int       count = 0;
381         static sm_stat  result;
382         notify_list     *clnt;
383         char            *my_name = argp->my_name;
384
385         xlog(D_CALL, "Received SM_UNMON_ALL for %s", my_name);
386
387         if (!caller_is_localhost(rqstp))
388                 goto failure;
389
390         result.state = MY_STATE;
391
392         if (rtnl == NULL) {
393                 xlog_warn("Received SM_UNMON_ALL request from %s "
394                         "while not monitoring any hosts", my_name);
395                 return (&result);
396         }
397         clnt = rtnl;
398
399         while ((clnt = nlist_gethost(clnt, my_name, 1))) {
400                 if (NL_MY_PROC(clnt) == argp->my_proc &&
401                         NL_MY_PROG(clnt) == argp->my_prog &&
402                         NL_MY_VERS(clnt) == argp->my_vers) {
403                         /* Watch stack! */
404                         char            mon_name[SM_MAXSTRLEN + 1];
405                         notify_list     *temp;
406
407                         xlog(D_GENERAL,
408                                 "UNMONITORING (SM_UNMON_ALL) %s for %s",
409                                 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
410                         strncpy(mon_name, NL_MON_NAME(clnt),
411                                 sizeof (mon_name) - 1);
412                         mon_name[sizeof (mon_name) - 1] = '\0';
413                         temp = NL_NEXT(clnt);
414                         /* PRC: do the HA callout: */
415                         ha_callout("del-client", mon_name, my_name, -1);
416                         xunlink(SM_DIR, clnt->dns_name);
417                         nlist_free(&rtnl, clnt);
418                         ++count;
419                         clnt = temp;
420                 } else
421                         clnt = NL_NEXT(clnt);
422         }
423
424         if (!count) {
425                 xlog(D_GENERAL, "SM_UNMON_ALL request from %s with no "
426                         "SM_MON requests from it", my_name);
427         }
428
429  failure:
430         return (&result);
431 }