-/*
- If the admin toggles the selinux enforcment mode this callback
- will get called before the next access check
-*/
-static int setenforce_callback(int enforcing)
-{
- selinux_enforcing = enforcing;
- return 0;
-}
-
-/* This mimics dbus_bus_get_unix_user() */
-static int bus_get_selinux_security_context(
- DBusConnection *connection,
- const char *name,
- char **scon,
- DBusError *error) {
-
- DBusMessage *m = NULL, *reply = NULL;
- int r;
-
- m = dbus_message_new_method_call(
- DBUS_SERVICE_DBUS,
- DBUS_PATH_DBUS,
- DBUS_INTERFACE_DBUS,
- "GetConnectionSELinuxSecurityContext");
- if (!m) {
- r = -errno;
- dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
- goto finish;
- }
-
- r = dbus_message_append_args(
- m,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID);
- if (!r) {
- r = -errno;
- dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
- goto finish;
- }
-
- reply = dbus_connection_send_with_reply_and_block(connection, m, -1, error);
- if (!reply) {
- r = -errno;
- goto finish;
- }
-
- r = dbus_set_error_from_message(error, reply);
- if (!r) {
- r = -errno;
- goto finish;
- }
-
- r = dbus_message_get_args(
- reply, error,
- DBUS_TYPE_STRING, scon,
- DBUS_TYPE_INVALID);
- if (!r) {
- r = -errno;
- goto finish;
- }
-
- r = 0;
-finish:
- if (m)
- dbus_message_unref(m);
-
- if (reply)
- dbus_message_unref(reply);
-
- return r;
-}
-
-static int get_cmdline(pid_t pid, char **cmdline) {
- char buf[PATH_MAX];
- FILE *f;
- int count;
- int n;
-
- snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long) pid);
- f = fopen(buf, "re");
- if (!f) {
- return -errno;
- }
- count = fread(buf, 1, sizeof(buf), f);
- fclose(f);
- if (! count) {
- return -errno;
- }
- for (n = 0; n < count - 1; n++)
- {
- if (buf[n] == '\0')
- buf[n] = ' ';
- }
- (*cmdline) = strdup(buf);
- if (! (*cmdline)) {
- return -errno;
- }
- return 0;
-}
-
-static int get_pid_id(pid_t pid, const char *file, uid_t *id) {
- char buf[PATH_MAX];
- int r = 0;
- FILE *f;
- snprintf(buf, sizeof(buf), "/proc/%lu/%s", (unsigned long) pid, file);
- f = fopen(buf, "re");
- if (!f)
- return -errno;
- fscanf(f, "%d", id);
- if (ferror(f))
- r = -errno;
- fclose(f);
- return r;
-}
-
-/* This mimics dbus_bus_get_unix_user() */
-static int bus_get_audit_data(
- DBusConnection *connection,
- const char *name,
- struct auditstruct *audit,
- DBusError *error) {
-
- pid_t pid;
- DBusMessage *m = NULL, *reply = NULL;
- int r = -1;
-
- m = dbus_message_new_method_call(
- DBUS_SERVICE_DBUS,
- DBUS_PATH_DBUS,
- DBUS_INTERFACE_DBUS,
- "GetConnectionUnixProcessID");
- if (!m) {
- r = -errno;
- dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
- goto finish;
- }
-
- r = dbus_message_append_args(
- m,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID);
- if (!r) {
- r = -errno;
- dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
- goto finish;
- }
-
- reply = dbus_connection_send_with_reply_and_block(connection, m, -1, error);
- if (!reply) {
- r = -errno;
- goto finish;
- }
-
- r = dbus_set_error_from_message(error, reply);
- if (!r) {
- r = -errno;
- goto finish;
- }
-
- r = dbus_message_get_args(
- reply, error,
- DBUS_TYPE_UINT32, &pid,
- DBUS_TYPE_INVALID);
- if (!r) {
- r = -errno;
- goto finish;
- }
-
- r = get_pid_id(pid, "loginuid", &(audit->loginuid));
- if (r)
- goto finish;
-
- r = get_pid_id(pid, "uid", &(audit->uid));
- if (r)
- goto finish;
-
- r = get_pid_id(pid, "gid", &(audit->gid));
- if (r)
- goto finish;
-
- r = get_cmdline(pid, &(audit->cmdline));
- if (r)
- goto finish;
-
- r = 0;
-finish:
- if (m)
- dbus_message_unref(m);
- if (reply)
- dbus_message_unref(reply);
- return r;
-}
-