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>
23 notify_list * rtnl = NULL; /* Run-time notify list. */
27 * Services SM_MON requests.
30 sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
32 static sm_stat_res result;
33 char *mon_name = argp->mon_id.mon_name,
34 *my_name = argp->mon_id.my_id.my_name;
35 struct my_id *id = &argp->mon_id.my_id;
39 struct in_addr my_addr;
40 #ifdef RESTRICTED_STATD
41 struct in_addr mon_addr, caller;
43 struct hostent *hostinfo = NULL;
46 /* Assume that we'll fail. */
47 result.res_stat = STAT_FAIL;
48 result.state = -1; /* State is undefined for STAT_FAIL. */
50 /* Restrict access to statd.
51 * In the light of CERT CA-99.05, we tighten access to
54 #ifdef RESTRICTED_STATD
55 /* 1. Reject anyone not calling from 127.0.0.1.
56 * Ignore the my_name specified by the caller, and
57 * use "127.0.0.1" instead.
59 caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
60 if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
62 "Call to statd from non-local host %s",
66 my_addr.s_addr = htonl(INADDR_LOOPBACK);
67 my_name = "127.0.0.1";
69 /* 2. Reject any registrations for non-lockd services.
71 * This is specific to the linux kernel lockd, which
72 * makes the callback procedure part of the lockd interface.
73 * It is also prone to break when lockd changes its callback
74 * procedure number -- which, in fact, has now happened once.
75 * There must be a better way.... XXX FIXME
77 if (id->my_prog != 100021 ||
78 (id->my_proc != 16 && id->my_proc != 24))
81 "Attempt to register callback to %d/%d",
82 id->my_prog, id->my_proc);
86 /* 3. mon_name must be an address in dotted quad.
87 * Again, specific to the linux kernel lockd.
89 if (!inet_aton(mon_name, &mon_addr)) {
91 "Attempt to register host %s (not a dotted quad)",
97 * Check hostnames. If I can't look them up, I won't monitor. This
98 * might not be legal, but it adds a little bit of safety and sanity.
101 /* must check for /'s in hostname! See CERT's CA-96.09 for details. */
102 if (strchr(mon_name, '/')) {
103 log(L_CRIT, "SM_MON request for hostname containing '/': %s",
105 log(L_CRIT, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
107 } else if (gethostbyname(mon_name) == NULL) {
108 log(L_WARNING, "gethostbyname error for %s", mon_name);
110 } else if (!(hostinfo = gethostbyname(my_name))) {
111 log(L_WARNING, "gethostbyname error for %s", my_name);
114 my_addr = *(struct in_addr *) hostinfo->h_addr;
118 * Hostnames checked OK.
119 * Now check to see if this is a duplicate, and warn if so.
120 * I will also return STAT_FAIL. (I *think* this is how I should
123 * Olaf requests that I allow duplicate SM_MON requests for
124 * hosts due to the way he is coding lockd. No problem,
125 * I'll just do a quickie success return and things should
129 notify_list *temp = rtnl;
131 while ((temp = nlist_gethost(temp, mon_name, 0))) {
132 if (matchhostname(NL_MY_NAME(temp), my_name) &&
133 NL_MY_PROC(temp) == id->my_proc &&
134 NL_MY_PROG(temp) == id->my_prog &&
135 NL_MY_VERS(temp) == id->my_vers) {
136 /* Hey! We already know you guys! */
138 "Duplicate SM_MON request for %s "
139 "from procedure on %s",
142 /* But we'll let you pass anyway. */
143 result.res_stat = STAT_SUCC;
144 result.state = MY_STATE;
147 temp = NL_NEXT(temp);
152 * We're committed...ignoring errors. Let's hope that a malloc()
153 * doesn't fail. (I should probably fix this assumption.)
155 if (!(clnt = nlist_new(my_name, mon_name, 0))) {
156 log(L_WARNING, "out of memory");
160 NL_ADDR(clnt) = my_addr;
161 NL_MY_PROG(clnt) = id->my_prog;
162 NL_MY_VERS(clnt) = id->my_vers;
163 NL_MY_PROC(clnt) = id->my_proc;
164 memcpy(NL_PRIV(clnt), argp->priv, SM_PRIV_SIZE);
167 * Now, Create file on stable storage for host.
170 path=xmalloc(strlen(SM_DIR)+strlen(mon_name)+2);
171 sprintf(path, "%s/%s", SM_DIR, mon_name);
172 if ((fd = open(path, O_WRONLY|O_SYNC|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
173 /* Didn't fly. We won't monitor. */
174 log(L_ERROR, "creat(%s) failed: %m", path);
175 nlist_free(NULL, clnt);
180 nlist_insert(&rtnl, clnt);
183 result.res_stat = STAT_SUCC;
184 result.state = MY_STATE;
185 dprintf(L_DEBUG, "MONITORING %s for %s", mon_name, my_name);
189 log(L_WARNING, "STAT_FAIL to %s for SM_MON of %s", my_name, mon_name);
195 * Services SM_UNMON requests.
197 * There is no statement in the X/Open spec's about returning an error
198 * for requests to unmonitor a host that we're *not* monitoring. I just
199 * return the state of the NSM when I get such foolish requests for lack
200 * of any better ideas. (I also log the "offense.")
203 sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
205 static sm_stat result;
207 char *mon_name = argp->mon_name,
208 *my_name = argp->my_id.my_name;
209 struct my_id *id = &argp->my_id;
211 result.state = MY_STATE;
213 /* Check if we're monitoring anyone. */
214 if (!(clnt = rtnl)) {
216 "Received SM_UNMON request from %s for %s while not "
217 "monitoring any hosts.", my_name, argp->mon_name);
222 * OK, we are. Now look for appropriate entry in run-time list.
223 * There should only be *one* match on this, since I block "duplicate"
224 * SM_MON calls. (Actually, duplicate calls are allowed, but only one
225 * entry winds up in the list the way I'm currently handling them.)
227 while ((clnt = nlist_gethost(clnt, mon_name, 0))) {
228 if (matchhostname(NL_MY_NAME(clnt), my_name) &&
229 NL_MY_PROC(clnt) == id->my_proc &&
230 NL_MY_PROG(clnt) == id->my_prog &&
231 NL_MY_VERS(clnt) == id->my_vers) {
233 dprintf(L_DEBUG, "UNMONITORING %s for %s",
235 nlist_free(&rtnl, clnt);
236 xunlink(SM_DIR, mon_name, 1);
240 clnt = NL_NEXT(clnt);
243 log(L_WARNING, "Received erroneous SM_UNMON request from %s for %s",
250 sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
253 static sm_stat result;
256 result.state = MY_STATE;
258 if (!(clnt = rtnl)) {
259 log(L_WARNING, "Received SM_UNMON_ALL request from %s "
260 "while not monitoring any hosts", argp->my_name);
264 while ((clnt = nlist_gethost(clnt, argp->my_name, 1))) {
265 if (NL_MY_PROC(clnt) == argp->my_proc &&
266 NL_MY_PROG(clnt) == argp->my_prog &&
267 NL_MY_VERS(clnt) == argp->my_vers) {
269 char mon_name[SM_MAXSTRLEN + 1];
273 "UNMONITORING (SM_UNMON_ALL) %s for %s",
274 NL_MON_NAME(clnt), NL_MY_NAME(clnt));
275 strncpy(mon_name, NL_MON_NAME(clnt),
276 sizeof (mon_name) - 1);
277 mon_name[sizeof (mon_name) - 1] = '\0';
278 temp = NL_NEXT(clnt);
279 nlist_free(&rtnl, clnt);
280 xunlink(SM_DIR, mon_name, 1);
284 clnt = NL_NEXT(clnt);
288 dprintf(L_DEBUG, "SM_UNMON_ALL request from %s with no "
289 "SM_MON requests from it.", argp->my_name);