doveadm-dsync.c revision 956b8eea7ae479a38b25175447fc8eac2df30480
/* Copyright (c) 2009-2013 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "lib-signals.h"
#include "array.h"
#include "execv-const.h"
#include "fd-set-nonblock.h"
#include "istream.h"
#include "ostream.h"
#include "iostream-ssl.h"
#include "iostream-rawlog.h"
#include "write-full.h"
#include "str.h"
#include "strescape.h"
#include "var-expand.h"
#include "settings-parser.h"
#include "master-service.h"
#include "master-service-ssl-settings.h"
#include "mail-storage-service.h"
#include "mail-user.h"
#include "mail-namespace.h"
#include "mailbox-list-private.h"
#include "doveadm-settings.h"
#include "doveadm-mail.h"
#include "doveadm-print.h"
#include "doveadm-server.h"
#include "client-connection.h"
#include "server-connection.h"
#include "dsync-brain.h"
#include "dsync-ibc.h"
#include "doveadm-dsync.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#define DSYNC_COMMON_GETOPT_ARGS "+1dEfg:l:m:n:Nr:Rs:Ux:"
#define DSYNC_REMOTE_CMD_EXIT_WAIT_SECS 30
/* The broken_char is mainly set to get a proper error message when trying to
convert a mailbox with a name that can't be used properly translated between
vname/storage_name and would otherwise be mixed up with a normal "mailbox
doesn't exist" error message. This could be any control character, since
none of them are allowed to be created in regular mailbox names. */
#define DSYNC_LIST_BROKEN_CHAR '\003'
enum dsync_run_type {
};
struct dsync_cmd_context {
struct doveadm_mail_cmd_context ctx;
const char *mailbox, *namespace_prefix;
const char *state_input, *rawlog_path;
const char *remote_name;
const char *local_location;
struct ssl_iostream_context *ssl_ctx;
struct ssl_iostream *ssl_iostream;
enum dsync_run_type run_type;
struct server_connection *tcp_conn;
const char *error;
unsigned int lock_timeout;
unsigned int lock:1;
unsigned int sync_visible_namespaces:1;
unsigned int default_replica_location:1;
unsigned int oneway:1;
unsigned int backup:1;
unsigned int reverse_backup:1;
unsigned int remote_user_prefix:1;
unsigned int no_mail_sync:1;
unsigned int local_location_from_arg:1;
unsigned int replicator_notify:1;
};
static bool legacy_dsync = FALSE;
{
const unsigned char *data;
const char *line;
case -2:
break;
case -1:
break;
default:
break;
}
}
static void
{
i_fatal("pipe() failed: %m");
switch (ctx->remote_pid) {
case -1:
i_fatal("fork() failed: %m");
case 0:
goes to pipes which we'll pass to proxy client. */
i_fatal("dup2() failed: %m");
i_close_fd(&fd_in[0]);
i_close_fd(&fd_out[0]);
i_close_fd(&fd_err[0]);
default:
/* parent */
break;
}
i_close_fd(&fd_in[0]);
if (ctx->remote_user_prefix) {
const char *prefix =
i_fatal("write(remote out) failed: %m");
}
}
static void
mirror_get_remote_cmd_line(const char *const *argv,
const char *const **cmd_args_r)
{
unsigned int i;
const char *p;
p = argv[i];
}
if (legacy_dsync) {
/* we're executing dsync */
p = "server";
} else {
/* we're executing doveadm */
p = "dsync-server";
}
}
static const char *const *
{
static struct var_expand_table static_tab[] = {
};
struct var_expand_table *tab;
else {
/* some automation: if parameter's all %variables
expand to empty, but the %variable isn't the only
text in the parameter, skip it. */
str_truncate(str, 0);
str_truncate(str2, 0);
continue;
}
}
}
const char *user,
const char *const **cmd_args_r)
{
/* more than one parameter, so it contains a full command
(e.g. ssh host dsync) */
return TRUE;
}
/* if it begins with /[a-z0-9]+:/, it's a mail location
(e.g. mdbox:~/mail) */
for (p = argv[0]; *p != '\0'; p++) {
if (!i_isalnum(*p)) {
if (*p == ':')
return FALSE;
break;
}
}
/* a) the whole command is in one string. this is mainly for
backwards compatibility.
return TRUE;
}
/* [user@]host */
else
/* we'll assume virtual users, so in user@host it really means not to
give ssh a username, but to give dsync -u user parameter. */
return TRUE;
}
{
struct mail_namespace *ns;
}
enum mailbox_list_path_type type)
{
}
static int
bool *changes_during_sync_r)
{
struct dsync_brain *brain2;
struct setting_parser_context *set_parser;
int ret;
if (ctx->local_location_from_arg)
else {
}
/* update mail_location and create another user for the
second location. */
i_unreached();
if (ret < 0) {
return -1;
}
i_error("Mail locations must use the same "
"virtual mailbox hierarchy separator "
"(specify separator for the default namespace)");
return -1;
}
i_error("Both source and destination mail_location "
"points to same directory: %s",
return -1;
}
while (brain1_running || brain2_running) {
if (dsync_brain_has_failed(brain) ||
break;
}
if (dsync_brain_deinit(&brain2) < 0) {
return -1;
}
return 0;
}
int *status_r)
{
/* wait for the remote command to finish to see any final errors.
don't wait very long though. */
i_error("waitpid(%ld) failed: %m",
(long)ctx->remote_pid);
} else {
i_error("Remote command process isn't dying, killing it");
i_error("kill(%ld, SIGKILL) failed: %m",
(long)ctx->remote_pid);
}
}
*status_r = -1;
}
}
{
if (status == -1)
;
else if (WIFSIGNALED(status))
/* remote most likely already logged the error.
don't bother logging another line about it */
} else if (WEXITSTATUS(status) != 0) {
}
}
{
}
static const char *const *
{
else {
login = "";
}
}
static struct dsync_ibc *
const char *name, const char *temp_prefix)
{
} else {
}
}
name, temp_prefix);
}
static void
const char *state_str)
{
#define REPLICATOR_HANDSHAKE "VERSION\treplicator-doveadm-client\t1\t0\n"
const char *path;
int fd;
if (fd == -1) {
/* replicator not running on this server. ignore. */
return;
}
return;
}
if (sync_type == DSYNC_BRAIN_SYNC_TYPE_FULL)
/* we only wanted to notify replicator. we don't care enough about the
answer to wait for it. */
}
static int
{
struct dsync_brain *brain;
struct dsync_brain_settings set;
enum dsync_brain_flags brain_flags;
bool remote_errors_logged = FALSE;
bool changes_during_sync = FALSE;
/* array is NULL-terminated in init() */
}
}
else {
str_c(temp_prefix));
}
}
if (ctx->sync_visible_namespaces)
if (ctx->reverse_backup)
if (ctx->no_mail_sync)
if (doveadm_debug)
brain_flags, &set);
&changes_during_sync) < 0)
ret = -1;
} else {
}
}
i_warning("Mailbox changes caused a desync. "
"You may want to run dsync again.");
}
if (dsync_brain_deinit(&brain) < 0) {
ret = -1;
}
}
/* print any final errors after the process has died. not closing
shouldn't (at least with ssh) and we need stderr to be open to be
able to print the final errors */
}
return ret;
}
void *context)
{
switch (exit_code) {
case 0:
break;
"Disconnected from remote: %s", error);
break;
case EX_NOUSER:
break;
default:
"Failed to start dsync-server command: %u", exit_code);
break;
}
}
const struct mail_storage_settings *mail_set,
const char **error_r)
{
struct ssl_iostream_settings ssl_set;
return 0;
}
static int
const struct mail_storage_settings *mail_set,
{
struct doveadm_server *server;
struct server_connection *conn;
const char *error;
if (ssl) {
"Couldn't initialize SSL context: %s", error);
return -1;
}
}
ioloop = io_loop_create();
*error_r = "Couldn't create server connection";
return -1;
}
/* <flags> <username> <command> [<args>] */
if (doveadm_debug)
if (ctx->replicator_notify)
return -1;
}
return 0;
}
static int
const struct mail_storage_settings *mail_set,
const char *location,
const char *const **remote_cmd_args_r, const char **error_r)
{
/* TCP connection to remote dsync */
}
/* TCP+SSL connection to remote dsync */
}
/* this is a remote (ssh) command */
/* this is a remote (ssh) command with a "user\n"
prefix sent before dsync actually starts */
} else {
/* local with e.g. maildir:path */
return 0;
}
return 0;
}
struct mail_storage_service_user *service_user,
const char **error_r)
{
const char *const *remote_cmd_args = NULL;
const struct mail_user_settings *user_set;
const struct mail_storage_settings *mail_set;
const char *username = "";
if (ctx->default_replica_location) {
*error_r = "User has no mail_replica in userdb";
return -1;
}
} else {
/* if we're executing remotely, give -u parameter if we also
did a userdb lookup. */
/* it's a mail_location */
}
}
&remote_cmd_args, error_r) < 0)
return -1;
}
if (remote_cmd_args != NULL) {
/* do this before mail_storage_service_next() in case it
drops process privileges */
}
if (ctx->sync_visible_namespaces &&
i_fatal("-N parameter requires syncing with remote host");
return 0;
}
const char *const args[])
{
if (ctx->default_replica_location) {
i_error("Don't give mail location with -d parameter");
} else {
}
}
{
}
static bool
{
const char *str;
switch (c) {
case '1':
break;
case 'd':
break;
case 'E':
/* dsync wrapper detection flag */
legacy_dsync = TRUE;
break;
case 'f':
break;
case 'g':
if (optarg[0] == '\0')
break;
case 'l':
break;
case 'm':
if (optarg[0] == '\0')
else
break;
case 'x':
break;
case 'n':
break;
case 'N':
break;
case 'r':
break;
case 'R':
break;
case 's':
*optarg != '\0')
break;
case 'U':
break;
default:
return FALSE;
}
return TRUE;
}
static struct doveadm_mail_cmd_context *cmd_dsync_alloc(void)
{
struct dsync_cmd_context *ctx;
}
static struct doveadm_mail_cmd_context *cmd_dsync_backup_alloc(void)
{
struct doveadm_mail_cmd_context *_ctx;
struct dsync_cmd_context *ctx;
_ctx = cmd_dsync_alloc();
return _ctx;
}
static int
{
struct dsync_brain *brain;
const char *name;
/* doveadm-server connection. start with a success reply.
after that follows the regular dsync protocol. */
} else {
/* the log messages go via stderr to the remote dsync,
so the names are reversed */
name = "local";
}
if (ctx->replicator_notify) {
}
if (dsync_brain_deinit(&brain) < 0)
/* make sure nothing more is written by the generic doveadm
connection code */
}
}
static bool
{
switch (c) {
case 'E':
/* dsync wrapper detection flag */
legacy_dsync = TRUE;
break;
case 'r':
break;
case 'U':
break;
default:
return FALSE;
}
return TRUE;
}
static struct doveadm_mail_cmd_context *cmd_dsync_server_alloc(void)
{
struct dsync_cmd_context *ctx;
}
struct doveadm_mail_cmd cmd_dsync_mirror = {
cmd_dsync_alloc, "sync",
"[-1dfR] [-l <secs>] [-r <rawlog path>] [-m <mailbox>] [-n <namespace> | -N] [-x <exclude>] [-s <state>] <dest>"
};
struct doveadm_mail_cmd cmd_dsync_backup = {
cmd_dsync_backup_alloc, "backup",
"[-dfR] [-l <secs>] [-r <rawlog path>] [-m <mailbox>] [-n <namespace> | -N] [-x <exclude>] [-s <state>] <dest>"
};
struct doveadm_mail_cmd cmd_dsync_server = {
};
{
const char *getopt_str;
bool dsync_server = FALSE;
return;
/* @UNSAFE: this is called when the "doveadm" binary is called as
"dsync" (for backwards compatibility) */
dest = 1;
/* add global doveadm flags */
break;
case 'C':
break;
case 'f':
break;
case 'R':
break;
case 'm':
break;
case 'u':
break;
default:
break;
}
}
if (j > 1) {
dup[j++] = '\0';
}
if (flag_m) {
i_fatal("-m missing parameter");
}
if (flag_u) {
i_fatal("-u missing parameter");
}
if (flag_C) {
i_fatal("-C missing parameter");
}
}
}
/* mirror|backup|server */
i_fatal("Missing mirror or backup parameter");
/* we're re-executing dsync due to doveconf.
"backup" re-exec detection is later. */
return;
}
dsync_server = TRUE;
} else
/* we're re-executing dsync due to doveconf */
return;
}
/* dsync flags */
new_flags[0] = '-';
if (!dsync_server) {
if (flag_f)
new_flags[i++] = 'f';
if (flag_R)
new_flags[i++] = 'R';
new_flags[i++] = 'm';
}
new_flags[i] = '\0';
if (i > 1) {
}
}
/* rest of the parameters */
legacy_dsync = TRUE;
optind = 1;
}