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