def modify_ssh_keys
authorized_keys_file = File.join(@homedir, ".ssh", "authorized_keys")
keys = Hash.new
@@MODIFY_SSH_KEY_MUTEX.synchronize do
File.open("/var/lock/oo-modify-ssh-keys", File::RDWR|File::CREAT, 0o0600) do | lock |
lock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
lock.flock(File::LOCK_EX)
begin
File.open(authorized_keys_file, File::RDWR|File::CREAT, 0o0440) do |file|
file.each_line do |line|
options, key_type, key, comment = line.split
keys[comment] = line.chomp
end
if block_given?
old_keys = keys.clone
yield keys
if old_keys != keys
file.seek(0, IO::SEEK_SET)
file.write(keys.values.join("\n")+"\n")
file.truncate(file.tell)
end
end
end
FileUtils.chown_R('root', @uuid, authorized_keys_file)
shellCmd("restorecon #{authorized_keys_file}")
ensure
lock.flock(File::LOCK_UN)
end
end
end
keys
end