File: conf.cpp

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (210 lines) | stat: -rw-r--r-- 6,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*************************************************
* Configuration Handling Source File             *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/conf.h>
#include <botan/lookup.h>
#include <botan/mutex.h>
#include <botan/charset.h>
#include <botan/parsing.h>
#include <string>
#include <map>

namespace Botan {

namespace {

/*************************************************
* Holder for name-value option pairs             *
*************************************************/
class Options
   {
   public:
      std::string get(const std::string&);
      void set(const std::string&, const std::string&, bool);

      Options() { options_mutex = get_mutex(); }
      ~Options() { delete options_mutex; }
   private:
      std::map<std::string, std::string> options;
      Mutex* options_mutex;
   };

/*************************************************
* Get an option by name                          *
*************************************************/
std::string Options::get(const std::string& name)
   {
   Mutex_Holder lock(options_mutex);

   std::map<std::string, std::string>::const_iterator i = options.find(name);
   if(i != options.end())
      return i->second;
   return "";
   }

/*************************************************
* Set an option by name                          *
*************************************************/
void Options::set(const std::string& name, const std::string& value,
                  bool overwrite)
   {
   const bool have_it = ((get(name) == "") ? false : true);

   Mutex_Holder lock(options_mutex);
   if(overwrite || !have_it)
      options[name] = value;
   }

/*************************************************
* Global state                                   *
*************************************************/
Options* options = 0;

}

namespace Init {

/*************************************************
* Startup the configuration system               *
*************************************************/
void startup_conf()
   {
   options = new Options;
   }

/*************************************************
* Shutdown the configuration system              *
*************************************************/
void shutdown_conf()
   {
   delete options;
   options = 0;
   }

}

namespace Config {

/*************************************************
* Set an option                                  *
*************************************************/
void set(const std::string& name, const std::string& value, bool overwrite)
   {
   if(!options)
      throw Internal_Error("Config::set: Conf system never started");

   options->set(name, value, overwrite);
   }

/*************************************************
* Get the value of an option as a string         *
*************************************************/
std::string get_string(const std::string& name)
   {
   if(!options)
      throw Internal_Error("Config::get: Conf system never started");

   return options->get(name);
   }

/*************************************************
* Get the value as a list of strings             *
*************************************************/
std::vector<std::string> get_list(const std::string& name)
   {
   return split_on(get_string(name), ':');
   }

/*************************************************
* Get the value as a u32bit                      *
*************************************************/
u32bit get_u32bit(const std::string& name)
   {
   return parse_expr(get_string(name));
   }

/*************************************************
* Get the value as a time                        *
*************************************************/
u32bit get_time(const std::string& name)
   {
   const std::string timespec = get_string(name);
   if(timespec == "")
      return 0;

   const char suffix = timespec[timespec.size()-1];
   std::string value = timespec.substr(0, timespec.size()-1);

   u32bit scale = 1;

   if(is_digit(suffix))
      value += suffix;
   else if(suffix == 's')
      scale = 1;
   else if(suffix == 'm')
      scale = 60;
   else if(suffix == 'h')
      scale = 60 * 60;
   else if(suffix == 'd')
      scale = 24 * 60 * 60;
   else if(suffix == 'y')
      scale = 365 * 24 * 60 * 60;
   else
      throw Decoding_Error("Config::get_time: Unknown time value " + value);

   return scale * to_u32bit(value);
   }

/*************************************************
* Get the value as a boolean                     *
*************************************************/
bool get_bool(const std::string& name)
   {
   const std::string value = get_string(name);
   if(value == "0" || value == "false")
      return false;
   if(value == "1" || value == "true")
      return true;
   throw Decoding_Error("Config::get_bool: Unknown boolean value " + value);
   }

/*************************************************
* Choose the signature format for a PK algorithm *
*************************************************/
void choose_sig_format(const std::string& algo_name, std::string& padding,
                       Signature_Format& format)
   {
   std::string dummy;
   choose_sig_format(algo_name, padding, dummy, format);
   }

/*************************************************
* Choose the signature format for a PK algorithm *
*************************************************/
void choose_sig_format(const std::string& algo_name, std::string& padding,
                       std::string& hash, Signature_Format& format)
   {
   if(algo_name == "RSA")
      {
      hash = deref_alias(get_string("x509/ca/rsa_hash"));
      if(hash == "")
         throw Invalid_State("No value set for x509/ca/rsa_hash");

      padding = "EMSA3(" + hash + ")";
      format = IEEE_1363;
      }
   else if(algo_name == "DSA")
      {
      hash = deref_alias("SHA-1");
      padding = "EMSA1(" + hash + ")";
      format = DER_SEQUENCE;
      }
   else
      throw Invalid_Argument("Unknown X.509 signing key type: " + algo_name);
   }

}

}