bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "lib.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "ioloop.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "array.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "str.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "var-expand.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "wildcard-match.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "settings-parser.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "master-service.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "master-service-settings.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "auth-client.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "auth-master.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "auth-server-connection.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "master-auth.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "master-login-auth.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "mail-storage-service.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "mail-user.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "ostream.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "json-parser.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "doveadm.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include "doveadm-print.h"
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include <stdio.h>
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi#include <unistd.h>
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistruct authtest_input {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool_t pool;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *username;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *master_user;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *password;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct auth_user_info info;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi bool success;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct auth_client_request *request;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct master_auth_request master_auth_req;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int auth_id;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int auth_pid;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *auth_cookie;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi};
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic struct auth_master_connection *
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomidoveadm_get_auth_master_conn(const char *auth_socket_path)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi enum auth_master_flags flags = 0;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (doveadm_debug)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi flags |= AUTH_MASTER_FLAG_DEBUG;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return auth_master_init(auth_socket_path, flags);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic int
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomicmd_user_input(struct auth_master_connection *conn,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const struct authtest_input *input,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *show_field, bool userdb)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *lookup_name = userdb ? "userdb lookup" : "passdb lookup";
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool_t pool;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *updated_username = NULL, *const *fields, *p;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi int ret;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool = pool_alloconly_create("auth master lookup", 1024);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (userdb) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi ret = auth_master_user_lookup(conn, input->username, &input->info,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool, &updated_username, &fields);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi ret = auth_master_pass_lookup(conn, input->username, &input->info,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool, &fields);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (ret < 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *msg;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (fields[0] == NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi msg = t_strdup_printf("\"error\":\"%s failed\"",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi lookup_name);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi msg = t_strdup_printf("\"error\":\"%s failed: %s\"",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi lookup_name,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi fields[0]);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, msg);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi ret = -1;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else if (ret == 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi t_strdup_printf("\"error\":\"%s: user doesn't exist\"",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi lookup_name));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else if (show_field != NULL) {
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen size_t show_field_len = strlen(show_field);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi string_t *json_field = t_str_new(show_field_len+1);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(json_field, show_field);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, t_strdup_printf("\"%s\":", str_c(json_field)));
b544772a9912ad55484f0ab3f6caa81db0d37458Timo Sirainen for (; *fields != NULL; fields++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (strncmp(*fields, show_field, show_field_len) == 0 &&
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi (*fields)[show_field_len] == '=') {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi string_t *jsonval = t_str_new(32);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(jsonval, *fields + show_field_len + 1);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jsonval));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi string_t *jsonval = t_str_new(64);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"source\":\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, userdb ? "userdb\"" : "passdb\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (updated_username != NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, ",\"updated_username\":\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi str_truncate(jsonval, 0);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(jsonval, updated_username);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jsonval));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
b544772a9912ad55484f0ab3f6caa81db0d37458Timo Sirainen for (; *fields != NULL; fields++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *field = *fields;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (*field == '\0') continue;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi p = strchr(*fields, '=');
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi str_truncate(jsonval, 0);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (p != NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi field = t_strcut(*fields, '=');
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi str_truncate(jsonval, 0);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(jsonval, field);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, ",\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jsonval));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\":");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (p != NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi str_truncate(jsonval, 0);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(jsonval, p+1);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jsonval));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "true");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return ret;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void auth_user_info_parse(struct auth_user_info *info, const char *arg)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (strncmp(arg, "service=", 8) == 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi info->service = arg + 8;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi else if (strncmp(arg, "lip=", 4) == 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (net_addr2ip(arg + 4, &info->local_ip) < 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_fatal("lip: Invalid ip");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else if (strncmp(arg, "rip=", 4) == 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (net_addr2ip(arg + 4, &info->remote_ip) < 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_fatal("rip: Invalid ip");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else if (strncmp(arg, "lport=", 6) == 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (net_str2port(arg + 6, &info->local_port) < 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_fatal("lport: Invalid port number");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else if (strncmp(arg, "rport=", 6) == 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (net_str2port(arg + 6, &info->remote_port) < 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_fatal("rport: Invalid port number");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_fatal("Unknown -x argument: %s", arg);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomicmd_user_list(struct auth_master_connection *conn,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const struct authtest_input *input,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi char *const *users)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct auth_master_user_list_ctx *ctx;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *username, *user_mask = "*";
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int i;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (users[0] != NULL && users[1] == NULL)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi user_mask = users[0];
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
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 Tuomi for (i = 0; users[i] != NULL; i++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (wildcard_match_icase(username, users[i]))
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi break;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (users[i] != NULL)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi printf("%s\n", username);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (auth_master_user_list_deinit(&ctx) < 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_fatal("user listing failed");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void cmd_auth_cache_flush(int argc, char *argv[])
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *master_socket_path = NULL;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct auth_master_connection *conn;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int count;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi int c;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi while ((c = getopt(argc, argv, "a:")) > 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi switch (c) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi case 'a':
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi master_socket_path = optarg;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi break;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi default:
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_exit_code = EX_USAGE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi argv += optind;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (master_socket_path == NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi master_socket_path = t_strconcat(doveadm_settings->base_dir,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi "/auth-master", NULL);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
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 i_error("Cache flush failed");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_exit_code = EX_TEMPFAIL;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi } else {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_print_init("formatted");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_print_formatted_set_format("%{entries} cache entries flushed\n");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_print_header_simple("entries");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_print_num(count);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi auth_master_deinit(&conn);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void cmd_user_mail_input_field(const char *key, const char *value,
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi const char *show_field, bool *first)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi string_t *jvalue = t_str_new(128);
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 */
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi if (!*first)
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, ",");
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi *first = FALSE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(jvalue, key);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jvalue));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\":\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi str_truncate(jvalue, 0);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(jvalue, value);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(jvalue));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomicmd_user_mail_print_fields(const struct authtest_input *input,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct mail_user *user,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *const *userdb_fields,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *show_field)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const struct mail_storage_settings *mail_set;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *key, *value;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int i;
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi bool first = TRUE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi if (strcmp(input->username, user->username) != 0)
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);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi mail_set = mail_user_set_get_storage_set(user);
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi cmd_user_mail_input_field("mail", mail_set->mail_location, show_field, &first);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (userdb_fields != NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi for (i = 0; userdb_fields[i] != NULL; i++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi value = strchr(userdb_fields[i], '=');
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (value != NULL)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi key = t_strdup_until(userdb_fields[i], value++);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi else {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi key = userdb_fields[i];
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi value = "";
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (strcmp(key, "uid") != 0 &&
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi strcmp(key, "gid") != 0 &&
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi strcmp(key, "home") != 0 &&
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi strcmp(key, "mail") != 0 &&
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi *key != '\0') {
c560fa078cb67ceb48a7ac6e8d565d6f42827d83Aki Tuomi cmd_user_mail_input_field(key, value, show_field, &first);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic int
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomicmd_user_mail_input(struct mail_storage_service_ctx *storage_service,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const struct authtest_input *input,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *show_field, const char *expand_field)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct mail_storage_service_input service_input;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct mail_storage_service_user *service_user;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct mail_user *user;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *error, *const *userdb_fields;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool_t pool;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi int ret;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&service_input);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.module = "mail";
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.service = input->info.service;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.username = input->username;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.local_ip = input->info.local_ip;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.local_port = input->info.local_port;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.remote_ip = input->info.remote_ip;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.remote_port = input->info.remote_port;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi service_input.debug = input->info.debug;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool = pool_alloconly_create("userdb fields", 1024);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi mail_storage_service_save_userdb_fields(storage_service, pool,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi &userdb_fields);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if ((ret = mail_storage_service_lookup_next(storage_service, &service_input,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi &service_user, &user,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi &error)) <= 0) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool_unref(&pool);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (ret < 0)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return -1;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi string_t *username = t_str_new(32);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(username, input->username);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi t_strdup_printf("\"error\":\"userdb lookup: user %s doesn't exist\"", str_c(username))
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi );
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return 0;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (expand_field == NULL)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi cmd_user_mail_print_fields(input, user, userdb_fields, show_field);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi else {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi string_t *str = t_str_new(128);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen if (var_expand_with_funcs(str, expand_field,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen mail_user_var_expand_table(user),
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen mail_user_var_expand_func_table, user,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen &error) <= 0) {
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen string_t *str = t_str_new(128);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen str_printfa(str, "\"error\":\"Failed to expand field: ");
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen json_append_escaped(str, error);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen str_append_c(str, '"');
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend(doveadm_print_ostream, str_data(str), str_len(str));
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen } else {
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen string_t *value = t_str_new(128);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen json_append_escaped(value, expand_field);
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 str_truncate(value, 0);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen json_append_escaped(value, str_c(str));
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend_str(doveadm_print_ostream, str_c(value));
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen o_stream_nsend_str(doveadm_print_ostream, "\"");
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi mail_user_unref(&user);
eb318ea05532d2e54ed3bfc89bc15dcf1adae838Timo Sirainen mail_storage_service_user_unref(&service_user);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi pool_unref(&pool);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return 1;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic void cmd_user_ver2(struct doveadm_cmd_context *cctx)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char * const *optval;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi const char *auth_socket_path = NULL;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct auth_master_connection *conn;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi struct authtest_input input;
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 int ret;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
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
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
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&input);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (doveadm_cmd_param_array(cctx, "auth-info", &optval))
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi for(;*optval != NULL; optval++)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi auth_user_info_parse(&input.info, *optval);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (!doveadm_cmd_param_array(cctx, "user-mask", &optval)) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_exit_code = EX_USAGE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_error("No user(s) specified");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (expand_field != NULL && userdb_only) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_error("-e can't be used with -u");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_exit_code = EX_USAGE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (expand_field != NULL && show_field != NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi i_error("-e can't be used with -f");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_exit_code = EX_USAGE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi conn = doveadm_get_auth_master_conn(auth_socket_path);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi have_wildcards = FALSE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi for(const char *const *val = optval; *val != NULL; val++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (strchr(*val, '*') != NULL ||
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi strchr(*val, '?') != NULL) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi have_wildcards = TRUE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi break;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (have_wildcards) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi cmd_user_list(conn, &input, (char*const*)optval);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi auth_master_deinit(&conn);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi return;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (!userdb_only) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi storage_service = mail_storage_service_init(master_service, NULL,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP |
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_CHDIR |
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_LOG_INIT |
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_PLUGINS |
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_NAMESPACES |
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_RESTRICT_ACCESS);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi mail_storage_service_set_auth_conn(storage_service, conn);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi conn = NULL;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi string_t *json = t_str_new(64);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "{");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi input.info.local_ip = cctx->local_ip;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi input.info.local_port = cctx->local_port;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi input.info.remote_ip = cctx->remote_ip;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi input.info.remote_port = cctx->remote_port;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi for(const char *const *val = optval; *val != NULL; val++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi str_truncate(json, 0);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi json_append_escaped(json, *val);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi input.username = *val;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (first)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi first = FALSE;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi else
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, ",");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, str_c(json));
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "\"");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, ":{");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi ret = !userdb_only ?
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi cmd_user_mail_input(storage_service, &input, show_field, expand_field) :
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi cmd_user_input(conn, &input, show_field, TRUE);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream, "}");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi switch (ret) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi case -1:
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_exit_code = EX_TEMPFAIL;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi break;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi case 0:
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_exit_code = EX_NOUSER;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi break;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi o_stream_nsend_str(doveadm_print_ostream,"}");
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (storage_service != NULL)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi mail_storage_service_deinit(&storage_service);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi if (conn != NULL)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi auth_master_deinit(&conn);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistatic
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomistruct doveadm_cmd_ver2 doveadm_cmd_auth_server[] = {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .name = "auth cache flush",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .old_cmd = cmd_auth_cache_flush,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .usage = "[-a <master socket path>] [<user> [...]]",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAMS_START
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 TuomiDOVEADM_CMD_PARAMS_END
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi},
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .name = "user",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .cmd = cmd_user_ver2,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .usage = "[-a <userdb socket path>] [-x <auth info>] [-f field] [-e <value>] [-u] <user mask> [...]",
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi .flags = CMD_FLAG_NO_PRINT,
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki TuomiDOVEADM_CMD_PARAMS_START
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('f', "field", CMD_PARAM_STR, 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 TuomiDOVEADM_CMD_PARAMS_END
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi};
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomivoid doveadm_register_auth_server_commands(void)
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi{
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi unsigned int i;
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi for (i = 0; i < N_ELEMENTS(doveadm_cmd_auth_server); i++) {
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_cmd_register_ver2(&doveadm_cmd_auth_server[i]);
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi }
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi}