23
26
#define AUTH_SOCKET_PATH "auth-login"
24
27
#define AUTH_USERDB_SOCKET_PATH "auth-userdb"
29
enum director_socket_type {
30
DIRECTOR_SOCKET_TYPE_UNKNOWN = 0,
31
DIRECTOR_SOCKET_TYPE_AUTH,
32
DIRECTOR_SOCKET_TYPE_USERDB,
33
DIRECTOR_SOCKET_TYPE_AUTHREPLY,
34
DIRECTOR_SOCKET_TYPE_RING,
35
DIRECTOR_SOCKET_TYPE_DOVEADM
26
38
static struct director *director;
27
39
static struct notify_connection *notify_conn;
40
static struct timeout *to_proctitle_refresh;
41
static ARRAY(enum director_socket_type) listener_socket_types;
43
static void director_refresh_proctitle_timeout(void *context ATTR_UNUSED)
45
static uint64_t prev_requests = 0, prev_input = 0, prev_output;
49
str_printfa(str, "[%u users", user_directory_count(director->users));
50
str_printfa(str, ", %lu req/s",
51
(unsigned long)(director->num_requests - prev_requests));
52
str_printfa(str, ", %llu+%llu kB/s",
53
(unsigned long long)(director->ring_traffic_input - prev_input)/1024,
54
(unsigned long long)(director->ring_traffic_output - prev_output)/1024);
55
str_append_c(str, ']');
57
prev_requests = director->num_requests;
58
prev_input = director->ring_traffic_input;
59
prev_output = director->ring_traffic_output;
61
process_title_set(str_c(str));
64
static enum director_socket_type
65
director_socket_type_get_from_name(const char *path)
67
const char *name, *suffix;
69
name = strrchr(path, '/');
75
suffix = strrchr(name, '-');
81
if (strcmp(suffix, "auth") == 0)
82
return DIRECTOR_SOCKET_TYPE_AUTH;
83
else if (strcmp(suffix, "userdb") == 0)
84
return DIRECTOR_SOCKET_TYPE_USERDB;
85
else if (strcmp(suffix, "authreply") == 0)
86
return DIRECTOR_SOCKET_TYPE_AUTHREPLY;
87
else if (strcmp(suffix, "ring") == 0)
88
return DIRECTOR_SOCKET_TYPE_RING;
89
else if (strcmp(suffix, "admin") == 0 ||
90
strcmp(suffix, "doveadm") == 0)
91
return DIRECTOR_SOCKET_TYPE_DOVEADM;
93
return DIRECTOR_SOCKET_TYPE_UNKNOWN;
96
static enum director_socket_type
97
listener_get_socket_type_fallback(const struct director_settings *set,
100
unsigned int local_port;
102
if (net_getsockname(listen_fd, NULL, &local_port) == 0 &&
104
/* TCP/IP connection */
105
if (local_port == set->director_doveadm_port)
106
return DIRECTOR_SOCKET_TYPE_DOVEADM;
108
return DIRECTOR_SOCKET_TYPE_RING;
110
return DIRECTOR_SOCKET_TYPE_AUTH;
113
static void listener_sockets_init(const struct director_settings *set,
114
struct ip_addr *listen_ip_r,
115
unsigned int *listen_port_r)
118
unsigned int i, socket_count, port;
120
enum director_socket_type type;
124
i_array_init(&listener_socket_types, 8);
125
socket_count = master_service_get_socket_count(master_service);
126
for (i = 0; i < socket_count; i++) {
127
int listen_fd = MASTER_LISTEN_FD_FIRST + i;
129
name = master_service_get_socket_name(master_service, listen_fd);
130
type = director_socket_type_get_from_name(name);
131
if (type == DIRECTOR_SOCKET_TYPE_UNKNOWN) {
132
/* mainly for backwards compatibility */
133
type = listener_get_socket_type_fallback(set, listen_fd);
135
if (type == DIRECTOR_SOCKET_TYPE_RING && *listen_port_r == 0 &&
136
net_getsockname(listen_fd, &ip, &port) == 0 && port > 0) {
138
*listen_port_r = port;
140
array_idx_set(&listener_socket_types, listen_fd, &type);
29
144
static int director_client_connected(int fd, const struct ip_addr *ip)
62
if (net_getpeername(conn->fd, &ip, NULL) == 0 &&
63
net_getsockname(conn->fd, NULL, &local_port) == 0 &&
64
(IPADDR_IS_V4(&ip) || IPADDR_IS_V6(&ip))) {
65
/* TCP/IP connection */
66
if (local_port == director->set->director_doveadm_port) {
176
typep = array_idx(&listener_socket_types, conn->listen_fd);
178
case DIRECTOR_SOCKET_TYPE_UNKNOWN:
180
case DIRECTOR_SOCKET_TYPE_AUTH:
181
case DIRECTOR_SOCKET_TYPE_USERDB:
182
/* a) userdb connection, probably for lmtp proxy
184
Both of them are handled exactly the same, except for which
185
auth socket they connect to. */
186
userdb = *typep == DIRECTOR_SOCKET_TYPE_USERDB;
187
socket_path = userdb ? AUTH_USERDB_SOCKET_PATH :
189
auth = auth_connection_init(socket_path);
190
if (auth_connection_connect(auth) < 0) {
191
auth_connection_deinit(&auth);
194
master_service_client_connection_accept(conn);
195
(void)login_connection_init(director, conn->fd, auth,
196
userdb ? LOGIN_CONNECTION_TYPE_USERDB :
197
LOGIN_CONNECTION_TYPE_AUTH);
199
case DIRECTOR_SOCKET_TYPE_AUTHREPLY:
200
master_service_client_connection_accept(conn);
201
(void)login_connection_init(director, conn->fd, NULL,
202
LOGIN_CONNECTION_TYPE_AUTHREPLY);
204
case DIRECTOR_SOCKET_TYPE_RING:
205
if (director_client_connected(conn->fd, &conn->remote_ip) == 0)
67
206
master_service_client_connection_accept(conn);
68
(void)doveadm_connection_init(director, conn->fd);
70
if (director_client_connected(conn->fd, &ip) == 0)
71
master_service_client_connection_accept(conn);
76
len = strlen(conn->name);
77
if (len > 6 && strcmp(conn->name + len - 6, "-admin") == 0) {
78
/* doveadm connection */
208
case DIRECTOR_SOCKET_TYPE_DOVEADM:
79
209
master_service_client_connection_accept(conn);
80
210
(void)doveadm_connection_init(director, conn->fd);
84
/* a) userdb connection, probably for lmtp proxy
86
Both of them are handled exactly the same, except for which
87
auth socket they connect to. */
88
userdb = len > 7 && strcmp(conn->name + len - 7, "-userdb") == 0;
89
socket_path = userdb ? AUTH_USERDB_SOCKET_PATH : AUTH_SOCKET_PATH;
90
auth = auth_connection_init(socket_path);
91
if (auth_connection_connect(auth) == 0) {
92
master_service_client_connection_accept(conn);
93
(void)login_connection_init(director, conn->fd, auth, userdb);
95
auth_connection_deinit(&auth);
100
find_inet_listener_port(struct ip_addr *ip_r,
101
const struct director_settings *set)
103
unsigned int i, socket_count, port;
105
socket_count = master_service_get_socket_count(master_service);
106
for (i = 0; i < socket_count; i++) {
107
int fd = MASTER_LISTEN_FD_FIRST + i;
109
if (net_getsockname(fd, ip_r, &port) == 0 && port > 0 &&
110
port != set->director_doveadm_port)
116
215
static void director_state_changed(struct director *dir)