History log of /openbsd-current/usr.sbin/smtpd/dns.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.92 16-Nov-2023 op

consider an MX of "localhost" as it were a "Null MX"

diff from Philipp (philipp+openbsd [at] bureaucracy [dot] de), thanks!

ok sthen@


# 1.91 08-Nov-2023 op

RFC 7505 ("Null MX") handling

mail delivery will not be attempted if a domain advertises a single MX
record with preference 0 and a zero-length label.

based on an initial diff from Philipp (philipp+openbsd [at] bureaucracy
[dot] de), thanks!

ok jung@


Revision tags: OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.90 14-Jun-2021 eric

add required headers for smtpd.h and remove unnecessary ones in other files.

ok jung@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.89 18-Sep-2019 eric

Implement server certificate validation in smtp(1).
Check certificate against MX name in smtpd(8) mta.

ok gilles@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.88 26-Sep-2018 eric

treat NOTIMP as NO_DATA in response to MX query: fallback to hostname lookup
instead of bouncing the mail.

ok gilles@


# 1.87 25-Jul-2018 eric

Implement a generic interface to forward resolver queries to the lka
process. Use it for the reverse lookups required by smtp and mta.

Until now, DNS-related lookups were implemented using ad-hoc IMSGs
between the lka and other processes. It turns out to be confusing and
difficult to maintain/extend. So we want to replace this with a better
set of IMSGs matching the standard resolver interface.

ok gilles@


# 1.86 31-May-2018 gilles

remove 'where' parameter from all x*() functions in utils.c, it doesn't
really help us with anything, propagate the change in codebase

ok millert@


Revision tags: OPENBSD_6_3_BASE
# 1.85 06-Jan-2018 sunil

Move unpack functions into a seperate file.

Required for upcoming 'smtpctl spf walk'.
Ok eric@ gilles@ millert@


Revision tags: OPENBSD_6_2_BASE
# 1.84 31-May-2017 deraadt

typo; from Edgar Pettijohn


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.83 28-Oct-2015 gilles

dns_lookup_host() needs to remove brackets and IPv6: prefix when receiving
a text representation otherwise getaddrinfo_async() will choke

ok eric@


# 1.82 17-Oct-2015 gilles

remove unused variables


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 20-Jan-2015 deraadt

use <limits.h> comprehensively. For now try to push <> includes to
each .c file, and out of the .h files. To avoid overinclude.
ok gilles, in principle. If this has been done right, -portable should
become easier to maintain.


# 1.80 06-Jan-2015 gilles

fix whitespace and indentation, by Kyle Milz


# 1.79 08-Oct-2014 eric

restrict address lookups to configured address families.

ok gilles@


Revision tags: OPENBSD_5_6_BASE
# 1.78 19-Apr-2014 gilles

(void) cast this strlcpy(), it cannot truncate


# 1.77 19-Apr-2014 gilles

these strlcpy can't truncate, the copy is from a buffer to a buffer of same
size and the first buffer handles the truncation already


# 1.76 04-Apr-2014 eric

Merge the mda, mta and smtp processes into a single unprivileged
process managing message reception, delivery and transfer. Mostly
mechanical, but very intrusive as it required to rewamp all IMSG to
fix ambiguities.

with and ok gilles@


# 1.75 03-Apr-2014 eric

use asr helpers after libevent update.


# 1.74 26-Mar-2014 eric

asr API is now public


# 1.73 25-Mar-2014 eric

update after asr API update


# 1.72 25-Mar-2014 eric

Integrate necessary dns packet parsing helpers from asr.
They are not supposed to be exposed.

ok gilles@


# 1.71 14-Mar-2014 eric

field rename


Revision tags: OPENBSD_5_5_BASE
# 1.70 26-Dec-2013 eric

bcopy -> memmove
bzero -> memset


# 1.69 18-Nov-2013 eric

When looking up a MX, parse the address if the domain is a "[ipaddr]" string.


# 1.68 26-Oct-2013 eric

%i -> %d in format strings


Revision tags: OPENBSD_5_4_BASE
# 1.67 12-Jul-2013 eric

update after asr changes.


# 1.66 24-May-2013 eric

sync with OpenSMTPD 5.3.2

ok gilles@


# 1.65 30-Apr-2013 eric

remove params after API change.


Revision tags: OPENBSD_5_3_BASE
# 1.64 26-Jan-2013 gilles

Sync with our smtpd repo:

* first bricks of ldap and sqlite support (not finished but both working)
* new table API to replace map API, all lookups are done through tables
* improved handling of temporary errors throughout the daemon
* improved scheduler and mta logic: connection reuse, optimizes batches
* improved queue: more tolerant to admin errors, new layout, less disk-IO
* improved memory usage under high load
* SSL certs/keys isolated to lookup process to avoid facing network
* VIRTUAL support improved, fully virtual setups possible now
* runtime tracing of processes through smtpctl trace
* ssl_privsep.c sync-ed with relayd
* ssl.c no longer contains smtpd specific interfaces
* smtpd-specific ssl bits moved to ssl_smtpd.c
* update mail address in copyright

FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE.

smtpd.conf(5) simplified, it will require adaptations

ok eric@


# 1.63 24-Nov-2012 eric

fix after asr update


# 1.62 23-Nov-2012 eric

knf

ok gilles@


# 1.61 12-Nov-2012 eric

Cleanups and improvements:

* Log more events (especially client session) and use a better scheme
for that: each messages is prefixed with a token to easily identify
its class:
- info/warn/debug: general server messages
- smtp-in: smtp client connections
- relay: status update for relayed messages
- delivery: status update for local deliveries

* Implement "smtpctl monitor" to display updates of selected internal
counters.

* When reloading the on-disk queue at startup do not commit a message
if no envelope was submitted for that message.

* Remove unused stuff in the config parser.

ok gilles@


# 1.60 08-Oct-2012 eric

skip RR if type is not MX. Use hostname if the list of MX is empty
after the loop.

spotted by huku at grhack.net

ok gilles@


# 1.59 03-Oct-2012 gilles

when requesting MX entries, the result can be appear in random orders.

the logic for inserting them in a lka session when acting as backup MX did
not take account for one specific case that could lead to an early exit
without smtpd getting a chance to detect the entry corrsponds to itself.

in such case, a backup MX woud try to connect to itself and bounce in the
loop detection code ... or it would sometimes work.


# 1.58 27-Sep-2012 chl

use xmalloc()/xcalloc() helpers

while there unify usage of log_trace() in ramstat_set()

ok gilles@ eric@


# 1.57 25-Aug-2012 gilles

- introduce struct stat_value
- statistics can now have a type (counter, timestamp, timeval, timespec and
possibly others in the future)
- stat_increment() / stat_decrement() now take an increment/decrement value
and are at the moment only of type counter
- stat_set() now takes a stat_value
- provide helpers to convert raw values to stat_value

ok eric@, ok chl@

while at it fix a rq_queue_dump() call using a bogus timestamp in scheduler
ramqueue.


# 1.56 21-Aug-2012 eric

Allow smtpd to work as a backup MX, relaying only to MXs with higher
priority in the DNS record. For example:

accept for domain "foo.org" relay backup "mx3.foo.org"

will relay mails for "foo.org" using only hosts with higher priority
(i.e. lower value) than "mx3.foo.org", which is supposed to be the
current server.

If the specified backup MX is not found in the DNS record, relaying
works as normal.

ok gilles@


# 1.55 21-Aug-2012 eric

Use TAILQ rather than array for mx list.

ok gilles@


# 1.54 21-Aug-2012 eric

dns sessions don't use lookup. no need to store them in a tree.

ok gilles@


# 1.53 19-Aug-2012 chl

coding style: replace all occurences of u_int* with uint*

ok eric@


# 1.52 18-Aug-2012 gilles

- introduce stat_backend, an API for pluggable statistic backends
> statistics are no longer static structures in shared memory
> statistics are only set, smtpd never uses them in its logic
> each statistic is a key/value where key can be any (dynamic) string
- convert all uses of the former API to use the new one
- implement stat_ramstat that keeps non-persistent stats in ram structure

ok eric@, ok chl@


# 1.51 08-Aug-2012 eric

cleanup some old debug traces

ok gilles@ chl@


# 1.50 29-Jul-2012 eric

fix bogus permfail when no MX is defined on a valid domain.

ok gilles@


Revision tags: OPENBSD_5_2_BASE
# 1.49 09-Jul-2012 eric

move to the new resolver implementation, with temporary glue to use
the relevant files from asr directly.

ok gilles@


# 1.48 14-Apr-2012 eric

improve readability

ok gilles@


Revision tags: OPENBSD_5_1_BASE
# 1.47 11-Jan-2012 eric

Try to parse hostnames as IP addresses before resolving. This allows
relays to be given as IP address in the config file.

ok gilles@


# 1.46 11-Jan-2012 eric

Improve error reporting. Most errors during hostname lookup are
now correctly reported as temporary failures.

from Nathanael Rensen, tweaks by me.


# 1.45 01-Sep-2011 eric

Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way.

ok gilles@ chl@


Revision tags: OPENBSD_5_0_BASE
# 1.44 20-Jul-2011 eric

Fix reporting of permanent/temporary failures for MX lookups.
Simplify code a bit while there.

ok gilles@


# 1.43 03-Jul-2011 nicm

imsg.h requires sys/queue.h and sys/uio.h.

ok eric


# 1.42 06-May-2011 eric

move dns session specific structs and prototypes out of smtpd.h.

ok gilles@


# 1.41 01-May-2011 eric

the smtpd env is meant to be global, so do not pass it all around.

discussed with and ok gilles@


# 1.40 17-Apr-2011 gilles

cleanups, cosmethic changes, functions that should be static are now static
no functionnal change


# 1.39 02-Apr-2011 eric

add stat counters for the lookup agent

ok gilles@


# 1.38 31-Mar-2011 eric

cleanup and simplification following the asr update.

- use a specific dispatch function for each type of query
- make the host handler work on a list of hosts by default (single host
queries are just a particular case) and use that to resolve the MX list
- various other code cleanup
- remove unused headers
- remove orphaned prototypes
- update copyright

ok gilles@


# 1.37 29-Mar-2011 eric

remove unused code now that reverse lookups are done through asr.

ok gilles@


# 1.36 29-Mar-2011 eric

make use the cname query interface from asr for reverse lookups

ok gilles@


# 1.35 27-Mar-2011 eric

add a function to factorize resetting of dnssession events.

ok gilles@


# 1.34 26-Mar-2011 eric

Fix the MX lookup process:

- the MX records were not always properly inserted into the sorted
array, which led to some MX being silently dropped.
- if an MX address could not be resolved, mail delivery would fail,
even though other valid MX exist for that domain. Now only report
the failure if no server address can be found at all.

grrrreeat gilles@


# 1.33 26-Mar-2011 eric

Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.

ok gilles@


# 1.32 26-Mar-2011 eric

use an index for iterating into the mx list.

ok gilles@


# 1.31 23-Mar-2011 eric

Simplify resolver initialization. This is done only once
since resolv.conf reloading is handled automatically by asr.

ok gilles@


# 1.30 09-Mar-2011 todd

make similar code .. more similar
ok gilles@

previous commit should have read:

when copying 'struct sockaddr' data, use sa_len not sizeof(struct sockaddr_in)
this fixes truncation of IPv6 addresses in the mail delivery path
ok gilles@


# 1.29 09-Mar-2011 todd

*** empty log message ***


Revision tags: OPENBSD_4_9_BASE
# 1.28 19-Dec-2010 gilles

If MX lookup fails, fallback to using the host itself. This has always been
the behavior but I introduced a regression when switching to ASR.

bug reported by jmc@, bugfix tested by jmc@ and I


# 1.27 12-Dec-2010 jsg

use memcpy instead of a cast/deref dance that was reading past
the end of the buffer.

tested by and ok gilles@


# 1.26 29-Nov-2010 gilles

replace the fork-based-non-blocking-resolver-hack by shiny async resolver
written by eric@. it is still experimental but still better than what we
had earlier so ... we'll improve in tree :)

diff by me with *lots* of help from eric@, tested by todd and I (and a
few people out there)


# 1.25 28-Nov-2010 gilles

remove unused headers


# 1.24 28-Nov-2010 gilles

a bit of .h cleanups, no functionnal change


# 1.23 08-Sep-2010 gilles

we do dns resolutions in a separate process because we don't have an async
resolver. if we run scarce on resources and we cannot fork a separate dns
process or we cannot socketpair() tell the caller that we have a temporary
failure rather than issueing a fatal(). message will stay in queue and be
rescheduled later ...

bug reported and bugfix tested by Sacha El Masry <lists@devilray.eu>


Revision tags: OPENBSD_4_8_BASE
# 1.22 29-Jun-2010 deraadt

force the dns buffers to be aligned using a union, until the retarded
"misalign strings on the stack" bug in gcc4 is fixed (even when that
is fixed this idiom is safer and quite common)
ok jacekm


# 1.21 02-Jun-2010 chl

check event_dispatch() return value

ok jacekm@


Revision tags: OPENBSD_4_7_BASE
# 1.20 14-Nov-2009 chl

add missing header needed by signal()

ok gilles@


# 1.19 11-Nov-2009 jacekm

Fix previous. When configured to relay via IP address, MX lookup would fail
(NXDOMAIN), leading to a bounce. Precede the MX lookup with an attempt to
parse the relay as numeric string.

"reads ok" gilles@


# 1.18 05-Nov-2009 jsing

Consider DNS lookups that result in NXDOMAIN to be a permanent failure.

ok gilles@ jacekm@


# 1.17 05-Nov-2009 jsing

Introduce a 6yz status code, used internally to report permanent errors.
The 1yz and 6yz status codes are now removed prior to reporting the status
message in bounce messages, which provides an easy way to distinguish
between local and remote status messages. Initial diff from jacekm@

ok gilles@ jacekm@


# 1.16 03-Sep-2009 jacekm

imsg_get sets errno so use fatal instead of fatalx.


# 1.15 08-Aug-2009 gilles

import some changes from portable smtpd to reduce the delta between both.
this commit contains mostly missing casts and cosmethic changes, do not
expect to build this anywhere but on OpenBSD, it does not contain any of
the portable glue.


Revision tags: OPENBSD_4_6_BASE
# 1.14 06-Jun-2009 pyr

Sync with relayd:
Stop pushing event handling in the imsg framework.
Instead, provide a small glue layer on top of both imsg and libevent.
This finally clearly separates event handling and imsg construction.

Sidetrack bonus: remove the mega-ugly hack of having a dummy imsg_event_add
stub in smtpctl.
ok jaceckm@


# 1.13 05-Jun-2009 pyr

make smtpd's imsg lib ready, just like relayd and ospfd.
ok gilles@, jacekm@


# 1.12 01-Jun-2009 jacekm

Fix EV_READ/EV_WRITE testing inside IMSG handlers. Based on similar change
to the routing daemons by claudio@; ok gilles@


# 1.11 09-May-2009 jacekm

- New API to handle all DNS query types (A, MX, PTR) asynchronously.

- Improve RFC compliance: CNAMEs are resolved, equal preference MXs
are randomized, relaying via MX that has equal/lower preference
than local server is prevented, decision on when to treat domain
name as implicit MX is better.

ok gilles@


Revision tags: OPENBSD_4_5_BASE
# 1.10 22-Feb-2009 form

replace MAX* constants by sizeof where possible

ok jacekm@


# 1.9 15-Feb-2009 jacekm

If MX query fails due to DNS error, do not attempt more queries; ok gilles@


# 1.8 15-Feb-2009 jacekm

Fix a bug where list of 6 MXs or more was not handled correctly.
Pointed out by & ok sthen@, ok gilles@.


# 1.7 01-Jan-2009 jacekm

remove unnecessary includes; ok gilles@


# 1.6 05-Dec-2008 gilles

- more err/errx -> fatal/fatalx, warn/warnx -> log_warn/log_warnx
contains bits based on an old diff from Jacek Masiulaniec and
other bits from me.


# 1.5 01-Dec-2008 gilles

