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
|
/* Copyright (C) 2014 Canonical Ltd.
* Author: Colin Watson <cjwatson@ubuntu.com>
*
* This program 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; version 3 of the License.
*
* This program 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/>.
*/
/* The Click D-Bus service. */
namespace Click {
[DBus (name = "com.ubuntu.Click")]
public class DBusService : Object, DBusInterface {
private DBusConnection conn;
private DBusProxy proxy_uid;
public DBusService (DBusConnection conn) throws Error
{
this.conn = conn;
proxy_uid = new DBusProxy.sync
(conn,
DBusProxyFlags.DO_NOT_LOAD_PROPERTIES |
DBusProxyFlags.DO_NOT_CONNECT_SIGNALS,
null,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus");
}
private string
get_connected_username (BusName sender) throws Error
{
var uid_reply = proxy_uid.call_sync
("GetConnectionUnixUser", new Variant ("(s)", sender),
DBusCallFlags.NONE, -1);
uint uid;
uid_reply.get ("(u)", out uid);
errno = 0;
unowned Posix.Passwd pw = Posix.getpwuid (uid);
if (pw == null)
throw new DBusError.AUTHENTICATION
("Cannot get password file entry for UID " +
"%d: %s", uid, strerror (errno));
return pw.pw_name;
}
private void
priv_drop_failure (string name) throws DBusError
{
throw new DBusError.DROP_PRIVS
("Cannot drop privileges (%s): %s",
name, strerror (errno));
}
private void
drop_privileges (string username) throws Error
{
if (Posix.geteuid () != 0)
return;
unowned Posix.Passwd pw = Posix.getpwnam (username);
if (pw == null)
throw new DBusError.DROP_PRIVS
("Cannot get password file entry for user " +
"'%s': %s", username, strerror (errno));
Posix.gid_t[] supp = {};
Posix.setgrent ();
unowned PosixExtra.Group? gr;
while ((gr = PosixExtra.getgrent ()) != null) {
foreach (unowned string member in gr.gr_mem) {
if (member == username) {
supp += gr.gr_gid;
break;
}
}
}
Posix.endgrent ();
if (PosixExtra.setgroups (supp.length, supp) < 0)
priv_drop_failure ("setgroups");
/* Portability note: this assumes that we have
* [gs]etres[gu]id, which is true on Linux but not
* necessarily elsewhere. If you need to support something
* else, there are reasonably standard alternatives
* involving other similar calls; see e.g.
* gnulib/lib/idpriv-drop.c.
*/
if (PosixExtra.setresgid (pw.pw_gid, pw.pw_gid, pw.pw_gid) < 0)
priv_drop_failure ("setresgid");
if (PosixExtra.setresuid (pw.pw_uid, pw.pw_uid, pw.pw_uid) < 0)
priv_drop_failure ("setresuid");
{
Posix.uid_t ruid, euid, suid;
Posix.gid_t rgid, egid, sgid;
assert (PosixExtra.getresuid (out ruid, out euid,
out suid) == 0 &&
ruid == pw.pw_uid && euid == pw.pw_uid &&
suid == pw.pw_uid);
assert (PosixExtra.getresgid (out rgid, out egid,
out sgid) == 0 &&
rgid == pw.pw_gid && egid == pw.pw_gid &&
sgid == pw.pw_gid);
}
Posix.umask (get_umask () | Posix.S_IWOTH);
}
private bool
euid_access (string username, string path, int mode) throws Error
{
var pid = Posix.fork ();
if (pid < 0)
throw new DBusError.DROP_PRIVS
("Cannot fork: %s", strerror (errno));
else if (pid == 0) { /* child */
drop_privileges (username);
Posix._exit (Posix.access (path, mode) == 0 ? 0 : 1);
throw new DBusError.DROP_PRIVS ("Unreachable");
} else { /* parent */
int status;
if (Posix.waitpid (pid, out status, 0) < 0)
throw new DBusError.DROP_PRIVS
("Cannot wait for privilege-dropped " +
"subprocess: %s", strerror (errno));
return status == 0;
}
}
private Json.Object
get_manifest (string filename) throws Error
{
string[] command = {"click", "info", filename};
string manifest_text;
string click_stderr;
int click_status;
Process.spawn_sync
(null, command, null, SpawnFlags.SEARCH_PATH, null,
out manifest_text, out click_stderr,
out click_status);
try {
Process.check_exit_status (click_status);
} catch (Error e) {
throw new DBusError.MANIFEST
("\"click info %s\" failed.\n%s",
filename, click_stderr);
}
var parser = new Json.Parser ();
parser.load_from_data (manifest_text);
return parser.get_root ().get_object ();
}
private string?
get_field_string (Json.Object manifest, string field)
{
var node = manifest.get_member (field);
return node != null ? node.dup_string () : null;
}
private Json.Object?
get_field_object (Json.Object manifest, string field)
{
var node = manifest.get_member (field);
return node != null ? node.dup_object () : null;
}
private bool
get_field_boolean (Json.Object manifest, string field, bool def)
{
var node = manifest.get_member (field);
return node != null ? node.get_boolean () : def;
}
private void
installed_manifest (Json.Object manifest, string username,
out string package, out string version,
ref HashTable<string, Variant> details)
{
package = get_field_string (manifest, "name");
version = get_field_string (manifest, "version");
if (package == null || version == null)
return;
var architecture = get_field_string (manifest, "architecture");
if (architecture != null)
details["architecture"] = architecture;
/* A missing "_removable" entry in the manifest means that
* we just installed the package, so it must be removable.
*/
details["removable"] = get_field_boolean
(manifest, "_removable", true);
string[] app_names = {};
var hooks = get_field_object (manifest, "hooks");
if (hooks != null) {
foreach (var app_name in hooks.get_members ())
app_names += app_name;
details["app_names"] = app_names;
}
}
public void
install_file (string filename, InstallFlags flags,
out string package, out string version,
out HashTable<string, Variant> details,
BusName sender)
throws Error
{
var username = get_connected_username (sender);
message (@"Click: installing $filename for $username");
if (! euid_access (username, filename, Posix.R_OK))
throw new DBusError.FAILED_TO_INSTALL
("User %s cannot read %s", username, filename);
string[] command = {"click", "install", @"--user=$username"};
if (InstallFlags.FORCE_MISSING_FRAMEWORK in flags)
command += "--force-missing-framework";
if (InstallFlags.ALLOW_UNAUTHENTICATED in flags)
command += "--allow-unauthenticated";
command += filename;
string click_stderr;
int click_status;
Process.spawn_sync
(null, command, null,
SpawnFlags.SEARCH_PATH |
SpawnFlags.STDOUT_TO_DEV_NULL,
null, null, out click_stderr, out click_status);
try {
Process.check_exit_status (click_status);
} catch (Error e) {
throw new DBusError.FAILED_TO_INSTALL
("%s failed to install.\n%s",
filename, click_stderr);
}
package = "";
version = "";
details = new HashTable<string, Variant> (str_hash, str_equal);
try {
installed_manifest
(get_manifest (filename), username,
out package, out version, ref details);
} catch (Error e) {
stderr.printf ("Failed to get installed package " +
"manifest: %s", e.message);
}
}
public void
remove_package (string package, string version, BusName sender)
throws Error
{
var username = get_connected_username (sender);
message (@"Click: removing $package $version for $username");
var db = new Click.DB ();
db.read ();
var registry = new Click.User.for_user (db, username);
var old_version = registry.get_version (package);
if (old_version != version) {
throw new DBusError.FAILED_TO_REMOVE
("Not removing Click package %s %s; " +
"does not match current version %s.",
package, version, old_version);
}
registry.remove (package);
db.maybe_remove (package, version);
/* TODO: remove data? */
}
private static void
on_bus_acquired (DBusConnection conn)
{
try {
conn.register_object
("/com/ubuntu/Click", new DBusService (conn));
} catch (Error e) {
stderr.printf ("Could not register service: %s\n",
e.message);
Process.exit (Posix.EXIT_FAILURE);
}
}
static void
main ()
{
Environment.set_variable ("CLICK_IN_DBUS_SERVICE", "1", true);
Bus.own_name
(BusType.SYSTEM, "com.ubuntu.Click",
BusNameOwnerFlags.NONE, on_bus_acquired, () => {},
() => stderr.printf ("Could not acquire name\n"));
new MainLoop ().run ();
}
}
}
|