]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
smbstatus: add a sessions dictionary
authorJule Anger <janger@samba.org>
Thu, 24 Mar 2022 13:09:35 +0000 (14:09 +0100)
committerJule Anger <janger@samba.org>
Mon, 8 Aug 2022 12:56:28 +0000 (12:56 +0000)
Adds an empty json dictionary under the key "sessions" and adds foreach
session a dictionary with information to the session dictionary. Uses the
session_id as key.
uid_str and gid_str are needed because both receive their own JSON field.

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 0dfb8e9b25c36b2092ee8fe1f8ac1f89b05230d7..cf5f0a71a0bd48016d1b8bd410897505c4c74e76 100644 (file)
@@ -584,11 +584,14 @@ static int traverse_sessionid_stdout(struct traverse_state *state,
 
 static int prepare_sessionid(struct traverse_state *state)
 {
-       /* always print header line */
-       d_printf("\nSamba version %s\n",samba_version_string());
-       d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
-       d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
-
+       if (!state->json_output) {
+               /* always print header line */
+               d_printf("\nSamba version %s\n",samba_version_string());
+               d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
+               d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
+       } else {
+               add_section_to_json(state, "sessions");
+       }
        return 0;
 
 }
@@ -597,6 +600,8 @@ static int traverse_sessionid(const char *key, struct sessionid *session,
                              void *private_data)
 {
        fstring uid_gid_str;
+       fstring uid_str;
+       fstring gid_str;
        struct server_id_buf tmp;
        char *machine_hostname = NULL;
        int result = 0;
@@ -621,6 +626,8 @@ static int traverse_sessionid(const char *key, struct sessionid *session,
        Ucrit_addPid(session->pid);
 
        if (numeric_only) {
+               fstr_sprintf(gid_str, "%u", (unsigned int)session->gid);
+               fstr_sprintf(uid_str, "%u", (unsigned int)session->uid);
                fstr_sprintf(uid_gid_str, "%-12u %-12u",
                             (unsigned int)session->uid,
                             (unsigned int)session->gid);
@@ -630,6 +637,8 @@ static int traverse_sessionid(const char *key, struct sessionid *session,
                         * The session is not fully authenticated yet.
                         */
                        fstrcpy(uid_gid_str, "(auth in progress)");
+                       fstrcpy(gid_str, "(auth in progress)");
+                       fstrcpy(uid_str, "(auth in progress)");
                } else {
                        /*
                         * In theory it should not happen that one of
@@ -654,6 +663,8 @@ static int traverse_sessionid(const char *key, struct sessionid *session,
                                        return -1;
                                }
                        }
+                       fstr_sprintf(gid_str, "%s", gid_name);
+                       fstr_sprintf(uid_str, "%s", uid_name);
                        fstr_sprintf(uid_gid_str, "%-12s %-12s",
                                     uid_name, gid_name);
                }
@@ -722,15 +733,23 @@ static int traverse_sessionid(const char *key, struct sessionid *session,
        }
 
 
-       traverse_sessionid_stdout(state,
-                                 server_id_str_buf(session->pid, &tmp),
-                                 uid_gid_str,
-                                 machine_hostname,
-                                 session_dialect_str(session->connection_dialect),
-                                 encryption,
-                                 encryption_degree,
-                                 signing,
-                                 signing_degree);
+       if (!state->json_output) {
+               traverse_sessionid_stdout(state,
+                        server_id_str_buf(session->pid, &tmp),
+                        uid_gid_str,
+                        machine_hostname,
+                        session_dialect_str(session->connection_dialect),
+                        encryption,
+                        encryption_degree,
+                        signing,
+                        signing_degree);
+       } else {
+               result = traverse_sessionid_json(state,
+                                                session,
+                                                uid_str,
+                                                gid_str,
+                                                session_dialect_str(session->connection_dialect));
+       }
 
        TALLOC_FREE(machine_hostname);
        TALLOC_FREE(tmp_ctx);
index 0eadd1a1867a929251d2722b4f24ecf791989c60..5800bfb5489833e332460093d76835220e7dc97c 100644 (file)
@@ -21,6 +21,7 @@
 #include "smbprofile.h"
 #include "lib/util/time_basic.h"
 #include "conn_tdb.h"
+#include "session.h"
 #include "status_json.h"
 #include "../libcli/security/security.h"
 #include "status.h"
@@ -262,3 +263,81 @@ failure:
        TALLOC_FREE(tmp_ctx);
        return -1;
 }
+
+int traverse_sessionid_json(struct traverse_state *state,
+                           struct sessionid *session,
+                           char *uid_str,
+                           char *gid_str,
+                           const char *connection_dialect)
+{
+       struct json_object sub_json;
+       struct json_object session_json;
+       int result = 0;
+       char *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;
+       }
+
+       session_json = json_get_object(&state->root_json, "sessions");
+       if (json_is_invalid(&session_json)) {
+               goto failure;
+       }
+
+       id_str = talloc_asprintf(tmp_ctx, "%u", session->id_num);
+       result = json_add_string(&sub_json, "session_id", id_str);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_int(&sub_json, "uid", session->uid);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_int(&sub_json, "gid", session->gid);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_string(&sub_json, "username", uid_str);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_string(&sub_json, "groupname", gid_str);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_string(&sub_json, "remote_machine", session->remote_machine);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_string(&sub_json, "hostname", session->hostname);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_string(&sub_json, "session_dialect", connection_dialect);
+       if (result < 0) {
+               goto failure;
+       }
+
+       result = json_add_object(&session_json, id_str, &sub_json);
+       if (result < 0) {
+               goto failure;
+       }
+
+       result = json_update_object(&state->root_json, "sessions", &session_json);
+       if (result < 0) {
+               goto failure;
+       }
+
+       TALLOC_FREE(tmp_ctx);
+       return 0;
+failure:
+       json_free(&sub_json);
+       TALLOC_FREE(tmp_ctx);
+       return -1;
+}
index 758fc8a6b9802e894a396832e8d11549b89ad199..c2760d04ba6df2b701905ffbc029b4d7982a62b3 100644 (file)
@@ -34,4 +34,10 @@ int traverse_connections_json(struct traverse_state *state,
                              const char *signing_cipher,
                              enum crypto_degree signing_degree);
 
+int traverse_sessionid_json(struct traverse_state *state,
+                           struct sessionid *session,
+                           char *uid_str,
+                           char *gid_str,
+                           const char *connection_dialect);
+
 #endif
index 471d61cdffa1514e657e71a178e8eb2a886b9fb7..6ea7d09f5eae2b59aedf30387ca60e0abced142a 100644 (file)
@@ -44,3 +44,12 @@ int traverse_connections_json(struct traverse_state *state,
 {
        return 0;
 }
+
+int traverse_sessionid_json(struct traverse_state *state,
+                           struct sessionid *session,
+                           char *uid_str,
+                           char *gid_str,
+                           const char *connection_dialect)
+{
+       return 0;
+}