- in mxsort, fix type of loop counter, it will never be < 0 if it is
unsigned and when running out of luck it will cause the lookup
process to crash.


# 1.4 25-Nov-2008 gilles

- move prototype to smtpd.h


# 1.3 10-Nov-2008 tedu

insertion sort is faster than bubble sort. ok gilles


# 1.2 05-Nov-2008 sobrado

add a few missing id tags; there are a bunch of files, and developers
will probably miss this change when working on more important matters,
so it is probably better to sort them now. there is a risk of losing
the tags if a change needs to be reverted too.

written with excellent advice from jmc@

ok gilles@


# 1.1 01-Nov-2008 gilles

smtpd is a smtp server implementation for OpenBSD. It is a work in progress
which still lacks many features. bringing it in tree will help working on it
more easily.

"at this stage it should go in" henning@, "move ahead" deraadt@


# 1.91 08-Nov-2023 op

RFC 7505 ("Null MX") handling

mail delivery will not be attempted if a domain advertises a single MX
record with preference 0 and a zero-length label.

based on an initial diff from Philipp (philipp+openbsd [at] bureaucracy
[dot] de), thanks!

ok jung@


Revision tags: OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.90 14-Jun-2021 eric

add required headers for smtpd.h and remove unnecessary ones in other files.

ok jung@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.89 18-Sep-2019 eric

Implement server certificate validation in smtp(1).
Check certificate against MX name in smtpd(8) mta.

ok gilles@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.88 26-Sep-2018 eric

treat NOTIMP as NO_DATA in response to MX query: fallback to hostname lookup
instead of bouncing the mail.

ok gilles@


# 1.87 25-Jul-2018 eric

Implement a generic interface to forward resolver queries to the lka
process. Use it for the reverse lookups required by smtp and mta.

Until now, DNS-related lookups were implemented using ad-hoc IMSGs
between the lka and other processes. It turns out to be confusing and
difficult to maintain/extend. So we want to replace this with a better
set of IMSGs matching the standard resolver interface.

ok gilles@


# 1.86 31-May-2018 gilles

remove 'where' parameter from all x*() functions in utils.c, it doesn't
really help us with anything, propagate the change in codebase

ok millert@


Revision tags: OPENBSD_6_3_BASE
# 1.85 06-Jan-2018 sunil

Move unpack functions into a seperate file.

Required for upcoming 'smtpctl spf walk'.
Ok eric@ gilles@ millert@


Revision tags: OPENBSD_6_2_BASE
# 1.84 31-May-2017 deraadt

typo; from Edgar Pettijohn


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.83 28-Oct-2015 gilles

dns_lookup_host() needs to remove brackets and IPv6: prefix when receiving
a text representation otherwise getaddrinfo_async() will choke

ok eric@


# 1.82 17-Oct-2015 gilles

remove unused variables


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 20-Jan-2015 deraadt

use <limits.h> comprehensively. For now try to push <> includes to
each .c file, and out of the .h files. To avoid overinclude.
ok gilles, in principle. If this has been done right, -portable should
become easier to maintain.


# 1.80 06-Jan-2015 gilles

fix whitespace and indentation, by Kyle Milz


# 1.79 08-Oct-2014 eric

restrict address lookups to configured address families.

ok gilles@


Revision tags: OPENBSD_5_6_BASE
# 1.78 19-Apr-2014 gilles

(void) cast this strlcpy(), it cannot truncate


# 1.77 19-Apr-2014 gilles

these strlcpy can't truncate, the copy is from a buffer to a buffer of same
size and the first buffer handles the truncation already


# 1.76 04-Apr-2014 eric

Merge the mda, mta and smtp processes into a single unprivileged
process managing message reception, delivery and transfer. Mostly
mechanical, but very intrusive as it required to rewamp all IMSG to
fix ambiguities.

with and ok gilles@


# 1.75 03-Apr-2014 eric

use asr helpers after libevent update.


# 1.74 26-Mar-2014 eric

asr API is now public


# 1.73 25-Mar-2014 eric

update after asr API update


# 1.72 25-Mar-2014 eric

Integrate necessary dns packet parsing helpers from asr.
They are not supposed to be exposed.

ok gilles@


# 1.71 14-Mar-2014 eric

field rename


Revision tags: OPENBSD_5_5_BASE
# 1.70 26-Dec-2013 eric

bcopy -> memmove
bzero -> memset


# 1.69 18-Nov-2013 eric

When looking up a MX, parse the address if the domain is a "[ipaddr]" string.


# 1.68 26-Oct-2013 eric

%i -> %d in format strings


Revision tags: OPENBSD_5_4_BASE
# 1.67 12-Jul-2013 eric

update after asr changes.


# 1.66 24-May-2013 eric

sync with OpenSMTPD 5.3.2

ok gilles@


# 1.65 30-Apr-2013 eric

remove params after API change.


Revision tags: OPENBSD_5_3_BASE
# 1.64 26-Jan-2013 gilles

Sync with our smtpd repo:

* first bricks of ldap and sqlite support (not finished but both working)
* new table API to replace map API, all lookups are done through tables
* improved handling of temporary errors throughout the daemon
* improved scheduler and mta logic: connection reuse, optimizes batches
* improved queue: more tolerant to admin errors, new layout, less disk-IO
* improved memory usage under high load
* SSL certs/keys isolated to lookup process to avoid facing network
* VIRTUAL support improved, fully virtual setups possible now
* runtime tracing of processes through smtpctl trace
* ssl_privsep.c sync-ed with relayd
* ssl.c no longer contains smtpd specific interfaces
* smtpd-specific ssl bits moved to ssl_smtpd.c
* update mail address in copyright

FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE.

smtpd.conf(5) simplified, it will require adaptations

ok eric@


# 1.63 24-Nov-2012 eric

fix after asr update


# 1.62 23-Nov-2012 eric

knf

ok gilles@


# 1.61 12-Nov-2012 eric

Cleanups and improvements:

* Log more events (especially client session) and use a better scheme
for that: each messages is prefixed with a token to easily identify
its class:
- info/warn/debug: general server messages
- smtp-in: smtp client connections
- relay: status update for relayed messages
- delivery: status update for local deliveries

* Implement "smtpctl monitor" to display updates of selected internal
counters.

* When reloading the on-disk queue at startup do not commit a message
if no envelope was submitted for that message.

* Remove unused stuff in the config parser.

ok gilles@


# 1.60 08-Oct-2012 eric

skip RR if type is not MX. Use hostname if the list of MX is empty
after the loop.

spotted by huku at grhack.net

ok gilles@


# 1.59 03-Oct-2012 gilles

when requesting MX entries, the result can be appear in random orders.

the logic for inserting them in a lka session when acting as backup MX did
not take account for one specific case that could lead to an early exit
without smtpd getting a chance to detect the entry corrsponds to itself.

in such case, a backup MX woud try to connect to itself and bounce in the
loop detection code ... or it would sometimes work.


# 1.58 27-Sep-2012 chl

use xmalloc()/xcalloc() helpers

while there unify usage of log_trace() in ramstat_set()

ok gilles@ eric@


# 1.57 25-Aug-2012 gilles

- introduce struct stat_value
- statistics can now have a type (counter, timestamp, timeval, timespec and
possibly others in the future)
- stat_increment() / stat_decrement() now take an increment/decrement value
and are at the moment only of type counter
- stat_set() now takes a stat_value
- provide helpers to convert raw values to stat_value

ok eric@, ok chl@

while at it fix a rq_queue_dump() call using a bogus timestamp in scheduler
ramqueue.


# 1.56 21-Aug-2012 eric

Allow smtpd to work as a backup MX, relaying only to MXs with higher
priority in the DNS record. For example:

accept for domain "foo.org" relay backup "mx3.foo.org"

will relay mails for "foo.org" using only hosts with higher priority
(i.e. lower value) than "mx3.foo.org", which is supposed to be the
current server.

If the specified backup MX is not found in the DNS record, relaying
works as normal.

ok gilles@


# 1.55 21-Aug-2012 eric

Use TAILQ rather than array for mx list.

ok gilles@


# 1.54 21-Aug-2012 eric

dns sessions don't use lookup. no need to store them in a tree.

ok gilles@


# 1.53 19-Aug-2012 chl

coding style: replace all occurences of u_int* with uint*

ok eric@


# 1.52 18-Aug-2012 gilles

- introduce stat_backend, an API for pluggable statistic backends
> statistics are no longer static structures in shared memory
> statistics are only set, smtpd never uses them in its logic
> each statistic is a key/value where key can be any (dynamic) string
- convert all uses of the former API to use the new one
- implement stat_ramstat that keeps non-persistent stats in ram structure

ok eric@, ok chl@


# 1.51 08-Aug-2012 eric

cleanup some old debug traces

ok gilles@ chl@


# 1.50 29-Jul-2012 eric

fix bogus permfail when no MX is defined on a valid domain.

ok gilles@


Revision tags: OPENBSD_5_2_BASE
# 1.49 09-Jul-2012 eric

move to the new resolver implementation, with temporary glue to use
the relevant files from asr directly.

ok gilles@


# 1.48 14-Apr-2012 eric

improve readability

ok gilles@


Revision tags: OPENBSD_5_1_BASE
# 1.47 11-Jan-2012 eric

Try to parse hostnames as IP addresses before resolving. This allows
relays to be given as IP address in the config file.

ok gilles@


# 1.46 11-Jan-2012 eric

Improve error reporting. Most errors during hostname lookup are
now correctly reported as temporary failures.

from Nathanael Rensen, tweaks by me.


# 1.45 01-Sep-2011 eric

Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way.

ok gilles@ chl@


Revision tags: OPENBSD_5_0_BASE
# 1.44 20-Jul-2011 eric

Fix reporting of permanent/temporary failures for MX lookups.
Simplify code a bit while there.

ok gilles@


# 1.43 03-Jul-2011 nicm

imsg.h requires sys/queue.h and sys/uio.h.

ok eric


# 1.42 06-May-2011 eric

move dns session specific structs and prototypes out of smtpd.h.

ok gilles@


# 1.41 01-May-2011 eric

the smtpd env is meant to be global, so do not pass it all around.

discussed with and ok gilles@


# 1.40 17-Apr-2011 gilles

cleanups, cosmethic changes, functions that should be static are now static
no functionnal change


# 1.39 02-Apr-2011 eric

add stat counters for the lookup agent

ok gilles@


# 1.38 31-Mar-2011 eric

cleanup and simplification following the asr update.

- use a specific dispatch function for each type of query
- make the host handler work on a list of hosts by default (single host
queries are just a particular case) and use that to resolve the MX list
- various other code cleanup
- remove unused headers
- remove orphaned prototypes
- update copyright

ok gilles@


# 1.37 29-Mar-2011 eric

remove unused code now that reverse lookups are done through asr.

ok gilles@


# 1.36 29-Mar-2011 eric

make use the cname query interface from asr for reverse lookups

ok gilles@


# 1.35 27-Mar-2011 eric

add a function to factorize resetting of dnssession events.

ok gilles@


# 1.34 26-Mar-2011 eric

Fix the MX lookup process:

- the MX records were not always properly inserted into the sorted
array, which led to some MX being silently dropped.
- if an MX address could not be resolved, mail delivery would fail,
even though other valid MX exist for that domain. Now only report
the failure if no server address can be found at all.

grrrreeat gilles@


# 1.33 26-Mar-2011 eric

Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.

ok gilles@


# 1.32 26-Mar-2011 eric

use an index for iterating into the mx list.

ok gilles@


# 1.31 23-Mar-2011 eric

Simplify resolver initialization. This is done only once
since resolv.conf reloading is handled automatically by asr.

ok gilles@


# 1.30 09-Mar-2011 todd

make similar code .. more similar
ok gilles@

previous commit should have read:

when copying 'struct sockaddr' data, use sa_len not sizeof(struct sockaddr_in)
this fixes truncation of IPv6 addresses in the mail delivery path
ok gilles@


# 1.29 09-Mar-2011 todd

*** empty log message ***


Revision tags: OPENBSD_4_9_BASE
# 1.28 19-Dec-2010 gilles

If MX lookup fails, fallback to using the host itself. This has always been
the behavior but I introduced a regression when switching to ASR.

bug reported by jmc@, bugfix tested by jmc@ and I


# 1.27 12-Dec-2010 jsg

use memcpy instead of a cast/deref dance that was reading past
the end of the buffer.

tested by and ok gilles@


# 1.26 29-Nov-2010 gilles

replace the fork-based-non-blocking-resolver-hack by shiny async resolver
written by eric@. it is still experimental but still better than what we
had earlier so ... we'll improve in tree :)

diff by me with *lots* of help from eric@, tested by todd and I (and a
few people out there)


# 1.25 28-Nov-2010 gilles

remove unused headers


# 1.24 28-Nov-2010 gilles

a bit of .h cleanups, no functionnal change


# 1.23 08-Sep-2010 gilles

we do dns resolutions in a separate process because we don't have an async
resolver. if we run scarce on resources and we cannot fork a separate dns
process or we cannot socketpair() tell the caller that we have a temporary
failure rather than issueing a fatal(). message will stay in queue and be
rescheduled later ...

bug reported and bugfix tested by Sacha El Masry <lists@devilray.eu>


Revision tags: OPENBSD_4_8_BASE
# 1.22 29-Jun-2010 deraadt

force the dns buffers to be aligned using a union, until the retarded
"misalign strings on the stack" bug in gcc4 is fixed (even when that
is fixed this idiom is safer and quite common)
ok jacekm


# 1.21 02-Jun-2010 chl

check event_dispatch() return value

ok jacekm@


Revision tags: OPENBSD_4_7_BASE
# 1.20 14-Nov-2009 chl

add missing header needed by signal()

ok gilles@


# 1.19 11-Nov-2009 jacekm

Fix previous. When configured to relay via IP address, MX lookup would fail
(NXDOMAIN), leading to a bounce. Precede the MX lookup with an attempt to
parse the relay as numeric string.

"reads ok" gilles@


# 1.18 05-Nov-2009 jsing

Consider DNS lookups that result in NXDOMAIN to be a permanent failure.

ok gilles@ jacekm@


# 1.17 05-Nov-2009 jsing

Introduce a 6yz status code, used internally to report permanent errors.
The 1yz and 6yz status codes are now removed prior to reporting the status
message in bounce messages, which provides an easy way to distinguish
between local and remote status messages. Initial diff from jacekm@

ok gilles@ jacekm@


# 1.16 03-Sep-2009 jacekm

imsg_get sets errno so use fatal instead of fatalx.


# 1.15 08-Aug-2009 gilles

import some changes from portable smtpd to reduce the delta between both.
this commit contains mostly missing casts and cosmethic changes, do not
expect to build this anywhere but on OpenBSD, it does not contain any of
the portable glue.


Revision tags: OPENBSD_4_6_BASE
# 1.14 06-Jun-2009 pyr

Sync with relayd:
Stop pushing event handling in the imsg framework.
Instead, provide a small glue layer on top of both imsg and libevent.
This finally clearly separates event handling and imsg construction.

Sidetrack bonus: remove the mega-ugly hack of having a dummy imsg_event_add
stub in smtpctl.
ok jaceckm@


# 1.13 05-Jun-2009 pyr

make smtpd's imsg lib ready, just like relayd and ospfd.
ok gilles@, jacekm@


# 1.12 01-Jun-2009 jacekm

Fix EV_READ/EV_WRITE testing inside IMSG handlers. Based on similar change
to the routing daemons by claudio@; ok gilles@


# 1.11 09-May-2009 jacekm

- New API to handle all DNS query types (A, MX, PTR) asynchronously.

- Improve RFC compliance: CNAMEs are resolved, equal preference MXs
are randomized, relaying via MX that has equal/lower preference
than local server is prevented, decision on when to treat domain
name as implicit MX is better.

ok gilles@


Revision tags: OPENBSD_4_5_BASE
# 1.10 22-Feb-2009 form

replace MAX* constants by sizeof where possible

ok jacekm@


# 1.9 15-Feb-2009 jacekm

If MX query fails due to DNS error, do not attempt more queries; ok gilles@


# 1.8 15-Feb-2009 jacekm

