1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 
5 #include "alloc-util.h"
6 #include "bus-get-properties.h"
7 #include "bus-polkit.h"
8 #include "bus-util.h"
9 #include "format-util.h"
10 #include "logind-dbus.h"
11 #include "logind-session-dbus.h"
12 #include "logind-user-dbus.h"
13 #include "logind-user.h"
14 #include "logind.h"
15 #include "missing_capability.h"
16 #include "signal-util.h"
17 #include "strv.h"
18 #include "user-util.h"
19 
property_get_uid(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)20 static int property_get_uid(
21                 sd_bus *bus,
22                 const char *path,
23                 const char *interface,
24                 const char *property,
25                 sd_bus_message *reply,
26                 void *userdata,
27                 sd_bus_error *error) {
28 
29         User *u = userdata;
30 
31         assert(bus);
32         assert(reply);
33         assert(u);
34 
35         return sd_bus_message_append(reply, "u", (uint32_t) u->user_record->uid);
36 }
37 
property_get_gid(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)38 static int property_get_gid(
39                 sd_bus *bus,
40                 const char *path,
41                 const char *interface,
42                 const char *property,
43                 sd_bus_message *reply,
44                 void *userdata,
45                 sd_bus_error *error) {
46 
47         User *u = userdata;
48 
49         assert(bus);
50         assert(reply);
51         assert(u);
52 
53         return sd_bus_message_append(reply, "u", (uint32_t) u->user_record->gid);
54 }
55 
property_get_name(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)56 static int property_get_name(
57                 sd_bus *bus,
58                 const char *path,
59                 const char *interface,
60                 const char *property,
61                 sd_bus_message *reply,
62                 void *userdata,
63                 sd_bus_error *error) {
64 
65         User *u = userdata;
66 
67         assert(bus);
68         assert(reply);
69         assert(u);
70 
71         return sd_bus_message_append(reply, "s", u->user_record->user_name);
72 }
73 
74 static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", User, user_get_state, user_state_to_string);
75 
property_get_display(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)76 static int property_get_display(
77                 sd_bus *bus,
78                 const char *path,
79                 const char *interface,
80                 const char *property,
81                 sd_bus_message *reply,
82                 void *userdata,
83                 sd_bus_error *error) {
84 
85         _cleanup_free_ char *p = NULL;
86         User *u = userdata;
87 
88         assert(bus);
89         assert(reply);
90         assert(u);
91 
92         p = u->display ? session_bus_path(u->display) : strdup("/");
93         if (!p)
94                 return -ENOMEM;
95 
96         return sd_bus_message_append(reply, "(so)", u->display ? u->display->id : "", p);
97 }
98 
property_get_sessions(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)99 static int property_get_sessions(
100                 sd_bus *bus,
101                 const char *path,
102                 const char *interface,
103                 const char *property,
104                 sd_bus_message *reply,
105                 void *userdata,
106                 sd_bus_error *error) {
107 
108         User *u = userdata;
109         int r;
110 
111         assert(bus);
112         assert(reply);
113         assert(u);
114 
115         r = sd_bus_message_open_container(reply, 'a', "(so)");
116         if (r < 0)
117                 return r;
118 
119         LIST_FOREACH(sessions_by_user, session, u->sessions) {
120                 _cleanup_free_ char *p = NULL;
121 
122                 p = session_bus_path(session);
123                 if (!p)
124                         return -ENOMEM;
125 
126                 r = sd_bus_message_append(reply, "(so)", session->id, p);
127                 if (r < 0)
128                         return r;
129 
130         }
131 
132         return sd_bus_message_close_container(reply);
133 }
134 
property_get_idle_hint(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)135 static int property_get_idle_hint(
136                 sd_bus *bus,
137                 const char *path,
138                 const char *interface,
139                 const char *property,
140                 sd_bus_message *reply,
141                 void *userdata,
142                 sd_bus_error *error) {
143 
144         User *u = userdata;
145 
146         assert(bus);
147         assert(reply);
148         assert(u);
149 
150         return sd_bus_message_append(reply, "b", user_get_idle_hint(u, NULL) > 0);
151 }
152 
property_get_idle_since_hint(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)153 static int property_get_idle_since_hint(
154                 sd_bus *bus,
155                 const char *path,
156                 const char *interface,
157                 const char *property,
158                 sd_bus_message *reply,
159                 void *userdata,
160                 sd_bus_error *error) {
161 
162         User *u = userdata;
163         dual_timestamp t = DUAL_TIMESTAMP_NULL;
164         uint64_t k;
165 
166         assert(bus);
167         assert(reply);
168         assert(u);
169 
170         (void) user_get_idle_hint(u, &t);
171         k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
172 
173         return sd_bus_message_append(reply, "t", k);
174 }
175 
property_get_linger(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)176 static int property_get_linger(
177                 sd_bus *bus,
178                 const char *path,
179                 const char *interface,
180                 const char *property,
181                 sd_bus_message *reply,
182                 void *userdata,
183                 sd_bus_error *error) {
184 
185         User *u = userdata;
186         int r;
187 
188         assert(bus);
189         assert(reply);
190         assert(u);
191 
192         r = user_check_linger_file(u);
193 
194         return sd_bus_message_append(reply, "b", r > 0);
195 }
196 
bus_user_method_terminate(sd_bus_message * message,void * userdata,sd_bus_error * error)197 int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
198         User *u = userdata;
199         int r;
200 
201         assert(message);
202         assert(u);
203 
204         r = bus_verify_polkit_async(
205                         message,
206                         CAP_KILL,
207                         "org.freedesktop.login1.manage",
208                         NULL,
209                         false,
210                         u->user_record->uid,
211                         &u->manager->polkit_registry,
212                         error);
213         if (r < 0)
214                 return r;
215         if (r == 0)
216                 return 1; /* Will call us back */
217 
218         r = user_stop(u, /* force = */ true);
219         if (r < 0)
220                 return r;
221 
222         return sd_bus_reply_method_return(message, NULL);
223 }
224 
bus_user_method_kill(sd_bus_message * message,void * userdata,sd_bus_error * error)225 int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
226         User *u = userdata;
227         int32_t signo;
228         int r;
229 
230         assert(message);
231         assert(u);
232 
233         r = bus_verify_polkit_async(
234                         message,
235                         CAP_KILL,
236                         "org.freedesktop.login1.manage",
237                         NULL,
238                         false,
239                         u->user_record->uid,
240                         &u->manager->polkit_registry,
241                         error);
242         if (r < 0)
243                 return r;
244         if (r == 0)
245                 return 1; /* Will call us back */
246 
247         r = sd_bus_message_read(message, "i", &signo);
248         if (r < 0)
249                 return r;
250 
251         if (!SIGNAL_VALID(signo))
252                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
253 
254         r = user_kill(u, signo);
255         if (r < 0)
256                 return r;
257 
258         return sd_bus_reply_method_return(message, NULL);
259 }
260 
user_object_find(sd_bus * bus,const char * path,const char * interface,void * userdata,void ** found,sd_bus_error * error)261 static int user_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
262         Manager *m = userdata;
263         uid_t uid;
264         User *user;
265         int r;
266 
267         assert(bus);
268         assert(path);
269         assert(interface);
270         assert(found);
271         assert(m);
272 
273         if (streq(path, "/org/freedesktop/login1/user/self")) {
274                 sd_bus_message *message;
275 
276                 message = sd_bus_get_current_message(bus);
277 
278                 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
279                 if (r == -ENXIO) {
280                         sd_bus_error_free(error);
281                         return 0;
282                 }
283                 if (r < 0)
284                         return r;
285         } else {
286                 const char *p;
287 
288                 p = startswith(path, "/org/freedesktop/login1/user/_");
289                 if (!p)
290                         return 0;
291 
292                 r = parse_uid(p, &uid);
293                 if (r < 0)
294                         return 0;
295 
296                 user = hashmap_get(m->users, UID_TO_PTR(uid));
297                 if (!user)
298                         return 0;
299         }
300 
301         *found = user;
302         return 1;
303 }
304 
user_bus_path(User * u)305 char *user_bus_path(User *u) {
306         char *s;
307 
308         assert(u);
309 
310         if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->user_record->uid) < 0)
311                 return NULL;
312 
313         return s;
314 }
315 
user_node_enumerator(sd_bus * bus,const char * path,void * userdata,char *** nodes,sd_bus_error * error)316 static int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
317         _cleanup_strv_free_ char **l = NULL;
318         sd_bus_message *message;
319         Manager *m = userdata;
320         User *user;
321         int r;
322 
323         assert(bus);
324         assert(path);
325         assert(nodes);
326 
327         HASHMAP_FOREACH(user, m->users) {
328                 char *p;
329 
330                 p = user_bus_path(user);
331                 if (!p)
332                         return -ENOMEM;
333 
334                 r = strv_consume(&l, p);
335                 if (r < 0)
336                         return r;
337         }
338 
339         message = sd_bus_get_current_message(bus);
340         if (message) {
341                 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
342 
343                 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
344                 if (r >= 0) {
345                         uid_t uid;
346 
347                         r = sd_bus_creds_get_owner_uid(creds, &uid);
348                         if (r >= 0) {
349                                 user = hashmap_get(m->users, UID_TO_PTR(uid));
350                                 if (user) {
351                                         r = strv_extend(&l, "/org/freedesktop/login1/user/self");
352                                         if (r < 0)
353                                                 return r;
354                                 }
355                         }
356                 }
357         }
358 
359         *nodes = TAKE_PTR(l);
360 
361         return 1;
362 }
363 
364 static const sd_bus_vtable user_vtable[] = {
365         SD_BUS_VTABLE_START(0),
366 
367         SD_BUS_PROPERTY("UID", "u", property_get_uid, 0, SD_BUS_VTABLE_PROPERTY_CONST),
368         SD_BUS_PROPERTY("GID", "u", property_get_gid, 0, SD_BUS_VTABLE_PROPERTY_CONST),
369         SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST),
370         BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
371         SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST),
372         SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), SD_BUS_VTABLE_PROPERTY_CONST),
373         SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), SD_BUS_VTABLE_PROPERTY_CONST),
374         SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
375         SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
376         SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, 0),
377         SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
378         SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
379         SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
380         SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, 0),
381 
382         SD_BUS_METHOD("Terminate", NULL, NULL, bus_user_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED),
383         SD_BUS_METHOD_WITH_ARGS("Kill",
384                                 SD_BUS_ARGS("i", signal_number),
385                                 SD_BUS_NO_RESULT,
386                                 bus_user_method_kill,
387                                 SD_BUS_VTABLE_UNPRIVILEGED),
388 
389         SD_BUS_VTABLE_END
390 };
391 
392 const BusObjectImplementation user_object = {
393         "/org/freedesktop/login1/user",
394         "org.freedesktop.login1.User",
395         .fallback_vtables = BUS_FALLBACK_VTABLES({user_vtable, user_object_find}),
396         .node_enumerator = user_node_enumerator,
397 };
398 
user_send_signal(User * u,bool new_user)399 int user_send_signal(User *u, bool new_user) {
400         _cleanup_free_ char *p = NULL;
401 
402         assert(u);
403 
404         p = user_bus_path(u);
405         if (!p)
406                 return -ENOMEM;
407 
408         return sd_bus_emit_signal(
409                         u->manager->bus,
410                         "/org/freedesktop/login1",
411                         "org.freedesktop.login1.Manager",
412                         new_user ? "UserNew" : "UserRemoved",
413                         "uo", (uint32_t) u->user_record->uid, p);
414 }
415 
user_send_changed(User * u,const char * properties,...)416 int user_send_changed(User *u, const char *properties, ...) {
417         _cleanup_free_ char *p = NULL;
418         char **l;
419 
420         assert(u);
421 
422         if (!u->started)
423                 return 0;
424 
425         p = user_bus_path(u);
426         if (!p)
427                 return -ENOMEM;
428 
429         l = strv_from_stdarg_alloca(properties);
430 
431         return sd_bus_emit_properties_changed_strv(u->manager->bus, p, "org.freedesktop.login1.User", l);
432 }
433