main.c revision 3281669db44d09a087a203201248abbc81b3cc1a
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2002-2012 Dovecot authors, see the included COPYING file */
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "pop3-common.h"
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#include "ioloop.h"
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen#include "buffer.h"
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen#include "istream.h"
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen#include "ostream.h"
1d3b9fce06b466bcf64f9ab7b622f3a6e4e939baTimo Sirainen#include "abspath.h"
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen#include "base64.h"
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen#include "str.h"
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen#include "process-title.h"
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen#include "restrict-access.h"
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen#include "master-service.h"
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen#include "master-login.h"
a9ba65037107e370c126d2b7e8c6e3f2a4aafd23Timo Sirainen#include "master-interface.h"
00f5efa3156ab6a0b4f21e8c703d0eb816cf3091Timo Sirainen#include "var-expand.h"
01758d8aaa05940397c8210af52d7f45a5b676aeTimo Sirainen#include "mail-error.h"
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen#include "mail-user.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() \
d482079eb385cd071bbc9637cacee225e4aff968Timo Sirainen (getenv(MASTER_IS_PARENT_ENV) == NULL)
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainenstatic bool verbose_proctitle = FALSE;
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenstatic struct mail_storage_service_ctx *storage_service;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenstatic struct master_login *master_login = NULL;
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainenpop3_client_created_func_t *hook_client_created = NULL;
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainen
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainenpop3_client_created_func_t *
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainenpop3_client_created_hook_set(pop3_client_created_func_t *new_hook)
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainen{
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainen pop3_client_created_func_t *old_hook = hook_client_created;
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainen
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainen hook_client_created = new_hook;
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainen return old_hook;
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainen}
8f1d14e3ada93a6d6ee64f73c6e6ae2364d8eba1Timo Sirainen
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainenvoid pop3_refresh_proctitle(void)
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen{
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen struct client *client;
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen string_t *title = t_str_new(128);
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen if (!verbose_proctitle)
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen return;
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_append_c(title, '[');
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen switch (pop3_client_count) {
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen case 0:
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_append(title, "idling");
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen break;
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen case 1:
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen client = pop3_clients;
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_append(title, client->user->username);
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen if (client->user->remote_ip != NULL) {
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_append_c(title, ' ');
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_append(title, net_ip2addr(client->user->remote_ip));
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen }
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen break;
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen default:
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_printfa(title, "%u connections", pop3_client_count);
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen break;
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen }
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_append_c(title, ']');
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen process_title_set(str_c(title));
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen}
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainenstatic void pop3_die(void)
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen{
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen /* do nothing. pop3 connections typically die pretty quick anyway. */
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen}
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenstatic void client_add_input(struct client *client, const buffer_t *buf)
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen{
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen struct ostream *output;
f9ef36afc131626754716d6f4874a2ad04da0ac4Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (buf != NULL && buf->used > 0) {
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (!i_stream_add_data(client->input, buf->data, buf->used))
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen i_panic("Couldn't add client input to stream");
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen }
cd94aeaa294f7cc507206b4b2075852f00e14d61Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen output = client->output;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen o_stream_ref(output);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen o_stream_cork(output);
7753eaa6a4275e074b4ce8428b85d9d04fc67f31Timo Sirainen if (!IS_STANDALONE())
7753eaa6a4275e074b4ce8428b85d9d04fc67f31Timo Sirainen client_send_line(client, "+OK Logged in.");
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen (void)client_handle_input(client);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen o_stream_uncork(output);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen o_stream_unref(&output);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen}
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainenstatic int
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainenclient_create_from_input(const struct mail_storage_service_input *input,
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen int fd_in, int fd_out, const buffer_t *input_buf,
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen const char **error_r)
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen{
01758d8aaa05940397c8210af52d7f45a5b676aeTimo Sirainen const char *lookup_error_str =
01758d8aaa05940397c8210af52d7f45a5b676aeTimo Sirainen "-ERR [IN-USE] "MAIL_ERRSTR_CRITICAL_MSG"\r\n";
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen struct mail_storage_service_user *user;
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen struct mail_user *mail_user;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen struct client *client;
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen const struct pop3_settings *set;
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen if (mail_storage_service_lookup_next(storage_service, input,
01758d8aaa05940397c8210af52d7f45a5b676aeTimo Sirainen &user, &mail_user, error_r) <= 0) {
01758d8aaa05940397c8210af52d7f45a5b676aeTimo Sirainen (void)write(fd_out, lookup_error_str, strlen(lookup_error_str));
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen return -1;
01758d8aaa05940397c8210af52d7f45a5b676aeTimo Sirainen }
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen restrict_access_allow_coredumps(TRUE);
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen set = mail_storage_service_user_get_set(user)[1];
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen if (set->verbose_proctitle)
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen verbose_proctitle = TRUE;
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen
b55f914c0ade77252cfd798ea8eb9a84bda56315Timo Sirainen client = client_create(fd_in, fd_out, input->session_id,
b55f914c0ade77252cfd798ea8eb9a84bda56315Timo Sirainen mail_user, user, set);
a1f9314bdd7da1a8f5596b49ec6b7b5c5daa4079Timo Sirainen if (client != NULL) T_BEGIN {
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen client_add_input(client, input_buf);
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen } T_END;
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen return 0;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen}
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
124e615e2949883473e30950a15a563feef26406Timo Sirainenstatic void main_stdio_run(const char *username)
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen{
d1fff80640050631b06bfab904a34b2ad24601e8Timo Sirainen struct mail_storage_service_input input;
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen buffer_t *input_buf;
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen const char *value, *error, *input_base64;
d1fff80640050631b06bfab904a34b2ad24601e8Timo Sirainen
d1fff80640050631b06bfab904a34b2ad24601e8Timo Sirainen memset(&input, 0, sizeof(input));
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen input.module = input.service = "pop3";
124e615e2949883473e30950a15a563feef26406Timo Sirainen input.username = username != NULL ? username : getenv("USER");
cef2be5fb553b05f421f86c1ef497f0dc29d069eTimo Sirainen if (input.username == NULL && IS_STANDALONE())
cef2be5fb553b05f421f86c1ef497f0dc29d069eTimo Sirainen input.username = getlogin();
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (input.username == NULL)
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen i_fatal("USER environment missing");
d1fff80640050631b06bfab904a34b2ad24601e8Timo Sirainen if ((value = getenv("IP")) != NULL)
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)net_addr2ip(value, &input.remote_ip);
d1fff80640050631b06bfab904a34b2ad24601e8Timo Sirainen if ((value = getenv("LOCAL_IP")) != NULL)
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)net_addr2ip(value, &input.local_ip);
1b04762685272a53643ac2179939537a44c7c044Timo Sirainen
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen input_base64 = getenv("CLIENT_INPUT");
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen input_buf = input_base64 == NULL ? NULL :
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen t_base64_decode_str(input_base64);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen if (client_create_from_input(&input, STDIN_FILENO, STDOUT_FILENO,
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen input_buf, &error) < 0)
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen i_fatal("%s", error);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen}
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenstatic void
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenlogin_client_connected(const struct master_login_client *client,
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen const char *username, const char *const *extra_fields)
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen{
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen struct mail_storage_service_input input;
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen const char *error;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen buffer_t input_buf;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen memset(&input, 0, sizeof(input));
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen input.module = input.service = "pop3";
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen input.local_ip = client->auth_req.local_ip;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen input.remote_ip = client->auth_req.remote_ip;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen input.username = username;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen input.userdb_fields = extra_fields;
b55f914c0ade77252cfd798ea8eb9a84bda56315Timo Sirainen input.session_id = client->session_id;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
3281669db44d09a087a203201248abbc81b3cc1aTimo Sirainen buffer_create_from_const_data(&input_buf, client->data,
3281669db44d09a087a203201248abbc81b3cc1aTimo Sirainen client->auth_req.data_size);
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen if (client_create_from_input(&input, client->fd, client->fd,
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen &input_buf, &error) < 0) {
4307c886579381dbb1897ea1388ae6978c96f560Timo Sirainen int fd = client->fd;
4307c886579381dbb1897ea1388ae6978c96f560Timo Sirainen
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen i_error("%s", error);
4307c886579381dbb1897ea1388ae6978c96f560Timo Sirainen i_close_fd(&fd);
6667f4006484bcaf9ab8fd03b97a7a3ae84ce0d5Timo Sirainen master_service_client_connection_destroyed(master_service);
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen }
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen}
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainenstatic void login_client_failed(const struct master_login_client *client,
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen const char *errormsg)
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen{
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen const char *msg;
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen msg = t_strdup_printf("-ERR [IN-USE] %s\r\n", errormsg);
747c5d36868aa738b64ceedc87cda169aa1dbe96Timo Sirainen if (write(client->fd, msg, strlen(msg)) < 0) {
747c5d36868aa738b64ceedc87cda169aa1dbe96Timo Sirainen /* ignored */
747c5d36868aa738b64ceedc87cda169aa1dbe96Timo Sirainen }
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen}
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen{
6469403946bdf147886daba5ee8de07516c32238Pascal Volk /* when running standalone, we shouldn't even get here */
6469403946bdf147886daba5ee8de07516c32238Pascal Volk i_assert(master_login != NULL);
6469403946bdf147886daba5ee8de07516c32238Pascal Volk
6469403946bdf147886daba5ee8de07516c32238Pascal Volk master_service_client_connection_accept(conn);
6469403946bdf147886daba5ee8de07516c32238Pascal Volk master_login_add(master_login, conn->fd);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen}
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
a3fe8c0c54d87822f4b4f8f0d10caac611861b2bTimo Sirainenint main(int argc, char *argv[])
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen{
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen static const struct setting_parser_info *set_roots[] = {
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen &pop3_setting_parser_info,
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen NULL
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen };
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen struct master_login_settings login_set;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen enum master_service_flags service_flags = 0;
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen enum mail_storage_service_flags storage_service_flags = 0;
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen const char *username = NULL;
124e615e2949883473e30950a15a563feef26406Timo Sirainen int c;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen memset(&login_set, 0, sizeof(login_set));
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.postlogin_timeout_secs = MASTER_POSTLOGIN_TIMEOUT_DEFAULT;
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (IS_STANDALONE() && getuid() == 0 &&
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen net_getpeername(1, NULL, NULL) == 0) {
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen printf("-ERR pop3 binary must not be started from "
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen "inetd, use pop3-login instead.\n");
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen return 1;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen }
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (IS_STANDALONE()) {
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen service_flags |= MASTER_SERVICE_FLAG_STANDALONE |
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen MASTER_SERVICE_FLAG_STD_CLIENT;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen } else {
3ba9a079592f46e94ce846e5aa80e4d479cd5e41Timo Sirainen service_flags |= MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen storage_service_flags |=
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen MAIL_STORAGE_SERVICE_FLAG_DISALLOW_ROOT;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen }
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen master_service = master_service_init("pop3", service_flags,
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen &argc, &argv, "t:u:");
124e615e2949883473e30950a15a563feef26406Timo Sirainen while ((c = master_getopt(master_service)) > 0) {
124e615e2949883473e30950a15a563feef26406Timo Sirainen switch (c) {
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen case 't':
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen if (str_to_uint(optarg, &login_set.postlogin_timeout_secs) < 0 ||
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.postlogin_timeout_secs == 0)
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen i_fatal("Invalid -t parameter: %s", optarg);
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen break;
124e615e2949883473e30950a15a563feef26406Timo Sirainen case 'u':
124e615e2949883473e30950a15a563feef26406Timo Sirainen storage_service_flags |=
124e615e2949883473e30950a15a563feef26406Timo Sirainen MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
124e615e2949883473e30950a15a563feef26406Timo Sirainen username = optarg;
124e615e2949883473e30950a15a563feef26406Timo Sirainen break;
124e615e2949883473e30950a15a563feef26406Timo Sirainen default:
124e615e2949883473e30950a15a563feef26406Timo Sirainen return FATAL_DEFAULT;
124e615e2949883473e30950a15a563feef26406Timo Sirainen }
124e615e2949883473e30950a15a563feef26406Timo Sirainen }
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.auth_socket_path = t_abspath("auth-master");
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen if (argv[optind] != NULL)
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.postlogin_socket_path = t_abspath(argv[optind]);
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.callback = login_client_connected;
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.failure_callback = login_client_failed;
1d3b9fce06b466bcf64f9ab7b622f3a6e4e939baTimo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen master_service_init_finish(master_service);
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen master_service_set_die_callback(master_service, pop3_die);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen storage_service =
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen mail_storage_service_init(master_service,
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen set_roots, storage_service_flags);
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen /* fake that we're running, so we know if client was destroyed
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen while handling its initial input */
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen io_loop_set_running(current_ioloop);
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (IS_STANDALONE()) {
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen T_BEGIN {
124e615e2949883473e30950a15a563feef26406Timo Sirainen main_stdio_run(username);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen } T_END;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen } else {
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen master_login = master_login_init(master_service, &login_set);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen io_loop_set_running(current_ioloop);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen }
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (io_loop_is_running(current_ioloop))
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen master_service_run(master_service, client_connected);
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen clients_destroy_all();
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (master_login != NULL)
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen master_login_deinit(&master_login);
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen mail_storage_service_deinit(&storage_service);
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen master_service_deinit(&master_service);
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen return 0;
548f87789cf9865572b7b86f7be5a9bbfa132f3fTimo Sirainen}