module-sshd/manifests/ssh_authorized_key.pp

86 lines
2.5 KiB
ObjectPascal
Raw Normal View History

2009-09-29 19:53:04 +02:00
# wrapper to have some defaults.
define sshd::ssh_authorized_key(
$ensure = 'present',
2009-09-29 19:53:04 +02:00
$type = 'ssh-dss',
2009-12-18 19:06:43 +01:00
$key = 'absent',
$user = '',
$target = undef,
$options = 'absent',
$override_builtin = undef
2009-09-29 19:53:04 +02:00
){
2009-12-18 19:06:43 +01:00
if ($ensure=='present') and ($key=='absent') {
fail("You have to set \$key for Sshd::Ssh_authorized_key[${name}]!")
}
$real_user = $user ? {
2013-02-03 00:30:54 +01:00
false => $name,
'' => $name,
default => $user,
}
case $target {
2009-12-10 23:34:57 +01:00
undef,'': {
case $real_user {
'root': { $real_target = '/root/.ssh/authorized_keys' }
default: { $real_target = "/home/${real_user}/.ssh/authorized_keys" }
}
2009-09-29 19:53:04 +02:00
}
default: {
$real_target = $target
2009-09-29 19:53:04 +02:00
}
}
2009-09-29 19:53:04 +02:00
# The ssh_authorized_key built-in function (in 2.7.23 at least)
# will not write an authorized_keys file for a mortal user to
# a directory they don't have write permission to, puppet attempts to
# create the file as the user specified with the user parameter and fails.
# Since ssh will refuse to use authorized_keys files not owned by the
# user, or in files/directories that allow other users to write, this
# behavior is deliberate in order to prevent typical non-working
# configurations. However, it also prevents the case of puppet, running
# as root, writing a file owned by a mortal user to a common
# authorized_keys directory such as one might specify in sshd_config with
# something like
# 'AuthorizedKeysFile /etc/ssh/authorized_keys/%u'
# So we provide a way to override the built-in and instead just install
# via a file resource. There is no additional security risk here, it's
# nothing a user can't already do by writing their own file resources,
# we still depend on the filesystem permissions to keep things safe.
if $override_builtin {
2015-05-21 19:19:40 +02:00
$header = "# HEADER: This file is managed by Puppet.\n"
2015-05-21 19:19:40 +02:00
if $options == 'absent' {
info("not setting any option for ssh_authorized_key: ${name}")
$content = "${header}${type} ${key}\n"
2015-05-21 19:19:40 +02:00
} else {
$content = "${header}${options} ${type} ${key}\n"
2015-05-21 19:19:40 +02:00
}
file { $real_target:
ensure => $ensure,
content => $content,
owner => $real_user,
mode => '0600',
}
2015-05-21 19:19:40 +02:00
} else {
2015-05-21 19:19:40 +02:00
if $options == 'absent' {
info("not setting any option for ssh_authorized_key: ${name}")
} else {
$real_options = $options
}
2015-05-21 19:19:40 +02:00
ssh_authorized_key{$name:
ensure => $ensure,
type => $type,
key => $key,
user => $real_user,
target => $real_target,
options => $real_options,
}
}
2015-05-21 19:19:40 +02:00
2009-09-29 19:53:04 +02:00
}