imap-proxy.c revision 45155bb1250cf5a120278f349465aded513a100f
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (c) 2004-2008 Dovecot authors, see the included COPYING file */
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainenstatic int proxy_input_line(struct imap_client *client,
1eff76c5dbd2ff14bbb7e40a164c290931bdf692Timo Sirainen const char *msg;
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* this is a banner */
45155bb1250cf5a120278f349465aded513a100fTimo Sirainen client_syslog(&client->common, t_strdup_printf(
45155bb1250cf5a120278f349465aded513a100fTimo Sirainen "proxy: Remote returned invalid banner: %s",
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* send LOGIN command */
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen imap_quote_append_string(str, client->proxy_user, FALSE);
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen imap_quote_append_string(str, client->proxy_password, FALSE);
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen (void)o_stream_send(output, str_data(str), str_len(str));
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* Login successful. Send this line to client. */
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen (void)o_stream_send_str(client->output, client->cmd_tag);
00b2227d6ff65629707670b7b8dfd236fced8293Timo Sirainen (void)o_stream_send_str(client->output, line + 1);
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen (void)o_stream_send(client->output, "\r\n", 2);
1eff76c5dbd2ff14bbb7e40a164c290931bdf692Timo Sirainen msg = t_strdup_printf("proxy(%s): started proxying to %s:%u",
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen login_proxy_detach(client->proxy, client->input,
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* Login failed. Send our own failure reply so client can't
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen figure out if user exists or not just by looking at the
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen reply string. */
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen client_send_tagline(client, "NO "AUTH_FAILED_MSG);
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* allow client input again */
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen client->io = io_add(client->common.fd, IO_READ,
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* probably some untagged reply */
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainenstatic void proxy_input(struct istream *input, struct ostream *output,
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* remote authentication failed, we're just
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen freeing the proxy */
d7cd49f01fad7c87c5a0865ebf54a548275e9feeTimo Sirainen /* we came here from client_destroy() */
c0594b0763a084d2648e0df8c9b525ef094ccedaTimo Sirainen /* failed for some reason, probably server disconnected */
c0594b0763a084d2648e0df8c9b525ef094ccedaTimo Sirainen client_send_line(client, "* BYE Temporary login failure.");
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* buffer full */
45155bb1250cf5a120278f349465aded513a100fTimo Sirainen "proxy: Remote input buffer full");
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* disconnected */
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen client_destroy(client, "Proxy: Remote disconnected");
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen while ((line = i_stream_next_line(input)) != NULL) {
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen if (proxy_input_line(client, output, line) < 0)
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainenint imap_proxy_new(struct imap_client *client, const char *host,
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen unsigned int port, const char *user, const char *password)
45155bb1250cf5a120278f349465aded513a100fTimo Sirainen client_syslog(&client->common, "proxy: password not given");
feb665db52583259a1f42037c6e8a22852aa8889Timo Sirainen /* connection_queue_add() decided that we were the oldest
feb665db52583259a1f42037c6e8a22852aa8889Timo Sirainen connection and killed us. */
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen client->proxy = login_proxy_new(&client->common, host, port,
f30577ff7cf29858f1878abe963b4f40a436434fTimo Sirainen /* disable input until authentication is finished */