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.
19 #include <arpa/inet.h>
23 #include "ha-callout.h"
25 notify_list * rtnl = NULL; /* Run-time notify list. */
29 * Services SM_MON requests.
32 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
34 static sm_stat_res result;
35 char *mon_name = argp->mon_id.mon_name,
36 *my_name = argp->mon_id.my_id.my_name;
37 struct my_id *id = &argp->mon_id.my_id;
41 struct in_addr my_addr;
42 #ifdef RESTRICTED_STATD
43 struct in_addr mon_addr, caller;
45 struct hostent *hostinfo = NULL;
48 /* Assume that we'll fail. */
49 result.res_stat = STAT_FAIL;
50 result.state = -1; /* State is undefined for STAT_FAIL. */
52 /* Restrict access to statd.
53 * In the light of CERT CA-99.05, we tighten access to
56 #ifdef RESTRICTED_STATD
57 /* 1. Reject anyone not calling from 127.0.0.1.
58 * Ignore the my_name specified by the caller, and
59 * use "127.0.0.1" instead.
61 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
62 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
64 "Call to statd from non-local host %s",
68 my_addr.s_addr = htonl(INADDR_LOOPBACK);
69 my_name = "127.0.0.1";
71 /* 2. Reject any registrations for non-lockd services.
73 * This is specific to the linux kernel lockd, which
74 * makes the callback procedure part of the lockd interface.
75 * It is also prone to break when lockd changes its callback
76 * procedure number -- which, in fact, has now happened once.
77 * There must be a better way.... XXX FIXME
79 if (id->my_prog != 100021 ||
80 (id->my_proc != 16 && id->my_proc != 24))
83 "Attempt to register callback to %d/%d",
84 id->my_prog, id->my_proc);
88 /* 3. mon_name must be an address in dotted quad.
89 * Again, specific to the linux kernel lockd.
91 if (!inet_aton(mon_name, &mon_addr)) {
93 "Attempt to register host %s (not a dotted quad)",
99 * Check hostnames. If I can't look them up, I won't monitor. This
100 * might not be legal, but it adds a little bit of safety and sanity.
103 /* must check for /'s in hostname! See CERT's CA-96.09 for details. */
104 if (strchr(mon_name, '/')) {
105 note(N_CRIT, "SM_MON request for hostname containing '/': %s",
107 note(N_CRIT, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
109 } else if (gethostbyname(mon_name) == NULL) {
110 note(N_WARNING, "gethostbyname error for %s", mon_name);
112 } else if (!(hostinfo = gethostbyname(my_name))) {
113 note(N_WARNING, "gethostbyname error for %s", my_name);
116 my_addr = *(struct in_addr *) hostinfo->h_addr;
120 * Hostnames checked OK.
121 * Now check to see if this is a duplicate, and warn if so.
122 * I will also return STAT_FAIL. (I *think* this is how I should
125 * Olaf requests that I allow duplicate SM_MON requests for
126 * hosts due to the way he is coding lockd. No problem,
127 * I'll just do a quickie success return and things should
131 notify_list *temp = rtnl;
133 while ((temp = nlist_gethost(temp, mon_name, 0))) {
134 if (matchhostname(NL_MY_NAME(temp), my_name) &&
135 NL_MY_PROC(temp) == id->my_proc &&
136 NL_MY_PROG(temp) == id->my_prog &&
137 NL_MY_VERS(temp) == id->my_vers) {
138 /* Hey! We already know you guys! */
140 "Duplicate SM_MON request for %s "
141 "from procedure on %s",
144 /* But we'll let you pass anyway. */
145 result.res_stat = STAT_SUCC;
146 result.state = MY_STATE;
149 temp = NL_NEXT(temp);
154 * We're committed...ignoring errors. Let's hope that a malloc()
155 * doesn't fail. (I should probably fix this assumption.)
157 if (!(clnt = nlist_new(my_name, mon_name, 0))) {
158 note(N_WARNING, "out of memory");
162 NL_ADDR(clnt) = my_addr;
163 NL_MY_PROG(clnt) = id->my_prog;
164 NL_MY_VERS(clnt) = id->my_vers;
165 NL_MY_PROC(clnt) = id->my_proc;
166 memcpy(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE);
169 * Now, Create file on stable storage for host.
172 path=xmalloc(strlen(SM_DIR)+strlen(mon_name)+2);
173 sprintf(path, "%s/%s", SM_DIR, mon_name);
174 if ((fd = open(path, O_WRONLY|O_SYNC|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
175 /* Didn't fly. We won't monitor. */
176 note(N_ERROR, "creat(%s) failed: %s", path, strerror (errno));
177 nlist_free(NULL, clnt);
182 /* PRC: do the HA callout: */
183 ha_callout("add-client", mon_name, my_name, -1);
184 nlist_insert(&rtnl, clnt);
187 result.res_stat = STAT_SUCC;
188 result.state = MY_STATE;
189 dprintf(N_DEBUG, "MONITORING %s for %s", mon_name, my_name);
193 note(N_WARNING, "STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
199 * Services SM_UNMON requests.
201 * There is no statement in the X/Open spec's about returning an error
202 * for requests to unmonitor a host that we're *not* monitoring. I just
203 * return the state of the NSM when I get such foolish requests for lack
204 * of any better ideas. (I also log the "offense.")
207 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
209 static sm_stat result;
211 char *mon_name = argp->mon_name,
212 *my_name = argp->my_id.my_name;
213 struct my_id *id = &argp->my_id;
214 #ifdef RESTRICTED_STATD
215 struct in_addr caller;
218 result.state = MY_STATE;
220 #ifdef RESTRICTED_STATD
221 /* 1. Reject anyone not calling from 127.0.0.1.
222 * Ignore the my_name specified by the caller, and
223 * use "127.0.0.1" instead.
225 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
226 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
228 "Call to statd from non-local host %s",
232 my_name = "127.0.0.1";
235 /* Check if we're monitoring anyone. */
236 if (!(clnt = rtnl)) {
238 "Received SM_UNMON request from %s for %s while not "
239 "monitoring any hosts.", my_name, argp->mon_name);
244 * OK, we are. Now look for appropriate entry in run-time list.
245 * There should only be *one* match on this, since I block "duplicate"
246 * SM_MON calls. (Actually, duplicate calls are allowed, but only one
247 * entry winds up in the list the way I'm currently handling them.)
249 while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
250 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
251 NL_MY_PROC(clnt) == id->my_proc &&
252 NL_MY_PROG(clnt) == id->my_prog &&
253 NL_MY_VERS(clnt) == id->my_vers) {
255 dprintf(N_DEBUG, "UNMONITORING %s for %s",
258 /* PRC: do the HA callout: */
259 ha_callout("del-client", mon_name, my_name, -1);
261 nlist_free(&rtnl, clnt);
262 xunlink(SM_DIR, mon_name, 1);
266 clnt = NL_NEXT(clnt);
270 note(N_WARNING, "Received erroneous SM_UNMON request from %s for %s",
277 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
280 static sm_stat result;
282 char *my_name = argp->my_name;
283 #ifdef RESTRICTED_STATD
284 struct in_addr caller;
286 /* 1. Reject anyone not calling from 127.0.0.1.
287 * Ignore the my_name specified by the caller, and
288 * use "127.0.0.1" instead.
290 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
291 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
293 "Call to statd from non-local host %s",
297 my_name = "127.0.0.1";
300 result.state = MY_STATE;
302 if (!(clnt = rtnl)) {
303 note(N_WARNING, "Received SM_UNMON_ALL request from %s "
304 "while not monitoring any hosts", my_name);
308 while ((clnt = nlist_gethost(clnt, my_name, 1))) {
309 if (NL_MY_PROC(clnt) == argp->my_proc &&
310 NL_MY_PROG(clnt) == argp->my_prog &&
311 NL_MY_VERS(clnt) == argp->my_vers) {
313 char mon_name[SM_MAXSTRLEN + 1];
317 "UNMONITORING (SM_UNMON_ALL) %s for %s",
318 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
319 strncpy(mon_name, NL_MON_NAME(clnt),
320 sizeof (mon_name) - 1);
321 mon_name[sizeof (mon_name) - 1] = '\0';
322 temp = NL_NEXT(clnt);
323 /* PRC: do the HA callout: */
324 ha_callout("del-client", mon_name, my_name, -1);
325 nlist_free(&rtnl, clnt);
326 xunlink(SM_DIR, mon_name, 1);
330 clnt = NL_NEXT(clnt);
334 dprintf(N_DEBUG, "SM_UNMON_ALL request from %s with no "
335 "SM_MON requests from it.", my_name);