bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen#include "lib.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "array.h"
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include "auth-client-private.h"
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen#include "auth-server-connection.h"
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenstruct auth_client *
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenauth_client_init(const char *auth_socket_path, unsigned int client_pid,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen bool debug)
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen{
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen struct auth_client *client;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen client = i_new(struct auth_client, 1);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen client->client_pid = client_pid;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen client->auth_socket_path = i_strdup(auth_socket_path);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen client->debug = debug;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen client->conn = auth_server_connection_init(client);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen (void)auth_server_connection_connect(client->conn);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen return client;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen}
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenvoid auth_client_deinit(struct auth_client **_client)
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen{
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen struct auth_client *client = *_client;
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen *_client = NULL;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen auth_server_connection_deinit(&client->conn);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen i_free(client->auth_socket_path);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen i_free(client);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen}
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
90ed03ab289947f5576d2c616ada27724f50e9cdTimo Sirainenvoid auth_client_connect(struct auth_client *client)
90ed03ab289947f5576d2c616ada27724f50e9cdTimo Sirainen{
90ed03ab289947f5576d2c616ada27724f50e9cdTimo Sirainen if (client->conn->fd == -1)
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)auth_server_connection_connect(client->conn);
90ed03ab289947f5576d2c616ada27724f50e9cdTimo Sirainen}
90ed03ab289947f5576d2c616ada27724f50e9cdTimo Sirainen
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainenvoid auth_client_disconnect(struct auth_client *client, const char *reason)
63b70dd3e4b4d68a02b1bf7d78e92076210e3e1aTimo Sirainen{
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainen auth_server_connection_disconnect(client->conn, reason);
63b70dd3e4b4d68a02b1bf7d78e92076210e3e1aTimo Sirainen}
63b70dd3e4b4d68a02b1bf7d78e92076210e3e1aTimo Sirainen
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenbool auth_client_is_connected(struct auth_client *client)
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return client->conn->handshake_received;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen}
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
05817ffe09295892e1aa5c4a7f91d060e249563cTimo Sirainenbool auth_client_is_disconnected(struct auth_client *client)
05817ffe09295892e1aa5c4a7f91d060e249563cTimo Sirainen{
05817ffe09295892e1aa5c4a7f91d060e249563cTimo Sirainen return client->conn->fd == -1;
05817ffe09295892e1aa5c4a7f91d060e249563cTimo Sirainen}
05817ffe09295892e1aa5c4a7f91d060e249563cTimo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainenvoid auth_client_set_connect_notify(struct auth_client *client,
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen auth_connect_notify_callback_t *callback,
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen void *context)
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen{
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen client->connect_notify_callback = callback;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen client->connect_notify_context = context;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen}
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenconst struct auth_mech_desc *
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenauth_client_get_available_mechs(struct auth_client *client,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen unsigned int *mech_count)
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen{
d2ec97341f40fb2467f01ce466a99cd6b64178a8Timo Sirainen i_assert(auth_client_is_connected(client));
d2ec97341f40fb2467f01ce466a99cd6b64178a8Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return array_get(&client->conn->available_auth_mechs, mech_count);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen}
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenconst struct auth_mech_desc *
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenauth_client_find_mech(struct auth_client *client, const char *name)
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen{
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Sirainen const struct auth_mech_desc *mech;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Sirainen array_foreach(&client->conn->available_auth_mechs, mech) {
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Sirainen if (strcasecmp(mech->name, name) == 0)
e20e638805c4bd54e039891a3e92760b1dfa189aTimo Sirainen return mech;
69e938d3250b3cf3b048d89275716a1831277bb5Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return NULL;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
69e938d3250b3cf3b048d89275716a1831277bb5Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenvoid auth_client_get_connect_id(struct auth_client *client,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen unsigned int *server_pid_r,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen unsigned int *connect_uid_r)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
d2ec97341f40fb2467f01ce466a99cd6b64178a8Timo Sirainen i_assert(auth_client_is_connected(client));
d2ec97341f40fb2467f01ce466a99cd6b64178a8Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen *server_pid_r = client->conn->server_pid;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen *connect_uid_r = client->conn->connect_uid;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen}