File: sbuild-chroot-facet-userdata.cc

package info (click to toggle)
schroot 1.6.4-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,684 kB
  • sloc: cpp: 21,427; sh: 12,516; makefile: 829; ansic: 231; sed: 16
file content (378 lines) | stat: -rw-r--r-- 10,112 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* Copyright © 2005-2009,2012  Roger Leigh <rleigh@debian.org>
 *
 * schroot is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * schroot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *********************************************************************/

#include <config.h>

#include "sbuild-chroot.h"
#include "sbuild-chroot-facet-userdata.h"
#include "sbuild-regex.h"

#include <locale>

#include <boost/format.hpp>

using boost::format;
using namespace sbuild;

namespace
{
  typedef std::pair<chroot_facet_userdata::error_code,const char *> emap;

  /**
   * This is a list of the supported error codes.  It's used to
   * construct the real error codes map.
   */
  emap init_errors[] =
    {
      emap(chroot_facet_userdata::ENV_AMBIGUOUS,
	   N_("Environment variable ‘%1%’ is ambiguous")),
      emap(chroot_facet_userdata::KEY_AMBIGUOUS,
	   N_("Configuration key ‘%1%’ is ambiguous")),
      emap(chroot_facet_userdata::KEY_DISALLOWED,
	   N_("Configuration key ‘%1%’ is not permitted to be modified.")),
      emap(chroot_facet_userdata::KEYNAME_INVALID,
	   N_("Configuration key name ‘%1%’ is not a permitted name."))
    };

  bool
  validate_keyname(std::string const& key)
  {
    // Valid names consist of one (or more) namespaces which consist
    // of [a-z][a-z0-9] followed by a . (namespace separator).  The
    // key name following the separator is [a-z][a-z0-9-].  When
    // converted to an environment variable, all alphabet characters
    // are uppercased, and all periods and hyphens converted to
    // underscores.
    static sbuild::regex permitted("^([a-z][a-z0-9]*\\.)+[a-z][a-z0-9-]*$");

    // These would permit clashes with existing setup environment
    // variables, and potentially security issues if they were
    // user-settable.
    static sbuild::regex reserved("^(((auth|chroot|host|libexec|mount|session|status|sysconf)\\..*)|setup.data.dir)$");

    return regex_search(key, permitted) && !regex_search(key, reserved);
  }

  std::string
  envname(std::string const& key)
  {
    std::string ret(key);

    static const std::ctype<char>& ct = std::use_facet<std::ctype<char> >(std::locale::classic());
    for (std::string::iterator pos = ret.begin(); pos != ret.end(); ++pos)
      {
	*pos = ct.toupper(*pos);
	if (*pos == '-' || *pos == '.')
	  *pos = '_';
      }
    return ret;
  }
}

template<>
error<chroot_facet_userdata::error_code>::map_type
error<chroot_facet_userdata::error_code>::error_strings
(init_errors,
 init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));

chroot_facet_userdata::chroot_facet_userdata ():
  chroot_facet(),
  userdata(),
  env(),
  user_modifiable_keys(),
  root_modifiable_keys()
{
}

chroot_facet_userdata::~chroot_facet_userdata ()
{
}

chroot_facet_userdata::ptr
chroot_facet_userdata::create ()
{
  return ptr(new chroot_facet_userdata());
}

chroot_facet::ptr
chroot_facet_userdata::clone () const
{
  return ptr(new chroot_facet_userdata(*this));
}

std::string const&
chroot_facet_userdata::get_name () const
{
  static const std::string name("userdata");

  return name;
}

void
chroot_facet_userdata::setup_env (chroot const& chroot,
				  environment&  env) const
{
  for (string_map::const_iterator pos = userdata.begin();
       pos != userdata.end();
       ++pos)
    {
      std::string name = envname(pos->first);
      std::string dummy;
      if (!env.get(name, dummy))
	env.add(name, pos->second);
      else
	{
	  error e(name, ENV_AMBIGUOUS);
	  format fmt(_("Configuration keys additional to ‘%1%’ would set this setup script environment variable"));
	  fmt % pos->first;
	  e.set_reason(fmt.str());
	  throw e;
	}
    }
}

sbuild::chroot::session_flags
chroot_facet_userdata::get_session_flags (chroot const& chroot) const
{
  return sbuild::chroot::SESSION_NOFLAGS;
}

void
chroot_facet_userdata::get_details (chroot const&  chroot,
				    format_detail& detail) const
{
  string_list userkeys(this->user_modifiable_keys.begin(),
		       this->user_modifiable_keys.end());
  std::sort(userkeys.begin(), userkeys.end());

  string_list rootkeys(this->root_modifiable_keys.begin(),
		       this->root_modifiable_keys.end());
  std::sort(rootkeys.begin(), rootkeys.end());

  detail.add(_("User Modifiable Keys"), userkeys);
  detail.add(_("Root Modifiable Keys"), rootkeys);
  detail.add(_("User Data"), "");

  string_list keys;
  for (string_map::const_iterator pos = userdata.begin();
       pos != userdata.end();
       ++pos)
    keys.push_back(pos->first);
  std::sort(keys.begin(), keys.end());

  for (string_list::const_iterator pos = keys.begin();
       pos != keys.end();
       ++pos)
    {
      string_map::const_iterator key = userdata.find(*pos);
      if (key != userdata.end())
	{
	  std::string name("  ");
	  name += key->first;
	  detail.add(name, key->second);
	}
    }
}

