main.c revision 4307c886579381dbb1897ea1388ae6978c96f560
183bea41fa640dc8117f3eb45ff935cd81377a84Timo Sirainen/* Copyright (c) 2002-2012 Dovecot authors, see the included COPYING file */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic struct mail_storage_service_ctx *storage_service;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic struct master_login *master_login = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenpop3_client_created_func_t *hook_client_created = NULL;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenpop3_client_created_hook_set(pop3_client_created_func_t *new_hook)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen pop3_client_created_func_t *old_hook = hook_client_created;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen str_append(title, net_ip2addr(client->user->remote_ip));
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen str_printfa(title, "%u connections", pop3_client_count);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic void pop3_die(void)
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen /* do nothing. pop3 connections typically die pretty quick anyway. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic void client_add_input(struct client *client, const buffer_t *buf)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!i_stream_add_data(client->input, buf->data, buf->used))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_panic("Couldn't add client input to stream");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenclient_create_from_input(const struct mail_storage_service_input *input,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen int fd_in, int fd_out, const buffer_t *input_buf,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen const char **error_r)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen "-ERR [IN-USE] "MAIL_ERRSTR_CRITICAL_MSG"\r\n";
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (mail_storage_service_lookup_next(storage_service, input,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen (void)write(fd_out, lookup_error_str, strlen(lookup_error_str));
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen set = mail_storage_service_user_get_set(user)[1];
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen client = client_create(fd_in, fd_out, input->session_id,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic void main_stdio_run(const char *username)
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen input.username = username != NULL ? username : getenv("USER");
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (input.username == NULL && IS_STANDALONE())
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (client_create_from_input(&input, STDIN_FILENO, STDOUT_FILENO,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenlogin_client_connected(const struct master_login_client *client,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen const char *username, const char *const *extra_fields)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen buffer_create_const_data(&input_buf, client->data,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (client_create_from_input(&input, client->fd, client->fd,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen master_service_client_connection_destroyed(master_service);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic void login_client_failed(const struct master_login_client *client,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen const char *msg;
902acc26200957a1f04da3cc947211f0b9ffce05Timo Sirainen msg = t_strdup_printf("-ERR [IN-USE] %s\r\n", errormsg);
902acc26200957a1f04da3cc947211f0b9ffce05Timo Sirainen if (write(client->fd, msg, strlen(msg)) < 0) {
902acc26200957a1f04da3cc947211f0b9ffce05Timo Sirainen /* ignored */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /* when running standalone, we shouldn't even get here */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen master_service_client_connection_accept(conn);
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen static const struct setting_parser_info *set_roots[] = {
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen enum mail_storage_service_flags storage_service_flags = 0;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen login_set.postlogin_timeout_secs = MASTER_POSTLOGIN_TIMEOUT_DEFAULT;
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen printf("-ERR pop3 binary must not be started from "
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen "inetd, use pop3-login instead.\n");
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen service_flags |= MASTER_SERVICE_FLAG_STANDALONE |
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen service_flags |= MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN;
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen master_service = master_service_init("pop3", service_flags,
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen while ((c = master_getopt(master_service)) > 0) {
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen if (str_to_uint(optarg, &login_set.postlogin_timeout_secs) < 0 ||
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen login_set.auth_socket_path = t_abspath("auth-master");
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen login_set.postlogin_socket_path = t_abspath(argv[optind]);
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen login_set.failure_callback = login_client_failed;
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen master_service_set_die_callback(master_service, pop3_die);
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen /* fake that we're running, so we know if client was destroyed
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen while handling its initial input */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen master_login = master_login_init(master_service, &login_set);
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen master_service_run(master_service, client_connected);