Adding code that was mostly reviewed privately

Most of this is unchanged from what was already reviewed except for the testing code.

I replaced test_mocks.c with test.{h,c} and stopped reading mock passwd/group data from the testdata dir. The mock users and groups are hard-coded now, but it's a lot cleaner.