xref: /openbsd/libexec/rpc.rwalld/rwalld.c (revision 9b7c3dbb)
1 /*	$OpenBSD: rwalld.c,v 1.15 2016/04/25 15:43:34 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 	setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid);
79 	setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
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).",
110 		    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_svc(char **s, struct svc_req *rqstp)
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 (*s);
132 }
133 
134 void
135 wallprog_1(struct svc_req *rqstp, SVCXPRT *transp)
136 {
137 	char *(*local)(char **, struct svc_req *);
138 	xdrproc_t xdr_argument, xdr_result;
139 	union {
140 		char *wallproc_wall_1_arg;
141 	} argument;
142 	char *result;
143 
144 	switch (rqstp->rq_proc) {
145 	case NULLPROC:
146 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
147 		goto leave;
148 
149 	case WALLPROC_WALL:
150 		xdr_argument = (xdrproc_t)xdr_wrapstring;
151 		xdr_result = (xdrproc_t)xdr_void;
152 		local = (char *(*)(char **, struct svc_req *))
153 		    wallproc_wall_1_svc;
154 		break;
155 
156 	default:
157 		svcerr_noproc(transp);
158 		goto leave;
159 	}
160 	bzero((char *)&argument, sizeof(argument));
161 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
162 		svcerr_decode(transp);
163 		goto leave;
164 	}
165 	result = (*local)((char **)&argument, rqstp);
166 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
167 		svcerr_systemerr(transp);
168 	}
169 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
170 		syslog(LOG_ERR, "unable to free arguments");
171 		exit(1);
172 	}
173 leave:
174 	if (from_inetd)
175 		exit(0);
176 }
177