summaryrefslogtreecommitdiff
path: root/clients/common/nm-polkit-listener.c
blob: 29c25b4e6739e152767c2c903166cf8dd69897bb (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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2014 Red Hat, Inc.
 */

/**
 * SECTION:nm-polkit-listener
 * @short_description: A polkit agent listener
 *
 * #NMPolkitListener is the polkit agent listener used by nmcli and nmtui.
 * http://www.freedesktop.org/software/polkit/docs/latest/index.html
 *
 * For an example polkit agent you can look at polkit source tree:
 * http://cgit.freedesktop.org/polkit/tree/src/polkitagent/polkitagenttextlistener.c
 * http://cgit.freedesktop.org/polkit/tree/src/programs/pkttyagent.c
 * or LXDE polkit agent:
 * http://git.lxde.org/gitweb/?p=debian/lxpolkit.git;a=blob;f=src/lxpolkit-listener.c
 * https://github.com/lxde/lxqt-policykit/tree/master/src
 */

#include "libnm-client-aux-extern/nm-default-client.h"

#include "nm-polkit-listener.h"

#include <gio/gio.h>
#include <pwd.h>
#include <fcntl.h>

#include "libnm-glib-aux/nm-dbus-aux.h"
#include "libnm-glib-aux/nm-str-buf.h"
#include "libnm-glib-aux/nm-secret-utils.h"
#include "libnm-glib-aux/nm-io-utils.h"
#include "libnm-core-aux-intern/nm-auth-subject.h"
#include "c-list/src/c-list.h"

#define LOGIND_BUS_NAME "org.freedesktop.login1"
#define POLKIT_BUS_NAME "org.freedesktop.PolicyKit1"

#define POLKIT_AUTHORITY_OBJ_PATH   "/org/freedesktop/PolicyKit1/Authority"
#define POLKIT_AUTHORITY_IFACE_NAME "org.freedesktop.PolicyKit1.Authority"

#define POLKIT_AGENT_OBJ_PATH       "/org/freedesktop/PolicyKit1/AuthenticationAgent"
#define POLKIT_AGENT_DBUS_INTERFACE "org.freedesktop.PolicyKit1.AuthenticationAgent"

#define LOGIND_OBJ_PATH          "/org/freedesktop/login1"
#define LOGIND_MANAGER_INTERFACE "org.freedesktop.login1.Manager"

#define NM_POLKIT_LISTENER_DBUS_CONNECTION "dbus-connection"
#define NM_POLKIT_LISTENER_SESSION_AGENT   "session-agent"

#define POLKIT_DBUS_ERROR_FAILED "org.freedesktop.PolicyKit1.Error.Failed"

/*****************************************************************************/

enum { REGISTERED, REQUEST_SYNC, ERROR, LAST_SIGNAL };

static guint signals[LAST_SIGNAL] = {0};

struct _NMPolkitListener {
    GObject          parent;
    GDBusConnection *dbus_connection;
    char *           name_owner;
    GCancellable *   cancellable;
    GMainContext *   main_context;
    CList            request_lst_head;
    guint            pk_auth_agent_reg_id;
    guint            name_owner_changed_id;
    bool             session_agent : 1;
};

struct _NMPolkitListenerClass {
    GObjectClass parent;
};

G_DEFINE_TYPE(NMPolkitListener, nm_polkit_listener, G_TYPE_OBJECT);

/*****************************************************************************/

typedef struct {
    CList request_lst;

    NMPolkitListener *     listener;
    GSource *              child_stdout_watch_source;
    GSource *              child_stdin_watch_source;
    GDBusMethodInvocation *dbus_invocation;
    char *                 action_id;
    char *                 message;
    char *                 username;
    char *                 cookie;
    NMStrBuf               in_buffer;
    NMStrBuf               out_buffer;
    gsize                  out_buffer_offset;

    int child_stdout;
    int child_stdin;

    bool request_any_response : 1;
    bool request_is_completed : 1;
} AuthRequest;

static const GDBusInterfaceInfo interface_info = NM_DEFINE_GDBUS_INTERFACE_INFO_INIT(
    POLKIT_AGENT_DBUS_INTERFACE,
    .methods = NM_DEFINE_GDBUS_METHOD_INFOS(
        NM_DEFINE_GDBUS_METHOD_INFO("BeginAuthentication",
                                    .in_args = NM_DEFINE_GDBUS_ARG_INFOS(
                                        NM_DEFINE_GDBUS_ARG_INFO("action_id", "s"),
                                        NM_DEFINE_GDBUS_ARG_INFO("message", "s"),
                                        NM_DEFINE_GDBUS_ARG_INFO("icon_name", "s"),
                                        NM_DEFINE_GDBUS_ARG_INFO("details", "a{ss}"),
                                        NM_DEFINE_GDBUS_ARG_INFO("cookie", "s"),
                                        NM_DEFINE_GDBUS_ARG_INFO("identities", "a(sa{sv})"), ), ),
        NM_DEFINE_GDBUS_METHOD_INFO("CancelAuthentication",
                                    .in_args = NM_DEFINE_GDBUS_ARG_INFOS(
                                        NM_DEFINE_GDBUS_ARG_INFO("cookie", "s"), ), ), ), );

static void
auth_request_complete(AuthRequest *request, gboolean success)
{
    c_list_unlink(&request->request_lst);

    if (success)
        g_dbus_method_invocation_return_value(request->dbus_invocation, NULL);
    else {
        g_dbus_method_invocation_return_dbus_error(request->dbus_invocation,
                                                   POLKIT_DBUS_ERROR_FAILED,
                                                   "");
    }

    nm_clear_g_free(&request->action_id);
    nm_clear_g_free(&request->message);
    nm_clear_g_free(&request->username);
    nm_clear_g_free(&request->cookie);
    nm_clear_g_source_inst(&request->child_stdout_watch_source);
    nm_clear_g_source_inst(&request->child_stdin_watch_source);

    nm_str_buf_destroy(&request->in_buffer);
    nm_str_buf_destroy(&request->out_buffer);

    if (request->child_stdout != -1) {
        nm_close(request->child_stdout);
        request->child_stdout = -1;
    }

    if (request->child_stdin != -1) {
        nm_close(request->child_stdin);
        request->child_stdin = -1;
    }

    nm_g_slice_free(request);
}

static gboolean
uid_to_name(uid_t uid, gboolean *out_cached, char **out_name)
{
    if (!*out_cached) {
        *out_cached = TRUE;
        *out_name   = nm_utils_uid_to_name(uid);
    }
    return !!(*out_name);
}

static char *
choose_identity(GVariant *identities)
{
    GVariantIter  identity_iter;
    GVariant *    details_tmp;
    const char *  kind;
    gs_free char *username_first = NULL;
    gs_free char *username_root  = NULL;
    const char *  user;

    /* Choose identity. First try current user, then root, and else
     * take the first one we find. */

    user = getenv("USER");

    g_variant_iter_init(&identity_iter, identities);
    while (g_variant_iter_next(&identity_iter, "(&s@a{sv})", &kind, &details_tmp)) {
        gs_unref_variant GVariant *details = g_steal_pointer(&details_tmp);

        if (nm_streq(kind, "unix-user")) {
            gs_unref_variant GVariant *v = NULL;

            v = g_variant_lookup_value(details, "uid", G_VARIANT_TYPE_UINT32);
            if (v) {
                guint32       uid    = g_variant_get_uint32(v);
                gs_free char *u      = NULL;
                gboolean      cached = FALSE;

                if (user) {
                    if (!uid_to_name(uid, &cached, &u))
                        continue;
                    if (nm_streq(u, user))
                        return g_steal_pointer(&u);
                }
                if (!username_root && uid == 0) {
                    if (!uid_to_name(uid, &cached, &u))
                        continue;
                    username_root = g_strdup(u);
                    if (!user)
                        break;
                }
                if (!username_root && !username_first) {
                    if (!uid_to_name(uid, &cached, &u))
                        continue;
                    username_first = g_strdup(u);
                }
            }
        }
    }

    if (username_root)
        return g_steal_pointer(&username_root);

    if (username_first)
        return g_steal_pointer(&username_first);

    return NULL;
}

static void
agent_register_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
    NMPolkitListener *listener        = NM_POLKIT_LISTENER(user_data);
    GDBusConnection * dbus_connection = G_DBUS_CONNECTION(source_object);
    gs_free_error GError *error       = NULL;
    gs_unref_variant GVariant *ret    = NULL;

    ret = g_dbus_connection_call_finish(dbus_connection, res, &error);

    if (nm_utils_error_is_cancelled(error)) {
        return;
    }

    if (ret) {
        g_signal_emit(listener, signals[REGISTERED], 0);
    } else {
        g_signal_emit(listener, signals[ERROR], 0, error->message);
    }
}

