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