xref: /netbsd-src/libexec/rpc.rusersd/rusersd.c (revision 5f7096188587a2c7c95fa3c69b78e1ec9c7923d0)
1 /*-
2  *  Copyright (c) 1993 John Brezak
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *  1. Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef lint
30 static char rcsid[] = "$Id: rusersd.c,v 1.5 1993/11/21 18:56:36 brezak Exp $";
31 #endif /* not lint */
32 
33 #include <stdio.h>
34 #include <rpc/rpc.h>
35 #include <signal.h>
36 #include <syslog.h>
37 #include <rpcsvc/rusers.h>	/* New version */
38 #include <rpcsvc/rnusers.h>	/* Old version */
39 
40 extern void rusers_service();
41 
42 int from_inetd = 1;
43 
44 void
45 cleanup()
46 {
47         (void) pmap_unset(RUSERSPROG, RUSERSVERS_3);
48         (void) pmap_unset(RUSERSPROG, RUSERSVERS_IDLE);
49         exit(0);
50 }
51 
52 main(argc, argv)
53         int argc;
54         char *argv[];
55 {
56 	SVCXPRT *transp;
57         int sock = 0;
58         int proto = 0;
59 	struct sockaddr_in from;
60 	int fromlen;
61 
62         /*
63          * See if inetd started us
64          */
65         if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
66                 from_inetd = 0;
67                 sock = RPC_ANYSOCK;
68                 proto = IPPROTO_UDP;
69         }
70 
71         if (!from_inetd) {
72                 daemon(0, 0);
73 
74                 (void) pmap_unset(RUSERSPROG, RUSERSVERS_3);
75                 (void) pmap_unset(RUSERSPROG, RUSERSVERS_IDLE);
76 
77 		(void) signal(SIGINT, cleanup);
78 		(void) signal(SIGTERM, cleanup);
79 		(void) signal(SIGHUP, cleanup);
80         }
81 
82         openlog("rpc.rusersd", LOG_CONS|LOG_PID, LOG_DAEMON);
83 
84 	transp = svcudp_create(sock);
85 	if (transp == NULL) {
86 		syslog(LOG_ERR, "cannot create udp service.");
87 		exit(1);
88 	}
89 	if (!svc_register(transp, RUSERSPROG, RUSERSVERS_3, rusers_service, proto)) {
90 		syslog(LOG_ERR, "unable to register (RUSERSPROG, RUSERSVERS_3, %s).", proto?"udp":"(inetd)");
91 		exit(1);
92 	}
93 
94 	if (!svc_register(transp, RUSERSPROG, RUSERSVERS_IDLE, rusers_service, proto)) {
95 		syslog(LOG_ERR, "unable to register (RUSERSPROG, RUSERSVERS_IDLE, %s).", proto?"udp":"(inetd)");
96 		exit(1);
97 	}
98 
99         svc_run();
100 	syslog(LOG_ERR, "svc_run returned");
101 	exit(1);
102 }
103