Commit 64e6ca0c authored by Mickaël Canévet's avatar Mickaël Canévet
Browse files

Merge pull request #28 from Roguelazer/add_canonical_maps

add support for generating canonical maps (Fixes #42)
parents ba2e54c0 2d8880c5
Loading
Loading
Loading
Loading
+54 −0
Original line number Diff line number Diff line
(*
Module: Postfix_Canonical
  Parses /etc/postfix/*canonical

Author: James Brown <jbrown@uber.com> based on work by Raphael Pinson <raphael.pinson@camptocamp.com>

About: Reference
  This lens tries to keep as close as possible to `man 5 canonical` where possible.

About: License
   This file is licenced under the LGPL v2+, like the rest of Augeas.

About: Lens Usage
   To be documented

About: Configuration files
   This lens applies to /etc/postfix/*canonical. See <filter>.

About: Examples
   The <Test_Postfix_Canonical> file contains various examples and tests.
*)

module Postfix_Canonical =

autoload xfm

(* Variable: space_or_eol_re *)
let space_or_eol_re = /([ \t]*\n)?[ \t]+/

(* View: space_or_eol *)
let space_or_eol (sep:regexp) (default:string) =
  del (space_or_eol_re? . sep . space_or_eol_re?) default 

(* View: word *)
let word = store /[A-Za-z0-9@\*.-]+/

(* View: comma *)
let comma = space_or_eol "," ", "

(* View: destination *)
let destination = [ label "destination" . word ]

(* View: record *)
let record = [ label "pattern" . word
     . space_or_eol Rx.space " " . destination
     . Util.eol ]

(* View: lns *)
let lns = (Util.empty | Util.comment | record)*

(* Variable: filter *)
let filter = incl "/etc/postfix/*canonical"

let xfm = transform lns filter
+26 −0
Original line number Diff line number Diff line
(*
Module: Test_Postfix_Canonical
  Provides unit tests and examples for the <Postfix_Canonical> lens.
*)

module Test_Postfix_Canonical =

(* View: conf *)
let conf = "# a comment
user@domain.com @domain
@otherdomain  @otherotherdomain
someuser  Full.Some.User
"

(* Test: Postfix_Canonical.lns *)
test Postfix_Canonical.lns get conf =
  { "#comment" = "a comment" }
  { "pattern" = "user@domain.com"
    { "destination" = "@domain" }
  }
  { "pattern" = "@otherdomain"
    { "destination" = "@otherotherdomain" }
  }
  { "pattern" = "someuser"
    { "destination" = "Full.Some.User" }
  }

manifests/canonical.pp

0 → 100644
+68 −0
Original line number Diff line number Diff line
#== Definition: postfix::canonical
#
#Manages content of the /etc/postfix/canonical map.
#
#Parameters:
#- *name*: name of address postfix will lookup. See canonical(5).
#- *destination*: where the emails will be delivered to. See canonical(5).
#- *ensure*: present/absent, defaults to present.
#
#Requires:
#- Class["postfix"]
#- Postfix::Hash["/etc/postfix/canonical"]
#- Postfix::Config["canonical_maps"] or Postfix::Config["sender_canonical_maps"] or Postfix::Config["recipient_canonical_maps"]
#- augeas
#
#Example usage:
#
#  node "toto.example.com" {
#
#    include postfix
#
#    postfix::hash { "/etc/postfix/recipient_canonical":
#      ensure => present,
#    }
#    postfix::config { "canonical_alias_maps":
#      value => "hash:/etc/postfix/recipient_canonical"
#    }
#    postfix::canonical {
#      "user@example.com":
#        file        => "/etc/postfix/recipient_canonical",
#        ensure      => present,
#        destination => "root";
#    }
#  }
#
define postfix::canonical (
  $destination,
  $nexthop='',
  $file='/etc/postfix/canonical',
  $ensure='present'
) {
  include postfix::augeas

  case $ensure {
    'present': {
      $changes = [
        "set pattern[. = '${name}'] '${name}'",
        "set pattern[. = '${name}']/destination '${destination}'",
      ]
    }

    'absent': {
      $changes = "rm pattern[. = '${name}']"
    }

    default: {
      fail("Wrong ensure value: ${ensure}")
    }
  }

  augeas {"Postfix canonical - ${name}":
    incl    => $file,
    lens    => 'Postfix_Canonical.lns',
    changes => $changes,
    require => [Package['postfix'], Augeas::Lens['postfix_canonical']],
    notify  => Exec["generate ${file}.db"],
  }
}