summaryrefslogtreecommitdiff
path: root/src/gclue-client-info.c
blob: 7fc2ac7bbc6958fcc2671a07f0f4b3691ac3e644 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* vim: set et ts=8 sw=8: */
/* gclue-client-info.c
 *
 * Copyright 2013 Red Hat, Inc.
 *
 * Geoclue 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 2 of the License, or (at your option)
 * any later version.
 *
 * Geoclue 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 Geoclue; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 */

#include <glib/gi18n.h>

#include "gclue-client-info.h"

#define MAX_CMDLINE_LEN 4096

static void
gclue_client_info_async_initable_init (GAsyncInitableIface *iface);

struct _GClueClientInfoPrivate
{
        char *bus_name;
        GDBusConnection *connection;
        GDBusProxy *dbus_proxy;
        guint watch_id;

        guint32 user_id;
        char *xdg_id;
};

G_DEFINE_TYPE_WITH_CODE (GClueClientInfo,
                         gclue_client_info,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE,
                                                gclue_client_info_async_initable_init)
                         G_ADD_PRIVATE (GClueClientInfo));

enum
{
        PROP_0,
        PROP_PEER,
        PROP_CONNECTION,
        LAST_PROP
};

static GParamSpec *gParamSpecs[LAST_PROP];

enum {
        PEER_VANISHED,
        SIGNAL_LAST
};

static guint signals[SIGNAL_LAST];

static void
gclue_client_info_finalize (GObject *object)
{
        GClueClientInfoPrivate *priv = GCLUE_CLIENT_INFO (object)->priv;

        if (priv->watch_id != 0) {
                g_bus_unwatch_name (priv->watch_id);
                priv->watch_id = 0;
        }

        g_clear_object (&priv->dbus_proxy);
        g_clear_pointer (&priv->bus_name, g_free);
        g_clear_pointer (&priv->xdg_id, g_free);
        g_clear_object (&priv->connection);

        /* Chain up to the parent class */
        G_OBJECT_CLASS (gclue_client_info_parent_class)->finalize (object);
}

