main.c revision 812883e7758a86f615b9508ef10df1339123da83
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#include "common.h"
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#include "ioloop.h"
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen#include "istream.h"
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen#include "buffer.h"
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen#include "base64.h"
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#include "restrict-access.h"
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#include "fd-close-on-exec.h"
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#include "process-title.h"
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen#include "master-service.h"
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen#include "var-expand.h"
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen#include "mail-storage-service.h"
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen#include <stdio.h>
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#include <stdlib.h>
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen#include <unistd.h>
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#define IS_STANDALONE() \
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen (getenv("LOGGED_IN") == NULL)
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainenstruct client_workaround_list {
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen const char *name;
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen enum client_workarounds num;
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen};
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen
ab0d9eecd85f74acae18fe88529302e0776cc500Timo Sirainenstatic struct client_workaround_list client_workaround_list[] = {
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen { "outlook-no-nuls", WORKAROUND_OUTLOOK_NO_NULS },
43a2293a8bddfbeed84b29193c599071c44d17c7Timo Sirainen { "oe-ns-eoh", WORKAROUND_OE_NS_EOH },
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen { NULL, 0 }
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen};
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainenstruct master_service *service;
8f1d14e3ada93a6d6ee64f73c6e6ae2364d8eba1Timo Sirainenvoid (*hook_client_created)(struct client **client) = NULL;
8f1d14e3ada93a6d6ee64f73c6e6ae2364d8eba1Timo Sirainen
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainenstatic struct io *log_io = NULL;
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainenstatic void log_error_callback(void *context ATTR_UNUSED)
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen{
2421fd34d51fed6da985c62b5e078d7e96640653Timo Sirainen /* the log fd is closed, don't die when trying to log later */
2421fd34d51fed6da985c62b5e078d7e96640653Timo Sirainen i_set_failure_ignore_errors(TRUE);
2421fd34d51fed6da985c62b5e078d7e96640653Timo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen master_service_stop(service);
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen}
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic enum client_workarounds
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenparse_workarounds(const struct pop3_settings *set)
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen{
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen enum client_workarounds client_workarounds = 0;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct client_workaround_list *list;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen const char *const *str;
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen str = t_strsplit_spaces(set->pop3_client_workarounds, " ,");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen for (; *str != NULL; str++) {
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen list = client_workaround_list;
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen for (; list->name != NULL; list++) {
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen if (strcasecmp(*str, list->name) == 0) {
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen client_workarounds |= list->num;
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen break;
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen }
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen }
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen if (list->name == NULL)
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen i_fatal("Unknown client workaround: %s", *str);
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return client_workarounds;
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen}
fa45f3f1ad423381ba07056e415ffc4d6449f089Timo Sirainen
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainenstatic enum uidl_keys parse_uidl_keymask(const char *format)
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen{
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen enum uidl_keys mask = 0;
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen for (; *format != '\0'; format++) {
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen if (format[0] == '%' && format[1] != '\0') {
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen switch (var_get_key(++format)) {
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen case 'v':
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen mask |= UIDL_UIDVALIDITY;
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen break;
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen case 'u':
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen mask |= UIDL_UID;
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen break;
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen case 'm':
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen mask |= UIDL_MD5;
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen break;
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen case 'f':
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen mask |= UIDL_FILE_NAME;
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen break;
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen }
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen }
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen }
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen return mask;
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen}
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainenstatic bool main_init(const struct pop3_settings *set, struct mail_user *user)
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen{
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen struct client *client;
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen const char *str;
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen bool ret = TRUE;
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (set->shutdown_clients) {
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen /* If master dies, the log fd gets closed and we'll quit */
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen log_io = io_add(STDERR_FILENO, IO_ERROR,
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen log_error_callback, NULL);
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen }
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen clients_init();
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client = client_create(0, 1, user, set);
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen if (client == NULL)
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen return FALSE;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client->workarounds = parse_workarounds(set);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client->uidl_keymask = parse_uidl_keymask(set->pop3_uidl_format);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (client->uidl_keymask == 0) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen i_fatal("pop3_uidl_format setting doesn't contain any "
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen "%% variables.");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen }
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen
7753eaa6a4275e074b4ce8428b85d9d04fc67f31Timo Sirainen if (!IS_STANDALONE())
7753eaa6a4275e074b4ce8428b85d9d04fc67f31Timo Sirainen client_send_line(client, "+OK Logged in.");
7753eaa6a4275e074b4ce8428b85d9d04fc67f31Timo Sirainen
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen str = getenv("CLIENT_INPUT");
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen if (str != NULL) T_BEGIN {
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen buffer_t *buf = t_base64_decode_str(str);
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen if (buf->used > 0) {
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen if (!i_stream_add_data(client->input, buf->data,
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen buf->used))
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen i_panic("Couldn't add client input to stream");
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen ret = client_handle_input(client);
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen }
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen } T_END;
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen return ret;
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen}
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainenstatic void main_deinit(void)
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen{
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen if (log_io != NULL)
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen io_remove(&log_io);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen clients_deinit();
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen}
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainenint main(int argc, char *argv[], char *envp[])
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen{
812883e7758a86f615b9508ef10df1339123da83Timo Sirainen const struct setting_parser_info *set_roots[] = {
812883e7758a86f615b9508ef10df1339123da83Timo Sirainen &pop3_setting_parser_info,
812883e7758a86f615b9508ef10df1339123da83Timo Sirainen NULL
812883e7758a86f615b9508ef10df1339123da83Timo Sirainen };
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen enum master_service_flags service_flags = 0;
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen enum mail_storage_service_flags storage_service_flags =
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen MAIL_STORAGE_SERVICE_FLAG_DISALLOW_ROOT;
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen struct mail_user *mail_user;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen const struct pop3_settings *set;
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen const char *user;
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen int c;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#ifdef DEBUG
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (!IS_STANDALONE() && getenv("GDB") == NULL)
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen fd_debug_verify_leaks(3, 1024);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#endif
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen if (IS_STANDALONE() && getuid() == 0 &&
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen net_getpeername(1, NULL, NULL) == 0) {
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen printf("-ERR pop3 binary must not be started from "
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen "inetd, use pop3-login instead.\n");
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen return 1;
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen }
4dbe08e1f7f1271299ada9338ff5015367efd0b7Timo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen if (IS_STANDALONE())
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen service_flags |= MASTER_SERVICE_FLAG_STANDALONE;
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen else
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen service_flags |= MAIL_STORAGE_SERVICE_FLAG_DISALLOW_ROOT;
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen service = master_service_init("pop3", service_flags, argc, argv);
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen while ((c = getopt(argc, argv, master_service_getopt_string())) > 0) {
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen if (!master_service_parse_option(service, c, optarg))
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen i_fatal("Unknown argument: %c", c);
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen }
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen user = getenv("USER");
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen if (user == NULL) {
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen if (IS_STANDALONE())
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen user = getlogin();
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen if (user == NULL)
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen i_fatal("USER environment missing");
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen }
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen
812883e7758a86f615b9508ef10df1339123da83Timo Sirainen mail_user = mail_storage_service_init_user(service, user, set_roots,
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen storage_service_flags);
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen set = mail_storage_service_get_settings(service);
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen restrict_access_allow_coredumps(TRUE);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen process_title_init(argv, envp);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen /* fake that we're running, so we know if client was destroyed
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen while initializing */
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen io_loop_set_running(current_ioloop);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen if (main_init(set, mail_user))
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen master_service_run(service);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen main_deinit();
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen mail_storage_service_deinit_user();
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen master_service_deinit(&service);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen return 0;
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen}