From 3751caa9a5d2a7a3669d089912e1504bafd6cf3f Mon Sep 17 00:00:00 2001 From: teru Date: Sun, 14 Aug 2016 13:03:32 +0900 Subject: [PATCH] Implement to generate configuration for postfix/dovecot #6 --- cmd/mailfull/command/genconfig.go | 67 ++++++++++++++ cmd/mailfull/main.go | 4 + generateconfig.go | 140 ++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 cmd/mailfull/command/genconfig.go create mode 100644 generateconfig.go diff --git a/cmd/mailfull/command/genconfig.go b/cmd/mailfull/command/genconfig.go new file mode 100644 index 0000000..e2a4b27 --- /dev/null +++ b/cmd/mailfull/command/genconfig.go @@ -0,0 +1,67 @@ +package command + +import ( + "fmt" + + "github.com/directorz/mailfull-go" +) + +// GenConfigCommand represents a GenConfigCommand. +type GenConfigCommand struct { + Meta +} + +// Synopsis returns a one-line synopsis. +func (c *GenConfigCommand) Synopsis() string { + return "Write a Postfix or Dovecot configuration to stdout." +} + +// Help returns long-form help text. +func (c *GenConfigCommand) Help() string { + txt := fmt.Sprintf(` +Usage: + %s %s name + +Description: + %s + +Required Args: + name + The software name that you want to generate a configuration. + Available names are "postfix" and "dovecot". +`, + c.CmdName, c.SubCmdName, + c.Synopsis()) + + return txt[1:] +} + +// Run runs the command and returns the exit status. +func (c *GenConfigCommand) Run(args []string) int { + if len(args) != 1 { + fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help()) + return 1 + } + + softwareName := args[0] + + repo, err := mailfull.OpenRepository(".") + if err != nil { + fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err) + return 1 + } + + switch softwareName { + case "postfix": + fmt.Fprintf(c.UI.Writer, "%s", repo.GenerateConfigPostfix()) + + case "dovecot": + fmt.Fprintf(c.UI.Writer, "%s", repo.GenerateConfigDovecot()) + + default: + fmt.Fprintf(c.UI.ErrorWriter, "[ERR] Specify \"postfix\" or \"dovecot\".\n") + return 1 + } + + return 0 +} diff --git a/cmd/mailfull/main.go b/cmd/mailfull/main.go index a0fd7a2..86c3d9a 100644 --- a/cmd/mailfull/main.go +++ b/cmd/mailfull/main.go @@ -43,6 +43,10 @@ func main() { meta.SubCmdName = c.Subcommand() return &command.InitCommand{Meta: meta}, nil }, + "genconfig": func() (cli.Command, error) { + meta.SubCmdName = c.Subcommand() + return &command.GenConfigCommand{Meta: meta}, nil + }, "domains": func() (cli.Command, error) { meta.SubCmdName = c.Subcommand() return &command.DomainsCommand{Meta: meta}, nil diff --git a/generateconfig.go b/generateconfig.go new file mode 100644 index 0000000..a2cc75f --- /dev/null +++ b/generateconfig.go @@ -0,0 +1,140 @@ +package mailfull + +import ( + "fmt" + "path/filepath" + "time" +) + +// GenerateConfigPostfix generate a configuration for Postfix. +func (r *Repository) GenerateConfigPostfix() string { + cfg := fmt.Sprintf(` +# +# Sample configuration: main.cf +# Generated by mailfull %s on %s +# + +#myhostname = host.example.com +mydomain = $myhostname +myorigin = $mydomain + +inet_interfaces = all +mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain +mynetworks_style = host +recipient_delimiter = - +message_size_limit = 10240000 +mailbox_size_limit = 51200000 +virtual_mailbox_limit = 51200000 + +virtual_mailbox_domains = hash:%s +virtual_mailbox_base = %s +virtual_mailbox_maps = hash:%s +virtual_uid_maps = static:%d +virtual_gid_maps = static:%d +virtual_alias_maps = hash:%s +transport_maps = regexp:%s +alias_maps = hash:/etc/aliases, hash:%s +alias_database = hash:/etc/aliases, hash:%s + +smtpd_sasl_auth_enable = yes +smtpd_sasl_local_domain = $myhostname +smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination +smtpd_sasl_type = dovecot +smtpd_sasl_path = private/auth + +smtpd_tls_cert_file = /etc/pki/dovecot/certs/dovecot.pem +smtpd_tls_key_file = /etc/pki/dovecot/private/dovecot.pem +#smtpd_tls_CAfile = +smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache +smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3 + +smtp_tls_security_level = may +smtp_tls_loglevel = 1 +smtpd_tls_security_level = may +smtpd_tls_loglevel = 1 +`, + Version, time.Now().Format(time.RFC3339), + filepath.Join(r.DirDatabasePath, FileNameDbDomains), + r.DirMailDataPath, + filepath.Join(r.DirDatabasePath, FileNameDbMaildirs), + r.uid, + r.gid, + filepath.Join(r.DirDatabasePath, FileNameDbDestinations), + filepath.Join(r.DirDatabasePath, FileNameDbLocaltable), + filepath.Join(r.DirDatabasePath, FileNameDbForwards), + filepath.Join(r.DirDatabasePath, FileNameDbForwards), + ) + + return cfg[1:] +} + +// GenerateConfigDovecot generate a configuration for Dovecot. +func (r *Repository) GenerateConfigDovecot() string { + cfg := fmt.Sprintf(` +# +# Sample configuration: dovecot.conf +# Generated by mailfull %s on %s +# + +protocols = imap pop3 +auth_mechanisms = plain login +mail_location = maildir:~/Maildir + +ssl = yes +ssl_cert =