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