imap-urlauth-connection.c revision f82dfbdcc736987b8da09b75408393f2e074b891
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
d06d6667bac64aabe1efb216af56ca45108d63b0Timo Sirainen struct imap_urlauth_request *requests_head, *requests_tail;
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen struct timeout *to_reconnect, *to_idle, *to_response;
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen /* userid => target struct */
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen struct imap_urlauth_target *targets_head, *targets_tail;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen#define IMAP_URLAUTH_RESPONSE_TIMEOUT_MSECS 2*60*1000
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen#define IMAP_URLAUTH_HANDSHAKE "VERSION\timap-urlauth\t1\t0\n"
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen#define IMAP_URLAUTH_MAX_INLINE_LITERAL_SIZE (1024*32)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen (struct imap_urlauth_connection *conn, const char *reason);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen (struct imap_urlauth_connection *conn, const char *reason);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenstatic void imap_urlauth_connection_idle_disconnect
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenstatic void imap_urlauth_connection_timeout_abort
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenimap_urlauth_connection_init(const char *path, struct mail_user *user,
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen conn = i_new(struct imap_urlauth_connection, 1);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen conn->idle_timeout_msecs = idle_timeout_msecs;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenvoid imap_urlauth_connection_deinit(struct imap_urlauth_connection **_conn)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen struct imap_urlauth_connection *conn = *_conn;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenimap_urlauth_stop_response_timeout(struct imap_urlauth_connection *conn)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenimap_urlauth_start_response_timeout(struct imap_urlauth_connection *conn)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen conn->to_response = timeout_add(IMAP_URLAUTH_RESPONSE_TIMEOUT_MSECS,
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainenimap_urlauth_connection_get_target(struct imap_urlauth_connection *conn,
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainen struct imap_urlauth_target *target = conn->targets_head;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen target = i_new(struct imap_urlauth_target, 1);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen DLLIST2_APPEND(&conn->targets_head, &conn->targets_tail, target);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenimap_urlauth_target_free(struct imap_urlauth_connection *conn,
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen DLLIST2_REMOVE(&conn->targets_head, &conn->targets_tail, target);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenimap_urlauth_connection_select_target(struct imap_urlauth_connection *conn)
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainen struct imap_urlauth_target *target = conn->targets_head;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen const char *cmd;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen if (target == NULL || conn->state != IMAP_URLAUTH_STATE_AUTHENTICATED)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen i_debug("imap-urlauth: Selecting target user `%s'", target->userid);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen conn->state = IMAP_URLAUTH_STATE_SELECTING_TARGET;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen cmd = t_strdup_printf("USER\t%s\n", str_tabescape(target->userid));
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen if (o_stream_send_str(conn->output, cmd) < 0) {
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen i_warning("Error sending USER request to imap-urlauth server: %m");
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenimap_urlauth_connection_send_request(struct imap_urlauth_connection *conn)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen i_debug("imap-urlauth: No more requests pending; scheduling disconnect");
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen conn->to_idle = timeout_add(conn->idle_timeout_msecs,
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen imap_urlauth_connection_idle_disconnect, conn);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen if (conn->state == IMAP_URLAUTH_STATE_AUTHENTICATED) {
struct imap_urlauth_request *
void *context)
return urlreq;
T_BEGIN {
} T_END;
const char *error)
} T_END;
void *context)
const char *reason)
void *context)
const char **path_r)
int fd;
return fd;
const char *path;
if (size > 0) {
const unsigned char *data;
if (size > 0) {
data[0]);
const unsigned char *data;
int ret;
if (ret <= 0)
return ret;
} T_END;
if (ret < 0) {
if (ret != 0)
args++;
const char *response;
int ret;
case IMAP_URLAUTH_STATE_READY:
return ret;
i_unreached();
int ret;
int fd;
static void imap_urlauth_connection_disconnect