From: Daniel P. BerrangĂ© Date: Thu, 29 Apr 2021 14:52:20 +0000 (+0100) Subject: src: add API to determine if current identity is a system identity X-Git-Tag: v7.4.0-rc1~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11f077e286997e2621fdb7bef75cc244f3eb24f1;p=thirdparty%2Flibvirt.git src: add API to determine if current identity is a system identity This is essentially a way to determine if the current identity is that of another libvirt daemon. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. BerrangĂ© --- diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2ea950c5cd..1df4b8cfe8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2411,6 +2411,7 @@ virIdentityGetUNIXGroupID; virIdentityGetUNIXUserID; virIdentityGetUserName; virIdentityGetX509DName; +virIdentityIsCurrentElevated; virIdentityNew; virIdentityNewCopy; virIdentityRestoreHelper; diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 2e3fcc5add..e7e5c31241 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -366,6 +366,34 @@ virIdentity *virIdentityGetSystem(void) } +/** + * virIdentityIsCurrentElevated: + * + * Determine if the current identity has elevated privileges. + * This indicates that it was invoked on behalf of the + * user by a libvirt daemon. + * + * Returns: true if elevated + */ +int virIdentityIsCurrentElevated(void) +{ + g_autoptr(virIdentity) current = virIdentityGetCurrent(); + const char *currentToken = NULL; + int rv; + + if (!current) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No current identity")); + return -1; + } + + rv = virIdentityGetSystemToken(current, ¤tToken); + if (rv <= 0) + return rv; + + return STREQ_NULLABLE(currentToken, systemToken); +} + /** * virIdentityNew: * diff --git a/src/util/viridentity.h b/src/util/viridentity.h index 848e5b2056..6da6d0c557 100644 --- a/src/util/viridentity.h +++ b/src/util/viridentity.h @@ -35,6 +35,7 @@ virIdentity *virIdentityElevateCurrent(void); void virIdentityRestoreHelper(virIdentity **identptr); +int virIdentityIsCurrentElevated(void); virIdentity *virIdentityGetSystem(void); virIdentity *virIdentityNew(void);