1
0
mirror of https://github.com/directorz/mailfull-go.git synced 2025-12-17 17:47:04 +00:00

Implement to generate configuration for postfix/dovecot #6

This commit is contained in:
teru
2016-08-14 13:03:32 +09:00
parent cd1101d096
commit 3751caa9a5
3 changed files with 211 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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

140
generateconfig.go Normal file
View File

@@ -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 = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem
#ssl_ca =
disable_plaintext_auth = yes
service imap-login {
inet_listener imap {
port = 143
}
inet_listener imaps {
port = 993
ssl = yes
}
}
service pop3-login {
inet_listener pop3 {
port = 110
}
inet_listener pop3s {
port = 995
ssl = yes
}
}
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
}
passdb {
driver = passwd-file
args = %s
}
userdb {
driver = static
args = uid=%d gid=%d home=%s/%%d/%%n
}
passdb {
driver = pam
}
userdb {
driver = passwd
}
`,
Version, time.Now().Format(time.RFC3339),
filepath.Join(r.DirDatabasePath, FileNameDbPasswords),
r.uid, r.gid, r.DirMailDataPath,
)
return cfg[1:]
}