converted all our existing shared memory code to use a tdb database
[tprouty/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
59 /* we need these because we link to locking*.o */
60  void become_root(BOOL save_dir) {}
61  void unbecome_root(BOOL restore_dir) {}
62
63
64 /* added by OH */
65 static void Ucrit_addUsername(char *username)
66 {
67         pstrcpy(Ucrit_username, username);
68         if(strlen(Ucrit_username) > 0)
69                 Ucrit_IsActive = 1;
70 }
71
72 static unsigned int Ucrit_checkUsername(char *username)
73 {
74         if ( !Ucrit_IsActive) return 1;
75         if (strcmp(Ucrit_username,username) ==0) return 1;
76         return 0;
77 }
78
79 static void Ucrit_addPid(pid_t pid)
80 {
81         int i;
82         if ( !Ucrit_IsActive) return;
83         for (i=0;i<Ucrit_MaxPid;i++)
84                 if( pid == Ucrit_pid[i] ) return;
85         Ucrit_pid[Ucrit_MaxPid++] = pid;
86 }
87
88 static unsigned int Ucrit_checkPid(pid_t pid)
89 {
90         int i;
91         if ( !Ucrit_IsActive) return 1;
92         for (i=0;i<Ucrit_MaxPid;i++)
93                 if( pid == Ucrit_pid[i] ) return 1;
94         return 0;
95 }
96
97
98 static void print_share_mode(share_mode_entry *e, char *fname)
99 {
100         static int count;
101         if (count==0) {
102                 printf("Locked files:\n");
103                 printf("Pid    DenyMode   R/W        Oplock           Name\n");
104                 printf("--------------------------------------------------\n");
105         }
106         count++;
107
108         if (Ucrit_checkPid(e->pid)) {
109           printf("%-5d  ",(int)e->pid);
110           switch ((e->share_mode>>4)&0xF) {
111           case DENY_NONE: printf("DENY_NONE  "); break;
112           case DENY_ALL:  printf("DENY_ALL   "); break;
113           case DENY_DOS:  printf("DENY_DOS   "); break;
114           case DENY_READ: printf("DENY_READ  "); break;
115           case DENY_WRITE:printf("DENY_WRITE "); break;
116           }
117           switch (e->share_mode&0xF) {
118           case 0: printf("RDONLY     "); break;
119           case 1: printf("WRONLY     "); break;
120           case 2: printf("RDWR       "); break;
121           }
122
123           if((e->op_type & 
124              (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
125               (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
126                 printf("EXCLUSIVE+BATCH ");
127           else if (e->op_type & EXCLUSIVE_OPLOCK)
128                 printf("EXCLUSIVE       ");
129           else if (e->op_type & BATCH_OPLOCK)
130                 printf("BATCH           ");
131           else if (e->op_type & LEVEL_II_OPLOCK)
132                 printf("LEVEL_II        ");
133           else
134                 printf("NONE            ");
135
136           printf(" %s   %s",dos_to_unix(fname,False),
137              asctime(LocalTime((time_t *)&e->time.tv_sec)));
138         }
139 }
140
141
142 /*******************************************************************
143  dump the elements of the profile structure
144   ******************************************************************/
145 static int profile_dump(void)
146 {
147 #ifndef WITH_PROFILE
148         fprintf(stderr,"ERROR: not compiled with profile support\n");
149         return -1;
150 #else
151         if (!profile_setup(True)) {
152                 fprintf(stderr,"Failed to initialise profile memory\n");
153                 return -1;
154         }
155
156         printf("smb_count:\t%u\n", profile_p->smb_count);
157         printf("uid_changes:\t%u\n", profile_p->uid_changes);
158         return 0;
159 #endif
160 }
161
162
163 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
164 {
165         static pid_t last_pid;
166         struct session_record *ptr;
167         struct connections_data crec;
168
169         memcpy(&crec, dbuf.dptr, sizeof(crec));
170
171         if (crec.cnum == -1) return 0;
172
173         if (!process_exists(crec.pid) || !Ucrit_checkUsername(uidtoname(crec.uid))) {
174                 return 0;
175         }
176
177         if (brief) {
178                 ptr=srecs;
179                 while (ptr!=NULL) {
180                         if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) {
181                                 if (ptr->start > crec.start)
182                                         ptr->start=crec.start;
183                                 break;
184                         }
185                         ptr=ptr->next;
186                 }
187                 if (ptr==NULL) {
188                         ptr=(struct session_record *) malloc(sizeof(struct session_record));
189                         ptr->uid=crec.uid;
190                         ptr->pid=crec.pid;
191                         ptr->start=crec.start;
192                         strncpy(ptr->machine,crec.machine,30);
193                         ptr->machine[30]='\0';
194                         ptr->next=srecs;
195                         srecs=ptr;
196                 }
197         } else {
198                 Ucrit_addPid(crec.pid);  
199                 if (processes_only) {
200                         if (last_pid != crec.pid)
201                                 printf("%d\n",(int)crec.pid);
202                         last_pid = crec.pid; /* XXXX we can still get repeats, have to
203                                                 add a sort at some time */
204                 } else {
205                         printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
206                                crec.name,uidtoname(crec.uid),gidtoname(crec.gid),(int)crec.pid,
207                                crec.machine,crec.addr,
208                                asctime(LocalTime(&crec.start)));
209                 }
210         }
211
212         return 0;
213 }
214
215
216
217
218  int main(int argc, char *argv[])
219 {
220         pstring fname;
221         int c;
222         static pstring servicesf = CONFIGFILE;
223         extern char *optarg;
224         int profile_only = 0;
225         TDB_CONTEXT *tdb;
226         struct session_record *ptr;
227
228         TimeInit();
229         setup_logging(argv[0],True);
230         
231         charset_initialise();
232         
233         DEBUGLEVEL = 0;
234         dbf = stderr;
235         
236         if (getuid() != geteuid()) {
237                 printf("smbstatus should not be run setuid\n");
238                 return(1);
239         }
240         
241         while ((c = getopt(argc, argv, "pdLSs:u:bP")) != EOF) {
242                 switch (c) {
243                 case 'b':
244                         brief = 1;
245                         break;
246                 case 'd':
247                         verbose = 1;
248                         break;
249                 case 'L':
250                         locks_only = 1;
251                         break;
252                 case 'p':
253                         processes_only = 1;
254                         break;
255                 case 'P':
256                         profile_only = 1;
257                         break;
258                 case 'S':
259                         shares_only = 1;
260                         break;
261                 case 's':
262                         pstrcpy(servicesf, optarg);
263                         break;
264                 case 'u':                                       /* added by OH */
265                         Ucrit_addUsername(optarg);                    /* added by OH */
266                         break;
267                 default:
268                         fprintf(stderr, "Usage: %s [-P] [-d] [-L] [-p] [-S] [-s configfile] [-u username]\n", *argv); /* changed by OH */
269                         return (-1);
270                 }
271         }
272         
273         if (!lp_load(servicesf,False,False,False)) {
274                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
275                 return (-1);
276         }
277         
278         if (verbose) {
279                 printf("using configfile = %s\n", servicesf);
280         }
281         
282         if (profile_only) {
283                 return profile_dump();
284         }
285         
286         tdb = tdb_open(lock_path("connections.tdb"), 0, O_RDONLY, 0);
287         if (!tdb) {
288                 printf("Couldn't open status file %s\n",fname);
289                 if (!lp_status(-1))
290                         printf("You need to have status=yes in your smb config file\n");
291                 return(0);
292         }  else if (verbose) {
293                 printf("Opened status file %s\n", fname);
294         }
295
296         if (locks_only) goto locks;
297
298         printf("\nSamba version %s\n",VERSION);
299         if (brief) {
300                 printf("PID     Username  Machine                       Time logged in\n");
301                 printf("-------------------------------------------------------------------\n");
302         } else {
303                 printf("Service      uid      gid      pid     machine\n");
304                 printf("----------------------------------------------\n");
305         }
306         tdb_traverse(tdb, traverse_fn1);
307
308  locks:
309         if (processes_only) exit(0);
310   
311         if (brief)  {
312                 ptr=srecs;
313                 while (ptr!=NULL) {
314                         printf("%-8d%-10.10s%-30.30s%s",
315                                (int)ptr->pid,uidtoname(ptr->uid),
316                                ptr->machine,
317                                asctime(LocalTime(&(ptr->start))));
318                         ptr=ptr->next;
319                 }
320                 printf("\n");
321                 exit(0);
322         }
323
324         printf("\n");
325
326         if (!shares_only) {
327                 if (!locking_init(1)) {
328                         printf("Can't initialise shared memory - exiting\n");
329                         exit(1);
330                 }
331                 
332                 if (share_mode_forall(print_share_mode) <= 0)
333                         printf("No locked files\n");
334                 
335                 printf("\n");
336                 
337                 locking_end();
338         }
339
340         return (0);
341 }
342