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.
18 #include <arpa/inet.h>
22 #include "ha-callout.h"
24 notify_list * rtnl = NULL; /* Run-time notify list. */
28 * Services SM_MON requests.
31 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
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;
40 struct in_addr my_addr;
41 #ifdef RESTRICTED_STATD
42 struct in_addr mon_addr, caller;
44 struct hostent *hostinfo = NULL;
47 /* Assume that we'll fail. */
48 result.res_stat = STAT_FAIL;
49 result.state = -1; /* State is undefined for STAT_FAIL. */
51 /* Restrict access to statd.
52 * In the light of CERT CA-99.05, we tighten access to
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.
60 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
61 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
63 "Call to statd from non-local host %s",
67 my_addr.s_addr = htonl(INADDR_LOOPBACK);
68 my_name = "127.0.0.1";
70 /* 2. Reject any registrations for non-lockd services.
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
78 if (id->my_prog != 100021 ||
79 (id->my_proc != 16 && id->my_proc != 24))
82 "Attempt to register callback to %d/%d",
83 id->my_prog, id->my_proc);
87 /* 3. mon_name must be an address in dotted quad.
88 * Again, specific to the linux kernel lockd.
90 if (!inet_aton(mon_name, &mon_addr)) {
92 "Attempt to register host %s (not a dotted quad)",
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.
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",
106 note(N_CRIT, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
108 } else if (gethostbyname(mon_name) == NULL) {
109 note(N_WARNING, "gethostbyname error for %s", mon_name);
111 } else if (!(hostinfo = gethostbyname(my_name))) {
112 note(N_WARNING, "gethostbyname error for %s", my_name);
115 my_addr = *(struct in_addr *) hostinfo->h_addr;
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
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
130 notify_list *temp = rtnl;
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! */
139 "Duplicate SM_MON request for %s "
140 "from procedure on %s",
143 /* But we'll let you pass anyway. */
144 result.res_stat = STAT_SUCC;
145 result.state = MY_STATE;
148 temp = NL_NEXT(temp);
153 * We're committed...ignoring errors. Let's hope that a malloc()
154 * doesn't fail. (I should probably fix this assumption.)
156 if (!(clnt = nlist_new(my_name, mon_name, 0))) {
157 note(N_WARNING, "out of memory");
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);
168 * Now, Create file on stable storage for host.
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);
181 /* PRC: do the HA callout: */
182 ha_callout("add-client", mon_name, my_name, -1);
183 nlist_insert(&rtnl, clnt);
186 result.res_stat = STAT_SUCC;
187 result.state = MY_STATE;
188 dprintf(N_DEBUG, "MONITORING %s for %s", mon_name, my_name);
192 note(N_WARNING, "STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
198 * Services SM_UNMON requests.
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.")
206 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
208 static sm_stat result;
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;
217 result.state = MY_STATE;
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.
224 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
225 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
227 "Call to statd from non-local host %s",
231 my_name = "127.0.0.1";
234 /* Check if we're monitoring anyone. */
235 if (!(clnt = rtnl)) {
237 "Received SM_UNMON request from %s for %s while not "
238 "monitoring any hosts.", my_name, argp->mon_name);
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.)
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) {
254 dprintf(N_DEBUG, "UNMONITORING %s for %s",
257 /* PRC: do the HA callout: */
258 ha_callout("del-client", mon_name, my_name, -1);
260 nlist_free(&rtnl, clnt);
261 xunlink(SM_DIR, mon_name, 1);
265 clnt = NL_NEXT(clnt);
269 note(N_WARNING, "Received erroneous SM_UNMON request from %s for %s",
276 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
279 static sm_stat result;
281 char *my_name = argp->my_name;
282 #ifdef RESTRICTED_STATD
283 struct in_addr caller;
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.
289 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
290 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
292 "Call to statd from non-local host %s",
296 my_name = "127.0.0.1";
299 result.state = MY_STATE;
301 if (!(clnt = rtnl)) {
302 note(N_WARNING, "Received SM_UNMON_ALL request from %s "
303 "while not monitoring any hosts", my_name);
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) {
312 char mon_name[SM_MAXSTRLEN + 1];
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);
329 clnt = NL_NEXT(clnt);
333 dprintf(N_DEBUG, "SM_UNMON_ALL request from %s with no "
334 "SM_MON requests from it.", my_name);