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