summaryrefslogtreecommitdiff
path: root/lib/sbuild/chroot/facet/session-clonable.cc
blob: aa757bad31668722ab8e7b5360c210bda3534ec9 (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
/* 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 <sbuild/chroot/chroot.h>
#include <sbuild/chroot/facet/factory.h>
#include <sbuild/chroot/facet/mountable.h>
#include <sbuild/chroot/facet/session.h>
#include <sbuild/chroot/facet/session-clonable.h>
#include <sbuild/chroot/facet/session-setup.h>
#include <sbuild/chroot/facet/source-clonable.h>
#include <sbuild/chroot/plain.h>
#ifdef SBUILD_FEATURE_BLOCKDEV
#include <sbuild/chroot/facet/block-device-base.h>
#endif
#include "format-detail.h"

#include <cassert>

#include <boost/format.hpp>

using boost::format;
using std::endl;
using namespace sbuild;

namespace sbuild
{
  namespace chroot
  {
    namespace facet
    {

      namespace
      {

        factory::facet_info session_clonable_info =
          {
            "session-clonable",
            N_("Support for session chroot cloning"),
            []() -> facet::ptr { return session_clonable::create(); }
          };

        factory session_clonable_register(session_clonable_info);

      }

      session_clonable::session_clonable ():
        facet()
      {
      }

      session_clonable::~session_clonable ()
      {
      }

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

      facet::ptr
      session_clonable::clone () const
      {
        return ptr(new session_clonable(*this));
      }

      std::string const&
      session_clonable::get_name () const
      {
        static const std::string name("session-clonable");

        return name;
      }

      chroot::ptr
      session_clonable::clone_session (std::string const& session_id,
                                       std::string const& alias,
                                       std::string const& user,
                                       bool               root) const
      {
        chroot::ptr clone = owner->clone();

        // Disable session cloning.
        clone->remove_facet<session_clonable>();
        // Disable source cloning.
        clone->remove_facet<source_clonable>();
        clone->add_facet(session::create());

        // Disable session, delete aliases.
        session::ptr psess(clone->get_facet<session>());
        assert(psess);

        psess->set_original_name(clone->get_name());
        psess->set_selected_name(alias);
        clone->set_name(session_id);
        assert(clone->get_name() == session_id);
        clone->set_description
          (clone->get_description() + ' ' + _("(session chroot)"));

        string_list empty_list;
        string_list allowed_users;
        if (!user.empty())
          allowed_users.push_back(user);

        if (root)
          {
            clone->set_users(empty_list);
            clone->set_root_users(allowed_users);
          }
        else
          {
            clone->set_users(allowed_users);
            clone->set_root_users(empty_list);
          }
        clone->set_groups(empty_list);
        clone->set_root_groups(empty_list);
        clone->set_aliases(empty_list);

        log_debug(DEBUG_INFO)
          << format("Cloned session %1%")
          % clone->get_name() << endl;

        /* If a chroot mount location has not yet been set, and the
           chroot is not a plain chroot, set a mount location with the
           session id.  Only set for non-plain chroots which run
           setup scripts. */
        {
          std::shared_ptr<plain> plain(std::dynamic_pointer_cast<plain>(clone));

          if (clone->get_mount_location().empty() && !plain)
            {
              log_debug(DEBUG_NOTICE) << "Setting mount location" << endl;
              std::string location(std::string(SCHROOT_MOUNT_DIR) + "/" +
                                   session_id);
              clone->set_mount_location(location);
            }
        }

        log_debug(DEBUG_NOTICE)
          << format("Mount Location: %1%") % clone->get_mount_location()
          << endl;

        chroot::facet_list& facets = clone->get_facets();

        for (chroot::facet_list::iterator facet = facets.begin();
             facet != facets.end();)
          {
            chroot::facet_list::iterator current = facet;
            ++facet;
            auto setup_facet = std::dynamic_pointer_cast<session_setup>(*current);
            if (setup_facet)
              {
                setup_facet->chroot_session_setup
                  (*owner, session_id, alias, user, root);
              }
          }

        return clone;
      }

      chroot::session_flags
      session_clonable::get_session_flags (chroot const& chroot) const
      {
        return chroot::SESSION_CREATE;
      }

    }
  }
}