Fix a bug where list of 6 MXs or more was not handled correctly.
Pointed out by & ok sthen@, ok gilles@.


# 1.7 01-Jan-2009 jacekm

remove unnecessary includes; ok gilles@


# 1.6 05-Dec-2008 gilles

- more err/errx -> fatal/fatalx, warn/warnx -> log_warn/log_warnx
contains bits based on an old diff from Jacek Masiulaniec and
other bits from me.


# 1.5 01-Dec-2008 gilles

- in mxsort, fix type of loop counter, it will never be < 0 if it is
unsigned and when running out of luck it will cause the lookup
process to crash.


# 1.4 25-Nov-2008 gilles

- move prototype to smtpd.h


# 1.3 10-Nov-2008 tedu

insertion sort is faster than bubble sort. ok gilles


# 1.2 05-Nov-2008 sobrado

add a few missing id tags; there are a bunch of files, and developers
will probably miss this change when working on more important matters,
so it is probably better to sort them now. there is a risk of losing
the tags if a change needs to be reverted too.

written with excellent advice from jmc@

ok gilles@


# 1.1 01-Nov-2008 gilles

smtpd is a smtp server implementation for OpenBSD. It is a work in progress
which still lacks many features. bringing it in tree will help working on it
more easily.

"at this stage it should go in" henning@, "move ahead" deraadt@


# 1.90 14-Jun-2021 eric

add required headers for smtpd.h and remove unnecessary ones in other files.

ok jung@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.89 18-Sep-2019 eric

Implement server certificate validation in smtp(1).
Check certificate against MX name in smtpd(8) mta.

ok gilles@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.88 26-Sep-2018 eric

treat NOTIMP as NO_DATA in response to MX query: fallback to hostname lookup
instead of bouncing the mail.

ok gilles@


# 1.87 25-Jul-2018 eric

Implement a generic interface to forward resolver queries to the lka
process. Use it for the reverse lookups required by smtp and mta.

Until now, DNS-related lookups were implemented using ad-hoc IMSGs
between the lka and other processes. It turns out to be confusing and
difficult to maintain/extend. So we want to replace this with a better
set of IMSGs matching the standard resolver interface.

ok gilles@


# 1.86 31-May-2018 gilles

remove 'where' parameter from all x*() functions in utils.c, it doesn't
really help us with anything, propagate the change in codebase

ok millert@


Revision tags: OPENBSD_6_3_BASE
# 1.85 06-Jan-2018 sunil

Move unpack functions into a seperate file.

Required for upcoming 'smtpctl spf walk'.
Ok eric@ gilles@ millert@


Revision tags: OPENBSD_6_2_BASE
# 1.84 31-May-2017 deraadt

typo; from Edgar Pettijohn


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.83 28-Oct-2015 gilles

dns_lookup_host() needs to remove brackets and IPv6: prefix when receiving
a text representation otherwise getaddrinfo_async() will choke

ok eric@


# 1.82 17-Oct-2015 gilles

remove unused variables


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 20-Jan-2015 deraadt

use <limits.h> comprehensively. For now try to push <> includes to
each .c file, and out of the .h files. To avoid overinclude.
ok gilles, in principle. If this has been done right, -portable should
become easier to maintain.


# 1.80 06-Jan-2015 gilles

fix whitespace and indentation, by Kyle Milz


# 1.79 08-Oct-2014 eric

restrict address lookups to configured address families.

ok gilles@


Revision tags: OPENBSD_5_6_BASE
# 1.78 19-Apr-2014 gilles

(void) cast this strlcpy(), it cannot truncate


# 1.77 19-Apr-2014 gilles

these strlcpy can't truncate, the copy is from a buffer to a buffer of same
size and the first buffer handles the truncation already


# 1.76 04-Apr-2014 eric

Merge the mda, mta and smtp processes into a single unprivileged
process managing message reception, delivery and transfer. Mostly
mechanical, but very intrusive as it required to rewamp all IMSG to
fix ambiguities.

with and ok gilles@


# 1.75 03-Apr-2014 eric

use asr helpers after libevent update.


# 1.74 26-Mar-2014 eric

asr API is now public


# 1.73 25-Mar-2014 eric

update after asr API update


# 1.72 25-Mar-2014 eric

Integrate necessary dns packet parsing helpers from asr.
They are not supposed to be exposed.

ok gilles@


# 1.71 14-Mar-2014 eric

field rename


Revision tags: OPENBSD_5_5_BASE
# 1.70 26-Dec-2013 eric

bcopy -> memmove
bzero -> memset


# 1.69 18-Nov-2013 eric

When looking up a MX, parse the address if the domain is a "[ipaddr]" string.


# 1.68 26-Oct-2013 eric

%i -> %d in format strings


Revision tags: OPENBSD_5_4_BASE
# 1.67 12-Jul-2013 eric

update after asr changes.


# 1.66 24-May-2013 eric

sync with OpenSMTPD 5.3.2

ok gilles@


# 1.65 30-Apr-2013 eric

remove params after API change.


Revision tags: OPENBSD_5_3_BASE
# 1.64 26-Jan-2013 gilles

Sync with our smtpd repo:

* first bricks of ldap and sqlite support (not finished but both working)
* new table API to replace map API, all lookups are done through tables
* improved handling of temporary errors throughout the daemon
* improved scheduler and mta logic: connection reuse, optimizes batches
* improved queue: more tolerant to admin errors, new layout, less disk-IO
* improved memory usage under high load
* SSL certs/keys isolated to lookup process to avoid facing network
* VIRTUAL support improved, fully virtual setups possible now
* runtime tracing of processes through smtpctl trace
* ssl_privsep.c sync-ed with relayd
* ssl.c no longer contains smtpd specific interfaces
* smtpd-specific ssl bits moved to ssl_smtpd.c
* update mail address in copyright

FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE.

smtpd.conf(5) simplified, it will require adaptations

ok eric@


# 1.63 24-Nov-2012 eric

fix after asr update


# 1.62 23-Nov-2012 eric

knf

ok gilles@


# 1.61 12-Nov-2012 eric

Cleanups and improvements:

* Log more events (especially client session) and use a better scheme
for that: each messages is prefixed with a token to easily identify
its class:
- info/warn/debug: general server messages
- smtp-in: smtp client connections
- relay: status update for relayed messages
- delivery: status update for local deliveries

* Implement "smtpctl monitor" to display updates of selected internal
counters.

* When reloading the on-disk queue at startup do not commit a message
if no envelope was submitted for that message.

* Remove unused stuff in the config parser.

ok gilles@


# 1.60 08-Oct-2012 eric

skip RR if type is not MX. Use hostname if the list of MX is empty
after the loop.

spotted by huku at grhack.net

ok gilles@


# 1.59 03-Oct-2012 gilles

when requesting MX entries, the result can be appear in random orders.

the logic for inserting them in a lka session when acting as backup MX did
not take account for one specific case that could lead to an early exit
without smtpd getting a chance to detect the entry corrsponds to itself.

in such case, a backup MX woud try to connect to itself and bounce in the
loop detection code ... or it would sometimes work.


# 1.58 27-Sep-2012 chl

use xmalloc()/xcalloc() helpers

while there unify usage of log_trace() in ramstat_set()

ok gilles@ eric@


# 1.57 25-Aug-2012 gilles

- introduce struct stat_value
- statistics can now have a type (counter, timestamp, timeval, timespec and
possibly others in the future)
- stat_increment() / stat_decrement() now take an increment/decrement value
and are at the moment only of type counter
- stat_set() now takes a stat_value
- provide helpers to convert raw values to stat_value

ok eric@, ok chl@

while at it fix a rq_queue_dump() call using a bogus timestamp in scheduler
ramqueue.


# 1.56 21-Aug-2012 eric

Allow smtpd to work as a backup MX, relaying only to MXs with higher
priority in the DNS record. For example:

accept for domain "foo.org" relay backup "mx3.foo.org"

will relay mails for "foo.org" using only hosts with higher priority
(i.e. lower value) than "mx3.foo.org", which is supposed to be the
current server.

If the specified backup MX is not found in the DNS record, relaying
works as normal.

ok gilles@


# 1.55 21-Aug-2012 eric

Use TAILQ rather than array for mx list.

ok gilles@


# 1.54 21-Aug-2012 eric

dns sessions don't use lookup. no need to store them in a tree.

ok gilles@


# 1.53 19-Aug-2012 chl

coding style: replace all occurences of u_int* with uint*

ok eric@


# 1.52 18-Aug-2012 gilles

- introduce stat_backend, an API for pluggable statistic backends
> statistics are no longer static structures in shared memory
> statistics are only set, smtpd never uses them in its logic
> each statistic is a key/value where key can be any (dynamic) string
- convert all uses of the former API to use the new one
- implement stat_ramstat that keeps non-persistent stats in ram structure

ok eric@, ok chl@


# 1.51 08-Aug-2012 eric

cleanup some old debug traces

ok gilles@ chl@


# 1.50 29-Jul-2012 eric

fix bogus permfail when no MX is defined on a valid domain.

ok gilles@


Revision tags: OPENBSD_5_2_BASE
# 1.49 09-Jul-2012 eric

move to the new resolver implementation, with temporary glue to use
the relevant files from asr directly.

ok gilles@


# 1.48 14-Apr-2012 eric

improve readability

ok gilles@


Revision tags: OPENBSD_5_1_BASE
# 1.47 11-Jan-2012 eric

Try to parse hostnames as IP addresses before resolving. This allows
relays to be given as IP address in the config file.

ok gilles@


# 1.46 11-Jan-2012 eric

Improve error reporting. Most errors during hostname lookup are
now correctly reported as temporary failures.

from Nathanael Rensen, tweaks by me.


# 1.45 01-Sep-2011 eric

Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way.

ok gilles@ chl@


Revision tags: OPENBSD_5_0_BASE
# 1.44 20-Jul-2011 eric

Fix reporting of permanent/temporary failures for MX lookups.
Simplify code a bit while there.

ok gilles@


# 1.43 03-Jul-2011 nicm

imsg.h requires sys/queue.h and sys/uio.h.

ok eric


# 1.42 06-May-2011 eric

move dns session specific structs and prototypes out of smtpd.h.

ok gilles@


# 1.41 01-May-2011 eric

the smtpd env is meant to be global, so do not pass it all around.

discussed with and ok gilles@


# 1.40 17-Apr-2011 gilles

cleanups, cosmethic changes, functions that should be static are now static
no functionnal change


# 1.39 02-Apr-2011 eric

add stat counters for the lookup agent

ok gilles@


# 1.38 31-Mar-2011 eric

cleanup and simplification following the asr update.

- use a specific dispatch function for each type of query
- make the host handler work on a list of hosts by default (single host
queries are just a particular case) and use that to resolve the MX list
- various other code cleanup
- remove unused headers
- remove orphaned prototypes
- update copyright

ok gilles@


# 1.37 29-Mar-2011 eric

remove unused code now that reverse lookups are done through asr.

ok gilles@


# 1.36 29-Mar-2011 eric

make use the cname query interface from asr for reverse lookups

ok gilles@


# 1.35 27-Mar-2011 eric

add a function to factorize resetting of dnssession events.

ok gilles@


# 1.34 26-Mar-2011 eric

Fix the MX lookup process:

- the MX records were not always properly inserted into the sorted
array, which led to some MX being silently dropped.
- if an MX address could not be resolved, mail delivery would fail,
even though other valid MX exist for that domain. Now only report
the failure if no server address can be found at all.

grrrreeat gilles@


# 1.33 26-Mar-2011 eric

Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.

ok gilles@


# 1.32 26-Mar-2011 eric

use an index for iterating into the mx list.

ok gilles@


# 1.31 23-Mar-2011 eric

Simplify resolver initialization. This is done only once
since resolv.conf reloading is handled automatically by asr.

ok gilles@


# 1.30 09-Mar-2011 todd

make similar code .. more similar
ok gilles@

previous commit should have read:

when copying 'struct sockaddr' data, use sa_len not sizeof(struct sockaddr_in)
this fixes truncation of IPv6 addresses in the mail delivery path
ok gilles@


# 1.29 09-Mar-2011 todd

*** empty log message ***


Revision tags: OPENBSD_4_9_BASE
# 1.28 19-Dec-2010 gilles

If MX lookup fails, fallback to using the host itself. This has always been
the behavior but I introduced a regression when switching to ASR.

bug reported by jmc@, bugfix tested by jmc@ and I


# 1.27 12-Dec-2010 jsg

use memcpy instead of a cast/deref dance that was reading past
the end of the buffer.

tested by and ok gilles@


# 1.26 29-Nov-2010 gilles

replace the fork-based-non-blocking-resolver-hack by shiny async resolver
written by eric@. it is still experimental but still better than what we
had earlier so ... we'll improve in tree :)

diff by me with *lots* of help from eric@, tested by todd and I (and a
few people out there)


# 1.25 28-Nov-2010 gilles

remove unused headers


# 1.24 28-Nov-2010 gilles

a bit of .h cleanups, no functionnal change


# 1.23 08-Sep-2010 gilles

we do dns resolutions in a separate process because we don't have an async
resolver. if we run scarce on resources and we cannot fork a separate dns
process or we cannot socketpair() tell the caller that we have a temporary
failure rather than issueing a fatal(). message will stay in queue and be
rescheduled later ...

bug reported and bugfix tested by Sacha El Masry <lists@devilray.eu>


Revision tags: OPENBSD_4_8_BASE
# 1.22 29-Jun-2010 deraadt

force the dns buffers to be aligned using a union, until the retarded
"misalign strings on the stack" bug in gcc4 is fixed (even when that
is fixed this idiom is safer and quite common)
ok jacekm


# 1.21 02-Jun-2010 chl

check event_dispatch() return value

ok jacekm@


Revision tags: OPENBSD_4_7_BASE
# 1.20 14-Nov-2009 chl

add missing header needed by signal()

ok gilles@


# 1.19 11-Nov-2009 jacekm

Fix previous. When configured to relay via IP address, MX lookup would fail
(NXDOMAIN), leading to a bounce. Precede the MX lookup with an attempt to
parse the relay as numeric string.

"reads ok" gilles@


# 1.18 05-Nov-2009 jsing

Consider DNS lookups that result in NXDOMAIN to be a permanent failure.

ok gilles@ jacekm@


# 1.17 05-Nov-2009 jsing

Introduce a 6yz status code, used internally to report permanent errors.
The 1yz and 6yz status codes are now removed prior to reporting the status
message in bounce messages, which provides an easy way to distinguish
between local and remote status messages. Initial diff from jacekm@

ok gilles@ jacekm@


# 1.16 03-Sep-2009 jacekm

imsg_get sets errno so use fatal instead of fatalx.


# 1.15 08-Aug-2009 gilles

import some changes from portable smtpd to reduce the delta between both.
this commit contains mostly missing casts and cosmethic changes, do not
expect to build this anywhere but on OpenBSD, it does not contain any of
the portable glue.


Revision tags: OPENBSD_4_6_BASE
# 1.14 06-Jun-2009 pyr

Sync with relayd:
Stop pushing event handling in the imsg framework.
Instead, provide a small glue layer on top of both imsg and libevent.
This finally clearly separates event handling and imsg construction.

Sidetrack bonus: remove the mega-ugly hack of having a dummy imsg_event_add
stub in smtpctl.
ok jaceckm@


# 1.13 05-Jun-2009 pyr

make smtpd's imsg lib ready, just like relayd and ospfd.
ok gilles@, jacekm@


# 1.12 01-Jun-2009 jacekm

Fix EV_READ/EV_WRITE testing inside IMSG handlers. Based on similar change
to the routing daemons by claudio@; ok gilles@


# 1.11 09-May-2009 jacekm

- New API to handle all DNS query types (A, MX, PTR) asynchronously.

- Improve RFC compliance: CNAMEs are resolved, equal preference MXs
are randomized, relaying via MX that has equal/lower preference
than local server is prevented, decision on when to treat domain
name as implicit MX is better.

ok gilles@


Revision tags: OPENBSD_4_5_BASE
# 1.10 22-Feb-2009 form

