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"
49 #include "status_profile.h"
50 #include "smbd/notifyd/notifyd.h"
51 #include "cmdline_contexts.h"
53 #define SMB_MAXPIDS 2048
54 static uid_t Ucrit_uid = 0; /* added by OH */
55 static struct server_id Ucrit_pid[SMB_MAXPIDS]; /* Ugly !!! */ /* added by OH */
56 static int Ucrit_MaxPid=0; /* added by OH */
57 static unsigned int Ucrit_IsActive = 0; /* added by OH */
59 static bool verbose, brief;
60 static bool shares_only; /* Added by RJS */
61 static bool locks_only; /* Added by RJS */
62 static bool processes_only;
64 static bool numeric_only;
65 static bool do_checks = true;
67 const char *username = NULL;
70 static void Ucrit_addUid(uid_t uid)
76 static unsigned int Ucrit_checkUid(uid_t uid)
78 if ( !Ucrit_IsActive )
81 if ( uid == Ucrit_uid )
87 static unsigned int Ucrit_checkPid(struct server_id pid)
91 if ( !Ucrit_IsActive )
94 for (i=0;i<Ucrit_MaxPid;i++) {
95 if (serverid_equal(&pid, &Ucrit_pid[i])) {
103 static bool Ucrit_addPid( struct server_id pid )
105 if ( !Ucrit_IsActive )
108 if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
109 d_printf("ERROR: More than %d pids for user %s!\n",
110 SMB_MAXPIDS, uidtoname(Ucrit_uid));
115 Ucrit_pid[Ucrit_MaxPid++] = pid;
120 static int print_share_mode(struct file_id fid,
121 const struct share_mode_data *d,
122 const struct share_mode_entry *e,
125 bool resolve_uids = *((bool *)private_data);
128 if (do_checks && !is_valid_share_mode_entry(e)) {
133 d_printf("Locked files:\n");
134 d_printf("Pid User(ID) 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));
148 d_printf("%-14s ", uidtoname(e->uid));
150 d_printf("%-9u ", (unsigned int)e->uid);
152 switch (map_share_mode_to_deny_mode(e->share_access,
153 e->private_options)) {
154 case DENY_NONE: d_printf("DENY_NONE "); break;
155 case DENY_ALL: d_printf("DENY_ALL "); break;
156 case DENY_DOS: d_printf("DENY_DOS "); break;
157 case DENY_READ: d_printf("DENY_READ "); break;
158 case DENY_WRITE:printf("DENY_WRITE "); break;
159 case DENY_FCB: d_printf("DENY_FCB "); break;
161 d_printf("unknown-please report ! "
162 "e->share_access = 0x%x, "
163 "e->private_options = 0x%x\n",
164 (unsigned int)e->share_access,
165 (unsigned int)e->private_options );
169 d_printf("0x%-8x ",(unsigned int)e->access_mask);
170 if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
171 (FILE_READ_DATA|FILE_WRITE_DATA)) {
173 } else if (e->access_mask & FILE_WRITE_DATA) {
179 if((e->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
180 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) {
181 d_printf("EXCLUSIVE+BATCH ");
182 } else if (e->op_type & EXCLUSIVE_OPLOCK) {
183 d_printf("EXCLUSIVE ");
184 } else if (e->op_type & BATCH_OPLOCK) {
186 } else if (e->op_type & LEVEL_II_OPLOCK) {
187 d_printf("LEVEL_II ");
188 } else if (e->op_type == LEASE_OPLOCK) {
189 struct share_mode_lease *l = &d->leases[e->lease_idx];
190 uint32_t lstate = l->current_state;
191 d_printf("LEASE(%s%s%s)%s%s%s ",
192 (lstate & SMB2_LEASE_READ)?"R":"",
193 (lstate & SMB2_LEASE_WRITE)?"W":"",
194 (lstate & SMB2_LEASE_HANDLE)?"H":"",
195 (lstate & SMB2_LEASE_READ)?"":" ",
196 (lstate & SMB2_LEASE_WRITE)?"":" ",
197 (lstate & SMB2_LEASE_HANDLE)?"":" ");
202 d_printf(" %s %s%s %s",
203 d->servicepath, d->base_name,
204 (d->stream_name != NULL) ? d->stream_name : "",
205 time_to_asc((time_t)e->time.tv_sec));
211 static void print_brl(struct file_id id,
212 struct server_id pid,
213 enum brl_type lock_type,
214 enum brl_flavour lock_flav,
221 static const struct {
222 enum brl_type lock_type;
227 { PENDING_READ_LOCK, "PR" },
228 { PENDING_WRITE_LOCK, "PW" },
231 const char *desc="X";
232 const char *sharepath = "";
234 struct share_mode_lock *share_mode;
235 struct server_id_buf tmp;
238 d_printf("Byte range locks:\n");
239 d_printf("Pid dev:inode R/W start size SharePath Name\n");
240 d_printf("--------------------------------------------------------------------------------\n");
244 share_mode = fetch_share_mode_unlocked(NULL, id);
246 bool has_stream = share_mode->data->stream_name != NULL;
248 fname = talloc_asprintf(NULL, "%s%s%s",
249 share_mode->data->base_name,
250 has_stream ? ":" : "",
252 share_mode->data->stream_name :
255 fname = talloc_strdup(NULL, "");
261 for (i=0;i<ARRAY_SIZE(lock_types);i++) {
262 if (lock_type == lock_types[i].lock_type) {
263 desc = lock_types[i].desc;
267 d_printf("%-10s %-15s %-4s %-9jd %-9jd %-24s %-24s\n",
268 server_id_str_buf(pid, &tmp), file_id_string_tos(&id),
270 (intmax_t)start, (intmax_t)size,
274 TALLOC_FREE(share_mode);
277 static const char *session_dialect_str(uint16_t dialect)
279 static fstring unkown_dialect;
282 case SMB2_DIALECT_REVISION_000:
284 case SMB2_DIALECT_REVISION_202:
286 case SMB2_DIALECT_REVISION_210:
288 case SMB2_DIALECT_REVISION_222:
290 case SMB2_DIALECT_REVISION_224:
292 case SMB3_DIALECT_REVISION_300:
294 case SMB3_DIALECT_REVISION_302:
296 case SMB3_DIALECT_REVISION_310:
298 case SMB3_DIALECT_REVISION_311:
302 fstr_sprintf(unkown_dialect, "Unknown (0x%04x)", dialect);
303 return unkown_dialect;
306 static int traverse_connections(const struct connections_key *key,
307 const struct connections_data *crec,
310 TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
311 struct server_id_buf tmp;
312 char *timestr = NULL;
314 const char *encryption = "-";
315 const char *signing = "-";
317 if (crec->cnum == TID_FIELD_INVALID)
321 (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid))) {
325 timestr = timestring(mem_ctx, crec->start);
326 if (timestr == NULL) {
330 if (smbXsrv_is_encrypted(crec->encryption_flags)) {
331 switch (crec->cipher) {
332 case SMB_ENCRYPTION_GSSAPI:
333 encryption = "GSSAPI";
335 case SMB2_ENCRYPTION_AES128_CCM:
336 encryption = "AES-128-CCM";
338 case SMB2_ENCRYPTION_AES128_GCM:
339 encryption = "AES-128-GCM";
348 if (smbXsrv_is_signed(crec->signing_flags)) {
349 if (crec->dialect >= SMB3_DIALECT_REVISION_302) {
350 signing = "AES-128-CMAC";
351 } else if (crec->dialect >= SMB2_DIALECT_REVISION_202) {
352 signing = "HMAC-SHA256";
354 signing = "HMAC-MD5";
358 d_printf("%-12s %-7s %-13s %-32s %-12s %-12s\n",
359 crec->servicename, server_id_str_buf(crec->pid, &tmp),
365 TALLOC_FREE(timestr);
370 static int traverse_sessionid(const char *key, struct sessionid *session,
373 TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
375 struct server_id_buf tmp;
376 char *machine_hostname = NULL;
378 const char *encryption = "-";
379 const char *signing = "-";
382 (!process_exists(session->pid) ||
383 !Ucrit_checkUid(session->uid))) {
387 Ucrit_addPid(session->pid);
390 fstr_sprintf(uid_gid_str, "%-12u %-12u",
391 (unsigned int)session->uid,
392 (unsigned int)session->gid);
394 if (session->uid == -1 && session->gid == -1) {
396 * The session is not fully authenticated yet.
398 fstrcpy(uid_gid_str, "(auth in progress)");
401 * In theory it should not happen that one of
402 * session->uid and session->gid is valid (ie != -1)
403 * while the other is not (ie = -1), so we a check for
404 * that case that bails out would be reasonable.
406 const char *uid_name = "-1";
407 const char *gid_name = "-1";
409 if (session->uid != -1) {
410 uid_name = uidtoname(session->uid);
411 if (uid_name == NULL) {
415 if (session->gid != -1) {
416 gid_name = gidtoname(session->gid);
417 if (gid_name == NULL) {
421 fstr_sprintf(uid_gid_str, "%-12s %-12s",
426 machine_hostname = talloc_asprintf(mem_ctx, "%s (%s)",
427 session->remote_machine,
429 if (machine_hostname == NULL) {
433 if (smbXsrv_is_encrypted(session->encryption_flags)) {
434 switch (session->cipher) {
435 case SMB2_ENCRYPTION_AES128_CCM:
436 encryption = "AES-128-CCM";
438 case SMB2_ENCRYPTION_AES128_GCM:
439 encryption = "AES-128-GCM";
446 } else if (smbXsrv_is_partially_encrypted(session->encryption_flags)) {
447 switch (session->cipher) {
448 case SMB_ENCRYPTION_GSSAPI:
449 encryption = "partial(GSSAPI)";
451 case SMB2_ENCRYPTION_AES128_CCM:
452 encryption = "partial(AES-128-CCM)";
454 case SMB2_ENCRYPTION_AES128_GCM:
455 encryption = "partial(AES-128-GCM)";
464 if (smbXsrv_is_signed(session->signing_flags)) {
465 if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) {
466 signing = "AES-128-CMAC";
467 } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) {
468 signing = "HMAC-SHA256";
470 signing = "HMAC-MD5";
472 } else if (smbXsrv_is_partially_signed(session->signing_flags)) {
473 if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) {
474 signing = "partial(AES-128-CMAC)";
475 } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) {
476 signing = "partial(HMAC-SHA256)";
478 signing = "partial(HMAC-MD5)";
483 d_printf("%-7s %-25s %-41s %-17s %-20s %-21s\n",
484 server_id_str_buf(session->pid, &tmp),
487 session_dialect_str(session->connection_dialect),
491 TALLOC_FREE(machine_hostname);
497 static bool print_notify_rec(const char *path, struct server_id server,
498 const struct notify_instance *instance,
501 struct server_id_buf idbuf;
503 d_printf("%s\\%s\\%x\\%x\n", path, server_id_str_buf(server, &idbuf),
504 (unsigned)instance->filter,
505 (unsigned)instance->subdir_filter);
511 OPT_RESOLVE_UIDS = 1000,
514 int main(int argc, const char *argv[])
517 int profile_only = 0;
518 bool show_processes, show_locks, show_shares;
519 bool show_notify = false;
520 bool resolve_uids = false;
522 struct poptOption long_options[] = {
525 .longName = "processes",
527 .argInfo = POPT_ARG_NONE,
530 .descrip = "Show processes only",
533 .longName = "verbose",
535 .argInfo = POPT_ARG_NONE,
538 .descrip = "Be verbose",
543 .argInfo = POPT_ARG_NONE,
546 .descrip = "Show locks only",
549 .longName = "shares",
551 .argInfo = POPT_ARG_NONE,
554 .descrip = "Show shares only",
557 .longName = "notify",
559 .argInfo = POPT_ARG_NONE,
562 .descrip = "Show notifies",
567 .argInfo = POPT_ARG_STRING,
570 .descrip = "Switch to user",
575 .argInfo = POPT_ARG_NONE,
578 .descrip = "Be brief",
581 .longName = "profile",
583 .argInfo = POPT_ARG_NONE,
586 .descrip = "Do profiling",
589 .longName = "profile-rates",
591 .argInfo = POPT_ARG_NONE,
594 .descrip = "Show call rates",
597 .longName = "byterange",
599 .argInfo = POPT_ARG_NONE,
602 .descrip = "Include byte range locks"
605 .longName = "numeric",
607 .argInfo = POPT_ARG_NONE,
610 .descrip = "Numeric uid/gid"
615 .argInfo = POPT_ARG_NONE,
618 .descrip = "Skip checks if processes still exist"
621 .longName = "resolve-uids",
623 .argInfo = POPT_ARG_NONE,
625 .val = OPT_RESOLVE_UIDS,
626 .descrip = "Try to resolve UIDs to usernames"
631 TALLOC_CTX *frame = talloc_stackframe();
633 struct messaging_context *msg_ctx = NULL;
640 setup_logging(argv[0], DEBUG_STDERR);
641 lp_set_cmdline("log level", "0");
643 if (getuid() != geteuid()) {
644 d_printf("smbstatus should not be run setuid\n");
650 d_printf("smbstatus only works as root!\n");
656 pc = poptGetContext(NULL, argc, argv, long_options,
657 POPT_CONTEXT_KEEP_FIRST);
659 while ((c = poptGetNextOpt(pc)) != -1) {
662 processes_only = true;
680 Ucrit_addUid(nametouid(poptGetOptArg(pc)));
695 case OPT_RESOLVE_UIDS:
701 /* setup the flags based on the possible combincations */
703 show_processes = !(shares_only || locks_only || profile_only) || processes_only;
704 show_locks = !(shares_only || processes_only || profile_only) || locks_only;
705 show_shares = !(processes_only || locks_only || profile_only) || shares_only;
708 Ucrit_addUid( nametouid(username) );
711 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
714 msg_ctx = cmdline_messaging_context(get_dyn_CONFIGFILE());
715 if (msg_ctx == NULL) {
716 fprintf(stderr, "Could not initialize messaging, not root?\n");
721 if (!lp_load_global(get_dyn_CONFIGFILE())) {
722 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
723 get_dyn_CONFIGFILE());
728 switch (profile_only) {
730 /* Dump profile data */
731 ok = status_profile_dump(verbose);
734 /* Continuously display rate-converted data */
735 ok = status_profile_rates(verbose);
741 if ( show_processes ) {
742 d_printf("\nSamba version %s\n",samba_version_string());
743 d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
744 d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
746 sessionid_traverse_read(traverse_sessionid, frame);
748 if (processes_only) {
758 d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing");
759 d_printf("---------------------------------------------------------------------------------------------\n");
761 connections_forall_read(traverse_connections, frame);
772 struct db_context *db;
774 db_path = lock_path(talloc_tos(), "locking.tdb");
775 if (db_path == NULL) {
776 d_printf("Out of memory - exiting\n");
781 db = db_open(NULL, db_path, 0,
782 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0,
783 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
786 d_printf("%s not initialised\n", db_path);
787 d_printf("This is normal if an SMB client has never "
788 "connected to your server.\n");
789 TALLOC_FREE(db_path);
793 TALLOC_FREE(db_path);
796 if (!locking_init_readonly()) {
797 d_printf("Can't initialise locking module - exiting\n");
802 result = share_entry_forall(print_share_mode, &resolve_uids);
805 d_printf("No locked files\n");
806 } else if (result < 0) {
807 d_printf("locked file list truncated\n");
813 brl_forall(print_brl, NULL);
820 struct notify_context *n;
822 n = notify_init(talloc_tos(), msg_ctx,
827 notify_walk(n, print_notify_rec, NULL);