summaryrefslogtreecommitdiff
path: root/lib/schroot-common/main.cc
blob: 40e5b942ac187689dacfea46e302302fac24e9b7 (plain)
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
379
380
381
382
383
384
385
386
387
388
/* Copyright © 2005-2013  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 <schroot-common/main.h>

#include <sbuild/config.h>
#ifdef SBUILD_FEATURE_PAM
#include <sbuild/auth/pam.h>
#include <sbuild/auth/pam-conv.h>
#include <sbuild/auth/pam-conv-tty.h>
#endif // SBUILD_FEATURE_PAM
#include <sbuild/chroot/facet/factory.h>
#include <sbuild/keyfile-writer.h>

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <locale>

#include <termios.h>
#include <unistd.h>

#include <boost/format.hpp>

using std::endl;
using boost::format;
using sbuild::_;
using sbuild::N_;

namespace schroot_common
{

  template<>
  sbuild::error<main::error_code>::map_type
  sbuild::error<main::error_code>::error_strings =
    {
      // TRANSLATORS: %4% = file
      {schroot_common::main::CHROOT_FILE,       N_("No chroots are defined in ‘%4%’")},
      // TRANSLATORS: %4% = file
      // TRANSLATORS: %5% = file
      {schroot_common::main::CHROOT_FILE2,      N_("No chroots are defined in ‘%4%’ or ‘%5%’")},
      // TRANSLATORS: %1% = file
      {schroot_common::main::CHROOT_NOTDEFINED, N_("The specified chroots are not defined in ‘%1%’")},
      {schroot_common::main::SESSION_INVALID,   N_("%1%: Invalid session name")}
    };

  main::main (const std::string& program_name,
              const std::string& program_usage,
              options::ptr&      options,
              bool               use_syslog):
    bin_common::main(program_name, program_usage,
                     std::static_pointer_cast<bin_common::options>(options),
                     use_syslog),
    options(options)
  {
  }

  main::~main ()
  {
  }

  void
  main::action_version (std::ostream& stream)
  {
    bin_common::main::action_version(stream);

    stream << '\n'
           << _("Installed chroot facets:")
           << '\n';
    sbuild::chroot::facet::factory::print_facets(stream);

    stream << std::flush;
  }

  void
  main::action_info ()
  {
    for(auto chroot_name = this->chroot_names.begin();
        chroot_name != this->chroot_names.end();
        ++chroot_name)
      {
        // This should never fail, so no error handling here--we already
        // validated everything when we got the chroot map.
        sbuild::chroot::config::chroot_map::const_iterator c = this->chroots.find(*chroot_name);
        assert(c->second);

        std::cout << c->second;
        if (chroot_name + 1 != this->chroot_names.end())
          std::cout << '\n';
      }

    std::cout << std::flush;
  }

  void
  main::action_location ()
  {
    for(const auto& chroot_name : this->chroot_names)
      {
        // This should never fail, so no error handling here--we already
        // validated everything when we got the chroot map.
        const auto chroot = this->chroots.find(chroot_name);
        assert(chroot->second);

        std::cout << chroot->second->get_path() << '\n';
      }

    std::cout << std::flush;
  }

  void
  main::action_config ()
  {
    std::cout << "# "
      // TRANSLATORS: %1% = program name
      // TRANSLATORS: %2% = program version
      // TRANSLATORS: %3% = current date
              << format(_("schroot configuration generated by %1% %2% on %3%"))
      % this->program_name % VERSION % sbuild::date(time(0))
              << endl;
    std::cout << endl;

    sbuild::keyfile info;

    for(const auto& chroot_name : this->chroot_names)
      {
        // This should never fail, so no error handling here--we already
        // validated everything when we got the chroot map.
        const auto chroot = this->chroots.find(chroot_name);
        assert(chroot->second);

        // Generated chroots (e.g. source chroots) are not printed.
        if (chroot->second->get_original())
          info << chroot->second;
      }

    std::cout << sbuild::keyfile_writer(info) << std::flush;
  }

  void
  main::get_chroot_options ()
  {

    if (this->options->all_chroots == true ||
        this->options->all_sessions == true ||
        this->options->all_source_chroots == true)
      {
        if (this->options->all_chroots)
          {
            sbuild::string_list chroots;
            if (this->options->action == options::ACTION_LIST &&
                !this->options->exclude_aliases)
              chroots = this->config->get_alias_list("chroot");
            else
              chroots = this->config->get_chroot_list("chroot");
            this->chroot_names.insert(this->chroot_names.end(), chroots.begin(), chroots.end());
          }
        if (this->options->all_sessions)
          {
            sbuild::string_list sessions;
            if (this->options->action == options::ACTION_LIST &&
                !this->options->exclude_aliases)
              sessions = this->config->get_alias_list("session");
            else
              sessions = this->config->get_chroot_list("session");
            this->chroot_names.insert(this->chroot_names.end(), sessions.begin(), sessions.end());
          }
        if (this->options->all_source_chroots)
          {
            sbuild::string_list sources;
            if (this->options->action == options::ACTION_LIST &&
                !this->options->exclude_aliases)
              sources = this->config->get_alias_list("source");
            else
              sources = this->config->get_chroot_list("source");
            this->chroot_names.insert(this->chroot_names.end(), sources.begin(), sources.end());
          }

        // Validate and normalise
        this->chroots = this->config->validate_chroots("", this->chroot_names);
      }
    else
      {
        // Search in the appropriate namespace.
        std::string chroot_namespace("chroot");
        if (this->options->action == options::ACTION_SESSION_RECOVER ||
            this->options->action == options::ACTION_SESSION_RUN ||
            this->options->action == options::ACTION_SESSION_END)
          chroot_namespace = "session";

        // Validate and normalise
        this->chroot_names = this->options->chroots;
        this->chroots = this->config->validate_chroots
          (chroot_namespace, this->chroot_names);
      }
  }

  void
  main::load_config ()
  {
    this->config = sbuild::chroot::config::ptr(new sbuild::chroot::config);
    /* The normal chroot list is used when starting a session or running
       any chroot type or session, or displaying chroot information. */
    if (this->options->load_chroots == true)
      {
        this->config->add("chroot", SCHROOT_CONF);
        this->config->add("chroot", SCHROOT_CONF_CHROOT_D);
      }
    /* The session chroot list is used when running or ending an
       existing session, or displaying chroot information. */
    if (this->options->load_sessions == true)
      this->config->add("session", SCHROOT_SESSION_DIR);
  }

  int
  main::run_impl ()
  {
    if (this->options->action == options::ACTION_HELP)
      {
        action_help(std::cout);
        return EXIT_SUCCESS;
      }

    if (this->options->action == options::ACTION_VERSION)
      {
        action_version(std::cout);
        return EXIT_SUCCESS;
      }

    /* Initialise chroot configuration. */
    load_config();

    if (this->options->load_chroots &&
        this->config->get_chroots("chroot").empty() &&
        this->options->quiet == false)
      log_exception_warning(error(CHROOT_FILE2, SCHROOT_CONF, SCHROOT_CONF_CHROOT_D));

    /* Get list of chroots to use */
    get_chroot_options();
    if (this->chroots.empty())
      {
        if (!(this->options->all_chroots == true ||
              this->options->all_sessions == true ||
              this->options->all_source_chroots == true))
          throw error(SCHROOT_CONF, CHROOT_NOTDEFINED);
        else
          {
            // If one of the --all options was used, then don't treat
            // the lack of chroots as an error.  TODO: Also check if any
            // additional chroots were specified with -c; this needs
            // changes in get_chroot_options.
            if (this->options->verbose)
              log_exception_warning(error((this->options->all_chroots == true) ?
                                          SCHROOT_CONF : SCHROOT_SESSION_DIR,
                                          CHROOT_NOTDEFINED));
            return EXIT_SUCCESS;
          }
      }
    this->chroot_objects.clear();
    for(const auto& chroot_name : this->chroot_names)
      {
        // This should never fail, so no error handling here--we already
        // validated everything when we got the chroot map.
        const auto chroot = this->chroots.find(chroot_name);
        assert(chroot->second);
        sbuild::session::chroot_list::value_type e;
        e.alias = chroot->first;
        e.chroot = chroot->second;
        chroot_objects.push_back(e);
      }

    /* Print chroot list. */
    if (this->options->action == options::ACTION_LIST)
      {
        action_list();
        return EXIT_SUCCESS;
      }

    if (this->config->find_alias("session", this->options->session_name))
      throw error(this->options->session_name, SESSION_INVALID);

    /* Print chroot information for specified chroots. */
    if (this->options->action == options::ACTION_INFO)
      {
        action_info();
        return EXIT_SUCCESS;
      }
    if (this->options->action == options::ACTION_LOCATION)
      {
        action_location();
        return EXIT_SUCCESS;
      }
    if (this->options->action == options::ACTION_CONFIG)
      {
        action_config();
        return EXIT_SUCCESS;
      }

    /* Create a session. */
    sbuild::session::operation sess_op(sbuild::session::OPERATION_AUTOMATIC);
    if (this->options->action == options::ACTION_SESSION_BEGIN)
      sess_op = sbuild::session::OPERATION_BEGIN;
    else if (this->options->action == options::ACTION_SESSION_RECOVER)
      sess_op = sbuild::session::OPERATION_RECOVER;
    else if (this->options->action == options::ACTION_SESSION_RUN)
      sess_op = sbuild::session::OPERATION_RUN;
    else if (this->options->action == options::ACTION_SESSION_END)
      sess_op = sbuild::session::OPERATION_END;

    try
      {
        create_session(sess_op);
        add_session_auth();

        if (!this->options->command.empty())
          this->session->get_auth()->set_command(this->options->command);
        if (!this->options->directory.empty())
          this->session->get_auth()->set_wd(this->options->directory);
        if (!this->options->shell.empty())
          this->session->set_shell_override(this->options->shell);
        this->session->set_preserve_environment(this->options->preserve);
        this->session->set_session_id(this->options->session_name);
        this->session->set_force(this->options->session_force);
        if (this->options->quiet)
          this->session->set_verbosity("quiet");
        else if (this->options->verbose)
          this->session->set_verbosity("verbose");
        this->session->set_user_options(this->options->useroptions_map);

        /* Run session. */
        this->session->run();
      }
    catch (const std::runtime_error& e)
      {
        if (!this->options->quiet)
          sbuild::log_exception_error(e);
      }

    if (this->session)
      return this->session->get_child_status();
    else
      return EXIT_FAILURE;
  }

  void
  main::add_session_auth ()
  {
    // Add PAM authentication handler.  If PAM isn't available, just
    // continue to use the default handler

#ifdef SBUILD_FEATURE_PAM
    sbuild::auth::auth::ptr auth = sbuild::auth::pam::create("schroot");

    sbuild::auth::pam_conv::auth_ptr pam_auth =
      std::dynamic_pointer_cast<sbuild::auth::pam>(auth);

    sbuild::auth::pam_conv::ptr conv = sbuild::auth::pam_conv_tty::create(pam_auth);


    /* Set up authentication timeouts. */
    time_t curtime = 0;
    time(&curtime);
    conv->set_warning_timeout(curtime + 15);
    conv->set_fatal_timeout(curtime + 20);
    pam_auth->set_conv(conv);

    this->session->set_auth(auth);
#endif // SBUILD_FEATURE_PAM
  }

}