aboutsummaryrefslogtreecommitdiff
path: root/src/mm-auth-provider.c
blob: 38ec04d3678463aff159a35648f2ba81dc411b93 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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:
 *
 * Copyright (C) 2010 - 2012 Red Hat, Inc.
 * Copyright (C) 2012 Google, Inc.
 * Copyright (C) 2020 Aleksander Morgado <aleksander@aleksander.es>
 */

#include <config.h>

#include <ModemManager.h>
#include "mm-errors-types.h"
#include "mm-log-object.h"
#include "mm-utils.h"
#include "mm-auth-provider.h"
#include "mm-context.h"

#if defined WITH_POLKIT
# include <polkit/polkit.h>
#endif

struct _MMAuthProvider {
    GObject parent;
#if defined WITH_POLKIT
    PolkitAuthority *authority;
#endif
};

struct _MMAuthProviderClass {
    GObjectClass parent;
};

static void log_object_iface_init (MMLogObjectInterface *iface);

G_DEFINE_TYPE_EXTENDED (MMAuthProvider, mm_auth_provider, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (MM_TYPE_LOG_OBJECT, log_object_iface_init))

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

gboolean
mm_auth_provider_authorize_finish (MMAuthProvider  *self,
                                   GAsyncResult    *res,
                                   GError        **error)
{
    return g_task_propagate_boolean (G_TASK (res), error);
}

#if defined WITH_POLKIT

typedef struct {
    PolkitSubject         *subject;
    gchar                 *authorization;
    GDBusMethodInvocation *invocation;
} AuthorizeContext;

static void
authorize_context_free (AuthorizeContext *ctx)
{
    g_object_unref (ctx->invocation);
    g_object_unref (ctx->subject);
    g_free (ctx->authorization);
    g_free (ctx);
}

static void
check_authorization_ready (PolkitAuthority *authority,
                           GAsyncResult    *res,
                           GTask           *task)
{
    PolkitAuthorizationResult *pk_result;
    GError                    *error = NULL;
    AuthorizeContext          *ctx;

    if (g_task_return_error_if_cancelled (task)) {
        g_object_unref (task);
        return;
    }

    ctx = g_task_get_task_data (task);
    pk_result = polkit_authority_check_authorization_finish (authority, res, &error);
    if (!pk_result) {
        g_task_return_new_error (task,
                                 MM_CORE_ERROR,
                                 MM_CORE_ERROR_FAILED,
                                 "PolicyKit authorization failed: '%s'",
                                 error->message);
        g_error_free (error);
    } else {
        if (polkit_authorization_result_get_is_authorized (pk_result))
            /* Good! */
            g_task_return_boolean (task, TRUE);
        else if (polkit_authorization_result_get_is_challenge (pk_result))
            g_task_return_new_error (task,
                                     MM_CORE_ERROR,
                                     MM_CORE_ERROR_UNAUTHORIZED,
                                     "PolicyKit authorization failed: challenge needed for '%s'",
                                     ctx->authorization);
        else
            g_task_return_new_error (task,
                                     MM_CORE_ERROR,
                                     MM_CORE_ERROR_UNAUTHORIZED,
                                     "PolicyKit authorization failed: not authorized for '%s'",
                                     ctx->authorization);
        g_object_unref (pk_result);
    }

    g_object_unref (task);
}
#endif

void
mm_auth_provider_authorize (MMAuthProvider        *self,
                            GDBusMethodInvocation *invocation,
                            const gchar           *authorization,
                            GCancellable          *cancellable,
                            GAsyncReadyCallback    callback,
                            gpointer               user_data)
{
    GTask *task;

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

    /* When running in the session bus for tests, default to always allow */
    if (mm_context_get_test_session ()) {
        g_task_return_boolean (task, TRUE);
        g_object_unref (task);
        return;
    }

#if defined WITH_POLKIT
    {
        AuthorizeContext *ctx;

        /* When creating the object, we actually allowed errors when looking for the
         * authority. If that is the case, we'll just forbid any incoming
         * authentication request */
        if (!self->authority) {
            g_task_return_new_error (task, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
                                     "PolicyKit authorization error: 'authority not found'");
            g_object_unref (task);
            return;
        }

        ctx = g_new (AuthorizeContext, 1);
        ctx->invocation = g_object_ref (invocation);
        ctx->authorization = g_strdup (authorization);
        ctx->subject = polkit_system_bus_name_new (g_dbus_method_invocation_get_sender (ctx->invocation));
        g_task_set_task_data (task, ctx, (GDestroyNotify)authorize_context_free);

        polkit_authority_check_authorization (self->authority,
                                              ctx->subject,
                                              authorization,
                                              NULL, /* details */
                                              POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
                                              cancellable,
                                              (GAsyncReadyCallback)check_authorization_ready,
                                              task);
    }
#else
    /* Just create the result and complete it */
    g_task_return_boolean (task, TRUE);
    g_object_unref (task);
#endif
}

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

static gchar *
log_object_build_id (MMLogObject *_self)
{
    return g_strdup ("auth-provider");
}

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

static void
mm_auth_provider_init (MMAuthProvider *self)
{
#if defined WITH_POLKIT
    {
        GError *error = NULL;

        self->authority = polkit_authority_get_sync (NULL, &error);
        if (!self->authority) {
            /* NOTE: we failed to create the polkit authority, but we still create
             * our AuthProvider. Every request will fail, though. */
            mm_obj_warn (self, "failed to create PolicyKit authority: '%s'",
                         error ? error->message : "unknown");
            g_clear_error (&error);
        }
    }
#endif
}

static void
dispose (GObject *object)
{
#if defined WITH_POLKIT
    g_clear_object (&(MM_AUTH_PROVIDER (object)->authority));
#endif

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

static void
log_object_iface_init (MMLogObjectInterface *iface)
{
    iface->build_id = log_object_build_id;
}

static void
mm_auth_provider_class_init (MMAuthProviderClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (class);

    object_class->dispose = dispose;
}

MM_DEFINE_SINGLETON_GETTER (MMAuthProvider, mm_auth_provider_get, MM_TYPE_AUTH_PROVIDER)