initialise repo
[debian/orchestra.git] / src / conductor / config.go
1 package main
2
3 import (
4         "os"
5         "bufio"
6         o "orchestra"
7         "strings"
8         "github.com/kuroneko/configureit"
9 )
10
11 var configFile *configureit.Config = configureit.New()
12
13 func init() {
14         configFile.Add("x509 certificate", configureit.NewStringOption("/etc/orchestra/conductor_crt.pem"))
15         configFile.Add("x509 private key", configureit.NewStringOption("/etc/orchestra/conductor_key.pem"))
16         configFile.Add("ca certificates", configureit.NewPathListOption(nil))
17         configFile.Add("bind address", configureit.NewStringOption(""))
18         configFile.Add("server name", configureit.NewStringOption(""))
19         configFile.Add("audience socket path", configureit.NewStringOption("/var/spool/orchestra/conductor.sock"))
20         configFile.Add("conductor state path", configureit.NewStringOption("/var/spool/orchestra"))
21         configFile.Add("player file path", configureit.NewStringOption("/etc/orchestra/players"))
22 }
23
24 func GetStringOpt(key string) string {
25         cnode := configFile.Get(key)
26         if cnode == nil {
27                 o.Assert("tried to get a configuration option that doesn't exist.")
28         }
29         sopt, ok := cnode.(*configureit.StringOption)
30         if !ok {
31                 o.Assert("tried to get a non-string configuration option with GetStringOpt")
32         }
33         return strings.TrimSpace(sopt.Value)
34 }
35
36
37 func GetCACertList() []string {
38         cnode := configFile.Get("ca certificates")
39         if cnode == nil {
40                 o.Assert("tried to get a configuration option that doesn't exist.")
41         }
42         plopt, _ := cnode.(*configureit.PathListOption)
43         return plopt.Values
44 }
45
46 func ConfigLoad() {
47         // attempt to open the configuration file.
48         fh, err := os.Open(*ConfigFile)
49         if nil == err {
50                 defer fh.Close()
51                 // reset the config File data, then reload it.
52                 configFile.Reset()
53                 ierr := configFile.Read(fh, 1)
54                 o.MightFail(ierr, "Couldn't parse configuration")
55         } else {
56                 o.Warn("Couldn't open configuration file: %s.  Proceeding anyway.", err)
57         }
58
59         playerpath := strings.TrimSpace(GetStringOpt("player file path"))
60         pfh, err := os.Open(playerpath)
61         o.MightFail(err, "Couldn't open \"%s\"", playerpath)
62
63         pbr := bufio.NewReader(pfh)
64
65         ahmap := make(map[string]bool)
66         for err = nil; err == nil; {
67                 var lb          []byte
68                 var prefix      bool
69
70                 lb, prefix, err = pbr.ReadLine()
71
72                 if nil == lb {
73                         break;
74                 }
75                 if prefix {
76                         o.Fail("ConfigLoad: Short Read (prefix only)!")
77                 }
78                 
79                 line := strings.TrimSpace(string(lb))
80                 if line == "" {
81                         continue;
82                 }
83                 if line[0] == '#' {
84                         continue;
85                 }
86                 ahmap[line] = true
87         }
88         // convert newAuthorisedHosts to a slice
89         authorisedHosts := make([]string, len(ahmap))
90         idx := 0
91         for k,_ := range ahmap {
92                 authorisedHosts[idx] = k
93                 idx++
94         }
95         ClientUpdateKnown(authorisedHosts)
96
97         // set the spool directory
98         SetSpoolDirectory(GetStringOpt("conductor state path"))
99 }
100
101
102 func HostAuthorised(hostname string) (r bool) {
103         /* if we haven't loaded the configuration, nobody is authorised */
104         ci := ClientGet(hostname)
105         return ci != nil
106 }