diff --git a/configure.ac b/configure.ac
index 2e925f35c15ad55e7c6bbc47786d6499da337fdc..2adf4d84330ccd44f747b943437d8d15bd071064 100644
--- a/configure.ac
+++ b/configure.ac
@@ -100,6 +100,7 @@ doc/example-config/conf.d/Makefile
 doc/rfc/Makefile
 src/Makefile
 src/lib-sieve/Makefile
+src/lib-sieve/util/Makefile
 src/lib-sieve/plugins/Makefile
 src/lib-sieve/plugins/vacation/Makefile
 src/lib-sieve/plugins/subaddress/Makefile
diff --git a/src/lib-sieve/Makefile.am b/src/lib-sieve/Makefile.am
index eea61baacea632194f0ab1742bb52e7f6d9c769e..0f8f1561990e51fa23101210be0c0237b0e1b2b4 100644
--- a/src/lib-sieve/Makefile.am
+++ b/src/lib-sieve/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = plugins
+SUBDIRS = util plugins
 
 dovecot_pkglib_LTLIBRARIES = libdovecot-sieve.la
 
@@ -6,6 +6,7 @@ AM_CPPFLAGS = \
 	$(LIBDOVECOT_INCLUDE) \
 	$(LIBDOVECOT_LDA_INCLUDE) \
 	$(LIBDOVECOT_SERVICE_INCLUDE) \
+	-I$(top_srcdir)/src/lib-sieve/util \
 	-DMODULEDIR=\""$(dovecot_moduledir)"\"
 
 tests = \
@@ -71,12 +72,20 @@ plugins = \
 	$(extdir)/vnd.dovecot/duplicate/libsieve_ext_duplicate.la \
 	$(unfinished_plugins)
 
-libdovecot_sieve_la_DEPENDENCIES = $(plugins) $(LIBDOVECOT_LDA_DEPS) $(LIBDOVECOT_STORAGE_DEPS) $(LIBDOVECOT_DEPS)
-libdovecot_sieve_la_LIBADD = $(plugins) $(LIBDOVECOT_LDA) $(LIBDOVECOT_STORAGE) $(LIBDOVECOT)
+libdovecot_sieve_la_DEPENDENCIES = \
+	$(plugins) \
+	$(top_builddir)/src/lib-sieve/util/libsieve_util.la \
+	$(LIBDOVECOT_LDA_DEPS) \
+	$(LIBDOVECOT_STORAGE_DEPS) \
+	$(LIBDOVECOT_DEPS)
+libdovecot_sieve_la_LIBADD = \
+	$(plugins) \
+	$(top_builddir)/src/lib-sieve/util/libsieve_util.la \
+	$(LIBDOVECOT_LDA) \
+	$(LIBDOVECOT_STORAGE) \
+	$(LIBDOVECOT)
 
 libdovecot_sieve_la_SOURCES = \
-	rfc2822.c \
-	edit-mail.c \
 	sieve-settings.c \
 	sieve-message.c \
 	sieve-smtp.c \
@@ -118,8 +127,6 @@ libdovecot_sieve_la_SOURCES = \
 	sieve.c
 
 headers = \
-	rfc2822.h \
-	edit-mail.h \
 	sieve-config.h \
 	sieve-types.h \
 	sieve-common.h \
diff --git a/src/lib-sieve/plugins/editheader/Makefile.am b/src/lib-sieve/plugins/editheader/Makefile.am
index d4524525c7249b0259b52167079b4a579c73c739..6824ec070a91f015331ae77ad6fa3c73c2519f28 100644
--- a/src/lib-sieve/plugins/editheader/Makefile.am
+++ b/src/lib-sieve/plugins/editheader/Makefile.am
@@ -1,7 +1,8 @@
 noinst_LTLIBRARIES = libsieve_ext_editheader.la
 
 AM_CPPFLAGS = \
-	-I$(top_srcdir)/src/lib-sieve \
+	-I$(srcdir)/../.. \
+	-I$(srcdir)/../../util \
 	$(LIBDOVECOT_INCLUDE)
 
 commands = \
