pax(8): Remove old prototypes.
[dragonfly.git] / libexec / rpc.rwalld / rwalld.c
blob8929a7cf4d5fb6513f6646979b9d94784b3962fa
1 /*
2 * Copyright (c) 1993 Christopher G. Demetriou
3 * All rights reserved.
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.
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.
29 * $FreeBSD: src/libexec/rpc.rwalld/rwalld.c,v 1.8 1999/08/28 00:09:57 peter Exp $
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 <rpcsvc/rwall.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <unistd.h>
46 #ifdef OSF
47 #define WALL_CMD "/usr/sbin/wall"
48 #else
49 #define WALL_CMD "/usr/bin/wall -n"
50 #endif
52 void wallprog_1();
53 void possess();
54 void killkids();
55 static void usage (void);
57 int nodaemon = 0;
58 int from_inetd = 1;
60 int
61 main(int argc, char *argv[])
63 SVCXPRT *transp;
64 int ok, salen;
65 struct sockaddr_storage sa;
67 if (argc == 2 && !strcmp(argv[1], "-n"))
68 nodaemon = 1;
69 if (argc != 1 && !nodaemon)
70 usage();
72 if (geteuid() == 0) {
73 struct passwd *pep = getpwnam("nobody");
74 if (pep)
75 setuid(pep->pw_uid);
76 else
77 setuid(getuid());
81 * See if inetd started us
83 salen = sizeof(sa);
84 if (getsockname(0, (struct sockaddr *)&sa, &salen) < 0) {
85 from_inetd = 0;
88 if (!from_inetd) {
89 if (!nodaemon)
90 possess();
92 (void)rpcb_unset(WALLPROG, WALLVERS, NULL);
95 (void)signal(SIGCHLD, killkids);
97 openlog("rpc.rwalld", LOG_CONS|LOG_PID, LOG_DAEMON);
99 /* create and register the service */
100 if (from_inetd) {
101 transp = svc_tli_create(0, NULL, NULL, 0, 0);
102 if (transp == NULL) {
103 syslog(LOG_ERR, "couldn't create udp service.");
104 exit(1);
106 ok = svc_reg(transp, WALLPROG, WALLVERS,
107 wallprog_1, NULL);
108 } else
109 ok = svc_create(wallprog_1,
110 WALLPROG, WALLVERS, "udp");
111 if (!ok) {
112 syslog(LOG_ERR, "unable to register (WALLPROG, WALLVERS, %s)", (!from_inetd)?"udp":"(inetd)");
113 exit(1);
115 svc_run();
116 syslog(LOG_ERR, "svc_run returned");
117 exit(1);
120 static void
121 usage(void)
123 fprintf(stderr, "usage: rpc.rwalld [-n]\n");
124 exit(1);
127 void
128 possess(void)
130 daemon(0, 0);
133 void
134 killkids(void)
136 while(wait4(-1, NULL, WNOHANG, NULL) > 0)
140 void *
141 wallproc_wall_1_svc(wrapstring *s, struct svc_req *rqstp)
143 static void *dummy = NULL;
145 /* fork, popen wall with special option, and send the message */
146 if (fork() == 0) {
147 FILE *pfp;
149 pfp = popen(WALL_CMD, "w");
150 if (pfp != NULL) {
151 fprintf(pfp, "\007\007%s", *s);
152 pclose(pfp);
153 exit(0);
156 return(&dummy);
159 void
160 wallprog_1(struct svc_req *rqstp, SVCXPRT *transp)
162 union {
163 char *wallproc_wall_1_arg;
164 } argument;
165 char *result;
166 bool_t (*xdr_argument)(), (*xdr_result)();
167 char *(*local)();
169 switch (rqstp->rq_proc) {
170 case NULLPROC:
171 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
172 goto leave;
174 case WALLPROC_WALL:
175 xdr_argument = xdr_wrapstring;
176 xdr_result = xdr_void;
177 local = (char *(*)()) wallproc_wall_1_svc;
178 break;
180 default:
181 svcerr_noproc(transp);
182 goto leave;
184 bzero((char *)&argument, sizeof(argument));
185 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (caddr_t)&argument)) {
186 svcerr_decode(transp);
187 goto leave;
189 result = (*local)(&argument, rqstp);
190 if (result != NULL &&
191 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
192 svcerr_systemerr(transp);
194 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (caddr_t)&argument)) {
195 syslog(LOG_ERR, "unable to free arguments");
196 exit(1);
198 leave:
199 if (from_inetd)
200 exit(0);