static void
agent_register(NMPolkitListener *self, const char *session_id)
{
    const char *    locale                         = NULL;
    gs_unref_object NMAuthSubject *subject         = NULL;
    GVariant *                     subject_variant = NULL;

    locale = g_getenv("LANG");
    if (locale == NULL) {
        locale = "en_US.UTF-8";
    }

    if (self->session_agent) {
        subject = nm_auth_subject_new_unix_session(session_id);
    } else {
        subject = nm_auth_subject_new_unix_process_self();
    }
    subject_variant = nm_auth_subject_unix_to_polkit_gvariant(subject);

    g_dbus_connection_call(
        self->dbus_connection,
        self->name_owner,
        POLKIT_AUTHORITY_OBJ_PATH,
        POLKIT_AUTHORITY_IFACE_NAME,
        "RegisterAuthenticationAgent",
        g_variant_new("(@(sa{sv})ss)", subject_variant, locale, POLKIT_AGENT_OBJ_PATH),
        NULL,
        G_DBUS_CALL_FLAGS_NONE,
        -1,
        self->cancellable,
        agent_register_cb,
        self);
}

static void
agent_unregister(NMPolkitListener *self)
{
    gs_unref_object NMAuthSubject *subject         = NULL;
    GVariant *                     subject_variant = NULL;

    subject         = nm_auth_subject_new_unix_process_self();
    subject_variant = nm_auth_subject_unix_to_polkit_gvariant(subject);

    g_dbus_connection_call(self->dbus_connection,
                           self->name_owner,
                           POLKIT_AUTHORITY_OBJ_PATH,
                           POLKIT_AUTHORITY_IFACE_NAME,
                           "UnregisterAuthenticationAgent",
                           g_variant_new("(@(sa{sv})s)", subject_variant, POLKIT_AGENT_OBJ_PATH),
                           NULL,
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
                           NULL,
                           NULL,
                           self);
}

