]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/monitor.c
8ee04414af050ef6c6de8a16bfeed2e6afd899fc
[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 "misc.h"
23 #include "statd.h"
24 #include "notlist.h"
25 #include "ha-callout.h"
26
27 notify_list *           rtnl = NULL;    /* Run-time notify list. */
28
29
30 /*
31  * Services SM_MON requests.
32  */
33 struct sm_stat_res *
34 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
35 {
36         static sm_stat_res result;
37         char            *mon_name = argp->mon_id.mon_name,
38                         *my_name  = argp->mon_id.my_id.my_name;
39         struct my_id    *id = &argp->mon_id.my_id;
40         char            *path;
41         int             fd;
42         notify_list     *clnt;
43         struct in_addr  my_addr;
44 #ifdef RESTRICTED_STATD
45         struct in_addr  caller;
46 #else
47         struct hostent  *hostinfo = NULL;
48 #endif
49
50         /* Assume that we'll fail. */
51         result.res_stat = STAT_FAIL;
52         result.state = -1;      /* State is undefined for STAT_FAIL. */
53
54         /* Restrict access to statd.
55          * In the light of CERT CA-99.05, we tighten access to
56          * statd.                       --okir
57          */
58 #ifdef RESTRICTED_STATD
59         /* 1.   Reject anyone not calling from 127.0.0.1.
60          *      Ignore the my_name specified by the caller, and
61          *      use "127.0.0.1" instead.
62          */
63         caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
64         if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
65                 note(N_WARNING,
66                         "Call to statd from non-local host %s",
67                         inet_ntoa(caller));
68                 goto failure;
69         }
70         my_addr.s_addr = htonl(INADDR_LOOPBACK);
71         my_name = "127.0.0.1";
72
73         /* 2.   Reject any registrations for non-lockd services.
74          *
75          *      This is specific to the linux kernel lockd, which
76          *      makes the callback procedure part of the lockd interface.
77          *      It is also prone to break when lockd changes its callback
78          *      procedure number -- which, in fact, has now happened once.
79          *      There must be a better way....   XXX FIXME
80          */
81         if (id->my_prog != 100021 ||
82             (id->my_proc != 16 && id->my_proc != 24))
83         {
84                 note(N_WARNING,
85                         "Attempt to register callback to %d/%d",
86                         id->my_prog, id->my_proc);
87                 goto failure;
88         }
89
90 #if 0
91         This is not usable anymore.  Linux-kernel can be configured to use
92         host names with NSM so that multi-homed hosts are handled properly.
93                 NeilBrown 15mar2007
94
95         /* 3.   mon_name must be an address in dotted quad.
96          *      Again, specific to the linux kernel lockd.
97          */
98         if (!inet_aton(mon_name, &mon_addr)) {
99                 note(N_WARNING,
100                         "Attempt to register host %s (not a dotted quad)",
101                         mon_name);
102                 goto failure;
103         }
104 #endif
105 #endif
106         /*
107          * Check hostnames.  If I can't look them up, I won't monitor.  This
108          * might not be legal, but it adds a little bit of safety and sanity.
109          */
110
111         /* must check for /'s in hostname!  See CERT's CA-96.09 for details. */
112         if (strchr(mon_name, '/') || mon_name[0] == '.') {
113                 note(N_CRIT, "SM_MON request for hostname containing '/' "
114                      "or starting '.': %s", mon_name);
115                 note(N_CRIT, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
116                 goto failure;
117         } else if (gethostbyname(mon_name) == NULL) {
118                 note(N_WARNING, "gethostbyname error for %s", mon_name);
119                 goto failure;
120         }
121 #ifndef RESTRICTED_STATD
122         if (!(hostinfo = gethostbyname(my_name))) {
123                 note(N_WARNING, "gethostbyname error for %s", my_name);
124                 goto failure;
125         } else
126                 my_addr = *(struct in_addr *) hostinfo->h_addr;
127 #endif
128
129         /*
130          * Hostnames checked OK.
131          * Now check to see if this is a duplicate, and warn if so.
132          * I will also return STAT_FAIL. (I *think* this is how I should
133          * handle it.)
134          *
135          * Olaf requests that I allow duplicate SM_MON requests for
136          * hosts due to the way he is coding lockd. No problem,
137          * I'll just do a quickie success return and things should
138          * be happy.
139          */
140         if (rtnl) {
141                 notify_list    *temp = rtnl;
142
143                 while ((temp = nlist_gethost(temp, mon_name, 0))) {
144                         if (matchhostname(NL_MY_NAME(temp), my_name) &&
145                                 NL_MY_PROC(temp) == id->my_proc &&
146                                 NL_MY_PROG(temp) == id->my_prog &&
147                                 NL_MY_VERS(temp) == id->my_vers) {
148                                 /* Hey!  We already know you guys! */
149                                 dprintf(N_DEBUG,
150                                         "Duplicate SM_MON request for %s "
151                                         "from procedure on %s",
152                                         mon_name, my_name);
153
154                                 /* But we'll let you pass anyway. */
155                                 result.res_stat = STAT_SUCC;
156                                 result.state = MY_STATE;
157                                 return (&result);
158                         }
159                         temp = NL_NEXT(temp);
160                 }
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
178         /*
179          * Now, Create file on stable storage for host.
180          */
181
182         path=xmalloc(strlen(SM_DIR)+strlen(mon_name)+2);
183         sprintf(path, "%s/%s", SM_DIR, mon_name);
184         if ((fd = open(path, O_WRONLY|O_SYNC|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
185                 /* Didn't fly.  We won't monitor. */
186                 note(N_ERROR, "creat(%s) failed: %s", path, strerror (errno));
187                 nlist_free(NULL, clnt);
188                 free(path);
189                 goto failure;
190         }
191         free(path);
192         /* PRC: do the HA callout: */
193         ha_callout("add-client", mon_name, my_name, -1);
194         nlist_insert(&rtnl, clnt);
195         close(fd);
196
197         result.res_stat = STAT_SUCC;
198         result.state = MY_STATE;
199         dprintf(N_DEBUG, "MONITORING %s for %s", mon_name, my_name);
200         return (&result);
201
202 failure:
203         note(N_WARNING, "STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
204         return (&result);
205 }
206
207
208 /*
209  * Services SM_UNMON requests.
210  *
211  * There is no statement in the X/Open spec's about returning an error
212  * for requests to unmonitor a host that we're *not* monitoring.  I just
213  * return the state of the NSM when I get such foolish requests for lack
214  * of any better ideas.  (I also log the "offense.")
215  */
216 struct sm_stat *
217 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
218 {
219         static sm_stat  result;
220         notify_list     *clnt;
221         char            *mon_name = argp->mon_name,
222                         *my_name  = argp->my_id.my_name;
223         struct my_id    *id = &argp->my_id;
224 #ifdef RESTRICTED_STATD
225         struct in_addr  caller;
226 #endif
227
228         result.state = MY_STATE;
229
230 #ifdef RESTRICTED_STATD
231         /* 1.   Reject anyone not calling from 127.0.0.1.
232          *      Ignore the my_name specified by the caller, and
233          *      use "127.0.0.1" instead.
234          */
235         caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
236         if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
237                 note(N_WARNING,
238                         "Call to statd from non-local host %s",
239                         inet_ntoa(caller));
240                 goto failure;
241         }
242         my_name = "127.0.0.1";
243 #endif
244
245         /* Check if we're monitoring anyone. */
246         if (!(clnt = rtnl)) {
247                 note(N_WARNING,
248                         "Received SM_UNMON request from %s for %s while not "
249                         "monitoring any hosts.", my_name, argp->mon_name);
250                 return (&result);
251         }
252
253         /*
254          * OK, we are.  Now look for appropriate entry in run-time list.
255          * There should only be *one* match on this, since I block "duplicate"
256          * SM_MON calls.  (Actually, duplicate calls are allowed, but only one
257          * entry winds up in the list the way I'm currently handling them.)
258          */
259         while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
260                 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
261                         NL_MY_PROC(clnt) == id->my_proc &&
262                         NL_MY_PROG(clnt) == id->my_prog &&
263                         NL_MY_VERS(clnt) == id->my_vers) {
264                         /* Match! */
265                         dprintf(N_DEBUG, "UNMONITORING %s for %s",
266                                         mon_name, my_name);
267
268                         /* PRC: do the HA callout: */
269                         ha_callout("del-client", mon_name, my_name, -1);
270
271                         nlist_free(&rtnl, clnt);
272                         xunlink(SM_DIR, mon_name, 1);
273
274                         return (&result);
275                 } else
276                         clnt = NL_NEXT(clnt);
277         }
278
279 #ifdef RESTRICTED_STATD
280  failure:
281 #endif
282         note(N_WARNING, "Received erroneous SM_UNMON request from %s for %s",
283                 my_name, mon_name);
284         return (&result);
285 }
286
287
288 struct sm_stat *
289 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
290 {
291         short int       count = 0;
292         static sm_stat  result;
293         notify_list     *clnt;
294         char            *my_name = argp->my_name;
295 #ifdef RESTRICTED_STATD
296         struct in_addr  caller;
297
298         /* 1.   Reject anyone not calling from 127.0.0.1.
299          *      Ignore the my_name specified by the caller, and
300          *      use "127.0.0.1" instead.
301          */
302         caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
303         if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
304                 note(N_WARNING,
305                         "Call to statd from non-local host %s",
306                         inet_ntoa(caller));
307                 goto failure;
308         }
309         my_name = "127.0.0.1";
310 #endif
311
312         result.state = MY_STATE;
313
314         if (!(clnt = rtnl)) {
315                 note(N_WARNING, "Received SM_UNMON_ALL request from %s "
316                         "while not monitoring any hosts", my_name);
317                 return (&result);
318         }
319
320         while ((clnt = nlist_gethost(clnt, my_name, 1))) {
321                 if (NL_MY_PROC(clnt) == argp->my_proc &&
322                         NL_MY_PROG(clnt) == argp->my_prog &&
323                         NL_MY_VERS(clnt) == argp->my_vers) {
324                         /* Watch stack! */
325                         char            mon_name[SM_MAXSTRLEN + 1];
326                         notify_list     *temp;
327
328                         dprintf(N_DEBUG,
329                                 "UNMONITORING (SM_UNMON_ALL) %s for %s",
330                                 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
331                         strncpy(mon_name, NL_MON_NAME(clnt),
332                                 sizeof (mon_name) - 1);
333                         mon_name[sizeof (mon_name) - 1] = '\0';
334                         temp = NL_NEXT(clnt);
335                         /* PRC: do the HA callout: */
336                         ha_callout("del-client", mon_name, my_name, -1);
337                         nlist_free(&rtnl, clnt);
338                         xunlink(SM_DIR, mon_name, 1);
339                         ++count;
340                         clnt = temp;
341                 } else
342                         clnt = NL_NEXT(clnt);
343         }
344
345         if (!count) {
346                 dprintf(N_DEBUG, "SM_UNMON_ALL request from %s with no "
347                         "SM_MON requests from it.", my_name);
348         }
349 #ifdef RESTRICTED_STATD
350  failure:
351 #endif
352         return (&result);
353 }