From: Jule Anger Date: Thu, 31 Mar 2022 08:20:20 +0000 (+0200) Subject: smbstatus: add a connections dictionary X-Git-Tag: samba-4.17.0rc1~35 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=138befe439152b6eaf990f5c33676b373c5dac6e;p=thirdparty%2Fsamba.git smbstatus: add a connections dictionary Adds an empty json dictionary under the key "tcons" and adds foreach connection a dictionary with information to the shares dictionary. Only print to stdout, if json_output is not set. Signed-off-by: Jule Anger Reviewed-by: Ralph Boehme --- diff --git a/source3/utils/status.c b/source3/utils/status.c index 28e673db09a..81b4a11ae5c 100644 --- a/source3/utils/status.c +++ b/source3/utils/status.c @@ -439,10 +439,13 @@ static int traverse_connections_stdout(struct traverse_state *state, static int prepare_connections(struct traverse_state *state) { - /* always print header line */ - d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing"); - d_printf("---------------------------------------------------------------------------------------------\n"); - + if (!state->json_output) { + /* always print header line */ + d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing"); + d_printf("---------------------------------------------------------------------------------------------\n"); + } else { + add_section_to_json(state, "tcons"); + } return 0; } @@ -517,13 +520,18 @@ static int traverse_connections(const struct connections_data *crec, } } - result = traverse_connections_stdout(state, - crec->servicename, - server_id_str_buf(crec->pid, &tmp), - crec->machine, - timestr, - encryption, - signing); + if (!state->json_output) { + result = traverse_connections_stdout(state, + crec->servicename, + server_id_str_buf(crec->pid, &tmp), + crec->machine, + timestr, + encryption, + signing); + } else { + result = traverse_connections_json(state, + crec); + } TALLOC_FREE(timestr); TALLOC_FREE(tmp_ctx); diff --git a/source3/utils/status_json.c b/source3/utils/status_json.c index 59d1a6bf85e..c460ce78c6a 100644 --- a/source3/utils/status_json.c +++ b/source3/utils/status_json.c @@ -19,6 +19,7 @@ #include "includes.h" #include "smbprofile.h" +#include "conn_tdb.h" #include "status_json.h" #include "../libcli/security/security.h" #include "status.h" @@ -67,3 +68,60 @@ int add_section_to_json(struct traverse_state *state, return result; } + +int traverse_connections_json(struct traverse_state *state, + const struct connections_data *crec) +{ + struct json_object sub_json; + struct json_object connections_json; + int result = 0; + char *tcon_id_str = NULL; + + TALLOC_CTX *tmp_ctx = talloc_stackframe(); + if (tmp_ctx == NULL) { + return -1; + } + + sub_json = json_new_object(); + if (json_is_invalid(&sub_json)) { + goto failure; + } + connections_json = json_get_object(&state->root_json, "tcons"); + if (json_is_invalid(&connections_json)) { + goto failure; + } + + result = json_add_string(&sub_json, "service", crec->servicename); + if (result < 0) { + goto failure; + } + tcon_id_str = talloc_asprintf(tmp_ctx, "%u", crec->cnum); + if (tcon_id_str == NULL) { + goto failure; + } + result = json_add_string(&sub_json, "tcon_id", tcon_id_str); + if (result < 0) { + goto failure; + } + result = json_add_string(&sub_json, "machine", crec->machine); + if (result < 0) { + goto failure; + } + + result = json_add_object(&connections_json, tcon_id_str, &sub_json); + if (result < 0) { + goto failure; + } + + result = json_update_object(&state->root_json, "tcons", &connections_json); + if (result < 0) { + goto failure; + } + + TALLOC_FREE(tmp_ctx); + return 0; +failure: + json_free(&sub_json); + TALLOC_FREE(tmp_ctx); + return -1; +} diff --git a/source3/utils/status_json.h b/source3/utils/status_json.h index b1c05c83597..d21f3e6da47 100644 --- a/source3/utils/status_json.h +++ b/source3/utils/status_json.h @@ -27,4 +27,7 @@ int add_section_to_json(struct traverse_state *state, int add_general_information_to_json(struct traverse_state *state); +int traverse_connections_json(struct traverse_state *state, + const struct connections_data *crec); + #endif diff --git a/source3/utils/status_json_dummy.c b/source3/utils/status_json_dummy.c index bedd014b436..ec341f42c4a 100644 --- a/source3/utils/status_json_dummy.c +++ b/source3/utils/status_json_dummy.c @@ -21,6 +21,7 @@ #include "smbprofile.h" #include "../libcli/security/security.h" #include "librpc/gen_ndr/open_files.h" +#include "conn_tdb.h" #include "status_json.h" int add_section_to_json(struct traverse_state *state, @@ -33,3 +34,9 @@ int add_general_information_to_json(struct traverse_state *state) { return 0; } + +int traverse_connections_json(struct traverse_state *state, + const struct connections_data *crec) +{ + return 0; +}