main.c revision 2e37d45867d081db150ab78dad303b9077aea24f
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2010-2011 Dovecot authors, see the included COPYING file */
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainenstatic char *auth_socket_path, *userdb_socket_path;
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic int director_client_connected(int fd, const struct ip_addr *ip)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (director_host_lookup_ip(director, ip) == NULL) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen i_warning("Connection from %s: Server not listed in "
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen "director_servers, dropping", net_ip2addr(ip));
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen director_connection_init_in(director, fd, ip);
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainen i_error("Received another proxy-notify connection");
db693bf6fcae96d834567f1782257517b7207655Timo Sirainen master_service_client_connection_accept(conn);
300e4e43ed1ca46d0614459161ca2fb460ef661aTimo Sirainen notify_conn = notify_connection_init(director, conn->fd);
5801ce4da7d807ab85d02051ece5969e7175eebaTimo Sirainen if (net_getpeername(conn->fd, &ip, NULL) == 0 &&
5801ce4da7d807ab85d02051ece5969e7175eebaTimo Sirainen net_getsockname(conn->fd, NULL, &local_port) == 0 &&
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainen /* TCP/IP connection */
5801ce4da7d807ab85d02051ece5969e7175eebaTimo Sirainen if (local_port == director->set->director_doveadm_port) {
db693bf6fcae96d834567f1782257517b7207655Timo Sirainen master_service_client_connection_accept(conn);
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainen (void)doveadm_connection_init(director, conn->fd);
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainen if (director_client_connected(conn->fd, &ip) == 0)
861f53be0cc2fa5665f3c107a7576e2a53bb2eb0Timo Sirainen master_service_client_connection_accept(conn);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (net_getunixname(conn->listen_fd, &path) < 0)
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen i_fatal("getunixname(%d) failed: %m", conn->listen_fd);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (len > 6 && strcmp(name + len - 6, "-admin") == 0) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen /* doveadm connection */
db693bf6fcae96d834567f1782257517b7207655Timo Sirainen master_service_client_connection_accept(conn);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen (void)doveadm_connection_init(director, conn->fd);
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen /* a) userdb connection, probably for lmtp proxy
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen b) login connection
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen Both of them are handled exactly the same, except for which
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen auth socket they connect to. */
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen userdb = len > 7 && strcmp(name + len - 7, "-userdb") == 0;
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen socket_path = userdb ? userdb_socket_path : auth_socket_path;
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen master_service_client_connection_accept(conn);
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen login_connection_init(director, conn->fd, auth, userdb);
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainenstatic unsigned int
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen socket_count = master_service_get_socket_count(master_service);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen for (i = 0; i < socket_count; i++) {
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainen if (net_getsockname(fd, ip_r, &port) == 0 && port > 0 &&
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void director_state_changed(struct director *dir)
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen mail_host_get_by_hash(dir->mail_hosts, 0) == NULL)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen /* if there are any pending client requests, finish them now */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen array_foreach(&dir->pending_requests, requestp) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void main_init(void)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen set = master_service_settings_get_others(master_service)[0];
b4ddb5b3c3722620a8fef387dd8c47bb411a5643Timo Sirainen userdb_socket_path = i_strconcat(set->base_dir,
fa87f0ef3636fa52ce42513533ee500c0bf29910Timo Sirainen listen_port = find_inet_listener_port(&listen_ip, set);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (listen_port == 0 && *set->director_servers != '\0') {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen i_fatal("No inet_listeners defined for director service "
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen "(for standalone keep director_servers empty)");
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen director = director_init(set, &listen_ip, listen_port,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen director_host_add_from_string(director, set->director_servers);
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen if (mail_hosts_parse_and_add(director->mail_hosts,
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen i_fatal("Invalid value for director_mail_servers setting");
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainen director->orig_config_hosts = mail_hosts_dup(director->mail_hosts);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void main_deinit(void)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen const struct setting_parser_info *set_roots[] = {
412b772c337428b72149605c1410524c2353e5d4Timo Sirainen const enum master_service_flags service_flags =
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen unsigned int test_port = 0;
412b772c337428b72149605c1410524c2353e5d4Timo Sirainen master_service = master_service_init("director", service_flags,
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen while ((c = master_getopt(master_service)) > 0) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (master_service_settings_read_simple(master_service, set_roots,
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen i_fatal("Error reading configuration: %s", error);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen master_service_init_log(master_service, "director: ");
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen /* we're testing, possibly writing to same log file.
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen make it clear which director we are. */