static void
retrieve_session_id_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
    NMPolkitListener *        listener = NM_POLKIT_LISTENER(user_data);
    char *                    session_id;
    guint32                   session_uid;
    nm_auto_free_variant_iter GVariantIter *iter = NULL;
    gs_unref_variant GVariant *ret               = NULL;
    gs_free_error GError *error                  = NULL;
    gs_free char *        err_str                = NULL;
    uid_t                 uid                    = getuid();

    ret = g_dbus_connection_call_finish(listener->dbus_connection, res, &error);

    if (nm_utils_error_is_cancelled(error)) {
        return;
    }

    if (ret) {
        g_variant_get_child(ret, 0, "a(susso)", &iter);

        while (
            g_variant_iter_next(iter, "(&su@s@s@o)", &session_id, &session_uid, NULL, NULL, NULL)) {
            if (session_uid == uid) {
                agent_register(listener, session_id);
                return;
            }
        }
        err_str = g_strdup_printf(_("Could not find any session id for uid %d"), uid);
    } else {
        err_str = g_strdup_printf(_("Could not retrieve session id: %s"), error->message);
    }

    g_signal_emit(listener, signals[ERROR], 0, err_str);
}

static void
retrieve_session_id(NMPolkitListener *self)
{
    g_dbus_connection_call(self->dbus_connection,
                           LOGIND_BUS_NAME,
                           LOGIND_OBJ_PATH,
                           LOGIND_MANAGER_INTERFACE,
                           "ListSessions",
                           NULL,
                           G_VARIANT_TYPE("(a(susso))"),
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
                           self->cancellable,
                           retrieve_session_id_cb,
                           self);
}

