remove unused header
[poetteringd.git] / utils.c
blob070461918645a9ecb8f286f187c7e9e55f387180
1 /*
2 Copyright 2020 Robert Nagy
4 Copyright 2012 Alexandre Rostovtsev
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include <libdaemon/dfork.h>
33 #include <glib.h>
34 #include <gio/gio.h>
35 #include <polkit/polkit.h>
37 #include "utils.h"
39 struct check_polkit_data {
40 const gchar *unique_name;
41 const gchar *action_id;
42 gboolean user_interaction;
43 GAsyncReadyCallback callback;
44 gpointer user_data;
45 GTask *task;
47 PolkitAuthority *authority;
48 PolkitSubject *subject;
51 void
52 check_polkit_data_free(struct check_polkit_data *data)
54 if (data == NULL)
55 return;
57 if (data->subject != NULL)
58 g_object_unref(data->subject);
59 if (data->authority != NULL)
60 g_object_unref(data->authority);
61 if (data->task != NULL)
62 g_object_unref(data->task);
64 g_free(data);
67 gboolean
68 check_polkit_finish(GAsyncResult *res, GError **error)
70 GTask *task = G_TASK(res);
72 return g_task_propagate_boolean(task, error);
75 static void
76 check_polkit_authorization_cb(GObject *source_object,
77 GAsyncResult *res, gpointer _data)
79 struct check_polkit_data *data;
80 PolkitAuthorizationResult *result;
81 GError *err = NULL;
83 data = (struct check_polkit_data *) _data;
84 if ((result = polkit_authority_check_authorization_finish(data->authority, res, &err)) == NULL) {
85 g_task_return_error(data->task, err);
86 goto out;
89 if (!polkit_authorization_result_get_is_authorized(result)) {
90 g_task_report_new_error(NULL, data->callback, data->user_data, check_polkit_async, POLKIT_ERROR,
91 POLKIT_ERROR_NOT_AUTHORIZED, "Authorizing for '%s': not authorized", data->action_id);
92 goto out;
95 g_task_return_boolean(data->task, TRUE);
97 out:
98 check_polkit_data_free(data);
99 if (result != NULL)
100 g_object_unref(result);
103 static void
104 check_polkit_authority_cb(GObject *source_object,
105 GAsyncResult *res, gpointer _data)
107 struct check_polkit_data *data;
108 GError *err = NULL;
110 data = (struct check_polkit_data *) _data;
111 if ((data->authority = polkit_authority_get_finish(res, &err)) == NULL) {
112 g_task_return_error(data->task, err);
113 check_polkit_data_free(data);
114 return;
117 if (data->unique_name == NULL || data->action_id == NULL ||
118 (data->subject = polkit_system_bus_name_new(data->unique_name)) == NULL) {
119 g_task_report_new_error(NULL, data->callback, data->user_data, check_polkit_async, POLKIT_ERROR,
120 POLKIT_ERROR_FAILED, "Authorizing for '%s': failed sanity check", data->action_id);
121 check_polkit_data_free(data);
122 return;
125 polkit_authority_check_authorization(data->authority, data->subject, data->action_id,
126 NULL, (PolkitCheckAuthorizationFlags) data->user_interaction, NULL,
127 check_polkit_authorization_cb, data);
130 void
131 check_polkit_async(const gchar *unique_name,
132 const gchar *action_id, const gboolean user_interaction,
133 GAsyncReadyCallback callback, gpointer user_data)
135 struct check_polkit_data *data;
137 data = g_new0(struct check_polkit_data, 1);
138 data->unique_name = unique_name;
139 data->action_id = action_id;
140 data->user_interaction = user_interaction;
141 data->callback = callback;
142 data->user_data = user_data;
144 data->task = g_task_new(NULL, NULL, data->callback, data->user_data);
146 polkit_authority_get_async(NULL, check_polkit_authority_cb, data);
149 gchar *
150 strstr0(const gchar *haystack, const gchar *needle)
152 if (haystack == NULL || needle == NULL)
153 return NULL;
154 return strstr(haystack, needle);