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 "popt_common.h"
38 #include "dbwrap/dbwrap.h"
39 #include "dbwrap/dbwrap_open.h"
40 #include "../libcli/security/security.h"
42 #include "locking/proto.h"
44 #include "librpc/gen_ndr/open_files.h"
45 #include "smbd/smbd.h"
46 #include "librpc/gen_ndr/notify.h"
47 #include "lib/conn_tdb.h"
49 #include "status_profile.h"
50 #include "smbd/notifyd/notifyd.h"
52 #define SMB_MAXPIDS 2048
53 static uid_t Ucrit_uid = 0; /* added by OH */
54 static struct server_id Ucrit_pid[SMB_MAXPIDS]; /* Ugly !!! */ /* added by OH */
55 static int Ucrit_MaxPid=0; /* added by OH */
56 static unsigned int Ucrit_IsActive = 0; /* added by OH */
58 static bool verbose, brief;
59 static bool shares_only; /* Added by RJS */
60 static bool locks_only; /* Added by RJS */
61 static bool processes_only;
63 static bool numeric_only;
64 static bool do_checks = true;
66 const char *username = NULL;
69 static void Ucrit_addUid(uid_t uid)
75 static unsigned int Ucrit_checkUid(uid_t uid)
77 if ( !Ucrit_IsActive )
80 if ( uid == Ucrit_uid )
86 static unsigned int Ucrit_checkPid(struct server_id pid)
90 if ( !Ucrit_IsActive )
93 for (i=0;i<Ucrit_MaxPid;i++) {
94 if (serverid_equal(&pid, &Ucrit_pid[i])) {
102 static bool Ucrit_addPid( struct server_id pid )
104 if ( !Ucrit_IsActive )
107 if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
108 d_printf("ERROR: More than %d pids for user %s!\n",
109 SMB_MAXPIDS, uidtoname(Ucrit_uid));
114 Ucrit_pid[Ucrit_MaxPid++] = pid;
119 static int print_share_mode(const struct share_mode_entry *e,
120 const struct file_id *id,
121 const char *sharepath,
128 if (do_checks && !is_valid_share_mode_entry(e)) {
133 d_printf("Locked files:\n");
134 d_printf("Pid Uid DenyMode Access R/W Oplock SharePath Name Time\n");
135 d_printf("--------------------------------------------------------------------------------------------------\n");
139 if (do_checks && !serverid_exists(&e->pid)) {
140 /* the process for this entry does not exist any more */
144 if (Ucrit_checkPid(e->pid)) {
145 struct server_id_buf tmp;
146 d_printf("%-11s ", server_id_str_buf(e->pid, &tmp));
147 d_printf("%-9u ", (unsigned int)e->uid);
148 switch (map_share_mode_to_deny_mode(e->share_access,
149 e->private_options)) {
150 case DENY_NONE: d_printf("DENY_NONE "); break;
151 case DENY_ALL: d_printf("DENY_ALL "); break;
152 case DENY_DOS: d_printf("DENY_DOS "); break;
153 case DENY_READ: d_printf("DENY_READ "); break;
154 case DENY_WRITE:printf("DENY_WRITE "); break;
155 case DENY_FCB: d_printf("DENY_FCB "); break;
157 d_printf("unknown-please report ! "
158 "e->share_access = 0x%x, "
159 "e->private_options = 0x%x\n",
160 (unsigned int)e->share_access,
161 (unsigned int)e->private_options );
165 d_printf("0x%-8x ",(unsigned int)e->access_mask);
166 if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
167 (FILE_READ_DATA|FILE_WRITE_DATA)) {
169 } else if (e->access_mask & FILE_WRITE_DATA) {
175 if((e->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
176 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) {
177 d_printf("EXCLUSIVE+BATCH ");
178 } else if (e->op_type & EXCLUSIVE_OPLOCK) {
179 d_printf("EXCLUSIVE ");
180 } else if (e->op_type & BATCH_OPLOCK) {
182 } else if (e->op_type & LEVEL_II_OPLOCK) {
183 d_printf("LEVEL_II ");
184 } else if (e->op_type == LEASE_OPLOCK) {
185 uint32_t lstate = e->lease->current_state;
186 d_printf("LEASE(%s%s%s)%s%s%s ",
187 (lstate & SMB2_LEASE_READ)?"R":"",
188 (lstate & SMB2_LEASE_WRITE)?"W":"",
189 (lstate & SMB2_LEASE_HANDLE)?"H":"",
190 (lstate & SMB2_LEASE_READ)?"":" ",
191 (lstate & SMB2_LEASE_WRITE)?"":" ",
192 (lstate & SMB2_LEASE_HANDLE)?"":" ");
197 d_printf(" %s %s%s %s",
200 time_to_asc((time_t)e->time.tv_sec));
206 static void print_brl(struct file_id id,
207 struct server_id pid,
208 enum brl_type lock_type,
209 enum brl_flavour lock_flav,
216 static const struct {
217 enum brl_type lock_type;
222 { PENDING_READ_LOCK, "PR" },
223 { PENDING_WRITE_LOCK, "PW" },
226 const char *desc="X";
227 const char *sharepath = "";
229 struct share_mode_lock *share_mode;
230 struct server_id_buf tmp;
233 d_printf("Byte range locks:\n");
234 d_printf("Pid dev:inode R/W start size SharePath Name\n");
235 d_printf("--------------------------------------------------------------------------------\n");
239 share_mode = fetch_share_mode_unlocked(NULL, id);
241 bool has_stream = share_mode->data->stream_name != NULL;
243 fname = talloc_asprintf(NULL, "%s%s%s",
244 share_mode->data->base_name,
245 has_stream ? ":" : "",
247 share_mode->data->stream_name :
250 fname = talloc_strdup(NULL, "");
256 for (i=0;i<ARRAY_SIZE(lock_types);i++) {
257 if (lock_type == lock_types[i].lock_type) {
258 desc = lock_types[i].desc;
262 d_printf("%-10s %-15s %-4s %-9jd %-9jd %-24s %-24s\n",
263 server_id_str_buf(pid, &tmp), file_id_string_tos(&id),
265 (intmax_t)start, (intmax_t)size,
269 TALLOC_FREE(share_mode);
272 static const char *session_dialect_str(uint16_t dialect)
274 static fstring unkown_dialect;
277 case SMB2_DIALECT_REVISION_000:
279 case SMB2_DIALECT_REVISION_202:
281 case SMB2_DIALECT_REVISION_210:
283 case SMB2_DIALECT_REVISION_222:
285 case SMB2_DIALECT_REVISION_224:
287 case SMB3_DIALECT_REVISION_300:
289 case SMB3_DIALECT_REVISION_302:
291 case SMB3_DIALECT_REVISION_310:
293 case SMB3_DIALECT_REVISION_311:
297 fstr_sprintf(unkown_dialect, "Unknown (0x%04x)", dialect);
298 return unkown_dialect;
301 static int traverse_connections(const struct connections_key *key,
302 const struct connections_data *crec,
305 TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
306 struct server_id_buf tmp;
307 char *timestr = NULL;
309 const char *encryption = "-";
310 const char *signing = "-";
312 if (crec->cnum == TID_FIELD_INVALID)
316 (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid))) {
320 timestr = timestring(mem_ctx, crec->start);
321 if (timestr == NULL) {
325 if (smbXsrv_is_encrypted(crec->encryption_flags)) {
326 switch (crec->cipher) {
327 case SMB_ENCRYPTION_GSSAPI:
328 encryption = "GSSAPI";
330 case SMB2_ENCRYPTION_AES128_CCM:
331 encryption = "AES-128-CCM";
333 case SMB2_ENCRYPTION_AES128_GCM:
334 encryption = "AES-128-GCM";
343 if (smbXsrv_is_signed(crec->signing_flags)) {
344 if (crec->dialect >= SMB3_DIALECT_REVISION_302) {
345 signing = "AES-128-CMAC";
346 } else if (crec->dialect >= SMB2_DIALECT_REVISION_202) {
347 signing = "HMAC-SHA256";
349 signing = "HMAC-MD5";
353 d_printf("%-12s %-7s %-13s %-32s %-12s %-12s\n",
354 crec->servicename, server_id_str_buf(crec->pid, &tmp),
360 TALLOC_FREE(timestr);
365 static int traverse_sessionid(const char *key, struct sessionid *session,
368 TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
370 struct server_id_buf tmp;
371 char *machine_hostname = NULL;
373 const char *encryption = "-";
374 const char *signing = "-";
377 (!process_exists(session->pid) ||
378 !Ucrit_checkUid(session->uid))) {
382 Ucrit_addPid(session->pid);
385 fstr_sprintf(uid_gid_str, "%-12u %-12u",
386 (unsigned int)session->uid,
387 (unsigned int)session->gid);
389 if (session->uid == -1 && session->gid == -1) {
391 * The session is not fully authenticated yet.
393 fstrcpy(uid_gid_str, "(auth in progress)");
396 * In theory it should not happen that one of
397 * session->uid and session->gid is valid (ie != -1)
398 * while the other is not (ie = -1), so we a check for
399 * that case that bails out would be reasonable.
401 const char *uid_name = "-1";
402 const char *gid_name = "-1";
404 if (session->uid != -1) {
405 uid_name = uidtoname(session->uid);
406 if (uid_name == NULL) {
410 if (session->gid != -1) {
411 gid_name = gidtoname(session->gid);
412 if (gid_name == NULL) {
416 fstr_sprintf(uid_gid_str, "%-12s %-12s",
421 machine_hostname = talloc_asprintf(mem_ctx, "%s (%s)",
422 session->remote_machine,
424 if (machine_hostname == NULL) {
428 if (smbXsrv_is_encrypted(session->encryption_flags)) {
429 switch (session->cipher) {
430 case SMB2_ENCRYPTION_AES128_CCM:
431 encryption = "AES-128-CCM";
433 case SMB2_ENCRYPTION_AES128_GCM:
434 encryption = "AES-128-GCM";
441 } else if (smbXsrv_is_partially_encrypted(session->encryption_flags)) {
442 switch (session->cipher) {
443 case SMB_ENCRYPTION_GSSAPI:
444 encryption = "partial(GSSAPI)";
446 case SMB2_ENCRYPTION_AES128_CCM:
447 encryption = "partial(AES-128-CCM)";
449 case SMB2_ENCRYPTION_AES128_GCM:
450 encryption = "partial(AES-128-GCM)";
459 if (smbXsrv_is_signed(session->signing_flags)) {
460 if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) {
461 signing = "AES-128-CMAC";
462 } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) {
463 signing = "HMAC-SHA256";
465 signing = "HMAC-MD5";
467 } else if (smbXsrv_is_partially_signed(session->signing_flags)) {
468 if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) {
469 signing = "partial(AES-128-CMAC)";
470 } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) {
471 signing = "partial(HMAC-SHA256)";
473 signing = "partial(HMAC-MD5)";
478 d_printf("%-7s %-25s %-41s %-17s %-20s %-21s\n",
479 server_id_str_buf(session->pid, &tmp),
482 session_dialect_str(session->connection_dialect),
486 TALLOC_FREE(machine_hostname);
492 static bool print_notify_rec(const char *path, struct server_id server,
493 const struct notify_instance *instance,
496 struct server_id_buf idbuf;
498 d_printf("%s\\%s\\%x\\%x\n", path, server_id_str_buf(server, &idbuf),
499 (unsigned)instance->filter,
500 (unsigned)instance->subdir_filter);
505 int main(int argc, const char *argv[])
508 int profile_only = 0;
509 bool show_processes, show_locks, show_shares;
510 bool show_notify = false;
512 struct poptOption long_options[] = {
514 {"processes", 'p', POPT_ARG_NONE, NULL, 'p', "Show processes only" },
515 {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Be verbose" },
516 {"locks", 'L', POPT_ARG_NONE, NULL, 'L', "Show locks only" },
517 {"shares", 'S', POPT_ARG_NONE, NULL, 'S', "Show shares only" },
518 {"notify", 'N', POPT_ARG_NONE, NULL, 'N', "Show notifies" },
519 {"user", 'u', POPT_ARG_STRING, &username, 'u', "Switch to user" },
520 {"brief", 'b', POPT_ARG_NONE, NULL, 'b', "Be brief" },
521 {"profile", 'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" },
522 {"profile-rates", 'R', POPT_ARG_NONE, NULL, 'R', "Show call rates" },
523 {"byterange", 'B', POPT_ARG_NONE, NULL, 'B', "Include byte range locks"},
524 {"numeric", 'n', POPT_ARG_NONE, NULL, 'n', "Numeric uid/gid"},
525 {"fast", 'f', POPT_ARG_NONE, NULL, 'f', "Skip checks if processes still exist"},
529 TALLOC_CTX *frame = talloc_stackframe();
531 struct tevent_context *ev;
532 struct messaging_context *msg_ctx = NULL;
539 setup_logging(argv[0], DEBUG_STDERR);
540 lp_set_cmdline("log level", "0");
542 if (getuid() != geteuid()) {
543 d_printf("smbstatus should not be run setuid\n");
549 d_printf("smbstatus only works as root!\n");
555 pc = poptGetContext(NULL, argc, argv, long_options,
556 POPT_CONTEXT_KEEP_FIRST);
558 while ((c = poptGetNextOpt(pc)) != -1) {
561 processes_only = true;
579 Ucrit_addUid(nametouid(poptGetOptArg(pc)));
597 /* setup the flags based on the possible combincations */
599 show_processes = !(shares_only || locks_only || profile_only) || processes_only;
600 show_locks = !(shares_only || processes_only || profile_only) || locks_only;
601 show_shares = !(processes_only || locks_only || profile_only) || shares_only;
604 Ucrit_addUid( nametouid(username) );
607 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
610 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
611 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
612 get_dyn_CONFIGFILE());
619 * This implicitly initializes the global ctdbd connection,
620 * usable by the db_open() calls further down.
622 ev = samba_tevent_context_init(NULL);
624 fprintf(stderr, "samba_tevent_context_init failed\n");
629 msg_ctx = messaging_init(NULL, ev);
630 if (msg_ctx == NULL) {
631 fprintf(stderr, "messaging_init failed\n");
636 if (!lp_load_global(get_dyn_CONFIGFILE())) {
637 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
638 get_dyn_CONFIGFILE());
643 switch (profile_only) {
645 /* Dump profile data */
646 ok = status_profile_dump(verbose);
649 /* Continuously display rate-converted data */
650 ok = status_profile_rates(verbose);
656 if ( show_processes ) {
657 d_printf("\nSamba version %s\n",samba_version_string());
658 d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
659 d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
661 sessionid_traverse_read(traverse_sessionid, frame);
663 if (processes_only) {
673 d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing");
674 d_printf("---------------------------------------------------------------------------------------------\n");
676 connections_forall_read(traverse_connections, frame);
687 struct db_context *db;
689 db_path = lock_path("locking.tdb");
690 if (db_path == NULL) {
691 d_printf("Out of memory - exiting\n");
696 db = db_open(NULL, db_path, 0,
697 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0,
698 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
701 d_printf("%s not initialised\n", db_path);
702 d_printf("This is normal if an SMB client has never "
703 "connected to your server.\n");
704 TALLOC_FREE(db_path);
708 TALLOC_FREE(db_path);
711 if (!locking_init_readonly()) {
712 d_printf("Can't initialise locking module - exiting\n");
717 result = share_entry_forall(print_share_mode, NULL);
720 d_printf("No locked files\n");
721 } else if (result < 0) {
722 d_printf("locked file list truncated\n");
728 brl_forall(print_brl, NULL);
735 struct notify_context *n;
737 n = notify_init(talloc_tos(), msg_ctx,
738 messaging_tevent_context(msg_ctx),
743 notify_walk(n, print_notify_rec, NULL);