static gboolean
io_watch_can_write(int fd, GIOCondition condition, gpointer user_data)
{
    AuthRequest *request = user_data;
    gssize       n_written;

    if (NM_FLAGS_ANY(condition, (G_IO_HUP | G_IO_ERR)))
        goto done;

    n_written =
        write(request->child_stdin,
              &((nm_str_buf_get_str_unsafe(&request->out_buffer))[request->out_buffer_offset]),
              request->out_buffer.len - request->out_buffer_offset);

    if (n_written < 0 && errno != EAGAIN)
        goto done;

    if (n_written > 0) {
        if ((gsize) n_written >= (request->out_buffer.len - request->out_buffer_offset)) {
            nm_assert((gsize) n_written == (request->out_buffer.len - request->out_buffer_offset));
            goto done;
        }
        request->out_buffer_offset += (gsize) n_written;
    }

    return G_SOURCE_CONTINUE;

done:
    nm_str_buf_set_size(&request->out_buffer, 0, TRUE, FALSE);
    request->out_buffer_offset = 0;
    nm_clear_g_source_inst(&request->child_stdin_watch_source);

    if (request->request_is_completed)
        auth_request_complete(request, TRUE);

    return G_SOURCE_CONTINUE;
}

static void
queue_string_to_helper(AuthRequest *request, const char *response)
{
    g_return_if_fail(response);

    if (!nm_str_buf_is_initalized(&request->out_buffer))
        nm_str_buf_init(&request->out_buffer, strlen(response) + 2u, TRUE);

    nm_str_buf_append(&request->out_buffer, response);
    nm_str_buf_ensure_trailing_c(&request->out_buffer, '\n');

    if (!request->child_stdin_watch_source) {
        request->child_stdin_watch_source = nm_g_unix_fd_source_new(request->child_stdin,
                                                                    G_IO_OUT | G_IO_ERR | G_IO_HUP,
                                                                    G_PRIORITY_DEFAULT,
                                                                    io_watch_can_write,
                                                                    request,
                                                                    NULL);
        g_source_attach(request->child_stdin_watch_source, request->listener->main_context);
    }
}

static gboolean
io_watch_have_data(int fd, GIOCondition condition, gpointer user_data)
{
    AuthRequest *request = user_data;
    gboolean     auth_result;
    gssize       n_read;

    if (NM_FLAGS_ANY(condition, G_IO_HUP | G_IO_ERR))
        n_read = -EIO;
    else
        n_read = nm_utils_fd_read(fd, &request->in_buffer);

    if (n_read <= 0) {
        if (n_read == -EAGAIN) {
            /* wait longer. */
            return G_SOURCE_CONTINUE;
        }

        /* Either an error or EOF happened. The data we parsed so far was not relevant.
         * Regardless of what we still have unprocessed in the receive buffers, we are done.
         *
         * We would expect that the other side completed with SUCCESS or FAILURE. Apparently
         * it didn't. If we had any good request, we assume success. */
        auth_result = request->request_any_response;
        goto out;
    }

    while (TRUE) {
        char *      line_terminator;
        const char *line;

        line            = nm_str_buf_get_str(&request->in_buffer);
        line_terminator = (char *) strchr(line, '\n');
        if (!line_terminator) {
            /* We don't have a complete line. Wait longer. */
            return G_SOURCE_CONTINUE;
        }
        line_terminator[0] = '\0';

        if (NM_STR_HAS_PREFIX(line, "PAM_PROMPT_ECHO_OFF ")
            || NM_STR_HAS_PREFIX(line, "PAM_PROMPT_ECHO_ON ")) {
            nm_auto_free_secret char *response = NULL;

            /* FIXME(cli-async): emit signal and wait for response (blocking) */
            g_signal_emit(request->listener,
                          signals[REQUEST_SYNC],
                          0,
                          request->action_id,
                          request->message,
                          request->username,
                          &response);

            if (response) {
                queue_string_to_helper(request, response);
                request->request_any_response = TRUE;
                goto erase_line;
            }
            auth_result = FALSE;
        } else if (nm_streq(line, "SUCCESS"))
            auth_result = TRUE;
        else if (nm_streq(line, "FAILURE"))
            auth_result = FALSE;
        else if (NM_STR_HAS_PREFIX(line, "PAM_ERROR_MSG ")
                 || NM_STR_HAS_PREFIX(line, "PAM_TEXT_INFO ")) {
            /* ignore. */
            goto erase_line;
        } else {
            /* unknown command. Fail. */
            auth_result = FALSE;
        }

        break;
erase_line:
        nm_str_buf_erase(&request->in_buffer, 0, line_terminator - line + 1u, TRUE);
    }

out:
    request->request_is_completed = TRUE;
    nm_clear_g_source_inst(&request->child_stdout_watch_source);
    if (auth_result && request->child_stdin_watch_source) {
        /* we need to wait for the buffer to send the response. */
    } else
        auth_request_complete(request, auth_result);

    return G_SOURCE_CONTINUE;
}

