lib: Remove server_id_str()
[Samba.git] / source3 / utils / status.c
blobafbba69126a0d3de91dfe11eed66b8eababe26e2
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 "system/filesys.h"
35 #include "popt_common.h"
36 #include "dbwrap/dbwrap.h"
37 #include "dbwrap/dbwrap_open.h"
38 #include "../libcli/security/security.h"
39 #include "session.h"
40 #include "locking/proto.h"
41 #include "messages.h"
42 #include "librpc/gen_ndr/open_files.h"
43 #include "smbd/smbd.h"
44 #include "librpc/gen_ndr/notify.h"
45 #include "lib/conn_tdb.h"
46 #include "serverid.h"
47 #include "status_profile.h"
49 #define SMB_MAXPIDS 2048
50 static uid_t Ucrit_uid = 0; /* added by OH */
51 static struct server_id Ucrit_pid[SMB_MAXPIDS]; /* Ugly !!! */ /* added by OH */
52 static int Ucrit_MaxPid=0; /* added by OH */
53 static unsigned int Ucrit_IsActive = 0; /* added by OH */
55 static bool verbose, brief;
56 static bool shares_only; /* Added by RJS */
57 static bool locks_only; /* Added by RJS */
58 static bool processes_only;
59 static bool show_brl;
60 static bool numeric_only;
61 static bool do_checks = true;
63 const char *username = NULL;
65 /* added by OH */
66 static void Ucrit_addUid(uid_t uid)
68 Ucrit_uid = uid;
69 Ucrit_IsActive = 1;
72 static unsigned int Ucrit_checkUid(uid_t uid)
74 if ( !Ucrit_IsActive )
75 return 1;
77 if ( uid == Ucrit_uid )
78 return 1;
80 return 0;
83 static unsigned int Ucrit_checkPid(struct server_id pid)
85 int i;
87 if ( !Ucrit_IsActive )
88 return 1;
90 for (i=0;i<Ucrit_MaxPid;i++) {
91 if (serverid_equal(&pid, &Ucrit_pid[i])) {
92 return 1;
96 return 0;
99 static bool Ucrit_addPid( struct server_id pid )
101 if ( !Ucrit_IsActive )
102 return True;
104 if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
105 d_printf("ERROR: More than %d pids for user %s!\n",
106 SMB_MAXPIDS, uidtoname(Ucrit_uid));
108 return False;
111 Ucrit_pid[Ucrit_MaxPid++] = pid;
113 return True;
116 static int print_share_mode(const struct share_mode_entry *e,
117 const char *sharepath,
118 const char *fname,
119 void *dummy)
121 static int count;
123 if (do_checks && !is_valid_share_mode_entry(e)) {
124 return 0;
127 if (count==0) {
128 d_printf("Locked files:\n");
129 d_printf("Pid Uid DenyMode Access R/W Oplock SharePath Name Time\n");
130 d_printf("--------------------------------------------------------------------------------------------------\n");
132 count++;
134 if (do_checks && !serverid_exists(&e->pid)) {
135 /* the process for this entry does not exist any more */
136 return 0;
139 if (Ucrit_checkPid(e->pid)) {
140 struct server_id_buf tmp;
141 d_printf("%-11s ", server_id_str_buf(e->pid, &tmp));
142 d_printf("%-9u ", (unsigned int)e->uid);
143 switch (map_share_mode_to_deny_mode(e->share_access,
144 e->private_options)) {
145 case DENY_NONE: d_printf("DENY_NONE "); break;
146 case DENY_ALL: d_printf("DENY_ALL "); break;
147 case DENY_DOS: d_printf("DENY_DOS "); break;
148 case DENY_READ: d_printf("DENY_READ "); break;
149 case DENY_WRITE:printf("DENY_WRITE "); break;
150 case DENY_FCB: d_printf("DENY_FCB "); break;
151 default: {
152 d_printf("unknown-please report ! "
153 "e->share_access = 0x%x, "
154 "e->private_options = 0x%x\n",
155 (unsigned int)e->share_access,
156 (unsigned int)e->private_options );
157 break;
160 d_printf("0x%-8x ",(unsigned int)e->access_mask);
161 if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
162 (FILE_READ_DATA|FILE_WRITE_DATA)) {
163 d_printf("RDWR ");
164 } else if (e->access_mask & FILE_WRITE_DATA) {
165 d_printf("WRONLY ");
166 } else {
167 d_printf("RDONLY ");
170 if((e->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
171 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) {
172 d_printf("EXCLUSIVE+BATCH ");
173 } else if (e->op_type & EXCLUSIVE_OPLOCK) {
174 d_printf("EXCLUSIVE ");
175 } else if (e->op_type & BATCH_OPLOCK) {
176 d_printf("BATCH ");
177 } else if (e->op_type & LEVEL_II_OPLOCK) {
178 d_printf("LEVEL_II ");
179 } else if (e->op_type == LEASE_OPLOCK) {
180 uint32_t lstate = e->lease->current_state;
181 d_printf("LEASE(%s%s%s)%s%s%s ",
182 (lstate & SMB2_LEASE_READ)?"R":"",
183 (lstate & SMB2_LEASE_WRITE)?"W":"",
184 (lstate & SMB2_LEASE_HANDLE)?"H":"",
185 (lstate & SMB2_LEASE_READ)?"":" ",
186 (lstate & SMB2_LEASE_WRITE)?"":" ",
187 (lstate & SMB2_LEASE_HANDLE)?"":" ");
188 } else {
189 d_printf("NONE ");
192 d_printf(" %s %s %s",sharepath, fname, time_to_asc((time_t)e->time.tv_sec));
195 return 0;
198 static void print_brl(struct file_id id,
199 struct server_id pid,
200 enum brl_type lock_type,
201 enum brl_flavour lock_flav,
202 br_off start,
203 br_off size,
204 void *private_data)
206 static int count;
207 unsigned int i;
208 static const struct {
209 enum brl_type lock_type;
210 const char *desc;
211 } lock_types[] = {
212 { READ_LOCK, "R" },
213 { WRITE_LOCK, "W" },
214 { PENDING_READ_LOCK, "PR" },
215 { PENDING_WRITE_LOCK, "PW" },
216 { UNLOCK_LOCK, "U" }
218 const char *desc="X";
219 const char *sharepath = "";
220 char *fname = NULL;
221 struct share_mode_lock *share_mode;
222 struct server_id_buf tmp;
224 if (count==0) {
225 d_printf("Byte range locks:\n");
226 d_printf("Pid dev:inode R/W start size SharePath Name\n");
227 d_printf("--------------------------------------------------------------------------------\n");
229 count++;
231 share_mode = fetch_share_mode_unlocked(NULL, id);
232 if (share_mode) {
233 bool has_stream = share_mode->data->stream_name != NULL;
235 fname = talloc_asprintf(NULL, "%s%s%s",
236 share_mode->data->base_name,
237 has_stream ? ":" : "",
238 has_stream ?
239 share_mode->data->stream_name :
240 "");
241 } else {
242 fname = talloc_strdup(NULL, "");
243 if (fname == NULL) {
244 return;
248 for (i=0;i<ARRAY_SIZE(lock_types);i++) {
249 if (lock_type == lock_types[i].lock_type) {
250 desc = lock_types[i].desc;
254 d_printf("%-10s %-15s %-4s %-9jd %-9jd %-24s %-24s\n",
255 server_id_str_buf(pid, &tmp), file_id_string_tos(&id),
256 desc,
257 (intmax_t)start, (intmax_t)size,
258 sharepath, fname);
260 TALLOC_FREE(fname);
261 TALLOC_FREE(share_mode);
264 static int traverse_connections(const struct connections_key *key,
265 const struct connections_data *crec,
266 void *state)
268 struct server_id_buf tmp;
270 if (crec->cnum == TID_FIELD_INVALID)
271 return 0;
273 if (do_checks &&
274 (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid))) {
275 return 0;
278 d_printf("%-10s %s %-12s %s",
279 crec->servicename, server_id_str_buf(crec->pid, &tmp),
280 crec->machine,
281 time_to_asc(crec->start));
283 return 0;
286 static int traverse_sessionid(const char *key, struct sessionid *session,
287 void *private_data)
289 fstring uid_str, gid_str;
290 struct server_id_buf tmp;
292 if (do_checks &&
293 (!process_exists(session->pid) ||
294 !Ucrit_checkUid(session->uid))) {
295 return 0;
298 Ucrit_addPid(session->pid);
300 fstrcpy(uid_str, "-1");
302 if (session->uid != -1) {
303 if (numeric_only) {
304 fstr_sprintf(uid_str, "%u", (unsigned int)session->uid);
305 } else {
306 fstrcpy(uid_str, uidtoname(session->uid));
310 fstrcpy(gid_str, "-1");
312 if (session->gid != -1) {
313 if (numeric_only) {
314 fstr_sprintf(gid_str, "%u", (unsigned int)session->gid);
315 } else {
316 fstrcpy(gid_str, gidtoname(session->gid));
320 d_printf("%-7s %-12s %-12s %-12s (%s) %-12s\n",
321 server_id_str_buf(session->pid, &tmp),
322 uid_str, gid_str,
323 session->remote_machine, session->hostname, session->protocol_ver);
325 return 0;
329 static void print_notify_recs(const char *path,
330 struct notify_db_entry *entries,
331 size_t num_entries,
332 time_t deleted_time, void *private_data)
334 size_t i;
335 d_printf("%s\n", path);
337 if (num_entries == 0) {
338 d_printf("deleted %s\n", time_to_asc(deleted_time));
341 for (i=0; i<num_entries; i++) {
342 struct notify_db_entry *e = &entries[i];
343 struct server_id_buf idbuf;
345 printf("%s %x %x\n", server_id_str_buf(e->server, &idbuf),
346 (unsigned)e->filter,
347 (unsigned)e->subdir_filter);
349 printf("\n");
352 int main(int argc, const char *argv[])
354 int c;
355 int profile_only = 0;
356 bool show_processes, show_locks, show_shares;
357 bool show_notify = false;
358 poptContext pc;
359 struct poptOption long_options[] = {
360 POPT_AUTOHELP
361 {"processes", 'p', POPT_ARG_NONE, NULL, 'p', "Show processes only" },
362 {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Be verbose" },
363 {"locks", 'L', POPT_ARG_NONE, NULL, 'L', "Show locks only" },
364 {"shares", 'S', POPT_ARG_NONE, NULL, 'S', "Show shares only" },
365 {"notify", 'N', POPT_ARG_NONE, NULL, 'N', "Show notifies" },
366 {"user", 'u', POPT_ARG_STRING, &username, 'u', "Switch to user" },
367 {"brief", 'b', POPT_ARG_NONE, NULL, 'b', "Be brief" },
368 {"profile", 'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" },
369 {"profile-rates", 'R', POPT_ARG_NONE, NULL, 'R', "Show call rates" },
370 {"byterange", 'B', POPT_ARG_NONE, NULL, 'B', "Include byte range locks"},
371 {"numeric", 'n', POPT_ARG_NONE, NULL, 'n', "Numeric uid/gid"},
372 {"fast", 'f', POPT_ARG_NONE, NULL, 'f', "Skip checks if processes still exist"},
373 POPT_COMMON_SAMBA
374 POPT_TABLEEND
376 TALLOC_CTX *frame = talloc_stackframe();
377 int ret = 0;
378 struct messaging_context *msg_ctx;
379 char *db_path;
380 bool ok;
382 sec_init();
383 smb_init_locale();
385 setup_logging(argv[0], DEBUG_STDERR);
387 if (getuid() != geteuid()) {
388 d_printf("smbstatus should not be run setuid\n");
389 ret = 1;
390 goto done;
393 if (getuid() != 0) {
394 d_printf("smbstatus only works as root!\n");
395 ret = 1;
396 goto done;
400 pc = poptGetContext(NULL, argc, argv, long_options,
401 POPT_CONTEXT_KEEP_FIRST);
403 while ((c = poptGetNextOpt(pc)) != -1) {
404 switch (c) {
405 case 'p':
406 processes_only = true;
407 break;
408 case 'v':
409 verbose = true;
410 break;
411 case 'L':
412 locks_only = true;
413 break;
414 case 'S':
415 shares_only = true;
416 break;
417 case 'N':
418 show_notify = true;
419 break;
420 case 'b':
421 brief = true;
422 break;
423 case 'u':
424 Ucrit_addUid(nametouid(poptGetOptArg(pc)));
425 break;
426 case 'P':
427 case 'R':
428 profile_only = c;
429 break;
430 case 'B':
431 show_brl = true;
432 break;
433 case 'n':
434 numeric_only = true;
435 break;
436 case 'f':
437 do_checks = false;
438 break;
442 /* setup the flags based on the possible combincations */
444 show_processes = !(shares_only || locks_only || profile_only) || processes_only;
445 show_locks = !(shares_only || processes_only || profile_only) || locks_only;
446 show_shares = !(processes_only || locks_only || profile_only) || shares_only;
448 if ( username )
449 Ucrit_addUid( nametouid(username) );
451 if (verbose) {
452 d_printf("using configfile = %s\n", get_dyn_CONFIGFILE());
455 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
456 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
457 get_dyn_CONFIGFILE());
458 ret = -1;
459 goto done;
463 if (lp_clustering()) {
465 * This implicitly initializes the global ctdbd
466 * connection, usable by the db_open() calls further
467 * down.
469 msg_ctx = messaging_init(NULL, samba_tevent_context_init(NULL));
470 if (msg_ctx == NULL) {
471 fprintf(stderr, "messaging_init failed\n");
472 ret = -1;
473 goto done;
477 if (!lp_load_global(get_dyn_CONFIGFILE())) {
478 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
479 get_dyn_CONFIGFILE());
480 ret = -1;
481 goto done;
484 switch (profile_only) {
485 case 'P':
486 /* Dump profile data */
487 ok = status_profile_dump(verbose);
488 return ok ? 0 : 1;
489 case 'R':
490 /* Continuously display rate-converted data */
491 ok = status_profile_rates(verbose);
492 return ok ? 0 : 1;
493 default:
494 break;
497 if ( show_processes ) {
498 d_printf("\nSamba version %s\n",samba_version_string());
499 d_printf("PID Username Group Machine Protocol Version \n");
500 d_printf("------------------------------------------------------------------------------\n");
502 sessionid_traverse_read(traverse_sessionid, NULL);
504 if (processes_only) {
505 goto done;
509 if ( show_shares ) {
510 if (verbose) {
511 db_path = lock_path("connections.tdb");
512 if (db_path == NULL) {
513 d_printf("Out of memory - exiting\n");
514 ret = -1;
515 goto done;
517 d_printf("Opened %s\n", db_path);
518 TALLOC_FREE(db_path);
521 if (brief) {
522 goto done;
525 d_printf("\nService pid machine Connected at\n");
526 d_printf("-------------------------------------------------------\n");
528 connections_forall_read(traverse_connections, NULL);
530 d_printf("\n");
532 if ( shares_only ) {
533 goto done;
537 if ( show_locks ) {
538 int result;
539 struct db_context *db;
541 db_path = lock_path("locking.tdb");
542 if (db_path == NULL) {
543 d_printf("Out of memory - exiting\n");
544 ret = -1;
545 goto done;
548 db = db_open(NULL, db_path, 0,
549 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0,
550 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
552 if (!db) {
553 d_printf("%s not initialised\n", db_path);
554 d_printf("This is normal if an SMB client has never "
555 "connected to your server.\n");
556 TALLOC_FREE(db_path);
557 exit(0);
558 } else {
559 TALLOC_FREE(db);
560 TALLOC_FREE(db_path);
563 if (!locking_init_readonly()) {
564 d_printf("Can't initialise locking module - exiting\n");
565 ret = 1;
566 goto done;
569 result = share_entry_forall(print_share_mode, NULL);
571 if (result == 0) {
572 d_printf("No locked files\n");
573 } else if (result < 0) {
574 d_printf("locked file list truncated\n");
577 d_printf("\n");
579 if (show_brl) {
580 brl_forall(print_brl, NULL);
583 locking_end();
586 if (show_notify) {
587 struct notify_context *n;
589 n = notify_init(talloc_tos(), NULL, NULL);
590 if (n == NULL) {
591 goto done;
593 notify_walk(n, print_notify_recs, NULL);
594 TALLOC_FREE(n);
597 done:
598 TALLOC_FREE(frame);
599 return ret;