Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!

Bug 513248

Summary: net-mail/dbmail-3.1.13 - LMTP daemon fails to connect to PostgreSQL database - dbmail/lmtpd[10204]: Error:[db] db_query(+370): SQLException: ERROR: insert or update on table "dbmail_messages" violates foreign key constraint "dbmail_messages_phys
Product: Gentoo Linux Reporter: David W Noon <david.w.noon>
Component: [OLD] ServerAssignee: Thomas Raschbacher <lordvan>
Status: RESOLVED TEST-REQUEST    
Severity: normal CC: net-mail+disabled
Priority: Normal Keywords: PATCH
Version: unspecified   
Hardware: All   
OS: Linux   
Whiteboard:
Package list:
Runtime testing required: ---
Attachments: emerge --info
ebuild log
Patch to use a reliable connection algorithm
Syslog entries showing dbmail start-up problems.
Patch for reliable retrieval of physical message id
Patch to defer database connection until after root privileges have been dropped

Description David W Noon 2014-06-14 20:59:06 UTC
The start-up code for the LMTP daemon does not connect reliably to a PostgreSQL database back-end. It uses an old kluge that is no longer valid on newer builds of PostgreSQL.

This gives the following problem:

Jun 14 18:53:21 karnak dbmail/lmtpd[10204]: Error:[db] db_query(+370): SQLException: ERROR:  insert or update on table "dbmail_messages" violates foreign key constraint "dbmail_messages_phys

