dovecot-2.0: *-login: Use a common client_send_line() API.
dovecot at dovecot.org
dovecot at dovecot.org
Mon Aug 10 04:53:25 EEST 2009
details: http://hg.dovecot.org/dovecot-2.0/rev/b9ad5b841f7e
changeset: 9754:b9ad5b841f7e
user: Timo Sirainen <tss at iki.fi>
date: Sun Aug 09 17:55:43 2009 -0400
description:
*-login: Use a common client_send_line() API.
diffstat:
10 files changed, 350 insertions(+), 175 deletions(-)
src/imap-login/client-authenticate.c | 67 ++++++++-----
src/imap-login/client-authenticate.h | 5 -
src/imap-login/client.c | 168 ++++++++++++++++++++++++----------
src/imap-login/client.h | 4
src/imap-login/imap-proxy.c | 37 ++++---
src/login-common/client-common.h | 15 +++
src/pop3-login/client-authenticate.c | 89 ++++++++++--------
src/pop3-login/client.c | 107 +++++++++++++++------
src/pop3-login/client.h | 4
src/pop3-login/pop3-proxy.c | 29 +++--
diffs (truncated from 1087 to 300 lines):
diff -r fc025d93b274 -r b9ad5b841f7e src/imap-login/client-authenticate.c
--- a/src/imap-login/client-authenticate.c Sun Aug 09 16:20:31 2009 -0400
+++ b/src/imap-login/client-authenticate.c Sun Aug 09 17:55:43 2009 -0400
@@ -189,6 +189,8 @@ static bool client_handle_args(struct im
.. [REFERRAL ..] (Reason from auth server)
*/
reply = t_str_new(128);
+ str_append(reply, client->cmd_tag);
+ str_append_c(reply, ' ');
str_append(reply, nologin ? "NO " : "OK ");
str_printfa(reply, "[REFERRAL imap://%s;AUTH=%s@%s",
destuser, client->common.auth_mech_name, host);
@@ -203,7 +205,8 @@ static bool client_handle_args(struct im
str_append(reply, "Logged in, but you should use "
"this server instead.");
}
- client_send_tagline(client, str_c(reply));
+ str_append(reply, "\r\n");
+ client_send_raw(client, str_c(reply));
if (!nologin) {
client_destroy_success(client, "Login with referral");
return TRUE;
@@ -211,18 +214,23 @@ static bool client_handle_args(struct im
} else if (nologin) {
/* Authentication went ok, but for some reason user isn't
allowed to log in. Shouldn't probably happen. */
- reply = t_str_new(128);
- if (reason != NULL)
- str_printfa(reply, "NO [ALERT] %s", reason);
- else if (temp) {
- str_append(reply, "NO ["IMAP_RESP_CODE_UNAVAILABLE"] "
- AUTH_TEMP_FAILED_MSG);
+ if (reason != NULL) {
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTH_FAIL_REASON,
+ reason);
+ } else if (temp) {
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTH_FAIL_TEMP,
+ AUTH_TEMP_FAILED_MSG);
} else if (authz_failure) {
- str_append(reply, "NO "IMAP_AUTHZ_FAILED_MSG);
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTHZ_FAILED,
+ "Authorization failed");
} else {
- str_append(reply, "NO "IMAP_AUTH_FAILED_MSG);
- }
- client_send_tagline(client, str_c(reply));
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTH_FAILED,
+ AUTH_FAILED_MSG);
+ }
} else {
/* normal login/failure */
return FALSE;
@@ -240,7 +248,6 @@ static void sasl_callback(struct client
{
struct imap_client *client = (struct imap_client *)_client;
struct const_iovec iov[3];
- const char *msg;
size_t data_len;
bool nodelay;
@@ -267,13 +274,18 @@ static void sasl_callback(struct client
break;
}
- if (reply == SASL_SERVER_REPLY_AUTH_ABORTED)
- msg = "BAD Authentication aborted by client.";
- else if (data == NULL)
- msg = "NO "IMAP_AUTH_FAILED_MSG;
- else
- msg = t_strconcat("NO [ALERT] ", data, NULL);
- client_send_tagline(client, msg);
+ if (reply == SASL_SERVER_REPLY_AUTH_ABORTED) {
+ client_send_line(&client->common, CLIENT_CMD_REPLY_BAD,
+ "Authentication aborted by client.");
+ } else if (data == NULL) {
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTH_FAILED,
+ AUTH_FAILED_MSG);
+ } else {
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTH_FAIL_REASON,
+ data);
+ }
if (!client->destroyed)
client_auth_failed(client, nodelay);
@@ -282,8 +294,8 @@ static void sasl_callback(struct client
if (data == NULL)
client_destroy_internal_failure(client);
else {
- client_send_tagline(client,
- t_strconcat("NO ", data, NULL));
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTH_FAIL_TEMP, data);
/* authentication itself succeeded, we just hit some
internal failure. */
client_destroy_success(client, data);
@@ -365,8 +377,8 @@ int cmd_authenticate(struct imap_client
"SSL required for authentication");
}
client->common.auth_attempts++;
- client_send_tagline(client,
- "NO ["IMAP_RESP_CODE_PRIVACYREQUIRED"] "
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTH_FAIL_NOSSL,
"Authentication not allowed until SSL/TLS is enabled.");
return 1;
}
@@ -401,12 +413,13 @@ int cmd_login(struct imap_client *client
}
client->common.auth_tried_disabled_plaintext = TRUE;
client->common.auth_attempts++;
- client_send_line(client,
+ client_send_raw(client,
"* BAD [ALERT] Plaintext authentication not allowed "
"without SSL/TLS, but your client did it anyway. "
- "If anyone was listening, the password was exposed.");
- client_send_tagline(client, "NO ["IMAP_RESP_CODE_CLIENTBUG"] "
- AUTH_PLAINTEXT_DISABLED_MSG);
+ "If anyone was listening, the password was exposed.\r\n");
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_AUTH_FAIL_NOSSL,
+ AUTH_PLAINTEXT_DISABLED_MSG);
return 1;
}
diff -r fc025d93b274 -r b9ad5b841f7e src/imap-login/client-authenticate.h
--- a/src/imap-login/client-authenticate.h Sun Aug 09 16:20:31 2009 -0400
+++ b/src/imap-login/client-authenticate.h Sun Aug 09 17:55:43 2009 -0400
@@ -2,11 +2,6 @@
#define CLIENT_AUTHENTICATE_H
struct imap_arg;
-
-#define IMAP_AUTH_FAILED_MSG \
- "["IMAP_RESP_CODE_AUTHFAILED"] "AUTH_FAILED_MSG
-#define IMAP_AUTHZ_FAILED_MSG \
- "["IMAP_RESP_CODE_AUTHZFAILED"] Authorization failed"
const char *client_authenticate_get_capabilities(struct imap_client *client);
diff -r fc025d93b274 -r b9ad5b841f7e src/imap-login/client.c
--- a/src/imap-login/client.c Sun Aug 09 16:20:31 2009 -0400
+++ b/src/imap-login/client.c Sun Aug 09 17:55:43 2009 -0400
@@ -11,6 +11,7 @@
#include "strescape.h"
#include "imap-parser.h"
#include "imap-id.h"
+#include "imap-resp-code.h"
#include "master-service.h"
#include "master-auth.h"
#include "client.h"
@@ -45,9 +46,9 @@
#endif
#define AUTH_SERVER_WAITING_MSG \
- "* OK Waiting for authentication process to respond.."
+ "Waiting for authentication process to respond.."
#define AUTH_MASTER_WAITING_MSG \
- "* OK Waiting for authentication master process to respond.."
+ "Waiting for authentication master process to respond.."
const char *login_protocol = "imap";
const char *login_process_name = "imap-login";
@@ -116,9 +117,10 @@ static int cmd_capability(struct imap_cl
CAPABILITY commands. */
if (!client->starttls)
client->client_ignores_capability_resp_code = TRUE;
- client_send_line(client, t_strconcat(
- "* CAPABILITY ", get_capability(client), NULL));
- client_send_tagline(client, "OK Capability completed.");
+ client_send_raw(client, t_strconcat(
+ "* CAPABILITY ", get_capability(client), "\r\n", NULL));
+ client_send_line(&client->common, CLIENT_CMD_REPLY_OK,
+ "Capability completed.");
return 1;
}
@@ -133,7 +135,8 @@ static void client_start_tls(struct imap
fd_ssl = ssl_proxy_new(client->common.fd, &client->common.ip,
client->common.set, &client->common.proxy);
if (fd_ssl == -1) {
- client_send_line(client, "* BYE TLS initialization failed.");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_BYE,
+ "TLS initialization failed.");
client_destroy(client,
"Disconnected: TLS initialization failed.");
return;
@@ -176,12 +179,14 @@ static int cmd_starttls(struct imap_clie
static int cmd_starttls(struct imap_client *client)
{
if (client->common.tls) {
- client_send_tagline(client, "BAD TLS is already active.");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_BAD,
+ "TLS is already active.");
return 1;
}
if (!ssl_initialized) {
- client_send_tagline(client, "BAD TLS support isn't enabled.");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_BAD,
+ "TLS support isn't enabled.");
return 1;
}
@@ -190,7 +195,8 @@ static int cmd_starttls(struct imap_clie
if (client->io != NULL)
io_remove(&client->io);
- client_send_tagline(client, "OK Begin TLS negotiation now.");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_OK,
+ "Begin TLS negotiation now.");
/* uncork the old fd */
o_stream_uncork(client->output);
@@ -249,31 +255,34 @@ static int cmd_id(struct imap_client *cl
}
env = getenv("IMAP_ID_SEND");
- client_send_line(client, t_strdup_printf("* ID %s",
- imap_id_reply_generate(env)));
- client_send_tagline(client, "OK ID completed.");
+ client_send_raw(client,
+ t_strdup_printf("* ID %s\r\n", imap_id_reply_generate(env)));
+ client_send_line(&client->common, CLIENT_CMD_REPLY_OK, "ID completed.");
return 1;
}
static int cmd_noop(struct imap_client *client)
{
- client_send_tagline(client, "OK NOOP completed.");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_OK,
+ "NOOP completed.");
return 1;
}
static int cmd_logout(struct imap_client *client)
{
- client_send_line(client, "* BYE Logging out");
- client_send_tagline(client, "OK Logout completed.");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_BYE,
+ "Logging out");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_OK,
+ "Logout completed.");
client_destroy(client, "Aborted login");
return 1;
}
static int cmd_enable(struct imap_client *client)
{
- client_send_line(client, "* ENABLED");
- client_send_tagline(client,
- "OK ENABLE ignored in non-authenticated state.");
+ client_send_raw(client, "* ENABLED\r\n");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_OK,
+ "ENABLE ignored in non-authenticated state.");
return 1;
}
@@ -345,14 +354,14 @@ static bool client_handle_input(struct i
/* error */
msg = imap_parser_get_error(client->parser, &fatal);
if (fatal) {
- client_send_line(client, t_strconcat("* BYE ",
- msg, NULL));
+ client_send_line(&client->common,
+ CLIENT_CMD_REPLY_BYE, msg);
client_destroy(client,
t_strconcat("Disconnected: ", msg, NULL));
return FALSE;
}
- client_send_tagline(client, t_strconcat("BAD ", msg, NULL));
+ client_send_line(&client->common, CLIENT_CMD_REPLY_BAD, msg);
client->cmd_finished = TRUE;
client->skip_line = TRUE;
return TRUE;
@@ -374,14 +383,14 @@ static bool client_handle_input(struct i
if (*client->cmd_tag == '\0')
client->cmd_tag = "*";
if (++client->bad_counter >= CLIENT_MAX_BAD_COMMANDS) {
- client_send_line(client,
- "* BYE Too many invalid IMAP commands.");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_BYE,
+ "Too many invalid IMAP commands.");
client_destroy(client,
"Disconnected: Too many invalid commands");
return FALSE;
}
- client_send_tagline(client,
- "BAD Error in IMAP command received by server.");
+ client_send_line(&client->common, CLIENT_CMD_REPLY_BAD,
+ "Error in IMAP command received by server.");
}
return ret != 0;
More information about the dovecot-cvs
mailing list