bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen#include "lib.h"
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen#include "dsasl-client-private.h"
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainenstruct external_dsasl_client {
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen struct dsasl_client client;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen bool output_sent;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen};
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainenstatic int
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainenmech_external_input(struct dsasl_client *_client,
a669d351502e15802b121e1a0bd83f27d1d95f01Timo Sirainen const unsigned char *input ATTR_UNUSED, size_t input_len,
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen const char **error_r)
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen{
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen struct external_dsasl_client *client =
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen (struct external_dsasl_client *)_client;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen if (!client->output_sent) {
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen if (input_len > 0) {
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen *error_r = "Server sent non-empty initial response";
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen return -1;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen }
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen } else {
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen *error_r = "Server didn't finish authentication";
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen return -1;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen }
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen return 0;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen}
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainenstatic int
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainenmech_external_output(struct dsasl_client *_client,
a669d351502e15802b121e1a0bd83f27d1d95f01Timo Sirainen const unsigned char **output_r, size_t *output_len_r,
5d08475be193d68abd1f61fe2842835d9b696858Timo Sirainen const char **error_r ATTR_UNUSED)
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen{
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen struct external_dsasl_client *client =
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen (struct external_dsasl_client *)_client;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen const char *username;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen if (_client->set.authzid != NULL)
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen username = _client->set.authzid;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen else if (_client->set.authid != NULL)
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen username = _client->set.authid;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen else
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen username = "";
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen *output_r = (const void *)username;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen *output_len_r = strlen(username);
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen client->output_sent = TRUE;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen return 0;
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen}
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainenconst struct dsasl_client_mech dsasl_client_mech_external = {
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen .name = "EXTERNAL",
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen .struct_size = sizeof(struct external_dsasl_client),
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen .input = mech_external_input,
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen .output = mech_external_output
405d5f230d6beded2de44f0f0b038d27c1c7c30eTimo Sirainen};