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