From: Timo Sirainen Date: Thu, 3 Sep 2015 13:04:26 +0000 (+0300) Subject: doveadm director update command added. X-Git-Tag: 2.2.19.rc1~116 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4502a71879d6018bd2c64f13614bb619911dd9f;p=thirdparty%2Fdovecot%2Fcore.git doveadm director update command added. The difference to "doveadm director add" is that if the IP doesn't already exist, it's not added. --- diff --git a/src/director/doveadm-connection.c b/src/director/doveadm-connection.c index 4261ec2bb2..ac93a6f189 100644 --- a/src/director/doveadm-connection.c +++ b/src/director/doveadm-connection.c @@ -246,7 +246,8 @@ doveadm_cmd_director_remove(struct doveadm_connection *conn, const char *line) } static bool -doveadm_cmd_host_set(struct doveadm_connection *conn, const char *line) +doveadm_cmd_host_set_or_update(struct doveadm_connection *conn, const char *line, + bool update) { struct director *dir = conn->dir; const char *const *args, *ip_str, *tag = ""; @@ -264,8 +265,10 @@ doveadm_cmd_host_set(struct doveadm_connection *conn, const char *line) ip_str = t_strdup_until(ip_str, tag++); } if (ip_str == NULL || net_addr2ip(ip_str, &ip) < 0 || - (args[1] != NULL && str_to_uint(args[1], &vhost_count) < 0)) { - i_error("doveadm sent invalid HOST-SET parameters: %s", line); + (args[1] != NULL && str_to_uint(args[1], &vhost_count) < 0) || + (args[1] == NULL && update)) { + i_error("doveadm sent invalid %s parameters: %s", + update ? "HOST-UPDATE" : "HOST-SET", line); return FALSE; } if (vhost_count > MAX_VALID_VHOST_COUNT && vhost_count != UINT_MAX) { @@ -273,11 +276,16 @@ doveadm_cmd_host_set(struct doveadm_connection *conn, const char *line) return TRUE; } host = mail_host_lookup(dir->mail_hosts, &ip); - if (host == NULL) + if (host == NULL) { + if (update) { + o_stream_nsend_str(conn->output, "NOTFOUND\n"); + return TRUE; + } host = mail_host_add_ip(dir->mail_hosts, &ip, tag); + } if (vhost_count != UINT_MAX) mail_host_set_vhost_count(dir->mail_hosts, host, vhost_count); - /* NOTE: we don't supporting changing a tag for an existing host. + /* NOTE: we don't support changing a tag for an existing host. it needs to be removed first. otherwise it would be a bit ugly to handle. */ director_update_host(dir, dir->self_host, NULL, host); @@ -286,6 +294,18 @@ doveadm_cmd_host_set(struct doveadm_connection *conn, const char *line) return TRUE; } +static bool +doveadm_cmd_host_set(struct doveadm_connection *conn, const char *line) +{ + return doveadm_cmd_host_set_or_update(conn, line, FALSE); +} + +static bool +doveadm_cmd_host_update(struct doveadm_connection *conn, const char *line) +{ + return doveadm_cmd_host_set_or_update(conn, line, TRUE); +} + static bool doveadm_cmd_host_updown(struct doveadm_connection *conn, bool down, const char *line) @@ -616,6 +636,8 @@ static void doveadm_connection_input(struct doveadm_connection *conn) ret = doveadm_cmd_director_remove(conn, args); else if (strcmp(cmd, "HOST-SET") == 0) ret = doveadm_cmd_host_set(conn, args); + else if (strcmp(cmd, "HOST-UPDATE") == 0) + ret = doveadm_cmd_host_update(conn, args); else if (strcmp(cmd, "HOST-UP") == 0) ret = doveadm_cmd_host_updown(conn, FALSE, args); else if (strcmp(cmd, "HOST-DOWN") == 0) diff --git a/src/doveadm/doveadm-director.c b/src/doveadm/doveadm-director.c index 4018c88656..376d199af7 100644 --- a/src/doveadm/doveadm-director.c +++ b/src/doveadm/doveadm-director.c @@ -407,7 +407,9 @@ deinit: pool_unref(&pool); } -static void cmd_director_add(int argc, char *argv[]) +static void +cmd_director_add_or_update(int argc, char *argv[], doveadm_command_t *cmd_func, + const char *director_cmd) { struct director_context *ctx; struct ip_addr *ips; @@ -415,18 +417,20 @@ static void cmd_director_add(int argc, char *argv[]) const char *host, *line; string_t *cmd; - ctx = cmd_director_init(argc, argv, "a:t:", cmd_director_add); + ctx = cmd_director_init(argc, argv, "a:t:", cmd_func); if (ctx->tag != NULL && ctx->tag[0] == '\0') ctx->tag = NULL; host = argv[optind++]; if (host == NULL) - director_cmd_help(cmd_director_add); + director_cmd_help(cmd_func); if (argv[optind] != NULL) { if (str_to_uint(argv[optind++], &vhost_count) < 0) - director_cmd_help(cmd_director_add); - } + director_cmd_help(cmd_func); + } else if (strcmp(director_cmd, "HOST-UPDATE") == 0) + director_cmd_help(cmd_func); + if (argv[optind] != NULL) - director_cmd_help(cmd_director_add); + director_cmd_help(cmd_func); if (ctx->tag == NULL) { ctx->tag = strchr(host, '@'); @@ -437,7 +441,7 @@ static void cmd_director_add(int argc, char *argv[]) cmd = t_str_new(128); for (i = 0; i < ips_count; i++) { str_truncate(cmd, 0); - str_printfa(cmd, "HOST-SET\t%s", net_ip2addr(&ips[i])); + str_printfa(cmd, "%s\t%s", director_cmd, net_ip2addr(&ips[i])); if (ctx->tag != NULL) str_printfa(cmd, "@%s", ctx->tag); if (vhost_count != UINT_MAX) @@ -449,7 +453,9 @@ static void cmd_director_add(int argc, char *argv[]) line = i_stream_read_next_line(ctx->input); if (line == NULL || strcmp(line, "OK") != 0) { fprintf(stderr, "%s: %s\n", net_ip2addr(&ips[i]), - line == NULL ? "failed" : line); + line == NULL ? "failed" : + strcmp(line, "NOTFOUND") == 0 ? + "doesn't exist" : line); doveadm_exit_code = EX_TEMPFAIL; } else if (doveadm_verbose) { printf("%s: OK\n", net_ip2addr(&ips[i])); @@ -458,6 +464,16 @@ static void cmd_director_add(int argc, char *argv[]) director_disconnect(ctx); } +static void cmd_director_add(int argc, char *argv[]) +{ + cmd_director_add_or_update(argc, argv, cmd_director_add, "HOST-SET"); +} + +static void cmd_director_update(int argc, char *argv[]) +{ + cmd_director_add_or_update(argc, argv, cmd_director_update, "HOST-UPDATE"); +} + static void cmd_director_ipcmd(const char *cmd_name, doveadm_command_t *cmd, const char *success_result, int argc, char *argv[]) @@ -808,6 +824,8 @@ struct doveadm_cmd doveadm_cmd_director[] = { "[-a ] [-f ] [-h | -u] []" }, { cmd_director_add, "director add", "[-a ] [-t ] []" }, + { cmd_director_update, "director update", + "[-a ] [-t ] " }, { cmd_director_up, "director up", "[-a ] " }, { cmd_director_down, "director down",