utils: Use leases_db in smbstatus
[amitay/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 #include "locking/leases_db.h"
53
54 #define SMB_MAXPIDS             2048
55 static uid_t            Ucrit_uid = 0;               /* added by OH */
56 static struct server_id Ucrit_pid[SMB_MAXPIDS];  /* Ugly !!! */   /* added by OH */
57 static int              Ucrit_MaxPid=0;                    /* added by OH */
58 static unsigned int     Ucrit_IsActive = 0;                /* added by OH */
59
60 static bool verbose, brief;
61 static bool shares_only;            /* Added by RJS */
62 static bool locks_only;            /* Added by RJS */
63 static bool processes_only;
64 static bool show_brl;
65 static bool numeric_only;
66 static bool do_checks = true;
67
68 const char *username = NULL;
69
70 /* added by OH */
71 static void Ucrit_addUid(uid_t uid)
72 {
73         Ucrit_uid = uid;
74         Ucrit_IsActive = 1;
75 }
76
77 static unsigned int Ucrit_checkUid(uid_t uid)
78 {
79         if ( !Ucrit_IsActive ) 
80                 return 1;
81
82         if ( uid == Ucrit_uid ) 
83                 return 1;
84
85         return 0;
86 }
87
88 static unsigned int Ucrit_checkPid(struct server_id pid)
89 {
90         int i;
91
92         if ( !Ucrit_IsActive ) 
93                 return 1;
94
95         for (i=0;i<Ucrit_MaxPid;i++) {
96                 if (serverid_equal(&pid, &Ucrit_pid[i])) {
97                         return 1;
98                 }
99         }
100
101         return 0;
102 }
103
104 static bool Ucrit_addPid( struct server_id pid )
105 {
106         if ( !Ucrit_IsActive )
107                 return True;
108
109         if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
110                 d_printf("ERROR: More than %d pids for user %s!\n",
111                          SMB_MAXPIDS, uidtoname(Ucrit_uid));
112
113                 return False;
114         }
115
116         Ucrit_pid[Ucrit_MaxPid++] = pid;
117
118         return True;
119 }
120
121 static int print_share_mode(struct file_id fid,
122                             const struct share_mode_data *d,
123                             const struct share_mode_entry *e,
124                             void *private_data)
125 {
126         bool resolve_uids = *((bool *)private_data);
127         static int count;
128
129         if (do_checks && !is_valid_share_mode_entry(e)) {
130                 return 0;
131         }
132
133         if (count==0) {
134                 d_printf("Locked files:\n");
135                 d_printf("Pid          User(ID)   DenyMode   Access      R/W        Oplock           SharePath   Name   Time\n");
136                 d_printf("--------------------------------------------------------------------------------------------------\n");
137         }
138         count++;
139
140         if (do_checks && !serverid_exists(&e->pid)) {
141                 /* the process for this entry does not exist any more */
142                 return 0;
143         }
144
145         if (Ucrit_checkPid(e->pid)) {
146                 struct server_id_buf tmp;
147                 d_printf("%-11s  ", server_id_str_buf(e->pid, &tmp));
148                 if (resolve_uids) {
149                         d_printf("%-14s  ", uidtoname(e->uid));
150                 } else {
151                         d_printf("%-9u  ", (unsigned int)e->uid);
152                 }
153                 switch (map_share_mode_to_deny_mode(e->share_access,
154                                                     e->private_options)) {
155                         case DENY_NONE: d_printf("DENY_NONE  "); break;
156                         case DENY_ALL:  d_printf("DENY_ALL   "); break;
157                         case DENY_DOS:  d_printf("DENY_DOS   "); break;
158                         case DENY_READ: d_printf("DENY_READ  "); break;
159                         case DENY_WRITE:printf("DENY_WRITE "); break;
160                         case DENY_FCB:  d_printf("DENY_FCB "); break;
161                         default: {
162                                 d_printf("unknown-please report ! "
163                                          "e->share_access = 0x%x, "
164                                          "e->private_options = 0x%x\n",
165                                          (unsigned int)e->share_access,
166                                          (unsigned int)e->private_options );
167                                 break;
168                         }
169                 }
170                 d_printf("0x%-8x  ",(unsigned int)e->access_mask);
171                 if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
172                                 (FILE_READ_DATA|FILE_WRITE_DATA)) {
173                         d_printf("RDWR       ");
174                 } else if (e->access_mask & FILE_WRITE_DATA) {
175                         d_printf("WRONLY     ");
176                 } else {
177                         d_printf("RDONLY     ");
178                 }
179
180                 if((e->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
181                                         (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) {
182                         d_printf("EXCLUSIVE+BATCH ");
183                 } else if (e->op_type & EXCLUSIVE_OPLOCK) {
184                         d_printf("EXCLUSIVE       ");
185                 } else if (e->op_type & BATCH_OPLOCK) {
186                         d_printf("BATCH           ");
187                 } else if (e->op_type & LEVEL_II_OPLOCK) {
188                         d_printf("LEVEL_II        ");
189                 } else if (e->op_type == LEASE_OPLOCK) {
190                         NTSTATUS status;
191                         uint32_t lstate;
192
193                         status = leases_db_get(
194                                 &e->client_guid,
195                                 &e->lease_key,
196                                 &d->id,
197                                 &lstate, /* current_state */
198                                 NULL, /* breaking */
199                                 NULL, /* breaking_to_requested */
200                                 NULL, /* breaking_to_required */
201                                 NULL, /* lease_version */
202                                 NULL); /* epoch */
203
204                         if (NT_STATUS_IS_OK(status)) {
205                                 d_printf("LEASE(%s%s%s)%s%s%s      ",
206                                          (lstate & SMB2_LEASE_READ)?"R":"",
207                                          (lstate & SMB2_LEASE_WRITE)?"W":"",
208                                          (lstate & SMB2_LEASE_HANDLE)?"H":"",
209                                          (lstate & SMB2_LEASE_READ)?"":" ",
210                                          (lstate & SMB2_LEASE_WRITE)?"":" ",
211                                          (lstate & SMB2_LEASE_HANDLE)?"":" ");
212                         } else {
213                                 d_printf("LEASE STATE UNKNOWN");
214                         }
215                 } else {
216                         d_printf("NONE            ");
217                 }
218
219                 d_printf(" %s   %s%s   %s",
220                          d->servicepath, d->base_name,
221                          (d->stream_name != NULL) ? d->stream_name : "",
222                          time_to_asc((time_t)e->time.tv_sec));
223         }
224
225         return 0;
226 }
227
228 static void print_brl(struct file_id id,
229                         struct server_id pid, 
230                         enum brl_type lock_type,
231                         enum brl_flavour lock_flav,
232                         br_off start,
233                         br_off size,
234                         void *private_data)
235 {
236         static int count;
237         unsigned int i;
238         static const struct {
239                 enum brl_type lock_type;
240                 const char *desc;
241         } lock_types[] = {
242                 { READ_LOCK, "R" },
243                 { WRITE_LOCK, "W" },
244                 { PENDING_READ_LOCK, "PR" },
245                 { PENDING_WRITE_LOCK, "PW" },
246                 { UNLOCK_LOCK, "U" }
247         };
248         const char *desc="X";
249         const char *sharepath = "";
250         char *fname = NULL;
251         struct share_mode_lock *share_mode;
252         struct server_id_buf tmp;
253
254         if (count==0) {
255                 d_printf("Byte range locks:\n");
256                 d_printf("Pid        dev:inode       R/W  start     size      SharePath               Name\n");
257                 d_printf("--------------------------------------------------------------------------------\n");
258         }
259         count++;
260
261         share_mode = fetch_share_mode_unlocked(NULL, id);
262         if (share_mode) {
263                 bool has_stream = share_mode->data->stream_name != NULL;
264
265                 fname = talloc_asprintf(NULL, "%s%s%s",
266                                         share_mode->data->base_name,
267                                         has_stream ? ":" : "",
268                                         has_stream ?
269                                         share_mode->data->stream_name :
270                                         "");
271         } else {
272                 fname = talloc_strdup(NULL, "");
273                 if (fname == NULL) {
274                         return;
275                 }
276         }
277
278         for (i=0;i<ARRAY_SIZE(lock_types);i++) {
279                 if (lock_type == lock_types[i].lock_type) {
280                         desc = lock_types[i].desc;
281                 }
282         }
283
284         d_printf("%-10s %-15s %-4s %-9jd %-9jd %-24s %-24s\n",
285                  server_id_str_buf(pid, &tmp), file_id_string_tos(&id),
286                  desc,
287                  (intmax_t)start, (intmax_t)size,
288                  sharepath, fname);
289
290         TALLOC_FREE(fname);
291         TALLOC_FREE(share_mode);
292 }
293
294 static const char *session_dialect_str(uint16_t dialect)
295 {
296         static fstring unkown_dialect;
297
298         switch(dialect){
299         case SMB2_DIALECT_REVISION_000:
300                 return "NT1";
301         case SMB2_DIALECT_REVISION_202:
302                 return "SMB2_02";
303         case SMB2_DIALECT_REVISION_210:
304                 return "SMB2_10";
305         case SMB2_DIALECT_REVISION_222:
306                 return "SMB2_22";
307         case SMB2_DIALECT_REVISION_224:
308                 return "SMB2_24";
309         case SMB3_DIALECT_REVISION_300:
310                 return "SMB3_00";
311         case SMB3_DIALECT_REVISION_302:
312                 return "SMB3_02";
313         case SMB3_DIALECT_REVISION_310:
314                 return "SMB3_10";
315         case SMB3_DIALECT_REVISION_311:
316                 return "SMB3_11";
317         }
318
319         fstr_sprintf(unkown_dialect, "Unknown (0x%04x)", dialect);
320         return unkown_dialect;
321 }
322
323 static int traverse_connections(const struct connections_key *key,
324                                 const struct connections_data *crec,
325                                 void *private_data)
326 {
327         TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
328         struct server_id_buf tmp;
329         char *timestr = NULL;
330         int result = 0;
331         const char *encryption = "-";
332         const char *signing = "-";
333
334         if (crec->cnum == TID_FIELD_INVALID)
335                 return 0;
336
337         if (do_checks &&
338             (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid))) {
339                 return 0;
340         }
341
342         timestr = timestring(mem_ctx, crec->start);
343         if (timestr == NULL) {
344                 return -1;
345         }
346
347         if (smbXsrv_is_encrypted(crec->encryption_flags)) {
348                 switch (crec->cipher) {
349                 case SMB_ENCRYPTION_GSSAPI:
350                         encryption = "GSSAPI";
351                         break;
352                 case SMB2_ENCRYPTION_AES128_CCM:
353                         encryption = "AES-128-CCM";
354                         break;
355                 case SMB2_ENCRYPTION_AES128_GCM:
356                         encryption = "AES-128-GCM";
357                         break;
358                 default:
359                         encryption = "???";
360                         result = -1;
361                         break;
362                 }
363         }
364
365         if (smbXsrv_is_signed(crec->signing_flags)) {
366                 if (crec->dialect >= SMB3_DIALECT_REVISION_302) {
367                         signing = "AES-128-CMAC";
368                 } else if (crec->dialect >= SMB2_DIALECT_REVISION_202) {
369                         signing = "HMAC-SHA256";
370                 } else {
371                         signing = "HMAC-MD5";
372                 }
373         }
374
375         d_printf("%-12s %-7s %-13s %-32s %-12s %-12s\n",
376                  crec->servicename, server_id_str_buf(crec->pid, &tmp),
377                  crec->machine,
378                  timestr,
379                  encryption,
380                  signing);
381
382         TALLOC_FREE(timestr);
383
384         return result;
385 }
386
387 static int traverse_sessionid(const char *key, struct sessionid *session,
388                               void *private_data)
389 {
390         TALLOC_CTX *mem_ctx = (TALLOC_CTX *)private_data;
391         fstring uid_gid_str;
392         struct server_id_buf tmp;
393         char *machine_hostname = NULL;
394         int result = 0;
395         const char *encryption = "-";
396         const char *signing = "-";
397
398         if (do_checks &&
399             (!process_exists(session->pid) ||
400              !Ucrit_checkUid(session->uid))) {
401                 return 0;
402         }
403
404         Ucrit_addPid(session->pid);
405
406         if (numeric_only) {
407                 fstr_sprintf(uid_gid_str, "%-12u %-12u",
408                              (unsigned int)session->uid,
409                              (unsigned int)session->gid);
410         } else {
411                 if (session->uid == -1 && session->gid == -1) {
412                         /*
413                          * The session is not fully authenticated yet.
414                          */
415                         fstrcpy(uid_gid_str, "(auth in progress)");
416                 } else {
417                         /*
418                          * In theory it should not happen that one of
419                          * session->uid and session->gid is valid (ie != -1)
420                          * while the other is not (ie = -1), so we a check for
421                          * that case that bails out would be reasonable.
422                          */
423                         const char *uid_name = "-1";
424                         const char *gid_name = "-1";
425
426                         if (session->uid != -1) {
427                                 uid_name = uidtoname(session->uid);
428                                 if (uid_name == NULL) {
429                                         return -1;
430                                 }
431                         }
432                         if (session->gid != -1) {
433                                 gid_name = gidtoname(session->gid);
434                                 if (gid_name == NULL) {
435                                         return -1;
436                                 }
437                         }
438                         fstr_sprintf(uid_gid_str, "%-12s %-12s",
439                                      uid_name, gid_name);
440                 }
441         }
442
443         machine_hostname = talloc_asprintf(mem_ctx, "%s (%s)",
444                                            session->remote_machine,
445                                            session->hostname);
446         if (machine_hostname == NULL) {
447                 return -1;
448         }
449
450         if (smbXsrv_is_encrypted(session->encryption_flags)) {
451                 switch (session->cipher) {
452                 case SMB2_ENCRYPTION_AES128_CCM:
453                         encryption = "AES-128-CCM";
454                         break;
455                 case SMB2_ENCRYPTION_AES128_GCM:
456                         encryption = "AES-128-GCM";
457                         break;
458                 default:
459                         encryption = "???";
460                         result = -1;
461                         break;
462                 }
463         } else if (smbXsrv_is_partially_encrypted(session->encryption_flags)) {
464                 switch (session->cipher) {
465                 case SMB_ENCRYPTION_GSSAPI:
466                         encryption = "partial(GSSAPI)";
467                         break;
468                 case SMB2_ENCRYPTION_AES128_CCM:
469                         encryption = "partial(AES-128-CCM)";
470                         break;
471                 case SMB2_ENCRYPTION_AES128_GCM:
472                         encryption = "partial(AES-128-GCM)";
473                         break;
474                 default:
475                         encryption = "???";
476                         result = -1;
477                         break;
478                 }
479         }
480
481         if (smbXsrv_is_signed(session->signing_flags)) {
482                 if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) {
483                         signing = "AES-128-CMAC";
484                 } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) {
485                         signing = "HMAC-SHA256";
486                 } else {
487                         signing = "HMAC-MD5";
488                 }
489         } else if (smbXsrv_is_partially_signed(session->signing_flags)) {
490                 if (session->connection_dialect >= SMB3_DIALECT_REVISION_302) {
491                         signing = "partial(AES-128-CMAC)";
492                 } else if (session->connection_dialect >= SMB2_DIALECT_REVISION_202) {
493                         signing = "partial(HMAC-SHA256)";
494                 } else {
495                         signing = "partial(HMAC-MD5)";
496                 }
497         }
498
499
500         d_printf("%-7s %-25s %-41s %-17s %-20s %-21s\n",
501                  server_id_str_buf(session->pid, &tmp),
502                  uid_gid_str,
503                  machine_hostname,
504                  session_dialect_str(session->connection_dialect),
505                  encryption,
506                  signing);
507
508         TALLOC_FREE(machine_hostname);
509
510         return result;
511 }
512
513
514 static bool print_notify_rec(const char *path, struct server_id server,
515                              const struct notify_instance *instance,
516                              void *private_data)
517 {
518         struct server_id_buf idbuf;
519
520         d_printf("%s\\%s\\%x\\%x\n", path, server_id_str_buf(server, &idbuf),
521                  (unsigned)instance->filter,
522                  (unsigned)instance->subdir_filter);
523
524         return true;
525 }
526
527 enum {
528         OPT_RESOLVE_UIDS = 1000,
529 };
530
531 int main(int argc, const char *argv[])
532 {
533         int c;
534         int profile_only = 0;
535         bool show_processes, show_locks, show_shares;
536         bool show_notify = false;
537         bool resolve_uids = false;
538         poptContext pc;
539         struct poptOption long_options[] = {
540                 POPT_AUTOHELP
541                 {
542                         .longName   = "processes",
543                         .shortName  = 'p',
544                         .argInfo    = POPT_ARG_NONE,
545                         .arg        = NULL,
546                         .val        = 'p',
547                         .descrip    = "Show processes only",
548                 },
549                 {
550                         .longName   = "verbose",
551                         .shortName  = 'v',
552                         .argInfo    = POPT_ARG_NONE,
553                         .arg        = NULL,
554                         .val        = 'v',
555                         .descrip    = "Be verbose",
556                 },
557                 {
558                         .longName   = "locks",
559                         .shortName  = 'L',
560                         .argInfo    = POPT_ARG_NONE,
561                         .arg        = NULL,
562                         .val        = 'L',
563                         .descrip    = "Show locks only",
564                 },
565                 {
566                         .longName   = "shares",
567                         .shortName  = 'S',
568                         .argInfo    = POPT_ARG_NONE,
569                         .arg        = NULL,
570                         .val        = 'S',
571                         .descrip    = "Show shares only",
572                 },
573                 {
574                         .longName   = "notify",
575                         .shortName  = 'N',
576                         .argInfo    = POPT_ARG_NONE,
577                         .arg        = NULL,
578                         .val        = 'N',
579                         .descrip    = "Show notifies",
580                 },
581                 {
582                         .longName   = "user",
583                         .shortName  = 'u',
584                         .argInfo    = POPT_ARG_STRING,
585                         .arg        = &username,
586                         .val        = 'u',
587                         .descrip    = "Switch to user",
588                 },
589                 {
590                         .longName   = "brief",
591                         .shortName  = 'b',
592                         .argInfo    = POPT_ARG_NONE,
593                         .arg        = NULL,
594                         .val        = 'b',
595                         .descrip    = "Be brief",
596                 },
597                 {
598                         .longName   = "profile",
599                         .shortName  =     'P',
600                         .argInfo    = POPT_ARG_NONE,
601                         .arg        = NULL,
602                         .val        = 'P',
603                         .descrip    = "Do profiling",
604                 },
605                 {
606                         .longName   = "profile-rates",
607                         .shortName  = 'R',
608                         .argInfo    = POPT_ARG_NONE,
609                         .arg        = NULL,
610                         .val        = 'R',
611                         .descrip    = "Show call rates",
612                 },
613                 {
614                         .longName   = "byterange",
615                         .shortName  = 'B',
616                         .argInfo    = POPT_ARG_NONE,
617                         .arg        = NULL,
618                         .val        = 'B',
619                         .descrip    = "Include byte range locks"
620                 },
621                 {
622                         .longName   = "numeric",
623                         .shortName  = 'n',
624                         .argInfo    = POPT_ARG_NONE,
625                         .arg        = NULL,
626                         .val        = 'n',
627                         .descrip    = "Numeric uid/gid"
628                 },
629                 {
630                         .longName   = "fast",
631                         .shortName  = 'f',
632                         .argInfo    = POPT_ARG_NONE,
633                         .arg        = NULL,
634                         .val        = 'f',
635                         .descrip    = "Skip checks if processes still exist"
636                 },
637                 {
638                         .longName   = "resolve-uids",
639                         .shortName  = 0,
640                         .argInfo    = POPT_ARG_NONE,
641                         .arg        = NULL,
642                         .val        = OPT_RESOLVE_UIDS,
643                         .descrip    = "Try to resolve UIDs to usernames"
644                 },
645                 POPT_COMMON_SAMBA
646                 POPT_TABLEEND
647         };
648         TALLOC_CTX *frame = talloc_stackframe();
649         int ret = 0;
650         struct messaging_context *msg_ctx = NULL;
651         char *db_path;
652         bool ok;
653
654         sec_init();
655         smb_init_locale();
656
657         setup_logging(argv[0], DEBUG_STDERR);
658         lp_set_cmdline("log level", "0");
659
660         if (getuid() != geteuid()) {
661                 d_printf("smbstatus should not be run setuid\n");
662                 ret = 1;
663                 goto done;
664         }
665
666         if (getuid() != 0) {
667                 d_printf("smbstatus only works as root!\n");
668                 ret = 1;
669                 goto done;
670         }
671
672
673         pc = poptGetContext(NULL, argc, argv, long_options,
674                             POPT_CONTEXT_KEEP_FIRST);
675
676         while ((c = poptGetNextOpt(pc)) != -1) {
677                 switch (c) {
678                 case 'p':
679                         processes_only = true;
680                         break;
681                 case 'v':
682                         verbose = true;
683                         break;
684                 case 'L':
685                         locks_only = true;
686                         break;
687                 case 'S':
688                         shares_only = true;
689                         break;
690                 case 'N':
691                         show_notify = true;
692                         break;
693                 case 'b':
694                         brief = true;
695                         break;
696                 case 'u':
697                         Ucrit_addUid(nametouid(poptGetOptArg(pc)));
698                         break;
699                 case 'P':
700                 case 'R':
701                         profile_only = c;
702                         break;
703                 case 'B':
704                         show_brl = true;
705                         break;
706                 case 'n':
707                         numeric_only = true;
708                         break;
709                 case 'f':
710                         do_checks = false;
711                         break;
712                 case OPT_RESOLVE_UIDS:
713                         resolve_uids = true;
714                         break;
715                 }
716         }
717
718         /* setup the flags based on the possible combincations */
719
720         show_processes = !(shares_only || locks_only || profile_only) || processes_only;
721         show_locks     = !(shares_only || processes_only || profile_only) || locks_only;
722         show_shares    = !(processes_only || locks_only || profile_only) || shares_only;
723
724         if ( username )
725                 Ucrit_addUid( nametouid(username) );
726
727         if (verbose) {
728                 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
729         }
730
731         msg_ctx = cmdline_messaging_context(get_dyn_CONFIGFILE());
732         if (msg_ctx == NULL) {
733                 fprintf(stderr, "Could not initialize messaging, not root?\n");
734                 ret = -1;
735                 goto done;
736         }
737
738         if (!lp_load_global(get_dyn_CONFIGFILE())) {
739                 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
740                         get_dyn_CONFIGFILE());
741                 ret = -1;
742                 goto done;
743         }
744
745         switch (profile_only) {
746                 case 'P':
747                         /* Dump profile data */
748                         ok = status_profile_dump(verbose);
749                         return ok ? 0 : 1;
750                 case 'R':
751                         /* Continuously display rate-converted data */
752                         ok = status_profile_rates(verbose);
753                         return ok ? 0 : 1;
754                 default:
755                         break;
756         }
757
758         if ( show_processes ) {
759                 d_printf("\nSamba version %s\n",samba_version_string());
760                 d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing");
761                 d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n");
762
763                 sessionid_traverse_read(traverse_sessionid, frame);
764
765                 if (processes_only) {
766                         goto done;
767                 }
768         }
769
770         if ( show_shares ) {
771                 if (brief) {
772                         goto done;
773                 }
774
775                 d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing");
776                 d_printf("---------------------------------------------------------------------------------------------\n");
777
778                 connections_forall_read(traverse_connections, frame);
779
780                 d_printf("\n");
781
782                 if ( shares_only ) {
783                         goto done;
784                 }
785         }
786
787         if ( show_locks ) {
788                 int result;
789                 struct db_context *db;
790
791                 db_path = lock_path(talloc_tos(), "locking.tdb");
792                 if (db_path == NULL) {
793                         d_printf("Out of memory - exiting\n");
794                         ret = -1;
795                         goto done;
796                 }
797
798                 db = db_open(NULL, db_path, 0,
799                              TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0,
800                              DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
801
802                 if (!db) {
803                         d_printf("%s not initialised\n", db_path);
804                         d_printf("This is normal if an SMB client has never "
805                                  "connected to your server.\n");
806                         TALLOC_FREE(db_path);
807                         exit(0);
808                 } else {
809                         TALLOC_FREE(db);
810                         TALLOC_FREE(db_path);
811                 }
812
813                 if (!locking_init_readonly()) {
814                         d_printf("Can't initialise locking module - exiting\n");
815                         ret = 1;
816                         goto done;
817                 }
818
819                 result = share_entry_forall(print_share_mode, &resolve_uids);
820
821                 if (result == 0) {
822                         d_printf("No locked files\n");
823                 } else if (result < 0) {
824                         d_printf("locked file list truncated\n");
825                 }
826
827                 d_printf("\n");
828
829                 if (show_brl) {
830                         brl_forall(print_brl, NULL);
831                 }
832
833                 locking_end();
834         }
835
836         if (show_notify) {
837                 struct notify_context *n;
838
839                 n = notify_init(talloc_tos(), msg_ctx,
840                                 NULL, NULL);
841                 if (n == NULL) {
842                         goto done;
843                 }
844                 notify_walk(n, print_notify_rec, NULL);
845                 TALLOC_FREE(n);
846         }
847
848 done:
849         TALLOC_FREE(frame);
850         return ret;
851 }