From 9c57fac9b7c4a2be4e34565a5881669b330b1ca7 Mon Sep 17 00:00:00 2001 From: Daniel Henrique Barboza Date: Thu, 26 Sep 2019 11:56:36 -0300 Subject: [PATCH] src/driver.c: add virConnectValidateURIPath() The code to validate the URI path is repeated across several files. This patch creates a common validation code to be used across all of them. Reviewed-by: Cole Robinson Suggested-by: Cole Robinson Signed-off-by: Daniel Henrique Barboza --- src/driver.c | 26 ++++++++++++++++++++++++++ src/driver.h | 4 ++++ src/libvirt_private.syms | 1 + 3 files changed, 31 insertions(+) diff --git a/src/driver.c b/src/driver.c index 5e8f68f6df..6b75622689 100644 --- a/src/driver.c +++ b/src/driver.c @@ -269,3 +269,29 @@ virSetConnectStorage(virConnectPtr conn) VIR_DEBUG("Override storage connection with %p", conn); return virThreadLocalSet(&connectStorage, conn); } + +bool +virConnectValidateURIPath(const char *uriPath, + const char *entityName, + bool privileged) +{ + if (privileged) { + if (STRNEQ(uriPath, "/system")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unexpected %s URI path '%s', try " + "%s:///system"), + entityName, uriPath, entityName); + return false; + } + } else { + if (STRNEQ(uriPath, "/session")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unexpected %s URI path '%s', try " + "%s:///session"), + entityName, uriPath, entityName); + return false; + } + } + + return true; +} diff --git a/src/driver.h b/src/driver.h index f7d667a03c..68c0004d86 100644 --- a/src/driver.h +++ b/src/driver.h @@ -127,3 +127,7 @@ int virSetConnectNWFilter(virConnectPtr conn); int virSetConnectNodeDev(virConnectPtr conn); int virSetConnectSecret(virConnectPtr conn); int virSetConnectStorage(virConnectPtr conn); + +bool virConnectValidateURIPath(const char *uriPath, + const char *entityName, + bool privileged); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2b10a1030e..7b681fac64 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1344,6 +1344,7 @@ virStreamClass; # driver.h +virConnectValidateURIPath; virGetConnectInterface; virGetConnectNetwork; virGetConnectNodeDev;