Jun 14 18:53:21 karnak dbmail/lmtpd[10204]: Error:[db] db_query(+371): failed query [INSERT INTO dbmail_messages(mailbox_idnr, physmessage_id, unique_id,recent_flag, status) VALUES (2, 20831

emerge --info and build log to follow.
Comment 1 David W Noon 2014-06-14 21:00:14 UTC
Created attachment 378898 [details]
emerge --info
Comment 2 David W Noon 2014-06-14 21:04:01 UTC
Created attachment 378900 [details]
ebuild log

Compressed to avoid size limtations.
Comment 3 David W Noon 2014-06-14 21:08:09 UTC
Created attachment 378904 [details, diff]
Patch to use a reliable connection algorithm

This patch has been tested here and is working with PostgreSQL. I have not tested it with MySQL/MariaDB or SQLite.
Comment 4 Thomas Raschbacher gentoo-dev 2014-06-17 08:41:07 UTC
Hmm I have no such problems with postgres (9.2.3-r1) here.

What version are you using?

Also it would be good if you sent this patch upstream instead of just here. Paul is usually fairly fast with things like this.

Also did you check if 3.3.13 still does this?
Comment 5 David W Noon 2014-06-17 11:16:14 UTC
(In reply to Thomas Raschbacher from comment #4)
> Hmm I have no such problems with postgres (9.2.3-r1) here.
> 
> What version are you using?

I am using PostgreSQL 9.3.3 and dbmail 3.1.13.

I also did not have this problem with earlier builds of PostgreSQL, but that was also without libzdb inside dbmail.

> Also it would be good if you sent this patch upstream instead of just here.
> Paul is usually fairly fast with things like this.

I don't have a bugzilla account with dbmail, but I can create one.

> Also did you check if 3.3.13 still does this?

3.3.13 of what?  The highest version of dbmail in Portage is 3.1.15.

Note that I originally created this patch for dbmail 3.1.2.  It has been a problem ever since dbmail has been using libzdb for database connectivity.  I stated in the original report that the login sequence [using libzdb] uses an old kluge that is no longer valid with newer PostgreSQL.  This is the hub of the problem: the code has always been wrong, but older PostgreSQL allowed us to get away with it.
Comment 6 Thomas Raschbacher gentoo-dev 2014-06-24 12:15:33 UTC
sorry that was a typo i meant 3.1.15 of course.

Did you make a bugreport on the dbmail bugtracker yet? if not I can do this if you want and link it here
Comment 7 Thomas Raschbacher gentoo-dev 2014-06-25 12:00:25 UTC
I put in a bugreport upstream for you:

http://www.dbmail.org/mantis/view.php?id=1050
Comment 8 David W Noon 2014-06-25 19:01:25 UTC
(In reply to Thomas Raschbacher from comment #7)
> I put in a bugreport upstream for you:
> 
> http://www.dbmail.org/mantis/view.php?id=1050

Thanks for that.  I have been a bit ill the last few days and haven't had a chance to report it.

I primarily want to be certain that there is nothing weird about my PostgreSQL installation.  I am still using the same USE flags as I was when running 9.2.x and earlier, so I am confident that the issue is in dbmail/libzdb.  The tracing I did to develop the patch also indicated it was dbmail, so hopefully we will get an "official" fix soon.
Comment 9 Thomas Raschbacher gentoo-dev 2014-12-30 16:23:30 UTC
I know there was no activity upstream, but are you still having this problem with recent versions of dbmail (and libzdb) ?
Comment 10 David W Noon 2014-12-30 17:09:30 UTC
(In reply to Thomas Raschbacher from comment #9)
> I know there was no activity upstream, but are you still having this problem
> with recent versions of dbmail (and libzdb) ?

Yes.

There has not been any new releases of dbMail since I reported this problem, and the existing release still needs my patch/kluge to start properly.
Comment 11 Thomas Raschbacher gentoo-dev 2014-12-31 13:39:12 UTC
actually there are new releases -- current one is 3.2.2 but it is not marked stable yet as I only added it yesterday - although it might get to stable faster than usual as it is a security fix.

could you test with ~arch libzdb and dbmail if possible?
Comment 12 David W Noon 2014-12-31 19:05:31 UTC
(In reply to Thomas Raschbacher from comment #11)
> actually there are new releases -- current one is 3.2.2 but it is not marked
> stable yet as I only added it yesterday - although it might get to stable
> faster than usual as it is a security fix.
> 
> could you test with ~arch libzdb and dbmail if possible?

Okay, I updated to libzdb 3.0 and dbmail 3.2.2 and the result was an unmitigated disaster.  The only saving grace was that the new version was not able to corrupt the database.

We should note that there was no upgrade script to go from 3.1.x to 3.2.y of dbmail.  The only upgrade recommendation was to run dbmail-util -by.  I did this.

I will attach some lines from my syslog that will show the problems.
Comment 13 David W Noon 2014-12-31 19:07:02 UTC
Created attachment 392812 [details]
Syslog entries showing dbmail start-up problems.
Comment 14 David W Noon 2015-01-29 20:52:57 UTC
(In reply to Thomas Raschbacher from comment #11)
> actually there are new releases -- current one is 3.2.2 but it is not marked
> stable yet as I only added it yesterday - although it might get to stable
> faster than usual as it is a security fix.
> 
> could you test with ~arch libzdb and dbmail if possible?

This new release has more bugs than a cheap hotel room.

It appears to be a major rewrite.  The old problem is still there in the new code and I have reworked the patch (above) for the new code base.

There is a small security issue with the server daemons connecting to PostgreSQL while still holding root permissions.  Most PostgreSQL DBAs prohibit root from connecting to the database cluster.

I have written patches to resolve these problems and I shall upload them when I am satisfied they are rock solid.

Worst of all is the socket handling.  The socket routines are hard-coded to use IPv4 data structures.  This means that an IPv6 connection is handled by creating a control block containing IPv4 versions of sock_addr, etc., which are too small to contain the IPv6 data.  The upshot is extensive heap corruption causing random crashes and substantial database corruption (typically dropped messages).  This bug it too extensive for me to produce simple patches; it will need to be fixed upstream.  For now, I have removed the IPv6 addresses from the "bindip" parameters in the dbmail.conf file.

I think the "resolved" status of this bug is now invalid, as the original bug is still extant in the unpatched code.

Patches to arrive in the next day or two.
Comment 15 David W Noon 2015-02-02 19:06:51 UTC
Created attachment 395404 [details, diff]
Patch for reliable retrieval of physical message id

Algorithm varies with the DBMS for backing store. This patch is simpler than its predecessor.
Comment 16 David W Noon 2015-02-02 19:08:35 UTC
Created attachment 395406 [details, diff]
Patch to defer database connection until after root privileges have been dropped
Comment 17 David W Noon 2015-02-02 19:09:56 UTC
The 2 patches I just added are for dbmail 3.2.2, not for earlier releases.