freebsd-skq/sbin/rpc.lockd/lockd.c
Peter Wemm 503d2aa8a2 Import Jan 15 version of Andrew Gordon <andrew.gordon@net-tel.co.uk>'s
stub lockd.

This implements just the protocol, but does not interact with the kernel.
It says "Yes!" to all requests.  This is useful if you have people using
tools that do locking for no reason (eg: some PC NFS systems running some
Microsoft products) and will happily report they couldn't lock the file
and merrily proceed anyway.  Running this will not change the reliability of
sharing files, it'll just keep it out of everybody's face.
1996-02-17 15:11:29 +00:00

326 lines
9.0 KiB
C

/*
* Copyright (c) 1995
* A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the FreeBSD project
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/* main() function for NFS lock daemon. Most of the code in this */
/* file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x */
/* The actual program logic is in the file procs.c */
#include "lockd.h"
int debug_level = 0; /* Zero means no debugging syslog() calls */
static void nlm_prog_1();
static void nlm_prog_3();
main(int argc, char **argv)
{
SVCXPRT *transp;
if (argc > 1)
{
if (strncmp(argv[1], "-d", 2))
{
fprintf(stderr, "Usage: rpc.lockd [-d [<debuglevel>]]\n");
exit(1);
}
if (argc > 2) debug_level = atoi(argv[2]);
else debug_level = atoi(argv[1] + 2);
/* Ensure at least some debug if -d with no specified level */
if (!debug_level) debug_level = 1;
}
(void)pmap_unset(NLM_PROG, NLM_VERS);
(void)pmap_unset(NLM_PROG, NLM_VERSX);
transp = svcudp_create(RPC_ANYSOCK);
if (transp == NULL)
{
(void)fprintf(stderr, "cannot create udp service.\n");
exit(1);
}
if (!svc_register(transp, NLM_PROG, NLM_VERS, nlm_prog_1, IPPROTO_UDP))
{
(void)fprintf(stderr, "unable to register (NLM_PROG, NLM_VERS, udp).\n");
exit(1);
}
if (!svc_register(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, IPPROTO_UDP))
{
(void)fprintf(stderr, "unable to register (NLM_PROG, NLM_VERSX, udp).\n");
exit(1);
}
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (transp == NULL)
{
(void)fprintf(stderr, "cannot create tcp service.\n");
exit(1);
}
if (!svc_register(transp, NLM_PROG, NLM_VERS, nlm_prog_1, IPPROTO_TCP))
{
(void)fprintf(stderr, "unable to register (NLM_PROG, NLM_VERS, tcp).\n");
exit(1);
}
if (!svc_register(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, IPPROTO_TCP))
{
(void)fprintf(stderr, "unable to register (NLM_PROG, NLM_VERSX, tcp).\n");
exit(1);
}
/* Note that it is NOT sensible to run this program from inetd - the */
/* protocol assumes that it will run immediately at boot time. */
daemon(0, 0);
openlog("rpc.lockd", 0, LOG_DAEMON);
if (debug_level) syslog(LOG_INFO, "Starting, debug level %d", debug_level);
else syslog(LOG_INFO, "Starting");
svc_run(); /* Should never return */
exit(1);
}
static void
nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp)
{
union
{
struct nlm_testargs nlm_test_1_arg;
struct nlm_lockargs nlm_lock_1_arg;
struct nlm_cancargs nlm_cancel_1_arg;
struct nlm_unlockargs nlm_unlock_1_arg;
struct nlm_testargs nlm_granted_1_arg;
struct nlm_testargs nlm_test_msg_1_arg;
struct nlm_lockargs nlm_lock_msg_1_arg;
struct nlm_cancargs nlm_cancel_msg_1_arg;
struct nlm_unlockargs nlm_unlock_msg_1_arg;
struct nlm_testargs nlm_granted_msg_1_arg;
nlm_testres nlm_test_res_1_arg;
nlm_res nlm_lock_res_1_arg;
nlm_res nlm_cancel_res_1_arg;
nlm_res nlm_unlock_res_1_arg;
nlm_res nlm_granted_res_1_arg;
} argument;
char *result;
bool_t (*xdr_argument)(), (*xdr_result)();
char *(*local)();
switch (rqstp->rq_proc)
{
case NULLPROC:
(void)svc_sendreply(transp, xdr_void, (char *)NULL);
return;
case NLM_TEST:
xdr_argument = xdr_nlm_testargs;
xdr_result = xdr_nlm_testres;
local = (char *(*)()) nlm_test_1;
break;
case NLM_LOCK:
xdr_argument = xdr_nlm_lockargs;
xdr_result = xdr_nlm_res;
local = (char *(*)()) nlm_lock_1;
break;
case NLM_CANCEL:
xdr_argument = xdr_nlm_cancargs;
xdr_result = xdr_nlm_res;
local = (char *(*)()) nlm_cancel_1;
break;
case NLM_UNLOCK:
xdr_argument = xdr_nlm_unlockargs;
xdr_result = xdr_nlm_res;
local = (char *(*)()) nlm_unlock_1;
break;
case NLM_GRANTED:
xdr_argument = xdr_nlm_testargs;
xdr_result = xdr_nlm_res;
local = (char *(*)()) nlm_granted_1;
break;
case NLM_TEST_MSG:
xdr_argument = xdr_nlm_testargs;
xdr_result = xdr_void;
local = (char *(*)()) nlm_test_msg_1;
break;
case NLM_LOCK_MSG:
xdr_argument = xdr_nlm_lockargs;
xdr_result = xdr_void;
local = (char *(*)()) nlm_lock_msg_1;
break;
case NLM_CANCEL_MSG:
xdr_argument = xdr_nlm_cancargs;
xdr_result = xdr_void;
local = (char *(*)()) nlm_cancel_msg_1;
break;
case NLM_UNLOCK_MSG:
xdr_argument = xdr_nlm_unlockargs;
xdr_result = xdr_void;
local = (char *(*)()) nlm_unlock_msg_1;
break;
case NLM_GRANTED_MSG:
xdr_argument = xdr_nlm_testargs;
xdr_result = xdr_void;
local = (char *(*)()) nlm_granted_msg_1;
break;
case NLM_TEST_RES:
xdr_argument = xdr_nlm_testres;
xdr_result = xdr_void;
local = (char *(*)()) nlm_test_res_1;
break;
case NLM_LOCK_RES:
xdr_argument = xdr_nlm_res;
xdr_result = xdr_void;
local = (char *(*)()) nlm_lock_res_1;
break;
case NLM_CANCEL_RES:
xdr_argument = xdr_nlm_res;
xdr_result = xdr_void;
local = (char *(*)()) nlm_cancel_res_1;
break;
case NLM_UNLOCK_RES:
xdr_argument = xdr_nlm_res;
xdr_result = xdr_void;
local = (char *(*)()) nlm_unlock_res_1;
break;
case NLM_GRANTED_RES:
xdr_argument = xdr_nlm_res;
xdr_result = xdr_void;
local = (char *(*)()) nlm_granted_res_1;
break;
default:
svcerr_noproc(transp);
return;
}
bzero((char *)&argument, sizeof(argument));
if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument))
{
syslog(LOG_ERR, "RPC received with invalid arguments");
svcerr_decode(transp);
return;
}
result = (*local)(&argument, rqstp);
if (result != NULL && !svc_sendreply(transp, xdr_result, result))
{
svcerr_systemerr(transp);
}
if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument))
{
syslog(LOG_ERR, "unable to free arguments");
}
}
static void
nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp)
{
union
{
nlm_shareargs nlm_share_3_arg;
nlm_shareargs nlm_unshare_3_arg;
nlm_lockargs nlm_nm_lock_3_arg;
nlm_notify nlm_free_all_3_arg;
} argument;
char *result;
bool_t (*xdr_argument)(), (*xdr_result)();
char *(*local)();
switch (rqstp->rq_proc)
{
case NULLPROC:
(void)svc_sendreply(transp, xdr_void, (char *)NULL);
return;
case NLM_SHARE:
xdr_argument = xdr_nlm_shareargs;
xdr_result = xdr_nlm_shareres;
local = (char *(*)()) nlm_share_3;
break;
case NLM_UNSHARE:
xdr_argument = xdr_nlm_shareargs;
xdr_result = xdr_nlm_shareres;
local = (char *(*)()) nlm_unshare_3;
break;
case NLM_NM_LOCK:
xdr_argument = xdr_nlm_lockargs;
xdr_result = xdr_nlm_res;
local = (char *(*)()) nlm_nm_lock_3;
break;
case NLM_FREE_ALL:
xdr_argument = xdr_nlm_notify;
xdr_result = xdr_void;
local = (char *(*)()) nlm_free_all_3;
break;
default:
svcerr_noproc(transp);
return;
}
bzero((char *)&argument, sizeof(argument));
if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument))
{
syslog(LOG_ERR, "RPC received with invalid arguments");
svcerr_decode(transp);
return;
}
result = (*local)(&argument, rqstp);
if (result != NULL && !svc_sendreply(transp, xdr_result, result))
{
svcerr_systemerr(transp);
}
if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument))
{
syslog(LOG_ERR, "unable to free arguments");
}
}