login-settings.c revision cca4ba2a504d70a9fe9fee37f8433997359de52c
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen* Copyright (c) 2005-2013 Dovecot authors, see the included COPYING file */
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "login-common.h"
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen#include "hostpid.h"
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen#include "var-expand.h"
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen#include "settings-parser.h"
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen#include "master-service.h"
aef92409cf369afdd2ecd81a4f80083cd4082f46Timo Sirainen#include "master-service-settings.h"
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen#include "master-service-ssl-settings.h"
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen#include "master-service-settings-cache.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "login-settings.h"
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen#include <stddef.h>
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen#include <unistd.h>
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic bool login_settings_check(void *_set, pool_t pool, const char **error_r);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0add8c99ca65e56dbf613595fc37c41aafff3f7fTimo Sirainen#undef DEF
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen#define DEF(type, name) \
105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen { type, #name, offsetof(struct login_settings, name), NULL }
aef92409cf369afdd2ecd81a4f80083cd4082f46Timo Sirainen
aef92409cf369afdd2ecd81a4f80083cd4082f46Timo Sirainenstatic const struct setting_define login_setting_defines[] = {
aef92409cf369afdd2ecd81a4f80083cd4082f46Timo Sirainen DEF(SET_STR, login_trusted_networks),
aef92409cf369afdd2ecd81a4f80083cd4082f46Timo Sirainen DEF(SET_STR_VARS, login_greeting),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEF(SET_STR, login_log_format_elements),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEF(SET_STR, login_log_format),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEF(SET_STR, login_access_sockets),
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen DEF(SET_STR, director_username_hash),
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen DEF(SET_STR, ssl_client_cert),
56f45b3f3ae20e5c933701f4657dda5ef1916855Timo Sirainen DEF(SET_STR, ssl_client_key),
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen DEF(SET_BOOL, ssl_require_crl),
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen DEF(SET_BOOL, auth_ssl_require_client_cert),
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen DEF(SET_BOOL, auth_ssl_username_from_cert),
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen DEF(SET_BOOL, disable_plaintext_auth),
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen DEF(SET_BOOL, auth_verbose),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEF(SET_BOOL, auth_debug),
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen DEF(SET_BOOL, verbose_proctitle),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen DEF(SET_UINT, mail_max_userip_connections),
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen SETTING_DEFINE_LIST_END
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic const struct login_settings login_default_settings = {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .login_trusted_networks = "",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .login_greeting = PACKAGE_NAME" ready.",
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen .login_log_format_elements = "user=<%u> method=%m rip=%r lip=%l mpid=%e %c session=<%{session}>",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .login_log_format = "%$: %s",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .login_access_sockets = "",
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen .director_username_hash = "%u",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .ssl_client_cert = "",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .ssl_client_key = "",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .ssl_require_crl = TRUE,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .auth_ssl_require_client_cert = FALSE,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .auth_ssl_username_from_cert = FALSE,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .disable_plaintext_auth = TRUE,
659fe5d24825b160cae512538088020d97a60239Timo Sirainen .auth_verbose = FALSE,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .auth_debug = FALSE,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .verbose_proctitle = FALSE,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
659fe5d24825b160cae512538088020d97a60239Timo Sirainen .mail_max_userip_connections = 10
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
232d5bef3c709e90e24f0874a36854b92187bb6cTimo Sirainenconst struct setting_parser_info login_setting_parser_info = {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen .module_name = "login",
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen .defines = login_setting_defines,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen .defaults = &login_default_settings,
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen .type_offset = (size_t)-1,
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen .struct_size = sizeof(struct login_settings),
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen .parent_offset = (size_t)-1,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen .check_func = login_settings_check
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainenstatic const struct setting_parser_info *default_login_set_roots[] = {
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen &login_setting_parser_info,
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen NULL
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen};
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainenconst struct setting_parser_info **login_set_roots = default_login_set_roots;
a928e7efabb1672b1476e597106d4b4b81ac6f3cTimo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainenstatic struct master_service_settings_cache *set_cache;
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
838f56174b963779a88083a0d0e85b30d2d846e7Timo Sirainen/* <settings checks> */
894987bf45718f8849cc3898afdfb1ac3cfa2445Timo Sirainenstatic bool login_settings_check(void *_set, pool_t pool,
894987bf45718f8849cc3898afdfb1ac3cfa2445Timo Sirainen const char **error_r ATTR_UNUSED)
894987bf45718f8849cc3898afdfb1ac3cfa2445Timo Sirainen{
838f56174b963779a88083a0d0e85b30d2d846e7Timo Sirainen struct login_settings *set = _set;
838f56174b963779a88083a0d0e85b30d2d846e7Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen set->log_format_elements_split =
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen p_strsplit(pool, set->login_log_format_elements, " ");
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen if (set->auth_debug_passwords)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen set->auth_debug = TRUE;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (set->auth_debug)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen set->auth_verbose = TRUE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return TRUE;
838f56174b963779a88083a0d0e85b30d2d846e7Timo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen/* </settings checks> */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic const struct var_expand_table *
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenlogin_set_var_expand_table(const struct master_service_settings_input *input)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen{
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen static struct var_expand_table static_tab[] = {
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen { 'l', NULL, "lip" },
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen { 'r', NULL, "rip" },
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen { 'p', NULL, "pid" },
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen { 's', NULL, "service" },
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen { '\0', NULL, NULL }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen };
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen struct var_expand_table *tab;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen tab = t_malloc(sizeof(static_tab));
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen memcpy(tab, static_tab, sizeof(static_tab));
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen tab[0].value = net_ip2addr(&input->local_ip);
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen tab[1].value = net_ip2addr(&input->remote_ip);
a928e7efabb1672b1476e597106d4b4b81ac6f3cTimo Sirainen tab[2].value = my_pid;
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen tab[3].value = input->service;
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen return tab;
0d70a702dec63d22535684fec6a7247c5f153208Timo Sirainen}
0d70a702dec63d22535684fec6a7247c5f153208Timo Sirainen
0d70a702dec63d22535684fec6a7247c5f153208Timo Sirainenstatic void *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenlogin_setting_dup(pool_t pool, const struct setting_parser_info *info,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const void *src_set)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen{
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const char *error;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen void *dest;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen dest = settings_dup(info, src_set, pool);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if (!settings_check(info, pool, dest, &error)) {
94aa90d2d17a7aebcda5a4193a62e80ddbb169b7Timo Sirainen const char *name = info->module_name;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen i_fatal("settings_check(%s) failed: %s",
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen name != NULL ? name : "unknown", error);
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen }
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen return dest;
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen}
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenstruct login_settings *
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenlogin_settings_read(pool_t pool,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const struct ip_addr *local_ip,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const struct ip_addr *remote_ip,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const char *local_name,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const struct master_service_ssl_settings **ssl_set_r,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen void ***other_settings_r)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen{
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct master_service_settings_input input;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const char *error;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const struct setting_parser_context *parser;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen void *const *cache_sets;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen void **sets;
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen unsigned int i, count;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memset(&input, 0, sizeof(input));
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen input.roots = login_set_roots;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen input.module = login_binary->process_name;
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen input.service = login_binary->protocol;
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen input.local_name = local_name;
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen if (local_ip != NULL)
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen input.local_ip = *local_ip;
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen if (remote_ip != NULL)
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen input.remote_ip = *remote_ip;
c680a6b35b459045e92814778908da5a93922107Timo Sirainen
c680a6b35b459045e92814778908da5a93922107Timo Sirainen if (set_cache == NULL) {
c680a6b35b459045e92814778908da5a93922107Timo Sirainen set_cache = master_service_settings_cache_init(master_service,
c680a6b35b459045e92814778908da5a93922107Timo Sirainen input.module,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen input.service);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (master_service_settings_cache_read(set_cache, &input, NULL,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen &parser, &error) < 0)
5f78b33aa505b17e23cdf27b071a24e127b3db54Timo Sirainen i_fatal("Error reading configuration: %s", error);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen cache_sets = master_service_settings_parser_get_others(master_service, parser);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen for (count = 0; input.roots[count] != NULL; count++) ;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen i_assert(cache_sets[count] == NULL);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen sets = p_new(pool, void *, count + 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (i = 0; i < count; i++)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen sets[i] = login_setting_dup(pool, input.roots[i], cache_sets[i]);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen settings_var_expand(&login_setting_parser_info, sets[0], pool,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen login_set_var_expand_table(&input));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *ssl_set_r =
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen login_setting_dup(pool, &master_service_ssl_setting_parser_info,
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen settings_parser_get_list(parser)[1]);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen *other_settings_r = sets + 1;
c9c24293550541307f1bb41bba4a0fdfe2fa59e0Timo Sirainen return sets[0];
c9c24293550541307f1bb41bba4a0fdfe2fa59e0Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid login_settings_deinit(void)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (set_cache != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen master_service_settings_cache_deinit(&set_cache);
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen}
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen