2020-10-08 00:03:59 +08:00
|
|
|
/*
|
|
|
|
3APA3A simpliest proxy server
|
2020-10-09 20:42:34 +08:00
|
|
|
(c) 2002-2020 by Vladimir Dubrovin <3proxy@3proxy.ru>
|
2020-10-08 00:03:59 +08:00
|
|
|
|
|
|
|
please read License Agreement
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "proxy.h"
|
2020-10-09 20:42:34 +08:00
|
|
|
pthread_mutex_t log_mutex;
|
2020-10-28 21:45:21 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE log_sem;
|
|
|
|
#else
|
|
|
|
sem_t log_sem;
|
|
|
|
#endif
|
2020-10-30 16:17:28 +08:00
|
|
|
#define MAXLOG 1024
|
|
|
|
#define EVENTSIZE (4096 - sizeof(void *))
|
|
|
|
#define LOGBUFSIZE (EVENTSIZE - sizeof(struct logevent))
|
|
|
|
#define MAX_SEM_COUNT 256
|
2020-10-28 21:45:21 +08:00
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
typedef enum {
|
|
|
|
REGISTER,
|
|
|
|
UNREGISTER,
|
|
|
|
LOG,
|
|
|
|
FLUSH,
|
|
|
|
FREEPARAM
|
|
|
|
} EVENTTYPE;
|
2020-10-28 21:45:21 +08:00
|
|
|
|
2020-10-08 00:03:59 +08:00
|
|
|
|
2020-10-09 20:42:34 +08:00
|
|
|
|
|
|
|
struct clientparam logparam;
|
|
|
|
struct srvparam logsrv;
|
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
struct LOGGER;
|
|
|
|
|
|
|
|
void(*prelog)(struct clientparam * param) = NULL;
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
|
|
|
|
struct logevent {
|
|
|
|
struct logevent *next;
|
|
|
|
struct LOGGER *log;
|
|
|
|
EVENTTYPE event;
|
|
|
|
int inbuf;
|
|
|
|
struct clientparam *param;
|
|
|
|
char * logstring;
|
|
|
|
char buf[1];
|
|
|
|
} *logtail=NULL, *loghead=NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void delayflushlogs(void);
|
|
|
|
static void delayunregisterlog (struct LOGGER * log);
|
|
|
|
static void delayregisterlog (struct LOGGER * log);
|
|
|
|
static void delaydolog(struct logevent *evt);
|
|
|
|
static void delayfreeparam(struct clientparam *param);
|
|
|
|
|
|
|
|
void logpush(struct logevent *evt);
|
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
#ifdef WITHMAIN
|
|
|
|
#define HAVERADIUS 0
|
|
|
|
#define HAVESQL 0
|
|
|
|
#else
|
2020-10-30 16:17:28 +08:00
|
|
|
int raddobuf(struct clientparam * param, unsigned char * buf, int bufsize, const unsigned char *s);
|
2020-10-14 21:10:35 +08:00
|
|
|
void logradius(const char * buf, int len, struct LOGGER *logger);
|
|
|
|
#define HAVERADIUS 1
|
|
|
|
|
|
|
|
#ifndef NOODBC
|
|
|
|
#define HAVESQL 1
|
2020-10-28 21:45:21 +08:00
|
|
|
static int sqlinit(struct LOGGER *logger);
|
2020-10-30 16:17:28 +08:00
|
|
|
static int sqldobuf(struct clientparam * param, unsigned char * buf, int bufsize, const unsigned char *s);
|
2020-10-14 21:10:35 +08:00
|
|
|
static void sqllog(const char * buf, int len, struct LOGGER *logger);
|
|
|
|
static void sqlrotate(struct LOGGER *logger);
|
|
|
|
static void sqlclose(struct LOGGER *logger);
|
2020-10-28 21:45:21 +08:00
|
|
|
#else
|
|
|
|
#define HAVESQL 0
|
2020-10-14 21:10:35 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define HAVESYSLOG 0
|
|
|
|
#else
|
|
|
|
#define HAVESYSLOG 1
|
2020-10-28 21:45:21 +08:00
|
|
|
static int sysloginit(struct LOGGER *logger);
|
2020-10-14 21:10:35 +08:00
|
|
|
static void logsyslog(const char * buf, int len, struct LOGGER *logger);
|
|
|
|
static void syslogrotate(struct LOGGER *logger);
|
|
|
|
static void syslogclose(struct LOGGER *logger);
|
|
|
|
#endif
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
static int stdloginit(struct LOGGER *logger);
|
2020-10-30 16:53:33 +08:00
|
|
|
static int stddobuf(struct clientparam * param, unsigned char * buf, int bufsize, const unsigned char *s);
|
2020-10-14 21:10:35 +08:00
|
|
|
static void stdlog(const char * buf, int len, struct LOGGER *logger);
|
|
|
|
static void stdlogrotate(struct LOGGER *logger);
|
|
|
|
static void stdlogclose(struct LOGGER *logger);
|
2020-10-30 16:17:28 +08:00
|
|
|
static void stdlogflush(struct LOGGER *logger);
|
2020-10-09 20:42:34 +08:00
|
|
|
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
struct LOGFUNC stdlogfuncs[] = {
|
2020-10-14 21:10:35 +08:00
|
|
|
#if HAVESYSLOG > 0
|
2020-10-30 16:17:28 +08:00
|
|
|
{stdlogfuncs+1, sysloginit, stddobuf, logsyslog, syslogrotate, NULL, syslogclose, "@"},
|
2020-10-14 21:10:35 +08:00
|
|
|
#endif
|
|
|
|
#if HAVERADIUS > 0
|
2020-10-30 16:17:28 +08:00
|
|
|
{stdlogfuncs+1+HAVESYSLOG, NULL, raddobuf, logradius, NULL, NULL, NULL, "radius"},
|
2020-10-14 21:10:35 +08:00
|
|
|
#endif
|
|
|
|
#if HAVESQL > 0
|
2020-10-30 16:17:28 +08:00
|
|
|
{stdlogfuncs+1+HAVESYSLOG+HAVERADIUS, sqlinit, sqldobuf, sqllog, sqlrotate, NULL, sqlclose, "&"},
|
2020-10-14 21:10:35 +08:00
|
|
|
#endif
|
2020-10-30 16:17:28 +08:00
|
|
|
{NULL, stdloginit, stddobuf, stdlog, stdlogrotate, stdlogflush, stdlogclose, ""}
|
2020-10-14 21:10:35 +08:00
|
|
|
};
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
struct LOGFUNC *logfuncs = stdlogfuncs;
|
2020-10-14 21:10:35 +08:00
|
|
|
|
|
|
|
struct stdlogdata{
|
|
|
|
FILE *fp;
|
2020-10-28 21:45:21 +08:00
|
|
|
} errld;
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
struct LOGGER errlogger = {NULL, NULL, "stderr", &errld, stdlogfuncs+1+HAVESYSLOG+HAVERADIUS+HAVESQL, 0, 0, 1};
|
2020-10-28 21:45:21 +08:00
|
|
|
|
|
|
|
struct LOGGER *loggers = &errlogger;
|
2020-10-14 21:10:35 +08:00
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void delayflushlogs(void){
|
2020-10-28 21:45:21 +08:00
|
|
|
struct LOGGER *log;
|
2020-10-30 16:17:28 +08:00
|
|
|
for(log = loggers; log; log=log->next){
|
|
|
|
if(log->logfunc && log->logfunc->flush)log->logfunc->flush(log);
|
2020-10-30 17:39:37 +08:00
|
|
|
#ifndef WITHMAIN
|
|
|
|
if(log->rotate && log->logfunc && log->logfunc->rotate && timechanged(log->rotated, conf.time, log->rotate)){
|
|
|
|
log->logfunc->rotate(log);
|
|
|
|
log->rotated = conf.time;
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-30 16:17:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void flushlogs(void){
|
|
|
|
struct logevent * evt;
|
|
|
|
evt = malloc(sizeof(struct logevent));
|
|
|
|
evt->event = FLUSH;
|
|
|
|
logpush(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void delayregisterlog(struct LOGGER *log){
|
2020-10-28 21:45:21 +08:00
|
|
|
struct LOGFUNC *funcs;
|
2020-10-14 21:10:35 +08:00
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
|
|
|
|
if(log->logfunc) return;
|
|
|
|
for(funcs = logfuncs; funcs; funcs=funcs->next){
|
|
|
|
if(!strncmp(log->selector, funcs->prefix, strlen(funcs->prefix))){
|
|
|
|
if(funcs->init && funcs->init(log)) break;
|
|
|
|
log->logfunc = funcs;
|
2020-10-30 17:39:37 +08:00
|
|
|
log->rotated = conf.time;
|
2020-10-30 16:17:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct LOGGER * registerlog(const char * logstring, int logtype){
|
|
|
|
struct LOGGER *log;
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
if(!logstring || !strcmp(logstring, "NUL") || !strcmp(logstring, "/dev/null")) return NULL;
|
2020-10-30 16:17:28 +08:00
|
|
|
pthread_mutex_lock(&log_mutex);
|
2020-10-28 21:45:21 +08:00
|
|
|
for(log = loggers; log; log=log->next){
|
|
|
|
if(!strcmp(logstring, log->selector)){
|
|
|
|
if(logtype >= 0) log->rotate = logtype;
|
|
|
|
log->registered++;
|
2020-10-30 16:17:28 +08:00
|
|
|
pthread_mutex_unlock(&log_mutex);
|
2020-10-28 21:45:21 +08:00
|
|
|
return log;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log = malloc(sizeof(struct LOGGER));
|
2020-10-30 16:17:28 +08:00
|
|
|
if(!log) {
|
|
|
|
pthread_mutex_unlock(&log_mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-28 21:45:21 +08:00
|
|
|
memset (log, 0, sizeof(struct LOGGER));
|
|
|
|
log->selector = mystrdup(logstring);
|
|
|
|
if(log->selector){
|
2020-10-30 16:17:28 +08:00
|
|
|
struct logevent *evt;
|
2020-10-28 21:45:21 +08:00
|
|
|
if(logtype)log->rotate = logtype;
|
2020-10-30 16:17:28 +08:00
|
|
|
log->registered++;
|
|
|
|
log->next = loggers;
|
|
|
|
if (log->next)log->next->prev = log;
|
|
|
|
loggers = log;
|
|
|
|
pthread_mutex_unlock(&log_mutex);
|
|
|
|
|
|
|
|
evt = malloc(sizeof(struct logevent));
|
|
|
|
evt->event = REGISTER;
|
|
|
|
evt->log = log;
|
|
|
|
logpush(evt);
|
|
|
|
return log;
|
2020-10-28 21:45:21 +08:00
|
|
|
}
|
2020-10-30 16:17:28 +08:00
|
|
|
pthread_mutex_unlock(&log_mutex);
|
2020-10-28 21:45:21 +08:00
|
|
|
myfree(log);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
static void delayunregisterlog (struct LOGGER * log){
|
|
|
|
if(log){
|
|
|
|
pthread_mutex_lock(&log_mutex);
|
|
|
|
log->registered--;
|
|
|
|
if(!log->registered){
|
|
|
|
if(log->prev)log->prev->next = log->next;
|
|
|
|
else loggers = log->next;
|
|
|
|
if(log->next)log->next->prev = log->prev;
|
|
|
|
pthread_mutex_unlock(&log_mutex);
|
|
|
|
|
|
|
|
if(log->logfunc){
|
|
|
|
if(log->logfunc->flush) log->logfunc->flush(log);
|
|
|
|
if(log->logfunc->close) log->logfunc->close(log);
|
|
|
|
}
|
|
|
|
myfree(log->selector);
|
|
|
|
myfree(log);
|
|
|
|
}
|
|
|
|
else pthread_mutex_unlock(&log_mutex);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
void unregisterlog (struct LOGGER * log){
|
2020-10-30 16:17:28 +08:00
|
|
|
struct logevent *evt;
|
|
|
|
|
|
|
|
if(!log) return;
|
|
|
|
evt = malloc(sizeof(struct logevent));
|
|
|
|
evt->event = UNREGISTER;
|
|
|
|
evt->log = log;
|
|
|
|
logpush(evt);
|
2020-10-28 21:45:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD WINAPI logthreadfunc(LPVOID p) {
|
|
|
|
#else
|
|
|
|
void * logthreadfunc (void *p) {
|
|
|
|
#endif
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
for(;;){
|
|
|
|
struct logevent *evt;
|
|
|
|
#ifdef _WIN32
|
|
|
|
WaitForSingleObject(log_sem, INFINITE);
|
|
|
|
#else
|
|
|
|
sem_wait(&log_sem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while(loghead){
|
|
|
|
pthread_mutex_lock(&log_mutex);
|
|
|
|
evt = loghead;
|
|
|
|
loghead = evt->next;
|
|
|
|
if(!loghead)logtail = NULL;
|
|
|
|
pthread_mutex_unlock(&log_mutex);
|
|
|
|
switch(evt->event){
|
|
|
|
case REGISTER:
|
|
|
|
delayregisterlog(evt->log);
|
|
|
|
break;
|
|
|
|
case UNREGISTER:
|
|
|
|
delayunregisterlog(evt->log);
|
|
|
|
break;
|
|
|
|
case FLUSH:
|
2020-10-30 16:58:40 +08:00
|
|
|
delayflushlogs();
|
2020-10-30 16:17:28 +08:00
|
|
|
break;
|
|
|
|
case LOG:
|
|
|
|
delaydolog(evt);
|
|
|
|
break;
|
|
|
|
case FREEPARAM:
|
|
|
|
delayfreeparam(evt->param);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
myfree(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void logpush(struct logevent *evt){
|
|
|
|
pthread_mutex_lock(&log_mutex);
|
|
|
|
if(logtail) logtail->next = evt;
|
|
|
|
logtail = evt;
|
|
|
|
evt->next = NULL;
|
|
|
|
if(!loghead)loghead = evt;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&log_mutex);
|
|
|
|
#ifdef _WIN32
|
|
|
|
ReleaseSemaphore(log_sem, 1, NULL);
|
|
|
|
#else
|
|
|
|
sem_post(&log_sem);
|
|
|
|
#endif
|
2020-10-28 21:45:21 +08:00
|
|
|
}
|
2020-10-14 21:10:35 +08:00
|
|
|
|
|
|
|
void initlog(void){
|
2020-10-28 21:45:21 +08:00
|
|
|
pthread_t thread;
|
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
srvinit(&logsrv, &logparam);
|
|
|
|
pthread_mutex_init(&log_mutex, NULL);
|
2020-10-28 21:45:21 +08:00
|
|
|
errld.fp = stdout;
|
|
|
|
#ifdef _WIN32
|
|
|
|
{
|
|
|
|
HANDLE h;
|
2020-10-30 16:17:28 +08:00
|
|
|
if(!(log_sem = CreateSemaphore(NULL, 0, MAX_SEM_COUNT, NULL))) exit(11);
|
2020-10-28 21:45:21 +08:00
|
|
|
#ifndef _WINCE
|
|
|
|
h = (HANDLE)_beginthreadex((LPSECURITY_ATTRIBUTES )NULL, 65536, (void *)logthreadfunc, NULL, 0, &thread);
|
|
|
|
#else
|
|
|
|
h = (HANDLE)CreateThread((LPSECURITY_ATTRIBUTES )NULL, 65536, (void *)logthreadfunc, NULL, 0, &thread);
|
|
|
|
#endif
|
|
|
|
if (h) {
|
|
|
|
CloseHandle(h);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
pthread_attr_t pa;
|
|
|
|
pthread_attr_init(&pa);
|
2020-10-30 16:17:28 +08:00
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
pthread_attr_setstacksize(&pa,PTHREAD_STACK_MIN + 1024*256);
|
|
|
|
pthread_attr_setdetachstate(&pa,PTHREAD_CREATE_DETACHED);
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
if(sem_init(&log_sem, 0, 0)) exit(11);
|
2020-10-30 16:53:33 +08:00
|
|
|
if(pthread_create(&thread, &pa, logthreadfunc, NULL)) exit(10);
|
2020-10-28 21:45:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
2020-10-30 16:17:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void delaydolog(struct logevent *evt){
|
|
|
|
|
|
|
|
if(!evt->log->logfunc || !evt->log->logfunc->log) return;
|
|
|
|
if(evt->inbuf){
|
|
|
|
evt->log->logfunc->log(evt->buf, evt->inbuf, evt->log);
|
|
|
|
}
|
|
|
|
else if(evt->param && evt->log->logfunc->dobuf){
|
|
|
|
char buf[LOGBUFSIZE];
|
|
|
|
|
|
|
|
evt->log->logfunc->log(buf, evt->log->logfunc->dobuf(evt->param, buf, LOGBUFSIZE, evt->logstring), evt->log);
|
|
|
|
}
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:42:34 +08:00
|
|
|
void dolog(struct clientparam * param, const unsigned char *s){
|
|
|
|
static int init = 0;
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
if(!param || !param->srv){
|
|
|
|
stdlog(s, strlen(s), &errlogger);
|
2020-10-30 16:17:28 +08:00
|
|
|
return;
|
2020-10-09 20:42:34 +08:00
|
|
|
}
|
2020-10-30 16:17:28 +08:00
|
|
|
if(prelog)prelog(param);
|
|
|
|
if(!param->nolog && param->srv->log) {
|
|
|
|
struct logevent *evt;
|
|
|
|
|
|
|
|
if(!param->srv->log->logfunc) {
|
2020-10-30 16:53:33 +08:00
|
|
|
int slen =0, hlen=0, ulen=0;
|
2020-10-30 16:17:28 +08:00
|
|
|
|
2020-10-30 16:53:33 +08:00
|
|
|
slen = s?strlen(s)+1 : 0;
|
|
|
|
hlen = param->hostname? strlen(param->hostname)+1 : 0;
|
|
|
|
ulen = param->username? strlen(param->username)+1 : 0;
|
|
|
|
if(!(evt = malloc(sizeof(struct logevent) + slen + ulen + hlen))) return;
|
2020-10-30 16:17:28 +08:00
|
|
|
evt->inbuf = 0;
|
|
|
|
evt->param=param;
|
|
|
|
evt->logstring = NULL;
|
2020-10-30 16:53:33 +08:00
|
|
|
if(slen){
|
|
|
|
memcpy(evt->buf,s, slen);
|
2020-10-30 16:17:28 +08:00
|
|
|
evt->logstring = evt->buf;
|
|
|
|
}
|
2020-10-30 16:53:33 +08:00
|
|
|
if(hlen){
|
|
|
|
memcpy(evt->buf+slen,param->hostname, hlen);
|
|
|
|
param->hostname = evt->buf + slen;
|
2020-10-30 16:17:28 +08:00
|
|
|
}
|
2020-10-30 16:53:33 +08:00
|
|
|
if(ulen){
|
|
|
|
memcpy(evt->buf+slen+hlen,param->username, ulen);
|
|
|
|
param->username = evt->buf + slen + hlen;
|
2020-10-30 16:17:28 +08:00
|
|
|
}
|
|
|
|
evt->event = LOG;
|
|
|
|
evt->log = param->srv->log;
|
|
|
|
logpush(evt);
|
|
|
|
}
|
|
|
|
else if (param->srv->log->logfunc->log){
|
|
|
|
|
|
|
|
if(!(evt = malloc(param->srv->log->logfunc->dobuf?EVENTSIZE:sizeof(struct logevent)))) return;
|
|
|
|
evt->inbuf = 0;
|
|
|
|
evt->param = NULL;
|
|
|
|
evt->logstring = NULL;
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
if(param->srv->log->logfunc->dobuf){
|
2020-10-30 16:17:28 +08:00
|
|
|
evt->inbuf = param->srv->log->logfunc->dobuf(param, evt->buf, LOGBUFSIZE, s);
|
2020-10-28 21:45:21 +08:00
|
|
|
}
|
2020-10-30 16:17:28 +08:00
|
|
|
evt->event = LOG;
|
|
|
|
evt->log = param->srv->log;
|
|
|
|
logpush(evt);
|
2020-10-28 21:45:21 +08:00
|
|
|
}
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
|
|
|
if(param->trafcountfunc)(*param->trafcountfunc)(param);
|
|
|
|
clearstat(param);
|
2020-10-09 20:42:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
static void delayfreeparam(struct clientparam * param) {
|
|
|
|
if(param->res == 2) return;
|
|
|
|
if(param->ctrlsocksrv != INVALID_SOCKET && param->ctrlsocksrv != param->remsock) {
|
|
|
|
so._shutdown(param->ctrlsocksrv, SHUT_RDWR);
|
|
|
|
so._closesocket(param->ctrlsocksrv);
|
|
|
|
}
|
|
|
|
if(param->ctrlsock != INVALID_SOCKET && param->ctrlsock != param->clisock) {
|
|
|
|
so._shutdown(param->ctrlsock, SHUT_RDWR);
|
|
|
|
so._closesocket(param->ctrlsock);
|
|
|
|
}
|
|
|
|
if(param->remsock != INVALID_SOCKET) {
|
|
|
|
so._shutdown(param->remsock, SHUT_RDWR);
|
|
|
|
so._closesocket(param->remsock);
|
|
|
|
}
|
|
|
|
if(param->clisock != INVALID_SOCKET) {
|
|
|
|
so._shutdown(param->clisock, SHUT_RDWR);
|
|
|
|
so._closesocket(param->clisock);
|
|
|
|
}
|
|
|
|
myfree(param->clibuf);
|
|
|
|
myfree(param->srvbuf);
|
|
|
|
if(param->datfilterssrv) myfree(param->datfilterssrv);
|
|
|
|
#ifndef STDMAIN
|
|
|
|
if(param->reqfilters) myfree(param->reqfilters);
|
|
|
|
if(param->hdrfilterscli) myfree(param->hdrfilterscli);
|
|
|
|
if(param->hdrfilterssrv) myfree(param->hdrfilterssrv);
|
|
|
|
if(param->predatfilters) myfree(param->predatfilters);
|
|
|
|
if(param->datfilterscli) myfree(param->datfilterscli);
|
|
|
|
if(param->filters){
|
|
|
|
if(param->nfilters)while(param->nfilters--){
|
|
|
|
if(param->filters[param->nfilters].filter->filter_clear)
|
|
|
|
(*param->filters[param->nfilters].filter->filter_clear)(param->filters[param->nfilters].data);
|
|
|
|
}
|
|
|
|
myfree(param->filters);
|
|
|
|
}
|
|
|
|
if(conf.connlimiter && (param->res != 95 || param->remsock != INVALID_SOCKET)) stopconnlims(param);
|
|
|
|
#endif
|
|
|
|
if(param->srv){
|
|
|
|
pthread_mutex_lock(¶m->srv->counter_mutex);
|
|
|
|
if(param->prev){
|
|
|
|
param->prev->next = param->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
param->srv->child = param->next;
|
|
|
|
if(param->next){
|
|
|
|
param->next->prev = param->prev;
|
|
|
|
}
|
|
|
|
(param->srv->childcount)--;
|
|
|
|
if(param->srv->service == S_ZOMBIE && !param->srv->child)srvpostfree(param->srv);
|
|
|
|
pthread_mutex_unlock(¶m->srv->counter_mutex);
|
|
|
|
}
|
|
|
|
if(param->hostname) myfree(param->hostname);
|
|
|
|
if(param->username) myfree(param->username);
|
|
|
|
if(param->password) myfree(param->password);
|
|
|
|
if(param->extusername) myfree(param->extusername);
|
|
|
|
if(param->extpassword) myfree(param->extpassword);
|
|
|
|
myfree(param);
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeparam(struct clientparam * param) {
|
|
|
|
struct logevent *evt;
|
|
|
|
|
|
|
|
evt = malloc(sizeof(struct logevent));
|
|
|
|
evt->event = FREEPARAM;
|
|
|
|
evt->param = param;
|
|
|
|
logpush(evt);
|
|
|
|
}
|
|
|
|
|
2020-10-08 00:03:59 +08:00
|
|
|
void clearstat(struct clientparam * param) {
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
struct timeb tb;
|
|
|
|
|
|
|
|
ftime(&tb);
|
|
|
|
param->time_start = (time_t)tb.time;
|
|
|
|
param->msec_start = (unsigned)tb.millitm;
|
|
|
|
|
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
struct timezone tz;
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
|
|
|
|
|
param->time_start = (time_t)tv.tv_sec;
|
|
|
|
param->msec_start = (tv.tv_usec / 1000);
|
|
|
|
#endif
|
|
|
|
param->statscli64 = param->statssrv64 = param->nreads = param->nwrites =
|
|
|
|
param->nconnects = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char months[12][4] = {
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
};
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
unsigned char * dologname (unsigned char *buf, int bufsize, unsigned char *name, const unsigned char *ext, ROTATION lt, time_t t) {
|
2020-10-28 21:45:21 +08:00
|
|
|
struct tm *ts;
|
|
|
|
|
|
|
|
ts = localtime(&t);
|
|
|
|
if(strchr((char *)name, '%')){
|
2020-10-30 17:39:37 +08:00
|
|
|
dobuf2(&logparam, buf, bufsize - (ext?strlen(ext):0), NULL, NULL, ts, (char *)name);
|
2020-10-28 21:45:21 +08:00
|
|
|
}
|
|
|
|
else switch(lt){
|
|
|
|
case NONE:
|
|
|
|
sprintf((char *)buf, "%s", name);
|
|
|
|
break;
|
|
|
|
case ANNUALLY:
|
|
|
|
sprintf((char *)buf, "%s.%04d", name, ts->tm_year+1900);
|
|
|
|
break;
|
|
|
|
case MONTHLY:
|
|
|
|
sprintf((char *)buf, "%s.%04d.%02d", name, ts->tm_year+1900, ts->tm_mon+1);
|
|
|
|
break;
|
|
|
|
case WEEKLY:
|
|
|
|
t = t - (ts->tm_wday * (60*60*24));
|
|
|
|
ts = localtime(&t);
|
|
|
|
sprintf((char *)buf, "%s.%04d.%02d.%02d", name, ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday);
|
|
|
|
break;
|
|
|
|
case DAILY:
|
|
|
|
sprintf((char *)buf, "%s.%04d.%02d.%02d", name, ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday);
|
|
|
|
break;
|
|
|
|
case HOURLY:
|
|
|
|
sprintf((char *)buf, "%s.%04d.%02d.%02d-%02d", name, ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday, ts->tm_hour);
|
|
|
|
break;
|
|
|
|
case MINUTELY:
|
|
|
|
sprintf((char *)buf, "%s.%04d.%02d.%02d-%02d.%02d", name, ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday, ts->tm_hour, ts->tm_min);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(ext){
|
|
|
|
strcat((char *)buf, ".");
|
|
|
|
strcat((char *)buf, (char *)ext);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
2020-10-08 00:03:59 +08:00
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
int dobuf2(struct clientparam * param, unsigned char * buf, int bufsize, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format){
|
2020-10-08 00:03:59 +08:00
|
|
|
int i, j;
|
|
|
|
int len;
|
|
|
|
time_t sec;
|
|
|
|
unsigned msec;
|
|
|
|
|
|
|
|
long timezone;
|
|
|
|
unsigned delay;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
struct timeb tb;
|
|
|
|
|
|
|
|
ftime(&tb);
|
|
|
|
sec = (time_t)tb.time;
|
|
|
|
msec = (unsigned)tb.millitm;
|
|
|
|
timezone = tm->tm_isdst*60 - tb.timezone;
|
|
|
|
|
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
struct timezone tz;
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
|
|
|
|
|
sec = (time_t)tv.tv_sec;
|
|
|
|
msec = tv.tv_usec / 1000;
|
|
|
|
#ifdef _SOLARIS
|
|
|
|
timezone = -altzone / 60;
|
|
|
|
#else
|
|
|
|
timezone = tm->tm_gmtoff / 60;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
delay = param->time_start?((unsigned) ((sec - param->time_start))*1000 + msec) - param->msec_start : 0;
|
|
|
|
*buf = 0;
|
2020-10-30 16:17:28 +08:00
|
|
|
for(i=0, j=0; format[j] && i < (bufsize-70); j++){
|
2020-10-08 00:03:59 +08:00
|
|
|
if(format[j] == '%' && format[j+1]){
|
|
|
|
j++;
|
|
|
|
switch(format[j]){
|
|
|
|
case '%':
|
|
|
|
buf[i++] = '%';
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
sprintf((char *)buf+i, "%.2d", tm->tm_year%100);
|
|
|
|
i+=2;
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
sprintf((char *)buf+i, "%.4d", tm->tm_year+1900);
|
|
|
|
i+=4;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
sprintf((char *)buf+i, "%.2d", tm->tm_mon+1);
|
|
|
|
i+=2;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
sprintf((char *)buf+i, "%s", months[tm->tm_mon]);
|
|
|
|
i+=3;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
sprintf((char *)buf+i, "%.2d", tm->tm_mday);
|
|
|
|
i+=2;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
sprintf((char *)buf+i, "%.2d", tm->tm_hour);
|
|
|
|
i+=2;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
sprintf((char *)buf+i, "%.2d", tm->tm_min);
|
|
|
|
i+=2;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
sprintf((char *)buf+i, "%.2d", tm->tm_sec);
|
|
|
|
i+=2;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
sprintf((char *)buf+i, "%.10u", (unsigned)sec);
|
|
|
|
i+=10;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
i+=sprintf((char *)buf+i, "%u", delay?(unsigned)(param->statscli64 * 1000./delay):0);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
i+=sprintf((char *)buf+i, "%u", delay?(unsigned)(param->statssrv64 * 1000./delay):0);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
i+=sprintf((char *)buf+i, "%u", delay);
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
sprintf((char *)buf+i, "%.3u", msec);
|
|
|
|
i+=3;
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
sprintf((char *)buf+i, "%+.2ld%.2u", timezone / 60, (unsigned)(timezone%60));
|
|
|
|
i+=5;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
if(param->username && *param->username){
|
2020-10-30 16:17:28 +08:00
|
|
|
for(len = 0; i< (bufsize - 3) && param->username[len]; len++){
|
2020-10-08 00:03:59 +08:00
|
|
|
buf[i] = param->username[len];
|
|
|
|
if(param->srv->nonprintable && (buf[i] < 0x20 || strchr((char *)param->srv->nonprintable, buf[i]))) buf[i] = param->srv->replace;
|
|
|
|
if(doublec && strchr((char *)doublec, buf[i])) {
|
|
|
|
buf[i+1] = buf[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf[i++] = '-';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
len = param->hostname? (int)strlen((char *)param->hostname) : 0;
|
2020-10-30 16:17:28 +08:00
|
|
|
if (len > 0 && !strchr((char *)param->hostname, ':')) for(len = 0; param->hostname[len] && i < (bufsize-3); len++, i++){
|
2020-10-08 00:03:59 +08:00
|
|
|
buf[i] = param->hostname[len];
|
|
|
|
if(param->srv->nonprintable && (buf[i] < 0x20 || strchr((char *)param->srv->nonprintable, buf[i]))) buf[i] = param->srv->replace;
|
|
|
|
if(doublec && strchr((char *)doublec, buf[i])) {
|
|
|
|
buf[i+1] = buf[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf[i++] = '[';
|
|
|
|
i += myinet_ntop(*SAFAMILY(¶m->req), SAADDR(¶m->req), (char *)buf + i, 64);
|
|
|
|
buf[i++] = ']';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N':
|
|
|
|
if(param->service < 15) {
|
|
|
|
len = (conf.stringtable)? (int)strlen((char *)conf.stringtable[SERVICES + param->service]) : 0;
|
|
|
|
if(len > 20) len = 20;
|
|
|
|
memcpy(buf+i, (len)?conf.stringtable[SERVICES + param->service]:(unsigned char*)"-", (len)?len:1);
|
|
|
|
i += (len)?len:1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
sprintf((char *)buf+i, "%.05d", param->res);
|
|
|
|
i += 5;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
if(s){
|
2020-10-30 16:17:28 +08:00
|
|
|
for(len = 0; i < (bufsize-3) && s[len]; len++){
|
2020-10-08 00:03:59 +08:00
|
|
|
buf[i] = s[len];
|
|
|
|
if(param->srv->nonprintable && (buf[i] < 0x20 || strchr((char *)param->srv->nonprintable, buf[i]))) buf[i] = param->srv->replace;
|
|
|
|
if(doublec && strchr((char *)doublec, buf[i])) {
|
|
|
|
buf[i+1] = buf[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
i += myinet_ntop(*SAFAMILY(¶m->sinsl), SAADDR(¶m->sinsl), (char *)buf + i, 64);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
i += myinet_ntop(*SAFAMILY(¶m->sincl), SAADDR(¶m->sincl), (char *)buf + i, 64);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
i += myinet_ntop(*SAFAMILY(¶m->sincr), SAADDR(¶m->sincr), (char *)buf + i, 64);
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
i += myinet_ntop(*SAFAMILY(¶m->sinsr), SAADDR(¶m->sinsr), (char *)buf + i, 64);
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
i += myinet_ntop(*SAFAMILY(¶m->req), SAADDR(¶m->req), (char *)buf + i, 64);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
sprintf((char *)buf+i, "%hu", ntohs(*SAPORT(¶m->srv->intsa)));
|
|
|
|
i += (int)strlen((char *)buf+i);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
sprintf((char *)buf+i, "%hu", ntohs(*SAPORT(¶m->sincr)));
|
|
|
|
i += (int)strlen((char *)buf+i);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
sprintf((char *)buf+i, "%hu", ntohs(*SAPORT(¶m->sinsr)));
|
|
|
|
i += (int)strlen((char *)buf+i);
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
sprintf((char *)buf+i, "%hu", ntohs(*SAPORT(¶m->req)));
|
|
|
|
i += (int)strlen((char *)buf+i);
|
|
|
|
break;
|
|
|
|
case 'L':
|
2020-10-28 21:45:21 +08:00
|
|
|
sprintf((char *)buf+i, "%"PRIu64, param->cycles);
|
2020-10-08 00:03:59 +08:00
|
|
|
i += (int)strlen((char *)buf+i);
|
|
|
|
break;
|
|
|
|
case 'I':
|
2020-10-28 21:45:21 +08:00
|
|
|
sprintf((char *)buf+i, "%"PRIu64, param->statssrv64);
|
2020-10-08 00:03:59 +08:00
|
|
|
i += (int)strlen((char *)buf+i);
|
|
|
|
break;
|
|
|
|
case 'O':
|
2020-10-28 21:45:21 +08:00
|
|
|
sprintf((char *)buf+i, "%"PRIu64, param->statscli64);
|
2020-10-08 00:03:59 +08:00
|
|
|
i += (int)strlen((char *)buf+i);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
sprintf((char *)buf+i, "%d", param->redirected);
|
|
|
|
i += (int)strlen((char *)buf+i);
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
{
|
|
|
|
int k, pmin=0, pmax=0;
|
|
|
|
for (k = j; isnumber(format[k]); k++);
|
|
|
|
if(format[k] == '-' && isnumber(format[k+1])){
|
|
|
|
pmin = atoi(format + j) - 1;
|
|
|
|
k++;
|
|
|
|
pmax = atoi(format + k) -1;
|
|
|
|
for (; isnumber(format[k]); k++);
|
|
|
|
j = k;
|
|
|
|
}
|
|
|
|
if(!s || format[k]!='T') break;
|
2020-10-28 21:45:21 +08:00
|
|
|
for(k = 0, len = 0; s[len]; len++){
|
2020-10-08 00:03:59 +08:00
|
|
|
if(isspace(s[len])){
|
|
|
|
k++;
|
|
|
|
while(isspace(s[len+1]))len++;
|
|
|
|
if(k == pmin) continue;
|
|
|
|
}
|
2020-10-30 16:17:28 +08:00
|
|
|
if(k>=pmin && k<=pmax && i < (bufsize-3)) {
|
2020-10-08 00:03:59 +08:00
|
|
|
buf[i] = s[len];
|
|
|
|
if(param->srv->nonprintable && (buf[i] < 0x20 || strchr((char *)param->srv->nonprintable, buf[i]))) buf[i] = param->srv->replace;
|
|
|
|
if(doublec && strchr((char *)doublec, buf[i])) {
|
|
|
|
buf[i+1] = buf[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
buf[i++] = format[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else buf[i++] = format[j];
|
|
|
|
}
|
|
|
|
buf[i] = 0;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
int dobuf(struct clientparam * param, unsigned char * buf, int bufsize, const unsigned char *s, const unsigned char * doublec){
|
2020-10-08 00:03:59 +08:00
|
|
|
struct tm* tm;
|
|
|
|
int i;
|
|
|
|
char * format;
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
if(!param) return 0;
|
|
|
|
format = param->srv->logformat?(char *)param->srv->logformat : DEFLOGFORMAT;
|
|
|
|
tm = (*format == 'G' || *format == 'g')?
|
|
|
|
gmtime(&t) : localtime(&t);
|
2020-10-30 16:17:28 +08:00
|
|
|
i = dobuf2(param, buf, bufsize, s, doublec, tm, format + 1);
|
2020-10-08 00:03:59 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
static int stdloginit(struct LOGGER *logger){
|
|
|
|
char tmpbuf[1024];
|
2020-10-14 21:10:35 +08:00
|
|
|
struct stdlogdata *lp;
|
2020-10-30 16:17:28 +08:00
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
lp = myalloc(sizeof(struct stdlogdata));
|
|
|
|
if(!lp) return 1;
|
|
|
|
logger->data = lp;
|
2020-10-30 16:17:28 +08:00
|
|
|
if(!*logger->selector || !strcmp(logger->selector, "stdout")){
|
2020-10-28 21:45:21 +08:00
|
|
|
logger->rotate = NONE;
|
2020-10-14 21:10:35 +08:00
|
|
|
lp->fp = stdout;
|
|
|
|
}
|
2020-10-28 21:45:21 +08:00
|
|
|
else if(!strcmp(logger->selector,"stderr")){
|
|
|
|
logger->rotate = NONE;
|
|
|
|
lp->fp = stderr;
|
|
|
|
}
|
2020-10-14 21:10:35 +08:00
|
|
|
else {
|
2020-10-30 16:17:28 +08:00
|
|
|
lp->fp = fopen((char *)dologname (tmpbuf, sizeof(tmpbuf) - 1, logger->selector, NULL, logger->rotate, time(NULL)), "a");
|
2020-10-14 21:10:35 +08:00
|
|
|
if(!lp->fp){
|
|
|
|
myfree(lp);
|
|
|
|
return(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
static int stddobuf(struct clientparam * param, unsigned char * buf, int bufsize, const unsigned char *s){
|
|
|
|
return dobuf(param, buf, bufsize, s, NULL);
|
2020-10-08 00:03:59 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
static void stdlog(const char * buf, int len, struct LOGGER *logger) {
|
|
|
|
FILE *log = ((struct stdlogdata *)logger->data)->fp;
|
2020-10-08 00:03:59 +08:00
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
fprintf(log, "%s\n", buf);
|
2020-10-08 00:03:59 +08:00
|
|
|
}
|
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
static void stdlogrotate(struct LOGGER *logger){
|
2020-10-28 21:45:21 +08:00
|
|
|
char tmpbuf[1024];
|
|
|
|
struct stdlogdata *lp = (struct stdlogdata *)logger->data;
|
2020-10-30 16:17:28 +08:00
|
|
|
if(lp->fp) lp->fp = freopen((char *)dologname (tmpbuf, sizeof(tmpbuf) -1, logger->selector, NULL, logger->rotate, conf.time), "a", lp->fp);
|
|
|
|
else lp->fp = fopen((char *)dologname (tmpbuf, sizeof(tmpbuf) -1, logger->selector, NULL, logger->rotate, conf.time), "a");
|
2020-10-28 21:45:21 +08:00
|
|
|
logger->rotated = conf.time;
|
2020-10-14 21:10:35 +08:00
|
|
|
if(logger->rotate) {
|
|
|
|
int t;
|
|
|
|
t = 1;
|
|
|
|
switch(logger->rotate){
|
|
|
|
case ANNUALLY:
|
|
|
|
t = t * 12;
|
|
|
|
case MONTHLY:
|
|
|
|
t = t * 4;
|
|
|
|
case WEEKLY:
|
|
|
|
t = t * 7;
|
|
|
|
case DAILY:
|
|
|
|
t = t * 24;
|
|
|
|
case HOURLY:
|
|
|
|
t = t * 60;
|
|
|
|
case MINUTELY:
|
|
|
|
t = t * 60;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-10-30 16:17:28 +08:00
|
|
|
/*
|
|
|
|
FIXME: move archiver to thread
|
|
|
|
*/
|
|
|
|
dologname (tmpbuf, sizeof(tmpbuf) -1, logger->selector, (conf.archiver)?conf.archiver[1]:NULL, logger->rotate, (logger->rotated - t * conf.rotate));
|
2020-10-14 21:10:35 +08:00
|
|
|
remove ((char *) tmpbuf);
|
|
|
|
if(conf.archiver) {
|
|
|
|
int i;
|
|
|
|
*tmpbuf = 0;
|
|
|
|
for(i = 2; i < conf.archiverc && strlen((char *)tmpbuf) < 512; i++){
|
|
|
|
strcat((char *)tmpbuf, " ");
|
|
|
|
if(!strcmp((char *)conf.archiver[i], "%A")){
|
|
|
|
strcat((char *)tmpbuf, "\"");
|
2020-10-30 16:17:28 +08:00
|
|
|
dologname (tmpbuf + strlen((char *)tmpbuf), sizeof(tmpbuf) - (strlen((char *)tmpbuf) + 1), logger->selector, conf.archiver[1], logger->rotate, (logger->rotated - t));
|
2020-10-14 21:10:35 +08:00
|
|
|
strcat((char *)tmpbuf, "\"");
|
|
|
|
}
|
|
|
|
else if(!strcmp((char *)conf.archiver[i], "%F")){
|
|
|
|
strcat((char *)tmpbuf, "\"");
|
2020-10-30 16:17:28 +08:00
|
|
|
dologname (tmpbuf+strlen((char *)tmpbuf), sizeof(tmpbuf) - (strlen((char *)tmpbuf) + 1), logger->selector, NULL, logger->rotate, (logger->rotated-t));
|
2020-10-14 21:10:35 +08:00
|
|
|
strcat((char *)tmpbuf, "\"");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcat((char *)tmpbuf, (char *)conf.archiver[i]);
|
|
|
|
}
|
|
|
|
system((char *)tmpbuf+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
static void stdlogflush(struct LOGGER *logger){
|
|
|
|
fflush(((struct stdlogdata *)logger->data)->fp);
|
|
|
|
}
|
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
static void stdlogclose(struct LOGGER *logger){
|
2020-10-30 16:17:28 +08:00
|
|
|
if(((struct stdlogdata *)logger->data)->fp != stdout && ((struct stdlogdata *)logger->data)->fp != stderr)
|
|
|
|
fclose(((struct stdlogdata *)logger->data)->fp);
|
|
|
|
myfree(logger->data);
|
2020-10-08 00:03:59 +08:00
|
|
|
}
|
2020-10-14 21:10:35 +08:00
|
|
|
|
|
|
|
#if HAVESYSLOG > 0
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
static int sysloginit(struct LOGGER *logger){
|
|
|
|
openlog(logger->selector, LOG_PID, LOG_DAEMON);
|
|
|
|
return 0;
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void logsyslog(const char * buf, int len, struct LOGGER *logger) {
|
2020-10-30 16:53:33 +08:00
|
|
|
syslog(LOG_INFO, "%s", buf);
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void syslogrotate(struct LOGGER *logger){
|
|
|
|
closelog();
|
|
|
|
openlog(logger->selector+1, LOG_PID, LOG_DAEMON);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void syslogclose(struct LOGGER *logger){
|
|
|
|
closelog();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-08 00:03:59 +08:00
|
|
|
#endif
|
|
|
|
|
2020-10-14 21:10:35 +08:00
|
|
|
#if HAVESQL > 0
|
|
|
|
|
|
|
|
struct sqldata {
|
|
|
|
SQLHENV henv;
|
|
|
|
SQLHSTMT hstmt;
|
|
|
|
SQLHDBC hdbc;
|
|
|
|
int attempt;
|
|
|
|
time_t attempt_time;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
static int sqlinit2(struct sqldata * sd, char * source){
|
2020-10-14 21:10:35 +08:00
|
|
|
SQLRETURN retcode;
|
|
|
|
char * datasource;
|
|
|
|
char * username;
|
|
|
|
char * password;
|
|
|
|
char * string;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sd->henv);
|
2020-10-28 21:45:21 +08:00
|
|
|
if (!sd->henv || (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)){
|
2020-10-14 21:10:35 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
retcode = SQLSetEnvAttr(sd->henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
|
|
|
|
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
|
|
|
|
ret = 2;
|
2020-10-28 21:45:21 +08:00
|
|
|
goto CLOSEENV;
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
2020-10-28 21:45:21 +08:00
|
|
|
retcode = SQLAllocHandle(SQL_HANDLE_DBC, sd->henv, &sd->hdbc);
|
2020-10-14 21:10:35 +08:00
|
|
|
if (!sd->hdbc || (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)) {
|
|
|
|
ret = 3;
|
2020-10-28 21:45:21 +08:00
|
|
|
goto CLOSEENV;
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
|
|
|
SQLSetConnectAttr(sd->hdbc, SQL_LOGIN_TIMEOUT, (void*)15, 0);
|
|
|
|
|
|
|
|
string = mystrdup(source);
|
|
|
|
if(!string) goto CLOSEHDBC;
|
|
|
|
datasource = strtok(string, ",");
|
|
|
|
username = strtok(NULL, ",");
|
|
|
|
password = strtok(NULL, ",");
|
|
|
|
|
|
|
|
|
|
|
|
/* Connect to data source */
|
|
|
|
retcode = SQLConnect(sd->hdbc, (SQLCHAR*) datasource, (SQLSMALLINT)strlen(datasource),
|
|
|
|
(SQLCHAR*) username, (SQLSMALLINT)((username)?strlen(username):0),
|
|
|
|
(SQLCHAR*) password, (SQLSMALLINT)((password)?strlen(password):0));
|
|
|
|
|
|
|
|
myfree(string);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO){
|
|
|
|
ret = 4;
|
|
|
|
goto CLOSEHDBC;
|
|
|
|
}
|
|
|
|
|
|
|
|
retcode = SQLAllocHandle(SQL_HANDLE_STMT, sd->hdbc, &sd->hstmt);
|
|
|
|
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO){
|
|
|
|
sd->hstmt = 0;
|
|
|
|
ret = 5;
|
|
|
|
goto CLOSEHDBC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
CLOSEHDBC:
|
2020-10-28 21:45:21 +08:00
|
|
|
SQLFreeHandle(SQL_HANDLE_DBC, sd->hdbc);
|
2020-10-14 21:10:35 +08:00
|
|
|
sd->hdbc = 0;
|
|
|
|
CLOSEENV:
|
2020-10-28 21:45:21 +08:00
|
|
|
SQLFreeHandle(SQL_HANDLE_ENV, sd->henv);
|
2020-10-14 21:10:35 +08:00
|
|
|
sd->henv = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-28 21:45:21 +08:00
|
|
|
static int sqlinit(struct LOGGER *logger){
|
2020-10-14 21:10:35 +08:00
|
|
|
struct sqldata *sd;
|
2020-10-28 21:45:21 +08:00
|
|
|
int res;
|
2020-10-14 21:10:35 +08:00
|
|
|
|
|
|
|
sd = (struct sqldata *)myalloc(sizeof(struct sqldata));
|
|
|
|
memset(sd, 0, sizeof(struct sqldata));
|
2020-10-28 21:45:21 +08:00
|
|
|
logger->data = sd;
|
|
|
|
if((res = sqlinit2(sd, logger->selector))) {
|
2020-10-14 21:10:35 +08:00
|
|
|
myfree(sd);
|
|
|
|
return res;
|
|
|
|
}
|
2020-10-28 21:45:21 +08:00
|
|
|
return 0;
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
|
|
|
|
2020-10-30 16:17:28 +08:00
|
|
|
static int sqldobuf(struct clientparam * param, unsigned char * buf, int bufsize, const unsigned char *s){
|
|
|
|
return dobuf(param, buf, bufsize, s, (unsigned char *)"\'");
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void sqllog(const char * buf, int len, struct LOGGER *logger){
|
|
|
|
SQLRETURN ret;
|
|
|
|
struct sqldata *sd = (struct sqldata *)logger->data;
|
|
|
|
|
|
|
|
|
|
|
|
if(sd->attempt > 5){
|
|
|
|
if (conf.time - sd->attempt_time < 180){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(sd->attempt){
|
|
|
|
sd->attempt++;
|
|
|
|
sqlrotate(logger);
|
|
|
|
|
|
|
|
if(!sd->hstmt){
|
|
|
|
sd->attempt_time=conf.time;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = SQLExecDirect(sd->hstmt, (SQLCHAR *)buf, (SQLINTEGER)len);
|
|
|
|
if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){
|
|
|
|
sqlrotate(logger);
|
|
|
|
if(sd->hstmt) {
|
2020-10-28 21:45:21 +08:00
|
|
|
ret = SQLExecDirect(sd->hstmt, (SQLCHAR *)buf, (SQLINTEGER)len);
|
2020-10-14 21:10:35 +08:00
|
|
|
if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){
|
|
|
|
sd->attempt++;
|
|
|
|
sd->attempt_time=conf.time;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sd->attempt=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sqlrotate(struct LOGGER *logger){
|
|
|
|
struct sqldata * sd;
|
|
|
|
sqlclose(logger);
|
|
|
|
sd = (struct sqldata *)myalloc(sizeof(struct sqldata));
|
|
|
|
memset(sd, 0, sizeof(struct sqldata));
|
2020-10-28 21:45:21 +08:00
|
|
|
logger->data = sd;
|
|
|
|
sqlinit2(sd, logger->selector+1);
|
2020-10-14 21:10:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sqlclose(struct LOGGER *logger){
|
2020-10-28 21:45:21 +08:00
|
|
|
struct sqldata *sd = (struct sqldata *)logger->data;
|
2020-10-14 21:10:35 +08:00
|
|
|
if(sd->hstmt) {
|
|
|
|
SQLFreeHandle(SQL_HANDLE_STMT, sd->hstmt);
|
|
|
|
sd->hstmt = NULL;
|
|
|
|
}
|
|
|
|
if(sd->hdbc){
|
|
|
|
SQLDisconnect(sd->hdbc);
|
|
|
|
SQLFreeHandle(SQL_HANDLE_DBC, sd->hdbc);
|
|
|
|
sd->hdbc = NULL;
|
|
|
|
}
|
|
|
|
if(sd->henv) {
|
|
|
|
SQLFreeHandle(SQL_HANDLE_ENV, sd->henv);
|
|
|
|
sd->henv = NULL;
|
|
|
|
}
|
|
|
|
myfree(sd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|