static void
begin_authentication(AuthRequest *request)
{
    int         fd_flags;
    const char *helper_argv[] = {
        POLKIT_AGENT_HELPER_1_PATH,
        request->username,
        NULL,
    };

    if (!request->username) {
        auth_request_complete(request, FALSE);
        return;
    }

    if (!g_spawn_async_with_pipes(NULL,
                                  (char **) helper_argv,
                                  NULL,
                                  G_SPAWN_STDERR_TO_DEV_NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  &request->child_stdin,
                                  &request->child_stdout,
                                  NULL,
                                  NULL)) {
        /* not findind the PolicyKit setuid helper is a critical error */
        request->child_stdin  = -1;
        request->child_stdout = -1;
        g_signal_emit(request->listener,
                      signals[ERROR],
                      0,
                      "The PolicyKit setuid helper 'polkit-agent-helper-1' has not been found");

        auth_request_complete(request, FALSE);
        return;
    }

    fd_flags = fcntl(request->child_stdin, F_GETFD, 0);
    fcntl(request->child_stdin, F_SETFL, fd_flags | O_NONBLOCK);

    fd_flags = fcntl(request->child_stdout, F_GETFD, 0);
    fcntl(request->child_stdout, F_SETFL, fd_flags | O_NONBLOCK);

    request->child_stdout_watch_source = nm_g_unix_fd_source_new(request->child_stdout,
                                                                 G_IO_IN | G_IO_ERR | G_IO_HUP,
                                                                 G_PRIORITY_DEFAULT,
                                                                 io_watch_have_data,
                                                                 request,
                                                                 NULL);
    g_source_attach(request->child_stdout_watch_source, request->listener->main_context);

    /* Write the cookie on stdin so it can't be seen by other processes */
    queue_string_to_helper(request, request->cookie);

    return;
}

static AuthRequest *
get_request(NMPolkitListener *listener, const char *cookie)
{
    AuthRequest *request;

    c_list_for_each_entry (request, &listener->request_lst_head, request_lst) {
        if (nm_streq0(cookie, request->cookie)) {
            return request;
        }
    }
    return NULL;
}

static AuthRequest *
create_request(NMPolkitListener *     listener,
               GDBusMethodInvocation *invocation,
               const char *           action_id,
               const char *           message,
               char *                 username_take,
               const char *           cookie)
{
    AuthRequest *request;

    request  = g_slice_new(AuthRequest);
    *request = (AuthRequest){
        .listener             = listener,
        .dbus_invocation      = invocation,
        .action_id            = g_strdup(action_id),
        .message              = g_strdup(message),
        .username             = g_steal_pointer(&username_take),
        .cookie               = g_strdup(cookie),
        .request_any_response = FALSE,
        .request_is_completed = FALSE,
    };

    nm_str_buf_init(&request->in_buffer, NM_UTILS_GET_NEXT_REALLOC_SIZE_1000, FALSE);

    c_list_link_tail(&listener->request_lst_head, &request->request_lst);
    return request;
}

static void
dbus_method_call_cb(GDBusConnection *      connection,
                    const char *           sender,
                    const char *           object_path,
                    const char *           interface_name,
                    const char *           method_name,
                    GVariant *             parameters,
                    GDBusMethodInvocation *invocation,
                    gpointer               user_data)
{
    NMPolkitListener *listener = NM_POLKIT_LISTENER(user_data);
    const char *      action_id;
    const char *      message;
    const char *      cookie;
    AuthRequest *     request;
    gs_unref_variant GVariant *identities_gvariant = NULL;

    if (nm_streq(method_name, "BeginAuthentication")) {
        g_variant_get(parameters,
                      "(&s&s&s@a{ss}&s@a(sa{sv}))",
                      &action_id,
                      &message,
                      NULL,
                      NULL,
                      &cookie,
                      &identities_gvariant);

        request = create_request(listener,
                                 invocation,
                                 action_id,
                                 message,
                                 choose_identity(identities_gvariant),
                                 cookie);
        begin_authentication(request);
        return;
    }

    if (nm_streq(method_name, "CancelAuthentication")) {
        g_variant_get(parameters, "&s", &cookie);
        request = get_request(listener, cookie);

        if (!request) {
            gs_free char *msg = NULL;

            msg = g_strdup_printf("No pending authentication request for cookie '%s'", cookie);
            g_dbus_method_invocation_return_dbus_error(invocation, POLKIT_DBUS_ERROR_FAILED, msg);
            return;
        }

        /* Complete a cancelled request with success. */
        auth_request_complete(request, TRUE);
        return;
    }

    g_dbus_method_invocation_return_error(invocation,
                                          G_DBUS_ERROR,
                                          G_DBUS_ERROR_UNKNOWN_METHOD,
                                          "Unknown method %s",
                                          method_name);
}

