bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen#include "lib.h"
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen#include "settings-parser.h"
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen#include "mail-storage-settings.h"
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen#include "imapc-settings.h"
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen#include <stddef.h>
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen#undef DEF
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen#define DEF(type, name) \
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen { type, #name, offsetof(struct imapc_settings, name), NULL }
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainenstatic bool imapc_settings_check(void *_set, pool_t pool, const char **error_r);
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainenstatic const struct setting_define imapc_setting_defines[] = {
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen DEF(SET_STR, imapc_host),
009217abb57a24a4076092e8e4e165545747839eStephan Bosch DEF(SET_IN_PORT, imapc_port),
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c458d2e8b99ec4ce939a58f5b4add83783bc14ccTimo Sirainen DEF(SET_STR_VARS, imapc_user),
5c6aa687a33a4d819f4b50bb9ec02535395865d6Timo Sirainen DEF(SET_STR_VARS, imapc_master_user),
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen DEF(SET_STR, imapc_password),
c6033074ada5c7441ff7bb12c4b433cae737fea2Timo Sirainen DEF(SET_STR, imapc_sasl_mechanisms),
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen DEF(SET_ENUM, imapc_ssl),
35779754964119c94b919b75bbf1ccc1c02aad52Timo Sirainen DEF(SET_BOOL, imapc_ssl_verify),
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen DEF(SET_STR, imapc_features),
0998339f5a8424ce0b9f108006fa6bbc74be70dfTimo Sirainen DEF(SET_STR, imapc_rawlog_dir),
b08822564e0fcb8f3eafd2e4be06ae2efedff0cdTimo Sirainen DEF(SET_STR, imapc_list_prefix),
cff23ec51177f11902c99b727268eb05ea7c97c7Timo Sirainen DEF(SET_TIME, imapc_cmd_timeout),
cee50689ea9f3cfec451a6e105307ee2d86ed5dcTimo Sirainen DEF(SET_TIME, imapc_max_idle_time),
c515f5c969f7a3a5b525ab15bde2f116cbe932deAki Tuomi DEF(SET_UINT, imapc_connection_retry_count),
efd4de737ee4febffced006511f357959234a6caTimo Sirainen DEF(SET_TIME_MSECS, imapc_connection_retry_interval),
c23ebb9b0e0b760ca8da16fde34ff33f5ece5e07Timo Sirainen DEF(SET_SIZE, imapc_max_line_length),
cee50689ea9f3cfec451a6e105307ee2d86ed5dcTimo Sirainen
6b23e5a3fff511cd64f1edb880fd080016c23c0fTimo Sirainen DEF(SET_STR, pop3_deleted_flag),
6b23e5a3fff511cd64f1edb880fd080016c23c0fTimo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen SETTING_DEFINE_LIST_END
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen};
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainenstatic const struct imapc_settings imapc_default_settings = {
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .imapc_host = "",
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .imapc_port = 143,
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
281c0a69d647c542a05ebf5ec06040da2fe09e7bTimo Sirainen .imapc_user = "",
5c6aa687a33a4d819f4b50bb9ec02535395865d6Timo Sirainen .imapc_master_user = "",
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .imapc_password = "",
c6033074ada5c7441ff7bb12c4b433cae737fea2Timo Sirainen .imapc_sasl_mechanisms = "",
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .imapc_ssl = "no:imaps:starttls",
35779754964119c94b919b75bbf1ccc1c02aad52Timo Sirainen .imapc_ssl_verify = TRUE,
0998339f5a8424ce0b9f108006fa6bbc74be70dfTimo Sirainen
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen .imapc_features = "",
11bbebd9f9a8043365e87a621a263749e93334bdTimo Sirainen .imapc_rawlog_dir = "",
b08822564e0fcb8f3eafd2e4be06ae2efedff0cdTimo Sirainen .imapc_list_prefix = "",
cff23ec51177f11902c99b727268eb05ea7c97c7Timo Sirainen .imapc_cmd_timeout = 5*60,
6b23e5a3fff511cd64f1edb880fd080016c23c0fTimo Sirainen .imapc_max_idle_time = 60*29,
c515f5c969f7a3a5b525ab15bde2f116cbe932deAki Tuomi .imapc_connection_retry_count = 1,
efd4de737ee4febffced006511f357959234a6caTimo Sirainen .imapc_connection_retry_interval = 1000,
c23ebb9b0e0b760ca8da16fde34ff33f5ece5e07Timo Sirainen .imapc_max_line_length = 0,
6b23e5a3fff511cd64f1edb880fd080016c23c0fTimo Sirainen
6b23e5a3fff511cd64f1edb880fd080016c23c0fTimo Sirainen .pop3_deleted_flag = ""
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen};
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainenstatic const struct setting_parser_info imapc_setting_parser_info = {
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .module_name = "imapc",
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .defines = imapc_setting_defines,
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .defaults = &imapc_default_settings,
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .type_offset = (size_t)-1,
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .struct_size = sizeof(struct imapc_settings),
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .parent_offset = (size_t)-1,
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .parent = &mail_user_setting_parser_info,
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen .check_func = imapc_settings_check
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen};
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainenconst struct setting_parser_info *imapc_get_setting_parser_info(void)
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen{
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen return &imapc_setting_parser_info;
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen}
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen/* <settings checks> */
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainenstruct imapc_feature_list {
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen const char *name;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen enum imapc_features num;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen};
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainenstatic const struct imapc_feature_list imapc_feature_list[] = {
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen { "rfc822.size", IMAPC_FEATURE_RFC822_SIZE },
e65df911d966a0b4623e287d4669253cc46bbe0fTimo Sirainen { "guid-forced", IMAPC_FEATURE_GUID_FORCED },
b8eb60a9ba83e2f3f6d969e810553eb937be2128Timo Sirainen { "fetch-headers", IMAPC_FEATURE_FETCH_HEADERS },
f424c157faae226f153578442fab1512275889fcTimo Sirainen { "gmail-migration", IMAPC_FEATURE_GMAIL_MIGRATION },
6e1cac3defe84a222b804f3ef41ff558e1a86391Timo Sirainen { "search", IMAPC_FEATURE_SEARCH },
eb1365e61674c54c7c453143356a891fb2e2b3d6Timo Sirainen { "zimbra-workarounds", IMAPC_FEATURE_ZIMBRA_WORKAROUNDS },
df596e34b604e6ac873de9ca92fb5df2a5fed45fTimo Sirainen { "no-examine", IMAPC_FEATURE_NO_EXAMINE },
bd06c77a12bb02871b25dceb749fa955f4a272ffTimo Sirainen { "proxyauth", IMAPC_FEATURE_PROXYAUTH },
53841bd6c67a24b60b64cec41b72eb8b474cfbc7Timo Sirainen { "fetch-msn-workarounds", IMAPC_FEATURE_FETCH_MSN_WORKAROUNDS },
2ddc5efd47452644be369fa86e978cc05a508217Timo Sirainen { "fetch-fix-broken-mails", IMAPC_FEATURE_FETCH_FIX_BROKEN_MAILS },
90355e35d1139e446a99733986a9181bd7f05dd4Timo Sirainen { "modseq", IMAPC_FEATURE_MODSEQ },
30871b77e627d3d6b244305fbea7aeee578f2927Timo Sirainen { "delay-login", IMAPC_FEATURE_DELAY_LOGIN },
e1005f8bf9190e1778010032dbd20c64acb53610Timo Sirainen { "fetch-bodystructure", IMAPC_FEATURE_FETCH_BODYSTRUCTURE },
3c8055b25beef96e7be25895bac34f3eda7ceca1Timo Sirainen { "send-id", IMAPC_FEATURE_SEND_ID },
e01ebd51ac369430ec81945e3c5317d6d6244138Timo Sirainen { "fetch-empty-is-expunged", IMAPC_FEATURE_FETCH_EMPTY_IS_EXPUNGED },
7484b1b01bee9b767f8b67396e1f4af4b0ea1dd3Timo Sirainen { "no-msn-updates", IMAPC_FEATURE_NO_MSN_UPDATES },
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen { NULL, 0 }
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen};
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainenstatic int
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainenimapc_settings_parse_throttle(struct imapc_settings *set,
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen const char *throttle_str, const char **error_r)
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen{
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen const char *const *tmp;
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen tmp = t_strsplit(throttle_str, ":");
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen if (str_array_length(tmp) != 3 ||
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen str_to_uint(tmp[0], &set->throttle_init_msecs) < 0 ||
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen str_to_uint(tmp[1], &set->throttle_max_msecs) < 0 ||
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen str_to_uint(tmp[2], &set->throttle_shrink_min_msecs) < 0) {
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen *error_r = "imapc_features: Invalid throttle settings";
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen return -1;
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen }
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen return 0;
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen}
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainenstatic int
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainenimapc_settings_parse_features(struct imapc_settings *set,
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen const char **error_r)
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen{
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen enum imapc_features features = 0;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen const struct imapc_feature_list *list;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen const char *const *str;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen str = t_strsplit_spaces(set->imapc_features, " ,");
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen for (; *str != NULL; str++) {
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen list = imapc_feature_list;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen for (; list->name != NULL; list++) {
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen if (strcasecmp(*str, list->name) == 0) {
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen features |= list->num;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen break;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen }
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen }
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen if (strncasecmp(*str, "throttle:", 9) == 0) {
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen if (imapc_settings_parse_throttle(set, *str + 9, error_r) < 0)
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen return -1;
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen continue;
de26c21cfadf24c1fa59f06414854d58b3d8baadTimo Sirainen }
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen if (list->name == NULL) {
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen *error_r = t_strdup_printf("imapc_features: "
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen "Unknown feature: %s", *str);
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen return -1;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen }
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen }
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen set->parsed_features = features;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen return 0;
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen}
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainenstatic bool imapc_settings_check(void *_set, pool_t pool ATTR_UNUSED,
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen const char **error_r)
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen{
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen struct imapc_settings *set = _set;
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen
bf5a238894b81b0719c60d6a9fdb66aafd5171dbTimo Sirainen if (set->imapc_max_idle_time == 0) {
bf5a238894b81b0719c60d6a9fdb66aafd5171dbTimo Sirainen *error_r = "imapc_max_idle_time must not be 0";
bf5a238894b81b0719c60d6a9fdb66aafd5171dbTimo Sirainen return FALSE;
bf5a238894b81b0719c60d6a9fdb66aafd5171dbTimo Sirainen }
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen if (imapc_settings_parse_features(set, error_r) < 0)
adcb46fe2c6d1139dfbd8ab08a8fbe96e53f8fd6Timo Sirainen return FALSE;
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen return TRUE;
c5d6b453eccc0962eae967abc10e028a740e1256Timo Sirainen}