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>
26 #include "ha-callout.h"
28 notify_list * rtnl = NULL; /* Run-time notify list. */
30 #define LINELEN (4*(8+1)+SM_PRIV_SIZE*2+1)
33 * Services SM_MON requests.
36 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
38 static sm_stat_res result;
39 char *mon_name = argp->mon_id.mon_name,
40 *my_name = argp->mon_id.my_id.my_name;
41 struct my_id *id = &argp->mon_id.my_id;
45 struct in_addr my_addr;
46 #ifdef RESTRICTED_STATD
47 struct in_addr caller;
49 struct hostent *hostinfo = NULL;
52 /* Assume that we'll fail. */
53 result.res_stat = STAT_FAIL;
54 result.state = -1; /* State is undefined for STAT_FAIL. */
56 /* Restrict access to statd.
57 * In the light of CERT CA-99.05, we tighten access to
60 #ifdef RESTRICTED_STATD
61 /* 1. Reject anyone not calling from 127.0.0.1.
62 * Ignore the my_name specified by the caller, and
63 * use "127.0.0.1" instead.
65 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
66 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
68 "Call to statd from non-local host %s",
72 my_addr.s_addr = htonl(INADDR_LOOPBACK);
73 my_name = "127.0.0.1";
75 /* 2. Reject any registrations for non-lockd services.
77 * This is specific to the linux kernel lockd, which
78 * makes the callback procedure part of the lockd interface.
79 * It is also prone to break when lockd changes its callback
80 * procedure number -- which, in fact, has now happened once.
81 * There must be a better way.... XXX FIXME
83 if (id->my_prog != 100021 ||
84 (id->my_proc != 16 && id->my_proc != 24))
87 "Attempt to register callback to %d/%d",
88 id->my_prog, id->my_proc);
93 This is not usable anymore. Linux-kernel can be configured to use
94 host names with NSM so that multi-homed hosts are handled properly.
97 /* 3. mon_name must be an address in dotted quad.
98 * Again, specific to the linux kernel lockd.
100 if (!inet_aton(mon_name, &mon_addr)) {
102 "Attempt to register host %s (not a dotted quad)",
109 * Check hostnames. If I can't look them up, I won't monitor. This
110 * might not be legal, but it adds a little bit of safety and sanity.
113 /* must check for /'s in hostname! See CERT's CA-96.09 for details. */
114 if (strchr(mon_name, '/') || mon_name[0] == '.') {
115 note(N_CRIT, "SM_MON request for hostname containing '/' "
116 "or starting '.': %s", mon_name);
117 note(N_CRIT, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
119 } else if (gethostbyname(mon_name) == NULL) {
120 note(N_WARNING, "gethostbyname error for %s", mon_name);
123 #ifndef RESTRICTED_STATD
124 if (!(hostinfo = gethostbyname(my_name))) {
125 note(N_WARNING, "gethostbyname error for %s", my_name);
128 my_addr = *(struct in_addr *) hostinfo->h_addr;
132 * Hostnames checked OK.
133 * Now check to see if this is a duplicate, and warn if so.
134 * I will also return STAT_FAIL. (I *think* this is how I should
137 * Olaf requests that I allow duplicate SM_MON requests for
138 * hosts due to the way he is coding lockd. No problem,
139 * I'll just do a quickie success return and things should
144 while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
145 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
146 NL_MY_PROC(clnt) == id->my_proc &&
147 NL_MY_PROG(clnt) == id->my_prog &&
148 NL_MY_VERS(clnt) == id->my_vers) {
149 /* Hey! We already know you guys! */
151 "Duplicate SM_MON request for %s "
152 "from procedure on %s",
155 /* But we'll let you pass anyway. */
156 result.res_stat = STAT_SUCC;
157 result.state = MY_STATE;
160 clnt = NL_NEXT(clnt);
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|O_APPEND,
185 S_IRUSR|S_IWUSR)) < 0) {
186 /* Didn't fly. We won't monitor. */
187 note(N_ERROR, "creat(%s) failed: %s", path, strerror (errno));
188 nlist_free(NULL, clnt);
193 char buf[LINELEN + 1 + SM_MAXSTRLEN + 2];
196 e = buf + sprintf(buf, "%08x %08x %08x %08x ",
197 my_addr.s_addr, id->my_prog,
198 id->my_vers, id->my_proc);
199 for (i=0; i<SM_PRIV_SIZE; i++)
200 e += sprintf(e, "%02x", 0xff & (argp->priv[i]));
201 if (e+1-buf != LINELEN) abort();
202 e += sprintf(e, " %s\n", mon_name);
203 write(fd, buf, e-buf);
207 /* PRC: do the HA callout: */
208 ha_callout("add-client", mon_name, my_name, -1);
209 nlist_insert(&rtnl, clnt);
212 result.res_stat = STAT_SUCC;
213 result.state = MY_STATE;
214 dprintf(N_DEBUG, "MONITORING %s for %s", mon_name, my_name);
218 note(N_WARNING, "STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
222 void load_state(void)
226 char buf[LINELEN + 1 + SM_MAXSTRLEN + 2];
231 while ((de = readdir(d))) {
236 if (de->d_name[0] == '.')
238 path = xmalloc(strlen(SM_DIR)+strlen(de->d_name)+2);
239 sprintf(path, "%s/%s", SM_DIR, de->d_name);
240 f = fopen(path, "r");
244 while (fgets(buf, sizeof(buf), f) != NULL) {
245 int addr, proc, prog, vers;
246 char priv[SM_PRIV_SIZE];
251 buf[sizeof(buf)-1] = 0;
252 b = strchr(buf, '\n');
254 sscanf(buf, "%x %x %x %x ",
255 &addr, &prog, &vers, &proc);
257 for (i=0; i<SM_PRIV_SIZE; i++) {
258 sscanf(b, "%2x", &p);
263 clnt = nlist_new("127.0.0.1", b, 0);
266 NL_ADDR(clnt).s_addr = addr;
267 NL_MY_PROG(clnt) = prog;
268 NL_MY_VERS(clnt) = vers;
269 NL_MY_PROC(clnt) = proc;
270 memcpy(NL_PRIV(clnt), priv, SM_PRIV_SIZE);
271 nlist_insert(&rtnl, clnt);
282 * Services SM_UNMON requests.
284 * There is no statement in the X/Open spec's about returning an error
285 * for requests to unmonitor a host that we're *not* monitoring. I just
286 * return the state of the NSM when I get such foolish requests for lack
287 * of any better ideas. (I also log the "offense.")
290 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
292 static sm_stat result;
294 char *mon_name = argp->mon_name,
295 *my_name = argp->my_id.my_name;
296 struct my_id *id = &argp->my_id;
297 #ifdef RESTRICTED_STATD
298 struct in_addr caller;
301 result.state = MY_STATE;
303 #ifdef RESTRICTED_STATD
304 /* 1. Reject anyone not calling from 127.0.0.1.
305 * Ignore the my_name specified by the caller, and
306 * use "127.0.0.1" instead.
308 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
309 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
311 "Call to statd from non-local host %s",
315 my_name = "127.0.0.1";
318 /* Check if we're monitoring anyone. */
319 if (!(clnt = rtnl)) {
321 "Received SM_UNMON request from %s for %s while not "
322 "monitoring any hosts.", my_name, argp->mon_name);
327 * OK, we are. Now look for appropriate entry in run-time list.
328 * There should only be *one* match on this, since I block "duplicate"
329 * SM_MON calls. (Actually, duplicate calls are allowed, but only one
330 * entry winds up in the list the way I'm currently handling them.)
332 while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
333 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
334 NL_MY_PROC(clnt) == id->my_proc &&
335 NL_MY_PROG(clnt) == id->my_prog &&
336 NL_MY_VERS(clnt) == id->my_vers) {
338 dprintf(N_DEBUG, "UNMONITORING %s for %s",
341 /* PRC: do the HA callout: */
342 ha_callout("del-client", mon_name, my_name, -1);
344 nlist_free(&rtnl, clnt);
345 xunlink(SM_DIR, mon_name, 1);
349 clnt = NL_NEXT(clnt);
352 #ifdef RESTRICTED_STATD
355 note(N_WARNING, "Received erroneous SM_UNMON request from %s for %s",
362 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
365 static sm_stat result;
367 char *my_name = argp->my_name;
368 #ifdef RESTRICTED_STATD
369 struct in_addr caller;
371 /* 1. Reject anyone not calling from 127.0.0.1.
372 * Ignore the my_name specified by the caller, and
373 * use "127.0.0.1" instead.
375 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
376 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
378 "Call to statd from non-local host %s",
382 my_name = "127.0.0.1";
385 result.state = MY_STATE;
387 if (!(clnt = rtnl)) {
388 note(N_WARNING, "Received SM_UNMON_ALL request from %s "
389 "while not monitoring any hosts", my_name);
393 while ((clnt = nlist_gethost(clnt, my_name, 1))) {
394 if (NL_MY_PROC(clnt) == argp->my_proc &&
395 NL_MY_PROG(clnt) == argp->my_prog &&
396 NL_MY_VERS(clnt) == argp->my_vers) {
398 char mon_name[SM_MAXSTRLEN + 1];
402 "UNMONITORING (SM_UNMON_ALL) %s for %s",
403 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
404 strncpy(mon_name, NL_MON_NAME(clnt),
405 sizeof (mon_name) - 1);
406 mon_name[sizeof (mon_name) - 1] = '\0';
407 temp = NL_NEXT(clnt);
408 /* PRC: do the HA callout: */
409 ha_callout("del-client", mon_name, my_name, -1);
410 nlist_free(&rtnl, clnt);
411 xunlink(SM_DIR, mon_name, 1);
415 clnt = NL_NEXT(clnt);
419 dprintf(N_DEBUG, "SM_UNMON_ALL request from %s with no "
420 "SM_MON requests from it.", my_name);
422 #ifdef RESTRICTED_STATD