summaryrefslogtreecommitdiffstats
path: root/src/lib-auth/auth-client.c
blob: 9523282cfd094a1046b8a81124224ae298c40b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "auth-client-private.h"

struct event_category event_category_auth_client = {
	.name = "auth-client"
};

struct auth_client *
auth_client_init(const char *auth_socket_path, unsigned int client_pid,
		 bool debug)
{
	struct auth_client *client;

	client = i_new(struct auth_client, 1);
	client->client_pid = client_pid;
	client->auth_socket_path = i_strdup(auth_socket_path);
	client->debug = debug;
	client->connect_timeout_msecs = AUTH_CONNECT_TIMEOUT_MSECS;
	client->clist = auth_client_connection_list_init();

	client->event = event_create(NULL);
	event_add_category(client->event, &event_category_auth_client);
	event_set_append_log_prefix(client->event, "auth-client: ");
	event_set_forced_debug(client->event, client->debug);

	client->conn = auth_client_connection_init(client);
	return client;
}

void auth_client_deinit(struct auth_client **_client)
{
	struct auth_client *client = *_client;

	*_client = NULL;

	auth_client_connection_deinit(&client->conn);
	connection_list_deinit(&client->clist);
	event_unref(&client->event);
	i_free(client->auth_socket_path);
	i_free(client);
}

void auth_client_connect(struct auth_client *client)
{
	if (!client->conn->connected)
		(void)auth_client_connection_connect(client->conn);
}

void auth_client_disconnect(struct auth_client *client, const char *reason)
{
	auth_client_connection_disconnect(client->conn, reason);
}

bool auth_client_is_connected(struct auth_client *client)
{
	/* handshake_received isn't unset immediately after disconnection */
	return client->conn->conn.handshake_received &&
		client->conn->connected;
}

bool auth_client_is_disconnected(struct auth_client *client)
{
	return !client->conn->connected;
}

void auth_client_set_connect_timeout(struct auth_client *client,
				     unsigned int msecs)
{
	client->connect_timeout_msecs = msecs;
}

void auth_client_set_connect_notify(struct auth_client *client,
				    auth_connect_notify_callback_t *callback,
				    void *context)
{
	client->connect_notify_callback = callback;
	client->connect_notify_context = context;
}

const struct auth_mech_desc *
auth_client_get_available_mechs(struct auth_client *client,
				unsigned int *mech_count)
{
	i_assert(auth_client_is_connected(client));

	return array_get(&client->conn->available_auth_mechs, mech_count);
}

const struct auth_mech_desc *
auth_client_find_mech(struct auth_client *client, const char *name)
{
	const struct auth_mech_desc *mech;

	array_foreach(&client->conn->available_auth_mechs, mech) {
		if (strcasecmp(mech->name, name) == 0)
			return mech;
	}
	return NULL;
}

void auth_client_get_connect_id(struct auth_client *client,
				unsigned int *server_pid_r,
				unsigned int *connect_uid_r)
{
	i_assert(auth_client_is_connected(client));

	*server_pid_r = client->conn->server_pid;
	*connect_uid_r = client->conn->connect_uid;
}