Skip to content

Commit

Permalink
imapc: Change auth_failed boolean to more specific auth_failed_state
Browse files Browse the repository at this point in the history
Since we now rely on auth_failed_state being initialized to zero,
explicitly set IMAPC_COMMAND_STATE_OK to be defined as 0.
  • Loading branch information
sirainen authored and villesavolainen committed Jun 13, 2017
1 parent 5ce94fd commit b139348
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/lib-imap-client/imapc-client.h
Expand Up @@ -7,7 +7,7 @@
#define IMAPC_DEFAULT_MAX_IDLE_TIME (60*29)

enum imapc_command_state {
IMAPC_COMMAND_STATE_OK,
IMAPC_COMMAND_STATE_OK = 0,
IMAPC_COMMAND_STATE_NO,
IMAPC_COMMAND_STATE_BAD,
/* Authentication to IMAP server failed (NO or BAD) */
Expand Down
8 changes: 4 additions & 4 deletions src/lib-storage/index/imapc/imapc-list.c
Expand Up @@ -128,7 +128,7 @@ static void imapc_list_simple_callback(const struct imapc_command_reply *reply,
imapc_list_copy_error_from_reply(ctx->client->_list,
MAIL_ERROR_PARAMS, reply);
ctx->ret = -1;
} else if (ctx->client->auth_failed) {
} else if (imapc_storage_client_handle_auth_failure(ctx->client)) {
ctx->ret = -1;
} else if (reply->state == IMAPC_COMMAND_STATE_DISCONNECTED) {
mailbox_list_set_internal_error(&ctx->client->_list->list);
Expand Down Expand Up @@ -284,7 +284,7 @@ static void imapc_storage_sep_callback(const struct imapc_command_reply *reply,
imapc_list_sep_verify(list);
else if (reply->state == IMAPC_COMMAND_STATE_NO)
imapc_list_copy_error_from_reply(list, MAIL_ERROR_PARAMS, reply);
else if (list->client->auth_failed)
else if (imapc_storage_client_handle_auth_failure(list->client))
;
else if (reply->state == IMAPC_COMMAND_STATE_DISCONNECTED)
mailbox_list_set_internal_error(&list->list);
Expand Down Expand Up @@ -312,7 +312,7 @@ static void imapc_list_send_hierarcy_sep_lookup(struct imapc_mailbox_list *list)
int imapc_list_try_get_root_sep(struct imapc_mailbox_list *list, char *sep_r)
{
if (list->root_sep == '\0') {
if (list->client->auth_failed)
if (imapc_storage_client_handle_auth_failure(list->client))
return -1;
imapc_list_send_hierarcy_sep_lookup(list);
while (list->root_sep_pending)
Expand Down Expand Up @@ -552,7 +552,7 @@ static int imapc_list_refresh(struct imapc_mailbox_list *list)
struct mailbox_node *node;
const char *pattern;

if (list->client->auth_failed)
if (imapc_storage_client_handle_auth_failure(list->client))
return -1;
if (list->root_sep_lookup_failed) {
mailbox_list_set_internal_error(&list->list);
Expand Down
19 changes: 12 additions & 7 deletions src/lib-storage/index/imapc/imapc-storage.c
Expand Up @@ -119,7 +119,7 @@ void imapc_simple_context_init(struct imapc_simple_context *sctx,

void imapc_simple_run(struct imapc_simple_context *sctx)
{
if (sctx->client->auth_failed) {
if (imapc_storage_client_handle_auth_failure(sctx->client)) {
imapc_client_logout(sctx->client->client);
sctx->ret = -1;
}
Expand Down Expand Up @@ -151,7 +151,7 @@ void imapc_simple_callback(const struct imapc_command_reply *reply,
imapc_copy_error_from_reply(ctx->client->_storage,
MAIL_ERROR_PARAMS, reply);
ctx->ret = -1;
} else if (ctx->client->auth_failed) {
} else if (imapc_storage_client_handle_auth_failure(ctx->client)) {
ctx->ret = -1;
} else if (reply->state == IMAPC_COMMAND_STATE_DISCONNECTED) {
mail_storage_set_internal_error(&ctx->client->_storage->storage);
Expand Down Expand Up @@ -229,7 +229,7 @@ imapc_storage_client_login_callback(const struct imapc_command_reply *reply,
return;
}

client->auth_failed = TRUE;
client->auth_failed_state = reply->state;
client->auth_error = i_strdup(reply->text_full);

if (client->_storage != NULL) {
Expand All @@ -250,6 +250,11 @@ imapc_storage_client_login_callback(const struct imapc_command_reply *reply,
}
}

bool imapc_storage_client_handle_auth_failure(struct imapc_storage_client *client)
{
return client->auth_failed_state != IMAPC_COMMAND_STATE_OK;
}

static void imapc_storage_client_login(struct imapc_storage_client *client,
struct mail_user *user, const char *host)
{
Expand All @@ -260,7 +265,7 @@ static void imapc_storage_client_login(struct imapc_storage_client *client,
if it fails. */
while (!client->auth_returned)
imapc_client_run(client->client);
if (client->auth_failed) {
if (imapc_storage_client_handle_auth_failure(client)) {
user->error = p_strdup_printf(user->pool,
"imapc: Login to %s failed: %s",
host, client->auth_error);
Expand Down Expand Up @@ -510,7 +515,7 @@ imapc_mailbox_exists(struct mailbox *box, bool auto_boxes ATTR_UNUSED,

struct imapc_mailbox_list *list = (struct imapc_mailbox_list *)box->list;

if (list->client->auth_failed) {
if (imapc_storage_client_handle_auth_failure(list->client)) {
mail_storage_copy_list_error(box->storage, box->list);
return -1;
}
Expand Down Expand Up @@ -636,7 +641,7 @@ imapc_mailbox_open_callback(const struct imapc_command_reply *reply,
imapc_copy_error_from_reply(ctx->mbox->storage,
MAIL_ERROR_NOTFOUND, reply);
ctx->ret = -1;
} else if (ctx->mbox->storage->client->auth_failed) {
} else if (imapc_storage_client_handle_auth_failure(ctx->mbox->storage->client)) {
ctx->ret = -1;
} else if (reply->state == IMAPC_COMMAND_STATE_DISCONNECTED) {
ctx->ret = -1;
Expand Down Expand Up @@ -676,7 +681,7 @@ int imapc_mailbox_select(struct imapc_mailbox *mbox)
i_assert(mbox->client_box == NULL);

/* If authentication failed, don't check again. */
if (mbox->storage->client->auth_failed) {
if (imapc_storage_client_handle_auth_failure(mbox->storage->client)) {
return -1;
}
if (imapc_mailbox_get_capabilities(mbox) < 0)
Expand Down
5 changes: 5 additions & 0 deletions src/lib-storage/index/imapc/imapc-storage.h
Expand Up @@ -52,7 +52,11 @@ struct imapc_storage_client {

ARRAY(struct imapc_storage_event_callback) untagged_callbacks;

/* IMAPC_COMMAND_STATE_OK if no auth failure (yet), otherwise result to
the LOGIN/AUTHENTICATE command. */
enum imapc_command_state auth_failed_state;
char *auth_error;

/* Authentication reply was received (success or failure) */
bool auth_returned:1;
/* Authentication failed */
Expand Down Expand Up @@ -154,6 +158,7 @@ int imapc_storage_client_create(struct mail_namespace *ns,
struct imapc_storage_client **client_r,
const char **error_r);
void imapc_storage_client_unref(struct imapc_storage_client **client);
bool imapc_storage_client_handle_auth_failure(struct imapc_storage_client *client);

struct mail_save_context *
imapc_save_alloc(struct mailbox_transaction_context *_t);
Expand Down

0 comments on commit b139348

Please sign in to comment.