1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
lefcha |
1.35 |
#include <stdlib.h> |
3 |
lefcha |
1.34 |
#include <string.h> |
4 |
lefcha |
1.1 |
#include <errno.h> |
5 |
lefcha |
1.35 |
#include <stdarg.h> |
6 |
lefcha |
1.34 |
#include <limits.h> |
7 |
lefcha |
1.38 |
#include <sys/types.h> |
8 |
lefcha |
1.9 |
#include <sys/stat.h> |
9 |
lefcha |
1.12 |
#include <time.h> |
10 |
lefcha |
1.38 |
#include <signal.h> |
11 |
lefcha |
1.1 |
|
12 |
|
|
#include "config.h" |
13 |
|
|
#include "imapfilter.h" |
14 |
lefcha |
1.40 |
#include "pathnames.h" |
15 |
lefcha |
1.1 |
|
16 |
|
|
|
17 |
lefcha |
1.43 |
extern connection_t connpri, connaux; |
18 |
|
|
extern options_t opts; |
19 |
lefcha |
1.32 |
extern unsigned int flags; |
20 |
lefcha |
1.6 |
|
21 |
lefcha |
1.40 |
static FILE *debugfp = NULL; /* Pointer to debug file. */ |
22 |
lefcha |
1.10 |
static FILE *logfp = NULL; /* Pointer to logfile. */ |
23 |
lefcha |
1.34 |
|
24 |
|
|
|
25 |
|
|
void signal_handler(int sig); |
26 |
lefcha |
1.44 |
char *log_time(void); |
27 |
lefcha |
1.1 |
|
28 |
|
|
|
29 |
|
|
/* |
30 |
lefcha |
1.41 |
* Print message if not in the lowest verbosity level. |
31 |
lefcha |
1.7 |
*/ |
32 |
lefcha |
1.30 |
void |
33 |
|
|
info(const char *info,...) |
34 |
lefcha |
1.7 |
{ |
35 |
lefcha |
1.30 |
va_list args; |
36 |
lefcha |
1.14 |
|
37 |
lefcha |
1.42 |
if (opts.verbosity == -2) |
38 |
lefcha |
1.30 |
return; |
39 |
lefcha |
1.14 |
|
40 |
lefcha |
1.30 |
va_start(args, info); |
41 |
|
|
vprintf(info, args); |
42 |
|
|
va_end(args); |
43 |
lefcha |
1.12 |
} |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
/* |
47 |
lefcha |
1.41 |
* Print message if in the highest verbosity level. |
48 |
lefcha |
1.1 |
*/ |
49 |
lefcha |
1.30 |
void |
50 |
|
|
verbose(const char *info,...) |
51 |
lefcha |
1.1 |
{ |
52 |
lefcha |
1.30 |
va_list args; |
53 |
lefcha |
1.1 |
|
54 |
lefcha |
1.42 |
if (opts.verbosity == 2) { |
55 |
lefcha |
1.30 |
va_start(args, info); |
56 |
|
|
vprintf(info, args); |
57 |
|
|
va_end(args); |
58 |
|
|
} |
59 |
lefcha |
1.1 |
} |
60 |
|
|
|
61 |
lefcha |
1.17 |
|
62 |
lefcha |
1.1 |
/* |
63 |
lefcha |
1.40 |
* Write message to debug file. |
64 |
|
|
*/ |
65 |
|
|
void |
66 |
|
|
debug(const char *debug,...) |
67 |
|
|
{ |
68 |
|
|
va_list args; |
69 |
|
|
|
70 |
lefcha |
1.42 |
if (opts.debug && debugfp) { |
71 |
lefcha |
1.40 |
va_start(args, debug); |
72 |
|
|
vfprintf(debugfp, debug, args); |
73 |
|
|
fflush(debugfp); |
74 |
|
|
va_end(args); |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
/* |
80 |
lefcha |
1.10 |
* Print error message and write it into logfile. |
81 |
lefcha |
1.1 |
*/ |
82 |
lefcha |
1.30 |
void |
83 |
|
|
error(const char *errmsg,...) |
84 |
lefcha |
1.1 |
{ |
85 |
lefcha |
1.30 |
va_list args; |
86 |
lefcha |
1.1 |
|
87 |
lefcha |
1.30 |
va_start(args, errmsg); |
88 |
lefcha |
1.1 |
|
89 |
lefcha |
1.30 |
fprintf(stderr, "imapfilter: "); |
90 |
|
|
vfprintf(stderr, errmsg, args); |
91 |
lefcha |
1.14 |
|
92 |
lefcha |
1.42 |
if (opts.errors && logfp) { |
93 |
lefcha |
1.30 |
vfprintf(logfp, errmsg, args); |
94 |
|
|
fflush(logfp); |
95 |
|
|
} |
96 |
|
|
va_end(args); |
97 |
lefcha |
1.1 |
} |
98 |
|
|
|
99 |
|
|
|
100 |
|
|
/* |
101 |
lefcha |
1.12 |
* Print error message and exit program. |
102 |
|
|
*/ |
103 |
lefcha |
1.30 |
void |
104 |
|
|
fatal(unsigned int errnum, const char *fatal,...) |
105 |
lefcha |
1.12 |
{ |
106 |
lefcha |
1.30 |
va_list args; |
107 |
|
|
|
108 |
|
|
va_start(args, fatal); |
109 |
|
|
fprintf(stderr, "imapfilter: "); |
110 |
|
|
vfprintf(stderr, fatal, args); |
111 |
|
|
va_end(args); |
112 |
lefcha |
1.14 |
|
113 |
lefcha |
1.33 |
close_connection(&connpri); |
114 |
|
|
close_connection(&connaux); |
115 |
lefcha |
1.30 |
secmem_clear(); |
116 |
|
|
tty_restore(); |
117 |
lefcha |
1.40 |
log_stop(); |
118 |
lefcha |
1.30 |
lockfile_remove(); |
119 |
lefcha |
1.40 |
debug_stop(); |
120 |
lefcha |
1.14 |
|
121 |
lefcha |
1.30 |
exit(errnum); |
122 |
lefcha |
1.12 |
} |
123 |
lefcha |
1.14 |
|
124 |
lefcha |
1.18 |
|
125 |
|
|
/* |
126 |
lefcha |
1.40 |
* Catch signals that cause rereading of the configuration file or program's |
127 |
|
|
* termination. |
128 |
lefcha |
1.18 |
*/ |
129 |
lefcha |
1.30 |
void |
130 |
|
|
catch_signals(void) |
131 |
lefcha |
1.18 |
{ |
132 |
lefcha |
1.45 |
|
133 |
lefcha |
1.36 |
signal(SIGUSR1, signal_handler); |
134 |
lefcha |
1.30 |
signal(SIGINT, signal_handler); |
135 |
|
|
signal(SIGQUIT, signal_handler); |
136 |
|
|
signal(SIGTERM, signal_handler); |
137 |
lefcha |
1.18 |
} |
138 |
|
|
|
139 |
|
|
|
140 |
|
|
/* |
141 |
lefcha |
1.40 |
* Signal handler for signals that cause rereading of the configuration file or |
142 |
|
|
* termination of program. |
143 |
lefcha |
1.18 |
*/ |
144 |
lefcha |
1.30 |
void |
145 |
|
|
signal_handler(int sig) |
146 |
lefcha |
1.18 |
{ |
147 |
lefcha |
1.45 |
|
148 |
lefcha |
1.39 |
if (sig == SIGUSR1) |
149 |
lefcha |
1.43 |
flags |= FLAG_SIGUSR1; |
150 |
lefcha |
1.39 |
else |
151 |
lefcha |
1.32 |
fatal(ERROR_SIGNAL, "killed by signal %d\n", sig); |
152 |
lefcha |
1.18 |
} |
153 |
lefcha |
1.12 |
|
154 |
|
|
|
155 |
|
|
/* |
156 |
lefcha |
1.40 |
* Open temporary debug file and associate a stream with the returned file |
157 |
|
|
* descriptor. |
158 |
|
|
*/ |
159 |
|
|
int |
160 |
|
|
debug_start(void) |
161 |
|
|
{ |
162 |
lefcha |
1.43 |
static char dt[] = PATHNAME_DEBUG; |
163 |
lefcha |
1.40 |
int fd; |
164 |
|
|
|
165 |
lefcha |
1.42 |
if (!opts.debug) |
166 |
lefcha |
1.40 |
return 0; |
167 |
|
|
|
168 |
|
|
fd = mkstemp(dt); |
169 |
|
|
|
170 |
|
|
if (fd != -1) { |
171 |
|
|
debugfp = fdopen(fd, "w"); |
172 |
|
|
if (debugfp == NULL) { |
173 |
lefcha |
1.42 |
error("opening debug file %s: %s\n", opts.logfile, |
174 |
lefcha |
1.40 |
strerror(errno)); |
175 |
|
|
return ERROR_TRIVIAL; |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
return 0; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
/* |
183 |
|
|
* Close temporary debug file. |
184 |
|
|
*/ |
185 |
|
|
int |
186 |
|
|
debug_stop(void) |
187 |
|
|
{ |
188 |
lefcha |
1.45 |
|
189 |
lefcha |
1.40 |
if (debugfp == NULL) |
190 |
|
|
return 0; |
191 |
|
|
else |
192 |
|
|
return fclose(debugfp); |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
|
196 |
|
|
/* |
197 |
lefcha |
1.17 |
* Open the file for saving of logging information. |
198 |
lefcha |
1.1 |
*/ |
199 |
lefcha |
1.30 |
int |
200 |
lefcha |
1.40 |
log_start(void) |
201 |
lefcha |
1.1 |
{ |
202 |
lefcha |
1.45 |
|
203 |
lefcha |
1.42 |
if (*opts.logfile == '\0') |
204 |
lefcha |
1.30 |
return 0; /* Logging not enabled. */ |
205 |
lefcha |
1.4 |
|
206 |
lefcha |
1.42 |
debug("log file: '%s'\n", opts.logfile); |
207 |
lefcha |
1.1 |
|
208 |
lefcha |
1.42 |
if (create_file(opts.logfile, S_IRUSR | S_IWUSR)) |
209 |
lefcha |
1.30 |
return ERROR_TRIVIAL; |
210 |
lefcha |
1.9 |
|
211 |
lefcha |
1.42 |
logfp = fopen(opts.logfile, "a"); |
212 |
lefcha |
1.30 |
if (logfp == NULL) { |
213 |
lefcha |
1.42 |
error("opening log file %s: %s\n", opts.logfile, |
214 |
|
|
strerror(errno)); |
215 |
lefcha |
1.30 |
return ERROR_TRIVIAL; |
216 |
|
|
} |
217 |
|
|
return 0; |
218 |
lefcha |
1.1 |
} |
219 |
|
|
|
220 |
|
|
|
221 |
|
|
/* |
222 |
lefcha |
1.40 |
* Close the log file. |
223 |
lefcha |
1.1 |
*/ |
224 |
lefcha |
1.30 |
int |
225 |
lefcha |
1.40 |
log_stop(void) |
226 |
lefcha |
1.1 |
{ |
227 |
lefcha |
1.45 |
|
228 |
lefcha |
1.30 |
if (logfp == NULL) |
229 |
|
|
return 0; |
230 |
|
|
else |
231 |
|
|
return fclose(logfp); |
232 |
lefcha |
1.1 |
} |
233 |
|
|
|
234 |
lefcha |
1.13 |
|
235 |
|
|
/* |
236 |
lefcha |
1.46 |
* Prepares the log entry to be saved through continues calls, and writes it to |
237 |
|
|
* logfile. |
238 |
lefcha |
1.13 |
*/ |
239 |
lefcha |
1.30 |
void |
240 |
|
|
log_info(int flag, void *ptr) |
241 |
lefcha |
1.13 |
{ |
242 |
lefcha |
1.30 |
static struct { |
243 |
|
|
char *account; |
244 |
|
|
char *mbox; |
245 |
|
|
char *filter; |
246 |
|
|
unsigned int *action; |
247 |
|
|
char *destaccount; |
248 |
|
|
char *destmbox; |
249 |
|
|
char *hdrs; |
250 |
|
|
} inf = { |
251 |
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL |
252 |
|
|
}; |
253 |
|
|
|
254 |
|
|
if (logfp == NULL) |
255 |
|
|
return; |
256 |
|
|
|
257 |
|
|
switch (flag) { |
258 |
lefcha |
1.31 |
case LOG_PREAMBLE: |
259 |
lefcha |
1.44 |
fprintf(logfp, "%s %s %s %s %s%s %s\n", log_time(), |
260 |
lefcha |
1.30 |
inf.account, inf.mbox, inf.filter, |
261 |
lefcha |
1.43 |
(*inf.action == ACTION_DELETE ? "delete" : |
262 |
lefcha |
1.46 |
*inf.action == ACTION_COPY ? "copy" : |
263 |
|
|
*inf.action == ACTION_MOVE ? "move" : |
264 |
|
|
*inf.action == ACTION_RCOPY ? "rcopy " : |
265 |
|
|
*inf.action == ACTION_RMOVE ? "rmove " : |
266 |
|
|
*inf.action == ACTION_FLAG_ADD || |
267 |
|
|
*inf.action == ACTION_FLAG_REMOVE || |
268 |
|
|
*inf.action == ACTION_FLAG_REPLACE ? "flag" : |
269 |
|
|
*inf.action == ACTION_LIST ? "list" : |
270 |
|
|
"unknown "), |
271 |
lefcha |
1.30 |
(inf.destaccount == NULL ? "" : inf.destaccount), |
272 |
|
|
(*inf.destmbox == '\0' ? "" : inf.destmbox)); |
273 |
|
|
fflush(logfp); |
274 |
|
|
break; |
275 |
|
|
case LOG_ACCOUNT: |
276 |
|
|
inf.account = (char *)ptr; |
277 |
|
|
break; |
278 |
lefcha |
1.43 |
case LOG_MBOX: |
279 |
lefcha |
1.30 |
inf.mbox = (char *)ptr; |
280 |
|
|
break; |
281 |
|
|
case LOG_FILTER: |
282 |
|
|
inf.filter = (char *)ptr; |
283 |
|
|
break; |
284 |
|
|
case LOG_ACTION: |
285 |
|
|
inf.action = (unsigned int *)ptr; |
286 |
|
|
break; |
287 |
lefcha |
1.43 |
case LOG_DEST_ACCOUNT: |
288 |
lefcha |
1.30 |
if (ptr == NULL) |
289 |
|
|
inf.destaccount = NULL; |
290 |
|
|
else |
291 |
|
|
inf.destaccount = ((account_t *) ptr)->key; |
292 |
|
|
break; |
293 |
lefcha |
1.43 |
case LOG_DEST_MBOX: |
294 |
lefcha |
1.30 |
inf.destmbox = (char *)ptr; |
295 |
lefcha |
1.31 |
break; |
296 |
|
|
case LOG_HEADER: |
297 |
|
|
if (ptr) { |
298 |
|
|
inf.hdrs = (char *)ptr; |
299 |
|
|
fputc('\t', logfp); |
300 |
|
|
while (*inf.hdrs != '\0') { |
301 |
|
|
if (*inf.hdrs == '\r') { |
302 |
|
|
fputc('\n', logfp); |
303 |
|
|
if (*(inf.hdrs + 2) != '\0') |
304 |
|
|
fputc('\t', logfp); |
305 |
|
|
inf.hdrs += 2; |
306 |
|
|
} else |
307 |
|
|
fputc(*(inf.hdrs++), logfp); |
308 |
|
|
} |
309 |
|
|
} |
310 |
|
|
fflush(logfp); |
311 |
lefcha |
1.30 |
break; |
312 |
lefcha |
1.13 |
} |
313 |
|
|
} |
314 |
lefcha |
1.14 |
|
315 |
lefcha |
1.1 |
|
316 |
lefcha |
1.2 |
/* |
317 |
lefcha |
1.12 |
* Return current local time and date. |
318 |
lefcha |
1.2 |
*/ |
319 |
lefcha |
1.30 |
char * |
320 |
lefcha |
1.44 |
log_time(void) |
321 |
lefcha |
1.1 |
{ |
322 |
lefcha |
1.30 |
char *ct; |
323 |
|
|
time_t t; |
324 |
lefcha |
1.14 |
|
325 |
lefcha |
1.30 |
t = time(NULL); |
326 |
lefcha |
1.14 |
|
327 |
lefcha |
1.30 |
ct = ctime(&t); |
328 |
|
|
*(strchr(ct, '\n')) = '\0'; |
329 |
lefcha |
1.1 |
|
330 |
lefcha |
1.30 |
return ct; |
331 |
lefcha |
1.1 |
} |