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