static gboolean
export_dbus_iface(NMPolkitListener *self, GError **error)
{
    GDBusInterfaceVTable interface_vtable = {
        .method_call  = dbus_method_call_cb,
        .set_property = NULL,
        .get_property = NULL,
    };

    g_return_val_if_fail(NM_IS_POLKIT_LISTENER(self), FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

    /* Agent listener iface has been exported already */
    if (self->pk_auth_agent_reg_id) {
        return TRUE;
    }

    self->pk_auth_agent_reg_id =
        g_dbus_connection_register_object(self->dbus_connection,
                                          POLKIT_AGENT_OBJ_PATH,
                                          (GDBusInterfaceInfo *) &interface_info,
                                          &interface_vtable,
                                          self,
                                          NULL,
                                          error);
    if (!self->pk_auth_agent_reg_id) {
        g_signal_emit(self,
                      signals[ERROR],
                      0,
                      "Could not register as a PolicyKit Authentication Agent");
    }
    return self->pk_auth_agent_reg_id;
}

static void
name_owner_changed(NMPolkitListener *self, const char *name_owner)
{
    gs_free_error GError *error = NULL;

    name_owner = nm_str_not_empty(name_owner);

    if (nm_streq0(self->name_owner, name_owner)) {
        return;
    }

    g_free(self->name_owner);
    self->name_owner = g_strdup(name_owner);

    if (!self->name_owner) {
        return;
    }

    if (export_dbus_iface(self, &error)) {
        if (self->session_agent) {
            retrieve_session_id(self);
        } else {
            agent_register(self, NULL);
        }
    } else {
        g_signal_emit(self,
                      signals[ERROR],
                      0,
                      "Could not export the PolicyKit Authentication Agent DBus interface");
    }
}

static void
name_owner_changed_cb(GDBusConnection *connection,
                      const char *     sender_name,
                      const char *     object_path,
                      const char *     interface_name,
                      const char *     signal_name,
                      GVariant *       parameters,
                      gpointer         user_data)
{
    NMPolkitListener *self = user_data;
    const char *      new_owner;

    if (!g_variant_is_of_type(parameters, G_VARIANT_TYPE("(sss)"))) {
        return;
    }

    g_variant_get(parameters, "(&s&s&s)", NULL, NULL, &new_owner);

    name_owner_changed(self, new_owner);
}

static void
get_name_owner_cb(const char *name_owner, GError *error, gpointer user_data)
{
    if (!name_owner && g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
        return;
    }
    name_owner_changed(user_data, name_owner);
}

/*****************************************************************************/

NM_GOBJECT_PROPERTIES_DEFINE(NMPolkitListener, PROP_DBUS_CONNECTION, PROP_SESSION_AGENT, );

static void
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    NMPolkitListener *self = NM_POLKIT_LISTENER(object);

    switch (prop_id) {
    case PROP_DBUS_CONNECTION:
        self->dbus_connection = g_value_dup_object(value);
        break;
    case PROP_SESSION_AGENT:
        self->session_agent = g_value_get_boolean(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nm_polkit_listener_init(NMPolkitListener *self)
{
    c_list_init(&self->request_lst_head);
    self->main_context = g_main_context_ref_thread_default();
}

static void
constructed(GObject *object)
{
    NMPolkitListener *self = NM_POLKIT_LISTENER(object);

    self->cancellable = g_cancellable_new();

    self->name_owner_changed_id =
        nm_dbus_connection_signal_subscribe_name_owner_changed(self->dbus_connection,
                                                               POLKIT_BUS_NAME,
                                                               name_owner_changed_cb,
                                                               self,
                                                               NULL);

    nm_dbus_connection_call_get_name_owner(self->dbus_connection,
                                           POLKIT_BUS_NAME,
                                           -1,
                                           self->cancellable,
                                           get_name_owner_cb,
                                           self);

    G_OBJECT_CLASS(nm_polkit_listener_parent_class)->constructed(object);
}

/**
 * nm_polkit_listener_new:
 * @dbus_connection: a open DBus connection
 * @session_agent: TRUE if a session agent is wanted, FALSE for a process agent
 *
 * Creates a new #NMPolkitListener and registers it as a polkit agent.
 *
 * Returns: a new #NMPolkitListener
 */
NMPolkitListener *
nm_polkit_listener_new(GDBusConnection *dbus_connection, gboolean session_agent)
{
    return g_object_new(NM_TYPE_POLKIT_LISTENER,
                        NM_POLKIT_LISTENER_DBUS_CONNECTION,
                        dbus_connection,
                        NM_POLKIT_LISTENER_SESSION_AGENT,
                        session_agent,
                        NULL);
}

static void
dispose(GObject *object)
{
    NMPolkitListener *self = NM_POLKIT_LISTENER(object);
    AuthRequest *     request;

    nm_clear_g_cancellable(&self->cancellable);

    while ((request = c_list_first_entry(&self->request_lst_head, AuthRequest, request_lst)))
        auth_request_complete(request, FALSE);

    if (self->dbus_connection) {
        nm_clear_g_dbus_connection_signal(self->dbus_connection, &self->name_owner_changed_id);
        g_dbus_connection_unregister_object(self->dbus_connection, self->pk_auth_agent_reg_id);
        agent_unregister(self);
        nm_clear_g_free(&self->name_owner);
        g_clear_object(&self->dbus_connection);
    }

    nm_clear_pointer(&self->main_context, g_main_context_unref);

    G_OBJECT_CLASS(nm_polkit_listener_parent_class)->dispose(object);
}

static void
nm_polkit_listener_class_init(NMPolkitListenerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->set_property = set_property;
    object_class->constructed  = constructed;
    object_class->dispose      = dispose;

    obj_properties[PROP_DBUS_CONNECTION] =
        g_param_spec_object(NM_POLKIT_LISTENER_DBUS_CONNECTION,
                            "",
                            "",
                            G_TYPE_DBUS_CONNECTION,
                            G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

    obj_properties[PROP_SESSION_AGENT] =
        g_param_spec_boolean(NM_POLKIT_LISTENER_SESSION_AGENT,
                             "",
                             "",
                             FALSE,
                             G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

    g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);

    signals[REQUEST_SYNC] = g_signal_new(NM_POLKIT_LISTENER_SIGNAL_REQUEST_SYNC,
                                         NM_TYPE_POLKIT_LISTENER,
                                         G_SIGNAL_RUN_LAST,
                                         0,
                                         NULL,
                                         NULL,
                                         NULL,
                                         G_TYPE_STRING,
                                         3,
                                         G_TYPE_STRING,
                                         G_TYPE_STRING,
                                         G_TYPE_STRING);

    signals[REGISTERED] = g_signal_new(NM_POLKIT_LISTENER_SIGNAL_REGISTERED,
                                       NM_TYPE_POLKIT_LISTENER,
                                       G_SIGNAL_RUN_FIRST,
                                       0,
                                       NULL,
                                       NULL,
                                       NULL,
                                       G_TYPE_NONE,
                                       0);

    signals[ERROR] = g_signal_new(NM_POLKIT_LISTENER_SIGNAL_ERROR,
                                  NM_TYPE_POLKIT_LISTENER,
                                  G_SIGNAL_RUN_FIRST,
                                  0,
                                  NULL,
                                  NULL,
                                  NULL,
                                  G_TYPE_NONE,
                                  1,
                                  G_TYPE_STRING);
}