From bba579b6e06b1035776489432f70d048051eaa56 Mon Sep 17 00:00:00 2001 From: Martin Kletzander Date: Fri, 24 May 2013 17:35:01 +0200 Subject: [PATCH] Expose ownership ID parsing Parsing 'user:group' is useful even outside the DAC security driver, so expose the most abstract function which has no DAC security driver bits in itself. --- src/libvirt_private.syms | 1 + src/security/security_dac.c | 51 ++------------------------------- src/util/virutil.c | 56 +++++++++++++++++++++++++++++++++++++ src/util/virutil.h | 2 ++ 4 files changed, 62 insertions(+), 48 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 22c2f25688..0f8edf872f 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2050,6 +2050,7 @@ virIsCapableVport; virIsDevMapperDevice; virManageVport; virParseNumber; +virParseOwnershipIds; virParseVersionString; virPipeReadUntilEOF; virReadFCHost; diff --git a/src/security/security_dac.c b/src/security/security_dac.c index fa3b869487..8cbb0832f1 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -33,6 +33,7 @@ #include "virscsi.h" #include "virstoragefile.h" #include "virstring.h" +#include "virutil.h" #define VIR_FROM_THIS VIR_FROM_SECURITY #define SECURITY_DAC_NAME "dac" @@ -72,52 +73,6 @@ virSecurityDACSetDynamicOwnership(virSecurityManagerPtr mgr, priv->dynamicOwnership = dynamicOwnership; } -static int -parseIds(const char *label, uid_t *uidPtr, gid_t *gidPtr) -{ - int rc = -1; - uid_t theuid; - gid_t thegid; - char *tmp_label = NULL; - char *sep = NULL; - char *owner = NULL; - char *group = NULL; - - if (VIR_STRDUP(tmp_label, label) < 0) - goto cleanup; - - /* Split label */ - sep = strchr(tmp_label, ':'); - if (sep == NULL) { - virReportError(VIR_ERR_INVALID_ARG, - _("Missing separator ':' in DAC label \"%s\""), - label); - goto cleanup; - } - *sep = '\0'; - owner = tmp_label; - group = sep + 1; - - /* Parse owner and group, error message is defined by - * virGetUserID or virGetGroupID. - */ - if (virGetUserID(owner, &theuid) < 0 || - virGetGroupID(group, &thegid) < 0) - goto cleanup; - - if (uidPtr) - *uidPtr = theuid; - if (gidPtr) - *gidPtr = thegid; - - rc = 0; - -cleanup: - VIR_FREE(tmp_label); - - return rc; -} - /* returns 1 if label isn't found, 0 on success, -1 on error */ static int virSecurityDACParseIds(virDomainDefPtr def, uid_t *uidPtr, gid_t *gidPtr) @@ -135,7 +90,7 @@ virSecurityDACParseIds(virDomainDefPtr def, uid_t *uidPtr, gid_t *gidPtr) return 1; } - if (parseIds(seclabel->label, &uid, &gid) < 0) + if (virParseOwnershipIds(seclabel->label, &uid, &gid) < 0) return -1; if (uidPtr) @@ -206,7 +161,7 @@ virSecurityDACParseImageIds(virDomainDefPtr def, return 1; } - if (parseIds(seclabel->imagelabel, &uid, &gid) < 0) + if (virParseOwnershipIds(seclabel->imagelabel, &uid, &gid) < 0) return -1; if (uidPtr) diff --git a/src/util/virutil.c b/src/util/virutil.c index 0b54ef76e5..c6c817ff55 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -2017,3 +2017,59 @@ virCompareLimitUlong(unsigned long long a, unsigned long b) return -1; } + +/** + * virParseOwnershipIds: + * + * Parse the usual "uid:gid" ownership specification into uid_t and + * gid_t passed as parameters. NULL value for those parameters mean + * the information is not needed. Also, none of those values are + * changed in case of any error. + * + * Returns -1 on error, 0 otherwise. + */ +int +virParseOwnershipIds(const char *label, uid_t *uidPtr, gid_t *gidPtr) +{ + int rc = -1; + uid_t theuid; + gid_t thegid; + char *tmp_label = NULL; + char *sep = NULL; + char *owner = NULL; + char *group = NULL; + + if (VIR_STRDUP(tmp_label, label) < 0) + goto cleanup; + + /* Split label */ + sep = strchr(tmp_label, ':'); + if (sep == NULL) { + virReportError(VIR_ERR_INVALID_ARG, + _("Failed to parse uid and gid from '%s'"), + label); + goto cleanup; + } + *sep = '\0'; + owner = tmp_label; + group = sep + 1; + + /* Parse owner and group, error message is defined by + * virGetUserID or virGetGroupID. + */ + if (virGetUserID(owner, &theuid) < 0 || + virGetGroupID(group, &thegid) < 0) + goto cleanup; + + if (uidPtr) + *uidPtr = theuid; + if (gidPtr) + *gidPtr = thegid; + + rc = 0; + +cleanup: + VIR_FREE(tmp_label); + + return rc; +} diff --git a/src/util/virutil.h b/src/util/virutil.h index 0083c8830e..526c610877 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -169,4 +169,6 @@ char *virFindFCHostCapableVport(const char *sysfs_prefix); int virCompareLimitUlong(unsigned long long a, unsigned long b); +int virParseOwnershipIds(const char *label, uid_t *uidPtr, gid_t *gidPtr); + #endif /* __VIR_UTIL_H__ */