bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING redis */
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen#define REDIS_DEFAULT_LOOKUP_TIMEOUT_MSECS (1000*30)
c7555525421dfef0327974946bb22040e325db66Timo Sirainen /* expecting +OK reply for AUTH */
640d320445eaad7f7ad834299a2726457f35f434Timo Sirainen /* expecting +OK reply for SELECT */
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* expecting $-1 / $<size> followed by GET reply */
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* expecting +QUEUED */
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* expecting +OK reply for DISCARD */
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* expecting *<nreplies> */
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* expecting EXEC reply */
c7555525421dfef0327974946bb22040e325db66Timo Sirainen char *username, *password, *key_prefix, *expire_value;
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainenstatic struct connection_list *redis_connections;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenredis_input_state_add(struct redis_dict *dict, enum redis_input_state state)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic void redis_input_state_remove(struct redis_dict *dict)
5c5e738bcc6e50228bdadbf77a8ab16a2eb915bbTimo Sirainenstatic void redis_callback(struct redis_dict *dict,
5c5e738bcc6e50228bdadbf77a8ab16a2eb915bbTimo Sirainen /* Don't let callback see that we've created our
5c5e738bcc6e50228bdadbf77a8ab16a2eb915bbTimo Sirainen internal ioloop in case it wants to add some ios
5c5e738bcc6e50228bdadbf77a8ab16a2eb915bbTimo Sirainen or timeouts. */
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainenredis_disconnected(struct redis_connection *conn, const char *reason)
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainenstatic void redis_conn_destroy(struct connection *_conn)
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen struct redis_connection *conn = (struct redis_connection *)_conn;
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen redis_disconnected(conn, connection_disconnect_reason(_conn));
c935c4494b7fe73d84bd546c6d702f0e79b9fabaTimo Sirainenstatic void redis_dict_wait_timeout(struct redis_dict *dict)
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "redis: Commit timed out in %u.%03u secs",
c935c4494b7fe73d84bd546c6d702f0e79b9fabaTimo Sirainen dict->timeout_msecs/1000, dict->timeout_msecs%1000);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic void redis_wait(struct redis_dict *dict)
c935c4494b7fe73d84bd546c6d702f0e79b9fabaTimo Sirainen to = timeout_add(dict->timeout_msecs, redis_dict_wait_timeout, dict);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen } while (array_count(&dict->input_states) > 0);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainenstatic int redis_input_get(struct redis_connection *conn, const char **error_r)
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen /* read the size first */
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen if (line[0] != '$' || str_to_uint(line+1, &conn->bytes_left) < 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "redis: Unexpected input (wanted $size): %s", line);
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen conn->bytes_left += 2; /* include trailing CRLF */
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen data = i_stream_get_data(conn->conn.input, &size);
531ddc880d8c8d1d3b4b6c5cf8abb1abb4a2e0daTimo Sirainen /* reply fully read - drop trailing CRLF */
531ddc880d8c8d1d3b4b6c5cf8abb1abb4a2e0daTimo Sirainen str_truncate(conn->last_reply, str_len(conn->last_reply)-2);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainenredis_conn_input_more(struct redis_connection *conn, const char **error_r)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen states = array_get(&dict->input_states, &count);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "redis: Unexpected input (expected nothing): %s", line);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen if (line[0] != '*' || str_to_uint(line+1, &num_replies) < 0)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen reply = array_idx_modifiable(&dict->replies, 0);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "redis: EXEC expected %u replies, not %u",
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* success, just ignore the actual reply */
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen reply = array_idx_modifiable(&dict->replies, 0);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* if we're running in a dict-ioloop, we're handling a
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen synchronous commit and need to stop now */
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen *error_r = t_strdup_printf("redis: Unexpected input (state=%d): %s", state, line);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic void redis_conn_input(struct connection *_conn)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen struct redis_connection *conn = (struct redis_connection *)_conn;
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen redis_disconnected(conn, i_stream_get_error(_conn->input));
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen while ((ret = redis_conn_input_more(conn, &error)) > 0) ;
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainenstatic void redis_conn_connected(struct connection *_conn, bool success)
aa8e63009c5aa3866bbf5a3e69a86b1ab480c4ddTimo Sirainen struct redis_connection *conn = (struct redis_connection *)_conn;
ba9ec8849b975c50aa60b7f661797415fac70ce5Timo Sirainen i_error("redis: connect(%s) failed: %m", _conn->name);
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainenstatic const struct connection_settings redis_conn_set = {
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainenstatic const struct connection_vfuncs redis_conn_vfuncs = {
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic const char *redis_escape_username(const char *username)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen const char *p;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen switch (*p) {
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainenredis_dict_init(struct dict *driver, const char *uri,
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen dict->timeout_msecs = REDIS_DEFAULT_LOOKUP_TIMEOUT_MSECS;
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("Invalid port: %s",
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen } else if (strncmp(*args, "prefix=", 7) == 0) {
e85a0dac7542bbae17e81e735f88897fff86becbTimo Sirainen } else if (strncmp(*args, "expire_secs=", 12) == 0) {
2480bdc0a4fbb75aad5408fd9f344556fc605082Timo Sirainen if (str_to_uint(value, &secs) < 0 || secs == 0) {
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen } else if (strncmp(*args, "timeout_msecs=", 14) == 0) {
8f2eb1ee9ec07661bd50275da99b5f351972a49aTimo Sirainen if (str_to_uint(*args+14, &dict->timeout_msecs) < 0) {
c7555525421dfef0327974946bb22040e325db66Timo Sirainen } else if (strncmp(*args, "password=", 9) == 0) {
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("Unknown parameter: %s",
ba9ec8849b975c50aa60b7f661797415fac70ce5Timo Sirainen connection_init_client_unix(redis_connections, &dict->conn.conn,
ba9ec8849b975c50aa60b7f661797415fac70ce5Timo Sirainen connection_init_client_ip(redis_connections, &dict->conn.conn,
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen dict->conn.last_reply = str_new(default_pool, 256);
39ea5717264668e2c7f9f7986eb821d21785f47fTimo Sirainen if (strchr(set->username, DICT_USERNAME_SEPARATOR) == NULL)
8f2eb1ee9ec07661bd50275da99b5f351972a49aTimo Sirainen /* escape the username */
39ea5717264668e2c7f9f7986eb821d21785f47fTimo Sirainen dict->username = i_strdup(redis_escape_username(set->username));
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainenstatic void redis_dict_deinit(struct dict *_dict)
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen struct redis_dict *dict = (struct redis_dict *)_dict;
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainenstatic void redis_dict_lookup_timeout(struct redis_dict *dict)
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen "redis: Lookup timed out in %u.%03u secs",
aa8e63009c5aa3866bbf5a3e69a86b1ab480c4ddTimo Sirainen dict->timeout_msecs/1000, dict->timeout_msecs%1000);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic const char *
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenredis_dict_get_full_key(struct redis_dict *dict, const char *key)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen if (strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen else if (strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0) {
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen key = t_strdup_printf("%s%c%s", dict->username,
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen key = t_strconcat(dict->key_prefix, key, NULL);
c7555525421dfef0327974946bb22040e325db66Timo Sirainenstatic void redis_dict_auth(struct redis_dict *dict)
c7555525421dfef0327974946bb22040e325db66Timo Sirainen cmd = t_strdup_printf("*2\r\n$4\r\nAUTH\r\n$%d\r\n%s\r\n",
c7555525421dfef0327974946bb22040e325db66Timo Sirainen o_stream_nsend_str(dict->conn.conn.output, cmd);
c7555525421dfef0327974946bb22040e325db66Timo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_AUTH);
640d320445eaad7f7ad834299a2726457f35f434Timo Sirainenstatic void redis_dict_select_db(struct redis_dict *dict)
640d320445eaad7f7ad834299a2726457f35f434Timo Sirainen /* 0 is the default */
640d320445eaad7f7ad834299a2726457f35f434Timo Sirainen cmd = t_strdup_printf("*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n",
640d320445eaad7f7ad834299a2726457f35f434Timo Sirainen o_stream_nsend_str(dict->conn.conn.output, cmd);
640d320445eaad7f7ad834299a2726457f35f434Timo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_SELECT);
b5052fbfdbc2678cc8f12899afe55c998f43b740Timo Sirainenstatic int redis_dict_lookup(struct dict *_dict, pool_t pool, const char *key,
8f9a18189f01448267100fa54c3b4bb8639a1a56Timo Sirainen struct redis_dict *dict = (struct redis_dict *)_dict;
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen connection_client_connect(&dict->conn.conn) < 0) {
ba9ec8849b975c50aa60b7f661797415fac70ce5Timo Sirainen i_error("redis: Couldn't connect to %s", dict->conn.conn.name);
aa8e63009c5aa3866bbf5a3e69a86b1ab480c4ddTimo Sirainen /* wait for connection */
aa8e63009c5aa3866bbf5a3e69a86b1ab480c4ddTimo Sirainen cmd = t_strdup_printf("*2\r\n$3\r\nGET\r\n$%d\r\n%s\r\n",
1d2c463d23f09f15727edae9c78b07ec6a7a27daTimo Sirainen o_stream_nsend_str(dict->conn.conn.output, cmd);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_GET);
97d5d23d41aab07e0e7d98c1f9b834e325d7d1b3Timo Sirainen } while (array_count(&dict->input_states) > 0);
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen /* we failed in some way. make sure we disconnect since the
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen connection state isn't known anymore */
2644ab25367d56f775540e3539886d54a8453d61Aki Tuomi *error_r = t_strdup_printf("redis: Communication failure (last reply: %s)",
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen *value_r = p_strdup(pool, str_c(dict->conn.last_reply));
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen struct redis_dict *dict = (struct redis_dict *)_dict;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen ctx = i_new(struct redis_dict_transaction_context, 1);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen connection_client_connect(&dict->conn.conn) < 0) {
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* wait for connection */
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenredis_transaction_commit(struct dict_transaction_context *_ctx, bool async,
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen (struct redis_dict_transaction_context *)_ctx;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen struct redis_dict *dict = (struct redis_dict *)_ctx->dict;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen unsigned int i;
9a382894724292e2af60ef94fc471d761f45e5d5Timo Sirainen struct dict_commit_result result = { .ret = DICT_COMMIT_RET_OK };
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* make sure we're disconnected */
1d2c463d23f09f15727edae9c78b07ec6a7a27daTimo Sirainen "*1\r\n$4\r\nEXEC\r\n");
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_EXEC);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_EXEC_REPLY);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic void redis_transaction_rollback(struct dict_transaction_context *_ctx)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen (struct redis_dict_transaction_context *)_ctx;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen struct redis_dict *dict = (struct redis_dict *)_ctx->dict;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen /* make sure we're disconnected */
1d2c463d23f09f15727edae9c78b07ec6a7a27daTimo Sirainen "*1\r\n$7\r\nDISCARD\r\n");
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_DISCARD);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic int redis_check_transaction(struct redis_dict_transaction_context *ctx)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen struct redis_dict *dict = (struct redis_dict *)ctx->ctx.dict;
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen ctx->error = i_strdup("Disconnected during transaction");
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_MULTI);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen "*1\r\n$5\r\nMULTI\r\n") < 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen ctx->error = i_strdup_printf("write() failed: %s",
5890b141d53b92449c61f257c613a4e13b48b61bTimo Sirainenredis_append_expire(struct redis_dict_transaction_context *ctx,
5890b141d53b92449c61f257c613a4e13b48b61bTimo Sirainen struct redis_dict *dict = (struct redis_dict *)ctx->ctx.dict;
5890b141d53b92449c61f257c613a4e13b48b61bTimo Sirainen str_printfa(cmd, "*3\r\n$6\r\nEXPIRE\r\n$%u\r\n%s\r\n$%u\r\n%s\r\n",
5890b141d53b92449c61f257c613a4e13b48b61bTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_MULTI);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic void redis_set(struct dict_transaction_context *_ctx,
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen (struct redis_dict_transaction_context *)_ctx;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen struct redis_dict *dict = (struct redis_dict *)_ctx->dict;
e85a0dac7542bbae17e81e735f88897fff86becbTimo Sirainen str_printfa(cmd, "*3\r\n$3\r\nSET\r\n$%u\r\n%s\r\n$%u\r\n%s\r\n",
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_MULTI);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen if (o_stream_send(dict->conn.conn.output, str_data(cmd), str_len(cmd)) < 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen ctx->error = i_strdup_printf("write() failed: %s",
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic void redis_unset(struct dict_transaction_context *_ctx,
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen const char *key)
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen (struct redis_dict_transaction_context *)_ctx;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen struct redis_dict *dict = (struct redis_dict *)_ctx->dict;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen cmd = t_strdup_printf("*2\r\n$3\r\nDEL\r\n$%u\r\n%s\r\n",
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen if (o_stream_send_str(dict->conn.conn.output, cmd) < 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen ctx->error = i_strdup_printf("write() failed: %s",
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_MULTI);
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainenstatic void redis_atomic_inc(struct dict_transaction_context *_ctx,
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen (struct redis_dict_transaction_context *)_ctx;
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen struct redis_dict *dict = (struct redis_dict *)_ctx->dict;
5890b141d53b92449c61f257c613a4e13b48b61bTimo Sirainen str_printfa(cmd, "*3\r\n$6\r\nINCRBY\r\n$%u\r\n%s\r\n$%u\r\n%s\r\n",
1c7df2f6d12f5e7f0cf1e73452d4ae216a3d092cTimo Sirainen redis_input_state_add(dict, REDIS_INPUT_STATE_MULTI);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen if (o_stream_send(dict->conn.conn.output, str_data(cmd), str_len(cmd)) < 0) {
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen ctx->error = i_strdup_printf("write() failed: %s",
ade5567577dadb0b275c840208d3ad21a9f00a36Timo Sirainen .transaction_commit = redis_transaction_commit,