-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsendreply.c
76 lines (62 loc) · 2.94 KB
/
sendreply.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
/*
Copyright (C) 2020 Martin Sandsmark
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 3 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <systemd/sd-bus.h>
#include <systemd/sd-journal.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[])
{
if (argc != 3) {
sd_journal_print(LOG_ERR, "Invalid args passed, need cookie and authed ID");
return EPERM;
}
if (getuid()) {
sd_journal_print(LOG_ERR, "warning: This needs to run as root");
return EPERM;
}
sd_bus *bus = NULL;
int ret = sd_bus_open_system(&bus);
if (ret < 0) {
sd_journal_print(LOG_ERR, "Failed to connect to system bus: %s", strerror(-ret));
return -ret;
}
sd_journal_print(LOG_NOTICE, "Responding, cookie: %s, authed uid: %d\n", argv[1], atoi(argv[2]));
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *dbusRet = NULL;
ret = sd_bus_call_method(bus,
"org.freedesktop.PolicyKit1", /* service to contact */
"/org/freedesktop/PolicyKit1/Authority", /* object path */
"org.freedesktop.PolicyKit1.Authority", /* interface name */
"AuthenticationAgentResponse2", /* method name */
&error, /* object to return error in */
&dbusRet, /* return message on success */
"us(sa{sv})", /* input signature */
// arguments
atoi(argv[2]), /* UID of our parent/user that su-ed, should always be the same as authed */
argv[1], /* cookie */
"unix-user", /* map key name ID type (UID) */
1, /* number of elements in a(rray), actually a map, describing unix-user */
"uid", /* the key for the entry in the map */
"u", /* map value v(ariant)'s real type */
atoi(argv[2]) /* authed UID */
);
if (ret < 0) {
sd_journal_print(LOG_ERR, "Failed to issue method call: %s (%s, %s)\n", error.message, strerror(errno), strerror(-ret));
}
sd_bus_error_free(&error);
sd_bus_message_unref(dbusRet);
sd_bus_unref(bus);
return -ret; /* systemd flips the sign */
}