Blame


1 02c349d2 2011-06-28 pbug /*
2 48603f7d 2014-04-13 pjp * Copyright (c) 2011-2014 Peter J. Philipp
3 02c349d2 2011-06-28 pbug * All rights reserved.
4 02c349d2 2011-06-28 pbug *
5 02c349d2 2011-06-28 pbug * Redistribution and use in source and binary forms, with or without
6 02c349d2 2011-06-28 pbug * modification, are permitted provided that the following conditions
7 02c349d2 2011-06-28 pbug * are met:
8 02c349d2 2011-06-28 pbug * 1. Redistributions of source code must retain the above copyright
9 02c349d2 2011-06-28 pbug * notice, this list of conditions and the following disclaimer.
10 02c349d2 2011-06-28 pbug * 2. Redistributions in binary form must reproduce the above copyright
11 02c349d2 2011-06-28 pbug * notice, this list of conditions and the following disclaimer in the
12 02c349d2 2011-06-28 pbug * documentation and/or other materials provided with the distribution.
13 02c349d2 2011-06-28 pbug * 3. The name of the author may not be used to endorse or promote products
14 02c349d2 2011-06-28 pbug * derived from this software without specific prior written permission
15 02c349d2 2011-06-28 pbug *
16 02c349d2 2011-06-28 pbug * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 02c349d2 2011-06-28 pbug * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 02c349d2 2011-06-28 pbug * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 02c349d2 2011-06-28 pbug * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 02c349d2 2011-06-28 pbug * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 02c349d2 2011-06-28 pbug * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 02c349d2 2011-06-28 pbug * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 02c349d2 2011-06-28 pbug * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 02c349d2 2011-06-28 pbug * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 02c349d2 2011-06-28 pbug * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 02c349d2 2011-06-28 pbug *
27 02c349d2 2011-06-28 pbug */
28 02c349d2 2011-06-28 pbug
29 02c349d2 2011-06-28 pbug #include "include.h"
30 02c349d2 2011-06-28 pbug #include "dns.h"
31 02c349d2 2011-06-28 pbug #include "db.h"
32 02c349d2 2011-06-28 pbug
33 02c349d2 2011-06-28 pbug #include <openssl/evp.h>
34 02c349d2 2011-06-28 pbug #include <openssl/hmac.h>
35 02c349d2 2011-06-28 pbug
36 02c349d2 2011-06-28 pbug extern struct logging logging;
37 dd869383 2013-02-16 pjp extern int debug;
38 dd869383 2013-02-16 pjp extern int verbose;
39 dd869383 2013-02-16 pjp
40 0e7d94ac 2014-05-18 pjp void dolog(int pri, char *fmt, ...);
41 0e7d94ac 2014-05-18 pjp void receivelog(char *buf, int len);
42 0e7d94ac 2014-05-18 pjp int remotelog(int fd, char *fmt, ...);
43 02c349d2 2011-06-28 pbug
44 dd869383 2013-02-16 pjp
45 02c349d2 2011-06-28 pbug /*
46 dd869383 2013-02-16 pjp * dolog() - is a wrapper to syslog and printf depending on debug flag
47 dd869383 2013-02-16 pjp *
48 dd869383 2013-02-16 pjp */
49 dd869383 2013-02-16 pjp
50 dd869383 2013-02-16 pjp void
51 dd869383 2013-02-16 pjp dolog(int pri, char *fmt, ...)
52 dd869383 2013-02-16 pjp {
53 dd869383 2013-02-16 pjp va_list ap;
54 dd869383 2013-02-16 pjp
55 dd869383 2013-02-16 pjp va_start(ap, fmt);
56 dd869383 2013-02-16 pjp
57 dd869383 2013-02-16 pjp /*
58 dd869383 2013-02-16 pjp * if the message is a debug message and verbose (-v) is set
59 dd869383 2013-02-16 pjp * then print it, otherwise
60 dd869383 2013-02-16 pjp */
61 dd869383 2013-02-16 pjp
62 dd869383 2013-02-16 pjp if (pri == LOG_DEBUG) {
63 9105332a 2013-02-16 pjp if (verbose && debug)
64 dd869383 2013-02-16 pjp vprintf(fmt, ap);
65 9105332a 2013-02-16 pjp else if (verbose)
66 dd869383 2013-02-16 pjp vsyslog(pri, fmt, ap);
67 dd869383 2013-02-16 pjp } else {
68 dd869383 2013-02-16 pjp if (debug)
69 dd869383 2013-02-16 pjp vprintf(fmt, ap);
70 9105332a 2013-02-16 pjp else
71 dd869383 2013-02-16 pjp vsyslog(pri, fmt, ap);
72 dd869383 2013-02-16 pjp }
73 dd869383 2013-02-16 pjp
74 dd869383 2013-02-16 pjp va_end(ap);
75 dd869383 2013-02-16 pjp
76 dd869383 2013-02-16 pjp }
77 dd869383 2013-02-16 pjp
78 dd869383 2013-02-16 pjp /*
79 02c349d2 2011-06-28 pbug * remotelog() - is like syslog() only the first argument is a filedescriptor
80 02c349d2 2011-06-28 pbug * instead of severity, it will send a packet to the loghost
81 02c349d2 2011-06-28 pbug * signed.
82 02c349d2 2011-06-28 pbug */
83 02c349d2 2011-06-28 pbug
84 02c349d2 2011-06-28 pbug int
85 02c349d2 2011-06-28 pbug remotelog(int fd, char *fmt, ...)
86 02c349d2 2011-06-28 pbug {
87 02c349d2 2011-06-28 pbug va_list ap;
88 02c349d2 2011-06-28 pbug static char buf[1500];
89 02c349d2 2011-06-28 pbug static char outbuf[1500];
90 02c349d2 2011-06-28 pbug char sign[20];
91 02c349d2 2011-06-28 pbug char *p;
92 02c349d2 2011-06-28 pbug u_int rlen;
93 02c349d2 2011-06-28 pbug static u_int64_t sequence = 0;
94 02c349d2 2011-06-28 pbug
95 02c349d2 2011-06-28 pbug
96 02c349d2 2011-06-28 pbug va_start(ap, fmt);
97 02c349d2 2011-06-28 pbug vsnprintf(buf, sizeof(buf), fmt, ap);
98 02c349d2 2011-06-28 pbug va_end(ap);
99 02c349d2 2011-06-28 pbug
100 02c349d2 2011-06-28 pbug #ifdef __NetBSD__
101 02c349d2 2011-06-28 pbug snprintf(outbuf, sizeof(outbuf), "XXXXXXXXXXXXXXXXXXXX%lu %s %s",
102 02c349d2 2011-06-28 pbug #else
103 02c349d2 2011-06-28 pbug snprintf(outbuf, sizeof(outbuf), "XXXXXXXXXXXXXXXXXXXX%llu %s %s",
104 02c349d2 2011-06-28 pbug #endif
105 02c349d2 2011-06-28 pbug sequence++, logging.hostname, buf);
106 02c349d2 2011-06-28 pbug
107 02c349d2 2011-06-28 pbug p = &outbuf[20];
108 02c349d2 2011-06-28 pbug
109 02c349d2 2011-06-28 pbug
110 02c349d2 2011-06-28 pbug HMAC(EVP_sha1(), logging.logpasswd, strlen(logging.logpasswd),
111 02c349d2 2011-06-28 pbug (unsigned char *)p, strlen(p), (unsigned char *)&sign,
112 02c349d2 2011-06-28 pbug &rlen);
113 02c349d2 2011-06-28 pbug
114 02c349d2 2011-06-28 pbug memcpy(outbuf, sign, 20);
115 02c349d2 2011-06-28 pbug
116 02c349d2 2011-06-28 pbug return (send(fd, outbuf, strlen(outbuf), 0));
117 02c349d2 2011-06-28 pbug }
118 02c349d2 2011-06-28 pbug
119 02c349d2 2011-06-28 pbug
120 02c349d2 2011-06-28 pbug void
121 02c349d2 2011-06-28 pbug receivelog(char *buf, int len)
122 02c349d2 2011-06-28 pbug {
123 02c349d2 2011-06-28 pbug static char inbuf[1500];
124 02c349d2 2011-06-28 pbug char sign[20];
125 02c349d2 2011-06-28 pbug char *p;
126 02c349d2 2011-06-28 pbug int rlen;
127 02c349d2 2011-06-28 pbug
128 02c349d2 2011-06-28 pbug if (len < 21 || len > 1450)
129 02c349d2 2011-06-28 pbug return;
130 02c349d2 2011-06-28 pbug
131 02c349d2 2011-06-28 pbug memcpy(&inbuf, buf, len);
132 02c349d2 2011-06-28 pbug inbuf[len] = '\0';
133 02c349d2 2011-06-28 pbug
134 02c349d2 2011-06-28 pbug p = &inbuf[20];
135 02c349d2 2011-06-28 pbug
136 02c349d2 2011-06-28 pbug HMAC(EVP_sha1(), logging.logpasswd, strlen(logging.logpasswd),
137 02c349d2 2011-06-28 pbug (unsigned char *)p, strlen(p), (unsigned char *)&sign,
138 02c349d2 2011-06-28 pbug (unsigned int *)&rlen);
139 02c349d2 2011-06-28 pbug
140 02c349d2 2011-06-28 pbug if (memcmp(inbuf, sign, 20) != 0)
141 02c349d2 2011-06-28 pbug return;
142 02c349d2 2011-06-28 pbug
143 02c349d2 2011-06-28 pbug /* skip sequence number */
144 02c349d2 2011-06-28 pbug p = strchr(p, ' ');
145 02c349d2 2011-06-28 pbug if (p == NULL)
146 02c349d2 2011-06-28 pbug return;
147 02c349d2 2011-06-28 pbug
148 02c349d2 2011-06-28 pbug p++;
149 02c349d2 2011-06-28 pbug
150 02c349d2 2011-06-28 pbug syslog(LOG_INFO, "%s", p);
151 02c349d2 2011-06-28 pbug
152 02c349d2 2011-06-28 pbug return;
153 02c349d2 2011-06-28 pbug }