]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
smbstatus: add a connections dictionary
authorJule Anger <janger@samba.org>
Thu, 31 Mar 2022 08:20:20 +0000 (10:20 +0200)
committerJule Anger <janger@samba.org>
Mon, 8 Aug 2022 12:56:28 +0000 (12:56 +0000)
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 <janger@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/utils/status.c
source3/utils/status_json.c
source3/utils/status_json.h
source3/utils/status_json_dummy.c

index 28e673db09a03750bfd032a4325376253bd58ab0..81b4a11ae5c050955df7fdf6512db8fc9d841eee 100644 (file)
@@ -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);
index 59d1a6bf85e34d8a748cad434e48f0840a411ad6..c460ce78c6a2730305ecba6ea045b341aea54ad9 100644 (file)
@@ -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;
+}
index b1c05c835976aa6ae5d72d8f59fdffc70cf498c9..d21f3e6da477d4f8a1261bc598fd86b9407b1472 100644 (file)
@@ -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
index bedd014b436053fdc12dc8b15531dd59394838f6..ec341f42c4a17d0266834567835ae3e6cc17d611 100644 (file)
@@ -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;
+}