replace MAX* constants by sizeof where possible

ok jacekm@


# 1.9 15-Feb-2009 jacekm

If MX query fails due to DNS error, do not attempt more queries; ok gilles@


# 1.8 15-Feb-2009 jacekm

Fix a bug where list of 6 MXs or more was not handled correctly.
Pointed out by & ok sthen@, ok gilles@.


# 1.7 01-Jan-2009 jacekm

remove unnecessary includes; ok gilles@


# 1.6 05-Dec-2008 gilles

- more err/errx -> fatal/fatalx, warn/warnx -> log_warn/log_warnx
contains bits based on an old diff from Jacek Masiulaniec and
other bits from me.


# 1.5 01-Dec-2008 gilles

- in mxsort, fix type of loop counter, it will never be < 0 if it is
unsigned and when running out of luck it will cause the lookup
process to crash.


# 1.4 25-Nov-2008 gilles

- move prototype to smtpd.h


# 1.3 10-Nov-2008 tedu

insertion sort is faster than bubble sort. ok gilles


# 1.2 05-Nov-2008 sobrado

add a few missing id tags; there are a bunch of files, and developers
will probably miss this change when working on more important matters,
so it is probably better to sort them now. there is a risk of losing
the tags if a change needs to be reverted too.

written with excellent advice from jmc@

ok gilles@


# 1.1 01-Nov-2008 gilles

smtpd is a smtp server implementation for OpenBSD. It is a work in progress
which still lacks many features. bringing it in tree will help working on it
more easily.

"at this stage it should go in" henning@, "move ahead" deraadt@


# 1.89 18-Sep-2019 eric

Implement server certificate validation in smtp(1).
Check certificate against MX name in smtpd(8) mta.

ok gilles@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.88 26-Sep-2018 eric

treat NOTIMP as NO_DATA in response to MX query: fallback to hostname lookup
instead of bouncing the mail.

ok gilles@


# 1.87 25-Jul-2018 eric

Implement a generic interface to forward resolver queries to the lka
process. Use it for the reverse lookups required by smtp and mta.

Until now, DNS-related lookups were implemented using ad-hoc IMSGs
between the lka and other processes. It turns out to be confusing and
difficult to maintain/extend. So we want to replace this with a better
set of IMSGs matching the standard resolver interface.

ok gilles@


# 1.86 31-May-2018 gilles

remove 'where' parameter from all x*() functions in utils.c, it doesn't
really help us with anything, propagate the change in codebase

ok millert@


Revision tags: OPENBSD_6_3_BASE
# 1.85 06-Jan-2018 sunil

Move unpack functions into a seperate file.

Required for upcoming 'smtpctl spf walk'.
Ok eric@ gilles@ millert@


Revision tags: OPENBSD_6_2_BASE
# 1.84 31-May-2017 deraadt

typo; from Edgar Pettijohn


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.83 28-Oct-2015 gilles

dns_lookup_host() needs to remove brackets and IPv6: prefix when receiving
a text representation otherwise getaddrinfo_async() will choke

ok eric@


# 1.82 17-Oct-2015 gilles

remove unused variables


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 20-Jan-2015 deraadt

use <limits.h> comprehensively. For now try to push <> includes to
each .c file, and out of the .h files. To avoid overinclude.
ok gilles, in principle. If this has been done right, -portable should
become easier to maintain.


# 1.80 06-Jan-2015 gilles

fix whitespace and indentation, by Kyle Milz


# 1.79 08-Oct-2014 eric

restrict address lookups to configured address families.

ok gilles@


Revision tags: OPENBSD_5_6_BASE
# 1.78 19-Apr-2014 gilles

(void) cast this strlcpy(), it cannot truncate


# 1.77 19-Apr-2014 gilles

these strlcpy can't truncate, the copy is from a buffer to a buffer of same
size and the first buffer handles the truncation already


# 1.76 04-Apr-2014 eric

Merge the mda, mta and smtp processes into a single unprivileged
process managing message reception, delivery and transfer. Mostly
mechanical, but very intrusive as it required to rewamp all IMSG to
fix ambiguities.

with and ok gilles@


# 1.75 03-Apr-2014 eric

use asr helpers after libevent update.


# 1.74 26-Mar-2014 eric

asr API is now public


# 1.73 25-Mar-2014 eric

update after asr API update


# 1.72 25-Mar-2014 eric

Integrate necessary dns packet parsing helpers from asr.
They are not supposed to be exposed.

ok gilles@


# 1.71 14-Mar-2014 eric

field rename


Revision tags: OPENBSD_5_5_BASE
# 1.70 26-Dec-2013 eric

bcopy -> memmove
bzero -> memset


# 1.69 18-Nov-2013 eric

When looking up a MX, parse the address if the domain is a "[ipaddr]" string.


# 1.68 26-Oct-2013 eric

%i -> %d in format strings


Revision tags: OPENBSD_5_4_BASE
# 1.67 12-Jul-2013 eric

update after asr changes.


# 1.66 24-May-2013 eric

sync with OpenSMTPD 5.3.2

ok gilles@


# 1.65 30-Apr-2013 eric

remove params after API change.


Revision tags: OPENBSD_5_3_BASE
# 1.64 26-Jan-2013 gilles

Sync with our smtpd repo:

* first bricks of ldap and sqlite support (not finished but both working)
* new table API to replace map API, all lookups are done through tables
* improved handling of temporary errors throughout the daemon
* improved scheduler and mta logic: connection reuse, optimizes batches
* improved queue: more tolerant to admin errors, new layout, less disk-IO
* improved memory usage under high load
* SSL certs/keys isolated to lookup process to avoid facing network
* VIRTUAL support improved, fully virtual setups possible now
* runtime tracing of processes through smtpctl trace
* ssl_privsep.c sync-ed with relayd
* ssl.c no longer contains smtpd specific interfaces
* smtpd-specific ssl bits moved to ssl_smtpd.c
* update mail address in copyright

FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE.

smtpd.conf(5) simplified, it will require adaptations

ok eric@


# 1.63 24-Nov-2012 eric

fix after asr update


# 1.62 23-Nov-2012 eric

knf

ok gilles@


# 1.61 12-Nov-2012 eric

Cleanups and improvements:

* Log more events (especially client session) and use a better scheme
for that: each messages is prefixed with a token to easily identify
its class:
- info/warn/debug: general server messages
- smtp-in: smtp client connections
- relay: status update for relayed messages
- delivery: status update for local deliveries

* Implement "smtpctl monitor" to display updates of selected internal
counters.

* When reloading the on-disk queue at startup do not commit a message
if no envelope was submitted for that message.

* Remove unused stuff in the config parser.

ok gilles@


# 1.60 08-Oct-2012 eric

skip RR if type is not MX. Use hostname if the list of MX is empty
after the loop.

spotted by huku at grhack.net

ok gilles@


# 1.59 03-Oct-2012 gilles

when requesting MX entries, the result can be appear in random orders.

the logic for inserting them in a lka session when acting as backup MX did
not take account for one specific case that could lead to an early exit
without smtpd getting a chance to detect the entry corrsponds to itself.

in such case, a backup MX woud try to connect to itself and bounce in the
loop detection code ... or it would sometimes work.


# 1.58 27-Sep-2012 chl

use xmalloc()/xcalloc() helpers

while there unify usage of log_trace() in ramstat_set()

ok gilles@ eric@


# 1.57 25-Aug-2012 gilles

- introduce struct stat_value
- statistics can now have a type (counter, timestamp, timeval, timespec and
possibly others in the future)
- stat_increment() / stat_decrement() now take an increment/decrement value
and are at the moment only of type counter
- stat_set() now takes a stat_value
- provide helpers to convert raw values to stat_value

ok eric@, ok chl@

while at it fix a rq_queue_dump() call using a bogus timestamp in scheduler
ramqueue.


# 1.56 21-Aug-2012 eric

Allow smtpd to work as a backup MX, relaying only to MXs with higher
priority in the DNS record. For example:

accept for domain "foo.org" relay backup "mx3.foo.org"

will relay mails for "foo.org" using only hosts with higher priority
(i.e. lower value) than "mx3.foo.org", which is supposed to be the
current server.

If the specified backup MX is not found in the DNS record, relaying
works as normal.

ok gilles@


# 1.55 21-Aug-2012 eric

Use TAILQ rather than array for mx list.

ok gilles@


# 1.54 21-Aug-2012 eric

dns sessions don't use lookup. no need to store them in a tree.

ok gilles@


# 1.53 19-Aug-2012 chl

coding style: replace all occurences of u_int* with uint*

ok eric@


# 1.52 18-Aug-2012 gilles

- introduce stat_backend, an API for pluggable statistic backends
> statistics are no longer static structures in shared memory
> statistics are only set, smtpd never uses them in its logic
> each statistic is a key/value where key can be any (dynamic) string
- convert all uses of the former API to use the new one
- implement stat_ramstat that keeps non-persistent stats in ram structure

ok eric@, ok chl@


# 1.51 08-Aug-2012 eric

cleanup some old debug traces

ok gilles@ chl@


# 1.50 29-Jul-2012 eric

fix bogus permfail when no MX is defined on a valid domain.

ok gilles@


Revision tags: OPENBSD_5_2_BASE
# 1.49 09-Jul-2012 eric

move to the new resolver implementation, with temporary glue to use
the relevant files from asr directly.

ok gilles@


# 1.48 14-Apr-2012 eric

improve readability

ok gilles@


Revision tags: OPENBSD_5_1_BASE
# 1.47 11-Jan-2012 eric

Try to parse hostnames as IP addresses before resolving. This allows
relays to be given as IP address in the config file.

ok gilles@


# 1.46 11-Jan-2012 eric

Improve error reporting. Most errors during hostname lookup are
now correctly reported as temporary failures.

from Nathanael Rensen, tweaks by me.


# 1.45 01-Sep-2011 eric

Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way.

ok gilles@ chl@


Revision tags: OPENBSD_5_0_BASE
# 1.44 20-Jul-2011 eric

Fix reporting of permanent/temporary failures for MX lookups.
Simplify code a bit while there.

ok gilles@


# 1.43 03-Jul-2011 nicm

imsg.h requires sys/queue.h and sys/uio.h.

ok eric


# 1.42 06-May-2011 eric

move dns session specific structs and prototypes out of smtpd.h.

ok gilles@


# 1.41 01-May-2011 eric

the smtpd env is meant to be global, so do not pass it all around.

discussed with and ok gilles@


# 1.40 17-Apr-2011 gilles

cleanups, cosmethic changes, functions that should be static are now static
no functionnal change


# 1.39 02-Apr-2011 eric

add stat counters for the lookup agent

ok gilles@


# 1.38 31-Mar-2011 eric

cleanup and simplification following the asr update.

- use a specific dispatch function for each type of query
- make the host handler work on a list of hosts by default (single host
queries are just a particular case) and use that to resolve the MX list
- various other code cleanup
- remove unused headers
- remove orphaned prototypes
- update copyright

ok gilles@


# 1.37 29-Mar-2011 eric

remove unused code now that reverse lookups are done through asr.

ok gilles@


# 1.36 29-Mar-2011 eric

make use the cname query interface from asr for reverse lookups

ok gilles@


# 1.35 27-Mar-2011 eric

add a function to factorize resetting of dnssession events.

ok gilles@


# 1.34 26-Mar-2011 eric

Fix the MX lookup process:

- the MX records were not always properly inserted into the sorted
array, which led to some MX being silently dropped.
- if an MX address could not be resolved, mail delivery would fail,
even though other valid MX exist for that domain. Now only report
the failure if no server address can be found at all.

grrrreeat gilles@


# 1.33 26-Mar-2011 eric

Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.

ok gilles@


# 1.32 26-Mar-2011 eric

use an index for iterating into the mx list.

ok gilles@


# 1.31 23-Mar-2011 eric

Simplify resolver initialization. This is done only once
since resolv.conf reloading is handled automatically by asr.

ok gilles@


# 1.30 09-Mar-2011 todd

make similar code .. more similar
ok gilles@

previous commit should have read:

when copying 'struct sockaddr' data, use sa_len not sizeof(struct sockaddr_in)
this fixes truncation of IPv6 addresses in the mail delivery path
ok gilles@


# 1.29 09-Mar-2011 todd

*** empty log message ***


Revision tags: OPENBSD_4_9_BASE
# 1.28 19-Dec-2010 gilles

If MX lookup fails, fallback to using the host itself. This has always been
the behavior but I introduced a regression when switching to ASR.

bug reported by jmc@, bugfix tested by jmc@ and I


# 1.27 12-Dec-2010 jsg

use memcpy instead of a cast/deref dance that was reading past
the end of the buffer.

tested by and ok gilles@


# 1.26 29-Nov-2010 gilles

replace the fork-based-non-blocking-resolver-hack by shiny async resolver
written by eric@. it is still experimental but still better than what we
had earlier so ... we'll improve in tree :)

diff by me with *lots* of help from eric@, tested by todd and I (and a
few people out there)


# 1.25 28-Nov-2010 gilles

remove unused headers


# 1.24 28-Nov-2010 gilles

a bit of .h cleanups, no functionnal change


# 1.23 08-Sep-2010 gilles

we do dns resolutions in a separate process because we don't have an async
resolver. if we run scarce on resources and we cannot fork a separate dns
process or we cannot socketpair() tell the caller that we have a temporary
failure rather than issueing a fatal(). message will stay in queue and be
rescheduled later ...

bug reported and bugfix tested by Sacha El Masry <lists@devilray.eu>


Revision tags: OPENBSD_4_8_BASE
# 1.22 29-Jun-2010 deraadt

force the dns buffers to be aligned using a union, until the retarded
"misalign strings on the stack" bug in gcc4 is fixed (even when that
is fixed this idiom is safer and quite common)
ok jacekm


# 1.21 02-Jun-2010 chl

check event_dispatch() return value

ok jacekm@


Revision tags: OPENBSD_4_7_BASE
# 1.20 14-Nov-2009 chl

add missing header needed by signal()

ok gilles@


# 1.19 11-Nov-2009 jacekm

Fix previous. When configured to relay via IP address, MX lookup would fail
(NXDOMAIN), leading to a bounce. Precede the MX lookup with an attempt to
parse the relay as numeric string.

"reads ok" gilles@


# 1.18 05-Nov-2009 jsing

Consider DNS lookups that result in NXDOMAIN to be a permanent failure.

ok gilles@ jacekm@


# 1.17 05-Nov-2009 jsing

Introduce a 6yz status code, used internally to report permanent errors.
The 1yz and 6yz status codes are now removed prior to reporting the status
message in bounce messages, which provides an easy way to distinguish
between local and remote status messages. Initial diff from jacekm@

ok gilles@ jacekm@


# 1.16 03-Sep-2009 jacekm

imsg_get sets errno so use fatal instead of fatalx.


# 1.15 08-Aug-2009 gilles

import some changes from portable smtpd to reduce the delta between both.
this commit contains mostly missing casts and cosmethic changes, do not
expect to build this anywhere but on OpenBSD, it does not contain any of
the portable glue.


Revision tags: OPENBSD_4_6_BASE
# 1.14 06-Jun-2009 pyr

Sync with relayd:
Stop pushing event handling in the imsg framework.
Instead, provide a small glue layer on top of both imsg and libevent.
This finally clearly separates event handling and imsg construction.

Sidetrack bonus: remove the mega-ugly hack of having a dummy imsg_event_add
stub in smtpctl.
ok jaceckm@


# 1.13 05-Jun-2009 pyr

make smtpd's imsg lib ready, just like relayd and ospfd.
ok gilles@, jacekm@


# 1.12 01-Jun-2009 jacekm

Fix EV_READ/EV_WRITE testing inside IMSG handlers. Based on similar change
to the routing daemons by claudio@; ok gilles@


# 1.11 09-May-2009 jacekm

- New API to handle all DNS query types (A, MX, PTR) asynchronously.

- Improve RFC compliance: CNAMEs are resolved, equal preference MXs
are randomized, relaying via MX that has equal/lower preference
than local server is prevented, decision on when to treat domain
name as implicit MX is better.

ok gilles@


