xref: /netbsd/libexec/rpc.rusersd/rusersd.c (revision bf9ec67e)
1 /*	$NetBSD: rusersd.c,v 1.14 2000/06/03 20:37:37 fvdl Exp $	*/
2 
3 /*-
4  *  Copyright (c) 1993 John Brezak
5  *  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. The name of the author may not be used to endorse or promote products
16  *     derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: rusersd.c,v 1.14 2000/06/03 20:37:37 fvdl Exp $");
34 #endif /* not lint */
35 
36 #include <stdio.h>
37 #include <rpc/rpc.h>
38 #include <sys/socket.h>
39 #include <signal.h>
40 #include <syslog.h>
41 #include <stdlib.h>
42 #include <rpcsvc/rusers.h>	/* New version */
43 #include <rpcsvc/rnusers.h>	/* Old version */
44 
45 #include "rusers_proc.h"
46 
47 
48 int from_inetd = 1;
49 
50 static void cleanup(int);
51 int main(int, char *[]);
52 
53 static void
54 cleanup(int n)
55 {
56 
57 	(void) rpcb_unset(RUSERSPROG, RUSERSVERS_3, NULL);
58 	(void) rpcb_unset(RUSERSPROG, RUSERSVERS_IDLE, NULL);
59 	exit(0);
60 }
61 
62 int
63 main(int argc, char *argv[])
64 {
65 	SVCXPRT *transp;
66 	struct sockaddr_storage from;
67 	int fromlen;
68 
69 	/*
70 	 * See if inetd started us
71 	 */
72 	fromlen = sizeof(from);
73 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
74 		from_inetd = 0;
75 
76 	if (!from_inetd) {
77 		daemon(0, 0);
78 
79 		(void) rpcb_unset(RUSERSPROG, RUSERSVERS_3, NULL);
80 		(void) rpcb_unset(RUSERSPROG, RUSERSVERS_IDLE, NULL);
81 
82 		(void) signal(SIGINT, cleanup);
83 		(void) signal(SIGTERM, cleanup);
84 		(void) signal(SIGHUP, cleanup);
85 	}
86 
87 	openlog("rpc.rusersd", LOG_PID, LOG_DAEMON);
88 
89 	if (from_inetd) {
90 		transp = svc_dg_create(0, 0, 0);
91 		if (transp == NULL) {
92 			syslog(LOG_ERR, "cannot create udp service.");
93 			exit(1);
94 		}
95 		if (!svc_reg(transp, RUSERSPROG, RUSERSVERS_3, rusers_service,
96 		    NULL)) {
97 			syslog(LOG_ERR, "unable to register "
98 			    "(RUSERSPROG, RUSERSVERS_3).");
99 			exit(1);
100 		}
101 		if (!svc_reg(transp, RUSERSPROG, RUSERSVERS_IDLE,
102 		    rusers_service, NULL)) {
103 			syslog(LOG_ERR, "unable to register "
104 			    "(RUSERSPROG, RUSERSVERS_IDLE).");
105 			exit(1);
106 		}
107 	} else {
108 		if (!svc_create(rusers_service, RUSERSPROG, RUSERSVERS_3,
109 		    "udp")) {
110 			syslog(LOG_ERR, "unable to create "
111 			    "(RUSERSPROG, RUSERSVERS_3).");
112 			exit(1);
113 		}
114 		if (!svc_create(rusers_service, RUSERSPROG, RUSERSVERS_IDLE,
115 		    "udp")) {
116 			syslog(LOG_ERR, "unable to create "
117 			    "(RUSERSPROG, RUSERSVERS_IDLE).");
118 			exit(1);
119 		}
120 	}
121 
122 	svc_run();
123 	syslog(LOG_ERR, "svc_run returned");
124 	exit(1);
125 }
126