utils: Move conn_tdb.c to utils/
[samba.git] / source3 / utils / status.c
1 /* 
2    Unix SMB/CIFS implementation.
3    status reporting
4    Copyright (C) Andrew Tridgell 1994-1998
5
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.
10
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.
15
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/>.
18
19    Revision History:
20
21    12 aug 96: Erik.Devriendt@te6.siemens.be
22    added support for shared memory implementation of share mode locking
23
24    21-Jul-1998: rsharpe@ns.aus.com (Richard Sharpe)
25    Added -L (locks only) -S (shares only) flags and code
26
27 */
28
29 /*
30  * This program reports current SMB connections
31  */
32
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 "conn_tdb.h"
48 #include "serverid.h"
49 #include "status_profile.h"
50 #include "smbd/notifyd/notifyd.h"
51 #include "cmdline_contexts.h"
52
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 */
58
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;
63 static bool show_brl;
64 static bool numeric_only;
65 static bool do_checks = true;
66
67 const char *username = NULL;
68
69 /* added by OH */
70 static void Ucrit_addUid(uid_t uid)
71 {
72         Ucrit_uid = uid;
73         Ucrit_IsActive = 1;
74 }
75
76 static unsigned int Ucrit_checkUid(uid_t uid)
77 {
78         if ( !Ucrit_IsActive ) 
79                 return 1;
80
81         if ( uid == Ucrit_uid ) 
82                 return 1;
83
84         return 0;
85 }
86
87 static unsigned int Ucrit_checkPid(struct server_id pid)
88 {
89         int i;
90
91         if ( !Ucrit_IsActive ) 
92                 return 1;
93
94         for (i=0;i<Ucrit_MaxPid;i++) {
95                 if (serverid_equal(&pid, &Ucrit_pid[i])) {
96                         return 1;
97                 }
98         }
99
100         return 0;
101 }
102
103 static bool Ucrit_addPid( struct server_id pid )
104 {
105         if ( !Ucrit_IsActive )
106                 return True;
107
108         if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
109                 d_printf("ERROR: More than %d pids for user %s!\n",
110                          SMB_MAXPIDS, uidtoname(Ucrit_uid));
111
112                 return False;
113         }
114
115         Ucrit_pid[Ucrit_MaxPid++] = pid;
116
117         return True;
118 }
119
120 static int print_share_mode(struct file_id fid,
121                             const struct share_mode_data *d,
122                             const struct share_mode_entry *e,
123                             void *private_data)
124 {
125         bool resolve_uids = *((bool *)private_data);
126         static int count;
127
128         if (do_checks && !is_valid_share_mode_entry(e)) {
129                 return 0;
130         }
131
132         if (count==0) {
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");
136         }
137         count++;
138
139         if (do_checks && !serverid_exists(&e->pid)) {
140                 /* the process for this entry does not exist any more */
141                 return 0;
142         }
143
144         if (Ucrit_checkPid(e->pid)) {
145                 struct server_id_buf tmp;
146                 d_printf("%-11s  ", server_id_str_buf(e->pid, &tmp));
147                 if (resolve_uids) {
148                         d_printf("%-14s  ", uidtoname(e->uid));
149                 } else {
150                         d_printf("%-9u  ", (unsigned int)e->uid);
151                 }
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;
160                         default: {
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 );
166                                 break;
167                         }
168                 }
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)) {
172                         d_printf("RDWR       ");
173                 } else if (e->access_mask & FILE_WRITE_DATA) {
174                         d_printf("WRONLY     ");
175                 } else {
176                         d_printf("RDONLY     ");
177                 }
178
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) {
185                         d_printf("BATCH           ");
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)?"":" ");
198                 } else {
199                         d_printf("NONE            ");
200                 }
201
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));
206         }
207
208         return 0;
209 }
210
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,
215                         br_off start,
216                         br_off size,
217                         void *private_data)
218 {
219         static int count;
220         unsigned int i;
221         static const struct {
222                 enum brl_type lock_type;
223                 const char *desc;
224         } lock_types[] = {
225                 { READ_LOCK, "R" },
226                 { WRITE_LOCK, "W" },
227                 { PENDING_READ_LOCK, "PR" },
228                 { PENDING_WRITE_LOCK, "PW" },
229                 { UNLOCK_LOCK, "U" }
230         };
231         const char *desc="X";
232         const char *sharepath = "";
233         char *fname = NULL;
234         struct share_mode_lock *share_mode;
235         struct server_id_buf tmp;
236
237         if (count==0) {
238                 d_printf("Byte range locks:\n");
239                 d_printf("Pid        dev:inode       R/W  start     size      SharePath               Name\n");
240                 d_printf("--------------------------------------------------------------------------------\n");
241         }
242         count++;
243
244         share_mode = fetch_share_mode_unlocked(NULL, id);
245         if (share_mode) {
246                 bool has_stream = share_mode->data->stream_name != NULL;
247
248                 fname = talloc_asprintf(NULL, "%s%s%s",
249                                         share_mode->data->base_name,
250                                         has_stream ? ":" : "",
251                                         has_stream ?
252                                         share_mode->data->stream_name :
253                                         "");
254         } else {
255                 fname = talloc_strdup(NULL, "");
256                 if (fname == NULL) {
257                         return;
258                 }
259         }
260
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;
264                 }
265         }
266
267         d_printf("%-10s %-15s %-4s %-9jd %-9jd %-24s %-24s\n",
268                  server_id_str_buf(pid, &tmp), file_id_string_tos(&id),
269                  desc,
270                  (intmax_t)start, (intmax_t)size,
271                  sharepath, fname);
272
273         TALLOC_FREE(fname);
274         TALLOC_FREE(share_mode);
275 }
276
277 static const char *session_dialect_str(uint16_t dialect)
278 {
279         static fstring unkown_dialect;
280
281         switch(dialect){
282         case SMB2_DIALECT_REVISION_000:
283                 return "NT1";
284         case SMB2_DIALECT_REVISION_202:
285                 return "SMB2_02";
286         case SMB2_DIALECT_REVISION_210:
287                 return "SMB2_10";
288         case SMB2_DIALECT_REVISION_222:
289                 return "SMB2_22";
290         case SMB2_DIALECT_REVISION_224:
291                 return "SMB2_24";
292         case SMB3_DIALECT_REVISION_300:
293                 return "SMB3_00";
294         case SMB3_DIALECT_REVISION_302:
295                 return "SMB3_02";
296         case SMB3_DIALECT_REVISION_310:
297                 return "SMB3_10";
298         case SMB3_DIALECT_REVISION_311:
299                 return "SMB3_11";
300         }
301
302         fstr_sprintf(unkown_dialect, "Unknown (0x%04x)", dialect);
303         return unkown_dialect;
304 }
305
306 static int traverse_connections(const struct connections_key *key,
307                                 const struct connections_data *crec,
308                                 void *private_data)
309 {
310         TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
311         struct server_id_buf tmp;
312         char *timestr = NULL;
313         int result = 0;
314         const char *encryption = "-";
315         const char *signing = "-";
316
317         if (crec->cnum == TID_FIELD_INVALID)
318                 return 0;
319
320         if (do_checks &&
321             (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid))) {
322                 return 0;
323         }
324
325         timestr = timestring(mem_ctx, crec->start);
326         if (timestr == NULL) {
327                 return -1;
328         }
329
330         if (smbXsrv_is_encrypted(crec->encryption_flags)) {
331                 switch (crec->cipher) {
332                 case SMB_ENCRYPTION_GSSAPI:
333                         encryption = "GSSAPI";
334                         break;
335                 case SMB2_ENCRYPTION_AES128_CCM:
336                         encryption = "AES-128-CCM";
337                         break;
338                 case SMB2_ENCRYPTION_AES128_GCM:
339                         encryption = "AES-128-GCM";
340                         break;
341                 default:
342                         encryption = "???";
343                         result = -1;
344                         break;
345                 }
346         }
347
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";
353                 } else {
354                         signing = "HMAC-MD5";
355                 }
356         }
357
358         d_printf("%-12s %-7s %-13s %-32s %-12s %-12s\n",
359                  crec->servicename, server_id_str_buf(crec->pid, &tmp),
360                  crec->machine,
361                  timestr,
362                  encryption,
363                  signing);
364
365         TALLOC_FREE(timestr);
366
367         return result;
368 }
369
370 static int traverse_sessionid(const char *key, struct sessionid *session,
371                               void *private_data)
372 {
373         TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
374         fstring uid_gid_str;
375         struct server_id_buf tmp;
376         char *machine_hostname = NULL;
377         int result = 0;
378         const char *encryption = "-";
379         const char *signing = "-";
380
381         if (do_checks &&
382             (!process_exists(session->pid) ||
383              !Ucrit_checkUid(session->uid))) {
384                 return 0;
385         }
386
387         Ucrit_addPid(session->pid);
388
389         if (numeric_only) {
390                 fstr_sprintf(uid_gid_str, "%-12u %-12u",
391                              (unsigned int)session->uid,
392                              (unsigned int)session->gid);
393         } else {
394                 if (session->uid == -1 && session->gid == -1) {
395                         /*
396                          * The session is not fully authenticated yet.
397                          */
398                         fstrcpy(uid_gid_str, "(auth in progress)");
399                 } else {
400                         /*
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.
405                          */
406                         const char *uid_name = "-1";
407                         const char *gid_name = "-1";
408
409                         if (session->uid != -1) {
410                                 uid_name = uidtoname(session->uid);
411                                 if (uid_name == NULL) {
412                                         return -1;
413                                 }
414                         }
415                         if (session->gid != -1) {
416                                 gid_name = gidtoname(session->gid);
417                                 if (gid_name == NULL) {
418                                         return -1;
419                                 }
420                         }
421                         fstr_sprintf(uid_gid_str, "%-12s %-12s",
422                                      uid_name, gid_name);
423                 }
424         }
425
426         machine_hostname = talloc_asprintf(mem_ctx, "%s (%s)",
427                                            session->remote_machine,
428                                            session->hostname);
429         if (machine_hostname == NULL) {
430                 return -1;
431         }
432
433         if (smbXsrv_is_encrypted(session->encryption_flags)) {
434                 switch (session->cipher) {
435                 case SMB2_ENCRYPTION_AES128_CCM:
436                         encryption = "AES-128-CCM";
437                         break;
438                 case SMB2_ENCRYPTION_AES128_GCM:
439                         encryption = "AES-128-GCM";
440                         break;
441                 default:
442                         encryption = "???";
443                         result = -1;
444                         break;
445                 }
446         } else if (smbXsrv_is_partially_encrypted(session->encryption_flags)) {
447                 switch (session->cipher) {
448                 case SMB_ENCRYPTION_GSSAPI:
449                         encryption = "partial(GSSAPI)";
450                         break;
451                 case SMB2_ENCRYPTION_AES128_CCM:
452                         encryption = "partial(AES-128-CCM)";
453                         break;
454                 case SMB2_ENCRYPTION_AES128_GCM:
455                         encryption = "partial(AES-128-GCM)";
456                         break;
457                 default:
458                         encryption = "???";
459                         result = -1;
460                         break;
461                 }
462         }
463
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";
469                 } else {
470                         signing = "HMAC-MD5";
471                 }
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)";
477                 } else {
478                         signing = "partial(HMAC-MD5)";
479                 }
480         }
481
482
483         d_printf("%-7s %-25s %-41s %-17s %-20s %-21s\n",
484                  server_id_str_buf(session->pid, &tmp),
485                  uid_gid_str,
486                  machine_hostname,
487                  session_dialect_str(session->connection_dialect),
488                  encryption,
489                  signing);
490
491         TALLOC_FREE(machine_hostname);
492
493         return result;
494 }
495
496
497 static bool print_notify_rec(const char *path, struct server_id server,
498                              const struct notify_instance *instance,
499                              void *private_data)
500 {
501         struct server_id_buf idbuf;
502
503         d_printf("%s\\%s\\%x\\%x\n", path, server_id_str_buf(server, &idbuf),
504                  (unsigned)instance->filter,
505                  (unsigned)instance->subdir_filter);
506
507         return true;
508 }
509
510 enum {
511         OPT_RESOLVE_UIDS = 1000,
512 };
513
514 int main(int argc, const char *argv[])
515 {
516         int c;
517         int profile_only = 0;
518         bool show_processes, show_locks, show_shares;
519         bool show_notify = false;
520         bool resolve_uids = false;
521         poptContext pc;
522         struct poptOption long_options[] = {
523                 POPT_AUTOHELP
524                 {
525                         .longName   = "processes",
526                         .shortName  = 'p',
527                         .argInfo    = POPT_ARG_NONE,
528                         .arg        = NULL,
529                         .val        = 'p',
530                         .descrip    = "Show processes only",
531                 },
532                 {
533                         .longName   = "verbose",
534                         .shortName  = 'v',
535                         .argInfo    = POPT_ARG_NONE,
536                         .arg        = NULL,
537                         .val        = 'v',
538                         .descrip    = "Be verbose",
539                 },
540                 {
541                         .longName   = "locks",
542                         .shortName  = 'L',
543                         .argInfo    = POPT_ARG_NONE,
544                         .arg        = NULL,
545                         .val        = 'L',
546                         .descrip    = "Show locks only",
547                 },
548                 {
549                         .longName   = "shares",
550                         .shortName  = 'S',
551                         .argInfo    = POPT_ARG_NONE,
552                         .arg        = NULL,
553                         .val        = 'S',
554                         .descrip    = "Show shares only",
555                 },
556                 {
557                         .longName   = "notify",
558                         .shortName  = 'N',
559                         .argInfo    = POPT_ARG_NONE,
560                         .arg        = NULL,
561                         .val        = 'N',
562                         .descrip    = "Show notifies",
563                 },
564                 {
565                         .longName   = "user",
566                         .shortName  = 'u',
567                         .argInfo    = POPT_ARG_STRING,
568                         .arg        = &username,
569                         .val        = 'u',
570                         .descrip    = "Switch to user",
571                 },
572                 {
573                         .longName   = "brief",
574                         .shortName  = 'b',
575                         .argInfo    = POPT_ARG_NONE,
576                         .arg        = NULL,
577                         .val        = 'b',
578                         .descrip    = "Be brief",
579                 },
580                 {
581                         .longName   = "profile",
582                         .shortName  =     'P',
583                         .argInfo    = POPT_ARG_NONE,
584                         .arg        = NULL,
585                         .val        = 'P',
586                         .descrip    = "Do profiling",
587                 },
588                 {
589                         .longName   = "profile-rates",
590                         .shortName  = 'R',
591                         .argInfo    = POPT_ARG_NONE,
592                         .arg        = NULL,
593                         .val        = 'R',
594                         .descrip    = "Show call rates",
595                 },
596                 {
597                         .longName   = "byterange",
598                         .shortName  = 'B',
599                         .argInfo    = POPT_ARG_NONE,
600                         .arg        = NULL,
601                         .val        = 'B',
602                         .descrip    = "Include byte range locks"
603                 },
604                 {
605                         .longName   = "numeric",
606                         .shortName  = 'n',
607                         .argInfo    = POPT_ARG_NONE,
608                         .arg        = NULL,
609                         .val        = 'n',
610                         .descrip    = "Numeric uid/gid"
611                 },
612                 {
613                         .longName   = "fast",
614                         .shortName  = 'f',
615                         .argInfo    = POPT_ARG_NONE,
616                         .arg        = NULL,
617                         .val        = 'f',
618                         .descrip    = "Skip checks if processes still exist"
619                 },
620                 {
621                         .longName   = "resolve-uids",
622                         .shortName  = 0,
623                         .argInfo    = POPT_ARG_NONE,
624                         .arg        = NULL,
625                         .val        = OPT_RESOLVE_UIDS,
626                         .descrip    = "Try to resolve UIDs to usernames"
627                 },
628                 POPT_COMMON_SAMBA
629                 POPT_TABLEEND
630         };
631         TALLOC_CTX *frame = talloc_stackframe();
632         int ret = 0;
633         struct messaging_context *msg_ctx = NULL;
634         char *db_path;
635         bool ok;
636
637         sec_init();
638         smb_init_locale();
639
640         setup_logging(argv[0], DEBUG_STDERR);
641         lp_set_cmdline("log level", "0");
642
643         if (getuid() != geteuid()) {
644                 d_printf("smbstatus should not be run setuid\n");
645                 ret = 1;
646                 goto done;
647         }
648
649         if (getuid() != 0) {
650                 d_printf("smbstatus only works as root!\n");
651                 ret = 1;
652                 goto done;
653         }
654
655
656         pc = poptGetContext(NULL, argc, argv, long_options,
657                             POPT_CONTEXT_KEEP_FIRST);
658
659         while ((c = poptGetNextOpt(pc)) != -1) {
660                 switch (c) {
661                 case 'p':
662                         processes_only = true;
663                         break;
664                 case 'v':
665                         verbose = true;
666                         break;
667                 case 'L':
668                         locks_only = true;
669                         break;
670                 case 'S':
671                         shares_only = true;
672                         break;
673                 case 'N':
674                         show_notify = true;
675                         break;
676                 case 'b':
677                         brief = true;
678                         break;
679                 case 'u':
680                         Ucrit_addUid(nametouid(poptGetOptArg(pc)));
681                         break;
682                 case 'P':
683                 case 'R':
684                         profile_only = c;
685                         break;
686                 case 'B':
687                         show_brl = true;
688                         break;
689                 case 'n':
690                         numeric_only = true;
691                         break;
692                 case 'f':
693                         do_checks = false;
694                         break;
695                 case OPT_RESOLVE_UIDS:
696                         resolve_uids = true;
697                         break;
698                 }
699         }
700
701         /* setup the flags based on the possible combincations */
702
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;
706
707         if ( username )
708                 Ucrit_addUid( nametouid(username) );
709
710         if (verbose) {
711                 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
712         }
713
714         msg_ctx = cmdline_messaging_context(get_dyn_CONFIGFILE());
715         if (msg_ctx == NULL) {
716                 fprintf(stderr, "Could not initialize messaging, not root?\n");
717                 ret = -1;
718                 goto done;
719         }
720
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());
724                 ret = -1;
725                 goto done;
726         }
727
728         switch (profile_only) {
729                 case 'P':
730                         /* Dump profile data */
731                         ok = status_profile_dump(verbose);
732                         return ok ? 0 : 1;
733                 case 'R':
734                         /* Continuously display rate-converted data */
735                         ok = status_profile_rates(verbose);
736                         return ok ? 0 : 1;
737                 default:
738                         break;
739         }
740
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");
745
746                 sessionid_traverse_read(traverse_sessionid, frame);
747
748                 if (processes_only) {
749                         goto done;
750                 }
751         }
752
753         if ( show_shares ) {
754                 if (brief) {
755                         goto done;
756                 }
757
758                 d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing");
759                 d_printf("---------------------------------------------------------------------------------------------\n");
760
761                 connections_forall_read(traverse_connections, frame);
762
763                 d_printf("\n");
764
765                 if ( shares_only ) {
766                         goto done;
767                 }
768         }
769
770         if ( show_locks ) {
771                 int result;
772                 struct db_context *db;
773
774                 db_path = lock_path(talloc_tos(), "locking.tdb");
775                 if (db_path == NULL) {
776                         d_printf("Out of memory - exiting\n");
777                         ret = -1;
778                         goto done;
779                 }
780
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);
784
785                 if (!db) {
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);
790                         exit(0);
791                 } else {
792                         TALLOC_FREE(db);
793                         TALLOC_FREE(db_path);
794                 }
795
796                 if (!locking_init_readonly()) {
797                         d_printf("Can't initialise locking module - exiting\n");
798                         ret = 1;
799                         goto done;
800                 }
801
802                 result = share_entry_forall(print_share_mode, &resolve_uids);
803
804                 if (result == 0) {
805                         d_printf("No locked files\n");
806                 } else if (result < 0) {
807                         d_printf("locked file list truncated\n");
808                 }
809
810                 d_printf("\n");
811
812                 if (show_brl) {
813                         brl_forall(print_brl, NULL);
814                 }
815
816                 locking_end();
817         }
818
819         if (show_notify) {
820                 struct notify_context *n;
821
822                 n = notify_init(talloc_tos(), msg_ctx,
823                                 NULL, NULL);
824                 if (n == NULL) {
825                         goto done;
826                 }
827                 notify_walk(n, print_notify_rec, NULL);
828                 TALLOC_FREE(n);
829         }
830
831 done:
832         TALLOC_FREE(frame);
833         return ret;
834 }