]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Expose ownership ID parsing
authorMartin Kletzander <mkletzan@redhat.com>
Fri, 24 May 2013 15:35:01 +0000 (17:35 +0200)
committerMartin Kletzander <mkletzan@redhat.com>
Wed, 24 Jul 2013 12:29:11 +0000 (14:29 +0200)
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
src/security/security_dac.c
src/util/virutil.c
src/util/virutil.h

index 22c2f25688d0bafbfff05326bed850c320992031..0f8edf872f262b3ff35b4ed143bcc179a39257eb 100644 (file)
@@ -2050,6 +2050,7 @@ virIsCapableVport;
 virIsDevMapperDevice;
 virManageVport;
 virParseNumber;
+virParseOwnershipIds;
 virParseVersionString;
 virPipeReadUntilEOF;
 virReadFCHost;
index fa3b869487055935d91cd7d8efcf3e5e9ec310ea..8cbb0832f1841b173699bb3f3e7c40f04374b169 100644 (file)
@@ -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)
index 0b54ef76e5d3546fd7df5b9d169966800e8c0132..c6c817ff550182328858e109949b08facb003b26 100644 (file)
@@ -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;
+}
index 0083c8830ed4ce25513ba1c183bdd84d5ad7b5aa..526c610877eac4f96f83be11b40a370302fd354e 100644 (file)
@@ -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__ */