Fix manage agents

Create or truncate the keys file and set permissions on the file before
writing the key data to the file. This should help prevent any possible
race conditions and is just the right way to do this. Doing things this
way also means that all of the unlink() on permission setting failures
can go away. Previously, when the key was written before the permissions
were set, if there was an error setting permissions afterward the key
file would get deleted to prevent unwanted access.
Maybe checking the return value and running chown(KEYS_FILE, 0, 0) previous?