1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
lefcha |
1.34 |
#include <string.h> |
3 |
lefcha |
1.1 |
#include <errno.h> |
4 |
lefcha |
1.34 |
#include <limits.h> |
5 |
lefcha |
1.18 |
#include <signal.h> |
6 |
lefcha |
1.9 |
#include <sys/types.h> |
7 |
|
|
#include <sys/stat.h> |
8 |
|
|
#include <fcntl.h> |
9 |
lefcha |
1.12 |
#include <time.h> |
10 |
lefcha |
1.1 |
|
11 |
|
|
#include "config.h" |
12 |
|
|
#include "imapfilter.h" |
13 |
|
|
|
14 |
|
|
|
15 |
lefcha |
1.33 |
extern conn_t connpri, connaux; |
16 |
lefcha |
1.5 |
extern char logfile[PATH_MAX]; |
17 |
lefcha |
1.8 |
extern unsigned int options; |
18 |
lefcha |
1.32 |
extern unsigned int flags; |
19 |
lefcha |
1.6 |
|
20 |
lefcha |
1.10 |
static FILE *logfp = NULL; /* Pointer to logfile. */ |
21 |
lefcha |
1.34 |
|
22 |
|
|
|
23 |
|
|
void signal_handler(int sig); |
24 |
|
|
char *get_time(void); |
25 |
lefcha |
1.1 |
|
26 |
|
|
|
27 |
|
|
/* |
28 |
lefcha |
1.17 |
* Print message if not in OPTION_DETAILS_QUIET mode. |
29 |
lefcha |
1.7 |
*/ |
30 |
lefcha |
1.30 |
void |
31 |
|
|
info(const char *info,...) |
32 |
lefcha |
1.7 |
{ |
33 |
lefcha |
1.30 |
va_list args; |
34 |
lefcha |
1.14 |
|
35 |
lefcha |
1.30 |
if (options & OPTION_DETAILS_QUIET) |
36 |
|
|
return; |
37 |
lefcha |
1.14 |
|
38 |
lefcha |
1.30 |
va_start(args, info); |
39 |
|
|
vprintf(info, args); |
40 |
|
|
va_end(args); |
41 |
lefcha |
1.12 |
} |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
/* |
45 |
lefcha |
1.10 |
* Print message if in OPTION_DETAILS_VERBOSE mode. |
46 |
lefcha |
1.1 |
*/ |
47 |
lefcha |
1.30 |
void |
48 |
|
|
verbose(const char *info,...) |
49 |
lefcha |
1.1 |
{ |
50 |
lefcha |
1.30 |
va_list args; |
51 |
lefcha |
1.1 |
|
52 |
lefcha |
1.30 |
if (options & OPTION_DETAILS_VERBOSE) { |
53 |
|
|
va_start(args, info); |
54 |
|
|
vprintf(info, args); |
55 |
|
|
va_end(args); |
56 |
|
|
} |
57 |
lefcha |
1.1 |
} |
58 |
|
|
|
59 |
lefcha |
1.17 |
|
60 |
lefcha |
1.1 |
/* |
61 |
lefcha |
1.10 |
* Print error message and write it into logfile. |
62 |
lefcha |
1.1 |
*/ |
63 |
lefcha |
1.30 |
void |
64 |
|
|
error(const char *errmsg,...) |
65 |
lefcha |
1.1 |
{ |
66 |
lefcha |
1.30 |
va_list args; |
67 |
lefcha |
1.1 |
|
68 |
lefcha |
1.30 |
va_start(args, errmsg); |
69 |
lefcha |
1.1 |
|
70 |
lefcha |
1.30 |
fprintf(stderr, "imapfilter: "); |
71 |
|
|
vfprintf(stderr, errmsg, args); |
72 |
lefcha |
1.14 |
|
73 |
lefcha |
1.30 |
if ((options & OPTION_ERRORS) && logfp) { |
74 |
|
|
vfprintf(logfp, errmsg, args); |
75 |
|
|
fflush(logfp); |
76 |
|
|
} |
77 |
|
|
va_end(args); |
78 |
lefcha |
1.1 |
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
/* |
82 |
lefcha |
1.12 |
* Print error message and exit program. |
83 |
|
|
*/ |
84 |
lefcha |
1.30 |
void |
85 |
|
|
fatal(unsigned int errnum, const char *fatal,...) |
86 |
lefcha |
1.12 |
{ |
87 |
lefcha |
1.30 |
va_list args; |
88 |
|
|
|
89 |
|
|
va_start(args, fatal); |
90 |
|
|
fprintf(stderr, "imapfilter: "); |
91 |
|
|
vfprintf(stderr, fatal, args); |
92 |
|
|
va_end(args); |
93 |
lefcha |
1.14 |
|
94 |
lefcha |
1.33 |
close_connection(&connpri); |
95 |
|
|
close_connection(&connaux); |
96 |
lefcha |
1.30 |
secmem_clear(); |
97 |
|
|
tty_restore(); |
98 |
|
|
close_logfile(); |
99 |
|
|
lockfile_remove(); |
100 |
lefcha |
1.14 |
|
101 |
lefcha |
1.30 |
exit(errnum); |
102 |
lefcha |
1.12 |
} |
103 |
lefcha |
1.14 |
|
104 |
lefcha |
1.18 |
|
105 |
|
|
/* |
106 |
|
|
* Catch signals that cause program's termination. |
107 |
|
|
*/ |
108 |
lefcha |
1.30 |
void |
109 |
|
|
catch_signals(void) |
110 |
lefcha |
1.18 |
{ |
111 |
lefcha |
1.32 |
signal(SIGHUP, signal_handler); |
112 |
lefcha |
1.30 |
signal(SIGINT, signal_handler); |
113 |
|
|
signal(SIGQUIT, signal_handler); |
114 |
|
|
signal(SIGTERM, signal_handler); |
115 |
lefcha |
1.18 |
} |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
/* |
119 |
|
|
* Signal handler for signals that cause termination of program. |
120 |
|
|
*/ |
121 |
lefcha |
1.30 |
void |
122 |
|
|
signal_handler(int sig) |
123 |
lefcha |
1.18 |
{ |
124 |
lefcha |
1.32 |
if (sig == SIGHUP) { |
125 |
|
|
flags |= FLAG_SIGHUP_RECEIVED; |
126 |
|
|
} else |
127 |
|
|
fatal(ERROR_SIGNAL, "killed by signal %d\n", sig); |
128 |
lefcha |
1.18 |
} |
129 |
lefcha |
1.12 |
|
130 |
|
|
|
131 |
|
|
/* |
132 |
lefcha |
1.17 |
* Open the file for saving of logging information. |
133 |
lefcha |
1.1 |
*/ |
134 |
lefcha |
1.30 |
int |
135 |
|
|
open_logfile(void) |
136 |
lefcha |
1.1 |
{ |
137 |
lefcha |
1.30 |
if (*logfile == '\0') |
138 |
|
|
return 0; /* Logging not enabled. */ |
139 |
lefcha |
1.4 |
|
140 |
lefcha |
1.1 |
#ifdef DEBUG |
141 |
lefcha |
1.30 |
fprintf(stderr, "debug: logfile: '%s'\n", logfile); |
142 |
lefcha |
1.1 |
#endif |
143 |
|
|
|
144 |
lefcha |
1.30 |
if (create_file(logfile, S_IRUSR | S_IWUSR)) |
145 |
|
|
return ERROR_TRIVIAL; |
146 |
lefcha |
1.9 |
|
147 |
lefcha |
1.30 |
logfp = fopen(logfile, "a"); |
148 |
|
|
if (logfp == NULL) { |
149 |
|
|
error("opening logfile %s: %s\n", logfile, strerror(errno)); |
150 |
|
|
return ERROR_TRIVIAL; |
151 |
|
|
} |
152 |
|
|
return 0; |
153 |
lefcha |
1.1 |
} |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
/* |
157 |
lefcha |
1.10 |
* Close the logfile. |
158 |
lefcha |
1.1 |
*/ |
159 |
lefcha |
1.30 |
int |
160 |
|
|
close_logfile(void) |
161 |
lefcha |
1.1 |
{ |
162 |
lefcha |
1.30 |
if (logfp == NULL) |
163 |
|
|
return 0; |
164 |
|
|
else |
165 |
|
|
return fclose(logfp); |
166 |
lefcha |
1.1 |
} |
167 |
|
|
|
168 |
lefcha |
1.13 |
|
169 |
|
|
/* |
170 |
|
|
* Prepares the log entry to be saved through continues calls, and writes it |
171 |
|
|
* to logfile. |
172 |
|
|
*/ |
173 |
lefcha |
1.30 |
void |
174 |
|
|
log_info(int flag, void *ptr) |
175 |
lefcha |
1.13 |
{ |
176 |
lefcha |
1.30 |
static struct { |
177 |
|
|
char *account; |
178 |
|
|
char *mbox; |
179 |
|
|
char *filter; |
180 |
|
|
unsigned int *action; |
181 |
|
|
char *destaccount; |
182 |
|
|
char *destmbox; |
183 |
|
|
char *hdrs; |
184 |
|
|
} inf = { |
185 |
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL |
186 |
|
|
}; |
187 |
|
|
|
188 |
|
|
if (logfp == NULL) |
189 |
|
|
return; |
190 |
|
|
|
191 |
|
|
switch (flag) { |
192 |
lefcha |
1.31 |
case LOG_PREAMBLE: |
193 |
lefcha |
1.30 |
fprintf(logfp, "%s %s %s %s %s%s %s\n", get_time(), |
194 |
|
|
inf.account, inf.mbox, inf.filter, |
195 |
|
|
(*inf.action == FILTER_ACTION_DELETE ? "delete" : |
196 |
|
|
*inf.action == FILTER_ACTION_COPY ? "copy" : |
197 |
|
|
*inf.action == FILTER_ACTION_MOVE ? "move" : |
198 |
|
|
*inf.action == FILTER_ACTION_RCOPY ? "rcopy " : |
199 |
|
|
*inf.action == FILTER_ACTION_RMOVE ? "rmove " : |
200 |
|
|
*inf.action == FILTER_ACTION_FLAG_ADD || |
201 |
|
|
*inf.action == FILTER_ACTION_FLAG_REMOVE || |
202 |
|
|
*inf.action == FILTER_ACTION_FLAG_REPLACE ? "flag" : |
203 |
|
|
*inf.action == FILTER_ACTION_LIST ? "list" : |
204 |
|
|
"unknown "), |
205 |
|
|
(inf.destaccount == NULL ? "" : inf.destaccount), |
206 |
|
|
(*inf.destmbox == '\0' ? "" : inf.destmbox)); |
207 |
|
|
fflush(logfp); |
208 |
|
|
break; |
209 |
|
|
case LOG_ACCOUNT: |
210 |
|
|
inf.account = (char *)ptr; |
211 |
|
|
break; |
212 |
|
|
case LOG_MAILBOX: |
213 |
|
|
inf.mbox = (char *)ptr; |
214 |
|
|
break; |
215 |
|
|
case LOG_FILTER: |
216 |
|
|
inf.filter = (char *)ptr; |
217 |
|
|
break; |
218 |
|
|
case LOG_ACTION: |
219 |
|
|
inf.action = (unsigned int *)ptr; |
220 |
|
|
break; |
221 |
|
|
case LOG_DESTINATION_ACCOUNT: |
222 |
|
|
if (ptr == NULL) |
223 |
|
|
inf.destaccount = NULL; |
224 |
|
|
else |
225 |
|
|
inf.destaccount = ((account_t *) ptr)->key; |
226 |
|
|
break; |
227 |
|
|
case LOG_DESTINATION_MAILBOX: |
228 |
|
|
inf.destmbox = (char *)ptr; |
229 |
lefcha |
1.31 |
break; |
230 |
|
|
case LOG_HEADER: |
231 |
|
|
if (ptr) { |
232 |
|
|
inf.hdrs = (char *)ptr; |
233 |
|
|
fputc('\t', logfp); |
234 |
|
|
while (*inf.hdrs != '\0') { |
235 |
|
|
if (*inf.hdrs == '\r') { |
236 |
|
|
fputc('\n', logfp); |
237 |
|
|
if (*(inf.hdrs + 2) != '\0') |
238 |
|
|
fputc('\t', logfp); |
239 |
|
|
inf.hdrs += 2; |
240 |
|
|
} else |
241 |
|
|
fputc(*(inf.hdrs++), logfp); |
242 |
|
|
} |
243 |
|
|
} |
244 |
|
|
fflush(logfp); |
245 |
lefcha |
1.30 |
break; |
246 |
lefcha |
1.13 |
} |
247 |
|
|
} |
248 |
lefcha |
1.14 |
|
249 |
lefcha |
1.1 |
|
250 |
lefcha |
1.2 |
/* |
251 |
lefcha |
1.12 |
* Return current local time and date. |
252 |
lefcha |
1.2 |
*/ |
253 |
lefcha |
1.30 |
char * |
254 |
|
|
get_time(void) |
255 |
lefcha |
1.1 |
{ |
256 |
lefcha |
1.30 |
char *ct; |
257 |
|
|
time_t t; |
258 |
lefcha |
1.14 |
|
259 |
lefcha |
1.30 |
t = time(NULL); |
260 |
lefcha |
1.14 |
|
261 |
lefcha |
1.30 |
ct = ctime(&t); |
262 |
|
|
*(strchr(ct, '\n')) = '\0'; |
263 |
lefcha |
1.1 |
|
264 |
lefcha |
1.30 |
return ct; |
265 |
lefcha |
1.1 |
} |