]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm director update command added.
authorTimo Sirainen <tss@iki.fi>
Thu, 3 Sep 2015 13:04:26 +0000 (16:04 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 3 Sep 2015 13:04:26 +0000 (16:04 +0300)
The difference to "doveadm director add" is that if the IP doesn't already
exist, it's not added.

src/director/doveadm-connection.c
src/doveadm/doveadm-director.c

index 4261ec2bb2e0712bc046b02c5af7fbe98a792894..ac93a6f1897cdf683f56ca66b9121f4528018e5b 100644 (file)
@@ -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)
index 4018c88656685cd6837b86c19d9c4d6f84b157a7..376d199af7b38b6548e95b82b6592b8aac078181 100644 (file)
@@ -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 <director socket path>] [-f <users file>] [-h | -u] [<host>]" },
        { cmd_director_add, "director add",
          "[-a <director socket path>] [-t <tag>] <host> [<vhost count>]" },
+       { cmd_director_update, "director update",
+         "[-a <director socket path>] [-t <tag>] <host> <vhost count>" },
        { cmd_director_up, "director up",
          "[-a <director socket path>] <host>" },
        { cmd_director_down, "director down",