r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / 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
35 #define SMB_MAXPIDS             2048
36 static uid_t            Ucrit_uid = 0;               /* added by OH */
37 static struct server_id Ucrit_pid[SMB_MAXPIDS];  /* Ugly !!! */   /* added by OH */
38 static int              Ucrit_MaxPid=0;                    /* added by OH */
39 static unsigned int     Ucrit_IsActive = 0;                /* added by OH */
40
41 static int verbose, brief;
42 static int            shares_only = 0;            /* Added by RJS */
43 static int            locks_only  = 0;            /* Added by RJS */
44 static BOOL processes_only=False;
45 static int show_brl;
46 static BOOL numeric_only = False;
47
48 const char *username = NULL;
49
50 extern BOOL status_profile_dump(BOOL be_verbose);
51 extern BOOL status_profile_rates(BOOL be_verbose);
52
53 /* added by OH */
54 static void Ucrit_addUid(uid_t uid)
55 {
56         Ucrit_uid = uid;
57         Ucrit_IsActive = 1;
58 }
59
60 static unsigned int Ucrit_checkUid(uid_t uid)
61 {
62         if ( !Ucrit_IsActive ) 
63                 return 1;
64         
65         if ( uid == Ucrit_uid ) 
66                 return 1;
67         
68         return 0;
69 }
70
71 static unsigned int Ucrit_checkPid(struct server_id pid)
72 {
73         int i;
74         
75         if ( !Ucrit_IsActive ) 
76                 return 1;
77         
78         for (i=0;i<Ucrit_MaxPid;i++) {
79                 if (cluster_id_equal(&pid, &Ucrit_pid[i])) 
80                         return 1;
81         }
82         
83         return 0;
84 }
85
86 static BOOL Ucrit_addPid( struct server_id pid )
87 {
88         if ( !Ucrit_IsActive )
89                 return True;
90
91         if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
92                 d_printf("ERROR: More than %d pids for user %s!\n",
93                          SMB_MAXPIDS, uidtoname(Ucrit_uid));
94
95                 return False;
96         }
97
98         Ucrit_pid[Ucrit_MaxPid++] = pid;
99         
100         return True;
101 }
102
103 static void print_share_mode(const struct share_mode_entry *e,
104                              const char *sharepath,
105                              const char *fname,
106                              void *dummy)
107 {
108         static int count;
109
110         if (!is_valid_share_mode_entry(e)) {
111                 return;
112         }
113
114         if (count==0) {
115                 d_printf("Locked files:\n");
116                 d_printf("Pid          Uid        DenyMode   Access      R/W        Oplock           SharePath   Name   Time\n");
117                 d_printf("--------------------------------------------------------------------------------------------------\n");
118         }
119         count++;
120
121         if (Ucrit_checkPid(e->pid)) {
122                 d_printf("%-11s  ",procid_str_static(&e->pid));
123                 d_printf("%-9u  ", (unsigned int)e->uid);
124                 switch (map_share_mode_to_deny_mode(e->share_access,
125                                                     e->private_options)) {
126                         case DENY_NONE: d_printf("DENY_NONE  "); break;
127                         case DENY_ALL:  d_printf("DENY_ALL   "); break;
128                         case DENY_DOS:  d_printf("DENY_DOS   "); break;
129                         case DENY_READ: d_printf("DENY_READ  "); break;
130                         case DENY_WRITE:printf("DENY_WRITE "); break;
131                         case DENY_FCB:  d_printf("DENY_FCB "); break;
132                         default: {
133                                 d_printf("unknown-please report ! "
134                                          "e->share_access = 0x%x, "
135                                          "e->private_options = 0x%x\n",
136                                          (unsigned int)e->share_access,
137                                          (unsigned int)e->private_options );
138                                 break;
139                         }
140                 }
141                 d_printf("0x%-8x  ",(unsigned int)e->access_mask);
142                 if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
143                                 (FILE_READ_DATA|FILE_WRITE_DATA)) {
144                         d_printf("RDWR       ");
145                 } else if (e->access_mask & FILE_WRITE_DATA) {
146                         d_printf("WRONLY     ");
147                 } else {
148                         d_printf("RDONLY     ");
149                 }
150
151                 if((e->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
152                                         (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) {
153                         d_printf("EXCLUSIVE+BATCH ");
154                 } else if (e->op_type & EXCLUSIVE_OPLOCK) {
155                         d_printf("EXCLUSIVE       ");
156                 } else if (e->op_type & BATCH_OPLOCK) {
157                         d_printf("BATCH           ");
158                 } else if (e->op_type & LEVEL_II_OPLOCK) {
159                         d_printf("LEVEL_II        ");
160                 } else {
161                         d_printf("NONE            ");
162                 }
163
164                 d_printf(" %s   %s   %s",sharepath, fname, time_to_asc((time_t)e->time.tv_sec));
165         }
166 }
167
168 static void print_brl(struct file_id id,
169                         struct server_id pid, 
170                         enum brl_type lock_type,
171                         enum brl_flavour lock_flav,
172                         br_off start,
173                         br_off size,
174                         void *private_data)
175 {
176         static int count;
177         int i;
178         static const struct {
179                 enum brl_type lock_type;
180                 const char *desc;
181         } lock_types[] = {
182                 { READ_LOCK, "R" },
183                 { WRITE_LOCK, "W" },
184                 { PENDING_READ_LOCK, "PR" },
185                 { PENDING_WRITE_LOCK, "PW" },
186                 { UNLOCK_LOCK, "U" }
187         };
188         const char *desc="X";
189         if (count==0) {
190                 d_printf("Byte range locks:\n");
191                 d_printf("   Pid     dev:inode  R/W      start        size\n");
192                 d_printf("------------------------------------------------\n");
193         }
194         count++;
195
196         for (i=0;i<ARRAY_SIZE(lock_types);i++) {
197                 if (lock_type == lock_types[i].lock_type) {
198                         desc = lock_types[i].desc;
199                 }
200         }
201
202         d_printf("%8s   %s    %2s  %9.0f   %9.0f\n", 
203                  procid_str_static(&pid), file_id_static_string(&id),
204                  desc,
205                  (double)start, (double)size);
206 }
207
208 static int traverse_fn1(struct db_record *rec,
209                         const struct connections_key *key,
210                         const struct connections_data *crec,
211                         void *state)
212 {
213         if (crec->cnum == -1)
214                 return 0;
215
216         if (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid)) {
217                 return 0;
218         }
219
220         d_printf("%-10s   %s   %-12s  %s",
221                  crec->servicename,procid_str_static(&crec->pid),
222                  crec->machine,
223                  time_to_asc(crec->start));
224
225         return 0;
226 }
227
228 static int traverse_sessionid(struct db_record *db, void *state)
229 {
230         struct sessionid sessionid;
231         fstring uid_str, gid_str;
232
233         if (db->value.dsize != sizeof(sessionid))
234                 return 0;
235
236         memcpy(&sessionid, db->value.dptr, sizeof(sessionid));
237
238         if (!process_exists(sessionid.pid) || !Ucrit_checkUid(sessionid.uid)) {
239                 return 0;
240         }
241
242         Ucrit_addPid( sessionid.pid );
243
244         fstr_sprintf(uid_str, "%d", sessionid.uid);
245         fstr_sprintf(gid_str, "%d", sessionid.gid);
246
247         d_printf("%-7s   %-12s  %-12s  %-12s (%s)\n",
248                  procid_str_static(&sessionid.pid),
249                  numeric_only ? uid_str : uidtoname(sessionid.uid),
250                  numeric_only ? gid_str : gidtoname(sessionid.gid), 
251                  sessionid.remote_machine, sessionid.hostname);
252         
253         return 0;
254 }
255
256
257
258
259  int main(int argc, char *argv[])
260 {
261         int c;
262         int profile_only = 0;
263         BOOL show_processes, show_locks, show_shares;
264         poptContext pc;
265         struct poptOption long_options[] = {
266                 POPT_AUTOHELP
267                 {"processes",   'p', POPT_ARG_NONE,     &processes_only, 'p', "Show processes only" },
268                 {"verbose",     'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
269                 {"locks",       'L', POPT_ARG_NONE,     &locks_only, 'L', "Show locks only" },
270                 {"shares",      'S', POPT_ARG_NONE,     &shares_only, 'S', "Show shares only" },
271                 {"user",        'u', POPT_ARG_STRING,   &username, 'u', "Switch to user" },
272                 {"brief",       'b', POPT_ARG_NONE,     &brief, 'b', "Be brief" },
273                 {"profile",     'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" },
274                 {"profile-rates", 'R', POPT_ARG_NONE, NULL, 'R', "Show call rates" },
275                 {"byterange",   'B', POPT_ARG_NONE,     &show_brl, 'B', "Include byte range locks"},
276                 {"numeric",     'n', POPT_ARG_NONE,     &numeric_only, 'n', "Numeric uid/gid"},
277                 POPT_COMMON_SAMBA
278                 POPT_TABLEEND
279         };
280
281         sec_init();
282         load_case_tables();
283
284         setup_logging(argv[0],True);
285         
286         dbf = x_stderr;
287         
288         if (getuid() != geteuid()) {
289                 d_printf("smbstatus should not be run setuid\n");
290                 return(1);
291         }
292
293         pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
294                             POPT_CONTEXT_KEEP_FIRST);
295         
296         while ((c = poptGetNextOpt(pc)) != -1) {
297                 switch (c) {
298                 case 'u':                                      
299                         Ucrit_addUid(nametouid(poptGetOptArg(pc)));
300                         break;
301                 case 'P':
302                 case 'R':
303                         profile_only = c;
304                 }
305         }
306
307         /* setup the flags based on the possible combincations */
308
309         show_processes = !(shares_only || locks_only || profile_only) || processes_only;
310         show_locks     = !(shares_only || processes_only || profile_only) || locks_only;
311         show_shares    = !(processes_only || locks_only || profile_only) || shares_only;
312
313         if ( username )
314                 Ucrit_addUid( nametouid(username) );
315
316         if (verbose) {
317                 d_printf("using configfile = %s\n", dyn_CONFIGFILE);
318         }
319
320         if (!lp_load(dyn_CONFIGFILE,False,False,False,True)) {
321                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
322                 return (-1);
323         }
324
325         /*
326          * This implicitly initializes the global ctdbd connection, usable by
327          * the db_open() calls further down.
328          */
329
330         messaging_init(NULL, procid_self(), event_context_init(NULL));
331         
332         switch (profile_only) {
333                 case 'P':
334                         /* Dump profile data */
335                         return status_profile_dump(verbose);
336                 case 'R':
337                         /* Continuously display rate-converted data */
338                         return status_profile_rates(verbose);
339                 default:
340                         break;
341         }
342
343         if ( show_processes ) {
344                 struct db_context *db;
345                 db = db_open(NULL, lock_path("sessionid.tdb"), 0,
346                              TDB_DEFAULT, O_RDWR, 0644);
347                 if (!db) {
348                         d_printf("sessionid.tdb not initialised\n");
349                 } else {
350                         d_printf("\nSamba version %s\n",SAMBA_VERSION_STRING);
351                         d_printf("PID     Username      Group         Machine                        \n");
352                         d_printf("-------------------------------------------------------------------\n");
353
354                         db->traverse_read(db, traverse_sessionid, NULL);
355                         talloc_free(db);
356                 }
357
358                 if (processes_only) 
359                         exit(0);        
360         }
361   
362         if ( show_shares ) {
363                 if (verbose) {
364                         d_printf("Opened %s\n", lock_path("connections.tdb"));
365                 }
366
367                 if (brief) 
368                         exit(0);
369                 
370                 d_printf("\nService      pid     machine       Connected at\n");
371                 d_printf("-------------------------------------------------------\n");
372         
373                 connections_forall(traverse_fn1, NULL);
374
375                 d_printf("\n");
376
377                 if ( shares_only )
378                         exit(0);
379         }
380
381         if ( show_locks ) {
382                 int ret;
383
384                 if (!locking_init(1)) {
385                         d_printf("Can't initialise locking module - exiting\n");
386                         exit(1);
387                 }
388                 
389                 ret = share_mode_forall(print_share_mode, NULL);
390
391                 if (ret == 0) {
392                         d_printf("No locked files\n");
393                 } else if (ret == -1) {
394                         d_printf("locked file list truncated\n");
395                 }
396                 
397                 d_printf("\n");
398
399                 if (show_brl) {
400                         brl_forall(print_brl, NULL);
401                 }
402                 
403                 locking_end();
404         }
405
406         return (0);
407 }