bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenstatic struct mail_storage_service_ctx *storage_service;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenstatic struct master_login *master_login = NULL;
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainenpop3_client_created_func_t *hook_client_created = NULL;
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainenpop3_client_created_hook_set(pop3_client_created_func_t *new_hook)
6f2e601fa36133320aa88258106be46a175a0e53Timo Sirainen pop3_client_created_func_t *old_hook = hook_client_created;
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_printfa(title, "%u connections", pop3_client_count);
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen /* do nothing. pop3 connections typically die pretty quick anyway. */
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenstatic void client_add_input(struct client *client, const buffer_t *buf)
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen if (!i_stream_add_data(client->input, buf->data, buf->used))
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen i_panic("Couldn't add client input to stream");
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainenclient_create_from_input(const struct mail_storage_service_input *input,
11b8ec1586ee7c54c42fdbdf207576b06bb47b90Josef 'Jeff' Sipek int fd_in, int fd_out, struct client **client_r,
348d897426ff46ae23aaa432aff0087ce4d034d5Timo Sirainen "-ERR [SYS/TEMP] "MAIL_ERRSTR_CRITICAL_MSG"\r\n";
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen if (mail_storage_service_lookup_next(storage_service, input,
f87938eab9249ad84681f4fa747aab7b9a719670Timo Sirainen if (write(fd_out, lookup_error_str, strlen(lookup_error_str)) < 0) {
f87938eab9249ad84681f4fa747aab7b9a719670Timo Sirainen /* ignored */
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen set = mail_storage_service_user_get_set(user)[1];
c13ec2148cc43cb36f61e781f2514705f563cf47Timo Sirainen if (settings_var_expand(&pop3_setting_parser_info, set,
c13ec2148cc43cb36f61e781f2514705f563cf47Timo Sirainen mail_user->pool, mail_user_var_expand_table(mail_user),
c13ec2148cc43cb36f61e781f2514705f563cf47Timo Sirainen *error_r = t_strdup_printf("Failed to expand settings: %s", errstr);
11b8ec1586ee7c54c42fdbdf207576b06bb47b90Josef 'Jeff' Sipek *client_r = client_create(fd_in, fd_out, input->session_id,
11b8ec1586ee7c54c42fdbdf207576b06bb47b90Josef 'Jeff' Sipekstatic int lock_session(struct client *client)
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek i_assert(client->user->namespaces != NULL);
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek i_assert(client->set->pop3_lock_session);
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek if ((ret = pop3_lock_session(client)) <= 0) {
0f0bf444b82bbc8c9c269a1580f37274121ce5f2Josef 'Jeff' Sipek "-ERR [SYS/TEMP] Failed to create POP3 session lock." :
0f0bf444b82bbc8c9c269a1580f37274121ce5f2Josef 'Jeff' Sipek "-ERR [IN-USE] Mailbox is locked by another POP3 session.");
0f0bf444b82bbc8c9c269a1580f37274121ce5f2Josef 'Jeff' Sipek client_destroy(client, "Couldn't lock POP3 session");
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek#define MSG_BYE_INTERNAL_ERROR "-ERR "MAIL_ERRSTR_CRITICAL_MSG
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipekstatic int init_namespaces(struct client *client, bool already_logged_in)
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek /* finish initializing the user (see comment in main()) */
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek if (mail_namespaces_init(client->user, &error) < 0) {
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek client_send_line(client, MSG_BYE_INTERNAL_ERROR);
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek client->inbox_ns = mail_namespace_find_inbox(client->user->namespaces);
11b8ec1586ee7c54c42fdbdf207576b06bb47b90Josef 'Jeff' Sipekstatic void add_input(struct client *client,
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * RFC 1939 requires that the session lock gets acquired before the
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * positive response is sent to the client indicating a transition
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * to the TRANSACTION state.
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * Since the session lock is stored under the INBOX's storage
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * directory, the locking code requires that the namespaces are
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * initialized first.
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * If the system administrator configured dovecot to not use session
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * locks, we can send back the positive response before the
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * potentially long-running namespace initialization occurs. This
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * avoids the client possibly timing out during authentication due
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek * to storage initialization taking too long.
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek return; /* no need to propagate an error */
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek return; /* no need to propagate an error */
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek client_send_line(client, "+OK Logged in.");
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek client_send_line(client, "+OK Logged in.");
07470e90d392d4bbb77c8f75b852d4497bb9a571Josef 'Jeff' Sipek return; /* no need to propagate an error */
11b8ec1586ee7c54c42fdbdf207576b06bb47b90Josef 'Jeff' Sipek if (client_init_mailbox(client, &error) < 0) {
124e615e2949883473e30950a15a563feef26406Timo Sirainenstatic void main_stdio_run(const char *username)
124e615e2949883473e30950a15a563feef26406Timo Sirainen input.username = username != NULL ? username : getenv("USER");
cef2be5fb553b05f421f86c1ef497f0dc29d069eTimo Sirainen if (input.username == NULL && IS_STANDALONE())
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen if (client_create_from_input(&input, STDIN_FILENO, STDOUT_FILENO,
6616907d9668d9df7bacbd863d439a31084b3d3cJosef 'Jeff' Sipek /* client may be destroyed now */
813009c4303c095ed66a6a01c110b5cfe84ea3c3Josef 'Jeff' Sipeklogin_client_connected(const struct master_login_client *login_client,
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen const char *username, const char *const *extra_fields)
211caf3c233d562b0c8137e5eefae3cb1ef13003Stephan Bosch enum mail_auth_request_flags flags = login_client->auth_req.flags;
813009c4303c095ed66a6a01c110b5cfe84ea3c3Josef 'Jeff' Sipek input.local_ip = login_client->auth_req.local_ip;
813009c4303c095ed66a6a01c110b5cfe84ea3c3Josef 'Jeff' Sipek input.remote_ip = login_client->auth_req.remote_ip;
211caf3c233d562b0c8137e5eefae3cb1ef13003Stephan Bosch input.local_port = login_client->auth_req.local_port;
211caf3c233d562b0c8137e5eefae3cb1ef13003Stephan Bosch input.remote_port = login_client->auth_req.remote_port;
813009c4303c095ed66a6a01c110b5cfe84ea3c3Josef 'Jeff' Sipek input.session_id = login_client->session_id;
211caf3c233d562b0c8137e5eefae3cb1ef13003Stephan Bosch if ((flags & MAIL_AUTH_REQUEST_FLAG_CONN_SECURED) != 0)
211caf3c233d562b0c8137e5eefae3cb1ef13003Stephan Bosch if ((flags & MAIL_AUTH_REQUEST_FLAG_CONN_SSL_SECURED) != 0)
813009c4303c095ed66a6a01c110b5cfe84ea3c3Josef 'Jeff' Sipek buffer_create_from_const_data(&input_buf, login_client->data,
813009c4303c095ed66a6a01c110b5cfe84ea3c3Josef 'Jeff' Sipek if (client_create_from_input(&input, login_client->fd, login_client->fd,
6667f4006484bcaf9ab8fd03b97a7a3ae84ce0d5Timo Sirainen master_service_client_connection_destroyed(master_service);
6616907d9668d9df7bacbd863d439a31084b3d3cJosef 'Jeff' Sipek /* client may be destroyed now */
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainenstatic void login_client_failed(const struct master_login_client *client,
348d897426ff46ae23aaa432aff0087ce4d034d5Timo Sirainen msg = t_strdup_printf("-ERR [SYS/TEMP] %s\r\n", errormsg);
747c5d36868aa738b64ceedc87cda169aa1dbe96Timo Sirainen if (write(client->fd, msg, strlen(msg)) < 0) {
747c5d36868aa738b64ceedc87cda169aa1dbe96Timo Sirainen /* ignored */
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
6469403946bdf147886daba5ee8de07516c32238Pascal Volk /* when running standalone, we shouldn't even get here */
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen static const struct setting_parser_info *set_roots[] = {
646817f0cfb39b0e0eb545900f1566b76a851b20Timo Sirainen enum mail_storage_service_flags storage_service_flags = 0;
2812f5744f44ffb070bb6f8bede8dcaa04fe6337Timo Sirainen const char *username = NULL, *auth_socket_path = "auth-master";
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.postlogin_timeout_secs = MASTER_POSTLOGIN_TIMEOUT_DEFAULT;
348d897426ff46ae23aaa432aff0087ce4d034d5Timo Sirainen printf("-ERR [SYS/PERM] pop3 binary must not be started from "
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen "inetd, use pop3-login instead.\n");
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen service_flags |= MASTER_SERVICE_FLAG_STANDALONE |
3ba9a079592f46e94ce846e5aa80e4d479cd5e41Timo Sirainen service_flags |= MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN;
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek * We include MAIL_STORAGE_SERVICE_FLAG_NO_NAMESPACES so that the
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek * mail_user initialization is fast and we can quickly send back the
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek * OK response to LOGIN/AUTHENTICATE. Otherwise we risk a very slow
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek * namespace initialization to cause client timeouts on login.
bd5cf4556e8a5ffd0b40dbbf404f25a2e840b2d2Josef 'Jeff' Sipek storage_service_flags |= MAIL_STORAGE_SERVICE_FLAG_NO_NAMESPACES;
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen master_service = master_service_init("pop3", service_flags,
124e615e2949883473e30950a15a563feef26406Timo Sirainen while ((c = master_getopt(master_service)) > 0) {
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen if (str_to_uint(optarg, &login_set.postlogin_timeout_secs) < 0 ||
b1c85a1f889a5e71f491e320bdac95df3c9fe550Martti Rannanjärvi if (t_abspath(auth_socket_path, &login_set.auth_socket_path, &error) < 0) {
b1c85a1f889a5e71f491e320bdac95df3c9fe550Martti Rannanjärvi i_fatal("t_abspath(%s) failed: %s", auth_socket_path, error);
b1c85a1f889a5e71f491e320bdac95df3c9fe550Martti Rannanjärvi if (t_abspath(argv[optind], &login_set.postlogin_socket_path, &error) < 0) {
b1c85a1f889a5e71f491e320bdac95df3c9fe550Martti Rannanjärvi i_fatal("t_abspath(%s) failed: %s", argv[optind], error);
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.failure_callback = login_client_failed;
7f4bcbb9f2d97745a12d301b9ee276200ac58605Timo Sirainen master_login = master_login_init(master_service, &login_set);
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen master_service_set_die_callback(master_service, pop3_die);
7f4bcbb9f2d97745a12d301b9ee276200ac58605Timo Sirainen /* NOTE: login_set.*_socket_path are now invalid due to data stack
7f4bcbb9f2d97745a12d301b9ee276200ac58605Timo Sirainen having been freed */
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen /* fake that we're running, so we know if client was destroyed
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen while handling its initial input */
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen master_service_run(master_service, client_connected);