Home | History | Annotate | Line # | Download | only in rpc.sprayd
sprayd.c revision 1.4
      1  1.1  deraadt /*
      2  1.1  deraadt  * Copyright (c) 1994 Christos Zoulas
      3  1.1  deraadt  * All rights reserved.
      4  1.1  deraadt  *
      5  1.1  deraadt  * Redistribution and use in source and binary forms, with or without
      6  1.1  deraadt  * modification, are permitted provided that the following conditions
      7  1.1  deraadt  * are met:
      8  1.1  deraadt  * 1. Redistributions of source code must retain the above copyright
      9  1.1  deraadt  *    notice, this list of conditions and the following disclaimer.
     10  1.1  deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  deraadt  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  deraadt  *    documentation and/or other materials provided with the distribution.
     13  1.1  deraadt  * 3. All advertising materials mentioning features or use of this software
     14  1.1  deraadt  *    must display the following acknowledgement:
     15  1.1  deraadt  *	This product includes software developed by Christos Zoulas.
     16  1.1  deraadt  * 4. The name of the author may not be used to endorse or promote products
     17  1.1  deraadt  *    derived from this software without specific prior written permission.
     18  1.1  deraadt  *
     19  1.1  deraadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  deraadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  deraadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  deraadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  deraadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  deraadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  deraadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  deraadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  deraadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  deraadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  deraadt  *
     30  1.4  mycroft  *	$Id: sprayd.c,v 1.4 1995/01/13 20:53:08 mycroft Exp $
     31  1.1  deraadt  */
     32  1.1  deraadt 
     33  1.1  deraadt #ifndef lint
     34  1.4  mycroft static char rcsid[] = "$Id: sprayd.c,v 1.4 1995/01/13 20:53:08 mycroft Exp $";
     35  1.1  deraadt #endif /* not lint */
     36  1.1  deraadt 
     37  1.1  deraadt #include <stdio.h>
     38  1.1  deraadt #include <signal.h>
     39  1.1  deraadt #include <rpc/rpc.h>
     40  1.1  deraadt #include <sys/time.h>
     41  1.1  deraadt #include <syslog.h>
     42  1.1  deraadt #include <rpcsvc/spray.h>
     43  1.1  deraadt 
     44  1.1  deraadt static void spray_service __P((struct svc_req *, SVCXPRT *));
     45  1.1  deraadt 
     46  1.1  deraadt static int from_inetd = 1;
     47  1.1  deraadt 
     48  1.1  deraadt #define TIMEOUT 120
     49  1.1  deraadt 
     50  1.1  deraadt static void
     51  1.1  deraadt cleanup()
     52  1.1  deraadt {
     53  1.2  deraadt 	(void) pmap_unset(SPRAYPROG, SPRAYVERS);
     54  1.2  deraadt 	exit(0);
     55  1.1  deraadt }
     56  1.1  deraadt 
     57  1.1  deraadt 
     58  1.1  deraadt int
     59  1.1  deraadt main(argc, argv)
     60  1.2  deraadt 	int argc;
     61  1.2  deraadt 	char *argv[];
     62  1.1  deraadt {
     63  1.1  deraadt 	SVCXPRT *transp;
     64  1.2  deraadt 	int sock = 0;
     65  1.2  deraadt 	int proto = 0;
     66  1.1  deraadt 	struct sockaddr_in from;
     67  1.1  deraadt 	int fromlen;
     68  1.2  deraadt 
     69  1.2  deraadt 	/*
     70  1.2  deraadt 	 * See if inetd started us
     71  1.2  deraadt 	 */
     72  1.3  mycroft 	fromlen = sizeof(from);
     73  1.2  deraadt 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
     74  1.2  deraadt 		from_inetd = 0;
     75  1.2  deraadt 		sock = RPC_ANYSOCK;
     76  1.2  deraadt 		proto = IPPROTO_UDP;
     77  1.2  deraadt 	}
     78  1.2  deraadt 
     79  1.2  deraadt 	if (!from_inetd) {
     80  1.2  deraadt 		daemon(0, 0);
     81  1.1  deraadt 
     82  1.2  deraadt 		(void) pmap_unset(SPRAYPROG, SPRAYVERS);
     83  1.1  deraadt 
     84  1.1  deraadt 		(void) signal(SIGINT, cleanup);
     85  1.1  deraadt 		(void) signal(SIGTERM, cleanup);
     86  1.1  deraadt 		(void) signal(SIGHUP, cleanup);
     87  1.4  mycroft 	} else {
     88  1.4  mycroft 		alarm(TIMEOUT);
     89  1.2  deraadt 	}
     90  1.1  deraadt 
     91  1.2  deraadt 	openlog("rpc.sprayd", LOG_CONS|LOG_PID, LOG_DAEMON);
     92  1.2  deraadt 
     93  1.1  deraadt 	transp = svcudp_create(sock);
     94  1.1  deraadt 	if (transp == NULL) {
     95  1.1  deraadt 		syslog(LOG_ERR, "cannot create udp service.");
     96  1.1  deraadt 		return 1;
     97  1.1  deraadt 	}
     98  1.1  deraadt 	if (!svc_register(transp, SPRAYPROG, SPRAYVERS, spray_service, proto)) {
     99  1.1  deraadt 		syslog(LOG_ERR,
    100  1.2  deraadt 		    "unable to register (SPRAYPROG, SPRAYVERS, %s).",
    101  1.2  deraadt 		    proto ? "udp" : "(inetd)");
    102  1.1  deraadt 		return 1;
    103  1.1  deraadt 	}
    104  1.1  deraadt 
    105  1.2  deraadt 	svc_run();
    106  1.1  deraadt 	syslog(LOG_ERR, "svc_run returned");
    107  1.1  deraadt 	return 1;
    108  1.1  deraadt }
    109  1.1  deraadt 
    110  1.1  deraadt 
    111  1.1  deraadt static void
    112  1.1  deraadt spray_service(rqstp, transp)
    113  1.1  deraadt 	struct svc_req *rqstp;
    114  1.1  deraadt 	SVCXPRT *transp;
    115  1.1  deraadt {
    116  1.1  deraadt 	static spraycumul scum;
    117  1.4  mycroft 	static struct timeval clear, get;
    118  1.1  deraadt 
    119  1.1  deraadt 	switch (rqstp->rq_proc) {
    120  1.4  mycroft 	case NULLPROC:
    121  1.4  mycroft 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    122  1.4  mycroft 		return;
    123  1.4  mycroft 
    124  1.1  deraadt 	case SPRAYPROC_CLEAR:
    125  1.1  deraadt 		scum.counter = 0;
    126  1.1  deraadt 		(void) gettimeofday(&clear, 0);
    127  1.1  deraadt 		/*FALLTHROUGH*/
    128  1.1  deraadt 
    129  1.1  deraadt 	case SPRAYPROC_SPRAY:
    130  1.1  deraadt 		scum.counter++;
    131  1.1  deraadt 		return;
    132  1.1  deraadt 
    133  1.1  deraadt 	case SPRAYPROC_GET:
    134  1.4  mycroft 		(void) gettimeofday(&get, 0);
    135  1.4  mycroft 		__timersub(&get, &clear);
    136  1.4  mycroft 		scum.clock.sec = get.tv_sec;
    137  1.4  mycroft 		scum.clock.usec = get.tv_usec;
    138  1.1  deraadt 		break;
    139  1.1  deraadt 
    140  1.1  deraadt 	default:
    141  1.1  deraadt 		svcerr_noproc(transp);
    142  1.1  deraadt 		return;
    143  1.1  deraadt 	}
    144  1.1  deraadt 
    145  1.1  deraadt 	if (!svc_sendreply(transp, xdr_spraycumul, (caddr_t)&scum)) {
    146  1.1  deraadt 		svcerr_systemerr(transp);
    147  1.1  deraadt 		syslog(LOG_ERR, "bad svc_sendreply");
    148  1.1  deraadt 	}
    149  1.1  deraadt }
    150