diff --git a/src/lib-sieve/plugins/enotify/mailto/Makefile.am b/src/lib-sieve/plugins/enotify/mailto/Makefile.am
index 06b926293c8713e9469abb103a157836fa2c8e2f..83f129c6226fb5a60807dbb59633a7a5b5401b2d 100644
--- a/src/lib-sieve/plugins/enotify/mailto/Makefile.am
+++ b/src/lib-sieve/plugins/enotify/mailto/Makefile.am
@@ -3,6 +3,7 @@ noinst_LTLIBRARIES = libsieve_ext_enotify_mailto.la
 AM_CPPFLAGS = \
 	-I$(srcdir)/.. \
 	-I$(srcdir)/../../.. \
+	-I$(srcdir)/../../../util \
 	$(LIBDOVECOT_INCLUDE)
 
 libsieve_ext_enotify_mailto_la_SOURCES = \
diff --git a/src/lib-sieve/plugins/ihave/Makefile.am b/src/lib-sieve/plugins/ihave/Makefile.am
index 41aa3c67f6663ffdb5cb49efdd8a3ccbb5c017e8..1eb0472ef4f4276f0f00d1cd8e6cb5d6242f115e 100644
--- a/src/lib-sieve/plugins/ihave/Makefile.am
+++ b/src/lib-sieve/plugins/ihave/Makefile.am
@@ -1,7 +1,7 @@
 noinst_LTLIBRARIES = libsieve_ext_ihave.la
 
 AM_CPPFLAGS = \
-	-I$(top_srcdir)/src/lib-sieve \
+	-I$(srcdir)/../.. \
 	$(LIBDOVECOT_INCLUDE)
 
 tests = \
diff --git a/src/lib-sieve/plugins/metadata/Makefile.am b/src/lib-sieve/plugins/metadata/Makefile.am
index bac6f5d75fa6862946c9a76d301c49563a92b3f2..7aeaf2e183c259a7920b64fca79b884b30ef2a22 100644
--- a/src/lib-sieve/plugins/metadata/Makefile.am
+++ b/src/lib-sieve/plugins/metadata/Makefile.am
@@ -1,11 +1,10 @@
-
 noinst_LTLIBRARIES = libsieve_ext_metadata.la
 
 libsieve_ext_metadata_la_LDFLAGS = -module -avoid-version
 
 AM_CPPFLAGS = \
-	-I$(top_srcdir)/src/lib-sieve \
-	-I$(top_srcdir)/src/lib-sieve/plugins/variables \
+	-I$(srcdir)/../.. \
+	-I$(srcdir)/../variables \
 	$(LIBDOVECOT_INCLUDE)
 
 tests = \
diff --git a/src/lib-sieve/plugins/notify/Makefile.am b/src/lib-sieve/plugins/notify/Makefile.am
index b5165b9c58aa875a652071113443b74fd2cf0ce8..aaa76b357b29b5f08d669fa8ea7fe281e0e7414b 100644
--- a/src/lib-sieve/plugins/notify/Makefile.am
+++ b/src/lib-sieve/plugins/notify/Makefile.am
@@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libsieve_ext_notify.la
 
 AM_CPPFLAGS = \
 	-I$(srcdir)/../.. \
+	-I$(srcdir)/../../util \
 	$(LIBDOVECOT_INCLUDE)
 
 commands = \
diff --git a/src/lib-sieve/plugins/subaddress/Makefile.am b/src/lib-sieve/plugins/subaddress/Makefile.am
index 02386f290d91e6779af8a274a4eb31cdbb70de8b..2bab53f782c7d45bd584bcb49c06e65a3453dc19 100644
--- a/src/lib-sieve/plugins/subaddress/Makefile.am
+++ b/src/lib-sieve/plugins/subaddress/Makefile.am
@@ -1,7 +1,7 @@
 noinst_LTLIBRARIES = libsieve_ext_subaddress.la
 
 AM_CPPFLAGS = \
-	-I$(srcdir)/../../ \
+	-I$(srcdir)/../.. \
 	$(LIBDOVECOT_INCLUDE)
 
 libsieve_ext_subaddress_la_SOURCES = \
