rwalld.c revision 1.15 1 1.15 fvdl /* $NetBSD: rwalld.c,v 1.15 2000/06/03 20:31:25 fvdl Exp $ */
2 1.10 thorpej
3 1.1 brezak /*
4 1.1 brezak * Copyright (c) 1993 Christopher G. Demetriou
5 1.1 brezak * All rights reserved.
6 1.1 brezak *
7 1.1 brezak * Redistribution and use in source and binary forms, with or without
8 1.1 brezak * modification, are permitted provided that the following conditions
9 1.1 brezak * are met:
10 1.1 brezak * 1. Redistributions of source code must retain the above copyright
11 1.1 brezak * notice, this list of conditions and the following disclaimer.
12 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 brezak * notice, this list of conditions and the following disclaimer in the
14 1.1 brezak * documentation and/or other materials provided with the distribution.
15 1.1 brezak * 3. The name of the author may not be used to endorse or promote
16 1.1 brezak * products derived from this software without specific prior written
17 1.1 brezak * permission.
18 1.1 brezak *
19 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.1 brezak * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 brezak * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 1.1 brezak * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 brezak * SUCH DAMAGE.
30 1.1 brezak */
31 1.1 brezak
32 1.11 christos #include <sys/cdefs.h>
33 1.1 brezak #ifndef lint
34 1.15 fvdl __RCSID("$NetBSD: rwalld.c,v 1.15 2000/06/03 20:31:25 fvdl Exp $");
35 1.2 mycroft #endif /* not lint */
36 1.1 brezak
37 1.1 brezak #include <unistd.h>
38 1.11 christos #include <stdlib.h>
39 1.1 brezak #include <sys/types.h>
40 1.1 brezak #include <pwd.h>
41 1.1 brezak #include <stdio.h>
42 1.1 brezak #include <string.h>
43 1.6 mycroft #include <syslog.h>
44 1.1 brezak #include <errno.h>
45 1.1 brezak #include <sys/socket.h>
46 1.1 brezak #include <signal.h>
47 1.1 brezak #include <sys/wait.h>
48 1.1 brezak #include <rpc/rpc.h>
49 1.1 brezak #include <rpcsvc/rwall.h>
50 1.1 brezak
51 1.1 brezak #ifdef OSF
52 1.1 brezak #define WALL_CMD "/usr/sbin/wall"
53 1.1 brezak #else
54 1.1 brezak #define WALL_CMD "/usr/bin/wall -n"
55 1.1 brezak #endif
56 1.1 brezak
57 1.11 christos static int from_inetd = 1;
58 1.1 brezak
59 1.15 fvdl static void cleanup(int);
60 1.15 fvdl static void wallprog_1(struct svc_req *, SVCXPRT *);
61 1.1 brezak
62 1.15 fvdl int main(int, char *[]);
63 1.11 christos
64 1.11 christos static void
65 1.15 fvdl cleanup(int n)
66 1.5 mycroft {
67 1.12 mrg
68 1.15 fvdl (void)rpcb_unset(WALLPROG, WALLVERS, NULL);
69 1.5 mycroft exit(0);
70 1.5 mycroft }
71 1.5 mycroft
72 1.11 christos int
73 1.15 fvdl main(int argc, char *argv[])
74 1.1 brezak {
75 1.1 brezak SVCXPRT *transp;
76 1.5 mycroft struct sockaddr_in from;
77 1.5 mycroft int fromlen;
78 1.1 brezak
79 1.1 brezak if (geteuid() == 0) {
80 1.1 brezak struct passwd *pep = getpwnam("nobody");
81 1.1 brezak if (pep)
82 1.1 brezak setuid(pep->pw_uid);
83 1.1 brezak else
84 1.1 brezak setuid(getuid());
85 1.1 brezak }
86 1.1 brezak
87 1.5 mycroft /*
88 1.5 mycroft * See if inetd started us
89 1.5 mycroft */
90 1.4 mycroft fromlen = sizeof(from);
91 1.15 fvdl if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
92 1.5 mycroft from_inetd = 0;
93 1.5 mycroft
94 1.5 mycroft if (!from_inetd) {
95 1.5 mycroft daemon(0, 0);
96 1.5 mycroft
97 1.15 fvdl (void) rpcb_unset(WALLPROG, WALLVERS, NULL);
98 1.5 mycroft
99 1.5 mycroft (void) signal(SIGINT, cleanup);
100 1.5 mycroft (void) signal(SIGTERM, cleanup);
101 1.5 mycroft (void) signal(SIGHUP, cleanup);
102 1.5 mycroft }
103 1.1 brezak
104 1.14 mrg openlog("rpc.rwalld", LOG_PID, LOG_DAEMON);
105 1.1 brezak
106 1.15 fvdl if (from_inetd) {
107 1.15 fvdl transp = svc_dg_create(0, 0, 0);
108 1.15 fvdl if (transp == NULL) {
109 1.15 fvdl syslog(LOG_ERR, "cannot create udp service.");
110 1.15 fvdl exit(1);
111 1.15 fvdl }
112 1.15 fvdl if (!svc_reg(transp, WALLPROG, WALLVERS, wallprog_1, NULL)) {
113 1.15 fvdl syslog(LOG_ERR, "unable to register "
114 1.15 fvdl "(WALLPROG, WALLVERS).");
115 1.15 fvdl exit(1);
116 1.15 fvdl }
117 1.15 fvdl } else {
118 1.15 fvdl if (!svc_create(wallprog_1, WALLPROG, WALLVERS, "udp")) {
119 1.15 fvdl syslog(LOG_ERR, "unable to create "
120 1.15 fvdl "(WALLPROG, WALLVERS.)");
121 1.15 fvdl exit(1);
122 1.15 fvdl }
123 1.1 brezak }
124 1.5 mycroft
125 1.1 brezak svc_run();
126 1.5 mycroft syslog(LOG_ERR, "svc_run returned");
127 1.1 brezak exit(1);
128 1.1 brezak
129 1.1 brezak }
130 1.1 brezak
131 1.5 mycroft void *
132 1.15 fvdl wallproc_wall_1_svc(char **s, struct svc_req *rqstp)
133 1.1 brezak {
134 1.5 mycroft FILE *pfp;
135 1.1 brezak
136 1.5 mycroft pfp = popen(WALL_CMD, "w");
137 1.5 mycroft if (pfp != NULL) {
138 1.5 mycroft fprintf(pfp, "\007\007%s", *s);
139 1.5 mycroft pclose(pfp);
140 1.5 mycroft }
141 1.1 brezak
142 1.7 mycroft return (*s);
143 1.1 brezak }
144 1.1 brezak
145 1.11 christos static void
146 1.15 fvdl wallprog_1(struct svc_req *rqstp, SVCXPRT *transp)
147 1.1 brezak {
148 1.1 brezak union {
149 1.1 brezak char *wallproc_wall_1_arg;
150 1.1 brezak } argument;
151 1.1 brezak char *result;
152 1.9 pk xdrproc_t xdr_argument, xdr_result;
153 1.8 pk char *(*local) __P((char **, struct svc_req *));
154 1.1 brezak
155 1.1 brezak switch (rqstp->rq_proc) {
156 1.1 brezak case NULLPROC:
157 1.1 brezak (void)svc_sendreply(transp, xdr_void, (char *)NULL);
158 1.1 brezak goto leave;
159 1.1 brezak
160 1.1 brezak case WALLPROC_WALL:
161 1.9 pk xdr_argument = (xdrproc_t)xdr_wrapstring;
162 1.9 pk xdr_result = (xdrproc_t)xdr_void;
163 1.8 pk local = (char *(*) __P((char **, struct svc_req *)))
164 1.8 pk wallproc_wall_1_svc;
165 1.1 brezak break;
166 1.1 brezak
167 1.1 brezak default:
168 1.1 brezak svcerr_noproc(transp);
169 1.1 brezak goto leave;
170 1.1 brezak }
171 1.13 perry memset((char *)&argument, 0, sizeof(argument));
172 1.3 cgd if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
173 1.1 brezak svcerr_decode(transp);
174 1.1 brezak goto leave;
175 1.1 brezak }
176 1.8 pk result = (*local)((char **)&argument, rqstp);
177 1.1 brezak if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
178 1.1 brezak svcerr_systemerr(transp);
179 1.1 brezak }
180 1.3 cgd if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
181 1.6 mycroft syslog(LOG_ERR, "unable to free arguments");
182 1.1 brezak exit(1);
183 1.1 brezak }
184 1.1 brezak leave:
185 1.5 mycroft if (from_inetd)
186 1.5 mycroft exit(0);
187 1.1 brezak }
188