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