aboutsummaryrefslogtreecommitdiffstats
path: root/mail/dbmail
diff options
context:
space:
mode:
authorbofh <bofh@FreeBSD.org>2017-02-25 05:15:52 +0800
committerbofh <bofh@FreeBSD.org>2017-02-25 05:15:52 +0800
commit8f75f2247ea2562e88addaaa7eeb169fd5418011 (patch)
tree443206cca0976456f3fd02dad4c4ebfe294c5904 /mail/dbmail
parent537dfb9b9a620aa258b58fd922488da2ba8adf1d (diff)
downloadfreebsd-ports-gnome-8f75f2247ea2562e88addaaa7eeb169fd5418011.tar.gz
freebsd-ports-gnome-8f75f2247ea2562e88addaaa7eeb169fd5418011.tar.zst
freebsd-ports-gnome-8f75f2247ea2562e88addaaa7eeb169fd5418011.zip
mail/dbmail: adopt latest fixes from git:
- login_disabled option before starttls for pop3 - fix compiler warnings for GCC5 - Fix IMAP mailbox maintanence - prevent assertion in p_string_erase - improve crypt authentication, also don't segfault when spasswd is empty - simplify log_query_time duration logic - Disconnect IMAP clients if only few free FDs left - Add primary key constraint to dbmail_authlog - Rework temporary connection failures - Give sensible default for retry 120s - Add retries for binding and searching - Bump search timeout to 60s - Increase ldap timeout to 600s 10 mins - Refactor deprecated functions - Get timeout from config - Remove redundant event_assign - Remove deprecated non functioning g_mem_profile - Add definition for authldap_free - Revert inadvertent event_assign removal - Reduce failed LDAP connection for search to error - Update LDAP to non deprecated search - Clear the ldap connection - Update ldap deprecated unbind - Fix typo - Update to ldap_unbind_ext_s and remove redundant sigaction - Rebalance commit rollback - Ensure mailbox2dbmail is using Python 2 - Tidy mailbox2dbmail man page - Update description of pid file location in server man page - Boundaries fixups ordering of parts do not add newline on - Prepend headers during delivery - Allow for systems that don't use proc PR: 210274 Submitted by: fluffy
Diffstat (limited to 'mail/dbmail')
-rw-r--r--mail/dbmail/Makefile6
-rw-r--r--mail/dbmail/files/patch-0001-login_disabled-option-before-starttls-for-pop377
-rw-r--r--mail/dbmail/files/patch-0002-fix-compiler-warnings-for-GCC5346
-rw-r--r--mail/dbmail/files/patch-0003-Fix-IMAP-mailbox-maintanence81
-rw-r--r--mail/dbmail/files/patch-0004-prevent-assertion-in-p_string_erase41
-rw-r--r--mail/dbmail/files/patch-0005-improve-crypt-authentication38
-rw-r--r--mail/dbmail/files/patch-0006-simplify-log_query_time-duration-logic119
-rw-r--r--mail/dbmail/files/patch-0007-Disconnect-IMAP-clients-if-only-few-free-FDs-left148
-rw-r--r--mail/dbmail/files/patch-0008-Add-primary-key-constraint-to-dbmail_authlog26
-rw-r--r--mail/dbmail/files/patch-0009-Rework-temporary-connection-failures162
-rw-r--r--mail/dbmail/files/patch-0010-Give-sensible-default-for-retry-120s36
-rw-r--r--mail/dbmail/files/patch-0011-Add-retries-for-binding-and-searching95
-rw-r--r--mail/dbmail/files/patch-0012-Bump-search-timeout-to-60s25
-rw-r--r--mail/dbmail/files/patch-0013-Increase-ldap-timeout-to-600s-10-mins34
-rw-r--r--mail/dbmail/files/patch-0014-Refactor-deprecared-functions123
-rw-r--r--mail/dbmail/files/patch-0015-Get-timeout-from-config61
-rw-r--r--mail/dbmail/files/patch-0016-Remove-redundant-event_assign30
-rw-r--r--mail/dbmail/files/patch-0017-Remove-deprecated-non-functioning-g_mem_profile26
-rw-r--r--mail/dbmail/files/patch-0018-Add-definition-for-authldap_free63
-rw-r--r--mail/dbmail/files/patch-0019-Revert-inadvertant-event_assign-removal30
-rw-r--r--mail/dbmail/files/patch-0020-Reduce-failed-LDAP-connection-for-search-to-error25
-rw-r--r--mail/dbmail/files/patch-0021-Update-LDAP-to-non-deprecated-search28
-rw-r--r--mail/dbmail/files/patch-0022-Clear-the-ldap-connection41
-rw-r--r--mail/dbmail/files/patch-0023-Update-ldap-deprecated-unbind25
-rw-r--r--mail/dbmail/files/patch-0024-Fix-typo25
-rw-r--r--mail/dbmail/files/patch-0025-Update-to-ldap_unbind_ext_s-and-remove-redundant-sigaction50
-rw-r--r--mail/dbmail/files/patch-0026-Rebalance-commit-rollback71
-rw-r--r--mail/dbmail/files/patch-0027-ensure-mailbox2dbmail-is-using-Python-222
-rw-r--r--mail/dbmail/files/patch-0028-tidy-mailbox2dbmail-man-page57
-rw-r--r--mail/dbmail/files/patch-0029-update-description-of-pid-file-location-in-server-man-page34
-rw-r--r--mail/dbmail/files/patch-0031-boundaries-fixups-ordering-of-parts-do-not-add-newline-on-180
-rw-r--r--mail/dbmail/files/patch-0032-10083-prepend-headers-during-delivery25
-rw-r--r--mail/dbmail/files/patch-0099-Allow-for-systems-that-don-t-use-proc35
-rw-r--r--mail/dbmail/files/patch-acinclude.m48
-rw-r--r--mail/dbmail/files/patch-src_clientbase.c10
-rw-r--r--mail/dbmail/files/patch-src_dbmail.h.in8
36 files changed, 2195 insertions, 16 deletions
diff --git a/mail/dbmail/Makefile b/mail/dbmail/Makefile
index c2e92de5061e..b1e597ae0cea 100644
--- a/mail/dbmail/Makefile
+++ b/mail/dbmail/Makefile
@@ -32,11 +32,11 @@ SIEVE_DESC= Sieve mail sorting language support
OPTIONS_SUB= yes
INSTALL_TARGET= install-strip
-USE_AUTOTOOLS= autoheader autoconf
USE_LDCONFIG= ${PREFIX}/lib/dbmail
USE_RC_SUBR= dbmail-pop3d dbmail-lmtpd dbmail-imapd dbmail-timsieved
-USE_OPENSSL= yes
-USES= gmake libtool pkgconfig shebangfix tar:bzip2
+USES= autoreconf gmake libtool pkgconfig shebangfix ssl tar:bzip2
+GNU_CONFIGURE= yes
+
SHEBANG_FILES= man/fixsp.pl
CPPFLAGS+= -I${LOCALBASE}/include
diff --git a/mail/dbmail/files/patch-0001-login_disabled-option-before-starttls-for-pop3 b/mail/dbmail/files/patch-0001-login_disabled-option-before-starttls-for-pop3
new file mode 100644
index 000000000000..65e3d2d6e8c3
--- /dev/null
+++ b/mail/dbmail/files/patch-0001-login_disabled-option-before-starttls-for-pop3
@@ -0,0 +1,77 @@
+From ec74380db22e3d92641c972aa53823d02a56a5ad Mon Sep 17 00:00:00 2001
+From: Paul J Stevens <p.stevens@lukkien.com>
+Date: Wed, 18 Feb 2015 21:26:38 +0100
+Subject: [PATCH 01/33] login_disabled option before starttls for pop3
+
+---
+ dbmail.conf | 5 +++++
+ src/pop3.c | 16 ++++++++++++++++
+ 2 files changed, 21 insertions(+)
+
+diff --git dbmail.conf dbmail.conf
+index 946e064..bf16d16 100644
+--- dbmail.conf
++++ dbmail.conf
+@@ -227,6 +227,11 @@ port = 110
+ # You can set an alternate banner to display when connecting to the service
+ # banner = DBMAIL pop3 server ready to rock
+
++# If TLS is enabled, login before starttls is normally
++# allowed. Use login_disabled=yes to change this
++#
++# login_disabled = no
++
+ #
+ # If yes, allows SMTP access from the host IP connecting by POP3.
+ # This requires addition configuration of your MTA
+diff --git src/pop3.c src/pop3.c
+index cc0863b..d03f71a 100644
+--- src/pop3.c
++++ src/pop3.c
+@@ -366,6 +366,7 @@ int pop3(ClientSession_T *session, const char *buffer)
+ int found = 0;
+ //int indx = 0;
+ int validate_result;
++ bool login_disabled = FALSE;
+ uint64_t result, top_lines, top_messageid, user_idnr;
+ unsigned char *md5_apop_he;
+ struct message *msg;
+@@ -431,6 +432,15 @@ int pop3(ClientSession_T *session, const char *buffer)
+ }
+ }
+
++ if (state == CLIENTSTATE_INITIAL_CONNECT) {
++ if (server_conf->ssl) {
++ Field_T val;
++ GETCONFIGVALUE("login_disabled", "POP", val);
++ if (SMATCH(val, "yes"))
++ login_disabled = TRUE;
++ }
++ }
++
+ switch (cmdtype) {
+
+ case POP3_QUIT:
+@@ -459,6 +469,9 @@ int pop3(ClientSession_T *session, const char *buffer)
+ if (state != CLIENTSTATE_INITIAL_CONNECT)
+ return pop3_error(session, "-ERR wrong command mode\r\n");
+
++ if (login_disabled && ! session->ci->sock->ssl_state)
++ return pop3_error(session, "-ERR try STLS\r\n");
++
+ if (session->username != NULL) {
+ /* reset username */
+ g_free(session->username);
+@@ -478,6 +491,9 @@ int pop3(ClientSession_T *session, const char *buffer)
+ if (state != CLIENTSTATE_INITIAL_CONNECT)
+ return pop3_error(session, "-ERR wrong command mode\r\n");
+
++ if (login_disabled && ! session->ci->sock->ssl_state)
++ return pop3_error(session, "-ERR try STLS\r\n");
++
+ if (session->password != NULL) {
+ g_free(session->password);
+ session->password = NULL;
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0002-fix-compiler-warnings-for-GCC5 b/mail/dbmail/files/patch-0002-fix-compiler-warnings-for-GCC5
new file mode 100644
index 000000000000..a33a1f973b47
--- /dev/null
+++ b/mail/dbmail/files/patch-0002-fix-compiler-warnings-for-GCC5
@@ -0,0 +1,346 @@
+From c3badbdb282a5ffb411ee2b5062ed345896fb149 Mon Sep 17 00:00:00 2001
+From: Paul J Stevens <p.stevens@lukkien.com>
+Date: Fri, 8 Jan 2016 18:42:24 +0100
+Subject: [PATCH 02/33] fix compiler warnings for GCC5
+
+---
+ src/dm_db.c | 26 +++++++++++++-------------
+ src/dm_mailbox.c | 6 +++---
+ src/dm_mailboxstate.c | 4 ++--
+ src/dm_message.c | 12 ++++++------
+ src/dm_misc.c | 6 +++---
+ src/dm_sievescript.c | 2 +-
+ src/maintenance.c | 2 +-
+ src/modules/authsql.c | 12 ++++++------
+ src/pop3.c | 2 +-
+ 9 files changed, 36 insertions(+), 36 deletions(-)
+
+diff --git src/dm_db.c src/dm_db.c
+index 8d72214..feb0b34 100644
+--- src/dm_db.c
++++ src/dm_db.c
+@@ -1047,7 +1047,7 @@ int db_check_version(void)
+ /* test existence of usermap table */
+ int db_use_usermap(void)
+ {
+- int use_usermap = TRUE;
++ volatile int use_usermap = TRUE;
+ Connection_T c = db_con_get();
+ TRY
+ if (! db_query(c, db_get_sql(SQL_TABLE_EXISTS), DBPFX, "usermap"))
+@@ -1214,7 +1214,7 @@ static int dm_quota_user_validate(uint64_t user_idnr, uint64_t msg_size)
+ int dm_quota_rebuild_user(uint64_t user_idnr)
+ {
+ Connection_T c; ResultSet_T r; volatile int t = DM_SUCCESS;
+- uint64_t quotum = 0;
++ volatile uint64_t quotum = 0;
+
+ c = db_con_get();
+ TRY
+@@ -1257,7 +1257,7 @@ int dm_quota_rebuild()
+
+ GList *quota = NULL;
+ struct used_quota *q;
+- int i = 0;
++ volatile int i = 0;
+ int result;
+
+ c = db_con_get();
+@@ -1375,7 +1375,7 @@ int db_get_reply_body(uint64_t user_idnr, char **reply_body)
+ uint64_t db_get_useridnr(uint64_t message_idnr)
+ {
+ Connection_T c; ResultSet_T r;
+- uint64_t user_idnr = 0;
++ volatile uint64_t user_idnr = 0;
+ c = db_con_get();
+ TRY
+ r = db_query(c, "SELECT %smailboxes.owner_idnr FROM %smailboxes, %smessages "
+@@ -1397,7 +1397,7 @@ uint64_t db_get_useridnr(uint64_t message_idnr)
+ int db_log_ip(const char *ip)
+ {
+ Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = DM_SUCCESS;
+- uint64_t id = 0;
++ volatile uint64_t id = 0;
+
+ c = db_con_get();
+ TRY
+@@ -1444,8 +1444,8 @@ int db_empty_mailbox(uint64_t user_idnr, int only_empty)
+ Connection_T c; ResultSet_T r; volatile int t = DM_SUCCESS;
+ GList *mboxids = NULL;
+ uint64_t *id;
+- unsigned i = 0;
+- int result = 0;
++ volatile unsigned i = 0;
++ volatile int result = 0;
+
+ c = db_con_get();
+
+@@ -2165,7 +2165,7 @@ static int mailboxes_by_regex(uint64_t user_idnr, int only_subscribed, const cha
+ char *spattern;
+ char *namespace, *username;
+ GString *qs = NULL;
+- int n_rows = 0;
++ volatile int n_rows = 0;
+ PreparedStatement_T stmt;
+ int prml;
+
+@@ -2862,7 +2862,7 @@ int db_movemsg(uint64_t mailbox_to, uint64_t mailbox_from)
+ #define EXPIRE_DAYS 3
+ int db_mailbox_has_message_id(uint64_t mailbox_idnr, const char *messageid)
+ {
+- int rows = 0;
++ volatile int rows = 0;
+ Connection_T c; ResultSet_T r; PreparedStatement_T s;
+ char expire[DEF_FRAGSIZE], partial[DEF_FRAGSIZE];
+ INIT_QUERY;
+@@ -2909,7 +2909,7 @@ int db_mailbox_has_message_id(uint64_t mailbox_idnr, const char *messageid)
+ static uint64_t message_get_size(uint64_t message_idnr)
+ {
+ Connection_T c; ResultSet_T r;
+- uint64_t size = 0;
++ volatile uint64_t size = 0;
+
+ c = db_con_get();
+ TRY
+@@ -3443,8 +3443,8 @@ int db_usermap_resolve(ClientBase_T *ci, const char *username, char *real_userna
+ {
+ char clientsock[DM_SOCKADDR_LEN];
+ const char *userid = NULL, *sockok = NULL, *sockno = NULL, *login = NULL;
+- unsigned row = 0;
+- int result = TRUE;
++ volatile unsigned row = 0;
++ volatile int result = TRUE;
+ int score, bestscore = -1;
+ char *bestlogin = NULL, *bestuserid = NULL;
+ Connection_T c; ResultSet_T r; PreparedStatement_T s;
+@@ -3653,7 +3653,7 @@ int db_user_validate(ClientBase_T *ci, const char *pwfield, uint64_t *user_idnr,
+ {
+ int is_validated = 0;
+ char salt[13], cryptres[35];
+- int t = FALSE;
++ volatile int t = FALSE;
+ char dbpass[COLUMN_WIDTH+1];
+ char encode[COLUMN_WIDTH+1];
+ char hashstr[FIELDSIZE];
+diff --git src/dm_mailbox.c src/dm_mailbox.c
+index 3558b8e..6f4b99c 100644
+--- src/dm_mailbox.c
++++ src/dm_mailbox.c
+@@ -181,7 +181,7 @@ static int _mimeparts_dump(DbmailMailbox *self, GMimeStream *ostream)
+ uint64_t msgid, physid, *id;
+ DbmailMessage *m;
+ GTree *uids;
+- int count = 0;
++ volatile int count = 0;
+ PreparedStatement_T stmt;
+ Connection_T c;
+ ResultSet_T r;
+@@ -475,7 +475,7 @@ char * dbmail_mailbox_sorted_as_string(DbmailMailbox *self)
+ uint64_t *msn;
+
+ l = g_list_first(self->sorted);
+- if (! g_list_length(l)>0)
++ if (! (g_list_length(l) > 0))
+ return s;
+
+ t = g_string_new("");
+@@ -1224,7 +1224,7 @@ static GTree * mailbox_search(DbmailMailbox *self, search_key *s)
+ char partial[DEF_FRAGSIZE];
+ Connection_T c; ResultSet_T r; PreparedStatement_T st;
+ GTree *ids;
+- char *inset = NULL;
++ volatile char *inset = NULL;
+
+ GString *t;
+ String_T q;
+diff --git src/dm_mailboxstate.c src/dm_mailboxstate.c
+index 2ef3fd3..723689a 100644
+--- src/dm_mailboxstate.c
++++ src/dm_mailboxstate.c
+@@ -977,7 +977,7 @@ int MailboxState_hasPermission(T M, uint64_t userid, const char *right_flag)
+ if (! owner_id) {
+ result = db_get_mailbox_owner(mboxid, &owner_id);
+ MailboxState_setOwner(M, owner_id);
+- if (! result > 0)
++ if (! (result > 0))
+ return result;
+ }
+
+@@ -1116,7 +1116,7 @@ int MailboxState_build_recent(T M)
+ return 0;
+ }
+
+-static long long int _update_recent(GList *slices, uint64_t seq)
++static long long int _update_recent(volatile GList *slices, uint64_t seq)
+ {
+ INIT_QUERY;
+ Connection_T c;
+diff --git src/dm_message.c src/dm_message.c
+index 066634a..9d30d52 100644
+--- src/dm_message.c
++++ src/dm_message.c
+@@ -361,11 +361,11 @@ static DbmailMessage * _mime_retrieve(DbmailMessage *self)
+ ResultSet_T r;
+ char internal_date[SQL_INTERNALDATE_LEN];
+ GMimeContentType *mimetype = NULL;
+- int prevdepth, depth = 0, row = 0;
++ volatile int prevdepth, depth = 0, row = 0;
+ volatile int t = FALSE;
+- gboolean got_boundary = FALSE, prev_boundary = FALSE, is_header = TRUE, prev_header, finalized=FALSE;
+- gboolean prev_is_message = FALSE, is_message = FALSE;
+- String_T m = NULL, n = NULL;
++ volatile gboolean got_boundary = FALSE, prev_boundary = FALSE, is_header = TRUE, prev_header, finalized=FALSE;
++ volatile gboolean prev_is_message = FALSE, is_message = FALSE;
++ volatile String_T m = NULL, n = NULL;
+ const void *blob;
+ Field_T frag;
+
+@@ -530,7 +530,7 @@ static gboolean store_mime_multipart(GMimeObject *object, DbmailMessage *m, cons
+ {
+ const char *boundary;
+ const char *preface = NULL, *postface = NULL;
+- int n, i, c;
++ int n = 0, i, c;
+
+ g_return_val_if_fail(GMIME_IS_OBJECT(object), TRUE);
+
+@@ -1977,7 +1977,7 @@ DbmailMessage * dbmail_message_construct(DbmailMessage *self,
+
+ static int get_mailbox_from_filters(DbmailMessage *message, uint64_t useridnr, const char *mailbox, char *into, size_t into_n)
+ {
+- int t = FALSE;
++ volatile int t = FALSE;
+ uint64_t anyone = 0;
+ PreparedStatement_T stmt;
+ Connection_T c;
+diff --git src/dm_misc.c src/dm_misc.c
+index e6ca9a0..1294930 100644
+--- src/dm_misc.c
++++ src/dm_misc.c
+@@ -1029,7 +1029,7 @@ int g_tree_merge(GTree *a, GTree *b, int condition)
+
+ type=g_strdup("AND");
+
+- if (! g_tree_nnodes(a) > 0)
++ if (! (g_tree_nnodes(a) > 0))
+ break;
+
+ /* delete from A all keys not in B */
+@@ -1053,7 +1053,7 @@ int g_tree_merge(GTree *a, GTree *b, int condition)
+ case IST_SUBSEARCH_OR:
+ type=g_strdup("OR");
+
+- if (! g_tree_nnodes(b) > 0)
++ if (! (g_tree_nnodes(b) > 0))
+ break;
+
+ merger->tree = a;
+@@ -1081,7 +1081,7 @@ int g_tree_merge(GTree *a, GTree *b, int condition)
+ case IST_SUBSEARCH_NOT:
+ type=g_strdup("NOT");
+
+- if (! g_tree_nnodes(b) > 0)
++ if (! (g_tree_nnodes(b) > 0))
+ break;
+
+ keys = g_tree_keys(b);
+diff --git src/dm_sievescript.c src/dm_sievescript.c
+index e163413..80f333d 100644
+--- src/dm_sievescript.c
++++ src/dm_sievescript.c
+@@ -132,7 +132,7 @@ int dm_sievescript_list(uint64_t user_idnr, GList **scriptlist)
+
+ int dm_sievescript_rename(uint64_t user_idnr, char *scriptname, char *newname)
+ {
+- int active = 0;
++ volatile int active = 0;
+ Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE;
+ assert(scriptname);
+
+diff --git src/maintenance.c src/maintenance.c
+index 2e46453..b4a020b 100644
+--- src/maintenance.c
++++ src/maintenance.c
+@@ -1012,7 +1012,7 @@ int do_migrate(int migrate_limit)
+ {
+ Connection_T c; ResultSet_T r;
+ int id = 0;
+- int count = 0;
++ volatile int count = 0;
+ DbmailMessage *m;
+
+ qprintf ("Migrate legacy 2.2.x messageblks to mimeparts...\n");
+diff --git src/modules/authsql.c src/modules/authsql.c
+index 49e1dc9..8e4829e 100644
+--- src/modules/authsql.c
++++ src/modules/authsql.c
+@@ -97,7 +97,7 @@ int auth_getclientid(uint64_t user_idnr, uint64_t * client_idnr)
+ {
+ assert(client_idnr != NULL);
+ *client_idnr = 0;
+- C c; R r; int t = TRUE;
++ C c; R r; volatile int t = TRUE;
+
+ c = db_con_get();
+ TRY
+@@ -118,7 +118,7 @@ int auth_getmaxmailsize(uint64_t user_idnr, uint64_t * maxmail_size)
+ {
+ assert(maxmail_size != NULL);
+ *maxmail_size = 0;
+- C c; R r; int t = TRUE;
++ C c; R r; volatile int t = TRUE;
+
+ c = db_con_get();
+ TRY
+@@ -361,7 +361,7 @@ uint64_t auth_md5_validate(ClientBase_T *ci UNUSED, char *username,
+ uint64_t user_idnr = 0;
+ const char *dbpass;
+ C c; R r;
+- int t = FALSE;
++ volatile int t = FALSE;
+
+ /* lookup the user_idnr */
+ if (! auth_user_exists(username, &user_idnr))
+@@ -430,7 +430,7 @@ char *auth_get_userid(uint64_t user_idnr)
+
+ int auth_check_userid(uint64_t user_idnr)
+ {
+- C c; R r; gboolean t = TRUE;
++ C c; R r; volatile gboolean t = TRUE;
+
+ c = db_con_get();
+ TRY
+@@ -570,7 +570,7 @@ int auth_addalias_ext(const char *alias,
+
+ int auth_removealias(uint64_t user_idnr, const char *alias)
+ {
+- C c; S s; gboolean t = FALSE;
++ C c; S s; volatile gboolean t = FALSE;
+
+ c = db_con_get();
+ TRY
+@@ -590,7 +590,7 @@ int auth_removealias(uint64_t user_idnr, const char *alias)
+
+ int auth_removealias_ext(const char *alias, const char *deliver_to)
+ {
+- C c; S s; gboolean t = FALSE;
++ C c; S s; volatile gboolean t = FALSE;
+
+ c = db_con_get();
+ TRY
+diff --git src/pop3.c src/pop3.c
+index d03f71a..b7106d3 100644
+--- src/pop3.c
++++ src/pop3.c
+@@ -99,7 +99,7 @@ static int db_createsession(uint64_t user_idnr, ClientSession_T * session)
+ {
+ Connection_T c; ResultSet_T r; volatile int t = DM_SUCCESS;
+ struct message *tmpmessage;
+- int message_counter = 0;
++ volatile int message_counter = 0;
+ const char *query_result;
+ uint64_t mailbox_idnr;
+ INIT_QUERY;
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0003-Fix-IMAP-mailbox-maintanence b/mail/dbmail/files/patch-0003-Fix-IMAP-mailbox-maintanence
new file mode 100644
index 000000000000..369f0030132b
--- /dev/null
+++ b/mail/dbmail/files/patch-0003-Fix-IMAP-mailbox-maintanence
@@ -0,0 +1,81 @@
+From f2d27f8727ae4159d356d63c1af6ac1a60b1261a Mon Sep 17 00:00:00 2001
+From: Pavlo Lavrenenko <Pavlo.Lavrenenko@portaone.com>
+Date: Thu, 21 May 2015 11:42:56 +0300
+Subject: [PATCH 03/33] Fix IMAP mailbox maintanence
+
+Update message's mailbox id in a separate explicit transaction to prevent
+SQLException: ORA-01453 error on the next db_begin_transaction() call
+(see db_mailbox_seq_update() next to 'UPDATE dbmail_messages SET mailbox_idnr'
+query).
+---
+ src/dm_db.c | 6 ++++++
+ src/dm_db.h | 1 +
+ src/maintenance.c | 8 ++------
+ 3 files changed, 9 insertions(+), 6 deletions(-)
+
+diff --git src/dm_db.c src/dm_db.c
+index feb0b34..313b33f 100644
+--- src/dm_db.c
++++ src/dm_db.c
+@@ -4290,6 +4290,12 @@ void db_message_set_seq(uint64_t message_id, uint64_t seq)
+ END_TRY;
+ }
+
++int db_move_message(uint64_t message_id, uint64_t mailbox_id)
++{
++ return db_update("UPDATE %smessages SET mailbox_idnr = %" PRIu64 " WHERE message_idnr = %" PRIu64 "",
++ DBPFX, mailbox_id, message_id);
++}
++
+ int db_rehash_store(void)
+ {
+ GList *ids = NULL;
+diff --git src/dm_db.h src/dm_db.h
+index 57aa256..84595d2 100644
+--- src/dm_db.h
++++ src/dm_db.h
+@@ -777,6 +777,7 @@ char * db_returning(const char *s);
+
+ uint64_t db_mailbox_seq_update(uint64_t mailbox_id, uint64_t message_id);
+ void db_message_set_seq(uint64_t message_id, uint64_t seq);
++int db_move_message(uint64_t message_id, uint64_t mailbox_id);
+
+ int db_rehash_store(void);
+
+diff --git src/maintenance.c src/maintenance.c
+index b4a020b..24b818c 100644
+--- src/maintenance.c
++++ src/maintenance.c
+@@ -1184,7 +1184,7 @@ int do_erase_old(int days, char * mbtrash_name)
+ /* Move message to Trash if the message is in INBOX mailbox and date less then passed date. */
+ int do_move_old (int days, char * mbinbox_name, char * mbtrash_name)
+ {
+- Connection_T c; ResultSet_T r; ResultSet_T r1; PreparedStatement_T s; PreparedStatement_T s1; PreparedStatement_T s2;
++ Connection_T c; ResultSet_T r; ResultSet_T r1; PreparedStatement_T s; PreparedStatement_T s1;
+ int skip = 1;
+ char expire [DEF_FRAGSIZE];
+ uint64_t mailbox_to;
+@@ -1200,9 +1200,7 @@ int do_move_old (int days, char * mbinbox_name, char * mbtrash_name)
+ "WHERE mb.name = ? AND msg.status < %d "
+ "AND phys.internal_date < %s",
+ DBPFX, DBPFX, DBPFX, MESSAGE_STATUS_DELETE, expire);
+-
+ s1 = db_stmt_prepare(c, "SELECT mailbox_idnr FROM %smailboxes WHERE owner_idnr = ? AND name = ?", DBPFX);
+- s2 = db_stmt_prepare(c, "UPDATE %smessages SET mailbox_idnr = ? WHERE message_idnr = ?", DBPFX);
+
+ db_stmt_set_str(s, 1, mbinbox_name);
+
+@@ -1225,9 +1223,7 @@ int do_move_old (int days, char * mbinbox_name, char * mbtrash_name)
+ }
+
+ if (!skip) {
+- db_stmt_set_u64(s2,1,mailbox_to);
+- db_stmt_set_u64(s2,2,id);
+- db_stmt_exec(s2);
++ db_move_message(id, mailbox_to);
+ db_mailbox_seq_update(mailbox_to, 0);
+ db_mailbox_seq_update(mailbox_from, 0);
+ }
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0004-prevent-assertion-in-p_string_erase b/mail/dbmail/files/patch-0004-prevent-assertion-in-p_string_erase
new file mode 100644
index 000000000000..dcef53496196
--- /dev/null
+++ b/mail/dbmail/files/patch-0004-prevent-assertion-in-p_string_erase
@@ -0,0 +1,41 @@
+From b6f28ffa54e6533fd0d8676248d0c9bc6d4bd859 Mon Sep 17 00:00:00 2001
+From: Paul J Stevens <p.stevens@lukkien.com>
+Date: Sun, 10 Jan 2016 20:01:31 +0100
+Subject: [PATCH 04/33] prevent assertion in p_string_erase
+
+---
+ src/dm_imapsession.c | 9 +++++----
+ 1 file changed, 5 insertions(+), 4 deletions(-)
+
+diff --git src/dm_imapsession.c src/dm_imapsession.c
+index dbf1834..67eae60 100644
+--- src/dm_imapsession.c
++++ src/dm_imapsession.c
+@@ -870,19 +870,20 @@ static void _imap_send_part(ImapSession *self, GMimeObject *part, body_fetch *bo
+ } else {
+ char *tmp = imap_get_logical_part(part,type);
+ String_T str = p_string_new(self->pool, tmp);
++ size_t len = p_string_len(str);
+ g_free(tmp);
+
+- if (p_string_len(str) < 1) {
++ if (len < 1) {
+ dbmail_imap_session_buff_printf(self, "] NIL");
+ } else {
+ uint64_t cnt = 0;
+ if (bodyfetch->octetcnt > 0) {
+- cnt = get_dumpsize(bodyfetch, p_string_len(str));
++ cnt = get_dumpsize(bodyfetch, len);
+ dbmail_imap_session_buff_printf(self, "]<%" PRIu64 "> {%" PRIu64 "}\r\n", bodyfetch->octetstart, cnt);
+- p_string_erase(str,0,bodyfetch->octetstart);
++ p_string_erase(str,0,min(bodyfetch->octetstart,len));
+ p_string_truncate(str,cnt);
+ } else {
+- dbmail_imap_session_buff_printf(self, "] {%" PRIu64 "}\r\n", p_string_len(str));
++ dbmail_imap_session_buff_printf(self, "] {%" PRIu64 "}\r\n", len);
+ }
+ dbmail_imap_session_buff_printf(self,"%s", p_string_str(str));
+ }
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0005-improve-crypt-authentication b/mail/dbmail/files/patch-0005-improve-crypt-authentication
new file mode 100644
index 000000000000..953c2d89794b
--- /dev/null
+++ b/mail/dbmail/files/patch-0005-improve-crypt-authentication
@@ -0,0 +1,38 @@
+From b4b82aca1dd1c8aece722b8370da02b715e4bb53 Mon Sep 17 00:00:00 2001
+From: Paul J Stevens <p.stevens@lukkien.com>
+Date: Wed, 10 Feb 2016 09:14:41 +0100
+Subject: [PATCH 05/33] improve crypt authentication
+
+also don't segfault when spasswd is empty
+---
+ src/dm_db.c | 7 ++++++-
+ 1 file changed, 6 insertions(+), 1 deletion(-)
+
+diff --git src/dm_db.c src/dm_db.c
+index 313b33f..bfe9601 100644
+--- src/dm_db.c
++++ src/dm_db.c
+@@ -3687,6 +3687,10 @@ int db_user_validate(ClientBase_T *ci, const char *pwfield, uint64_t *user_idnr,
+ return t;
+
+ if (! t) return FALSE;
++ if (! strlen(dbpass)) {
++ TRACE(TRACE_INFO, "Empty password for [%" PRIu64 "] in [%s]", *user_idnr, pwfield);
++ return FALSE;
++ }
+
+ if (SMATCH(encode, "")) {
+ TRACE(TRACE_DEBUG, "validating using plaintext passwords");
+@@ -3699,7 +3703,8 @@ int db_user_validate(ClientBase_T *ci, const char *pwfield, uint64_t *user_idnr,
+
+ if (SMATCH(encode, "crypt")) {
+ TRACE(TRACE_DEBUG, "validating using crypt() encryption");
+- is_validated = (strcmp((const char *) crypt(password, dbpass), dbpass) == 0) ? 1 : 0;
++ strncpy(salt, dbpass, 2);
++ is_validated = (strcmp((const char *) crypt(password, salt), dbpass) == 0) ? 1 : 0;
+ } else if (SMATCH(encode, "md5")) {
+ /* get password */
+ if (strncmp(dbpass, "$1$", 3)) { // no match
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0006-simplify-log_query_time-duration-logic b/mail/dbmail/files/patch-0006-simplify-log_query_time-duration-logic
new file mode 100644
index 000000000000..55a93c49f65c
--- /dev/null
+++ b/mail/dbmail/files/patch-0006-simplify-log_query_time-duration-logic
@@ -0,0 +1,119 @@
+From f5b73b342b4a6cae1bb15340e5da7330e29319c1 Mon Sep 17 00:00:00 2001
+From: Paul J Stevens <p.stevens@lukkien.com>
+Date: Sat, 5 Mar 2016 18:43:12 +0100
+Subject: [PATCH 06/33] simplify log_query_time duration logic
+
+---
+ src/dm_db.c | 16 ++++++++--------
+ src/dm_misc.c | 12 ++++++++++++
+ src/dm_misc.h | 2 ++
+ test/check_dbmail_db.c | 27 +++++++++++++++++++++++++++
+ 4 files changed, 49 insertions(+), 8 deletions(-)
+
+diff --git src/dm_db.c src/dm_db.c
+index bfe9601..429d0ae 100644
+--- src/dm_db.c
++++ src/dm_db.c
+@@ -354,14 +354,14 @@ void db_con_clear(Connection_T c)
+
+ void log_query_time(char *query, struct timeval before, struct timeval after)
+ {
+- double elapsed = ((double)after.tv_sec + ((double)after.tv_usec / 1000000)) - ((double)before.tv_sec + ((double)before.tv_usec / 1000000));
+- TRACE(TRACE_DATABASE, "last query took [%.3f] seconds", elapsed);
+- if (elapsed > (double)db_params.query_time_warning)
+- TRACE(TRACE_WARNING, "slow query [%s] took [%.3f] seconds", query, elapsed);
+- else if (elapsed > (double)db_params.query_time_notice)
+- TRACE(TRACE_NOTICE, "slow query [%s] took [%.3f] seconds", query, elapsed);
+- else if (elapsed > (double)db_params.query_time_info)
+- TRACE(TRACE_INFO, "slow query [%s] took [%.3f] seconds", query, elapsed);
++ unsigned int elapsed = (unsigned int)diff_time(before, after);
++ TRACE(TRACE_DATABASE, "last query took [%d] seconds", elapsed);
++ if (elapsed > db_params.query_time_warning)
++ TRACE(TRACE_WARNING, "slow query [%s] took [%d] seconds", query, elapsed);
++ else if (elapsed > db_params.query_time_notice)
++ TRACE(TRACE_NOTICE, "slow query [%s] took [%d] seconds", query, elapsed);
++ else if (elapsed > db_params.query_time_info)
++ TRACE(TRACE_INFO, "slow query [%s] took [%d] seconds", query, elapsed);
+ return;
+ }
+
+diff --git src/dm_misc.c src/dm_misc.c
+index 1294930..e27ef34 100644
+--- src/dm_misc.c
++++ src/dm_misc.c
+@@ -2422,4 +2422,16 @@ void uint64_free(void *data)
+ mempool_push(small_pool, data, sizeof(uint64_t));
+ }
+
++/*
++ * calculate the difference between two timeval values
++ * as number of seconds, using default rounding
++ */
++int diff_time(struct timeval before, struct timeval after)
++{
++ int tbefore = before.tv_sec * 1000000 + before.tv_usec;
++ int tafter = after.tv_sec * 1000000 + after.tv_usec;
++ int tdiff = tafter - tbefore;
++ return (int)rint((double)tdiff/1000000);
++}
++
+
+diff --git src/dm_misc.h src/dm_misc.h
+index a5dd04f..9660dfa 100644
+--- src/dm_misc.h
++++ src/dm_misc.h
+@@ -178,4 +178,6 @@ gchar * get_crlf_encoded_opt(const gchar *string, int dots);
+ void strip_crlf(char *buffer);
+ void uint64_free(void *);
+
++int diff_time(struct timeval before, struct timeval after);
++
+ #endif
+diff --git test/check_dbmail_db.c test/check_dbmail_db.c
+index 47c34be..e6cb170 100644
+--- test/check_dbmail_db.c
++++ test/check_dbmail_db.c
+@@ -414,6 +414,32 @@ START_TEST(test_db_get_sql)
+ }
+ END_TEST
+
++START_TEST(test_diff_time)
++{
++ struct timeval before, after;
++ int diff;
++
++ before.tv_sec = 1; before.tv_usec = 0;
++ after.tv_sec = 2; after.tv_usec = 0;
++ diff = diff_time(before, after);
++ fail_unless(diff == 1);
++
++ before.tv_sec = 1; before.tv_usec = 1000000 - 1;
++ after.tv_sec = 2; after.tv_usec = 0;
++ diff = diff_time(before, after);
++ fail_unless(diff == 0);
++
++ before.tv_sec = 1; before.tv_usec = 500001;
++ after.tv_sec = 2; after.tv_usec = 0;
++ diff = diff_time(before, after);
++ fail_unless(diff == 0);
++
++ before.tv_sec = 1; before.tv_usec = 499999;
++ after.tv_sec = 2; after.tv_usec = 0;
++ diff = diff_time(before, after);
++ fail_unless(diff == 1);
++}
++END_TEST
+
+ Suite *dbmail_db_suite(void)
+ {
+@@ -435,6 +461,7 @@ Suite *dbmail_db_suite(void)
+ tcase_add_test(tc_db, test_mailbox_match_new);
+ tcase_add_test(tc_db, test_db_findmailbox_by_regex);
+ tcase_add_test(tc_db, test_db_get_sql);
++ tcase_add_test(tc_db, test_diff_time);
+
+ return s;
+ }
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0007-Disconnect-IMAP-clients-if-only-few-free-FDs-left b/mail/dbmail/files/patch-0007-Disconnect-IMAP-clients-if-only-few-free-FDs-left
new file mode 100644
index 000000000000..eca0ae403168
--- /dev/null
+++ b/mail/dbmail/files/patch-0007-Disconnect-IMAP-clients-if-only-few-free-FDs-left
@@ -0,0 +1,148 @@
+From 6b7eccfec4f76b7d9d1f865caf741ff3214b5964 Mon Sep 17 00:00:00 2001
+From: Pavlo Lavrenenko <santa.ssh@gmail.com>
+Date: Thu, 2 Jun 2016 08:18:51 +0300
+Subject: [PATCH 07/33] Disconnect IMAP clients if only few free FDs left (#37)
+
+After network connection to DB server goes down the processing of IMAP session
+stalls: DB connection pool becomes exhausted as active connections do not
+close till TCP timeout kicks in (true at least for Oracle). While DBMail still
+accepts incoming connections it quickly reaches the RLIMIT_NOFILE and becomes
+unresponsive. Send BYE response if the number of opened FDs reaches the
+RLIMIT_NOFILE value.
+---
+ src/dbmail.h.in | 5 +++++
+ src/dm_misc.c | 20 ++++++++++++++++++++
+ src/dm_misc.h | 8 ++++++++
+ src/imap4.c | 23 ++++++++++++++++++++++-
+ 4 files changed, 55 insertions(+), 1 deletion(-)
+
+diff --git src/dbmail.h.in src/dbmail.h.in
+index d826dc3..17215ef 100644
+--- src/dbmail.h.in
++++ src/dbmail.h.in
+@@ -69,12 +69,14 @@
+ #include <string.h>
+ #include <strings.h>
+ #include <sysexits.h>
++#include <dirent.h>
+ #include <sys/types.h>
+ #include <sys/stat.h>
+ #include <sys/mman.h>
+ #include <sys/socket.h>
+ #include <sys/un.h>
+ #include <sys/time.h>
++#include <sys/resource.h>
+ #include <sys/wait.h>
+ #include <sys/ipc.h>
+ #include <sys/shm.h>
+@@ -252,6 +254,9 @@
+ /* input reading linelimit */
+ #define MAX_LINESIZE (64*1024)
+
++/* minumun number of free file descriptors required to run the daemon */
++#define FREE_DF_THRESHOLD 16
++
+ /* string length for query */
+ #define DEF_QUERYSIZE (32*1024)
+ #define DEF_FRAGSIZE 256
+diff --git src/dm_misc.c src/dm_misc.c
+index e27ef34..e795de1 100644
+--- src/dm_misc.c
++++ src/dm_misc.c
+@@ -104,6 +104,26 @@ int drop_privileges(char *newuser, char *newgroup)
+ return 0;
+ }
+
++int get_opened_fd_count(void)
++{
++ DIR* dir = NULL;
++ struct dirent* entry = NULL;
++ char buf[32];
++ int fd_count = 0;
++
++ snprintf(buf, 32, "/proc/%i/fd/", getpid());
++
++ dir = opendir(buf);
++ if (dir == NULL)
++ return -1;
++
++ while ((entry = readdir(dir)) != NULL)
++ fd_count++;
++ closedir(dir);
++
++ return fd_count - 2; /* exclude '.' and '..' entries */
++}
++
+ void create_unique_id(char *target, uint64_t message_idnr)
+ {
+ char md5_str[FIELDSIZE];
+diff --git src/dm_misc.h src/dm_misc.h
+index 9660dfa..b6cf24f 100644
+--- src/dm_misc.h
++++ src/dm_misc.h
+@@ -45,6 +45,14 @@ void g_string_maybe_shrink(GString *s);
+ int drop_privileges(char *newuser, char *newgroup);
+
+ /**
++ \brief get the number of opened files (requires /proc mounted)
++ \return
++ - -1 on error
++ - number of opened files
++*/
++int get_opened_fd_count(void);
++
++/**
+ * \brief create a unique id for a message (used for pop, stored per message)
+ * \param target target string. Length should be UID_SIZE
+ * \param message_idnr message_idnr of message
+diff --git src/imap4.c src/imap4.c
+index 0532f2e..e523edc 100644
+--- src/imap4.c
++++ src/imap4.c
+@@ -351,6 +351,12 @@ static void send_greeting(ImapSession *session)
+ dbmail_imap_session_set_state(session, CLIENTSTATE_NON_AUTHENTICATED);
+ }
+
++static void disconnect_user(ImapSession *session)
++{
++ imap_session_printf(session, "* BYE [Service unavailable.]\r\n");
++ imap_handle_abort(session);
++}
++
+ /*
+ * the default timeout callback */
+
+@@ -601,6 +607,8 @@ int imap_handle_connection(client_sock *c)
+ {
+ ImapSession *session;
+ ClientBase_T *ci;
++ struct rlimit fd_limit;
++ int fd_count;
+
+ ci = client_init(c);
+
+@@ -617,7 +625,20 @@ int imap_handle_connection(client_sock *c)
+ Capa_remove(session->capa, "LOGINDISABLED");
+ }
+
+- send_greeting(session);
++ fd_count = get_opened_fd_count();
++ if (fd_count < 0 || getrlimit(RLIMIT_NPROC, &fd_limit) < 0) {
++ TRACE(TRACE_ERR,
++ "[%p] failed to retrieve fd limits, dropping client connection",
++ session);
++ disconnect_user(session);
++ } else if (fd_limit.rlim_cur - fd_count < FREE_DF_THRESHOLD) {
++ TRACE(TRACE_WARNING,
++ "[%p] fd count [%d], fd limit [%d], fd threshold [%d]: dropping client connection",
++ session, fd_count, fd_limit.rlim_cur, FREE_DF_THRESHOLD);
++ disconnect_user(session);
++ } else {
++ send_greeting(session);
++ }
+
+ reset_callbacks(session);
+
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0008-Add-primary-key-constraint-to-dbmail_authlog b/mail/dbmail/files/patch-0008-Add-primary-key-constraint-to-dbmail_authlog
new file mode 100644
index 000000000000..5b2c8001e631
--- /dev/null
+++ b/mail/dbmail/files/patch-0008-Add-primary-key-constraint-to-dbmail_authlog
@@ -0,0 +1,26 @@
+From 024ba19d1c83bee22b6b5901d2fff586d9391cf3 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Tue, 24 Feb 2015 11:27:45 +0000
+Subject: [PATCH 08/33] Add primary key constraint to dbmail_authlog
+
+---
+ sql/postgresql/create_tables.pgsql | 3 ++-
+ 1 file changed, 2 insertions(+), 1 deletion(-)
+
+diff --git sql/postgresql/create_tables.pgsql sql/postgresql/create_tables.pgsql
+index da3b694..45740fd 100644
+--- sql/postgresql/create_tables.pgsql
++++ sql/postgresql/create_tables.pgsql
+@@ -46,7 +46,8 @@ CREATE TABLE dbmail_authlog (
+ dst_port INT8,
+ status VARCHAR(32) DEFAULT 'active',
+ bytes_rx INT8 DEFAULT '0' NOT NULL,
+- bytes_tx INT8 DEFAULT '0' NOT NULL
++ bytes_tx INT8 DEFAULT '0' NOT NULL,
++ PRIMARY KEY (id)
+ );
+
+
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0009-Rework-temporary-connection-failures b/mail/dbmail/files/patch-0009-Rework-temporary-connection-failures
new file mode 100644
index 000000000000..8d5486fdfd8b
--- /dev/null
+++ b/mail/dbmail/files/patch-0009-Rework-temporary-connection-failures
@@ -0,0 +1,162 @@
+From 9d093f76a82c88647df1cd0ef08b3e8f82e26abb Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Fri, 30 Sep 2016 17:17:05 +0100
+Subject: [PATCH 09/33] Rework temporary connection failures
+
+---
+ src/modules/authldap.c | 102 ++++++++++++++++++++++++++++++++-----------------
+ 1 file changed, 67 insertions(+), 35 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 19802c8..2073768 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -110,18 +110,48 @@ static gpointer authldap_once(gpointer UNUSED data)
+ return (gpointer)NULL;
+ }
+
+-/*
+- lookup thread-local ldap connection
+-*/
++/*
++ * ldap_con_get()
++ *
++ * Lookup thread-local ldap connection and bind using config credentials
++ * retrying a few times if the server is temporarily unavailable
++ *
++ * returns connection on success, NULL on failure
++ */
+ static LDAP * ldap_con_get(void)
+ {
+- LDAP * c = (LDAP *)g_static_private_get(&ldap_conn_key);
+- if (! c) {
+- authldap_connect();
+- c = (LDAP *)g_static_private_get(&ldap_conn_key);
++ LDAP * ld = (LDAP *)g_static_private_get(&ldap_conn_key);
++ if (ld) {
++ TRACE(TRACE_DEBUG, "connection [%p]", ld);
++ return ld;
+ }
+- TRACE(TRACE_DEBUG, "connection [%p]", c);
+- return c;
++ int c = 0;
++ int err = -1; // Start wanting success
++ while (err != 0 && c++ < 5) {
++ // Loop until success or too many retries
++ TRACE(TRACE_DEBUG, "No connection trying [%d]", c);
++
++ err = authldap_connect();
++
++ switch (err) {
++ case LDAP_SUCCESS:
++ ld = (LDAP *)g_static_private_get(&ldap_conn_key);
++ TRACE(TRACE_DEBUG, "connection [%p]", ld);
++ break;
++ case LDAP_SERVER_DOWN:
++ TRACE(TRACE_WARNING, "LDAP gone away: %s. Trying to reconnect(%d/5).", ldap_err2string(err),c);
++ sleep(2); // reconnect failed. wait before trying again
++ break;
++ default:
++ TRACE(TRACE_ERR, "LDAP error(%d): %s", err, ldap_err2string(err));
++ break;
++ }
++ }
++ if (! ld) {
++ TRACE(TRACE_ERR, "Unable to connect to LDAP giving up");
++ }
++ TRACE(TRACE_DEBUG, "connection [%p]", ld);
++ return ld;
+ }
+
+ /*
+@@ -140,6 +170,13 @@ static void authldap_free(gpointer data)
+ sigaction(SIGPIPE, &oldact, 0);
+ }
+
++/*
++ * auth_ldap_bind()
++ *
++ * Bind to server using config credentials
++ *
++ * returns 0 on success, -1 on failure
++ */
+ static int auth_ldap_bind(void)
+ {
+ int err;
+@@ -153,7 +190,6 @@ static int auth_ldap_bind(void)
+ }
+
+ return 0;
+-
+ }
+
+ /*
+@@ -213,44 +249,40 @@ static int authldap_connect(void)
+ return auth_ldap_bind();
+ }
+
+-static int authldap_reconnect(void)
+-{
+- LDAP *c;
+- if ((c = ldap_con_get())) authldap_free((gpointer)c);
+- return authldap_connect();
+-}
+-
++/*
++ * authldap_search()
++ *
++ * Perform an LDAP search
++ *
++ * returns search results on success, NULL on failure
++ */
+ static LDAPMessage * authldap_search(const gchar *query)
+ {
+ LDAPMessage *ldap_res;
+ int _ldap_attrsonly = 0;
+ char **_ldap_attrs = NULL;
+- int c=0, err;
++ int err;
+ LDAP *_ldap_conn;
+
+ g_return_val_if_fail(query!=NULL, NULL);
+
+ _ldap_conn = ldap_con_get();
+
+- while (c++ < 5) {
+- TRACE(TRACE_DEBUG, " [%s]", query);
+- err = ldap_search_s(_ldap_conn, _ldap_cfg.base_dn, _ldap_cfg.scope_int,
+- query, _ldap_attrs, _ldap_attrsonly, &ldap_res);
++ TRACE(TRACE_DEBUG, " [%s]", query);
++ err = ldap_search_s(_ldap_conn, _ldap_cfg.base_dn, _ldap_cfg.scope_int,
++ query, _ldap_attrs, _ldap_attrsonly, &ldap_res);
+
+- if (! err)
+- return ldap_res;
++ if (! err)
++ return ldap_res;
+
+- switch (err) {
+- case LDAP_SERVER_DOWN:
+- TRACE(TRACE_WARNING, "LDAP gone away: %s. Try to reconnect(%d/5).", ldap_err2string(err),c);
+- if (authldap_reconnect())
+- sleep(2); // reconnect failed. wait before trying again
+- break;
+- default:
+- TRACE(TRACE_ERR, "LDAP error(%d): %s", err, ldap_err2string(err));
+- return NULL;
+- break;
+- }
++ switch (err) {
++ case LDAP_SERVER_DOWN:
++ TRACE(TRACE_WARNING, "LDAP gone away: %s).", ldap_err2string(err));
++ break;
++ default:
++ TRACE(TRACE_ERR, "LDAP error(%d): %s", err, ldap_err2string(err));
++ return NULL;
++ break;
+ }
+
+ TRACE(TRACE_EMERG,"unrecoverable error while talking to ldap server");
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0010-Give-sensible-default-for-retry-120s b/mail/dbmail/files/patch-0010-Give-sensible-default-for-retry-120s
new file mode 100644
index 000000000000..0d33885dcf3a
--- /dev/null
+++ b/mail/dbmail/files/patch-0010-Give-sensible-default-for-retry-120s
@@ -0,0 +1,36 @@
+From fd3d09d5bbd3bd03e5eeaaec900f0e45b67ca064 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Sun, 2 Oct 2016 17:45:42 +0100
+Subject: [PATCH 10/33] Give sensible default for retry 120s
+
+---
+ src/modules/authldap.c | 6 +++---
+ 1 file changed, 3 insertions(+), 3 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 2073768..1b1b1bd 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -127,7 +127,7 @@ static LDAP * ldap_con_get(void)
+ }
+ int c = 0;
+ int err = -1; // Start wanting success
+- while (err != 0 && c++ < 5) {
++ while (err != 0 && c++ < 120) {
+ // Loop until success or too many retries
+ TRACE(TRACE_DEBUG, "No connection trying [%d]", c);
+
+@@ -139,8 +139,8 @@ static LDAP * ldap_con_get(void)
+ TRACE(TRACE_DEBUG, "connection [%p]", ld);
+ break;
+ case LDAP_SERVER_DOWN:
+- TRACE(TRACE_WARNING, "LDAP gone away: %s. Trying to reconnect(%d/5).", ldap_err2string(err),c);
+- sleep(2); // reconnect failed. wait before trying again
++ TRACE(TRACE_WARNING, "LDAP gone away: %s. Trying to reconnect(%d/120).", ldap_err2string(err),c);
++ sleep(1); // reconnect failed. wait before trying again
+ break;
+ default:
+ TRACE(TRACE_ERR, "LDAP error(%d): %s", err, ldap_err2string(err));
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0011-Add-retries-for-binding-and-searching b/mail/dbmail/files/patch-0011-Add-retries-for-binding-and-searching
new file mode 100644
index 000000000000..743ead38b986
--- /dev/null
+++ b/mail/dbmail/files/patch-0011-Add-retries-for-binding-and-searching
@@ -0,0 +1,95 @@
+From cca81b0164c83a90eafa8d27d4887638cae080b5 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Tue, 4 Oct 2016 15:34:04 +0100
+Subject: [PATCH 11/33] Add retries for binding and searching
+
+---
+ src/modules/authldap.c | 43 +++++++++++++++++++++++++++----------------
+ 1 file changed, 27 insertions(+), 16 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 1b1b1bd..475c985 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -126,10 +126,11 @@ static LDAP * ldap_con_get(void)
+ return ld;
+ }
+ int c = 0;
++ int c_tries = 120;
+ int err = -1; // Start wanting success
+- while (err != 0 && c++ < 120) {
++ while (err != 0 && c++ < c_tries) {
+ // Loop until success or too many retries
+- TRACE(TRACE_DEBUG, "No connection trying [%d]", c);
++ TRACE(TRACE_DEBUG, "No connection trying [%d/%d]", c, c_tries);
+
+ err = authldap_connect();
+
+@@ -139,11 +140,13 @@ static LDAP * ldap_con_get(void)
+ TRACE(TRACE_DEBUG, "connection [%p]", ld);
+ break;
+ case LDAP_SERVER_DOWN:
+- TRACE(TRACE_WARNING, "LDAP gone away: %s. Trying to reconnect(%d/120).", ldap_err2string(err),c);
++ TRACE(TRACE_WARNING, "LDAP gone away: %s. Trying to reconnect(%d/%d).", ldap_err2string(err), c, c_tries);
+ sleep(1); // reconnect failed. wait before trying again
+ break;
+ default:
++ // Includes timeouts etc. Should probably refactor.
+ TRACE(TRACE_ERR, "LDAP error(%d): %s", err, ldap_err2string(err));
++ sleep(1);
+ break;
+ }
+ }
+@@ -261,7 +264,9 @@ static LDAPMessage * authldap_search(const gchar *query)
+ LDAPMessage *ldap_res;
+ int _ldap_attrsonly = 0;
+ char **_ldap_attrs = NULL;
+- int err;
++ int err = -1; // Start wanting success
++ int c = 0;
++ int c_tries = 10;
+ LDAP *_ldap_conn;
+
+ g_return_val_if_fail(query!=NULL, NULL);
+@@ -269,20 +274,26 @@ static LDAPMessage * authldap_search(const gchar *query)
+ _ldap_conn = ldap_con_get();
+
+ TRACE(TRACE_DEBUG, " [%s]", query);
+- err = ldap_search_s(_ldap_conn, _ldap_cfg.base_dn, _ldap_cfg.scope_int,
+- query, _ldap_attrs, _ldap_attrsonly, &ldap_res);
+
+- if (! err)
+- return ldap_res;
++ while (err != 0 && c++ < c_tries) {
++ // Loop until success or too many retries
++
++ err = ldap_search_s(_ldap_conn, _ldap_cfg.base_dn, _ldap_cfg.scope_int,
++ query, _ldap_attrs, _ldap_attrsonly, &ldap_res);
+
+- switch (err) {
+- case LDAP_SERVER_DOWN:
+- TRACE(TRACE_WARNING, "LDAP gone away: %s).", ldap_err2string(err));
+- break;
+- default:
+- TRACE(TRACE_ERR, "LDAP error(%d): %s", err, ldap_err2string(err));
+- return NULL;
+- break;
++ switch (err) {
++ case LDAP_SUCCESS:
++ return ldap_res;
++ break;
++ case LDAP_SERVER_DOWN:
++ TRACE(TRACE_WARNING, "LDAP gone away: %s. Trying again(%d/%d).", ldap_err2string(err), c, c_tries);
++ break;
++ default:
++ // Includes timeouts etc. Should probably refactor.
++ TRACE(TRACE_ERR, "LDAP error(%d): %s. Trying again (%d/%d).", err, ldap_err2string(err), c, c_tries);
++ break;
++ }
++ sleep(1); // Search failed. Wait before trying again.
+ }
+
+ TRACE(TRACE_EMERG,"unrecoverable error while talking to ldap server");
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0012-Bump-search-timeout-to-60s b/mail/dbmail/files/patch-0012-Bump-search-timeout-to-60s
new file mode 100644
index 000000000000..677028f5f201
--- /dev/null
+++ b/mail/dbmail/files/patch-0012-Bump-search-timeout-to-60s
@@ -0,0 +1,25 @@
+From 5ed508954309e9a5ad6610be47fd02be60f1e726 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Thu, 6 Oct 2016 15:04:51 +0100
+Subject: [PATCH 12/33] Bump search timeout to 60s
+
+---
+ src/modules/authldap.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 475c985..2a39e0e 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -266,7 +266,7 @@ static LDAPMessage * authldap_search(const gchar *query)
+ char **_ldap_attrs = NULL;
+ int err = -1; // Start wanting success
+ int c = 0;
+- int c_tries = 10;
++ int c_tries = 60;
+ LDAP *_ldap_conn;
+
+ g_return_val_if_fail(query!=NULL, NULL);
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0013-Increase-ldap-timeout-to-600s-10-mins b/mail/dbmail/files/patch-0013-Increase-ldap-timeout-to-600s-10-mins
new file mode 100644
index 000000000000..ee606d7204f7
--- /dev/null
+++ b/mail/dbmail/files/patch-0013-Increase-ldap-timeout-to-600s-10-mins
@@ -0,0 +1,34 @@
+From 9a13e4878b94b742e17519c2f4379ee5a3498a73 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Tue, 11 Oct 2016 14:34:59 +0100
+Subject: [PATCH 13/33] Increase ldap timeout to 600s (10 mins)
+
+---
+ src/modules/authldap.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 2a39e0e..3f43a56 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -126,7 +126,7 @@ static LDAP * ldap_con_get(void)
+ return ld;
+ }
+ int c = 0;
+- int c_tries = 120;
++ int c_tries = 600;
+ int err = -1; // Start wanting success
+ while (err != 0 && c++ < c_tries) {
+ // Loop until success or too many retries
+@@ -266,7 +266,7 @@ static LDAPMessage * authldap_search(const gchar *query)
+ char **_ldap_attrs = NULL;
+ int err = -1; // Start wanting success
+ int c = 0;
+- int c_tries = 60;
++ int c_tries = 600;
+ LDAP *_ldap_conn;
+
+ g_return_val_if_fail(query!=NULL, NULL);
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0014-Refactor-deprecared-functions b/mail/dbmail/files/patch-0014-Refactor-deprecared-functions
new file mode 100644
index 000000000000..a40f72cc87c2
--- /dev/null
+++ b/mail/dbmail/files/patch-0014-Refactor-deprecared-functions
@@ -0,0 +1,123 @@
+From 30e4904b6baf5c3f00d4b096f5b51cfbb69958d0 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Wed, 12 Oct 2016 16:56:22 +0100
+Subject: [PATCH 14/33] Refactor deprecared functions
+
+---
+ src/modules/authldap.c | 44 ++++++++++++++++++++------------------------
+ 1 file changed, 20 insertions(+), 24 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 3f43a56..bbe553b 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -28,7 +28,23 @@
+
+ extern char configFile[PATH_MAX];
+
+-GStaticPrivate ldap_conn_key;
++/*
++ signal-safe releasing of thread-local ldap connection
++*/
++static void authldap_free(gpointer data)
++{
++ LDAP *c = (LDAP *)data;
++ struct sigaction act, oldact;
++
++ memset(&act, 0, sizeof(act));
++ memset(&oldact, 0, sizeof(oldact));
++ act.sa_handler = SIG_IGN;
++ sigaction(SIGPIPE, &act, &oldact);
++ ldap_unbind(c);
++ sigaction(SIGPIPE, &oldact, 0);
++}
++
++static GPrivate ldap_conn_key = G_PRIVATE_INIT (authldap_free);
+ static GOnce ldap_conn_once = G_ONCE_INIT;
+ static int authldap_connect(void);
+
+@@ -105,7 +121,6 @@ static void __auth_get_config(void)
+ */
+ static gpointer authldap_once(gpointer UNUSED data)
+ {
+- g_static_private_init(&ldap_conn_key);
+ __auth_get_config();
+ return (gpointer)NULL;
+ }
+@@ -120,7 +135,7 @@ static gpointer authldap_once(gpointer UNUSED data)
+ */
+ static LDAP * ldap_con_get(void)
+ {
+- LDAP * ld = (LDAP *)g_static_private_get(&ldap_conn_key);
++ LDAP * ld = (LDAP *)g_private_get(&ldap_conn_key);
+ if (ld) {
+ TRACE(TRACE_DEBUG, "connection [%p]", ld);
+ return ld;
+@@ -136,7 +151,7 @@ static LDAP * ldap_con_get(void)
+
+ switch (err) {
+ case LDAP_SUCCESS:
+- ld = (LDAP *)g_static_private_get(&ldap_conn_key);
++ ld = (LDAP *)g_private_get(&ldap_conn_key);
+ TRACE(TRACE_DEBUG, "connection [%p]", ld);
+ break;
+ case LDAP_SERVER_DOWN:
+@@ -144,7 +159,6 @@ static LDAP * ldap_con_get(void)
+ sleep(1); // reconnect failed. wait before trying again
+ break;
+ default:
+- // Includes timeouts etc. Should probably refactor.
+ TRACE(TRACE_ERR, "LDAP error(%d): %s", err, ldap_err2string(err));
+ sleep(1);
+ break;
+@@ -158,22 +172,6 @@ static LDAP * ldap_con_get(void)
+ }
+
+ /*
+- signal-safe releasing of thread-local ldap connection
+-*/
+-static void authldap_free(gpointer data)
+-{
+- LDAP *c = (LDAP *)data;
+- struct sigaction act, oldact;
+-
+- memset(&act, 0, sizeof(act));
+- memset(&oldact, 0, sizeof(oldact));
+- act.sa_handler = SIG_IGN;
+- sigaction(SIGPIPE, &act, &oldact);
+- ldap_unbind(c);
+- sigaction(SIGPIPE, &oldact, 0);
+-}
+-
+-/*
+ * auth_ldap_bind()
+ *
+ * Bind to server using config credentials
+@@ -247,7 +245,7 @@ static int authldap_connect(void)
+ if (strncasecmp(_ldap_cfg.referrals, "no", 2) == 0)
+ ldap_set_option(_ldap_conn, LDAP_OPT_REFERRALS, 0);
+
+- g_static_private_set(&ldap_conn_key, _ldap_conn, (GDestroyNotify)authldap_free);
++ g_private_replace(&ldap_conn_key, _ldap_conn);
+
+ return auth_ldap_bind();
+ }
+@@ -289,7 +287,6 @@ static LDAPMessage * authldap_search(const gchar *query)
+ TRACE(TRACE_WARNING, "LDAP gone away: %s. Trying again(%d/%d).", ldap_err2string(err), c, c_tries);
+ break;
+ default:
+- // Includes timeouts etc. Should probably refactor.
+ TRACE(TRACE_ERR, "LDAP error(%d): %s. Trying again (%d/%d).", err, ldap_err2string(err), c, c_tries);
+ break;
+ }
+@@ -634,7 +631,6 @@ int auth_connect(void)
+ }
+ int auth_disconnect(void)
+ {
+- g_static_private_free(&ldap_conn_key);
+ return 0;
+ }
+
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0015-Get-timeout-from-config b/mail/dbmail/files/patch-0015-Get-timeout-from-config
new file mode 100644
index 000000000000..b0f879c4da2b
--- /dev/null
+++ b/mail/dbmail/files/patch-0015-Get-timeout-from-config
@@ -0,0 +1,61 @@
+From af47f87dec649ad495a704926d5e0e004ad17301 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Wed, 12 Oct 2016 18:23:40 +0100
+Subject: [PATCH 15/33] Get timeout from config
+
+---
+ src/modules/authldap.c | 9 +++++++--
+ 1 file changed, 7 insertions(+), 2 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index bbe553b..2bf3b95 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -59,7 +59,9 @@ typedef struct _ldap_cfg {
+ Field_T field_members;
+ Field_T query_string;
+ Field_T referrals;
++ Field_T query_timeout;
+ int scope_int, port_int, version_int;
++ int query_timeout_int;
+ } _ldap_cfg_t;
+
+ static _ldap_cfg_t _ldap_cfg;
+@@ -90,6 +92,7 @@ static void __auth_get_config(void)
+ GETCONFIGVALUE("QUERY_STRING", "LDAP", _ldap_cfg.query_string);
+ GETCONFIGVALUE("SCOPE", "LDAP", _ldap_cfg.scope);
+ GETCONFIGVALUE("REFERRALS", "LDAP", _ldap_cfg.referrals);
++ GETCONFIGVALUE("QUERY_TIMEOUT", "LDAP", _ldap_cfg.query_timeout);
+
+ /* Store the port as an integer for later use. */
+ _ldap_cfg.port_int = atoi(_ldap_cfg.port);
+@@ -113,6 +116,8 @@ static void __auth_get_config(void)
+ else
+ _ldap_cfg.scope_int = LDAP_SCOPE_SUBTREE;
+ }
++ /* Store the timeout as an integer. */
++ _ldap_cfg.query_timeout_int = atoi(_ldap_cfg.query_timeout);
+ TRACE(TRACE_DEBUG, "integer ldap scope is [%d]", _ldap_cfg.scope_int);
+ }
+
+@@ -141,7 +146,7 @@ static LDAP * ldap_con_get(void)
+ return ld;
+ }
+ int c = 0;
+- int c_tries = 600;
++ int c_tries = _ldap_cfg.query_timeout_int;
+ int err = -1; // Start wanting success
+ while (err != 0 && c++ < c_tries) {
+ // Loop until success or too many retries
+@@ -264,7 +269,7 @@ static LDAPMessage * authldap_search(const gchar *query)
+ char **_ldap_attrs = NULL;
+ int err = -1; // Start wanting success
+ int c = 0;
+- int c_tries = 600;
++ int c_tries = _ldap_cfg.query_timeout_int;
+ LDAP *_ldap_conn;
+
+ g_return_val_if_fail(query!=NULL, NULL);
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0016-Remove-redundant-event_assign b/mail/dbmail/files/patch-0016-Remove-redundant-event_assign
new file mode 100644
index 000000000000..c306826b06d1
--- /dev/null
+++ b/mail/dbmail/files/patch-0016-Remove-redundant-event_assign
@@ -0,0 +1,30 @@
+From 5be7e049c56442111a0a6ffa1d82b66a8547e6a8 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Thu, 13 Oct 2016 16:09:49 +0100
+Subject: [PATCH 16/33] Remove redundant event_assign
+
+---
+ src/server.c | 2 --
+ 1 file changed, 2 deletions(-)
+
+diff --git src/server.c src/server.c
+index ba8e5c5..a940030 100644
+--- src/server.c
++++ src/server.c
+@@ -826,13 +826,11 @@ int server_run(ServerConfig_T *conf)
+ for (i = 0; i < conf->socketcount; i++) {
+ TRACE(TRACE_DEBUG, "Adding event for plain socket [%d] [%d/%d]", conf->listenSockets[i], i+1, total);
+ evsock[i] = event_new(evbase, conf->listenSockets[i], EV_READ, server_sock_cb, NULL);
+- event_assign(evsock[i], evbase, conf->listenSockets[i], EV_READ, server_sock_cb, evsock[i]);
+ event_add(evsock[i], NULL);
+ }
+ for (k = i, i = 0; i < conf->ssl_socketcount; i++, k++) {
+ TRACE(TRACE_DEBUG, "Adding event for ssl socket [%d] [%d/%d]", conf->ssl_listenSockets[i], k+1, total);
+ evsock[k] = event_new(evbase, conf->ssl_listenSockets[i], EV_READ, server_sock_ssl_cb, NULL);
+- event_assign(evsock[k], evbase, conf->ssl_listenSockets[i], EV_READ, server_sock_ssl_cb, evsock[k]);
+ event_add(evsock[k], NULL);
+ }
+ }
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0017-Remove-deprecated-non-functioning-g_mem_profile b/mail/dbmail/files/patch-0017-Remove-deprecated-non-functioning-g_mem_profile
new file mode 100644
index 000000000000..1d272a44757a
--- /dev/null
+++ b/mail/dbmail/files/patch-0017-Remove-deprecated-non-functioning-g_mem_profile
@@ -0,0 +1,26 @@
+From 2caa12157f2efc7c4482ab4130f7aaec1f48bef2 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Thu, 13 Oct 2016 16:10:51 +0100
+Subject: [PATCH 17/33] Remove deprecated non functioning g_mem_profile
+
+---
+ src/server.c | 3 ---
+ 1 file changed, 3 deletions(-)
+
+diff --git src/server.c src/server.c
+index a940030..e013ac0 100644
+--- src/server.c
++++ src/server.c
+@@ -656,9 +656,6 @@ void server_sig_cb(int UNUSED fd, short UNUSED event, void *arg)
+ mainReload = 1;
+ case SIGPIPE: // ignore
+ break;
+- case SIGUSR1:
+- g_mem_profile();
+- break;
+ default:
+ exit(0);
+ break;
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0018-Add-definition-for-authldap_free b/mail/dbmail/files/patch-0018-Add-definition-for-authldap_free
new file mode 100644
index 000000000000..c0f86c1e3b2b
--- /dev/null
+++ b/mail/dbmail/files/patch-0018-Add-definition-for-authldap_free
@@ -0,0 +1,63 @@
+From cf3b24b2ea79c262c7827a954da9105a21b2c7d7 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Fri, 14 Oct 2016 13:59:56 +0100
+Subject: [PATCH 18/33] Add definition for authldap_free
+
+---
+ src/modules/authldap.c | 33 +++++++++++++++++----------------
+ 1 file changed, 17 insertions(+), 16 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 2bf3b95..065ee31 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -28,22 +28,7 @@
+
+ extern char configFile[PATH_MAX];
+
+-/*
+- signal-safe releasing of thread-local ldap connection
+-*/
+-static void authldap_free(gpointer data)
+-{
+- LDAP *c = (LDAP *)data;
+- struct sigaction act, oldact;
+-
+- memset(&act, 0, sizeof(act));
+- memset(&oldact, 0, sizeof(oldact));
+- act.sa_handler = SIG_IGN;
+- sigaction(SIGPIPE, &act, &oldact);
+- ldap_unbind(c);
+- sigaction(SIGPIPE, &oldact, 0);
+-}
+-
++static void authldap_free(gpointer data);
+ static GPrivate ldap_conn_key = G_PRIVATE_INIT (authldap_free);
+ static GOnce ldap_conn_once = G_ONCE_INIT;
+ static int authldap_connect(void);
+@@ -177,6 +162,22 @@ static LDAP * ldap_con_get(void)
+ }
+
+ /*
++ signal-safe releasing of thread-local ldap connection
++*/
++static void authldap_free(gpointer data)
++{
++ LDAP *c = (LDAP *)data;
++ struct sigaction act, oldact;
++
++ memset(&act, 0, sizeof(act));
++ memset(&oldact, 0, sizeof(oldact));
++ act.sa_handler = SIG_IGN;
++ sigaction(SIGPIPE, &act, &oldact);
++ ldap_unbind(c);
++ sigaction(SIGPIPE, &oldact, 0);
++}
++
++/*
+ * auth_ldap_bind()
+ *
+ * Bind to server using config credentials
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0019-Revert-inadvertant-event_assign-removal b/mail/dbmail/files/patch-0019-Revert-inadvertant-event_assign-removal
new file mode 100644
index 000000000000..7bf72992c58b
--- /dev/null
+++ b/mail/dbmail/files/patch-0019-Revert-inadvertant-event_assign-removal
@@ -0,0 +1,30 @@
+From 91014a1cd01e17215fdc03fb5d922b5b73ac564a Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Fri, 14 Oct 2016 17:54:49 +0100
+Subject: [PATCH 19/33] Revert inadvertant event_assign removal
+
+---
+ src/server.c | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git src/server.c src/server.c
+index e013ac0..72fab52 100644
+--- src/server.c
++++ src/server.c
+@@ -823,11 +823,13 @@ int server_run(ServerConfig_T *conf)
+ for (i = 0; i < conf->socketcount; i++) {
+ TRACE(TRACE_DEBUG, "Adding event for plain socket [%d] [%d/%d]", conf->listenSockets[i], i+1, total);
+ evsock[i] = event_new(evbase, conf->listenSockets[i], EV_READ, server_sock_cb, NULL);
++ event_assign(evsock[i], evbase, conf->listenSockets[i], EV_READ, server_sock_cb, evsock[i]);
+ event_add(evsock[i], NULL);
+ }
+ for (k = i, i = 0; i < conf->ssl_socketcount; i++, k++) {
+ TRACE(TRACE_DEBUG, "Adding event for ssl socket [%d] [%d/%d]", conf->ssl_listenSockets[i], k+1, total);
+ evsock[k] = event_new(evbase, conf->ssl_listenSockets[i], EV_READ, server_sock_ssl_cb, NULL);
++ event_assign(evsock[k], evbase, conf->ssl_listenSockets[i], EV_READ, server_sock_ssl_cb, evsock[k]);
+ event_add(evsock[k], NULL);
+ }
+ }
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0020-Reduce-failed-LDAP-connection-for-search-to-error b/mail/dbmail/files/patch-0020-Reduce-failed-LDAP-connection-for-search-to-error
new file mode 100644
index 000000000000..16e0daed9619
--- /dev/null
+++ b/mail/dbmail/files/patch-0020-Reduce-failed-LDAP-connection-for-search-to-error
@@ -0,0 +1,25 @@
+From 7daaac91d559f15a476c39f52228cc643f4608ba Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Tue, 18 Oct 2016 13:38:04 +0100
+Subject: [PATCH 20/33] Reduce failed LDAP connection for search to error
+
+---
+ src/modules/authldap.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 065ee31..fc9db8e 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -299,7 +299,7 @@ static LDAPMessage * authldap_search(const gchar *query)
+ sleep(1); // Search failed. Wait before trying again.
+ }
+
+- TRACE(TRACE_EMERG,"unrecoverable error while talking to ldap server");
++ TRACE(TRACE_ERR,"unrecoverable error while talking to ldap server");
+ return NULL;
+ }
+
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0021-Update-LDAP-to-non-deprecated-search b/mail/dbmail/files/patch-0021-Update-LDAP-to-non-deprecated-search
new file mode 100644
index 000000000000..edb8beff6c50
--- /dev/null
+++ b/mail/dbmail/files/patch-0021-Update-LDAP-to-non-deprecated-search
@@ -0,0 +1,28 @@
+From 00ff6ae1f64337ca6e723d060ad14873f1661447 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Wed, 19 Oct 2016 17:52:04 +0100
+Subject: [PATCH 21/33] Update LDAP to non deprecated search
+
+---
+ src/modules/authldap.c | 5 +++--
+ 1 file changed, 3 insertions(+), 2 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index fc9db8e..11c1305 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -282,8 +282,9 @@ static LDAPMessage * authldap_search(const gchar *query)
+ while (err != 0 && c++ < c_tries) {
+ // Loop until success or too many retries
+
+- err = ldap_search_s(_ldap_conn, _ldap_cfg.base_dn, _ldap_cfg.scope_int,
+- query, _ldap_attrs, _ldap_attrsonly, &ldap_res);
++ // timeout must be NULL as any value times out!
++ err = ldap_search_ext_s(_ldap_conn, _ldap_cfg.base_dn, _ldap_cfg.scope_int,
++ query, _ldap_attrs, _ldap_attrsonly, NULL, NULL, NULL, LDAP_NO_LIMIT, &ldap_res);
+
+ switch (err) {
+ case LDAP_SUCCESS:
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0022-Clear-the-ldap-connection b/mail/dbmail/files/patch-0022-Clear-the-ldap-connection
new file mode 100644
index 000000000000..cf6ffcbf62bf
--- /dev/null
+++ b/mail/dbmail/files/patch-0022-Clear-the-ldap-connection
@@ -0,0 +1,41 @@
+From 24869db9c10be2eb90ba2ca9095d92c5645b6320 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Mon, 24 Oct 2016 15:12:09 +0100
+Subject: [PATCH 22/33] Clear the ldap connection
+
+---
+ src/modules/authldap.c | 4 ++++
+ src/server.c | 2 ++
+ 2 files changed, 6 insertions(+)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 11c1305..7fa3a73 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -638,6 +638,10 @@ int auth_connect(void)
+ }
+ int auth_disconnect(void)
+ {
++ // Just free the pointer,
++ // G_PRIVATE_INIT calls GDestroyNotify
++ // which calls authldap_free()
++ g_private_replace(&ldap_conn_key, NULL)
+ return 0;
+ }
+
+diff --git src/server.c src/server.c
+index 72fab52..ba3ce10 100644
+--- src/server.c
++++ src/server.c
+@@ -282,6 +282,8 @@ static int server_start_cli(ServerConfig_T *conf)
+ TRACE(TRACE_ERR, "could not connect to authentication");
+ return -1;
+ }
++ // Disconnect this connection as threads will create their own
++ auth_disconnect();
+
+ srand((int) ((int) time(NULL) + (int) getpid()));
+
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0023-Update-ldap-deprecated-unbind b/mail/dbmail/files/patch-0023-Update-ldap-deprecated-unbind
new file mode 100644
index 000000000000..7b1bf9d23c47
--- /dev/null
+++ b/mail/dbmail/files/patch-0023-Update-ldap-deprecated-unbind
@@ -0,0 +1,25 @@
+From de4a37ae92bec56314318647a0561c891fee6e33 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Mon, 24 Oct 2016 15:13:21 +0100
+Subject: [PATCH 23/33] Update ldap deprecated unbind
+
+---
+ src/modules/authldap.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 7fa3a73..9c9d82e 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -173,7 +173,7 @@ static void authldap_free(gpointer data)
+ memset(&oldact, 0, sizeof(oldact));
+ act.sa_handler = SIG_IGN;
+ sigaction(SIGPIPE, &act, &oldact);
+- ldap_unbind(c);
++ ldap_unbind_ext(c, NULL, NULL);
+ sigaction(SIGPIPE, &oldact, 0);
+ }
+
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0024-Fix-typo b/mail/dbmail/files/patch-0024-Fix-typo
new file mode 100644
index 000000000000..2badc0e014d7
--- /dev/null
+++ b/mail/dbmail/files/patch-0024-Fix-typo
@@ -0,0 +1,25 @@
+From bd6b9d7d43640abbdd7f0ca410576e0c4d97c3dd Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Mon, 24 Oct 2016 17:02:49 +0100
+Subject: [PATCH 24/33] Fix typo
+
+---
+ src/modules/authldap.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 9c9d82e..2180ed0 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -641,7 +641,7 @@ int auth_disconnect(void)
+ // Just free the pointer,
+ // G_PRIVATE_INIT calls GDestroyNotify
+ // which calls authldap_free()
+- g_private_replace(&ldap_conn_key, NULL)
++ g_private_replace(&ldap_conn_key, NULL);
+ return 0;
+ }
+
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0025-Update-to-ldap_unbind_ext_s-and-remove-redundant-sigaction b/mail/dbmail/files/patch-0025-Update-to-ldap_unbind_ext_s-and-remove-redundant-sigaction
new file mode 100644
index 000000000000..02fae6257d8a
--- /dev/null
+++ b/mail/dbmail/files/patch-0025-Update-to-ldap_unbind_ext_s-and-remove-redundant-sigaction
@@ -0,0 +1,50 @@
+From 53ca6f11530a73838d73d3064f4b3d48e477f614 Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Fri, 28 Oct 2016 16:38:20 +0100
+Subject: [PATCH 25/33] Update to ldap_unbind_ext_s and remove redundant
+ sigaction
+
+---
+ src/modules/authldap.c | 19 ++++++++++---------
+ 1 file changed, 10 insertions(+), 9 deletions(-)
+
+diff --git src/modules/authldap.c src/modules/authldap.c
+index 2180ed0..05c3331 100644
+--- src/modules/authldap.c
++++ src/modules/authldap.c
+@@ -167,14 +167,15 @@ static LDAP * ldap_con_get(void)
+ static void authldap_free(gpointer data)
+ {
+ LDAP *c = (LDAP *)data;
+- struct sigaction act, oldact;
+-
+- memset(&act, 0, sizeof(act));
+- memset(&oldact, 0, sizeof(oldact));
+- act.sa_handler = SIG_IGN;
+- sigaction(SIGPIPE, &act, &oldact);
+- ldap_unbind_ext(c, NULL, NULL);
+- sigaction(SIGPIPE, &oldact, 0);
++
++ int err = ldap_set_option(c, LDAP_OPT_SERVER_CONTROLS, NULL );
++ if ( err != LDAP_OPT_SUCCESS ) {
++ TRACE(TRACE_ERR, "Could not unset controls");
++ }
++
++ if ((err = ldap_unbind_ext_s(c, NULL, NULL))) {
++ TRACE(TRACE_ERR, "ldap_unbind_ext_s failed: %s", ldap_err2string(err));
++ }
+ }
+
+ /*
+@@ -639,7 +640,7 @@ int auth_connect(void)
+ int auth_disconnect(void)
+ {
+ // Just free the pointer,
+- // G_PRIVATE_INIT calls GDestroyNotify
++ // G_PRIVATE_INIT calls GDestroyNotify
+ // which calls authldap_free()
+ g_private_replace(&ldap_conn_key, NULL);
+ return 0;
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0026-Rebalance-commit-rollback b/mail/dbmail/files/patch-0026-Rebalance-commit-rollback
new file mode 100644
index 000000000000..7e414d75e9ca
--- /dev/null
+++ b/mail/dbmail/files/patch-0026-Rebalance-commit-rollback
@@ -0,0 +1,71 @@
+From 1a09a645c82e07fee8d42642a83652c6c85473ff Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Mon, 31 Oct 2016 18:27:45 +0000
+Subject: [PATCH 26/33] Rebalance commit/rollback
+
+---
+ src/dm_mailboxstate.c | 9 ++++++---
+ src/imapcommands.c | 1 +
+ 2 files changed, 7 insertions(+), 3 deletions(-)
+
+diff --git src/dm_mailboxstate.c src/dm_mailboxstate.c
+index 723689a..c04b6d7 100644
+--- src/dm_mailboxstate.c
++++ src/dm_mailboxstate.c
+@@ -212,11 +212,12 @@ T MailboxState_new(Mempool_T pool, uint64_t id)
+ db_begin_transaction(c); // we need read-committed isolation
+ state_load_metadata(M, c);
+ state_load_messages(M, c);
++ db_commit_transaction(c);
+ CATCH(SQLException)
+ LOG_SQLERROR;
++ db_rollback_transaction(c);
+ t = DM_EQUERY;
+ FINALLY
+- db_commit_transaction(c);
+ db_con_close(c);
+ END_TRY;
+
+@@ -888,11 +889,12 @@ int MailboxState_info(T M)
+ TRY
+ db_begin_transaction(c);
+ db_getmailbox_info(M, c);
++ db_commit_transaction(c);
+ CATCH(SQLException)
+ LOG_SQLERROR;
++ db_rollback_transaction(c);
+ t = DM_EQUERY;
+ FINALLY
+- db_commit_transaction(c);
+ db_con_close(c);
+ END_TRY;
+
+@@ -928,11 +930,12 @@ int MailboxState_count(T M)
+ TRY
+ db_begin_transaction(c);
+ db_getmailbox_count(M, c);
++ db_commit_transaction(c);
+ CATCH(SQLException)
+ LOG_SQLERROR;
++ db_rollback_transaction(c);
+ t = DM_EQUERY;
+ FINALLY
+- db_commit_transaction(c);
+ db_con_close(c);
+ END_TRY;
+
+diff --git src/imapcommands.c src/imapcommands.c
+index bdedb4a..43760fc 100644
+--- src/imapcommands.c
++++ src/imapcommands.c
+@@ -946,6 +946,7 @@ void _ic_delete_enter(dm_thread_data *D)
+ db_commit_transaction(c);
+ CATCH(SQLException)
+ LOG_SQLERROR;
++ db_rollback_transaction(c);
+ t = DM_EQUERY;
+ FINALLY
+ db_con_close(c);
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0027-ensure-mailbox2dbmail-is-using-Python-2 b/mail/dbmail/files/patch-0027-ensure-mailbox2dbmail-is-using-Python-2
new file mode 100644
index 000000000000..7077be3c31a5
--- /dev/null
+++ b/mail/dbmail/files/patch-0027-ensure-mailbox2dbmail-is-using-Python-2
@@ -0,0 +1,22 @@
+From 650dd4bd29c245a290a9e3f2dc4b0f734de22b69 Mon Sep 17 00:00:00 2001
+From: Chris Mayo <aklhfex@gmail.com>
+Date: Sat, 23 Jul 2016 17:32:43 +0100
+Subject: [PATCH 27/33] ensure mailbox2dbmail is using Python 2
+
+---
+ contrib/mailbox2dbmail/mailbox2dbmail | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git contrib/mailbox2dbmail/mailbox2dbmail contrib/mailbox2dbmail/mailbox2dbmail
+index 6babffd..fa5c59d 100755
+--- contrib/mailbox2dbmail/mailbox2dbmail
++++ contrib/mailbox2dbmail/mailbox2dbmail
+@@ -1,4 +1,4 @@
+-#!/usr/bin/python
++#!/usr/bin/python2
+
+ # Copyright (C) 2004 Dan Weber <dan at mirrorlynx dot com>
+ # Copyright (C) 2012 Paul J Stevens <paul@nfg.nl>
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0028-tidy-mailbox2dbmail-man-page b/mail/dbmail/files/patch-0028-tidy-mailbox2dbmail-man-page
new file mode 100644
index 000000000000..7df8a75bf692
--- /dev/null
+++ b/mail/dbmail/files/patch-0028-tidy-mailbox2dbmail-man-page
@@ -0,0 +1,57 @@
+From 82b4d7ac22cccd411568e7ad471fc3aee93ef7a5 Mon Sep 17 00:00:00 2001
+From: Chris Mayo <aklhfex@gmail.com>
+Date: Sat, 23 Jul 2016 17:33:25 +0100
+Subject: [PATCH 28/33] tidy mailbox2dbmail man page
+
+---
+ contrib/mailbox2dbmail/mailbox2dbmail.1 | 8 ++++----
+ contrib/mailbox2dbmail/mailbox2dbmail.yo | 8 ++++----
+ 2 files changed, 8 insertions(+), 8 deletions(-)
+
+diff --git contrib/mailbox2dbmail/mailbox2dbmail.1 contrib/mailbox2dbmail/mailbox2dbmail.1
+index 0d35702..402ff34 100644
+--- contrib/mailbox2dbmail/mailbox2dbmail.1
++++ contrib/mailbox2dbmail/mailbox2dbmail.1
+@@ -9,15 +9,15 @@ Version info
+ .IP "\fB-h,--help\fP"
+ Help Display
+ .IP "\fB-b,--box\fP"
+-IMAP folder to stick mail in under user (Default: Inbox)
++IMAP folder to stick mail in under user (default: Inbox)
+ .IP "\fB-m,--mail\fP"
+-Location of Mailbox(required)
++Location of Mailbox (required)
+ .IP "\fB-u,--user\fP"
+-Username(required)
++Username (required)
+ .IP "\fB-t,--type\fP"
+ Type of Mailbox: mbox, maildir, mhdir (required)
+ .IP "\fB-p,--path\fP"
+-Path to dbmail-deliver(default: /usr/sbin/dbmail-deliver)
++Path to dbmail-deliver (default: /usr/sbin/dbmail-deliver)
+ .PP
+ .SH "SEE ALSO"
+ \fBpython(1)\fP, and \fBpydoc mailbox\fP
+diff --git contrib/mailbox2dbmail/mailbox2dbmail.yo contrib/mailbox2dbmail/mailbox2dbmail.yo
+index 46ad068..88c5fb1 100644
+--- contrib/mailbox2dbmail/mailbox2dbmail.yo
++++ contrib/mailbox2dbmail/mailbox2dbmail.yo
+@@ -6,11 +6,11 @@ manpageoptions()
+ startdit()
+ dit(bf(-v,--version)) Version info
+ dit(bf(-h,--help)) Help Display
+-dit(bf(-b,--box)) IMAP folder to stick mail in under user (Default: Inbox)
+-dit(bf(-m,--mail)) Location of Mailbox(required)
+-dit(bf(-u,--user)) Username(required)
++dit(bf(-b,--box)) IMAP folder to stick mail in under user (default: Inbox)
++dit(bf(-m,--mail)) Location of Mailbox (required)
++dit(bf(-u,--user)) Username (required)
+ dit(bf(-t,--type)) Type of Mailbox: mbox, maildir, mhdir (required)
+-dit(bf(-p,--path)) Path to dbmail-deliver(default: /usr/sbin/dbmail-deliver)
++dit(bf(-p,--path)) Path to dbmail-deliver (default: /usr/sbin/dbmail-deliver)
+ enddit()
+
+ manpageseealso()
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0029-update-description-of-pid-file-location-in-server-man-page b/mail/dbmail/files/patch-0029-update-description-of-pid-file-location-in-server-man-page
new file mode 100644
index 000000000000..7b88c134e424
--- /dev/null
+++ b/mail/dbmail/files/patch-0029-update-description-of-pid-file-location-in-server-man-page
@@ -0,0 +1,34 @@
+From 17c7e5f34b66cdfc4e9621cb69431ee657cfb346 Mon Sep 17 00:00:00 2001
+From: Chris Mayo <aklhfex@gmail.com>
+Date: Fri, 22 Jul 2016 20:38:00 +0100
+Subject: [PATCH 29/33] update description of pid file location in server man
+ pages
+
+---
+ man/serveropts.txt | 10 ++++++----
+ 1 file changed, 6 insertions(+), 4 deletions(-)
+
+diff --git man/serveropts.txt man/serveropts.txt
+index b01400d..5e14527 100644
+--- man/serveropts.txt
++++ man/serveropts.txt
+@@ -3,10 +3,12 @@ OPTIONS
+ -------
+
+ -p pidfile::
+- Specify an alternate pid file. The daemons are currently hardcoded
+- to use /var/run/dbmail-<daemon> for their pid files, and will halt
+- if the pid file cannot be written. Use the -p pidfile option to
+- place the pid file in your system's preferred location.
++ Specify an alternate pid file. By default the daemons use dbmail-<daemon>.pid
++ for their pid files, saving them in the directory specified by the
++ pid_directory entry in dbmail.conf or if that does not exist the value set by
++ the configuration option --localstatedir, and will halt if the pid file
++ cannot be written. Use the -p pidfile option to place the pid file in
++ your system's preferred location.
+
+ -n::
+ No daemonize: inetd mode. The program remains attached to the console from
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0031-boundaries-fixups-ordering-of-parts-do-not-add-newline-on- b/mail/dbmail/files/patch-0031-boundaries-fixups-ordering-of-parts-do-not-add-newline-on-
new file mode 100644
index 000000000000..d2f2f6bf163b
--- /dev/null
+++ b/mail/dbmail/files/patch-0031-boundaries-fixups-ordering-of-parts-do-not-add-newline-on-
@@ -0,0 +1,180 @@
+From 3fd6782988f01c2f56af19012e470ea16e800f51 Mon Sep 17 00:00:00 2001
+From: Michele Comitini <michele.comitini@gmail.com>
+Date: Tue, 1 Nov 2016 21:20:16 +0100
+Subject: [PATCH 31/33] boundaries fixups: ordering of parts, do not add
+ newline on boundary if there's no part after previous header (#34)
+
+* Fix IMAP mailbox maintanence
+
+Update message's mailbox id in a separate explicit transaction to prevent
+SQLException: ORA-01453 error on the next db_begin_transaction() call
+(see db_mailbox_seq_update() next to 'UPDATE dbmail_messages SET mailbox_idnr'
+query).
+
+* add newline only if is_message
+
+* better fix for rfc822 multipart messages with boundary after header
+
+* fixed some race condition when closing boundaries, since key does not change. Needed ordering parts on depth DESC when key does not vary.
+
+* fixes ordering in insertion
+---
+ src/dm_message.c | 12 ++++---
+ test/check_dbmail.h | 78 ++++++++++++++++++++++++++++++++++++++++++++-
+ test/check_dbmail_message.c | 8 +++++
+ 3 files changed, 93 insertions(+), 5 deletions(-)
+
+diff --git src/dm_message.c src/dm_message.c
+index 9d30d52..784c091 100644
+--- src/dm_message.c
++++ src/dm_message.c
+@@ -387,7 +387,7 @@ static DbmailMessage * _mime_retrieve(DbmailMessage *self)
+ "FROM %smimeparts p "
+ "JOIN %spartlists l ON p.id = l.part_id "
+ "JOIN %sphysmessage ph ON ph.id = l.physmessage_id "
+- "WHERE l.physmessage_id = ? ORDER BY l.part_key,l.part_order ASC",
++ "WHERE l.physmessage_id = ? ORDER BY l.part_key, l.part_order ASC, l.part_depth DESC",
+ frag, p_string_str(n), DBPFX, DBPFX, DBPFX);
+ db_stmt_set_u64(stmt, 1, self->id);
+ r = db_stmt_query(stmt);
+@@ -454,10 +454,14 @@ static DbmailMessage * _mime_retrieve(DbmailMessage *self)
+ if ((depth > 0) && (blist[depth-1][0]))
+ strncpy(boundary, blist[depth-1], MAX_MIME_BLEN-1);
+
+- if (is_header && (!prev_header || prev_boundary || (prev_header && depth>0 && !prev_is_message))) {
++ if (is_header)
++ if (prev_header && depth>0 && !prev_is_message) {
++ dprint("--%s\n", boundary);
++ p_string_append_printf(m, "--%s\n", boundary);
++ } else if (!prev_header || prev_boundary) {
+ dprint("\n--%s\n", boundary);
+ p_string_append_printf(m, "\n--%s\n", boundary);
+- }
++ }
+
+ p_string_append_printf(m, "%s", str);
+ dprint("<part is_header=\"%d\" depth=\"%d\" key=\"%d\" order=\"%d\">\n%s\n</part>\n",
+@@ -558,7 +562,7 @@ static gboolean store_mime_multipart(GMimeObject *object, DbmailMessage *m, cons
+ if (boundary) {
+ n++;
+ m->part_depth--;
+- m->part_order=n;
++ m->part_order++;
+ }
+
+ if (g_mime_content_type_is_type(GMIME_CONTENT_TYPE(content_type), "multipart", "*") &&
+diff --git test/check_dbmail.h test/check_dbmail.h
+index 09c9958..439c8a1 100644
+--- test/check_dbmail.h
++++ test/check_dbmail.h
+@@ -1561,7 +1561,7 @@ char *multipart_message8 = "From: nobody@example.org\n"
+ "bmdzLnhtbFBLAQItABQABgAIAAAAIQDv0+Pp+gEAAPsDAAAQAAAAAAAAAAAAAAAAALREAABkb2NQ\n"
+ "cm9wcy9hcHAueG1sUEsFBgAAAAAPAA8A3AMAAORHAAAAAA==\n"
+ "\n"
+- "--_004_AAD42BB12C540843AB5C952ADD4D978901830A1Bswi52mbx1orfloc_--\";\n";
++ "--_004_AAD42BB12C540843AB5C952ADD4D978901830A1Bswi52mbx1orfloc_--\n";
+
+ char *multipart_message9 = "From: a\n"
+ "To: b\n"
+@@ -28440,3 +28440,79 @@ char *multipart_message_big = "From: \"Gennadiy Poryev\" <vecanoi@gmail.com>\n"
+ "/g8HAAAAAwANNP0/pQ4DAA80/T+lDvA8\n"
+ "\n"
+ "------=_NextPart_000_0000_01CDFBC6.932A1F00--\n";
++
++char *multipart_message_submessage = "Content-Type: multipart/mixed; boundary=\"===============9147350442359610775==\"\n"
++"MIME-Version: 1.0\n"
++"To: test@test.it\n"
++"From: test@test.it\n"
++"\n"
++"--===============9147350442359610775==\n"
++"Content-Type: message/rfc822\n"
++"MIME-Version: 1.0\n"
++"\n"
++"Content-Type: multipart/alternative;\n"
++" boundary=\"===============1150329730008994878==\"\n"
++"MIME-Version: 1.0\n"
++"Subject: Link\n"
++"From: my@email.com\n"
++"To: your@email.com\n"
++"\n"
++"--===============1150329730008994878==\n"
++"Content-Type: text/plain; charset=\"us-ascii\"\n"
++"MIME-Version: 1.0\n"
++"Content-Transfer-Encoding: 7bit\n"
++"\n"
++"Hi!\n"
++"How are you?\n"
++"Here is the link you wanted:\n"
++"https://www.python.org\n"
++"--===============1150329730008994878==\n"
++"Content-Type: text/html; charset=\"us-ascii\"\n"
++"MIME-Version: 1.0\n"
++"Content-Transfer-Encoding: 7bit\n"
++"\n"
++"<html>\n"
++" <head></head>\n"
++" <body>\n"
++" <p>Hi!<br>\n"
++" How are you?<br>\n"
++" Here is the <a href=\"https://www.python.org\">link</a> you wanted.\n"
++" </p>\n"
++" </body>\n"
++"</html>\n"
++"\n"
++"--===============1150329730008994878==\n"
++"Content-Type: multipart/mixed; boundary=\"===============0782181963306111896==\"\n"
++"MIME-Version: 1.0\n"
++"\n"
++"--===============0782181963306111896==\n"
++"Content-Type: text/plain; charset=\"us-ascii\"\n"
++"MIME-Version: 1.0\n"
++"Content-Transfer-Encoding: 7bit\n"
++"\n"
++"Text part3_1.\n"
++"--===============0782181963306111896==\n"
++"Content-Type: text/plain; charset=\"us-ascii\"\n"
++"MIME-Version: 1.0\n"
++"Content-Transfer-Encoding: 7bit\n"
++"\n"
++"Text part3_2.\n"
++"--===============0782181963306111896==--\n"
++"\n"
++"\n"
++"\n"
++"\n"
++"--===============1150329730008994878==--\n"
++"\n"
++"--===============9147350442359610775==\n"
++"Content-Type: text/plain; charset=\"us-ascii\"\n"
++"MIME-Version: 1.0\n"
++"Content-Transfer-Encoding: 7bit\n"
++"\n"
++"This is a simple text attachment.\n"
++"with some newlines at the end.\n"
++"\n"
++"\n"
++"\n"
++"--===============9147350442359610775==--\n"
++"\n";
+diff --git test/check_dbmail_message.c test/check_dbmail_message.c
+index e36a2ea..f070257 100644
+--- test/check_dbmail_message.c
++++ test/check_dbmail_message.c
+@@ -431,6 +431,14 @@ START_TEST(test_dbmail_message_store)
+ COMPARE(e,t);
+ g_free(e);
+ g_free(t);
++ //-----------------------------------------
++ m = message_init(multipart_message_submessage);
++ e = dbmail_message_to_string(m);
++ t = store_and_retrieve(m);
++ COMPARE(e,t);
++ COMPARE(multipart_message_submessage, t);
++ g_free(e);
++ g_free(t);
+ }
+ END_TEST
+
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0032-10083-prepend-headers-during-delivery b/mail/dbmail/files/patch-0032-10083-prepend-headers-during-delivery
new file mode 100644
index 000000000000..09426e582079
--- /dev/null
+++ b/mail/dbmail/files/patch-0032-10083-prepend-headers-during-delivery
@@ -0,0 +1,25 @@
+From d213fd84529b3ff3da8b32aa15ef58f2b335383d Mon Sep 17 00:00:00 2001
+From: Paul J Stevens <p.stevens@labdigital.nl>
+Date: Sat, 3 Dec 2016 17:33:13 +0100
+Subject: [PATCH 32/33] [#10083] prepend headers during delivery
+
+---
+ src/dm_message.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git src/dm_message.c src/dm_message.c
+index 784c091..82b0656 100644
+--- src/dm_message.c
++++ src/dm_message.c
+@@ -892,7 +892,7 @@ const char * dbmail_message_get_envelope_recipient(const DbmailMessage *self)
+
+ void dbmail_message_set_header(DbmailMessage *self, const char *header, const char *value)
+ {
+- g_mime_object_set_header(GMIME_OBJECT(self->content), header, value);
++ g_mime_object_prepend_header(GMIME_OBJECT(self->content), header, value);
+ }
+
+ const gchar * dbmail_message_get_header(const DbmailMessage *self, const char *header)
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-0099-Allow-for-systems-that-don-t-use-proc b/mail/dbmail/files/patch-0099-Allow-for-systems-that-don-t-use-proc
new file mode 100644
index 000000000000..13ae81dc7d2a
--- /dev/null
+++ b/mail/dbmail/files/patch-0099-Allow-for-systems-that-don-t-use-proc
@@ -0,0 +1,35 @@
+From 6169c48e7815f7e999e23f65c91ab7299f437b7a Mon Sep 17 00:00:00 2001
+From: Alan Hicks <ahicks@p-o.co.uk>
+Date: Sun, 15 Jan 2017 14:29:20 +0000
+Subject: [PATCH 27/27] Allow for systems that don't use proc
+
+---
+ src/dm_misc.c | 5 +++++
+ 1 file changed, 5 insertions(+)
+
+diff --git src/dm_misc.c src/dm_misc.c
+index e795de1..1599c64 100644
+--- src/dm_misc.c
++++ src/dm_misc.c
+@@ -106,6 +106,10 @@ int drop_privileges(char *newuser, char *newgroup)
+
+ int get_opened_fd_count(void)
+ {
++#if defined(__FreeBSD__) || defined(__APPLE__) || defined(__SUNPRO_C)
++ // BSD like systems don't use proc
++ return 0;
++#else
+ DIR* dir = NULL;
+ struct dirent* entry = NULL;
+ char buf[32];
+@@ -122,6 +126,7 @@ int get_opened_fd_count(void)
+ closedir(dir);
+
+ return fd_count - 2; /* exclude '.' and '..' entries */
++#endif
+ }
+
+ void create_unique_id(char *target, uint64_t message_idnr)
+--
+2.10.1 (Apple Git-78)
+
diff --git a/mail/dbmail/files/patch-acinclude.m4 b/mail/dbmail/files/patch-acinclude.m4
index fcec2a2bccca..1c95e299b6d5 100644
--- a/mail/dbmail/files/patch-acinclude.m4
+++ b/mail/dbmail/files/patch-acinclude.m4
@@ -1,6 +1,6 @@
---- acinclude.m4.orig
+--- acinclude.m4.orig 2015-01-25 10:20:50 UTC
+++ acinclude.m4
-@@ -236,7 +236,7 @@ if ( test [ "x$lookforldap" != "xno" ] || test [ "x$lookforauthldap" != "xno" ]
+@@ -221,7 +221,7 @@ if ( test [ "x$lookforldap" != "xno" ] |
dnl See if we already have the paths we need in the environment.
dnl ...but only if --with-ldap was given without a specific path.
if ( test [ "x$lookforldap" = "xyes" ] || test [ "x$lookforauthldap" = "xyes" ] ); then
@@ -9,7 +9,7 @@
if test [ "x$LDAPLIB" != "xfailed" ]; then
break
fi
-@@ -248,7 +248,7 @@ if ( test [ "x$lookforldap" != "xno" ] || test [ "x$lookforauthldap" != "xno" ]
+@@ -233,7 +233,7 @@ if ( test [ "x$lookforldap" != "xno" ] |
SAVE_CFLAGS=$CFLAGS
dnl The headers might be in a funny place, so we need to use -Ipath
CFLAGS="$CFLAGS -L$TEST_PATH $LDAPINC"
@@ -18,7 +18,7 @@
CFLAGS=$SAVE_CFLAGS
if test [ "x$LDAPLIB" != "xfailed" ]; then
break 2
-@@ -262,7 +262,7 @@ if ( test [ "x$lookforldap" != "xno" ] || test [ "x$lookforauthldap" != "xno" ]
+@@ -247,7 +247,7 @@ if ( test [ "x$lookforldap" != "xno" ] |
AC_MSG_ERROR([Could not find LDAP library.])
else
AC_DEFINE([AUTHLDAP], 1, [Define if LDAP will be used.])
diff --git a/mail/dbmail/files/patch-src_clientbase.c b/mail/dbmail/files/patch-src_clientbase.c
index 5bf60d5166b4..7363b1a736f0 100644
--- a/mail/dbmail/files/patch-src_clientbase.c
+++ b/mail/dbmail/files/patch-src_clientbase.c
@@ -1,6 +1,6 @@
---- src/clientbase.c.orig 2014-08-23 14:01:38.000000000 +0100
-+++ src/clientbase.c 2014-08-24 10:12:06.000000000 +0100
-@@ -151,7 +151,7 @@
+--- src/clientbase.c.orig 2015-01-25 10:20:50 UTC
++++ src/clientbase.c
+@@ -151,7 +151,7 @@ ClientBase_T * client_init(client_sock *
client->tx = STDOUT_FILENO;
} else {
/* server-side */
@@ -9,7 +9,7 @@
NI_MAXHOST, client->dst_port,
NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV))) {
TRACE(TRACE_INFO, "getnameinfo::error [%s]", gai_strerror(serr));
-@@ -159,7 +159,7 @@
+@@ -159,7 +159,7 @@ ClientBase_T * client_init(client_sock *
/* client-side */
if (server_conf->resolveIP) {
@@ -18,7 +18,7 @@
NI_MAXHOST-1, NULL, 0, NI_NAMEREQD))) {
TRACE(TRACE_INFO, "getnameinfo:error [%s]", gai_strerror(serr));
}
-@@ -170,7 +170,7 @@
+@@ -170,7 +170,7 @@ ClientBase_T * client_init(client_sock *
client->clientname[0] ? client->clientname : "Lookup failed");
} else {
diff --git a/mail/dbmail/files/patch-src_dbmail.h.in b/mail/dbmail/files/patch-src_dbmail.h.in
index 084ae434334d..875a8062f011 100644
--- a/mail/dbmail/files/patch-src_dbmail.h.in
+++ b/mail/dbmail/files/patch-src_dbmail.h.in
@@ -1,6 +1,6 @@
---- src/dbmail.h.in.orig
+--- src/dbmail.h.in.orig 2015-01-25 10:20:50 UTC
+++ src/dbmail.h.in
-@@ -154,6 +160,11 @@
+@@ -155,6 +155,11 @@
#include <endian.h>
#endif
@@ -10,5 +10,5 @@
+
+
#define GETCONFIGVALUE(key, sect, var) \
- config_get_value(key, sect, var); \
- if (strlen(var) > 0) \
+ config_get_value(key, sect, var); \
+ if (strlen(var) > 0) \