// Copyright (c) 2022 Lukasz Janyst // // This file is part of Peroxide. // // Peroxide is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Peroxide is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Peroxide. If not, see . package main import ( "flag" "fmt" "io/ioutil" "os" "text/template" "github.com/ghodss/yaml" "github.com/google/uuid" ) type Config struct { UUID string Identifier string CardDAV []DAV CalDAV []DAV Email []Email } type DAV struct { AccountDescription string HostName string Username string Password string UseSSL bool Port int PrincipalURL string UUID string Identifier string } type Email struct { AccountName string AccountDescription string Address string IncomingHostName string IncomingPortNumber int IncomingUsername string IncomingPassword string IncomingUseSSL bool OutgoingHostName string OutgoingPortNumber int OutgoingUsername string OutgoingPassword string OutgoingUseSSL bool UUID string Identifier string } var configTmpl = ` PayloadVersion 1 PayloadUUID {{ .UUID }} PayloadType Configuration PayloadIdentifier {{ .Identifier }} Label Account Configuration PayloadContent {{ range .CardDAV }} CardDAVAccountDescription {{ .AccountDescription }} CardDAVHostName {{ .HostName }} CardDAVPrincipalURL {{ .PrincipalURL }} CardDAVUsername {{ .Username }} CardDAVPassword {{ .Password }} CardDAVUseSSL <{{ .UseSSL }} /> CardDAVPort {{ .Port }} PayloadVersion 1 PayloadType com.apple.carddav.account PayloadIdentifier {{ .Identifier }} PayloadUUID {{ .UUID }} PayloadOrganization A nice company PayloadDescription Configures CardDAV account {{ end }} {{ range .CalDAV }} CalDAVAccountDescription {{ .AccountDescription }} CalDAVHostName {{ .HostName }} CalDAVPrincipalURL {{ .PrincipalURL }} CalDAVUsername {{ .Username }} CalDAVPassword {{ .Password }} CalDAVUseSSL <{{ .UseSSL }} /> CalDAVPort {{ .Port }} PayloadVersion 1 PayloadType com.apple.caldav.account PayloadIdentifier {{ .Identifier }} PayloadUUID {{ .UUID }} PayloadOrganization A nice company PayloadDescription Configures CalDAV account {{ end }} {{ range .Email }} EmailAccountDescription {{ .AccountDescription }} EmailAccountName {{ .AccountName }} EmailAccountType EmailTypeIMAP EmailAddress {{ .Address }} IncomingMailServerAuthentication EmailAuthPassword IncomingMailServerHostName {{ .IncomingHostName }} IncomingMailServerPortNumber {{ .IncomingPortNumber }} IncomingMailServerUsername {{ .IncomingUsername }} IncomingPassword {{ .IncomingPassword }} IncomingMailServerUseSSL <{{ .IncomingUseSSL }} /> IncomingMailServerIMAPPathPrefix OutgoingMailServerAuthentication EmailAuthPassword OutgoingMailServerHostName {{ .OutgoingHostName }} OutgoingMailServerPortNumber {{ .OutgoingPortNumber }} OutgoingMailServerUsername {{ .OutgoingUsername }} OutgoingPassword {{ .OutgoingPassword }} OutgoingMailServerUseSSL <{{ .OutgoingUseSSL }} /> PayloadVersion 1 PayloadType com.apple.mail.managed PayloadIdentifier {{ .Identifier }} PayloadUUID {{ .UUID }} PayloadOrganization A nice company PayloadDescription Configures an Email account {{ end }} ` var out = flag.String("out", "account.mobileconfig", "output config") var in = flag.String("in", "account.json", "configuration data") func main() { flag.Parse() templ := template.Must(template.New("mobileconfig").Parse(configTmpl)) outFile, err := os.OpenFile(*out, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) if err != nil { fmt.Printf("Cannot open the output file for writing: %s\n", err) os.Exit(1) } defer outFile.Close() data, err := ioutil.ReadFile(*in) if err != nil { fmt.Printf("Cannot read the configuration data: %s\n", err) os.Exit(1) } var cfg Config if err := yaml.Unmarshal(data, &cfg); err != nil { fmt.Printf("Cannot unmarshal the configuration data: %s\n", err) os.Exit(1) } cfg.UUID = uuid.New().String() for i := 0; i < len(cfg.CardDAV); i++ { cfg.CardDAV[i].UUID = uuid.New().String() cfg.CardDAV[i].Identifier = uuid.New().String() } for i := 0; i < len(cfg.CalDAV); i++ { cfg.CalDAV[i].UUID = uuid.New().String() cfg.CalDAV[i].Identifier = uuid.New().String() } for i := 0; i < len(cfg.Email); i++ { cfg.Email[i].UUID = uuid.New().String() cfg.Email[i].Identifier = uuid.New().String() } if cfg.Identifier == "" { cfg.Identifier = "com.example.account" } if err := templ.Execute(outFile, cfg); err != nil { fmt.Printf("Cannot execute the config template: %s\n", err) os.Exit(1) } }