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