1 1.18 plunky /* $NetBSD: sprayd.c,v 1.18 2011/09/16 16:13:17 plunky 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 * 16 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 deraadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 deraadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 deraadt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 deraadt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 deraadt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 deraadt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 deraadt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 deraadt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 deraadt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 deraadt */ 27 1.1 deraadt 28 1.9 christos #include <sys/cdefs.h> 29 1.1 deraadt #ifndef lint 30 1.18 plunky __RCSID("$NetBSD: sprayd.c,v 1.18 2011/09/16 16:13:17 plunky Exp $"); 31 1.1 deraadt #endif /* not lint */ 32 1.1 deraadt 33 1.1 deraadt #include <stdio.h> 34 1.1 deraadt #include <signal.h> 35 1.9 christos #include <stdlib.h> 36 1.9 christos #include <unistd.h> 37 1.9 christos #include <syslog.h> 38 1.9 christos #include <sys/socket.h> 39 1.9 christos #include <sys/time.h> 40 1.1 deraadt #include <rpc/rpc.h> 41 1.1 deraadt #include <rpcsvc/spray.h> 42 1.1 deraadt 43 1.16 joerg __dead static void cleanup(int); 44 1.16 joerg __dead static void die(int); 45 1.12 fvdl static void spray_service(struct svc_req *, SVCXPRT *); 46 1.1 deraadt 47 1.1 deraadt static int from_inetd = 1; 48 1.1 deraadt 49 1.1 deraadt #define TIMEOUT 120 50 1.1 deraadt 51 1.9 christos static void 52 1.12 fvdl cleanup(int n) 53 1.1 deraadt { 54 1.10 mrg 55 1.12 fvdl (void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL); 56 1.2 deraadt exit(0); 57 1.1 deraadt } 58 1.1 deraadt 59 1.9 christos static void 60 1.12 fvdl die(int n) 61 1.6 mycroft { 62 1.10 mrg 63 1.6 mycroft exit(0); 64 1.6 mycroft } 65 1.1 deraadt 66 1.1 deraadt int 67 1.12 fvdl main(int argc, char *argv[]) 68 1.1 deraadt { 69 1.1 deraadt SVCXPRT *transp; 70 1.12 fvdl struct sockaddr_storage from; 71 1.14 mrg socklen_t fromlen; 72 1.6 mycroft 73 1.2 deraadt /* 74 1.2 deraadt * See if inetd started us 75 1.2 deraadt */ 76 1.3 mycroft fromlen = sizeof(from); 77 1.12 fvdl if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) 78 1.2 deraadt from_inetd = 0; 79 1.6 mycroft 80 1.2 deraadt if (!from_inetd) { 81 1.2 deraadt daemon(0, 0); 82 1.1 deraadt 83 1.12 fvdl (void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL); 84 1.1 deraadt 85 1.10 mrg (void)signal(SIGINT, cleanup); 86 1.10 mrg (void)signal(SIGTERM, cleanup); 87 1.10 mrg (void)signal(SIGHUP, cleanup); 88 1.4 mycroft } else { 89 1.10 mrg (void)signal(SIGALRM, die); 90 1.4 mycroft alarm(TIMEOUT); 91 1.2 deraadt } 92 1.1 deraadt 93 1.11 mrg openlog("rpc.sprayd", LOG_PID, LOG_DAEMON); 94 1.6 mycroft 95 1.12 fvdl if (from_inetd) { 96 1.12 fvdl transp = svc_dg_create(0, 0, 0); 97 1.12 fvdl if (transp == NULL) { 98 1.12 fvdl syslog(LOG_ERR, "cannot create udp service."); 99 1.13 lukem exit(1); 100 1.12 fvdl } 101 1.12 fvdl if (!svc_reg(transp, SPRAYPROG, SPRAYVERS, spray_service, 102 1.12 fvdl NULL)) { 103 1.12 fvdl syslog(LOG_ERR, 104 1.12 fvdl "unable to register (SPRAYPROG, SPRAYVERS)."); 105 1.12 fvdl exit(1); 106 1.12 fvdl } 107 1.12 fvdl } else { 108 1.12 fvdl if (!svc_create(spray_service, SPRAYPROG, SPRAYVERS, "udp")) { 109 1.12 fvdl syslog(LOG_ERR, 110 1.12 fvdl "unable to register (SPRAYPROG, SPRAYVERS)."); 111 1.12 fvdl exit(1); 112 1.12 fvdl } 113 1.1 deraadt } 114 1.1 deraadt 115 1.2 deraadt svc_run(); 116 1.1 deraadt syslog(LOG_ERR, "svc_run returned"); 117 1.13 lukem exit(1); 118 1.1 deraadt } 119 1.1 deraadt 120 1.1 deraadt 121 1.1 deraadt static void 122 1.12 fvdl spray_service(struct svc_req *rqstp, SVCXPRT *transp) 123 1.1 deraadt { 124 1.1 deraadt static spraycumul scum; 125 1.4 mycroft static struct timeval clear, get; 126 1.1 deraadt 127 1.1 deraadt switch (rqstp->rq_proc) { 128 1.1 deraadt case SPRAYPROC_CLEAR: 129 1.1 deraadt scum.counter = 0; 130 1.10 mrg (void)gettimeofday(&clear, 0); 131 1.1 deraadt /*FALLTHROUGH*/ 132 1.5 mycroft 133 1.5 mycroft case NULLPROC: 134 1.18 plunky (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL); 135 1.5 mycroft return; 136 1.1 deraadt 137 1.1 deraadt case SPRAYPROC_SPRAY: 138 1.1 deraadt scum.counter++; 139 1.1 deraadt return; 140 1.1 deraadt 141 1.1 deraadt case SPRAYPROC_GET: 142 1.10 mrg (void)gettimeofday(&get, 0); 143 1.7 mycroft timersub(&get, &clear, &get); 144 1.4 mycroft scum.clock.sec = get.tv_sec; 145 1.4 mycroft scum.clock.usec = get.tv_usec; 146 1.1 deraadt break; 147 1.1 deraadt 148 1.1 deraadt default: 149 1.1 deraadt svcerr_noproc(transp); 150 1.1 deraadt return; 151 1.1 deraadt } 152 1.1 deraadt 153 1.17 plunky if (!svc_sendreply(transp, (xdrproc_t)xdr_spraycumul, (caddr_t)&scum)) { 154 1.1 deraadt svcerr_systemerr(transp); 155 1.13 lukem syslog(LOG_WARNING, "bad svc_sendreply"); 156 1.1 deraadt } 157 1.1 deraadt } 158