2016-05-04 20:53:48 +02:00
|
|
|
// Copyright (C) 2016 Opsmate, Inc.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License, v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// This software is distributed WITHOUT A WARRANTY OF ANY KIND.
|
|
|
|
// See the Mozilla Public License for details.
|
|
|
|
|
2016-05-04 20:49:07 +02:00
|
|
|
package certspotter
|
2016-02-05 03:45:37 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-07-28 20:55:46 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-02-05 03:45:37 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
2016-07-28 20:55:46 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-02-05 03:45:37 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-07-28 20:55:46 +02:00
|
|
|
"time"
|
2016-02-05 03:45:37 +01:00
|
|
|
|
2016-05-04 21:19:59 +02:00
|
|
|
"software.sslmate.com/src/certspotter/ct"
|
2016-02-05 03:45:37 +01:00
|
|
|
)
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func ReadSTHFile(path string) (*ct.SignedTreeHead, error) {
|
2016-02-05 03:45:37 +01:00
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2016-02-17 23:54:25 +01:00
|
|
|
return nil, nil
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
2016-02-17 23:54:25 +01:00
|
|
|
return nil, err
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 23:54:25 +01:00
|
|
|
var sth ct.SignedTreeHead
|
|
|
|
if err := json.Unmarshal(content, &sth); err != nil {
|
|
|
|
return nil, err
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 23:54:25 +01:00
|
|
|
return &sth, nil
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func WriteSTHFile(path string, sth *ct.SignedTreeHead) error {
|
2016-02-17 23:54:25 +01:00
|
|
|
sthJson, err := json.MarshalIndent(sth, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sthJson = append(sthJson, byte('\n'))
|
|
|
|
return ioutil.WriteFile(path, sthJson, 0666)
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func WriteProofFile(path string, proof ct.ConsistencyProof) error {
|
2016-04-06 17:10:06 +02:00
|
|
|
proofJson, err := json.MarshalIndent(proof, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
proofJson = append(proofJson, byte('\n'))
|
|
|
|
return ioutil.WriteFile(path, proofJson, 0666)
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func IsPrecert(entry *ct.LogEntry) bool {
|
2016-03-17 00:58:00 +01:00
|
|
|
return entry.Leaf.TimestampedEntry.EntryType == ct.PrecertLogEntryType
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func GetFullChain(entry *ct.LogEntry) [][]byte {
|
|
|
|
certs := make([][]byte, 0, len(entry.Chain)+1)
|
2016-03-17 00:58:00 +01:00
|
|
|
|
|
|
|
if entry.Leaf.TimestampedEntry.EntryType == ct.X509LogEntryType {
|
|
|
|
certs = append(certs, entry.Leaf.TimestampedEntry.X509Entry)
|
|
|
|
}
|
|
|
|
for _, cert := range entry.Chain {
|
|
|
|
certs = append(certs, cert)
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
2016-03-17 00:58:00 +01:00
|
|
|
|
|
|
|
return certs
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func formatSerialNumber(serial *big.Int) string {
|
2016-03-17 00:58:00 +01:00
|
|
|
if serial != nil {
|
|
|
|
return fmt.Sprintf("%x", serial)
|
|
|
|
} else {
|
|
|
|
return ""
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func sha256sum(data []byte) []byte {
|
2016-03-17 00:58:00 +01:00
|
|
|
sum := sha256.Sum256(data)
|
2016-03-27 03:04:22 +02:00
|
|
|
return sum[:]
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func sha256hex(data []byte) string {
|
2016-03-27 03:04:22 +02:00
|
|
|
return hex.EncodeToString(sha256sum(data))
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
type EntryInfo struct {
|
2016-07-28 20:55:46 +02:00
|
|
|
LogUri string
|
|
|
|
Entry *ct.LogEntry
|
|
|
|
IsPrecert bool
|
|
|
|
FullChain [][]byte // first entry is logged X509 cert or pre-cert
|
|
|
|
CertInfo *CertInfo
|
|
|
|
ParseError error // set iff CertInfo is nil
|
|
|
|
Identifiers *Identifiers
|
|
|
|
IdentifiersParseError error
|
|
|
|
Filename string
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
type CertInfo struct {
|
2016-07-28 20:55:46 +02:00
|
|
|
TBS *TBSCertificate
|
|
|
|
|
|
|
|
Subject RDNSequence
|
|
|
|
SubjectParseError error
|
|
|
|
Issuer RDNSequence
|
|
|
|
IssuerParseError error
|
|
|
|
SANs []SubjectAltName
|
|
|
|
SANsParseError error
|
|
|
|
SerialNumber *big.Int
|
|
|
|
SerialNumberParseError error
|
|
|
|
Validity *CertValidity
|
|
|
|
ValidityParseError error
|
|
|
|
IsCA *bool
|
|
|
|
IsCAParseError error
|
2016-03-17 00:58:00 +01:00
|
|
|
}
|
2016-02-05 03:45:37 +01:00
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MakeCertInfoFromTBS(tbs *TBSCertificate) *CertInfo {
|
2016-03-17 00:58:00 +01:00
|
|
|
info := &CertInfo{TBS: tbs}
|
2016-02-05 03:45:37 +01:00
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
info.Subject, info.SubjectParseError = tbs.ParseSubject()
|
|
|
|
info.Issuer, info.IssuerParseError = tbs.ParseIssuer()
|
2016-05-02 20:59:55 +02:00
|
|
|
info.SANs, info.SANsParseError = tbs.ParseSubjectAltNames()
|
2016-03-17 00:58:00 +01:00
|
|
|
info.SerialNumber, info.SerialNumberParseError = tbs.ParseSerialNumber()
|
|
|
|
info.Validity, info.ValidityParseError = tbs.ParseValidity()
|
2016-04-27 03:06:59 +02:00
|
|
|
info.IsCA, info.IsCAParseError = tbs.ParseBasicConstraints()
|
2016-02-05 03:45:37 +01:00
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
return info
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MakeCertInfoFromRawTBS(tbsBytes []byte) (*CertInfo, error) {
|
2016-03-17 00:58:00 +01:00
|
|
|
tbs, err := ParseTBSCertificate(tbsBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
2016-03-17 00:58:00 +01:00
|
|
|
return MakeCertInfoFromTBS(tbs), nil
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MakeCertInfoFromRawCert(certBytes []byte) (*CertInfo, error) {
|
2016-03-17 00:58:00 +01:00
|
|
|
cert, err := ParseCertificate(certBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return MakeCertInfoFromRawTBS(cert.GetRawTBSCertificate())
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MakeCertInfoFromLogEntry(entry *ct.LogEntry) (*CertInfo, error) {
|
2016-02-09 19:28:52 +01:00
|
|
|
switch entry.Leaf.TimestampedEntry.EntryType {
|
|
|
|
case ct.X509LogEntryType:
|
2016-03-17 00:58:00 +01:00
|
|
|
return MakeCertInfoFromRawCert(entry.Leaf.TimestampedEntry.X509Entry)
|
|
|
|
|
2016-02-09 19:28:52 +01:00
|
|
|
case ct.PrecertLogEntryType:
|
2016-03-17 00:58:00 +01:00
|
|
|
return MakeCertInfoFromRawTBS(entry.Leaf.TimestampedEntry.PrecertEntry.TBSCertificate)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("MakeCertInfoFromCTEntry: unknown CT entry type (neither X509 nor precert)")
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *CertInfo) NotBefore() *time.Time {
|
2016-03-17 00:58:00 +01:00
|
|
|
if info.ValidityParseError == nil {
|
|
|
|
return &info.Validity.NotBefore
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *CertInfo) NotAfter() *time.Time {
|
2016-03-17 00:58:00 +01:00
|
|
|
if info.ValidityParseError == nil {
|
|
|
|
return &info.Validity.NotAfter
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *CertInfo) PubkeyHash() string {
|
2016-03-17 00:58:00 +01:00
|
|
|
return sha256hex(info.TBS.GetRawPublicKey())
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *CertInfo) PubkeyHashBytes() []byte {
|
2016-03-27 03:04:22 +02:00
|
|
|
return sha256sum(info.TBS.GetRawPublicKey())
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *CertInfo) Environ() []string {
|
2016-03-17 00:58:00 +01:00
|
|
|
env := make([]string, 0, 10)
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "PUBKEY_HASH="+info.PubkeyHash())
|
2016-03-17 00:58:00 +01:00
|
|
|
|
|
|
|
if info.SerialNumberParseError != nil {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "SERIAL_PARSE_ERROR="+info.SerialNumberParseError.Error())
|
2016-03-17 00:58:00 +01:00
|
|
|
} else {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "SERIAL="+formatSerialNumber(info.SerialNumber))
|
2016-03-17 00:58:00 +01:00
|
|
|
}
|
2016-02-09 19:28:52 +01:00
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
if info.ValidityParseError != nil {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "VALIDITY_PARSE_ERROR="+info.ValidityParseError.Error())
|
2016-03-17 00:58:00 +01:00
|
|
|
} else {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "NOT_BEFORE="+info.Validity.NotBefore.String())
|
|
|
|
env = append(env, "NOT_BEFORE_UNIXTIME="+strconv.FormatInt(info.Validity.NotBefore.Unix(), 10))
|
|
|
|
env = append(env, "NOT_AFTER="+info.Validity.NotAfter.String())
|
|
|
|
env = append(env, "NOT_AFTER_UNIXTIME="+strconv.FormatInt(info.Validity.NotAfter.Unix(), 10))
|
2016-03-17 00:58:00 +01:00
|
|
|
}
|
2016-02-09 19:28:52 +01:00
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
if info.SubjectParseError != nil {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "SUBJECT_PARSE_ERROR="+info.SubjectParseError.Error())
|
2016-03-17 00:58:00 +01:00
|
|
|
} else {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "SUBJECT_DN="+info.Subject.String())
|
2016-03-17 00:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if info.IssuerParseError != nil {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "ISSUER_PARSE_ERROR="+info.IssuerParseError.Error())
|
2016-03-17 00:58:00 +01:00
|
|
|
} else {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "ISSUER_DN="+info.Issuer.String())
|
2016-03-17 00:58:00 +01:00
|
|
|
}
|
|
|
|
|
2016-05-02 20:59:55 +02:00
|
|
|
// TODO: include SANs in environment
|
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
return env
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *EntryInfo) HasParseErrors() bool {
|
2016-03-24 04:18:12 +01:00
|
|
|
return info.ParseError != nil ||
|
2016-05-03 20:58:59 +02:00
|
|
|
info.IdentifiersParseError != nil ||
|
2016-03-24 04:18:12 +01:00
|
|
|
info.CertInfo.SubjectParseError != nil ||
|
|
|
|
info.CertInfo.IssuerParseError != nil ||
|
2016-05-02 20:59:55 +02:00
|
|
|
info.CertInfo.SANsParseError != nil ||
|
2016-03-24 04:18:12 +01:00
|
|
|
info.CertInfo.SerialNumberParseError != nil ||
|
|
|
|
info.CertInfo.ValidityParseError != nil ||
|
2016-04-27 03:06:59 +02:00
|
|
|
info.CertInfo.IsCAParseError != nil
|
2016-03-24 04:18:12 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *EntryInfo) Fingerprint() string {
|
2016-03-17 00:58:00 +01:00
|
|
|
if len(info.FullChain) > 0 {
|
|
|
|
return sha256hex(info.FullChain[0])
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *EntryInfo) FingerprintBytes() []byte {
|
2016-03-27 03:04:22 +02:00
|
|
|
if len(info.FullChain) > 0 {
|
|
|
|
return sha256sum(info.FullChain[0])
|
|
|
|
} else {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *EntryInfo) typeString() string {
|
2016-03-17 00:58:00 +01:00
|
|
|
if info.IsPrecert {
|
2016-02-05 03:45:37 +01:00
|
|
|
return "precert"
|
|
|
|
} else {
|
|
|
|
return "cert"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *EntryInfo) typeFriendlyString() string {
|
2016-03-17 00:58:00 +01:00
|
|
|
if info.IsPrecert {
|
2016-02-05 03:45:37 +01:00
|
|
|
return "Pre-certificate"
|
|
|
|
} else {
|
|
|
|
return "Certificate"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func yesnoString(value bool) string {
|
2016-02-09 19:28:52 +01:00
|
|
|
if value {
|
|
|
|
return "yes"
|
2016-02-05 17:20:12 +01:00
|
|
|
} else {
|
2016-02-09 19:28:52 +01:00
|
|
|
return "no"
|
2016-02-05 17:20:12 +01:00
|
|
|
}
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *EntryInfo) Environ() []string {
|
2016-02-09 19:28:52 +01:00
|
|
|
env := []string{
|
|
|
|
"FINGERPRINT=" + info.Fingerprint(),
|
|
|
|
"CERT_TYPE=" + info.typeString(),
|
2016-03-17 00:58:00 +01:00
|
|
|
"CERT_PARSEABLE=" + yesnoString(info.ParseError == nil),
|
2016-02-09 19:28:52 +01:00
|
|
|
"LOG_URI=" + info.LogUri,
|
|
|
|
"ENTRY_INDEX=" + strconv.FormatInt(info.Entry.Index, 10),
|
|
|
|
}
|
2016-02-05 03:45:37 +01:00
|
|
|
|
2016-02-09 19:28:52 +01:00
|
|
|
if info.Filename != "" {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "CERT_FILENAME="+info.Filename)
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
2016-05-03 20:58:59 +02:00
|
|
|
if info.ParseError != nil {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "PARSE_ERROR="+info.ParseError.Error())
|
2016-05-03 20:58:59 +02:00
|
|
|
} else if info.CertInfo != nil {
|
2016-03-17 00:58:00 +01:00
|
|
|
certEnv := info.CertInfo.Environ()
|
|
|
|
env = append(env, certEnv...)
|
2016-05-03 20:58:59 +02:00
|
|
|
}
|
|
|
|
if info.IdentifiersParseError != nil {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "IDENTIFIERS_PARSE_ERROR="+info.IdentifiersParseError.Error())
|
2016-05-03 20:58:59 +02:00
|
|
|
} else if info.Identifiers != nil {
|
2016-07-28 20:55:46 +02:00
|
|
|
env = append(env, "DNS_NAMES="+info.Identifiers.dnsNamesString(","))
|
|
|
|
env = append(env, "IP_ADDRESSES="+info.Identifiers.ipAddrsString(","))
|
2016-02-05 17:20:12 +01:00
|
|
|
}
|
2016-02-09 19:28:52 +01:00
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func writeField(out io.Writer, name string, value interface{}, err error) {
|
2016-03-17 00:58:00 +01:00
|
|
|
if err == nil {
|
|
|
|
fmt.Fprintf(out, "\t%13s = %s\n", name, value)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(out, "\t%13s = *** UNKNOWN (%s) ***\n", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *EntryInfo) Write(out io.Writer) {
|
2016-02-09 19:28:52 +01:00
|
|
|
fingerprint := info.Fingerprint()
|
|
|
|
fmt.Fprintf(out, "%s:\n", fingerprint)
|
2016-05-03 20:58:59 +02:00
|
|
|
if info.IdentifiersParseError != nil {
|
|
|
|
writeField(out, "Identifiers", nil, info.IdentifiersParseError)
|
|
|
|
} else if info.Identifiers != nil {
|
|
|
|
for _, dnsName := range info.Identifiers.DNSNames {
|
|
|
|
writeField(out, "DNS Name", dnsName, nil)
|
|
|
|
}
|
|
|
|
for _, ipaddr := range info.Identifiers.IPAddrs {
|
|
|
|
writeField(out, "IP Address", ipaddr, nil)
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 19:28:52 +01:00
|
|
|
if info.ParseError != nil {
|
2016-07-28 20:55:46 +02:00
|
|
|
writeField(out, "Parse Error", "*** "+info.ParseError.Error()+" ***", nil)
|
2016-05-03 20:58:59 +02:00
|
|
|
} else if info.CertInfo != nil {
|
2016-03-17 00:58:00 +01:00
|
|
|
writeField(out, "Pubkey", info.CertInfo.PubkeyHash(), nil)
|
|
|
|
writeField(out, "Issuer", info.CertInfo.Issuer, info.CertInfo.IssuerParseError)
|
|
|
|
writeField(out, "Not Before", info.CertInfo.NotBefore(), info.CertInfo.ValidityParseError)
|
|
|
|
writeField(out, "Not After", info.CertInfo.NotAfter(), info.CertInfo.ValidityParseError)
|
|
|
|
}
|
2016-05-10 00:41:00 +02:00
|
|
|
writeField(out, "Log Entry", fmt.Sprintf("%d @ %s (%s)", info.Entry.Index, info.LogUri, info.typeFriendlyString()), nil)
|
2016-07-28 20:55:46 +02:00
|
|
|
writeField(out, "crt.sh", "https://crt.sh/?sha256="+fingerprint, nil)
|
2016-03-17 00:58:00 +01:00
|
|
|
if info.Filename != "" {
|
|
|
|
writeField(out, "Filename", info.Filename, nil)
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func (info *EntryInfo) InvokeHookScript(command string) error {
|
2016-02-09 19:28:52 +01:00
|
|
|
cmd := exec.Command(command)
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
infoEnv := info.Environ()
|
|
|
|
cmd.Env = append(cmd.Env, infoEnv...)
|
2016-02-05 03:45:37 +01:00
|
|
|
stderrBuffer := bytes.Buffer{}
|
|
|
|
cmd.Stderr = &stderrBuffer
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
if _, isExitError := err.(*exec.ExitError); isExitError {
|
2016-07-28 21:11:32 +02:00
|
|
|
return fmt.Errorf("Script failed: %s: %s", command, strings.TrimSpace(stderrBuffer.String()))
|
2016-02-05 03:45:37 +01:00
|
|
|
} else {
|
2016-07-28 21:11:32 +02:00
|
|
|
return fmt.Errorf("Failed to execute script: %s: %s", command, err)
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MatchesWildcard(dnsName string, pattern string) bool {
|
2016-05-10 19:37:10 +02:00
|
|
|
for len(pattern) > 0 {
|
|
|
|
if pattern[0] == '*' {
|
2016-05-10 23:29:04 +02:00
|
|
|
if len(dnsName) > 0 && dnsName[0] != '.' && MatchesWildcard(dnsName[1:], pattern) {
|
2016-05-10 19:37:10 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
pattern = pattern[1:]
|
|
|
|
} else {
|
|
|
|
if len(dnsName) == 0 || pattern[0] != dnsName[0] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
pattern = pattern[1:]
|
|
|
|
dnsName = dnsName[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(dnsName) == 0
|
|
|
|
}
|