Blob


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