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.5 |
extern char logfile[PATH_MAX]; |
19 |
lefcha |
1.8 |
extern unsigned int options; |
20 |
lefcha |
1.6 |
|
21 |
lefcha |
1.10 |
static FILE *logfp = NULL; /* Pointer to logfile. */ |
22 |
lefcha |
1.1 |
|
23 |
|
|
|
24 |
|
|
/* |
25 |
lefcha |
1.17 |
* Print message if not in OPTION_DETAILS_QUIET mode. |
26 |
lefcha |
1.7 |
*/ |
27 |
lefcha |
1.14 |
void info(const char *info,...) |
28 |
lefcha |
1.7 |
{ |
29 |
|
|
va_list args; |
30 |
lefcha |
1.14 |
|
31 |
lefcha |
1.16 |
if (options & OPTION_DETAILS_QUIET) |
32 |
lefcha |
1.13 |
return; |
33 |
lefcha |
1.14 |
|
34 |
lefcha |
1.12 |
va_start(args, info); |
35 |
lefcha |
1.13 |
vprintf(info, args); |
36 |
lefcha |
1.12 |
va_end(args); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
/* |
41 |
lefcha |
1.10 |
* Print message if in OPTION_DETAILS_VERBOSE mode. |
42 |
lefcha |
1.1 |
*/ |
43 |
lefcha |
1.14 |
void verbose(const char *info,...) |
44 |
lefcha |
1.1 |
{ |
45 |
|
|
va_list args; |
46 |
|
|
|
47 |
lefcha |
1.10 |
if (options & OPTION_DETAILS_VERBOSE) { |
48 |
lefcha |
1.1 |
va_start(args, info); |
49 |
|
|
vprintf(info, args); |
50 |
|
|
va_end(args); |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
|
54 |
lefcha |
1.17 |
|
55 |
lefcha |
1.1 |
/* |
56 |
lefcha |
1.10 |
* Print error message and write it into logfile. |
57 |
lefcha |
1.1 |
*/ |
58 |
lefcha |
1.14 |
void error(const char *errmsg,...) |
59 |
lefcha |
1.1 |
{ |
60 |
|
|
va_list args; |
61 |
|
|
|
62 |
|
|
va_start(args, errmsg); |
63 |
|
|
|
64 |
lefcha |
1.14 |
vfprintf(stderr, errmsg, args); |
65 |
|
|
|
66 |
lefcha |
1.8 |
if (logfp) { |
67 |
lefcha |
1.1 |
vfprintf(logfp, errmsg, args); |
68 |
|
|
} |
69 |
|
|
va_end(args); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
|
73 |
|
|
/* |
74 |
lefcha |
1.12 |
* Print error message and exit program. |
75 |
|
|
*/ |
76 |
lefcha |
1.14 |
void fatal(unsigned int errnum, const char *fatal,...) |
77 |
lefcha |
1.12 |
{ |
78 |
|
|
va_list args; |
79 |
lefcha |
1.14 |
|
80 |
lefcha |
1.12 |
va_start(args, fatal); |
81 |
|
|
vfprintf(stderr, fatal, args); |
82 |
|
|
va_end(args); |
83 |
lefcha |
1.14 |
|
84 |
lefcha |
1.12 |
close_connection(); |
85 |
lefcha |
1.22 |
secmem_clear(); |
86 |
lefcha |
1.21 |
tty_restore(); |
87 |
lefcha |
1.12 |
close_logfile(); |
88 |
lefcha |
1.14 |
|
89 |
lefcha |
1.12 |
exit(errnum); |
90 |
|
|
} |
91 |
lefcha |
1.14 |
|
92 |
lefcha |
1.18 |
|
93 |
|
|
/* |
94 |
|
|
* Catch signals that cause program's termination. |
95 |
|
|
*/ |
96 |
|
|
void catch_signals(void) |
97 |
|
|
{ |
98 |
|
|
signal(SIGINT, signal_handler); |
99 |
|
|
signal(SIGQUIT, signal_handler); |
100 |
|
|
signal(SIGTERM, signal_handler); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
|
104 |
|
|
/* |
105 |
|
|
* Signal handler for signals that cause termination of program. |
106 |
|
|
*/ |
107 |
|
|
void signal_handler(int sig) |
108 |
|
|
{ |
109 |
lefcha |
1.19 |
fatal(ERROR_SIGNAL, "imapfilter: killed by signal %d\n", sig); |
110 |
lefcha |
1.18 |
} |
111 |
lefcha |
1.12 |
|
112 |
|
|
|
113 |
|
|
/* |
114 |
lefcha |
1.17 |
* Open the file for saving of logging information. |
115 |
lefcha |
1.1 |
*/ |
116 |
|
|
int open_logfile(void) |
117 |
|
|
{ |
118 |
lefcha |
1.10 |
if (!*logfile) |
119 |
lefcha |
1.6 |
return 0; /* Logging not enabled. */ |
120 |
lefcha |
1.4 |
|
121 |
lefcha |
1.1 |
#ifdef DEBUG |
122 |
lefcha |
1.8 |
printf("debug: logfile: '%s'\n", logfile); |
123 |
lefcha |
1.1 |
#endif |
124 |
|
|
|
125 |
lefcha |
1.21 |
if (create_file(logfile, S_IRUSR | S_IWUSR)) |
126 |
lefcha |
1.12 |
return ERROR_TRIVIAL; |
127 |
lefcha |
1.9 |
|
128 |
lefcha |
1.5 |
logfp = fopen(logfile, "a"); |
129 |
lefcha |
1.1 |
|
130 |
|
|
if (!logfp) { |
131 |
lefcha |
1.12 |
error("imapfilter: opening logfile %s: %s\n", |
132 |
lefcha |
1.14 |
logfile, strerror(errno)); |
133 |
lefcha |
1.12 |
return ERROR_TRIVIAL; |
134 |
lefcha |
1.1 |
} |
135 |
lefcha |
1.6 |
return 0; |
136 |
lefcha |
1.1 |
} |
137 |
|
|
|
138 |
|
|
|
139 |
|
|
/* |
140 |
lefcha |
1.10 |
* Close the logfile. |
141 |
lefcha |
1.1 |
*/ |
142 |
|
|
int close_logfile(void) |
143 |
|
|
{ |
144 |
lefcha |
1.8 |
if (!logfp) |
145 |
lefcha |
1.6 |
return 0; |
146 |
lefcha |
1.1 |
else |
147 |
|
|
return fclose(logfp); |
148 |
|
|
} |
149 |
|
|
|
150 |
lefcha |
1.13 |
|
151 |
|
|
/* |
152 |
|
|
* Prepares the log entry to be saved through continues calls, and writes it |
153 |
|
|
* to logfile. |
154 |
|
|
*/ |
155 |
|
|
void log_info(int flag, void *ptr) |
156 |
|
|
{ |
157 |
|
|
static struct { |
158 |
|
|
char *server; |
159 |
|
|
char *username; |
160 |
|
|
char *mbox; |
161 |
|
|
char *filter; |
162 |
|
|
unsigned int *action; |
163 |
|
|
char *destmbox; |
164 |
|
|
char *hdrs; |
165 |
|
|
} inf = { |
166 |
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL |
167 |
|
|
}; |
168 |
lefcha |
1.14 |
|
169 |
lefcha |
1.13 |
if (!logfp) |
170 |
|
|
return; |
171 |
lefcha |
1.14 |
|
172 |
|
|
switch (flag) { |
173 |
lefcha |
1.13 |
case LOG_WRITE: |
174 |
|
|
fprintf(logfp, "%s %s %s %s %s %s%s\n", get_time(), |
175 |
|
|
inf.server, inf.username, inf.mbox, inf.filter, |
176 |
|
|
(*inf.action == FILTER_ACTION_DELETE ? "delete" : |
177 |
|
|
*inf.action == FILTER_ACTION_COPY ? "copy " : |
178 |
|
|
*inf.action == FILTER_ACTION_MOVE ? "move " : "list"), |
179 |
lefcha |
1.15 |
(!*inf.destmbox ? "" : inf.destmbox)); |
180 |
lefcha |
1.14 |
|
181 |
lefcha |
1.13 |
if (ptr) { |
182 |
|
|
inf.hdrs = (char *) ptr; |
183 |
|
|
fputc('\t', logfp); |
184 |
|
|
while (*inf.hdrs) { |
185 |
|
|
if (*inf.hdrs == '\n') { |
186 |
|
|
fputc('\n', logfp); |
187 |
|
|
if (*(inf.hdrs + 1)) |
188 |
|
|
fputc('\t', logfp); |
189 |
|
|
inf.hdrs++; |
190 |
|
|
} else |
191 |
|
|
fputc(*(inf.hdrs++), logfp); |
192 |
|
|
} |
193 |
|
|
} |
194 |
|
|
break; |
195 |
|
|
case LOG_SERVER: |
196 |
|
|
inf.server = (char *) ptr; |
197 |
|
|
break; |
198 |
|
|
case LOG_USERNAME: |
199 |
|
|
inf.username = (char *) ptr; |
200 |
lefcha |
1.14 |
break; |
201 |
lefcha |
1.13 |
case LOG_MAILBOX: |
202 |
|
|
inf.mbox = (char *) ptr; |
203 |
|
|
break; |
204 |
|
|
case LOG_FILTER: |
205 |
|
|
inf.filter = (char *) ptr; |
206 |
lefcha |
1.14 |
break; |
207 |
lefcha |
1.13 |
case LOG_ACTION: |
208 |
|
|
inf.action = (unsigned int *) ptr; |
209 |
|
|
break; |
210 |
|
|
case LOG_DESTINATION_MAILBOX: |
211 |
|
|
inf.destmbox = (char *) ptr; |
212 |
|
|
break; |
213 |
|
|
} |
214 |
|
|
} |
215 |
lefcha |
1.14 |
|
216 |
lefcha |
1.1 |
|
217 |
lefcha |
1.2 |
/* |
218 |
lefcha |
1.12 |
* Return current local time and date. |
219 |
lefcha |
1.2 |
*/ |
220 |
lefcha |
1.12 |
char *get_time(void) |
221 |
lefcha |
1.1 |
{ |
222 |
lefcha |
1.12 |
char *ct; |
223 |
|
|
time_t t; |
224 |
lefcha |
1.14 |
|
225 |
lefcha |
1.12 |
t = time(NULL); |
226 |
lefcha |
1.14 |
|
227 |
lefcha |
1.12 |
ct = ctime(&t); |
228 |
|
|
*(strchr(ct, '\n')) = 0; |
229 |
lefcha |
1.1 |
|
230 |
lefcha |
1.12 |
return ct; |
231 |
lefcha |
1.1 |
} |