2015-06-08 21:54:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-06-13 01:56:34 +00:00
|
|
|
"io/ioutil"
|
2015-06-08 21:54:15 +00:00
|
|
|
"os"
|
2015-06-13 01:56:34 +00:00
|
|
|
"path"
|
2015-06-08 21:54:15 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/xenolf/lego/acme"
|
|
|
|
)
|
|
|
|
|
|
|
|
func checkFolder(path string) error {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return os.MkdirAll(path, 0700)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(c *cli.Context) {
|
2015-06-12 21:34:49 +00:00
|
|
|
err := checkFolder(c.GlobalString("path"))
|
2015-06-08 21:54:15 +00:00
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("Cound not check/create path: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := NewConfiguration(c)
|
|
|
|
|
|
|
|
//TODO: move to account struct? Currently MUST pass email.
|
|
|
|
if !c.GlobalIsSet("email") {
|
|
|
|
logger().Fatal("You have to pass an account (email address) to the program using --email or -m")
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := NewAccount(c.GlobalString("email"), conf)
|
2015-06-13 01:55:53 +00:00
|
|
|
client := acme.NewClient(c.GlobalString("server"), acc, conf.RsaBits(), conf.OptPort())
|
2015-06-08 21:54:15 +00:00
|
|
|
if acc.Registration == nil {
|
|
|
|
reg, err := client.Register()
|
|
|
|
if err != nil {
|
2015-09-26 17:45:52 +00:00
|
|
|
logger().Fatalf("Could not complete registration\n\t%v", err)
|
2015-06-08 21:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.Registration = reg
|
|
|
|
acc.Save()
|
|
|
|
|
|
|
|
logger().Print("!!!! HEADS UP !!!!")
|
|
|
|
logger().Printf(`
|
|
|
|
Your account credentials have been saved in your Let's Encrypt
|
|
|
|
configuration directory at "%s".
|
|
|
|
You should make a secure backup of this folder now. This
|
|
|
|
configuration directory will also contain certificates and
|
|
|
|
private keys obtained from Let's Encrypt so making regular
|
2015-09-26 17:45:52 +00:00
|
|
|
backups of this folder is ideal.`, c.GlobalString("config-dir"))
|
2015-06-08 21:54:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if acc.Registration.Body.Agreement == "" {
|
|
|
|
if !c.GlobalBool("agree-tos") {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
logger().Printf("Please review the TOS at %s", acc.Registration.TosURL)
|
|
|
|
|
|
|
|
for {
|
|
|
|
logger().Println("Do you accept the TOS? Y/n")
|
|
|
|
text, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("Could not read from console -> %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
text = strings.Trim(text, "\r\n")
|
|
|
|
|
|
|
|
if text == "n" {
|
|
|
|
logger().Fatal("You did not accept the TOS. Unable to proceed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if text == "Y" || text == "y" || text == "" {
|
|
|
|
err = client.AgreeToTos()
|
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("Could not agree to tos -> %v", err)
|
|
|
|
}
|
|
|
|
acc.Save()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
logger().Println("Your input was invalid. Please answer with one of Y/y, n or by pressing enter.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.GlobalIsSet("domains") {
|
|
|
|
logger().Fatal("Please specify --domains")
|
|
|
|
}
|
|
|
|
|
2015-06-13 01:56:34 +00:00
|
|
|
certs, err := client.ObtainCertificates(c.GlobalStringSlice("domains"))
|
|
|
|
if err != nil {
|
2015-09-26 17:45:52 +00:00
|
|
|
logger().Fatalf("Could not obtain certificates\n\t%v", err)
|
2015-06-13 01:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = checkFolder(conf.CertPath())
|
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("Cound not check/create path: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, certRes := range certs {
|
|
|
|
certOut := path.Join(conf.CertPath(), certRes.Domain+".crt")
|
|
|
|
privOut := path.Join(conf.CertPath(), certRes.Domain+".key")
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(certOut, certRes.Certificate, 0700)
|
|
|
|
if err != nil {
|
2015-09-26 17:45:52 +00:00
|
|
|
logger().Printf("Unable to save Certificate for domain %s\n\t%v", certRes.Domain, err)
|
2015-06-13 01:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(privOut, certRes.PrivateKey, 0700)
|
|
|
|
if err != nil {
|
2015-09-26 17:45:52 +00:00
|
|
|
logger().Printf("Unable to save PrivateKey for domain %s\n\t%v", certRes.Domain, err)
|
2015-06-13 01:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-06-08 21:54:15 +00:00
|
|
|
}
|