Blame
Date:
Mon Jan 11 09:00:47 2021 UTC
Message:
a usleep makes it unnecessary slow, I guess it forces a context switch...
001
2014-11-14
pjp
/*
002
2019-11-20
pjp
* Copyright (c) 2011-2019 Peter J. Philipp
003
2014-11-14
pjp
* All rights reserved.
004
2014-11-14
pjp
*
005
2014-11-14
pjp
* Redistribution and use in source and binary forms, with or without
006
2014-11-14
pjp
* modification, are permitted provided that the following conditions
007
2014-11-14
pjp
* are met:
008
2014-11-14
pjp
* 1. Redistributions of source code must retain the above copyright
009
2014-11-14
pjp
* notice, this list of conditions and the following disclaimer.
010
2014-11-14
pjp
* 2. Redistributions in binary form must reproduce the above copyright
011
2014-11-14
pjp
* notice, this list of conditions and the following disclaimer in the
012
2014-11-14
pjp
* documentation and/or other materials provided with the distribution.
013
2014-11-14
pjp
* 3. The name of the author may not be used to endorse or promote products
014
2014-11-14
pjp
* derived from this software without specific prior written permission
015
2014-11-14
pjp
*
016
2014-11-14
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017
2014-11-14
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018
2014-11-14
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019
2014-11-14
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020
2014-11-14
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021
2014-11-14
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022
2014-11-14
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023
2014-11-14
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024
2014-11-14
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025
2014-11-14
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026
2014-11-14
pjp
*
027
2014-11-14
pjp
*/
028
2014-11-14
pjp
029
2019-06-06
pjp
#include <sys/types.h>
030
2019-06-06
pjp
#include <sys/socket.h>
031
2014-11-14
pjp
032
2019-06-06
pjp
#include <netinet/in.h>
033
2019-06-06
pjp
#include <arpa/inet.h>
034
2019-06-06
pjp
#include <netdb.h>
035
2017-10-26
pjp
036
2019-06-06
pjp
#include <stdio.h>
037
2019-06-06
pjp
#include <stdlib.h>
038
2019-06-06
pjp
#include <string.h>
039
2019-11-20
pjp
#include <ctype.h>
040
2017-10-26
pjp
041
2019-06-06
pjp
#include <syslog.h>
042
2019-06-06
pjp
043
2019-06-06
pjp
#ifdef __linux__
044
2019-06-06
pjp
#include <grp.h>
045
2019-06-06
pjp
#define __USE_BSD 1
046
2019-06-06
pjp
#include <endian.h>
047
2019-06-06
pjp
#include <bsd/stdlib.h>
048
2019-06-06
pjp
#include <bsd/string.h>
049
2019-06-06
pjp
#include <bsd/sys/queue.h>
050
2019-06-06
pjp
#define __unused
051
2019-06-06
pjp
#include <bsd/sys/tree.h>
052
2019-06-06
pjp
#include <bsd/sys/endian.h>
053
2019-06-06
pjp
#else /* not linux */
054
2019-06-06
pjp
#include <sys/queue.h>
055
2019-06-06
pjp
#include <sys/tree.h>
056
2019-06-06
pjp
#endif /* __linux__ */
057
2019-06-06
pjp
058
2014-11-14
pjp
#include <openssl/evp.h>
059
2014-11-14
pjp
#include <openssl/hmac.h>
060
2014-11-14
pjp
061
2019-06-06
pjp
#include "ddd-dns.h"
062
2019-06-06
pjp
#include "ddd-db.h"
063
2019-06-06
pjp
064
2014-11-14
pjp
extern int debug;
065
2014-11-14
pjp
extern int verbose;
066
2014-11-14
pjp
067
2014-11-14
pjp
void dolog(int pri, char *fmt, ...);
068
2019-11-20
pjp
char *input_sanitize(char *);
069
2014-11-14
pjp
070
2014-11-14
pjp
071
2014-11-14
pjp
/*
072
2019-11-20
pjp
* INPUT_SANITIZE - syslogd does this sanitization, but in debug mode we want
073
2019-11-20
pjp
* this sanitizer at least.
074
2019-11-20
pjp
*/
075
2019-11-20
pjp
076
2019-11-20
pjp
char *
077
2019-11-20
pjp
input_sanitize(char *fmt)
078
2019-11-20
pjp
{
079
2019-11-20
pjp
char *buf;
080
2019-11-20
pjp
char *p, *q;
081
2019-11-20
pjp
char backslash = '\\';
082
2019-11-20
pjp
083
2019-11-20
pjp
buf = malloc((4 * strlen(fmt)) + 1);
084
2019-11-20
pjp
if (buf == NULL)
085
2019-11-20
pjp
return NULL;
086
2019-11-20
pjp
087
2019-11-20
pjp
q = buf;
088
2019-11-20
pjp
089
2019-11-20
pjp
for (p = fmt; *p; p++) {
090
2019-11-20
pjp
if (*p == backslash) {
091
2019-11-20
pjp
*q++ = *p++;
092
2019-11-20
pjp
if (*p == '\0')
093
2019-11-20
pjp
break;
094
2019-11-20
pjp
switch (*p) {
095
2019-11-20
pjp
case 'n':
096
2019-11-20
pjp
case 't':
097
2019-11-20
pjp
case 'r':
098
2019-11-20
pjp
case '\'':
099
2019-11-20
pjp
case '\\':
100
2019-11-20
pjp
case '"':
101
2019-11-20
pjp
*q++ = *p;
102
2019-11-20
pjp
break;
103
2019-11-20
pjp
default:
104
2019-11-20
pjp
*q++ = '\\';
105
2019-11-20
pjp
*q++ = *p;
106
2019-11-20
pjp
break;
107
2019-11-20
pjp
}
108
2019-11-20
pjp
} else {
109
2019-11-20
pjp
if (isprint(*p) || *p == '\n') {
110
2019-11-20
pjp
*q++ = *p;
111
2019-11-20
pjp
} else {
112
2019-11-20
pjp
*q++ = '\\';
113
2019-11-20
pjp
*q++ = 'x';
114
2019-11-20
pjp
snprintf(q, 3, "%02X", *p & 0xff);
115
2019-11-20
pjp
q += 2;
116
2019-11-20
pjp
}
117
2019-11-20
pjp
}
118
2019-11-20
pjp
}
119
2019-11-20
pjp
120
2019-11-20
pjp
*q = '\0';
121
2019-11-20
pjp
122
2019-11-20
pjp
return (buf);
123
2019-11-20
pjp
}
124
2019-11-20
pjp
125
2019-11-20
pjp
126
2019-11-20
pjp
/*
127
2014-11-14
pjp
* dolog() - is a wrapper to syslog and printf depending on debug flag
128
2014-11-14
pjp
*
129
2014-11-14
pjp
*/
130
2014-11-14
pjp
131
2014-11-14
pjp
void
132
2014-11-14
pjp
dolog(int pri, char *fmt, ...)
133
2014-11-14
pjp
{
134
2014-11-14
pjp
va_list ap;
135
2019-11-20
pjp
char *buf, *sanitize;
136
2014-11-14
pjp
137
2014-11-14
pjp
va_start(ap, fmt);
138
2014-11-14
pjp
139
2014-11-14
pjp
/*
140
2014-11-14
pjp
* if the message is a debug message and verbose (-v) is set
141
2014-11-14
pjp
* then print it, otherwise
142
2014-11-14
pjp
*/
143
2014-11-14
pjp
144
2014-11-14
pjp
if (pri == LOG_DEBUG) {
145
2019-11-20
pjp
if (verbose && debug) {
146
2019-11-20
pjp
buf = malloc(1024);
147
2019-11-20
pjp
if (buf == NULL) {
148
2019-11-20
pjp
printf("-= failed to allocate memory for output buffer =-\n");
149
2019-11-20
pjp
} else {
150
2019-11-20
pjp
vsnprintf(buf, 1024, fmt, ap);
151
2019-11-20
pjp
sanitize = input_sanitize(buf);
152
2019-11-20
pjp
if (sanitize == NULL) {
153
2019-11-20
pjp
printf("-= failed to allocate memory for output buffer =-\n");
154
2019-11-20
pjp
} else {
155
2019-11-20
pjp
printf("%s", sanitize);
156
2019-11-20
pjp
free(sanitize);
157
2019-11-20
pjp
}
158
2019-11-20
pjp
free(buf);
159
2019-11-20
pjp
}
160
2019-11-20
pjp
} else if (verbose)
161
2014-11-14
pjp
vsyslog(pri, fmt, ap);
162
2014-11-14
pjp
} else {
163
2019-11-20
pjp
if (debug) {
164
2019-11-20
pjp
buf = malloc(1024);
165
2019-11-20
pjp
if (buf == NULL) {
166
2019-11-20
pjp
printf("-= failed to allocate memory for output buffer =-\n");
167
2019-11-20
pjp
} else {
168
2019-11-20
pjp
vsnprintf(buf, 1024, fmt, ap);
169
2019-11-20
pjp
sanitize = input_sanitize(buf);
170
2019-11-20
pjp
if (sanitize == NULL) {
171
2019-11-20
pjp
printf("-= failed to allocate memory for output buffer =-\n");
172
2019-11-20
pjp
} else {
173
2019-11-20
pjp
printf("%s", sanitize);
174
2019-11-20
pjp
free(sanitize);
175
2019-11-20
pjp
}
176
2019-11-20
pjp
free(buf);
177
2019-11-20
pjp
}
178
2019-11-20
pjp
} else
179
2014-11-14
pjp
vsyslog(pri, fmt, ap);
180
2014-11-14
pjp
}
181
2014-11-14
pjp
182
2014-11-14
pjp
va_end(ap);
183
2014-11-14
pjp
184
2014-11-14
pjp
}
repomaster@centroid.eu