Validates the wwn while parsing, error out if it's malformed. * src/util/util.h: Declare virValidateWWN * src/util/util.c: Implement virValidateWWN * src/libvirt_private.syms: Export virValidateWWN. * src/conf/domain_conf.h: New member 'wwn' for disk def. * src/conf/domain_conf.c: Parse and format disk <wwn> --- src/conf/domain_conf.c | 12 +++++++++++- src/conf/domain_conf.h | 1 + src/libvirt_private.syms | 1 + src/util/util.c | 24 ++++++++++++++++++++++++ src/util/util.h | 2 ++ 5 files changed, 39 insertions(+), 1 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 554298d..df6599a 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -950,6 +950,7 @@ void virDomainDiskDefFree(virDomainDiskDefPtr def) VIR_FREE(def->mirror); VIR_FREE(def->mirrorFormat); VIR_FREE(def->auth.username); + VIR_FREE(def->wwn); if (def->auth.secretType == VIR_DOMAIN_DISK_SECRET_TYPE_USAGE) VIR_FREE(def->auth.secret.usage); virStorageEncryptionFree(def->encryption); @@ -3353,7 +3354,6 @@ cleanup: goto cleanup; } - /* Parse the XML definition for a disk * @param node XML nodeset to parse for disk definition */ @@ -3402,6 +3402,7 @@ virDomainDiskDefParseXML(virCapsPtr caps, char *authUUID = NULL; char *usageType = NULL; char *tray = NULL; + char *wwn = NULL; if (VIR_ALLOC(def) < 0) { virReportOOMError(); @@ -3704,6 +3705,12 @@ virDomainDiskDefParseXML(virCapsPtr caps, } else if (!serial && xmlStrEqual(cur->name, BAD_CAST "serial")) { serial = (char *)xmlNodeGetContent(cur); + } else if (!wwn && + xmlStrEqual(cur->name, BAD_CAST "wwn")) { + wwn = (char *)xmlNodeGetContent(cur); + + if (!virValidateWWN(wwn)) + goto error; } else if (xmlStrEqual(cur->name, BAD_CAST "boot")) { /* boot is parsed as part of virDomainDeviceInfoParseXML */ } @@ -4000,6 +4007,8 @@ virDomainDiskDefParseXML(virCapsPtr caps, encryption = NULL; def->serial = serial; serial = NULL; + def->wwn = wwn; + wwn = NULL; if (!def->driverType && caps->defaultDiskDriverType && @@ -11561,6 +11570,7 @@ virDomainDiskDefFormat(virBufferPtr buf, if (def->transient) virBufferAddLit(buf, " <transient/>\n"); virBufferEscapeString(buf, " <serial>%s</serial>\n", def->serial); + virBufferEscapeString(buf, " <wwn>%s</wwn>\n", def->wwn); if (def->encryption) { virBufferAdjustIndent(buf, 6); if (virStorageEncryptionFormat(buf, def->encryption) < 0) diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index dfdae49..3b335a0 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -571,6 +571,7 @@ struct _virDomainDiskDef { virDomainBlockIoTuneInfo blkdeviotune; char *serial; + char *wwn; int cachemode; int error_policy; /* enum virDomainDiskErrorPolicy */ int rerror_policy; /* enum virDomainDiskErrorPolicy */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 27eb43e..a0675f4 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1240,6 +1240,7 @@ virStrToLong_ull; virStrcpy; virStrncpy; virTrimSpaces; +virValidateWWN; virVasprintf; diff --git a/src/util/util.c b/src/util/util.c index 91eab72..682a504 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -45,6 +45,7 @@ #include <termios.h> #include <pty.h> #include <locale.h> +#include <regex.h> #if HAVE_LIBDEVMAPPER_H # include <libdevmapper.h> @@ -3052,3 +3053,26 @@ bool virIsDevMapperDevice(const char *dev_name ATTRIBUTE_UNUSED) return false; } #endif + +#define WWN_REG_PATTERN "[0-9a-zA-Z]{16}" +bool +virValidateWWN(const char *wwn) { + regex_t re; + int err; + char error[100]; + + if ((err = regcomp(&re, WWN_REG_PATTERN, REG_EXTENDED)) != 0) + goto fail; + + if ((err = regexec(&re, wwn, 0, NULL, 0)) != 0) + goto fail; + + regfree(&re); + return true; + +fail: + regerror(err, &re, error, sizeof(error)); + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Malformed wwn: %s"), error); + return false; +} diff --git a/src/util/util.h b/src/util/util.h index a5d892d..0c0efad 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -277,4 +277,6 @@ int virBuildPathInternal(char **path, ...) ATTRIBUTE_SENTINEL; bool virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1); +bool virValidateWWN(const char *wwn); + #endif /* __VIR_UTIL_H__ */ -- 1.7.7.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list