bus-creds.c revision cccb0b2cdbd25e90ae92d2d5b107125cb1ca3433
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/***
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen This file is part of systemd.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Copyright 2013 Lennart Poettering
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is free software; you can redistribute it and/or modify it
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen under the terms of the GNU Lesser General Public License as published by
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen (at your option) any later version.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is distributed in the hope that it will be useful, but
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Lesser General Public License for more details.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen You should have received a copy of the GNU Lesser General Public License
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen***/
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include <stdlib.h>
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include "util.h"
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen#include "cgroup-util.h"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen#include "fileio.h"
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen#include "audit.h"
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen#include "bus-message.h"
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen#include "bus-util.h"
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen#include "time-util.h"
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include "strv.h"
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include "bus-creds.h"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenenum {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen CAP_OFFSET_INHERITABLE = 0,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen CAP_OFFSET_PERMITTED = 1,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen CAP_OFFSET_EFFECTIVE = 2,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen CAP_OFFSET_BOUNDING = 3
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen};
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
3aeb37bc4f32b5edc334f2ac7c5d3c7b0a121328Tom Gundersenvoid bus_creds_done(sd_bus_creds *c) {
c6f7c917a1b494d4455800823472227463f87438Tom Gundersen assert(c);
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* For internal bus cred structures that are allocated by
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * something else */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen free(c->session);
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen free(c->unit);
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen free(c->user_unit);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen free(c->slice);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen strv_free(c->cmdline_array);
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen strv_free(c->well_known_names);
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ sd_bus_creds *sd_bus_creds_ref(sd_bus_creds *c) {
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek assert_return(c, NULL);
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek if (c->allocated) {
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek assert(c->n_ref > 0);
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek c->n_ref++;
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek } else {
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek sd_bus_message *m;
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek /* If this is an embedded creds structure, then
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen * forward ref counting to the message */
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen m = container_of(c, sd_bus_message, creds);
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen sd_bus_message_ref(m);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return c;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ sd_bus_creds *sd_bus_creds_unref(sd_bus_creds *c) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!c)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (c->allocated) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert(c->n_ref > 0);
977085794d2996320e345433403de75f662b0622Tom Gundersen c->n_ref--;
977085794d2996320e345433403de75f662b0622Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen if (c->n_ref == 0) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen bus_creds_done(c);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen free(c->comm);
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen free(c->tid_comm);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen free(c->exe);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen free(c->cmdline);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen free(c->cgroup);
977085794d2996320e345433403de75f662b0622Tom Gundersen free(c->capability);
977085794d2996320e345433403de75f662b0622Tom Gundersen free(c->label);
977085794d2996320e345433403de75f662b0622Tom Gundersen free(c->unique_name);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen free(c->cgroup_root);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen free(c->conn_name);
866ee3682213789f85b877700457fdca05695a0eTom Gundersen free(c);
866ee3682213789f85b877700457fdca05695a0eTom Gundersen }
866ee3682213789f85b877700457fdca05695a0eTom Gundersen } else {
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen sd_bus_message *m;
866ee3682213789f85b877700457fdca05695a0eTom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen m = container_of(c, sd_bus_message, creds);
977085794d2996320e345433403de75f662b0622Tom Gundersen sd_bus_message_unref(m);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen }
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
866ee3682213789f85b877700457fdca05695a0eTom Gundersen
866ee3682213789f85b877700457fdca05695a0eTom Gundersen return NULL;
866ee3682213789f85b877700457fdca05695a0eTom Gundersen}
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
866ee3682213789f85b877700457fdca05695a0eTom Gundersen_public_ uint64_t sd_bus_creds_get_mask(const sd_bus_creds *c) {
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen assert_return(c, 0);
977085794d2996320e345433403de75f662b0622Tom Gundersen
977085794d2996320e345433403de75f662b0622Tom Gundersen return c->mask;
977085794d2996320e345433403de75f662b0622Tom Gundersen}
977085794d2996320e345433403de75f662b0622Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersensd_bus_creds* bus_creds_new(void) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen sd_bus_creds *c;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen c = new0(sd_bus_creds, 1);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!c)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen c->allocated = true;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen c->n_ref = 1;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return c;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
d2df0d0ed3a88e491405b403e6022e6619750130Tom Gundersen_public_ int sd_bus_creds_new_from_pid(pid_t pid, uint64_t mask, sd_bus_creds **ret) {
edf029b7fd9a5853a87d3ca99aac2922bb8a277eTom Gundersen sd_bus_creds *c;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen int r;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(pid >= 0, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(ret, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (pid == 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen pid = getpid();
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering c = bus_creds_new();
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen if (!c)
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen return -ENOMEM;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen r = bus_creds_add_more(c, mask, pid, 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (r < 0) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen sd_bus_creds_unref(c);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return r;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* Check if the process existed at all, in case we haven't
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * figured that out already */
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek if (kill(pid, 0) < 0 && errno == ESRCH) {
6e37cd2f4af8928d905203108a4331e375d7127cThomas Hindoe Paaboel Andersen sd_bus_creds_unref(c);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -ESRCH;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen *ret = c;
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen return 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ int sd_bus_creds_get_uid(sd_bus_creds *c, uid_t *uid) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(c, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(uid, -EINVAL);
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!(c->mask & SD_BUS_CREDS_UID))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -ENODATA;
ed88bcfb7c15029f9fc95ee2380759a9eb782d46Zbigniew Jędrzejewski-Szmek
ed88bcfb7c15029f9fc95ee2380759a9eb782d46Zbigniew Jędrzejewski-Szmek *uid = c->uid;
ed88bcfb7c15029f9fc95ee2380759a9eb782d46Zbigniew Jędrzejewski-Szmek return 0;
ed88bcfb7c15029f9fc95ee2380759a9eb782d46Zbigniew Jędrzejewski-Szmek}
ed88bcfb7c15029f9fc95ee2380759a9eb782d46Zbigniew Jędrzejewski-Szmek
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ int sd_bus_creds_get_gid(sd_bus_creds *c, gid_t *gid) {
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek assert_return(c, -EINVAL);
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek assert_return(gid, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen if (!(c->mask & SD_BUS_CREDS_UID))
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen return -ENODATA;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen *gid = c->gid;
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek return 0;
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek}
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek
36f822c4bd077f9121757e24b6516e5c7ada63b5Zbigniew Jędrzejewski-Szmek_public_ int sd_bus_creds_get_pid(sd_bus_creds *c, pid_t *pid) {
36f822c4bd077f9121757e24b6516e5c7ada63b5Zbigniew Jędrzejewski-Szmek assert_return(c, -EINVAL);
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek assert_return(pid, -EINVAL);
36f822c4bd077f9121757e24b6516e5c7ada63b5Zbigniew Jędrzejewski-Szmek
98a375f6d5cac24eb80d6d4e00699851324afdecTom Gundersen if (!(c->mask & SD_BUS_CREDS_PID))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -ENODATA;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert(c->pid > 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen *pid = c->pid;
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek return 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ int sd_bus_creds_get_tid(sd_bus_creds *c, pid_t *tid) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(c, -EINVAL);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_return(tid, -EINVAL);
f8a0bb5285024b6ce372c3157e761e6543ebdcd2Andreas Henriksson
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek if (!(c->mask & SD_BUS_CREDS_TID))
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return -ENODATA;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert(c->tid > 0);
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering *tid = c->tid;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering return 0;
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen}
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering_public_ int sd_bus_creds_get_pid_starttime(sd_bus_creds *c, uint64_t *usec) {
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering assert_return(c, -EINVAL);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_return(usec, -EINVAL);
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek if (!(c->mask & SD_BUS_CREDS_PID_STARTTIME))
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return -ENODATA;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert(c->pid_starttime > 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen *usec = c->pid_starttime;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
edf029b7fd9a5853a87d3ca99aac2922bb8a277eTom Gundersen_public_ int sd_bus_creds_get_selinux_context(sd_bus_creds *c, const char **ret) {
edf029b7fd9a5853a87d3ca99aac2922bb8a277eTom Gundersen assert_return(c, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!(c->mask & SD_BUS_CREDS_SELINUX_CONTEXT))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -ENODATA;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert(c->label);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen *ret = c->label;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return 0;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen}
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek_public_ int sd_bus_creds_get_comm(sd_bus_creds *c, const char **ret) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(c, -EINVAL);
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek assert_return(ret, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!(c->mask & SD_BUS_CREDS_COMM))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -ENODATA;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert(c->comm);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen *ret = c->comm;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ int sd_bus_creds_get_tid_comm(sd_bus_creds *c, const char **ret) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(c, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(ret, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!(c->mask & SD_BUS_CREDS_TID_COMM))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -ENODATA;
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert(c->tid_comm);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen *ret = c->tid_comm;
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen return 0;
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ int sd_bus_creds_get_exe(sd_bus_creds *c, const char **ret) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_return(c, -EINVAL);
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen assert_return(ret, -EINVAL);
edbb03e95a3c31bf719d5c6c46eec14d0bcb9c8fTom Gundersen
edbb03e95a3c31bf719d5c6c46eec14d0bcb9c8fTom Gundersen if (!(c->mask & SD_BUS_CREDS_EXE))
edbb03e95a3c31bf719d5c6c46eec14d0bcb9c8fTom Gundersen return -ENODATA;
505f8da7325591defe5f751f328bd26915267602Tom Gundersen
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen assert(c->exe);
9b1c2626cef16722603bded9bb52033aba34dd74Tom Gundersen *ret = c->exe;
bf175aafd20c9ef974709ef12c5acf836121af33Tom Gundersen return 0;
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen}
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen_public_ int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **ret) {
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen assert_return(c, -EINVAL);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen assert_return(ret, -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!(c->mask & SD_BUS_CREDS_CGROUP))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -ENODATA;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert(c->cgroup);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen *ret = c->cgroup;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen return 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen int r;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen assert_return(c, -EINVAL);
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen assert_return(ret, -EINVAL);
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen if (!(c->mask & SD_BUS_CREDS_UNIT))
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return -ENODATA;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen assert(c->cgroup);
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen if (!c->unit) {
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen const char *shifted;
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (r < 0)
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen return r;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen r = cg_path_get_unit(shifted, (char**) &c->unit);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (r < 0)
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen return r;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen }
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen *ret = c->unit;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen return 0;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen}
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen_public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) {
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen int r;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen assert_return(c, -EINVAL);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen assert_return(ret, -EINVAL);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (!(c->mask & SD_BUS_CREDS_USER_UNIT))
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen return -ENODATA;
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen assert(c->cgroup);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen if (!c->user_unit) {
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen const char *shifted;
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (r < 0)
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen return r;
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen r = cg_path_get_user_unit(shifted, (char**) &c->user_unit);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (r < 0)
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return r;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen }
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen *ret = c->user_unit;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering return 0;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen}
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering_public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen int r;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering assert_return(c, -EINVAL);
b5db00e52ee2e20578839e4e4488f7b9af9abc38Umut Tezduyar Lindskog assert_return(ret, -EINVAL);
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
55428d84f31b52da1c50b7469f14e15740547f20Tom Gundersen if (!(c->mask & SD_BUS_CREDS_SLICE))
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return -ENODATA;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering assert(c->cgroup);
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen if (!c->slice) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen const char *shifted;
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen if (r < 0)
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return r;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen r = cg_path_get_slice(shifted, (char**) &c->slice);
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen if (r < 0)
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen return r;
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen }
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen *ret = c->slice;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return 0;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen}
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen_public_ int sd_bus_creds_get_session(sd_bus_creds *c, const char **ret) {
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen int r;
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen assert_return(c, -EINVAL);
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen assert_return(ret, -EINVAL);
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen
977085794d2996320e345433403de75f662b0622Tom Gundersen if (!(c->mask & SD_BUS_CREDS_SESSION))
977085794d2996320e345433403de75f662b0622Tom Gundersen return -ENODATA;
977085794d2996320e345433403de75f662b0622Tom Gundersen
977085794d2996320e345433403de75f662b0622Tom Gundersen assert(c->cgroup);
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen if (!c->session) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const char *shifted;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen if (r < 0)
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen return r;
733f7a2c69c794a81978a08a79916c224ba355a6Tom Gundersen
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen r = cg_path_get_session(shifted, (char**) &c->session);
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen if (r < 0)
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen return r;
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen }
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen *ret = c->session;
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen return 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen_public_ int sd_bus_creds_get_owner_uid(sd_bus_creds *c, uid_t *uid) {
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen const char *shifted;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen int r;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen assert_return(c, -EINVAL);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_return(uid, -EINVAL);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen if (!(c->mask & SD_BUS_CREDS_OWNER_UID))
68ba38770640413b4fa06773447666eb88a38d4cTom Gundersen return -ENODATA;
68ba38770640413b4fa06773447666eb88a38d4cTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen assert(c->cgroup);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (r < 0)
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen return r;
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen return cg_path_get_owner_uid(shifted, uid);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen}
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen_public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) {
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen assert_return(c, -EINVAL);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen if (!(c->mask & SD_BUS_CREDS_CMDLINE))
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen return -ENODATA;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen assert_return(c->cmdline, -ESRCH);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen assert(c->cmdline);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen if (!c->cmdline_array) {
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen c->cmdline_array = strv_parse_nulstr(c->cmdline, c->cmdline_size);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen if (!c->cmdline_array)
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen return -ENOMEM;
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen }
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen *cmdline = c->cmdline_array;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen return 0;
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen}
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen_public_ int sd_bus_creds_get_audit_session_id(sd_bus_creds *c, uint32_t *sessionid) {
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen assert_return(c, -EINVAL);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen assert_return(sessionid, -EINVAL);
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (!(c->mask & SD_BUS_CREDS_AUDIT_SESSION_ID))
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen return -ENODATA;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen *sessionid = c->audit_session_id;
92d927f850d4b668b44f3e5f41e266d934d03726Tom Gundersen return 0;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen}
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen_public_ int sd_bus_creds_get_audit_login_uid(sd_bus_creds *c, uid_t *uid) {
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen assert_return(c, -EINVAL);
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen assert_return(uid, -EINVAL);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen if (!(c->mask & SD_BUS_CREDS_AUDIT_LOGIN_UID))
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen return -ENODATA;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen *uid = c->audit_login_uid;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen return 0;
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen}
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen_public_ int sd_bus_creds_get_unique_name(sd_bus_creds *c, const char **unique_name) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen assert_return(c, -EINVAL);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen assert_return(unique_name, -EINVAL);
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen if (!(c->mask & SD_BUS_CREDS_UNIQUE_NAME))
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen return -ENODATA;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen *unique_name = c->unique_name;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return 0;
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen}
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen_public_ int sd_bus_creds_get_well_known_names(sd_bus_creds *c, char ***well_known_names) {
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen assert_return(c, -EINVAL);
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen assert_return(well_known_names, -EINVAL);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen if (!(c->mask & SD_BUS_CREDS_WELL_KNOWN_NAMES))
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen return -ENODATA;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen *well_known_names = c->well_known_names;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen return 0;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen}
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen_public_ int sd_bus_creds_get_connection_name(sd_bus_creds *c, const char **ret) {
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen assert_return(c, -EINVAL);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen assert_return(ret, -EINVAL);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen if (!(c->mask & SD_BUS_CREDS_CONNECTION_NAME))
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen return -ENODATA;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen assert(c->conn_name);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen *ret = c->conn_name;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen return 0;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen}
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersenstatic int has_cap(sd_bus_creds *c, unsigned offset, int capability) {
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen size_t sz;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen assert(c);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen assert(c->capability);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen sz = c->capability_size / 4;
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack if ((size_t) capability >= sz*8)
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen return 0;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen return !!(c->capability[offset * sz + (capability / 8)] & (1 << (capability % 8)));
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen}
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen_public_ int sd_bus_creds_has_effective_cap(sd_bus_creds *c, int capability) {
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen assert_return(c, -EINVAL);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen assert_return(capability >= 0, -EINVAL);
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (!(c->mask & SD_BUS_CREDS_EFFECTIVE_CAPS))
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen return -ENODATA;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen return has_cap(c, CAP_OFFSET_EFFECTIVE, capability);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen}
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen_public_ int sd_bus_creds_has_permitted_cap(sd_bus_creds *c, int capability) {
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen assert_return(c, -EINVAL);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen assert_return(capability >= 0, -EINVAL);
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen if (!(c->mask & SD_BUS_CREDS_PERMITTED_CAPS))
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen return -ENODATA;
return has_cap(c, CAP_OFFSET_PERMITTED, capability);
}
_public_ int sd_bus_creds_has_inheritable_cap(sd_bus_creds *c, int capability) {
assert_return(c, -EINVAL);
assert_return(capability >= 0, -EINVAL);
if (!(c->mask & SD_BUS_CREDS_INHERITABLE_CAPS))
return -ENODATA;
return has_cap(c, CAP_OFFSET_INHERITABLE, capability);
}
_public_ int sd_bus_creds_has_bounding_cap(sd_bus_creds *c, int capability) {
assert_return(c, -EINVAL);
assert_return(capability >= 0, -EINVAL);
if (!(c->mask & SD_BUS_CREDS_BOUNDING_CAPS))
return -ENODATA;
return has_cap(c, CAP_OFFSET_BOUNDING, capability);
}
static int parse_caps(sd_bus_creds *c, unsigned offset, const char *p) {
size_t sz;
unsigned i;
assert(c);
assert(p);
p += strspn(p, WHITESPACE);
sz = strlen(p);
if (sz % 2 != 0)
return -EINVAL;
sz /= 2;
if (!c->capability) {
c->capability = new0(uint8_t, sz * 4);
if (!c->capability)
return -ENOMEM;
c->capability_size = sz * 4;
}
for (i = 0; i < sz; i ++) {
int x, y;
x = unhexchar(p[i*2]);
y = unhexchar(p[i*2+1]);
if (x < 0 || y < 0)
return -EINVAL;
c->capability[offset * sz + (sz - i - 1)] = (uint8_t) x << 4 | (uint8_t) y;
}
return 0;
}
int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) {
uint64_t missing;
int r;
assert(c);
assert(c->allocated);
missing = mask & ~c->mask;
if (missing == 0)
return 0;
/* Try to retrieve PID from creds if it wasn't passed to us */
if (pid <= 0 && (c->mask & SD_BUS_CREDS_PID))
pid = c->pid;
if (tid <= 0 && (c->mask & SD_BUS_CREDS_TID))
tid = c->pid;
/* Without pid we cannot do much... */
if (pid <= 0)
return 0;
if (missing & (SD_BUS_CREDS_UID | SD_BUS_CREDS_GID |
SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_INHERITABLE_CAPS |
SD_BUS_CREDS_PERMITTED_CAPS | SD_BUS_CREDS_BOUNDING_CAPS)) {
_cleanup_fclose_ FILE *f = NULL;
char line[LINE_MAX];
const char *p;
p = procfs_file_alloca(pid, "status");
f = fopen(p, "re");
if (!f)
return errno == ENOENT ? -ESRCH : -errno;
FOREACH_LINE(line, f, return -errno) {
truncate_nl(line);
if (missing & SD_BUS_CREDS_UID) {
p = startswith(line, "Uid:");
if (p) {
unsigned long uid;
p += strspn(p, WHITESPACE);
if (sscanf(p, "%lu", &uid) != 1)
return -EIO;
c->uid = (uid_t) uid;
c->mask |= SD_BUS_CREDS_UID;
continue;
}
}
if (missing & SD_BUS_CREDS_GID) {
p = startswith(line, "Gid:");
if (p) {
unsigned long gid;
p += strspn(p, WHITESPACE);
if (sscanf(p, "%lu", &gid) != 1)
return -EIO;
c->gid = (uid_t) gid;
c->mask |= SD_BUS_CREDS_GID;
continue;
}
}
if (missing & SD_BUS_CREDS_EFFECTIVE_CAPS) {
p = startswith(line, "CapEff:");
if (p) {
r = parse_caps(c, CAP_OFFSET_EFFECTIVE, p);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_EFFECTIVE_CAPS;
continue;
}
}
if (missing & SD_BUS_CREDS_PERMITTED_CAPS) {
p = startswith(line, "CapPrm:");
if (p) {
r = parse_caps(c, CAP_OFFSET_PERMITTED, p);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_PERMITTED_CAPS;
continue;
}
}
if (missing & SD_BUS_CREDS_INHERITABLE_CAPS) {
p = startswith(line, "CapInh:");
if (p) {
r = parse_caps(c, CAP_OFFSET_INHERITABLE, p);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_INHERITABLE_CAPS;
continue;
}
}
if (missing & SD_BUS_CREDS_BOUNDING_CAPS) {
p = startswith(line, "CapBnd:");
if (p) {
r = parse_caps(c, CAP_OFFSET_BOUNDING, p);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_BOUNDING_CAPS;
continue;
}
}
}
}
if (missing & (SD_BUS_CREDS_PID_STARTTIME)) {
unsigned long long st;
r = get_starttime_of_pid(pid, &st);
if (r < 0)
return r;
c->pid_starttime = ((usec_t) st * USEC_PER_SEC) / (usec_t) sysconf(_SC_CLK_TCK);
c->mask |= SD_BUS_CREDS_PID_STARTTIME;
}
if (missing & SD_BUS_CREDS_SELINUX_CONTEXT) {
const char *p;
p = procfs_file_alloca(pid, "attr/current");
r = read_one_line_file(p, &c->label);
if (r < 0 && r != -ENOENT && r != -EINVAL)
return r;
else if (r >= 0)
c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
}
if (missing & SD_BUS_CREDS_COMM) {
r = get_process_comm(pid, &c->comm);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_COMM;
}
if (missing & SD_BUS_CREDS_EXE) {
r = get_process_exe(pid, &c->exe);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_EXE;
}
if (missing & SD_BUS_CREDS_CMDLINE) {
const char *p;
p = procfs_file_alloca(pid, "cmdline");
r = read_full_file(p, &c->cmdline, &c->cmdline_size);
if (r < 0)
return r;
if (c->cmdline_size == 0) {
free(c->cmdline);
c->cmdline = NULL;
} else
c->mask |= SD_BUS_CREDS_CMDLINE;
}
if (tid > 0 && (missing & SD_BUS_CREDS_TID_COMM)) {
_cleanup_free_ char *p = NULL;
if (asprintf(&p, "/proc/%lu/task/%lu/comm", (unsigned long) pid, (unsigned long) tid) < 0)
return -ENOMEM;
r = read_one_line_file(p, &c->tid_comm);
if (r < 0)
return r == -ENOENT ? -ESRCH : r;
c->mask |= SD_BUS_CREDS_TID_COMM;
}
if (missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID)) {
r = cg_pid_get_path(NULL, pid, &c->cgroup);
if (r < 0)
return r;
r = cg_get_root_path(&c->cgroup_root);
if (r < 0)
return r;
c->mask |= missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID);
}
if (missing & SD_BUS_CREDS_AUDIT_SESSION_ID) {
r = audit_session_from_pid(pid, &c->audit_session_id);
if (r < 0 && r != -ENOTSUP && r != -ENXIO && r != -ENOENT)
return r;
else if (r >= 0)
c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
}
if (missing & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
r = audit_loginuid_from_pid(pid, &c->audit_login_uid);
if (r < 0 && r != -ENOTSUP && r != -ENXIO && r != -ENOENT)
return r;
else if (r >= 0)
c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
return 0;
}
int bus_creds_extend_by_pid(sd_bus_creds *c, uint64_t mask, sd_bus_creds **ret) {
_cleanup_bus_creds_unref_ sd_bus_creds *n = NULL;
int r;
assert(c);
assert(ret);
if ((mask & ~c->mask) == 0) {
/* There's already all data we need. */
*ret = sd_bus_creds_ref(c);
return 0;
}
n = bus_creds_new();
if (!n)
return -ENOMEM;
/* Copy the original data over */
if (c->mask & mask & SD_BUS_CREDS_UID) {
n->uid = c->uid;
n->mask |= SD_BUS_CREDS_UID;
}
if (c->mask & mask & SD_BUS_CREDS_GID) {
n->gid = c->gid;
n->mask |= SD_BUS_CREDS_GID;
}
if (c->mask & mask & SD_BUS_CREDS_PID) {
n->pid = c->pid;
n->mask |= SD_BUS_CREDS_PID;
}
if (c->mask & mask & SD_BUS_CREDS_TID) {
n->tid = c->tid;
n->mask |= SD_BUS_CREDS_TID;
}
if (c->mask & mask & SD_BUS_CREDS_PID_STARTTIME) {
n->pid_starttime = c->pid_starttime;
n->mask |= SD_BUS_CREDS_PID_STARTTIME;
}
if (c->mask & mask & SD_BUS_CREDS_COMM) {
n->comm = strdup(c->comm);
if (!n->comm)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_COMM;
}
if (c->mask & mask & SD_BUS_CREDS_TID_COMM) {
n->tid_comm = strdup(c->tid_comm);
if (!n->tid_comm)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_TID_COMM;
}
if (c->mask & mask & SD_BUS_CREDS_EXE) {
n->exe = strdup(c->exe);
if (!n->exe)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_EXE;
}
if (c->mask & mask & SD_BUS_CREDS_CMDLINE) {
n->cmdline = memdup(c->cmdline, c->cmdline_size);
if (!n->cmdline)
return -ENOMEM;
n->cmdline_size = c->cmdline_size;
n->mask |= SD_BUS_CREDS_CMDLINE;
}
if (c->mask & mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_OWNER_UID)) {
n->cgroup = strdup(c->cgroup);
if (!n->cgroup)
return -ENOMEM;
n->cgroup_root = strdup(c->cgroup_root);
if (!n->cgroup_root)
return -ENOMEM;
n->mask |= mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_OWNER_UID);
}
if (c->mask & mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS)) {
n->capability = memdup(c->capability, c->capability_size);
if (!n->capability)
return -ENOMEM;
n->capability_size = c->capability_size;
n->mask |= c->mask & mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS);
}
if (c->mask & mask & SD_BUS_CREDS_AUDIT_SESSION_ID) {
n->audit_session_id = c->audit_session_id;
n->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
}
if (c->mask & mask & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
n->audit_login_uid = c->audit_login_uid;
n->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
if (c->mask & mask & SD_BUS_CREDS_UNIQUE_NAME) {
n->unique_name = strdup(c->unique_name);
if (!n->unique_name)
return -ENOMEM;
}
if (c->mask & mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
n->well_known_names = strv_copy(c->well_known_names);
if (!n->well_known_names)
return -ENOMEM;
}
/* Get more data */
r = bus_creds_add_more(n, mask,
c->mask & SD_BUS_CREDS_PID ? c->pid : 0,
c->mask & SD_BUS_CREDS_TID ? c->tid : 0);
if (r < 0)
return r;
*ret = n;
n = NULL;
return 0;
}