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