winbind: Keep "force_reauth" in invalidate_cm_connection
[Samba.git] / source3 / utils / status.c
blobbeae85c4d3e4490faef6ce10baaf73ac7af2944f
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 struct file_id *id,
121 const char *sharepath,
122 const char *fname,
123 const char *sname,
124 void *dummy)
126 static int count;
128 if (do_checks && !is_valid_share_mode_entry(e)) {
129 return 0;
132 if (count==0) {
133 d_printf("Locked files:\n");
134 d_printf("Pid Uid DenyMode Access R/W Oplock SharePath Name Time\n");
135 d_printf("--------------------------------------------------------------------------------------------------\n");
137 count++;
139 if (do_checks && !serverid_exists(&e->pid)) {
140 /* the process for this entry does not exist any more */
141 return 0;
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;
156 default: {
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 );
162 break;
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)) {
168 d_printf("RDWR ");
169 } else if (e->access_mask & FILE_WRITE_DATA) {
170 d_printf("WRONLY ");
171 } else {
172 d_printf("RDONLY ");
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) {
181 d_printf("BATCH ");
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)?"":" ");
193 } else {
194 d_printf("NONE ");
197 d_printf(" %s %s%s %s",
198 sharepath, fname,
199 sname ? sname : "",
200 time_to_asc((time_t)e->time.tv_sec));
203 return 0;
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,
210 br_off start,
211 br_off size,
212 void *private_data)
214 static int count;
215 unsigned int i;
216 static const struct {
217 enum brl_type lock_type;
218 const char *desc;
219 } lock_types[] = {
220 { READ_LOCK, "R" },
221 { WRITE_LOCK, "W" },
222 { PENDING_READ_LOCK, "PR" },
223 { PENDING_WRITE_LOCK, "PW" },
224 { UNLOCK_LOCK, "U" }
226 const char *desc="X";
227 const char *sharepath = "";
228 char *fname = NULL;
229 struct share_mode_lock *share_mode;
230 struct server_id_buf tmp;
232 if (count==0) {
233 d_printf("Byte range locks:\n");
234 d_printf("Pid dev:inode R/W start size SharePath Name\n");
235 d_printf("--------------------------------------------------------------------------------\n");
237 count++;
239 share_mode = fetch_share_mode_unlocked(NULL, id);
240 if (share_mode) {
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 ? ":" : "",
246 has_stream ?
247 share_mode->data->stream_name :
248 "");
249 } else {
250 fname = talloc_strdup(NULL, "");
251 if (fname == NULL) {
252 return;
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),
264 desc,
265 (intmax_t)start, (intmax_t)size,
266 sharepath, fname);
268 TALLOC_FREE(fname);
269 TALLOC_FREE(share_mode);
272 static const char *session_dialect_str(uint16_t dialect)
274 static fstring unkown_dialect;
276 switch(dialect){
277 case SMB2_DIALECT_REVISION_000:
278 return "NT1";
279 case SMB2_DIALECT_REVISION_202:
280 return "SMB2_02";
281 case SMB2_DIALECT_REVISION_210:
282 return "SMB2_10";
283 case SMB2_DIALECT_REVISION_222:
284 return "SMB2_22";
285 case SMB2_DIALECT_REVISION_224:
286 return "SMB2_24";
287 case SMB3_DIALECT_REVISION_300:
288 return "SMB3_00";
289 case SMB3_DIALECT_REVISION_302:
290 return "SMB3_02";
291 case SMB3_DIALECT_REVISION_310:
292 return "SMB3_10";
293 case SMB3_DIALECT_REVISION_311:
294 return "SMB3_11";
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,
303 void *private_data)
305 TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
306 struct server_id_buf tmp;
307 char *timestr = NULL;
308 int result = 0;
309 const char *encryption = "-";
310 const char *signing = "-";
312 if (crec->cnum == TID_FIELD_INVALID)
313 return 0;
315 if (do_checks &&
316 (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid))) {
317 return 0;
320 timestr = timestring(mem_ctx, crec->start);
321 if (timestr == NULL) {
322 return -1;
325 if (smbXsrv_is_encrypted(crec->encryption_flags)) {
326 switch (crec->cipher) {
327 case SMB_ENCRYPTION_GSSAPI:
328 encryption = "GSSAPI";
329 break;
330 case SMB2_ENCRYPTION_AES128_CCM:
331 encryption = "AES-128-CCM";
332 break;
333 case SMB2_ENCRYPTION_AES128_GCM:
334 encryption = "AES-128-GCM";
335 break;
336 default:
337 encryption = "???";
338 result = -1;
339 break;
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";
348 } else {
349 signing = "HMAC-MD5";
353 d_printf("%-12s %-7s %-13s %-32s %-12s %-12s\n",
354 crec->servicename, server_id_str_buf(crec->pid, &tmp),
355 crec->machine,
356 timestr,
357 encryption,
358 signing);
360 TALLOC_FREE(timestr);
362 return result;
365 static int traverse_sessionid(const char *key, struct sessionid *session,
366 void *private_data)
368 TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
369 fstring uid_gid_str;
370 struct server_id_buf tmp;
371 char *machine_hostname = NULL;
372 int result = 0;
373 const char *encryption = "-";
374 const char *signing = "-";
376 if (do_checks &&
377 (!process_exists(session->pid) ||
378 !Ucrit_checkUid(session->uid))) {
379 return 0;
382 Ucrit_addPid(session->pid);
384 if (numeric_only) {
385 fstr_sprintf(uid_gid_str, "%-12u %-12u",
386 (unsigned int)session->uid,
387 (unsigned int)session->gid);
388 } else {
389 if (session->uid == -1 && session->gid == -1) {
391 * The session is not fully authenticated yet.
393 fstrcpy(uid_gid_str, "(auth in progress)");
394 } else {
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) {
407 return -1;
410 if (session->gid != -1) {
411 gid_name = gidtoname(session->gid);
412 if (gid_name == NULL) {
413 return -1;
416 fstr_sprintf(uid_gid_str, "%-12s %-12s",
417 uid_name, gid_name);
421 machine_hostname = talloc_asprintf(mem_ctx, "%s (%s)",
422 session->remote_machine,
423 session->hostname);
424 if (machine_hostname == NULL) {
425 return -1;
428 if (smbXsrv_is_encrypted(session->encryption_flags)) {
429 switch (session->cipher) {
430 case SMB2_ENCRYPTION_AES128_CCM:
431 encryption = "AES-128-CCM";
432 break;
433 case SMB2_ENCRYPTION_AES128_GCM:
434 encryption = "AES-128-GCM";
435 break;
436 default:
437 encryption = "???";
438 result = -1;
439 break;
441 } else if (smbXsrv_is_partially_encrypted(session->encryption_flags)) {
442 switch (session->cipher) {
443 case SMB_ENCRYPTION_GSSAPI:
444 encryption = "partial(GSSAPI)";
445 break;
446 case SMB2_ENCRYPTION_AES128_CCM:
447 encryption = "partial(AES-128-CCM)";
448 break;
449 case SMB2_ENCRYPTION_AES128_GCM:
450 encryption = "partial(AES-128-GCM)";
451 break;
452 default:
453 encryption = "???";
454 result = -1;
455 break;
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";
464 } else {
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)";
472 } else {
473 signing = "partial(HMAC-MD5)";
478 d_printf("%-7s %-25s %-41s %-17s %-20s %-21s\n",
479 server_id_str_buf(session->pid, &tmp),
480 uid_gid_str,
481 machine_hostname,
482 session_dialect_str(session->connection_dialect),
483 encryption,
484 signing);
486 TALLOC_FREE(machine_hostname);
488 return result;
492 static bool print_notify_rec(const char *path, struct server_id server,
493 const struct notify_instance *instance,
494 void *private_data)
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);
502 return true;
505 int main(int argc, const char *argv[])
507 int c;
508 int profile_only = 0;
509 bool show_processes, show_locks, show_shares;
510 bool show_notify = false;
511 poptContext pc;
512 struct poptOption long_options[] = {
513 POPT_AUTOHELP
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"},
526 POPT_COMMON_SAMBA
527 POPT_TABLEEND
529 TALLOC_CTX *frame = talloc_stackframe();
530 int ret = 0;
531 struct messaging_context *msg_ctx = NULL;
532 char *db_path;
533 bool ok;
535 sec_init();
536 smb_init_locale();
538 setup_logging(argv[0], DEBUG_STDERR);
539 lp_set_cmdline("log level", "0");
541 if (getuid() != geteuid()) {
542 d_printf("smbstatus should not be run setuid\n");
543 ret = 1;
544 goto done;
547 if (getuid() != 0) {
548 d_printf("smbstatus only works as root!\n");
549 ret = 1;
550 goto done;
554 pc = poptGetContext(NULL, argc, argv, long_options,
555 POPT_CONTEXT_KEEP_FIRST);
557 while ((c = poptGetNextOpt(pc)) != -1) {
558 switch (c) {
559 case 'p':
560 processes_only = true;
561 break;
562 case 'v':
563 verbose = true;
564 break;
565 case 'L':
566 locks_only = true;
567 break;
568 case 'S':
569 shares_only = true;
570 break;
571 case 'N':
572 show_notify = true;
573 break;
574 case 'b':
575 brief = true;
576 break;
577 case 'u':
578 Ucrit_addUid(nametouid(poptGetOptArg(pc)));
579 break;
580 case 'P':
581 case 'R':
582 profile_only = c;
583 break;
584 case 'B':
585 show_brl = true;
586 break;
587 case 'n':
588 numeric_only = true;
589 break;
590 case 'f':
591 do_checks = false;
592 break;
596 /* setup the flags based on the possible combincations */
598 show_processes = !(shares_only || locks_only || profile_only) || processes_only;
599 show_locks = !(shares_only || processes_only || profile_only) || locks_only;
600 show_shares = !(processes_only || locks_only || profile_only) || shares_only;
602 if ( username )
603 Ucrit_addUid( nametouid(username) );
605 if (verbose) {
606 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
609 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
610 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
611 get_dyn_CONFIGFILE());
612 ret = -1;
613 goto done;
618 * This implicitly initializes the global ctdbd connection,
619 * usable by the db_open() calls further down.
621 msg_ctx = messaging_init(NULL, samba_tevent_context_init(NULL));
622 if (msg_ctx == NULL) {
623 fprintf(stderr, "messaging_init failed\n");
624 ret = -1;
625 goto done;
628 if (!lp_load_global(get_dyn_CONFIGFILE())) {
629 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
630 get_dyn_CONFIGFILE());
631 ret = -1;
632 goto done;
635 switch (profile_only) {
636 case 'P':
637 /* Dump profile data */
638 ok = status_profile_dump(verbose);
639 return ok ? 0 : 1;
640 case 'R':
641 /* Continuously display rate-converted data */
642 ok = status_profile_rates(verbose);
643 return ok ? 0 : 1;
644 default:
645 break;
648 if ( show_processes ) {
649 d_printf("\nSamba version %s\n",samba_version_string());
650 d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
651 d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
653 sessionid_traverse_read(traverse_sessionid, frame);
655 if (processes_only) {
656 goto done;
660 if ( show_shares ) {
661 if (brief) {
662 goto done;
665 d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing");
666 d_printf("---------------------------------------------------------------------------------------------\n");
668 connections_forall_read(traverse_connections, frame);
670 d_printf("\n");
672 if ( shares_only ) {
673 goto done;
677 if ( show_locks ) {
678 int result;
679 struct db_context *db;
681 db_path = lock_path("locking.tdb");
682 if (db_path == NULL) {
683 d_printf("Out of memory - exiting\n");
684 ret = -1;
685 goto done;
688 db = db_open(NULL, db_path, 0,
689 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0,
690 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
692 if (!db) {
693 d_printf("%s not initialised\n", db_path);
694 d_printf("This is normal if an SMB client has never "
695 "connected to your server.\n");
696 TALLOC_FREE(db_path);
697 exit(0);
698 } else {
699 TALLOC_FREE(db);
700 TALLOC_FREE(db_path);
703 if (!locking_init_readonly()) {
704 d_printf("Can't initialise locking module - exiting\n");
705 ret = 1;
706 goto done;
709 result = share_entry_forall(print_share_mode, NULL);
711 if (result == 0) {
712 d_printf("No locked files\n");
713 } else if (result < 0) {
714 d_printf("locked file list truncated\n");
717 d_printf("\n");
719 if (show_brl) {
720 brl_forall(print_brl, NULL);
723 locking_end();
726 if (show_notify) {
727 struct notify_context *n;
729 n = notify_init(talloc_tos(), msg_ctx,
730 messaging_tevent_context(msg_ctx),
731 NULL, NULL);
732 if (n == NULL) {
733 goto done;
735 notify_walk(n, print_notify_rec, NULL);
736 TALLOC_FREE(n);
739 done:
740 TALLOC_FREE(frame);
741 return ret;