]> git.samba.org - samba.git/blob - source/utils/status.c
The big character set handling changeover!
[samba.git] / source / 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",fname,
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         if (dbuf.dsize != sizeof(crec))
190                 return 0;
191
192         memcpy(&crec, dbuf.dptr, sizeof(crec));
193
194         if (crec.cnum == -1)
195                 return 0;
196
197         if (!process_exists(crec.pid) || !Ucrit_checkUsername(uidtoname(crec.uid))) {
198                 return 0;
199         }
200
201         if (brief) {
202                 ptr=srecs;
203                 while (ptr!=NULL) {
204                         if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) {
205                                 if (ptr->start > crec.start)
206                                         ptr->start=crec.start;
207                                 break;
208                         }
209                         ptr=ptr->next;
210                 }
211                 if (ptr==NULL) {
212                         ptr=(struct session_record *) malloc(sizeof(struct session_record));
213                         ptr->uid=crec.uid;
214                         ptr->pid=crec.pid;
215                         ptr->start=crec.start;
216                         strncpy(ptr->machine,crec.machine,30);
217                         ptr->machine[30]='\0';
218                         ptr->next=srecs;
219                         srecs=ptr;
220                 }
221         } else {
222                 Ucrit_addPid(crec.pid);  
223                 if (processes_only) {
224                         if (last_pid != crec.pid)
225                                 printf("%d\n",(int)crec.pid);
226                         last_pid = crec.pid; /* XXXX we can still get repeats, have to
227                                                 add a sort at some time */
228                 } else {
229                         printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
230                                crec.name,uidtoname(crec.uid),gidtoname(crec.gid),(int)crec.pid,
231                                crec.machine,crec.addr,
232                                asctime(LocalTime(&crec.start)));
233                 }
234         }
235
236         return 0;
237 }
238
239
240
241
242  int main(int argc, char *argv[])
243 {
244         pstring fname;
245         int c;
246         static pstring servicesf = CONFIGFILE;
247         extern char *optarg;
248         int profile_only = 0, new_debuglevel = -1;
249         TDB_CONTEXT *tdb;
250         struct session_record *ptr;
251
252         TimeInit();
253         setup_logging(argv[0],True);
254         
255         DEBUGLEVEL = 0;
256         dbf = stderr;
257         
258         if (getuid() != geteuid()) {
259                 printf("smbstatus should not be run setuid\n");
260                 return(1);
261         }
262         
263         while ((c = getopt(argc, argv, "pvLSs:u:bPBd:")) != EOF) {
264                 switch (c) {
265                 case 'b':
266                         brief = 1;
267                         break;
268                 case 'B':
269                         show_brl = 1;
270                         break;
271                 case 'd':
272                         new_debuglevel = atoi(optarg);
273                         break;
274                         
275                 case 'v':
276                         verbose = 1;
277                         break;
278                 case 'L':
279                         locks_only = 1;
280                         break;
281                 case 'p':
282                         processes_only = 1;
283                         break;
284                 case 'P':
285                         profile_only = 1;
286                         break;
287                 case 'S':
288                         shares_only = 1;
289                         break;
290                 case 's':
291                         pstrcpy(servicesf, optarg);
292                         break;
293                 case 'u':                                      
294                         Ucrit_addUsername(optarg);             
295                         break;
296                 default:
297                         fprintf(stderr, "Usage: %s [-P] [-v] [-L] [-p] [-S] [-s configfile] [-u username] [-d debuglevel]\n", *argv);
298                         return (-1);
299                 }
300         }
301         
302         if (!lp_load(servicesf,False,False,False)) {
303                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
304                 return (-1);
305         }
306         
307         if (new_debuglevel != -1) {
308                 DEBUGLEVEL = new_debuglevel;
309         }
310
311         if (verbose) {
312                 printf("using configfile = %s\n", servicesf);
313         }
314         
315         if (profile_only) {
316                 return profile_dump();
317         }
318         
319         tdb = tdb_open_log(lock_path("connections.tdb"), 0, 0, O_RDONLY, 0);
320         if (!tdb) {
321                 printf("connections.tdb not initialised\n");
322                 if (!lp_status(-1))
323                         printf("You need to have status=yes in your smb config file\n");
324                 return(0);
325         }  else if (verbose) {
326                 printf("Opened status file %s\n", fname);
327         }
328
329         if (locks_only) goto locks;
330
331         printf("\nSamba version %s\n",VERSION);
332         if (brief) {
333                 printf("PID     Username  Machine                       Time logged in\n");
334                 printf("-------------------------------------------------------------------\n");
335         } else {
336                 printf("Service      uid      gid      pid     machine\n");
337                 printf("----------------------------------------------\n");
338         }
339         tdb_traverse(tdb, traverse_fn1, NULL);
340         tdb_close(tdb);
341
342  locks:
343         if (processes_only) exit(0);
344   
345         if (brief)  {
346                 ptr=srecs;
347                 while (ptr!=NULL) {
348                         printf("%-8d%-10.10s%-30.30s%s",
349                                (int)ptr->pid,uidtoname(ptr->uid),
350                                ptr->machine,
351                                asctime(LocalTime(&(ptr->start))));
352                         ptr=ptr->next;
353                 }
354                 printf("\n");
355                 exit(0);
356         }
357
358         printf("\n");
359
360         if (!shares_only) {
361                 int ret;
362
363                 if (!locking_init(1)) {
364                         printf("Can't initialise locking module - exiting\n");
365                         exit(1);
366                 }
367                 
368                 ret = share_mode_forall(print_share_mode);
369
370                 if (ret == 0) {
371                         printf("No locked files\n");
372                 } else if (ret == -1) {
373                         printf("locked file list truncated\n");
374                 }
375                 
376                 printf("\n");
377
378                 if (show_brl) {
379                         brl_forall(print_brl);
380                 }
381                 
382                 locking_end();
383         }
384
385         return (0);
386 }
387