2017-11-27 15:25:02 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-08-28 14:58:36 +00:00
|
|
|
* Copyright (c) 1993 Christopher G. Demetriou
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2010-12-19 13:05:17 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-08-28 14:58:36 +00:00
|
|
|
|
1997-12-02 12:20:17 +00:00
|
|
|
#include <err.h>
|
1994-08-28 14:58:36 +00:00
|
|
|
#include <pwd.h>
|
1997-12-02 12:20:17 +00:00
|
|
|
#include <signal.h>
|
1994-08-28 14:58:36 +00:00
|
|
|
#include <stdio.h>
|
1997-12-02 12:20:17 +00:00
|
|
|
#include <stdlib.h>
|
1994-08-28 14:58:36 +00:00
|
|
|
#include <string.h>
|
1997-12-02 12:20:17 +00:00
|
|
|
#include <syslog.h>
|
2002-02-18 20:35:27 +00:00
|
|
|
#include <arpa/inet.h>
|
1994-08-28 14:58:36 +00:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpcsvc/rwall.h>
|
1997-12-02 12:20:17 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
1994-08-28 14:58:36 +00:00
|
|
|
|
|
|
|
#ifdef OSF
|
|
|
|
#define WALL_CMD "/usr/sbin/wall"
|
|
|
|
#else
|
|
|
|
#define WALL_CMD "/usr/bin/wall -n"
|
|
|
|
#endif
|
|
|
|
|
2002-02-07 05:24:53 +00:00
|
|
|
void wallprog_1(struct svc_req *rqstp, SVCXPRT *transp);
|
|
|
|
void possess(void);
|
|
|
|
void killkids(int sig);
|
|
|
|
static void usage(void);
|
1994-08-28 14:58:36 +00:00
|
|
|
|
|
|
|
int nodaemon = 0;
|
|
|
|
int from_inetd = 1;
|
|
|
|
|
1997-12-02 12:20:17 +00:00
|
|
|
int
|
2002-02-07 05:24:53 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-08-28 14:58:36 +00:00
|
|
|
{
|
|
|
|
SVCXPRT *transp;
|
2005-02-14 17:42:58 +00:00
|
|
|
socklen_t salen;
|
|
|
|
int ok;
|
2002-07-15 18:51:57 +00:00
|
|
|
struct sockaddr_storage sa;
|
1994-08-28 14:58:36 +00:00
|
|
|
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "-n"))
|
|
|
|
nodaemon = 1;
|
1997-12-02 12:20:17 +00:00
|
|
|
if (argc != 1 && !nodaemon)
|
|
|
|
usage();
|
1994-08-28 14:58:36 +00:00
|
|
|
|
|
|
|
if (geteuid() == 0) {
|
|
|
|
struct passwd *pep = getpwnam("nobody");
|
|
|
|
if (pep)
|
|
|
|
setuid(pep->pw_uid);
|
|
|
|
else
|
|
|
|
setuid(getuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if inetd started us
|
|
|
|
*/
|
1994-11-18 22:50:22 +00:00
|
|
|
salen = sizeof(sa);
|
1994-08-28 14:58:36 +00:00
|
|
|
if (getsockname(0, (struct sockaddr *)&sa, &salen) < 0) {
|
|
|
|
from_inetd = 0;
|
|
|
|
}
|
1995-05-30 05:51:47 +00:00
|
|
|
|
1994-08-28 14:58:36 +00:00
|
|
|
if (!from_inetd) {
|
|
|
|
if (!nodaemon)
|
|
|
|
possess();
|
|
|
|
|
2002-07-15 18:51:57 +00:00
|
|
|
(void)rpcb_unset(WALLPROG, WALLVERS, NULL);
|
1994-08-28 14:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)signal(SIGCHLD, killkids);
|
|
|
|
|
1997-12-02 12:20:17 +00:00
|
|
|
openlog("rpc.rwalld", LOG_CONS|LOG_PID, LOG_DAEMON);
|
|
|
|
|
2002-07-15 18:51:57 +00:00
|
|
|
/* create and register the service */
|
|
|
|
if (from_inetd) {
|
|
|
|
transp = svc_tli_create(0, NULL, NULL, 0, 0);
|
|
|
|
if (transp == NULL) {
|
|
|
|
syslog(LOG_ERR, "couldn't create udp service.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ok = svc_reg(transp, WALLPROG, WALLVERS,
|
|
|
|
wallprog_1, NULL);
|
|
|
|
} else
|
|
|
|
ok = svc_create(wallprog_1,
|
|
|
|
WALLPROG, WALLVERS, "udp");
|
|
|
|
if (!ok) {
|
|
|
|
syslog(LOG_ERR, "unable to register (WALLPROG, WALLVERS, %s)", (!from_inetd)?"udp":"(inetd)");
|
1994-08-28 14:58:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
svc_run();
|
1997-12-02 12:20:17 +00:00
|
|
|
syslog(LOG_ERR, "svc_run returned");
|
1994-08-28 14:58:36 +00:00
|
|
|
exit(1);
|
1997-12-02 12:20:17 +00:00
|
|
|
}
|
1994-08-28 14:58:36 +00:00
|
|
|
|
1997-12-02 12:20:17 +00:00
|
|
|
static void
|
2002-02-07 05:24:53 +00:00
|
|
|
usage(void)
|
1997-12-02 12:20:17 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: rpc.rwalld [-n]\n");
|
|
|
|
exit(1);
|
1994-08-28 14:58:36 +00:00
|
|
|
}
|
|
|
|
|
2002-02-07 05:24:53 +00:00
|
|
|
void
|
|
|
|
possess(void)
|
1994-08-28 14:58:36 +00:00
|
|
|
{
|
|
|
|
daemon(0, 0);
|
|
|
|
}
|
|
|
|
|
2002-02-07 05:24:53 +00:00
|
|
|
void
|
2010-12-19 13:05:17 +00:00
|
|
|
killkids(int sig __unused)
|
1994-08-28 14:58:36 +00:00
|
|
|
{
|
|
|
|
while(wait4(-1, NULL, WNOHANG, NULL) > 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2002-02-07 05:24:53 +00:00
|
|
|
void *
|
2010-12-19 13:05:17 +00:00
|
|
|
wallproc_wall_1_svc(wrapstring *s, struct svc_req *rqstp __unused)
|
1994-08-28 14:58:36 +00:00
|
|
|
{
|
1998-01-19 23:13:38 +00:00
|
|
|
static void *dummy = NULL;
|
|
|
|
|
1994-08-28 14:58:36 +00:00
|
|
|
/* fork, popen wall with special option, and send the message */
|
|
|
|
if (fork() == 0) {
|
|
|
|
FILE *pfp;
|
|
|
|
|
|
|
|
pfp = popen(WALL_CMD, "w");
|
|
|
|
if (pfp != NULL) {
|
|
|
|
fprintf(pfp, "\007\007%s", *s);
|
|
|
|
pclose(pfp);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
1998-01-19 23:13:38 +00:00
|
|
|
return(&dummy);
|
1994-08-28 14:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-02-07 05:24:53 +00:00
|
|
|
wallprog_1(struct svc_req *rqstp, SVCXPRT *transp)
|
1994-08-28 14:58:36 +00:00
|
|
|
{
|
|
|
|
union {
|
|
|
|
char *wallproc_wall_1_arg;
|
|
|
|
} argument;
|
|
|
|
char *result;
|
|
|
|
bool_t (*xdr_argument)(), (*xdr_result)();
|
|
|
|
char *(*local)();
|
|
|
|
|
|
|
|
switch (rqstp->rq_proc) {
|
|
|
|
case NULLPROC:
|
2002-04-28 15:18:50 +00:00
|
|
|
(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
|
1994-08-28 14:58:36 +00:00
|
|
|
goto leave;
|
|
|
|
|
|
|
|
case WALLPROC_WALL:
|
|
|
|
xdr_argument = xdr_wrapstring;
|
|
|
|
xdr_result = xdr_void;
|
1998-01-19 23:13:38 +00:00
|
|
|
local = (char *(*)()) wallproc_wall_1_svc;
|
1994-08-28 14:58:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
svcerr_noproc(transp);
|
|
|
|
goto leave;
|
|
|
|
}
|
2002-04-28 15:18:50 +00:00
|
|
|
bzero(&argument, sizeof(argument));
|
|
|
|
if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
|
1994-08-28 14:58:36 +00:00
|
|
|
svcerr_decode(transp);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
result = (*local)(&argument, rqstp);
|
2002-04-28 15:18:50 +00:00
|
|
|
if (result != NULL &&
|
|
|
|
!svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
|
1994-08-28 14:58:36 +00:00
|
|
|
svcerr_systemerr(transp);
|
|
|
|
}
|
2002-04-28 15:18:50 +00:00
|
|
|
if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
|
1997-12-02 12:20:17 +00:00
|
|
|
syslog(LOG_ERR, "unable to free arguments");
|
1994-08-28 14:58:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
leave:
|
|
|
|
if (from_inetd)
|
|
|
|
exit(0);
|
|
|
|
}
|