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