Merge from vendor branch GROFF:
[dragonfly.git] / libexec / rpc.rwalld / rwalld.c
CommitLineData
984263bc
MD
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.
1de703da
MD
28 *
29 * $FreeBSD: src/libexec/rpc.rwalld/rwalld.c,v 1.8 1999/08/28 00:09:57 peter Exp $
3eefb696 30 * $DragonFly: src/libexec/rpc.rwalld/rwalld.c,v 1.3 2003/11/14 03:54:31 dillon Exp $
984263bc
MD
31 */
32
984263bc
MD
33#include <err.h>
34#include <pwd.h>
35#include <signal.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <syslog.h>
40#include <rpc/rpc.h>
41#include <rpc/pmap_clnt.h>
42#include <rpcsvc/rwall.h>
43#include <sys/socket.h>
44#include <sys/types.h>
45#include <sys/wait.h>
46#include <unistd.h>
47
48#ifdef OSF
49#define WALL_CMD "/usr/sbin/wall"
50#else
51#define WALL_CMD "/usr/bin/wall -n"
52#endif
53
54void wallprog_1();
55void possess();
56void killkids();
3eefb696 57static void usage (void);
984263bc
MD
58
59int nodaemon = 0;
60int from_inetd = 1;
61
62int
63main(argc, argv)
64 int argc;
65 char *argv[];
66{
67 SVCXPRT *transp;
68 int s, salen;
69 struct sockaddr_in sa;
70 int sock = 0;
71 int proto = 0;
72
73 if (argc == 2 && !strcmp(argv[1], "-n"))
74 nodaemon = 1;
75 if (argc != 1 && !nodaemon)
76 usage();
77
78 if (geteuid() == 0) {
79 struct passwd *pep = getpwnam("nobody");
80 if (pep)
81 setuid(pep->pw_uid);
82 else
83 setuid(getuid());
84 }
85
86 /*
87 * See if inetd started us
88 */
89 salen = sizeof(sa);
90 if (getsockname(0, (struct sockaddr *)&sa, &salen) < 0) {
91 from_inetd = 0;
92 sock = RPC_ANYSOCK;
93 proto = IPPROTO_UDP;
94 }
95
96 if (!from_inetd) {
97 if (!nodaemon)
98 possess();
99
100 (void)pmap_unset(WALLPROG, WALLVERS);
101 if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
102 err(1, "socket");
103 bzero((char *)&sa, sizeof sa);
104 if (bind(s, (struct sockaddr *)&sa, sizeof sa) < 0)
105 err(1, "bind");
106
107 salen = sizeof sa;
108 if (getsockname(s, (struct sockaddr *)&sa, &salen))
109 err(1, "getsockname");
110
111 pmap_set(WALLPROG, WALLVERS, IPPROTO_UDP, ntohs(sa.sin_port));
112 if (dup2(s, 0) < 0)
113 err(1, "dup2");
114 (void)pmap_unset(WALLPROG, WALLVERS);
115 }
116
117 (void)signal(SIGCHLD, killkids);
118
119 openlog("rpc.rwalld", LOG_CONS|LOG_PID, LOG_DAEMON);
120
121 transp = svcudp_create(sock);
122 if (transp == NULL) {
123 syslog(LOG_ERR, "cannot create udp service");
124 exit(1);
125 }
126 if (!svc_register(transp, WALLPROG, WALLVERS, wallprog_1, proto)) {
127 syslog(LOG_ERR, "unable to register (WALLPROG, WALLVERS, %s)", proto?"udp":"(inetd)");
128 exit(1);
129 }
130 svc_run();
131 syslog(LOG_ERR, "svc_run returned");
132 exit(1);
133}
134
135static void
136usage()
137{
138 fprintf(stderr, "usage: rpc.rwalld [-n]\n");
139 exit(1);
140}
141
142void possess()
143{
144 daemon(0, 0);
145}
146
147void killkids()
148{
149 while(wait4(-1, NULL, WNOHANG, NULL) > 0)
150 ;
151}
152
153void *wallproc_wall_1_svc(s, rqstp)
154 wrapstring *s;
155 struct svc_req *rqstp;
156{
157 static void *dummy = NULL;
158
159 /* fork, popen wall with special option, and send the message */
160 if (fork() == 0) {
161 FILE *pfp;
162
163 pfp = popen(WALL_CMD, "w");
164 if (pfp != NULL) {
165 fprintf(pfp, "\007\007%s", *s);
166 pclose(pfp);
167 exit(0);
168 }
169 }
170 return(&dummy);
171}
172
173void
174wallprog_1(rqstp, transp)
175 struct svc_req *rqstp;
176 SVCXPRT *transp;
177{
178 union {
179 char *wallproc_wall_1_arg;
180 } argument;
181 char *result;
182 bool_t (*xdr_argument)(), (*xdr_result)();
183 char *(*local)();
184
185 switch (rqstp->rq_proc) {
186 case NULLPROC:
187 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
188 goto leave;
189
190 case WALLPROC_WALL:
191 xdr_argument = xdr_wrapstring;
192 xdr_result = xdr_void;
193 local = (char *(*)()) wallproc_wall_1_svc;
194 break;
195
196 default:
197 svcerr_noproc(transp);
198 goto leave;
199 }
200 bzero((char *)&argument, sizeof(argument));
201 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
202 svcerr_decode(transp);
203 goto leave;
204 }
205 result = (*local)(&argument, rqstp);
206 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
207 svcerr_systemerr(transp);
208 }
209 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
210 syslog(LOG_ERR, "unable to free arguments");
211 exit(1);
212 }
213leave:
214 if (from_inetd)
215 exit(0);
216}