2 Unix SMB/CIFS implementation.
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/>.
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
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"
42 #include "locking/share_mode_lock.h"
43 #include "locking/proto.h"
45 #include "librpc/gen_ndr/open_files.h"
46 #include "smbd/smbd.h"
47 #include "librpc/gen_ndr/notify.h"
50 #include "status_profile.h"
51 #include "smbd/notifyd/notifyd_db.h"
52 #include "cmdline_contexts.h"
53 #include "locking/leases_db.h"
54 #include "lib/util/string_wrappers.h"
56 #define SMB_MAXPIDS 2048
57 static uid_t Ucrit_uid
= 0; /* added by OH */
58 static struct server_id Ucrit_pid
[SMB_MAXPIDS
]; /* Ugly !!! */ /* added by OH */
59 static int Ucrit_MaxPid
=0; /* added by OH */
60 static unsigned int Ucrit_IsActive
= 0; /* added by OH */
62 static bool verbose
, brief
;
63 static bool shares_only
; /* Added by RJS */
64 static bool locks_only
; /* Added by RJS */
65 static bool processes_only
;
67 static bool numeric_only
;
68 static bool do_checks
= true;
70 const char *username
= NULL
;
73 static void Ucrit_addUid(uid_t uid
)
79 static unsigned int Ucrit_checkUid(uid_t uid
)
81 if ( !Ucrit_IsActive
)
84 if ( uid
== Ucrit_uid
)
90 static unsigned int Ucrit_checkPid(struct server_id pid
)
94 if ( !Ucrit_IsActive
)
97 for (i
=0;i
<Ucrit_MaxPid
;i
++) {
98 if (server_id_equal(&pid
, &Ucrit_pid
[i
])) {
106 static bool Ucrit_addPid( struct server_id pid
)
108 if ( !Ucrit_IsActive
)
111 if ( Ucrit_MaxPid
>= SMB_MAXPIDS
) {
112 d_printf("ERROR: More than %d pids for user %s!\n",
113 SMB_MAXPIDS
, uidtoname(Ucrit_uid
));
118 Ucrit_pid
[Ucrit_MaxPid
++] = pid
;
123 static int print_share_mode(struct file_id fid
,
124 const struct share_mode_data
*d
,
125 const struct share_mode_entry
*e
,
128 bool resolve_uids
= *((bool *)private_data
);
131 if (do_checks
&& !is_valid_share_mode_entry(e
)) {
136 d_printf("Locked files:\n");
137 d_printf("Pid User(ID) DenyMode Access R/W Oplock SharePath Name Time\n");
138 d_printf("--------------------------------------------------------------------------------------------------\n");
142 if (do_checks
&& !serverid_exists(&e
->pid
)) {
143 /* the process for this entry does not exist any more */
147 if (Ucrit_checkPid(e
->pid
)) {
148 struct server_id_buf tmp
;
149 d_printf("%-11s ", server_id_str_buf(e
->pid
, &tmp
));
151 d_printf("%-14s ", uidtoname(e
->uid
));
153 d_printf("%-9u ", (unsigned int)e
->uid
);
155 switch (map_share_mode_to_deny_mode(e
->share_access
,
156 e
->private_options
)) {
157 case DENY_NONE
: d_printf("DENY_NONE "); break;
158 case DENY_ALL
: d_printf("DENY_ALL "); break;
159 case DENY_DOS
: d_printf("DENY_DOS "); break;
160 case DENY_READ
: d_printf("DENY_READ "); break;
161 case DENY_WRITE
:d_printf("DENY_WRITE "); break;
162 case DENY_FCB
: d_printf("DENY_FCB "); break;
164 d_printf("unknown-please report ! "
165 "e->share_access = 0x%x, "
166 "e->private_options = 0x%x\n",
167 (unsigned int)e
->share_access
,
168 (unsigned int)e
->private_options
);
172 d_printf("0x%-8x ",(unsigned int)e
->access_mask
);
173 if ((e
->access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
))==
174 (FILE_READ_DATA
|FILE_WRITE_DATA
)) {
176 } else if (e
->access_mask
& FILE_WRITE_DATA
) {
182 if((e
->op_type
& (EXCLUSIVE_OPLOCK
|BATCH_OPLOCK
)) ==
183 (EXCLUSIVE_OPLOCK
|BATCH_OPLOCK
)) {
184 d_printf("EXCLUSIVE+BATCH ");
185 } else if (e
->op_type
& EXCLUSIVE_OPLOCK
) {
186 d_printf("EXCLUSIVE ");
187 } else if (e
->op_type
& BATCH_OPLOCK
) {
189 } else if (e
->op_type
& LEVEL_II_OPLOCK
) {
190 d_printf("LEVEL_II ");
191 } else if (e
->op_type
== LEASE_OPLOCK
) {
195 status
= leases_db_get(
199 &lstate
, /* current_state */
201 NULL
, /* breaking_to_requested */
202 NULL
, /* breaking_to_required */
203 NULL
, /* lease_version */
206 if (NT_STATUS_IS_OK(status
)) {
207 d_printf("LEASE(%s%s%s)%s%s%s ",
208 (lstate
& SMB2_LEASE_READ
)?"R":"",
209 (lstate
& SMB2_LEASE_WRITE
)?"W":"",
210 (lstate
& SMB2_LEASE_HANDLE
)?"H":"",
211 (lstate
& SMB2_LEASE_READ
)?"":" ",
212 (lstate
& SMB2_LEASE_WRITE
)?"":" ",
213 (lstate
& SMB2_LEASE_HANDLE
)?"":" ");
215 d_printf("LEASE STATE UNKNOWN");
221 d_printf(" %s %s%s %s",
222 d
->servicepath
, d
->base_name
,
223 (d
->stream_name
!= NULL
) ? d
->stream_name
: "",
224 time_to_asc((time_t)e
->time
.tv_sec
));
230 static void print_brl(struct file_id id
,
231 struct server_id pid
,
232 enum brl_type lock_type
,
233 enum brl_flavour lock_flav
,
240 static const struct {
241 enum brl_type lock_type
;
248 const char *desc
="X";
249 const char *sharepath
= "";
251 struct share_mode_lock
*share_mode
;
252 struct server_id_buf tmp
;
253 struct file_id_buf ftmp
;
256 d_printf("Byte range locks:\n");
257 d_printf("Pid dev:inode R/W start size SharePath Name\n");
258 d_printf("--------------------------------------------------------------------------------\n");
262 share_mode
= fetch_share_mode_unlocked(NULL
, id
);
264 fname
= share_mode_filename(NULL
, share_mode
);
266 fname
= talloc_strdup(NULL
, "");
272 for (i
=0;i
<ARRAY_SIZE(lock_types
);i
++) {
273 if (lock_type
== lock_types
[i
].lock_type
) {
274 desc
= lock_types
[i
].desc
;
278 d_printf("%-10s %-15s %-4s %-9jd %-9jd %-24s %-24s\n",
279 server_id_str_buf(pid
, &tmp
),
280 file_id_str_buf(id
, &ftmp
),
282 (intmax_t)start
, (intmax_t)size
,
286 TALLOC_FREE(share_mode
);
289 static const char *session_dialect_str(uint16_t dialect
)
291 static fstring unkown_dialect
;
294 case SMB2_DIALECT_REVISION_000
:
296 case SMB2_DIALECT_REVISION_202
:
298 case SMB2_DIALECT_REVISION_210
:
300 case SMB2_DIALECT_REVISION_222
:
302 case SMB2_DIALECT_REVISION_224
:
304 case SMB3_DIALECT_REVISION_300
:
306 case SMB3_DIALECT_REVISION_302
:
308 case SMB3_DIALECT_REVISION_310
:
310 case SMB3_DIALECT_REVISION_311
:
314 fstr_sprintf(unkown_dialect
, "Unknown (0x%04x)", dialect
);
315 return unkown_dialect
;
318 static int traverse_connections(const struct connections_key
*key
,
319 const struct connections_data
*crec
,
322 TALLOC_CTX
*mem_ctx
= (TALLOC_CTX
*)private_data
;
323 struct server_id_buf tmp
;
324 char *timestr
= NULL
;
326 const char *encryption
= "-";
327 const char *signing
= "-";
329 if (crec
->cnum
== TID_FIELD_INVALID
)
333 (!process_exists(crec
->pid
) || !Ucrit_checkUid(crec
->uid
))) {
337 timestr
= timestring(mem_ctx
, crec
->start
);
338 if (timestr
== NULL
) {
342 if (smbXsrv_is_encrypted(crec
->encryption_flags
)) {
343 switch (crec
->cipher
) {
344 case SMB_ENCRYPTION_GSSAPI
:
345 encryption
= "GSSAPI";
347 case SMB2_ENCRYPTION_AES128_CCM
:
348 encryption
= "AES-128-CCM";
350 case SMB2_ENCRYPTION_AES128_GCM
:
351 encryption
= "AES-128-GCM";
360 if (smbXsrv_is_signed(crec
->signing_flags
)) {
361 switch (crec
->signing
) {
362 case SMB2_SIGNING_MD5_SMB1
:
363 signing
= "HMAC-MD5";
365 case SMB2_SIGNING_HMAC_SHA256
:
366 signing
= "HMAC-SHA256";
368 case SMB2_SIGNING_AES128_CMAC
:
369 signing
= "AES-128-CMAC";
371 case SMB2_SIGNING_AES128_GMAC
:
372 signing
= "AES-128-GMAC";
381 d_printf("%-12s %-7s %-13s %-32s %-12s %-12s\n",
382 crec
->servicename
, server_id_str_buf(crec
->pid
, &tmp
),
388 TALLOC_FREE(timestr
);
393 static int traverse_sessionid(const char *key
, struct sessionid
*session
,
396 TALLOC_CTX
*mem_ctx
= (TALLOC_CTX
*)private_data
;
398 struct server_id_buf tmp
;
399 char *machine_hostname
= NULL
;
401 const char *encryption
= "-";
402 const char *signing
= "-";
405 (!process_exists(session
->pid
) ||
406 !Ucrit_checkUid(session
->uid
))) {
410 Ucrit_addPid(session
->pid
);
413 fstr_sprintf(uid_gid_str
, "%-12u %-12u",
414 (unsigned int)session
->uid
,
415 (unsigned int)session
->gid
);
417 if (session
->uid
== -1 && session
->gid
== -1) {
419 * The session is not fully authenticated yet.
421 fstrcpy(uid_gid_str
, "(auth in progress)");
424 * In theory it should not happen that one of
425 * session->uid and session->gid is valid (ie != -1)
426 * while the other is not (ie = -1), so we a check for
427 * that case that bails out would be reasonable.
429 const char *uid_name
= "-1";
430 const char *gid_name
= "-1";
432 if (session
->uid
!= -1) {
433 uid_name
= uidtoname(session
->uid
);
434 if (uid_name
== NULL
) {
438 if (session
->gid
!= -1) {
439 gid_name
= gidtoname(session
->gid
);
440 if (gid_name
== NULL
) {
444 fstr_sprintf(uid_gid_str
, "%-12s %-12s",
449 machine_hostname
= talloc_asprintf(mem_ctx
, "%s (%s)",
450 session
->remote_machine
,
452 if (machine_hostname
== NULL
) {
456 if (smbXsrv_is_encrypted(session
->encryption_flags
)) {
457 switch (session
->cipher
) {
458 case SMB2_ENCRYPTION_AES128_CCM
:
459 encryption
= "AES-128-CCM";
461 case SMB2_ENCRYPTION_AES128_GCM
:
462 encryption
= "AES-128-GCM";
464 case SMB2_ENCRYPTION_AES256_CCM
:
465 encryption
= "AES-256-CCM";
467 case SMB2_ENCRYPTION_AES256_GCM
:
468 encryption
= "AES-256-GCM";
475 } else if (smbXsrv_is_partially_encrypted(session
->encryption_flags
)) {
476 switch (session
->cipher
) {
477 case SMB_ENCRYPTION_GSSAPI
:
478 encryption
= "partial(GSSAPI)";
480 case SMB2_ENCRYPTION_AES128_CCM
:
481 encryption
= "partial(AES-128-CCM)";
483 case SMB2_ENCRYPTION_AES128_GCM
:
484 encryption
= "partial(AES-128-GCM)";
486 case SMB2_ENCRYPTION_AES256_CCM
:
487 encryption
= "partial(AES-256-CCM)";
489 case SMB2_ENCRYPTION_AES256_GCM
:
490 encryption
= "partial(AES-256-GCM)";
499 if (smbXsrv_is_signed(session
->signing_flags
)) {
500 switch (session
->signing
) {
501 case SMB2_SIGNING_MD5_SMB1
:
502 signing
= "HMAC-MD5";
504 case SMB2_SIGNING_HMAC_SHA256
:
505 signing
= "HMAC-SHA256";
507 case SMB2_SIGNING_AES128_CMAC
:
508 signing
= "AES-128-CMAC";
510 case SMB2_SIGNING_AES128_GMAC
:
511 signing
= "AES-128-GMAC";
518 } else if (smbXsrv_is_partially_signed(session
->signing_flags
)) {
519 switch (session
->signing
) {
520 case SMB2_SIGNING_MD5_SMB1
:
521 signing
= "partial(HMAC-MD5)";
523 case SMB2_SIGNING_HMAC_SHA256
:
524 signing
= "partial(HMAC-SHA256)";
526 case SMB2_SIGNING_AES128_CMAC
:
527 signing
= "partial(AES-128-CMAC)";
529 case SMB2_SIGNING_AES128_GMAC
:
530 signing
= "partial(AES-128-GMAC)";
540 d_printf("%-7s %-25s %-41s %-17s %-20s %-21s\n",
541 server_id_str_buf(session
->pid
, &tmp
),
544 session_dialect_str(session
->connection_dialect
),
548 TALLOC_FREE(machine_hostname
);
554 static bool print_notify_rec(const char *path
, struct server_id server
,
555 const struct notify_instance
*instance
,
558 struct server_id_buf idbuf
;
560 d_printf("%s\\%s\\%x\\%x\n", path
, server_id_str_buf(server
, &idbuf
),
561 (unsigned)instance
->filter
,
562 (unsigned)instance
->subdir_filter
);
568 OPT_RESOLVE_UIDS
= 1000,
571 int main(int argc
, const char *argv
[])
574 int profile_only
= 0;
575 bool show_processes
, show_locks
, show_shares
;
576 bool show_notify
= false;
577 bool resolve_uids
= false;
578 poptContext pc
= NULL
;
579 struct poptOption long_options
[] = {
582 .longName
= "processes",
584 .argInfo
= POPT_ARG_NONE
,
587 .descrip
= "Show processes only",
590 .longName
= "verbose",
592 .argInfo
= POPT_ARG_NONE
,
595 .descrip
= "Be verbose",
600 .argInfo
= POPT_ARG_NONE
,
603 .descrip
= "Show locks only",
606 .longName
= "shares",
608 .argInfo
= POPT_ARG_NONE
,
611 .descrip
= "Show shares only",
614 .longName
= "notify",
616 .argInfo
= POPT_ARG_NONE
,
619 .descrip
= "Show notifies",
624 .argInfo
= POPT_ARG_STRING
,
627 .descrip
= "Switch to user",
632 .argInfo
= POPT_ARG_NONE
,
635 .descrip
= "Be brief",
638 .longName
= "profile",
640 .argInfo
= POPT_ARG_NONE
,
643 .descrip
= "Do profiling",
646 .longName
= "profile-rates",
648 .argInfo
= POPT_ARG_NONE
,
651 .descrip
= "Show call rates",
654 .longName
= "byterange",
656 .argInfo
= POPT_ARG_NONE
,
659 .descrip
= "Include byte range locks"
662 .longName
= "numeric",
664 .argInfo
= POPT_ARG_NONE
,
667 .descrip
= "Numeric uid/gid"
672 .argInfo
= POPT_ARG_NONE
,
675 .descrip
= "Skip checks if processes still exist"
678 .longName
= "resolve-uids",
680 .argInfo
= POPT_ARG_NONE
,
682 .val
= OPT_RESOLVE_UIDS
,
683 .descrip
= "Try to resolve UIDs to usernames"
689 TALLOC_CTX
*frame
= talloc_stackframe();
691 struct messaging_context
*msg_ctx
= NULL
;
697 ok
= samba_cmdline_init(frame
,
698 SAMBA_CMDLINE_CONFIG_CLIENT
,
699 false /* require_smbconf */);
701 DBG_ERR("Failed to init cmdline parser!\n");
705 lp_set_cmdline("log level", "0");
707 pc
= samba_popt_get_context(getprogname(),
711 POPT_CONTEXT_KEEP_FIRST
);
713 DBG_ERR("Failed to setup popt context!\n");
718 while ((c
= poptGetNextOpt(pc
)) != -1) {
721 processes_only
= true;
739 Ucrit_addUid(nametouid(poptGetOptArg(pc
)));
754 case OPT_RESOLVE_UIDS
:
757 case POPT_ERROR_BADOPT
:
758 fprintf(stderr
, "\nInvalid option %s: %s\n\n",
759 poptBadOption(pc
, 0), poptStrerror(c
));
760 poptPrintUsage(pc
, stderr
, 0);
767 if (getuid() != geteuid()) {
768 d_printf("smbstatus should not be run setuid\n");
774 d_printf("smbstatus only works as root!\n");
779 /* setup the flags based on the possible combincations */
781 show_processes
= !(shares_only
|| locks_only
|| profile_only
) || processes_only
;
782 show_locks
= !(shares_only
|| processes_only
|| profile_only
) || locks_only
;
783 show_shares
= !(processes_only
|| locks_only
|| profile_only
) || shares_only
;
786 Ucrit_addUid( nametouid(username
) );
789 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
792 msg_ctx
= cmdline_messaging_context(get_dyn_CONFIGFILE());
793 if (msg_ctx
== NULL
) {
794 fprintf(stderr
, "Could not initialize messaging, not root?\n");
799 switch (profile_only
) {
801 /* Dump profile data */
802 ok
= status_profile_dump(verbose
);
806 /* Continuously display rate-converted data */
807 ok
= status_profile_rates(verbose
);
814 if ( show_processes
) {
815 d_printf("\nSamba version %s\n",samba_version_string());
816 d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
817 d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
819 sessionid_traverse_read(traverse_sessionid
, frame
);
821 if (processes_only
) {
831 d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing");
832 d_printf("---------------------------------------------------------------------------------------------\n");
834 connections_forall_read(traverse_connections
, frame
);
845 struct db_context
*db
;
847 db_path
= lock_path(talloc_tos(), "locking.tdb");
848 if (db_path
== NULL
) {
849 d_printf("Out of memory - exiting\n");
854 db
= db_open(NULL
, db_path
, 0,
855 TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
, O_RDONLY
, 0,
856 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
859 d_printf("%s not initialised\n", db_path
);
860 d_printf("This is normal if an SMB client has never "
861 "connected to your server.\n");
862 TALLOC_FREE(db_path
);
866 TALLOC_FREE(db_path
);
869 if (!locking_init_readonly()) {
870 d_printf("Can't initialise locking module - exiting\n");
875 result
= share_entry_forall(print_share_mode
, &resolve_uids
);
878 d_printf("No locked files\n");
879 } else if (result
< 0) {
880 d_printf("locked file list truncated\n");
886 brl_forall(print_brl
, NULL
);
893 notify_walk(msg_ctx
, print_notify_rec
, NULL
);
897 cmdline_messaging_context_free();