This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_bus.c
162 lines (132 loc) · 4.06 KB
/
test_bus.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
/*
* groupcheck is a minimal polkit replacement for group-based authentication.
* Copyright (c) 2016, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <errno.h>
#include <systemd/sd-bus.h>
#include "groupcheck.h"
int main(int argc, char *argv[])
{
sd_bus *bus = NULL;
sd_bus_message *msg = NULL, *reply = NULL;
int r = -1, i;
const char *action_id;
const char *name = NULL;
bool *allowed;
gid_t supplementary_groups[argc];
if (argc < 2) {
fprintf(stderr, "Usage:\n\ttest_bus <action_id> [group1 group2 ...]\n");
return EXIT_FAILURE;
}
action_id = argv[1];
if (argc > 2) {
for (i = 0; i < argc-2; i++) {
struct group *grp;
grp = getgrnam(argv[i+2]);
if (grp == NULL) {
fprintf(stderr, "Error: group '%s' was not found.\n", argv[i+2]);
goto end;
}
supplementary_groups[i] = grp->gr_gid;
}
r = setgroups(argc-2, supplementary_groups);
if (r < 0) {
fprintf(stderr, "Error setting the supplementary groups: %s\n", strerror(errno));
goto end;
}
}
r = sd_bus_open_system(&bus);
if (r < 0) {
fprintf(stderr, "Error connecting to the system bus: %s\n", strerror(-r));
goto end;
}
r = sd_bus_get_unique_name(bus, &name);
if (r < 0) {
fprintf(stderr, "Error getting unique name: %s\n", strerror(-r));
goto end;
}
r = sd_bus_message_new_method_call(bus,
&msg,
"org.freedesktop.PolicyKit1",
"/org/freedesktop/PolicyKit1/Authority",
"org.freedesktop.PolicyKit1.Authority",
"CheckAuthorization");
if (r < 0) {
fprintf(stderr, "Error creating method call: %s\n", strerror(-r));
goto end;
}
r = sd_bus_message_open_container(msg, SD_BUS_TYPE_STRUCT, "sa{sv}");
if (r < 0)
goto end;
r = sd_bus_message_append(msg, "s", "system-bus-name");
if (r < 0)
goto end;
r = sd_bus_message_open_container(msg, SD_BUS_TYPE_ARRAY, "{sv}");
if (r < 0)
goto end;
r = sd_bus_message_open_container(msg, SD_BUS_TYPE_DICT_ENTRY, "sv");
if (r < 0)
goto end;
r = sd_bus_message_append(msg, "s", "name");
if (r < 0)
goto end;
r = sd_bus_message_append(msg, "v", "s", name);
if (r < 0)
goto end;
/* dict entry */
r = sd_bus_message_close_container(msg);
if (r < 0)
goto end;
/* array */
r = sd_bus_message_close_container(msg);
if (r < 0)
goto end;
/* struct */
r = sd_bus_message_close_container(msg);
if (r < 0)
goto end;
r = sd_bus_message_append(msg, "s", action_id);
if (r < 0)
goto end;
r = sd_bus_message_append(msg, "a{ss}", 0, NULL);
if (r < 0)
goto end;
r = sd_bus_message_append(msg, "u", 1);
if (r < 0)
goto end;
r = sd_bus_message_append(msg, "s", "");
if (r < 0)
goto end;
r = sd_bus_call(bus, msg, 0, NULL, &reply);
if (r < 0) {
fprintf(stderr, "D-Bus method call failed: %s\n", strerror(-r));
goto end;
}
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_STRUCT, "bba{ss}");
if (r < 0)
return r;
r = sd_bus_message_read(reply, "b", &allowed);
if (r < 0)
return r;
printf("Permission was %sgranted\n", allowed ? "" : "NOT ");
end:
if (r < 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}