Clear netlink receive queue after reading to avoid an overflow

tlsdated only reads its NETLINK_ROUTE socket until finding the first
RTM_NEWROUTE message. After that, it will sleep at least
wait_between_tries seconds before reading the socket again.

On subnets with many hosts (such as a large public WiFi), the kernel
can generate a _lot_ of route updates for IPv6 solicited-node multicast
addresses. If tlsdated does not read them fast enough, they will queue
up and eventually overflow the socket receive buffer. This results in an
ENOBUFS error which will cause the process to terminate itself.

This patch ensures that tlsdated always reads all available packets in
its netlink receive queue before proceeding. As we only care if there
have been any route updates at all during one read, the additional
packets can simply be discarded. This also makes sure that they will not
be read at a later date (and misinterpreted as current packets at a time
when there are no more route updates).

Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-by: Elly Jones <ellyjones@chromium.org>
[tlsdate] support minimum check interval.

On systems where routes come up and down a lot, tlsdated can end up checking the
date too often and thus loading the check host; support limiting it to a minimum
frequency of successful checks. Failing checks can still happen arbitrarily
often.

BUG=chromium-os:37184
TEST=Adhoc

Change-Id: Ief413e91fcce98ed6bfc6c44d844a50b650438e0
Signed-off-by: Elly Fong-Jones <ellyjones@chromium.org>
(cherry picked from commit 6a7ded4a1848d8eb50a181f898fa9330f4e3ba34)
[tlsdate] fix certificate time compensation

Fix the code that compensates for certificates whose time is in the future, to
prevent a situation where it's impossible to fetch a new time because our
current time is too old. There are two bugs:

1. We try to use the server's time too late, after we've already done cert
validation; we need to do it earlier (during the server hello).

2. We set the time on the wrong context - we need to set the time on the cert
store instead of just the context object, since when OpenSSL is setting up to
verify the cert, it copies the cert store's params instead of the SSL context's
params.

BUG=chromium-os:36738
TEST=adhoc
date -s '2012-01-01 00:00:00' && tlsdate -l -v -H clients3.google.com -p 443

Change-Id: Ie1b11a2e0ecdc2196b729f6d5483b53c54a1933d
Signed-off-by: Elly Fong-Jones <ellyjones@chromium.org>
(cherry picked from commit 87ee9c2c23eff1816adc069c3b41f84933779643)
Merge pull request #52 from elly/min-delay

[tlsdate] support minimum check interval.
Merge pull request #53 from elly/cert-time

[tlsdate] fix certificate time compensation; in theory - we want this ordering.
configure.ac: use dnl for comments

"#" are shell comments while "dnl" are m4 comments.  This way
they don't get written to the generated configure file.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
ignore more files

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
clean up clock_gettime check

Some C libraries (like uClibc or >=glibc-2.17) put clock_gettime into
-lc, so we don't want to force linking of -lrt when it is not needed.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Clear netlink receive queue after reading to avoid an overflow

tlsdated only reads its NETLINK_ROUTE socket until finding the first
RTM_NEWROUTE message. After that, it will sleep at least
wait_between_tries seconds before reading the socket again.

On subnets with many hosts (such as a large public WiFi), the kernel
can generate a _lot_ of route updates for IPv6 solicited-node multicast
addresses. If tlsdated does not read them fast enough, they will queue
up and eventually overflow the socket receive buffer. This results in an
ENOBUFS error which will cause the process to terminate itself.

This patch ensures that tlsdated always reads all available packets in
its netlink receive queue before proceeding. As we only care if there
have been any route updates at all during one read, the additional
packets can simply be discarded. This also makes sure that they will not
be read at a later date (and misinterpreted as current packets at a time
when there are no more route updates).

BUG=chromium-os:36426
TEST=Join GoogleGuest or a similar large network. strace tlsdated and
observe how it will not die despite occasional large bursts of netlink
packets.

Change-Id: Idbaf4be6d888c45256220055242b6f9b4d77bc93
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/38173
Reviewed-by: Elly Jones <ellyjones@chromium.org>

Conflicts:
	src/routeup.c
Merge pull request #56 from elly/netlink-drain

Clear netlink receive queue after reading to avoid an overflow
resolve merge issue
fix openssl const warning

Building against dev-libs/openssl-1.0.1c results in:

src/tlsdate-helper.c: In function 'check_san':
src/tlsdate-helper.c:523:22: warning: assignment discards 'const'
	qualifier from pointer target type [enabled by default]

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
make hardened compiler flags optional

First note: the default behavior is unchanged.

For people building tlsdate themselves with a known toolchain env, the
checked flags can be redundant, or even harmful.  A compile/link check
for PIE behavior for example might not catch the full supported status
for some architectures/C libraries.

Further, the current code starts by throwing away the user's compiler
settings which can also be harmful.  Those often times include settings
related to ABI or target cpu selection and ignoring them can produce an
unusable binary.

So add a flag that people (including distros) can leverage to bypass
all of the checks.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Merge pull request #57 from vapier/master

build clean ups (try 2)
add configure flags for setting droppriv user/group

In order to override the default user/group, you have to append your
CPPFLAGS before compiling.  Since you also have to pass along double
quotes, it can get ugly/messy fast in shell (due to the escaping).
Plus, the existing code always defines UNPRIV_USER to "nobody" which
leads to compile time warnings.

Add dedicated configure flags to make the interface simple for people
to control.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Merge pull request #58 from vapier/master

add configure flags for setting droppriv user/group
Add (not complete) files.