bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "lib.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "str.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "strescape.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "ostream.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "auth-client-private.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "auth-server-connection.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "auth-client-request.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenstruct auth_client_request {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen pool_t pool;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen struct auth_server_connection *conn;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen unsigned int id;
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainen time_t created;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen auth_request_callback_t *callback;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen void *context;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen};
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenstatic void auth_server_send_new_request(struct auth_server_connection *conn,
6215a7ed04836eb66776e92b8686eff09b39a93fTimo Sirainen struct auth_client_request *request,
6215a7ed04836eb66776e92b8686eff09b39a93fTimo Sirainen const struct auth_request_info *info)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen string_t *str;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str = t_str_new(512);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_printfa(str, "AUTH\t%u\t", request->id);
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen str_append_tabescaped(str, info->mech);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_append(str, "\tservice=");
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen str_append_tabescaped(str, info->service);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
38505846b6d083e19f0a7d1373761bdda5d9a5a9Timo Sirainen if ((info->flags & AUTH_REQUEST_FLAG_SUPPORT_FINAL_RESP) != 0)
38505846b6d083e19f0a7d1373761bdda5d9a5a9Timo Sirainen str_append(str, "\tfinal-resp-ok");
ff2f20bbc039ad8e362ead199366e21bc41f18edAki Tuomi if ((info->flags & AUTH_REQUEST_FLAG_SECURED) != 0) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_append(str, "\tsecured");
ff2f20bbc039ad8e362ead199366e21bc41f18edAki Tuomi if ((info->flags & AUTH_REQUEST_FLAG_TRANSPORT_SECURITY_TLS) != 0)
ff2f20bbc039ad8e362ead199366e21bc41f18edAki Tuomi str_append(str, "=tls");
ff2f20bbc039ad8e362ead199366e21bc41f18edAki Tuomi } else {
ff2f20bbc039ad8e362ead199366e21bc41f18edAki Tuomi i_assert((info->flags & AUTH_REQUEST_FLAG_TRANSPORT_SECURITY_TLS) == 0);
ff2f20bbc039ad8e362ead199366e21bc41f18edAki Tuomi }
d8702d15ee7721ed1fcfc8f00a589970bd6b3598Timo Sirainen if ((info->flags & AUTH_REQUEST_FLAG_NO_PENALTY) != 0)
d8702d15ee7721ed1fcfc8f00a589970bd6b3598Timo Sirainen str_append(str, "\tno-penalty");
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if ((info->flags & AUTH_REQUEST_FLAG_VALID_CLIENT_CERT) != 0)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_append(str, "\tvalid-client-cert");
2087543679f0b35a8f70e1277b234e66ffdf96edTimo Sirainen if ((info->flags & AUTH_REQUEST_FLAG_DEBUG) != 0)
2087543679f0b35a8f70e1277b234e66ffdf96edTimo Sirainen str_append(str, "\tdebug");
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
f016dec9837e6a41867708e4b89ca5308dedab05Timo Sirainen if (info->session_id != NULL) {
f016dec9837e6a41867708e4b89ca5308dedab05Timo Sirainen str_append(str, "\tsession=");
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen str_append_tabescaped(str, info->session_id);
f016dec9837e6a41867708e4b89ca5308dedab05Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (info->cert_username != NULL) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_append(str, "\tcert_username=");
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen str_append_tabescaped(str, info->cert_username);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (info->local_ip.family != 0)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_printfa(str, "\tlip=%s", net_ip2addr(&info->local_ip));
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (info->remote_ip.family != 0)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_printfa(str, "\trip=%s", net_ip2addr(&info->remote_ip));
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (info->local_port != 0)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_printfa(str, "\tlport=%u", info->local_port);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (info->remote_port != 0)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_printfa(str, "\trport=%u", info->remote_port);
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen /* send the real_* variants only when they differ from the unreal
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen ones */
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen if (info->real_local_ip.family != 0 &&
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen !net_ip_compare(&info->real_local_ip, &info->local_ip)) {
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen str_printfa(str, "\treal_lip=%s",
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen net_ip2addr(&info->real_local_ip));
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen }
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen if (info->real_remote_ip.family != 0 &&
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen !net_ip_compare(&info->real_remote_ip, &info->remote_ip)) {
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen str_printfa(str, "\treal_rip=%s",
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen net_ip2addr(&info->real_remote_ip));
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen }
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen if (info->real_local_port != 0 &&
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen info->real_local_port != info->local_port)
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen str_printfa(str, "\treal_lport=%u", info->real_local_port);
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen if (info->real_remote_port != 0 &&
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen info->real_remote_port != info->remote_port)
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen str_printfa(str, "\treal_rport=%u", info->real_remote_port);
fe791e96fdf796f7d8997ee0515b163dc5eddd72Aki Tuomi if (info->local_name != NULL &&
0b808b62a679c81f59958392507fd0f6b5bd4e34Aki Tuomi *info->local_name != '\0') {
0b808b62a679c81f59958392507fd0f6b5bd4e34Aki Tuomi str_append(str, "\tlocal_name=");
0b808b62a679c81f59958392507fd0f6b5bd4e34Aki Tuomi str_append_tabescaped(str, info->local_name);
0b808b62a679c81f59958392507fd0f6b5bd4e34Aki Tuomi }
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi if (info->ssl_cipher_bits != 0 && info->ssl_cipher != NULL) {
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi str_append(str, "\tssl_cipher=");
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi str_append_tabescaped(str, info->ssl_cipher);
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi str_printfa(str, "\tssl_cipher_bits=%u", info->ssl_cipher_bits);
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi if (info->ssl_pfs != NULL) {
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi str_append(str, "\tssl_pfs=");
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi str_append_tabescaped(str, info->ssl_pfs);
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi }
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi }
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi if (info->ssl_protocol != NULL) {
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi str_append(str, "\tssl_protocol=");
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi str_append_tabescaped(str, info->ssl_protocol);
8c79de417ae66b4e54daeb9d998af2ab301151b4Aki Tuomi }
a8dac1be6a0c3adbbce5887ca395f418194c6c06Aki Tuomi if (info->client_id != NULL &&
a8dac1be6a0c3adbbce5887ca395f418194c6c06Aki Tuomi *info->client_id != '\0') {
a8dac1be6a0c3adbbce5887ca395f418194c6c06Aki Tuomi str_append(str, "\tclient_id=");
a8dac1be6a0c3adbbce5887ca395f418194c6c06Aki Tuomi str_append_tabescaped(str, info->client_id);
a8dac1be6a0c3adbbce5887ca395f418194c6c06Aki Tuomi }
53f97800b16ab3a8d263c5331132dec1e8fea9a0Aki Tuomi if (info->forward_fields != NULL &&
53f97800b16ab3a8d263c5331132dec1e8fea9a0Aki Tuomi *info->forward_fields != '\0') {
53f97800b16ab3a8d263c5331132dec1e8fea9a0Aki Tuomi str_append(str, "\tforward_fields=");
53f97800b16ab3a8d263c5331132dec1e8fea9a0Aki Tuomi str_append_tabescaped(str, info->forward_fields);
53f97800b16ab3a8d263c5331132dec1e8fea9a0Aki Tuomi }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (info->initial_resp_base64 != NULL) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_append(str, "\tresp=");
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen str_append_tabescaped(str, info->initial_resp_base64);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen str_append_c(str, '\n');
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (o_stream_send(conn->output, str_data(str), str_len(str)) < 0)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen i_error("Error sending request to auth server: %m");
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenstruct auth_client_request *
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenauth_client_request_new(struct auth_client *client,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen const struct auth_request_info *request_info,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen auth_request_callback_t *callback, void *context)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen struct auth_client_request *request;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen pool_t pool;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen pool = pool_alloconly_create("auth client request", 512);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen request = p_new(pool, struct auth_client_request, 1);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen request->pool = pool;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen request->conn = client->conn;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen request->callback = callback;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen request->context = context;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen request->id =
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen auth_server_connection_add_request(request->conn, request);
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainen request->created = ioloop_time;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen T_BEGIN {
6215a7ed04836eb66776e92b8686eff09b39a93fTimo Sirainen auth_server_send_new_request(request->conn, request, request_info);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen } T_END;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return request;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenvoid auth_client_request_continue(struct auth_client_request *request,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen const char *data_base64)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen struct const_iovec iov[3];
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen const char *prefix;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen prefix = t_strdup_printf("CONT\t%u\t", request->id);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen iov[0].iov_base = prefix;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen iov[0].iov_len = strlen(prefix);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen iov[1].iov_base = data_base64;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen iov[1].iov_len = strlen(data_base64);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen iov[2].iov_base = "\n";
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen iov[2].iov_len = 1;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (o_stream_sendv(request->conn->output, iov, 3) < 0)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen i_error("Error sending continue request to auth server: %m");
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainenstatic void ATTR_NULL(3, 4)
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainencall_callback(struct auth_client_request *request,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen enum auth_request_status status,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen const char *data_base64,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen const char *const *args)
4a0641e1ff10f0b0299fd36baf38057c54268e48Timo Sirainen{
4a0641e1ff10f0b0299fd36baf38057c54268e48Timo Sirainen auth_request_callback_t *callback = request->callback;
4a0641e1ff10f0b0299fd36baf38057c54268e48Timo Sirainen
57593ca3c443884bac880b8deff7c0655ddd9a30Timo Sirainen if (status != AUTH_REQUEST_STATUS_CONTINUE)
57593ca3c443884bac880b8deff7c0655ddd9a30Timo Sirainen request->callback = NULL;
4a0641e1ff10f0b0299fd36baf38057c54268e48Timo Sirainen callback(request, status, data_base64, args, request->context);
4a0641e1ff10f0b0299fd36baf38057c54268e48Timo Sirainen}
4a0641e1ff10f0b0299fd36baf38057c54268e48Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenvoid auth_client_request_abort(struct auth_client_request **_request)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen struct auth_client_request *request = *_request;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen *_request = NULL;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
4a0641e1ff10f0b0299fd36baf38057c54268e48Timo Sirainen auth_client_send_cancel(request->conn->client, request->id);
7c849dbc7be089175c1a83a84ee7249ed695810dTimo Sirainen call_callback(request, AUTH_REQUEST_STATUS_ABORT, NULL, NULL);
a9b135760aea6d1790d447d351c56b78889dac22Aki Tuomi /* remove the request */
a9b135760aea6d1790d447d351c56b78889dac22Aki Tuomi auth_server_connection_remove_request(request->conn, request->id);
1a29ed2f96da1be22fa5a4d96c7583aa81b8b060Timo Sirainen pool_unref(&request->pool);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenunsigned int auth_client_request_get_id(struct auth_client_request *request)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return request->id;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenunsigned int
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenauth_client_request_get_server_pid(struct auth_client_request *request)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return request->conn->server_pid;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
66c3f635f2f33905af527d49b27f95322aa7dfa7Timo Sirainenconst char *auth_client_request_get_cookie(struct auth_client_request *request)
66c3f635f2f33905af527d49b27f95322aa7dfa7Timo Sirainen{
66c3f635f2f33905af527d49b27f95322aa7dfa7Timo Sirainen return request->conn->cookie;
66c3f635f2f33905af527d49b27f95322aa7dfa7Timo Sirainen}
66c3f635f2f33905af527d49b27f95322aa7dfa7Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenbool auth_client_request_is_aborted(struct auth_client_request *request)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return request->callback == NULL;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainentime_t auth_client_request_get_create_time(struct auth_client_request *request)
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainen{
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainen return request->created;
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainen}
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenvoid auth_client_request_server_input(struct auth_client_request *request,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen enum auth_request_status status,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen const char *const *args)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen const char *const *tmp, *base64_data = NULL;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (request->callback == NULL) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen /* aborted already */
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen switch (status) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen case AUTH_REQUEST_STATUS_OK:
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen for (tmp = args; *tmp != NULL; tmp++) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (strncmp(*tmp, "resp=", 5) == 0) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen base64_data = *tmp + 5;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen break;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen break;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen case AUTH_REQUEST_STATUS_CONTINUE:
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen base64_data = args[0];
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen args = NULL;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen break;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen case AUTH_REQUEST_STATUS_FAIL:
7c849dbc7be089175c1a83a84ee7249ed695810dTimo Sirainen case AUTH_REQUEST_STATUS_INTERNAL_FAIL:
7c849dbc7be089175c1a83a84ee7249ed695810dTimo Sirainen case AUTH_REQUEST_STATUS_ABORT:
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen break;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
4a0641e1ff10f0b0299fd36baf38057c54268e48Timo Sirainen call_callback(request, status, base64_data, args);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (status != AUTH_REQUEST_STATUS_CONTINUE)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen pool_unref(&request->pool);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainenvoid auth_client_send_cancel(struct auth_client *client, unsigned int id)
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen{
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen const char *str = t_strdup_printf("CANCEL\t%u\n", id);
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen if (o_stream_send_str(client->conn->output, str) < 0)
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen i_error("Error sending request to auth server: %m");
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen}