int bus_check_peercred(DBusConnection *c) { int fd; struct ucred ucred; socklen_t l; assert(c); assert_se(dbus_connection_get_unix_fd(c, &fd)); l = sizeof(struct ucred); if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) { log_error("SO_PEERCRED failed: %m"); return -errno; } if (l != sizeof(struct ucred)) { log_error("SO_PEERCRED returned wrong size."); return -E2BIG; } if (ucred.uid != 0 && ucred.uid != geteuid()) return -EPERM; return 1; }
CRJinkeWindowManager( int dx, int dy ) : CRGUIWindowManager(NULL) { int bus_fd; if ( CRJinkeScreen::instance==NULL ) _screen = new CRJinkeScreen( dx, dy ); else _screen = CRJinkeScreen::instance; if ( _screen ) { _wid = ((CRJinkeScreen*)_screen)->getWID(); _ownScreen = true; instance = this; #if ENABLE_DBUS_VIEWER_EVENTS==1 //dbus, nanox m_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL); if (!m_bus) { printf ("Failed to connect to the D-BUS daemon"); //return 0; } else { dbus_bus_add_match (m_bus, "type='signal',interface='com.burtonini.dbus.Signal'", NULL); dbus_connection_get_unix_fd(m_bus, &bus_fd); GrRegisterInput(bus_fd); } #endif } }
/** * Attempt to connect to the system bus, and set a filter to deal with * disconnection (see message_filter above). * * @return 1 on success, 0 on failure. */ static int connect_to_bus(void) { DBusError error; struct dbus_core_hook *hook; dbus_error_init(&error); bus_info.connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); if (!bus_info.connection || dbus_error_is_set(&error)) { LogMessage(X_ERROR, "dbus-core: error connecting to system bus: %s (%s)\n", error.name, error.message); goto err_begin; } /* Thankyou. Really, thankyou. */ dbus_connection_set_exit_on_disconnect(bus_info.connection, FALSE); if (!dbus_connection_get_unix_fd(bus_info.connection, &bus_info.fd)) { ErrorF("[dbus-core] couldn't get fd for system bus\n"); goto err_unref; } if (!dbus_connection_add_filter(bus_info.connection, message_filter, &bus_info, NULL)) { ErrorF("[dbus-core] couldn't add filter: %s (%s)\n", error.name, error.message); goto err_fd; } dbus_error_free(&error); AddGeneralSocket(bus_info.fd); RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, &bus_info); for (hook = bus_info.hooks; hook; hook = hook->next) { if (hook->connect) hook->connect(bus_info.connection, hook->data); } return 1; err_fd: bus_info.fd = -1; err_unref: dbus_connection_unref(bus_info.connection); bus_info.connection = NULL; err_begin: dbus_error_free(&error); return 0; }
/* This function returns the security context of the remote end of the dbus connections. Whether it is on the bus or a local connection. */ static int get_calling_context( DBusConnection *connection, DBusMessage *message, security_context_t *scon, DBusError *error) { const char *sender; int r; int fd; /* If sender exists then if sender is NULL this indicates a local connection. Grab the fd from dbus and do an getpeercon to peers process context */ sender = dbus_message_get_sender(message); if (sender) { log_error("SELinux Got Sender %s", sender); r = bus_get_selinux_security_context(connection, sender, scon, error); if (r >= 0) return r; log_error("bus_get_selinux_security_context failed: %m"); return r; } log_debug("SELinux No Sender"); if (!dbus_connection_get_unix_fd(connection, &fd)) { log_error("bus_connection_get_unix_fd failed %m"); return -EINVAL; } r = getpeercon(fd, scon); if (r < 0) { log_error("getpeercon failed %m"); return -errno; } return 0; }
static int get_audit_data( DBusConnection *connection, DBusMessage *message, struct auditstruct *audit, DBusError *error) { const char *sender; int r, fd; struct ucred ucred; socklen_t len; sender = dbus_message_get_sender(message); if (sender) return bus_get_audit_data(connection, sender, audit, error); if (!dbus_connection_get_unix_fd(connection, &fd)) return -EINVAL; r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len); if (r < 0) { log_error("Failed to determine peer credentials: %m"); return -errno; } audit->uid = ucred.uid; audit->gid = ucred.gid; r = audit_loginuid_from_pid(ucred.pid, &audit->loginuid); if (r < 0) return r; r = get_process_cmdline(ucred.pid, LINE_MAX, true, &audit->cmdline); if (r < 0) return r; return 0; }
void* serverthread(void*) { DBusMessage* msg; DBusError err; int ret; printf("Listening for method calls\n"); if (pipe(quitpipe) == -1) { fprintf(stderr, "Could not create pipe.\n"); return 0; } // initialise the error dbus_error_init(&err); // connect to the bus and check for errors conn = dbus_bus_get(DBUS_BUS_SESSION, &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Connection Error (%s)\n", err.message); dbus_error_free(&err); return 0; } if (NULL == conn) { fprintf(stderr, "Connection Null\n"); return 0; } // request our name on the bus and check for errors ret = dbus_bus_request_name(conn, "vandenoever.strigi", DBUS_NAME_FLAG_REPLACE_EXISTING , &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Name Error (%s)\n", err.message); dbus_error_free(&err); } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { fprintf(stderr, "Not Primary Owner (%d)\n", ret); return 0; } for (int i=0; i<10; ++i) { printf("server\n"); } int fd; dbus_connection_get_unix_fd(conn, &fd); bool run; do { // block until there's activit on the port fd_set rfds; struct timeval tv; FD_ZERO(&rfds); FD_SET(fd, &rfds); FD_SET(quitpipe[0], &rfds); printf("server\n"); tv.tv_sec = 50000; tv.tv_usec = 0; int retval = select(fd+1, &rfds, 0, 0, &tv); if (retval == -1 || FD_ISSET(quitpipe[0], &rfds)) break; // non blocking read of the next available message dbus_connection_read_write_dispatch(conn, 0); // dbus_connection_read_write(conn, 0); msg = dbus_connection_pop_message(conn); dbus_connection_flush(conn); dbus_message_unref(msg); STRIGI_MUTEX_LOCK(&lock); run = keeprunning; STRIGI_MUTEX_UNLOCK(&lock); } while (run); dbus_connection_unref(conn); dbus_connection_unref(conn); dbus_shutdown(); printf("after dbus_shutdown\n"); return 0; }