Revision tags: OPENBSD_4_5_BASE
# 1.10 22-Feb-2009 form

replace MAX* constants by sizeof where possible

ok jacekm@


# 1.9 15-Feb-2009 jacekm

If MX query fails due to DNS error, do not attempt more queries; ok gilles@


# 1.8 15-Feb-2009 jacekm

Fix a bug where list of 6 MXs or more was not handled correctly.
Pointed out by & ok sthen@, ok gilles@.


# 1.7 01-Jan-2009 jacekm

remove unnecessary includes; ok gilles@


# 1.6 05-Dec-2008 gilles

- more err/errx -> fatal/fatalx, warn/warnx -> log_warn/log_warnx
contains bits based on an old diff from Jacek Masiulaniec and
other bits from me.


# 1.5 01-Dec-2008 gilles

- in mxsort, fix type of loop counter, it will never be < 0 if it is
unsigned and when running out of luck it will cause the lookup
process to crash.


# 1.4 25-Nov-2008 gilles

- move prototype to smtpd.h


# 1.3 10-Nov-2008 tedu

insertion sort is faster than bubble sort. ok gilles


# 1.2 05-Nov-2008 sobrado

add a few missing id tags; there are a bunch of files, and developers
will probably miss this change when working on more important matters,
so it is probably better to sort them now. there is a risk of losing
the tags if a change needs to be reverted too.

written with excellent advice from jmc@

ok gilles@


# 1.1 01-Nov-2008 gilles

smtpd is a smtp server implementation for OpenBSD. It is a work in progress
which still lacks many features. bringing it in tree will help working on it
more easily.

"at this stage it should go in" henning@, "move ahead" deraadt@


# 1.88 26-Sep-2018 eric

treat NOTIMP as NO_DATA in response to MX query: fallback to hostname lookup
instead of bouncing the mail.

ok gilles@


# 1.87 25-Jul-2018 eric

Implement a generic interface to forward resolver queries to the lka
process. Use it for the reverse lookups required by smtp and mta.

Until now, DNS-related lookups were implemented using ad-hoc IMSGs
between the lka and other processes. It turns out to be confusing and
difficult to maintain/extend. So we want to replace this with a better
set of IMSGs matching the standard resolver interface.

ok gilles@


# 1.86 31-May-2018 gilles

remove 'where' parameter from all x*() functions in utils.c, it doesn't
really help us with anything, propagate the change in codebase

ok millert@


Revision tags: OPENBSD_6_3_BASE
# 1.85 06-Jan-2018 sunil

Move unpack functions into a seperate file.

Required for upcoming 'smtpctl spf walk'.
Ok eric@ gilles@ millert@


Revision tags: OPENBSD_6_2_BASE
# 1.84 31-May-2017 deraadt

typo; from Edgar Pettijohn


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.83 28-Oct-2015 gilles

dns_lookup_host() needs to remove brackets and IPv6: prefix when receiving
a text representation otherwise getaddrinfo_async() will choke

ok eric@


# 1.82 17-Oct-2015 gilles

remove unused variables


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 20-Jan-2015 deraadt

use <limits.h> comprehensively. For now try to push <> includes to
each .c file, and out of the .h files. To avoid overinclude.
ok gilles, in principle. If this has been done right, -portable should
become easier to maintain.


# 1.80 06-Jan-2015 gilles

fix whitespace and indentation, by Kyle Milz


# 1.79 08-Oct-2014 eric

restrict address lookups to configured address families.

ok gilles@


Revision tags: OPENBSD_5_6_BASE
# 1.78 19-Apr-2014 gilles

(void) cast this strlcpy(), it cannot truncate


# 1.77 19-Apr-2014 gilles

these strlcpy can't truncate, the copy is from a buffer to a buffer of same
size and the first buffer handles the truncation already


# 1.76 04-Apr-2014 eric

Merge the mda, mta and smtp processes into a single unprivileged
process managing message reception, delivery and transfer. Mostly
mechanical, but very intrusive as it required to rewamp all IMSG to
fix ambiguities.

with and ok gilles@


# 1.75 03-Apr-2014 eric

use asr helpers after libevent update.


# 1.74 26-Mar-2014 eric

asr API is now public


# 1.73 25-Mar-2014 eric

update after asr API update


# 1.72 25-Mar-2014 eric

Integrate necessary dns packet parsing helpers from asr.
They are not supposed to be exposed.

ok gilles@


# 1.71 14-Mar-2014 eric

field rename


Revision tags: OPENBSD_5_5_BASE
# 1.70 26-Dec-2013 eric

bcopy -> memmove
bzero -> memset


# 1.69 18-Nov-2013 eric

When looking up a MX, parse the address if the domain is a "[ipaddr]" string.


# 1.68 26-Oct-2013 eric

%i -> %d in format strings


Revision tags: OPENBSD_5_4_BASE
# 1.67 12-Jul-2013 eric

update after asr changes.


# 1.66 24-May-2013 eric

sync with OpenSMTPD 5.3.2

ok gilles@


# 1.65 30-Apr-2013 eric

remove params after API change.


Revision tags: OPENBSD_5_3_BASE
# 1.64 26-Jan-2013 gilles

Sync with our smtpd repo:

* first bricks of ldap and sqlite support (not finished but both working)
* new table API to replace map API, all lookups are done through tables
* improved handling of temporary errors throughout the daemon
* improved scheduler and mta logic: connection reuse, optimizes batches
* improved queue: more tolerant to admin errors, new layout, less disk-IO
* improved memory usage under high load
* SSL certs/keys isolated to lookup process to avoid facing network
* VIRTUAL support improved, fully virtual setups possible now
* runtime tracing of processes through smtpctl trace
* ssl_privsep.c sync-ed with relayd
* ssl.c no longer contains smtpd specific interfaces
* smtpd-specific ssl bits moved to ssl_smtpd.c
* update mail address in copyright

FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE.

smtpd.conf(5) simplified, it will require adaptations

ok eric@


# 1.63 24-Nov-2012 eric

fix after asr update


# 1.62 23-Nov-2012 eric

knf

ok gilles@


# 1.61 12-Nov-2012 eric

Cleanups and improvements:

* Log more events (especially client session) and use a better scheme
for that: each messages is prefixed with a token to easily identify
its class:
- info/warn/debug: general server messages
- smtp-in: smtp client connections
- relay: status update for relayed messages
- delivery: status update for local deliveries

* Implement "smtpctl monitor" to display updates of selected internal
counters.

* When reloading the on-disk queue at startup do not commit a message
if no envelope was submitted for that message.

* Remove unused stuff in the config parser.

ok gilles@


# 1.60 08-Oct-2012 eric

skip RR if type is not MX. Use hostname if the list of MX is empty
after the loop.

spotted by huku at grhack.net

ok gilles@


# 1.59 03-Oct-2012 gilles

when requesting MX entries, the result can be appear in random orders.

the logic for inserting them in a lka session when acting as backup MX did
not take account for one specific case that could lead to an early exit
without smtpd getting a chance to detect the entry corrsponds to itself.

in such case, a backup MX woud try to connect to itself and bounce in the
loop detection code ... or it would sometimes work.


# 1.58 27-Sep-2012 chl

use xmalloc()/xcalloc() helpers

while there unify usage of log_trace() in ramstat_set()

ok gilles@ eric@


# 1.57 25-Aug-2012 gilles

- introduce struct stat_value
- statistics can now have a type (counter, timestamp, timeval, timespec and
possibly others in the future)
- stat_increment() / stat_decrement() now take an increment/decrement value
and are at the moment only of type counter
- stat_set() now takes a stat_value
- provide helpers to convert raw values to stat_value

ok eric@, ok chl@

while at it fix a rq_queue_dump() call using a bogus timestamp in scheduler
ramqueue.


# 1.56 21-Aug-2012 eric

Allow smtpd to work as a backup MX, relaying only to MXs with higher
priority in the DNS record. For example:

accept for domain "foo.org" relay backup "mx3.foo.org"

will relay mails for "foo.org" using only hosts with higher priority
(i.e. lower value) than "mx3.foo.org", which is supposed to be the
current server.

If the specified backup MX is not found in the DNS record, relaying
works as normal.

ok gilles@


# 1.55 21-Aug-2012 eric

Use TAILQ rather than array for mx list.

ok gilles@


# 1.54 21-Aug-2012 eric

dns sessions don't use lookup. no need to store them in a tree.

ok gilles@


# 1.53 19-Aug-2012 chl

coding style: replace all occurences of u_int* with uint*

ok eric@


# 1.52 18-Aug-2012 gilles

- introduce stat_backend, an API for pluggable statistic backends
> statistics are no longer static structures in shared memory
> statistics are only set, smtpd never uses them in its logic
> each statistic is a key/value where key can be any (dynamic) string
- convert all uses of the former API to use the new one
- implement stat_ramstat that keeps non-persistent stats in ram structure

ok eric@, ok chl@


# 1.51 08-Aug-2012 eric

cleanup some old debug traces

ok gilles@ chl@


# 1.50 29-Jul-2012 eric

fix bogus permfail when no MX is defined on a valid domain.

ok gilles@


Revision tags: OPENBSD_5_2_BASE
# 1.49 09-Jul-2012 eric

move to the new resolver implementation, with temporary glue to use
the relevant files from asr directly.

ok gilles@


# 1.48 14-Apr-2012 eric

improve readability

ok gilles@


Revision tags: OPENBSD_5_1_BASE
# 1.47 11-Jan-2012 eric

Try to parse hostnames as IP addresses before resolving. This allows
relays to be given as IP address in the config file.

ok gilles@


# 1.46 11-Jan-2012 eric

Improve error reporting. Most errors during hostname lookup are
now correctly reported as temporary failures.

from Nathanael Rensen, tweaks by me.


# 1.45 01-Sep-2011 eric

Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way.

ok gilles@ chl@


Revision tags: OPENBSD_5_0_BASE
# 1.44 20-Jul-2011 eric

Fix reporting of permanent/temporary failures for MX lookups.
Simplify code a bit while there.

ok gilles@


# 1.43 03-Jul-2011 nicm

imsg.h requires sys/queue.h and sys/uio.h.

ok eric


# 1.42 06-May-2011 eric

move dns session specific structs and prototypes out of smtpd.h.

ok gilles@


# 1.41 01-May-2011 eric

the smtpd env is meant to be global, so do not pass it all around.

discussed with and ok gilles@


# 1.40 17-Apr-2011 gilles

cleanups, cosmethic changes, functions that should be static are now static
no functionnal change


# 1.39 02-Apr-2011 eric

add stat counters for the lookup agent

ok gilles@


# 1.38 31-Mar-2011 eric

cleanup and simplification following the asr update.

- use a specific dispatch function for each type of query
- make the host handler work on a list of hosts by default (single host
queries are just a particular case) and use that to resolve the MX list
- various other code cleanup
- remove unused headers
- remove orphaned prototypes
- update copyright

ok gilles@


# 1.37 29-Mar-2011 eric

remove unused code now that reverse lookups are done through asr.

ok gilles@


# 1.36 29-Mar-2011 eric

make use the cname query interface from asr for reverse lookups

ok gilles@


# 1.35 27-Mar-2011 eric

add a function to factorize resetting of dnssession events.

ok gilles@


# 1.34 26-Mar-2011 eric

Fix the MX lookup process:

- the MX records were not always properly inserted into the sorted
array, which led to some MX being silently dropped.
- if an MX address could not be resolved, mail delivery would fail,
even though other valid MX exist for that domain. Now only report
the failure if no server address can be found at all.

grrrreeat gilles@


# 1.33 26-Mar-2011 eric

Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.

ok gilles@


# 1.32 26-Mar-2011 eric

use an index for iterating into the mx list.

ok gilles@


# 1.31 23-Mar-2011 eric

Simplify resolver initialization. This is done only once
since resolv.conf reloading is handled automatically by asr.

ok gilles@


# 1.30 09-Mar-2011 todd

make similar code .. more similar
ok gilles@

previous commit should have read:

when copying 'struct sockaddr' data, use sa_len not sizeof(struct sockaddr_in)
this fixes truncation of IPv6 addresses in the mail delivery path
ok gilles@


# 1.29 09-Mar-2011 todd

*** empty log message ***


Revision tags: OPENBSD_4_9_BASE
# 1.28 19-Dec-2010 gilles

If MX lookup fails, fallback to using the host itself. This has always been
the behavior but I introduced a regression when switching to ASR.

bug reported by jmc@, bugfix tested by jmc@ and I


# 1.27 12-Dec-2010 jsg

use memcpy instead of a cast/deref dance that was reading past
the end of the buffer.

tested by and ok gilles@


# 1.26 29-Nov-2010 gilles

replace the fork-based-non-blocking-resolver-hack by shiny async resolver
written by eric@. it is still experimental but still better than what we
had earlier so ... we'll improve in tree :)

diff by me with *lots* of help from eric@, tested by todd and I (and a
few people out there)


# 1.25 28-Nov-2010 gilles

remove unused headers


# 1.24 28-Nov-2010 gilles

a bit of .h cleanups, no functionnal change


# 1.23 08-Sep-2010 gilles

we do dns resolutions in a separate process because we don't have an async
resolver. if we run scarce on resources and we cannot fork a separate dns
process or we cannot socketpair() tell the caller that we have a temporary
failure rather than issueing a fatal(). message will stay in queue and be
rescheduled later ...

bug reported and bugfix tested by Sacha El Masry <lists@devilray.eu>


Revision tags: OPENBSD_4_8_BASE
# 1.22 29-Jun-2010 deraadt

force the dns buffers to be aligned using a union, until the retarded
"misalign strings on the stack" bug in gcc4 is fixed (even when that
is fixed this idiom is safer and quite common)
ok jacekm


# 1.21 02-Jun-2010 chl

check event_dispatch() return value

ok jacekm@


Revision tags: OPENBSD_4_7_BASE
# 1.20 14-Nov-2009 chl

add missing header needed by signal()

ok gilles@


# 1.19 11-Nov-2009 jacekm

Fix previous. When configured to relay via IP address, MX lookup would fail
(NXDOMAIN), leading to a bounce. Precede the MX lookup with an attempt to
parse the relay as numeric string.

"reads ok" gilles@


# 1.18 05-Nov-2009 jsing

Consider DNS lookups that result in NXDOMAIN to be a permanent failure.

ok gilles@ jacekm@


# 1.17 05-Nov-2009 jsing

Introduce a 6yz status code, used internally to report permanent errors.
The 1yz and 6yz status codes are now removed prior to reporting the status
message in bounce messages, which provides an easy way to distinguish
between local and remote status messages. Initial diff from jacekm@

ok gilles@ jacekm@


# 1.16 03-Sep-2009 jacekm

imsg_get sets errno so use fatal instead of fatalx.


# 1.15 08-Aug-2009 gilles

import some changes from portable smtpd to reduce the delta between both.
this commit contains mostly missing casts and cosmethic changes, do not
expect to build this anywhere but on OpenBSD, it does not contain any of
the portable glue.


Revision tags: OPENBSD_4_6_BASE
# 1.14 06-Jun-2009 pyr

Sync with relayd:
Stop pushing event handling in the imsg framework.
Instead, provide a small glue layer on top of both imsg and libevent.
This finally clearly separates event handling and imsg construction.

Sidetrack bonus: remove the mega-ugly hack of having a dummy imsg_event_add
stub in smtpctl.
ok jaceckm@


# 1.13 05-Jun-2009 pyr

make smtpd's imsg lib ready, just like relayd and ospfd.
ok gilles@, jacekm@


# 1.12 01-Jun-2009 jacekm

Fix EV_READ/EV_WRITE testing inside IMSG handlers. Based on similar change
to the routing daemons by claudio@; ok gilles@


# 1.11 09-May-2009 jacekm

- New API to handle all DNS query types (A, MX, PTR) asynchronously.

- Improve RFC compliance: CNAMEs are resolved, equal preference MXs
are randomized, relaying via MX that has equal/lower preference
than local server is prevented, decision on when to treat domain
name as implicit MX is better.

ok gilles@


Revision tags: OPENBSD_4_5_BASE
# 1.10 22-Feb-2009 form

