1 /* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
2
3 #include "lib.h"
4 #include "array.h"
5 #include "auth-client-private.h"
6
7 struct event_category event_category_auth_client = {
8 .name = "auth-client"
9 };
10
11 struct auth_client *
auth_client_init(const char * auth_socket_path,unsigned int client_pid,bool debug)12 auth_client_init(const char *auth_socket_path, unsigned int client_pid,
13 bool debug)
14 {
15 struct auth_client *client;
16
17 client = i_new(struct auth_client, 1);
18 client->client_pid = client_pid;
19 client->auth_socket_path = i_strdup(auth_socket_path);
20 client->debug = debug;
21 client->connect_timeout_msecs = AUTH_CONNECT_TIMEOUT_MSECS;
22 client->clist = auth_client_connection_list_init();
23
24 client->event = event_create(NULL);
25 event_add_category(client->event, &event_category_auth_client);
26 event_set_append_log_prefix(client->event, "auth-client: ");
27 event_set_forced_debug(client->event, client->debug);
28
29 client->conn = auth_client_connection_init(client);
30 return client;
31 }
32
auth_client_deinit(struct auth_client ** _client)33 void auth_client_deinit(struct auth_client **_client)
34 {
35 struct auth_client *client = *_client;
36
37 *_client = NULL;
38
39 auth_client_connection_deinit(&client->conn);
40 connection_list_deinit(&client->clist);
41 event_unref(&client->event);
42 i_free(client->auth_socket_path);
43 i_free(client);
44 }
45
auth_client_connect(struct auth_client * client)46 void auth_client_connect(struct auth_client *client)
47 {
48 if (!client->conn->connected)
49 (void)auth_client_connection_connect(client->conn);
50 }
51
auth_client_disconnect(struct auth_client * client,const char * reason)52 void auth_client_disconnect(struct auth_client *client, const char *reason)
53 {
54 auth_client_connection_disconnect(client->conn, reason);
55 }
56
auth_client_is_connected(struct auth_client * client)57 bool auth_client_is_connected(struct auth_client *client)
58 {
59 /* handshake_received isn't unset immediately after disconnection */
60 return client->conn->conn.handshake_received &&
61 client->conn->connected;
62 }
63
auth_client_is_disconnected(struct auth_client * client)64 bool auth_client_is_disconnected(struct auth_client *client)
65 {
66 return !client->conn->connected;
67 }
68
auth_client_set_connect_timeout(struct auth_client * client,unsigned int msecs)69 void auth_client_set_connect_timeout(struct auth_client *client,
70 unsigned int msecs)
71 {
72 client->connect_timeout_msecs = msecs;
73 }
74
auth_client_set_connect_notify(struct auth_client * client,auth_connect_notify_callback_t * callback,void * context)75 void auth_client_set_connect_notify(struct auth_client *client,
76 auth_connect_notify_callback_t *callback,
77 void *context)
78 {
79 client->connect_notify_callback = callback;
80 client->connect_notify_context = context;
81 }
82
83 const struct auth_mech_desc *
auth_client_get_available_mechs(struct auth_client * client,unsigned int * mech_count)84 auth_client_get_available_mechs(struct auth_client *client,
85 unsigned int *mech_count)
86 {
87 i_assert(auth_client_is_connected(client));
88
89 return array_get(&client->conn->available_auth_mechs, mech_count);
90 }
91
92 const struct auth_mech_desc *
auth_client_find_mech(struct auth_client * client,const char * name)93 auth_client_find_mech(struct auth_client *client, const char *name)
94 {
95 const struct auth_mech_desc *mech;
96
97 array_foreach(&client->conn->available_auth_mechs, mech) {
98 if (strcasecmp(mech->name, name) == 0)
99 return mech;
100 }
101 return NULL;
102 }
103
auth_client_get_connect_id(struct auth_client * client,unsigned int * server_pid_r,unsigned int * connect_uid_r)104 void auth_client_get_connect_id(struct auth_client *client,
105 unsigned int *server_pid_r,
106 unsigned int *connect_uid_r)
107 {
108 i_assert(auth_client_is_connected(client));
109
110 *server_pid_r = client->conn->server_pid;
111 *connect_uid_r = client->conn->connect_uid;
112 }
113