forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlogger.c
119 lines (103 loc) · 3.32 KB
/
vlogger.c
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
/*
* Copyright (C) 2022 TLK Games
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "vlogger.h"
#include "config.h"
#include "vpopmail.h"
#include <stdio.h>
#include <string.h>
#ifdef ENABLE_LOGGER_SYSLOG
#include <syslog.h>
#endif
#ifdef ENABLE_LOGGER_SQLITE
#include <sqlite3.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#endif
#ifdef ENABLE_LOGGER_SQLITE
int createTableLog(sqlite3 *sqlite_log) {
char *err_msg = NULL;
int rc;
rc = sqlite3_exec(sqlite_log,
"create table log ("
"id integer PRIMARY KEY, "
"timestamp integer default 0 NOT NULL, "
"command text, arg1 text, arg2 text)",
0, 0, &err_msg);
if (rc != SQLITE_OK) {
fprintf(stderr, "error creating log table : %s\n", err_msg);
}
return rc;
}
#endif
/* log message to syslog or sqlite database */
int logmessage(const char *cmd, const char *arg1, const char *arg2) {
#ifdef ENABLE_LOGGER_SYSLOG
openlog(LOG_NAME, LOG_PID, LOG_USER);
syslog(LOG_NOTICE, "vpopmail-logger: %s %s %s", cmd, arg1, arg2);
closelog();
#endif
#ifdef ENABLE_LOGGER_SQLITE
char SqlBuf[2048];
char filedb[MAX_BUFF];
sqlite3 *sqlite_log = NULL;
mode_t oldmask;
int rc;
time_t mytime;
char *err_msg = NULL;
mytime = time(NULL);
snprintf(filedb, MAX_BUFF, "%s/%s", VPOPMAILDIR, "_vpopmail_log.sqlite");
if (access(filedb, F_OK) != 0) {
oldmask = umask(VPOPMAIL_UMASK);
rc = sqlite3_open_v2(filedb, &sqlite_log,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
umask(oldmask);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot create log database : %s\n",
sqlite3_errmsg(sqlite_log));
sqlite3_close(sqlite_log);
return 1;
}
sqlite3_close(sqlite_log);
chown(filedb, VPOPMAILUID, VPOPMAILGID);
}
rc = sqlite3_open_v2(filedb, &sqlite_log, SQLITE_OPEN_READWRITE, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database : %s\n", sqlite3_errmsg(sqlite_log));
sqlite3_close(sqlite_log);
return 1;
}
snprintf(SqlBuf, 2048,
"insert into log (timestamp, command, arg1, arg2) values "
"('%d', '%s', '%s', '%s')",
(int)mytime, cmd, arg1, arg2);
if (sqlite3_exec(sqlite_log, SqlBuf, 0, 0, &err_msg) != SQLITE_OK) {
if (createTableLog(sqlite_log) != SQLITE_OK) {
sqlite3_close(sqlite_log);
return 1;
}
if (sqlite3_exec(sqlite_log, SqlBuf, 0, 0, &err_msg) != SQLITE_OK) {
fprintf(stderr, "error inserting into log table : %s\n", err_msg);
sqlite3_close(sqlite_log);
return 1;
}
}
sqlite3_close(sqlite_log);
#endif
return 0;
}