replace MAX* constants by sizeof where possible

ok jacekm@


# 1.9 15-Feb-2009 jacekm

If MX query fails due to DNS error, do not attempt more queries; ok gilles@


# 1.8 15-Feb-2009 jacekm

Fix a bug where list of 6 MXs or more was not handled correctly.
Pointed out by & ok sthen@, ok gilles@.


# 1.7 01-Jan-2009 jacekm

remove unnecessary includes; ok gilles@


# 1.6 05-Dec-2008 gilles

- more err/errx -> fatal/fatalx, warn/warnx -> log_warn/log_warnx
contains bits based on an old diff from Jacek Masiulaniec and
other bits from me.


# 1.5 01-Dec-2008 gilles

- in mxsort, fix type of loop counter, it will never be < 0 if it is
unsigned and when running out of luck it will cause the lookup
process to crash.


# 1.4 25-Nov-2008 gilles

- move prototype to smtpd.h


# 1.3 10-Nov-2008 tedu

insertion sort is faster than bubble sort. ok gilles


# 1.2 05-Nov-2008 sobrado

add a few missing id tags; there are a bunch of files, and developers
will probably miss this change when working on more important matters,
so it is probably better to sort them now. there is a risk of losing
the tags if a change needs to be reverted too.

written with excellent advice from jmc@

ok gilles@


# 1.1 01-Nov-2008 gilles

smtpd is a smtp server implementation for OpenBSD. It is a work in progress
which still lacks many features. bringing it in tree will help working on it
more easily.

"at this stage it should go in" henning@, "move ahead" deraadt@


# 1.87 25-Jul-2018 eric

Implement a generic interface to forward resolver queries to the lka
process. Use it for the reverse lookups required by smtp and mta.

Until now, DNS-related lookups were implemented using ad-hoc IMSGs
between the lka and other processes. It turns out to be confusing and
difficult to maintain/extend. So we want to replace this with a better
set of IMSGs matching the standard resolver interface.

ok gilles@


# 1.86 31-May-2018 gilles

remove 'where' parameter from all x*() functions in utils.c, it doesn't
really help us with anything, propagate the change in codebase

ok millert@


Revision tags: OPENBSD_6_3_BASE
# 1.85 06-Jan-2018 sunil

Move unpack functions into a seperate file.

Required for upcoming 'smtpctl spf walk'.
Ok eric@ gilles@ millert@


Revision tags: OPENBSD_6_2_BASE
# 1.84 31-May-2017 deraadt

typo; from Edgar Pettijohn


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.83 28-Oct-2015 gilles

dns_lookup_host() needs to remove brackets and IPv6: prefix when receiving
a text representation otherwise getaddrinfo_async() will choke

ok eric@


# 1.82 17-Oct-2015 gilles

remove unused variables


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 20-Jan-2015 deraadt

use <limits.h> comprehensively. For now try to push <> includes to
each .c file, and out of the .h files. To avoid overinclude.
ok gilles, in principle. If this has been done right, -portable should
become easier to maintain.


# 1.80 06-Jan-2015 gilles

fix whitespace and indentation, by Kyle Milz


# 1.79 08-Oct-2014 eric

restrict address lookups to configured address families.

ok gilles@


Revision tags: OPENBSD_5_6_BASE
# 1.78 19-Apr-2014 gilles

(void) cast this strlcpy(), it cannot truncate


# 1.77 19-Apr-2014 gilles

these strlcpy can't truncate, the copy is from a buffer to a buffer of same
size and the first buffer handles the truncation already


# 1.76 04-Apr-2014 eric

Merge the mda, mta and smtp processes into a single unprivileged
process managing message reception, delivery and transfer. Mostly
mechanical, but very intrusive as it required to rewamp all IMSG to
fix ambiguities.

with and ok gilles@


# 1.75 03-Apr-2014 eric

use asr helpers after libevent update.


# 1.74 26-Mar-2014 eric

asr API is now public


# 1.73 25-Mar-2014 eric

update after asr API update


# 1.72 25-Mar-2014 eric

Integrate necessary dns packet parsing helpers from asr.
They are not supposed to be exposed.

ok gilles@


# 1.71 14-Mar-2014 eric

field rename


Revision tags: OPENBSD_5_5_BASE
# 1.70 26-Dec-2013 eric

bcopy -> memmove
bzero -> memset


# 1.69 18-Nov-2013 eric

When looking up a MX, parse the address if the domain is a "[ipaddr]" string.


# 1.68 26-Oct-2013 eric

%i -> %d in format strings


Revision tags: OPENBSD_5_4_BASE
# 1.67 12-Jul-2013 eric

update after asr changes.


# 1.66 24-May-2013 eric

sync with OpenSMTPD 5.3.2

ok gilles@


# 1.65 30-Apr-2013 eric

remove params after API change.


Revision tags: OPENBSD_5_3_BASE
# 1.64 26-Jan-2013 gilles

Sync with our smtpd repo:

* first bricks of ldap and sqlite support (not finished but both working)
* new table API to replace map API, all lookups are done through tables
* improved handling of temporary errors throughout the daemon
* improved scheduler and mta logic: connection reuse, optimizes batches
* improved queue: more tolerant to admin errors, new layout, less disk-IO
* improved memory usage under high load
* SSL certs/keys isolated to lookup process to avoid facing network
* VIRTUAL support improved, fully virtual setups possible now
* runtime tracing of processes through smtpctl trace
* ssl_privsep.c sync-ed with relayd
* ssl.c no longer contains smtpd specific interfaces
* smtpd-specific ssl bits moved to ssl_smtpd.c
* update mail address in copyright

FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE.

smtpd.conf(5) simplified, it will require adaptations

ok eric@


# 1.63 24-Nov-2012 eric

fix after asr update


# 1.62 23-Nov-2012 eric

knf

ok gilles@


# 1.61 12-Nov-2012 eric

Cleanups and improvements:

* Log more events (especially client session) and use a better scheme
for that: each messages is prefixed with a token to easily identify
its class:
- info/warn/debug: general server messages
- smtp-in: smtp client connections
- relay: status update for relayed messages
- delivery: status update for local deliveries

* Implement "smtpctl monitor" to display updates of selected internal
counters.

* When reloading the on-disk queue at startup do not commit a message
if no envelope was submitted for that message.

* Remove unused stuff in the config parser.

ok gilles@


# 1.60 08-Oct-2012 eric

skip RR if type is not MX. Use hostname if the list of MX is empty
after the loop.

spotted by huku at grhack.net

ok gilles@


# 1.59 03-Oct-2012 gilles

when requesting MX entries, the result can be appear in random orders.

the logic for inserting them in a lka session when acting as backup MX did
not take account for one specific case that could lead to an early exit
without smtpd getting a chance to detect the entry corrsponds to itself.

in such case, a backup MX woud try to connect to itself and bounce in the
loop detection code ... or it would sometimes work.


# 1.58 27-Sep-2012 chl

use xmalloc()/xcalloc() helpers

while there unify usage of log_trace() in ramstat_set()

ok gilles@ eric@


# 1.57 25-Aug-2012 gilles

- introduce struct stat_value
- statistics can now have a type (counter, timestamp, timeval, timespec and
possibly others in the future)
- stat_increment() / stat_decrement() now take an increment/decrement value
and are at the moment only of type counter
- stat_set() now takes a stat_value
- provide helpers to convert raw values to stat_value

ok eric@, ok chl@

while at it fix a rq_queue_dump() call using a bogus timestamp in scheduler
ramqueue.


# 1.56 21-Aug-2012 eric

Allow smtpd to work as a backup MX, relaying only to MXs with higher
priority in the DNS record. For example:

accept for domain "foo.org" relay backup "mx3.foo.org"

will relay mails for "foo.org" using only hosts with higher priority
(i.e. lower value) than "mx3.foo.org", which is supposed to be the
current server.

If the specified backup MX is not found in the DNS record, relaying
works as normal.

ok gilles@


# 1.55 21-Aug-2012 eric

Use TAILQ rather than array for mx list.

ok gilles@


# 1.54 21-Aug-2012 eric

dns sessions don't use lookup. no need to store them in a tree.

ok gilles@


# 1.53 19-Aug-2012 chl

coding style: replace all occurences of u_int* with uint*

ok eric@


# 1.52 18-Aug-2012 gilles

- introduce stat_backend, an API for pluggable statistic backends
> statistics are no longer static structures in shared memory
> statistics are only set, smtpd never uses them in its logic
> each statistic is a key/value where key can be any (dynamic) string
- convert all uses of the former API to use the new one
- implement stat_ramstat that keeps non-persistent stats in ram structure

ok eric@, ok chl@


# 1.51 08-Aug-2012 eric

cleanup some old debug traces

ok gilles@ chl@


# 1.50 29-Jul-2012 eric

fix bogus permfail when no MX is defined on a valid domain.

ok gilles@


Revision tags: OPENBSD_5_2_BASE
# 1.49 09-Jul-2012 eric

move to the new resolver implementation, with temporary glue to use
the relevant files from asr directly.

ok gilles@


# 1.48 14-Apr-2012 eric

improve readability

ok gilles@


Revision tags: OPENBSD_5_1_BASE
# 1.47 11-Jan-2012 eric

Try to parse hostnames as IP addresses before resolving. This allows
relays to be given as IP address in the config file.

ok gilles@


# 1.46 11-Jan-2012 eric

Improve error reporting. Most errors during hostname lookup are
now correctly reported as temporary failures.

from Nathanael Rensen, tweaks by me.


# 1.45 01-Sep-2011 eric

Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way.

ok gilles@ chl@


Revision tags: OPENBSD_5_0_BASE
# 1.44 20-Jul-2011 eric

Fix reporting of permanent/temporary failures for MX lookups.
Simplify code a bit while there.

ok gilles@


# 1.43 03-Jul-2011 nicm

imsg.h requires sys/queue.h and sys/uio.h.

ok eric


# 1.42 06-May-2011 eric

move dns session specific structs and prototypes out of smtpd.h.

ok gilles@


# 1.41 01-May-2011 eric

the smtpd env is meant to be global, so do not pass it all around.

discussed with and ok gilles@


# 1.40 17-Apr-2011 gilles

cleanups, cosmethic changes, functions that should be static are now static
no functionnal change


# 1.39 02-Apr-2011 eric

add stat counters for the lookup agent

ok gilles@


# 1.38 31-Mar-2011 eric

cleanup and simplification following the asr update.

- use a specific dispatch function for each type of query
- make the host handler work on a list of hosts by default (single host
queries are just a particular case) and use that to resolve the MX list
- various other code cleanup
- remove unused headers
- remove orphaned prototypes
- update copyright

ok gilles@


# 1.37 29-Mar-2011 eric

remove unused code now that reverse lookups are done through asr.

ok gilles@


# 1.36 29-Mar-2011 eric

make use the cname query interface from asr for reverse lookups

ok gilles@


# 1.35 27-Mar-2011 eric

add a function to factorize resetting of dnssession events.

ok gilles@


# 1.34 26-Mar-2011 eric

Fix the MX lookup process:

- the MX records were not always properly inserted into the sorted
array, which led to some MX being silently dropped.
- if an MX address could not be resolved, mail delivery would fail,
even though other valid MX exist for that domain. Now only report
the failure if no server address can be found at all.

grrrreeat gilles@


# 1.33 26-Mar-2011 eric

Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.

ok gilles@


# 1.32 26-Mar-2011 eric

use an index for iterating into the mx list.

ok gilles@


# 1.31 23-Mar-2011 eric

Simplify resolver initialization. This is done only once
since resolv.conf reloading is handled automatically by asr.

ok gilles@


# 1.30 09-Mar-2011 todd

make similar code .. more similar
ok gilles@

previous commit should have read:

when copying 'struct sockaddr' data, use sa_len not sizeof(struct sockaddr_in)
this fixes truncation of IPv6 addresses in the mail delivery path
ok gilles@


# 1.29 09-Mar-2011 todd

*** empty log message ***


Revision tags: OPENBSD_4_9_BASE
# 1.28 19-Dec-2010 gilles

If MX lookup fails, fallback to using the host itself. This has always been
the behavior but I introduced a regression when switching to ASR.

bug reported by jmc@, bugfix tested by jmc@ and I


# 1.27 12-Dec-2010 jsg

use memcpy instead of a cast/deref dance that was reading past
the end of the buffer.

tested by and ok gilles@


# 1.26 29-Nov-2010 gilles

replace the fork-based-non-blocking-resolver-hack by shiny async resolver
written by eric@. it is still experimental but still better than what we
had earlier so ... we'll improve in tree :)

diff by me with *lots* of help from eric@, tested by todd and I (and a
few people out there)


# 1.25 28-Nov-2010 gilles

remove unused headers


# 1.24 28-Nov-2010 gilles

a bit of .h cleanups, no functionnal change


# 1.23 08-Sep-2010 gilles

we do dns resolutions in a separate process because we don't have an async
resolver. if we run scarce on resources and we cannot fork a separate dns
process or we cannot socketpair() tell the caller that we have a temporary
failure rather than issueing a fatal(). message will stay in queue and be
rescheduled later ...

bug reported and bugfix tested by Sacha El Masry <lists@devilray.eu>


Revision tags: OPENBSD_4_8_BASE
# 1.22 29-Jun-2010 deraadt

force the dns buffers to be aligned using a union, until the retarded
"misalign strings on the stack" bug in gcc4 is fixed (even when that
is fixed this idiom is safer and quite common)
ok jacekm


# 1.21 02-Jun-2010 chl

check event_dispatch() return value

ok jacekm@


Revision tags: OPENBSD_4_7_BASE
# 1.20 14-Nov-2009 chl

add missing header needed by signal()

ok gilles@


# 1.19 11-Nov-2009 jacekm

Fix previous. When configured to relay via IP address, MX lookup would fail
(NXDOMAIN), leading to a bounce. Precede the MX lookup with an attempt to
parse the relay as numeric string.

"reads ok" gilles@


# 1.18 05-Nov-2009 jsing

Consider DNS lookups that result in NXDOMAIN to be a permanent failure.

ok gilles@ jacekm@


# 1.17 05-Nov-2009 jsing

Introduce a 6yz status code, used internally to report permanent errors.
The 1yz and 6yz status codes are now removed prior to reporting the status
message in bounce messages, which provides an easy way to distinguish
between local and remote status messages. Initial diff from jacekm@

ok gilles@ jacekm@


# 1.16 03-Sep-2009 jacekm

imsg_get sets errno so use fatal instead of fatalx.


# 1.15 08-Aug-2009 gilles

import some changes from portable smtpd to reduce the delta between both.
this commit contains mostly missing casts and cosmethic changes, do not
expect to build this anywhere but on OpenBSD, it does not contain any of
the portable glue.


Revision tags: OPENBSD_4_6_BASE
# 1.14 06-Jun-2009 pyr

Sync with relayd:
Stop pushing event handling in the imsg framework.
Instead, provide a small glue layer on top of both imsg and libevent.
This finally clearly separates event handling and imsg construction.

Sidetrack bonus: remove the mega-ugly hack of having a dummy imsg_event_add
stub in smtpctl.
ok jaceckm@


# 1.13 05-Jun-2009 pyr

make smtpd's imsg lib ready, just like relayd and ospfd.
ok gilles@, jacekm@


# 1.12 01-Jun-2009 jacekm

Fix EV_READ/EV_WRITE testing inside IMSG handlers. Based on similar change
to the routing daemons by claudio@; ok gilles@


# 1.11 09-May-2009 jacekm

- New API to handle all DNS query types (A, MX, PTR) asynchronously.

- Improve RFC compliance: CNAMEs are resolved, equal preference MXs
are randomized, relaying via MX that has equal/lower preference
than local server is prevented, decision on when to treat domain
name as implicit MX is better.

ok gilles@


Revision tags: OPENBSD_4_5_BASE
# 1.10 22-Feb-2009 form

replace MAX* constants by sizeof where possible

ok jacekm@


# 1.9 15-Feb-2009 jacekm

If MX query fails due to DNS error, do not attempt more queries; ok gilles@


