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