static void
gclue_client_info_get_property (GObject    *object,
                                guint       prop_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
        GClueClientInfo *info = GCLUE_CLIENT_INFO (object);

        switch (prop_id) {
        case PROP_PEER:
                g_value_set_string (value, info->priv->bus_name);
                break;

        case PROP_CONNECTION:
                g_value_set_object (value, info->priv->connection);
                break;

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
gclue_client_info_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
        GClueClientInfo *info = GCLUE_CLIENT_INFO (object);

        switch (prop_id) {
        case PROP_PEER:
                info->priv->bus_name = g_value_dup_string (value);
                break;

        case PROP_CONNECTION:
                info->priv->connection = g_value_dup_object (value);
                break;

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
gclue_client_info_class_init (GClueClientInfoClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);
        object_class->finalize = gclue_client_info_finalize;
        object_class->get_property = gclue_client_info_get_property;
        object_class->set_property = gclue_client_info_set_property;

        gParamSpecs[PROP_PEER] = g_param_spec_string ("bus-name",
                                                      "BusName",
                                                      "Bus name of client",
                                                      NULL,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class,
                                         PROP_PEER,
                                         gParamSpecs[PROP_PEER]);

        gParamSpecs[PROP_CONNECTION] = g_param_spec_object ("connection",
                                                            "Connection",
                                                            "DBus Connection",
                                                            G_TYPE_DBUS_CONNECTION,
                                                            G_PARAM_READWRITE |
                                                            G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class,
                                         PROP_CONNECTION,
                                         gParamSpecs[PROP_CONNECTION]);

        signals[PEER_VANISHED] =
                g_signal_new ("peer-vanished",
                              GCLUE_TYPE_CLIENT_INFO,
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (GClueClientInfoClass,
                                               peer_vanished),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0,
                              G_TYPE_NONE);
}

static void
on_name_vanished (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
{
        g_signal_emit (GCLUE_CLIENT_INFO (user_data),
                       signals[PEER_VANISHED],
                       0);
}


static gchar *
parse_cgroup_v2 (GStrv lines)
{
        const char *unit, *name;
        char *dash, *xdg_id;
        g_autofree char *scope = NULL;

        /* Cgroup v2 is always a single line:
         * 0::/user.slice/user-1000.slice/user@1000.service/app.slice/app-flatpak-org.gnome.Maps-3358.scope
         */
        if (g_strv_length (lines) != 2)
                return NULL;

        if (!g_str_has_prefix (lines[0], "0::"))
                 return NULL;

        unit = lines[0] + strlen ("0::");
        scope = g_path_get_basename (unit);
        if (!g_str_has_prefix (scope, "app-flatpak-") ||
            !g_str_has_suffix (scope, ".scope"))
                return NULL;

        name = scope + strlen("app-flatpak-");
        dash = strchr (name, '-');
        if (dash == NULL)
                return NULL;
        *dash = 0;

        xdg_id = g_strdup (name);
        g_debug ("Found xdg_id %s", xdg_id);

        return xdg_id;
}


/* Based on got_credentials_cb() from xdg-app source code */
static char *
get_xdg_id (guint32 pid)
{
        char *xdg_id = NULL;
        g_autofree char *path = NULL;
        g_autofree char *content = NULL;
        g_auto(GStrv) lines = NULL;
        int i;

        path = g_strdup_printf ("/proc/%u/cgroup", pid);

        if (!g_file_get_contents (path, &content, NULL, NULL))
                return NULL;
        lines =  g_strsplit (content, "\n", -1);

	xdg_id = parse_cgroup_v2 (lines);
	if (xdg_id != NULL)
		return xdg_id;

        for (i = 0; lines[i] != NULL; i++) {
                const char *unit = lines[i] + strlen ("1:name=systemd:");
                g_autofree char *scope = NULL;
                const char *name;
                char *dash;
                const char *prefix = NULL;

                if (!g_str_has_prefix (lines[i], "1:name=systemd:"))
                        continue;

                scope = g_path_get_basename (unit);

                if (g_str_has_prefix (scope, "xdg-app-"))
                        prefix = "xdg-app-";
                else if (g_str_has_prefix (scope, "flatpak-"))
                        prefix = "flatpak-";
                else if (g_str_has_prefix (scope, "app-flatpak-"))
                        prefix = "app-flatpak-";

                if (prefix == NULL || !g_str_has_suffix (scope, ".scope"))
                        break;

                name = scope + strlen(prefix);
                dash = strchr (name, '-');

                if (dash == NULL)
                        break;

                *dash = 0;
                xdg_id = g_strdup (name);
        }

        return xdg_id;
}

static void
on_get_pid_ready (GObject      *source_object,
                  GAsyncResult *res,
                  gpointer      user_data)
{
        GTask *task = G_TASK (user_data);
        gpointer *info = g_task_get_source_object (task);
        GClueClientInfoPrivate *priv = GCLUE_CLIENT_INFO (info)->priv;
        guint32 pid;
        GError *error = NULL;
        GVariant *results = NULL;

        results = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object),
                                            res,
                                            &error);
        if (results == NULL) {
                g_task_return_error (task, error);
                g_object_unref (task);

                return;
        }

        g_assert (g_variant_n_children (results) > 0);
        g_variant_get_child (results, 0, "u", &pid);
        g_variant_unref (results);

        priv->xdg_id = get_xdg_id (pid);

        priv->watch_id = g_bus_watch_name_on_connection (priv->connection,
                                                         priv->bus_name,
                                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
                                                         NULL,
                                                         on_name_vanished,
                                                         info,
                                                         NULL);

        g_task_return_boolean (task, TRUE);

        g_object_unref (task);
}

static void
on_get_user_id_ready (GObject      *source_object,
                      GAsyncResult *res,
                      gpointer      user_data)
{
        GTask *task = G_TASK (user_data);
        gpointer *info = g_task_get_source_object (task);
        GClueClientInfoPrivate *priv = GCLUE_CLIENT_INFO (info)->priv;
        GError *error = NULL;
        GVariant *results = NULL;

        results = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object),
                                            res,
                                            &error);
        if (results == NULL) {
                g_task_return_error (task, error);
                g_object_unref (task);

                return;
        }

        g_assert (g_variant_n_children (results) > 0);
        g_variant_get_child (results, 0, "u", &priv->user_id);
        g_variant_unref (results);

        g_dbus_proxy_call (priv->dbus_proxy,
                           "GetConnectionUnixProcessID",
                           g_variant_new ("(s)", priv->bus_name),
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
                           g_task_get_cancellable (task),
                           on_get_pid_ready,
                           task);
}

