imap-proxy.c revision b3ae85f3552d98bb2f7a6efd8039e1d3f0423058
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (C) 2004 Timo Sirainen */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int proxy_input_line(struct imap_client *client,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* this is a banner */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen "Remote returned invalid banner: %s",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* send LOGIN command */
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen imap_quote_append_string(str, client->proxy_user, FALSE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen imap_quote_append_string(str, client->proxy_password, FALSE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (void)o_stream_send(output, str_data(str), str_len(str));
d22301419109ed4a38351715e6760011421dadecTimo Sirainen /* Login successful. Send this line to client. */
d22301419109ed4a38351715e6760011421dadecTimo Sirainen (void)o_stream_send_str(client->output, client->cmd_tag);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen (void)o_stream_send_str(client->output, line + 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (void)o_stream_send(client->output, "\r\n", 2);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen login_proxy_detach(client->proxy, client->input,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen client_destroy(client, t_strdup_printf("proxy(%s): started",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* Login failed. Send our own failure reply so client can't
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen figure out if user exists or not just by looking at the
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen reply string. */
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen client_send_tagline(client, "NO "AUTH_FAILED_MSG);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* allow client input again */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen client->io = io_add(client->common.fd, IO_READ,
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen /* probably some untagged reply */
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainenstatic void proxy_input(struct istream *input, struct ostream *output,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* remote authentication failed, we're just
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen freeing the proxy */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* failed for some reason, probably server disconnected */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen client_send_line(client, "* BYE Temporary login failure.");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* buffer full */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_error("imap-proxy(%s): Remote input buffer full",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* disconnected */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen client_destroy(client, "Proxy: Remote disconnected");
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen while ((line = i_stream_next_line(input)) != NULL) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (proxy_input_line(client, output, line) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint imap_proxy_new(struct imap_client *client, const char *host,
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen unsigned int port, const char *user, const char *password)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* connection_queue_add() decided that we were the oldest
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen connection and killed us. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen client->proxy = login_proxy_new(&client->common, host, port,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* disable input until authentication is finished */