client-common-auth.c revision a8c5a86d183db25a57bf193c06b41e092ec2e151
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2002-2014 Dovecot authors, see the included COPYING file */
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include "hostpid.h"
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include "login-common.h"
d172c364637944d667aa98eb5de0d4a3a97ab92aTimo Sirainen#include "istream.h"
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen#include "ostream.h"
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include "str.h"
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include "safe-memset.h"
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen#include "time-util.h"
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen#include "login-proxy.h"
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include "auth-client.h"
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include "dsasl-client.h"
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include "master-service-ssl-settings.h"
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include "client-common.h"
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#include <stdlib.h>
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen#define PROXY_FAILURE_MSG "Account is temporarily unavailable."
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#define PROXY_DEFAULT_TIMEOUT_MSECS (1000*30)
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen/* If we've been waiting auth server to respond for over this many milliseconds,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen send a "waiting" message. */
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen#define AUTH_WAITING_TIMEOUT_MSECS (30*1000)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen#define AUTH_WAITING_WARNING_TIMEOUT_MSECS (10*1000)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenstatic void client_auth_failed(struct client *client)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen{
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen i_free_and_null(client->master_data_prefix);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (client->auth_response != NULL)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen str_truncate(client->auth_response, 0);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (client->auth_initializing || client->destroyed)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen return;
a12399903f415a7e14c2816cffa2f7a09dcbb097Timo Sirainen
a12399903f415a7e14c2816cffa2f7a09dcbb097Timo Sirainen if (client->io != NULL)
a12399903f415a7e14c2816cffa2f7a09dcbb097Timo Sirainen io_remove(&client->io);
a12399903f415a7e14c2816cffa2f7a09dcbb097Timo Sirainen
a12399903f415a7e14c2816cffa2f7a09dcbb097Timo Sirainen client->io = io_add(client->fd, IO_READ, client_input, client);
a12399903f415a7e14c2816cffa2f7a09dcbb097Timo Sirainen client_input(client);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen}
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenstatic void client_auth_waiting_timeout(struct client *client)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen{
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (!client->notified_auth_ready) {
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen client_log_warn(client, "Auth process not responding, "
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen "delayed sending initial response (greeting)");
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen }
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen client_notify_status(client, FALSE, client->master_tag == 0 ?
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen AUTH_SERVER_WAITING_MSG : AUTH_MASTER_WAITING_MSG);
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen timeout_remove(&client->to_auth_waiting);
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen}
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainenvoid client_set_auth_waiting(struct client *client)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen{
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen i_assert(client->to_auth_waiting == NULL);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen client->to_auth_waiting =
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen timeout_add(!client->notified_auth_ready ?
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen AUTH_WAITING_WARNING_TIMEOUT_MSECS :
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen AUTH_WAITING_TIMEOUT_MSECS,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen client_auth_waiting_timeout, client);
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainen}
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainen
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainenstatic void client_auth_parse_args(struct client *client,
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainen const char *const *args,
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainen struct client_auth_reply *reply_r)
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainen{
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainen const char *key, *value, *p;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen memset(reply_r, 0, sizeof(*reply_r));
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen for (; *args != NULL; args++) {
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen p = strchr(*args, '=');
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (p == NULL) {
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen key = *args;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen value = "";
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen } else {
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen key = t_strdup_until(*args, p);
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen value = p + 1;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen }
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (strcmp(key, "nologin") == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->nologin = TRUE;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen else if (strcmp(key, "proxy") == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->proxy = TRUE;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "temp") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->temp = TRUE;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen else if (strcmp(key, "authz") == 0)
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen reply_r->authz_failure = TRUE;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen else if (strcmp(key, "user_disabled") == 0)
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen client->auth_user_disabled = TRUE;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen else if (strcmp(key, "pass_expired") == 0)
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen client->auth_pass_expired = TRUE;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen else if (strcmp(key, "reason") == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->reason = value;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "host") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->host = value;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "hostip") == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->hostip = value;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "port") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->port = atoi(value);
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen else if (strcmp(key, "destuser") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->destuser = value;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "pass") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->password = value;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen else if (strcmp(key, "proxy_timeout") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->proxy_timeout_msecs = 1000*atoi(value);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "proxy_refresh") == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->proxy_refresh_secs = atoi(value);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "proxy_mech") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->proxy_mech = value;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "proxy_nopipelining") == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->proxy_nopipelining = TRUE;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen else if (strcmp(key, "master") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->master_user = value;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen else if (strcmp(key, "ssl") == 0) {
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->ssl_flags |= PROXY_SSL_FLAG_YES;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (strcmp(value, "any-cert") == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->ssl_flags |= PROXY_SSL_FLAG_ANY_CERT;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (reply_r->port == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->port = login_binary->default_ssl_port;
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen } else if (strcmp(key, "starttls") == 0) {
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen reply_r->ssl_flags |= PROXY_SSL_FLAG_YES |
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen PROXY_SSL_FLAG_STARTTLS;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (strcmp(value, "any-cert") == 0)
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen reply_r->ssl_flags |= PROXY_SSL_FLAG_ANY_CERT;
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen } else if (strcmp(key, "user") == 0) {
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen /* already handled in login-common */
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen } else if (client->set->auth_debug)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen i_debug("Ignoring unknown passdb extra field: %s", key);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen }
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (reply_r->port == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen reply_r->port = login_binary->default_port;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (reply_r->destuser == NULL)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen reply_r->destuser = client->virtual_user;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen}
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainenstatic void proxy_free_password(struct client *client)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen{
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (client->proxy_password == NULL)
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen return;
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen safe_memset(client->proxy_password, 0, strlen(client->proxy_password));
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen i_free_and_null(client->proxy_password);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen}
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenvoid client_proxy_finish_destroy_client(struct client *client)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen{
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen string_t *str = t_str_new(128);
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen if (client->input->closed) {
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen /* input stream got closed in client_send_raw_data().
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen In most places we don't have to check for this explicitly,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen but login_proxy_detach() attempts to get and use the
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen istream's fd, which is now -1. */
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen client_destroy(client, "Disconnected");
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen }
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen str_printfa(str, "proxy(%s): started proxying to %s:%u",
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen client->virtual_user,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen login_proxy_get_host(client->login_proxy),
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen login_proxy_get_port(client->login_proxy));
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (strcmp(client->virtual_user, client->proxy_user) != 0) {
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen /* remote username is different, log it */
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen str_append_c(str, '/');
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen str_append(str, client->proxy_user);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen }
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (client->proxy_master_user != NULL)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen str_printfa(str, " (master %s)", client->proxy_master_user);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen login_proxy_detach(client->login_proxy);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen client_destroy_success(client, str_c(str));
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen}
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenstatic void client_proxy_error(struct client *client, const char *text)
efb373fd7e30df171452c9f40881a8bd06b81780Timo Sirainen{
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen client->v.proxy_error(client, text);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen}
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenvoid client_proxy_log_failure(struct client *client, const char *line)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen{
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen string_t *str = t_str_new(128);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen str_printfa(str, "proxy(%s): Login failed to %s:%u",
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen client->virtual_user,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen login_proxy_get_host(client->login_proxy),
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen login_proxy_get_port(client->login_proxy));
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (strcmp(client->virtual_user, client->proxy_user) != 0) {
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen /* remote username is different, log it */
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen str_append_c(str, '/');
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen str_append(str, client->proxy_user);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen }
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen if (client->proxy_master_user != NULL)
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen str_printfa(str, " (master %s)", client->proxy_master_user);
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen str_append(str, ": ");
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen str_append(str, line);
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen i_info("%s", str_c(str));
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen}
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen
838e367716bbd5e44b4a1691db9cbf72af53e9f0Timo Sirainenvoid client_proxy_failed(struct client *client, bool send_line)
838e367716bbd5e44b4a1691db9cbf72af53e9f0Timo Sirainen{
838e367716bbd5e44b4a1691db9cbf72af53e9f0Timo Sirainen if (send_line) {
838e367716bbd5e44b4a1691db9cbf72af53e9f0Timo Sirainen client_proxy_error(client, PROXY_FAILURE_MSG);
838e367716bbd5e44b4a1691db9cbf72af53e9f0Timo Sirainen }
838e367716bbd5e44b4a1691db9cbf72af53e9f0Timo Sirainen
838e367716bbd5e44b4a1691db9cbf72af53e9f0Timo Sirainen if (client->proxy_sasl_client != NULL)
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen dsasl_client_free(&client->proxy_sasl_client);
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen login_proxy_free(&client->login_proxy);
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen proxy_free_password(client);
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen i_free_and_null(client->proxy_user);
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen i_free_and_null(client->proxy_master_user);
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen /* call this last - it may destroy the client */
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen client_auth_failed(client);
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen}
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainenstatic const char *get_disconnect_reason(struct istream *input)
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen{
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen errno = input->stream_errno;
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen return errno == 0 || errno == EPIPE ? "Connection closed" :
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen t_strdup_printf("Connection closed: %m");
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen}
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainenstatic void proxy_input(struct client *client)
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen{
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen struct istream *input;
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen struct ostream *output;
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen const char *line;
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen unsigned int duration;
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen if (client->login_proxy == NULL) {
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen /* we're just freeing the proxy */
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen return;
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen }
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen input = login_proxy_get_istream(client->login_proxy);
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen if (input == NULL) {
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen if (client->destroyed) {
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen /* we came here from client_destroy() */
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen return;
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen }
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen /* failed for some reason, probably server disconnected */
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen client_proxy_failed(client, TRUE);
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen return;
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen }
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen i_assert(!client->destroyed);
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen switch (i_stream_read(input)) {
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen case -2:
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen client_log_err(client, "proxy: Remote input buffer full");
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen client_proxy_failed(client, TRUE);
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen return;
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen case -1:
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen line = i_stream_next_line(input);
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen duration = ioloop_time - client->created;
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen client_log_err(client, t_strdup_printf(
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen "proxy: Remote %s:%u disconnected: %s "
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen "(state=%u, duration=%us)%s",
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen login_proxy_get_host(client->login_proxy),
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen login_proxy_get_port(client->login_proxy),
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen get_disconnect_reason(input),
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen client->proxy_state, duration,
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen line == NULL ? "" : t_strdup_printf(
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen " - BUG: line not read: %s", line)));
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen client_proxy_failed(client, TRUE);
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen return;
3c52b294054995d62bd1851c5c42b975fd8c22c9Timo Sirainen }
output = client->output;
o_stream_ref(output);
o_stream_cork(output);
while ((line = i_stream_next_line(input)) != NULL) {
if (client->v.proxy_parse_line(client, line) != 0)
break;
}
o_stream_uncork(output);
o_stream_unref(&output);
}
static int proxy_start(struct client *client,
const struct client_auth_reply *reply)
{
struct login_proxy_settings proxy_set;
const struct dsasl_client_mech *sasl_mech = NULL;
i_assert(reply->destuser != NULL);
i_assert(!client->destroyed);
i_assert(client->proxy_sasl_client == NULL);
client->proxy_mech = NULL;
client->v.proxy_reset(client);
if (reply->password == NULL) {
client_log_err(client, "proxy: password not given");
client_proxy_error(client, PROXY_FAILURE_MSG);
return -1;
}
if (reply->host == NULL || *reply->host == '\0') {
client_log_err(client, "proxy: host not given");
client_proxy_error(client, PROXY_FAILURE_MSG);
return -1;
}
if (reply->proxy_mech != NULL) {
sasl_mech = dsasl_client_mech_find(reply->proxy_mech);
if (sasl_mech == NULL) {
client_log_err(client, t_strdup_printf(
"proxy: Unsupported SASL mechanism %s",
reply->proxy_mech));
client_proxy_error(client, PROXY_FAILURE_MSG);
return -1;
}
} else if (reply->master_user != NULL) {
/* have to use PLAIN authentication with master user logins */
sasl_mech = &dsasl_client_mech_plain;
}
i_assert(client->refcount > 1);
if (client->destroyed) {
/* connection_queue_add() decided that we were the oldest
connection and killed us. */
return -1;
}
if (login_proxy_is_ourself(client, reply->host, reply->port,
reply->destuser)) {
client_log_err(client, "Proxying loops to itself");
client_proxy_error(client, PROXY_FAILURE_MSG);
return -1;
}
memset(&proxy_set, 0, sizeof(proxy_set));
proxy_set.host = reply->host;
if (reply->hostip != NULL &&
net_addr2ip(reply->hostip, &proxy_set.ip) < 0)
proxy_set.ip.family = 0;
proxy_set.port = reply->port;
proxy_set.connect_timeout_msecs = reply->proxy_timeout_msecs;
if (proxy_set.connect_timeout_msecs == 0)
proxy_set.connect_timeout_msecs = PROXY_DEFAULT_TIMEOUT_MSECS;
proxy_set.notify_refresh_secs = reply->proxy_refresh_secs;
proxy_set.ssl_flags = reply->ssl_flags;
if (login_proxy_new(client, &proxy_set, proxy_input) < 0) {
client_proxy_error(client, PROXY_FAILURE_MSG);
return -1;
}
client->proxy_mech = sasl_mech;
client->proxy_user = i_strdup(reply->destuser);
client->proxy_master_user = i_strdup(reply->master_user);
client->proxy_password = i_strdup(reply->password);
client->proxy_nopipelining = reply->proxy_nopipelining;
/* disable input until authentication is finished */
if (client->io != NULL)
io_remove(&client->io);
return 0;
}
static void ATTR_NULL(3, 4)
client_auth_result(struct client *client, enum client_auth_result result,
const struct client_auth_reply *reply, const char *text)
{
o_stream_cork(client->output);
client->v.auth_result(client, result, reply, text);
o_stream_uncork(client->output);
}
static bool
client_auth_handle_reply(struct client *client,
const struct client_auth_reply *reply, bool success)
{
if (reply->proxy) {
/* we want to proxy the connection to another server.
don't do this unless authentication succeeded. with
master user proxying we can get FAIL with proxy still set.
proxy host=.. [port=..] [destuser=..] pass=.. */
if (!success)
return FALSE;
if (proxy_start(client, reply) < 0)
client_auth_failed(client);
return TRUE;
}
if (reply->host != NULL) {
const char *reason;
if (reply->reason != NULL)
reason = reply->reason;
else if (reply->nologin)
reason = "Try this server instead.";
else
reason = "Logged in, but you should use this server instead.";
if (reply->nologin) {
client_auth_result(client,
CLIENT_AUTH_RESULT_REFERRAL_NOLOGIN,
reply, reason);
} else {
client_auth_result(client,
CLIENT_AUTH_RESULT_REFERRAL_SUCCESS,
reply, reason);
return TRUE;
}
} else if (reply->nologin) {
/* Authentication went ok, but for some reason user isn't
allowed to log in. Shouldn't probably happen. */
if (reply->reason != NULL) {
client_auth_result(client,
CLIENT_AUTH_RESULT_AUTHFAILED_REASON,
reply, reply->reason);
} else if (reply->temp) {
const char *timestamp, *msg;
timestamp = t_strflocaltime("%Y-%m-%d %H:%M:%S", ioloop_time);
msg = t_strdup_printf(AUTH_TEMP_FAILED_MSG" [%s:%s]",
my_hostname, timestamp);
client_auth_result(client, CLIENT_AUTH_RESULT_TEMPFAIL,
reply, msg);
} else if (reply->authz_failure) {
client_auth_result(client,
CLIENT_AUTH_RESULT_AUTHZFAILED, reply,
"Authorization failed");
} else {
client_auth_result(client,
CLIENT_AUTH_RESULT_AUTHFAILED, reply,
AUTH_FAILED_MSG);
}
} else {
/* normal login/failure */
return FALSE;
}
i_assert(reply->nologin);
if (!client->destroyed)
client_auth_failed(client);
return TRUE;
}
void client_auth_respond(struct client *client, const char *response)
{
client->auth_waiting = FALSE;
client_set_auth_waiting(client);
auth_client_request_continue(client->auth_request, response);
io_remove(&client->io);
}
void client_auth_abort(struct client *client)
{
sasl_server_auth_abort(client);
}
void client_auth_fail(struct client *client, const char *text)
{
sasl_server_auth_failed(client, text);
}
int client_auth_read_line(struct client *client)
{
const unsigned char *data;
size_t i, size;
unsigned int len;
if (i_stream_read_data(client->input, &data, &size, 0) == -1) {
client_destroy(client, "Disconnected");
return -1;
}
/* see if we have a full line */
for (i = 0; i < size; i++) {
if (data[i] == '\n')
break;
}
if (client->auth_response == NULL)
client->auth_response = str_new(default_pool, I_MAX(i+1, 256));
if (str_len(client->auth_response) + i > LOGIN_MAX_AUTH_BUF_SIZE) {
client_destroy(client, "Authentication response too large");
return -1;
}
str_append_n(client->auth_response, data, i);
i_stream_skip(client->input, i == size ? size : i+1);
/* drop trailing \r */
len = str_len(client->auth_response);
if (len > 0 && str_c(client->auth_response)[len-1] == '\r')
str_truncate(client->auth_response, len-1);
return i < size;
}
void client_auth_parse_response(struct client *client)
{
if (client_auth_read_line(client) <= 0)
return;
if (strcmp(str_c(client->auth_response), "*") == 0) {
sasl_server_auth_abort(client);
return;
}
client_auth_respond(client, str_c(client->auth_response));
memset(str_c_modifiable(client->auth_response), 0,
str_len(client->auth_response));
}
static void client_auth_input(struct client *client)
{
i_assert(client->v.auth_parse_response != NULL);
client->v.auth_parse_response(client);
}
void client_auth_send_challenge(struct client *client, const char *data)
{
struct const_iovec iov[3];
iov[0].iov_base = "+ ";
iov[0].iov_len = 2;
iov[1].iov_base = data;
iov[1].iov_len = strlen(data);
iov[2].iov_base = "\r\n";
iov[2].iov_len = 2;
o_stream_nsendv(client->output, iov, 3);
}
static void
sasl_callback(struct client *client, enum sasl_server_reply sasl_reply,
const char *data, const char *const *args)
{
struct client_auth_reply reply;
i_assert(!client->destroyed ||
sasl_reply == SASL_SERVER_REPLY_AUTH_ABORTED ||
sasl_reply == SASL_SERVER_REPLY_MASTER_FAILED);
switch (sasl_reply) {
case SASL_SERVER_REPLY_SUCCESS:
if (client->to_auth_waiting != NULL)
timeout_remove(&client->to_auth_waiting);
if (args != NULL) {
client_auth_parse_args(client, args, &reply);
if (client_auth_handle_reply(client, &reply, TRUE))
break;
}
client_auth_result(client, CLIENT_AUTH_RESULT_SUCCESS,
NULL, NULL);
client_destroy_success(client, "Login");
break;
case SASL_SERVER_REPLY_AUTH_FAILED:
case SASL_SERVER_REPLY_AUTH_ABORTED:
if (client->to_auth_waiting != NULL)
timeout_remove(&client->to_auth_waiting);
if (args != NULL) {
client_auth_parse_args(client, args, &reply);
reply.nologin = TRUE;
if (client_auth_handle_reply(client, &reply, FALSE))
break;
}
if (sasl_reply == SASL_SERVER_REPLY_AUTH_ABORTED) {
client_auth_result(client, CLIENT_AUTH_RESULT_ABORTED,
NULL, "Authentication aborted by client.");
} else if (data == NULL) {
client_auth_result(client,
CLIENT_AUTH_RESULT_AUTHFAILED, NULL,
AUTH_FAILED_MSG);
} else {
client_auth_result(client,
CLIENT_AUTH_RESULT_AUTHFAILED_REASON, NULL,
data);
}
if (!client->destroyed)
client_auth_failed(client);
break;
case SASL_SERVER_REPLY_MASTER_FAILED:
if (data != NULL) {
/* authentication itself succeeded, we just hit some
internal failure. */
client_auth_result(client, CLIENT_AUTH_RESULT_TEMPFAIL,
NULL, data);
}
/* the fd may still be hanging somewhere in kernel or another
process. make sure the client gets disconnected. */
if (shutdown(client->fd, SHUT_RDWR) < 0 && errno != ENOTCONN)
i_error("shutdown() failed: %m");
if (data == NULL)
client_destroy_internal_failure(client);
else
client_destroy_success(client, data);
break;
case SASL_SERVER_REPLY_CONTINUE:
i_assert(client->v.auth_send_challenge != NULL);
client->v.auth_send_challenge(client, data);
if (client->to_auth_waiting != NULL)
timeout_remove(&client->to_auth_waiting);
if (client->auth_response != NULL)
str_truncate(client->auth_response, 0);
i_assert(client->io == NULL);
client->auth_waiting = TRUE;
client->io = io_add(client->fd, IO_READ,
client_auth_input, client);
client_auth_input(client);
return;
}
client_unref(&client);
}
int client_auth_begin(struct client *client, const char *mech_name,
const char *init_resp)
{
if (!client->secured && strcmp(client->ssl_set->ssl, "required") == 0) {
if (client->set->auth_verbose) {
client_log(client, "Login failed: "
"SSL required for authentication");
}
client->auth_attempts++;
client_auth_result(client, CLIENT_AUTH_RESULT_SSL_REQUIRED, NULL,
"Authentication not allowed until SSL/TLS is enabled.");
return 1;
}
client_ref(client);
client->auth_initializing = TRUE;
sasl_server_auth_begin(client, login_binary->protocol, mech_name,
init_resp, sasl_callback);
client->auth_initializing = FALSE;
if (!client->authenticating)
return 1;
/* don't handle input until we get the initial auth reply */
if (client->io != NULL)
io_remove(&client->io);
client_set_auth_waiting(client);
return 0;
}
bool client_check_plaintext_auth(struct client *client, bool pass_sent)
{
if (client->secured || (!client->set->disable_plaintext_auth &&
strcmp(client->ssl_set->ssl, "required") != 0))
return TRUE;
if (client->set->auth_verbose) {
client_log(client, "Login failed: "
"Plaintext authentication disabled");
}
if (pass_sent) {
client_notify_status(client, TRUE,
"Plaintext authentication not allowed "
"without SSL/TLS, but your client did it anyway. "
"If anyone was listening, the password was exposed.");
}
client_auth_result(client, CLIENT_AUTH_RESULT_SSL_REQUIRED, NULL,
AUTH_PLAINTEXT_DISABLED_MSG);
client->auth_tried_disabled_plaintext = TRUE;
client->auth_attempts++;
return FALSE;
}
void clients_notify_auth_connected(void)
{
struct client *client, *next;
for (client = clients; client != NULL; client = next) {
next = client->next;
if (client->to_auth_waiting != NULL)
timeout_remove(&client->to_auth_waiting);
client_notify_auth_ready(client);
if (client->input_blocked) {
client->input_blocked = FALSE;
client_input(client);
}
}
}