dbus-common.c revision 720ce21d444f6497299c4c99a76fda546b06716a
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include "log.h"
#include "dbus-common.h"
#include "util.h"
int bus_check_peercred(DBusConnection *c) {
int fd;
socklen_t l;
assert(c);
l = sizeof(struct ucred);
log_error("SO_PEERCRED failed: %m");
return -errno;
}
if (l != sizeof(struct ucred)) {
log_error("SO_PEERCRED returned wrong size.");
return -E2BIG;
}
return -EPERM;
return 1;
}
/* If we are root, then let's not go via the bus */
if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error)))
return -EIO;
if (bus_check_peercred(bus) < 0) {
return -EACCES;
}
/* This complexity should probably move into D-Bus itself:
*
for (;;) {
break;
break;
if (!dbus_connection_read_write_dispatch(bus, ((begin + TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
break;
}
if (!dbus_connection_get_is_connected(bus)) {
return -ECONNREFUSED;
}
if (!dbus_connection_get_is_authenticated(bus)) {
return -EACCES;
}
if (private)
*private = true;
} else {
return -EIO;
if (private)
*private = false;
}
return 0;
}
/* Sometimes the D-Bus server is a little bit too verbose with
* its error messages, so let's override them here */
return "Access denied";
}