main.c revision 279ac7e2783ed3b945e2ecb378d87da35dae42f9
5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294Timo Sirainen/* Copyright (c) 2002-2013 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_append(title, net_ip2addr(client->user->remote_ip));
8846e6eed6177a39b662f4f1ebf9b84ad1f0b7ecTimo Sirainen str_printfa(title, "%u connections", pop3_client_count);
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainenstatic void pop3_die(void)
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,
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen int fd_in, int fd_out, const buffer_t *input_buf,
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen const char **error_r)
348d897426ff46ae23aaa432aff0087ce4d034d5Timo Sirainen "-ERR [SYS/TEMP] "MAIL_ERRSTR_CRITICAL_MSG"\r\n";
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen if (mail_storage_service_lookup_next(storage_service, input,
01758d8aaa05940397c8210af52d7f45a5b676aeTimo Sirainen (void)write(fd_out, lookup_error_str, strlen(lookup_error_str));
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen set = mail_storage_service_user_get_set(user)[1];
279ac7e2783ed3b945e2ecb378d87da35dae42f9Timo Sirainen if (client_create(fd_in, fd_out, input->session_id,
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,
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenlogin_client_connected(const struct master_login_client *client,
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen const char *username, const char *const *extra_fields)
3281669db44d09a087a203201248abbc81b3cc1aTimo Sirainen buffer_create_from_const_data(&input_buf, client->data,
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen if (client_create_from_input(&input, client->fd, client->fd,
6667f4006484bcaf9ab8fd03b97a7a3ae84ce0d5Timo Sirainen master_service_client_connection_destroyed(master_service);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainenstatic void login_client_failed(const struct master_login_client *client,
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen const char *msg;
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[] = {
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen enum mail_storage_service_flags storage_service_flags = 0;
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;
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 ||
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.auth_socket_path = t_abspath("auth-master");
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.postlogin_socket_path = t_abspath(argv[optind]);
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen login_set.failure_callback = login_client_failed;
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen master_service_set_die_callback(master_service, pop3_die);
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen /* fake that we're running, so we know if client was destroyed
573731df7d9b2ebb9028311a6c33b338dd2dd34dTimo Sirainen while handling its initial input */
53dff078a5f49e9d28d6c81d3437755e27526e3eTimo Sirainen master_login = master_login_init(master_service, &login_set);
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen master_service_run(master_service, client_connected);