Skip to content

Commit

Permalink
Add FreeBSD support (#288)
Browse files Browse the repository at this point in the history
* Do not hardcode /etc/postfix

In order to bring support for FreeBSD, do not use the hardcoded
/etc/postfix path for the directory containing Postfix configuration:
FreeBSD ports are installed with a /usr/local/ prefix (by default) so
the configuration files on FreeBSD are stored in the
/usr/local/etc/postfix directory.

* Add support for FreeBSD

* Make it possible to tune the "root" group

FreeBSD does not have a "root" group.  The corresponding group is named
"wheel".

Allow to setup a custom "root_group", and adjust FreeBSD configuration
to set it to "wheel".

* Adjust the test suite so that it pass on FreeBSD

* Do not depend on $postfix::* before including postfix

Some resources parameters depend on the value of variable from the
postfix class.  Ensure these values are substitued only after including
postfix.

* Move default values from hiera to init.pp

These values are system-dependent, but this helps seeing the usual
default value when genering references.

* Move $manage_mailname parameter

* Explicitely mark internal classes as private

* Remove redundant postfix::params inclusion

Co-authored-by: Raphaël Pinson <[email protected]>

Co-authored-by: Raphaël Pinson <[email protected]>
  • Loading branch information
smortex and raphink authored Feb 3, 2021
1 parent 460dc7a commit 2d1906b
Show file tree
Hide file tree
Showing 22 changed files with 342 additions and 86 deletions.
8 changes: 8 additions & 0 deletions data/os/FreeBSD.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
postfix::confdir: "/usr/local/etc/postfix"
postfix::manage_mailname: false
postfix::manage_mailx: false
postfix::root_group: "wheel"
postfix::params::master_os_template: "postfix/master.cf.FreeBSD.erb"
postfix::params::restart_cmd: "/usr/local/etc/rc.d/postfix reload"
...
9 changes: 6 additions & 3 deletions manifests/canonical.pp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
#
define postfix::canonical (
$destination,
$file='/etc/postfix/canonical',
$file=undef,
$ensure='present'
) {
include postfix
include ::postfix::augeas

$_file = pick($file, "${postfix::confdir}/canonical")

case $ensure {
'present': {
$changes = [
Expand All @@ -58,10 +61,10 @@
}

augeas {"Postfix canonical - ${name}":
incl => $file,
incl => $_file,
lens => 'Postfix_Canonical.lns',
changes => $changes,
require => [Package['postfix'], Augeas::Lens['postfix_canonical']],
notify => Exec["generate ${file}.db"],
notify => Exec["generate ${_file}.db"],
}
}
8 changes: 5 additions & 3 deletions manifests/conffile.pp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@
Enum['present', 'absent', 'directory'] $ensure = 'present',
Variant[Array[String], String, Undef] $source = undef,
Optional[String] $content = undef,
Stdlib::Absolutepath $path = "/etc/postfix/${name}",
Optional[Stdlib::Absolutepath] $path = undef,
String $mode = '0640',
Hash $options = {},
Boolean $show_diff = true,
) {
include ::postfix::params
include postfix

$_path = pick($path, "${postfix::confdir}/${name}")

if (!defined(Class['postfix'])) {
fail 'You must define class postfix before using postfix::config!'
Expand Down Expand Up @@ -84,7 +86,7 @@

file { "postfix conffile ${name}":
ensure => $ensure,
path => $path,
path => $_path,
mode => $mode,
owner => 'root',
group => 'postfix',
Expand Down
5 changes: 3 additions & 2 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Optional[String] $value = undef,
Enum['present', 'absent', 'blank'] $ensure = 'present',
) {
include postfix

if ($ensure == 'present') {
assert_type(Pattern[/^.+$/], $value) |$e, $a| {
Expand Down Expand Up @@ -58,10 +59,10 @@
}

augeas { "manage postfix '${title}'":
incl => '/etc/postfix/main.cf',
incl => "${postfix::confdir}/main.cf",
lens => 'Postfix_Main.lns',
changes => $changes,
require => File['/etc/postfix/main.cf'],
require => File["${postfix::confdir}/main.cf"],
}

Postfix::Config[$title] ~> Class['postfix::service']
Expand Down
24 changes: 14 additions & 10 deletions manifests/files.pp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class postfix::files {
include ::postfix::params
assert_private()


$alias_maps = $postfix::all_alias_maps
$amavis_procs = $postfix::amavis_procs
Expand All @@ -18,6 +19,7 @@
$master_bounce_command = $postfix::master_bounce_command
$master_defer_command = $postfix::master_defer_command
$myorigin = $postfix::myorigin
$manage_mailname = $postfix::manage_mailname
$manage_aliases = $postfix::manage_aliases
$manage_root_alias = $postfix::manage_root_alias
$root_mail_recipient = $postfix::root_mail_recipient
Expand All @@ -41,11 +43,13 @@
replace => $manage_conffiles,
}

file { '/etc/mailname':
ensure => 'file',
content => "${::fqdn}\n",
mode => '0644',
seltype => $postfix::params::seltype,
if $manage_mailname {
file { '/etc/mailname':
ensure => 'file',
content => "${::fqdn}\n",
mode => '0644',
seltype => $postfix::params::seltype,
}
}

# Aliases
Expand Down Expand Up @@ -73,20 +77,20 @@
)
}

file { '/etc/postfix/master.cf':
file { "${postfix::confdir}/master.cf":
ensure => 'file',
content => $_mastercf_content,
group => 'root',
group => $postfix::root_group,
mode => '0644',
owner => 'root',
seltype => $postfix::params::seltype,
source => $mastercf_source,
}

# Config files
file { '/etc/postfix/main.cf':
file { "${postfix::confdir}/main.cf":
ensure => 'file',
group => 'root',
group => $postfix::root_group,
mode => '0644',
owner => 'root',
replace => false,
Expand Down
5 changes: 4 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
# }
#
class postfix (
Stdlib::Absolutepath $confdir = '/etc/postfix',
String $root_group = 'root',
String $alias_maps = 'hash:/etc/aliases',
Optional[Hash] $configs = {},
Integer $amavis_procs = 2,
Expand All @@ -106,6 +108,7 @@
Boolean $mailman = false,
String $maincf_source = "puppet:///modules/${module_name}/main.cf",
Boolean $manage_conffiles = true,
Boolean $manage_mailname = true,
Boolean $manage_mailx = true,
Optional[String] $mastercf_source = undef,
Optional[String] $mastercf_content = undef,
Expand Down Expand Up @@ -151,7 +154,7 @@

$all_alias_maps = $ldap ? {
false => $alias_maps,
true => "${alias_maps}, ldap:/etc/postfix/ldap-aliases.cf",
true => "${alias_maps}, ldap:${confdir}/ldap-aliases.cf",
}

create_resources('::postfix::config', $configs)
Expand Down
4 changes: 2 additions & 2 deletions manifests/ldap.pp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

if $::osfamily == 'Debian' {
package {'postfix-ldap':
before => File['/etc/postfix/ldap-aliases.cf'],
before => File["${postfix::confdir}/ldap-aliases.cf"],
}
}

Expand All @@ -39,7 +39,7 @@
default => $postfix::ldap_options,
}

file {'/etc/postfix/ldap-aliases.cf':
file {"${postfix::confdir}/ldap-aliases.cf":
ensure => 'file',
owner => 'root',
group => 'postfix',
Expand Down
9 changes: 5 additions & 4 deletions manifests/mailman.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@
# mailman => true,
# }
class postfix::mailman {
include postfix

postfix::config {
'virtual_alias_maps':
value => 'hash:/etc/postfix/virtual';
value => "hash:${postfix::confdir}/virtual";
'transport_maps':
value => 'hash:/etc/postfix/transport';
value => "hash:${postfix::confdir}/transport";
'mailman_destination_recipient_limit':
value => '1';
}

postfix::hash { '/etc/postfix/virtual':
postfix::hash { "${postfix::confdir}/virtual":
ensure => 'present',
}

postfix::hash { '/etc/postfix/transport':
postfix::hash { "${postfix::confdir}/transport":
ensure => 'present',
}

Expand Down
13 changes: 8 additions & 5 deletions manifests/map.pp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
Variant[Array[String], String, Undef] $source = undef,
Optional[Variant[Sensitive[String],String]] $content = undef,
String $type = 'hash',
Stdlib::Absolutepath $path = "/etc/postfix/${name}",
Optional[Stdlib::Absolutepath] $path = undef,
String[4,4] $mode = '0640'
) {
include postfix
include ::postfix::params

$_path = pick($path, "${postfix::confdir}/${name}")

if (!defined(Class['postfix'])) {
fail 'You must define class postfix before using postfix::config!'
}
Expand All @@ -58,7 +61,7 @@

file { "postfix map ${name}":
ensure => $ensure,
path => $path,
path => $_path,
source => $source,
content => $content,
owner => 'root',
Expand All @@ -71,7 +74,7 @@
if $type !~ /^(cidr|pcre)$/ {
file {"postfix map ${name}.db":
ensure => $ensure,
path => "${path}.db",
path => "${_path}.db",
owner => 'root',
group => 'postfix',
mode => $mode,
Expand All @@ -81,8 +84,8 @@
}

$generate_cmd = $ensure ? {
'absent' => "rm ${path}.db",
'present' => "postmap ${path}",
'absent' => "rm ${_path}.db",
'present' => "postmap ${_path}",
}

exec {"generate ${name}.db":
Expand Down
29 changes: 17 additions & 12 deletions manifests/mta.pp
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,41 @@
# }
#
class postfix::mta (
Pattern[/^\S+(?:,\s*\S+)*$/] $mydestination = $postfix::mydestination,
Pattern[/^(?:\S+?(?:(?:,\s+)|(?:\s+))?)*$/] $mynetworks = $postfix::mynetworks,
Pattern[/^\S+$/] $relayhost = $postfix::relayhost,
Optional[Pattern[/^\S+(?:,\s*\S+)*$/]] $mydestination = undef,
Optional[Pattern[/^(?:\S+?(?:(?:,\s+)|(?:\s+))?)*$/]] $mynetworks = undef,
Optional[Pattern[/^\S+$/]] $relayhost = undef,
) {
include postfix

$_mydestination = pick($mydestination, $postfix::mydestination)
$_mynetworks = pick($mynetworks, $postfix::mynetworks)
$_relayhost = pick($relayhost, $postfix::relayhost)

# If direct is specified then relayhost should be blank
if ($relayhost == 'direct') {
if ($_relayhost == 'direct') {
postfix::config { 'relayhost': ensure => 'blank' }
}
else {
postfix::config { 'relayhost': value => $relayhost }
postfix::config { 'relayhost': value => $_relayhost }
}

if ($mydestination == 'blank') {
if ($_mydestination == 'blank') {
postfix::config { 'mydestination': ensure => 'blank' }
} else {
postfix::config { 'mydestination': value => $mydestination }
postfix::config { 'mydestination': value => $_mydestination }
}

postfix::config {
'mynetworks': value => $mynetworks;
'virtual_alias_maps': value => 'hash:/etc/postfix/virtual';
'transport_maps': value => 'hash:/etc/postfix/transport';
'mynetworks': value => $_mynetworks;
'virtual_alias_maps': value => "hash:${postfix::confdir}/virtual";
'transport_maps': value => "hash:${postfix::confdir}/transport";
}

postfix::hash { '/etc/postfix/virtual':
postfix::hash { "${postfix::confdir}/virtual":
ensure => 'present',
}

postfix::hash { '/etc/postfix/transport':
postfix::hash { "${postfix::confdir}/transport":
ensure => 'present',
}

Expand Down
2 changes: 1 addition & 1 deletion manifests/packages.pp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class postfix::packages {
include ::postfix::params
assert_private()

package { 'postfix':
ensure => $postfix::postfix_ensure,
Expand Down
17 changes: 11 additions & 6 deletions manifests/satellite.pp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@
# }
#
class postfix::satellite (
$mydestination = $postfix::mydestination,
$mynetworks = $postfix::mynetworks,
$relayhost = $postfix::relayhost,
$mydestination = undef,
$mynetworks = undef,
$relayhost = undef,
) {
include postfix

assert_type(Pattern[/^\S+$/], $postfix::myorigin)

$_mydestination = pick($mydestination, $postfix::mydestination)
$_mynetworks = pick($mynetworks, $postfix::mynetworks)
$_relayhost = pick($relayhost, $postfix::relayhost)

class { '::postfix::mta':
mydestination => $mydestination,
mynetworks => $mynetworks,
relayhost => $relayhost,
mydestination => $_mydestination,
mynetworks => $_mynetworks,
relayhost => $_relayhost,
}

postfix::virtual { "@${postfix::myorigin}":
Expand Down
1 change: 1 addition & 0 deletions manifests/service.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class postfix::service {
assert_private()

$manage_aliases = $postfix::manage_aliases

Expand Down
11 changes: 7 additions & 4 deletions manifests/transport.pp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
define postfix::transport (
Optional[String] $destination = undef,
Optional[String] $nexthop=undef,
Stdlib::Absolutepath $file='/etc/postfix/transport',
Optional[Stdlib::Absolutepath] $file=undef,
Enum['present', 'absent'] $ensure='present'
) {
include postfix
include ::postfix::augeas

$_file = pick($file, "${postfix::confdir}/transport")

$smtp_nexthop = (String($nexthop) =~ /\[.*\]/)

case $ensure {
Expand Down Expand Up @@ -104,7 +107,7 @@

augeas {"Postfix transport - ${name}":
lens => 'Postfix_Transport.lns',
incl => $file,
incl => $_file,
changes => $changes,
require => Augeas::Lens['postfix_transport'],
}
Expand All @@ -113,7 +116,7 @@
Package['postfix'] -> Postfix::Transport[$title]
}

if defined(Postfix::Hash[$file]) {
Postfix::Transport[$title] ~> Postfix::Hash[$file]
if defined(Postfix::Hash[$_file]) {
Postfix::Transport[$title] ~> Postfix::Hash[$_file]
}
}
Loading

0 comments on commit 2d1906b

Please sign in to comment.