Home | History | Annotate | Line # | Download | only in rpc.rwalld
rwalld.c revision 1.10
      1 /*	$NetBSD: rwalld.c,v 1.10 1996/08/30 20:22:15 thorpej 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 #ifndef lint
     33 static char rcsid[] = "$NetBSD: rwalld.c,v 1.10 1996/08/30 20:22:15 thorpej Exp $";
     34 #endif /* not lint */
     35 
     36 #include <unistd.h>
     37 #include <sys/types.h>
     38 #include <pwd.h>
     39 #include <stdio.h>
     40 #include <string.h>
     41 #include <syslog.h>
     42 #include <errno.h>
     43 #include <sys/socket.h>
     44 #include <signal.h>
     45 #include <sys/wait.h>
     46 #include <rpc/rpc.h>
     47 #include <rpcsvc/rwall.h>
     48 
     49 #ifdef OSF
     50 #define WALL_CMD "/usr/sbin/wall"
     51 #else
     52 #define WALL_CMD "/usr/bin/wall -n"
     53 #endif
     54 
     55 void wallprog_1();
     56 
     57 int from_inetd = 1;
     58 
     59 void
     60 cleanup()
     61 {
     62 	(void) pmap_unset(WALLPROG, WALLVERS);
     63 	exit(0);
     64 }
     65 
     66 main(argc, argv)
     67 	int argc;
     68 	char *argv[];
     69 {
     70 	SVCXPRT *transp;
     71 	int sock = 0;
     72 	int proto = 0;
     73 	struct sockaddr_in from;
     74 	int fromlen;
     75 
     76 	if (geteuid() == 0) {
     77 		struct passwd *pep = getpwnam("nobody");
     78 		if (pep)
     79 			setuid(pep->pw_uid);
     80 		else
     81 			setuid(getuid());
     82 	}
     83 
     84 	/*
     85 	 * See if inetd started us
     86 	 */
     87 	fromlen = sizeof(from);
     88 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
     89 		from_inetd = 0;
     90 		sock = RPC_ANYSOCK;
     91 		proto = IPPROTO_UDP;
     92 	}
     93 
     94 	if (!from_inetd) {
     95 		daemon(0, 0);
     96 
     97 		(void) pmap_unset(WALLPROG, WALLVERS);
     98 
     99 		(void) signal(SIGINT, cleanup);
    100 		(void) signal(SIGTERM, cleanup);
    101 		(void) signal(SIGHUP, cleanup);
    102 	}
    103 
    104 	openlog("rpc.rwalld", LOG_CONS|LOG_PID, LOG_DAEMON);
    105 
    106 	transp = svcudp_create(sock);
    107 	if (transp == NULL) {
    108 		syslog(LOG_ERR, "cannot create udp service.");
    109 		exit(1);
    110 	}
    111 	if (!svc_register(transp, WALLPROG, WALLVERS, wallprog_1, proto)) {
    112 		syslog(LOG_ERR, "unable to register (WALLPROG, WALLVERS, %s).", proto?"udp":"(inetd)");
    113 		exit(1);
    114 	}
    115 
    116 	svc_run();
    117 	syslog(LOG_ERR, "svc_run returned");
    118 	exit(1);
    119 
    120 }
    121 
    122 void *
    123 wallproc_wall_1_svc(s, rqstp )
    124 	char **s;
    125 	struct svc_req *rqstp;
    126 {
    127 	FILE *pfp;
    128 
    129 	pfp = popen(WALL_CMD, "w");
    130 	if (pfp != NULL) {
    131 		fprintf(pfp, "\007\007%s", *s);
    132 		pclose(pfp);
    133 	}
    134 
    135 	return (*s);
    136 }
    137 
    138 void
    139 wallprog_1(rqstp, transp)
    140 	struct svc_req *rqstp;
    141 	SVCXPRT *transp;
    142 {
    143 	union {
    144 		char *wallproc_wall_1_arg;
    145 	} argument;
    146 	char *result;
    147 	xdrproc_t xdr_argument, xdr_result;
    148 	char *(*local) __P((char **, struct svc_req *));
    149 
    150 	switch (rqstp->rq_proc) {
    151 	case NULLPROC:
    152 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    153 		goto leave;
    154 
    155 	case WALLPROC_WALL:
    156 		xdr_argument = (xdrproc_t)xdr_wrapstring;
    157 		xdr_result = (xdrproc_t)xdr_void;
    158 		local = (char *(*) __P((char **, struct svc_req *)))
    159 			wallproc_wall_1_svc;
    160 		break;
    161 
    162 	default:
    163 		svcerr_noproc(transp);
    164 		goto leave;
    165 	}
    166 	bzero((char *)&argument, sizeof(argument));
    167 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
    168 		svcerr_decode(transp);
    169 		goto leave;
    170 	}
    171 	result = (*local)((char **)&argument, rqstp);
    172 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    173 		svcerr_systemerr(transp);
    174 	}
    175 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
    176 		syslog(LOG_ERR, "unable to free arguments");
    177 		exit(1);
    178 	}
    179 leave:
    180 	if (from_inetd)
    181 		exit(0);
    182 }
    183