xref: /freebsd/libexec/rpc.rwalld/rwalld.c (revision 9f44a47fd07924afc035991af15d84e6585dea4f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1993 Christopher G. Demetriou
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
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <err.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <arpa/inet.h>
43 #include <rpc/rpc.h>
44 #include <rpcsvc/rwall.h>
45 #include <sys/socket.h>
46 #include <sys/types.h>
47 #include <sys/wait.h>
48 #include <unistd.h>
49 
50 #ifdef OSF
51 #define WALL_CMD "/usr/sbin/wall"
52 #else
53 #define WALL_CMD "/usr/bin/wall -n"
54 #endif
55 
56 void wallprog_1(struct svc_req *rqstp, SVCXPRT *transp);
57 void possess(void);
58 void killkids(int sig);
59 static void usage(void);
60 
61 int nodaemon = 0;
62 int from_inetd = 1;
63 
64 int
65 main(int argc, char *argv[])
66 {
67 	SVCXPRT *transp;
68 	socklen_t salen;
69 	int ok;
70 	struct sockaddr_storage sa;
71 
72 	if (argc == 2 && !strcmp(argv[1], "-n"))
73 		nodaemon = 1;
74 	if (argc != 1 && !nodaemon)
75 		usage();
76 
77 	if (geteuid() == 0) {
78 		struct passwd *pep = getpwnam("nobody");
79 		if (pep)
80 			setuid(pep->pw_uid);
81 		else
82 			setuid(getuid());
83 	}
84 
85         /*
86          * See if inetd started us
87          */
88 	salen = sizeof(sa);
89         if (getsockname(0, (struct sockaddr *)&sa, &salen) < 0) {
90                 from_inetd = 0;
91         }
92 
93         if (!from_inetd) {
94                 if (!nodaemon)
95                         possess();
96 
97 		(void)rpcb_unset(WALLPROG, WALLVERS, NULL);
98         }
99 
100 	(void)signal(SIGCHLD, killkids);
101 
102 	openlog("rpc.rwalld", LOG_CONS|LOG_PID, LOG_DAEMON);
103 
104 	/* create and register the service */
105 	if (from_inetd) {
106 		transp = svc_tli_create(0, NULL, NULL, 0, 0);
107 		if (transp == NULL) {
108 			syslog(LOG_ERR, "couldn't create udp service.");
109 			exit(1);
110 		}
111 		ok = svc_reg(transp, WALLPROG, WALLVERS,
112 			     wallprog_1, NULL);
113 	} else
114 		ok = svc_create(wallprog_1,
115 				WALLPROG, WALLVERS, "udp");
116 	if (!ok) {
117 		syslog(LOG_ERR, "unable to register (WALLPROG, WALLVERS, %s)", (!from_inetd)?"udp":"(inetd)");
118 		exit(1);
119 	}
120 	svc_run();
121 	syslog(LOG_ERR, "svc_run returned");
122 	exit(1);
123 }
124 
125 static void
126 usage(void)
127 {
128 	fprintf(stderr, "usage: rpc.rwalld [-n]\n");
129 	exit(1);
130 }
131 
132 void
133 possess(void)
134 {
135 	daemon(0, 0);
136 }
137 
138 void
139 killkids(int sig __unused)
140 {
141 	while(wait4(-1, NULL, WNOHANG, NULL) > 0)
142 		;
143 }
144 
145 void *
146 wallproc_wall_1_svc(wrapstring *s, struct svc_req *rqstp __unused)
147 {
148 	static void		*dummy = NULL;
149 
150 	/* fork, popen wall with special option, and send the message */
151 	if (fork() == 0) {
152 		FILE *pfp;
153 
154 		pfp = popen(WALL_CMD, "w");
155 		if (pfp != NULL) {
156 			fprintf(pfp, "\007\007%s", *s);
157 			pclose(pfp);
158 			exit(0);
159 		}
160 	}
161 	return(&dummy);
162 }
163 
164 void
165 wallprog_1(struct svc_req *rqstp, SVCXPRT *transp)
166 {
167 	union {
168 		char *wallproc_wall_1_arg;
169 	} argument;
170 	void *result;
171 	xdrproc_t xdr_argument, xdr_result;
172 	typedef void *(svc_cb)(void *arg, struct svc_req *rqstp);
173 	svc_cb *local;
174 
175 	switch (rqstp->rq_proc) {
176 	case NULLPROC:
177 		(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
178 		goto leave;
179 
180 	case WALLPROC_WALL:
181 		xdr_argument = (xdrproc_t)xdr_wrapstring;
182 		xdr_result = (xdrproc_t)xdr_void;
183 		local = (svc_cb *)wallproc_wall_1_svc;
184 		break;
185 
186 	default:
187 		svcerr_noproc(transp);
188 		goto leave;
189 	}
190 	bzero(&argument, sizeof(argument));
191 	if (!svc_getargs(transp, xdr_argument, &argument)) {
192 		svcerr_decode(transp);
193 		goto leave;
194 	}
195 	result = (*local)(&argument, rqstp);
196 	if (result != NULL &&
197 	    !svc_sendreply(transp, xdr_result, result)) {
198 		svcerr_systemerr(transp);
199 	}
200 	if (!svc_freeargs(transp, xdr_argument, &argument)) {
201 		syslog(LOG_ERR, "unable to free arguments");
202 		exit(1);
203 	}
204 leave:
205         if (from_inetd)
206                 exit(0);
207 }
208