initialise repo
[debian/orchestra.git] / src / player / config.go
1 // config.go
2 //
3 // configuration file handling for orchestra.
4
5 package main
6
7 import (
8         o "orchestra"
9         "strings"
10         "github.com/kuroneko/configureit"
11         "crypto/tls"
12         "crypto/x509"
13         "os"
14 )
15
16 var configFile = configureit.New()
17
18 func init() {
19         configFile.Add("x509 certificate", configureit.NewStringOption("/etc/orchestra/player_crt.pem"))
20         configFile.Add("x509 private key", configureit.NewStringOption("/etc/orchestra/player_key.pem"))
21         configFile.Add("ca certificates", configureit.NewPathListOption(nil))
22         configFile.Add("master", configureit.NewStringOption("conductor"))
23         configFile.Add("score directory", configureit.NewStringOption("/usr/lib/orchestra/scores"))
24         configFile.Add("player name", configureit.NewStringOption(""))
25 }
26
27 func GetStringOpt(key string) string {
28         cnode := configFile.Get(key)
29         if cnode == nil {
30                 o.Assert("tried to get a configuration option that doesn't exist.")
31         }
32         sopt, ok := cnode.(*configureit.StringOption)
33         if !ok {
34                 o.Assert("tried to get a non-string configuration option with GetStringOpt")
35         }
36         return strings.TrimSpace(sopt.Value)
37 }
38
39 func GetCACertList() []string {
40         cnode := configFile.Get("ca certificates")
41         if cnode == nil {
42                 o.Assert("tried to get a configuration option that doesn't exist.")
43         }
44         plopt, _ := cnode.(*configureit.PathListOption)
45         return plopt.Values
46 }
47
48 func ConfigLoad() {
49         // attempt to open the configuration file.
50         fh, err := os.Open(*ConfigFile)
51         if nil == err {
52                 defer fh.Close()
53                 // reset the config File data, then reload it.
54                 configFile.Reset()
55                 ierr := configFile.Read(fh, 1)
56                 o.MightFail(ierr, "Couldn't parse configuration")
57         } else {
58                 o.Warn("Couldn't open configuration file: %s.  Proceeding anyway.", err)
59         }
60
61         // load the x509 certificates
62         x509CertFilename := GetStringOpt("x509 certificate")
63         x509PrivateKeyFilename := GetStringOpt("x509 private key")
64         CertPair, err = tls.LoadX509KeyPair(x509CertFilename, x509PrivateKeyFilename)
65         o.MightFail(err, "Couldn't load certificates")
66
67         // load the CA Certs
68         CACertPool = x509.NewCertPool()
69         caCertNames := GetCACertList()
70         if caCertNames != nil {
71                 for _, filename := range caCertNames {
72                         fh, err := os.Open(filename)
73                         if err != nil {
74                                 o.Warn("Whilst parsing CA certs, couldn't open %s: %s", filename, err)
75                                 continue
76                         }
77                         defer fh.Close()
78                         fi, err := fh.Stat()
79                         o.MightFail(err, "Couldn't stat CA certificate file: %s", filename)
80                         data := make([]byte, fi.Size)
81                         fh.Read(data)
82                         CACertPool.AppendCertsFromPEM(data)
83                 }
84         }
85 }