Implemented safe_open() in order to avoid symlink race conditions.

It makes sure that we do not follow unsafe symlinks, that is a link
that belongs to a user, but has a target owned by a different user.
It does this in a way that avoids race conditions (between testing if
a file is a link, and opening that file, there is a small window of
opportunity).

Also implements safe_chdir(), to also safeguard chdir() operations.

Fixes issue #3693.
(cherry picked from commit 995b9d18d750713cd40ce0203550a45646a53cd0)

Conflicts:

	cf-agent/files_editline.c
	cf-agent/verify_files.c
	cf-agent/verify_packages.c
	cf-execd/cf-execd-runner.c
	cf-key/cf-key-functions.c
	cf-serverd/server_common.c
	configure.ac
	libcfnet/client_code.c
	libpromises/cf3lex.l
	libpromises/env_context.c
	libpromises/parser.c
	libpromises/pipes_unix.c
	libpromises/sysinfo.c
	libpromises/verify_reports.c
	libutils/hashes.c
	tests/unit/Makefile.am
Merge pull request #1116 from kacfengine/safe_open_3.5.x

[3.5.x] Implemented safe_open() in order to avoid symlink race conditions.
Work around fdopendir() missing on some platforms.
(cherry picked from commit 19be8b73b5ee94b890ca09ccf7346ea923b36c1c)
Merge pull request #1118 from kacfengine/safe_open_3.5.x

[3.5.x] Work around fdopendir() missing on some platforms.
Add missing header.
Merge pull request #1122 from kacfengine/safe_open_3.5.x

Add missing header.
Don't use O_DIRECTORY flag, which is Linux specific.

Directories will fail when we try to chdir into them anyway, so all
we lose is the early warning.
(cherry picked from commit fd0488f19bc8325bbb51171dd6fc69b074d2b51c)
Switch back to test folder after each test.

Otherwise the test framework may try to write into the wrong
directory.
(cherry picked from commit 7828a2ad7a4efb1799b8c9208c33e03f3af83aea)
Merge pull request #1124 from kacfengine/safe_open_3.5.x

[3.5.x] Some fixes for the safe_open() function.
Don't use GetErrorStrFromCode(), which is not defined in 3.5.x.
Merge pull request #1126 from kacfengine/safe_open_3.5.x

[3.5.x] Don't use GetErrorStrFromCode(), which is not defined in 3.5.x.
Pre-release version bump
Merge branch '3.5.x' of github.com:cfengine/core into 3.5.x