# 1.8 15-Feb-2009 jacekm

Fix a bug where list of 6 MXs or more was not handled correctly.
Pointed out by & ok sthen@, ok gilles@.


# 1.7 01-Jan-2009 jacekm

remove unnecessary includes; ok gilles@


# 1.6 05-Dec-2008 gilles

- more err/errx -> fatal/fatalx, warn/warnx -> log_warn/log_warnx
contains bits based on an old diff from Jacek Masiulaniec and
other bits from me.


# 1.5 01-Dec-2008 gilles

- in mxsort, fix type of loop counter, it will never be < 0 if it is
unsigned and when running out of luck it will cause the lookup
process to crash.


# 1.4 25-Nov-2008 gilles

- move prototype to smtpd.h


# 1.3 10-Nov-2008 tedu

insertion sort is faster than bubble sort. ok gilles


# 1.2 05-Nov-2008 sobrado

add a few missing id tags; there are a bunch of files, and developers
will probably miss this change when working on more important matters,
so it is probably better to sort them now. there is a risk of losing
the tags if a change needs to be reverted too.

written with excellent advice from jmc@

ok gilles@


# 1.1 01-Nov-2008 gilles

smtpd is a smtp server implementation for OpenBSD. It is a work in progress
which still lacks many features. bringing it in tree will help working on it
more easily.

"at this stage it should go in" henning@, "move ahead" deraadt@


# 1.86 31-May-2018 gilles

remove 'where' parameter from all x*() functions in utils.c, it doesn't
really help us with anything, propagate the change in codebase

ok millert@


Revision tags: OPENBSD_6_3_BASE
# 1.85 06-Jan-2018 sunil

Move unpack functions into a seperate file.

Required for upcoming 'smtpctl spf walk'.
Ok eric@ gilles@ millert@


Revision tags: OPENBSD_6_2_BASE
# 1.84 31-May-2017 deraadt

typo; from Edgar Pettijohn


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.83 28-Oct-2015 gilles

dns_lookup_host() needs to remove brackets and IPv6: prefix when receiving
a text representation otherwise getaddrinfo_async() will choke

ok eric@


# 1.82 17-Oct-2015 gilles

remove unused variables


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 20-Jan-2015 deraadt

use <limits.h> comprehensively. For now try to push <> includes to
each .c file, and out of the .h files. To avoid overinclude.
ok gilles, in principle. If this has been done right, -portable should
become easier to maintain.


# 1.80 06-Jan-2015 gilles

fix whitespace and indentation, by Kyle Milz


# 1.79 08-Oct-2014 eric

restrict address lookups to configured address families.

ok gilles@


Revision tags: OPENBSD_5_6_BASE
# 1.78 19-Apr-2014 gilles

(void) cast this strlcpy(), it cannot truncate


# 1.77 19-Apr-2014 gilles

these strlcpy can't truncate, the copy is from a buffer to a buffer of same
size and the first buffer handles the truncation already


# 1.76 04-Apr-2014 eric

Merge the mda, mta and smtp processes into a single unprivileged
process managing message reception, delivery and transfer. Mostly
mechanical, but very intrusive as it required to rewamp all IMSG to
fix ambiguities.

with and ok gilles@


# 1.75 03-Apr-2014 eric

use asr helpers after libevent update.


# 1.74 26-Mar-2014 eric

asr API is now public


# 1.73 25-Mar-2014 eric

update after asr API update


# 1.72 25-Mar-2014 eric

Integrate necessary dns packet parsing helpers from asr.
They are not supposed to be exposed.

ok gilles@


# 1.71 14-Mar-2014 eric

field rename


Revision tags: OPENBSD_5_5_BASE
# 1.70 26-Dec-2013 eric

bcopy -> memmove
bzero -> memset


# 1.69 18-Nov-2013 eric

When looking up a MX, parse the address if the domain is a "[ipaddr]" string.


# 1.68 26-Oct-2013 eric

%i -> %d in format strings


Revision tags: OPENBSD_5_4_BASE
# 1.67 12-Jul-2013 eric

update after asr changes.


# 1.66 24-May-2013 eric

sync with OpenSMTPD 5.3.2

ok gilles@


# 1.65 30-Apr-2013 eric

remove params after API change.


Revision tags: OPENBSD_5_3_BASE
# 1.64 26-Jan-2013 gilles

Sync with our smtpd repo:

* first bricks of ldap and sqlite support (not finished but both working)
* new table API to replace map API, all lookups are done through tables
* improved handling of temporary errors throughout the daemon
* improved scheduler and mta logic: connection reuse, optimizes batches
* improved queue: more tolerant to admin errors, new layout, less disk-IO
* improved memory usage under high load
* SSL certs/keys isolated to lookup process to avoid facing network
* VIRTUAL support improved, fully virtual setups possible now
* runtime tracing of processes through smtpctl trace
* ssl_privsep.c sync-ed with relayd
* ssl.c no longer contains smtpd specific interfaces
* smtpd-specific ssl bits moved to ssl_smtpd.c
* update mail address in copyright

FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE.

smtpd.conf(5) simplified, it will require adaptations

ok eric@


# 1.63 24-Nov-2012 eric

fix after asr update


# 1.62 23-Nov-2012 eric

knf

ok gilles@


# 1.61 12-Nov-2012 eric

Cleanups and improvements:

* Log more events (especially client session) and use a better scheme
for that: each messages is prefixed with a token to easily identify
its class:
- info/warn/debug: general server messages
- smtp-in: smtp client connections
- relay: status update for relayed messages
- delivery: status update for local deliveries

* Implement "smtpctl monitor" to display updates of selected internal
counters.

* When reloading the on-disk queue at startup do not commit a message
if no envelope was submitted for that message.

* Remove unused stuff in the config parser.

ok gilles@


# 1.60 08-Oct-2012 eric

skip RR if type is not MX. Use hostname if the list of MX is empty
after the loop.

spotted by huku at grhack.net

ok gilles@


# 1.59 03-Oct-2012 gilles

when requesting MX entries, the result can be appear in random orders.

the logic for inserting them in a lka session when acting as backup MX did
not take account for one specific case that could lead to an early exit
without smtpd getting a chance to detect the entry corrsponds to itself.

in such case, a backup MX woud try to connect to itself and bounce in the
loop detection code ... or it would sometimes work.


# 1.58 27-Sep-2012 chl

use xmalloc()/xcalloc() helpers

while there unify usage of log_trace() in ramstat_set()

ok gilles@ eric@


# 1.57 25-Aug-2012 gilles

- introduce struct stat_value
- statistics can now have a type (counter, timestamp, timeval, timespec and
possibly others in the future)
- stat_increment() / stat_decrement() now take an increment/decrement value
and are at the moment only of type counter
- stat_set() now takes a stat_value
- provide helpers to convert raw values to stat_value

ok eric@, ok chl@

while at it fix a rq_queue_dump() call using a bogus timestamp in scheduler
ramqueue.


# 1.56 21-Aug-2012 eric

Allow smtpd to work as a backup MX, relaying only to MXs with higher
priority in the DNS record. For example:

accept for domain "foo.org" relay backup "mx3.foo.org"

will relay mails for "foo.org" using only hosts with higher priority
(i.e. lower value) than "mx3.foo.org", which is supposed to be the
current server.

If the specified backup MX is not found in the DNS record, relaying
works as normal.

ok gilles@


# 1.55 21-Aug-2012 eric

Use TAILQ rather than array for mx list.

ok gilles@


# 1.54 21-Aug-2012 eric

dns sessions don't use lookup. no need to store them in a tree.

ok gilles@


# 1.53 19-Aug-2012 chl

coding style: replace all occurences of u_int* with uint*

ok eric@


# 1.52 18-Aug-2012 gilles

- introduce stat_backend, an API for pluggable statistic backends
> statistics are no longer static structures in shared memory
> statistics are only set, smtpd never uses them in its logic
> each statistic is a key/value where key can be any (dynamic) string
- convert all uses of the former API to use the new one
- implement stat_ramstat that keeps non-persistent stats in ram structure

ok eric@, ok chl@


# 1.51 08-Aug-2012 eric

cleanup some old debug traces

ok gilles@ chl@


# 1.50 29-Jul-2012 eric

fix bogus permfail when no MX is defined on a valid domain.

ok gilles@


Revision tags: OPENBSD_5_2_BASE
# 1.49 09-Jul-2012 eric

move to the new resolver implementation, with temporary glue to use
the relevant files from asr directly.

ok gilles@


# 1.48 14-Apr-2012 eric

improve readability

ok gilles@


Revision tags: OPENBSD_5_1_BASE
# 1.47 11-Jan-2012 eric

Try to parse hostnames as IP addresses before resolving. This allows
relays to be given as IP address in the config file.

ok gilles@


# 1.46 11-Jan-2012 eric

Improve error reporting. Most errors during hostname lookup are
now correctly reported as temporary failures.

from Nathanael Rensen, tweaks by me.


# 1.45 01-Sep-2011 eric

Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way.

ok gilles@ chl@


Revision tags: OPENBSD_5_0_BASE
# 1.44 20-Jul-2011 eric

Fix reporting of permanent/temporary failures for MX lookups.
Simplify code a bit while there.

ok gilles@


# 1.43 03-Jul-2011 nicm

imsg.h requires sys/queue.h and sys/uio.h.

ok eric


# 1.42 06-May-2011 eric

move dns session specific structs and prototypes out of smtpd.h.

ok gilles@


# 1.41 01-May-2011 eric

the smtpd env is meant to be global, so do not pass it all around.

discussed with and ok gilles@


# 1.40 17-Apr-2011 gilles

cleanups, cosmethic changes, functions that should be static are now static
no functionnal change


# 1.39 02-Apr-2011 eric

add stat counters for the lookup agent

ok gilles@


# 1.38 31-Mar-2011 eric

cleanup and simplification following the asr update.

- use a specific dispatch function for each type of query
- make the host handler work on a list of hosts by default (single host
queries are just a particular case) and use that to resolve the MX list
- various other code cleanup
- remove unused headers
- remove orphaned prototypes
- update copyright

ok gilles@


# 1.37 29-Mar-2011 eric

remove unused code now that reverse lookups are done through asr.

ok gilles@


# 1.36 29-Mar-2011 eric

make use the cname query interface from asr for reverse lookups

ok gilles@


# 1.35 27-Mar-2011 eric

add a function to factorize resetting of dnssession events.

ok gilles@


# 1.34 26-Mar-2011 eric

Fix the MX lookup process:

- the MX records were not always properly inserted into the sorted
array, which led to some MX being silently dropped.
- if an MX address could not be resolved, mail delivery would fail,
even though other valid MX exist for that domain. Now only report
the failure if no server address can be found at all.

grrrreeat gilles@


# 1.33 26-Mar-2011 eric

Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.

ok gilles@


# 1.32 26-Mar-2011 eric

use an index for iterating into the mx list.

ok gilles@


# 1.31 23-Mar-2011 eric

Simplify resolver initialization. This is done only once
since resolv.conf reloading is handled automatically by asr.

ok gilles@


# 1.30 09-Mar-2011 todd

make similar code .. more similar
ok gilles@

previous commit should have read:

when copying 'struct sockaddr' data, use sa_len not sizeof(struct sockaddr_in)
this fixes truncation of IPv6 addresses in the mail delivery path
ok gilles@


# 1.29 09-Mar-2011 todd

*** empty log message ***


Revision tags: OPENBSD_4_9_BASE
# 1.28 19-Dec-2010 gilles

If MX lookup fails, fallback to using the host itself. This has always been
the behavior but I introduced a regression when switching to ASR.

bug reported by jmc@, bugfix tested by jmc@ and I


# 1.27 12-Dec-2010 jsg

use memcpy instead of a cast/deref dance that was reading past
the end of the buffer.

tested by and ok gilles@


# 1.26 29-Nov-2010 gilles

replace the fork-based-non-blocking-resolver-hack by shiny async resolver
written by eric@. it is still experimental but still better than what we
had earlier so ... we'll improve in tree :)

diff by me with *lots* of help from eric@, tested by todd and I (and a
few people out there)


# 1.25 28-Nov-2010 gilles

remove unused headers


# 1.24 28-Nov-2010 gilles

a bit of .h cleanups, no functionnal change


# 1.23 08-Sep-2010 gilles

we do dns resolutions in a separate process because we don't have an async
resolver. if we run scarce on resources and we cannot fork a separate dns
process or we cannot socketpair() tell the caller that we have a temporary
failure rather than issueing a fatal(). message will stay in queue and be
rescheduled later ...

bug reported and bugfix tested by Sacha El Masry <lists@devilray.eu>


Revision tags: OPENBSD_4_8_BASE
# 1.22 29-Jun-2010 deraadt

force the dns buffers to be aligned using a union, until the retarded
"misalign strings on the stack" bug in gcc4 is fixed (even when that
is fixed this idiom is safer and quite common)
ok jacekm


# 1.21 02-Jun-2010 chl

check event_dispatch() return value

ok jacekm@


Revision tags: OPENBSD_4_7_BASE
# 1.20 14-Nov-2009 chl

add missing header needed by signal()

ok gilles@


# 1.19 11-Nov-2009 jacekm

Fix previous. When configured to relay via IP address, MX lookup would fail
(NXDOMAIN), leading to a bounce. Precede the MX lookup with an attempt to
parse the relay as numeric string.

"reads ok" gilles@


# 1.18 05-Nov-2009 jsing

Consider DNS lookups that result in NXDOMAIN to be a permanent failure.

ok gilles@ jacekm@


# 1.17 05-Nov-2009 jsing

Introduce a 6yz status code, used internally to report permanent errors.
The 1yz and 6yz status codes are now removed prior to reporting the status
message in bounce messages, which provides an easy way to distinguish
between local and remote status messages. Initial diff from jacekm@

ok gilles@ jacekm@


# 1.16 03-Sep-2009 jacekm

imsg_get sets errno so use fatal instead of fatalx.


# 1.15 08-Aug-2009 gilles

import some changes from portable smtpd to reduce the delta between both.
this commit contains mostly missing casts and cosmethic changes, do not
expect to build this anywhere but on OpenBSD, it does not contain any of
the portable glue.


Revision tags: OPENBSD_4_6_BASE
# 1.14 06-Jun-2009 pyr

Sync with relayd:
Stop pushing event handling in the imsg framework.
Instead, provide a small glue layer on top of both imsg and libevent.
This finally clearly separates event handling and imsg construction.

Sidetrack bonus: remove the mega-ugly hack of having a dummy imsg_event_add
stub in smtpctl.
ok jaceckm@


# 1.13 05-Jun-2009 pyr

make smtpd's imsg lib ready, just like relayd and ospfd.
ok gilles@, jacekm@


# 1.12 01-Jun-2009 jacekm

Fix EV_READ/EV_WRITE testing inside IMSG handlers. Based on similar change
to the routing daemons by claudio@; ok gilles@


# 1.11 09-May-2009 jacekm

- New API to handle all DNS query types (A, MX, PTR) asynchronously.

- Improve RFC compliance: CNAMEs are resolved, equal preference MXs
are randomized, relaying via MX that has equal/lower preference
than local server is prevented, decision on when to treat domain
name as implicit MX is better.

ok gilles@


Revision tags: OPENBSD_4_5_BASE
# 1.10 22-Feb-2009 form

replace MAX* constants by sizeof where possible

ok jacekm@


# 1.9 15-Feb-2009 jacekm

If MX query fails due to DNS error, do not attempt more queries; ok gilles@


# 1.8 15-Feb-2009 jacekm

Fix a bug where list of 6 MXs or more was not handled correctly.
Pointed out by & ok sthen@, ok gilles@.


# 1.7 01-Jan-2009 jacekm

remove unnecessary includes; ok gilles@


# 1.6 05-Dec-2008 gilles

- more err/errx -> fatal/fatalx, warn/warnx -> log_warn/log_warnx
contains bits based on an old diff from Jacek Masiulaniec and
other bits from me.


# 1.5 01-Dec-2008 gilles

- in mxsort, fix type of loop counter, it will never be < 0 if it is
unsigned and when running out of luck it will cause the lookup
process to crash.


