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