Bug 6076 - WMI: Better format of outputs in wmic
Summary: WMI: Better format of outputs in wmic
Status: ASSIGNED
Alias: None
Product: Samba 4.0
Classification: Unclassified
Component: Tools (show other bugs)
Version: unspecified
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---
Assignee: Jelmer Vernooij
QA Contact: samba4-qa@samba.org
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-01-29 12:15 UTC by Dmitry Karasik
Modified: 2009-10-19 06:37 UTC (History)
0 users

See Also:


Attachments
Changed wmic.c (8.44 KB, text/plain)
2009-09-22 09:04 UTC, Matthias Dieter Wallnöfer
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Karasik 2009-01-29 12:15:56 UTC
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");
 	}
 }
Comment 1 Matthias Dieter Wallnöfer 2009-02-01 10:32:06 UTC
Jelmer will handle this.
Comment 2 Matthias Dieter Wallnöfer 2009-06-22 09:01:05 UTC
Here we'll have to wait: consider this: http://lists.samba.org/archive/samba-technical/2009-May/064983.html
Comment 3 Matthias Dieter Wallnöfer 2009-09-07 14:58:24 UTC
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)?
Comment 4 Dmitry Karasik 2009-09-07 15:49:32 UTC
(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

Comment 5 Matthias Dieter Wallnöfer 2009-09-08 04:11:53 UTC
Applied to "master". Thanks for the patch!
Comment 6 Matthias Dieter Wallnöfer 2009-09-22 09:00:58 UTC
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
Comment 7 Matthias Dieter Wallnöfer 2009-09-22 09:04:23 UTC
Created attachment 4729 [details]
Changed wmic.c

Updated wmic.c with patch. Later we need to reintroduce this file (after the WMI merge).
Comment 8 Matthias Dieter Wallnöfer 2009-09-22 09:06:01 UTC
The URL contains the old patch - therefore remove it.
Change to a better title