]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm director/penalty/who: Support also communicating via TCP sockets.
authorTimo Sirainen <tss@iki.fi>
Sun, 11 Jul 2010 23:33:12 +0000 (00:33 +0100)
committerTimo Sirainen <tss@iki.fi>
Sun, 11 Jul 2010 23:33:12 +0000 (00:33 +0100)
src/doveadm/doveadm-director.c
src/doveadm/doveadm-penalty.c
src/doveadm/doveadm-who.c
src/doveadm/doveadm.c
src/doveadm/doveadm.h

index da3d7dfdd3b430a0df1a4e840c4794499695bfee..25c512f2ca2b172db4e52d748d70ea87f9bbd01a 100644 (file)
@@ -32,9 +32,7 @@ static void director_connect(struct director_context *ctx)
        const char *line;
        int fd;
 
-       fd = net_connect_unix(ctx->socket_path);
-       if (fd == -1)
-               i_fatal("net_connect_unix(%s) failed: %m", ctx->socket_path);
+       fd = doveadm_connect(ctx->socket_path);
        net_set_nonblock(fd, FALSE);
 
        ctx->input = i_stream_create_fd(fd, (size_t)-1, TRUE);
index cddda375fda227639b380c04aa798a35e782a1f6..d2e068d0373dd0ec460d67d75ca372ff8e6c20b5 100644 (file)
@@ -68,9 +68,7 @@ static void penalty_lookup(struct penalty_context *ctx)
        const char *line;
        int fd;
 
-       fd = net_connect_unix(ctx->anvil_path);
-       if (fd == -1)
-               i_fatal("net_connect_unix(%s) failed: %m", ctx->anvil_path);
+       fd = doveadm_connect(ctx->anvil_path);
        net_set_nonblock(fd, FALSE);
 
        input = i_stream_create_fd(fd, (size_t)-1, TRUE);
index 461df2390ce81815e15b5daab96b22de436bcc38..30c638421a2c71ca67ceb35b9d64c9c8f35b38b7 100644 (file)
@@ -137,9 +137,7 @@ void who_lookup(struct who_context *ctx, who_callback_t *callback)
        const char *line;
        int fd;
 
-       fd = net_connect_unix(ctx->anvil_path);
-       if (fd == -1)
-               i_fatal("net_connect_unix(%s) failed: %m", ctx->anvil_path);
+       fd = doveadm_connect(ctx->anvil_path);
        net_set_nonblock(fd, FALSE);
 
        input = i_stream_create_fd(fd, (size_t)-1, TRUE);
index 60353bb75c4d752f834b7f248825a66de95657a9..595367533d226268b7a7a91841f9ef40684e7670 100644 (file)
@@ -5,6 +5,7 @@
 #include "str.h"
 #include "env-util.h"
 #include "execv-const.h"
+#include "network.h"
 #include "module-dir.h"
 #include "master-service.h"
 #include "master-service-settings.h"
@@ -17,6 +18,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <time.h>
+#include <sys/stat.h>
 
 bool doveadm_verbose = FALSE, doveadm_debug = FALSE;
 
@@ -148,6 +150,52 @@ const char *doveadm_plugin_getenv(const char *name)
        return NULL;
 }
 
+static bool
+parse_hostport(const char *str, const char **host_r, unsigned int *port_r)
+{
+       const char *p;
+
+       /* host:port */
+       p = strrchr(str, ':');
+       if (p == NULL || str_to_uint(p+1, port_r) < 0)
+               return FALSE;
+       *host_r = t_strdup_until(str, p);
+
+       /* there is any '/' character (unlikely to be found from host names),
+          assume ':' is part of a file path */
+       if (strchr(str, '/') != NULL)
+               return FALSE;
+       return TRUE;
+}
+
+int doveadm_connect(const char *path)
+{
+       struct stat st;
+       const char *host;
+       struct ip_addr *ips;
+       unsigned int port, ips_count;
+       int fd, ret;
+
+       if (parse_hostport(path, &host, &port) && stat(path, &st) < 0) {
+               /* it's a host:port, connect via TCP */
+               ret = net_gethostbyname(host, &ips, &ips_count);
+               if (ret != 0) {
+                       i_fatal("Lookup of host %s failed: %s",
+                               host, net_gethosterror(ret));
+               }
+               fd = net_connect_ip_blocking(&ips[0], port, NULL);
+               if (fd == -1) {
+                       i_fatal("connect(%s:%u) failed: %m",
+                               net_ip2addr(&ips[0]), port);
+               }
+       } else {
+               fd = net_connect_unix(path);
+               if (fd == -1)
+                       i_fatal("net_connect_unix(%s) failed: %m", path);
+       }
+       return fd;
+}
+
 static bool doveadm_has_subcommands(const char *cmd_name)
 {
        const struct doveadm_cmd *cmd;
index f5ab19a11b6016e24744cb7db459748ed1639e03..c62c4aab6e79f2fa594fda37c20da1594b3aa6c5 100644 (file)
@@ -34,6 +34,7 @@ void help(const struct doveadm_cmd *cmd) ATTR_NORETURN;
 
 const char *unixdate2str(time_t timestamp);
 const char *doveadm_plugin_getenv(const char *name);
+int doveadm_connect(const char *path);
 void doveadm_master_send_signal(int signo);
 
 void doveadm_register_director_commands(void);