xref: /openbsd/libexec/rpc.rwalld/rwalld.c (revision 3d8817e4)
1 /*	$OpenBSD: rwalld.c,v 1.14 2009/10/27 23:59:31 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 #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(struct svc_req *, SVCXPRT *);
53 
54 int from_inetd = 1;
55 
56 static void
57 cleanup(int signo)
58 {
59 	(void) pmap_unset(WALLPROG, WALLVERS);		/* XXX signal race */
60 	_exit(0);
61 }
62 
63 int
64 main(int argc, char *argv[])
65 {
66 	int sock = 0, proto = 0;
67 	socklen_t fromlen;
68 	struct sockaddr_storage from;
69 	SVCXPRT *transp;
70 
71 	struct passwd *pw = getpwnam("_rwalld");
72 	if (pw == NULL) {
73 		syslog(LOG_ERR, "no such user _rwalld");
74 		exit(1);
75 	}
76 
77 	setgroups(1, &pw->pw_gid);
78 	setegid(pw->pw_gid);
79 	setgid(pw->pw_gid);
80 	seteuid(pw->pw_uid);
81 	setuid(pw->pw_uid);
82 
83 	/*
84 	 * See if inetd started us
85 	 */
86 	fromlen = sizeof(from);
87 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
88 		from_inetd = 0;
89 		sock = RPC_ANYSOCK;
90 		proto = IPPROTO_UDP;
91 	}
92 
93 	if (!from_inetd) {
94 		daemon(0, 0);
95 
96 		(void) pmap_unset(WALLPROG, WALLVERS);
97 
98 		(void) signal(SIGINT, cleanup);
99 		(void) signal(SIGTERM, cleanup);
100 		(void) signal(SIGHUP, cleanup);
101 	}
102 
103 	openlog("rpc.rwalld", LOG_CONS|LOG_PID, LOG_DAEMON);
104 
105 	transp = svcudp_create(sock);
106 	if (transp == NULL) {
107 		syslog(LOG_ERR, "cannot create udp service.");
108 		exit(1);
109 	}
110 	if (!svc_register(transp, WALLPROG, WALLVERS, wallprog_1, proto)) {
111 		syslog(LOG_ERR, "unable to register (WALLPROG, WALLVERS, %s).",
112 		    proto ? "udp" : "(inetd)");
113 		exit(1);
114 	}
115 
116 	svc_run();
117 	syslog(LOG_ERR, "svc_run returned");
118 	exit(1);
119 
120 }
121 
122 void *
123 wallproc_wall_1_svc(char **s, struct svc_req *rqstp)
124 {
125 	FILE *pfp;
126 
127 	pfp = popen(WALL_CMD, "w");
128 	if (pfp != NULL) {
129 		fprintf(pfp, "\007\007%s", *s);
130 		pclose(pfp);
131 	}
132 
133 	return (*s);
134 }
135 
136 void
137 wallprog_1(struct svc_req *rqstp, SVCXPRT *transp)
138 {
139 	char *(*local)(char **, struct svc_req *);
140 	xdrproc_t xdr_argument, xdr_result;
141 	union {
142 		char *wallproc_wall_1_arg;
143 	} argument;
144 	char *result;
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 = (xdrproc_t)xdr_wrapstring;
153 		xdr_result = (xdrproc_t)xdr_void;
154 		local = (char *(*)(char **, struct svc_req *))
155 		    wallproc_wall_1_svc;
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)((char **)&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