vfs_aio_fork: Use a shorter random delay
[Samba.git] / source3 / utils / status.c
blobdfb1d921a4230f4af0a2c67c1c4186aa2b81398b
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 "popt_common.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/proto.h"
43 #include "messages.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"
48 #include "serverid.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;
62 static bool show_brl;
63 static bool numeric_only;
64 static bool do_checks = true;
66 const char *username = NULL;
68 /* added by OH */
69 static void Ucrit_addUid(uid_t uid)
71 Ucrit_uid = uid;
72 Ucrit_IsActive = 1;
75 static unsigned int Ucrit_checkUid(uid_t uid)
77 if ( !Ucrit_IsActive )
78 return 1;
80 if ( uid == Ucrit_uid )
81 return 1;
83 return 0;
86 static unsigned int Ucrit_checkPid(struct server_id pid)
88 int i;
90 if ( !Ucrit_IsActive )
91 return 1;
93 for (i=0;i<Ucrit_MaxPid;i++) {
94 if (serverid_equal(&pid, &Ucrit_pid[i])) {
95 return 1;
99 return 0;
102 static bool Ucrit_addPid( struct server_id pid )
104 if ( !Ucrit_IsActive )
105 return True;
107 if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
108 d_printf("ERROR: More than %d pids for user %s!\n",
109 SMB_MAXPIDS, uidtoname(Ucrit_uid));
111 return False;
114 Ucrit_pid[Ucrit_MaxPid++] = pid;
116 return True;
119 static int print_share_mode(const struct share_mode_entry *e,
120 const char *sharepath,
121 const char *fname,
122 const char *sname,
123 void *dummy)
125 static int count;
127 if (do_checks && !is_valid_share_mode_entry(e)) {
128 return 0;
131 if (count==0) {
132 d_printf("Locked files:\n");
133 d_printf("Pid Uid DenyMode Access R/W Oplock SharePath Name Time\n");
134 d_printf("--------------------------------------------------------------------------------------------------\n");
136 count++;
138 if (do_checks && !serverid_exists(&e->pid)) {
139 /* the process for this entry does not exist any more */
140 return 0;
143 if (Ucrit_checkPid(e->pid)) {
144 struct server_id_buf tmp;
145 d_printf("%-11s ", server_id_str_buf(e->pid, &tmp));
146 d_printf("%-9u ", (unsigned int)e->uid);
147 switch (map_share_mode_to_deny_mode(e->share_access,
148 e->private_options)) {
149 case DENY_NONE: d_printf("DENY_NONE "); break;
150 case DENY_ALL: d_printf("DENY_ALL "); break;
151 case DENY_DOS: d_printf("DENY_DOS "); break;
152 case DENY_READ: d_printf("DENY_READ "); break;
153 case DENY_WRITE:printf("DENY_WRITE "); break;
154 case DENY_FCB: d_printf("DENY_FCB "); break;
155 default: {
156 d_printf("unknown-please report ! "
157 "e->share_access = 0x%x, "
158 "e->private_options = 0x%x\n",
159 (unsigned int)e->share_access,
160 (unsigned int)e->private_options );
161 break;
164 d_printf("0x%-8x ",(unsigned int)e->access_mask);
165 if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
166 (FILE_READ_DATA|FILE_WRITE_DATA)) {
167 d_printf("RDWR ");
168 } else if (e->access_mask & FILE_WRITE_DATA) {
169 d_printf("WRONLY ");
170 } else {
171 d_printf("RDONLY ");
174 if((e->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
175 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) {
176 d_printf("EXCLUSIVE+BATCH ");
177 } else if (e->op_type & EXCLUSIVE_OPLOCK) {
178 d_printf("EXCLUSIVE ");
179 } else if (e->op_type & BATCH_OPLOCK) {
180 d_printf("BATCH ");
181 } else if (e->op_type & LEVEL_II_OPLOCK) {
182 d_printf("LEVEL_II ");
183 } else if (e->op_type == LEASE_OPLOCK) {
184 uint32_t lstate = e->lease->current_state;
185 d_printf("LEASE(%s%s%s)%s%s%s ",
186 (lstate & SMB2_LEASE_READ)?"R":"",
187 (lstate & SMB2_LEASE_WRITE)?"W":"",
188 (lstate & SMB2_LEASE_HANDLE)?"H":"",
189 (lstate & SMB2_LEASE_READ)?"":" ",
190 (lstate & SMB2_LEASE_WRITE)?"":" ",
191 (lstate & SMB2_LEASE_HANDLE)?"":" ");
192 } else {
193 d_printf("NONE ");
196 d_printf(" %s %s%s %s",
197 sharepath, fname,
198 sname ? sname : "",
199 time_to_asc((time_t)e->time.tv_sec));
202 return 0;
205 static void print_brl(struct file_id id,
206 struct server_id pid,
207 enum brl_type lock_type,
208 enum brl_flavour lock_flav,
209 br_off start,
210 br_off size,
211 void *private_data)
213 static int count;
214 unsigned int i;
215 static const struct {
216 enum brl_type lock_type;
217 const char *desc;
218 } lock_types[] = {
219 { READ_LOCK, "R" },
220 { WRITE_LOCK, "W" },
221 { PENDING_READ_LOCK, "PR" },
222 { PENDING_WRITE_LOCK, "PW" },
223 { UNLOCK_LOCK, "U" }
225 const char *desc="X";
226 const char *sharepath = "";
227 char *fname = NULL;
228 struct share_mode_lock *share_mode;
229 struct server_id_buf tmp;
231 if (count==0) {
232 d_printf("Byte range locks:\n");
233 d_printf("Pid dev:inode R/W start size SharePath Name\n");
234 d_printf("--------------------------------------------------------------------------------\n");
236 count++;
238 share_mode = fetch_share_mode_unlocked(NULL, id);
239 if (share_mode) {
240 bool has_stream = share_mode->data->stream_name != NULL;
242 fname = talloc_asprintf(NULL, "%s%s%s",
243 share_mode->data->base_name,
244 has_stream ? ":" : "",
245 has_stream ?
246 share_mode->data->stream_name :
247 "");
248 } else {
249 fname = talloc_strdup(NULL, "");
250 if (fname == NULL) {
251 return;
255 for (i=0;i<ARRAY_SIZE(lock_types);i++) {
256 if (lock_type == lock_types[i].lock_type) {
257 desc = lock_types[i].desc;
261 d_printf("%-10s %-15s %-4s %-9jd %-9jd %-24s %-24s\n",
262 server_id_str_buf(pid, &tmp), file_id_string_tos(&id),
263 desc,
264 (intmax_t)start, (intmax_t)size,
265 sharepath, fname);
267 TALLOC_FREE(fname);
268 TALLOC_FREE(share_mode);
271 static const char *session_dialect_str(uint16_t dialect)
273 static fstring unkown_dialect;
275 switch(dialect){
276 case SMB2_DIALECT_REVISION_000:
277 return "NT1";
278 case SMB2_DIALECT_REVISION_202:
279 return "SMB2_02";
280 case SMB2_DIALECT_REVISION_210:
281 return "SMB2_10";
282 case SMB2_DIALECT_REVISION_222:
283 return "SMB2_22";
284 case SMB2_DIALECT_REVISION_224:
285 return "SMB2_24";
286 case SMB3_DIALECT_REVISION_300:
287 return "SMB3_00";
288 case SMB3_DIALECT_REVISION_302:
289 return "SMB3_02";
290 case SMB3_DIALECT_REVISION_310:
291 return "SMB3_10";
292 case SMB3_DIALECT_REVISION_311:
293 return "SMB3_11";
296 fstr_sprintf(unkown_dialect, "Unknown (0x%04x)", dialect);
297 return unkown_dialect;
300 static int traverse_connections(const struct connections_key *key,
301 const struct connections_data *crec,
302 void *private_data)
304 TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
305 struct server_id_buf tmp;
306 char *timestr = NULL;
307 int result = 0;
308 const char *encryption = "-";
309 const char *signing = "-";
311 if (crec->cnum == TID_FIELD_INVALID)
312 return 0;
314 if (do_checks &&
315 (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid))) {
316 return 0;
319 timestr = timestring(mem_ctx, crec->start);
320 if (timestr == NULL) {
321 return -1;
324 if (smbXsrv_is_encrypted(crec->encryption_flags)) {
325 switch (crec->cipher) {
326 case SMB_ENCRYPTION_GSSAPI:
327 encryption = "GSSAPI";
328 break;
329 case SMB2_ENCRYPTION_AES128_CCM:
330 encryption = "AES-128-CCM";
331 break;
332 case SMB2_ENCRYPTION_AES128_GCM:
333 encryption = "AES-128-GCM";
334 break;
335 default:
336 encryption = "???";
337 result = -1;
338 break;
342 if (smbXsrv_is_signed(crec->signing_flags)) {
343 if (crec->dialect >= SMB3_DIALECT_REVISION_302) {
344 signing = "AES-128-CMAC";
345 } else if (crec->dialect >= SMB2_DIALECT_REVISION_202) {
346 signing = "HMAC-SHA256";
347 } else {
348 signing = "HMAC-MD5";
352 d_printf("%-12s %-7s %-13s %-32s %-12s %-12s\n",
353 crec->servicename, server_id_str_buf(crec->pid, &tmp),
354 crec->machine,
355 timestr,
356 encryption,
357 signing);
359 TALLOC_FREE(timestr);
361 return result;
364 static int traverse_sessionid(const char *key, struct sessionid *session,
365 void *private_data)
367 TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
368 fstring uid_gid_str;
369 struct server_id_buf tmp;
370 char *machine_hostname = NULL;
371 int result = 0;
372 const char *encryption = "-";
373 const char *signing = "-";
375 if (do_checks &&
376 (!process_exists(session->pid) ||
377 !Ucrit_checkUid(session->uid))) {
378 return 0;
381 Ucrit_addPid(session->pid);
383 if (numeric_only) {
384 fstr_sprintf(uid_gid_str, "%-12u %-12u",
385 (unsigned int)session->uid,
386 (unsigned int)session->gid);
387 } else {
388 if (session->uid == -1 && session->gid == -1) {
390 * The session is not fully authenticated yet.
392 fstrcpy(uid_gid_str, "(auth in progress)");
393 } else {
395 * In theory it should not happen that one of
396 * session->uid and session->gid is valid (ie != -1)
397 * while the other is not (ie = -1), so we a check for
398 * that case that bails out would be reasonable.
400 const char *uid_name = "-1";
401 const char *gid_name = "-1";
403 if (session->uid != -1) {
404 uid_name = uidtoname(session->uid);
405 if (uid_name == NULL) {
406 return -1;
409 if (session->gid != -1) {
410 gid_name = gidtoname(session->gid);
411 if (gid_name == NULL) {
412 return -1;
415 fstr_sprintf(uid_gid_str, "%-12s %-12s",
416 uid_name, gid_name);
420 machine_hostname = talloc_asprintf(mem_ctx, "%s (%s)",
421 session->remote_machine,
422 session->hostname);
423 if (machine_hostname == NULL) {
424 return -1;
427 if (smbXsrv_is_encrypted(session->encryption_flags)) {
428 switch (session->cipher) {
429 case SMB2_ENCRYPTION_AES128_CCM:
430 encryption = "AES-128-CCM";
431 break;
432 case SMB2_ENCRYPTION_AES128_GCM:
433 encryption = "AES-128-GCM";
434 break;
435 default:
436 encryption = "???";
437 result = -1;
438 break;
440 } else if (smbXsrv_is_partially_encrypted(session->encryption_flags)) {
441 switch (session->cipher) {
442 case SMB_ENCRYPTION_GSSAPI:
443 encryption = "partial(GSSAPI)";
444 break;
445 case SMB2_ENCRYPTION_AES128_CCM:
446 encryption = "partial(AES-128-CCM)";
447 break;
448 case SMB2_ENCRYPTION_AES128_GCM:
449 encryption = "partial(AES-128-GCM)";
450 break;
451 default:
452 encryption = "???";
453 result = -1;
454 break;
458 if (smbXsrv_is_signed(session->signing_flags)) {
459 if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) {
460 signing = "AES-128-CMAC";
461 } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) {
462 signing = "HMAC-SHA256";
463 } else {
464 signing = "HMAC-MD5";
466 } else if (smbXsrv_is_partially_signed(session->signing_flags)) {
467 if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) {
468 signing = "partial(AES-128-CMAC)";
469 } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) {
470 signing = "partial(HMAC-SHA256)";
471 } else {
472 signing = "partial(HMAC-MD5)";
477 d_printf("%-7s %-25s %-41s %-17s %-20s %-21s\n",
478 server_id_str_buf(session->pid, &tmp),
479 uid_gid_str,
480 machine_hostname,
481 session_dialect_str(session->connection_dialect),
482 encryption,
483 signing);
485 TALLOC_FREE(machine_hostname);
487 return result;
491 static bool print_notify_rec(const char *path, struct server_id server,
492 const struct notify_instance *instance,
493 void *private_data)
495 struct server_id_buf idbuf;
497 d_printf("%s\\%s\\%x\\%x\n", path, server_id_str_buf(server, &idbuf),
498 (unsigned)instance->filter,
499 (unsigned)instance->subdir_filter);
501 return true;
504 int main(int argc, const char *argv[])
506 int c;
507 int profile_only = 0;
508 bool show_processes, show_locks, show_shares;
509 bool show_notify = false;
510 poptContext pc;
511 struct poptOption long_options[] = {
512 POPT_AUTOHELP
513 {"processes", 'p', POPT_ARG_NONE, NULL, 'p', "Show processes only" },
514 {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Be verbose" },
515 {"locks", 'L', POPT_ARG_NONE, NULL, 'L', "Show locks only" },
516 {"shares", 'S', POPT_ARG_NONE, NULL, 'S', "Show shares only" },
517 {"notify", 'N', POPT_ARG_NONE, NULL, 'N', "Show notifies" },
518 {"user", 'u', POPT_ARG_STRING, &username, 'u', "Switch to user" },
519 {"brief", 'b', POPT_ARG_NONE, NULL, 'b', "Be brief" },
520 {"profile", 'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" },
521 {"profile-rates", 'R', POPT_ARG_NONE, NULL, 'R', "Show call rates" },
522 {"byterange", 'B', POPT_ARG_NONE, NULL, 'B', "Include byte range locks"},
523 {"numeric", 'n', POPT_ARG_NONE, NULL, 'n', "Numeric uid/gid"},
524 {"fast", 'f', POPT_ARG_NONE, NULL, 'f', "Skip checks if processes still exist"},
525 POPT_COMMON_SAMBA
526 POPT_TABLEEND
528 TALLOC_CTX *frame = talloc_stackframe();
529 int ret = 0;
530 struct messaging_context *msg_ctx = NULL;
531 char *db_path;
532 bool ok;
534 sec_init();
535 smb_init_locale();
537 setup_logging(argv[0], DEBUG_STDERR);
538 lp_set_cmdline("log level", "0");
540 if (getuid() != geteuid()) {
541 d_printf("smbstatus should not be run setuid\n");
542 ret = 1;
543 goto done;
546 if (getuid() != 0) {
547 d_printf("smbstatus only works as root!\n");
548 ret = 1;
549 goto done;
553 pc = poptGetContext(NULL, argc, argv, long_options,
554 POPT_CONTEXT_KEEP_FIRST);
556 while ((c = poptGetNextOpt(pc)) != -1) {
557 switch (c) {
558 case 'p':
559 processes_only = true;
560 break;
561 case 'v':
562 verbose = true;
563 break;
564 case 'L':
565 locks_only = true;
566 break;
567 case 'S':
568 shares_only = true;
569 break;
570 case 'N':
571 show_notify = true;
572 break;
573 case 'b':
574 brief = true;
575 break;
576 case 'u':
577 Ucrit_addUid(nametouid(poptGetOptArg(pc)));
578 break;
579 case 'P':
580 case 'R':
581 profile_only = c;
582 break;
583 case 'B':
584 show_brl = true;
585 break;
586 case 'n':
587 numeric_only = true;
588 break;
589 case 'f':
590 do_checks = false;
591 break;
595 /* setup the flags based on the possible combincations */
597 show_processes = !(shares_only || locks_only || profile_only) || processes_only;
598 show_locks = !(shares_only || processes_only || profile_only) || locks_only;
599 show_shares = !(processes_only || locks_only || profile_only) || shares_only;
601 if ( username )
602 Ucrit_addUid( nametouid(username) );
604 if (verbose) {
605 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
608 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
609 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
610 get_dyn_CONFIGFILE());
611 ret = -1;
612 goto done;
617 * This implicitly initializes the global ctdbd connection,
618 * usable by the db_open() calls further down.
620 msg_ctx = messaging_init(NULL, samba_tevent_context_init(NULL));
621 if (msg_ctx == NULL) {
622 fprintf(stderr, "messaging_init failed\n");
623 ret = -1;
624 goto done;
627 if (!lp_load_global(get_dyn_CONFIGFILE())) {
628 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
629 get_dyn_CONFIGFILE());
630 ret = -1;
631 goto done;
634 switch (profile_only) {
635 case 'P':
636 /* Dump profile data */
637 ok = status_profile_dump(verbose);
638 return ok ? 0 : 1;
639 case 'R':
640 /* Continuously display rate-converted data */
641 ok = status_profile_rates(verbose);
642 return ok ? 0 : 1;
643 default:
644 break;
647 if ( show_processes ) {
648 d_printf("\nSamba version %s\n",samba_version_string());
649 d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
650 d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
652 sessionid_traverse_read(traverse_sessionid, frame);
654 if (processes_only) {
655 goto done;
659 if ( show_shares ) {
660 if (brief) {
661 goto done;
664 d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing");
665 d_printf("---------------------------------------------------------------------------------------------\n");
667 connections_forall_read(traverse_connections, frame);
669 d_printf("\n");
671 if ( shares_only ) {
672 goto done;
676 if ( show_locks ) {
677 int result;
678 struct db_context *db;
680 db_path = lock_path("locking.tdb");
681 if (db_path == NULL) {
682 d_printf("Out of memory - exiting\n");
683 ret = -1;
684 goto done;
687 db = db_open(NULL, db_path, 0,
688 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0,
689 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
691 if (!db) {
692 d_printf("%s not initialised\n", db_path);
693 d_printf("This is normal if an SMB client has never "
694 "connected to your server.\n");
695 TALLOC_FREE(db_path);
696 exit(0);
697 } else {
698 TALLOC_FREE(db);
699 TALLOC_FREE(db_path);
702 if (!locking_init_readonly()) {
703 d_printf("Can't initialise locking module - exiting\n");
704 ret = 1;
705 goto done;
708 result = share_entry_forall(print_share_mode, NULL);
710 if (result == 0) {
711 d_printf("No locked files\n");
712 } else if (result < 0) {
713 d_printf("locked file list truncated\n");
716 d_printf("\n");
718 if (show_brl) {
719 brl_forall(print_brl, NULL);
722 locking_end();
725 if (show_notify) {
726 struct notify_context *n;
728 n = notify_init(talloc_tos(), msg_ctx,
729 messaging_tevent_context(msg_ctx),
730 NULL, NULL);
731 if (n == NULL) {
732 goto done;
734 notify_walk(n, print_notify_rec, NULL);
735 TALLOC_FREE(n);
738 done:
739 TALLOC_FREE(frame);
740 return ret;