Instead of requiring the caller to format to/from strings,
add typesafe APIs todo this work.
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/libvirt_private.syms | 18 ++++
src/util/viridentity.c | 241 +++++++++++++++++++++++++++++++++++++++++++++++
src/util/viridentity.h | 40 ++++++++
3 files changed, 299 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index de334bb..7f0bb63 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1395,11 +1395,29 @@ virHostdevUpdateDomainActiveDevices;
# util/viridentity.h
virIdentityGetAttr;
virIdentityGetCurrent;
+virIdentityGetSASLUserName;
+virIdentityGetSELinuxContext;
virIdentityGetSystem;
+virIdentityGetUNIXGroupID;
+virIdentityGetUNIXGroupName;
+virIdentityGetUNIXProcessID;
+virIdentityGetUNIXProcessTime;
+virIdentityGetUNIXUserID;
+virIdentityGetUNIXUserName;
+virIdentityGetX509DName;
virIdentityIsEqual;
virIdentityNew;
virIdentitySetAttr;
virIdentitySetCurrent;
+virIdentitySetSASLUserName;
+virIdentitySetSELinuxContext;
+virIdentitySetUNIXGroupID;
+virIdentitySetUNIXGroupName;
+virIdentitySetUNIXProcessID;
+virIdentitySetUNIXProcessTime;
+virIdentitySetUNIXUserID;
+virIdentitySetUNIXUserName;
+virIdentitySetX509DName;
# util/virinitctl.h
diff --git a/src/util/viridentity.c b/src/util/viridentity.c
index a997385..68ccab9 100644
--- a/src/util/viridentity.c
+++ b/src/util/viridentity.c
@@ -352,3 +352,244 @@ bool virIdentityIsEqual(virIdentityPtr identA,
cleanup:
return ret;
}
+
+
+int virIdentityGetUNIXUserName(virIdentityPtr ident,
+ const char **username)
+{
+ return virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_USER_NAME,
+ username);
+}
+
+
+int virIdentityGetUNIXUserID(virIdentityPtr ident,
+ uid_t *uid)
+{
+ int val;
+ const char *userid;
+
+ *uid = -1;
+ if (virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_USER_ID,
+ &userid) < 0)
+ return -1;
+
+ if (!userid)
+ return -1;
+
+ if (virStrToLong_i(userid, NULL, 10, &val) < 0)
+ return -1;
+
+ *uid = (uid_t)val;
+
+ return 0;
+}
+
+int virIdentityGetUNIXGroupName(virIdentityPtr ident,
+ const char **groupname)
+{
+ return virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_GROUP_NAME,
+ groupname);
+}
+
+
+int virIdentityGetUNIXGroupID(virIdentityPtr ident,
+ gid_t *gid)
+{
+ int val;
+ const char *groupid;
+
+ *gid = -1;
+ if (virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
+ &groupid) < 0)
+ return -1;
+
+ if (!groupid)
+ return -1;
+
+ if (virStrToLong_i(groupid, NULL, 10, &val) < 0)
+ return -1;
+
+ *gid = (gid_t)val;
+
+ return 0;
+}
+
+
+int virIdentityGetUNIXProcessID(virIdentityPtr ident,
+ pid_t *pid)
+{
+ unsigned long long val;
+ const char *processid;
+
+ *pid = 0;
+ if (virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
+ &processid) < 0)
+ return -1;
+
+ if (!processid)
+ return -1;
+
+ if (virStrToLong_ull(processid, NULL, 10, &val) < 0)
+ return -1;
+
+ *pid = (pid_t)val;
+
+ return 0;
+}
+
+
+int virIdentityGetUNIXProcessTime(virIdentityPtr ident,
+ unsigned long long *timestamp)
+{
+ const char *processtime;
+ if (virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
+ &processtime) < 0)
+ return -1;
+
+ if (!processtime)
+ return -1;
+
+ if (virStrToLong_ull(processtime, NULL, 10, timestamp) < 0)
+ return -1;
+
+ return 0;
+}
+
+
+int virIdentityGetSASLUserName(virIdentityPtr ident,
+ const char **username)
+{
+ return virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_SASL_USER_NAME,
+ username);
+}
+
+
+int virIdentityGetX509DName(virIdentityPtr ident,
+ const char **dname)
+{
+ return virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_X509_DISTINGUISHED_NAME,
+ dname);
+}
+
+
+int virIdentityGetSELinuxContext(virIdentityPtr ident,
+ const char **context)
+{
+ return virIdentityGetAttr(ident,
+ VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
+ context);
+}
+
+
+int virIdentitySetUNIXUserName(virIdentityPtr ident,
+ const char *username)
+{
+ return virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_USER_NAME,
+ username);
+}
+
+
+int virIdentitySetUNIXUserID(virIdentityPtr ident,
+ uid_t uid)
+{
+ char *val;
+ int ret;
+ if (virAsprintf(&val, "%d", (int)uid) < 0)
+ return -1;
+ ret = virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_USER_ID,
+ val);
+ VIR_FREE(val);
+ return ret;
+}
+
+
+int virIdentitySetUNIXGroupName(virIdentityPtr ident,
+ const char *groupname)
+{
+ return virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_GROUP_NAME,
+ groupname);
+}
+
+
+int virIdentitySetUNIXGroupID(virIdentityPtr ident,
+ gid_t gid)
+{
+ char *val;
+ int ret;
+ if (virAsprintf(&val, "%d", (int)gid) < 0)
+ return -1;
+ ret = virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
+ val);
+ VIR_FREE(val);
+ return ret;
+}
+
+
+int virIdentitySetUNIXProcessID(virIdentityPtr ident,
+ pid_t pid)
+{
+ char *val;
+ int ret;
+ if (virAsprintf(&val, "%llu", (unsigned long long)pid) < 0)
+ return -1;
+ ret = virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
+ val);
+ VIR_FREE(val);
+ return ret;
+}
+
+
+int virIdentitySetUNIXProcessTime(virIdentityPtr ident,
+ unsigned long long timestamp)
+{
+ char *val;
+ int ret;
+ if (virAsprintf(&val, "%llu", timestamp) < 0)
+ return -1;
+ ret = virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
+ val);
+ VIR_FREE(val);
+ return ret;
+}
+
+
+
+int virIdentitySetSASLUserName(virIdentityPtr ident,
+ const char *username)
+{
+ return virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_SASL_USER_NAME,
+ username);
+}
+
+
+int virIdentitySetX509DName(virIdentityPtr ident,
+ const char *dname)
+{
+ return virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_X509_DISTINGUISHED_NAME,
+ dname);
+}
+
+
+int virIdentitySetSELinuxContext(virIdentityPtr ident,
+ const char *context)
+{
+ return virIdentitySetAttr(ident,
+ VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
+ context);
+}
diff --git a/src/util/viridentity.h b/src/util/viridentity.h
index a240c2d..63aa63d 100644
--- a/src/util/viridentity.h
+++ b/src/util/viridentity.h
@@ -65,4 +65,44 @@ bool virIdentityIsEqual(virIdentityPtr identA,
ATTRIBUTE_NONNULL(1)
ATTRIBUTE_NONNULL(2);
+int virIdentityGetUNIXUserName(virIdentityPtr ident,
+ const char **username);
+int virIdentityGetUNIXUserID(virIdentityPtr ident,
+ uid_t *uid);
+int virIdentityGetUNIXGroupName(virIdentityPtr ident,
+ const char **groupname);
+int virIdentityGetUNIXGroupID(virIdentityPtr ident,
+ gid_t *gid);
+int virIdentityGetUNIXProcessID(virIdentityPtr ident,
+ pid_t *pid);
+int virIdentityGetUNIXProcessTime(virIdentityPtr ident,
+ unsigned long long *timestamp);
+int virIdentityGetSASLUserName(virIdentityPtr ident,
+ const char **username);
+int virIdentityGetX509DName(virIdentityPtr ident,
+ const char **dname);
+int virIdentityGetSELinuxContext(virIdentityPtr ident,
+ const char **context);
+
+
+int virIdentitySetUNIXUserName(virIdentityPtr ident,
+ const char *username);
+int virIdentitySetUNIXUserID(virIdentityPtr ident,
+ uid_t uid);
+int virIdentitySetUNIXGroupName(virIdentityPtr ident,
+ const char *groupname);
+int virIdentitySetUNIXGroupID(virIdentityPtr ident,
+ gid_t gid);
+int virIdentitySetUNIXProcessID(virIdentityPtr ident,
+ pid_t pid);
+int virIdentitySetUNIXProcessTime(virIdentityPtr ident,
+ unsigned long long timestamp);
+int virIdentitySetSASLUserName(virIdentityPtr ident,
+ const char *username);
+int virIdentitySetX509DName(virIdentityPtr ident,
+ const char *dname);
+int virIdentitySetSELinuxContext(virIdentityPtr ident,
+ const char *context);
+
+
#endif /* __VIR_IDENTITY_H__ */
--
1.9.3