client.c revision decb23442f9e6cd5c4845a9cb162029b8c6d5f0f
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Copyright (c) 2002-2011 Dovecot authors, see the included COPYING file */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "login-common.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "buffer.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "ioloop.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "istream.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "ostream.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "safe-memset.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "str.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "strescape.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "imap-parser.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "imap-id.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "imap-resp-code.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "master-service.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "master-auth.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "client.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "client-authenticate.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "auth-client.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "ssl-proxy.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "imap-proxy.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "imap-login-settings.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <stdlib.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* maximum length for IMAP command line. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#define MAX_IMAP_LINE 8192
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Disconnect client when it sends too many bad commands */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#define CLIENT_MAX_BAD_COMMANDS 3
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Skip incoming data until newline is found,
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen returns TRUE if newline was found. */
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenbool client_skip_line(struct imap_client *client)
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen{
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen const unsigned char *data;
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen size_t i, data_size;
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen data = i_stream_get_data(client->common.input, &data_size);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen for (i = 0; i < data_size; i++) {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen if (data[i] == '\n') {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen i_stream_skip(client->common.input, i+1);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen return TRUE;
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen }
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen }
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen return FALSE;
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen}
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstatic const char *get_capability(struct client *client)
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen{
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen struct imap_client *imap_client = (struct imap_client *)client;
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen string_t *cap_str = t_str_new(256);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen if (*imap_client->set->imap_capability == '\0')
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen str_append(cap_str, CAPABILITY_BANNER_STRING);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen else if (*imap_client->set->imap_capability != '+')
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen str_append(cap_str, imap_client->set->imap_capability);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen else {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen str_append(cap_str, CAPABILITY_BANNER_STRING);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen str_append_c(cap_str, ' ');
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen str_append(cap_str, imap_client->set->imap_capability + 1);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen }
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen if (ssl_initialized && !client->tls)
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen str_append(cap_str, " STARTTLS");
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen if (client->set->disable_plaintext_auth && !client->secured)
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen str_append(cap_str, " LOGINDISABLED");
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen client_authenticate_get_capabilities(client, cap_str);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen return str_c(cap_str);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen}
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstatic int cmd_capability(struct imap_client *imap_client)
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen{
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen struct client *client = &imap_client->common;
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen /* Client is required to send CAPABILITY after STARTTLS, so the
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen capability resp-code workaround checks only pre-STARTTLS
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen CAPABILITY commands. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!client->starttls)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen imap_client->client_ignores_capability_resp_code = TRUE;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_send_raw(client, t_strconcat(
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "* CAPABILITY ", get_capability(client), "\r\n", NULL));
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen client_send_line(client, CLIENT_CMD_REPLY_OK,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Pre-login capabilities listed, post-login capabilities have more.");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int cmd_starttls(struct imap_client *client)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_cmd_starttls(&client->common);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic void
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenclient_update_info(struct imap_client *client, const struct imap_arg *args)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *key, *value;
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen if (!imap_arg_get_list(args, &args))
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen return;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen while (imap_arg_get_string(&args[0], &key) &&
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen imap_arg_get_nstring(&args[1], &value)) {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen if (strcasecmp(key, "x-originating-ip") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen (void)net_addr2ip(value, &client->common.ip);
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen else if (strcasecmp(key, "x-originating-port") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client->common.remote_port = atoi(value);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen else if (strcasecmp(key, "x-connected-ip") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen (void)net_addr2ip(value, &client->common.local_ip);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen else if (strcasecmp(key, "x-connected-port") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client->common.local_port = atoi(value);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen args += 2;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int cmd_id(struct imap_client *client, const struct imap_arg *args)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *env, *value;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (!client->id_logged) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client->id_logged = TRUE;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (client->common.trusted)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_update_info(client, args);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen env = getenv("IMAP_ID_LOG");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen value = imap_id_args_get_log_reply(args, env);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (value != NULL) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_log(&client->common,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen t_strdup_printf("ID sent: %s", value));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen env = getenv("IMAP_ID_SEND");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_send_raw(&client->common,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen t_strdup_printf("* ID %s\r\n", imap_id_reply_generate(env)));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_send_line(&client->common, CLIENT_CMD_REPLY_OK, "ID completed.");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int cmd_noop(struct imap_client *client)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_send_line(&client->common, CLIENT_CMD_REPLY_OK,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "NOOP completed.");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int cmd_logout(struct imap_client *client)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_send_line(&client->common, CLIENT_CMD_REPLY_BYE, "Logging out");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_send_line(&client->common, CLIENT_CMD_REPLY_OK,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Logout completed.");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_destroy(&client->common, "Aborted login");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int cmd_enable(struct imap_client *client)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_send_raw(&client->common, "* ENABLED\r\n");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen client_send_line(&client->common, CLIENT_CMD_REPLY_OK,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "ENABLE ignored in non-authenticated state.");
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return 1;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int client_command_execute(struct imap_client *client, const char *cmd,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const struct imap_arg *args)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen cmd = t_str_ucase(cmd);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (strcmp(cmd, "LOGIN") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return cmd_login(client, args);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (strcmp(cmd, "CAPABILITY") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return cmd_capability(client);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (strcmp(cmd, "STARTTLS") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return cmd_starttls(client);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (strcmp(cmd, "ID") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return cmd_id(client, args);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (strcmp(cmd, "NOOP") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return cmd_noop(client);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (strcmp(cmd, "LOGOUT") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return cmd_logout(client);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (strcmp(cmd, "ENABLE") == 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return cmd_enable(client);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return -2;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic bool imap_is_valid_tag(const char *tag)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen for (; *tag != '\0'; tag++) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen switch (*tag) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case '+':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* atom-specials: */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case '(':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case ')':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case '{':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case '/':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case ' ':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* list-wildcards: */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case '%':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case '*':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* quoted-specials: */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case '"':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen case '\\':
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return FALSE;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen default:
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (*tag < ' ') /* CTL */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return FALSE;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen break;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen return TRUE;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic int client_parse_command(struct imap_client *client,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const struct imap_arg **args_r)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *msg;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen bool fatal;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
switch (imap_parser_read_args(client->parser, 0, 0, args_r)) {
case -1:
/* error */
msg = imap_parser_get_error(client->parser, &fatal);
if (fatal) {
client_send_line(&client->common,
CLIENT_CMD_REPLY_BYE, msg);
client_destroy(&client->common,
t_strconcat("Disconnected: ", msg, NULL));
return FALSE;
}
client_send_line(&client->common, CLIENT_CMD_REPLY_BAD, msg);
client->cmd_finished = TRUE;
client->skip_line = TRUE;
return -1;
case -2:
/* not enough data */
return 0;
default:
/* we read the entire line - skip over the CRLF */
if (!client_skip_line(client))
i_unreached();
return 1;
}
}
static bool client_handle_input(struct imap_client *client)
{
const struct imap_arg *args;
int ret;
i_assert(!client->common.authenticating);
if (client->cmd_finished) {
/* clear the previous command from memory. don't do this
immediately after handling command since we need the
cmd_tag to stay some time after authentication commands. */
client->cmd_tag = NULL;
client->cmd_name = NULL;
imap_parser_reset(client->parser);
/* remove \r\n */
if (client->skip_line) {
if (!client_skip_line(client))
return FALSE;
client->skip_line = FALSE;
}
client->cmd_finished = FALSE;
}
if (client->cmd_tag == NULL) {
client->cmd_tag = imap_parser_read_word(client->parser);
if (client->cmd_tag == NULL)
return FALSE; /* need more data */
if (!imap_is_valid_tag(client->cmd_tag) ||
strlen(client->cmd_tag) > IMAP_TAG_MAX_LEN) {
/* the tag is invalid, don't allow it and don't
send it back. this attempts to prevent any
potentially dangerous replies in case someone tries
to access us using HTTP protocol. */
client->cmd_tag = "";
}
}
if (client->cmd_name == NULL) {
client->cmd_name = imap_parser_read_word(client->parser);
if (client->cmd_name == NULL)
return FALSE; /* need more data */
}
if (strcasecmp(client->cmd_name, "AUTHENTICATE") == 0) {
/* SASL-IR may need more space than input buffer's size,
so we'll handle it as a special case. */
ret = cmd_authenticate(client);
if (ret == 0)
return FALSE;
} else {
ret = client_parse_command(client, &args);
if (ret < 0)
return TRUE;
if (ret == 0)
return FALSE;
ret = *client->cmd_tag == '\0' ? -1 :
client_command_execute(client, client->cmd_name, args);
}
client->cmd_finished = TRUE;
if (ret == -2 && strcasecmp(client->cmd_tag, "LOGIN") == 0) {
client_send_line(&client->common, CLIENT_CMD_REPLY_BAD,
"First parameter in line is IMAP's command tag, "
"not the command name. Add that before the command, "
"like: a login user pass");
} else if (ret < 0) {
if (*client->cmd_tag == '\0')
client->cmd_tag = "*";
if (++client->common.bad_counter >= CLIENT_MAX_BAD_COMMANDS) {
client_send_line(&client->common, CLIENT_CMD_REPLY_BYE,
"Too many invalid IMAP commands.");
client_destroy(&client->common,
"Disconnected: Too many invalid commands");
return FALSE;
}
client_send_line(&client->common, CLIENT_CMD_REPLY_BAD,
"Error in IMAP command received by server.");
}
return ret != 0 && !client->common.destroyed;
}
static void imap_client_input(struct client *client)
{
struct imap_client *imap_client = (struct imap_client *)client;
if (!client_read(client))
return;
client_ref(client);
o_stream_cork(imap_client->common.output);
for (;;) {
if (!auth_client_is_connected(auth_client)) {
/* we're not currently connected to auth process -
don't allow any commands */
client_send_line(client, CLIENT_CMD_REPLY_STATUS,
AUTH_SERVER_WAITING_MSG);
if (client->to_auth_waiting != NULL)
timeout_remove(&client->to_auth_waiting);
client->input_blocked = TRUE;
break;
} else {
if (!client_handle_input(imap_client))
break;
}
}
o_stream_uncork(imap_client->common.output);
client_unref(&client);
}
static struct client *imap_client_alloc(pool_t pool)
{
struct imap_client *imap_client;
imap_client = p_new(pool, struct imap_client, 1);
return &imap_client->common;
}
static void imap_client_create(struct client *client, void **other_sets)
{
struct imap_client *imap_client = (struct imap_client *)client;
imap_client->set = other_sets[0];
imap_client->parser =
imap_parser_create(imap_client->common.input,
imap_client->common.output, MAX_IMAP_LINE);
client->io = io_add(client->fd, IO_READ, client_input, client);
}
static void imap_client_destroy(struct client *client)
{
struct imap_client *imap_client = (struct imap_client *)client;
i_free_and_null(imap_client->proxy_backend_capability);
imap_parser_unref(&imap_client->parser);
}
static void imap_client_send_greeting(struct client *client)
{
string_t *greet;
greet = t_str_new(128);
str_append(greet, "* OK ");
str_printfa(greet, "[CAPABILITY %s] ", get_capability(client));
str_append(greet, client->set->login_greeting);
str_append(greet, "\r\n");
client_send_raw(client, str_c(greet));
client->greeting_sent = TRUE;
}
static void imap_client_starttls(struct client *client)
{
struct imap_client *imap_client = (struct imap_client *)client;
imap_parser_unref(&imap_client->parser);
imap_client->parser =
imap_parser_create(imap_client->common.input,
imap_client->common.output, MAX_IMAP_LINE);
/* CRLF is lost from buffer when streams are reopened. */
imap_client->skip_line = FALSE;
}
static void
imap_client_send_line(struct client *client, enum client_cmd_reply reply,
const char *text)
{
struct imap_client *imap_client = (struct imap_client *)client;
const char *resp_code = NULL;
const char *prefix = "NO";
bool tagged = TRUE;
switch (reply) {
case CLIENT_CMD_REPLY_OK:
prefix = "OK";
break;
case CLIENT_CMD_REPLY_AUTH_FAILED:
resp_code = IMAP_RESP_CODE_AUTHFAILED;
break;
case CLIENT_CMD_REPLY_AUTHZ_FAILED:
resp_code = IMAP_RESP_CODE_AUTHZFAILED;
break;
case CLIENT_CMD_REPLY_AUTH_FAIL_TEMP:
resp_code = IMAP_RESP_CODE_UNAVAILABLE;
break;
case CLIENT_CMD_REPLY_AUTH_FAIL_REASON:
resp_code = "ALERT";
break;
case CLIENT_CMD_REPLY_AUTH_FAIL_NOSSL:
resp_code = IMAP_RESP_CODE_PRIVACYREQUIRED;
break;
case CLIENT_CMD_REPLY_BAD:
prefix = "BAD";
break;
case CLIENT_CMD_REPLY_BYE:
prefix = "BYE";
tagged = FALSE;
break;
case CLIENT_CMD_REPLY_STATUS:
prefix = "OK";
tagged = FALSE;
break;
case CLIENT_CMD_REPLY_STATUS_BAD:
prefix = "BAD";
tagged = FALSE;
resp_code = "ALERT";
break;
}
T_BEGIN {
string_t *line = t_str_new(256);
if (tagged)
str_append(line, imap_client->cmd_tag);
else
str_append_c(line, '*');
str_append_c(line, ' ');
str_append(line, prefix);
str_append_c(line, ' ');
if (resp_code != NULL)
str_printfa(line, "[%s] ", resp_code);
str_append(line, text);
str_append(line, "\r\n");
client_send_raw_data(client, str_data(line),
str_len(line));
} T_END;
}
static void imap_login_preinit(void)
{
login_set_roots = imap_login_setting_roots;
}
static void imap_login_init(void)
{
}
static void imap_login_deinit(void)
{
clients_destroy_all();
}
static struct client_vfuncs imap_client_vfuncs = {
imap_client_alloc,
imap_client_create,
imap_client_destroy,
imap_client_send_greeting,
imap_client_starttls,
imap_client_input,
imap_client_send_line,
imap_client_auth_handle_reply,
NULL,
NULL,
imap_proxy_reset,
imap_proxy_parse_line
};
static const struct login_binary imap_login_binary = {
.protocol = "imap",
.process_name = "imap-login",
.default_port = 143,
.default_ssl_port = 993,
.client_vfuncs = &imap_client_vfuncs,
.preinit = imap_login_preinit,
.init = imap_login_init,
.deinit = imap_login_deinit,
.sasl_support_final_reply = FALSE
};
int main(int argc, char *argv[])
{
return login_binary_run(&imap_login_binary, argc, argv);
}