From a60d22e3677c2bc328fdf26acf1b9987efe072ad Mon Sep 17 00:00:00 2001 From: Holger Macht Date: Thu, 26 Oct 2006 19:28:47 +0200 Subject: add function to query if the _current_ user possesses a privilege --- liblazy/liblazy.h | 14 ++++++++++++++ liblazy/liblazy_polkit.c | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/liblazy/liblazy.h b/liblazy/liblazy.h index fd8a721..59bc82f 100644 --- a/liblazy/liblazy.h +++ b/liblazy/liblazy.h @@ -250,6 +250,20 @@ int liblazy_hal_find_device_by_string_match(char *key, char *value, char ***strl int liblazy_polkit_is_user_allowed_by_name(char *user, char *privilege, char *ressource); +/** @brief check if a user possesses a privilege + * + * Functions asks the PolicyKit daemon if the user with the given uid + * possesses a given privilege on a optional given ressource + * + * @param uid the uid to check against + * @param privilege the privilege to check for + * @param ressource the ressource to check for or NULL + * + * @return 0 on success, LIBLAZY_ERROR_* on failure + */ +int liblazy_polkit_is_user_allowed_by_uid(int uid, char *privilege, + char *ressource); + /** @brief check if a user possesses a privilege * * Functions asks the PolicyKit daemon if the current user possesses a diff --git a/liblazy/liblazy_polkit.c b/liblazy/liblazy_polkit.c index e33cf2e..0a2db2f 100644 --- a/liblazy/liblazy_polkit.c +++ b/liblazy/liblazy_polkit.c @@ -28,6 +28,9 @@ #include #include +#include +#include +#include #define DBUS_POLKIT_SERVICE "org.freedesktop.PolicyKit" #define DBUS_POLKIT_PATH "/org/freedesktop/PolicyKit/Manager" @@ -82,9 +85,24 @@ int liblazy_polkit_is_user_allowed_by_name(char *user, return is_allowed; } +int liblazy_polkit_is_user_allowed_by_uid(int uid, char *privilege, + char *ressource) +{ + struct passwd *pw = getpwuid(uid); + + if (pw == NULL) { + ERROR("Could not get current username: %s", strerror(errno)); + return LIBLAZY_ERROR_GENERAL; + } + + return liblazy_polkit_is_user_allowed_by_name(pw->pw_name, privilege, + ressource); +} + int liblazy_polkit_is_user_allowed(char *privilege, char *ressource) { char *user = getenv("USER"); return liblazy_polkit_is_user_allowed_by_name(user, privilege, ressource); } + -- cgit v1.2.3