gp: Fix startup scripts add not always set runonce
[Samba.git] / source3 / utils / status.c
blobe311f067c9c57514eb564b494b7ce45618f23e75
1 /*
2 Unix SMB/CIFS implementation.
3 status reporting
4 Copyright (C) Andrew Tridgell 1994-1998
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/>.
19 Revision History:
21 12 aug 96: Erik.Devriendt@te6.siemens.be
22 added support for shared memory implementation of share mode locking
24 21-Jul-1998: rsharpe@ns.aus.com (Richard Sharpe)
25 Added -L (locks only) -S (shares only) flags and code
30 * This program reports current SMB connections
33 #include "includes.h"
34 #include "lib/util/server_id.h"
35 #include "smbd/globals.h"
36 #include "system/filesys.h"
37 #include "lib/cmdline/cmdline.h"
38 #include "dbwrap/dbwrap.h"
39 #include "dbwrap/dbwrap_open.h"
40 #include "../libcli/security/security.h"
41 #include "session.h"
42 #include "locking/share_mode_lock.h"
43 #include "locking/proto.h"
44 #include "messages.h"
45 #include "librpc/gen_ndr/open_files.h"
46 #include "smbd/smbd.h"
47 #include "librpc/gen_ndr/notify.h"
48 #include "conn_tdb.h"
49 #include "serverid.h"
50 #include "status_profile.h"
51 #include "status.h"
52 #include "status_json.h"
53 #include "smbd/notifyd/notifyd_db.h"
54 #include "cmdline_contexts.h"
55 #include "locking/leases_db.h"
56 #include "lib/util/string_wrappers.h"
58 #ifdef HAVE_JANSSON
59 #include <jansson.h>
60 #include "audit_logging.h" /* various JSON helpers */
61 #include "auth/common_auth.h"
62 #endif /* HAVE_JANSSON */
64 #define SMB_MAXPIDS 2048
65 static uid_t Ucrit_uid = 0; /* added by OH */
66 static struct server_id Ucrit_pid[SMB_MAXPIDS]; /* Ugly !!! */ /* added by OH */
67 static int Ucrit_MaxPid=0; /* added by OH */
68 static unsigned int Ucrit_IsActive = 0; /* added by OH */
70 static bool verbose, brief;
71 static bool shares_only; /* Added by RJS */
72 static bool locks_only; /* Added by RJS */
73 static bool processes_only;
74 static bool show_brl;
75 static bool numeric_only;
76 static bool do_checks = true;
78 const char *username = NULL;
80 /* added by OH */
81 static void Ucrit_addUid(uid_t uid)
83 Ucrit_uid = uid;
84 Ucrit_IsActive = 1;
87 static unsigned int Ucrit_checkUid(uid_t uid)
89 if ( !Ucrit_IsActive )
90 return 1;
92 if ( uid == Ucrit_uid )
93 return 1;
95 return 0;
98 static unsigned int Ucrit_checkPid(struct server_id pid)
100 int i;
102 if ( !Ucrit_IsActive )
103 return 1;
105 for (i=0;i<Ucrit_MaxPid;i++) {
106 if (server_id_equal(&pid, &Ucrit_pid[i])) {
107 return 1;
111 return 0;
114 static bool Ucrit_addPid( struct server_id pid )
116 if ( !Ucrit_IsActive )
117 return True;
119 if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
120 fprintf(stderr, "ERROR: More than %d pids for user %s!\n",
121 SMB_MAXPIDS, uidtoname(Ucrit_uid));
123 return False;
126 Ucrit_pid[Ucrit_MaxPid++] = pid;
128 return True;
131 static int print_share_mode_stdout(struct traverse_state *state,
132 const char *pid,
133 const char *user_name,
134 const char *denymode,
135 int access_mask,
136 const char *rw,
137 const char *oplock,
138 const char *servicepath,
139 const char *filename,
140 const char *timestr)
142 if (state->first) {
143 d_printf("\nLocked files:\n");
144 d_printf("Pid User(ID) DenyMode Access R/W Oplock SharePath Name Time\n");
145 d_printf("--------------------------------------------------------------------------------------------------\n");
147 state->first = false;
150 d_printf("%-11s %-9s %-10s 0x%-8x %-10s %-14s %s %s %s",
151 pid, user_name, denymode, access_mask, rw, oplock,
152 servicepath, filename, timestr);
153 return 0;
156 static int prepare_share_mode(struct traverse_state *state)
158 if (!state->json_output) {
159 /* only print header line if there are open files */
160 state->first = true;
161 } else {
162 add_section_to_json(state, "open_files");
164 return 0;
167 static int print_share_mode(struct file_id fid,
168 const struct share_mode_data *d,
169 const struct share_mode_entry *e,
170 void *private_data)
172 const char *denymode = NULL;
173 uint denymode_int;
174 const char *oplock = NULL;
175 const char *pid = NULL;
176 const char *rw = NULL;
177 const char *filename = NULL;
178 const char *timestr = NULL;
179 const char *user_str = NULL;
180 uint32_t lstate;
181 struct traverse_state *state = (struct traverse_state *)private_data;
183 TALLOC_CTX *tmp_ctx = talloc_stackframe();
184 if (tmp_ctx == NULL) {
185 return -1;
188 if (do_checks && !is_valid_share_mode_entry(e)) {
189 TALLOC_FREE(tmp_ctx);
190 return 0;
193 if (do_checks && !serverid_exists(&e->pid)) {
194 /* the process for this entry does not exist any more */
195 TALLOC_FREE(tmp_ctx);
196 return 0;
199 if (Ucrit_checkPid(e->pid)) {
200 struct server_id_buf tmp;
201 pid = server_id_str_buf(e->pid, &tmp);
202 if (state->resolve_uids) {
203 user_str = talloc_asprintf(tmp_ctx, "%s", uidtoname(e->uid));
204 } else {
205 user_str = talloc_asprintf(tmp_ctx, "%u", (unsigned int)e->uid);
207 if (user_str == NULL) {
208 TALLOC_FREE(tmp_ctx);
209 return -1;
212 denymode_int = map_share_mode_to_deny_mode(e->share_access,
213 e->private_options);
214 switch (denymode_int) {
215 case DENY_NONE:
216 denymode = "DENY_NONE";
217 break;
218 case DENY_ALL:
219 denymode = "DENY_ALL";
220 break;
221 case DENY_DOS:
222 denymode = "DENY_DOS";
223 break;
224 case DENY_READ:
225 denymode = "DENY_READ";
226 break;
227 case DENY_WRITE:
228 denymode = "DENY_WRITE";
229 break;
230 case DENY_FCB:
231 denymode = "DENY_FCB";
232 break;
233 default: {
234 denymode = talloc_asprintf(tmp_ctx,
235 "UNKNOWN(0x%08x)",
236 denymode_int);
237 if (denymode == NULL) {
238 TALLOC_FREE(tmp_ctx);
239 return -1;
241 fprintf(stderr,
242 "unknown-please report ! "
243 "e->share_access = 0x%x, "
244 "e->private_options = 0x%x\n",
245 (unsigned int)e->share_access,
246 (unsigned int)e->private_options);
247 break;
250 filename = talloc_asprintf(tmp_ctx,
251 "%s%s",
252 d->base_name,
253 (d->stream_name != NULL) ? d->stream_name : "");
254 if (filename == NULL) {
255 TALLOC_FREE(tmp_ctx);
256 return -1;
258 if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
259 (FILE_READ_DATA|FILE_WRITE_DATA)) {
260 rw = "RDWR";
261 } else if (e->access_mask & FILE_WRITE_DATA) {
262 rw = "WRONLY";
263 } else {
264 rw = "RDONLY";
267 if (e->op_type & BATCH_OPLOCK) {
268 oplock = "BATCH";
269 } else if (e->op_type & EXCLUSIVE_OPLOCK) {
270 oplock = "EXCLUSIVE";
271 } else if (e->op_type & LEVEL_II_OPLOCK) {
272 oplock = "LEVEL_II";
273 } else if (e->op_type == LEASE_OPLOCK) {
274 NTSTATUS status;
276 status = leases_db_get(
277 &e->client_guid,
278 &e->lease_key,
279 &d->id,
280 &lstate, /* current_state */
281 NULL, /* breaking */
282 NULL, /* breaking_to_requested */
283 NULL, /* breaking_to_required */
284 NULL, /* lease_version */
285 NULL); /* epoch */
287 if (NT_STATUS_IS_OK(status)) {
288 oplock = talloc_asprintf(tmp_ctx, "LEASE(%s%s%s)%s%s%s",
289 (lstate & SMB2_LEASE_READ)?"R":"",
290 (lstate & SMB2_LEASE_WRITE)?"W":"",
291 (lstate & SMB2_LEASE_HANDLE)?"H":"",
292 (lstate & SMB2_LEASE_READ)?"":" ",
293 (lstate & SMB2_LEASE_WRITE)?"":" ",
294 (lstate & SMB2_LEASE_HANDLE)?"":" ");
295 } else {
296 oplock = "LEASE STATE UNKNOWN";
298 } else {
299 oplock = "NONE";
302 timestr = time_to_asc((time_t)e->time.tv_sec);
304 if (!state->json_output) {
305 print_share_mode_stdout(state,
306 pid,
307 user_str,
308 denymode,
309 (unsigned int)e->access_mask,
311 oplock,
312 d->servicepath,
313 filename,
314 timestr);
315 } else {
316 print_share_mode_json(state,
319 fid,
320 user_str,
321 oplock,
322 lstate,
323 filename);
326 TALLOC_FREE(tmp_ctx);
327 return 0;
330 static void print_brl_stdout(struct traverse_state *state,
331 char *pid,
332 char *id,
333 const char *desc,
334 intmax_t start,
335 intmax_t size,
336 const char *sharepath,
337 char *fname)
339 if (state->first) {
340 d_printf("Byte range locks:\n");
341 d_printf("Pid dev:inode R/W start size SharePath Name\n");
342 d_printf("--------------------------------------------------------------------------------\n");
344 state->first = false;
346 d_printf("%-10s %-15s %-4s %-9jd %-9jd %-24s %-24s\n",
347 pid, id, desc, start, size, sharepath, fname);
350 static int prepare_brl(struct traverse_state *state)
352 if (!state->json_output) {
353 /* only print header line if there are locked files */
354 state->first = true;
355 } else {
356 add_section_to_json(state, "byte_range_locks");
358 return 0;
361 static void print_brl(struct file_id id,
362 struct server_id pid,
363 enum brl_type lock_type,
364 enum brl_flavour lock_flav,
365 br_off start,
366 br_off size,
367 void *private_data)
369 unsigned int i;
370 static const struct {
371 enum brl_type lock_type;
372 const char *desc;
373 } lock_types[] = {
374 { READ_LOCK, "R" },
375 { WRITE_LOCK, "W" },
376 { UNLOCK_LOCK, "U" }
378 const char *desc="X";
379 const char *sharepath = "";
380 char *fname = NULL;
381 struct share_mode_lock *share_mode;
382 struct server_id_buf tmp;
383 struct file_id_buf ftmp;
384 struct traverse_state *state = (struct traverse_state *)private_data;
386 share_mode = fetch_share_mode_unlocked(NULL, id);
387 if (share_mode) {
388 fname = share_mode_filename(NULL, share_mode);
389 sharepath = share_mode_servicepath(share_mode);
390 } else {
391 fname = talloc_strdup(NULL, "");
392 if (fname == NULL) {
393 return;
397 for (i=0;i<ARRAY_SIZE(lock_types);i++) {
398 if (lock_type == lock_types[i].lock_type) {
399 desc = lock_types[i].desc;
403 if (!state->json_output) {
404 print_brl_stdout(state,
405 server_id_str_buf(pid, &tmp),
406 file_id_str_buf(id, &ftmp),
407 desc,
408 (intmax_t)start,
409 (intmax_t)size,
410 sharepath,
411 fname);
412 } else {
413 print_brl_json(state,
414 pid,
416 desc,
417 lock_flav,
418 (intmax_t)start,
419 (intmax_t)size,
420 sharepath,
421 fname);
425 TALLOC_FREE(fname);
426 TALLOC_FREE(share_mode);
429 static const char *session_dialect_str(uint16_t dialect)
431 static fstring unknown_dialect;
433 switch(dialect){
434 case SMB2_DIALECT_REVISION_000:
435 return "NT1";
436 case SMB2_DIALECT_REVISION_202:
437 return "SMB2_02";
438 case SMB2_DIALECT_REVISION_210:
439 return "SMB2_10";
440 case SMB2_DIALECT_REVISION_222:
441 return "SMB2_22";
442 case SMB2_DIALECT_REVISION_224:
443 return "SMB2_24";
444 case SMB3_DIALECT_REVISION_300:
445 return "SMB3_00";
446 case SMB3_DIALECT_REVISION_302:
447 return "SMB3_02";
448 case SMB3_DIALECT_REVISION_310:
449 return "SMB3_10";
450 case SMB3_DIALECT_REVISION_311:
451 return "SMB3_11";
454 fstr_sprintf(unknown_dialect, "Unknown (0x%04x)", dialect);
455 return unknown_dialect;
458 static int traverse_connections_stdout(struct traverse_state *state,
459 const char *servicename,
460 char *server_id,
461 const char *machine,
462 const char *timestr,
463 const char *encryption,
464 const char *signing)
466 d_printf("%-12s %-7s %-13s %-32s %-12s %-12s\n",
467 servicename, server_id, machine, timestr, encryption, signing);
469 return 0;
472 static int prepare_connections(struct traverse_state *state)
474 if (!state->json_output) {
475 /* always print header line */
476 d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing");
477 d_printf("---------------------------------------------------------------------------------------------\n");
478 } else {
479 add_section_to_json(state, "tcons");
481 return 0;
484 static int traverse_connections(const struct connections_data *crec,
485 void *private_data)
487 struct server_id_buf tmp;
488 char *timestr = NULL;
489 int result = 0;
490 const char *encryption = "-";
491 enum crypto_degree encryption_degree = CRYPTO_DEGREE_NONE;
492 const char *signing = "-";
493 enum crypto_degree signing_degree = CRYPTO_DEGREE_NONE;
494 struct traverse_state *state = (struct traverse_state *)private_data;
496 TALLOC_CTX *tmp_ctx = talloc_stackframe();
497 if (tmp_ctx == NULL) {
498 return -1;
501 if (crec->cnum == TID_FIELD_INVALID) {
502 TALLOC_FREE(tmp_ctx);
503 return 0;
506 if (do_checks &&
507 (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid))) {
508 TALLOC_FREE(tmp_ctx);
509 return 0;
512 timestr = timestring(tmp_ctx, nt_time_to_unix(crec->start));
513 if (timestr == NULL) {
514 TALLOC_FREE(tmp_ctx);
515 return -1;
518 if (smbXsrv_is_encrypted(crec->encryption_flags)) {
519 switch (crec->cipher) {
520 case SMB_ENCRYPTION_GSSAPI:
521 encryption = "GSSAPI";
522 break;
523 case SMB2_ENCRYPTION_AES128_CCM:
524 encryption = "AES-128-CCM";
525 break;
526 case SMB2_ENCRYPTION_AES128_GCM:
527 encryption = "AES-128-GCM";
528 break;
529 default:
530 encryption = "???";
531 result = -1;
532 break;
534 encryption_degree = CRYPTO_DEGREE_FULL;
537 if (smbXsrv_is_signed(crec->signing_flags)) {
538 switch (crec->signing) {
539 case SMB2_SIGNING_MD5_SMB1:
540 signing = "HMAC-MD5";
541 break;
542 case SMB2_SIGNING_HMAC_SHA256:
543 signing = "HMAC-SHA256";
544 break;
545 case SMB2_SIGNING_AES128_CMAC:
546 signing = "AES-128-CMAC";
547 break;
548 case SMB2_SIGNING_AES128_GMAC:
549 signing = "AES-128-GMAC";
550 break;
551 default:
552 signing = "???";
553 result = -1;
554 break;
556 signing_degree = CRYPTO_DEGREE_FULL;
559 if (!state->json_output) {
560 result = traverse_connections_stdout(state,
561 crec->servicename,
562 server_id_str_buf(crec->pid, &tmp),
563 crec->machine,
564 timestr,
565 encryption,
566 signing);
567 } else {
568 result = traverse_connections_json(state,
569 crec,
570 encryption,
571 encryption_degree,
572 signing,
573 signing_degree);
576 TALLOC_FREE(timestr);
577 TALLOC_FREE(tmp_ctx);
579 return result;
582 static int traverse_sessionid_stdout(struct traverse_state *state,
583 char *server_id,
584 char *uid_gid_str,
585 char *machine_hostname,
586 const char *dialect,
587 const char *encryption_cipher,
588 enum crypto_degree encryption_degree,
589 const char *signing_cipher,
590 enum crypto_degree signing_degree)
592 fstring encryption;
593 fstring signing;
595 if (encryption_degree == CRYPTO_DEGREE_FULL) {
596 fstr_sprintf(encryption, "%s", encryption_cipher);
597 } else if (encryption_degree == CRYPTO_DEGREE_PARTIAL) {
598 fstr_sprintf(encryption, "partial(%s)", encryption_cipher);
599 } else {
600 fstr_sprintf(encryption, "-");
602 if (signing_degree == CRYPTO_DEGREE_FULL) {
603 fstr_sprintf(signing, "%s", signing_cipher);
604 } else if (signing_degree == CRYPTO_DEGREE_PARTIAL) {
605 fstr_sprintf(signing, "partial(%s)", signing_cipher);
606 } else {
607 fstr_sprintf(signing, "-");
610 d_printf("%-7s %-25s %-41s %-17s %-20s %-21s\n",
611 server_id, uid_gid_str, machine_hostname, dialect, encryption,
612 signing);
614 return 0;
617 static int prepare_sessionid(struct traverse_state *state)
619 if (!state->json_output) {
620 /* always print header line */
621 d_printf("\nSamba version %s\n",samba_version_string());
622 d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
623 d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
624 } else {
625 add_section_to_json(state, "sessions");
627 return 0;
631 static int traverse_sessionid(const char *key, struct sessionid *session,
632 void *private_data)
634 fstring uid_gid_str;
635 fstring uid_str;
636 fstring gid_str;
637 struct server_id_buf tmp;
638 char *machine_hostname = NULL;
639 int result = 0;
640 const char *encryption = "-";
641 enum crypto_degree encryption_degree = CRYPTO_DEGREE_NONE;
642 const char *signing = "-";
643 enum crypto_degree signing_degree = CRYPTO_DEGREE_NONE;
644 struct traverse_state *state = (struct traverse_state *)private_data;
646 TALLOC_CTX *tmp_ctx = talloc_stackframe();
647 if (tmp_ctx == NULL) {
648 return -1;
651 if (do_checks &&
652 (!process_exists(session->pid) ||
653 !Ucrit_checkUid(session->uid))) {
654 TALLOC_FREE(tmp_ctx);
655 return 0;
658 Ucrit_addPid(session->pid);
660 if (numeric_only) {
661 fstr_sprintf(gid_str, "%u", (unsigned int)session->gid);
662 fstr_sprintf(uid_str, "%u", (unsigned int)session->uid);
663 fstr_sprintf(uid_gid_str, "%-12u %-12u",
664 (unsigned int)session->uid,
665 (unsigned int)session->gid);
666 } else {
667 if (session->uid == -1 && session->gid == -1) {
669 * The session is not fully authenticated yet.
671 fstrcpy(uid_gid_str, "(auth in progress)");
672 fstrcpy(gid_str, "(auth in progress)");
673 fstrcpy(uid_str, "(auth in progress)");
674 } else {
676 * In theory it should not happen that one of
677 * session->uid and session->gid is valid (ie != -1)
678 * while the other is not (ie = -1), so we a check for
679 * that case that bails out would be reasonable.
681 const char *uid_name = "-1";
682 const char *gid_name = "-1";
684 if (session->uid != -1) {
685 uid_name = uidtoname(session->uid);
686 if (uid_name == NULL) {
687 TALLOC_FREE(tmp_ctx);
688 return -1;
691 if (session->gid != -1) {
692 gid_name = gidtoname(session->gid);
693 if (gid_name == NULL) {
694 TALLOC_FREE(tmp_ctx);
695 return -1;
698 fstr_sprintf(gid_str, "%s", gid_name);
699 fstr_sprintf(uid_str, "%s", uid_name);
700 fstr_sprintf(uid_gid_str, "%-12s %-12s",
701 uid_name, gid_name);
705 machine_hostname = talloc_asprintf(tmp_ctx, "%s (%s)",
706 session->remote_machine,
707 session->hostname);
708 if (machine_hostname == NULL) {
709 TALLOC_FREE(tmp_ctx);
710 return -1;
713 if (smbXsrv_is_encrypted(session->encryption_flags) ||
714 smbXsrv_is_partially_encrypted(session->encryption_flags)) {
715 switch (session->cipher) {
716 case SMB2_ENCRYPTION_AES128_CCM:
717 encryption = "AES-128-CCM";
718 break;
719 case SMB2_ENCRYPTION_AES128_GCM:
720 encryption = "AES-128-GCM";
721 break;
722 case SMB2_ENCRYPTION_AES256_CCM:
723 encryption = "AES-256-CCM";
724 break;
725 case SMB2_ENCRYPTION_AES256_GCM:
726 encryption = "AES-256-GCM";
727 break;
728 default:
729 encryption = "???";
730 result = -1;
731 break;
733 if (smbXsrv_is_encrypted(session->encryption_flags)) {
734 encryption_degree = CRYPTO_DEGREE_FULL;
735 } else if (smbXsrv_is_partially_encrypted(session->encryption_flags)) {
736 encryption_degree = CRYPTO_DEGREE_PARTIAL;
740 if (smbXsrv_is_signed(session->signing_flags) ||
741 smbXsrv_is_partially_signed(session->signing_flags)) {
742 switch (session->signing) {
743 case SMB2_SIGNING_MD5_SMB1:
744 signing = "HMAC-MD5";
745 break;
746 case SMB2_SIGNING_HMAC_SHA256:
747 signing = "HMAC-SHA256";
748 break;
749 case SMB2_SIGNING_AES128_CMAC:
750 signing = "AES-128-CMAC";
751 break;
752 case SMB2_SIGNING_AES128_GMAC:
753 signing = "AES-128-GMAC";
754 break;
755 default:
756 signing = "???";
757 result = -1;
758 break;
760 if (smbXsrv_is_signed(session->signing_flags)) {
761 signing_degree = CRYPTO_DEGREE_FULL;
762 } else if (smbXsrv_is_partially_signed(session->signing_flags)) {
763 signing_degree = CRYPTO_DEGREE_PARTIAL;
768 if (!state->json_output) {
769 traverse_sessionid_stdout(state,
770 server_id_str_buf(session->pid, &tmp),
771 uid_gid_str,
772 machine_hostname,
773 session_dialect_str(session->connection_dialect),
774 encryption,
775 encryption_degree,
776 signing,
777 signing_degree);
778 } else {
779 result = traverse_sessionid_json(state,
780 session,
781 uid_str,
782 gid_str,
783 encryption,
784 encryption_degree,
785 signing,
786 signing_degree,
787 session_dialect_str(session->connection_dialect));
790 TALLOC_FREE(machine_hostname);
791 TALLOC_FREE(tmp_ctx);
793 return result;
797 static bool print_notify_rec_stdout(struct traverse_state *state,
798 const char *path,
799 char *server_id_str,
800 unsigned filter,
801 unsigned subdir_filter)
803 d_printf("%s\\%s\\%x\\%x\n", path, server_id_str,
804 filter, subdir_filter);
806 return true;
809 static int prepare_notify(struct traverse_state *state)
811 if (!state->json_output) {
812 /* don't print header line */
813 } else {
814 add_section_to_json(state, "notifies");
816 return 0;
819 static bool print_notify_rec(const char *path, struct server_id server,
820 const struct notify_instance *instance,
821 void *private_data)
823 struct server_id_buf idbuf;
824 struct traverse_state *state = (struct traverse_state *)private_data;
825 bool result;
827 if (!state->json_output) {
828 result = print_notify_rec_stdout(state,
829 path,
830 server_id_str_buf(server, &idbuf),
831 (unsigned)instance->filter,
832 (unsigned)instance->subdir_filter);
834 } else {
835 result = print_notify_rec_json(state,
836 instance,
837 server,
838 path);
841 return result;
844 enum {
845 OPT_RESOLVE_UIDS = 1000,
848 int main(int argc, const char *argv[])
850 int c;
851 int profile_only = 0;
852 bool show_processes, show_locks, show_shares;
853 bool show_notify = false;
854 poptContext pc = NULL;
855 struct traverse_state state = {0};
856 struct poptOption long_options[] = {
857 POPT_AUTOHELP
859 .longName = "processes",
860 .shortName = 'p',
861 .argInfo = POPT_ARG_NONE,
862 .arg = NULL,
863 .val = 'p',
864 .descrip = "Show processes only",
867 .longName = "verbose",
868 .shortName = 'v',
869 .argInfo = POPT_ARG_NONE,
870 .arg = NULL,
871 .val = 'v',
872 .descrip = "Be verbose",
875 .longName = "locks",
876 .shortName = 'L',
877 .argInfo = POPT_ARG_NONE,
878 .arg = NULL,
879 .val = 'L',
880 .descrip = "Show locks only",
883 .longName = "shares",
884 .shortName = 'S',
885 .argInfo = POPT_ARG_NONE,
886 .arg = NULL,
887 .val = 'S',
888 .descrip = "Show shares only",
891 .longName = "notify",
892 .shortName = 'N',
893 .argInfo = POPT_ARG_NONE,
894 .arg = NULL,
895 .val = 'N',
896 .descrip = "Show notifies",
899 .longName = "user",
900 .shortName = 'u',
901 .argInfo = POPT_ARG_STRING,
902 .arg = &username,
903 .val = 'u',
904 .descrip = "Switch to user",
907 .longName = "brief",
908 .shortName = 'b',
909 .argInfo = POPT_ARG_NONE,
910 .arg = NULL,
911 .val = 'b',
912 .descrip = "Be brief",
915 .longName = "profile",
916 .shortName = 'P',
917 .argInfo = POPT_ARG_NONE,
918 .arg = NULL,
919 .val = 'P',
920 .descrip = "Do profiling",
923 .longName = "profile-rates",
924 .shortName = 'R',
925 .argInfo = POPT_ARG_NONE,
926 .arg = NULL,
927 .val = 'R',
928 .descrip = "Show call rates",
931 .longName = "byterange",
932 .shortName = 'B',
933 .argInfo = POPT_ARG_NONE,
934 .arg = NULL,
935 .val = 'B',
936 .descrip = "Include byte range locks"
939 .longName = "numeric",
940 .shortName = 'n',
941 .argInfo = POPT_ARG_NONE,
942 .arg = NULL,
943 .val = 'n',
944 .descrip = "Numeric uid/gid"
947 .longName = "json",
948 .shortName = 'j',
949 .argInfo = POPT_ARG_NONE,
950 .arg = NULL,
951 .val = 'j',
952 .descrip = "JSON output"
955 .longName = "fast",
956 .shortName = 'f',
957 .argInfo = POPT_ARG_NONE,
958 .arg = NULL,
959 .val = 'f',
960 .descrip = "Skip checks if processes still exist"
963 .longName = "resolve-uids",
964 .shortName = 0,
965 .argInfo = POPT_ARG_NONE,
966 .arg = NULL,
967 .val = OPT_RESOLVE_UIDS,
968 .descrip = "Try to resolve UIDs to usernames"
970 POPT_COMMON_SAMBA
971 POPT_COMMON_VERSION
972 POPT_TABLEEND
974 TALLOC_CTX *frame = talloc_stackframe();
975 int ret = 0;
976 struct messaging_context *msg_ctx = NULL;
977 char *db_path;
978 bool ok;
980 state.first = true;
981 state.json_output = false;
982 state.resolve_uids = false;
984 smb_init_locale();
986 ok = samba_cmdline_init(frame,
987 SAMBA_CMDLINE_CONFIG_CLIENT,
988 false /* require_smbconf */);
989 if (!ok) {
990 DBG_ERR("Failed to init cmdline parser!\n");
991 TALLOC_FREE(frame);
992 exit(1);
994 lp_set_cmdline("log level", "0");
996 pc = samba_popt_get_context(getprogname(),
997 argc,
998 argv,
999 long_options,
1000 POPT_CONTEXT_KEEP_FIRST);
1001 if (pc == NULL) {
1002 DBG_ERR("Failed to setup popt context!\n");
1003 TALLOC_FREE(frame);
1004 exit(1);
1007 while ((c = poptGetNextOpt(pc)) != -1) {
1008 switch (c) {
1009 case 'p':
1010 processes_only = true;
1011 break;
1012 case 'v':
1013 verbose = true;
1014 break;
1015 case 'L':
1016 locks_only = true;
1017 break;
1018 case 'S':
1019 shares_only = true;
1020 break;
1021 case 'N':
1022 show_notify = true;
1023 break;
1024 case 'b':
1025 brief = true;
1026 break;
1027 case 'u':
1028 Ucrit_addUid(nametouid(poptGetOptArg(pc)));
1029 break;
1030 case 'P':
1031 case 'R':
1032 profile_only = c;
1033 break;
1034 case 'B':
1035 show_brl = true;
1036 break;
1037 case 'n':
1038 numeric_only = true;
1039 break;
1040 case 'j':
1041 state.json_output = true;
1042 break;
1043 case 'f':
1044 do_checks = false;
1045 break;
1046 case OPT_RESOLVE_UIDS:
1047 state.resolve_uids = true;
1048 break;
1049 case POPT_ERROR_BADOPT:
1050 fprintf(stderr, "\nInvalid option %s: %s\n\n",
1051 poptBadOption(pc, 0), poptStrerror(c));
1052 poptPrintUsage(pc, stderr, 0);
1053 exit(1);
1057 sec_init();
1059 #ifdef HAVE_JANSSON
1060 state.root_json = json_new_object();
1061 add_general_information_to_json(&state);
1062 #else /* HAVE_JANSSON */
1063 if (state.json_output) {
1064 fprintf(stderr, "JSON support not available, please install lib Jansson\n");
1065 goto done;
1067 #endif /* HAVE_JANSSON */
1069 if (getuid() != geteuid()) {
1070 fprintf(stderr, "smbstatus should not be run setuid\n");
1071 ret = 1;
1072 goto done;
1075 if (getuid() != 0) {
1076 fprintf(stderr, "smbstatus only works as root!\n");
1077 ret = 1;
1078 goto done;
1081 /* setup the flags based on the possible combincations */
1083 show_processes = !(shares_only || locks_only || profile_only) || processes_only;
1084 show_locks = !(shares_only || processes_only || profile_only) || locks_only;
1085 show_shares = !(processes_only || locks_only || profile_only) || shares_only;
1087 if ( username )
1088 Ucrit_addUid( nametouid(username) );
1090 if (verbose && !state.json_output) {
1091 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
1094 msg_ctx = cmdline_messaging_context(get_dyn_CONFIGFILE());
1095 if (msg_ctx == NULL) {
1096 fprintf(stderr, "Could not initialize messaging, not root?\n");
1097 ret = -1;
1098 goto done;
1101 switch (profile_only) {
1102 case 'P':
1103 /* Dump profile data */
1104 ok = status_profile_dump(verbose, &state);
1105 ret = ok ? 0 : 1;
1106 goto done;
1107 case 'R':
1108 /* Continuously display rate-converted data */
1109 if (!state.json_output) {
1110 ok = status_profile_rates(verbose);
1111 ret = ok ? 0 : 1;
1112 } else {
1113 fprintf(stderr, "Call rates not available in a json output.\n");
1114 ret = 1;
1116 goto done;
1117 default:
1118 break;
1121 if ( show_processes ) {
1122 prepare_sessionid(&state);
1123 sessionid_traverse_read(traverse_sessionid, &state);
1125 if (processes_only) {
1126 goto done;
1130 if ( show_shares ) {
1131 if (brief) {
1132 goto done;
1134 prepare_connections(&state);
1135 connections_forall_read(traverse_connections, &state);
1137 if (!state.json_output) {
1138 d_printf("\n");
1141 if ( shares_only ) {
1142 goto done;
1146 if ( show_locks ) {
1147 int result;
1148 struct db_context *db;
1150 db_path = lock_path(talloc_tos(), "locking.tdb");
1151 if (db_path == NULL) {
1152 fprintf(stderr, "Out of memory - exiting\n");
1153 ret = -1;
1154 goto done;
1157 db = db_open(NULL, db_path, 0,
1158 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0,
1159 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
1161 if (!db) {
1162 fprintf(stderr, "%s not initialised\n", db_path);
1163 fprintf(stderr, "This is normal if an SMB client has never "
1164 "connected to your server.\n");
1165 TALLOC_FREE(db_path);
1166 exit(0);
1167 } else {
1168 TALLOC_FREE(db);
1169 TALLOC_FREE(db_path);
1172 if (!locking_init_readonly()) {
1173 fprintf(stderr, "Can't initialise locking module - exiting\n");
1174 ret = 1;
1175 goto done;
1178 prepare_share_mode(&state);
1179 result = share_entry_forall(print_share_mode, &state);
1181 if (result == 0 && !state.json_output) {
1182 fprintf(stderr, "No locked files\n");
1183 } else if (result < 0 && !state.json_output) {
1184 fprintf(stderr, "locked file list truncated\n");
1187 if (!state.json_output) {
1188 d_printf("\n");
1191 if (show_brl) {
1192 prepare_brl(&state);
1193 brl_forall(print_brl, &state);
1196 locking_end();
1199 if (show_notify) {
1200 prepare_notify(&state);
1201 notify_walk(msg_ctx, print_notify_rec, &state);
1204 done:
1205 cmdline_messaging_context_free();
1206 poptFreeContext(pc);
1207 #ifdef HAVE_JANSSON
1208 if (state.json_output) {
1209 d_printf("%s\n", json_to_string(frame, &state.root_json));
1211 json_free(&state.root_json);
1212 #endif /* HAVE_JANSSON */
1213 TALLOC_FREE(frame);
1214 return ret;