xref: /openbsd-src/usr.sbin/rpc.lockd/procs.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: procs.c,v 1.11 2003/07/06 21:26:14 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1995
5  *	A.R. Gordon (andrew.gordon@net-tel.co.uk).  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the FreeBSD project
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <rpc/rpc.h>
40 #include <rpc/pmap_clnt.h>
41 #include <rpcsvc/sm_inter.h>
42 #include "nlm_prot.h"
43 #include <arpa/inet.h>
44 #include <stdio.h>
45 #include <syslog.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <netdb.h>
49 
50 #include "lockd.h"
51 
52 #define	CLIENT_CACHE_SIZE	64	/* No. of client sockets cached	 */
53 #define	CLIENT_CACHE_LIFETIME	120	/* In seconds			 */
54 
55 static void
56 log_from_addr(char *fun_name, struct svc_req *req)
57 {
58 	struct	sockaddr_in *addr;
59 	struct	hostent *host;
60 	char	hostname_buf[MAXHOSTNAMELEN];
61 
62 	addr = svc_getcaller(req->rq_xprt);
63 	host = gethostbyaddr((char *) &(addr->sin_addr), addr->sin_len, AF_INET);
64 	if (host)
65 		strlcpy(hostname_buf, host->h_name, sizeof(hostname_buf));
66 	else
67 		strlcpy(hostname_buf, inet_ntoa(addr->sin_addr),
68 		    sizeof hostname_buf);
69 	syslog(LOG_DEBUG, "%s from %s", fun_name, hostname_buf);
70 }
71 
72 
73 static CLIENT *clnt_cache_ptr[CLIENT_CACHE_SIZE];
74 static long clnt_cache_time[CLIENT_CACHE_SIZE];	/* time entry created	 */
75 static struct in_addr clnt_cache_addr[CLIENT_CACHE_SIZE];
76 static int clnt_cache_next_to_use = 0;
77 
78 static CLIENT *
79 get_client(struct sockaddr_in *host_addr)
80 {
81 	CLIENT *client;
82 	int     sock_no, i;
83 	struct timeval retry_time, time_now;
84 
85 	gettimeofday(&time_now, NULL);
86 
87 	/* Search for the given client in the cache, zapping any expired	 */
88 	/* entries that we happen to notice in passing.			 */
89 	for (i = 0; i < CLIENT_CACHE_SIZE; i++) {
90 		client = clnt_cache_ptr[i];
91 		if (client &&
92 		    ((clnt_cache_time[i] + CLIENT_CACHE_LIFETIME) < time_now.tv_sec)) {
93 			/* Cache entry has expired. */
94 			if (debug_level > 3)
95 				syslog(LOG_DEBUG, "Expired CLIENT* in cache");
96 			clnt_cache_time[i] = 0L;
97 			clnt_destroy(client);
98 			clnt_cache_ptr[i] = NULL;
99 			client = NULL;
100 		}
101 		if (client && !memcmp(&clnt_cache_addr[i], &host_addr->sin_addr,
102 			sizeof(struct in_addr))) {
103 			/* Found it! */
104 			if (debug_level > 3)
105 				syslog(LOG_DEBUG, "Found CLIENT* in cache");
106 			return (client);
107 		}
108 	}
109 
110 	/* Not found in cache.  Free the next entry if it is in use */
111 	if (clnt_cache_ptr[clnt_cache_next_to_use]) {
112 		clnt_destroy(clnt_cache_ptr[clnt_cache_next_to_use]);
113 		clnt_cache_ptr[clnt_cache_next_to_use] = NULL;
114 	}
115 
116 	sock_no = RPC_ANYSOCK;
117 	retry_time.tv_sec = 5;
118 	retry_time.tv_usec = 0;
119 	host_addr->sin_port = 0;
120 	client = clntudp_create(host_addr, NLM_PROG, NLM_VERS, retry_time, &sock_no);
121 	if (!client) {
122 		syslog(LOG_ERR, "%s", clnt_spcreateerror("clntudp_create"));
123 		syslog(LOG_ERR, "Unable to return result to %s",
124 		    inet_ntoa(host_addr->sin_addr));
125 		return (NULL);
126 	}
127 	clnt_cache_ptr[clnt_cache_next_to_use] = client;
128 	clnt_cache_addr[clnt_cache_next_to_use] = host_addr->sin_addr;
129 	clnt_cache_time[clnt_cache_next_to_use] = time_now.tv_sec;
130 	if (++clnt_cache_next_to_use > CLIENT_CACHE_SIZE)
131 		clnt_cache_next_to_use = 0;
132 
133 	retry_time.tv_sec = -1;
134 	retry_time.tv_usec = -1;
135 	clnt_control(client, CLSET_TIMEOUT, &retry_time);
136 
137 	if (debug_level > 3)
138 		syslog(LOG_DEBUG, "Created CLIENT* for %s",
139 		    inet_ntoa(host_addr->sin_addr));
140 	return (client);
141 }
142 
143 
144 static void
145 transmit_result(int opcode, nlm_res *result, struct svc_req *req)
146 {
147 	static char dummy;
148 	struct sockaddr_in *addr;
149 	CLIENT *cli;
150 	int     success;
151 	struct timeval timeo;
152 
153 	addr = svc_getcaller(req->rq_xprt);
154 	if ((cli = get_client(addr))) {
155 		timeo.tv_sec = 0;
156 		timeo.tv_usec = 0;
157 
158 		success = clnt_call(cli, opcode, xdr_nlm_res, result, xdr_void,
159 		    &dummy, timeo);
160 		if (debug_level > 2)
161 			syslog(LOG_DEBUG, "clnt_call returns %d", success);
162 	}
163 }
164 
165 nlm_testres *
166 nlm_test_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
167 {
168 	static nlm_testres res;
169 
170 	if (debug_level)
171 		log_from_addr("nlm_test", rqstp);
172 	res.cookie = arg->cookie;
173 	res.stat.stat = nlm_granted;
174 	return (&res);
175 }
176 
177 void *
178 nlm_test_msg_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
179 {
180 	nlm_testres res;
181 	static char dummy;
182 	struct sockaddr_in *addr;
183 	CLIENT *cli;
184 	int     success;
185 	struct timeval timeo;
186 
187 	if (debug_level)
188 		log_from_addr("nlm_test_msg", rqstp);
189 
190 	res.cookie = arg->cookie;
191 	res.stat.stat = nlm_granted;
192 
193 	addr = svc_getcaller(rqstp->rq_xprt);
194 	if ((cli = get_client(addr))) {
195 		timeo.tv_sec = 0;
196 		timeo.tv_usec = 0;
197 		success = clnt_call(cli, NLM_TEST_RES, xdr_nlm_testres, &res, xdr_void,
198 		    &dummy, timeo);
199 		if (debug_level > 2)
200 			syslog(LOG_DEBUG, "clnt_call returns %d", success);
201 	}
202 	return (NULL);
203 }
204 
205 nlm_res *
206 nlm_lock_1_svc(nlm_lockargs *arg, struct svc_req *rqstp)
207 {
208 	static nlm_res res;
209 
210 	if (debug_level)
211 		log_from_addr("nlm_lock", rqstp);
212 	res.cookie = arg->cookie;
213 	res.stat.stat = nlm_granted;
214 	return (&res);
215 }
216 
217 void *
218 nlm_lock_msg_1_svc(nlm_lockargs *arg, struct svc_req *rqstp)
219 {
220 	static nlm_res res;
221 
222 	if (debug_level)
223 		log_from_addr("nlm_lock_msg", rqstp);
224 	res.cookie = arg->cookie;
225 	res.stat.stat = nlm_granted;
226 	transmit_result(NLM_LOCK_RES, &res, rqstp);
227 	return (NULL);
228 }
229 
230 nlm_res *
231 nlm_cancel_1_svc(nlm_cancargs *arg, struct svc_req *rqstp)
232 {
233 	static nlm_res res;
234 
235 	if (debug_level)
236 		log_from_addr("nlm_cancel", rqstp);
237 	res.cookie = arg->cookie;
238 	res.stat.stat = nlm_denied;
239 	return (&res);
240 }
241 
242 void *
243 nlm_cancel_msg_1_svc(nlm_cancargs *arg, struct svc_req *rqstp)
244 {
245 	static nlm_res res;
246 
247 	if (debug_level)
248 		log_from_addr("nlm_cancel_msg", rqstp);
249 	res.cookie = arg->cookie;
250 	res.stat.stat = nlm_denied;
251 	transmit_result(NLM_CANCEL_RES, &res, rqstp);
252 	return (NULL);
253 }
254 
255 nlm_res *
256 nlm_unlock_1_svc(nlm_unlockargs *arg, struct svc_req *rqstp)
257 {
258 	static nlm_res res;
259 
260 	if (debug_level)
261 		log_from_addr("nlm_unlock", rqstp);
262 	res.stat.stat = nlm_granted;
263 	res.cookie = arg->cookie;
264 	return (&res);
265 }
266 
267 void *
268 nlm_unlock_msg_1_svc(nlm_unlockargs *arg, struct svc_req *rqstp)
269 {
270 	static nlm_res res;
271 
272 	if (debug_level)
273 		log_from_addr("nlm_unlock_msg", rqstp);
274 	res.stat.stat = nlm_granted;
275 	res.cookie = arg->cookie;
276 	transmit_result(NLM_UNLOCK_RES, &res, rqstp);
277 	return (NULL);
278 }
279 
280 nlm_res *
281 nlm_granted_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
282 {
283 	static nlm_res res;
284 
285 	if (debug_level)
286 		log_from_addr("nlm_granted", rqstp);
287 	res.cookie = arg->cookie;
288 	res.stat.stat = nlm_granted;
289 	return (&res);
290 }
291 
292 void *
293 nlm_granted_msg_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
294 {
295 	nlm_res res;
296 
297 	if (debug_level)
298 		log_from_addr("nlm_granted_msg", rqstp);
299 	res.cookie = arg->cookie;
300 	res.stat.stat = nlm_granted;
301 	transmit_result(NLM_GRANTED_RES, &res, rqstp);
302 	return (NULL);
303 }
304 
305 void *
306 nlm_test_res_1_svc(nlm_testres *arg, struct svc_req *rqstp)
307 {
308 	if (debug_level)
309 		log_from_addr("nlm_test_res", rqstp);
310 	return (NULL);
311 }
312 
313 void *
314 nlm_lock_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
315 {
316 	if (debug_level)
317 		log_from_addr("nlm_lock_res", rqstp);
318 
319 	return (NULL);
320 }
321 
322 void *
323 nlm_cancel_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
324 {
325 	if (debug_level)
326 		log_from_addr("nlm_cancel_res", rqstp);
327 	return (NULL);
328 }
329 
330 void *
331 nlm_unlock_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
332 {
333 	if (debug_level)
334 		log_from_addr("nlm_unlock_res", rqstp);
335 	return (NULL);
336 }
337 
338 void *
339 nlm_granted_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
340 {
341 	if (debug_level)
342 		log_from_addr("nlm_granted_res", rqstp);
343 	return (NULL);
344 }
345 
346 nlm_shareres *
347 nlm_share_3_svc(nlm_shareargs *arg, struct svc_req *rqstp)
348 {
349 	static nlm_shareres res;
350 
351 	if (debug_level)
352 		log_from_addr("nlm_share", rqstp);
353 	res.cookie = arg->cookie;
354 	res.stat = nlm_granted;
355 	res.sequence = 1234356;	/* X/Open says this field is ignored?	 */
356 	return (&res);
357 }
358 
359 nlm_shareres *
360 nlm_unshare_3_svc(nlm_shareargs *arg, struct svc_req *rqstp)
361 {
362 	static nlm_shareres res;
363 
364 	if (debug_level)
365 		log_from_addr("nlm_unshare", rqstp);
366 	res.cookie = arg->cookie;
367 	res.stat = nlm_granted;
368 	res.sequence = 1234356;	/* X/Open says this field is ignored?	 */
369 	return (&res);
370 }
371 
372 nlm_res *
373 nlm_nm_lock_3_svc(nlm_lockargs *arg, struct svc_req *rqstp)
374 {
375 	static nlm_res res;
376 
377 	if (debug_level)
378 		log_from_addr("nlm_nm_lock", rqstp);
379 	res.cookie = arg->cookie;
380 	res.stat.stat = nlm_granted;
381 	return (&res);
382 }
383 
384 void *
385 nlm_free_all_3_svc(nlm_notify *arg, struct svc_req *rqstp)
386 {
387 	static char dummy;
388 
389 	if (debug_level)
390 		log_from_addr("nlm_free_all", rqstp);
391 	return (&dummy);
392 }
393