Arnt Gulbrandsen
About meAbout this blog

Three programs, one feature

It's not something one does often, but I've implemented the same feature in three different programs. Not very different, all are written in the same programming language for the same platform, and all are servers.

Same platform, same language, same task, same developer... you would think the three patches would end up looking similar? They did not, not at all.

The feature I wrote is is support for using UTF8 on SMTP, which I've implemented for Postfix, Sendmail and Qmail, which all run on linux/posix systems. I tried to follow the code style for each of them, and surprised myself at how different my code looked.

One patch is well-engineered, prim and proper.

The next is for an amorphous blob of software. The patch is itself amorphous, and makes functions even longer that were too long already. Yet it's half as long as the first patch. The two are, in my own judgment, about equally readable. One wins on length, the other on readability, they're roughly tied overall. This surprised me not a little.

The third is a short, readable patch which one might call an inspired hack. It's a much smaller than the others and easily wins on readability too.

It wasn't supposed to be like that, was it? Good engineering shouldn't give the most verbose patch, and the hack shouldn't be the most lucid of the three.

I see two things here:

First: Proper engineering has its value, but perhaps not as much as common wisdom says. Moderately clean code offers almost all of the value of really clean code.

Second: A small program is easy to work with, such as the MVPs that are so fashionable these days. But ease of modification isn't all, the smallest among the three servers has fallen out of use because the world changed and it stopped being viable.

Some random verbiage on each of the three servers and patches:

Qmail

Qmail, the last MTA I patched, is an inspired hack, and so's my patch. I expect Qmail works wondrously provided you use it as Dan Bernstein intended (ie. in the world of 1995, to solve the problems of 1995). Since the SMTPUTF8 extension is well-designed and Qmail doesn't implement DSN, my patch ends up being only a few tens of lines:

Qmail's SMTP server advertises advertises a new extension, accepts it, and uses the right keyword in the Received field.

When Qmail's SMTP client is told to send mail, it checks for that Received keyword as well as for UTF8 addresses, and adds the right protocol keyword if necessary.

That's all. Qmail's simplicity makes it that simple. One function implements a Qmail-like state machine to avoid keeping state in the queue file and looks different from my typical code, the rest is overwhelmingly simple.

This same simplicity has its drawbacks and I wouldn't recommend using Qmail for receiving mail on today's internet. I understand some people are using it for outgoing mail at online shops, though.

This patch is in Gentoo, you can read it there or install qmail-1.06-r5. I also have a version that doesn't depend on the TLS patch, send me mail if you'd like a copy of that.

Sendmail

Sendmail is the oldest MTA on the planet, and shows its age. While working with the source I saw that the maintainers mean well and have tried to update the program to comply with good new practice, but they're not doing very much of that. I think refactoring and dropping features is unusually difficult in their case. Maybe they dare not remove code that may still be in use, and their user base includes the oldest and most diverse sites. If there are 20 year old mail servers anywhere, they probably run sendmail.

Sendmail suffers from some overlong functions. Over the decades those functions have grown, and grown, and grown, and today they're so big that they effectively block dependency injection. That, in turn, blocks unit testing. My patch adds zero unit tests and my automatic system tests aren't portable to the maintainers' systems. Sad.

The Sendmail patch is much larger than the Qmail one, partly because sendmail's more orderly and partly because it's less orderly.

Sendmail stores information in queue files in an orderly, extensible way, unlike Qmail. Because of that, my patch can store its extra bit sensibly, which adds code to define, read, write and process the extra bit. Qmail didn't allow any of that, which was limiting, but oh how simple!

Sendmail's overlong functions led to less orderly changes than Qmail. I wanted to write code as tidy as that in Qmail, but it just wouldn't fit.

This patch is in the Sendmail maintainers' queue; send me mail if you'd like to have a copy.

Postfix

The first MTA I patched was Postfix, almost three years ago now. That patch is even bigger than the one for Sendmail.

Postfix is similar to Sendmail in some ways, but more well-ordered, better documented and without all he dinosaur features. Postfix does not contain any overlong functions. (Maybe you think some functions are too long or have other problems, but the maintainers like Postfix as it is. I keep my opinion to myself — a good patch follows the maintainer's desired style and that's all I have to say.)

Postfix contains all the functionality you need on the network today, and the maintainers (chiefly Wietse Venema) are amazingly good at keeping the features complete and orthogonal. If you combine any two Postfix settings, the combination works as intended because Wietse has thought about that combination.

The price for that is that there are a lot of cases to handle. Qmail autodetects ASCII/UTF8 when you send mail via its command-line interface, Sendmail does the same, Postfix has has more complex logic and supports four cases instead of two.

This patch was integrated into Postfix 3.0.0, and is in all the major Linux distributions by now.