bus-creds.c revision 5b12334d35eadf1f45cc3d631fd1a2e72ffaea0a
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include "util.h"
#include "cgroup-util.h"
#include "fileio.h"
#include "audit.h"
#include "bus-message.h"
#include "bus-util.h"
#include "time-util.h"
#include "bus-creds.h"
enum {
CAP_OFFSET_PERMITTED = 1,
CAP_OFFSET_EFFECTIVE = 2,
};
void bus_creds_done(sd_bus_creds *c) {
assert(c);
/* For internal bus cred structures that are allocated by
* something else */
free(c->cmdline_array);
}
assert_return(c, NULL);
if (c->allocated) {
c->n_ref++;
} else {
sd_bus_message *m;
/* If this is an embedded creds structure, then
* forward ref counting to the message */
}
return c;
}
assert_return(c, NULL);
if (c->allocated) {
c->n_ref--;
if (c->n_ref == 0) {
bus_creds_done(c);
free(c->capability);
free(c);
}
} else {
sd_bus_message *m;
}
return NULL;
}
assert_return(c, 0);
return c->mask;
}
sd_bus_creds* bus_creds_new(void) {
sd_bus_creds *c;
if (!c)
return NULL;
c->allocated = true;
c->n_ref = 1;
return c;
}
sd_bus_creds *c;
int r;
if (pid == 0)
c = bus_creds_new();
if (!c)
return -ENOMEM;
if (r < 0) {
free(c);
return r;
}
/* Check if the process existed at all, in case we haven't
* figured that out already */
return -ESRCH;
}
*ret = c;
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
assert_return(c, -EINVAL);
assert(c->pid_starttime > 0);
*usec = c->pid_starttime;
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
assert_return(c, -EINVAL);
return 0;
}
int r;
assert_return(c, -EINVAL);
if (!c->unit) {
if (r < 0)
return r;
}
return 0;
}
int r;
assert_return(c, -EINVAL);
if (!c->user_unit) {
if (r < 0)
return r;
}
return 0;
}
int r;
assert_return(c, -EINVAL);
if (!c->slice) {
if (r < 0)
return r;
}
return 0;
}
int r;
assert_return(c, -EINVAL);
if (!c->session) {
if (r < 0)
return r;
}
return 0;
}
assert_return(c, -EINVAL);
}
size_t n, i;
const char *p;
bool first;
assert_return(c, -EINVAL);
if (*p == 0)
n++;
if (!c->cmdline_array)
return -ENOMEM;
if (first)
c->cmdline_array[i++] = (char*) p;
first = *p == 0;
}
c->cmdline_array[i] = NULL;
*cmdline = c->cmdline_array;
return 0;
}
assert_return(c, -EINVAL);
*sessionid = c->audit_session_id;
return 0;
}
assert_return(c, -EINVAL);
*uid = c->audit_login_uid;
return 0;
}
assert(c);
assert(c->capability);
return 0;
}
assert_return(c, -EINVAL);
}
assert_return(c, -EINVAL);
}
assert_return(c, -EINVAL);
}
assert_return(c, -EINVAL);
}
unsigned i;
assert(c);
assert(p);
p += strspn(p, WHITESPACE);
if (sz % 2 != 0)
return -EINVAL;
sz /= 2;
if (!c->capability) {
if (!c->capability)
return -ENOMEM;
}
for (i = 0; i < sz; i ++) {
int x, y;
x = unhexchar(p[i*2]);
if (x < 0 || y < 0)
return -EINVAL;
}
return 0;
}
int r;
assert(c);
if (missing == 0)
return 0;
/* Try to retrieve PID from creds if it wasn't passed to us */
/* Without pid we cannot do much... */
if (pid <= 0)
return 0;
const char *p;
f = fopen(p, "re");
if (!f)
if (missing & SD_BUS_CREDS_UID) {
if (p) {
unsigned long uid;
p += strspn(p, WHITESPACE);
return -EIO;
c->mask |= SD_BUS_CREDS_UID;
continue;
}
}
if (missing & SD_BUS_CREDS_GID) {
if (p) {
unsigned long gid;
p += strspn(p, WHITESPACE);
return -EIO;
c->mask |= SD_BUS_CREDS_GID;
continue;
}
}
if (missing & SD_BUS_CREDS_EFFECTIVE_CAPS) {
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) {
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) {
if (p) {
r = parse_caps(c, CAP_OFFSET_INHERITABLE, p);
if (r < 0)
return r;
continue;
}
}
if (missing & SD_BUS_CREDS_BOUNDING_CAPS) {
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;
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_PID_STARTTIME;
}
if (missing & SD_BUS_CREDS_SELINUX_CONTEXT) {
const char *p;
r = read_one_line_file(p, &c->label);
if (r < 0 && r != -ENOENT)
return r;
else if (r >= 0)
c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
}
if (missing & SD_BUS_CREDS_COMM) {
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_COMM;
}
if (missing & SD_BUS_CREDS_EXE) {
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_EXE;
}
if (missing & SD_BUS_CREDS_CMDLINE) {
const char *p;
if (r < 0)
return r;
if (c->cmdline_length == 0) {
} else
c->mask |= SD_BUS_CREDS_CMDLINE;
}
_cleanup_free_ char *p = NULL;
return -ENOMEM;
r = read_one_line_file(p, &c->tid_comm);
if (r < 0)
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)) {
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) {
return r;
else if (r >= 0)
}
if (missing & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
return r;
else if (r >= 0)
c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
return 0;
}
int r;
assert_return(c, -EINVAL);
/* 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 */
n->mask |= SD_BUS_CREDS_UID;
}
n->mask |= SD_BUS_CREDS_GID;
}
n->mask |= SD_BUS_CREDS_PID;
}
n->mask |= SD_BUS_CREDS_TID;
}
n->pid_starttime = c->pid_starttime;
n->mask |= SD_BUS_CREDS_PID_STARTTIME;
}
if (!n->comm)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_COMM;
}
if (!n->tid_comm)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_TID_COMM;
}
if (!n->exe)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_EXE;
}
if (!n->cmdline)
return -ENOMEM;
n->cmdline_length = c->cmdline_length;
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)) {
if (!n->cgroup)
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)) {
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);
}
n->audit_session_id = c->audit_session_id;
}
n->audit_login_uid = c->audit_login_uid;
n->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
/* Get more data */
r = bus_creds_add_more(n, mask,
if (r < 0)
return r;
*ret = n;
n = NULL;
return 0;
}