string_map const&
chroot_facet_userdata::get_data () const
{
  return userdata;
}

bool
chroot_facet_userdata::get_data (std::string const& key,
				 std::string& value) const
{
  string_map::const_iterator pos = this->userdata.find(key);
  bool found = (pos != this->userdata.end());
  if (found)
    value = pos->second;
  return found;
}

void
chroot_facet_userdata::set_system_data (std::string const& key,
					std::string const& value)
{
  string_map::const_iterator inserted = userdata.find(key);
  if (inserted == userdata.end()) // Requires uniqueness checking.
    {
      std::string name = envname(key);
      string_set::const_iterator found = this->env.find(name);
      if (found != this->env.end())
	{
	  error e(key, KEY_AMBIGUOUS);
	  format fmt(_("More than one configuration key would set the ‘%1%’ environment variable"));
	  fmt % name;
	  e.set_reason(fmt.str());
	  throw e;
	}
      this->env.insert(name);
    }
  else
    this->userdata.erase(key);

  this->userdata.insert(std::make_pair(key, value));
}

void
chroot_facet_userdata::remove_data (std::string const& key)
{
  this->userdata.erase(key);
}

void
chroot_facet_userdata::set_data (std::string const& key,
				 std::string const& value)
{
  if (!validate_keyname(key))
    throw error(key, KEYNAME_INVALID);

  set_system_data(key, value);
}

void
chroot_facet_userdata::set_data (string_map const& data)
{
  for (string_map::const_iterator pos = data.begin();
       pos != data.end();
       ++pos)
    set_data(pos->first, pos->second);
}

void
chroot_facet_userdata::set_user_data(string_map const&  data)
{
  set_data(data, this->user_modifiable_keys, false);
}

void
chroot_facet_userdata::set_root_data(string_map const&  data)
{
  // root can use both user and root keys, so combine the sets.
  string_set modifiable_keys;
  set_union(this->user_modifiable_keys.begin(),
	    this->user_modifiable_keys.end(),
	    this->root_modifiable_keys.begin(),
	    this->root_modifiable_keys.end(),
	    inserter(modifiable_keys, modifiable_keys.begin()));
  set_data(data, modifiable_keys, true);
}

void
chroot_facet_userdata::set_system_data(string_map const&  data)
{
  for (string_map::const_iterator pos = data.begin();
       pos != data.end();
       ++pos)
    set_system_data(pos->first, pos->second);
}

void
chroot_facet_userdata::set_data(string_map const&  data,
				string_set const&  allowed_keys,
				bool               root)
{
  // Require the key to be present in order to set it.  This ensures
  // that the key name has been pre-validated.
  for (string_map::const_iterator pos = data.begin();
       pos != data.end();
       ++pos)
    {
      string_set::const_iterator allowed = allowed_keys.find(pos->first);
      if (allowed == allowed_keys.end())
	{
	  error e(pos->first, KEY_DISALLOWED);
	  if (root)
	    e.set_reason(_("The key is not present in user-modifiable-keys or root-modifiable-keys"));
	  else
	    e.set_reason(_("The key is not present in user-modifiable-keys"));
	  throw e;
	}
      set_data(pos->first, pos->second);
    }
}

string_set const&
chroot_facet_userdata::get_user_modifiable_keys() const
{
  return this->user_modifiable_keys;
}

void
chroot_facet_userdata::set_user_modifiable_keys (string_set const& keys)
{
  this->user_modifiable_keys = keys;
}

string_set const&
chroot_facet_userdata::get_root_modifiable_keys() const
{
  return this->root_modifiable_keys;
}

void
chroot_facet_userdata::set_root_modifiable_keys (string_set const& keys)
{
  this->root_modifiable_keys = keys;
}

void
chroot_facet_userdata::get_keyfile (chroot const& chroot,
				    keyfile&      keyfile) const
{
  keyfile::set_object_set_value(*this,
				&chroot_facet_userdata::get_user_modifiable_keys,
				keyfile, chroot.get_name(),
				"user-modifiable-keys");

  keyfile::set_object_set_value(*this,
				&chroot_facet_userdata::get_root_modifiable_keys,
				keyfile, chroot.get_name(),
				"root-modifiable-keys");

  for (string_map::const_iterator pos = userdata.begin();
       pos != userdata.end();
       ++pos)
    {
      keyfile.set_value(chroot.get_name(),
			pos->first,
			pos->second);
    }
}

void
chroot_facet_userdata::set_keyfile (chroot&        chroot,
				    keyfile const& keyfile,
				    string_list&   used_keys)
{
  keyfile::get_object_set_value(*this,
				&chroot_facet_userdata::set_user_modifiable_keys,
				keyfile, chroot.get_name(),
				"user-modifiable-keys",
				keyfile::PRIORITY_OPTIONAL);
  used_keys.push_back("user-modifiable-keys");

  keyfile::get_object_set_value(*this,
				&chroot_facet_userdata::set_root_modifiable_keys,
				keyfile, chroot.get_name(),
				"root-modifiable-keys",
				keyfile::PRIORITY_OPTIONAL);
  used_keys.push_back("root-modifiable-keys");
}