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