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"
24 #include "status_json.h"
25 #include "../libcli/security/security.h"
27 #include "lib/util/server_id.h"
30 #include "audit_logging.h" /* various JSON helpers */
31 #include "auth/common_auth.h"
33 int add_general_information_to_json(struct traverse_state
*state
)
37 result
= json_add_timestamp(&state
->root_json
);
42 result
= json_add_string(&state
->root_json
, "version", samba_version_string());
47 result
= json_add_string(&state
->root_json
, "smb_conf", get_dyn_CONFIGFILE());
55 static int add_server_id_to_json(struct json_object
*parent_json
,
56 const struct server_id server_id
)
58 struct json_object sub_json
;
60 char *task_id_str
= NULL
;
62 char *unique_id_str
= NULL
;
65 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
66 if (tmp_ctx
== NULL
) {
70 sub_json
= json_new_object();
71 if (json_is_invalid(&sub_json
)) {
75 pid_str
= talloc_asprintf(tmp_ctx
, "%lu", server_id
.pid
);
76 result
= json_add_string(&sub_json
, "pid", pid_str
);
80 task_id_str
= talloc_asprintf(tmp_ctx
, "%u", server_id
.task_id
);
81 result
= json_add_string(&sub_json
, "task_id", task_id_str
);
85 vnn_str
= talloc_asprintf(tmp_ctx
, "%u", server_id
.vnn
);
86 result
= json_add_string(&sub_json
, "vnn", vnn_str
);
90 unique_id_str
= talloc_asprintf(tmp_ctx
, "%lu", server_id
.unique_id
);
91 result
= json_add_string(&sub_json
, "unique_id", unique_id_str
);
96 result
= json_add_object(parent_json
, "server_id", &sub_json
);
101 json_free(&sub_json
);
102 TALLOC_FREE(tmp_ctx
);
105 json_free(&sub_json
);
106 TALLOC_FREE(tmp_ctx
);
110 int add_section_to_json(struct traverse_state
*state
,
113 struct json_object empty_json
;
116 empty_json
= json_new_object();
117 if (json_is_invalid(&empty_json
)) {
121 result
= json_add_object(&state
->root_json
, key
, &empty_json
);
129 static int add_crypto_to_json(struct json_object
*parent_json
,
132 enum crypto_degree degree
)
134 struct json_object sub_json
;
135 const char *degree_str
;
138 if (degree
== CRYPTO_DEGREE_NONE
) {
140 } else if (degree
== CRYPTO_DEGREE_PARTIAL
) {
141 degree_str
= "partial";
146 sub_json
= json_new_object();
147 if (json_is_invalid(&sub_json
)) {
151 result
= json_add_string(&sub_json
, "cipher", cipher
);
155 result
= json_add_string(&sub_json
, "degree", degree_str
);
159 result
= json_add_object(parent_json
, key
, &sub_json
);
166 json_free(&sub_json
);
170 int traverse_connections_json(struct traverse_state
*state
,
171 const struct connections_data
*crec
,
172 const char *encryption_cipher
,
173 enum crypto_degree encryption_degree
,
174 const char *signing_cipher
,
175 enum crypto_degree signing_degree
)
177 struct json_object sub_json
;
178 struct json_object connections_json
;
180 struct timeval_buf tv_buf
;
183 char *sess_id_str
= NULL
;
184 char *tcon_id_str
= NULL
;
186 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
187 if (tmp_ctx
== NULL
) {
191 sub_json
= json_new_object();
192 if (json_is_invalid(&sub_json
)) {
195 connections_json
= json_get_object(&state
->root_json
, "tcons");
196 if (json_is_invalid(&connections_json
)) {
200 result
= json_add_string(&sub_json
, "service", crec
->servicename
);
204 result
= add_server_id_to_json(&sub_json
, crec
->pid
);
208 tcon_id_str
= talloc_asprintf(tmp_ctx
, "%u", crec
->cnum
);
209 if (tcon_id_str
== NULL
) {
212 result
= json_add_string(&sub_json
, "tcon_id", tcon_id_str
);
216 sess_id_str
= talloc_asprintf(tmp_ctx
, "%u", crec
->sess_id
);
217 if (sess_id_str
== NULL
) {
220 result
= json_add_string(&sub_json
, "session_id", sess_id_str
);
224 result
= json_add_string(&sub_json
, "machine", crec
->machine
);
228 nttime_to_timeval(&tv
, crec
->start
);
229 time
= timeval_str_buf(&tv
, true, true, &tv_buf
);
233 result
= json_add_string(&sub_json
, "connected_at", time
);
237 result
= add_crypto_to_json(&sub_json
, "encryption",
238 encryption_cipher
, encryption_degree
);
242 result
= add_crypto_to_json(&sub_json
, "signing",
243 signing_cipher
, signing_degree
);
248 result
= json_add_object(&connections_json
, tcon_id_str
, &sub_json
);
253 result
= json_update_object(&state
->root_json
, "tcons", &connections_json
);
258 TALLOC_FREE(tmp_ctx
);
261 json_free(&sub_json
);
262 TALLOC_FREE(tmp_ctx
);