bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomidoveadm_get_auth_master_conn(const char *auth_socket_path)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomicmd_user_input(struct auth_master_connection *conn,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *lookup_name = userdb ? "userdb lookup" : "passdb lookup";
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *updated_username = NULL, *const *fields, *p;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool = pool_alloconly_create("auth master lookup", 1024);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi ret = auth_master_user_lookup(conn, input->username, &input->info,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi ret = auth_master_pass_lookup(conn, input->username, &input->info,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi msg = t_strdup_printf("\"error\":\"%s failed: %s\"",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else if (ret == 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi t_strdup_printf("\"error\":\"%s: user doesn't exist\"",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi string_t *json_field = t_str_new(show_field_len+1);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, t_strdup_printf("\"%s\":", str_c(json_field)));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (strncmp(*fields, show_field, show_field_len) == 0 &&
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(jsonval, *fields + show_field_len + 1);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jsonval));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"source\":\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, userdb ? "userdb\"" : "passdb\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, ",\"updated_username\":\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jsonval));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jsonval));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jsonval));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "true");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void auth_user_info_parse(struct auth_user_info *info, const char *arg)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (net_str2port(arg + 6, &info->remote_port) < 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi char *const *users)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int i;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi ctx = auth_master_user_list_init(conn, user_mask, &input->info);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi while ((username = auth_master_user_list_next(ctx)) != NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void cmd_auth_cache_flush(int argc, char *argv[])
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi switch (c) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi master_socket_path = t_strconcat(doveadm_settings->base_dir,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi conn = doveadm_get_auth_master_conn(master_socket_path);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (auth_master_cache_flush(conn, (void *)argv, &count) < 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_print_formatted_set_format("%{entries} cache entries flushed\n");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void cmd_user_mail_input_field(const char *key, const char *value,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (show_field != NULL && strcmp(show_field, key) != 0) return;
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi /* do not emit comma on first field. we need to keep track
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi of when the first field actually gets printed as it
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi might change due to show_field */
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jvalue));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\":\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jvalue));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomicmd_user_mail_print_fields(const struct authtest_input *input,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *const *userdb_fields,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int i;
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi cmd_user_mail_input_field("user", user->username, show_field, &first);
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi cmd_user_mail_input_field("uid", user->set->mail_uid, show_field, &first);
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi cmd_user_mail_input_field("gid", user->set->mail_gid, show_field, &first);
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi cmd_user_mail_input_field("home", user->set->mail_home, show_field, &first);
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi cmd_user_mail_input_field("mail", mail_set->mail_location, show_field, &first);
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi cmd_user_mail_input_field(key, value, show_field, &first);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomicmd_user_mail_input(struct mail_storage_service_ctx *storage_service,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.local_port = input->info.local_port;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.remote_port = input->info.remote_port;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool = pool_alloconly_create("userdb fields", 1024);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi mail_storage_service_save_userdb_fields(storage_service, pool,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if ((ret = mail_storage_service_lookup_next(storage_service, &service_input,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi t_strdup_printf("\"error\":\"userdb lookup: user %s doesn't exist\"", str_c(username))
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi cmd_user_mail_print_fields(input, user, userdb_fields, show_field);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen str_printfa(str, "\"error\":\"Failed to expand field: ");
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend(doveadm_print_ostream, str_data(str), str_len(str));
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend_str(doveadm_print_ostream, "\"");
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend_str(doveadm_print_ostream, str_c(value));
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend_str(doveadm_print_ostream, "\":\"");
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend_str(doveadm_print_ostream, str_c(value));
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend_str(doveadm_print_ostream, "\"");
eb318ea05532d2e54ed3bfc89bc15dcf1adae838Timo Sirainen mail_storage_service_user_unref(&service_user);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void cmd_user_ver2(struct doveadm_cmd_context *cctx)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *show_field = NULL, *expand_field = NULL;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct mail_storage_service_ctx *storage_service = NULL;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi bool have_wildcards, userdb_only = FALSE, first = TRUE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (!doveadm_cmd_param_str(cctx, "socket-path", &auth_socket_path))
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi auth_socket_path = doveadm_settings->auth_socket_path;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi (void)doveadm_cmd_param_str(cctx, "expand-field", &expand_field);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi (void)doveadm_cmd_param_str(cctx, "field", &show_field);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi (void)doveadm_cmd_param_bool(cctx, "userdb-only", &userdb_only);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (doveadm_cmd_param_array(cctx, "auth-info", &optval))
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (!doveadm_cmd_param_array(cctx, "user-mask", &optval)) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi conn = doveadm_get_auth_master_conn(auth_socket_path);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi for(const char *const *val = optval; *val != NULL; val++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi storage_service = mail_storage_service_init(master_service, NULL,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi mail_storage_service_set_auth_conn(storage_service, conn);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi for(const char *const *val = optval; *val != NULL; val++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(json));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi cmd_user_mail_input(storage_service, &input, show_field, expand_field) :
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistruct doveadm_cmd_ver2 doveadm_cmd_auth_server[] = {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .usage = "[-a <master socket path>] [<user> [...]]",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAM('a', "socket-path", CMD_PARAM_STR, 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAM('\0', "user", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .usage = "[-a <userdb socket path>] [-x <auth info>] [-f field] [-e <value>] [-u] <user mask> [...]",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAM('a', "socket-path", CMD_PARAM_STR, 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAM('x', "auth-info", CMD_PARAM_ARRAY, 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAM('e', "expand-field", CMD_PARAM_STR, 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAM('u', "userdb-only", CMD_PARAM_BOOL, 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAM('\0', "user-mask", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int i;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi for (i = 0; i < N_ELEMENTS(doveadm_cmd_auth_server); i++) {