1 /* $NetBSD: lock_proc.c,v 1.7 2000/10/11 20:23:56 is Exp $ */
5 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: lock_proc.c,v 1.7 2000/10/11 20:23:56 is Exp $");
41 #include <sys/param.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
52 #include <netconfig.h>
55 #include <rpcsvc/sm_inter.h>
58 #include <rpcsvc/nlm_prot.h>
59 #include "lockd_lock.h"
62 #define CLIENT_CACHE_SIZE 64 /* No. of client sockets cached */
63 #define CLIENT_CACHE_LIFETIME 120 /* In seconds */
65 #define getrpcaddr(rqstp) (struct sockaddr *)(svc_getrpccaller((rqstp)->rq_xprt)->buf)
67 static void log_from_addr(const char *, struct svc_req *);
68 static void log_netobj(netobj *obj);
69 static int addrcmp(struct sockaddr *, struct sockaddr *);
71 /* log_from_addr ----------------------------------------------------------- */
73 * Purpose: Log name of function called and source address
75 * Notes: Extracts the source address from the transport handle
76 * passed in as part of the called procedure specification
79 log_from_addr(fun_name, req)
83 struct sockaddr *addr;
84 char hostname_buf[NI_MAXHOST];
86 addr = svc_getrpccaller(req->rq_xprt)->buf;
87 if (getnameinfo(addr , addr->sa_len, hostname_buf, sizeof hostname_buf,
91 syslog(LOG_DEBUG, "%s from %s", fun_name, hostname_buf);
94 /* log_netobj ----------------------------------------------------------- */
96 * Purpose: Log a netobj
98 * Notes: This function should only really be called as part of
105 char objvalbuffer[(sizeof(char)*2)*MAX_NETOBJ_SZ+2];
106 char objascbuffer[sizeof(char)*MAX_NETOBJ_SZ+1];
107 unsigned int i, maxlen;
110 /* Notify of potential security attacks */
111 if (obj->n_len > MAX_NETOBJ_SZ) {
112 syslog(LOG_DEBUG, "SOMEONE IS TRYING TO DO SOMETHING NASTY!\n");
113 syslog(LOG_DEBUG, "netobj too large! Should be %d was %d\n",
114 MAX_NETOBJ_SZ, obj->n_len);
116 /* Prevent the security hazard from the buffer overflow */
117 maxlen = (obj->n_len < MAX_NETOBJ_SZ ? obj->n_len : MAX_NETOBJ_SZ);
118 for (i=0, tmp1 = objvalbuffer, tmp2 = objascbuffer; i < maxlen;
119 i++, tmp1 +=2, tmp2 +=1) {
120 sprintf(tmp1,"%02X",*(obj->n_bytes+i));
121 sprintf(tmp2,"%c",*(obj->n_bytes+i));
125 syslog(LOG_DEBUG,"netobjvals: %s\n",objvalbuffer);
126 syslog(LOG_DEBUG,"netobjascs: %s\n",objascbuffer);
128 /* get_client -------------------------------------------------------------- */
130 * Purpose: Get a CLIENT* for making RPC calls to lockd on given host
131 * Returns: CLIENT* pointer, from clnt_udp_create, or NULL if error
132 * Notes: Creating a CLIENT* is quite expensive, involving a
133 * conversation with the remote portmapper to get the
134 * port number. Since a given client is quite likely
135 * to make several locking requests in succession, it is
136 * desirable to cache the created CLIENT*.
138 * Since we are using UDP rather than TCP, there is no cost
139 * to the remote system in keeping these cached indefinitely.
140 * Unfortunately there is a snag: if the remote system
141 * reboots, the cached portmapper results will be invalid,
142 * and we will never detect this since all of the xxx_msg()
143 * calls return no result - we just fire off a udp packet
144 * and hope for the best.
146 * We solve this by discarding cached values after two
147 * minutes, regardless of whether they have been used
148 * in the meanwhile (since a bad one might have been used
149 * plenty of times, as the host keeps retrying the request
150 * and we keep sending the reply back to the wrong port).
152 * Given that the entries will always expire in the order
153 * that they were created, there is no point in a LRU
154 * algorithm for when the cache gets full - entries are
155 * always re-used in sequence.
157 static CLIENT *clnt_cache_ptr[CLIENT_CACHE_SIZE];
158 static long clnt_cache_time[CLIENT_CACHE_SIZE]; /* time entry created */
159 static struct sockaddr_storage clnt_cache_addr[CLIENT_CACHE_SIZE];
160 static rpcvers_t clnt_cache_vers[CLIENT_CACHE_SIZE];
161 static int clnt_cache_next_to_use = 0;
165 struct sockaddr *sa1;
166 struct sockaddr *sa2;
171 if (sa1->sa_family != sa2->sa_family)
174 switch (sa1->sa_family) {
176 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
177 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
181 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
182 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
189 return memcmp(p1, p2, len);
193 get_client(host_addr, vers)
194 struct sockaddr *host_addr;
198 struct timeval retry_time, time_now;
201 struct netconfig *nconf;
202 char host[NI_MAXHOST];
206 gettimeofday(&time_now, NULL);
209 * Search for the given client in the cache, zapping any expired
210 * entries that we happen to notice in passing.
212 for (i = 0; i < CLIENT_CACHE_SIZE; i++) {
213 client = clnt_cache_ptr[i];
214 if (client && ((clnt_cache_time[i] + CLIENT_CACHE_LIFETIME)
215 < time_now.tv_sec)) {
216 /* Cache entry has expired. */
218 syslog(LOG_DEBUG, "Expired CLIENT* in cache");
219 clnt_cache_time[i] = 0L;
220 clnt_destroy(client);
221 clnt_cache_ptr[i] = NULL;
224 if (client && !addrcmp((struct sockaddr *)&clnt_cache_addr[i],
225 host_addr) && clnt_cache_vers[i] == vers) {
228 syslog(LOG_DEBUG, "Found CLIENT* in cache");
234 syslog(LOG_DEBUG, "CLIENT* not found in cache, creating");
236 /* Not found in cache. Free the next entry if it is in use. */
237 if (clnt_cache_ptr[clnt_cache_next_to_use]) {
238 clnt_destroy(clnt_cache_ptr[clnt_cache_next_to_use]);
239 clnt_cache_ptr[clnt_cache_next_to_use] = NULL;
243 * Need a host string for clnt_tp_create. Use NI_NUMERICHOST
244 * to avoid DNS lookups.
246 error = getnameinfo(host_addr, host_addr->sa_len, host, sizeof host,
247 NULL, 0, NI_NUMERICHOST);
249 syslog(LOG_ERR, "unable to get name string for caller: %s",
250 gai_strerror(error));
255 if (host_addr->sa_family == AF_INET6)
260 if (host_addr->sa_family == AF_INET6)
265 nconf = getnetconfigent(netid);
267 syslog(LOG_ERR, "could not get netconfig info for '%s': "
268 "no /etc/netconfig file?", netid);
272 client = clnt_tp_create(host, NLM_PROG, vers, nconf);
273 freenetconfigent(nconf);
276 syslog(LOG_ERR, "%s", clnt_spcreateerror("clntudp_create"));
277 syslog(LOG_ERR, "Unable to return result to %s", host);
281 /* Get the FD of the client, for bindresvport. */
282 clnt_control(client, CLGET_FD, &clnt_fd);
284 /* Regain root privileges, for bindresvport. */
285 old_euid = geteuid();
289 * Bind the client FD to a reserved port.
290 * Some NFS servers reject any NLM request from a non-reserved port.
292 bindresvport(clnt_fd, NULL);
294 /* Drop root privileges again. */
297 /* Success - update the cache entry */
298 clnt_cache_ptr[clnt_cache_next_to_use] = client;
299 memcpy(&clnt_cache_addr[clnt_cache_next_to_use], host_addr,
301 clnt_cache_vers[clnt_cache_next_to_use] = vers;
302 clnt_cache_time[clnt_cache_next_to_use] = time_now.tv_sec;
303 if (++clnt_cache_next_to_use >= CLIENT_CACHE_SIZE)
304 clnt_cache_next_to_use = 0;
307 * Disable the default timeout, so we can specify our own in calls
308 * to clnt_call(). (Note that the timeout is a different concept
309 * from the retry period set in clnt_udp_create() above.)
311 retry_time.tv_sec = -1;
312 retry_time.tv_usec = -1;
313 clnt_control(client, CLSET_TIMEOUT, (char *)&retry_time);
316 syslog(LOG_DEBUG, "Created CLIENT* for %s", host);
321 /* transmit_result --------------------------------------------------------- */
323 * Purpose: Transmit result for nlm_xxx_msg pseudo-RPCs
324 * Returns: Nothing - we have no idea if the datagram got there
325 * Notes: clnt_call() will always fail (with timeout) as we are
326 * calling it with timeout 0 as a hack to just issue a datagram
327 * without expecting a result
330 transmit_result(opcode, result, addr)
333 struct sockaddr *addr;
337 struct timeval timeo;
340 if ((cli = get_client(addr, NLM_VERS)) != NULL) {
341 timeo.tv_sec = 0; /* No timeout - not expecting response */
344 success = clnt_call(cli, opcode, (xdrproc_t)xdr_nlm_res, result,
345 (xdrproc_t)xdr_void, &dummy, timeo);
348 syslog(LOG_DEBUG, "clnt_call returns %d(%s)",
349 success, clnt_sperrno(success));
352 /* transmit4_result --------------------------------------------------------- */
354 * Purpose: Transmit result for nlm4_xxx_msg pseudo-RPCs
355 * Returns: Nothing - we have no idea if the datagram got there
356 * Notes: clnt_call() will always fail (with timeout) as we are
357 * calling it with timeout 0 as a hack to just issue a datagram
358 * without expecting a result
361 transmit4_result(opcode, result, addr)
364 struct sockaddr *addr;
368 struct timeval timeo;
371 if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
372 timeo.tv_sec = 0; /* No timeout - not expecting response */
375 success = clnt_call(cli, opcode,
376 (xdrproc_t)xdr_nlm4_res, result,
377 (xdrproc_t)xdr_void, &dummy, timeo);
380 syslog(LOG_DEBUG, "clnt_call returns %d(%s)",
381 success, clnt_sperrno(success));
386 * converts a struct nlm_lock to struct nlm4_lock
388 static void nlmtonlm4(struct nlm_lock *, struct nlm4_lock *);
391 struct nlm_lock *arg;
392 struct nlm4_lock *arg4;
394 arg4->caller_name = arg->caller_name;
397 arg4->svid = arg->svid;
398 arg4->l_offset = arg->l_offset;
399 arg4->l_len = arg->l_len;
401 /* ------------------------------------------------------------------------- */
403 * Functions for Unix<->Unix locking (ie. monitored locking, with rpc.statd
404 * involved to ensure reclaim of locks after a crash of the "stateless"
407 * These all come in two flavours - nlm_xxx() and nlm_xxx_msg().
408 * The first are standard RPCs with argument and result.
409 * The nlm_xxx_msg() calls implement exactly the same functions, but
410 * use two pseudo-RPCs (one in each direction). These calls are NOT
411 * standard use of the RPC protocol in that they do not return a result
412 * at all (NB. this is quite different from returning a void result).
413 * The effect of this is to make the nlm_xxx_msg() calls simple unacknowledged
414 * datagrams, requiring higher-level code to perform retries.
416 * Despite the disadvantages of the nlm_xxx_msg() approach (some of which
417 * are documented in the comments to get_client() above), this is the
418 * interface used by all current commercial NFS implementations
419 * [Solaris, SCO, AIX etc.]. This is presumed to be because these allow
420 * implementations to continue using the standard RPC libraries, while
421 * avoiding the block-until-result nature of the library interface.
423 * No client implementations have been identified so far that make use
424 * of the true RPC version (early SunOS releases would be a likely candidate
428 /* nlm_test ---------------------------------------------------------------- */
430 * Purpose: Test whether a specified lock would be granted if requested
431 * Returns: nlm_granted (or error code)
435 nlm_test_1_svc(arg, rqstp)
437 struct svc_req *rqstp;
439 static nlm_testres res;
440 struct nlm4_lock arg4;
441 struct nlm4_holder *holder;
442 nlmtonlm4(&arg->alock, &arg4);
445 log_from_addr("nlm_test", rqstp);
447 holder = testlock(&arg4, arg->exclusive, 0);
449 * Copy the cookie from the argument into the result. Note that this
450 * is slightly hazardous, as the structure contains a pointer to a
451 * malloc()ed buffer that will get freed by the caller. However, the
452 * main function transmits the result before freeing the argument
453 * so it is in fact safe.
455 res.cookie = arg->cookie;
456 if (holder == NULL) {
457 res.stat.stat = nlm_granted;
459 res.stat.stat = nlm_denied;
460 memcpy(&res.stat.nlm_testrply_u.holder, holder,
461 sizeof(struct nlm_holder));
462 res.stat.nlm_testrply_u.holder.l_offset = holder->l_offset;
463 res.stat.nlm_testrply_u.holder.l_len = holder->l_len;
469 nlm_test_msg_1_svc(arg, rqstp)
471 struct svc_req *rqstp;
475 struct sockaddr *addr;
478 struct timeval timeo;
479 struct nlm4_lock arg4;
480 struct nlm4_holder *holder;
482 nlmtonlm4(&arg->alock, &arg4);
485 log_from_addr("nlm_test_msg", rqstp);
487 holder = testlock(&arg4, arg->exclusive, 0);
489 res.cookie = arg->cookie;
490 if (holder == NULL) {
491 res.stat.stat = nlm_granted;
493 res.stat.stat = nlm_denied;
494 memcpy(&res.stat.nlm_testrply_u.holder, holder,
495 sizeof(struct nlm_holder));
496 res.stat.nlm_testrply_u.holder.l_offset = holder->l_offset;
497 res.stat.nlm_testrply_u.holder.l_len = holder->l_len;
501 * nlm_test has different result type to the other operations, so
502 * can't use transmit_result() in this case
504 addr = svc_getrpccaller(rqstp->rq_xprt)->buf;
505 if ((cli = get_client(addr, NLM_VERS)) != NULL) {
506 timeo.tv_sec = 0; /* No timeout - not expecting response */
509 success = clnt_call(cli, NLM_TEST_RES,
510 (xdrproc_t)xdr_nlm_testres, &res,
511 (xdrproc_t)xdr_void, &dummy, timeo);
514 syslog(LOG_DEBUG, "clnt_call returns %d", success);
519 /* nlm_lock ---------------------------------------------------------------- */
521 * Purposes: Establish a lock
522 * Returns: granted, denied or blocked
523 * Notes: *** grace period support missing
526 nlm_lock_1_svc(arg, rqstp)
528 struct svc_req *rqstp;
531 struct nlm4_lockargs arg4;
532 nlmtonlm4(&arg->alock, &arg4.alock);
533 arg4.cookie = arg->cookie;
534 arg4.block = arg->block;
535 arg4.exclusive = arg->exclusive;
536 arg4.reclaim = arg->reclaim;
537 arg4.state = arg->state;
540 log_from_addr("nlm_lock", rqstp);
542 /* copy cookie from arg to result. See comment in nlm_test_1() */
543 res.cookie = arg->cookie;
545 res.stat.stat = getlock(&arg4, rqstp, LOCK_MON);
550 nlm_lock_msg_1_svc(arg, rqstp)
552 struct svc_req *rqstp;
555 struct nlm4_lockargs arg4;
557 nlmtonlm4(&arg->alock, &arg4.alock);
558 arg4.cookie = arg->cookie;
559 arg4.block = arg->block;
560 arg4.exclusive = arg->exclusive;
561 arg4.reclaim = arg->reclaim;
562 arg4.state = arg->state;
565 log_from_addr("nlm_lock_msg", rqstp);
567 res.cookie = arg->cookie;
568 res.stat.stat = getlock(&arg4, rqstp, LOCK_ASYNC | LOCK_MON);
569 transmit_result(NLM_LOCK_RES, &res, getrpcaddr(rqstp));
574 /* nlm_cancel -------------------------------------------------------------- */
576 * Purpose: Cancel a blocked lock request
577 * Returns: granted or denied
581 nlm_cancel_1_svc(arg, rqstp)
583 struct svc_req *rqstp;
586 struct nlm4_lock arg4;
588 nlmtonlm4(&arg->alock, &arg4);
591 log_from_addr("nlm_cancel", rqstp);
593 /* copy cookie from arg to result. See comment in nlm_test_1() */
594 res.cookie = arg->cookie;
597 * Since at present we never return 'nlm_blocked', there can never be
598 * a lock to cancel, so this call always fails.
600 res.stat.stat = unlock(&arg4, LOCK_CANCEL);
605 nlm_cancel_msg_1_svc(arg, rqstp)
607 struct svc_req *rqstp;
610 struct nlm4_lock arg4;
612 nlmtonlm4(&arg->alock, &arg4);
615 log_from_addr("nlm_cancel_msg", rqstp);
617 res.cookie = arg->cookie;
619 * Since at present we never return 'nlm_blocked', there can never be
620 * a lock to cancel, so this call always fails.
622 res.stat.stat = unlock(&arg4, LOCK_CANCEL);
623 transmit_result(NLM_CANCEL_RES, &res, getrpcaddr(rqstp));
627 /* nlm_unlock -------------------------------------------------------------- */
629 * Purpose: Release an existing lock
630 * Returns: Always granted, unless during grace period
631 * Notes: "no such lock" error condition is ignored, as the
632 * protocol uses unreliable UDP datagrams, and may well
633 * re-try an unlock that has already succeeded.
636 nlm_unlock_1_svc(arg, rqstp)
638 struct svc_req *rqstp;
641 struct nlm4_lock arg4;
643 nlmtonlm4(&arg->alock, &arg4);
646 log_from_addr("nlm_unlock", rqstp);
648 res.stat.stat = unlock(&arg4, 0);
649 res.cookie = arg->cookie;
655 nlm_unlock_msg_1_svc(arg, rqstp)
657 struct svc_req *rqstp;
660 struct nlm4_lock arg4;
662 nlmtonlm4(&arg->alock, &arg4);
665 log_from_addr("nlm_unlock_msg", rqstp);
667 res.stat.stat = unlock(&arg4, 0);
668 res.cookie = arg->cookie;
670 transmit_result(NLM_UNLOCK_RES, &res, getrpcaddr(rqstp));
674 /* ------------------------------------------------------------------------- */
676 * Client-side pseudo-RPCs for results. Note that for the client there
677 * are only nlm_xxx_msg() versions of each call, since the 'real RPC'
678 * version returns the results in the RPC result, and so the client
679 * does not normally receive incoming RPCs.
681 * The exception to this is nlm_granted(), which is genuinely an RPC
682 * call from the server to the client - a 'call-back' in normal procedure
686 /* nlm_granted ------------------------------------------------------------- */
688 * Purpose: Receive notification that formerly blocked lock now granted
689 * Returns: always success ('granted')
693 nlm_granted_1_svc(arg, rqstp)
695 struct svc_req *rqstp;
700 log_from_addr("nlm_granted", rqstp);
702 res.stat.stat = lock_answer(arg->alock.svid, &arg->cookie,
703 nlm_granted, NULL, NLM_VERS) == 0 ?
704 nlm_granted : nlm_denied;
706 /* copy cookie from arg to result. See comment in nlm_test_1() */
707 res.cookie = arg->cookie;
713 nlm_granted_msg_1_svc(arg, rqstp)
715 struct svc_req *rqstp;
720 log_from_addr("nlm_granted_msg", rqstp);
722 res.cookie = arg->cookie;
723 res.stat.stat = lock_answer(arg->alock.svid, &arg->cookie,
724 nlm_granted, NULL, NLM_VERS) == 0 ?
725 nlm_granted : nlm_denied;
727 transmit_result(NLM_GRANTED_RES, &res, getrpcaddr(rqstp));
731 /* nlm_test_res ------------------------------------------------------------ */
733 * Purpose: Accept result from earlier nlm_test_msg() call
737 nlm_test_res_1_svc(arg, rqstp)
739 struct svc_req *rqstp;
742 log_from_addr("nlm_test_res", rqstp);
743 (void)lock_answer(-1, &arg->cookie, arg->stat.stat,
744 &arg->stat.nlm_testrply_u.holder.svid, NLM_VERS);
748 /* nlm_lock_res ------------------------------------------------------------ */
750 * Purpose: Accept result from earlier nlm_lock_msg() call
754 nlm_lock_res_1_svc(arg, rqstp)
756 struct svc_req *rqstp;
759 log_from_addr("nlm_lock_res", rqstp);
761 (void)lock_answer(-1, &arg->cookie, arg->stat.stat, NULL, NLM_VERS);
766 /* nlm_cancel_res ---------------------------------------------------------- */
768 * Purpose: Accept result from earlier nlm_cancel_msg() call
772 nlm_cancel_res_1_svc(arg, rqstp)
773 nlm_res *arg __unused;
774 struct svc_req *rqstp;
777 log_from_addr("nlm_cancel_res", rqstp);
781 /* nlm_unlock_res ---------------------------------------------------------- */
783 * Purpose: Accept result from earlier nlm_unlock_msg() call
787 nlm_unlock_res_1_svc(arg, rqstp)
789 struct svc_req *rqstp;
792 log_from_addr("nlm_unlock_res", rqstp);
794 lock_answer(-1, &arg->cookie, arg->stat.stat, NULL, NLM_VERS);
799 /* nlm_granted_res --------------------------------------------------------- */
801 * Purpose: Accept result from earlier nlm_granted_msg() call
805 nlm_granted_res_1_svc(arg, rqstp)
806 nlm_res *arg __unused;
807 struct svc_req *rqstp;
810 log_from_addr("nlm_granted_res", rqstp);
814 /* ------------------------------------------------------------------------- */
816 * Calls for PCNFS locking (aka non-monitored locking, no involvement
819 * These are all genuine RPCs - no nlm_xxx_msg() nonsense here.
822 /* nlm_share --------------------------------------------------------------- */
824 * Purpose: Establish a DOS-style lock
825 * Returns: success or failure
826 * Notes: Blocking locks are not supported - client is expected
827 * to retry if required.
830 nlm_share_3_svc(arg, rqstp)
832 struct svc_req *rqstp;
834 static nlm_shareres res;
837 log_from_addr("nlm_share", rqstp);
839 res.cookie = arg->cookie;
840 res.stat = nlm_granted;
841 res.sequence = 1234356; /* X/Open says this field is ignored? */
845 /* nlm_unshare ------------------------------------------------------------ */
847 * Purpose: Release a DOS-style lock
848 * Returns: nlm_granted, unless in grace period
852 nlm_unshare_3_svc(arg, rqstp)
854 struct svc_req *rqstp;
856 static nlm_shareres res;
859 log_from_addr("nlm_unshare", rqstp);
861 res.cookie = arg->cookie;
862 res.stat = nlm_granted;
863 res.sequence = 1234356; /* X/Open says this field is ignored? */
867 /* nlm_nm_lock ------------------------------------------------------------ */
869 * Purpose: non-monitored version of nlm_lock()
870 * Returns: as for nlm_lock()
871 * Notes: These locks are in the same style as the standard nlm_lock,
872 * but the rpc.statd should not be called to establish a
873 * monitor for the client machine, since that machine is
874 * declared not to be running a rpc.statd, and so would not
875 * respond to the statd protocol.
878 nlm_nm_lock_3_svc(arg, rqstp)
880 struct svc_req *rqstp;
885 log_from_addr("nlm_nm_lock", rqstp);
887 /* copy cookie from arg to result. See comment in nlm_test_1() */
888 res.cookie = arg->cookie;
889 res.stat.stat = nlm_granted;
893 /* nlm_free_all ------------------------------------------------------------ */
895 * Purpose: Release all locks held by a named client
897 * Notes: Potential denial of service security problem here - the
898 * locks to be released are specified by a host name, independent
899 * of the address from which the request has arrived.
900 * Should probably be rejected if the named host has been
901 * using monitored locks.
904 nlm_free_all_3_svc(arg, rqstp)
905 nlm_notify *arg __unused;
906 struct svc_req *rqstp;
911 log_from_addr("nlm_free_all", rqstp);
915 /* calls for nlm version 4 (NFSv3) */
916 /* nlm_test ---------------------------------------------------------------- */
918 * Purpose: Test whether a specified lock would be granted if requested
919 * Returns: nlm_granted (or error code)
923 nlm4_test_4_svc(arg, rqstp)
925 struct svc_req *rqstp;
927 static nlm4_testres res;
928 struct nlm4_holder *holder;
931 log_from_addr("nlm4_test", rqstp);
932 if (debug_level > 5) {
933 syslog(LOG_DEBUG, "Locking arguments:\n");
934 log_netobj(&(arg->cookie));
935 syslog(LOG_DEBUG, "Alock arguments:\n");
936 syslog(LOG_DEBUG, "Caller Name: %s\n",arg->alock.caller_name);
937 syslog(LOG_DEBUG, "File Handle:\n");
938 log_netobj(&(arg->alock.fh));
939 syslog(LOG_DEBUG, "Owner Handle:\n");
940 log_netobj(&(arg->alock.oh));
941 syslog(LOG_DEBUG, "SVID: %d\n", arg->alock.svid);
942 syslog(LOG_DEBUG, "Lock Offset: %llu\n",
943 (unsigned long long)arg->alock.l_offset);
944 syslog(LOG_DEBUG, "Lock Length: %llu\n",
945 (unsigned long long)arg->alock.l_len);
946 syslog(LOG_DEBUG, "Exclusive: %s\n",
947 (arg->exclusive ? "true" : "false"));
950 holder = testlock(&arg->alock, arg->exclusive, LOCK_V4);
953 * Copy the cookie from the argument into the result. Note that this
954 * is slightly hazardous, as the structure contains a pointer to a
955 * malloc()ed buffer that will get freed by the caller. However, the
956 * main function transmits the result before freeing the argument
957 * so it is in fact safe.
959 res.cookie = arg->cookie;
960 if (holder == NULL) {
961 res.stat.stat = nlm4_granted;
963 res.stat.stat = nlm4_denied;
964 memcpy(&res.stat.nlm4_testrply_u.holder, holder,
965 sizeof(struct nlm4_holder));
971 nlm4_test_msg_4_svc(arg, rqstp)
973 struct svc_req *rqstp;
977 struct sockaddr *addr;
980 struct timeval timeo;
981 struct nlm4_holder *holder;
984 log_from_addr("nlm4_test_msg", rqstp);
986 holder = testlock(&arg->alock, arg->exclusive, LOCK_V4);
988 res.cookie = arg->cookie;
989 if (holder == NULL) {
990 res.stat.stat = nlm4_granted;
992 res.stat.stat = nlm4_denied;
993 memcpy(&res.stat.nlm4_testrply_u.holder, holder,
994 sizeof(struct nlm4_holder));
998 * nlm_test has different result type to the other operations, so
999 * can't use transmit4_result() in this case
1001 addr = svc_getrpccaller(rqstp->rq_xprt)->buf;
1002 if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
1003 timeo.tv_sec = 0; /* No timeout - not expecting response */
1006 success = clnt_call(cli, NLM4_TEST_RES,
1007 (xdrproc_t)xdr_nlm4_testres, &res,
1008 (xdrproc_t)xdr_void, &dummy, timeo);
1010 if (debug_level > 2)
1011 syslog(LOG_DEBUG, "clnt_call returns %d", success);
1016 /* nlm_lock ---------------------------------------------------------------- */
1018 * Purposes: Establish a lock
1019 * Returns: granted, denied or blocked
1020 * Notes: *** grace period support missing
1023 nlm4_lock_4_svc(arg, rqstp)
1025 struct svc_req *rqstp;
1027 static nlm4_res res;
1030 log_from_addr("nlm4_lock", rqstp);
1031 if (debug_level > 5) {
1032 syslog(LOG_DEBUG, "Locking arguments:\n");
1033 log_netobj(&(arg->cookie));
1034 syslog(LOG_DEBUG, "Alock arguments:\n");
1035 syslog(LOG_DEBUG, "Caller Name: %s\n",arg->alock.caller_name);
1036 syslog(LOG_DEBUG, "File Handle:\n");
1037 log_netobj(&(arg->alock.fh));
1038 syslog(LOG_DEBUG, "Owner Handle:\n");
1039 log_netobj(&(arg->alock.oh));
1040 syslog(LOG_DEBUG, "SVID: %d\n", arg->alock.svid);
1041 syslog(LOG_DEBUG, "Lock Offset: %llu\n",
1042 (unsigned long long)arg->alock.l_offset);
1043 syslog(LOG_DEBUG, "Lock Length: %llu\n",
1044 (unsigned long long)arg->alock.l_len);
1045 syslog(LOG_DEBUG, "Block: %s\n", (arg->block ? "true" : "false"));
1046 syslog(LOG_DEBUG, "Exclusive: %s\n", (arg->exclusive ? "true" : "false"));
1047 syslog(LOG_DEBUG, "Reclaim: %s\n", (arg->reclaim ? "true" : "false"));
1048 syslog(LOG_DEBUG, "State num: %d\n", arg->state);
1051 /* copy cookie from arg to result. See comment in nlm_test_4() */
1052 res.cookie = arg->cookie;
1054 res.stat.stat = getlock(arg, rqstp, LOCK_MON | LOCK_V4);
1059 nlm4_lock_msg_4_svc(arg, rqstp)
1061 struct svc_req *rqstp;
1063 static nlm4_res res;
1066 log_from_addr("nlm4_lock_msg", rqstp);
1068 res.cookie = arg->cookie;
1069 res.stat.stat = getlock(arg, rqstp, LOCK_MON | LOCK_ASYNC | LOCK_V4);
1070 transmit4_result(NLM4_LOCK_RES, &res, getrpcaddr(rqstp));
1075 /* nlm_cancel -------------------------------------------------------------- */
1077 * Purpose: Cancel a blocked lock request
1078 * Returns: granted or denied
1082 nlm4_cancel_4_svc(arg, rqstp)
1084 struct svc_req *rqstp;
1086 static nlm4_res res;
1089 log_from_addr("nlm4_cancel", rqstp);
1091 /* copy cookie from arg to result. See comment in nlm_test_1() */
1092 res.cookie = arg->cookie;
1095 * Since at present we never return 'nlm_blocked', there can never be
1096 * a lock to cancel, so this call always fails.
1098 res.stat.stat = unlock(&arg->alock, LOCK_CANCEL);
1103 nlm4_cancel_msg_4_svc(arg, rqstp)
1105 struct svc_req *rqstp;
1107 static nlm4_res res;
1110 log_from_addr("nlm4_cancel_msg", rqstp);
1112 res.cookie = arg->cookie;
1114 * Since at present we never return 'nlm_blocked', there can never be
1115 * a lock to cancel, so this call always fails.
1117 res.stat.stat = unlock(&arg->alock, LOCK_CANCEL | LOCK_V4);
1118 transmit4_result(NLM4_CANCEL_RES, &res, getrpcaddr(rqstp));
1122 /* nlm_unlock -------------------------------------------------------------- */
1124 * Purpose: Release an existing lock
1125 * Returns: Always granted, unless during grace period
1126 * Notes: "no such lock" error condition is ignored, as the
1127 * protocol uses unreliable UDP datagrams, and may well
1128 * re-try an unlock that has already succeeded.
1131 nlm4_unlock_4_svc(arg, rqstp)
1132 nlm4_unlockargs *arg;
1133 struct svc_req *rqstp;
1135 static nlm4_res res;
1138 log_from_addr("nlm4_unlock", rqstp);
1140 res.stat.stat = unlock(&arg->alock, LOCK_V4);
1141 res.cookie = arg->cookie;
1147 nlm4_unlock_msg_4_svc(arg, rqstp)
1148 nlm4_unlockargs *arg;
1149 struct svc_req *rqstp;
1151 static nlm4_res res;
1154 log_from_addr("nlm4_unlock_msg", rqstp);
1156 res.stat.stat = unlock(&arg->alock, LOCK_V4);
1157 res.cookie = arg->cookie;
1159 transmit4_result(NLM4_UNLOCK_RES, &res, getrpcaddr(rqstp));
1163 /* ------------------------------------------------------------------------- */
1165 * Client-side pseudo-RPCs for results. Note that for the client there
1166 * are only nlm_xxx_msg() versions of each call, since the 'real RPC'
1167 * version returns the results in the RPC result, and so the client
1168 * does not normally receive incoming RPCs.
1170 * The exception to this is nlm_granted(), which is genuinely an RPC
1171 * call from the server to the client - a 'call-back' in normal procedure
1175 /* nlm_granted ------------------------------------------------------------- */
1177 * Purpose: Receive notification that formerly blocked lock now granted
1178 * Returns: always success ('granted')
1182 nlm4_granted_4_svc(arg, rqstp)
1184 struct svc_req *rqstp;
1186 static nlm4_res res;
1189 log_from_addr("nlm4_granted", rqstp);
1191 res.stat.stat = lock_answer(arg->alock.svid, &arg->cookie,
1192 nlm4_granted, NULL, NLM_VERS4) == 0 ?
1193 nlm4_granted : nlm4_denied;
1195 /* copy cookie from arg to result. See comment in nlm_test_1() */
1196 res.cookie = arg->cookie;
1202 nlm4_granted_msg_4_svc(arg, rqstp)
1204 struct svc_req *rqstp;
1206 static nlm4_res res;
1209 log_from_addr("nlm4_granted_msg", rqstp);
1211 res.cookie = arg->cookie;
1212 res.stat.stat = lock_answer(arg->alock.svid, &arg->cookie,
1213 nlm4_granted, NULL, NLM_VERS4) == 0 ?
1214 nlm4_granted : nlm4_denied;
1215 transmit4_result(NLM4_GRANTED_RES, &res, getrpcaddr(rqstp));
1219 /* nlm_test_res ------------------------------------------------------------ */
1221 * Purpose: Accept result from earlier nlm_test_msg() call
1225 nlm4_test_res_4_svc(arg, rqstp)
1227 struct svc_req *rqstp;
1230 log_from_addr("nlm4_test_res", rqstp);
1232 (void)lock_answer(-1, &arg->cookie, arg->stat.stat,
1233 (int *)&arg->stat.nlm4_testrply_u.holder.svid,
1238 /* nlm_lock_res ------------------------------------------------------------ */
1240 * Purpose: Accept result from earlier nlm_lock_msg() call
1244 nlm4_lock_res_4_svc(arg, rqstp)
1246 struct svc_req *rqstp;
1249 log_from_addr("nlm4_lock_res", rqstp);
1251 (void)lock_answer(-1, &arg->cookie, arg->stat.stat, NULL, NLM_VERS4);
1256 /* nlm_cancel_res ---------------------------------------------------------- */
1258 * Purpose: Accept result from earlier nlm_cancel_msg() call
1262 nlm4_cancel_res_4_svc(arg, rqstp)
1263 nlm4_res *arg __unused;
1264 struct svc_req *rqstp;
1267 log_from_addr("nlm4_cancel_res", rqstp);
1271 /* nlm_unlock_res ---------------------------------------------------------- */
1273 * Purpose: Accept result from earlier nlm_unlock_msg() call
1277 nlm4_unlock_res_4_svc(arg, rqstp)
1278 nlm4_res *arg __unused;
1279 struct svc_req *rqstp;
1282 log_from_addr("nlm4_unlock_res", rqstp);
1286 /* nlm_granted_res --------------------------------------------------------- */
1288 * Purpose: Accept result from earlier nlm_granted_msg() call
1292 nlm4_granted_res_4_svc(arg, rqstp)
1293 nlm4_res *arg __unused;
1294 struct svc_req *rqstp;
1297 log_from_addr("nlm4_granted_res", rqstp);
1301 /* ------------------------------------------------------------------------- */
1303 * Calls for PCNFS locking (aka non-monitored locking, no involvement
1306 * These are all genuine RPCs - no nlm_xxx_msg() nonsense here.
1309 /* nlm_share --------------------------------------------------------------- */
1311 * Purpose: Establish a DOS-style lock
1312 * Returns: success or failure
1313 * Notes: Blocking locks are not supported - client is expected
1314 * to retry if required.
1317 nlm4_share_4_svc(arg, rqstp)
1318 nlm4_shareargs *arg;
1319 struct svc_req *rqstp;
1321 static nlm4_shareres res;
1324 log_from_addr("nlm4_share", rqstp);
1326 res.cookie = arg->cookie;
1327 res.stat = nlm4_granted;
1328 res.sequence = 1234356; /* X/Open says this field is ignored? */
1332 /* nlm4_unshare ------------------------------------------------------------ */
1334 * Purpose: Release a DOS-style lock
1335 * Returns: nlm_granted, unless in grace period
1339 nlm4_unshare_4_svc(arg, rqstp)
1340 nlm4_shareargs *arg;
1341 struct svc_req *rqstp;
1343 static nlm4_shareres res;
1346 log_from_addr("nlm_unshare", rqstp);
1348 res.cookie = arg->cookie;
1349 res.stat = nlm4_granted;
1350 res.sequence = 1234356; /* X/Open says this field is ignored? */
1354 /* nlm4_nm_lock ------------------------------------------------------------ */
1356 * Purpose: non-monitored version of nlm4_lock()
1357 * Returns: as for nlm4_lock()
1358 * Notes: These locks are in the same style as the standard nlm4_lock,
1359 * but the rpc.statd should not be called to establish a
1360 * monitor for the client machine, since that machine is
1361 * declared not to be running a rpc.statd, and so would not
1362 * respond to the statd protocol.
1365 nlm4_nm_lock_4_svc(arg, rqstp)
1367 struct svc_req *rqstp;
1369 static nlm4_res res;
1372 log_from_addr("nlm4_nm_lock", rqstp);
1374 /* copy cookie from arg to result. See comment in nlm4_test_1() */
1375 res.cookie = arg->cookie;
1376 res.stat.stat = nlm4_granted;
1380 /* nlm4_free_all ------------------------------------------------------------ */
1382 * Purpose: Release all locks held by a named client
1384 * Notes: Potential denial of service security problem here - the
1385 * locks to be released are specified by a host name, independent
1386 * of the address from which the request has arrived.
1387 * Should probably be rejected if the named host has been
1388 * using monitored locks.
1391 nlm4_free_all_4_svc(arg, rqstp)
1392 struct nlm4_notify *arg __unused;
1393 struct svc_req *rqstp;
1398 log_from_addr("nlm4_free_all", rqstp);
1402 /* nlm_sm_notify --------------------------------------------------------- */
1404 * Purpose: called by rpc.statd when a monitored host state changes.
1408 nlm_sm_notify_0_svc(arg, rqstp)
1409 struct nlm_sm_status *arg;
1410 struct svc_req *rqstp __unused;
1413 notify(arg->mon_name, arg->state);