doveadm-connection.c revision 8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765c
5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294Timo Sirainen/* Copyright (c) 2010-2013 Dovecot authors, see the included COPYING file */
f158d9a303bb15a6848ca276c9391c7ca52e452bTimo Sirainen#define DOVEADM_HANDSHAKE "VERSION\tdirector-doveadm\t1\t0\n"
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic struct doveadm_connection *doveadm_connections;
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void doveadm_connection_deinit(struct doveadm_connection **_conn);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void doveadm_cmd_host_list(struct doveadm_connection *conn)
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen array_foreach(mail_hosts_get(conn->dir->mail_hosts), hostp) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen net_ip2addr(&(*hostp)->ip), (*hostp)->vhost_count,
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend(conn->output, str_data(str), str_len(str));
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainenstatic void doveadm_cmd_host_list_removed(struct doveadm_connection *conn)
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen struct mail_host *const *orig_hosts, *const *cur_hosts;
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen unsigned int i, j, orig_hosts_count, cur_hosts_count;
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen (void)mail_hosts_parse_and_add(orig_hosts_list,
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen orig_hosts = array_get(mail_hosts_get(orig_hosts_list),
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen cur_hosts = array_get(mail_hosts_get(conn->dir->mail_hosts),
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen /* the hosts are sorted by IP */
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen for (i = j = 0; i < orig_hosts_count && j < cur_hosts_count; ) {
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen ret = net_ip_cmp(&orig_hosts[i]->ip, &cur_hosts[j]->ip);
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen else if (ret > 0)
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen for (; i < orig_hosts_count; i++)
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen str_printfa(str, "%s\n", net_ip2addr(&orig_hosts[i]->ip));
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend(conn->output, str_data(str), str_len(str));
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void doveadm_cmd_director_list(struct doveadm_connection *conn)
4fda77c9e9fc68feb292c4dacae1fac49dd08165Timo Sirainen director_connection_get_host(dir->left) == host;
4fda77c9e9fc68feb292c4dacae1fac49dd08165Timo Sirainen director_connection_get_host(dir->right) == host;
8c2b4a45f17a5cb13bb01058ca37798cf48d91baTimo Sirainen last_failed = I_MAX(host->last_network_failure,
8c2b4a45f17a5cb13bb01058ca37798cf48d91baTimo Sirainen (unsigned long)last_failed);
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend(conn->output, str_data(str), str_len(str));
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainendoveadm_cmd_director_add(struct doveadm_connection *conn, const char *line)
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen const char *const *args;
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen (args[1] != NULL && str_to_uint(args[1], &port) < 0)) {
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen i_error("doveadm sent invalid DIRECTOR-ADD parameters");
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen if (director_host_lookup(conn->dir, &ip, port) == NULL) {
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen host = director_host_add(conn->dir, &ip, port);
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen director_notify_ring_added(host, conn->dir->self_host);
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainendoveadm_cmd_director_remove(struct doveadm_connection *conn, const char *line)
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen const char *const *args;
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen unsigned int port = 0;
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen (args[1] != NULL && str_to_uint(args[1], &port) < 0)) {
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen i_error("doveadm sent invalid DIRECTOR-REMOVE parameters");
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend_str(conn->output, "NOTFOUND\n");
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen director_ring_remove(host, conn->dir->self_host);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainendoveadm_cmd_host_set(struct doveadm_connection *conn, const char *line)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen const char *const *args;
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen (args[1] != NULL && str_to_uint(args[1], &vhost_count) < 0)) {
242abe3ad2423776e9cf05e1304eb8fda4831b23Timo Sirainen i_error("doveadm sent invalid HOST-SET parameters: %s", line);
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainen if (vhost_count > MAX_VALID_VHOST_COUNT && vhost_count != UINT_MAX) {
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend_str(conn->output, "vhost count too large\n");
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen host = mail_host_lookup(dir->mail_hosts, &ip);
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen host = mail_host_add_ip(dir->mail_hosts, &ip);
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen mail_host_set_vhost_count(dir->mail_hosts, host, vhost_count);
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen director_update_host(dir, dir->self_host, NULL, host);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainendoveadm_cmd_host_remove(struct doveadm_connection *conn, const char *line)
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen i_error("doveadm sent invalid HOST-REMOVE parameters");
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen host = mail_host_lookup(conn->dir->mail_hosts, &ip);
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend_str(conn->output, "NOTFOUND\n");
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen director_remove_host(conn->dir, conn->dir->self_host,
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainendoveadm_cmd_host_flush_all(struct doveadm_connection *conn)
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen array_foreach(mail_hosts_get(conn->dir->mail_hosts), hostp) {
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen director_flush_host(conn->dir, conn->dir->self_host,
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainendoveadm_cmd_host_flush(struct doveadm_connection *conn, const char *line)
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen i_error("doveadm sent invalid HOST-FLUSH parameters");
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen host = mail_host_lookup(conn->dir->mail_hosts, &ip);
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend_str(conn->output, "NOTFOUND\n");
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen director_flush_host(conn->dir, conn->dir->self_host,
b2024fa4e6ed39f9b5b6bb6c051f6d535fc0e011Timo Sirainendoveadm_cmd_user_lookup(struct doveadm_connection *conn, const char *line)
5733207dc3ec10e6e5a6e0a8b30fbd1b061062b9Timo Sirainen username_hash = user_directory_get_username_hash(conn->dir->users, line);
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen /* get user's current host */
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen user = user_directory_lookup(conn->dir->users, username_hash);
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen str_printfa(str, "%s\t%u", net_ip2addr(&user->host->ip),
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen /* get host if it wasn't in user directory */
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen host = mail_host_get_by_hash(conn->dir->mail_hosts, username_hash);
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen str_printfa(str, "\t%s", net_ip2addr(&host->ip));
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen /* get host with default configuration */
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen host = mail_host_get_by_hash(conn->dir->orig_config_hosts,
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen str_printfa(str, "\t%s\n", net_ip2addr(&host->ip));
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend(conn->output, str_data(str), str_len(str));
caa1fa99c79c568ce2e42477bc169e7024fb220bTimo Sirainendoveadm_cmd_user_list(struct doveadm_connection *conn, const char *line)
caa1fa99c79c568ce2e42477bc169e7024fb220bTimo Sirainen i_error("doveadm sent invalid USER-LIST parameters");
caa1fa99c79c568ce2e42477bc169e7024fb220bTimo Sirainen iter = user_directory_iter_init(conn->dir->users);
caa1fa99c79c568ce2e42477bc169e7024fb220bTimo Sirainen while ((user = user_directory_iter_next(iter)) != NULL) {
caa1fa99c79c568ce2e42477bc169e7024fb220bTimo Sirainen net_ip_compare(&ip, &user->host->ip)) T_BEGIN {
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend_str(conn->output, t_strdup_printf(
caa1fa99c79c568ce2e42477bc169e7024fb220bTimo Sirainen "%u\t%u\t%s\n",
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainendoveadm_cmd_user_move(struct doveadm_connection *conn, const char *line)
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainen const char *const *args;
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainen i_error("doveadm sent invalid USER-MOVE parameters: %s", line);
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainen host = mail_host_lookup(conn->dir->mail_hosts, &ip);
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend_str(conn->output, "NOTFOUND\n");
5733207dc3ec10e6e5a6e0a8b30fbd1b061062b9Timo Sirainen username_hash = user_directory_get_username_hash(conn->dir->users, line);
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainen user = user_directory_lookup(conn->dir->users, username_hash);
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainen if (user != NULL && user->kill_state != USER_KILL_STATE_NONE) {
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend_str(conn->output, "TRYAGAIN\n");
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainen director_move_user(conn->dir, conn->dir->self_host, NULL,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void doveadm_connection_input(struct doveadm_connection *conn)
d2cadbf5445156fc12988506279d51d0e53b0449Timo Sirainen if ((line = i_stream_read_next_line(conn->input)) == NULL) {
d2cadbf5445156fc12988506279d51d0e53b0449Timo Sirainen if (conn->input->eof || conn->input->stream_errno != 0)
f158d9a303bb15a6848ca276c9391c7ca52e452bTimo Sirainen if (!version_string_verify(line, "director-doveadm",
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen i_error("doveadm not compatible with this server "
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen "(mixed old and new binaries?)");
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen while ((line = i_stream_read_next_line(conn->input)) != NULL && ret) {
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen else if (strcmp(cmd, "HOST-LIST-REMOVED") == 0)
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen ret = doveadm_cmd_director_remove(conn, args);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen i_error("doveadm sent unknown command: %s", line);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (conn->input->eof || conn->input->stream_errno != 0 || !ret)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainendoveadm_connection_init(struct director *dir, int fd)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen conn->input = i_stream_create_fd(conn->fd, 1024, FALSE);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen conn->output = o_stream_create_fd(conn->fd, (size_t)-1, FALSE);
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_set_no_error_handling(conn->output, TRUE);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen conn->io = io_add(conn->fd, IO_READ, doveadm_connection_input, conn);
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend_str(conn->output, DOVEADM_HANDSHAKE);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void doveadm_connection_deinit(struct doveadm_connection **_conn)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen i_error("close(doveadm connection) failed: %m");
9522aa5f33cc37fe8ccd0d647cc51dd3ba6a9b55Timo Sirainen master_service_client_connection_destroyed(master_service);