1 | /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | |
---|
21 | #include "dchroot-dsa-session.h" |
---|
22 | |
---|
23 | #include <cassert> |
---|
24 | #include <cerrno> |
---|
25 | #include <cstdlib> |
---|
26 | #include <cstring> |
---|
27 | #include <iostream> |
---|
28 | #include <memory> |
---|
29 | |
---|
30 | #include <unistd.h> |
---|
31 | |
---|
32 | #include <syslog.h> |
---|
33 | |
---|
34 | #include <boost/format.hpp> |
---|
35 | |
---|
36 | #include <uuid/uuid.h> |
---|
37 | |
---|
38 | using std::cout; |
---|
39 | using std::endl; |
---|
40 | using sbuild::_; |
---|
41 | using sbuild::auth; |
---|
42 | using boost::format; |
---|
43 | using namespace dchroot_dsa; |
---|
44 | |
---|
45 | session::session (std::string const& service, |
---|
46 | config_ptr& config, |
---|
47 | operation operation, |
---|
48 | sbuild::string_list const& chroots, |
---|
49 | bool compat): |
---|
50 | dchroot::session_base(service, config, operation, chroots, compat) |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | session::~session () |
---|
55 | { |
---|
56 | } |
---|
57 | |
---|
58 | sbuild::auth::status |
---|
59 | session::get_chroot_auth_status (sbuild::auth::status status, |
---|
60 | sbuild::chroot::ptr const& chroot) const |
---|
61 | { |
---|
62 | /* DSA dchroot checks for a valid user in the groups list, unless |
---|
63 | the groups lists is empty in which case there are no |
---|
64 | restrictions. This only applies if not switching users (dchroot |
---|
65 | does not support user switching) */ |
---|
66 | |
---|
67 | if (get_compat() == true) |
---|
68 | { |
---|
69 | sbuild::string_list const& users = chroot->get_users(); |
---|
70 | sbuild::string_list const& groups = chroot->get_groups(); |
---|
71 | |
---|
72 | if (get_auth()->get_ruid() == get_auth()->get_uid() && |
---|
73 | users.empty() && groups.empty()) |
---|
74 | status = auth::change_auth(status, auth::STATUS_NONE); |
---|
75 | else |
---|
76 | status = |
---|
77 | auth::change_auth(status, |
---|
78 | sbuild::session::get_chroot_auth_status(status, |
---|
79 | chroot)); |
---|
80 | } |
---|
81 | else // schroot compatibility |
---|
82 | { |
---|
83 | status = |
---|
84 | auth::change_auth(status, |
---|
85 | sbuild::session::get_chroot_auth_status(status, |
---|
86 | chroot)); |
---|
87 | } |
---|
88 | |
---|
89 | return status; |
---|
90 | } |
---|
91 | |
---|
92 | sbuild::string_list |
---|
93 | session::get_login_directories () const |
---|
94 | { |
---|
95 | sbuild::string_list ret; |
---|
96 | |
---|
97 | std::string const& wd(get_auth()->get_wd()); |
---|
98 | if (!wd.empty()) |
---|
99 | { |
---|
100 | // Set specified working directory. |
---|
101 | ret.push_back(wd); |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | ret.push_back(get_auth()->get_home()); |
---|
106 | |
---|
107 | // Final fallback to root. |
---|
108 | if (std::find(ret.begin(), ret.end(), "/") == ret.end()) |
---|
109 | ret.push_back("/"); |
---|
110 | } |
---|
111 | |
---|
112 | return ret; |
---|
113 | } |
---|
114 | |
---|
115 | void |
---|
116 | session::get_user_command (sbuild::chroot::ptr& session_chroot, |
---|
117 | std::string& file, |
---|
118 | sbuild::string_list& command) const |
---|
119 | { |
---|
120 | std::string programstring = command[0]; |
---|
121 | file = programstring; |
---|
122 | |
---|
123 | if (!sbuild::is_absname(file)) |
---|
124 | throw error(file, COMMAND_ABS); |
---|
125 | |
---|
126 | std::string commandstring = sbuild::string_list_to_string(command, " "); |
---|
127 | sbuild::log_debug(sbuild::DEBUG_NOTICE) |
---|
128 | << format("Running command: %1%") % commandstring << endl; |
---|
129 | if (get_auth()->get_uid() == 0 || |
---|
130 | get_auth()->get_ruid() != get_auth()->get_uid()) |
---|
131 | syslog(LOG_USER|LOG_NOTICE, "[%s chroot] (%s->%s) Running command: \"%s\"", |
---|
132 | session_chroot->get_name().c_str(), |
---|
133 | get_auth()->get_ruser().c_str(), |
---|
134 | get_auth()->get_user().c_str(), |
---|
135 | commandstring.c_str()); |
---|
136 | |
---|
137 | if (get_auth()->get_verbosity() != auth::VERBOSITY_QUIET) |
---|
138 | { |
---|
139 | std::string format_string; |
---|
140 | // TRANSLATORS: %1% = chroot name |
---|
141 | // TRANSLATORS: %2% = command |
---|
142 | format_string = (_("[%1% chroot] Running command: \"%2%\"")); |
---|
143 | |
---|
144 | format fmt(format_string); |
---|
145 | fmt % session_chroot->get_name() |
---|
146 | % programstring; |
---|
147 | sbuild::log_info() << fmt << endl; |
---|
148 | } |
---|
149 | } |
---|