I'm using wmic as a standalone program, and I need output of bin/wmic to be defined with a more strict syntax, so that data that reported by the tool do not clash with metadata characters (|,()) etc. Please find attached the patch that changes the wmic output so that a) such characters are escaped b) null values are reported as "(null)" string instead of "NULL", so a literal "NULL" that WMI may eventually report won't be treated as a null value (if WMI reports "(null)" then the output will be properly quoted as "\(null\)"). diff --git a/source4/lib/wmi/tools/wmic.c b/source4/lib/wmi/tools/wmic.c index bbfe5ed..afbd1da 100644 --- a/source4/lib/wmi/tools/wmic.c +++ b/source4/lib/wmi/tools/wmic.c @@ -92,6 +92,37 @@ static void parse_args(int argc, char *argv[], struct program_args *pmyargs) poptFreeContext(pc); } +static void +escape_string( const char * src, char * dst, int len) +{ + char *p = dst, *end = dst + len - 1; + const char *q = src; + + if ( q == NULL) { + strncpy( dst, "(null)", len); + return; + } + + while ( *q && p <= end) { + if ( strchr( "|\\(),", *q)) { + *p++ = '\\'; + *p++ = *q++; + } else if ( *q == '\n') { + *p++ = '\\'; + *p++ = 'n'; + q++; + } else if ( *q == '\r') { + *p++ = '\\'; + *p++ = 'r'; + q++; + } else { + *p++ = *q++; + } + } + + *p++ = 0; +} + #define WERR_CHECK(msg) if (!W_ERROR_IS_OK(result)) { \ DEBUG(0, ("ERROR: %s\n", msg)); \ goto error; \ @@ -99,20 +130,33 @@ static void parse_args(int argc, char *argv[], struct program_args *pmyargs) DEBUG(1, ("OK : %s\n", msg)); \ } -#define RETURN_CVAR_ARRAY_STR(fmt, arr) {\ +#define RETURN_CVAR_ARRAY_STR_START(arr) {\ uint32_t i;\ char *r;\ \ if (!arr) {\ - return talloc_strdup(mem_ctx, "NULL");\ + return talloc_strdup(mem_ctx, "(null)");\ }\ r = talloc_strdup(mem_ctx, "(");\ - for (i = 0; i < arr->count; ++i) {\ - r = talloc_asprintf_append(r, fmt "%s", arr->item[i], (i+1 == arr->count)?"":",");\ + for (i = 0; i < arr->count; ++i) { + + +#define RETURN_CVAR_ARRAY_STR_END(fmt, arr, item) \ + r = talloc_asprintf_append(r, fmt "%s", item, (i+1 == arr->count)?"":",");\ }\ return talloc_asprintf_append(r, ")");\ } +#define RETURN_CVAR_ARRAY_STR(fmt, arr) \ + RETURN_CVAR_ARRAY_STR_START(arr) \ + RETURN_CVAR_ARRAY_STR_END(fmt, arr, arr->item[i]) + +#define RETURN_CVAR_ARRAY_ESCAPED(fmt, arr) \ + RETURN_CVAR_ARRAY_STR_START(arr) \ + char buf[2048]; \ + escape_string( arr->item[i], buf, 2048); \ + RETURN_CVAR_ARRAY_STR_END(fmt, arr, buf) + char *string_CIMVAR(TALLOC_CTX *mem_ctx, union CIMVAR *v, enum CIMTYPE_ENUMERATION cimtype) { switch (cimtype) { @@ -129,7 +173,11 @@ char *string_CIMVAR(TALLOC_CTX *mem_ctx, union CIMVAR *v, enum CIMTYPE_ENUMERATI case CIM_BOOLEAN: return talloc_asprintf(mem_ctx, "%s", v->v_boolean?"True":"False"); case CIM_STRING: case CIM_DATETIME: - case CIM_REFERENCE: return talloc_asprintf(mem_ctx, "%s", v->v_string); + case CIM_REFERENCE: { + char buf[2048]; + escape_string((char*) v-> v_string, buf, 2048); + return talloc_asprintf(mem_ctx, "%s", buf); + } case CIM_CHAR16: return talloc_asprintf(mem_ctx, "Unsupported"); case CIM_OBJECT: return talloc_asprintf(mem_ctx, "Unsupported"); case CIM_ARR_SINT8: RETURN_CVAR_ARRAY_STR("%d", v->a_sint8); @@ -143,9 +191,9 @@ char *string_CIMVAR(TALLOC_CTX *mem_ctx, union CIMVAR *v, enum CIMTYPE_ENUMERATI case CIM_ARR_REAL32: RETURN_CVAR_ARRAY_STR("%f", v->a_real32); case CIM_ARR_REAL64: RETURN_CVAR_ARRAY_STR("%f", v->a_real64); case CIM_ARR_BOOLEAN: RETURN_CVAR_ARRAY_STR("%d", v->a_boolean); - case CIM_ARR_STRING: RETURN_CVAR_ARRAY_STR("%s", v->a_string); - case CIM_ARR_DATETIME: RETURN_CVAR_ARRAY_STR("%s", v->a_datetime); - case CIM_ARR_REFERENCE: RETURN_CVAR_ARRAY_STR("%s", v->a_reference); + case CIM_ARR_STRING: RETURN_CVAR_ARRAY_ESCAPED("%s", v->a_string); + case CIM_ARR_DATETIME: RETURN_CVAR_ARRAY_ESCAPED("%s", v->a_datetime); + case CIM_ARR_REFERENCE: RETURN_CVAR_ARRAY_ESCAPED("%s", v->a_reference); default: return talloc_asprintf(mem_ctx, "Unsupported"); } }
Jelmer will handle this.
Here we'll have to wait: consider this: http://lists.samba.org/archive/samba-technical/2009-May/064983.html
I think it would be better to fix this (half of a year is a long time to wait - and the WMI work seems still not complete). I don't hesitate anymore to apply it - but I'm not able to do it with your patch (it gives me always errors). Could you provide your complete modified wmic.c (maybe as attachment)?
(In reply to comment #3) > Could you provide your complete modified wmic.c (maybe as attachment)? I've sent wmic.c to Matthias. If anyone else needs it, ping me. /dk
Applied to "master". Thanks for the patch!
Sorry I have revert the patch soon. Consider the discussion with Jelmer for the reason: http://lists.samba.org/archive/samba-technical/2009-September/066944.html
Created attachment 4729 [details] Changed wmic.c Updated wmic.c with patch. Later we need to reintroduce this file (after the WMI merge).
The URL contains the old patch - therefore remove it. Change to a better title