diff --git a/src/lib-sieve/plugins/vacation/Makefile.am b/src/lib-sieve/plugins/vacation/Makefile.am
index 10389fd46e5cc4d9fab1eeb5a2742677a2bd6829..09df27b987dbc087507a8131d65273d3440612a3 100644
--- a/src/lib-sieve/plugins/vacation/Makefile.am
+++ b/src/lib-sieve/plugins/vacation/Makefile.am
@@ -1,7 +1,8 @@
 noinst_LTLIBRARIES = libsieve_ext_vacation.la
 
 AM_CPPFLAGS = \
-	-I$(srcdir)/../../ \
+	-I$(srcdir)/../.. \
+	-I$(srcdir)/../../util \
 	$(LIBDOVECOT_INCLUDE)
 
 cmds = \
diff --git a/src/lib-sieve/plugins/vnd.dovecot/debug/Makefile.am b/src/lib-sieve/plugins/vnd.dovecot/debug/Makefile.am
index 04d2b0f50386a762448d149ae8f4b34dda156d30..1e08946337af3d22521a47915fa865a80a9573fb 100644
--- a/src/lib-sieve/plugins/vnd.dovecot/debug/Makefile.am
+++ b/src/lib-sieve/plugins/vnd.dovecot/debug/Makefile.am
@@ -1,7 +1,7 @@
 noinst_LTLIBRARIES = libsieve_ext_debug.la
 
 AM_CPPFLAGS = \
-	-I$(top_srcdir)/src/lib-sieve \
+	-I$(srcdir)/../../.. \
 	$(LIBDOVECOT_INCLUDE)
 
 commands = \
diff --git a/src/lib-sieve/plugins/vnd.dovecot/duplicate/Makefile.am b/src/lib-sieve/plugins/vnd.dovecot/duplicate/Makefile.am
index d50e0c4ddfbb90b29167301a7c7846170bb18897..3d72ae2239546865a8af9b53b646e74613666df6 100644
--- a/src/lib-sieve/plugins/vnd.dovecot/duplicate/Makefile.am
+++ b/src/lib-sieve/plugins/vnd.dovecot/duplicate/Makefile.am
@@ -1,7 +1,7 @@
 noinst_LTLIBRARIES = libsieve_ext_duplicate.la
 
 AM_CPPFLAGS = \
-	-I$(top_srcdir)/src/lib-sieve \
+	-I$(srcdir)/../../.. \
 	$(LIBDOVECOT_INCLUDE)
 
 tests = \
diff --git a/src/lib-sieve/util/Makefile.am b/src/lib-sieve/util/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..31c9a746b0fb504a7e86e1986cbefe0cd96a540a
--- /dev/null
+++ b/src/lib-sieve/util/Makefile.am
@@ -0,0 +1,19 @@
+noinst_LTLIBRARIES = libsieve_util.la
+
+AM_CPPFLAGS = \
+	$(LIBDOVECOT_INCLUDE) \
+	$(LIBDOVECOT_SERVICE_INCLUDE) \
+	-DMODULEDIR=\""$(dovecot_moduledir)"\"
+
+libsieve_util_la_DEPENDENCIES = $(LIBDOVECOT_STORAGE_DEPS) $(LIBDOVECOT_DEPS)
+
+libsieve_util_la_SOURCES = \
+	rfc2822.c \
+	edit-mail.c
+
+headers = \
+	rfc2822.h \
+	edit-mail.h
+
+pkginc_libdir=$(dovecot_pkgincludedir)/sieve
+pkginc_lib_HEADERS = $(headers)
diff --git a/src/lib-sieve/edit-mail.c b/src/lib-sieve/util/edit-mail.c
similarity index 100%
rename from src/lib-sieve/edit-mail.c
rename to src/lib-sieve/util/edit-mail.c
diff --git a/src/lib-sieve/edit-mail.h b/src/lib-sieve/util/edit-mail.h
similarity index 100%
rename from src/lib-sieve/edit-mail.h
rename to src/lib-sieve/util/edit-mail.h
diff --git a/src/lib-sieve/rfc2822.c b/src/lib-sieve/util/rfc2822.c
similarity index 100%
rename from src/lib-sieve/rfc2822.c
rename to src/lib-sieve/util/rfc2822.c
diff --git a/src/lib-sieve/rfc2822.h b/src/lib-sieve/util/rfc2822.h
similarity index 100%
rename from src/lib-sieve/rfc2822.h
rename to src/lib-sieve/util/rfc2822.h