Merge of new 2.2 code into HEAD (Gerald I hate you :-) :-). Allows new SAMR
[nivanova/samba-autobuild/.git] / source3 / utils / status.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    status reporting
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21    Revision History:
22
23    12 aug 96: Erik.Devriendt@te6.siemens.be
24    added support for shared memory implementation of share mode locking
25
26    21-Jul-1998: rsharpe@ns.aus.com (Richard Sharpe)
27    Added -L (locks only) -S (shares only) flags and code
28
29 */
30
31 /*
32  * This program reports current SMB connections
33  */
34
35 #define NO_SYSLOG
36
37 #include "includes.h"
38
39 struct session_record{
40   pid_t pid;
41   uid_t uid;
42   char machine[31];
43   time_t start;
44   struct session_record *next;
45 } *srecs;
46
47 extern int DEBUGLEVEL;
48 extern FILE *dbf;
49
50 static pstring Ucrit_username = "";                   /* added by OH */
51 static pid_t    Ucrit_pid[100];  /* Ugly !!! */        /* added by OH */
52 static int            Ucrit_MaxPid=0;                        /* added by OH */
53 static unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
54 static int verbose, brief;
55 static int            shares_only = 0;            /* Added by RJS */
56 static int            locks_only  = 0;            /* Added by RJS */
57 static BOOL processes_only=False;
58 static int show_brl;
59
60 /* we need these because we link to locking*.o */
61  void become_root(void) {}
62  void unbecome_root(void) {}
63
64
65 /* added by OH */
66 static void Ucrit_addUsername(char *username)
67 {
68         pstrcpy(Ucrit_username, username);
69         if(strlen(Ucrit_username) > 0)
70                 Ucrit_IsActive = 1;
71 }
72
73 static unsigned int Ucrit_checkUsername(char *username)
74 {
75         if ( !Ucrit_IsActive) return 1;
76         if (strcmp(Ucrit_username,username) ==0) return 1;
77         return 0;
78 }
79
80 static void Ucrit_addPid(pid_t pid)
81 {
82         int i;
83         if ( !Ucrit_IsActive) return;
84         for (i=0;i<Ucrit_MaxPid;i++)
85                 if( pid == Ucrit_pid[i] ) return;
86         Ucrit_pid[Ucrit_MaxPid++] = pid;
87 }
88
89 static unsigned int Ucrit_checkPid(pid_t pid)
90 {
91         int i;
92         if ( !Ucrit_IsActive) return 1;
93         for (i=0;i<Ucrit_MaxPid;i++)
94                 if( pid == Ucrit_pid[i] ) return 1;
95         return 0;
96 }
97
98
99 static void print_share_mode(share_mode_entry *e, char *fname)
100 {
101         static int count;
102         if (count==0) {
103                 printf("Locked files:\n");
104                 printf("Pid    DenyMode   R/W        Oplock           Name\n");
105                 printf("--------------------------------------------------\n");
106         }
107         count++;
108
109         if (Ucrit_checkPid(e->pid)) {
110           printf("%-5d  ",(int)e->pid);
111           switch (GET_DENY_MODE(e->share_mode)) {
112           case DENY_NONE: printf("DENY_NONE  "); break;
113           case DENY_ALL:  printf("DENY_ALL   "); break;
114           case DENY_DOS:  printf("DENY_DOS   "); break;
115           case DENY_READ: printf("DENY_READ  "); break;
116           case DENY_WRITE:printf("DENY_WRITE "); break;
117           case DENY_FCB:  printf("DENY_FCB "); break;
118           }
119           switch (e->share_mode&0xF) {
120           case 0: printf("RDONLY     "); break;
121           case 1: printf("WRONLY     "); break;
122           case 2: printf("RDWR       "); break;
123           }
124
125           if((e->op_type & 
126              (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
127               (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
128                 printf("EXCLUSIVE+BATCH ");
129           else if (e->op_type & EXCLUSIVE_OPLOCK)
130                 printf("EXCLUSIVE       ");
131           else if (e->op_type & BATCH_OPLOCK)
132                 printf("BATCH           ");
133           else if (e->op_type & LEVEL_II_OPLOCK)
134                 printf("LEVEL_II        ");
135           else
136                 printf("NONE            ");
137
138           printf(" %s   %s",dos_to_unix(fname,False),
139              asctime(LocalTime((time_t *)&e->time.tv_sec)));
140         }
141 }
142
143 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid, 
144                       enum brl_type lock_type,
145                       br_off start, br_off size)
146 {
147         static int count;
148         if (count==0) {
149                 printf("Byte range locks:\n");
150                 printf("   Pid     dev:inode  R/W      start        size\n");
151                 printf("------------------------------------------------\n");
152         }
153         count++;
154
155         printf("%6d   %05x:%05x    %s  %9.0f   %9.0f\n", 
156                (int)pid, (int)dev, (int)ino, 
157                lock_type==READ_LOCK?"R":"W",
158                (double)start, (double)size);
159 }
160
161
162 /*******************************************************************
163  dump the elements of the profile structure
164   ******************************************************************/
165 static int profile_dump(void)
166 {
167 #ifndef WITH_PROFILE
168         fprintf(stderr,"ERROR: not compiled with profile support\n");
169         return -1;
170 #else
171         if (!profile_setup(True)) {
172                 fprintf(stderr,"Failed to initialise profile memory\n");
173                 return -1;
174         }
175
176         printf("smb_count:\t%u\n", profile_p->smb_count);
177         printf("uid_changes:\t%u\n", profile_p->uid_changes);
178         return 0;
179 #endif
180 }
181
182
183 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
184 {
185         static pid_t last_pid;
186         struct session_record *ptr;
187         struct connections_data crec;
188
189         memcpy(&crec, dbuf.dptr, sizeof(crec));
190
191         if (crec.cnum == -1) return 0;
192
193         if (!process_exists(crec.pid) || !Ucrit_checkUsername(uidtoname(crec.uid))) {
194                 return 0;
195         }
196
197         if (brief) {
198                 ptr=srecs;
199                 while (ptr!=NULL) {
200                         if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) {
201                                 if (ptr->start > crec.start)
202                                         ptr->start=crec.start;
203                                 break;
204                         }
205                         ptr=ptr->next;
206                 }
207                 if (ptr==NULL) {
208                         ptr=(struct session_record *) malloc(sizeof(struct session_record));
209                         ptr->uid=crec.uid;
210                         ptr->pid=crec.pid;
211                         ptr->start=crec.start;
212                         strncpy(ptr->machine,crec.machine,30);
213                         ptr->machine[30]='\0';
214                         ptr->next=srecs;
215                         srecs=ptr;
216                 }
217         } else {
218                 Ucrit_addPid(crec.pid);  
219                 if (processes_only) {
220                         if (last_pid != crec.pid)
221                                 printf("%d\n",(int)crec.pid);
222                         last_pid = crec.pid; /* XXXX we can still get repeats, have to
223                                                 add a sort at some time */
224                 } else {
225                         printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
226                                crec.name,uidtoname(crec.uid),gidtoname(crec.gid),(int)crec.pid,
227                                crec.machine,crec.addr,
228                                asctime(LocalTime(&crec.start)));
229                 }
230         }
231
232         return 0;
233 }
234
235
236
237
238  int main(int argc, char *argv[])
239 {
240         pstring fname;
241         int c;
242         static pstring servicesf = CONFIGFILE;
243         extern char *optarg;
244         int profile_only = 0;
245         TDB_CONTEXT *tdb;
246         struct session_record *ptr;
247
248         TimeInit();
249         setup_logging(argv[0],True);
250         
251         charset_initialise();
252         
253         DEBUGLEVEL = 0;
254         dbf = stderr;
255         
256         if (getuid() != geteuid()) {
257                 printf("smbstatus should not be run setuid\n");
258                 return(1);
259         }
260         
261         while ((c = getopt(argc, argv, "pdLSs:u:bPB")) != EOF) {
262                 switch (c) {
263                 case 'b':
264                         brief = 1;
265                         break;
266                 case 'B':
267                         show_brl = 1;
268                         break;
269                 case 'd':
270                         verbose = 1;
271                         break;
272                 case 'L':
273                         locks_only = 1;
274                         break;
275                 case 'p':
276                         processes_only = 1;
277                         break;
278                 case 'P':
279                         profile_only = 1;
280                         break;
281                 case 'S':
282                         shares_only = 1;
283                         break;
284                 case 's':
285                         pstrcpy(servicesf, optarg);
286                         break;
287                 case 'u':                                      
288                         Ucrit_addUsername(optarg);             
289                         break;
290                 default:
291                         fprintf(stderr, "Usage: %s [-P] [-d] [-L] [-p] [-S] [-s configfile] [-u username]\n", *argv);
292                         return (-1);
293                 }
294         }
295         
296         if (!lp_load(servicesf,False,False,False)) {
297                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
298                 return (-1);
299         }
300         
301         if (verbose) {
302                 printf("using configfile = %s\n", servicesf);
303         }
304         
305         if (profile_only) {
306                 return profile_dump();
307         }
308         
309         tdb = tdb_open(lock_path("connections.tdb"), 0, 0, O_RDONLY, 0);
310         if (!tdb) {
311                 printf("connections.tdb not initialised\n");
312                 if (!lp_status(-1))
313                         printf("You need to have status=yes in your smb config file\n");
314                 return(0);
315         }  else if (verbose) {
316                 printf("Opened status file %s\n", fname);
317         }
318
319         if (locks_only) goto locks;
320
321         printf("\nSamba version %s\n",VERSION);
322         if (brief) {
323                 printf("PID     Username  Machine                       Time logged in\n");
324                 printf("-------------------------------------------------------------------\n");
325         } else {
326                 printf("Service      uid      gid      pid     machine\n");
327                 printf("----------------------------------------------\n");
328         }
329         tdb_traverse(tdb, traverse_fn1, NULL);
330
331  locks:
332         if (processes_only) exit(0);
333   
334         if (brief)  {
335                 ptr=srecs;
336                 while (ptr!=NULL) {
337                         printf("%-8d%-10.10s%-30.30s%s",
338                                (int)ptr->pid,uidtoname(ptr->uid),
339                                ptr->machine,
340                                asctime(LocalTime(&(ptr->start))));
341                         ptr=ptr->next;
342                 }
343                 printf("\n");
344                 exit(0);
345         }
346
347         printf("\n");
348
349         if (!shares_only) {
350                 int ret;
351
352                 if (!locking_init(1)) {
353                         printf("Can't initialise locking module - exiting\n");
354                         exit(1);
355                 }
356                 
357                 ret = share_mode_forall(print_share_mode);
358
359                 if (ret == 0) {
360                         printf("No locked files\n");
361                 } else if (ret == -1) {
362                         printf("locked file list truncated\n");
363                 }
364                 
365                 printf("\n");
366
367                 if (show_brl) {
368                         brl_forall(print_brl);
369                 }
370                 
371                 locking_end();
372         }
373
374         return (0);
375 }
376