chiark / gitweb /
socket: introduce SELinuxLabelViaNet option
[elogind.git] / src / core / dbus-job.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "log.h"
23 #include "sd-bus.h"
24 #include "selinux-access.h"
25 #include "job.h"
26 #include "dbus-job.h"
27 #include "dbus.h"
28
29 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, job_type, JobType);
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state, job_state, JobState);
31
32 static int verify_sys_admin_or_owner_sync(sd_bus_message *message, Job *j, sd_bus_error *error) {
33         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
34         int r;
35
36         if (sd_bus_track_contains(j->clients, sd_bus_message_get_sender(message)))
37                 return 0; /* One of the job owners is calling us */
38
39         r = sd_bus_query_sender_privilege(message, CAP_SYS_ADMIN);
40         if (r < 0)
41                 return r;
42         if (r == 0)
43                 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Access denied to perform action");
44
45         /* Root has called us */
46         return 0;
47 }
48
49 static int property_get_unit(
50                 sd_bus *bus,
51                 const char *path,
52                 const char *interface,
53                 const char *property,
54                 sd_bus_message *reply,
55                 void *userdata,
56                 sd_bus_error *error) {
57
58         _cleanup_free_ char *p = NULL;
59         Job *j = userdata;
60
61         assert(bus);
62         assert(reply);
63         assert(j);
64
65         p = unit_dbus_path(j->unit);
66         if (!p)
67                 return -ENOMEM;
68
69         return sd_bus_message_append(reply, "(so)", j->unit->id, p);
70 }
71
72 int bus_job_method_cancel(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
73         Job *j = userdata;
74         int r;
75
76         assert(bus);
77         assert(message);
78         assert(j);
79
80         r = verify_sys_admin_or_owner_sync(message, j, error);
81         if (r < 0)
82                 return r;
83
84         r = selinux_unit_access_check(j->unit, message, "stop", error);
85         if (r < 0)
86                 return r;
87
88         job_finish_and_invalidate(j, JOB_CANCELED, true);
89
90         return sd_bus_reply_method_return(message, NULL);
91 }
92
93 const sd_bus_vtable bus_job_vtable[] = {
94         SD_BUS_VTABLE_START(0),
95         SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
96         SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), SD_BUS_VTABLE_PROPERTY_CONST),
97         SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
98         SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), SD_BUS_VTABLE_PROPERTY_CONST),
99         SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
100         SD_BUS_VTABLE_END
101 };
102
103 static int send_new_signal(sd_bus *bus, void *userdata) {
104         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
105         _cleanup_free_ char *p = NULL;
106         Job *j = userdata;
107         int r;
108
109         assert(bus);
110         assert(j);
111
112         p = job_dbus_path(j);
113         if (!p)
114                 return -ENOMEM;
115
116         r = sd_bus_message_new_signal(
117                         bus,
118                         &m,
119                         "/org/freedesktop/systemd1",
120                         "org.freedesktop.systemd1.Manager",
121                         "JobNew");
122         if (r < 0)
123                 return r;
124
125         r = sd_bus_message_append(m, "uos", j->id, p, j->unit->id);
126         if (r < 0)
127                 return r;
128
129         return sd_bus_send(bus, m, NULL);
130 }
131
132 static int send_changed_signal(sd_bus *bus, void *userdata) {
133         _cleanup_free_ char *p = NULL;
134         Job *j = userdata;
135
136         assert(bus);
137         assert(j);
138
139         p = job_dbus_path(j);
140         if (!p)
141                 return -ENOMEM;
142
143         return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL);
144 }
145
146 void bus_job_send_change_signal(Job *j) {
147         int r;
148
149         assert(j);
150
151         if (j->in_dbus_queue) {
152                 LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
153                 j->in_dbus_queue = false;
154         }
155
156         r = bus_foreach_bus(j->manager, j->clients, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j);
157         if (r < 0)
158                 log_debug("Failed to send job change signal for %u: %s", j->id, strerror(-r));
159
160         j->sent_dbus_new_signal = true;
161 }
162
163 static int send_removed_signal(sd_bus *bus, void *userdata) {
164         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
165         _cleanup_free_ char *p = NULL;
166         Job *j = userdata;
167         int r;
168
169         assert(bus);
170         assert(j);
171
172         p = job_dbus_path(j);
173         if (!p)
174                 return -ENOMEM;
175
176         r = sd_bus_message_new_signal(
177                         bus,
178                         &m,
179                         "/org/freedesktop/systemd1",
180                         "org.freedesktop.systemd1.Manager",
181                         "JobRemoved");
182         if (r < 0)
183                 return r;
184
185         r = sd_bus_message_append(m, "uoss", j->id, p, j->unit->id, job_result_to_string(j->result));
186         if (r < 0)
187                 return r;
188
189         return sd_bus_send(bus, m, NULL);
190 }
191
192 void bus_job_send_removed_signal(Job *j) {
193         int r;
194
195         assert(j);
196
197         if (!j->sent_dbus_new_signal)
198                 bus_job_send_change_signal(j);
199
200         r = bus_foreach_bus(j->manager, j->clients, send_removed_signal, j);
201         if (r < 0)
202                 log_debug("Failed to send job remove signal for %u: %s", j->id, strerror(-r));
203 }