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.
21 #include <arpa/inet.h>
25 #include "ha-callout.h"
27 notify_list * rtnl = NULL; /* Run-time notify list. */
31 * Services SM_MON requests.
34 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
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;
43 struct in_addr my_addr;
44 #ifdef RESTRICTED_STATD
45 struct in_addr caller;
47 struct hostent *hostinfo = NULL;
50 /* Assume that we'll fail. */
51 result.res_stat = STAT_FAIL;
52 result.state = -1; /* State is undefined for STAT_FAIL. */
54 /* Restrict access to statd.
55 * In the light of CERT CA-99.05, we tighten access to
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.
63 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
64 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
66 "Call to statd from non-local host %s",
70 my_addr.s_addr = htonl(INADDR_LOOPBACK);
71 my_name = "127.0.0.1";
73 /* 2. Reject any registrations for non-lockd services.
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
81 if (id->my_prog != 100021 ||
82 (id->my_proc != 16 && id->my_proc != 24))
85 "Attempt to register callback to %d/%d",
86 id->my_prog, id->my_proc);
91 This is not usable anymore. Linux-kernel can be configured to use
92 host names with NSM so that multi-homed hosts are handled properly.
95 /* 3. mon_name must be an address in dotted quad.
96 * Again, specific to the linux kernel lockd.
98 if (!inet_aton(mon_name, &mon_addr)) {
100 "Attempt to register host %s (not a dotted quad)",
107 * Check hostnames. If I can't look them up, I won't monitor. This
108 * might not be legal, but it adds a little bit of safety and sanity.
111 /* must check for /'s in hostname! See CERT's CA-96.09 for details. */
112 if (strchr(mon_name, '/') || mon_name[0] == '.') {
113 note(N_CRIT, "SM_MON request for hostname containing '/' "
114 "or starting '.': %s", mon_name);
115 note(N_CRIT, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
117 } else if (gethostbyname(mon_name) == NULL) {
118 note(N_WARNING, "gethostbyname error for %s", mon_name);
121 #ifndef RESTRICTED_STATD
122 if (!(hostinfo = gethostbyname(my_name))) {
123 note(N_WARNING, "gethostbyname error for %s", my_name);
126 my_addr = *(struct in_addr *) hostinfo->h_addr;
130 * Hostnames checked OK.
131 * Now check to see if this is a duplicate, and warn if so.
132 * I will also return STAT_FAIL. (I *think* this is how I should
135 * Olaf requests that I allow duplicate SM_MON requests for
136 * hosts due to the way he is coding lockd. No problem,
137 * I'll just do a quickie success return and things should
141 notify_list *temp = rtnl;
143 while ((temp = nlist_gethost(temp, mon_name, 0))) {
144 if (matchhostname(NL_MY_NAME(temp), my_name) &&
145 NL_MY_PROC(temp) == id->my_proc &&
146 NL_MY_PROG(temp) == id->my_prog &&
147 NL_MY_VERS(temp) == id->my_vers) {
148 /* Hey! We already know you guys! */
150 "Duplicate SM_MON request for %s "
151 "from procedure on %s",
154 /* But we'll let you pass anyway. */
155 result.res_stat = STAT_SUCC;
156 result.state = MY_STATE;
159 temp = NL_NEXT(temp);
164 * We're committed...ignoring errors. Let's hope that a malloc()
165 * doesn't fail. (I should probably fix this assumption.)
167 if (!(clnt = nlist_new(my_name, mon_name, 0))) {
168 note(N_WARNING, "out of memory");
172 NL_ADDR(clnt) = my_addr;
173 NL_MY_PROG(clnt) = id->my_prog;
174 NL_MY_VERS(clnt) = id->my_vers;
175 NL_MY_PROC(clnt) = id->my_proc;
176 memcpy(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE);
179 * Now, Create file on stable storage for host.
182 path=xmalloc(strlen(SM_DIR)+strlen(mon_name)+2);
183 sprintf(path, "%s/%s", SM_DIR, mon_name);
184 if ((fd = open(path, O_WRONLY|O_SYNC|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
185 /* Didn't fly. We won't monitor. */
186 note(N_ERROR, "creat(%s) failed: %s", path, strerror (errno));
187 nlist_free(NULL, clnt);
192 /* PRC: do the HA callout: */
193 ha_callout("add-client", mon_name, my_name, -1);
194 nlist_insert(&rtnl, clnt);
197 result.res_stat = STAT_SUCC;
198 result.state = MY_STATE;
199 dprintf(N_DEBUG, "MONITORING %s for %s", mon_name, my_name);
203 note(N_WARNING, "STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
209 * Services SM_UNMON requests.
211 * There is no statement in the X/Open spec's about returning an error
212 * for requests to unmonitor a host that we're *not* monitoring. I just
213 * return the state of the NSM when I get such foolish requests for lack
214 * of any better ideas. (I also log the "offense.")
217 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
219 static sm_stat result;
221 char *mon_name = argp->mon_name,
222 *my_name = argp->my_id.my_name;
223 struct my_id *id = &argp->my_id;
224 #ifdef RESTRICTED_STATD
225 struct in_addr caller;
228 result.state = MY_STATE;
230 #ifdef RESTRICTED_STATD
231 /* 1. Reject anyone not calling from 127.0.0.1.
232 * Ignore the my_name specified by the caller, and
233 * use "127.0.0.1" instead.
235 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
236 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
238 "Call to statd from non-local host %s",
242 my_name = "127.0.0.1";
245 /* Check if we're monitoring anyone. */
246 if (!(clnt = rtnl)) {
248 "Received SM_UNMON request from %s for %s while not "
249 "monitoring any hosts.", my_name, argp->mon_name);
254 * OK, we are. Now look for appropriate entry in run-time list.
255 * There should only be *one* match on this, since I block "duplicate"
256 * SM_MON calls. (Actually, duplicate calls are allowed, but only one
257 * entry winds up in the list the way I'm currently handling them.)
259 while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
260 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
261 NL_MY_PROC(clnt) == id->my_proc &&
262 NL_MY_PROG(clnt) == id->my_prog &&
263 NL_MY_VERS(clnt) == id->my_vers) {
265 dprintf(N_DEBUG, "UNMONITORING %s for %s",
268 /* PRC: do the HA callout: */
269 ha_callout("del-client", mon_name, my_name, -1);
271 nlist_free(&rtnl, clnt);
272 xunlink(SM_DIR, mon_name, 1);
276 clnt = NL_NEXT(clnt);
279 #ifdef RESTRICTED_STATD
282 note(N_WARNING, "Received erroneous SM_UNMON request from %s for %s",
289 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
292 static sm_stat result;
294 char *my_name = argp->my_name;
295 #ifdef RESTRICTED_STATD
296 struct in_addr caller;
298 /* 1. Reject anyone not calling from 127.0.0.1.
299 * Ignore the my_name specified by the caller, and
300 * use "127.0.0.1" instead.
302 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
303 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
305 "Call to statd from non-local host %s",
309 my_name = "127.0.0.1";
312 result.state = MY_STATE;
314 if (!(clnt = rtnl)) {
315 note(N_WARNING, "Received SM_UNMON_ALL request from %s "
316 "while not monitoring any hosts", my_name);
320 while ((clnt = nlist_gethost(clnt, my_name, 1))) {
321 if (NL_MY_PROC(clnt) == argp->my_proc &&
322 NL_MY_PROG(clnt) == argp->my_prog &&
323 NL_MY_VERS(clnt) == argp->my_vers) {
325 char mon_name[SM_MAXSTRLEN + 1];
329 "UNMONITORING (SM_UNMON_ALL) %s for %s",
330 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
331 strncpy(mon_name, NL_MON_NAME(clnt),
332 sizeof (mon_name) - 1);
333 mon_name[sizeof (mon_name) - 1] = '\0';
334 temp = NL_NEXT(clnt);
335 /* PRC: do the HA callout: */
336 ha_callout("del-client", mon_name, my_name, -1);
337 nlist_free(&rtnl, clnt);
338 xunlink(SM_DIR, mon_name, 1);
342 clnt = NL_NEXT(clnt);
346 dprintf(N_DEBUG, "SM_UNMON_ALL request from %s with no "
347 "SM_MON requests from it.", my_name);
349 #ifdef RESTRICTED_STATD