From b5e69c08b5238bee1b672c671c49cf20d9b0a3fc Mon Sep 17 00:00:00 2001 From: Stephan Bosch <stephan.bosch@open-xchange.com> Date: Tue, 22 Jun 2021 17:06:58 +0200 Subject: [PATCH] managesieve-login: Reformat managesieve-proxy.c. --- src/managesieve-login/managesieve-proxy.c | 234 +++++++++++----------- 1 file changed, 118 insertions(+), 116 deletions(-) diff --git a/src/managesieve-login/managesieve-proxy.c b/src/managesieve-login/managesieve-proxy.c index 1e534c9ce..568310582 100644 --- a/src/managesieve-login/managesieve-proxy.c +++ b/src/managesieve-login/managesieve-proxy.c @@ -31,20 +31,18 @@ static const char *managesieve_proxy_state_names[MSIEVE_PROXY_STATE_COUNT] = { "none", "tls-start", "tls-ready", "xclient", "auth" }; -static void proxy_write_xclient -(struct managesieve_client *client, string_t *str) +static void +proxy_write_xclient(struct managesieve_client *client, string_t *str) { - str_printfa(str, - "XCLIENT ADDR=%s PORT=%u SESSION=%s TTL=%u\r\n", - net_ip2addr(&client->common.ip), - client->common.remote_port, - client_get_session_id(&client->common), - client->common.proxy_ttl - 1); + str_printfa(str, "XCLIENT ADDR=%s PORT=%u SESSION=%s TTL=%u\r\n", + net_ip2addr(&client->common.ip), client->common.remote_port, + client_get_session_id(&client->common), + client->common.proxy_ttl - 1); } -static void proxy_write_auth_data -(const unsigned char *data, unsigned int data_len, - string_t *str) +static void +proxy_write_auth_data(const unsigned char *data, unsigned int data_len, + string_t *str) { if (data_len == 0) str_append(str, "\"\""); @@ -55,8 +53,8 @@ static void proxy_write_auth_data } } -static int proxy_write_auth -(struct managesieve_client *client, string_t *str) +static int +proxy_write_auth(struct managesieve_client *client, string_t *str) { struct dsasl_client_settings sasl_set; const unsigned char *output; @@ -65,9 +63,9 @@ static int proxy_write_auth i_assert(client->common.proxy_ttl > 1); - if ( !client->proxy_sasl ) { - /* Prevent sending credentials to a server that has login disabled; - i.e., due to the lack of TLS */ + if (!client->proxy_sasl) { + /* Prevent sending credentials to a server that has login + disabled; i.e., due to the lack of TLS */ login_proxy_failed(client->common.login_proxy, login_proxy_get_event(client->common.login_proxy), LOGIN_PROXY_FAILURE_TYPE_REMOTE_CONFIG, @@ -80,8 +78,9 @@ static int proxy_write_auth i_assert(client->common.proxy_sasl_client == NULL); i_zero(&sasl_set); - sasl_set.authid = client->common.proxy_master_user != NULL ? - client->common.proxy_master_user : client->common.proxy_user; + sasl_set.authid = (client->common.proxy_master_user != NULL ? + client->common.proxy_master_user : + client->common.proxy_user); sasl_set.authzid = client->common.proxy_user; sasl_set.password = client->common.proxy_password; client->common.proxy_sasl_client = @@ -108,9 +107,9 @@ static int proxy_write_auth return 0; } -static int proxy_input_auth_challenge -(struct managesieve_client *client, const char *line, - const char **challenge_r) +static int +proxy_input_auth_challenge(struct managesieve_client *client, const char *line, + const char **challenge_r) { struct istream *input; struct managesieve_parser *parser; @@ -122,8 +121,8 @@ static int proxy_input_auth_challenge i_assert(client->common.proxy_sasl_client != NULL); *challenge_r = NULL; - /* Build an input stream for the managesieve parser - * FIXME: Ugly, see proxy_input_capability(). + /* Build an input stream for the managesieve parser. + FIXME: Ugly, see proxy_input_capability(). */ line = t_strconcat(line, "\r\n", NULL); input = i_stream_create_from_data(line, strlen(line)); @@ -133,8 +132,9 @@ static int proxy_input_auth_challenge (void)i_stream_read(input); ret = managesieve_parser_read_args(parser, 1, 0, &args); - if ( ret >= 0 ) { - if ( ret > 0 && managesieve_arg_get_string(&args[0], &challenge) ) { + if (ret >= 0) { + if (ret > 0 && + managesieve_arg_get_string(&args[0], &challenge)) { *challenge_r = t_strdup(challenge); } else { const char *reason = t_strdup_printf( @@ -146,18 +146,19 @@ static int proxy_input_auth_challenge fatal = TRUE; } - } else if ( ret == -2 ) { + } else if (ret == -2) { /* Parser needs more data (not possible on mem stream) */ i_unreached(); } else { - const char *error_str = managesieve_parser_get_error(parser, &fatal); - error_str = (error_str != NULL ? error_str : "unknown (bug)" ); + const char *error_str = + managesieve_parser_get_error(parser, &fatal); + error_str = (error_str != NULL ? error_str : "unknown (bug)"); /* Do not accept faulty server */ const char *reason = t_strdup_printf( - "Protocol parse error(%d) int SASL challenge line: %s (line=`%s')", - ret, error_str, line); + "Protocol parse error(%d) int SASL challenge line: %s " + "(line=`%s')", ret, error_str, line); login_proxy_failed(client->common.login_proxy, login_proxy_get_event(client->common.login_proxy), LOGIN_PROXY_FAILURE_TYPE_PROTOCOL, reason); @@ -166,17 +167,18 @@ static int proxy_input_auth_challenge /* Cleanup parser */ - managesieve_parser_destroy(&parser); + managesieve_parser_destroy(&parser); i_stream_destroy(&input); /* Time to exit if greeting was not accepted */ - if ( fatal ) return -1; + if (fatal) + return -1; return 0; } -static int proxy_write_auth_response -(struct managesieve_client *client, - const char *challenge, string_t *str) +static int +proxy_write_auth_response(struct managesieve_client *client, + const char *challenge, string_t *str) { const unsigned char *data; size_t data_len; @@ -212,21 +214,21 @@ static int proxy_write_auth_response return 0; } -static managesieve_response_t proxy_read_response -(const struct managesieve_arg *args) +static managesieve_response_t +proxy_read_response(const struct managesieve_arg *args) { const char *response; - if ( managesieve_arg_get_atom(&args[0], &response) ) { - if ( strcasecmp(response, "OK") == 0 ) { + if (managesieve_arg_get_atom(&args[0], &response)) { + if (strcasecmp(response, "OK") == 0) { /* Received OK response; greeting is finished */ return MANAGESIEVE_RESPONSE_OK; - } else if ( strcasecmp(response, "NO") == 0 ) { + } else if (strcasecmp(response, "NO") == 0) { /* Received OK response; greeting is finished */ return MANAGESIEVE_RESPONSE_NO; - } else if ( strcasecmp(response, "BYE") == 0 ) { + } else if (strcasecmp(response, "BYE") == 0) { /* Received OK response; greeting is finished */ return MANAGESIEVE_RESPONSE_BYE; } @@ -234,9 +236,9 @@ static managesieve_response_t proxy_read_response return MANAGESIEVE_RESPONSE_NONE; } -static int proxy_input_capability -(struct managesieve_client *client, const char *line, - managesieve_response_t *resp_r) +static int +proxy_input_capability(struct managesieve_client *client, const char *line, + managesieve_response_t *resp_r) { struct istream *input; struct managesieve_parser *parser; @@ -248,10 +250,11 @@ static int proxy_input_capability *resp_r = MANAGESIEVE_RESPONSE_NONE; /* Build an input stream for the managesieve parser - * FIXME: It would be nice if the line-wise parsing could be - * substituded by something similar to the command line interpreter. - * However, the current login_proxy structure does not make streams - * known until inside proxy_input handler. + + FIXME: It would be nice if the line-wise parsing could be substituded + by something similar to the command line interpreter. However, + the current login_proxy structure does not make streams known + until inside proxy_input handler. */ line = t_strconcat(line, "\r\n", NULL); input = i_stream_create_from_data(line, strlen(line)); @@ -259,13 +262,14 @@ static int proxy_input_capability managesieve_parser_reset(parser); /* Parse input - * FIXME: Theoretically the OK response could include a - * response code which could be rejected by the parser. + + FIXME: Theoretically the OK response could include a response code + which could be rejected by the parser. */ (void)i_stream_read(input); ret = managesieve_parser_read_args(parser, 2, 0, &args); - if ( ret == 0 ) { + if (ret == 0) { const char *reason = t_strdup_printf( "Remote returned with invalid capability/greeting line: %s", str_sanitize(line,160)); @@ -273,11 +277,11 @@ static int proxy_input_capability login_proxy_get_event(client->common.login_proxy), LOGIN_PROXY_FAILURE_TYPE_PROTOCOL, reason); fatal = TRUE; - } else if ( ret > 0 ) { - if ( args[0].type == MANAGESIEVE_ARG_ATOM ) { + } else if (ret > 0) { + if (args[0].type == MANAGESIEVE_ARG_ATOM) { *resp_r = proxy_read_response(args); - if ( *resp_r == MANAGESIEVE_RESPONSE_NONE ) { + if (*resp_r == MANAGESIEVE_RESPONSE_NONE) { const char *reason = t_strdup_printf( "Remote sent invalid response: %s", str_sanitize(line,160)); @@ -288,17 +292,18 @@ static int proxy_input_capability fatal = TRUE; } - } else if ( managesieve_arg_get_string(&args[0], &capability) ) { - if ( strcasecmp(capability, "SASL") == 0 ) { + } else if (managesieve_arg_get_string(&args[0], &capability)) { + if (strcasecmp(capability, "SASL") == 0) { const char *sasl_mechs; /* Check whether the server supports the SASL mechanism - * we are going to use (currently only PLAIN supported). + we are going to use (currently only PLAIN supported). */ - if ( ret == 2 && managesieve_arg_get_string(&args[1], &sasl_mechs) ) { + if (ret == 2 && + managesieve_arg_get_string(&args[1], &sasl_mechs)) { const char *const *mechs = t_strsplit(sasl_mechs, " "); - if ( *mechs != NULL ) { + if (*mechs != NULL) { /* At least one SASL mechanism is supported */ client->proxy_sasl = TRUE; } @@ -311,9 +316,9 @@ static int proxy_input_capability fatal = TRUE; } - } else if ( strcasecmp(capability, "STARTTLS") == 0 ) { + } else if (strcasecmp(capability, "STARTTLS") == 0) { client->proxy_starttls = TRUE; - } else if ( strcasecmp(capability, "XCLIENT") == 0 ) { + } else if (strcasecmp(capability, "XCLIENT") == 0) { client->proxy_xclient = TRUE; } @@ -328,18 +333,19 @@ static int proxy_input_capability fatal = TRUE; } - } else if ( ret == -2 ) { + } else if (ret == -2) { /* Parser needs more data (not possible on mem stream) */ i_unreached(); } else { - const char *error_str = managesieve_parser_get_error(parser, &fatal); - error_str = (error_str != NULL ? error_str : "unknown (bug)" ); + const char *error_str = + managesieve_parser_get_error(parser, &fatal); + error_str = (error_str != NULL ? error_str : "unknown (bug)"); /* Do not accept faulty server */ const char *reason = t_strdup_printf( - "Protocol parse error(%d) in capability/greeting line: %s (line=`%s')", - ret, error_str, line); + "Protocol parse error(%d) in capability/greeting line: %s " + "(line=`%s')", ret, error_str, line); login_proxy_failed(client->common.login_proxy, login_proxy_get_event(client->common.login_proxy), LOGIN_PROXY_FAILURE_TYPE_PROTOCOL, reason); @@ -351,10 +357,12 @@ static int proxy_input_capability i_stream_destroy(&input); /* Time to exit if greeting was not accepted */ - if ( fatal ) return -1; + if (fatal) + return -1; /* Wait until greeting is received completely */ - if ( *resp_r == MANAGESIEVE_RESPONSE_NONE ) return 1; + if (*resp_r == MANAGESIEVE_RESPONSE_NONE) + return 1; return 0; } @@ -394,7 +402,7 @@ managesieve_proxy_parse_auth_reply(const char *line, } } - /* parse the string */ + /* Parse the string */ input = i_stream_create_from_data(line, strlen(line)); parser = managesieve_parser_create(input, (size_t)-1); (void)i_stream_read(input); @@ -407,7 +415,7 @@ managesieve_proxy_parse_auth_reply(const char *line, int managesieve_proxy_parse_line(struct client *client, const char *line) { struct managesieve_client *msieve_client = - (struct managesieve_client *) client; + (struct managesieve_client *)client; struct ostream *output; enum login_proxy_ssl_flags ssl_flags; managesieve_response_t response = MANAGESIEVE_RESPONSE_NONE; @@ -417,14 +425,13 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) i_assert(!client->destroyed); output = login_proxy_get_ostream(client->login_proxy); - switch ( msieve_client->proxy_state ) { + switch (msieve_client->proxy_state) { case MSIEVE_PROXY_STATE_NONE: - if ( (ret=proxy_input_capability - (msieve_client, line, &response)) < 0 ) + ret = proxy_input_capability(msieve_client, line, &response); + if (ret < 0) return -1; - - if ( ret == 0 ) { - if ( response != MANAGESIEVE_RESPONSE_OK ) { + if (ret == 0) { + if (response != MANAGESIEVE_RESPONSE_OK) { login_proxy_failed(client->login_proxy, login_proxy_get_event(client->login_proxy), LOGIN_PROXY_FAILURE_TYPE_PROTOCOL, @@ -436,7 +443,7 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) ssl_flags = login_proxy_get_ssl_flags(client->login_proxy); if ((ssl_flags & PROXY_SSL_FLAG_STARTTLS) != 0) { - if ( !msieve_client->proxy_starttls ) { + if (!msieve_client->proxy_starttls) { login_proxy_failed(client->login_proxy, login_proxy_get_event(client->login_proxy), LOGIN_PROXY_FAILURE_TYPE_REMOTE_CONFIG, @@ -446,13 +453,11 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) str_append(command, "STARTTLS\r\n"); msieve_client->proxy_state = MSIEVE_PROXY_STATE_TLS_START; - } else if (msieve_client->proxy_xclient) { proxy_write_xclient(msieve_client, command); msieve_client->proxy_state = MSIEVE_PROXY_STATE_XCLIENT; - } else { - if ( proxy_write_auth(msieve_client, command) < 0 ) + if (proxy_write_auth(msieve_client, command) < 0) return -1; msieve_client->proxy_state = MSIEVE_PROXY_STATE_AUTH; } @@ -460,13 +465,11 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) o_stream_nsend(output, str_data(command), str_len(command)); } return 0; - case MSIEVE_PROXY_STATE_TLS_START: - if ( strncasecmp(line, "OK", 2) == 0 && - ( strlen(line) == 2 || line[2] == ' ' ) ) { - + if (strncasecmp(line, "OK", 2) == 0 && + (strlen(line) == 2 || line[2] == ' ')) { /* STARTTLS successful, begin TLS negotiation. */ - if ( login_proxy_starttls(client->login_proxy) < 0 ) + if (login_proxy_starttls(client->login_proxy) < 0) return -1; msieve_client->proxy_sasl = FALSE; @@ -480,13 +483,12 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) LOGIN_PROXY_FAILURE_TYPE_REMOTE, "Remote refused STARTTLS command"); return -1; - case MSIEVE_PROXY_STATE_TLS_READY: - if ( (ret=proxy_input_capability(msieve_client, line, &response)) < 0 ) + ret = proxy_input_capability(msieve_client, line, &response); + if (ret < 0) return -1; - - if ( ret == 0 ) { - if ( response != MANAGESIEVE_RESPONSE_OK ) { + if (ret == 0) { + if (response != MANAGESIEVE_RESPONSE_OK) { /* STARTTLS failed */ const char *reason = t_strdup_printf( "Remote STARTTLS failed: %s", @@ -498,25 +500,22 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) } command = t_str_new(128); - if ( msieve_client->proxy_xclient ) { + if (msieve_client->proxy_xclient) { proxy_write_xclient(msieve_client, command); msieve_client->proxy_state = MSIEVE_PROXY_STATE_XCLIENT; - } else { - if ( proxy_write_auth(msieve_client, command) < 0 ) + if (proxy_write_auth(msieve_client, command) < 0) return -1; msieve_client->proxy_state = MSIEVE_PROXY_STATE_AUTH; } o_stream_nsend(output, str_data(command), str_len(command)); } return 0; - case MSIEVE_PROXY_STATE_XCLIENT: - if ( strncasecmp(line, "OK", 2) == 0 && - ( strlen(line) == 2 || line[2] == ' ' ) ) { - + if (strncasecmp(line, "OK", 2) == 0 && + (strlen(line) == 2 || line[2] == ' ')) { command = t_str_new(128); - if ( proxy_write_auth(msieve_client, command) < 0 ) + if (proxy_write_auth(msieve_client, command) < 0) return -1; o_stream_nsend(output, str_data(command), str_len(command)); msieve_client->proxy_state = MSIEVE_PROXY_STATE_AUTH; @@ -530,36 +529,39 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) login_proxy_get_event(client->login_proxy), LOGIN_PROXY_FAILURE_TYPE_REMOTE, reason); return -1; - case MSIEVE_PROXY_STATE_AUTH: /* Challenge? */ - if ( *line == '"' ) { + if (*line == '"') { const char *challenge; - if ( proxy_input_auth_challenge - (msieve_client, line, &challenge) < 0 ) + if (proxy_input_auth_challenge(msieve_client, line, + &challenge) < 0) return -1; command = t_str_new(128); - if ( proxy_write_auth_response - (msieve_client, challenge, command) < 0 ) + if (proxy_write_auth_response(msieve_client, challenge, + command) < 0) return -1; - o_stream_nsend(output, str_data(command), str_len(command)); + o_stream_nsend(output, str_data(command), + str_len(command)); return 0; } /* Check login status */ - if ( strncasecmp(line, "OK", 2) == 0 && - (strlen(line) == 2 || line[2] == ' ') ) { + if (strncasecmp(line, "OK", 2) == 0 && + (strlen(line) == 2 || line[2] == ' ')) { string_t *str = t_str_new(128); /* Login successful */ - /* FIXME: some SASL mechanisms cause a capability response to be sent */ + /* FIXME: Some SASL mechanisms cause a capability + response to be sent. + */ /* Send this line to client. */ - str_append(str, line ); + str_append(str, line); str_append(str, "\r\n"); - o_stream_nsend(client->output, str_data(str), str_len(str)); + o_stream_nsend(client->output, str_data(str), + str_len(str)); (void)client_skip_line(msieve_client); client_proxy_finish_destroy_client(client); @@ -568,11 +570,12 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) /* Authentication failed */ bool try_later; - (void)managesieve_proxy_parse_auth_reply(line, &reason, &try_later); + (void)managesieve_proxy_parse_auth_reply(line, &reason, + &try_later); /* Login failed. Send our own failure reply so client can't - * figure out if user exists or not just by looking at the - * reply string. + figure out if user exists or not just by looking at the reply + string. */ enum login_proxy_failure_type failure_type; if (try_later) @@ -586,7 +589,6 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) login_proxy_get_event(client->login_proxy), failure_type, reason); return -1; - default: /* Not supposed to happen */ break; @@ -599,7 +601,7 @@ int managesieve_proxy_parse_line(struct client *client, const char *line) void managesieve_proxy_reset(struct client *client) { struct managesieve_client *msieve_client = - (struct managesieve_client *) client; + (struct managesieve_client *)client; msieve_client->proxy_starttls = FALSE; msieve_client->proxy_sasl = FALSE; @@ -647,7 +649,7 @@ void managesieve_proxy_failed(struct client *client, const char *managesieve_proxy_get_state(struct client *client) { struct managesieve_client *msieve_client = - (struct managesieve_client *) client; + (struct managesieve_client *)client; return managesieve_proxy_state_names[msieve_client->proxy_state]; } -- GitLab