rwalld.c revision 50476
1/*
2 * Copyright (c) 1993 Christopher G. Demetriou
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
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char rcsid[] =
32  "$FreeBSD: head/libexec/rpc.rwalld/rwalld.c 50476 1999-08-28 00:22:10Z peter $";
33#endif /* not lint */
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 <rpc/rpc.h>
43#include <rpc/pmap_clnt.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
56void wallprog_1();
57void possess();
58void killkids();
59static void usage __P((void));
60
61int nodaemon = 0;
62int from_inetd = 1;
63
64int
65main(argc, argv)
66	int argc;
67	char *argv[];
68{
69	SVCXPRT *transp;
70	int s, salen;
71	struct sockaddr_in sa;
72        int sock = 0;
73        int proto = 0;
74
75	if (argc == 2 && !strcmp(argv[1], "-n"))
76		nodaemon = 1;
77	if (argc != 1 && !nodaemon)
78		usage();
79
80	if (geteuid() == 0) {
81		struct passwd *pep = getpwnam("nobody");
82		if (pep)
83			setuid(pep->pw_uid);
84		else
85			setuid(getuid());
86	}
87
88        /*
89         * See if inetd started us
90         */
91	salen = sizeof(sa);
92        if (getsockname(0, (struct sockaddr *)&sa, &salen) < 0) {
93                from_inetd = 0;
94                sock = RPC_ANYSOCK;
95                proto = IPPROTO_UDP;
96        }
97
98        if (!from_inetd) {
99                if (!nodaemon)
100                        possess();
101
102                (void)pmap_unset(WALLPROG, WALLVERS);
103                if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
104                        err(1, "socket");
105                bzero((char *)&sa, sizeof sa);
106                if (bind(s, (struct sockaddr *)&sa, sizeof sa) < 0)
107                        err(1, "bind");
108
109                salen = sizeof sa;
110                if (getsockname(s, (struct sockaddr *)&sa, &salen))
111                        err(1, "getsockname");
112
113                pmap_set(WALLPROG, WALLVERS, IPPROTO_UDP, ntohs(sa.sin_port));
114                if (dup2(s, 0) < 0)
115                        err(1, "dup2");
116                (void)pmap_unset(WALLPROG, WALLVERS);
117        }
118
119	(void)signal(SIGCHLD, killkids);
120
121	openlog("rpc.rwalld", LOG_CONS|LOG_PID, LOG_DAEMON);
122
123	transp = svcudp_create(sock);
124	if (transp == NULL) {
125		syslog(LOG_ERR, "cannot create udp service");
126		exit(1);
127	}
128	if (!svc_register(transp, WALLPROG, WALLVERS, wallprog_1, proto)) {
129		syslog(LOG_ERR, "unable to register (WALLPROG, WALLVERS, %s)", proto?"udp":"(inetd)");
130		exit(1);
131	}
132	svc_run();
133	syslog(LOG_ERR, "svc_run returned");
134	exit(1);
135}
136
137static void
138usage()
139{
140	fprintf(stderr, "usage: rpc.rwalld [-n]\n");
141	exit(1);
142}
143
144void possess()
145{
146	daemon(0, 0);
147}
148
149void killkids()
150{
151	while(wait4(-1, NULL, WNOHANG, NULL) > 0)
152		;
153}
154
155void *wallproc_wall_1_svc(s, rqstp)
156	wrapstring		*s;
157	struct svc_req		*rqstp;
158{
159	static void		*dummy = NULL;
160
161	/* fork, popen wall with special option, and send the message */
162	if (fork() == 0) {
163		FILE *pfp;
164
165		pfp = popen(WALL_CMD, "w");
166		if (pfp != NULL) {
167			fprintf(pfp, "\007\007%s", *s);
168			pclose(pfp);
169			exit(0);
170		}
171	}
172	return(&dummy);
173}
174
175void
176wallprog_1(rqstp, transp)
177	struct svc_req *rqstp;
178	SVCXPRT *transp;
179{
180	union {
181		char *wallproc_wall_1_arg;
182	} argument;
183	char *result;
184	bool_t (*xdr_argument)(), (*xdr_result)();
185	char *(*local)();
186
187	switch (rqstp->rq_proc) {
188	case NULLPROC:
189		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
190		goto leave;
191
192	case WALLPROC_WALL:
193		xdr_argument = xdr_wrapstring;
194		xdr_result = xdr_void;
195		local = (char *(*)()) wallproc_wall_1_svc;
196		break;
197
198	default:
199		svcerr_noproc(transp);
200		goto leave;
201	}
202	bzero((char *)&argument, sizeof(argument));
203	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
204		svcerr_decode(transp);
205		goto leave;
206	}
207	result = (*local)(&argument, rqstp);
208	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
209		svcerr_systemerr(transp);
210	}
211	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
212		syslog(LOG_ERR, "unable to free arguments");
213		exit(1);
214	}
215leave:
216        if (from_inetd)
217                exit(0);
218}
219