core: Avoid bus errors and sigsegvs on weird configurations

When servicedependencies are created with dependent_hostgroup_name
and/or hostgroup_name and the groups mentioned there are empty, we
could previously bomb out with an illegal memory access when verifying
cyclic dependency paths.

With this patch, we provide belts, suspenders, helmet and condoms to
protect against such mishaps. Firstly, we set object tables created
with zero elements to NULL instead of just returning blindly, thereby
avoiding memory taints.

We also iterate using the object count and the static table rather
than list pointers, avoiding memory taints (but not counting errors),
and finally we make sure the dependency type is within the valid
range before we attempt to assign an array for checking the invalid
dependency.

Many, many thanks to Daniel Wittenberg who was invaluable in tracking
down and debugging this problem.

Signed-off-by: Andreas Ericsson <ae@op5.se>
xdata/xodtemplate: Add some missing error messages

It appears as though processing group names could earlier lead to
errors when '*groups' directives were null (and void) in member
directives, and we didn't report where the error was, which is
very unfriendly, since users may have fairly huge configurations
to dig through to find the error.

This patch adds the missing error messages.

Again, many thanks to Daniel Wittenberg for assistance in debugging
this. He's now a certified gdb virtuoso ;)

Signed-off-by: Andreas Ericsson <ae@op5.se>
core: Fix a small compiler warning

When we have gnu getopt and are compiling with -ansi, we would get a
warning about declaration after statement. We fix that here, and
reduce the loc-count a little with a simple getopt() macro that
wraps getoptlong if it's available. This reduces the possibilities
of maintenance errors somewhat as there's only one optstring to take
care of.

Signed-off-by: Andreas Ericsson <ae@op5.se>
core: Make precaching work without -v or -s

The two execution paths for scheduling tests and config verification
are almost identical in setup, so by merging them and joining the
precache option path to them, we can get precaching to work without
having to swim through additional output. We will get error messages
though, and now we don't have to duplicate them for either of the
codepaths.

Signed-off-by: Andreas Ericsson <ae@op5.se>
xdata/xodtemplate: Unify slave object free()ing

Most slave objects (escalations and dependencies) had their variables
free()'d at the end, but it appears we free()'d the hostescalation
host_name and hostgroup_name twice, and we left a lot of dead memory
lying around when we didn't need to. With this patch, we unify the
logic and are once again free from "invalid free() / delete[]" from
valgrind.

Signed-off-by: Andreas Ericsson <ae@op5.se>
xdata/xodtemplate: Fix typo s/contactgroup/hostgroup

It's odd when you call functionlike macros with wrong arguments and
happen to end up getting it right when you do it wrong. This was one
of those times.

Signed-off-by: Andreas Ericsson <ae@op5.se>
common/objects: Don't copy "display_name" unnecessarily

In large configs this will be a huge waste of memory, so let's avoid
it whenever possible.

Signed-off-by: Andreas Ericsson <ae@op5.se>