1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
* tcpproxy
*
* tcpproxy is a simple tcp connection proxy which combines the
* features of rinetd and 6tunnel. tcpproxy supports IPv4 and
* IPv6 and also supports connections from IPv6 to IPv4
* endpoints and vice versa.
*
*
* Copyright (C) 2010-2015 Christian Pointner <equinox@spreadspace.org>
*
* This file is part of tcpproxy.
*
* tcpproxy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* tcpproxy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tcpproxy. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TCPPROXY_log_targets_h_INCLUDED
#define TCPPROXY_log_targets_h_INCLUDED
#include <time.h>
static char* get_time_formatted()
{
char* time_string;
time_t t = time(NULL);
if(t < 0)
time_string = "<time read error>";
else {
time_string = ctime(&t);
if(!time_string)
time_string = "<time format error>";
else {
char* newline = strchr(time_string, '\n');
if(newline)
newline[0] = 0;
}
}
return time_string;
}
#ifndef WINVER
enum syslog_facility_enum { USER = LOG_USER, MAIL = LOG_MAIL,
DAEMON = LOG_DAEMON, AUTH = LOG_AUTH,
SYSLOG = LOG_SYSLOG, LPR = LOG_LPR,
NEWS = LOG_NEWS, UUCP = LOG_UUCP,
CRON = LOG_CRON, AUTHPRIV = LOG_AUTHPRIV,
FTP = LOG_FTP, LOCAL0 = LOG_LOCAL0,
LOCAL1 = LOG_LOCAL1, LOCAL2 = LOG_LOCAL2,
LOCAL3 = LOG_LOCAL3, LOCAL4 = LOG_LOCAL4,
LOCAL5 = LOG_LOCAL5, LOCAL6 = LOG_LOCAL6,
LOCAL7 = LOG_LOCAL7 };
typedef enum syslog_facility_enum syslog_facility_t;
struct log_target_syslog_param_struct {
char* logname_;
syslog_facility_t facility_;
};
typedef struct log_target_syslog_param_struct log_target_syslog_param_t;
int log_target_syslog_init(log_target_t* self, const char* conf)
{
if(!self || (conf && conf[0] == 0))
return -1;
self->param_ = malloc(sizeof(log_target_syslog_param_t));
if(!self->param_)
return -2;
char* logname;
const char* end = NULL;
if(!conf)
logname = strdup("tcpproxy");
else {
end = strchr(conf, ',');
if(end) {
size_t len = (size_t)(end - conf);
if(!len) {
free(self->param_);
return -1;
}
logname = malloc(len+1);
if(logname) {
strncpy(logname, conf, len);
logname[len] = 0;
}
}
else
logname = strdup(conf);
}
if(!logname) {
free(self->param_);
return -2;
}
((log_target_syslog_param_t*)(self->param_))->logname_ = logname;
if(!end) {
((log_target_syslog_param_t*)(self->param_))->facility_ = DAEMON;
return 0;
}
if(end[1] == 0 || end[1] == ',') {
free(logname);
free(self->param_);
return -1;
}
const char* start = end + 1;
end = strchr(start, ',');
int i;
for(i=0;;++i) {
if(facilitynames[i].c_name == NULL) {
free(logname);
free(self->param_);
return -1;
}
if(( end && !strncmp(start, facilitynames[i].c_name, (size_t)(end - start)) && facilitynames[i].c_name[(size_t)(end-start)] == 0) ||
(!end && !strcmp(start, facilitynames[i].c_name))) {
((log_target_syslog_param_t*)(self->param_))->facility_ = facilitynames[i].c_val;
break;
}
}
return 0;
}
void log_target_syslog_open(log_target_t* self)
{
if(!self || !self->param_)
return;
openlog(((log_target_syslog_param_t*)(self->param_))->logname_, LOG_PID, ((log_target_syslog_param_t*)(self->param_))->facility_);
self->opened_ = 1;
}
void log_target_syslog_log(log_target_t* self, log_prio_t prio, const char* msg)
{
if(!self || !self->param_ || !self->opened_)
return;
syslog((prio + 2) | ((log_target_syslog_param_t*)(self->param_))->facility_, "%s", msg);
}
void log_target_syslog_close(log_target_t* self)
{
closelog();
self->opened_ = 0;
}
void log_target_syslog_clear(log_target_t* self)
{
if(!self || !self->param_)
return;
if(((log_target_syslog_param_t*)(self->param_))->logname_)
free(((log_target_syslog_param_t*)(self->param_))->logname_);
free(self->param_);
}
log_target_t* log_target_syslog_new()
{
log_target_t* tmp = malloc(sizeof(log_target_t));
if(!tmp)
return NULL;
tmp->type_ = TARGET_SYSLOG;
tmp->init = &log_target_syslog_init;
tmp->open = &log_target_syslog_open;
tmp->log = &log_target_syslog_log;
tmp->close = &log_target_syslog_close;
tmp->clear = &log_target_syslog_clear;
tmp->opened_ = 0;
tmp->enabled_ = 0;
tmp->max_prio_ = NOTICE;
tmp->param_ = NULL;
tmp->next_ = NULL;
return tmp;
}
#endif
struct log_target_file_param_struct {
char* logfilename_;
FILE* file_;
};
typedef struct log_target_file_param_struct log_target_file_param_t;
int log_target_file_init(log_target_t* self, const char* conf)
{
if(!self || (conf && conf[0] == 0))
return -1;
self->param_ = malloc(sizeof(log_target_file_param_t));
if(!self->param_)
return -2;
char* logfilename;
if(!conf)
logfilename = strdup("tcpproxy.log");
else {
const char* end = strchr(conf, ',');
if(end) {
size_t len = (size_t)(end - conf);
if(!len) {
free(self->param_);
return -1;
}
logfilename = malloc(len+1);
if(logfilename) {
strncpy(logfilename, conf, len);
logfilename[len] = 0;
}
}
else
logfilename = strdup(conf);
}
if(!logfilename) {
free(self->param_);
return -2;
}
((log_target_file_param_t*)(self->param_))->logfilename_ = logfilename;
((log_target_file_param_t*)(self->param_))->file_ = NULL;
return 0;
}
void log_target_file_open(log_target_t* self)
{
if(!self || !self->param_)
return;
((log_target_file_param_t*)(self->param_))->file_ = fopen(((log_target_file_param_t*)(self->param_))->logfilename_, "w");
if(((log_target_file_param_t*)(self->param_))->file_)
self->opened_ = 1;
}
void log_target_file_log(log_target_t* self, log_prio_t prio, const char* msg)
{
if(!self || !self->param_ || !self->opened_)
return;
fprintf(((log_target_file_param_t*)(self->param_))->file_, "%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
fflush(((log_target_file_param_t*)(self->param_))->file_);
}
void log_target_file_close(log_target_t* self)
{
if(!self || !self->param_)
return;
fclose(((log_target_file_param_t*)(self->param_))->file_);
self->opened_ = 0;
}
void log_target_file_clear(log_target_t* self)
{
if(!self || !self->param_)
return;
if(((log_target_file_param_t*)(self->param_))->logfilename_)
free(((log_target_file_param_t*)(self->param_))->logfilename_);
free(self->param_);
}
log_target_t* log_target_file_new()
{
log_target_t* tmp = malloc(sizeof(log_target_t));
if(!tmp)
return NULL;
tmp->type_ = TARGET_FILE;
tmp->init = &log_target_file_init;
tmp->open = &log_target_file_open;
tmp->log = &log_target_file_log;
tmp->close = &log_target_file_close;
tmp->clear = &log_target_file_clear;
tmp->opened_ = 0;
tmp->enabled_ = 0;
tmp->max_prio_ = NOTICE;
tmp->param_ = NULL;
tmp->next_ = NULL;
return tmp;
}
void log_target_stdout_log(log_target_t* self, log_prio_t prio, const char* msg)
{
printf("%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
}
log_target_t* log_target_stdout_new()
{
log_target_t* tmp = malloc(sizeof(log_target_t));
if(!tmp)
return NULL;
tmp->type_ = TARGET_STDOUT;
tmp->init = NULL;
tmp->open = NULL;
tmp->log = &log_target_stdout_log;
tmp->close = NULL;
tmp->clear = NULL;
tmp->opened_ = 0;
tmp->enabled_ = 0;
tmp->max_prio_ = NOTICE;
tmp->param_ = NULL;
tmp->next_ = NULL;
return tmp;
}
void log_target_stderr_log(log_target_t* self, log_prio_t prio, const char* msg)
{
fprintf(stderr, "%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
}
log_target_t* log_target_stderr_new()
{
log_target_t* tmp = malloc(sizeof(log_target_t));
if(!tmp)
return NULL;
tmp->type_ = TARGET_STDERR;
tmp->init = NULL;
tmp->open = NULL;
tmp->log = &log_target_stderr_log;
tmp->close = NULL;
tmp->clear = NULL;
tmp->opened_ = 0;
tmp->enabled_ = 0;
tmp->max_prio_ = NOTICE;
tmp->param_ = NULL;
tmp->next_ = NULL;
return tmp;
}
#endif
|