2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Jule Anger 2022
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "smbprofile.h"
22 #include "lib/util/time_basic.h"
25 #include "librpc/gen_ndr/open_files.h"
26 #include "status_json.h"
27 #include "../libcli/security/security.h"
29 #include "lib/util/server_id.h"
32 #include "audit_logging.h" /* various JSON helpers */
33 #include "auth/common_auth.h"
35 int add_general_information_to_json(struct traverse_state
*state
)
39 result
= json_add_timestamp(&state
->root_json
);
44 result
= json_add_string(&state
->root_json
, "version", samba_version_string());
49 result
= json_add_string(&state
->root_json
, "smb_conf", get_dyn_CONFIGFILE());
57 static int add_server_id_to_json(struct json_object
*parent_json
,
58 const struct server_id server_id
)
60 struct json_object sub_json
;
62 char *task_id_str
= NULL
;
64 char *unique_id_str
= NULL
;
67 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
68 if (tmp_ctx
== NULL
) {
72 sub_json
= json_new_object();
73 if (json_is_invalid(&sub_json
)) {
77 pid_str
= talloc_asprintf(tmp_ctx
, "%lu", server_id
.pid
);
78 result
= json_add_string(&sub_json
, "pid", pid_str
);
82 task_id_str
= talloc_asprintf(tmp_ctx
, "%u", server_id
.task_id
);
83 result
= json_add_string(&sub_json
, "task_id", task_id_str
);
87 vnn_str
= talloc_asprintf(tmp_ctx
, "%u", server_id
.vnn
);
88 result
= json_add_string(&sub_json
, "vnn", vnn_str
);
92 unique_id_str
= talloc_asprintf(tmp_ctx
, "%lu", server_id
.unique_id
);
93 result
= json_add_string(&sub_json
, "unique_id", unique_id_str
);
98 result
= json_add_object(parent_json
, "server_id", &sub_json
);
103 TALLOC_FREE(tmp_ctx
);
106 json_free(&sub_json
);
107 TALLOC_FREE(tmp_ctx
);
111 int add_section_to_json(struct traverse_state
*state
,
114 struct json_object empty_json
;
117 empty_json
= json_new_object();
118 if (json_is_invalid(&empty_json
)) {
122 result
= json_add_object(&state
->root_json
, key
, &empty_json
);
130 static int add_crypto_to_json(struct json_object
*parent_json
,
133 enum crypto_degree degree
)
135 struct json_object sub_json
;
136 const char *degree_str
;
139 if (degree
== CRYPTO_DEGREE_NONE
) {
141 } else if (degree
== CRYPTO_DEGREE_PARTIAL
) {
142 degree_str
= "partial";
147 sub_json
= json_new_object();
148 if (json_is_invalid(&sub_json
)) {
152 result
= json_add_string(&sub_json
, "cipher", cipher
);
156 result
= json_add_string(&sub_json
, "degree", degree_str
);
160 result
= json_add_object(parent_json
, key
, &sub_json
);
167 json_free(&sub_json
);
171 int traverse_connections_json(struct traverse_state
*state
,
172 const struct connections_data
*crec
,
173 const char *encryption_cipher
,
174 enum crypto_degree encryption_degree
,
175 const char *signing_cipher
,
176 enum crypto_degree signing_degree
)
178 struct json_object sub_json
;
179 struct json_object connections_json
;
181 struct timeval_buf tv_buf
;
184 char *sess_id_str
= NULL
;
185 char *tcon_id_str
= NULL
;
187 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
188 if (tmp_ctx
== NULL
) {
192 sub_json
= json_new_object();
193 if (json_is_invalid(&sub_json
)) {
196 connections_json
= json_get_object(&state
->root_json
, "tcons");
197 if (json_is_invalid(&connections_json
)) {
201 result
= json_add_string(&sub_json
, "service", crec
->servicename
);
205 result
= add_server_id_to_json(&sub_json
, crec
->pid
);
209 tcon_id_str
= talloc_asprintf(tmp_ctx
, "%u", crec
->cnum
);
210 if (tcon_id_str
== NULL
) {
213 result
= json_add_string(&sub_json
, "tcon_id", tcon_id_str
);
217 sess_id_str
= talloc_asprintf(tmp_ctx
, "%u", crec
->sess_id
);
218 if (sess_id_str
== NULL
) {
221 result
= json_add_string(&sub_json
, "session_id", sess_id_str
);
225 result
= json_add_string(&sub_json
, "machine", crec
->machine
);
229 nttime_to_timeval(&tv
, crec
->start
);
230 time
= timeval_str_buf(&tv
, true, true, &tv_buf
);
234 result
= json_add_string(&sub_json
, "connected_at", time
);
238 result
= add_crypto_to_json(&sub_json
, "encryption",
239 encryption_cipher
, encryption_degree
);
243 result
= add_crypto_to_json(&sub_json
, "signing",
244 signing_cipher
, signing_degree
);
249 result
= json_add_object(&connections_json
, tcon_id_str
, &sub_json
);
254 result
= json_update_object(&state
->root_json
, "tcons", &connections_json
);
259 TALLOC_FREE(tmp_ctx
);
262 json_free(&sub_json
);
263 TALLOC_FREE(tmp_ctx
);
267 int traverse_sessionid_json(struct traverse_state
*state
,
268 struct sessionid
*session
,
271 const char *encryption_cipher
,
272 enum crypto_degree encryption_degree
,
273 const char *signing_cipher
,
274 enum crypto_degree signing_degree
,
275 const char *connection_dialect
)
277 struct json_object sub_json
;
278 struct json_object session_json
;
282 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
283 if (tmp_ctx
== NULL
) {
287 sub_json
= json_new_object();
288 if (json_is_invalid(&sub_json
)) {
292 session_json
= json_get_object(&state
->root_json
, "sessions");
293 if (json_is_invalid(&session_json
)) {
297 id_str
= talloc_asprintf(tmp_ctx
, "%u", session
->id_num
);
298 result
= json_add_string(&sub_json
, "session_id", id_str
);
302 result
= add_server_id_to_json(&sub_json
, session
->pid
);
306 result
= json_add_int(&sub_json
, "uid", session
->uid
);
310 result
= json_add_int(&sub_json
, "gid", session
->gid
);
314 result
= json_add_string(&sub_json
, "username", uid_str
);
318 result
= json_add_string(&sub_json
, "groupname", gid_str
);
322 result
= json_add_string(&sub_json
, "remote_machine", session
->remote_machine
);
326 result
= json_add_string(&sub_json
, "hostname", session
->hostname
);
330 result
= json_add_string(&sub_json
, "session_dialect", connection_dialect
);
334 result
= add_crypto_to_json(&sub_json
, "encryption",
335 encryption_cipher
, encryption_degree
);
339 result
= add_crypto_to_json(&sub_json
, "signing",
340 signing_cipher
, signing_degree
);
345 result
= json_add_object(&session_json
, id_str
, &sub_json
);
350 result
= json_update_object(&state
->root_json
, "sessions", &session_json
);
355 TALLOC_FREE(tmp_ctx
);
358 json_free(&sub_json
);
359 TALLOC_FREE(tmp_ctx
);
363 int print_share_mode_json(struct traverse_state
*state
,
364 const struct share_mode_data
*d
,
365 const char *filename
)
367 struct json_object locks_json
;
368 struct json_object file_json
;
372 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
373 if (tmp_ctx
== NULL
) {
377 if (d
->servicepath
[strlen(d
->servicepath
)-1] == '/') {
378 key
= talloc_asprintf(tmp_ctx
, "%s%s", d
->servicepath
, filename
);
380 key
= talloc_asprintf(tmp_ctx
, "%s/%s", d
->servicepath
, filename
);
383 locks_json
= json_get_object(&state
->root_json
, "open_files");
384 if (json_is_invalid(&locks_json
)) {
387 file_json
= json_get_object(&locks_json
, key
);
388 if (json_is_invalid(&file_json
)) {
392 result
= json_add_string(&file_json
, "service_path", d
->servicepath
);
396 result
= json_add_string(&file_json
, "filename", filename
);
400 result
= json_add_int(&file_json
, "num_pending_deletes", d
->num_delete_tokens
);
405 result
= json_update_object(&locks_json
, key
, &file_json
);
409 result
= json_update_object(&state
->root_json
, "open_files", &locks_json
);
414 TALLOC_FREE(tmp_ctx
);
417 json_free(&file_json
);
418 json_free(&locks_json
);
419 TALLOC_FREE(tmp_ctx
);