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