static void
on_dbus_proxy_ready (GObject      *source_object,
                     GAsyncResult *res,
                     gpointer      user_data)
{
        GTask *task = G_TASK (user_data);
        gpointer *info = g_task_get_source_object (task);
        GClueClientInfoPrivate *priv = GCLUE_CLIENT_INFO (info)->priv;
        GError *error = NULL;

        priv->dbus_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
        if (priv->dbus_proxy == NULL) {
                g_task_return_error (task, error);
                g_object_unref (task);
                return;
        }

        g_dbus_proxy_call (priv->dbus_proxy,
                           "GetConnectionUnixUser",
                           g_variant_new ("(s)", priv->bus_name),
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
                           g_task_get_cancellable (task),
                           on_get_user_id_ready,
                           task);
}

static void
gclue_client_info_init_async (GAsyncInitable     *initable,
                              int                 io_priority,
                              GCancellable       *cancellable,
                              GAsyncReadyCallback callback,
                              gpointer            user_data)
{
        GTask *task;

        task = g_task_new (initable, cancellable, callback, user_data);

        g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
                                  NULL,
                                  "org.freedesktop.DBus",
                                  "/org/freedesktop/DBus",
                                  "org.freedesktop.DBus",
                                  cancellable,
                                  on_dbus_proxy_ready,
                                  task);
}

static gboolean
gclue_client_info_init_finish (GAsyncInitable *initable,
                               GAsyncResult   *result,
                               GError        **error)
{
        return g_task_propagate_boolean (G_TASK (result), error);
}

static void
gclue_client_info_async_initable_init (GAsyncInitableIface *iface)
{
        iface->init_async = gclue_client_info_init_async;
        iface->init_finish = gclue_client_info_init_finish;
}

static void
gclue_client_info_init (GClueClientInfo *info)
{
        info->priv = gclue_client_info_get_instance_private(info);
}

void
gclue_client_info_new_async (const char         *bus_name,
                             GDBusConnection    *connection,
                             GCancellable       *cancellable,
                             GAsyncReadyCallback callback,
                             gpointer            user_data)
{
        g_async_initable_new_async (GCLUE_TYPE_CLIENT_INFO,
                                    G_PRIORITY_DEFAULT,
                                    cancellable,
                                    callback,
                                    user_data,
                                    "bus-name", bus_name,
                                    "connection", connection,
                                    NULL);
}

GClueClientInfo *
gclue_client_info_new_finish (GAsyncResult *res,
                              GError      **error)
{
        GObject *object;
        GObject *source_object;

        source_object = g_async_result_get_source_object (res);
        object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
                                              res,
                                              error);
        g_object_unref (source_object);
        if (object != NULL)
                return GCLUE_CLIENT_INFO (object);
        else
                return NULL;
}

const gchar *
gclue_client_info_get_bus_name (GClueClientInfo *info)
{
        g_return_val_if_fail (GCLUE_IS_CLIENT_INFO(info), NULL);

        return info->priv->bus_name;
}

guint32
gclue_client_info_get_user_id (GClueClientInfo *info)
{
        g_return_val_if_fail (GCLUE_IS_CLIENT_INFO(info), 0);

        return info->priv->user_id;
}

gboolean
gclue_client_info_check_bus_name (GClueClientInfo *info,
                                  const char      *bus_name)
{
        g_return_val_if_fail (GCLUE_IS_CLIENT_INFO(info), FALSE);

        return (strcmp (bus_name, info->priv->bus_name) == 0);
}

const char *
gclue_client_info_get_xdg_id (GClueClientInfo *info)
{
        g_return_val_if_fail (GCLUE_IS_CLIENT_INFO(info), FALSE);

        return info->priv->xdg_id;
}