pop3-proxy.c revision 45312f52ff3a3d4c137447be4c7556500c2f8bf2
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter/* Copyright (c) 2004-2009 Dovecot authors, see the included COPYING file */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#define PROXY_FAILURE_MSG "-ERR [IN-USE] "AUTH_TEMP_FAILED_MSG
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterstatic void proxy_free_password(struct pop3_client *client)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter safe_memset(client->proxy_password, 0, strlen(client->proxy_password));
8dc21698c4ed699801d2b6f9135b3d6cb8512917Lukas Slebodnikstatic void proxy_failed(struct pop3_client *client, bool send_line)
1203e462650f035b0df2304075d60b9a99e36715Stef Walter /* call this last - it may destroy the client */
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnikstatic void get_plain_auth(struct pop3_client *client, string_t *dest)
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik base64_encode(str_data(str), str_len(str), dest);
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterstatic int proxy_input_line(struct pop3_client *client,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter /* this is a banner */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter client_syslog_err(&client->common, t_strdup_printf(
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter "proxy: Remote returned invalid banner: %s",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter /* send USER command */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter /* master user login - use AUTH PLAIN. */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter (void)o_stream_send(output, str_data(str), str_len(str));
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter /* USER successful, send PASS */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter /* AUTH successful, send the authentication data */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter (void)o_stream_send(output, str_data(str), str_len(str));
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter /* Login successful. Send this line to client. */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter str_printfa(str, "proxy(%s): started proxying to %s:%u",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter /* remote username is different, log it */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter login_proxy_detach(client->proxy, client->common.input,
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter /* Login failed. Pass through the error message to client
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter (see imap-proxy code for potential problems with this) */
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter client_send_line(client, "-ERR "AUTH_FAILED_MSG);
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter str_printfa(str, "proxy(%s): Login failed to %s:%u",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter /* remote username is different, log it */
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walterstatic void proxy_input(struct istream *input, struct ostream *output,
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter const char *line;
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter /* we're just freeing the proxy */
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter /* we came here from client_destroy() */
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter /* failed for some reason, probably server disconnected */
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter "proxy: Remote input buffer full");
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter "proxy: Remote disconnected");
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter while ((line = i_stream_next_line(input)) != NULL) {
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterint pop3_proxy_new(struct pop3_client *client, const char *host,
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter unsigned int port, const char *user, const char *master_user,
1203e462650f035b0df2304075d60b9a99e36715Stef Walter client_syslog_err(&client->common, "proxy: password not given");
1203e462650f035b0df2304075d60b9a99e36715Stef Walter /* connection_queue_add() decided that we were the oldest
1203e462650f035b0df2304075d60b9a99e36715Stef Walter connection and killed us. */
1203e462650f035b0df2304075d60b9a99e36715Stef Walter if (login_proxy_is_ourself(&client->common, host, port, user)) {
1203e462650f035b0df2304075d60b9a99e36715Stef Walter client_syslog_err(&client->common, "Proxying loops to itself");
1203e462650f035b0df2304075d60b9a99e36715Stef Walter client->proxy = login_proxy_new(&client->common, host, port,
1203e462650f035b0df2304075d60b9a99e36715Stef Walter client->proxy_master_user = i_strdup(master_user);
1203e462650f035b0df2304075d60b9a99e36715Stef Walter /* disable input until authentication is finished */