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