1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-05 20:58:40 +03:00

smbstatus: add a sessions dictionary

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>
This commit is contained in:
Jule Anger 2022-03-24 14:09:35 +01:00
parent 1abae1c255
commit 836fd468c0
4 changed files with 127 additions and 14 deletions

View 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);

View 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;
}

View 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

View 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;
}