From: Timo Sirainen Date: Thu, 11 Aug 2022 19:16:03 +0000 (+0300) Subject: doveadm: Move username printing from server side to client side X-Git-Tag: 2.4.0~3665 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=704de5e46819082ccc552301693067da53057f22;p=thirdparty%2Fdovecot%2Fcore.git doveadm: Move username printing from server side to client side The client requests output for only a single user from the server side, so it makes sense that the client is the one printing the username. This also changes sticky headers to be usable only for the username. Unfortunately it's not easy to remove support for sticky header entirely, because the username must not be printed before the first proper doveadm_print() call. For example if "doveadm fetch" doesn't find any mails, it shouldn't print the username either. --- diff --git a/src/doveadm/doveadm-mail-server.c b/src/doveadm/doveadm-mail-server.c index feb430835f..0b48279798 100644 --- a/src/doveadm/doveadm-mail-server.c +++ b/src/doveadm/doveadm-mail-server.c @@ -44,6 +44,7 @@ struct doveadm_mail_server_cmd { char *cmdline; struct istream *input; bool streaming; + bool print_username; }; struct doveadm_server_request { @@ -51,6 +52,7 @@ struct doveadm_server_request { struct doveadm_server *server; struct doveadm_client_settings set; const char *username; + bool print_username; }; static HASH_TABLE(char *, struct doveadm_server *) servers; @@ -63,7 +65,8 @@ static void doveadm_cmd_callback(const struct doveadm_server_reply *reply, void *context); static void doveadm_mail_server_handle(struct doveadm_server *server, struct doveadm_client *conn, - const char *username); + const char *username, + bool print_username); static void doveadm_server_request_free(struct doveadm_server_request *request) { @@ -462,6 +465,10 @@ doveadm_cmd_print_callback(const unsigned char *data, struct doveadm_mail_server_cmd *servercmd) { string_t *str = t_str_new(size); + if (servercmd->print_username) { + doveadm_print_sticky("username", servercmd->username); + servercmd->print_username = FALSE; + } if (!finished) { servercmd->streaming = TRUE; str_append_tabunescaped(str, data, size); @@ -533,7 +540,8 @@ static void doveadm_cmd_callback(const struct doveadm_server_reply *reply, array_delete(&doveadm_server_request_queue, idx, 1); doveadm_mail_server_handle(server, conn, - request_copy.username); + request_copy.username, + request_copy.print_username); doveadm_server_request_free(&request_copy); doveadm_client_unref(&conn); break; @@ -544,7 +552,8 @@ static void doveadm_cmd_callback(const struct doveadm_server_reply *reply, static void doveadm_mail_server_handle(struct doveadm_server *server, struct doveadm_client *conn, - const char *username) + const char *username, + bool print_username) { struct doveadm_mail_server_cmd *servercmd; string_t *cmd; @@ -574,6 +583,7 @@ static void doveadm_mail_server_handle(struct doveadm_server *server, servercmd->username = i_strdup(username); servercmd->cmdline = i_strdup(str_c(cmd)); servercmd->input = cmd_ctx->cmd_input; + servercmd->print_username = print_username; if (servercmd->input != NULL) i_stream_ref(servercmd->input); doveadm_client_set_print(conn, doveadm_cmd_print_callback, @@ -602,7 +612,8 @@ static int doveadm_mail_server_request_queue_handle_next(const char **error_r) return -1; } doveadm_mail_server_handle(request_copy.server, conn, - request_copy.username); + request_copy.username, + request_copy.print_username); doveadm_server_request_free(&request_copy); doveadm_client_unref(&conn); return 0; @@ -727,6 +738,8 @@ int doveadm_mail_server_user(struct doveadm_mail_cmd_context *ctx, struct doveadm_server_request *request; struct auth_proxy_settings proxy_set; const char *server_name, *socket_path, *referral; + bool print_username = + doveadm_print_is_initialized() && !ctx->iterate_single_user; int ret; i_assert(cmd_ctx == ctx || cmd_ctx == NULL); @@ -756,10 +769,6 @@ int doveadm_mail_server_user(struct doveadm_mail_cmd_context *ctx, ctx->cctx->proxy_redirect_reauth = proxy_set.redirect_reauth; - /* server sends the sticky headers for each row as well, - so undo any sticks we might have added already */ - doveadm_print_unstick_headers(); - struct doveadm_client_settings conn_set = { .socket_path = socket_path, .hostname = proxy_set.host, @@ -804,7 +813,8 @@ int doveadm_mail_server_user(struct doveadm_mail_cmd_context *ctx, return -1; } else { doveadm_mail_server_handle(server, conn, - proxy_set.username); + proxy_set.username, + print_username); doveadm_client_unref(&conn); } } else { @@ -812,6 +822,7 @@ int doveadm_mail_server_user(struct doveadm_mail_cmd_context *ctx, request->pool = pool_alloconly_create("doveadm server request", 256); request->server = server; request->username = p_strdup(request->pool, proxy_set.username); + request->print_username = print_username; doveadm_client_settings_dup(&conn_set, &request->set, request->pool); } *error_r = "doveadm server failure"; diff --git a/src/doveadm/doveadm-mail.c b/src/doveadm/doveadm-mail.c index 87a62c7898..b31b3a859a 100644 --- a/src/doveadm/doveadm-mail.c +++ b/src/doveadm/doveadm-mail.c @@ -447,6 +447,9 @@ doveadm_mail_next_user(struct doveadm_mail_cmd_context *ctx, return ret; } + if (doveadm_print_is_initialized() && !ctx->iterate_single_user) + doveadm_print_sticky("username", cctx->username); + if (ctx->v.prerun != NULL) { if (ctx->v.prerun(ctx, ctx->cur_service_user, error_r) < 0) { mail_storage_service_user_unref(&ctx->cur_service_user); @@ -525,7 +528,6 @@ doveadm_mail_all_users(struct doveadm_mail_cmd_context *ctx, continue; } cctx->username = user; - doveadm_print_sticky("username", user); T_BEGIN { ret = doveadm_mail_next_user(ctx, &error); if (ret < 0) @@ -633,8 +635,7 @@ doveadm_mail_cmd_exec(struct doveadm_mail_cmd_context *ctx, ctx->iterate_single_user = !ctx->iterate_all_users && wildcard_user == NULL; - if (doveadm_print_is_initialized() && - (!ctx->iterate_single_user || ctx->add_username_header)) { + if (doveadm_print_is_initialized() && !ctx->iterate_single_user) { doveadm_print_header("username", "Username", DOVEADM_PRINT_HEADER_FLAG_STICKY | DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE); @@ -648,8 +649,6 @@ doveadm_mail_cmd_exec(struct doveadm_mail_cmd_context *ctx, ctx->service_flags |= MAIL_STORAGE_SERVICE_FLAG_TEMP_PRIV_DROP; } - if (ctx->add_username_header) - doveadm_print_sticky("username", cctx->username); ret = doveadm_mail_single_user(ctx, &error); if (ret < 0) { /* user lookup/init failed somehow */ @@ -812,9 +811,7 @@ doveadm_cmdv2_wrapper_parse_common_options(struct doveadm_mail_cmd_context *mctx const char *value_str; if (doveadm_cmd_param_flag(cctx, "all-users")) { - if (tcp_server) - mctx->add_username_header = TRUE; - else + if (!tcp_server) mctx->iterate_all_users = TRUE; } @@ -837,9 +834,7 @@ doveadm_cmdv2_wrapper_parse_common_options(struct doveadm_mail_cmd_context *mctx if (strchr(value_str, '*') != NULL || strchr(value_str, '?') != NULL) { - if (tcp_server) - mctx->add_username_header = TRUE; - else { + if (!tcp_server) { *wildcard_user_r = value_str; cctx->username = NULL; } diff --git a/src/doveadm/doveadm-mail.h b/src/doveadm/doveadm-mail.h index c4d2ee19f7..d879ef47f1 100644 --- a/src/doveadm/doveadm-mail.h +++ b/src/doveadm/doveadm-mail.h @@ -94,8 +94,6 @@ struct doveadm_mail_cmd_context { bool iterate_single_user:1; /* We're going through all users (not set for wildcard usernames) */ bool iterate_all_users:1; - /* Add username header to all replies */ - bool add_username_header:1; }; struct doveadm_mail_cmd { diff --git a/src/doveadm/doveadm-print.h b/src/doveadm/doveadm-print.h index 2d610e8255..5f9540a010 100644 --- a/src/doveadm/doveadm-print.h +++ b/src/doveadm/doveadm-print.h @@ -11,6 +11,9 @@ enum doveadm_print_header_flags { DOVEADM_PRINT_HEADER_FLAG_RIGHT_JUSTIFY = 0x01, + /* Sticky headers work only on the doveadm client side. Trying to use + them via doveadm proxy won't work correctly. Practically this + restricts their usage only to the username. */ DOVEADM_PRINT_HEADER_FLAG_STICKY = 0x02, DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE = 0x04, DOVEADM_PRINT_HEADER_FLAG_EXPAND = 0x08,