summaryrefslogtreecommitdiffstats
path: root/src/dict
diff options
context:
space:
mode:
Diffstat (limited to 'src/dict')
-rw-r--r--src/dict/Makefile.am43
-rw-r--r--src/dict/Makefile.in847
-rw-r--r--src/dict/dict-commands.c797
-rw-r--r--src/dict/dict-commands.h21
-rw-r--r--src/dict/dict-connection.c272
-rw-r--r--src/dict/dict-connection.h48
-rw-r--r--src/dict/dict-init-cache.c164
-rw-r--r--src/dict/dict-init-cache.h12
-rw-r--r--src/dict/dict-settings.c117
-rw-r--r--src/dict/dict-settings.h15
-rw-r--r--src/dict/main.c179
-rw-r--r--src/dict/main.h6
12 files changed, 2521 insertions, 0 deletions
diff --git a/src/dict/Makefile.am b/src/dict/Makefile.am
new file mode 100644
index 0000000..e408046
--- /dev/null
+++ b/src/dict/Makefile.am
@@ -0,0 +1,43 @@
+pkglibexecdir = $(libexecdir)/dovecot
+
+pkglibexec_PROGRAMS = dict
+
+AM_CPPFLAGS = \
+ -I$(top_srcdir)/src/lib \
+ -I$(top_srcdir)/src/lib-master \
+ -I$(top_srcdir)/src/lib-settings \
+ -I$(top_srcdir)/src/lib-dict \
+ -I$(top_srcdir)/src/lib-sql \
+ -DDICT_MODULE_DIR=\""$(moduledir)/dict"\" \
+ -DPKG_RUNDIR=\""$(rundir)"\" \
+ $(BINARY_CFLAGS)
+
+dict_LDFLAGS = -export-dynamic \
+ $(BINARY_LDFLAGS)
+
+libs = \
+ ../lib-dict-backend/libdict_backend.la \
+ $(LIBDOVECOT_SQL)
+
+dict_LDADD = \
+ $(libs) \
+ $(LIBDOVECOT) \
+ $(DICT_LIBS) \
+ $(SQL_LIBS) \
+ -lm
+
+dict_DEPENDENCIES = $(libs) $(LIBDOVECOT_DEPS)
+
+dict_SOURCES = \
+ dict-connection.c \
+ dict-commands.c \
+ dict-settings.c \
+ dict-init-cache.c \
+ main.c
+
+noinst_HEADERS = \
+ dict-connection.h \
+ dict-commands.h \
+ dict-settings.h \
+ dict-init-cache.h \
+ main.h
diff --git a/src/dict/Makefile.in b/src/dict/Makefile.in
new file mode 100644
index 0000000..f4b323f
--- /dev/null
+++ b/src/dict/Makefile.in
@@ -0,0 +1,847 @@
+# Makefile.in generated by automake 1.16.3 from Makefile.am.
+# @configure_input@
+
+# Copyright (C) 1994-2020 Free Software Foundation, Inc.
+
+# This Makefile.in is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+@SET_MAKE@
+
+
+VPATH = @srcdir@
+am__is_gnu_make = { \
+ if test -z '$(MAKELEVEL)'; then \
+ false; \
+ elif test -n '$(MAKE_HOST)'; then \
+ true; \
+ elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \
+ true; \
+ else \
+ false; \
+ fi; \
+}
+am__make_running_with_option = \
+ case $${target_option-} in \
+ ?) ;; \
+ *) echo "am__make_running_with_option: internal error: invalid" \
+ "target option '$${target_option-}' specified" >&2; \
+ exit 1;; \
+ esac; \
+ has_opt=no; \
+ sane_makeflags=$$MAKEFLAGS; \
+ if $(am__is_gnu_make); then \
+ sane_makeflags=$$MFLAGS; \
+ else \
+ case $$MAKEFLAGS in \
+ *\\[\ \ ]*) \
+ bs=\\; \
+ sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \
+ | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \
+ esac; \
+ fi; \
+ skip_next=no; \
+ strip_trailopt () \
+ { \
+ flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \
+ }; \
+ for flg in $$sane_makeflags; do \
+ test $$skip_next = yes && { skip_next=no; continue; }; \
+ case $$flg in \
+ *=*|--*) continue;; \
+ -*I) strip_trailopt 'I'; skip_next=yes;; \
+ -*I?*) strip_trailopt 'I';; \
+ -*O) strip_trailopt 'O'; skip_next=yes;; \
+ -*O?*) strip_trailopt 'O';; \
+ -*l) strip_trailopt 'l'; skip_next=yes;; \
+ -*l?*) strip_trailopt 'l';; \
+ -[dEDm]) skip_next=yes;; \
+ -[JT]) skip_next=yes;; \
+ esac; \
+ case $$flg in \
+ *$$target_option*) has_opt=yes; break;; \
+ esac; \
+ done; \
+ test $$has_opt = yes
+am__make_dryrun = (target_option=n; $(am__make_running_with_option))
+am__make_keepgoing = (target_option=k; $(am__make_running_with_option))
+pkgdatadir = $(datadir)/@PACKAGE@
+pkgincludedir = $(includedir)/@PACKAGE@
+pkglibdir = $(libdir)/@PACKAGE@
+am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
+install_sh_DATA = $(install_sh) -c -m 644
+install_sh_PROGRAM = $(install_sh) -c
+install_sh_SCRIPT = $(install_sh) -c
+INSTALL_HEADER = $(INSTALL_DATA)
+transform = $(program_transform_name)
+NORMAL_INSTALL = :
+PRE_INSTALL = :
+POST_INSTALL = :
+NORMAL_UNINSTALL = :
+PRE_UNINSTALL = :
+POST_UNINSTALL = :
+build_triplet = @build@
+host_triplet = @host@
+pkglibexec_PROGRAMS = dict$(EXEEXT)
+subdir = src/dict
+ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
+am__aclocal_m4_deps = $(top_srcdir)/m4/ac_checktype2.m4 \
+ $(top_srcdir)/m4/ac_typeof.m4 $(top_srcdir)/m4/arc4random.m4 \
+ $(top_srcdir)/m4/blockdev.m4 $(top_srcdir)/m4/c99_vsnprintf.m4 \
+ $(top_srcdir)/m4/clock_gettime.m4 $(top_srcdir)/m4/crypt.m4 \
+ $(top_srcdir)/m4/crypt_xpg6.m4 $(top_srcdir)/m4/dbqlk.m4 \
+ $(top_srcdir)/m4/dirent_dtype.m4 $(top_srcdir)/m4/dovecot.m4 \
+ $(top_srcdir)/m4/fd_passing.m4 $(top_srcdir)/m4/fdatasync.m4 \
+ $(top_srcdir)/m4/flexible_array_member.m4 \
+ $(top_srcdir)/m4/glibc.m4 $(top_srcdir)/m4/gmtime_max.m4 \
+ $(top_srcdir)/m4/gmtime_tm_gmtoff.m4 \
+ $(top_srcdir)/m4/ioloop.m4 $(top_srcdir)/m4/iovec.m4 \
+ $(top_srcdir)/m4/ipv6.m4 $(top_srcdir)/m4/libcap.m4 \
+ $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/libwrap.m4 \
+ $(top_srcdir)/m4/linux_mremap.m4 $(top_srcdir)/m4/ltoptions.m4 \
+ $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \
+ $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/m4/mmap_write.m4 \
+ $(top_srcdir)/m4/mntctl.m4 $(top_srcdir)/m4/modules.m4 \
+ $(top_srcdir)/m4/notify.m4 $(top_srcdir)/m4/nsl.m4 \
+ $(top_srcdir)/m4/off_t_max.m4 $(top_srcdir)/m4/pkg.m4 \
+ $(top_srcdir)/m4/pr_set_dumpable.m4 \
+ $(top_srcdir)/m4/q_quotactl.m4 $(top_srcdir)/m4/quota.m4 \
+ $(top_srcdir)/m4/random.m4 $(top_srcdir)/m4/rlimit.m4 \
+ $(top_srcdir)/m4/sendfile.m4 $(top_srcdir)/m4/size_t_signed.m4 \
+ $(top_srcdir)/m4/sockpeercred.m4 $(top_srcdir)/m4/sql.m4 \
+ $(top_srcdir)/m4/ssl.m4 $(top_srcdir)/m4/st_tim.m4 \
+ $(top_srcdir)/m4/static_array.m4 $(top_srcdir)/m4/test_with.m4 \
+ $(top_srcdir)/m4/time_t.m4 $(top_srcdir)/m4/typeof.m4 \
+ $(top_srcdir)/m4/typeof_dev_t.m4 \
+ $(top_srcdir)/m4/uoff_t_max.m4 $(top_srcdir)/m4/vararg.m4 \
+ $(top_srcdir)/m4/want_apparmor.m4 \
+ $(top_srcdir)/m4/want_bsdauth.m4 \
+ $(top_srcdir)/m4/want_bzlib.m4 \
+ $(top_srcdir)/m4/want_cassandra.m4 \
+ $(top_srcdir)/m4/want_cdb.m4 \
+ $(top_srcdir)/m4/want_checkpassword.m4 \
+ $(top_srcdir)/m4/want_clucene.m4 $(top_srcdir)/m4/want_db.m4 \
+ $(top_srcdir)/m4/want_gssapi.m4 $(top_srcdir)/m4/want_icu.m4 \
+ $(top_srcdir)/m4/want_ldap.m4 $(top_srcdir)/m4/want_lua.m4 \
+ $(top_srcdir)/m4/want_lz4.m4 $(top_srcdir)/m4/want_lzma.m4 \
+ $(top_srcdir)/m4/want_mysql.m4 $(top_srcdir)/m4/want_pam.m4 \
+ $(top_srcdir)/m4/want_passwd.m4 $(top_srcdir)/m4/want_pgsql.m4 \
+ $(top_srcdir)/m4/want_prefetch.m4 \
+ $(top_srcdir)/m4/want_shadow.m4 \
+ $(top_srcdir)/m4/want_sodium.m4 $(top_srcdir)/m4/want_solr.m4 \
+ $(top_srcdir)/m4/want_sqlite.m4 \
+ $(top_srcdir)/m4/want_stemmer.m4 \
+ $(top_srcdir)/m4/want_systemd.m4 \
+ $(top_srcdir)/m4/want_textcat.m4 \
+ $(top_srcdir)/m4/want_unwind.m4 $(top_srcdir)/m4/want_zlib.m4 \
+ $(top_srcdir)/m4/want_zstd.m4 $(top_srcdir)/configure.ac
+am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
+ $(ACLOCAL_M4)
+DIST_COMMON = $(srcdir)/Makefile.am $(noinst_HEADERS) \
+ $(am__DIST_COMMON)
+mkinstalldirs = $(install_sh) -d
+CONFIG_HEADER = $(top_builddir)/config.h
+CONFIG_CLEAN_FILES =
+CONFIG_CLEAN_VPATH_FILES =
+am__installdirs = "$(DESTDIR)$(pkglibexecdir)"
+PROGRAMS = $(pkglibexec_PROGRAMS)
+am_dict_OBJECTS = dict-connection.$(OBJEXT) dict-commands.$(OBJEXT) \
+ dict-settings.$(OBJEXT) dict-init-cache.$(OBJEXT) \
+ main.$(OBJEXT)
+dict_OBJECTS = $(am_dict_OBJECTS)
+am__DEPENDENCIES_1 =
+am__DEPENDENCIES_2 = ../lib-dict-backend/libdict_backend.la \
+ $(am__DEPENDENCIES_1)
+AM_V_lt = $(am__v_lt_@AM_V@)
+am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
+am__v_lt_0 = --silent
+am__v_lt_1 =
+dict_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
+ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
+ $(dict_LDFLAGS) $(LDFLAGS) -o $@
+AM_V_P = $(am__v_P_@AM_V@)
+am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
+am__v_P_0 = false
+am__v_P_1 = :
+AM_V_GEN = $(am__v_GEN_@AM_V@)
+am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@)
+am__v_GEN_0 = @echo " GEN " $@;
+am__v_GEN_1 =
+AM_V_at = $(am__v_at_@AM_V@)
+am__v_at_ = $(am__v_at_@AM_DEFAULT_V@)
+am__v_at_0 = @
+am__v_at_1 =
+DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
+depcomp = $(SHELL) $(top_srcdir)/depcomp
+am__maybe_remake_depfiles = depfiles
+am__depfiles_remade = ./$(DEPDIR)/dict-commands.Po \
+ ./$(DEPDIR)/dict-connection.Po ./$(DEPDIR)/dict-init-cache.Po \
+ ./$(DEPDIR)/dict-settings.Po ./$(DEPDIR)/main.Po
+am__mv = mv -f
+COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
+ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
+ $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \
+ $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \
+ $(AM_CFLAGS) $(CFLAGS)
+AM_V_CC = $(am__v_CC_@AM_V@)
+am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@)
+am__v_CC_0 = @echo " CC " $@;
+am__v_CC_1 =
+CCLD = $(CC)
+LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
+ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
+ $(AM_LDFLAGS) $(LDFLAGS) -o $@
+AM_V_CCLD = $(am__v_CCLD_@AM_V@)
+am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
+am__v_CCLD_0 = @echo " CCLD " $@;
+am__v_CCLD_1 =
+SOURCES = $(dict_SOURCES)
+DIST_SOURCES = $(dict_SOURCES)
+am__can_run_installinfo = \
+ case $$AM_UPDATE_INFO_DIR in \
+ n|no|NO) false;; \
+ *) (install-info --version) >/dev/null 2>&1;; \
+ esac
+HEADERS = $(noinst_HEADERS)
+am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP)
+# Read a list of newline-separated strings from the standard input,
+# and print each of them once, without duplicates. Input order is
+# *not* preserved.
+am__uniquify_input = $(AWK) '\
+ BEGIN { nonempty = 0; } \
+ { items[$$0] = 1; nonempty = 1; } \
+ END { if (nonempty) { for (i in items) print i; }; } \
+'
+# Make sure the list of sources is unique. This is necessary because,
+# e.g., the same source file might be shared among _SOURCES variables
+# for different programs/libraries.
+am__define_uniq_tagged_files = \
+ list='$(am__tagged_files)'; \
+ unique=`for i in $$list; do \
+ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+ done | $(am__uniquify_input)`
+ETAGS = etags
+CTAGS = ctags
+am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp
+DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
+pkglibexecdir = $(libexecdir)/dovecot
+ACLOCAL = @ACLOCAL@
+ACLOCAL_AMFLAGS = @ACLOCAL_AMFLAGS@
+AMTAR = @AMTAR@
+AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@
+APPARMOR_LIBS = @APPARMOR_LIBS@
+AR = @AR@
+AUTH_CFLAGS = @AUTH_CFLAGS@
+AUTH_LIBS = @AUTH_LIBS@
+AUTOCONF = @AUTOCONF@
+AUTOHEADER = @AUTOHEADER@
+AUTOMAKE = @AUTOMAKE@
+AWK = @AWK@
+BINARY_CFLAGS = @BINARY_CFLAGS@
+BINARY_LDFLAGS = @BINARY_LDFLAGS@
+BISON = @BISON@
+CASSANDRA_CFLAGS = @CASSANDRA_CFLAGS@
+CASSANDRA_LIBS = @CASSANDRA_LIBS@
+CC = @CC@
+CCDEPMODE = @CCDEPMODE@
+CDB_LIBS = @CDB_LIBS@
+CFLAGS = @CFLAGS@
+CLUCENE_CFLAGS = @CLUCENE_CFLAGS@
+CLUCENE_LIBS = @CLUCENE_LIBS@
+COMPRESS_LIBS = @COMPRESS_LIBS@
+CPP = @CPP@
+CPPFLAGS = @CPPFLAGS@
+CRYPT_LIBS = @CRYPT_LIBS@
+CXX = @CXX@
+CXXCPP = @CXXCPP@
+CXXDEPMODE = @CXXDEPMODE@
+CXXFLAGS = @CXXFLAGS@
+CYGPATH_W = @CYGPATH_W@
+DEFS = @DEFS@
+DEPDIR = @DEPDIR@
+DICT_LIBS = @DICT_LIBS@
+DLLIB = @DLLIB@
+DLLTOOL = @DLLTOOL@
+DSYMUTIL = @DSYMUTIL@
+DUMPBIN = @DUMPBIN@
+ECHO_C = @ECHO_C@
+ECHO_N = @ECHO_N@
+ECHO_T = @ECHO_T@
+EGREP = @EGREP@
+EXEEXT = @EXEEXT@
+FGREP = @FGREP@
+FLEX = @FLEX@
+FUZZER_CPPFLAGS = @FUZZER_CPPFLAGS@
+FUZZER_LDFLAGS = @FUZZER_LDFLAGS@
+GREP = @GREP@
+INSTALL = @INSTALL@
+INSTALL_DATA = @INSTALL_DATA@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_SCRIPT = @INSTALL_SCRIPT@
+INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
+KRB5CONFIG = @KRB5CONFIG@
+KRB5_CFLAGS = @KRB5_CFLAGS@
+KRB5_LIBS = @KRB5_LIBS@
+LD = @LD@
+LDAP_LIBS = @LDAP_LIBS@
+LDFLAGS = @LDFLAGS@
+LD_NO_WHOLE_ARCHIVE = @LD_NO_WHOLE_ARCHIVE@
+LD_WHOLE_ARCHIVE = @LD_WHOLE_ARCHIVE@
+LIBCAP = @LIBCAP@
+LIBDOVECOT = @LIBDOVECOT@
+LIBDOVECOT_COMPRESS = @LIBDOVECOT_COMPRESS@
+LIBDOVECOT_DEPS = @LIBDOVECOT_DEPS@
+LIBDOVECOT_DSYNC = @LIBDOVECOT_DSYNC@
+LIBDOVECOT_LA_LIBS = @LIBDOVECOT_LA_LIBS@
+LIBDOVECOT_LDA = @LIBDOVECOT_LDA@
+LIBDOVECOT_LDAP = @LIBDOVECOT_LDAP@
+LIBDOVECOT_LIBFTS = @LIBDOVECOT_LIBFTS@
+LIBDOVECOT_LIBFTS_DEPS = @LIBDOVECOT_LIBFTS_DEPS@
+LIBDOVECOT_LOGIN = @LIBDOVECOT_LOGIN@
+LIBDOVECOT_LUA = @LIBDOVECOT_LUA@
+LIBDOVECOT_LUA_DEPS = @LIBDOVECOT_LUA_DEPS@
+LIBDOVECOT_SQL = @LIBDOVECOT_SQL@
+LIBDOVECOT_STORAGE = @LIBDOVECOT_STORAGE@
+LIBDOVECOT_STORAGE_DEPS = @LIBDOVECOT_STORAGE_DEPS@
+LIBEXTTEXTCAT_CFLAGS = @LIBEXTTEXTCAT_CFLAGS@
+LIBEXTTEXTCAT_LIBS = @LIBEXTTEXTCAT_LIBS@
+LIBICONV = @LIBICONV@
+LIBICU_CFLAGS = @LIBICU_CFLAGS@
+LIBICU_LIBS = @LIBICU_LIBS@
+LIBOBJS = @LIBOBJS@
+LIBS = @LIBS@
+LIBSODIUM_CFLAGS = @LIBSODIUM_CFLAGS@
+LIBSODIUM_LIBS = @LIBSODIUM_LIBS@
+LIBTIRPC_CFLAGS = @LIBTIRPC_CFLAGS@
+LIBTIRPC_LIBS = @LIBTIRPC_LIBS@
+LIBTOOL = @LIBTOOL@
+LIBUNWIND_CFLAGS = @LIBUNWIND_CFLAGS@
+LIBUNWIND_LIBS = @LIBUNWIND_LIBS@
+LIBWRAP_LIBS = @LIBWRAP_LIBS@
+LINKED_STORAGE_LDADD = @LINKED_STORAGE_LDADD@
+LIPO = @LIPO@
+LN_S = @LN_S@
+LTLIBICONV = @LTLIBICONV@
+LTLIBOBJS = @LTLIBOBJS@
+LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@
+LUA_CFLAGS = @LUA_CFLAGS@
+LUA_LIBS = @LUA_LIBS@
+MAINT = @MAINT@
+MAKEINFO = @MAKEINFO@
+MANIFEST_TOOL = @MANIFEST_TOOL@
+MKDIR_P = @MKDIR_P@
+MODULE_LIBS = @MODULE_LIBS@
+MODULE_SUFFIX = @MODULE_SUFFIX@
+MYSQL_CFLAGS = @MYSQL_CFLAGS@
+MYSQL_CONFIG = @MYSQL_CONFIG@
+MYSQL_LIBS = @MYSQL_LIBS@
+NM = @NM@
+NMEDIT = @NMEDIT@
+NOPLUGIN_LDFLAGS = @NOPLUGIN_LDFLAGS@
+OBJDUMP = @OBJDUMP@
+OBJEXT = @OBJEXT@
+OTOOL = @OTOOL@
+OTOOL64 = @OTOOL64@
+PACKAGE = @PACKAGE@
+PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
+PACKAGE_NAME = @PACKAGE_NAME@
+PACKAGE_STRING = @PACKAGE_STRING@
+PACKAGE_TARNAME = @PACKAGE_TARNAME@
+PACKAGE_URL = @PACKAGE_URL@
+PACKAGE_VERSION = @PACKAGE_VERSION@
+PANDOC = @PANDOC@
+PATH_SEPARATOR = @PATH_SEPARATOR@
+PGSQL_CFLAGS = @PGSQL_CFLAGS@
+PGSQL_LIBS = @PGSQL_LIBS@
+PG_CONFIG = @PG_CONFIG@
+PIE_CFLAGS = @PIE_CFLAGS@
+PIE_LDFLAGS = @PIE_LDFLAGS@
+PKG_CONFIG = @PKG_CONFIG@
+PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@
+PKG_CONFIG_PATH = @PKG_CONFIG_PATH@
+QUOTA_LIBS = @QUOTA_LIBS@
+RANLIB = @RANLIB@
+RELRO_LDFLAGS = @RELRO_LDFLAGS@
+RPCGEN = @RPCGEN@
+RUN_TEST = @RUN_TEST@
+SED = @SED@
+SETTING_FILES = @SETTING_FILES@
+SET_MAKE = @SET_MAKE@
+SHELL = @SHELL@
+SQLITE_CFLAGS = @SQLITE_CFLAGS@
+SQLITE_LIBS = @SQLITE_LIBS@
+SQL_CFLAGS = @SQL_CFLAGS@
+SQL_LIBS = @SQL_LIBS@
+SSL_CFLAGS = @SSL_CFLAGS@
+SSL_LIBS = @SSL_LIBS@
+STRIP = @STRIP@
+SYSTEMD_CFLAGS = @SYSTEMD_CFLAGS@
+SYSTEMD_LIBS = @SYSTEMD_LIBS@
+VALGRIND = @VALGRIND@
+VERSION = @VERSION@
+ZSTD_CFLAGS = @ZSTD_CFLAGS@
+ZSTD_LIBS = @ZSTD_LIBS@
+abs_builddir = @abs_builddir@
+abs_srcdir = @abs_srcdir@
+abs_top_builddir = @abs_top_builddir@
+abs_top_srcdir = @abs_top_srcdir@
+ac_ct_AR = @ac_ct_AR@
+ac_ct_CC = @ac_ct_CC@
+ac_ct_CXX = @ac_ct_CXX@
+ac_ct_DUMPBIN = @ac_ct_DUMPBIN@
+am__include = @am__include@
+am__leading_dot = @am__leading_dot@
+am__quote = @am__quote@
+am__tar = @am__tar@
+am__untar = @am__untar@
+bindir = @bindir@
+build = @build@
+build_alias = @build_alias@
+build_cpu = @build_cpu@
+build_os = @build_os@
+build_vendor = @build_vendor@
+builddir = @builddir@
+datadir = @datadir@
+datarootdir = @datarootdir@
+dict_drivers = @dict_drivers@
+docdir = @docdir@
+dvidir = @dvidir@
+exec_prefix = @exec_prefix@
+host = @host@
+host_alias = @host_alias@
+host_cpu = @host_cpu@
+host_os = @host_os@
+host_vendor = @host_vendor@
+htmldir = @htmldir@
+includedir = @includedir@
+infodir = @infodir@
+install_sh = @install_sh@
+libdir = @libdir@
+libexecdir = @libexecdir@
+localedir = @localedir@
+localstatedir = @localstatedir@
+mandir = @mandir@
+mkdir_p = @mkdir_p@
+moduledir = @moduledir@
+oldincludedir = @oldincludedir@
+pdfdir = @pdfdir@
+prefix = @prefix@
+program_transform_name = @program_transform_name@
+psdir = @psdir@
+rundir = @rundir@
+runstatedir = @runstatedir@
+sbindir = @sbindir@
+sharedstatedir = @sharedstatedir@
+sql_drivers = @sql_drivers@
+srcdir = @srcdir@
+ssldir = @ssldir@
+statedir = @statedir@
+sysconfdir = @sysconfdir@
+systemdservicetype = @systemdservicetype@
+systemdsystemunitdir = @systemdsystemunitdir@
+target_alias = @target_alias@
+top_build_prefix = @top_build_prefix@
+top_builddir = @top_builddir@
+top_srcdir = @top_srcdir@
+AM_CPPFLAGS = \
+ -I$(top_srcdir)/src/lib \
+ -I$(top_srcdir)/src/lib-master \
+ -I$(top_srcdir)/src/lib-settings \
+ -I$(top_srcdir)/src/lib-dict \
+ -I$(top_srcdir)/src/lib-sql \
+ -DDICT_MODULE_DIR=\""$(moduledir)/dict"\" \
+ -DPKG_RUNDIR=\""$(rundir)"\" \
+ $(BINARY_CFLAGS)
+
+dict_LDFLAGS = -export-dynamic \
+ $(BINARY_LDFLAGS)
+
+libs = \
+ ../lib-dict-backend/libdict_backend.la \
+ $(LIBDOVECOT_SQL)
+
+dict_LDADD = \
+ $(libs) \
+ $(LIBDOVECOT) \
+ $(DICT_LIBS) \
+ $(SQL_LIBS) \
+ -lm
+
+dict_DEPENDENCIES = $(libs) $(LIBDOVECOT_DEPS)
+dict_SOURCES = \
+ dict-connection.c \
+ dict-commands.c \
+ dict-settings.c \
+ dict-init-cache.c \
+ main.c
+
+noinst_HEADERS = \
+ dict-connection.h \
+ dict-commands.h \
+ dict-settings.h \
+ dict-init-cache.h \
+ main.h
+
+all: all-am
+
+.SUFFIXES:
+.SUFFIXES: .c .lo .o .obj
+$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps)
+ @for dep in $?; do \
+ case '$(am__configure_deps)' in \
+ *$$dep*) \
+ ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \
+ && { if test -f $@; then exit 0; else break; fi; }; \
+ exit 1;; \
+ esac; \
+ done; \
+ echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/dict/Makefile'; \
+ $(am__cd) $(top_srcdir) && \
+ $(AUTOMAKE) --foreign src/dict/Makefile
+Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
+ @case '$?' in \
+ *config.status*) \
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \
+ *) \
+ echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \
+ cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \
+ esac;
+
+$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+
+$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+$(am__aclocal_m4_deps):
+install-pkglibexecPROGRAMS: $(pkglibexec_PROGRAMS)
+ @$(NORMAL_INSTALL)
+ @list='$(pkglibexec_PROGRAMS)'; test -n "$(pkglibexecdir)" || list=; \
+ if test -n "$$list"; then \
+ echo " $(MKDIR_P) '$(DESTDIR)$(pkglibexecdir)'"; \
+ $(MKDIR_P) "$(DESTDIR)$(pkglibexecdir)" || exit 1; \
+ fi; \
+ for p in $$list; do echo "$$p $$p"; done | \
+ sed 's/$(EXEEXT)$$//' | \
+ while read p p1; do if test -f $$p \
+ || test -f $$p1 \
+ ; then echo "$$p"; echo "$$p"; else :; fi; \
+ done | \
+ sed -e 'p;s,.*/,,;n;h' \
+ -e 's|.*|.|' \
+ -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | \
+ sed 'N;N;N;s,\n, ,g' | \
+ $(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } \
+ { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } \
+ if ($$2 == $$4) files[d] = files[d] " " $$1; \
+ else { print "f", $$3 "/" $$4, $$1; } } \
+ END { for (d in files) print "f", d, files[d] }' | \
+ while read type dir files; do \
+ if test "$$dir" = .; then dir=; else dir=/$$dir; fi; \
+ test -z "$$files" || { \
+ echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(pkglibexecdir)$$dir'"; \
+ $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(pkglibexecdir)$$dir" || exit $$?; \
+ } \
+ ; done
+
+uninstall-pkglibexecPROGRAMS:
+ @$(NORMAL_UNINSTALL)
+ @list='$(pkglibexec_PROGRAMS)'; test -n "$(pkglibexecdir)" || list=; \
+ files=`for p in $$list; do echo "$$p"; done | \
+ sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' \
+ -e 's/$$/$(EXEEXT)/' \
+ `; \
+ test -n "$$list" || exit 0; \
+ echo " ( cd '$(DESTDIR)$(pkglibexecdir)' && rm -f" $$files ")"; \
+ cd "$(DESTDIR)$(pkglibexecdir)" && rm -f $$files
+
+clean-pkglibexecPROGRAMS:
+ @list='$(pkglibexec_PROGRAMS)'; test -n "$$list" || exit 0; \
+ echo " rm -f" $$list; \
+ rm -f $$list || exit $$?; \
+ test -n "$(EXEEXT)" || exit 0; \
+ list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \
+ echo " rm -f" $$list; \
+ rm -f $$list
+
+dict$(EXEEXT): $(dict_OBJECTS) $(dict_DEPENDENCIES) $(EXTRA_dict_DEPENDENCIES)
+ @rm -f dict$(EXEEXT)
+ $(AM_V_CCLD)$(dict_LINK) $(dict_OBJECTS) $(dict_LDADD) $(LIBS)
+
+mostlyclean-compile:
+ -rm -f *.$(OBJEXT)
+
+distclean-compile:
+ -rm -f *.tab.c
+
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dict-commands.Po@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dict-connection.Po@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dict-init-cache.Po@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dict-settings.Po@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ # am--include-marker
+
+$(am__depfiles_remade):
+ @$(MKDIR_P) $(@D)
+ @echo '# dummy' >$@-t && $(am__mv) $@-t $@
+
+am--depfiles: $(am__depfiles_remade)
+
+.c.o:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $<
+
+.c.obj:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
+
+.c.lo:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $<
+
+mostlyclean-libtool:
+ -rm -f *.lo
+
+clean-libtool:
+ -rm -rf .libs _libs
+
+ID: $(am__tagged_files)
+ $(am__define_uniq_tagged_files); mkid -fID $$unique
+tags: tags-am
+TAGS: tags
+
+tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files)
+ set x; \
+ here=`pwd`; \
+ $(am__define_uniq_tagged_files); \
+ shift; \
+ if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \
+ test -n "$$unique" || unique=$$empty_fix; \
+ if test $$# -gt 0; then \
+ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+ "$$@" $$unique; \
+ else \
+ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+ $$unique; \
+ fi; \
+ fi
+ctags: ctags-am
+
+CTAGS: ctags
+ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files)
+ $(am__define_uniq_tagged_files); \
+ test -z "$(CTAGS_ARGS)$$unique" \
+ || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \
+ $$unique
+
+GTAGS:
+ here=`$(am__cd) $(top_builddir) && pwd` \
+ && $(am__cd) $(top_srcdir) \
+ && gtags -i $(GTAGS_ARGS) "$$here"
+cscopelist: cscopelist-am
+
+cscopelist-am: $(am__tagged_files)
+ list='$(am__tagged_files)'; \
+ case "$(srcdir)" in \
+ [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \
+ *) sdir=$(subdir)/$(srcdir) ;; \
+ esac; \
+ for i in $$list; do \
+ if test -f "$$i"; then \
+ echo "$(subdir)/$$i"; \
+ else \
+ echo "$$sdir/$$i"; \
+ fi; \
+ done >> $(top_builddir)/cscope.files
+
+distclean-tags:
+ -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
+
+distdir: $(BUILT_SOURCES)
+ $(MAKE) $(AM_MAKEFLAGS) distdir-am
+
+distdir-am: $(DISTFILES)
+ @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
+ topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
+ list='$(DISTFILES)'; \
+ dist_files=`for file in $$list; do echo $$file; done | \
+ sed -e "s|^$$srcdirstrip/||;t" \
+ -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \
+ case $$dist_files in \
+ */*) $(MKDIR_P) `echo "$$dist_files" | \
+ sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \
+ sort -u` ;; \
+ esac; \
+ for file in $$dist_files; do \
+ if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
+ if test -d $$d/$$file; then \
+ dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \
+ if test -d "$(distdir)/$$file"; then \
+ find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
+ fi; \
+ if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
+ cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \
+ find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
+ fi; \
+ cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \
+ else \
+ test -f "$(distdir)/$$file" \
+ || cp -p $$d/$$file "$(distdir)/$$file" \
+ || exit 1; \
+ fi; \
+ done
+check-am: all-am
+check: check-am
+all-am: Makefile $(PROGRAMS) $(HEADERS)
+installdirs:
+ for dir in "$(DESTDIR)$(pkglibexecdir)"; do \
+ test -z "$$dir" || $(MKDIR_P) "$$dir"; \
+ done
+install: install-am
+install-exec: install-exec-am
+install-data: install-data-am
+uninstall: uninstall-am
+
+install-am: all-am
+ @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+installcheck: installcheck-am
+install-strip:
+ if test -z '$(STRIP)'; then \
+ $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
+ install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
+ install; \
+ else \
+ $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
+ install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
+ "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \
+ fi
+mostlyclean-generic:
+
+clean-generic:
+
+distclean-generic:
+ -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
+ -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)
+
+maintainer-clean-generic:
+ @echo "This command is intended for maintainers to use"
+ @echo "it deletes files that may require special tools to rebuild."
+clean: clean-am
+
+clean-am: clean-generic clean-libtool clean-pkglibexecPROGRAMS \
+ mostlyclean-am
+
+distclean: distclean-am
+ -rm -f ./$(DEPDIR)/dict-commands.Po
+ -rm -f ./$(DEPDIR)/dict-connection.Po
+ -rm -f ./$(DEPDIR)/dict-init-cache.Po
+ -rm -f ./$(DEPDIR)/dict-settings.Po
+ -rm -f ./$(DEPDIR)/main.Po
+ -rm -f Makefile
+distclean-am: clean-am distclean-compile distclean-generic \
+ distclean-tags
+
+dvi: dvi-am
+
+dvi-am:
+
+html: html-am
+
+html-am:
+
+info: info-am
+
+info-am:
+
+install-data-am:
+
+install-dvi: install-dvi-am
+
+install-dvi-am:
+
+install-exec-am: install-pkglibexecPROGRAMS
+
+install-html: install-html-am
+
+install-html-am:
+
+install-info: install-info-am
+
+install-info-am:
+
+install-man:
+
+install-pdf: install-pdf-am
+
+install-pdf-am:
+
+install-ps: install-ps-am
+
+install-ps-am:
+
+installcheck-am:
+
+maintainer-clean: maintainer-clean-am
+ -rm -f ./$(DEPDIR)/dict-commands.Po
+ -rm -f ./$(DEPDIR)/dict-connection.Po
+ -rm -f ./$(DEPDIR)/dict-init-cache.Po
+ -rm -f ./$(DEPDIR)/dict-settings.Po
+ -rm -f ./$(DEPDIR)/main.Po
+ -rm -f Makefile
+maintainer-clean-am: distclean-am maintainer-clean-generic
+
+mostlyclean: mostlyclean-am
+
+mostlyclean-am: mostlyclean-compile mostlyclean-generic \
+ mostlyclean-libtool
+
+pdf: pdf-am
+
+pdf-am:
+
+ps: ps-am
+
+ps-am:
+
+uninstall-am: uninstall-pkglibexecPROGRAMS
+
+.MAKE: install-am install-strip
+
+.PHONY: CTAGS GTAGS TAGS all all-am am--depfiles check check-am clean \
+ clean-generic clean-libtool clean-pkglibexecPROGRAMS \
+ cscopelist-am ctags ctags-am distclean distclean-compile \
+ distclean-generic distclean-libtool distclean-tags distdir dvi \
+ dvi-am html html-am info info-am install install-am \
+ install-data install-data-am install-dvi install-dvi-am \
+ install-exec install-exec-am install-html install-html-am \
+ install-info install-info-am install-man install-pdf \
+ install-pdf-am install-pkglibexecPROGRAMS install-ps \
+ install-ps-am install-strip installcheck installcheck-am \
+ installdirs maintainer-clean maintainer-clean-generic \
+ mostlyclean mostlyclean-compile mostlyclean-generic \
+ mostlyclean-libtool pdf pdf-am ps ps-am tags tags-am uninstall \
+ uninstall-am uninstall-pkglibexecPROGRAMS
+
+.PRECIOUS: Makefile
+
+
+# Tell versions [3.59,3.63) of GNU make to not export all variables.
+# Otherwise a system limit (for SysV at least) may be exceeded.
+.NOEXPORT:
diff --git a/src/dict/dict-commands.c b/src/dict/dict-commands.c
new file mode 100644
index 0000000..da6acce
--- /dev/null
+++ b/src/dict/dict-commands.c
@@ -0,0 +1,797 @@
+/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "ostream.h"
+#include "str.h"
+#include "strescape.h"
+#include "stats-dist.h"
+#include "time-util.h"
+#include "dict-private.h"
+#include "dict-client.h"
+#include "dict-settings.h"
+#include "dict-connection.h"
+#include "dict-commands.h"
+#include "main.h"
+
+#define DICT_CLIENT_PROTOCOL_TIMINGS_MIN_VERSION 1
+#define DICT_CLIENT_PROTOCOL_UNORDERED_MIN_VERSION 1
+#define DICT_OUTPUT_OPTIMAL_SIZE 1024
+
+struct dict_cmd_func {
+ enum dict_protocol_cmd cmd;
+ int (*func)(struct dict_connection_cmd *cmd, const char *const *args);
+};
+
+struct dict_connection_cmd {
+ const struct dict_cmd_func *cmd;
+ struct dict_connection *conn;
+ struct timeval start_timeval;
+ struct event *event;
+ char *reply;
+
+ struct dict_iterate_context *iter;
+ enum dict_iterate_flags iter_flags;
+
+ unsigned int async_reply_id;
+ unsigned int trans_id; /* obsolete */
+ unsigned int rows;
+
+ bool uncork_pending;
+};
+
+struct dict_command_stats cmd_stats;
+
+static int cmd_iterate_flush(struct dict_connection_cmd *cmd);
+
+static bool dict_connection_cmd_output_more(struct dict_connection_cmd *cmd);
+
+static void dict_connection_cmd_free(struct dict_connection_cmd *cmd)
+{
+ const char *error;
+
+ if (dict_iterate_deinit(&cmd->iter, &error) < 0)
+ e_error(cmd->event, "dict_iterate() failed: %s", error);
+ i_free(cmd->reply);
+ if (cmd->uncork_pending)
+ o_stream_uncork(cmd->conn->conn.output);
+
+ if (dict_connection_unref(cmd->conn) && !cmd->conn->destroyed)
+ connection_input_resume(&cmd->conn->conn);
+ event_unref(&cmd->event);
+ i_free(cmd);
+}
+
+static void dict_connection_cmd_remove(struct dict_connection_cmd *cmd)
+{
+ struct dict_connection_cmd *const *cmds;
+ unsigned int i, count;
+
+ cmds = array_get(&cmd->conn->cmds, &count);
+ for (i = 0; i < count; i++) {
+ if (cmds[i] == cmd) {
+ array_delete(&cmd->conn->cmds, i, 1);
+ dict_connection_cmd_free(cmd);
+ return;
+ }
+ }
+ i_unreached();
+}
+
+static void dict_connection_cmds_flush(struct dict_connection *conn)
+{
+ struct dict_connection_cmd *cmd, *const *first_cmdp;
+
+ i_assert(conn->conn.minor_version < DICT_CLIENT_PROTOCOL_UNORDERED_MIN_VERSION);
+
+ dict_connection_ref(conn);
+ while (array_count(&conn->cmds) > 0) {
+ first_cmdp = array_front(&conn->cmds);
+ cmd = *first_cmdp;
+
+ i_assert(cmd->async_reply_id == 0);
+
+ /* we may be able to start outputting iterations now. */
+ if (cmd->iter != NULL)
+ (void)cmd_iterate_flush(cmd);
+
+ if (cmd->reply == NULL) {
+ /* command not finished yet */
+ break;
+ }
+
+ o_stream_nsend_str(conn->conn.output, cmd->reply);
+ dict_connection_cmd_remove(cmd);
+ }
+ dict_connection_unref_safe(conn);
+}
+
+static void dict_connection_cmd_try_flush(struct dict_connection_cmd **_cmd)
+{
+ struct dict_connection_cmd *cmd = *_cmd;
+
+ *_cmd = NULL;
+ if (cmd->conn->conn.minor_version < DICT_CLIENT_PROTOCOL_UNORDERED_MIN_VERSION) {
+ dict_connection_cmds_flush(cmd->conn);
+ return;
+ }
+ i_assert(cmd->async_reply_id != 0);
+ i_assert(cmd->reply != NULL);
+
+ o_stream_nsend_str(cmd->conn->conn.output, t_strdup_printf("%c%u\t%s",
+ DICT_PROTOCOL_REPLY_ASYNC_REPLY,
+ cmd->async_reply_id, cmd->reply));
+ dict_connection_cmd_remove(cmd);
+}
+
+static void dict_connection_cmd_async(struct dict_connection_cmd *cmd)
+{
+ if (cmd->conn->conn.minor_version < DICT_CLIENT_PROTOCOL_UNORDERED_MIN_VERSION)
+ return;
+
+ i_assert(cmd->async_reply_id == 0);
+ cmd->async_reply_id = ++cmd->conn->async_id_counter;
+ if (cmd->async_reply_id == 0)
+ cmd->async_reply_id = ++cmd->conn->async_id_counter;
+ o_stream_nsend_str(cmd->conn->conn.output, t_strdup_printf("%c%u\n",
+ DICT_PROTOCOL_REPLY_ASYNC_ID, cmd->async_reply_id));
+}
+
+static void
+cmd_stats_update(struct dict_connection_cmd *cmd, struct stats_dist *stats)
+{
+ long long diff;
+
+ if (!dict_settings->verbose_proctitle)
+ return;
+
+ diff = timeval_diff_usecs(&ioloop_timeval, &cmd->start_timeval);
+ if (diff < 0)
+ diff = 0;
+ stats_dist_add(stats, diff);
+ dict_proctitle_update_later();
+}
+
+static void
+dict_cmd_reply_handle_stats(struct dict_connection_cmd *cmd,
+ string_t *str, struct stats_dist *stats)
+{
+ io_loop_time_refresh();
+ cmd_stats_update(cmd, stats);
+
+ if (cmd->conn->conn.minor_version < DICT_CLIENT_PROTOCOL_TIMINGS_MIN_VERSION)
+ return;
+ str_printfa(str, "\t%ld\t%u\t%ld\t%u",
+ (long)cmd->start_timeval.tv_sec,
+ (unsigned int)cmd->start_timeval.tv_usec,
+ (long)ioloop_timeval.tv_sec,
+ (unsigned int)ioloop_timeval.tv_usec);
+}
+
+static void
+cmd_lookup_write_reply(struct dict_connection_cmd *cmd,
+ const char *const *values, string_t *str)
+{
+ string_t *tmp;
+
+ i_assert(values[0] != NULL);
+
+ if (cmd->conn->conn.minor_version < DICT_CLIENT_PROTOCOL_VERSION_MIN_MULTI_OK ||
+ values[1] == NULL) {
+ str_append_c(str, DICT_PROTOCOL_REPLY_OK);
+ str_append_tabescaped(str, values[0]);
+ return;
+ }
+ /* the results get double-tabescaped so they end up becoming a single
+ parameter */
+ tmp = t_str_new(128);
+ for (unsigned int i = 0; values[i] != NULL; i++) {
+ str_append_c(tmp, '\t');
+ str_append_tabescaped(tmp, values[i]);
+ }
+ str_append_c(str, DICT_PROTOCOL_REPLY_MULTI_OK);
+ str_append_tabescaped(str, str_c(tmp) + 1);
+}
+
+static void
+cmd_lookup_callback(const struct dict_lookup_result *result,
+ struct dict_connection_cmd *cmd)
+{
+ string_t *str = t_str_new(128);
+
+ event_set_name(cmd->event, "dict_server_lookup_finished");
+ if (result->ret > 0) {
+ cmd_lookup_write_reply(cmd, result->values, str);
+ e_debug(cmd->event, "Lookup finished");
+ } else if (result->ret == 0) {
+ event_add_str(cmd->event, "key_not_found", "yes");
+ str_append_c(str, DICT_PROTOCOL_REPLY_NOTFOUND);
+ e_debug(cmd->event, "Lookup finished without results");
+ } else {
+ event_add_str(cmd->event, "error", result->error);
+ e_error(cmd->event, "Lookup failed: %s", result->error);
+ str_append_c(str, DICT_PROTOCOL_REPLY_FAIL);
+ str_append_tabescaped(str, result->error);
+ }
+ dict_cmd_reply_handle_stats(cmd, str, cmd_stats.lookups);
+ str_append_c(str, '\n');
+
+ cmd->reply = i_strdup(str_c(str));
+ dict_connection_cmd_try_flush(&cmd);
+}
+
+static int cmd_lookup(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ const char *username;
+
+ if (str_array_length(args) < 1) {
+ e_error(cmd->event, "LOOKUP: broken input");
+ return -1;
+ }
+ username = args[1];
+
+ /* <key> [<username>] */
+ dict_connection_cmd_async(cmd);
+ event_add_str(cmd->event, "key", args[0]);
+ event_add_str(cmd->event, "user", username);
+ const struct dict_op_settings set = {
+ .username = username,
+ };
+ dict_lookup_async(cmd->conn->dict, &set, args[0], cmd_lookup_callback, cmd);
+ return 1;
+}
+
+static bool dict_connection_flush_if_full(struct dict_connection *conn)
+{
+ if (o_stream_get_buffer_used_size(conn->conn.output) >
+ DICT_OUTPUT_OPTIMAL_SIZE) {
+ if (o_stream_flush(conn->conn.output) <= 0) {
+ /* continue later when there's more space
+ in output buffer */
+ o_stream_set_flush_pending(conn->conn.output, TRUE);
+ conn->iter_flush_pending = TRUE;
+ return FALSE;
+ }
+ /* flushed everything, continue */
+ }
+ return TRUE;
+}
+
+static void
+cmd_iterate_flush_finish(struct dict_connection_cmd *cmd, string_t *str)
+{
+ const char *error;
+
+ event_set_name(cmd->event, "dict_server_iteration_finished");
+ str_truncate(str, 0);
+ if (dict_iterate_deinit(&cmd->iter, &error) < 0) {
+ event_add_str(cmd->event, "error", error);
+ e_error(cmd->event, "dict_iterate() failed: %s", error);
+ str_printfa(str, "%c%s", DICT_PROTOCOL_REPLY_FAIL, error);
+ } else {
+ event_add_int(cmd->event, "rows", cmd->rows);
+ e_debug(cmd->event, "Iteration finished");
+ }
+ dict_cmd_reply_handle_stats(cmd, str, cmd_stats.iterations);
+ str_append_c(str, '\n');
+
+ cmd->reply = i_strdup(str_c(str));
+}
+
+static int cmd_iterate_flush(struct dict_connection_cmd *cmd)
+{
+ string_t *str = t_str_new(256);
+ const char *key, *const *values;
+
+ if (cmd->conn->destroyed) {
+ cmd_iterate_flush_finish(cmd, str);
+ return 1;
+ }
+
+ if (!dict_connection_flush_if_full(cmd->conn))
+ return 0;
+
+ while (dict_iterate_values(cmd->iter, &key, &values)) {
+ cmd->rows++;
+ str_truncate(str, 0);
+ if (cmd->async_reply_id != 0) {
+ str_append_c(str, DICT_PROTOCOL_REPLY_ASYNC_REPLY);
+ str_printfa(str, "%u\t", cmd->async_reply_id);
+ }
+ str_append_c(str, DICT_PROTOCOL_REPLY_OK);
+ str_append_tabescaped(str, key);
+ str_append_c(str, '\t');
+ if ((cmd->iter_flags & DICT_ITERATE_FLAG_NO_VALUE) == 0) {
+ str_append_tabescaped(str, values[0]);
+ for (unsigned int i = 1; values[i] != NULL; i++) {
+ str_append_c(str, '\t');
+ str_append_tabescaped(str, values[i]);
+ }
+ }
+ str_append_c(str, '\n');
+ o_stream_nsend(cmd->conn->conn.output, str_data(str), str_len(str));
+
+ if (!dict_connection_flush_if_full(cmd->conn))
+ return 0;
+ }
+ if (dict_iterate_has_more(cmd->iter)) {
+ /* wait for the next iteration callback */
+ return 0;
+ }
+
+ cmd_iterate_flush_finish(cmd, str);
+ return 1;
+}
+
+static void cmd_iterate_callback(struct dict_connection_cmd *cmd)
+{
+ struct dict_connection *conn = cmd->conn;
+
+ dict_connection_ref(conn);
+ o_stream_cork(conn->conn.output);
+ /* Don't uncork if we're just waiting for more input from the dict
+ driver. Some dict drivers (e.g. dict-client) don't do any kind of
+ buffering internally, so this callback can write out only a single
+ iteration. By leaving the ostream corked it doesn't result in many
+ tiny writes. However, we could be here also because the connection
+ output buffer is full already, in which case don't want to leave a
+ cork. */
+ conn->iter_flush_pending = FALSE;
+ cmd->uncork_pending = FALSE;
+ if (dict_connection_cmd_output_more(cmd)) {
+ /* NOTE: cmd may be freed now */
+ o_stream_uncork(conn->conn.output);
+ } else if (conn->iter_flush_pending) {
+ /* Don't leave the stream uncorked or we might get stuck. */
+ o_stream_uncork(conn->conn.output);
+ } else {
+ /* It's possible that the command gets finished via some other
+ code path. To make sure this doesn't cause hangs, uncork the
+ output when command gets freed. */
+ cmd->uncork_pending = TRUE;
+ }
+ dict_connection_unref_safe(conn);
+}
+
+static int cmd_iterate(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ const char *username;
+ unsigned int flags;
+ uint64_t max_rows;
+
+ if (str_array_length(args) < 3 ||
+ str_to_uint(args[0], &flags) < 0 ||
+ str_to_uint64(args[1], &max_rows) < 0) {
+ e_error(cmd->event, "ITERATE: broken input");
+ return -1;
+ }
+ dict_connection_cmd_async(cmd);
+ username = args[3];
+
+ const struct dict_op_settings set = {
+ .username = username,
+ };
+
+ /* <flags> <max_rows> <path> [<username>] */
+ flags |= DICT_ITERATE_FLAG_ASYNC;
+ event_add_str(cmd->event, "key", args[2]);
+ event_add_str(cmd->event, "user", username);
+ cmd->iter = dict_iterate_init(cmd->conn->dict, &set, args[2], flags);
+ cmd->iter_flags = flags;
+ if (max_rows > 0)
+ dict_iterate_set_limit(cmd->iter, max_rows);
+ dict_iterate_set_async_callback(cmd->iter, cmd_iterate_callback, cmd);
+ (void)dict_connection_cmd_output_more(cmd);
+ return 1;
+}
+
+static struct dict_connection_transaction *
+dict_connection_transaction_lookup(struct dict_connection *conn,
+ unsigned int id)
+{
+ struct dict_connection_transaction *transaction;
+
+ if (!array_is_created(&conn->transactions))
+ return NULL;
+
+ array_foreach_modifiable(&conn->transactions, transaction) {
+ if (transaction->id == id)
+ return transaction;
+ }
+ return NULL;
+}
+
+static void
+dict_connection_transaction_array_remove(struct dict_connection *conn,
+ unsigned int id)
+{
+ const struct dict_connection_transaction *transactions;
+ unsigned int i, count;
+
+ transactions = array_get(&conn->transactions, &count);
+ for (i = 0; i < count; i++) {
+ if (transactions[i].id == id) {
+ i_assert(transactions[i].ctx == NULL);
+ array_delete(&conn->transactions, i, 1);
+ return;
+ }
+ }
+ i_unreached();
+}
+
+static int cmd_begin(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+ unsigned int id;
+ const char *username;
+
+ if (str_array_length(args) < 1) {
+ e_error(cmd->event, "BEGIN: broken input");
+ return -1;
+ }
+ username = args[1];
+
+ /* <id> [<username>] */
+ if (str_to_uint(args[0], &id) < 0) {
+ e_error(cmd->event, "Invalid transaction ID %s", args[0]);
+ return -1;
+ }
+ if (dict_connection_transaction_lookup(cmd->conn, id) != NULL) {
+ e_error(cmd->event, "Transaction ID %u already exists", id);
+ return -1;
+ }
+
+ if (!array_is_created(&cmd->conn->transactions))
+ i_array_init(&cmd->conn->transactions, 4);
+
+ struct dict_op_settings set = {
+ .username = username,
+ };
+ trans = array_append_space(&cmd->conn->transactions);
+ trans->id = id;
+ trans->conn = cmd->conn;
+ trans->ctx = dict_transaction_begin(cmd->conn->dict, &set);
+ return 0;
+}
+
+static int
+dict_connection_transaction_lookup_parse(struct dict_connection *conn,
+ const char *id_str,
+ struct dict_connection_transaction **trans_r)
+{
+ unsigned int id;
+
+ if (str_to_uint(id_str, &id) < 0) {
+ e_error(conn->conn.event, "Invalid transaction ID %s", id_str);
+ return -1;
+ }
+ *trans_r = dict_connection_transaction_lookup(conn, id);
+ if (*trans_r == NULL) {
+ e_error(conn->conn.event, "Transaction ID %u doesn't exist", id);
+ return -1;
+ }
+ return 0;
+}
+
+static void
+cmd_commit_finish(struct dict_connection_cmd *cmd,
+ const struct dict_commit_result *result, bool async)
+{
+ string_t *str = t_str_new(64);
+ char chr;
+
+ event_set_name(cmd->event, "dict_server_transaction_finished");
+ switch (result->ret) {
+ case DICT_COMMIT_RET_OK:
+ chr = DICT_PROTOCOL_REPLY_OK;
+ break;
+ case DICT_COMMIT_RET_NOTFOUND:
+ event_add_str(cmd->event, "key_not_found", "yes");
+ chr = DICT_PROTOCOL_REPLY_NOTFOUND;
+ break;
+ case DICT_COMMIT_RET_WRITE_UNCERTAIN:
+ i_assert(result->error != NULL);
+ event_add_str(cmd->event, "write_uncertain", "yes");
+ event_add_str(cmd->event, "error", result->error);
+ chr = DICT_PROTOCOL_REPLY_WRITE_UNCERTAIN;
+ break;
+ case DICT_COMMIT_RET_FAILED:
+ default:
+ i_assert(result->error != NULL);
+ event_add_str(cmd->event, "error", result->error);
+ chr = DICT_PROTOCOL_REPLY_FAIL;
+ break;
+ }
+ if (async)
+ str_append_c(str, DICT_PROTOCOL_REPLY_ASYNC_COMMIT);
+ str_printfa(str, "%c%u", chr, cmd->trans_id);
+ if (chr != DICT_PROTOCOL_REPLY_OK &&
+ chr != DICT_PROTOCOL_REPLY_NOTFOUND) {
+ str_append_c(str, '\t');
+ str_append_tabescaped(str, result->error);
+ }
+ dict_cmd_reply_handle_stats(cmd, str, cmd_stats.commits);
+ str_append_c(str, '\n');
+ cmd->reply = i_strdup(str_c(str));
+
+ if (result->ret < 0)
+ e_debug(cmd->event, "Transaction finished: %s", result->error);
+ else
+ e_debug(cmd->event, "Transaction finished");
+ dict_connection_transaction_array_remove(cmd->conn, cmd->trans_id);
+ dict_connection_cmd_try_flush(&cmd);
+}
+
+static void cmd_commit_callback(const struct dict_commit_result *result,
+ struct dict_connection_cmd *cmd)
+{
+ cmd_commit_finish(cmd, result, FALSE);
+}
+
+static void cmd_commit_callback_async(const struct dict_commit_result *result,
+ struct dict_connection_cmd *cmd)
+{
+ cmd_commit_finish(cmd, result, TRUE);
+}
+
+static int
+cmd_commit(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+
+ if (dict_connection_transaction_lookup_parse(cmd->conn, args[0], &trans) < 0)
+ return -1;
+ cmd->trans_id = trans->id;
+ event_add_str(cmd->event, "user", trans->ctx->set.username);
+
+ dict_connection_cmd_async(cmd);
+ dict_transaction_commit_async(&trans->ctx, cmd_commit_callback, cmd);
+ return 1;
+}
+
+static int
+cmd_commit_async(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+
+ if (dict_connection_transaction_lookup_parse(cmd->conn, args[0], &trans) < 0)
+ return -1;
+ cmd->trans_id = trans->id;
+ event_add_str(cmd->event, "user", trans->ctx->set.username);
+
+ dict_connection_cmd_async(cmd);
+ dict_transaction_commit_async(&trans->ctx, cmd_commit_callback_async, cmd);
+ return 1;
+}
+
+static int
+cmd_rollback(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+
+ if (dict_connection_transaction_lookup_parse(cmd->conn, args[0], &trans) < 0)
+ return -1;
+
+ event_add_str(cmd->event, "user", trans->ctx->set.username);
+ dict_transaction_rollback(&trans->ctx);
+ dict_connection_transaction_array_remove(cmd->conn, trans->id);
+ return 0;
+}
+
+static int cmd_set(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+
+ /* <id> <key> <value> */
+ if (str_array_length(args) != 3) {
+ e_error(cmd->event, "SET: broken input");
+ return -1;
+ }
+
+ if (dict_connection_transaction_lookup_parse(cmd->conn, args[0], &trans) < 0)
+ return -1;
+ event_add_str(cmd->event, "user", trans->ctx->set.username);
+ dict_set(trans->ctx, args[1], args[2]);
+ return 0;
+}
+
+static int cmd_unset(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+
+ /* <id> <key> */
+ if (str_array_length(args) != 2) {
+ e_error(cmd->event, "UNSET: broken input");
+ return -1;
+ }
+
+ if (dict_connection_transaction_lookup_parse(cmd->conn, args[0], &trans) < 0)
+ return -1;
+ dict_unset(trans->ctx, args[1]);
+ return 0;
+}
+
+static int
+cmd_atomic_inc(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+ long long diff;
+
+ /* <id> <key> <diff> */
+ if (str_array_length(args) != 3 ||
+ str_to_llong(args[2], &diff) < 0) {
+ e_error(cmd->event, "ATOMIC_INC: broken input");
+ return -1;
+ }
+
+ if (dict_connection_transaction_lookup_parse(cmd->conn, args[0], &trans) < 0)
+ return -1;
+
+ dict_atomic_inc(trans->ctx, args[1], diff);
+ return 0;
+}
+
+static int cmd_timestamp(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+ long long tv_sec;
+ unsigned int tv_nsec;
+
+ /* <id> <secs> <nsecs> */
+ if (str_array_length(args) != 3 ||
+ str_to_llong(args[1], &tv_sec) < 0 ||
+ str_to_uint(args[2], &tv_nsec) < 0) {
+ e_error(cmd->event, "TIMESTAMP: broken input");
+ return -1;
+ }
+
+ if (dict_connection_transaction_lookup_parse(cmd->conn, args[0], &trans) < 0)
+ return -1;
+
+ struct timespec ts = {
+ .tv_sec = tv_sec,
+ .tv_nsec = tv_nsec
+ };
+ dict_transaction_set_timestamp(trans->ctx, &ts);
+ return 0;
+}
+
+static int
+cmd_hide_log_values(struct dict_connection_cmd *cmd, const char *const *args)
+{
+ struct dict_connection_transaction *trans;
+ bool value;
+
+ /* <id> <hide_log_values> */
+ if (str_array_length(args) != 2 ) {
+ e_error(cmd->event, "HIDE_LOG_VALUES: broken input");
+ return -1;
+ }
+ if (strcasecmp(args[1], "yes") == 0 || strcasecmp(args[1], "y") == 0 ||
+ strcmp(args[1], "1") == 0)
+ value = TRUE;
+ else if (strcasecmp(args[1], "no") == 0)
+ value = FALSE;
+ else {
+ e_error(cmd->event, "HIDE_LOG_VALUES: broken input");
+ return -1;
+ }
+
+ if (dict_connection_transaction_lookup_parse(cmd->conn, args[0], &trans) < 0)
+ return -1;
+
+ dict_transaction_set_hide_log_values(trans->ctx, value);
+ return 0;
+}
+
+static const struct dict_cmd_func cmds[] = {
+ { DICT_PROTOCOL_CMD_LOOKUP, cmd_lookup },
+ { DICT_PROTOCOL_CMD_ITERATE, cmd_iterate },
+ { DICT_PROTOCOL_CMD_BEGIN, cmd_begin },
+ { DICT_PROTOCOL_CMD_COMMIT, cmd_commit },
+ { DICT_PROTOCOL_CMD_COMMIT_ASYNC, cmd_commit_async },
+ { DICT_PROTOCOL_CMD_ROLLBACK, cmd_rollback },
+ { DICT_PROTOCOL_CMD_SET, cmd_set },
+ { DICT_PROTOCOL_CMD_UNSET, cmd_unset },
+ { DICT_PROTOCOL_CMD_ATOMIC_INC, cmd_atomic_inc },
+ { DICT_PROTOCOL_CMD_TIMESTAMP, cmd_timestamp },
+ { DICT_PROTOCOL_CMD_HIDE_LOG_VALUES, cmd_hide_log_values },
+
+ { 0, NULL }
+};
+
+static const struct dict_cmd_func *dict_command_find(enum dict_protocol_cmd cmd)
+{
+ unsigned int i;
+
+ for (i = 0; cmds[i].cmd != '\0'; i++) {
+ if (cmds[i].cmd == cmd)
+ return &cmds[i];
+ }
+ return NULL;
+}
+
+int dict_command_input(struct dict_connection *conn, const char *line)
+{
+ const struct dict_cmd_func *cmd_func;
+ struct dict_connection_cmd *cmd;
+ int ret;
+ const char *const *args;
+
+ cmd_func = dict_command_find((enum dict_protocol_cmd)*line);
+ if (cmd_func == NULL) {
+ e_error(conn->conn.event, "Unknown command %c", *line);
+ return -1;
+ }
+
+ cmd = i_new(struct dict_connection_cmd, 1);
+ cmd->conn = conn;
+ cmd->event = event_create(cmd->conn->conn.event);
+ cmd->cmd = cmd_func;
+ cmd->start_timeval = ioloop_timeval;
+ array_push_back(&conn->cmds, &cmd);
+ dict_connection_ref(conn);
+
+ args = t_strsplit_tabescaped(line + 1);
+ if ((ret = cmd_func->func(cmd, args)) <= 0) {
+ dict_connection_cmd_remove(cmd);
+ return ret;
+ }
+ return 0;
+}
+
+static bool dict_connection_cmds_try_output_more(struct dict_connection *conn)
+{
+ struct dict_connection_cmd *cmd;
+
+ /* only iterators may be returning a lot of data */
+ array_foreach_elem(&conn->cmds, cmd) {
+ if (cmd->iter == NULL) {
+ /* not an iterator */
+ } else if (cmd_iterate_flush(cmd) == 0) {
+ /* unfinished */
+ } else {
+ dict_connection_cmd_try_flush(&cmd);
+ /* cmd should be freed now, restart output */
+ return TRUE;
+ }
+ if (conn->conn.minor_version < DICT_CLIENT_PROTOCOL_TIMINGS_MIN_VERSION)
+ break;
+ /* try to flush the rest */
+ }
+ return FALSE;
+}
+
+void dict_connection_cmds_output_more(struct dict_connection *conn)
+{
+ while (array_count(&conn->cmds) > 0) {
+ if (!dict_connection_cmds_try_output_more(conn))
+ break;
+ }
+}
+
+static bool dict_connection_cmd_output_more(struct dict_connection_cmd *cmd)
+{
+ struct dict_connection_cmd *const *first_cmdp;
+
+ if (cmd->conn->conn.minor_version < DICT_CLIENT_PROTOCOL_TIMINGS_MIN_VERSION) {
+ first_cmdp = array_front(&cmd->conn->cmds);
+ if (*first_cmdp != cmd)
+ return TRUE;
+ }
+ return dict_connection_cmds_try_output_more(cmd->conn);
+}
+
+void dict_commands_init(void)
+{
+ cmd_stats.lookups = stats_dist_init();
+ cmd_stats.iterations = stats_dist_init();
+ cmd_stats.commits = stats_dist_init();
+}
+
+void dict_commands_deinit(void)
+{
+ stats_dist_deinit(&cmd_stats.lookups);
+ stats_dist_deinit(&cmd_stats.iterations);
+ stats_dist_deinit(&cmd_stats.commits);
+}
diff --git a/src/dict/dict-commands.h b/src/dict/dict-commands.h
new file mode 100644
index 0000000..ce4a04f
--- /dev/null
+++ b/src/dict/dict-commands.h
@@ -0,0 +1,21 @@
+#ifndef DICT_COMMANDS_H
+#define DICT_COMMANDS_H
+
+struct dict_connection;
+
+struct dict_command_stats {
+ struct stats_dist *lookups;
+ struct stats_dist *iterations;
+ struct stats_dist *commits;
+};
+
+extern struct dict_command_stats cmd_stats;
+
+int dict_command_input(struct dict_connection *conn, const char *line);
+
+void dict_connection_cmds_output_more(struct dict_connection *conn);
+
+void dict_commands_init(void);
+void dict_commands_deinit(void);
+
+#endif
diff --git a/src/dict/dict-connection.c b/src/dict/dict-connection.c
new file mode 100644
index 0000000..6a87c27
--- /dev/null
+++ b/src/dict/dict-connection.c
@@ -0,0 +1,272 @@
+/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "ioloop.h"
+#include "istream.h"
+#include "ostream.h"
+#include "llist.h"
+#include "strescape.h"
+#include "master-service.h"
+#include "dict-client.h"
+#include "dict-settings.h"
+#include "dict-commands.h"
+#include "dict-connection.h"
+#include "dict-init-cache.h"
+
+#include <unistd.h>
+
+#define DICT_CONN_MAX_PENDING_COMMANDS 1000
+
+static int dict_connection_dict_init(struct dict_connection *conn);
+static void dict_connection_destroy(struct connection *_conn);
+struct connection_list *dict_connections = NULL;
+
+static struct event_category dict_server_event_category = {
+ .name = "dict-server",
+};
+
+static int dict_connection_handshake_args(struct connection *_conn,
+ const char *const *args)
+{
+ unsigned int major, value_type_num;
+ struct dict_connection *conn =
+ container_of(_conn, struct dict_connection, conn);
+
+ /* protocol handshake is Hmajor minor value_type */
+ if (str_array_length(args) < 5 || **args != 'H')
+ return -1;
+
+ /* check major version which comes right after 'H' in the
+ first parameter, store minor version. */
+ if (str_to_uint(args[0]+1, &major) < 0 ||
+ str_to_uint(args[1], &conn->conn.minor_version) < 0 ||
+ major != DICT_CLIENT_PROTOCOL_MAJOR_VERSION)
+ return -1;
+
+ /* check value type */
+ if (str_to_uint(args[2], &value_type_num) < 0 ||
+ value_type_num >= DICT_DATA_TYPE_LAST)
+ return -1;
+
+ conn->value_type = (enum dict_data_type)value_type_num;
+ conn->name = i_strdup(args[4]);
+
+ /* try initialize the given dict */
+ if (dict_connection_dict_init(conn) < 0)
+ return -1;
+
+ return 1;
+}
+
+static int dict_connection_handshake_line(struct connection *conn,
+ const char *line)
+{
+ const char *const *args = t_strsplit_tabescaped(line);
+ return dict_connection_handshake_args(conn, args);
+}
+
+static int dict_connection_dict_init(struct dict_connection *conn)
+{
+ struct dict_settings dict_set;
+ const char *const *strlist;
+ unsigned int i, count;
+ const char *uri, *error;
+
+ if (!array_is_created(&dict_settings->dicts)) {
+ e_error(conn->conn.event, "No dictionaries configured");
+ return -1;
+ }
+ strlist = array_get(&dict_settings->dicts, &count);
+ for (i = 0; i < count; i += 2) {
+ if (strcmp(strlist[i], conn->name) == 0)
+ break;
+ }
+
+ if (i == count) {
+ e_error(conn->conn.event, "Unconfigured dictionary name '%s'",
+ conn->name);
+ return -1;
+ }
+ event_set_append_log_prefix(conn->conn.event,
+ t_strdup_printf("%s: ", conn->name));
+ event_add_str(conn->conn.event, "dict_name", conn->name);
+ uri = strlist[i+1];
+
+ i_zero(&dict_set);
+ dict_set.base_dir = dict_settings->base_dir;
+ dict_set.event_parent = conn->conn.event;
+ if (dict_init_cache_get(conn->name, uri, &dict_set, &conn->dict, &error) < 0) {
+ /* dictionary initialization failed */
+ e_error(conn->conn.event, "Failed to initialize dictionary '%s': %s",
+ conn->name, error);
+ return -1;
+ }
+ return 0;
+}
+
+static int dict_connection_output(struct connection *_conn)
+{
+ struct dict_connection *conn = container_of(_conn, struct dict_connection, conn);
+ int ret;
+
+ if ((ret = o_stream_flush(conn->conn.output)) < 0) {
+ dict_connection_destroy(&conn->conn);
+ return 1;
+ }
+ if (ret > 0)
+ dict_connection_cmds_output_more(conn);
+ return ret;
+}
+
+struct dict_connection *
+dict_connection_create(struct master_service_connection *master_conn)
+{
+ struct dict_connection *conn;
+
+ conn = i_new(struct dict_connection, 1);
+ conn->refcount = 1;
+
+ connection_init_server(dict_connections, &conn->conn, master_conn->name,
+ master_conn->fd, master_conn->fd);
+ event_add_category(conn->conn.event, &dict_server_event_category);
+
+ o_stream_set_flush_callback(conn->conn.output, dict_connection_output,
+ &conn->conn);
+
+ i_array_init(&conn->cmds, DICT_CONN_MAX_PENDING_COMMANDS);
+
+ return conn;
+}
+
+void dict_connection_ref(struct dict_connection *conn)
+{
+ i_assert(conn->refcount > 0);
+ conn->refcount++;
+}
+
+bool dict_connection_unref(struct dict_connection *conn)
+{
+ struct dict_connection_transaction *transaction;
+
+ i_assert(conn->refcount > 0);
+ if (--conn->refcount > 0)
+ return TRUE;
+
+ i_assert(array_count(&conn->cmds) == 0);
+
+ /* we should have only transactions that haven't been committed or
+ rollbacked yet. close those before dict is deinitialized. */
+ if (array_is_created(&conn->transactions)) {
+ array_foreach_modifiable(&conn->transactions, transaction)
+ dict_transaction_rollback(&transaction->ctx);
+ }
+
+ if (conn->dict != NULL)
+ dict_init_cache_unref(&conn->dict);
+
+ if (array_is_created(&conn->transactions))
+ array_free(&conn->transactions);
+
+ array_free(&conn->cmds);
+
+ connection_deinit(&conn->conn);
+
+ i_free(conn->name);
+ i_free(conn);
+
+ master_service_client_connection_destroyed(master_service);
+ return FALSE;
+}
+
+static int dict_connection_input_line(struct connection *_conn, const char *line)
+{
+ struct dict_connection *conn =
+ container_of(_conn, struct dict_connection, conn);
+
+ i_assert(conn->dict != NULL);
+
+ if (dict_command_input(conn, line) < 0)
+ return -1;
+
+ if (array_count(&conn->cmds) >= DICT_CONN_MAX_PENDING_COMMANDS) {
+ connection_input_halt(_conn);
+ return 0;
+ }
+
+ return 1;
+}
+
+static void dict_connection_unref_safe_callback(struct dict_connection *conn)
+{
+ timeout_remove(&conn->to_unref);
+ (void)dict_connection_unref(conn);
+}
+
+void dict_connection_unref_safe(struct dict_connection *conn)
+{
+ if (conn->refcount == 1) {
+ /* delayed unref to make sure we don't try to call
+ dict_deinit() from a dict-callback. that's too much trouble
+ for each dict driver to be able to handle. */
+ if (conn->to_unref == NULL) {
+ conn->to_unref = timeout_add_short(0,
+ dict_connection_unref_safe_callback, conn);
+ }
+ } else {
+ (void)dict_connection_unref(conn);
+ }
+}
+
+static void dict_connection_destroy(struct connection *_conn)
+{
+ struct dict_connection *conn = container_of(_conn, struct dict_connection, conn);
+
+ /* If there are commands still running, we delay disconnecting can may
+ come back here. Track this so we unreference the connection only
+ once. */
+ if (conn->destroyed)
+ return;
+ conn->destroyed = TRUE;
+
+ /* the connection is closed, but there may still be commands left
+ running. finish them, even if the calling client can't be notified
+ about whether they succeeded (clients may not even care).
+
+ flush the command output here in case we were waiting on iteration
+ output. */
+ i_stream_close(conn->conn.input);
+ o_stream_close(conn->conn.output);
+ dict_connection_cmds_output_more(conn);
+
+ io_remove(&conn->conn.io);
+ dict_connection_unref(conn);
+}
+
+unsigned int dict_connections_current_count(void)
+{
+ return dict_connections->connections_count;
+}
+
+void dict_connections_destroy_all(void)
+{
+ connection_list_deinit(&dict_connections);
+}
+
+static struct connection_settings dict_connections_set = {
+ .dont_send_version = TRUE,
+ .input_max_size = DICT_CLIENT_MAX_LINE_LENGTH,
+ .output_max_size = 128*1024,
+};
+
+static struct connection_vfuncs dict_connections_vfuncs = {
+ .destroy = dict_connection_destroy,
+ .handshake_line = dict_connection_handshake_line,
+ .input_line = dict_connection_input_line,
+};
+
+void dict_connections_init(void)
+{
+ dict_connections = connection_list_init(&dict_connections_set,
+ &dict_connections_vfuncs);
+}
diff --git a/src/dict/dict-connection.h b/src/dict/dict-connection.h
new file mode 100644
index 0000000..2241dad
--- /dev/null
+++ b/src/dict/dict-connection.h
@@ -0,0 +1,48 @@
+#ifndef DICT_CONNECTION_H
+#define DICT_CONNECTION_H
+
+#include "dict.h"
+#include "connection.h"
+
+struct dict_connection_transaction {
+ unsigned int id;
+ struct dict_connection *conn;
+ struct dict_transaction_context *ctx;
+};
+
+struct dict_connection {
+ struct connection conn;
+ struct dict_connection *prev, *next;
+ struct dict_server *server;
+ int refcount;
+
+ char *name;
+ struct dict *dict;
+ enum dict_data_type value_type;
+
+ struct timeout *to_unref;
+
+ /* There are only a few transactions per client, so keeping them in
+ array is fast enough */
+ ARRAY(struct dict_connection_transaction) transactions;
+ ARRAY(struct dict_connection_cmd *) cmds;
+ unsigned int async_id_counter;
+
+ bool iter_flush_pending;
+ bool destroyed;
+};
+
+struct master_service_connection;
+
+struct dict_connection *
+dict_connection_create(struct master_service_connection *master_conn);
+
+void dict_connection_ref(struct dict_connection *conn);
+bool dict_connection_unref(struct dict_connection *conn);
+void dict_connection_unref_safe(struct dict_connection *conn);
+
+unsigned int dict_connections_current_count(void);
+void dict_connections_init(void);
+void dict_connections_destroy_all(void);
+
+#endif
diff --git a/src/dict/dict-init-cache.c b/src/dict/dict-init-cache.c
new file mode 100644
index 0000000..ed76940
--- /dev/null
+++ b/src/dict/dict-init-cache.c
@@ -0,0 +1,164 @@
+/* Copyright (c) 2021 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "ioloop.h"
+#include "dict.h"
+#include "dict-private.h"
+#include "dict-init-cache.h"
+#include "llist.h"
+
+/* How many seconds to keep dict opened for reuse after it's been closed */
+#define DICT_CACHE_TIMEOUT_SECS 30
+/* How many closed dicts to keep */
+#define DICT_CACHE_MAX_COUNT 10
+
+struct dict_init_cache_list {
+ struct dict_init_cache_list *prev, *next;
+
+ struct dict *dict;
+ char *dict_name;
+ int refcount;
+
+ time_t destroy_time;
+};
+
+static struct dict_init_cache_list *dicts = NULL;
+static struct timeout *to_dict = NULL;
+
+static struct dict_init_cache_list *
+dict_init_cache_add(const char *dict_name, struct dict *dict)
+{
+ struct dict_init_cache_list *list;
+
+ list = i_new(struct dict_init_cache_list, 1);
+ list->refcount = 1;
+ list->dict = dict;
+ list->dict_name = i_strdup(dict_name);
+
+ DLLIST_PREPEND(&dicts, list);
+
+ return list;
+}
+
+static void dict_init_cache_list_free(struct dict_init_cache_list *list)
+{
+ i_assert(list->refcount == 0);
+
+ DLLIST_REMOVE(&dicts, list);
+ dict_deinit(&list->dict);
+ i_free(list->dict_name);
+ i_free(list);
+}
+
+static struct dict_init_cache_list *dict_init_cache_find(const char *dict_name)
+{
+ struct dict_init_cache_list *listp = dicts, *next = NULL, *match = NULL;
+ unsigned int ref0_count = 0;
+
+ while (listp != NULL) {
+ next = listp->next;
+ if (match != NULL) {
+ /* already found the dict. we're just going through
+ the rest of them to drop 0 refcounts */
+ } else if (strcmp(dict_name, listp->dict_name) == 0)
+ match = listp;
+
+ if (listp->refcount == 0 && listp != match) {
+ if (listp->destroy_time <= ioloop_time ||
+ ref0_count >= DICT_CACHE_MAX_COUNT - 1)
+ dict_init_cache_list_free(listp);
+ else
+ ref0_count++;
+ }
+ listp = next;
+ }
+ return match;
+}
+
+int dict_init_cache_get(const char *dict_name, const char *uri,
+ const struct dict_settings *set,
+ struct dict **dict_r, const char **error_r)
+{
+ struct dict_init_cache_list *match;
+ int ret = 0;
+
+ match = dict_init_cache_find(dict_name);
+ if (match == NULL) {
+ if (dict_init(uri, set, dict_r, error_r) < 0)
+ return -1;
+ match = dict_init_cache_add(dict_name, *dict_r);
+ } else {
+ match->refcount++;
+ *dict_r = match->dict;
+ }
+ i_assert(match->dict != NULL);
+ return ret;
+}
+
+static void destroy_unrefed(void)
+{
+ struct dict_init_cache_list *listp, *next = NULL;
+ bool seen_ref0 = FALSE;
+
+ for (listp = dicts; listp != NULL; listp = next) {
+ next = listp->next;
+
+ i_assert(listp->refcount >= 0);
+ if (listp->refcount > 0)
+ ;
+ else if (listp->destroy_time <= ioloop_time)
+ dict_init_cache_list_free(listp);
+ else
+ seen_ref0 = TRUE;
+ }
+
+ if (!seen_ref0 && to_dict != NULL)
+ timeout_remove(&to_dict);
+}
+
+static void dict_removal_timeout(void *context ATTR_UNUSED)
+{
+ destroy_unrefed();
+}
+
+void dict_init_cache_unref(struct dict **_dict)
+{
+ struct dict *dict = *_dict;
+ struct dict_init_cache_list *listp;
+
+ if (dict == NULL)
+ return;
+
+ *_dict = NULL;
+ for (listp = dicts; listp != NULL; listp = listp->next) {
+ if (listp->dict == dict)
+ break;
+ }
+
+ i_assert(listp != NULL && listp->dict == dict);
+ i_assert(listp->refcount > 0);
+
+ listp->refcount--;
+ listp->destroy_time = ioloop_time + DICT_CACHE_TIMEOUT_SECS;
+
+ if (to_dict == NULL) {
+ to_dict = timeout_add_to(io_loop_get_root(),
+ DICT_CACHE_TIMEOUT_SECS*1000/2,
+ dict_removal_timeout, NULL);
+ }
+}
+
+void dict_init_cache_wait_all(void)
+{
+ struct dict_init_cache_list *listp;
+
+ for (listp = dicts; listp != NULL; listp = listp->next)
+ dict_wait(listp->dict);
+}
+
+void dict_init_cache_destroy_all(void)
+{
+ timeout_remove(&to_dict);
+ while (dicts != NULL)
+ dict_init_cache_list_free(dicts);
+}
diff --git a/src/dict/dict-init-cache.h b/src/dict/dict-init-cache.h
new file mode 100644
index 0000000..1342ca3
--- /dev/null
+++ b/src/dict/dict-init-cache.h
@@ -0,0 +1,12 @@
+#ifndef DICT_INIT_CACHE_H
+#define DICT_INIT_CACHE_H
+
+int dict_init_cache_get(const char *dict_name, const char *uri,
+ const struct dict_settings *set,
+ struct dict **dict_r, const char **error_r);
+void dict_init_cache_unref(struct dict **dict);
+
+void dict_init_cache_wait_all(void);
+void dict_init_cache_destroy_all(void);
+
+#endif
diff --git a/src/dict/dict-settings.c b/src/dict/dict-settings.c
new file mode 100644
index 0000000..23c33f6
--- /dev/null
+++ b/src/dict/dict-settings.c
@@ -0,0 +1,117 @@
+/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "buffer.h"
+#include "settings-parser.h"
+#include "service-settings.h"
+#include "dict-settings.h"
+
+/* <settings checks> */
+static struct file_listener_settings dict_unix_listeners_array[] = {
+ { "dict", 0660, "", "$default_internal_group" }
+};
+static struct file_listener_settings *dict_unix_listeners[] = {
+ &dict_unix_listeners_array[0]
+};
+static buffer_t dict_unix_listeners_buf = {
+ { { dict_unix_listeners, sizeof(dict_unix_listeners) } }
+};
+
+static struct file_listener_settings dict_async_unix_listeners_array[] = {
+ { "dict-async", 0660, "", "$default_internal_group" }
+};
+static struct file_listener_settings *dict_async_unix_listeners[] = {
+ &dict_async_unix_listeners_array[0]
+};
+static buffer_t dict_async_unix_listeners_buf = {
+ { { dict_async_unix_listeners, sizeof(dict_async_unix_listeners) } }
+};
+/* </settings checks> */
+
+struct service_settings dict_service_settings = {
+ .name = "dict",
+ .protocol = "",
+ .type = "",
+ .executable = "dict",
+ .user = "$default_internal_user",
+ .group = "",
+ .privileged_group = "",
+ .extra_groups = "",
+ .chroot = "",
+
+ .drop_priv_before_exec = FALSE,
+
+ .process_min_avail = 0,
+ .process_limit = 0,
+ .client_limit = 1,
+ .service_count = 0,
+ .idle_kill = 0,
+ .vsz_limit = UOFF_T_MAX,
+
+ .unix_listeners = { { &dict_unix_listeners_buf,
+ sizeof(dict_unix_listeners[0]) } },
+ .fifo_listeners = ARRAY_INIT,
+ .inet_listeners = ARRAY_INIT
+};
+
+struct service_settings dict_async_service_settings = {
+ .name = "dict-async",
+ .protocol = "",
+ .type = "",
+ .executable = "dict",
+ .user = "$default_internal_user",
+ .group = "",
+ .privileged_group = "",
+ .extra_groups = "",
+ .chroot = "",
+
+ .drop_priv_before_exec = FALSE,
+
+ .process_min_avail = 0,
+ .process_limit = 0,
+ .client_limit = 0,
+ .service_count = 0,
+ .idle_kill = 0,
+ .vsz_limit = UOFF_T_MAX,
+
+ .unix_listeners = { { &dict_async_unix_listeners_buf,
+ sizeof(dict_async_unix_listeners[0]) } },
+ .fifo_listeners = ARRAY_INIT,
+ .inet_listeners = ARRAY_INIT
+};
+
+#undef DEF
+#define DEF(type, name) \
+ SETTING_DEFINE_STRUCT_##type(#name, name, struct dict_server_settings)
+
+static const struct setting_define dict_setting_defines[] = {
+ DEF(STR, base_dir),
+ DEF(BOOL, verbose_proctitle),
+
+ DEF(STR, dict_db_config),
+ { .type = SET_STRLIST, .key = "dict",
+ .offset = offsetof(struct dict_server_settings, dicts) },
+
+ SETTING_DEFINE_LIST_END
+};
+
+const struct dict_server_settings dict_default_settings = {
+ .base_dir = PKG_RUNDIR,
+ .verbose_proctitle = FALSE,
+
+ .dict_db_config = "",
+ .dicts = ARRAY_INIT
+};
+
+const struct setting_parser_info dict_setting_parser_info = {
+ .module_name = "dict",
+ .defines = dict_setting_defines,
+ .defaults = &dict_default_settings,
+
+ .type_offset = SIZE_MAX,
+ .struct_size = sizeof(struct dict_server_settings),
+
+ .parent_offset = SIZE_MAX
+};
+
+const struct dict_server_settings *dict_settings;
diff --git a/src/dict/dict-settings.h b/src/dict/dict-settings.h
new file mode 100644
index 0000000..3c37589
--- /dev/null
+++ b/src/dict/dict-settings.h
@@ -0,0 +1,15 @@
+#ifndef DICT_SETTINGS_H
+#define DICT_SETTINGS_H
+
+struct dict_server_settings {
+ const char *base_dir;
+ bool verbose_proctitle;
+
+ const char *dict_db_config;
+ ARRAY(const char *) dicts;
+};
+
+extern const struct setting_parser_info dict_setting_parser_info;
+extern const struct dict_server_settings *dict_settings;
+
+#endif
diff --git a/src/dict/main.c b/src/dict/main.c
new file mode 100644
index 0000000..14b1cea
--- /dev/null
+++ b/src/dict/main.c
@@ -0,0 +1,179 @@
+/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "restrict-access.h"
+#include "ioloop.h"
+#include "randgen.h"
+#include "str.h"
+#include "stats-dist.h"
+#include "process-title.h"
+#include "env-util.h"
+#include "module-dir.h"
+#include "master-service.h"
+#include "master-service-settings.h"
+#include "sql-api.h"
+#include "dict.h"
+#include "dict-client.h"
+#include "dict-commands.h"
+#include "dict-connection.h"
+#include "dict-settings.h"
+#include "dict-init-cache.h"
+#include "main.h"
+
+#include <math.h>
+
+static struct module *modules;
+static struct timeout *to_proctitle;
+static bool proctitle_updated;
+static struct ioloop *main_ioloop;
+
+static void
+add_stats_string(string_t *str, struct stats_dist *stats, const char *name)
+{
+ uint64_t min, max, p95;
+ double avg;
+
+ min = stats_dist_get_min(stats);
+ avg = stats_dist_get_avg(stats);
+ p95 = stats_dist_get_95th(stats);
+ max = stats_dist_get_max(stats);
+
+ str_printfa(str, ", %u %s:%llu/%lld/%llu/%llu",
+ stats_dist_get_count(stats), name,
+ (unsigned long long)min/1000, llrint(avg/1000),
+ (unsigned long long)p95/1000,
+ (unsigned long long)max/1000);
+ stats_dist_reset(stats);
+}
+
+static void dict_proctitle_update(void *context ATTR_UNUSED)
+{
+ string_t *str = t_str_new(128);
+
+ if (!proctitle_updated)
+ timeout_remove(&to_proctitle);
+
+ str_printfa(str, "[%u clients", dict_connections_current_count());
+
+ add_stats_string(str, cmd_stats.lookups, "lookups");
+ add_stats_string(str, cmd_stats.iterations, "iters");
+ add_stats_string(str, cmd_stats.commits, "commits");
+ str_append_c(str, ']');
+
+ process_title_set(str_c(str));
+ proctitle_updated = FALSE;
+}
+
+void dict_proctitle_update_later(void)
+{
+ if (!dict_settings->verbose_proctitle)
+ return;
+
+ if (to_proctitle == NULL)
+ to_proctitle = timeout_add_to(main_ioloop, 1000, dict_proctitle_update, NULL);
+ proctitle_updated = TRUE;
+}
+
+static void dict_die(void)
+{
+ /* hope that other processes relying on us will die first. */
+}
+
+static void client_connected(struct master_service_connection *conn)
+{
+ master_service_client_connection_accept(conn);
+ (void)dict_connection_create(conn);
+}
+
+static void main_preinit(void)
+{
+ /* Load built-in SQL drivers (if any) */
+ sql_drivers_init();
+ sql_drivers_register_all();
+#ifdef HAVE_CDB
+ dict_driver_register(&dict_driver_cdb);
+#endif
+
+ restrict_access_by_env(RESTRICT_ACCESS_FLAG_ALLOW_ROOT, NULL);
+ restrict_access_allow_coredumps(TRUE);
+}
+
+static void main_init(void)
+{
+ struct module_dir_load_settings mod_set;
+ void **sets;
+
+ sets = master_service_settings_get_others(master_service);
+ dict_settings = sets[0];
+
+ if (*dict_settings->dict_db_config != '\0') {
+ /* for berkeley db library */
+ env_put("DB_CONFIG", dict_settings->dict_db_config);
+ }
+
+ i_zero(&mod_set);
+ mod_set.abi_version = DOVECOT_ABI_VERSION;
+ mod_set.require_init_funcs = TRUE;
+
+ modules = module_dir_load(DICT_MODULE_DIR, NULL, &mod_set);
+ module_dir_init(modules);
+
+ /* Register only after loading modules. They may contain SQL drivers,
+ which we'll need to register. */
+ dict_drivers_register_all();
+ dict_commands_init();
+ dict_connections_init();
+
+ if (dict_settings->verbose_proctitle)
+ dict_proctitle_update(NULL);
+}
+
+static void main_deinit(void)
+{
+ /* wait for all dict operations to finish */
+ dict_init_cache_wait_all();
+ /* connections should no longer have any extra refcounts */
+ dict_connections_destroy_all();
+ dict_init_cache_destroy_all();
+
+ dict_drivers_unregister_all();
+ dict_commands_deinit();
+
+ module_dir_unload(&modules);
+
+ sql_drivers_deinit();
+ timeout_remove(&to_proctitle);
+}
+
+int main(int argc, char *argv[])
+{
+ const enum master_service_flags service_flags = 0;
+ const struct setting_parser_info *set_roots[] = {
+ &dict_setting_parser_info,
+ NULL
+ };
+ const char *error;
+
+ master_service = master_service_init("dict", service_flags,
+ &argc, &argv, "");
+ if (master_getopt(master_service) > 0)
+ return FATAL_DEFAULT;
+
+ if (master_service_settings_read_simple(master_service, set_roots,
+ &error) < 0)
+ i_fatal("Error reading configuration: %s", error);
+
+ master_service_init_log_with_pid(master_service);
+ main_preinit();
+ master_service_set_die_callback(master_service, dict_die);
+
+ main_ioloop = current_ioloop;
+ main_init();
+ master_service_init_finish(master_service);
+ master_service_run(master_service, client_connected);
+
+ /* clean up cached dicts */
+ main_deinit();
+ master_service_deinit(&master_service);
+ return 0;
+}
diff --git a/src/dict/main.h b/src/dict/main.h
new file mode 100644
index 0000000..e505e13
--- /dev/null
+++ b/src/dict/main.h
@@ -0,0 +1,6 @@
+#ifndef MAIN_H
+#define MAIN_H
+
+void dict_proctitle_update_later(void);
+
+#endif