# 1.4 25-Nov-2008 gilles

- move prototype to smtpd.h


# 1.3 10-Nov-2008 tedu

insertion sort is faster than bubble sort. ok gilles


# 1.2 05-Nov-2008 sobrado

add a few missing id tags; there are a bunch of files, and developers
will probably miss this change when working on more important matters,
so it is probably better to sort them now. there is a risk of losing
the tags if a change needs to be reverted too.

written with excellent advice from jmc@

ok gilles@


# 1.1 01-Nov-2008 gilles

smtpd is a smtp server implementation for OpenBSD. It is a work in progress
which still lacks many features. bringing it in tree will help working on it
more easily.

"at this stage it should go in" henning@, "move ahead" deraadt@


# 1.85 06-Jan-2018 sunil

Move unpack functions into a seperate file.

Required for upcoming 'smtpctl spf walk'.
Ok eric@ gilles@ millert@


Revision tags: OPENBSD_6_2_BASE
# 1.84 31-May-2017 deraadt

typo; from Edgar Pettijohn


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.83 28-Oct-2015 gilles

dns_lookup_host() needs to remove brackets and IPv6: prefix when receiving
a text representation otherwise getaddrinfo_async() will choke

ok eric@


# 1.82 17-Oct-2015 gilles

remove unused variables


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 20-Jan-2015 deraadt

use <limits.h> comprehensively. For now try to push <> includes to
each .c file, and out of the .h files. To avoid overinclude.
ok gilles, in principle. If this has been done right, -portable should
become easier to maintain.


# 1.80 06-Jan-2015 gilles

fix whitespace and indentation, by Kyle Milz


# 1.79 08-Oct-2014 eric

restrict address lookups to configured address families.

ok gilles@


Revision tags: OPENBSD_5_6_BASE
# 1.78 19-Apr-2014 gilles

(void) cast this strlcpy(), it cannot truncate


# 1.77 19-Apr-2014 gilles

these strlcpy can't truncate, the copy is from a buffer to a buffer of same
size and the first buffer handles the truncation already


# 1.76 04-Apr-2014 eric

Merge the mda, mta and smtp processes into a single unprivileged
process managing message reception, delivery and transfer. Mostly
mechanical, but very intrusive as it required to rewamp all IMSG to
fix ambiguities.

with and ok gilles@


# 1.75 03-Apr-2014 eric

use asr helpers after libevent update.


# 1.74 26-Mar-2014 eric

asr API is now public


# 1.73 25-Mar-2014 eric

update after asr API update


# 1.72 25-Mar-2014 eric

Integrate necessary dns packet parsing helpers from asr.
They are not supposed to be exposed.

ok gilles@


# 1.71 14-Mar-2014 eric

field rename


Revision tags: OPENBSD_5_5_BASE
# 1.70 26-Dec-2013 eric

bcopy -> memmove
bzero -> memset


# 1.69 18-Nov-2013 eric

When looking up a MX, parse the address if the domain is a "[ipaddr]" string.


# 1.68 26-Oct-2013 eric

%i -> %d in format strings


Revision tags: OPENBSD_5_4_BASE
# 1.67 12-Jul-2013 eric

update after asr changes.


# 1.66 24-May-2013 eric

sync with OpenSMTPD 5.3.2

ok gilles@


# 1.65 30-Apr-2013 eric

remove params after API change.


Revision tags: OPENBSD_5_3_BASE
# 1.64 26-Jan-2013 gilles

Sync with our smtpd repo:

* first bricks of ldap and sqlite support (not finished but both working)
* new table API to replace map API, all lookups are done through tables
* improved handling of temporary errors throughout the daemon
* improved scheduler and mta logic: connection reuse, optimizes batches
* improved queue: more tolerant to admin errors, new layout, less disk-IO
* improved memory usage under high load
* SSL certs/keys isolated to lookup process to avoid facing network
* VIRTUAL support improved, fully virtual setups possible now
* runtime tracing of processes through smtpctl trace
* ssl_privsep.c sync-ed with relayd
* ssl.c no longer contains smtpd specific interfaces
* smtpd-specific ssl bits moved to ssl_smtpd.c
* update mail address in copyright

FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE. FLUSH YOUR QUEUE.

smtpd.conf(5) simplified, it will require adaptations

ok eric@


# 1.63 24-Nov-2012 eric

fix after asr update


# 1.62 23-Nov-2012 eric

knf

ok gilles@


# 1.61 12-Nov-2012 eric

Cleanups and improvements:

* Log more events (especially client session) and use a better scheme
for that: each messages is prefixed with a token to easily identify
its class:
- info/warn/debug: general server messages
- smtp-in: smtp client connections
- relay: status update for relayed messages
- delivery: status update for local deliveries

* Implement "smtpctl monitor" to display updates of selected internal
counters.

* When reloading the on-disk queue at startup do not commit a message
if no envelope was submitted for that message.

* Remove unused stuff in the config parser.

ok gilles@


# 1.60 08-Oct-2012 eric

skip RR if type is not MX. Use hostname if the list of MX is empty
after the loop.

spotted by huku at grhack.net

ok gilles@


# 1.59 03-Oct-2012 gilles

when requesting MX entries, the result can be appear in random orders.

the logic for inserting them in a lka session when acting as backup MX did
not take account for one specific case that could lead to an early exit
without smtpd getting a chance to detect the entry corrsponds to itself.

in such case, a backup MX woud try to connect to itself and bounce in the
loop detection code ... or it would sometimes work.


# 1.58 27-Sep-2012 chl

use xmalloc()/xcalloc() helpers

while there unify usage of log_trace() in ramstat_set()

ok gilles@ eric@


# 1.57 25-Aug-2012 gilles

- introduce struct stat_value
- statistics can now have a type (counter, timestamp, timeval, timespec and
possibly others in the future)
- stat_increment() / stat_decrement() now take an increment/decrement value
and are at the moment only of type counter
- stat_set() now takes a stat_value
- provide helpers to convert raw values to stat_value

ok eric@, ok chl@

while at it fix a rq_queue_dump() call using a bogus timestamp in scheduler
ramqueue.


# 1.56 21-Aug-2012 eric

Allow smtpd to work as a backup MX, relaying only to MXs with higher
priority in the DNS record. For example:

accept for domain "foo.org" relay backup "mx3.foo.org"

will relay mails for "foo.org" using only hosts with higher priority
(i.e. lower value) than "mx3.foo.org", which is supposed to be the
current server.

If the specified backup MX is not found in the DNS record, relaying
works as normal.

ok gilles@


# 1.55 21-Aug-2012 eric

Use TAILQ rather than array for mx list.

ok gilles@


# 1.54 21-Aug-2012 eric

dns sessions don't use lookup. no need to store them in a tree.

ok gilles@


# 1.53 19-Aug-2012 chl

coding style: replace all occurences of u_int* with uint*

ok eric@


# 1.52 18-Aug-2012 gilles

- introduce stat_backend, an API for pluggable statistic backends
> statistics are no longer static structures in shared memory
> statistics are only set, smtpd never uses them in its logic
> each statistic is a key/value where key can be any (dynamic) string
- convert all uses of the former API to use the new one
- implement stat_ramstat that keeps non-persistent stats in ram structure

ok eric@, ok chl@


# 1.51 08-Aug-2012 eric

cleanup some old debug traces

ok gilles@ chl@


# 1.50 29-Jul-2012 eric

fix bogus permfail when no MX is defined on a valid domain.

ok gilles@


Revision tags: OPENBSD_5_2_BASE
# 1.49 09-Jul-2012 eric

move to the new resolver implementation, with temporary glue to use
the relevant files from asr directly.

ok gilles@


# 1.48 14-Apr-2012 eric

improve readability

ok gilles@


Revision tags: OPENBSD_5_1_BASE
# 1.47 11-Jan-2012 eric

Try to parse hostnames as IP addresses before resolving. This allows
relays to be given as IP address in the config file.

ok gilles@


# 1.46 11-Jan-2012 eric

Improve error reporting. Most errors during hostname lookup are
now correctly reported as temporary failures.

from Nathanael Rensen, tweaks by me.


# 1.45 01-Sep-2011 eric

Introduce a small set of functions to manage stat counters in a
simpler and hopefully saner way.

ok gilles@ chl@


Revision tags: OPENBSD_5_0_BASE
# 1.44 20-Jul-2011 eric

Fix reporting of permanent/temporary failures for MX lookups.
Simplify code a bit while there.

ok gilles@


# 1.43 03-Jul-2011 nicm

imsg.h requires sys/queue.h and sys/uio.h.

ok eric


# 1.42 06-May-2011 eric

move dns session specific structs and prototypes out of smtpd.h.

ok gilles@


# 1.41 01-May-2011 eric

the smtpd env is meant to be global, so do not pass it all around.

discussed with and ok gilles@


# 1.40 17-Apr-2011 gilles

cleanups, cosmethic changes, functions that should be static are now static
no functionnal change


# 1.39 02-Apr-2011 eric

add stat counters for the lookup agent

ok gilles@


# 1.38 31-Mar-2011 eric

cleanup and simplification following the asr update.

- use a specific dispatch function for each type of query
- make the host handler work on a list of hosts by default (single host
queries are just a particular case) and use that to resolve the MX list
- various other code cleanup
- remove unused headers
- remove orphaned prototypes
- update copyright

ok gilles@


# 1.37 29-Mar-2011 eric

remove unused code now that reverse lookups are done through asr.

ok gilles@


# 1.36 29-Mar-2011 eric

make use the cname query interface from asr for reverse lookups

ok gilles@


# 1.35 27-Mar-2011 eric

add a function to factorize resetting of dnssession events.

ok gilles@


# 1.34 26-Mar-2011 eric

Fix the MX lookup process:

- the MX records were not always properly inserted into the sorted
array, which led to some MX being silently dropped.
- if an MX address could not be resolved, mail delivery would fail,
even though other valid MX exist for that domain. Now only report
the failure if no server address can be found at all.

grrrreeat gilles@


# 1.33 26-Mar-2011 eric

Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.

ok gilles@


# 1.32 26-Mar-2011 eric

use an index for iterating into the mx list.

ok gilles@


# 1.31 23-Mar-2011 eric

Simplify resolver initialization. This is done only once
since resolv.conf reloading is handled automatically by asr.

ok gilles@


# 1.30 09-Mar-2011 todd

make similar code .. more similar
ok gilles@

previous commit should have read:

when copying 'struct sockaddr' data, use sa_len not sizeof(struct sockaddr_in)
this fixes truncation of IPv6 addresses in the mail delivery path
ok gilles@


# 1.29 09-Mar-2011 todd

*** empty log message ***


Revision tags: OPENBSD_4_9_BASE
# 1.28 19-Dec-2010 gilles

If MX lookup fails, fallback to using the host itself. This has always been
the behavior but I introduced a regression when switching to ASR.

bug reported by jmc@, bugfix tested by jmc@ and I


# 1.27 12-Dec-2010 jsg

use memcpy instead of a cast/deref dance that was reading past
the end of the buffer.

tested by and ok gilles@


# 1.26 29-Nov-2010 gilles

replace the fork-based-non-blocking-resolver-hack by shiny async resolver
written by eric@. it is still experimental but still better than what we
had earlier so ... we'll improve in tree :)

diff by me with *lots* of help from eric@, tested by todd and I (and a
few people out there)


# 1.25 28-Nov-2010 gilles

remove unused headers


# 1.24 28-Nov-2010 gilles

a bit of .h cleanups, no functionnal change


# 1.23 08-Sep-2010 gilles

we do dns resolutions in a separate process because we don't have an async
resolver. if we run scarce on resources and we cannot fork a separate dns
process or we cannot socketpair() tell the caller that we have a temporary
failure rather than issueing a fatal(). message will stay in queue and be
rescheduled later ...

bug reported and bugfix tested by Sacha El Masry <lists@devilray.eu>


Revision tags: OPENBSD_4_8_BASE
# 1.22 29-Jun-2010 deraadt

force the dns buffers to be aligned using a union, until the retarded
"misalign strings on the stack" bug in gcc4 is fixed (even when that
is fixed this idiom is safer and quite common)
ok jacekm


# 1.21 02-Jun-2010 chl

check event_dispatch() return value

ok jacekm@


Revision tags: OPENBSD_4_7_BASE
# 1.20 14-Nov-2009 chl

add missing header needed by signal()

ok gilles@


# 1.19 11-Nov-2009 jacekm

Fix previous. When configured to relay via IP address, MX lookup would fail
(NXDOMAIN), leading to a bounce. Precede the MX lookup with an attempt to
parse the relay as numeric string.

"reads ok" gilles@


# 1.18 05-Nov-2009 jsing

Consider DNS lookups that result in NXDOMAIN to be a permanent failure.

ok gilles@ jacekm@


# 1.17 05-Nov-2009 jsing

Introduce a 6yz status code, used internally to report permanent errors.
The 1yz and 6yz status codes are now removed prior to reporting the status
message in bounce messages, which provides an easy way to distinguish
between local and remote status messages. Initial diff from jacekm@

ok gilles@ jacekm@


# 1.16 03-Sep-2009 jacekm

imsg_get sets errno so use fatal instead of fatalx.


# 1.15 08-Aug-2009 gilles

import some changes from portable smtpd to reduce the delta between both.
this commit contains mostly missing casts and cosmethic changes, do not
expect to build this anywhere but on OpenBSD, it does not contain any of
the portable glue.


Revision tags: OPENBSD_4_6_BASE
# 1.14 06-Jun-2009 pyr

Sync with relayd:
Stop pushing event handling in the imsg framework.
Instead, provide a small glue layer on top of both imsg and libevent.
This finally clearly separates event handling and imsg construction.

Sidetrack bonus: remove the mega-ugly hack of having a dummy imsg_event_add
stub in smtpctl.
ok jaceckm@


# 1.13 05-Jun-2009 pyr

make smtpd's imsg lib ready, just like relayd and ospfd.
ok gilles@, jacekm@


# 1.12 01-Jun-2009 jacekm

Fix EV_READ/EV_WRITE testing inside IMSG handlers. Based on similar change
to the routing daemons by claudio@; ok gilles@


# 1.11 09-May-2009 jacekm

- New API to handle all DNS query types (A, MX, PTR) asynchronously.

- Improve RFC compliance: CNAMEs are resolved, equal preference MXs
are randomized, relaying via MX that has equal/lower preference
than local server is prevented, decision on when to treat domain
name as implicit MX is better.

ok gilles@


Revision tags: OPENBSD_4_5_BASE
# 1.10 22-Feb-2009 form

replace MAX* constants by sizeof where possible

ok jacekm@


# 1.9 15-Feb-2009 jacekm

If MX query fails due to DNS error, do not attempt more queries; ok gilles@


# 1.8 15-Feb-2009 jacekm

Fix a bug where list of 6 MXs or more was not handled correctly.
Pointed out by & ok sthen@, ok gilles@.


# 1.7 01-Jan-2009 jacekm

remove unnecessary includes; ok gilles@


# 1.6 05-Dec-2008 gilles

- more err/errx -> fatal/fatalx, warn/warnx -> log_warn/log_warnx
contains bits based on an old diff from Jacek Masiulaniec and
other bits from me.


# 1.5 01-Dec-2008 gilles

- in mxsort, fix type of loop counter, it will never be < 0 if it is
unsigned and when running out of luck it will cause the lookup
process to crash.


# 1.4 25-Nov-2008 gilles

- move prototype to smtpd.h


# 1.3 10-Nov-2008 tedu

insertion sort is faster than bubble sort. ok gilles


# 1.2 05-Nov-2008 sobrado

add a few missing id tags; there are a bunch of files, and developers
will probably miss this change when working on more important matters,
so it is probably better to sort them now. there is a risk of losing
the tags if a change needs to be reverted too.

written with excellent advice from jmc@

ok gilles@


# 1.1 01-Nov-2008 gilles

smtpd is a smtp server implementation for OpenBSD. It is a work in progress
which still lacks many features. bringing it in tree will help working on it
more easily.

"at this stage it should go in" henning@, "move ahead" deraadt@