Cross Reference: /dovecot/src/pop3/main.c
main.c revision d4e141553b783ed96d0709ff52a6a41c0384206e
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* Copyright (C) 2002-2003 Timo Sirainen */
#include "common.h"
#include "ioloop.h"
#include "file-lock.h"
#include "network.h"
#include "lib-signals.h"
#include "restrict-access.h"
#include "fd-close-on-exec.h"
#include "process-title.h"
#include "randgen.h"
#include "module-dir.h"
#include "var-expand.h"
#include "dict-client.h"
#include "mail-storage.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#define IS_STANDALONE() \
(getenv("LOGGED_IN") == NULL)
struct client_workaround_list {
const char *name;
enum client_workarounds num;
};
struct client_workaround_list client_workaround_list[] = {
{ "outlook-no-nuls", WORKAROUND_OUTLOOK_NO_NULS },
{ "oe-ns-eoh", WORKAROUND_OE_NS_EOH },
{ NULL, 0 }
};
struct ioloop *ioloop;
void (*hook_client_created)(struct client **client) = NULL;
static struct module *modules = NULL;
static char log_prefix[128]; /* syslog() needs this to be permanent */
static struct io *log_io = NULL;
enum client_workarounds client_workarounds = 0;
bool enable_last_command = FALSE;
bool no_flag_updates = FALSE;
bool reuse_xuidl = FALSE;
bool lock_session = FALSE;
const char *uidl_format, *logout_format;
enum uidl_keys uidl_keymask;
static void sig_die(int signo, void *context __attr_unused__)
{
/* warn about being killed because of some signal, except SIGINT (^C)
which is too common at least while testing :) */
if (signo != SIGINT)
i_warning("Killed with signal %d", signo);
io_loop_stop(ioloop);
}
static void log_error_callback(void *context __attr_unused__)
{
io_loop_stop(ioloop);
}
static void parse_workarounds(void)
{
struct client_workaround_list *list;
const char *env, *const *str;
env = getenv("POP3_CLIENT_WORKAROUNDS");
if (env == NULL)
return;
for (str = t_strsplit_spaces(env, " ,"); *str != NULL; str++) {
list = client_workaround_list;
for (; list->name != NULL; list++) {
if (strcasecmp(*str, list->name) == 0) {
client_workarounds |= list->num;
break;
}
}
if (list->name == NULL)
i_fatal("Unknown client workaround: %s", *str);
}
}
static enum uidl_keys parse_uidl_keymask(const char *format)
{
enum uidl_keys mask = 0;
for (; *format != '\0'; format++) {
if (format[0] == '%' && format[1] != '\0') {
switch (var_get_key(++format)) {
case 'v':
mask |= UIDL_UIDVALIDITY;
break;
case 'u':
mask |= UIDL_UID;
break;
case 'm':
mask |= UIDL_MD5;
break;
case 'f':
mask |= UIDL_FILE_NAME;
break;
}
}
}
return mask;
}
static void open_logfile(void)
{
const char *user;
if (getenv("LOG_TO_MASTER") != NULL) {
i_set_failure_internal();
return;
}
if (getenv("LOG_PREFIX") != NULL)
strocpy(log_prefix, getenv("LOG_PREFIX"), sizeof(log_prefix));
else {
user = getenv("USER");
if (user == NULL) user = "??";
if (strlen(user) >= sizeof(log_prefix)-6) {
/* quite a long user name, cut it */
user = t_strndup(user, sizeof(log_prefix)-6-2);
user = t_strconcat(user, "..", NULL);
}
i_snprintf(log_prefix, sizeof(log_prefix), "pop3(%s): ", user);
}
if (getenv("USE_SYSLOG") != NULL) {
const char *env = getenv("SYSLOG_FACILITY");
i_set_failure_syslog(log_prefix, LOG_NDELAY,
env == NULL ? LOG_MAIL : atoi(env));
} else {
/* log to file or stderr */
i_set_failure_file(getenv("LOGFILE"), log_prefix);
}
if (getenv("INFOLOGFILE") != NULL)
i_set_info_file(getenv("INFOLOGFILE"));
i_set_failure_timestamp_format(getenv("LOGSTAMP"));
}
static void drop_privileges(void)
{
const char *version;
version = getenv("DOVECOT_VERSION");
if (version != NULL && strcmp(version, PACKAGE_VERSION) != 0) {
i_fatal("Dovecot version mismatch: "
"Master is v%s, pop3 is v"PACKAGE_VERSION" "
"(if you don't care, set version_ignore=yes)", version);
}
/* Log file or syslog opening probably requires roots */
open_logfile();
/* Most likely needed. Have to open /dev/urandom before possible
chrooting. */
random_init();
/* Load the plugins before chrooting. Their init() is called later. */
if (getenv("MAIL_PLUGINS") != NULL) {
const char *plugin_dir = getenv("MAIL_PLUGIN_DIR");
if (plugin_dir == NULL)
plugin_dir = MODULEDIR"/pop3";
modules = module_dir_load(plugin_dir, getenv("MAIL_PLUGINS"),
TRUE, version);
}
restrict_access_by_env(!IS_STANDALONE());
}
static int main_init(void)
{
enum mail_storage_flags flags;
enum file_lock_method lock_method;
struct mail_storage *storage;
const char *mail;
lib_signals_init();
lib_signals_set_handler(SIGINT, TRUE, sig_die, NULL);
lib_signals_set_handler(SIGTERM, TRUE, sig_die, NULL);
lib_signals_ignore(SIGPIPE, TRUE);
lib_signals_ignore(SIGALRM, FALSE);
if (getenv("USER") == NULL)
i_fatal("USER environment missing");
if (getenv("DEBUG") != NULL) {
i_info("Effective uid=%s, gid=%s",
dec2str(geteuid()), dec2str(getegid()));
}
if (getenv("STDERR_CLOSE_SHUTDOWN") != NULL) {
/* If master dies, the log fd gets closed and we'll quit */
log_io = io_add(STDERR_FILENO, IO_ERROR,
log_error_callback, NULL);
}
dict_driver_register(&dict_driver_client);
mail_storage_init();
mail_storage_register_all();
mailbox_list_register_all();
clients_init();
module_dir_init(modules);
mail = getenv("MAIL");
if (mail == NULL) {
/* support also maildir-specific environment */
mail = getenv("MAILDIR");
if (mail != NULL)
mail = t_strconcat("maildir:", mail, NULL);
}
parse_workarounds();
enable_last_command = getenv("POP3_ENABLE_LAST") != NULL;
no_flag_updates = getenv("POP3_NO_FLAG_UPDATES") != NULL;
reuse_xuidl = getenv("POP3_REUSE_XUIDL") != NULL;
lock_session = getenv("POP3_LOCK_SESSION") != NULL;
uidl_format = getenv("POP3_UIDL_FORMAT");
if (uidl_format == NULL || *uidl_format == '\0')
i_fatal("Set pop3_uidl_format in config file");
logout_format = getenv("POP3_LOGOUT_FORMAT");
if (logout_format == NULL)
logout_format = "top=%t/%p, retr=%r/%b, del=%d/%m, size=%s";
uidl_keymask = parse_uidl_keymask(uidl_format);
if (uidl_keymask == 0)
i_fatal("pop3_uidl_format setting doesn't contain any "
"%% variables.");
mail_storage_parse_env(&flags, &lock_method);
storage = mail_storage_create_with_data(mail, getenv("USER"),
flags, lock_method);
if (storage == NULL) {
/* failed */
if (mail != NULL && *mail != '\0')
i_fatal("Failed to create storage with data: %s", mail);
else {
const char *home;
home = getenv("HOME");
if (home == NULL) home = "not set";
i_fatal("MAIL environment missing and "
"autodetection failed (home %s)", home);
}
}
return client_create(0, 1, storage) != NULL;
}
static void main_deinit(void)
{
if (log_io != NULL)
io_remove(&log_io);
clients_deinit();
module_dir_unload(&modules);
mail_storage_deinit();
dict_driver_unregister(&dict_driver_client);
random_deinit();
lib_signals_deinit();
closelog();
}
int main(int argc __attr_unused__, char *argv[], char *envp[])
{
#ifdef DEBUG
if (getenv("LOGGED_IN") != NULL && getenv("GDB") == NULL)
fd_debug_verify_leaks(3, 1024);
#endif
if (IS_STANDALONE() && getuid() == 0 &&
net_getpeername(1, NULL, NULL) == 0) {
printf("-ERR pop3 binary must not be started from "
"inetd, use pop3-login instead.\n");
return 1;
}
/* NOTE: we start rooted, so keep the code minimal until
restrict_access_by_env() is called */
lib_init();
drop_privileges();
process_title_init(argv, envp);
ioloop = io_loop_create();
if (main_init())
io_loop_run(ioloop);
main_deinit();
io_loop_destroy(&ioloop);
lib_deinit();
return 0;
}