rwalld.c revision 1.11 1 /* $NetBSD: rwalld.c,v 1.11 1997/09/17 20:19:45 christos Exp $ */
2
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 #ifndef lint
34 __RCSID("$NetBSD: rwalld.c,v 1.11 1997/09/17 20:19:45 christos Exp $");
35 #endif /* not lint */
36
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <errno.h>
45 #include <sys/socket.h>
46 #include <signal.h>
47 #include <sys/wait.h>
48 #include <rpc/rpc.h>
49 #include <rpcsvc/rwall.h>
50
51 #ifdef OSF
52 #define WALL_CMD "/usr/sbin/wall"
53 #else
54 #define WALL_CMD "/usr/bin/wall -n"
55 #endif
56
57 static int from_inetd = 1;
58
59 static void cleanup __P((int));
60 static void wallprog_1 __P((struct svc_req *, SVCXPRT *));
61
62 int main __P((int, char *[]));
63
64 static void
65 cleanup(n)
66 int n;
67 {
68 (void) pmap_unset(WALLPROG, WALLVERS);
69 exit(0);
70 }
71
72 int
73 main(argc, argv)
74 int argc;
75 char *argv[];
76 {
77 SVCXPRT *transp;
78 int sock = 0;
79 int proto = 0;
80 struct sockaddr_in from;
81 int fromlen;
82
83 if (geteuid() == 0) {
84 struct passwd *pep = getpwnam("nobody");
85 if (pep)
86 setuid(pep->pw_uid);
87 else
88 setuid(getuid());
89 }
90
91 /*
92 * See if inetd started us
93 */
94 fromlen = sizeof(from);
95 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
96 from_inetd = 0;
97 sock = RPC_ANYSOCK;
98 proto = IPPROTO_UDP;
99 }
100
101 if (!from_inetd) {
102 daemon(0, 0);
103
104 (void) pmap_unset(WALLPROG, WALLVERS);
105
106 (void) signal(SIGINT, cleanup);
107 (void) signal(SIGTERM, cleanup);
108 (void) signal(SIGHUP, cleanup);
109 }
110
111 openlog("rpc.rwalld", LOG_CONS|LOG_PID, LOG_DAEMON);
112
113 transp = svcudp_create(sock);
114 if (transp == NULL) {
115 syslog(LOG_ERR, "cannot create udp service.");
116 exit(1);
117 }
118 if (!svc_register(transp, WALLPROG, WALLVERS, wallprog_1, proto)) {
119 syslog(LOG_ERR, "unable to register (WALLPROG, WALLVERS, %s).", proto?"udp":"(inetd)");
120 exit(1);
121 }
122
123 svc_run();
124 syslog(LOG_ERR, "svc_run returned");
125 exit(1);
126
127 }
128
129 void *
130 wallproc_wall_1_svc(s, rqstp )
131 char **s;
132 struct svc_req *rqstp;
133 {
134 FILE *pfp;
135
136 pfp = popen(WALL_CMD, "w");
137 if (pfp != NULL) {
138 fprintf(pfp, "\007\007%s", *s);
139 pclose(pfp);
140 }
141
142 return (*s);
143 }
144
145 static void
146 wallprog_1(rqstp, transp)
147 struct svc_req *rqstp;
148 SVCXPRT *transp;
149 {
150 union {
151 char *wallproc_wall_1_arg;
152 } argument;
153 char *result;
154 xdrproc_t xdr_argument, xdr_result;
155 char *(*local) __P((char **, struct svc_req *));
156
157 switch (rqstp->rq_proc) {
158 case NULLPROC:
159 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
160 goto leave;
161
162 case WALLPROC_WALL:
163 xdr_argument = (xdrproc_t)xdr_wrapstring;
164 xdr_result = (xdrproc_t)xdr_void;
165 local = (char *(*) __P((char **, struct svc_req *)))
166 wallproc_wall_1_svc;
167 break;
168
169 default:
170 svcerr_noproc(transp);
171 goto leave;
172 }
173 bzero((char *)&argument, sizeof(argument));
174 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
175 svcerr_decode(transp);
176 goto leave;
177 }
178 result = (*local)((char **)&argument, rqstp);
179 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
180 svcerr_systemerr(transp);
181 }
182 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
183 syslog(LOG_ERR, "unable to free arguments");
184 exit(1);
185 }
186 leave:
187 if (from_inetd)
188 exit(0);
189 }
190