bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
8aa6fe58b1832da498b49de4383f9f1ef458945dTimo Sirainenconst struct doveadm_print_vfuncs *doveadm_print_vfuncs_all[] = {
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen enum fatal_exit_status fatal_status = *status;
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainendoveadm_usage_compress_lines(FILE *out, const char *str, const char *prefix)
bb1a7da5a76625640a5a207b19ed3abdb70c9617Timo Sirainen const char *cmd, *args, *p, *short_name, *sub_name;
bb1a7da5a76625640a5a207b19ed3abdb70c9617Timo Sirainen const char *prev_name = "", *prev_sub_name = "";
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen /* split lines */
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen lines = (void *)p_strsplit(pool_datastack_create(), str, "\n");
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen for (count = 0; lines[count] != NULL; count++) ;
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen /* sort lines */
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen i_qsort(lines, count, sizeof(*lines), i_strcmp_p);
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen /* print lines, compress subcommands into a single line */
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen for (i = 0; i < count; i++) {
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen fprintf(out, USAGE_CMDNAME_FMT" %s\n", cmd, args);
10635b60b6f7776cd4c1f364ae8dc94c3b9254ecPascal Volk fprintf(out, "usage: doveadm [-Dv] [-f <formatter>] ");
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen str_printfa(str, "%s\t%s\n", cmd->name, cmd->short_usage);
bef3447d29acf374bbae54bc7d6bba2583c6d5eaTimo Sirainen str_printfa(str, "%s\t%s\n", cmd2->name, cmd2->usage);
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen doveadm_usage_compress_lines(out, str_c(str), prefix);
9ce62fcb795a4bb57f1c003fc8cbd63bff6e5463Timo Sirainenhelp_to(const struct doveadm_cmd *cmd, FILE *out)
9f3bb0e10835efb0c9b1eb9e09e16b614ec41b97Timo Sirainen fprintf(out, "doveadm %s %s\n", cmd->name, cmd->short_usage);
14af7be4aa26d55c341cd6efe32bb2add2c39830Aki Tuomihelp_to_ver2(const struct doveadm_cmd_ver2 *cmd, FILE *out)
14af7be4aa26d55c341cd6efe32bb2add2c39830Aki Tuomi fprintf(out, "doveadm %s %s\n", cmd->name, cmd->usage);
28b8434ca4cba2e310d13ffc55e895d658725f43Timo Sirainenstatic void cmd_help(int argc ATTR_UNUSED, char *argv[])
28b8434ca4cba2e310d13ffc55e895d658725f43Timo Sirainen man_argv[1] = t_strconcat("doveadm-", argv[1], NULL);
a3a55999bcfe2e57941cb64343f4ea80beabdab7Timo Sirainenstatic void cmd_config(int argc ATTR_UNUSED, char *argv[])
06af65f82453bb976cf8aa6fe2507e3a6253a04fTimo Sirainen env_put(t_strconcat(MASTER_CONFIG_FILE_ENV"=",
06af65f82453bb976cf8aa6fe2507e3a6253a04fTimo Sirainen master_service_get_config_path(master_service), NULL));
a3a55999bcfe2e57941cb64343f4ea80beabdab7Timo Sirainenstatic struct doveadm_cmd doveadm_cmd_config = {
cc52f19439f17c03e37fd65c6299a77d5c5e638aTimo Sirainenstatic void cmd_exec(int argc ATTR_UNUSED, char *argv[]);
cc52f19439f17c03e37fd65c6299a77d5c5e638aTimo Sirainen cmd_exec, "exec", "<binary> [binary parameters]"
f2a1955d993f67982bc40ad7bbae9a036dabfd64Timo Sirainenstatic void cmd_exec(int argc ATTR_UNUSED, char *argv[])
f2a1955d993f67982bc40ad7bbae9a036dabfd64Timo Sirainen path = t_strdup_printf("%s/%s", doveadm_settings->libexec_dir, binary);
36a052b7bd94ccb47abbb6b15c1380f03780ba20Timo Sirainenstatic bool doveadm_try_run(const char *cmd_name, int argc,
36a052b7bd94ccb47abbb6b15c1380f03780ba20Timo Sirainen const char *const argv[])
7d500ecf27acc5b65615ee9e72d6da6bacf799d2Timo Sirainen cmd = doveadm_cmd_find_with_args(cmd_name, &argc, &argv);
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainenstatic bool doveadm_has_subcommands(const char *cmd_name)
bef3447d29acf374bbae54bc7d6bba2583c6d5eaTimo Sirainen if (strncmp(cmd2->name, cmd_name, len) == 0 &&
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen return doveadm_mail_has_subcommands(cmd_name);
968b2f633b7405bc2cf0596d04762994ceb279d0Timo Sirainen static const struct setting_parser_info *set_roots[] = {
968b2f633b7405bc2cf0596d04762994ceb279d0Timo Sirainen if (master_service_settings_read(master_service, &input,
968b2f633b7405bc2cf0596d04762994ceb279d0Timo Sirainen i_fatal("Error reading configuration: %s", error);
2e533fb1283b5f06a4063b519e47f1861c910386Timo Sirainen service_set = master_service_settings_get(master_service);
2e533fb1283b5f06a4063b519e47f1861c910386Timo Sirainen service_set = settings_dup(&master_service_setting_parser_info,
6c40413c6e64fde2d977cd6076afa97fa57b67fcTimo Sirainen doveadm_verbose_proctitle = service_set->verbose_proctitle;
d5ef38077adbff5b3e4d0b3c94a2057581dc78b6Timo Sirainen set = master_service_settings_get_others(master_service)[0];
d5ef38077adbff5b3e4d0b3c94a2057581dc78b6Timo Sirainen doveadm_settings = settings_dup(&doveadm_setting_parser_info, set,
afd6d387ea65843b59fb6051fb567719d2a5279cAki Tuomi doveadm_settings->parsed_features = set->parsed_features; /* copy this value by hand */
55accf49e32ff93bfdd92961cb54ccc8c329147cTimo Sirainenstatic struct doveadm_cmd *doveadm_cmdline_commands[] = {
ff3337516aad9843599905aeeb29812ea67c09d1Timo Sirainen unsigned int i;
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen i_set_failure_exit_callback(failure_exit_callback);
5296198635718c9bf5b2f972c9d5be52092d3d58Timo Sirainen /* "+" is GNU extension to stop at the first non-option.
5296198635718c9bf5b2f972c9d5be52092d3d58Timo Sirainen others just accept -+ option. */
a672f99363d5f37060c1331d00d2ee3c4626310fTimo Sirainen master_service = master_service_init("doveadm", service_flags,
5296198635718c9bf5b2f972c9d5be52092d3d58Timo Sirainen while ((c = master_getopt(master_service)) > 0) {
4a26584a87ee0e986d23a224b3b3e85c44254d7fTimo Sirainen if (cmd_name != NULL && strcmp(cmd_name, "help") == 0 &&
4a26584a87ee0e986d23a224b3b3e85c44254d7fTimo Sirainen /* "help cmd" doesn't need any configuration */
851404049fe55a02fb69d7c2b4d851d2d0fd4b8dTimo Sirainen master_service_init_log(master_service, "doveadm: ");
55accf49e32ff93bfdd92961cb54ccc8c329147cTimo Sirainen for (i = 0; i < N_ELEMENTS(doveadm_cmdline_commands); i++)
55accf49e32ff93bfdd92961cb54ccc8c329147cTimo Sirainen doveadm_register_cmd(doveadm_cmdline_commands[i]);
6ef83bcdc4e40d6b387857e5f7d58cd86c71ec50Timo Sirainen doveadm_cmd_register_ver2(&doveadm_cmd_oldstats_top_ver2);
a2857829c642e2671779576b00c37b7d04693731Timo Sirainen /* special case commands: even if there is something wrong
a2857829c642e2671779576b00c37b7d04693731Timo Sirainen with the config (e.g. mail_plugins), don't fail these
d3c2ca49bbac9f5eea8038a9016aaf68679ba0c5Timo Sirainen master_service_init_stats_client(master_service, TRUE);
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvi doveadm_print_ostream = o_stream_create_fd(STDOUT_FILENO, 0);
57591c5ea045b6829ebcfed9b145d719d63f935aTimo Sirainen o_stream_set_no_error_handling(doveadm_print_ostream, TRUE);
a2857829c642e2671779576b00c37b7d04693731Timo Sirainen /* show usage after registering all plugins */
aee3e2f7ab2b27572a90b9e7fd8fe60f13c6637eTimo Sirainen /* disable debugging unless -D is given */
619994941f9d9e8e2d108eb3f9f9821a35f60caeAki Tuomi /* this has to be done here because proctitle hack can break
619994941f9d9e8e2d108eb3f9f9821a35f60caeAki Tuomi the env pointer */
aaa1b6bb4cd2d7f8f4e7977d61176ea1c8f7e32bAki Tuomi if (!doveadm_cmd_try_run_ver2(cmd_name, argc, (const char**)argv, &cctx) &&
79bbb900ffba886779474dfb04c41408f0ba0602Timo Sirainen !doveadm_try_run(cmd_name, argc, (const char **)argv) &&
bd417d416988d11a6b555b9aa57779e7ed976951Timo Sirainen !doveadm_mail_try_run(cmd_name, argc, argv)) {
10515cb90514b169ab6c3693c72c4bf1017476dbTimo Sirainen i_fatal("Unknown command '%s', but plugin %s exists. "
10515cb90514b169ab6c3693c72c4bf1017476dbTimo Sirainen "Try to set mail_plugins=%s",