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