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