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>
28 #include "ha-callout.h"
30 notify_list * rtnl = NULL; /* Run-time notify list. */
32 #define LINELEN (4*(8+1)+SM_PRIV_SIZE*2+1)
35 * Reject requests from non-loopback addresses in order
36 * to prevent attack described in CERT CA-99.05.
39 caller_is_localhost(struct svc_req *rqstp)
41 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
42 struct in_addr caller;
44 caller = sin->sin_addr;
45 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
47 "Call to statd from non-local host %s",
55 * Services SM_MON requests.
58 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
60 static sm_stat_res result;
61 char *mon_name = argp->mon_id.mon_name,
62 *my_name = argp->mon_id.my_id.my_name;
63 struct my_id *id = &argp->mon_id.my_id;
68 struct in_addr my_addr;
70 struct hostent *hostinfo = NULL;
72 /* Assume that we'll fail. */
73 result.res_stat = STAT_FAIL;
74 result.state = -1; /* State is undefined for STAT_FAIL. */
76 /* 1. Reject any remote callers.
77 * Ignore the my_name specified by the caller, and
78 * use "127.0.0.1" instead.
80 if (!caller_is_localhost(rqstp))
82 my_addr.s_addr = htonl(INADDR_LOOPBACK);
84 /* 2. Reject any registrations for non-lockd services.
86 * This is specific to the linux kernel lockd, which
87 * makes the callback procedure part of the lockd interface.
88 * It is also prone to break when lockd changes its callback
89 * procedure number -- which, in fact, has now happened once.
90 * There must be a better way.... XXX FIXME
92 if (id->my_prog != 100021 ||
93 (id->my_proc != 16 && id->my_proc != 24))
96 "Attempt to register callback to %d/%d",
97 id->my_prog, id->my_proc);
102 * Check hostnames. If I can't look them up, I won't monitor. This
103 * might not be legal, but it adds a little bit of safety and sanity.
106 /* must check for /'s in hostname! See CERT's CA-96.09 for details. */
107 if (strchr(mon_name, '/') || mon_name[0] == '.') {
108 note(N_CRIT, "SM_MON request for hostname containing '/' "
109 "or starting '.': %s", mon_name);
110 note(N_CRIT, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
112 } else if ((hostinfo = gethostbyname(mon_name)) == NULL) {
113 note(N_WARNING, "gethostbyname error for %s", mon_name);
117 /* my_name must not have white space */
118 for (cp=my_name ; *cp ; cp++)
119 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
123 * Hostnames checked OK.
124 * Now choose a hostname to use for matching. We cannot
125 * really trust much in the incoming NOTIFY, so to make
126 * sure that multi-homed hosts work nicely, we get an
127 * FQDN now, and use that for matching
129 hostinfo = gethostbyaddr(hostinfo->h_addr,
131 hostinfo->h_addrtype);
133 dnsname = xstrdup(hostinfo->h_name);
135 dnsname = xstrdup(my_name);
137 /* Now check to see if this is a duplicate, and warn if so.
138 * I will also return STAT_FAIL. (I *think* this is how I should
141 * Olaf requests that I allow duplicate SM_MON requests for
142 * hosts due to the way he is coding lockd. No problem,
143 * I'll just do a quickie success return and things should
148 while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
149 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
150 NL_MY_PROC(clnt) == id->my_proc &&
151 NL_MY_PROG(clnt) == id->my_prog &&
152 NL_MY_VERS(clnt) == id->my_vers &&
153 memcmp(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE) == 0) {
154 /* Hey! We already know you guys! */
156 "Duplicate SM_MON request for %s "
157 "from procedure on %s",
160 /* But we'll let you pass anyway. */
163 clnt = NL_NEXT(clnt);
167 * We're committed...ignoring errors. Let's hope that a malloc()
168 * doesn't fail. (I should probably fix this assumption.)
170 if (!(clnt = nlist_new(my_name, mon_name, 0))) {
171 note(N_WARNING, "out of memory");
175 NL_ADDR(clnt) = my_addr;
176 NL_MY_PROG(clnt) = id->my_prog;
177 NL_MY_VERS(clnt) = id->my_vers;
178 NL_MY_PROC(clnt) = id->my_proc;
179 memcpy(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE);
180 clnt->dns_name = dnsname;
183 * Now, Create file on stable storage for host.
186 path=xmalloc(strlen(SM_DIR)+strlen(dnsname)+2);
187 sprintf(path, "%s/%s", SM_DIR, dnsname);
188 if ((fd = open(path, O_WRONLY|O_SYNC|O_CREAT|O_APPEND,
189 S_IRUSR|S_IWUSR)) < 0) {
190 /* Didn't fly. We won't monitor. */
191 note(N_ERROR, "creat(%s) failed: %s", path, strerror (errno));
192 nlist_free(NULL, clnt);
197 char buf[LINELEN + 1 + SM_MAXSTRLEN*2 + 4];
200 e = buf + sprintf(buf, "%08x %08x %08x %08x ",
201 my_addr.s_addr, id->my_prog,
202 id->my_vers, id->my_proc);
203 for (i=0; i<SM_PRIV_SIZE; i++)
204 e += sprintf(e, "%02x", 0xff & (argp->priv[i]));
205 if (e+1-buf != LINELEN) abort();
206 e += sprintf(e, " %s %s\n", mon_name, my_name);
207 if (write(fd, buf, e-buf) != (e-buf)) {
208 note(N_WARNING, "writing to %s failed: errno %d (%s)",
209 path, errno, strerror(errno));
214 /* PRC: do the HA callout: */
215 ha_callout("add-client", mon_name, my_name, -1);
216 nlist_insert(&rtnl, clnt);
218 dprintf(N_DEBUG, "MONITORING %s for %s", mon_name, my_name);
220 result.res_stat = STAT_SUCC;
221 /* SUN's sm_inter.x says this should be "state number of local site".
222 * X/Open says '"state" will be contain the state of the remote NSM.'
223 * href=http://www.opengroup.org/onlinepubs/9629799/SM_MON.htm
224 * Linux lockd currently (2.6.21 and prior) ignores whatever is
225 * returned, and given the above contraction, it probably always will..
226 * So we just return what we always returned. If possible, we
227 * have already told lockd about our state number via a sysctl.
228 * If lockd wants the remote state, it will need to
229 * use SM_STAT (and prayer).
231 result.state = MY_STATE;
235 note(N_WARNING, "STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
239 void load_state(void)
243 char buf[LINELEN + 1 + SM_MAXSTRLEN + 2];
248 while ((de = readdir(d))) {
253 if (de->d_name[0] == '.')
255 path = xmalloc(strlen(SM_DIR)+strlen(de->d_name)+2);
256 sprintf(path, "%s/%s", SM_DIR, de->d_name);
257 f = fopen(path, "r");
261 while (fgets(buf, sizeof(buf), f) != NULL) {
262 int addr, proc, prog, vers;
263 char priv[SM_PRIV_SIZE];
264 char *monname, *myname;
269 buf[sizeof(buf)-1] = 0;
270 b = strchr(buf, '\n');
272 sscanf(buf, "%x %x %x %x ",
273 &addr, &prog, &vers, &proc);
275 for (i=0; i<SM_PRIV_SIZE; i++) {
276 sscanf(b, "%2x", &p);
282 while (*b && *b != ' ') b++;
284 while (*b == ' ') b++;
286 clnt = nlist_new(myname, monname, 0);
289 NL_ADDR(clnt).s_addr = addr;
290 NL_MY_PROG(clnt) = prog;
291 NL_MY_VERS(clnt) = vers;
292 NL_MY_PROC(clnt) = proc;
293 clnt->dns_name = xstrdup(de->d_name);
294 memcpy(NL_PRIV(clnt), priv, SM_PRIV_SIZE);
295 nlist_insert(&rtnl, clnt);
306 * Services SM_UNMON requests.
308 * There is no statement in the X/Open spec's about returning an error
309 * for requests to unmonitor a host that we're *not* monitoring. I just
310 * return the state of the NSM when I get such foolish requests for lack
311 * of any better ideas. (I also log the "offense.")
314 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
316 static sm_stat result;
318 char *mon_name = argp->mon_name,
319 *my_name = argp->my_id.my_name;
320 struct my_id *id = &argp->my_id;
323 result.state = MY_STATE;
325 if (!caller_is_localhost(rqstp))
328 /* my_name must not have white space */
329 for (cp=my_name ; *cp ; cp++)
330 if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
334 /* Check if we're monitoring anyone. */
337 "Received SM_UNMON request from %s for %s while not "
338 "monitoring any hosts.", my_name, argp->mon_name);
344 * OK, we are. Now look for appropriate entry in run-time list.
345 * There should only be *one* match on this, since I block "duplicate"
346 * SM_MON calls. (Actually, duplicate calls are allowed, but only one
347 * entry winds up in the list the way I'm currently handling them.)
349 while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
350 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
351 NL_MY_PROC(clnt) == id->my_proc &&
352 NL_MY_PROG(clnt) == id->my_prog &&
353 NL_MY_VERS(clnt) == id->my_vers) {
355 dprintf(N_DEBUG, "UNMONITORING %s for %s",
358 /* PRC: do the HA callout: */
359 ha_callout("del-client", mon_name, my_name, -1);
361 xunlink(SM_DIR, clnt->dns_name);
362 nlist_free(&rtnl, clnt);
366 clnt = NL_NEXT(clnt);
370 note(N_WARNING, "Received erroneous SM_UNMON request from %s for %s",
377 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
380 static sm_stat result;
382 char *my_name = argp->my_name;
384 if (!caller_is_localhost(rqstp))
387 result.state = MY_STATE;
390 note(N_WARNING, "Received SM_UNMON_ALL request from %s "
391 "while not monitoring any hosts", my_name);
396 while ((clnt = nlist_gethost(clnt, my_name, 1))) {
397 if (NL_MY_PROC(clnt) == argp->my_proc &&
398 NL_MY_PROG(clnt) == argp->my_prog &&
399 NL_MY_VERS(clnt) == argp->my_vers) {
401 char mon_name[SM_MAXSTRLEN + 1];
405 "UNMONITORING (SM_UNMON_ALL) %s for %s",
406 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
407 strncpy(mon_name, NL_MON_NAME(clnt),
408 sizeof (mon_name) - 1);
409 mon_name[sizeof (mon_name) - 1] = '\0';
410 temp = NL_NEXT(clnt);
411 /* PRC: do the HA callout: */
412 ha_callout("del-client", mon_name, my_name, -1);
413 xunlink(SM_DIR, clnt->dns_name);
414 nlist_free(&rtnl, clnt);
418 clnt = NL_NEXT(clnt);
422 dprintf(N_DEBUG, "SM_UNMON_ALL request from %s with no "
423 "SM_MON requests from it.", my_name);